1
1
#version 430 core
2
2
#include "raytraceCommon.glsl"
3
3
4
-
4
+ // for per pixel inputs
5
5
#include <irr/builtin/glsl/utils/normal_decode.glsl>
6
+ #include <irr/builtin/glsl/random/xoroshiro.glsl>
7
+
6
8
// rng
7
9
layout(set = 2, binding = 0) uniform usamplerBuffer sampleSequence;
8
10
layout(set = 2, binding = 1) uniform usampler2D scramblebuf;
9
11
// vis buffer
10
12
layout(set = 2, binding = 2) uniform sampler2D depthbuf;
11
- layout(set = 2, binding = 3) uniform usampler2D objectTriangleFrontFacing ;
13
+ layout(set = 2, binding = 3) uniform usampler2D frontFacing_Object_Triangle ;
12
14
layout(set = 2, binding = 4) uniform sampler2D encodedNormal;
13
- layout(set = 2, binding = 5) uniform sampler2D uv;
15
+ layout(set = 2, binding = 5) uniform sampler2D uvCoords;
16
+
17
+
18
+ // for materials
19
+ // dummies needed because of the functions for fragment shader surface intersections
20
+ vec3 dFdx(in vec3 r) {return vec3(0.0/0.0);}
21
+ vec3 dFdy(in vec3 r) {return vec3(0.0/0.0);}
22
+ // no idea yet
23
+ #define sizeof_bsdf_data 1u
24
+ #include <irr/builtin/material_compiler/glsl/common_declarations.glsl>
25
+ vec3 normalizedV;
26
+ vec3 irr_glsl_MC_getNormalizedWorldSpaceV()
27
+ {
28
+ return normalizedV;
29
+ }
30
+ vec3 normalizedN;
31
+ vec3 irr_glsl_MC_getNormalizedWorldSpaceN()
32
+ {
33
+ return normalizedN;
34
+ }
35
+ vec3 worldPosition;
36
+ vec3 irr_glsl_MC_WorldSpacePosition()
37
+ {
38
+ return worldPosition;
39
+ }
40
+ /*
41
+ #define _IRR_USER_PROVIDED_MATERIAL_COMPILER_GLSL_BACKEND_FUNCTIONS_
42
+ instr_stream_t getEvalStream(in MC_precomputed_t precomp)
43
+ {
44
+ instr_stream_t stream;
45
+ if (precomp.frontface)
46
+ {
47
+ stream.offset = InstData.data[InstanceIndex].front_instr_offset;
48
+ stream.count = InstData.data[InstanceIndex].front_rem_pdf_count;
49
+ }
50
+ else
51
+ {
52
+ stream.offset = InstData.data[InstanceIndex].back_instr_offset;
53
+ stream.count = InstData.data[InstanceIndex].back_rem_pdf_count;
54
+ }
55
+ return stream;
56
+ }
57
+ instr_t nbl_glsl_MC_fetchInstr(in uint ix)
58
+ {
59
+ return instr_buf.data[ix];
60
+ }
61
+ prefetch_instr_t nbl_glsl_MC_fetchPrefetchInstr(in uint ix)
62
+ {
63
+ return prefetch_instr_buf.data[ix];
64
+ }
65
+ bsdf_data_t nbl_glsl_MC_fetchBSDFData(in uint ix)
66
+ {
67
+ return bsdf_buf.data[ix];
68
+ }
69
+ ///
70
+ //rem'n'pdf and eval use the same instruction stream
71
+ instr_stream_t getRemAndPdfStream(in MC_precomputed_t precomp)
72
+ {
73
+ return getEvalStream(precomp);
74
+ }
75
+ instr_stream_t getGenChoiceStream(in MC_precomputed_t precomp)
76
+ {
77
+ instr_stream_t stream;
78
+ if (precomp.frontface)
79
+ {
80
+ stream.offset = InstData.data[InstanceIndex].front_instr_offset + InstData.data[InstanceIndex].front_rem_pdf_count;
81
+ stream.count = InstData.data[InstanceIndex].front_genchoice_count;
82
+ }
83
+ else
84
+ {
85
+ stream.offset = InstData.data[InstanceIndex].back_instr_offset + InstData.data[InstanceIndex].back_rem_pdf_count;
86
+ stream.count = InstData.data[InstanceIndex].back_genchoice_count;
87
+ }
88
+ return stream;
89
+ }
90
+ instr_stream_t getTexPrefetchStream(in MC_precomputed_t precomp)
91
+ {
92
+ instr_stream_t stream;
93
+ if (precomp.frontface)
94
+ {
95
+ stream.offset = InstData.data[InstanceIndex].front_prefetch_offset;
96
+ stream.count = InstData.data[InstanceIndex].front_prefetch_count;
97
+ }
98
+ else
99
+ {
100
+ stream.offset = InstData.data[InstanceIndex].back_prefetch_offset;
101
+ stream.count = InstData.data[InstanceIndex].back_prefetch_count;
102
+ }
103
+ return stream;
104
+ }
105
+ instr_stream_t getNormalPrecompStream(in MC_precomputed_t precomp)
106
+ {
107
+ instr_stream_t stream;
108
+ if (precomp.frontface)
109
+ {
110
+ stream.offset = InstData.data[InstanceIndex].front_instr_offset + InstData.data[InstanceIndex].front_rem_pdf_count + InstData.data[InstanceIndex].front_genchoice_count;
111
+ stream.count = InstData.data[InstanceIndex].front_nprecomp_count;
112
+ }
113
+ else
114
+ {
115
+ stream.offset = InstData.data[InstanceIndex].back_instr_offset + InstData.data[InstanceIndex].back_rem_pdf_count + InstData.data[InstanceIndex].back_genchoice_count;
116
+ stream.count = InstData.data[InstanceIndex].back_nprecomp_count;
117
+ }
118
+ return stream;
119
+ }
120
+ #include <irr/builtin/material_compiler/glsl/common.glsl>
121
+ */
14
122
123
+ //
124
+ ///
15
125
16
126
127
+ // functions
128
+ vec3 rand3d(in uint _sample, inout irr_glsl_xoroshiro64star_state_t scramble_state)
129
+ {
130
+ uvec3 seqVal = texelFetch(sampleSequence,int(_sample)).xyz;
131
+ seqVal ^= uvec3(irr_glsl_xoroshiro64star(scramble_state),irr_glsl_xoroshiro64star(scramble_state),irr_glsl_xoroshiro64star(scramble_state));
132
+ return vec3(seqVal)*uintBitsToFloat(0x2f800004u);
133
+ }
134
+
17
135
float linearizeZBufferVal(in float nonLinearZBufferVal)
18
136
{
19
137
// 1-(Ax+B)/(Cx) = y
@@ -151,42 +269,64 @@ void main()
151
269
uvec2 outputLocation = gl_GlobalInvocationID.xy;
152
270
if (all(lessThan(outputLocation,staticViewData.imageDimensions)))
153
271
{
154
- ivec2 uv = ivec2(outputLocation);
155
- float revdepth = texelFetch(depthbuf,uv ,0).r;
272
+ ivec2 pixelCoord = ivec2(outputLocation);
273
+ float revdepth = texelFetch(depthbuf,pixelCoord ,0).r;
156
274
157
275
const uint outputID = outputLocation.y*staticViewData.samplesPerRowPerDispatch+outputLocation.x*staticViewData.samplesPerPixelPerDispatch;
158
276
159
277
SamplingData_t samplingData;
160
- // unproject
161
- vec3 viewDir;
162
- vec3 position;
163
- {
164
- const mat4x3 frustumCorners = pc.cummon.frustumCorners;
165
- const vec2 NDC = vec2(outputLocation)*staticViewData.rcpPixelSize+staticViewData.rcpHalfPixelSize;
166
-
167
- viewDir = mix(frustumCorners[0]*NDC.x+frustumCorners[1],frustumCorners[2]*NDC.x+frustumCorners[3],NDC.yyy);
168
- position = viewDir*linearizeZBufferVal(revdepth)+pc.cummon.normalMatrixAndCameraPos[3];
169
- }
170
278
171
279
bool alive = false;
172
- uint scramble,objectID,triangleID;
173
- bool backfacing;
174
- vec3 normal = vec3(0.0);
175
- vec2 uvCoord;
280
+ MC_precomputed_t precomputed;
281
+ irr_glsl_xoroshiro64star_state_t start_scramble;
282
+ vec2 uv;
176
283
if (revdepth>0.0)
177
- {
178
- scramble = texelFetch(scramblebuf,uv,0).r;
284
+ {
285
+ // vis buffer read
286
+ const uvec2 visBuffer = texelFetch(frontFacing_Object_Triangle,pixelCoord,0).rg;
287
+ // init scramble
288
+ start_scramble = texelFetch(scramblebuf,pixelCoord,0).rg;
289
+ // tmp gbuffer reads
290
+ const vec2 normalBuffer = texelFetch(encodedNormal,pixelCoord,0).rg;
291
+ uv = texelFetch(uvCoords,pixelCoord,0).xy;
292
+
293
+ // unproject
294
+ {
295
+ const mat4x3 frustumCorners = pc.cummon.frustumCorners;
296
+ const vec2 NDC = vec2(outputLocation)*staticViewData.rcpPixelSize+staticViewData.rcpHalfPixelSize;
297
+
298
+ const vec3 viewDir = mix(frustumCorners[0]*NDC.x+frustumCorners[1],frustumCorners[2]*NDC.x+frustumCorners[3],NDC.yyy);
299
+ worldPosition = viewDir*linearizeZBufferVal(revdepth)+pc.cummon.normalMatrixAndCameraPos[3];
300
+ normalizedV = normalize(viewDir);
301
+ }
179
302
303
+ // decode vis buffer
304
+ bool frontfacing;
305
+ {
306
+ frontfacing = visBuffer[0]<0x80000000u;
307
+ const uint objectID = visBuffer[0]&0x7fffffffu;
308
+ const uint triangleID = visBuffer[1];
309
+ }
310
+
311
+ // normally we'd use MeshPackerV2's vertex attribute data for this, but now we read from temporary GBuffer
312
+ {
313
+ normalizedN = irr_glsl_NormalDecode_signedSpherical(normalBuffer);
314
+ }
315
+
316
+ // TODO: once we merge the material compiler with the raygen
317
+ //precomputed = precomputeData(frontfacing);
318
+
319
+ //
180
320
const vec3 emissive = vec3(0.0,1.0,0.0);
181
321
vec3 acc;
182
322
if (pc.cummon.rcpFramesDispatched<1.0)
183
- acc = fetchFramebuffer(uv )+emissive/float(pc.cummon.framesDispatched-1u);
323
+ acc = fetchAccumulation(pixelCoord )+emissive/float(pc.cummon.framesDispatched-1u);
184
324
else
185
325
acc = emissive;
186
326
187
327
//
188
328
189
- storeFramebuffer (acc,uv );
329
+ storeAccumulation (acc,pixelCoord );
190
330
alive = true;
191
331
}
192
332
#ifdef USE_OPTIX_DENOISER
@@ -212,7 +352,7 @@ void main()
212
352
{
213
353
throughput /= float(staticViewData.samplesPerPixelPerDispatch);
214
354
215
- rays[realOutputID].origin = position ;/*+newray.direction*err?; TODO */
355
+ rays[realOutputID].origin = irr_glsl_MC_WorldSpacePosition() ;/*+newray.direction*err?; TODO */
216
356
rays[realOutputID].maxT = 0.0;
217
357
rays[realOutputID].direction = direction;
218
358
rays[realOutputID].mask = -1;
0 commit comments