aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2012-05-07 18:45:05 -0700
committerAlon Zakai <alonzakai@gmail.com>2012-05-07 18:45:05 -0700
commit053bb451f0056274b931dc2178ba00eec57e2477 (patch)
tree50f4fdca967014f319eb309ad99498c59af4b8bf
parent65e50a4c0c12a0f2d881123eca13094a88765445 (diff)
make test_cubegeom_mt a proper test of multitexturing
-rw-r--r--src/library_gl.js56
-rw-r--r--tests/cubegeom_mt.c42
-rwxr-xr-xtests/runner.py4
3 files changed, 62 insertions, 40 deletions
diff --git a/src/library_gl.js b/src/library_gl.js
index ab31ca83..3dac8ae3 100644
--- a/src/library_gl.js
+++ b/src/library_gl.js
@@ -1295,7 +1295,9 @@ var LibraryGL = {
},
createRenderer: function(renderer) {
- var vertexSize = 0, positionSize = 0, positionOffset = 0, textureSize = 0, textureOffset = 0, colorSize = 0, colorOffset = 0, normalSize = 0, normalOffset = 0, which, size;
+ var vertexSize = 0, positionSize = 0, positionOffset = 0, colorSize = 0, colorOffset = 0, normalSize = 0, normalOffset = 0;
+ var textureSizes = [], textureOffsets = [], textureTypes = [], hasTextures = false;
+ var which, size, index;
for (var i = 0; i < renderer.length; i+=2) {
var which = renderer[i];
if (which == 'V') {
@@ -1307,16 +1309,17 @@ var LibraryGL = {
index = parseInt(renderer[i+1])
size = parseInt(renderer[i+2]);
i++;
- if (index == 0) { // TODO: support other textures
- textureSize = size;
- textureOffset = vertexSize;
- }
+ textureSizes[index] = size;
+ textureOffsets[index] = vertexSize;
if (renderer[i+2] == 's') { // special case: half-size texture
i++;
- vertexSize += size * 2; // short
+ vertexSize += size * 2;
+ textureTypes[index] = Module.ctx.SHORT;
} else {
- vertexSize += size * 4; // float
+ vertexSize += size * 4;
+ textureTypes[index] = Module.ctx.FLOAT;
}
+ hasTextures = true;
} else if (which == 'N') {
size = parseInt(renderer[i+1]);
normalSize = size;
@@ -1354,7 +1357,7 @@ var LibraryGL = {
var zero = positionSize == 2 ? '0, ' : '';
Module.ctx.shaderSource(this.vertexShader, 'attribute vec' + positionSize + ' a_position; \n' +
'attribute vec2 a_texCoord0; \n' +
- (textureSize ? 'varying vec2 v_texCoord; \n' : '') +
+ (hasTextures ? 'varying vec2 v_texCoord; \n' : '') +
'varying vec4 v_color; \n' +
(colorSize ? 'attribute vec4 a_color; \n': 'uniform vec4 u_color; \n') +
'uniform mat4 u_modelView; \n' +
@@ -1362,7 +1365,7 @@ var LibraryGL = {
'void main() \n' +
'{ \n' +
' gl_Position = u_projection * (u_modelView * vec4(a_position, ' + zero + '1.0)); \n' +
- (textureSize ? 'v_texCoord = a_texCoord0; \n' : '') +
+ (hasTextures ? 'v_texCoord = a_texCoord0; \n' : '') +
(colorSize ? 'v_color = a_color; \n' : 'v_color = u_color; \n') +
'} \n');
Module.ctx.compileShader(this.vertexShader);
@@ -1374,7 +1377,7 @@ var LibraryGL = {
'varying vec4 v_color; \n' +
'void main() \n' +
'{ \n' +
- (textureSize ? 'gl_FragColor = v_color * texture2D( u_texture, v_texCoord );\n' :
+ (hasTextures ? 'gl_FragColor = v_color * texture2D( u_texture, v_texCoord );\n' :
'gl_FragColor = v_color;\n') +
'} \n');
Module.ctx.compileShader(this.fragmentShader);
@@ -1386,20 +1389,27 @@ var LibraryGL = {
}
this.positionLocation = Module.ctx.getAttribLocation(this.program, 'a_position');
- this.texCoordLocation = Module.ctx.getAttribLocation(this.program, 'a_texCoord0');
+ this.texCoordLocations = [];
+ for (var i = 0; i < textureSizes.length; i++) {
+ if (textureSizes[i]) {
+ this.texCoordLocations[i] = Module.ctx.getAttribLocation(this.program, 'a_texCoord' + i);
+ }
+ }
this.colorLocation = Module.ctx.getAttribLocation(this.program, 'a_color');
this.normalLocation = Module.ctx.getAttribLocation(this.program, 'a_normal');
- this.textureLocation = Module.ctx.getUniformLocation(this.program, 'u_texture');
+ this.textureLocation = Module.ctx.getUniformLocation(this.program, 'u_texture'); // only for immediate mode with no shaders, so only one is enough
this.modelViewLocation = Module.ctx.getUniformLocation(this.program, 'u_modelView');
this.projectionLocation = Module.ctx.getUniformLocation(this.program, 'u_projection');
this.hasColorAttribLocation = Module.ctx.getUniformLocation(this.program, 'u_hasColorAttrib');
this.colorUniformLocation = Module.ctx.getUniformLocation(this.program, 'u_color');
- this.hasTexture = textureSize > 0 && this.texCoordLocation >= 0;
+ this.hasTextures = hasTextures;
this.hasColorAttrib = colorSize > 0 && this.colorLocation >= 0;
this.hasColorUniform = !!this.colorUniformLocation;
this.hasNormal = this.normalLocation >= 0;
+
+ this.floatType = Module.ctx.FLOAT; // minor optimization
},
prepare: function() {
@@ -1409,10 +1419,14 @@ var LibraryGL = {
Module.ctx.vertexAttribPointer(this.positionLocation, positionSize, Module.ctx.FLOAT, false,
vertexSize, positionOffset);
Module.ctx.enableVertexAttribArray(this.positionLocation);
- if (this.hasTexture) {
- Module.ctx.vertexAttribPointer(this.texCoordLocation, textureSize, Module.ctx.FLOAT, false,
- vertexSize, textureOffset);
- Module.ctx.enableVertexAttribArray(this.texCoordLocation);
+ if (this.hasTextures) {
+ for (var i = 0; i < textureSizes.length; i++) {
+ if (textureSizes[i] && this.texCoordLocations[i] >= 0) {
+ Module.ctx.vertexAttribPointer(this.texCoordLocations[i], textureSizes[i], textureTypes[i], false,
+ vertexSize, textureOffsets[i]);
+ Module.ctx.enableVertexAttribArray(this.texCoordLocations[i]);
+ }
+ }
}
if (this.hasColorAttrib) {
Module.ctx.vertexAttribPointer(this.colorLocation, colorSize, Module.ctx.UNSIGNED_BYTE, true,
@@ -1438,8 +1452,12 @@ var LibraryGL = {
cleanup: function() {
Module.ctx.disableVertexAttribArray(this.positionLocation);
- if (this.hasTexture) {
- Module.ctx.disableVertexAttribArray(this.texCoordLocation);
+ if (this.hasTextures) {
+ for (var i = 0; i < textureSizes.length; i++) {
+ if (textureSizes[i] && this.texCoordLocations[i] >= 0) {
+ Module.ctx.disableVertexAttribArray(this.texCoordLocations[i]);
+ }
+ }
}
if (this.hasColorAttrib) {
Module.ctx.disableVertexAttribArray(this.colorLocation);
diff --git a/tests/cubegeom_mt.c b/tests/cubegeom_mt.c
index 43aa4b38..464de7cc 100644
--- a/tests/cubegeom_mt.c
+++ b/tests/cubegeom_mt.c
@@ -71,7 +71,7 @@ int main(int argc, char *argv[])
GLubyte textureData[16*16*4];
for (int x = 0; x < 16; x++) {
for (int y = 0; y < 16; y++) {
- *((int*)&textureData[(x*16 + y) * 4]) = x*16 + ((y*16) << 8);
+ *((int*)&textureData[(x*16 + y) * 4]) = x*16 + ((y*16) << 8) + ((x*y) << 16);
}
}
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0,
@@ -84,10 +84,10 @@ int main(int argc, char *argv[])
glBindTexture( GL_TEXTURE_2D, texture2 );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
- GLubyte texture2Data[] = { 0xff, 0, 0, 0xff,
- 0, 0xff, 0, 0xaa,
- 0, 0, 0xff, 0x55,
- 0x80, 0x90, 0x70, 0 };
+ GLubyte texture2Data[] = { 0x00, 0x00, 0xff, 0x77,
+ 0x00, 0x44, 0xaa, 0x77,
+ 0x44, 0x00, 0xcc, 0x77,
+ 0x00, 0x44, 0x77, 0x77 };
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0,
GL_RGBA, GL_UNSIGNED_BYTE, texture2Data );
@@ -138,21 +138,21 @@ int main(int argc, char *argv[])
[0, 0, 128, 68] ==> 1024 float
[vertex x ] [vertex y ] [vertex z ] [nr] [texture u ] [texture v ] [lm u ] [lm v ] [color r,g,b,a ] */
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 0
- 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 1
- 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 2
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 128, 128, 128, 128, // 0
+ 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 0, 1, 0, 1, 128, 128, 128, 128, // 1
+ 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 1, 128, 128, 128, 128, // 2
0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 3
- 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 4
- 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 5
- 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 6
+ 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 0, 1, 0, 0, 128, 128, 128, 128, // 4
+ 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 0, 0, 1, 0, 1, 128, 128, 128, 128, // 5
+ 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 67, 0, 0, 0, 1, 128, 128, 128, 128, // 6
0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 7
- 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 8
- 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 9
- 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 10
+ 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 1, 0, 0, 128, 128, 128, 128, // 8
+ 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 1, 0, 1, 128, 128, 128, 128, // 9
+ 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 128, 67, 0, 0, 0, 1, 128, 128, 128, 128, // 10
0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 11
- 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 12
- 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 13
- 0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 14
+ 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 1, 0, 0, 128, 128, 128, 128, // 12
+ 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 67, 0, 1, 0, 1, 128, 128, 128, 128, // 13
+ 0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 128, 67, 0, 0, 0, 1, 128, 128, 128, 128, // 14
0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 15
0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128,
@@ -200,11 +200,15 @@ int main(int argc, char *argv[])
// sauer vertex data is apparently 0-12: V3F, 12: N1B, 16-24: T2F, 24-28: T2S, 28-32: C4B
glVertexPointer(3, GL_FLOAT, 32, (void*)0); // all these apply to the ARRAY_BUFFER that is bound
+
+ glClientActiveTexture(GL_TEXTURE1);
+ glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 32, (void*)16);
- glClientActiveTexture(GL_TEXTURE1); // XXX seems to be ignored in native build
+
+ glClientActiveTexture(GL_TEXTURE0);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_SHORT, 32, (void*)24);
- glClientActiveTexture(GL_TEXTURE0); // likely not needed, it is a cleanup
+
glNormalPointer(GL_BYTE, 32, (void*)12);
glColorPointer(4, GL_UNSIGNED_BYTE, 32, (void*)28);
diff --git a/tests/runner.py b/tests/runner.py
index 9c4c8e1f..2dbcb760 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -7252,7 +7252,7 @@ elif 'browser' in str(sys.argv):
self.btest('cubegeom_pre.c', expected='-1472804742')
def test_cubegeom_pre2(self):
- self.btest('cubegeom_pre2.c', expected='-1472804742', args=['-s', 'GL_DEBUG=1'])
+ self.btest('cubegeom_pre2.c', expected='-1472804742', args=['-s', 'GL_DEBUG=1']) # some coverage for GL_DEBUG not breaking the build
def test_cubegeom_pre3(self):
self.btest('cubegeom_pre3.c', expected='-1472804742')
@@ -7267,7 +7267,7 @@ elif 'browser' in str(sys.argv):
self.btest('cubegeom_normal.c', expected='752917084')
def test_cubegeom_mt(self):
- self.btest('cubegeom_mt.c', expected='188641320')
+ self.btest('cubegeom_mt.c', expected='-457159152') # multitexture
elif 'benchmark' in str(sys.argv):
# Benchmarks. Run them with argument |benchmark|. To run a specific test, do