diff --git a/Shaders/raytrace/raytrace_bake_ao.cso b/Shaders/raytrace/raytrace_bake_ao.cso index 89859ae8..cf59d34d 100644 Binary files a/Shaders/raytrace/raytrace_bake_ao.cso and b/Shaders/raytrace/raytrace_bake_ao.cso differ diff --git a/Shaders/raytrace/raytrace_bake_bent.cso b/Shaders/raytrace/raytrace_bake_bent.cso index 3e92e65c..77a239ca 100644 Binary files a/Shaders/raytrace/raytrace_bake_bent.cso and b/Shaders/raytrace/raytrace_bake_bent.cso differ diff --git a/Shaders/raytrace/raytrace_bake_light.cso b/Shaders/raytrace/raytrace_bake_light.cso index 5febbfa8..1a1c24e6 100644 Binary files a/Shaders/raytrace/raytrace_bake_light.cso and b/Shaders/raytrace/raytrace_bake_light.cso differ diff --git a/Shaders/raytrace/raytrace_bake_thick.cso b/Shaders/raytrace/raytrace_bake_thick.cso index 4506f3b9..66e0beee 100644 Binary files a/Shaders/raytrace/raytrace_bake_thick.cso and b/Shaders/raytrace/raytrace_bake_thick.cso differ diff --git a/Shaders/raytrace/raytrace_brute.cso b/Shaders/raytrace/raytrace_brute.cso index a0f86a57..2fad2407 100644 Binary files a/Shaders/raytrace/raytrace_brute.cso and b/Shaders/raytrace/raytrace_brute.cso differ diff --git a/Shaders/raytrace/src/raytrace_bake_ao.hlsl b/Shaders/raytrace/src/raytrace_bake_ao.hlsl index 51b68df0..e6f6dd41 100644 --- a/Shaders/raytrace/src/raytrace_bake_ao.hlsl +++ b/Shaders/raytrace/src/raytrace_bake_ao.hlsl @@ -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 { diff --git a/Shaders/raytrace/src/raytrace_bake_bent.hlsl b/Shaders/raytrace/src/raytrace_bake_bent.hlsl index 991f9a63..64cb6ef6 100644 --- a/Shaders/raytrace/src/raytrace_bake_bent.hlsl +++ b/Shaders/raytrace/src/raytrace_bake_bent.hlsl @@ -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 { diff --git a/Shaders/raytrace/src/raytrace_bake_light.hlsl b/Shaders/raytrace/src/raytrace_bake_light.hlsl index 51899f89..604ceca8 100644 --- a/Shaders/raytrace/src/raytrace_bake_light.hlsl +++ b/Shaders/raytrace/src/raytrace_bake_light.hlsl @@ -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); diff --git a/Shaders/raytrace/src/raytrace_bake_thick.hlsl b/Shaders/raytrace/src/raytrace_bake_thick.hlsl index 1596104d..8af10002 100644 --- a/Shaders/raytrace/src/raytrace_bake_thick.hlsl +++ b/Shaders/raytrace/src/raytrace_bake_thick.hlsl @@ -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 { diff --git a/Shaders/raytrace/src/raytrace_brute.hlsl b/Shaders/raytrace/src/raytrace_brute.hlsl index 9a069ad0..2e6d4ab7 100644 --- a/Shaders/raytrace/src/raytrace_brute.hlsl +++ b/Shaders/raytrace/src/raytrace_brute.hlsl @@ -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)); diff --git a/Shaders/raytrace/src/std/attrib.hlsl b/Shaders/raytrace/src/std/attrib.hlsl index f2f6081f..3fa5f682 100644 --- a/Shaders/raytrace/src/std/attrib.hlsl +++ b/Shaders/raytrace/src/std/attrib.hlsl @@ -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(); } diff --git a/Sources/arm/render/RenderPathRaytrace.hx b/Sources/arm/render/RenderPathRaytrace.hx index a365ee08..a76d0d34 100644 --- a/Sources/arm/render/RenderPathRaytrace.hx +++ b/Sources/arm/render/RenderPathRaytrace.hx @@ -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 {