-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscene_gi.c
More file actions
1625 lines (1408 loc) · 66.1 KB
/
Copy pathscene_gi.c
File metadata and controls
1625 lines (1408 loc) · 66.1 KB
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: MIT
// The authors below grant copyright rights under the MIT license:
// Copyright (c) 2025 Nick Klingensmith
// Copyright (c) 2025 Qualcomm Technologies, Inc.
#include "scene.h"
#include "tools/scene_util.h"
#include "app.h"
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <math.h>
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include <cimgui.h>
// GI scene - GLTF model with environment skybox and shadow mapping
// Desktop: WASD+QE fly camera
// Android: Arc rotation camera (touch drag to orbit, pinch to zoom)
///////////////////////////////////////////
// Shadow configuration
///////////////////////////////////////////
static const float SHADOW_MAP_SIZE = 20.0f;
static const int32_t SHADOW_MAP_RESOLUTION = 2048;
static const float SHADOW_MAP_NEAR = 0.01f;
static const float SHADOW_MAP_FAR = 50.0f;
///////////////////////////////////////////
// GI probe configuration
///////////////////////////////////////////
#define GI_GRID_SIZE 32
#define GI_CYCLE_LENGTH (GI_GRID_SIZE * 3) // 96 frames per full update
#define GI_CAM_DISTANCE 50.0f
#define GI_CAM_NEAR 0.000f
///////////////////////////////////////////
// Shadow buffer (matches ShadowBuffer in pbr_shadow.hlsl / shadow_receiver.hlsl)
///////////////////////////////////////////
typedef struct {
float4x4 shadow_transform;
float3 light_direction;
float shadow_bias;
float3 light_color;
float shadow_pixel_size;
} shadow_buffer_data_t;
///////////////////////////////////////////
// GI buffer (matches GIBuffer in pbr_gi.hlsl)
///////////////////////////////////////////
typedef struct {
float3 gi_volume_min;
float gi_intensity;
float3 gi_volume_inv; // 1.0 / (volume_max - volume_min)
float _gi_pad;
} gi_buffer_data_t;
typedef enum {
gi_mode_per_pixel,
gi_mode_per_vertex,
gi_mode_cubemap,
} gi_mode_;
///////////////////////////////////////////
// Scene state
///////////////////////////////////////////
typedef struct {
scene_t base;
// GLTF model
su_gltf_t* model;
char* model_path;
skr_shader_t shader; // pbr_gi shader (per-pixel GI)
skr_shader_t shader_vertex_gi; // pbr_gi_vertex shader (per-vertex GI)
skr_shader_t shader_cubemap; // pbr_cubemap shader (cubemap irradiance, no GI)
gi_mode_ gi_mode;
float model_scale;
// Placeholder while loading
skr_mesh_t placeholder_mesh;
skr_material_t placeholder_material;
skr_tex_t white_texture;
skr_tex_t black_texture;
// Skybox / environment cubemap
skr_tex_t cubemap_texture;
skr_tex_t equirect_texture;
skr_material_t equirect_convert_material;
skr_shader_t equirect_to_cubemap_shader;
skr_shader_t mipgen_shader;
skr_shader_t skybox_shader;
skr_material_t skybox_material;
skr_mesh_t skybox_mesh;
bool cubemap_ready;
char* skybox_path;
// Shadow mapping
skr_tex_t shadow_map;
skr_shader_t shadow_caster_shader;
skr_material_t shadow_caster_material;
skr_render_list_t shadow_list;
skr_buffer_t shadow_buffer;
float light_angle; // degrees, 0-360
float light_elevation; // 0 = horizontal, 1 = straight down
float3 light_dir; // computed from angle/elevation
float3 light_color;
// Floor
skr_mesh_t floor_mesh;
skr_material_t floor_material;
skr_material_t floor_material_vtx; // per-vertex GI variant
skr_material_t floor_material_cubemap; // cubemap irradiance variant
skr_tex_t floor_texture;
// GI probe system (single buffer, continuous temporal accumulation)
skr_tex_t gi_sh_r; // SH R (32^3 rgba128, 3D + compute)
skr_tex_t gi_sh_g; // SH G
skr_tex_t gi_sh_b; // SH B
skr_tex_t gi_capture_color; // 32x32 color render target for captures
skr_tex_t gi_capture_depth; // 32x32 depth for capture occlusion
skr_shader_t gi_decay_shader; // Voxel decay (gi_clear.hlsl)
skr_compute_t gi_decay_compute;
skr_render_list_t gi_capture_list;
skr_shader_t gi_capture_shader; // Simplified diffuse for captures
skr_material_t gi_capture_material;
skr_buffer_t gi_const_buffer; // GI constant buffer (b12)
int32_t gi_frame; // 0 to GI_CYCLE_LENGTH-1
float gi_intensity;
float gi_sh_decay; // SH EMA decay per frame (0.98 = ~50 frame window)
uint32_t gi_total_frames; // Monotonic frame counter for random seeds
int32_t gi_ray_count; // Rays per probe per frame (1-8)
float gi_env_mip; // Cubemap mip for environment fallback
float gi_env_strength; // Multiplier for environment contribution
float3 gi_volume_min;
float3 gi_volume_max;
// Voxel radiance (double-buffered: build one while reading the other)
skr_tex_t gi_voxel[2]; // 32^3 rgba128, radiance + opacity
int32_t gi_voxel_write; // Index we're building into (0 or 1)
skr_shader_t gi_voxelize_shader;
skr_compute_t gi_voxelize_compute;
// Fast voxelization (12-view depth-based)
skr_tex_t gi_fast_color; // 32x32 x6-layer rgba64f array RT
skr_tex_t gi_fast_depth; // 32x32 x6-layer depth16 array RT
skr_shader_t gi_fast_voxelize_shader;
skr_compute_t gi_fast_voxelize_compute;
int32_t gi_voxel_mode; // 0=Layer Scan, 1=Fast, 2=Fast Periodic
bool gi_fast_init_done; // Has fast voxelize run at least once?
bool gi_volume_computed; // Volume bounds computed from model?
float gi_prev_model_scale; // Track for recompute
// Voxel-to-SH conversion (per-frame random ray march with EMA)
skr_shader_t gi_voxel_to_sh_shader;
skr_compute_t gi_voxel_to_sh_compute;
// GI debug visualization
skr_shader_t gi_debug_shader;
skr_material_t gi_debug_material;
skr_mesh_t gi_debug_mesh;
bool gi_show_probes;
bool gi_show_voxels;
int32_t gi_debug_mode; // 0=SH, 1=UVW, 2=L0, 3=SH detail
skr_shader_t gi_debug_voxel_shader;
skr_material_t gi_debug_voxel_material;
skr_mesh_t gi_debug_voxel_mesh;
bool gi_stepping; // Pause auto-advance, step manually
bool gi_step_next; // Advance one layer then pause
skr_shader_t gi_slice_shader;
skr_material_t gi_slice_material;
skr_mesh_t gi_slice_mesh;
bool show_floor;
// Camera state
float time;
float delta_time;
#ifdef __ANDROID__
// Arc rotation camera (touch)
float cam_yaw;
float cam_pitch;
float cam_distance;
float cam_yaw_vel;
float cam_pitch_vel;
float cam_distance_vel;
float3 cam_target;
float3 cam_target_vel;
#else
// Fly camera (WASD+QE)
float3 cam_pos;
float cam_yaw;
float cam_pitch;
#endif
} scene_gi_t;
///////////////////////////////////////////
// Shadow map helpers
///////////////////////////////////////////
static float3 _quantize_light_pos(float3 pos, float4x4 view_matrix, float texel_size) {
float4 view_pos = float4x4_transform_float4(view_matrix, (float4){pos.x, pos.y, pos.z, 1.0f});
view_pos.x = roundf(view_pos.x / texel_size) * texel_size;
view_pos.y = roundf(view_pos.y / texel_size) * texel_size;
float4x4 view_inv = float4x4_invert(view_matrix);
float4 world_pos = float4x4_transform_float4(view_inv, view_pos);
return (float3){world_pos.x, world_pos.y, world_pos.z};
}
///////////////////////////////////////////
// Skybox loading (mirrors scene_gltf pattern)
///////////////////////////////////////////
static void _destroy_skybox(scene_gi_t* scene) {
if (!scene->cubemap_ready) return;
skr_mesh_destroy (&scene->skybox_mesh);
skr_material_destroy(&scene->skybox_material);
skr_shader_destroy (&scene->skybox_shader);
skr_shader_destroy (&scene->mipgen_shader);
skr_shader_destroy (&scene->equirect_to_cubemap_shader);
skr_tex_destroy (&scene->cubemap_texture);
free(scene->skybox_path);
scene->skybox_path = NULL;
scene->cubemap_ready = false;
}
static void _load_skybox(scene_gi_t* scene, const char* path) {
_destroy_skybox(scene);
int32_t equirect_width = 0;
int32_t equirect_height = 0;
skr_tex_fmt_ equirect_format = skr_tex_fmt_rgba32_srgb;
void* equirect_data = su_image_load(path, &equirect_width, &equirect_height, &equirect_format, 4);
if (!equirect_data || equirect_width <= 0 || equirect_height <= 0) {
su_log(su_log_warning, "Failed to load skybox: %s", path);
return;
}
skr_tex_create(
equirect_format,
skr_tex_flags_readable,
su_sampler_linear_wrap,
(skr_vec3i_t){equirect_width, equirect_height, 1},
1, 0,
&(skr_tex_data_t){.data = equirect_data, .mip_count = 1, .layer_count = 1},
&scene->equirect_texture
);
skr_tex_set_name(&scene->equirect_texture, "gi_equirect_source");
su_image_free(equirect_data);
const int32_t cube_size = equirect_height / 2;
skr_tex_create(
equirect_format,
skr_tex_flags_readable | skr_tex_flags_writeable | skr_tex_flags_cubemap | skr_tex_flags_gen_mips,
su_sampler_linear_clamp,
(skr_vec3i_t){cube_size, cube_size, 6},
1, 0, NULL, &scene->cubemap_texture
);
skr_tex_set_name(&scene->cubemap_texture, "gi_environment_cubemap");
scene->equirect_to_cubemap_shader = su_shader_load("shaders/equirect_to_cubemap.hlsl.sks", "equirect_to_cubemap");
skr_material_create((skr_material_info_t){
.shader = &scene->equirect_to_cubemap_shader,
.write_mask = skr_write_rgba,
.cull = skr_cull_none,
}, &scene->equirect_convert_material);
skr_material_set_tex(&scene->equirect_convert_material, "equirect_tex", &scene->equirect_texture);
skr_renderer_blit(&scene->equirect_convert_material, &scene->cubemap_texture, (skr_recti_t){0, 0, cube_size, cube_size});
skr_material_destroy(&scene->equirect_convert_material);
skr_tex_destroy(&scene->equirect_texture);
scene->mipgen_shader = su_shader_load("shaders/cubemap_mipgen.hlsl.sks", "cubemap_mipgen");
skr_tex_generate_mips(&scene->cubemap_texture, &scene->mipgen_shader);
scene->skybox_shader = su_shader_load("shaders/cubemap_skybox.hlsl.sks", "gi_skybox_shader");
skr_material_create((skr_material_info_t){
.shader = &scene->skybox_shader,
.write_mask = skr_write_rgba,
.depth_test = skr_compare_less_or_eq,
.cull = skr_cull_none,
.queue_offset = 100,
}, &scene->skybox_material);
skr_material_set_tex(&scene->skybox_material, "cubemap", &scene->cubemap_texture);
scene->skybox_mesh = su_mesh_create_fullscreen_quad();
skr_mesh_set_name(&scene->skybox_mesh, "gi_skybox_quad");
scene->cubemap_ready = true;
scene->skybox_path = strdup(path);
su_log(su_log_info, "GI: Loaded skybox: %s (%dx%d)", path, cube_size, cube_size);
}
///////////////////////////////////////////
// Model loading
///////////////////////////////////////////
static void _load_model(scene_gi_t* scene, const char* path) {
if (scene->model) {
su_gltf_destroy(scene->model);
}
free(scene->model_path);
skr_material_info_t infos[] = {
{ .shader = &scene->shader, .cull = skr_cull_back, .write_mask = skr_write_default, .depth_test = skr_compare_less },
{ .shader = &scene->gi_capture_shader, .cull = skr_cull_back, .depth_test = skr_compare_less },
{ .shader = &scene->shader_vertex_gi, .cull = skr_cull_back, .write_mask = skr_write_default, .depth_test = skr_compare_less },
{ .shader = &scene->shader_cubemap, .cull = skr_cull_back, .write_mask = skr_write_default, .depth_test = skr_compare_less },
};
scene->model = su_gltf_load_ex(path, infos, 4);
scene->model_path = strdup(path);
scene->gi_volume_computed = false;
su_log(su_log_info, "GI: Loading model: %s", path);
}
///////////////////////////////////////////
// Create / Destroy
///////////////////////////////////////////
static scene_t* _scene_gi_create(void) {
scene_gi_t* scene = calloc(1, sizeof(scene_gi_t));
if (!scene) return NULL;
scene->base.size = sizeof(scene_gi_t);
scene->model_scale = 1.0f;
scene->time = 0.0f;
scene->light_angle = 240.0f;
scene->light_elevation = 0.2f;
scene->light_color = (float3){.83f, .49f, .38f};
// Camera defaults
#ifdef __ANDROID__
scene->cam_yaw = 0.4f;
scene->cam_pitch = 0.3f;
scene->cam_distance = 8.0f;
scene->cam_target = (float3){0.0f, 1.0f, 0.0f};
#else
scene->cam_pos = (float3){0.0f, 3.0f, 6.0f};
scene->cam_yaw = 0.0f;
scene->cam_pitch = -0.2f;
#endif
// Fallback textures
scene->white_texture = su_tex_create_solid_color(0xFFFFFFFF);
scene->black_texture = su_tex_create_solid_color(0xFF000000);
skr_tex_set_name(&scene->white_texture, "gi_white_fallback");
skr_tex_set_name(&scene->black_texture, "gi_black_fallback");
// Placeholder mesh
skr_vec4_t gray = {0.5f, 0.5f, 0.5f, 1.0f};
scene->placeholder_mesh = su_mesh_create_sphere(16, 12, 1.0f, gray);
skr_mesh_set_name(&scene->placeholder_mesh, "gi_placeholder_sphere");
// PBR shaders (per-pixel GI, per-vertex GI, cubemap-only)
scene->shader = su_shader_load("shaders/pbr_gi.hlsl.sks", "pbr_gi");
scene->shader_vertex_gi = su_shader_load("shaders/pbr_gi_vertex.hlsl.sks", "pbr_gi_vertex");
scene->shader_cubemap = su_shader_load("shaders/pbr_cubemap.hlsl.sks", "pbr_cubemap");
// Placeholder material
skr_material_create((skr_material_info_t){
.shader = &scene->shader,
.cull = skr_cull_back,
.write_mask = skr_write_default,
.depth_test = skr_compare_less,
}, &scene->placeholder_material);
skr_material_set_tex (&scene->placeholder_material, "albedo_tex", &scene->white_texture);
skr_material_set_tex (&scene->placeholder_material, "emission_tex", &scene->black_texture);
skr_vec4_t mat_color = {0.5f, 0.5f, 0.5f, 1.0f};
skr_material_set_param(&scene->placeholder_material, "color", sksc_shader_var_float, 4, &mat_color);
skr_vec4_t emission = {0.0f, 0.0f, 0.0f, 1.0f};
skr_material_set_param(&scene->placeholder_material, "emission_factor", sksc_shader_var_float, 4, &emission);
skr_vec4_t tex_trans = {0.0f, 0.0f, 1.0f, 1.0f};
skr_material_set_param(&scene->placeholder_material, "tex_trans", sksc_shader_var_float, 4, &tex_trans);
// Shadow map
skr_tex_create(
skr_tex_fmt_depth16,
skr_tex_flags_writeable | skr_tex_flags_readable,
(skr_tex_sampler_t){
.sample = skr_tex_sample_linear,
.address = skr_tex_address_clamp,
.sample_compare = skr_compare_less_or_eq,
.anisotropy = 1,
},
(skr_vec3i_t){SHADOW_MAP_RESOLUTION, SHADOW_MAP_RESOLUTION, 1},
1, 0, NULL, &scene->shadow_map
);
skr_tex_set_name(&scene->shadow_map, "gi_shadow_map");
// Shadow caster shader + material
scene->shadow_caster_shader = su_shader_load("shaders/shadow_caster.hlsl.sks", "gi_shadow_caster");
skr_material_create((skr_material_info_t){
.shader = &scene->shadow_caster_shader,
.write_mask = skr_write_depth,
.depth_test = skr_compare_less,
.depth_clamp = true,
.cull = skr_cull_none,
}, &scene->shadow_caster_material);
skr_render_list_create(&scene->shadow_list);
// Shadow constant buffer
shadow_buffer_data_t shadow_data = {0};
skr_buffer_create(&shadow_data, sizeof(shadow_buffer_data_t), 1, skr_buffer_type_constant, skr_use_dynamic, &scene->shadow_buffer);
skr_buffer_set_name(&scene->shadow_buffer, "gi_shadow_constants");
// Floor
skr_vec4_t white = {1.0f, 1.0f, 1.0f, 1.0f};
scene->floor_mesh = su_mesh_create_quad(20.0f, 20.0f, (skr_vec3_t){0, 1, 0}, false, white);
skr_mesh_set_name(&scene->floor_mesh, "gi_floor");
skr_material_create((skr_material_info_t){
.shader = &scene->shader,
.write_mask = skr_write_default,
.depth_test = skr_compare_less,
}, &scene->floor_material);
scene->floor_texture = su_tex_create_checkerboard(512, 32, 0xFFFFFFFF, 0xFF888888, true);
skr_tex_set_name(&scene->floor_texture, "gi_floor_checker");
skr_material_set_tex (&scene->floor_material, "albedo_tex", &scene->floor_texture);
skr_material_set_tex (&scene->floor_material, "emission_tex", &scene->black_texture);
skr_vec4_t floor_color = {1.0f, 1.0f, 1.0f, 1.0f};
skr_vec4_t floor_emission = {0.0f, 0.0f, 0.0f, 0.0f};
skr_vec4_t floor_tex_trans = {0.0f, 0.0f, 1.0f, 1.0f};
skr_material_set_param(&scene->floor_material, "color", sksc_shader_var_float, 4, &floor_color);
skr_material_set_param(&scene->floor_material, "emission_factor", sksc_shader_var_float, 4, &floor_emission);
skr_material_set_param(&scene->floor_material, "tex_trans", sksc_shader_var_float, 4, &floor_tex_trans);
// Floor material (per-vertex GI variant)
skr_material_create((skr_material_info_t){
.shader = &scene->shader_vertex_gi,
.write_mask = skr_write_default,
.depth_test = skr_compare_less,
}, &scene->floor_material_vtx);
skr_material_set_tex (&scene->floor_material_vtx, "albedo_tex", &scene->floor_texture);
skr_material_set_tex (&scene->floor_material_vtx, "emission_tex", &scene->black_texture);
skr_material_set_param(&scene->floor_material_vtx, "color", sksc_shader_var_float, 4, &floor_color);
skr_material_set_param(&scene->floor_material_vtx, "emission_factor", sksc_shader_var_float, 4, &floor_emission);
skr_material_set_param(&scene->floor_material_vtx, "tex_trans", sksc_shader_var_float, 4, &floor_tex_trans);
// Floor material (cubemap irradiance variant)
skr_material_create((skr_material_info_t){
.shader = &scene->shader_cubemap,
.write_mask = skr_write_default,
.depth_test = skr_compare_less,
}, &scene->floor_material_cubemap);
skr_material_set_tex (&scene->floor_material_cubemap, "albedo_tex", &scene->floor_texture);
skr_material_set_tex (&scene->floor_material_cubemap, "emission_tex", &scene->black_texture);
skr_material_set_param(&scene->floor_material_cubemap, "color", sksc_shader_var_float, 4, &floor_color);
skr_material_set_param(&scene->floor_material_cubemap, "emission_factor", sksc_shader_var_float, 4, &floor_emission);
skr_material_set_param(&scene->floor_material_cubemap, "tex_trans", sksc_shader_var_float, 4, &floor_tex_trans);
// GI probe system (volume bounds computed dynamically from model + floor)
scene->gi_intensity = 1.0f;
scene->gi_volume_min = (float3){-10.0f, -10.0f, -10.0f};
scene->gi_volume_max = (float3){ 10.0f, 10.0f, 10.0f};
scene->gi_sh_decay = 0.99f;
scene->gi_total_frames = 0;
scene->gi_ray_count = 16;
scene->gi_env_mip = 3.0f;
scene->gi_env_strength = 1.0f;
scene->gi_frame = 0;
scene->gi_mode = gi_mode_per_pixel;
// Create 3D SH textures (single buffer, continuous temporal accumulation)
skr_tex_sampler_t gi_sampler = { .sample = skr_tex_sample_linear, .address = skr_tex_address_clamp };
skr_tex_create(skr_tex_fmt_rgba128f, skr_tex_flags_3d | skr_tex_flags_readable | skr_tex_flags_compute,
gi_sampler, (skr_vec3i_t){GI_GRID_SIZE, GI_GRID_SIZE, GI_GRID_SIZE}, 1, 1, NULL, &scene->gi_sh_r);
skr_tex_create(skr_tex_fmt_rgba128f, skr_tex_flags_3d | skr_tex_flags_readable | skr_tex_flags_compute,
gi_sampler, (skr_vec3i_t){GI_GRID_SIZE, GI_GRID_SIZE, GI_GRID_SIZE}, 1, 1, NULL, &scene->gi_sh_g);
skr_tex_create(skr_tex_fmt_rgba128f, skr_tex_flags_3d | skr_tex_flags_readable | skr_tex_flags_compute,
gi_sampler, (skr_vec3i_t){GI_GRID_SIZE, GI_GRID_SIZE, GI_GRID_SIZE}, 1, 1, NULL, &scene->gi_sh_b);
skr_tex_set_name(&scene->gi_sh_r, "gi_sh_r");
skr_tex_set_name(&scene->gi_sh_g, "gi_sh_g");
skr_tex_set_name(&scene->gi_sh_b, "gi_sh_b");
// Capture render targets (32x32)
skr_tex_create(skr_tex_fmt_rgba64f, skr_tex_flags_writeable | skr_tex_flags_readable,
(skr_tex_sampler_t){ .sample = skr_tex_sample_linear, .address = skr_tex_address_clamp },
(skr_vec3i_t){GI_GRID_SIZE, GI_GRID_SIZE, 1}, 1, 0, NULL, &scene->gi_capture_color);
skr_tex_set_name(&scene->gi_capture_color, "gi_capture_color");
skr_tex_create(skr_tex_fmt_depth16, skr_tex_flags_writeable,
(skr_tex_sampler_t){ .sample = skr_tex_sample_linear, .address = skr_tex_address_clamp, .anisotropy = 1 },
(skr_vec3i_t){GI_GRID_SIZE, GI_GRID_SIZE, 1}, 1, 0, NULL, &scene->gi_capture_depth);
skr_tex_set_name(&scene->gi_capture_depth, "gi_capture_depth");
// GI decay compute shader (voxel-only decay at cycle start, SH handled by EMA)
scene->gi_decay_shader = su_shader_load("shaders/gi_clear.hlsl.sks", "gi_decay");
skr_compute_create(&scene->gi_decay_shader, (skr_compute_info_t){0}, &scene->gi_decay_compute);
// Voxel radiance textures (double-buffered)
scene->gi_voxel_write = 0;
for (int32_t i = 0; i < 2; i++) {
skr_tex_create(skr_tex_fmt_rgba128f, skr_tex_flags_3d | skr_tex_flags_readable | skr_tex_flags_compute,
gi_sampler, (skr_vec3i_t){GI_GRID_SIZE, GI_GRID_SIZE, GI_GRID_SIZE}, 1, 1, NULL, &scene->gi_voxel[i]);
char name[32];
snprintf(name, sizeof(name), "gi_voxel_%d", i);
skr_tex_set_name(&scene->gi_voxel[i], name);
}
// Voxelize compute shader
scene->gi_voxelize_shader = su_shader_load("shaders/gi_voxelize.hlsl.sks", "gi_voxelize");
skr_compute_create(&scene->gi_voxelize_shader, (skr_compute_info_t){0}, &scene->gi_voxelize_compute);
skr_compute_set_tex (&scene->gi_voxelize_compute, "capture_tex", &scene->gi_capture_color);
skr_compute_set_param(&scene->gi_voxelize_compute, "grid_size", sksc_shader_var_uint, 1, &(uint32_t){GI_GRID_SIZE});
// Fast voxelization render targets (6-layer arrays, same resolution as GI grid)
skr_tex_create(skr_tex_fmt_rgba64f,
skr_tex_flags_writeable | skr_tex_flags_readable | skr_tex_flags_array,
(skr_tex_sampler_t){ .sample = skr_tex_sample_linear, .address = skr_tex_address_clamp },
(skr_vec3i_t){GI_GRID_SIZE, GI_GRID_SIZE, 6}, 1, 0, NULL, &scene->gi_fast_color);
skr_tex_set_name(&scene->gi_fast_color, "gi_fast_color");
skr_tex_create(skr_tex_fmt_depth16,
skr_tex_flags_writeable | skr_tex_flags_readable | skr_tex_flags_array,
(skr_tex_sampler_t){ .sample = skr_tex_sample_linear, .address = skr_tex_address_clamp, .anisotropy = 1 },
(skr_vec3i_t){GI_GRID_SIZE, GI_GRID_SIZE, 6}, 1, 0, NULL, &scene->gi_fast_depth);
skr_tex_set_name(&scene->gi_fast_depth, "gi_fast_depth");
// Fast voxelize compute shader
scene->gi_fast_voxelize_shader = su_shader_load("shaders/gi_fast_voxelize.hlsl.sks", "gi_fast_voxelize");
skr_compute_create(&scene->gi_fast_voxelize_shader, (skr_compute_info_t){0}, &scene->gi_fast_voxelize_compute);
skr_compute_set_param(&scene->gi_fast_voxelize_compute, "grid_size", sksc_shader_var_uint, 1, &(uint32_t){GI_GRID_SIZE});
// Voxel-to-SH conversion (ray march the completed voxel volume → SH)
scene->gi_voxel_to_sh_shader = su_shader_load("shaders/gi_voxel_to_sh.hlsl.sks", "gi_voxel_to_sh");
skr_compute_create(&scene->gi_voxel_to_sh_shader, (skr_compute_info_t){0}, &scene->gi_voxel_to_sh_compute);
skr_compute_set_tex (&scene->gi_voxel_to_sh_compute, "sh_r", &scene->gi_sh_r);
skr_compute_set_tex (&scene->gi_voxel_to_sh_compute, "sh_g", &scene->gi_sh_g);
skr_compute_set_tex (&scene->gi_voxel_to_sh_compute, "sh_b", &scene->gi_sh_b);
skr_compute_set_param(&scene->gi_voxel_to_sh_compute, "grid_size", sksc_shader_var_uint, 1, &(uint32_t){GI_GRID_SIZE});
// GI capture render list and capture material
// Use gi_capture for fast/simple captures, or pbr_gi for full PBR during captures
skr_render_list_create(&scene->gi_capture_list);
scene->gi_capture_shader = su_shader_load("shaders/gi_capture.hlsl.sks", "gi_capture");
//scene->gi_capture_shader = su_shader_load("shaders/pbr_gi.hlsl.sks", "gi_capture_pbr");
skr_material_create((skr_material_info_t){
.shader = &scene->gi_capture_shader,
.depth_test = skr_compare_less,
.cull = skr_cull_back,
}, &scene->gi_capture_material);
skr_material_set_tex (&scene->gi_capture_material, "albedo_tex", &scene->floor_texture);
skr_material_set_tex (&scene->gi_capture_material, "emission_tex", &scene->black_texture);
skr_material_set_param(&scene->gi_capture_material, "color", sksc_shader_var_float, 4, &(skr_vec4_t){1, 1, 1, 1});
skr_material_set_param(&scene->gi_capture_material, "emission_factor", sksc_shader_var_float, 4, &(skr_vec4_t){0, 0, 0, 0});
skr_material_set_param(&scene->gi_capture_material, "tex_trans", sksc_shader_var_float, 4, &(skr_vec4_t){0, 0, 1, 1});
// GI constant buffer
gi_buffer_data_t gi_data = {0};
skr_buffer_create(&gi_data, sizeof(gi_buffer_data_t), 1, skr_buffer_type_constant, skr_use_dynamic, &scene->gi_const_buffer);
skr_buffer_set_name(&scene->gi_const_buffer, "gi_constants");
// GI debug visualization
scene->gi_show_probes = false;
scene->show_floor = false;
scene->gi_debug_shader = su_shader_load("shaders/gi_debug.hlsl.sks", "gi_debug");
skr_material_create((skr_material_info_t){
.shader = &scene->gi_debug_shader,
.cull = skr_cull_back,
.write_mask = skr_write_default,
.depth_test = skr_compare_less,
}, &scene->gi_debug_material);
scene->gi_debug_mesh = su_mesh_create_sphere(8, 6, 1.0f, (skr_vec4_t){1, 1, 1, 1});
skr_mesh_set_name(&scene->gi_debug_mesh, "gi_debug_probe_sphere");
// Voxel cube debug visualization
scene->gi_show_voxels = false;
scene->gi_debug_voxel_shader = su_shader_load("shaders/gi_debug_voxel.hlsl.sks", "gi_debug_voxel");
skr_material_create((skr_material_info_t){
.shader = &scene->gi_debug_voxel_shader,
.cull = skr_cull_back,
.write_mask = skr_write_default,
.depth_test = skr_compare_less,
}, &scene->gi_debug_voxel_material);
scene->gi_debug_voxel_mesh = su_mesh_create_cube(1.0f, NULL);
skr_mesh_set_name(&scene->gi_debug_voxel_mesh, "gi_debug_voxel_cube");
// Slice visualization cube (shows active capture slab when stepping)
scene->gi_slice_shader = su_shader_load("shaders/unlit.hlsl.sks", "gi_slice");
skr_material_create((skr_material_info_t){
.shader = &scene->gi_slice_shader,
.cull = skr_cull_none,
.write_mask = skr_write_rgba,
.depth_test = skr_compare_less,
.blend_state = skr_blend_alpha,
.queue_offset = 100,
}, &scene->gi_slice_material);
skr_vec4_t slice_colors[6] = {
{0, 1, 1, 0.15f}, {0, 1, 1, 0.15f}, // cyan
{0, 1, 1, 0.15f}, {0, 1, 1, 0.15f},
{0, 1, 1, 0.15f}, {0, 1, 1, 0.15f},
};
scene->gi_slice_mesh = su_mesh_create_cube(1.0f, slice_colors);
skr_mesh_set_name(&scene->gi_slice_mesh, "gi_slice_cube");
// /home/koujaku/Art/Modeling/BounceRoom.glb
// Load default assets (shader 0=per-pixel, 1=capture, 2=per-vertex, 3=cubemap)
skr_material_info_t model_infos[] = {
{ .shader = &scene->shader, .cull = skr_cull_back, .write_mask = skr_write_default, .depth_test = skr_compare_less },
{ .shader = &scene->gi_capture_shader, .cull = skr_cull_back, .depth_test = skr_compare_less },
{ .shader = &scene->shader_vertex_gi, .cull = skr_cull_back, .write_mask = skr_write_default, .depth_test = skr_compare_less },
{ .shader = &scene->shader_cubemap, .cull = skr_cull_back, .write_mask = skr_write_default, .depth_test = skr_compare_less },
};
scene->model = su_gltf_load_ex("LightingRoom.glb", model_infos, 4);
scene->model_path = strdup("LightingRoom.glb");
_load_skybox(scene, "cubemap.jpg");
return (scene_t*)scene;
}
static void _scene_gi_destroy(scene_t* base) {
scene_gi_t* scene = (scene_gi_t*)base;
// Model
su_gltf_destroy(scene->model);
free(scene->model_path);
// Placeholder
skr_mesh_destroy (&scene->placeholder_mesh);
skr_material_destroy(&scene->placeholder_material);
skr_tex_destroy (&scene->white_texture);
skr_tex_destroy (&scene->black_texture);
// Skybox
_destroy_skybox(scene);
// Shadow
skr_render_list_destroy(&scene->shadow_list);
skr_material_destroy (&scene->shadow_caster_material);
skr_shader_destroy (&scene->shadow_caster_shader);
skr_tex_destroy (&scene->shadow_map);
skr_buffer_destroy (&scene->shadow_buffer);
// Floor (materials before shaders — materials hold refs to shader meta)
skr_mesh_destroy (&scene->floor_mesh);
skr_material_destroy(&scene->floor_material);
skr_material_destroy(&scene->floor_material_vtx);
skr_material_destroy(&scene->floor_material_cubemap);
skr_tex_destroy (&scene->floor_texture);
// PBR shaders (after all materials referencing them are destroyed)
skr_shader_destroy (&scene->shader);
skr_shader_destroy (&scene->shader_vertex_gi);
skr_shader_destroy (&scene->shader_cubemap);
// GI probes
skr_tex_destroy (&scene->gi_sh_r);
skr_tex_destroy (&scene->gi_sh_g);
skr_tex_destroy (&scene->gi_sh_b);
skr_tex_destroy (&scene->gi_capture_color);
skr_tex_destroy (&scene->gi_capture_depth);
skr_compute_destroy (&scene->gi_decay_compute);
skr_shader_destroy (&scene->gi_decay_shader);
skr_render_list_destroy(&scene->gi_capture_list);
skr_material_destroy (&scene->gi_capture_material);
skr_shader_destroy (&scene->gi_capture_shader);
skr_buffer_destroy (&scene->gi_const_buffer);
skr_tex_destroy (&scene->gi_voxel[0]);
skr_tex_destroy (&scene->gi_voxel[1]);
skr_compute_destroy (&scene->gi_voxelize_compute);
skr_shader_destroy (&scene->gi_voxelize_shader);
skr_tex_destroy (&scene->gi_fast_color);
skr_tex_destroy (&scene->gi_fast_depth);
skr_compute_destroy (&scene->gi_fast_voxelize_compute);
skr_shader_destroy (&scene->gi_fast_voxelize_shader);
skr_compute_destroy (&scene->gi_voxel_to_sh_compute);
skr_shader_destroy (&scene->gi_voxel_to_sh_shader);
// GI debug
skr_mesh_destroy (&scene->gi_debug_mesh);
skr_material_destroy(&scene->gi_debug_material);
skr_shader_destroy (&scene->gi_debug_shader);
skr_mesh_destroy (&scene->gi_debug_voxel_mesh);
skr_material_destroy(&scene->gi_debug_voxel_material);
skr_shader_destroy (&scene->gi_debug_voxel_shader);
skr_mesh_destroy (&scene->gi_slice_mesh);
skr_material_destroy(&scene->gi_slice_material);
skr_shader_destroy (&scene->gi_slice_shader);
free(scene);
}
///////////////////////////////////////////
// Update (camera controls)
///////////////////////////////////////////
static void _scene_gi_update(scene_t* base, float delta_time) {
scene_gi_t* scene = (scene_gi_t*)base;
scene->time += delta_time;
scene->delta_time = delta_time;
}
///////////////////////////////////////////
// GI capture helpers
///////////////////////////////////////////
// Compute GI volume bounds from the scaled model.
// Adds one voxel of padding on each side.
static void _gi_update_volume_bounds(scene_gi_t* scene, su_bounds_t model_bounds, float scale) {
// Model is scaled from its own origin, no translation
float3 scene_min = {
model_bounds.min.x * scale,
model_bounds.min.y * scale,
model_bounds.min.z * scale,
};
float3 scene_max = {
model_bounds.max.x * scale,
model_bounds.max.y * scale,
model_bounds.max.z * scale,
};
// Pad by ~1.137 voxels on each side to offset grid from unit-aligned geometry
float3 tight_size = float3_sub(scene_max, scene_min);
float3 padding = float3_mul_s(tight_size, 1.137f / (float)(GI_GRID_SIZE - 2.0f * 1.137f));
scene->gi_volume_min = float3_sub(scene_min, padding);
scene->gi_volume_max = float3_add(scene_max, padding);
scene->gi_volume_computed = true;
scene->gi_prev_model_scale = scene->model_scale;
scene->gi_fast_init_done = false;
scene->gi_frame = 0;
}
// Build an orthographic view/proj for capturing one axis-aligned direction.
// axis: 0=X, 1=Y, 2=Z. dir_sign: +1 or -1.
// layer_pos: world-space position at the cell EDGE along the capture axis.
// Camera looks through the full cell depth (near=0, far=cell_size).
static void _gi_build_capture_camera(
float3 vol_min, float3 vol_max,
int32_t axis, float dir_sign, float layer_pos,
float4x4* out_view, float4x4* out_proj, float4x4* out_viewproj)
{
float3 center = {
(vol_min.x + vol_max.x) * 0.5f,
(vol_min.y + vol_max.y) * 0.5f,
(vol_min.z + vol_max.z) * 0.5f,
};
float3 cam_pos = center;
float3 up = {0, 1, 0};
// Place camera at the layer position along the capture axis.
if (axis == 0) { cam_pos.x = layer_pos; }
else if (axis == 1) { cam_pos.y = layer_pos; up = (float3){0, 0, -1}; }
else { cam_pos.z = layer_pos; }
// Look outward in the capture direction.
float3 forward = {0, 0, 0};
if (axis == 0) forward = (float3){dir_sign, 0, 0};
else if (axis == 1) forward = (float3){0, dir_sign, 0};
else forward = (float3){0, 0, dir_sign};
float3 cam_target = float3_add(cam_pos, forward);
*out_view = float4x4_lookat(cam_pos, cam_target, up);
// Orthographic half-extents cover the perpendicular axes of the volume.
float half_a, half_b;
if (axis == 0) {
half_a = (vol_max.z - vol_min.z) * 0.5f;
half_b = (vol_max.y - vol_min.y) * 0.5f;
} else if (axis == 1) {
half_a = (vol_max.x - vol_min.x) * 0.5f;
half_b = (vol_max.z - vol_min.z) * 0.5f;
} else {
half_a = (vol_max.x - vol_min.x) * 0.5f;
half_b = (vol_max.y - vol_min.y) * 0.5f;
}
// Camera is at cell edge, far plane covers full cell depth.
// Both +dir and -dir see the entire cell from opposite edges.
float cell_size;
if (axis == 0) cell_size = (vol_max.x - vol_min.x) / GI_GRID_SIZE;
else if (axis == 1) cell_size = (vol_max.y - vol_min.y) / GI_GRID_SIZE;
else cell_size = (vol_max.z - vol_min.z) / GI_GRID_SIZE;
float far_dist = cell_size;
*out_proj = float4x4_orthographic(-half_a, half_a, -half_b, half_b, GI_CAM_NEAR, far_dist);
*out_viewproj = float4x4_mul(*out_proj, *out_view);
}
///////////////////////////////////////////
// Fast voxelization (6-view depth-based)
///////////////////////////////////////////
// Build 6 orthographic cameras along major axes for fast voxelization.
// from_center=true: cameras at volume center, looking outward (far = half_size)
// from_center=false: cameras at volume edges, looking inward (far = full_size)
static void _gi_build_fast_cameras(
float3 vol_min, float3 vol_max, bool from_center,
su_system_buffer_t* out_sys)
{
float3 center = float3_mul_s(float3_add(vol_min, vol_max), 0.5f);
float3 vol_size = float3_sub(vol_max, vol_min);
float3 half_size = float3_mul_s(vol_size, 0.5f);
memset(out_sys, 0, sizeof(*out_sys));
out_sys->view_count = 6;
// Views: 0=+X, 1=-X, 2=+Y, 3=-Y, 4=+Z, 5=-Z
float3 forwards[6] = {
{ 1, 0, 0}, {-1, 0, 0},
{ 0, 1, 0}, { 0,-1, 0},
{ 0, 0, 1}, { 0, 0,-1},
};
float3 ups[6] = {
{0, 1, 0}, {0, 1, 0},
{0, 0,-1}, {0, 0,-1},
{0, 1, 0}, {0, 1, 0},
};
float axis_sizes[3] = { vol_size.x, vol_size.y, vol_size.z };
float half_sizes[3] = { half_size.x, half_size.y, half_size.z };
float vol_min_arr[3] = { vol_min.x, vol_min.y, vol_min.z };
float vol_max_arr[3] = { vol_max.x, vol_max.y, vol_max.z };
for (int32_t i = 0; i < 6; i++) {
int32_t axis = i / 2;
bool is_positive = (i % 2 == 0);
float3 cam_pos = center;
float far_dist;
if (from_center) {
far_dist = half_sizes[axis];
} else {
far_dist = axis_sizes[axis];
// Place camera at the near edge
float edge = is_positive ? vol_min_arr[axis] : vol_max_arr[axis];
if (axis == 0) cam_pos.x = edge;
else if (axis == 1) cam_pos.y = edge;
else cam_pos.z = edge;
}
float3 target = float3_add(cam_pos, forwards[i]);
float4x4 view = float4x4_lookat(cam_pos, target, ups[i]);
// Orthographic bounds (perpendicular axes)
float half_a, half_b;
if (axis == 0) { half_a = half_size.z; half_b = half_size.y; }
else if (axis == 1) { half_a = half_size.x; half_b = half_size.z; }
else { half_a = half_size.x; half_b = half_size.y; }
float4x4 proj = float4x4_orthographic(-half_a, half_a, -half_b, half_b, GI_CAM_NEAR, far_dist);
out_sys->view [i] = view;
out_sys->projection[i] = proj;
out_sys->viewproj [i] = float4x4_mul(proj, view);
out_sys->cam_pos [i] = (float4){cam_pos.x, cam_pos.y, cam_pos.z, 0};
out_sys->cam_dir [i] = (float4){forwards[i].x, forwards[i].y, forwards[i].z, 0};
}
}
// Run complete fast voxelization: 2 passes (center-out + edges-in), then swap buffers.
static void _gi_run_fast_voxelize(
scene_gi_t* scene,
float4x4 floor_instance,
float4x4 model_transform,
su_gltf_state_ model_state)
{
float3 vol_size = float3_sub(scene->gi_volume_max, scene->gi_volume_min);
// Clear voxel write buffer
skr_compute_set_tex (&scene->gi_decay_compute, "voxel", &scene->gi_voxel[scene->gi_voxel_write]);
skr_compute_set_param(&scene->gi_decay_compute, "decay", sksc_shader_var_float, 1, &(float){0.0f});
int32_t clear_dispatch = (GI_GRID_SIZE + 3) / 4;
skr_compute_execute(&scene->gi_decay_compute, clear_dispatch, clear_dispatch, clear_dispatch);
// Set GI intensity to 0 during captures (prevent feedback)
gi_buffer_data_t gi_capture_data = {
.gi_volume_min = scene->gi_volume_min,
.gi_intensity = 1.0f,
.gi_volume_inv = {
1.0f / vol_size.x,
1.0f / vol_size.y,
1.0f / vol_size.z,
},
};
skr_buffer_set(&scene->gi_const_buffer, &gi_capture_data, sizeof(gi_buffer_data_t));
skr_renderer_set_global_constants(12, &scene->gi_const_buffer);
// Bind SH textures globally (shader sees zeros since gi_intensity=0)
skr_renderer_set_global_texture(6, &scene->gi_sh_r);
skr_renderer_set_global_texture(7, &scene->gi_sh_g);
skr_renderer_set_global_texture(8, &scene->gi_sh_b);
// Shadow data needs to be bound for the capture shader
skr_renderer_set_global_constants(13, &scene->shadow_buffer);
skr_renderer_set_global_texture (14, &scene->shadow_map);
// Two passes: center outward, then edges inward
bool passes[2] = { true, false };
for (int32_t pass = 0; pass < 2; pass++) {
bool from_center = passes[pass];
su_system_buffer_t cap_sys;
_gi_build_fast_cameras(scene->gi_volume_min, scene->gi_volume_max, from_center, &cap_sys);
cap_sys.time = scene->time;
if (scene->cubemap_ready) {
cap_sys.cubemap_info = (float4){
(float)scene->cubemap_texture.size.x,
(float)scene->cubemap_texture.size.y,
(float)scene->cubemap_texture.mip_levels,
0.0f
};
}
// Render scene into 6-layer capture RT (uses pass system for multi-view fallback)
if (scene->show_floor)
skr_render_list_add(&scene->gi_capture_list, &scene->floor_mesh, &scene->gi_capture_material, &floor_instance, sizeof(float4x4), 1);
if (model_state == su_gltf_state_ready) {
su_gltf_add_to_render_list_shader(scene->model, &scene->gi_capture_list, &model_transform, 1);
}
skr_pass_t gi_pass = {
.color = &scene->gi_fast_color,
.depth = &scene->gi_fast_depth,
.clear = skr_clear_all,
.clear_color = {0, 0, 0, 0},
.clear_depth = 1.0f,
.viewport = {0, 0, (float)GI_GRID_SIZE, (float)GI_GRID_SIZE},
.scissor = {0, 0, GI_GRID_SIZE, GI_GRID_SIZE},
.view_count = cap_sys.view_count,
};
skr_pass_add_draw(&gi_pass, &scene->gi_capture_list, &cap_sys, sizeof(su_system_buffer_t));
skr_pass_submit(&gi_pass);
skr_render_list_clear(&scene->gi_capture_list);
// Dispatch fast voxelize compute: map depth captures to 3D voxels
uint32_t from_center_val = from_center ? 1 : 0;
skr_compute_set_tex (&scene->gi_fast_voxelize_compute, "capture_color", &scene->gi_fast_color);
skr_compute_set_tex (&scene->gi_fast_voxelize_compute, "capture_depth", &scene->gi_fast_depth);
skr_compute_set_tex (&scene->gi_fast_voxelize_compute, "voxel_tex", &scene->gi_voxel[scene->gi_voxel_write]);
skr_compute_set_param(&scene->gi_fast_voxelize_compute, "from_center", sksc_shader_var_uint, 1, &from_center_val);
skr_compute_execute(&scene->gi_fast_voxelize_compute, (GI_GRID_SIZE + 7) / 8, (GI_GRID_SIZE + 7) / 8, 6);
}
// Swap voxel buffers (fast mode completes in one frame)
scene->gi_voxel_write = 1 - scene->gi_voxel_write;
}
///////////////////////////////////////////
// Render
///////////////////////////////////////////
static void _scene_gi_render(scene_t* base, int32_t width, int32_t height, skr_render_list_t* ref_render_list, su_system_buffer_t* ref_system_buffer) {
scene_gi_t* scene = (scene_gi_t*)base;
// Compute light direction from angle/elevation
float elevation = fmaxf(-0.999999f, fminf(0.999999f, scene->light_elevation));
float angle_rad = scene->light_angle * (3.14159265f / 180.0f);
float horiz = sqrtf(1.0f - elevation * elevation);
scene->light_dir = (float3){ horiz * cosf(angle_rad), -elevation, horiz * sinf(angle_rad) };
// Environment cubemap setup
if (scene->cubemap_ready && ref_system_buffer) {
ref_system_buffer->cubemap_info = (float4){
(float)scene->cubemap_texture.size.x,
(float)scene->cubemap_texture.size.y,
(float)scene->cubemap_texture.mip_levels,
0.0f
};
ref_system_buffer->time = scene->time;
skr_renderer_set_global_texture(5, &scene->cubemap_texture);
}
// Compute model transform (needed by both GI captures and main pass)
su_gltf_state_ state = su_gltf_get_state(scene->model);
float4x4 model_transform = float4x4_identity();
float4x4 floor_instance = float4x4_identity();
if (state == su_gltf_state_ready) {
su_bounds_t bounds = su_gltf_get_bounds(scene->model);
float scale = scene->model_scale;
model_transform = float4x4_s((float3){scale, scale, scale});
// Floor covers the model's XZ footprint at its base Y
float floor_cx = (bounds.min.x + bounds.max.x) * 0.5f * scale;
float floor_cz = (bounds.min.z + bounds.max.z) * 0.5f * scale;
float floor_sx = (bounds.max.x - bounds.min.x) * scale / 20.0f;
float floor_sz = (bounds.max.z - bounds.min.z) * scale / 20.0f;
floor_instance = float4x4_trs(
(float3){floor_cx, bounds.min.y * scale, floor_cz},
(float4){0, 0, 0, 1},
(float3){floor_sx, 1, floor_sz});
// Recompute volume bounds when model first loads or scale changes
if (!scene->gi_volume_computed ||
scene->model_scale != scene->gi_prev_model_scale) {
_gi_update_volume_bounds(scene, bounds, scale);
}
}