@@ -456,24 +456,35 @@ void main() {
456456 diffuse_light_interp = vec4 (0.0 );
457457 specular_light_interp = vec4 (0.0 );
458458
459+ uint omni_light_count = sc_omni_lights(8 );
459460 uvec2 omni_light_indices = instances.data[draw_call.instance_index].omni_lights;
460- for (uint i = 0 ; i < sc_omni_lights() ; i++ ) {
461+ for (uint i = 0 ; i < omni_light_count ; i++ ) {
461462 uint light_index = (i > 3 ) ? ((omni_light_indices.y >> ((i - 4 ) * 8 )) & 0xFF) : ((omni_light_indices.x >> (i * 8 )) & 0xFF);
463+ if (i > 0 && light_index == 0xFF) {
464+ break ;
465+ }
466+
462467 light_process_omni_vertex(light_index, vertex, view, normal_interp, roughness, diffuse_light_interp.rgb, specular_light_interp.rgb);
463468 }
464469
470+ uint spot_light_count = sc_spot_lights(8 );
465471 uvec2 spot_light_indices = instances.data[draw_call.instance_index].spot_lights;
466- for (uint i = 0 ; i < sc_spot_lights() ; i++ ) {
472+ for (uint i = 0 ; i < spot_light_count ; i++ ) {
467473 uint light_index = (i > 3 ) ? ((spot_light_indices.y >> ((i - 4 ) * 8 )) & 0xFF) : ((spot_light_indices.x >> (i * 8 )) & 0xFF);
474+ if (i > 0 && light_index == 0xFF) {
475+ break ;
476+ }
477+
468478 light_process_spot_vertex(light_index, vertex, view, normal_interp, roughness, diffuse_light_interp.rgb, specular_light_interp.rgb);
469479 }
470480
471- if (sc_directional_lights() > 0 ) {
481+ uint directional_lights_count = sc_directional_lights(scene_data.directional_light_count);
482+ if (directional_lights_count > 0 ) {
472483 // We process the first directional light separately as it may have shadows.
473484 vec3 directional_diffuse = vec3 (0.0 );
474485 vec3 directional_specular = vec3 (0.0 );
475486
476- for (uint i = 0 ; i < sc_directional_lights() ; i++ ) {
487+ for (uint i = 0 ; i < directional_lights_count ; i++ ) {
477488 if (! bool (directional_lights.data[i].mask & instances.data[draw_call.instance_index].layer_mask)) {
478489 continue ; // Not masked, skip.
479490 }
@@ -729,6 +740,8 @@ layout(set = MATERIAL_UNIFORM_SET, binding = 0, std140) uniform MaterialUniforms
729740
730741#GLOBALS
731742
743+ #define scene_data scene_data_block.data
744+
732745/* clang-format on */
733746
734747#ifdef MODE_RENDER_DEPTH
@@ -799,7 +812,8 @@ vec4 fog_process(vec3 vertex) {
799812 float sun_total = 0.0 ;
800813 vec3 view = normalize (vertex);
801814
802- for (uint i = 0 ; i < sc_directional_lights(); i++ ) {
815+ uint directional_lights_count = sc_directional_lights(scene_data.directional_light_count);
816+ for (uint i = 0 ; i < directional_lights_count; i++ ) {
803817 vec3 light_color = directional_lights.data[i].color * directional_lights.data[i].energy;
804818 float light_amount = pow (max (dot (view, directional_lights.data[i].direction), 0.0 ), 8.0 );
805819 fog_color += light_color * light_amount * scene_data_block.data.fog_sun_scatter;
@@ -831,8 +845,6 @@ vec4 fog_process(vec3 vertex) {
831845
832846#endif // !MODE_RENDER DEPTH
833847
834- #define scene_data scene_data_block.data
835-
836848void main() {
837849#ifdef UBERSHADER
838850 bool front_facing = gl_FrontFacing ;
@@ -1129,9 +1141,13 @@ void main() {
11291141 vec3 vertex_ddx = dFdx (vertex);
11301142 vec3 vertex_ddy = dFdy (vertex);
11311143
1144+ uint decal_count = sc_decals(8 );
11321145 uvec2 decal_indices = instances.data[draw_call.instance_index].decals;
1133- for (uint i = 0 ; i < sc_decals() ; i++ ) {
1146+ for (uint i = 0 ; i < decal_count ; i++ ) {
11341147 uint decal_index = (i > 3 ) ? ((decal_indices.y >> ((i - 4 ) * 8 )) & 0xFF) : ((decal_indices.x >> (i * 8 )) & 0xFF);
1148+ if (decal_index == 0xFF) {
1149+ break ;
1150+ }
11351151
11361152 vec3 uv_local = (decals.data[decal_index].xform * vec4 (vertex, 1.0 )).xyz;
11371153 if (any (lessThan (uv_local, vec3 (0.0 , - 1.0 , 0.0 ))) || any (greaterThan (uv_local, vec3 (1.0 )))) {
@@ -1405,7 +1421,8 @@ void main() {
14051421
14061422 // skipping ssao, do we remove ssao totally?
14071423
1408- if (sc_reflection_probes() > 0 ) {
1424+ uint reflection_probe_count = sc_reflection_probes(8 );
1425+ if (reflection_probe_count > 0 ) {
14091426 vec4 reflection_accum = vec4 (0.0 , 0.0 , 0.0 , 0.0 );
14101427 vec4 ambient_accum = vec4 (0.0 , 0.0 , 0.0 , 0.0 );
14111428
@@ -1423,8 +1440,11 @@ void main() {
14231440 ref_vec = mix (ref_vec, bent_normal, roughness * roughness * roughness * roughness);
14241441
14251442 uvec2 reflection_indices = instances.data[draw_call.instance_index].reflection_probes;
1426- for (uint i = 0 ; i < sc_reflection_probes() ; i++ ) {
1443+ for (uint i = 0 ; i < reflection_probe_count ; i++ ) {
14271444 uint reflection_index = (i > 3 ) ? ((reflection_indices.y >> ((i - 4 ) * 8 )) & 0xFF) : ((reflection_indices.x >> (i * 8 )) & 0xFF);
1445+ if (reflection_index == 0xFF) {
1446+ break ;
1447+ }
14281448
14291449 if (reflection_accum.a >= 1.0 && ambient_accum.a >= 1.0 ) {
14301450 break ;
@@ -1519,7 +1539,8 @@ void main() {
15191539 direct_specular_light += specular_light_interp.rgb * f0;
15201540#endif
15211541
1522- if (sc_directional_lights() > 0 ) {
1542+ uint directional_lights_count = sc_directional_lights(scene_data.directional_light_count);
1543+ if (directional_lights_count > 0 ) {
15231544#ifndef SHADOWS_DISABLED
15241545 // Do shadow and lighting in two passes to reduce register pressure
15251546 uint shadow0 = 0 ;
@@ -1554,7 +1575,7 @@ void main() {
15541575 // Only process the first light's shadow for vertex lighting.
15551576 for (uint i = 0 ; i < 1 ; i++ ) {
15561577#else
1557- for (uint i = 0 ; i < sc_directional_lights() ; i++ ) {
1578+ for (uint i = 0 ; i < directional_lights_count ; i++ ) {
15581579#endif
15591580 if (! bool (directional_lights.data[i].mask & instances.data[draw_call.instance_index].layer_mask)) {
15601581 continue ; // not masked
@@ -1696,7 +1717,8 @@ void main() {
16961717#endif // SHADOWS_DISABLED
16971718
16981719#ifndef USE_VERTEX_LIGHTING
1699- for (uint i = 0 ; i < sc_directional_lights(); i++ ) {
1720+ uint directional_lights_count = sc_directional_lights(scene_data.directional_light_count);
1721+ for (uint i = 0 ; i < directional_lights_count; i++ ) {
17001722 if (! bool (directional_lights.data[i].mask & instances.data[draw_call.instance_index].layer_mask)) {
17011723 continue ; // not masked
17021724 }
@@ -1767,9 +1789,14 @@ void main() {
17671789 } // directional light
17681790
17691791#ifndef USE_VERTEX_LIGHTING
1792+ uint omni_light_count = sc_omni_lights(8 );
17701793 uvec2 omni_indices = instances.data[draw_call.instance_index].omni_lights;
1771- for (uint i = 0 ; i < sc_omni_lights() ; i++ ) {
1794+ for (uint i = 0 ; i < omni_light_count ; i++ ) {
17721795 uint light_index = (i > 3 ) ? ((omni_indices.y >> ((i - 4 ) * 8 )) & 0xFF) : ((omni_indices.x >> (i * 8 )) & 0xFF);
1796+ if (i > 0 && light_index == 0xFF) {
1797+ break ;
1798+ }
1799+
17731800 light_process_omni(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, orms, scene_data.taa_frame_count, albedo, alpha, screen_uv, vec3 (1.0 ),
17741801#ifdef LIGHT_BACKLIGHT_USED
17751802 backlight,
@@ -1795,9 +1822,14 @@ void main() {
17951822 diffuse_light, direct_specular_light);
17961823 }
17971824
1825+ uint spot_light_count = sc_spot_lights(8 );
17981826 uvec2 spot_indices = instances.data[draw_call.instance_index].spot_lights;
1799- for (uint i = 0 ; i < sc_spot_lights() ; i++ ) {
1827+ for (uint i = 0 ; i < spot_light_count ; i++ ) {
18001828 uint light_index = (i > 3 ) ? ((spot_indices.y >> ((i - 4 ) * 8 )) & 0xFF) : ((spot_indices.x >> (i * 8 )) & 0xFF);
1829+ if (i > 0 && light_index == 0xFF) {
1830+ break ;
1831+ }
1832+
18011833 light_process_spot(light_index, vertex, view, normal, vertex_ddx, vertex_ddy, f0, orms, scene_data.taa_frame_count, albedo, alpha, screen_uv, vec3 (1.0 ),
18021834#ifdef LIGHT_BACKLIGHT_USED
18031835 backlight,
0 commit comments