Skip to content

Commit 6e35817

Browse files
committed
Fixed issue with multiple "texture not bound to unit X" webgl warning
1 parent 3e1595f commit 6e35817

4 files changed

Lines changed: 25 additions & 16 deletions

File tree

src/pixi/renderers/webgl/shaders/PixiFastShader.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,17 @@ PIXI.PixiFastShader.prototype.init = function () {
125125
this.uSampler = gl.getUniformLocation(program, 'uSamplerArray[0]');
126126

127127
var indices = [];
128+
// HACK: we bind an empty texture to avoid WebGL warning spam.
129+
var tempTexture = gl.createTexture();
130+
gl.activeTexture(gl.TEXTURE0);
131+
gl.bindTexture(gl.TEXTURE_2D, tempTexture);
132+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 1, 1, 0, gl.RGB, gl.UNSIGNED_BYTE, null);
128133
for (var i = 0; i < this.MAX_TEXTURES; ++i) {
134+
gl.activeTexture(gl.TEXTURE0 + i);
135+
gl.bindTexture(gl.TEXTURE_2D, tempTexture);
129136
indices.push(i);
130137
}
131-
// NOTE:!!!
132-
// If textures are not bound
133-
// then we'll get a bunch of warnings like:
134-
// "WARNING: there is no texture bound to the unit X"
135-
// Don't be scared, everything will be alright.
138+
gl.activeTexture(gl.TEXTURE0);
136139
gl.uniform1iv(this.uSampler, indices);
137140

138141
this.projectionVector = gl.getUniformLocation(program, 'projectionVector');

src/pixi/renderers/webgl/shaders/PixiShader.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,17 @@ PIXI.PixiShader.prototype.init = function()
121121
this.aTextureIndex = gl.getAttribLocation(program, 'aTextureIndex');
122122

123123
var indices = [];
124+
// HACK: we bind an empty texture to avoid WebGL warning spam.
125+
var tempTexture = gl.createTexture();
126+
gl.activeTexture(gl.TEXTURE0);
127+
gl.bindTexture(gl.TEXTURE_2D, tempTexture);
128+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 1, 1, 0, gl.RGB, gl.UNSIGNED_BYTE, null);
124129
for (var i = 0; i < this.MAX_TEXTURES; ++i) {
130+
gl.activeTexture(gl.TEXTURE0 + i);
131+
gl.bindTexture(gl.TEXTURE_2D, tempTexture);
125132
indices.push(i);
126133
}
127-
// NOTE:!!!
128-
// If textures are not bound
129-
// then we'll get a bunch of warnings like:
130-
// "WARNING: there is no texture bound to the unit X"
131-
// Don't be scared, everything will be alright.
134+
gl.activeTexture(gl.TEXTURE0);
132135
gl.uniform1iv(this.uSamplerArray, indices);
133136

134137
// Begin worst hack eva //

src/pixi/renderers/webgl/shaders/StripShader.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,17 @@ PIXI.StripShader.prototype.init = function()
111111
this.uSampler = gl.getUniformLocation(program, 'uSamplerArray[0]');
112112

113113
var indices = [];
114+
// HACK: we bind an empty texture to avoid WebGL warning spam.
115+
var tempTexture = gl.createTexture();
116+
gl.activeTexture(gl.TEXTURE0);
117+
gl.bindTexture(gl.TEXTURE_2D, tempTexture);
118+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 1, 1, 0, gl.RGB, gl.UNSIGNED_BYTE, null);
114119
for (var i = 0; i < this.MAX_TEXTURES; ++i) {
120+
gl.activeTexture(gl.TEXTURE0 + i);
121+
gl.bindTexture(gl.TEXTURE_2D, tempTexture);
115122
indices.push(i);
116123
}
117-
// NOTE:!!!
118-
// If textures are not bound
119-
// then we'll get a bunch of warnings like:
120-
// "WARNING: there is no texture bound to the unit X"
121-
// Don't be scared, everything will be alright.
124+
gl.activeTexture(gl.TEXTURE0);
122125
gl.uniform1iv(this.uSampler, indices);
123126

124127
this.projectionVector = gl.getUniformLocation(program, 'projectionVector');

src/pixi/renderers/webgl/utils/WebGLSpriteBatch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,10 @@ PIXI.WebGLSpriteBatch.prototype.render = function (sprite, matrix) {
225225
var baseTexture = texture.baseTexture;
226226
var gl = this.gl;
227227
if (this.textureArray[baseTexture.textureIndex] != baseTexture) {
228+
this.flush();
228229
gl.activeTexture(gl.TEXTURE0 + baseTexture.textureIndex);
229230
gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[gl.id]);
230231
this.textureArray[baseTexture.textureIndex] = baseTexture;
231-
this.flush();
232232
}
233233

234234
// They provided an alternative rendering matrix, so use it

0 commit comments

Comments
 (0)