Skip to content

Commit 8840392

Browse files
refactor normal storage
1 parent fc14152 commit 8840392

File tree

5 files changed

+20
-106
lines changed

5 files changed

+20
-106
lines changed

examples_tests/22.RaytracedAO/common.glsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
};
2222
#define mat4 nbl::core::matrix4SIMD
2323
#define mat4x3 nbl::core::matrix3x4SIMD
24+
#else
25+
#include <nbl/builtin/glsl/ext/MitsubaLoader/instance_data_descriptor.glsl>
2426
#endif
2527

2628
#endif

examples_tests/22.RaytracedAO/fillVisBuffer.frag

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
#include <nbl/builtin/glsl/utils/normal_encode.glsl>
88

99

10-
layout(location = 0) flat in uint ObjectID;
10+
layout(location = 0) flat in uint BackfacingBit_ObjectID;
1111
layout(location = 1) in vec3 Normal;
1212
layout(location = 2) in vec2 UV;
1313

14-
layout(location = 0) out uvec2 frontFacing_Object_Triangle;
14+
layout(location = 0) out uvec2 frontFacing_Object_Triangle; // should it be called backfacing or frontfacing?
1515
layout(location = 1) out vec2 encodedNormal;
1616
layout(location = 2) out vec2 uv;
1717

1818
void main()
1919
{
20-
frontFacing_Object_Triangle = uvec2(ObjectID^(gl_FrontFacing ? 0x0u:0x80000000u),gl_PrimitiveID);
21-
// these will disappear once we finally have MeshPackerV2 and settle on a way to obtain barycentrics
20+
frontFacing_Object_Triangle = uvec2(BackfacingBit_ObjectID^(gl_FrontFacing ? 0x0u:0x80000000u),gl_PrimitiveID);
21+
// TODO: these will disappear once we finally have MeshPackerV2 and settle on a way to obtain barycentrics
2222
encodedNormal = nbl_glsl_NormalEncode_signedSpherical(normalize(Normal));
2323
uv = UV;
2424
}
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
#version 430 core
22

33
#include "drawCommon.glsl"
4-
layout(set=1, binding=0, std430, row_major) restrict readonly buffer PerInstanceStatic
5-
{
6-
ObjectStaticData_t staticData[];
7-
};
8-
layout(set=1, binding=1, row_major) readonly restrict buffer PerInstancePerCamera
4+
layout(set=1, binding=0, row_major) readonly restrict buffer PerInstancePerCamera
95
{
106
DrawData_t data[];
117
} instanceDataPerCamera;
@@ -14,7 +10,7 @@ layout(location = 0) in vec3 vPosition;
1410
layout(location = 2) in vec2 vUV;
1511
layout(location = 3) in vec3 vNormal;
1612

17-
layout(location = 0) flat out uint ObjectID;
13+
layout(location = 0) flat out uint BackfacingBit_ObjectID;
1814
layout(location = 1) out vec3 Normal;
1915
layout(location = 2) out vec2 UV;
2016

@@ -23,14 +19,11 @@ layout(location = 2) out vec2 UV;
2319
void main()
2420
{
2521
DrawData_t self = instanceDataPerCamera.data[gl_InstanceIndex];
26-
ObjectID = self.objectID|(floatBitsToUint(self.detMVP)&0x80000000u); // use MSB to denote if face orientation should be flipped
22+
BackfacingBit_ObjectID = self.backfacingBit_objectID;
2723

2824
gl_Position = nbl_glsl_pseudoMul4x4with3x1(self.MVP,vPosition);
2925

30-
const vec3 localNormal = normalize(vNormal); //have to normalize twice because of normal quantization
31-
Normal[0] = dot(staticData[self.objectID].normalMatrixRow0,localNormal);
32-
Normal[1] = dot(staticData[self.objectID].normalMatrixRow1,localNormal);
33-
Normal[2] = dot(staticData[self.objectID].normalMatrixRow2,localNormal);
26+
Normal = normalize(vNormal);
3427

3528
UV = vUV;
3629
}

examples_tests/22.RaytracedAO/raygen.comp

Lines changed: 8 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,18 @@ layout(set = 2, binding = 1) uniform usampler2D scramblebuf;
1111
// vis buffer
1212
layout(set = 2, binding = 2) uniform sampler2D depthbuf;
1313
layout(set = 2, binding = 3) uniform usampler2D frontFacing_Object_Triangle;
14-
//layout(set = 2, binding = 3) uniform sampler2D barycentricDerivatives;
1514
layout(set = 2, binding = 4) uniform sampler2D encodedNormal;
1615
layout(set = 2, binding = 5) uniform sampler2D uvCoords;
16+
//layout(set = 2, binding = 6) uniform sampler2D barycentricDerivatives; // in the future we shall compute them from triangle vertex positions
1717

1818

1919
#include "bin/material_declarations.glsl"
20-
21-
/*
22-
// something to get our GLSL intellisense to "shut up"
23-
#ifndef _NBL_EXT_MITSUBA_LOADER_VT_STORAGE_VIEW_COUNT
24-
#define _NBL_EXT_MITSUBA_LOADER_VT_STORAGE_VIEW_COUNT 4
25-
#include <nbl/builtin/glsl/virtual_texturing/impl_functions.glsl>
26-
#endif
27-
*/
28-
#include <nbl/builtin/glsl/ext/MitsubaLoader/material_compiler_compatibility_impl.glsl>
29-
3020
vec3 normalizedV;
3121
vec3 nbl_glsl_MC_getNormalizedWorldSpaceV()
3222
{
3323
return normalizedV;
3424
}
35-
vec3 normalizedN;
25+
vec3 normalizedN; // TODO
3626
vec3 nbl_glsl_MC_getNormalizedWorldSpaceN()
3727
{
3828
return normalizedN;
@@ -128,71 +118,6 @@ bool gen_sample_ray(out float maxT, out vec3 direction, out vec3 throughput, in
128118
return true;
129119
}
130120

131-
#ifdef TODO
132-
// TODO: define lower and upper bound over `lightCDF`
133-
vec3 light_sample(out vec3 incoming, in uint sampleIx, in uint scramble, inout float maxT, inout bool alive, in vec3 position)
134-
{
135-
uint lightIDSample = ugen_uniform_sample1(0u,sampleIx,scramble);
136-
vec2 lightSurfaceSample = gen_uniform_sample2(2u,sampleIx,scramble);
137-
138-
uint lightID = upper_bound(lightIDSample,uint(lightCDF.length()-1));
139-
140-
SLight light = light[lightID];
141-
142-
#define SHADOW_RAY_LEN 0.93
143-
float factor; // 1.0/light_probability already baked into the light factor
144-
switch (SLight_extractType(light))
145-
{
146-
case SLight_ET_ELLIPSOID:
147-
lightSurfaceSample.x = lightSurfaceSample.x*2.0-1.0;
148-
{
149-
mat4x3 tform = light.transform;
150-
float equator = lightSurfaceSample.y*2.0*kPI;
151-
vec3 pointOnSurface = vec3(vec2(cos(equator),sin(equator))*sqrt(1.0-lightSurfaceSample.x*lightSurfaceSample.x),lightSurfaceSample.x);
152-
153-
incoming = mat3(tform)*pointOnSurface+(tform[3]-position);
154-
float incomingInvLen = inversesqrt(dot(incoming,incoming));
155-
incoming *= incomingInvLen;
156-
157-
maxT = SHADOW_RAY_LEN/incomingInvLen;
158-
159-
factor = 4.0*kPI; // compensate for the domain of integration
160-
// don't normalize, length of the normal times determinant is very handy for differential area after a 3x3 matrix transform
161-
vec3 negLightNormal = light.transformCofactors*pointOnSurface;
162-
163-
factor *= max(dot(negLightNormal,incoming),0.0)*incomingInvLen*incomingInvLen;
164-
}
165-
break;
166-
default: // SLight_ET_TRIANGLE:
167-
{
168-
vec3 pointOnSurface = transpose(light.transformCofactors)[0];
169-
vec3 shortEdge = transpose(light.transformCofactors)[1];
170-
vec3 longEdge = transpose(light.transformCofactors)[2];
171-
172-
lightSurfaceSample.x = sqrt(lightSurfaceSample.x);
173-
174-
pointOnSurface += (shortEdge*(1.0-lightSurfaceSample.y)+longEdge*lightSurfaceSample.y)*lightSurfaceSample.x;
175-
176-
vec3 negLightNormal = cross(shortEdge,longEdge);
177-
178-
incoming = pointOnSurface-position;
179-
float incomingInvLen = inversesqrt(dot(incoming,incoming));
180-
incoming *= incomingInvLen;
181-
182-
maxT = SHADOW_RAY_LEN/incomingInvLen;
183-
184-
factor = 0.5*max(dot(negLightNormal,incoming),0.0)*incomingInvLen*incomingInvLen;
185-
}
186-
break;
187-
}
188-
189-
if (factor<FLT_MIN)
190-
alive = false;
191-
192-
return light.factor*factor;
193-
}
194-
#endif
195-
196121
void main()
197122
{
198123
uvec2 outputLocation = gl_GlobalInvocationID.xy;
@@ -235,7 +160,7 @@ void main()
235160
const vec2 NDC = vec2(outputLocation)*staticViewData.rcpPixelSize+staticViewData.rcpHalfPixelSize;
236161

237162
const vec3 V = mix(frustumCornersToCamera[1]*NDC.x+frustumCornersToCamera[0],frustumCornersToCamera[3]*NDC.x+frustumCornersToCamera[2],NDC.yyy); // could maybe compute this more precisely
238-
worldPosition = pc.cummon.normalMatrixAndCameraPos[3]-V*linearizeZBufferVal(revdepth);
163+
worldPosition = pc.cummon.cameraPosition-V*linearizeZBufferVal(revdepth);
239164
normalizedV = normalize(V);
240165
}
241166

@@ -252,12 +177,15 @@ void main()
252177
material = nbl_glsl_MC_material_data_t_getOriented(InstData.data[objectID].material,precomputed.frontface);
253178

254179
//
255-
emissive = vec3(float(objectID),float(triangleID),0.0);//nbl_glsl_MC_oriented_material_t_getEmissive(material);
180+
emissive = nbl_glsl_MC_oriented_material_t_getEmissive(material);
256181
}
257182

258183
// normally we'd use MeshPackerV2's vertex attribute data for this, but now we read from temporary GBuffer
259184
{
260-
normalizedN = nbl_glsl_NormalDecode_signedSpherical(normalBuffer);
185+
const vec3 normal = nbl_glsl_NormalDecode_signedSpherical(normalBuffer);
186+
normalizedN.x = dot(InstData.data[objectID].normalMatrixRow0,normal);
187+
normalizedN.y = dot(InstData.data[objectID].normalMatrixRow1,normal);
188+
normalizedN.z = dot(InstData.data[objectID].normalMatrixRow2,normal);
261189
}
262190

263191
//

examples_tests/22.RaytracedAO/raytraceCommon.glsl

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ struct StaticViewData_t
9595
struct RaytraceShaderCommonData_t
9696
{
9797
mat4x3 frustumCornersToCamera;
98-
mat4x3 normalMatrixAndCameraPos;
98+
vec3 cameraPosition;
99+
uint padding;
99100
float depthLinearizationConstant;
100101
uint samplesComputedPerPixel;
101102
uint framesDispatched;
@@ -118,14 +119,6 @@ layout(set = 1, binding = 2, std430) restrict /*writeonly/readonly TODO dependin
118119
{
119120
nbl_glsl_ext_RadeonRays_ray rays[];
120121
};
121-
// materials
122-
/*
123-
#include <nbl/builtin/glsl/material_compiler/common_invariant_declarations.glsl>
124-
layout(set = 1, binding = 3, std430) restrict readonly buffer Materials
125-
{
126-
nbl_glsl_MC_MaterialData materials[];
127-
};
128-
*/
129122
// lights
130123
layout(set = 1, binding = 3, std430) restrict readonly buffer CumulativeLightPDF
131124
{
@@ -156,8 +149,6 @@ void storeAccumulation(in vec3 color, in ivec2 coord)
156149
imageStore(accumulation,coord,uvec4(data,0u,0u));
157150
}
158151

159-
//#include <nbl/builtin/glsl/ext/MitsubaLoader/material_compiler_compatibility.glsl/>
160-
161152
#endif
162153

163154
#endif

0 commit comments

Comments
 (0)