Faster raytracing

This commit is contained in:
luboslenco
2020-01-27 19:33:56 +01:00
parent 7de977d4c2
commit 8f10468cc3
12 changed files with 45 additions and 47 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+4 -3
View File
@@ -3,9 +3,10 @@
#include "std/math.hlsl"
struct Vertex {
float3 position;
float3 normal;
float2 tex;
uint posxy;
uint poszw;
uint nor;
uint tex;
};
struct RayGenConstantBuffer {
+4 -3
View File
@@ -3,9 +3,10 @@
#include "std/math.hlsl"
struct Vertex {
float3 position;
float3 normal;
float2 tex;
uint posxy;
uint poszw;
uint nor;
uint tex;
};
struct RayGenConstantBuffer {
+10 -9
View File
@@ -4,9 +4,10 @@
#include "std/attrib.hlsl"
struct Vertex {
float3 position;
float3 normal;
float2 tex;
uint posxy;
uint poszw;
uint nor;
uint tex;
};
struct RayGenConstantBuffer {
@@ -91,16 +92,16 @@ void closesthit(inout RayPayload payload, in BuiltInTriangleIntersectionAttribut
uint3 indices_sample = indices.Load3(base_index);
float3 vertex_normals[3] = {
float3(vertices[indices_sample[0]].normal),
float3(vertices[indices_sample[1]].normal),
float3(vertices[indices_sample[2]].normal)
float3(S16toF32(vertices[indices_sample[0]].nor), S16toF32(vertices[indices_sample[0]].poszw).y),
float3(S16toF32(vertices[indices_sample[1]].nor), S16toF32(vertices[indices_sample[1]].poszw).y),
float3(S16toF32(vertices[indices_sample[2]].nor), S16toF32(vertices[indices_sample[2]].poszw).y)
};
float3 n = normalize(hit_attribute(vertex_normals, attr));
float2 vertex_uvs[3] = {
float2(vertices[indices_sample[0]].tex),
float2(vertices[indices_sample[1]].tex),
float2(vertices[indices_sample[2]].tex)
S16toF32(vertices[indices_sample[0]].tex),
S16toF32(vertices[indices_sample[1]].tex),
S16toF32(vertices[indices_sample[2]].tex)
};
float2 tex_coord = hit_attribute2d(vertex_uvs, attr);
@@ -3,9 +3,10 @@
#include "std/math.hlsl"
struct Vertex {
float3 position;
float3 normal;
float2 tex;
uint posxy;
uint poszw;
uint nor;
uint tex;
};
struct RayGenConstantBuffer {
+10 -9
View File
@@ -4,9 +4,10 @@
#include "std/math.hlsl"
struct Vertex {
float3 position;
float3 normal;
float2 tex;
uint posxy;
uint poszw;
uint nor;
uint tex;
};
struct RayGenConstantBuffer {
@@ -122,9 +123,9 @@ void closesthit(inout RayPayload payload, in BuiltInTriangleIntersectionAttribut
uint3 indices_sample = indices.Load3(base_index);
float2 vertex_uvs[3] = {
float2(vertices[indices_sample[0]].tex),
float2(vertices[indices_sample[1]].tex),
float2(vertices[indices_sample[2]].tex)
S16toF32(vertices[indices_sample[0]].tex),
S16toF32(vertices[indices_sample[1]].tex),
S16toF32(vertices[indices_sample[2]].tex)
};
float2 tex_coord = hit_attribute2d(vertex_uvs, attr);
@@ -140,9 +141,9 @@ void closesthit(inout RayPayload payload, in BuiltInTriangleIntersectionAttribut
}
float3 vertex_normals[3] = {
float3(vertices[indices_sample[0]].normal),
float3(vertices[indices_sample[1]].normal),
float3(vertices[indices_sample[2]].normal)
float3(S16toF32(vertices[indices_sample[0]].nor), S16toF32(vertices[indices_sample[0]].poszw).y),
float3(S16toF32(vertices[indices_sample[1]].nor), S16toF32(vertices[indices_sample[1]].poszw).y),
float3(S16toF32(vertices[indices_sample[2]].nor), S16toF32(vertices[indices_sample[2]].poszw).y)
};
float3 n = normalize(hit_attribute(vertex_normals, attr));
+6
View File
@@ -2,6 +2,12 @@
#ifndef _ATTRIB_HLSL_
#define _ATTRIB_HLSL_
float2 S16toF32(uint val) {
int a = (int)(val << 16) >> 16;
int b = (int)(val & 0xffff0000) >> 16;
return float2(a, b) / 32767.0f;
}
float3 hit_world_position() {
return WorldRayOrigin() + RayTCurrent() * WorldRayDirection();
}
+7 -20
View File
@@ -17,8 +17,9 @@ class RenderPathRaytrace {
static var first = true;
static var f32 = new kha.arrays.Float32Array(24);
static var helpMat = iron.math.Mat4.identity();
static var vb: kha.arrays.Float32Array;
static var ib: kha.arrays.Uint32Array;
static var vb_scale = 1.0;
static var vb: kha.graphics4.VertexBuffer;
static var ib: kha.graphics4.IndexBuffer;
static var raysTimer = 0.0;
static var raysCounter = 0;
static var lastLayer: kha.Image = null;
@@ -218,32 +219,18 @@ class RenderPathRaytrace {
var bnoise_sobol = Scene.active.embedded.get("bnoise_sobol.k");
var bnoise_scramble = Scene.active.embedded.get("bnoise_scramble.k");
var bnoise_rank = Scene.active.embedded.get("bnoise_rank.k");
Krom.raytraceInit(shader.bytes.getData(), untyped vb.buffer, untyped ib.buffer);
Krom.raytraceInit(shader.bytes.getData(), untyped vb.buffer, untyped ib.buffer, vb_scale);
});
}
static function buildData() {
if (Context.mergedObject == null) arm.util.MeshUtil.mergeMesh();
var mo = Context.mergedObject;
var sc = mo.parent.transform.scale.x * mo.data.scalePos;
var md = mo.data;
var geom = md.geom;
var count = Std.int(geom.positions.length / 4);
vb = new kha.arrays.Float32Array(count * 8);
for (i in 0...count) {
vb[i * 8 ] = (geom.positions[i * 4 ] * sc / 32767);
vb[i * 8 + 1] = (geom.positions[i * 4 + 1] * sc / 32767);
vb[i * 8 + 2] = (geom.positions[i * 4 + 2] * sc / 32767);
vb[i * 8 + 3] = geom.normals [i * 2 ] / 32767;
vb[i * 8 + 4] = geom.normals [i * 2 + 1] / 32767;
vb[i * 8 + 5] = geom.positions[i * 4 + 3] / 32767;
vb[i * 8 + 6] = (geom.uvs[i * 2 ] / 32767);
vb[i * 8 + 7] = (geom.uvs[i * 2 + 1] / 32767);
}
var indices = geom.indices[0];
ib = new kha.arrays.Uint32Array(indices.length);
for (i in 0...indices.length) ib[i] = indices[i];
vb_scale = mo.parent.transform.scale.x * md.scalePos;
vb = geom.vertexBuffer;
ib = geom.indexBuffers[0];
}
static function getBakeShaderName(): String {