Skip to content

Commit 97d9cc6

Browse files
committed
Create 1 shader per light count. Avoids all pointless iteration within the shaders
1 parent 8b6d03f commit 97d9cc6

1 file changed

Lines changed: 34 additions & 19 deletions

File tree

src/renderer/webgl/pipelines/LightPipeline.js

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,19 @@ var LightPipeline = new Class({
7272
{
7373
LIGHT_COUNT = config.game.renderer.config.maxLights;
7474

75-
config.fragShader = GetFastValue(config, 'fragShader', LightShaderSourceFS).replace('%LIGHT_COUNT%', LIGHT_COUNT.toString());
75+
var fragShader = GetFastValue(config, 'fragShader', LightShaderSourceFS);
76+
77+
var shaders = [];
78+
79+
for (var i = 1; i <= LIGHT_COUNT; i++)
80+
{
81+
shaders.push({
82+
name: 'lights' + i,
83+
fragShader: fragShader.replace('%LIGHT_COUNT%', i.toString())
84+
});
85+
}
86+
87+
config.shaders = shaders;
7688

7789
MultiPipeline.call(this, config);
7890

@@ -125,15 +137,12 @@ var LightPipeline = new Class({
125137
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([ 127, 127, 255, 255 ]));
126138

127139
this.defaultNormalMap = { glTexture: tempTexture };
128-
},
129-
130-
onActive: function ()
131-
{
132-
this.renderer.resetTextures();
133140

134-
this.set1i('uMainSampler', 0);
135-
this.set1i('uNormSampler', 1);
136-
this.set2f('uResolution', this.width / 2, this.height / 2);
141+
// Set the lights shaders
142+
for (var i = 0; i < this.shaders.length; i++)
143+
{
144+
this['lightShader' + (i + 1)] = this.shaders[i];
145+
}
137146
},
138147

139148
/**
@@ -150,28 +159,34 @@ var LightPipeline = new Class({
150159
{
151160
var lightManager = scene.sys.lights;
152161

153-
if (!lightManager || lightManager.lights.length <= 0 || !lightManager.active)
162+
if (!lightManager || !lightManager.active)
154163
{
155164
return;
156165
}
157166

158167
var lights = lightManager.getLights(camera);
168+
var lightsCount = lights.length;
159169

160-
var renderer = this.renderer;
161-
var cameraMatrix = camera.matrix;
162-
var height = renderer.height;
163-
var i;
164-
165-
for (i = 0; i < LIGHT_COUNT; i++)
170+
if (lightsCount === 0)
166171
{
167-
this.set1f('uLights[' + i + '].radius', 0);
172+
return;
168173
}
169174

170-
this.set4f('uCamera', camera.x, camera.y, camera.rotation, camera.zoom);
175+
// Ok, we're good to go ...
176+
this.setShader(this['lightShader' + lightsCount], true);
177+
178+
var i;
179+
var renderer = this.renderer;
180+
var height = renderer.height;
181+
var cameraMatrix = camera.matrix;
171182

183+
this.set1i('uMainSampler', 0);
184+
this.set1i('uNormSampler', 1);
185+
this.set2f('uResolution', this.width / 2, this.height / 2);
186+
this.set4f('uCamera', camera.x, camera.y, camera.rotation, camera.zoom);
172187
this.set3f('uAmbientLightColor', lightManager.ambientColor.r, lightManager.ambientColor.g, lightManager.ambientColor.b);
173188

174-
for (i = 0; i < lights.length; i++)
189+
for (i = 0; i < lightsCount; i++)
175190
{
176191
var light = lights[i].light;
177192
var color = light.color;

0 commit comments

Comments
 (0)