forked from FWGS/xash3d-fwgs
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathray.rgen
589 lines (487 loc) · 21.7 KB
/
ray.rgen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#version 460 core
#extension GL_EXT_nonuniform_qualifier : enable
#extension GL_GOOGLE_include_directive : require
#include "ray_common.glsl"
#include "ray_kusochki.glsl"
#include "noise.glsl"
#include "brdf.h"
//#define DEBUG_LIGHT_CULLING
// FIXME what should these be?
const float shadow_offset_fudge = .1;
const float pdf_culling_threshold = 1e6;//100.;
const float color_factor = 1.;
const float color_culling_threshold = 1e-6;//600./color_factor;
const float throughput_threshold = 1e-3;
layout (constant_id = 4) const float LIGHT_GRID_CELL_SIZE = 256.;
layout (constant_id = 5) const uint MAX_LIGHT_CLUSTERS = 32768;
layout (constant_id = 6) const uint MAX_TEXTURES = 4096;
layout (constant_id = 7) const uint SBT_RECORD_SIZE = 64;
//const uint LIGHT_CLUSTER_SIZE = 2 + MAX_VISIBLE_POINT_LIGHTS + MAX_VISIBLE_SURFACE_LIGHTS;
//const uint LIGHT_CLUSTER_NUM_DLIGHTS_OFFSET = 0;
//const uint LIGHT_CLUSTER_NUM_EMISSIVE_SURFACES_OFFSET = 1;
//const uint LIGHT_CLUSTER_DLIGHTS_DATA_OFFSET = 2;
//const uint LIGHT_CLUSTER_EMISSIVE_SURFACES_DATA_OFFSET = 3 + MAX_VISIBLE_DLIGHTS;
layout(set = 0, binding = 0, rgba8) uniform image2D out_image_base_color;
layout(set = 0, binding = 6) uniform sampler2D textures[MAX_TEXTURES];
layout(set = 0, binding = 9, rgba16f) uniform image2D out_image_diffuse_gi;
layout(set = 0, binding = 10, rgba16f) uniform image2D out_image_specular;
layout(set = 0, binding = 11, rgba16f) uniform image2D out_image_additive;
layout(set = 0, binding = 12, rgba16f) uniform image2D out_image_normals;
layout(set = 0, binding = 1) uniform accelerationStructureEXT tlas;
layout(set = 0, binding = 2) uniform UBO {
mat4 inv_proj, inv_view;
} ubo;
layout (set = 0, binding = 7/*, align=4*/) uniform UBOLights { Lights lights; };
layout (set = 0, binding = 8, align = 1) readonly buffer UBOLightClusters {
ivec3 grid_min, grid_size;
//uint8_t clusters_data[MAX_LIGHT_CLUSTERS * LIGHT_CLUSTER_SIZE + HACK_OFFSET];
LightCluster clusters[MAX_LIGHT_CLUSTERS];
} light_grid;
layout (push_constant) uniform PC_ {
PushConstants push_constants;
};
layout(location = PAYLOAD_LOCATION_OPAQUE) rayPayloadEXT RayPayloadOpaque payload_opaque;
layout(location = PAYLOAD_LOCATION_SHADOW) rayPayloadEXT RayPayloadShadow payload_shadow;
layout(location = PAYLOAD_LOCATION_ADDITIVE) rayPayloadEXT RayPayloadAdditive payload_additive;
bool shadowed(vec3 pos, vec3 dir, float dist) {
payload_shadow.hit_type = SHADOW_HIT;
const uint flags = 0
//| gl_RayFlagsCullFrontFacingTrianglesEXT
//| gl_RayFlagsOpaqueEXT
| gl_RayFlagsTerminateOnFirstHitEXT
| gl_RayFlagsSkipClosestHitShaderEXT
;
traceRayEXT(tlas,
flags,
GEOMETRY_BIT_OPAQUE,
SHADER_OFFSET_HIT_SHADOW_BASE, SBT_RECORD_SIZE, SHADER_OFFSET_MISS_SHADOW,
pos, 0., dir, dist - shadow_offset_fudge, PAYLOAD_LOCATION_SHADOW);
return payload_shadow.hit_type == SHADOW_HIT;
}
// TODO join with just shadowed()
bool shadowedSky(vec3 pos, vec3 dir, float dist) {
payload_shadow.hit_type = SHADOW_HIT;
const uint flags = 0
//| gl_RayFlagsCullFrontFacingTrianglesEXT
//| gl_RayFlagsOpaqueEXT
//| gl_RayFlagsTerminateOnFirstHitEXT
//| gl_RayFlagsSkipClosestHitShaderEXT
;
traceRayEXT(tlas,
flags,
GEOMETRY_BIT_OPAQUE,
SHADER_OFFSET_HIT_SHADOW_BASE, SBT_RECORD_SIZE, SHADER_OFFSET_MISS_SHADOW,
pos, 0., dir, dist - shadow_offset_fudge, PAYLOAD_LOCATION_SHADOW);
return payload_shadow.hit_type != SHADOW_SKY;
}
// This is an entry point for evaluation of all other BRDFs based on selected configuration (for direct light)
void evalSplitBRDF(vec3 N, vec3 L, vec3 V, MaterialProperties material, out vec3 diffuse, out vec3 specular) {
// Prepare data needed for BRDF evaluation - unpack material properties and evaluate commonly used terms (e.g. Fresnel, NdotL, ...)
const BrdfData data = prepareBRDFData(N, L, V, material);
// Ignore V and L rays "below" the hemisphere
//if (data.Vbackfacing || data.Lbackfacing) return vec3(0.0f, 0.0f, 0.0f);
// Eval specular and diffuse BRDFs
specular = evalSpecular(data);
diffuse = evalDiffuse(data);
// Combine specular and diffuse layers
#if COMBINE_BRDFS_WITH_FRESNEL
// Specular is already multiplied by F, just attenuate diffuse
diffuse *= vec3(1.) - data.F;
#endif
}
float triangleSolidAngle(vec3 p, vec3 a, vec3 b, vec3 c) {
a = normalize(a - p);
b = normalize(b - p);
c = normalize(c - p);
const float tanHalfOmega = dot(a, cross(b,c)) / (1. + dot(b,c) + dot(c,a) + dot(a,b));
return atan(tanHalfOmega) * 2.;
}
vec3 baryMix(vec3 v1, vec3 v2, vec3 v3, vec2 bary) {
return v1 * (1. - bary.x - bary.y) + v2 * bary.x + v3 * bary.y;
}
vec2 baryMix(vec2 v1, vec2 v2, vec2 v3, vec2 bary) {
return v1 * (1. - bary.x - bary.y) + v2 * bary.x + v3 * bary.y;
}
void sampleSurfaceTriangle(
vec3 color, vec3 view_dir, MaterialProperties material /* TODO BrdfData instead is supposedly more efficient */,
mat4x3 emissive_transform, mat3 emissive_transform_normal,
uint triangle_index, uint index_offset, uint vertex_offset,
uint kusok_index,
out vec3 diffuse, out vec3 specular)
{
diffuse = specular = vec3(0.);
const uint first_index_offset = index_offset + triangle_index * 3;
// TODO this is not entirely correct -- need to mix between all normals, or have this normal precomputed
const uint vi1 = uint(indices[first_index_offset+0]) + vertex_offset;
const uint vi2 = uint(indices[first_index_offset+1]) + vertex_offset;
const uint vi3 = uint(indices[first_index_offset+2]) + vertex_offset;
const vec3 v1 = (emissive_transform * vec4(vertices[vi1].pos, 1.)).xyz;
const vec3 v2 = (emissive_transform * vec4(vertices[vi2].pos, 1.)).xyz;
const vec3 v3 = (emissive_transform * vec4(vertices[vi3].pos, 1.)).xyz;
// TODO projected uniform sampling
vec2 bary = vec2(sqrt(rand01()), rand01());
bary.y *= bary.x;
bary.x = 1. - bary.x;
const vec3 sample_pos = baryMix(v1, v2, v3, bary);
vec3 light_dir = sample_pos - payload_opaque.hit_pos_t.xyz;
const float light_dir_normal_dot = dot(light_dir, payload_opaque.normal);
if (light_dir_normal_dot <= 0.)
#ifdef DEBUG_LIGHT_CULLING
return vec3(1., 0., 1.) * color_factor;
#else
return;
#endif
// Consider area light sources as planes, take the first normal
const vec3 normal = normalize(emissive_transform_normal * vertices[vi1].normal);
const float light_dot = -dot(light_dir, normal);
if (light_dot <= 0.)
#ifdef DEBUG_LIGHT_CULLING
return vec3(1., 0., 0.) * color_factor;
#else
return;
#endif
// TODO emissive normals and areas can be precomputed
const float area = 1.;//.5 * length(cross(v1 - v2, v1 - v3));
const float light_dist2 = dot(light_dir, light_dir);
//float pdf = /*light_dist2 */ 1./ (area * light_dot);
float pdf = TWO_PI / triangleSolidAngle(payload_opaque.hit_pos_t.xyz, v1, v2, v3);
if (pdf > pdf_culling_threshold)
#ifdef DEBUG_LIGHT_CULLING
return vec3(0., 1., 0.) * color_factor;
#else
return;
#endif
#if 0
{
const uint tex_index = kusochki[kusok_index].tex_base_color;
if ((KUSOK_MATERIAL_FLAG_SKYBOX & tex_index) == 0) {
const vec2 uv1 = vertices[vi1].gl_tc;
const vec2 uv2 = vertices[vi2].gl_tc;
const vec2 uv3 = vertices[vi3].gl_tc;
const vec2 uv = baryMix(uv1, uv2, uv3, bary);
color *= texture(textures[nonuniformEXT(tex_index)], uv).rgb;
}
}
#endif
color /= pdf;
if (dot(color,color) < color_culling_threshold)
#ifdef DEBUG_LIGHT_CULLING
return vec3(0., 1., 0.) * color_factor;
#else
return;
#endif
light_dir = normalize(light_dir);
// TODO sample emissive texture
evalSplitBRDF(payload_opaque.normal, light_dir, view_dir, material, diffuse, specular);
diffuse *= color;
specular *= color;
vec3 combined = diffuse + specular;
if (dot(combined,combined) < color_culling_threshold)
#ifdef DEBUG_LIGHT_CULLING
return vec3(1., 1., 0.) * color_factor;
#else
return;
#endif
if (shadowed(payload_opaque.hit_pos_t.xyz, light_dir, sqrt(light_dist2))) {
diffuse = specular = vec3(0.);
}
}
void computePointLights(uint cluster_index, vec3 throughput, vec3 view_dir, MaterialProperties material, out vec3 diffuse, out vec3 specular) {
diffuse = specular = vec3(0.);
const uint num_point_lights = uint(light_grid.clusters[cluster_index].num_point_lights);
for (uint j = 0; j < num_point_lights; ++j) {
const uint i = uint(light_grid.clusters[cluster_index].point_lights[j]);
vec3 color = lights.point_lights[i].color_stopdot.rgb * throughput;
if (dot(color,color) < color_culling_threshold)
continue;
const vec4 origin_r = lights.point_lights[i].origin_r;
const float stopdot = lights.point_lights[i].color_stopdot.a;
const vec3 dir = lights.point_lights[i].dir_stopdot2.xyz;
const float stopdot2 = lights.point_lights[i].dir_stopdot2.a;
const bool not_environment = (lights.point_lights[i].environment == 0);
const vec3 light_dir = not_environment ? (origin_r.xyz - payload_opaque.hit_pos_t.xyz) : -dir; // TODO need to randomize sampling direction for environment soft shadow
const float radius = origin_r.w;
const vec3 light_dir_norm = normalize(light_dir);
const float light_dot = dot(light_dir_norm, payload_opaque.normal);
if (light_dot < 1e-5)
continue;
const float spot_dot = -dot(light_dir_norm, dir);
if (spot_dot < stopdot2)
continue;
float spot_attenuation = 1.f;
if (spot_dot < stopdot)
spot_attenuation = (spot_dot - stopdot2) / (stopdot - stopdot2);
//float fdist = 1.f;
float light_dist = 1e5; // TODO this is supposedly not the right way to do shadows for environment lights. qrad checks for hitting SURF_SKY, and maybe we should too?
const float d2 = dot(light_dir, light_dir);
const float r2 = origin_r.w * origin_r.w;
if (not_environment) {
if (radius < 1e-3)
continue;
const float dist = length(light_dir);
if (radius > dist)
continue;
#if 1
//light_dist = sqrt(d2);
light_dist = dist - radius;
//fdist = 2.f / (r2 + d2 + light_dist * sqrt(d2 + r2));
#else
light_dist = dist;
//const float fdist = 2.f / (r2 + d2 + light_dist * sqrt(d2 + r2));
//const float fdist = 2.f / (r2 + d2 + light_dist * sqrt(d2 + r2));
//fdist = (light_dist > 1.) ? 1.f / d2 : 1.f; // qrad workaround
#endif
//const float pdf = 1.f / (fdist * light_dot * spot_attenuation);
//const float pdf = TWO_PI / asin(radius / dist);
const float pdf = 1. / ((1. - sqrt(d2 - r2) / dist) * spot_attenuation);
color /= pdf;
}
// if (dot(color,color) < color_culling_threshold)
// continue;
vec3 ldiffuse, lspecular;
evalSplitBRDF(payload_opaque.normal, light_dir_norm, view_dir, material, ldiffuse, lspecular);
ldiffuse *= color;
lspecular *= color;
vec3 combined = ldiffuse + lspecular;
if (dot(combined,combined) < color_culling_threshold)
continue;
if (not_environment) {
if (shadowed(payload_opaque.hit_pos_t.xyz, light_dir_norm, light_dist + shadow_offset_fudge))
continue;
} else {
// for environment light check that we've hit SURF_SKY
if (shadowedSky(payload_opaque.hit_pos_t.xyz, light_dir_norm, light_dist + shadow_offset_fudge))
continue;
}
diffuse += ldiffuse;
specular += lspecular;
} // for all lights
}
void computeLighting(vec3 throughput, vec3 view_dir, MaterialProperties material, out vec3 diffuse, out vec3 specular) {
diffuse = specular = vec3(0.);
const ivec3 light_cell = ivec3(floor(payload_opaque.hit_pos_t.xyz / LIGHT_GRID_CELL_SIZE)) - light_grid.grid_min;
const uint cluster_index = uint(dot(light_cell, ivec3(1, light_grid.grid_size.x, light_grid.grid_size.x * light_grid.grid_size.y)));
if (any(greaterThanEqual(light_cell, light_grid.grid_size)) || cluster_index >= MAX_LIGHT_CLUSTERS)
return; // throughput * vec3(1., 0., 0.);
// const uint cluster_offset = cluster_index * LIGHT_CLUSTER_SIZE + HACK_OFFSET;
// const int num_dlights = int(light_grid.clusters_data[cluster_offset + LIGHT_CLUSTER_NUM_DLIGHTS_OFFSET]);
// const int num_emissive_surfaces = int(light_grid.clusters_data[cluster_offset + LIGHT_CLUSTER_NUM_EMISSIVE_SURFACES_OFFSET]);
// const uint emissive_surfaces_offset = cluster_offset + LIGHT_CLUSTER_EMISSIVE_SURFACES_DATA_OFFSET;
//C = vec3(float(num_emissive_surfaces));
//C = vec3(float(int(light_grid.clusters[cluster_index].num_emissive_surfaces)));
//C += .3 * fract(vec3(light_cell) / 4.);
const uint num_emissive_kusochki = uint(light_grid.clusters[cluster_index].num_emissive_surfaces);
float sampling_light_scale = 1.;
#if 0
const uint max_lights_per_frame = 4;
uint begin_i = 0, end_i = num_emissive_kusochki;
if (end_i > max_lights_per_frame) {
begin_i = rand() % (num_emissive_kusochki - max_lights_per_frame);
end_i = begin_i + max_lights_per_frame;
sampling_light_scale = float(num_emissive_kusochki) / float(max_lights_per_frame);
}
for (uint i = begin_i; i < end_i; ++i) {
#else
for (uint i = 0; i < num_emissive_kusochki; ++i) {
#endif
const uint index_into_emissive_kusochki = uint(light_grid.clusters[cluster_index].emissive_surfaces[i]);
if (push_constants.debug_light_index_begin < push_constants.debug_light_index_end) {
if (index_into_emissive_kusochki < push_constants.debug_light_index_begin || index_into_emissive_kusochki >= push_constants.debug_light_index_end)
continue;
}
const EmissiveKusok ek = lights.kusochki[index_into_emissive_kusochki];
const uint emissive_kusok_index = lights.kusochki[index_into_emissive_kusochki].kusok_index;
const Kusok ekusok = kusochki[emissive_kusok_index];
// TODO streamline matrices layouts
const mat4x3 emissive_transform = mat4x3(
vec3(ek.tx_row_x.x, ek.tx_row_y.x, ek.tx_row_z.x),
vec3(ek.tx_row_x.y, ek.tx_row_y.y, ek.tx_row_z.y),
vec3(ek.tx_row_x.z, ek.tx_row_y.z, ek.tx_row_z.z),
vec3(ek.tx_row_x.w, ek.tx_row_y.w, ek.tx_row_z.w)
);
const mat3 emissive_transform_normal = transpose(inverse(mat3(emissive_transform)));
if (emissive_kusok_index == uint(payload_opaque.kusok_index))
continue;
const uint triangle_index = rand_range(ekusok.triangles);
vec3 ldiffuse, lspecular;
sampleSurfaceTriangle(throughput * ek.emissive, view_dir, material, emissive_transform, emissive_transform_normal, triangle_index, ekusok.index_offset, ekusok.vertex_offset, emissive_kusok_index, ldiffuse, lspecular);
diffuse += ldiffuse * sampling_light_scale;
specular += lspecular * sampling_light_scale;
} // for all emissive kusochki
vec3 ldiffuse, lspecular;
computePointLights(cluster_index, throughput, view_dir, material, ldiffuse, lspecular);
diffuse += ldiffuse;
specular += lspecular;
}
// Additive translucency
vec3 traceAdditive(vec3 origin, vec3 direction, float ray_distance) {
const uint flags = 0
/* TODO try without*/ | gl_RayFlagsCullFrontFacingTrianglesEXT
//| gl_RayFlagsOpaqueEXT
| gl_RayFlagsSkipClosestHitShaderEXT
;
const uint sbt_offset = 0;
const uint sbt_stride = 0;
payload_additive.color = vec3(0.);
payload_additive.ray_distance = ray_distance;
traceRayEXT(tlas, flags, GEOMETRY_BIT_ADDITIVE,
sbt_offset, sbt_stride, SHADER_OFFSET_MISS_EMPTY,
origin, 0., direction, ray_distance + additive_soft_overshoot,
PAYLOAD_LOCATION_ADDITIVE);
return payload_additive.color * color_factor;
}
void main() {
rand01_state = push_constants.random_seed + gl_LaunchIDEXT.x * 1833 + gl_LaunchIDEXT.y * 31337;
vec2 uv = (gl_LaunchIDEXT.xy + .5) / gl_LaunchSizeEXT.xy * 2. - 1.;
vec3 origin = (ubo.inv_view * vec4(0, 0, 0, 1)).xyz;
vec4 target = ubo.inv_proj * vec4(uv.x, uv.y, 1, 1);
vec3 direction = (ubo.inv_view * vec4(normalize(target.xyz), 0)).xyz;
payload_opaque.material_index = 0;
payload_opaque.t_offset = .0;
payload_opaque.pixel_cone_spread_angle = push_constants.pixel_cone_spread_angle;
float out_material_index = 0.;
vec3 out_additive = vec3(0.);
vec3 out_diffuse_gi = vec3(0.);
vec3 out_specular = vec3(0.);
// Can be specular or diffuse_gi based on first bounce
vec3 out_accumulated = vec3(0.);
int first_bounce_brdf_type = 0;
int brdfType = SPECULAR_TYPE;
vec3 throughput = vec3(1.);
for (int bounce = 0; bounce < push_constants.bounces; ++bounce) {
const uint flags = gl_RayFlagsCullFrontFacingTrianglesEXT;
const uint sbt_offset = 0;
const uint sbt_stride = 0;
const float L = 10000.; // Why 10k?
traceRayEXT(tlas, flags, GEOMETRY_BIT_OPAQUE | GEOMETRY_BIT_REFRACTIVE,
sbt_offset, sbt_stride, SHADER_OFFSET_MISS_REGULAR,
origin, 0., direction, L,
PAYLOAD_LOCATION_OPAQUE);
vec3 additive = traceAdditive(origin, direction, payload_opaque.hit_pos_t.w <= 0. ? L : payload_opaque.hit_pos_t.w);
// Sky/envmap/emissive
if ((payload_opaque.kusok_index < 0) || any(greaterThan(payload_opaque.emissive, vec3(0.)))) {
if (bounce == 0) {
out_additive += payload_opaque.emissive * color_factor + additive;
} else {
out_accumulated += throughput * (/*payload_opaque.emissive * color_factor +*/ additive);
}
break;
}
#if 0 //def DEBUG_LIGHT_CULLING
// light clusters debugging
{
const ivec3 light_cell = ivec3(floor(payload_opaque.hit_pos_t.xyz / LIGHT_GRID_CELL_SIZE)) - light_grid.grid_min;
const uint cluster_index = uint(dot(light_cell, ivec3(1, light_grid.grid_size.x, light_grid.grid_size.x * light_grid.grid_size.y)));
if (any(greaterThanEqual(light_cell, light_grid.grid_size)) || cluster_index >= MAX_LIGHT_CLUSTERS) {
out_additive = vec3(1., 0., 0.) * color_factor;
break;
}
const uint num_emissive_kusochki = uint(light_grid.clusters[cluster_index].num_emissive_surfaces);
for (uint i = 0; i < num_emissive_kusochki; ++i) {
const uint index_into_emissive_kusochki = uint(light_grid.clusters[cluster_index].emissive_surfaces[i]);
if (push_constants.debug_light_index_begin < push_constants.debug_light_index_end) {
if (index_into_emissive_kusochki < push_constants.debug_light_index_begin || index_into_emissive_kusochki >= push_constants.debug_light_index_end)
continue;
}
out_additive = vec3(0., 0., 1.) * color_factor;
}
const uvec3 cellrand = pcg3d(uvec3(light_cell));
out_additive = .2 * color_factor * vec3(
uintToFloat01(cellrand.r),
uintToFloat01(cellrand.g),
uintToFloat01(cellrand.b));
break;
}
#endif
MaterialProperties material;
material.baseColor = payload_opaque.base_color;
material.metalness = payload_opaque.metalness;
material.emissive = payload_opaque.emissive;
material.roughness = payload_opaque.roughness;
// material.roughness = uintToFloat01(xxhash32(uvec3(abs(floor(payload_opaque.hit_pos_t.xyz/64.)))));
// material.metalness = step(.5, xxhash32(uvec3(abs(floor(payload_opaque.hit_pos_t.xyz/32.)))));
const vec3 shadingNormal = payload_opaque.normal;
const vec3 geometryNormal = payload_opaque.geometry_normal;
if (bounce == 0) { //brdfType == SPECULAR_TYPE)
out_additive = payload_opaque.emissive + additive;
additive = vec3(0.);
out_material_index = float(payload_opaque.material_index);
imageStore(out_image_base_color, ivec2(gl_LaunchIDEXT.xy), vec4(payload_opaque.base_color, 0.));
imageStore(out_image_normals, ivec2(gl_LaunchIDEXT.xy), vec4(geometryNormal.xy, shadingNormal.xy));
payload_opaque.base_color = vec3(1.);
//out_material_index = float(kusochki[payload_opaque.kusok_index].tex_roughness);
#if 0
//imageStore(out_image_base_color, ivec2(gl_LaunchIDEXT.xy), vec4(fract(payload_opaque.debug.xy), 0., 0.));
//imageStore(out_image_base_color, ivec2(gl_LaunchIDEXT.xy), vec4(payload_opaque.kusok_index));
imageStore(out_image_base_color, ivec2(gl_LaunchIDEXT.xy), vec4(payload_opaque.roughness));
imageStore(out_image_diffuse_gi, ivec2(gl_LaunchIDEXT.xy), vec4(0));
imageStore(out_image_specular, ivec2(gl_LaunchIDEXT.xy), vec4(0.));
imageStore(out_image_additive, ivec2(gl_LaunchIDEXT.xy), vec4(clamp(payload_opaque.normal, vec3(0.), vec3(1.)), 0.));
return;
#endif
}
// TODO should we do this after reflect/transmit decision?
#define SKIP_TRASMITTED_LIGHT
#ifndef SKIP_TRASMITTED_LIGHT
C += computeLighting(throughput, -direction, material);
if (bounce == push_constants.bounces - 1)
break;
#else
vec3 prev_throughput = throughput;
#endif
const vec3 V = -direction;
if (material.metalness == 1.0f && material.roughness == 0.0f) {
// Fast path for mirrors
brdfType = SPECULAR_TYPE;
} else {
// Decide whether to sample diffuse or specular BRDF (based on Fresnel term)
float brdfProbability = getBrdfProbability(material, V, shadingNormal);
if (rand01() < brdfProbability) {
brdfType = SPECULAR_TYPE;
throughput /= brdfProbability;
} else {
// Refraction
if (rand01() < payload_opaque.transmissiveness) {
throughput *= material.baseColor;
direction = refract(direction, payload_opaque.geometry_normal, .8);
origin = payload_opaque.hit_pos_t.xyz - payload_opaque.geometry_normal * shadow_offset_fudge;
continue;
}
brdfType = DIFFUSE_TYPE;
throughput /= (1.0f - brdfProbability);
}
}
#ifdef SKIP_TRASMITTED_LIGHT
vec3 diffuse, specular;
computeLighting(prev_throughput, -direction, material, diffuse, specular);
if (bounce == 0) {
out_diffuse_gi += diffuse;
out_specular += specular;
} else {
out_accumulated += payload_opaque.base_color * (diffuse + specular);
out_accumulated += prev_throughput * additive;
}
if (bounce == push_constants.bounces - 1)
break;
#endif
vec2 u = vec2(rand01(), rand01());
vec3 brdfWeight;
if (!evalIndirectCombinedBRDF(u, shadingNormal, geometryNormal, V, material, brdfType, direction, brdfWeight)) {
break; // Ray was eaten by the surface :(
}
throughput *= brdfWeight;
if (dot(throughput, throughput) < throughput_threshold)
break;
origin = payload_opaque.hit_pos_t.xyz;
if (bounce == 0)
first_bounce_brdf_type = brdfType;
} // for all bounces
if (first_bounce_brdf_type == DIFFUSE_TYPE) {
out_diffuse_gi += out_accumulated;
} else {
out_specular += out_accumulated;
}
imageStore(out_image_diffuse_gi, ivec2(gl_LaunchIDEXT.xy), vec4(out_diffuse_gi / color_factor, out_material_index));
imageStore(out_image_specular, ivec2(gl_LaunchIDEXT.xy), vec4(out_specular / color_factor, 0.));
imageStore(out_image_additive, ivec2(gl_LaunchIDEXT.xy), vec4(out_additive / color_factor, 0.));
}