Skip to content

Commit 82a1709

Browse files
committed
wip: set NUM_LIGHT_LAYERS as uint from the start
1 parent fcd71f7 commit 82a1709

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

src/engine/renderer/gl_shader.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,11 @@ static void AddConst( std::string& str, const std::string& name, int value )
489489
str += Str::Format("const int %s = %d;\n", name, value);
490490
}
491491

492+
static void AddConst( std::string& str, const std::string& name, uint32_t value )
493+
{
494+
str += Str::Format("const uint %s = %uu;\n", name, value);
495+
}
496+
492497
static void AddConst( std::string& str, const std::string& name, float value )
493498
{
494499
str += Str::Format("const float %s = %.8e;\n", name, value);
@@ -667,7 +672,9 @@ static std::string GenEngineConstants() {
667672
AddDefine( str, "M_PI", static_cast< float >( M_PI ) );
668673
AddDefine( str, "MAX_SHADOWMAPS", MAX_SHADOWMAPS );
669674
AddDefine( str, "MAX_REF_LIGHTS", MAX_REF_LIGHTS );
670-
AddDefine( str, "NUM_LIGHT_LAYERS", glConfig2.realtimeLightLayers );
675+
676+
AddConst( str, "NUM_LIGHT_LAYERS", (uint32_t) glConfig2.realtimeLightLayers );
677+
671678
AddDefine( str, "TILE_SIZE", TILE_SIZE );
672679

673680
AddDefine( str, "r_FBufSize", glConfig.vidWidth, glConfig.vidHeight );

src/engine/renderer/glsl_source/computeLight_fp.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ void computeDynamicLights( vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse, vec4
225225

226226
vec2 tile = floor( gl_FragCoord.xy * ( 1.0 / float( TILE_SIZE ) ) ) + 0.5;
227227

228-
for( uint layer = 0u; layer < uint( NUM_LIGHT_LAYERS ); layer++ ) {
228+
for( uint layer = 0u; layer < NUM_LIGHT_LAYERS; layer++ ) {
229229
uint lightCount = 0u;
230230
idxs_t idxs = fetchIdxs( tileScale * vec3( tile, float( layer ) + 0.5 ), u_LightTiles );
231231

@@ -238,7 +238,7 @@ void computeDynamicLights( vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse, vec4
238238

239239
/* Light IDs are stored relative to the layer
240240
Subtract 1 because 0 means there's no light */
241-
idx = ( idx - 1u ) * uint( NUM_LIGHT_LAYERS ) + layer;
241+
idx = ( idx - 1u ) * NUM_LIGHT_LAYERS + layer;
242242

243243
computeDynamicLight( idx, P, normal, viewDir, diffuse, material, color );
244244
lightCount++;

src/engine/renderer/glsl_source/lighttile_fp.glsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void main() {
120120
only process 1 / 4 of different lights for each layer, extra lights going into the last layer. This can fail to add some lights
121121
if 1 / 4 of all lights is more than the amount of lights that each layer can hold (16). To fix this, we'd need to either do this on CPU
122122
or use compute shaders with atomics so we can have a variable amount of lights for each tile. */
123-
for( uint i = uint( u_lightLayer ); i < uint( u_numLights ); i += uint( NUM_LIGHT_LAYERS ) ) {
123+
for( uint i = uint( u_lightLayer ); i < uint( u_numLights ); i += NUM_LIGHT_LAYERS ) {
124124
Light l = GetLight( i );
125125
vec3 center = ( u_ModelMatrix * vec4( l.center, 1.0 ) ).xyz;
126126
float radius = max( 2.0 * l.radius, 2.0 * 32.0 ); // Avoid artifacts with weak light sources
@@ -136,7 +136,7 @@ void main() {
136136
if( radius > 0.0 ) {
137137
/* Light IDs are stored relative to the layer
138138
Add 1 because 0 means there's no light */
139-
pushIdxs( ( i / uint( NUM_LIGHT_LAYERS ) ) + 1u, lightCount, idxs );
139+
pushIdxs( ( i / NUM_LIGHT_LAYERS ) + 1u, lightCount, idxs );
140140
lightCount++;
141141

142142
if( lightCount == lightsPerLayer ) {

0 commit comments

Comments
 (0)