diff options
-rw-r--r-- | src/library_gl.js | 103 | ||||
-rw-r--r-- | src/settings.js | 1 | ||||
-rw-r--r-- | tests/glbook/CH09_SimpleTexture2D.png | bin | 0 -> 1943 bytes | |||
-rw-r--r-- | tests/glbook/Chapter_9/Simple_Texture2D/Simple_Texture2D.c | 52 | ||||
-rw-r--r-- | tests/glbook/Makefile | 2 | ||||
-rwxr-xr-x | tests/runner.py | 5 | ||||
-rw-r--r-- | tools/clean_webconsole.py | 15 |
7 files changed, 107 insertions, 71 deletions
diff --git a/src/library_gl.js b/src/library_gl.js index eddf9b73..d893acf0 100644 --- a/src/library_gl.js +++ b/src/library_gl.js @@ -1,15 +1,7 @@ //"use strict"; // FIXME: -// * glGetUniformLocation should return -1 when the value is not valid, not null -// * glUniform1fi should be glUniform1iv -// * glGetAttribLocation lacks return value (and should be -1 when not valid) // * single-underscore deps need double underscore (and, just auto-add them all) -// * glGetProgramInfoLog and *shader* should be essentially identical -// * glGetIntegerv set to bool etc needs fixing -// * glVertexAttribPointer - the last param can be an offset or in gles, a raw pointer to clientside data. need some way to -// warn about that since it silently fails in WebGL (it's a huge offset into the last bound buffer, zeros) - var LibraryGL = { $GL: { @@ -81,7 +73,7 @@ var LibraryGL = { {{{ makeSetValue('p', '0', 'result', 'i32') }}}; break; case "boolean": - {{{ makeSetValue('p', '0', 'result ? 1 : 0', 'i32') }}}; + {{{ makeSetValue('p', '0', 'result ? 1 : 0', 'i8') }}}; break; case "string": throw 'Native code calling glGetIntegerv(' + name_ + ') on a name which returns a string!'; @@ -350,8 +342,7 @@ var LibraryGL = { }, glBufferData: function(target, size, data, usage) { - var floatArray = new Float32Array(TypedArray_copy(data, size)); - Module.ctx.bufferData(target, floatArray, usage); + Module.ctx.bufferData(target, HEAPU8.subarray(data, data+size), usage); }, glBufferSubData: function(target, offset, size, data) { @@ -406,8 +397,9 @@ var LibraryGL = { glGetUniformLocation__deps: ['$GL'], glGetUniformLocation: function(program, name) { name = Pointer_stringify(name); - return GL.hashtable("uniform").add( - Module.ctx.getUniformLocation(GL.hashtable("program").get(program), name)); + var loc = Module.ctx.getUniformLocation(GL.hashtable("program").get(program), name); + if (!loc) return -1; + return GL.hashtable("uniform").add(loc); }, glUniform1f__deps: ['$GL'], @@ -489,37 +481,6 @@ var LibraryGL = { Module.ctx.uniform4fv(Location, value); }, - glUniform1fi__deps: ['$GL'], - glUniform1fi: function(Location, count, value) { - Location = GL.hashtable("uniform").get(Location); - value = new Uint32Array(TypedArray_copy(value, count*4)); // TODO: optimize - Module.ctx.uniform1fi(Location, value); - }, - - glUniform2fi__deps: ['$GL'], - glUniform2fi: function(Location, count, value) { - Location = GL.hashtable("uniform").get(Location); - count *= 2; - value = new Uint32Array(TypedArray_copy(value, count*4)); // TODO: optimize - Module.ctx.uniform2fi(Location, value); - }, - - glUniform3fi__deps: ['$GL'], - glUniform3fi: function(Location, count, value) { - Location = GL.hashtable("uniform").get(Location); - count *= 3; - value = new Uint32Array(TypedArray_copy(value, count*4)); // TODO: optimize - Module.ctx.uniform3fi(Location, value); - }, - - glUniform4fi__deps: ['$GL'], - glUniform4fi: function(Location, count, value) { - Location = GL.hashtable("uniform").get(Location); - count *= 4; - value = new Uint32Array(TypedArray_copy(value, count*4)); // TODO: optimize - Module.ctx.uniform4fi(Location, value); - }, - glUniformMatrix2fv__deps: ['$GL'], glUniformMatrix2fv: function(Location, count, transpose, value) { Location = GL.hashtable("uniform").get(Location); @@ -573,7 +534,7 @@ var LibraryGL = { glGetAttribLocation: function(program, name) { program = GL.hashtable("program").get(program); name = Pointer_stringify(name); - Module.ctx.getAttribLocation(program, name); + return Module.ctx.getAttribLocation(program, name); }, glCreateShader__deps: ['$GL'], @@ -644,7 +605,11 @@ var LibraryGL = { glGetShaderInfoLog__deps: ['$GL'], glGetShaderInfoLog: function(shader, maxLength, length, infoLog) { var log = Module.ctx.getShaderInfoLog(GL.hashtable("shader").get(shader)); - log.slice(0, maxLength - 1); + // Work around a bug in Chromium which causes getShaderInfoLog to return null + if (!log) { + log = ""; + } + log = log.substr(0, maxLength - 1); writeStringToMemory(log, infoLog); if (length) { {{{ makeSetValue('length', '0', 'log.length', 'i32') }}} @@ -791,6 +756,7 @@ var LibraryGL = { // Simple pass-through functions [[0, 'shadeModel fogi fogfv getError finish flush'], [1, 'clearDepth depthFunc enable disable frontFace cullFace clear enableVertexAttribArray disableVertexAttribArray lineWidth clearStencil depthMask stencilMask stencilMaskSeparate checkFramebufferStatus generateMipmap activeTexture'], + [2, 'pixelStorei'], [3, 'texParameteri texParameterf drawArrays vertexAttrib2f'], [4, 'viewport clearColor scissor vertexAttrib3f colorMask drawElements renderbufferStorage'], [5, 'vertexAttrib4f'], @@ -802,11 +768,8 @@ var LibraryGL = { var stub = '(function(' + args + ') { ' + (num > 0 ? 'Module.ctx.NAME(' + args + ')' : '') + ' })'; names.split(' ').forEach(function(name_) { var cName = 'gl' + name_[0].toUpperCase() + name_.substr(1); -#if ASSERTIONS assert(!(cName in LibraryGL), "Cannot reimplement the existing function " + cName); -#endif LibraryGL[cName] = eval(stub.replace('NAME', name_)); - //print(cName + ': ' + LibraryGL[cName]); }); }); @@ -1047,7 +1010,49 @@ var LibraryGLUT = { try { var ctx = Module.canvas.getContext('experimental-webgl'); if (!ctx) throw 'Could not create canvas :('; +#if GL_DEBUG + var wrapper = {}; + wrapper.objectMap = new WeakMap(); + wrapper.objectCounter = 1; + for (var prop in ctx) { + (function(prop) { + switch (typeof ctx[prop]) { + case 'function': { + wrapper[prop] = function() { + var printArgs = Array.prototype.slice.call(arguments).map(function(arg) { + if (wrapper.objectMap[arg]) return '<' + arg + '|' + wrapper.objectMap[arg] + '>'; + return arg; + }); + Module.printErr('[gl_f:' + prop + ':' + printArgs + ']'); + var ret = ctx[prop].apply(ctx, arguments); + var printRet = ret; + if (typeof ret == 'object') { + wrapper.objectMap[ret] = wrapper.objectCounter++; + printRet = '<' + ret + '|' + wrapper.objectMap[ret] + '>'; + } + Module.printErr('[ gl:' + prop + ':return:' + printRet + ']'); + return ret; + } + break; + } + case 'number': case 'string': { + wrapper.__defineGetter__(prop, function() { + //Module.printErr('[gl_g:' + prop + ':' + ctx[prop] + ']'); + return ctx[prop]; + }); + wrapper.__defineSetter__(prop, function(value) { + Module.printErr('[gl_s:' + prop + ':' + value + ']'); + ctx[prop] = value; + }); + break; + } + } + })(prop); + } + Module.ctx = wrapper; +#else Module.ctx = ctx; +#endif // Set the background of the canvas to black, because glut gives us a // window which has a black background by default. Module.canvas.style.backgroundColor = "black"; diff --git a/src/settings.js b/src/settings.js index 15c92176..901812b6 100644 --- a/src/settings.js +++ b/src/settings.js @@ -101,6 +101,7 @@ var SAFE_HEAP_LOG = 0; // Log out all SAFE_HEAP operations var LABEL_DEBUG = 0; // Print out labels and functions as we enter them var EXCEPTION_DEBUG = 1; // Print out exceptions in emscriptened code var LIBRARY_DEBUG = 0; // Print out when we enter a library call (library*.js) +var GL_DEBUG = 0; // Print out all calls into WebGL var DISABLE_EXCEPTION_CATCHING = 0; // Disables generating code to actually catch exceptions. If the code you // are compiling does not actually rely on catching exceptions (but the // compiler generates code for it, maybe because of stdlibc++ stuff), diff --git a/tests/glbook/CH09_SimpleTexture2D.png b/tests/glbook/CH09_SimpleTexture2D.png Binary files differnew file mode 100644 index 00000000..e33539e9 --- /dev/null +++ b/tests/glbook/CH09_SimpleTexture2D.png diff --git a/tests/glbook/Chapter_9/Simple_Texture2D/Simple_Texture2D.c b/tests/glbook/Chapter_9/Simple_Texture2D/Simple_Texture2D.c index cc465c9a..5276bda4 100644 --- a/tests/glbook/Chapter_9/Simple_Texture2D/Simple_Texture2D.c +++ b/tests/glbook/Chapter_9/Simple_Texture2D/Simple_Texture2D.c @@ -32,6 +32,8 @@ typedef struct // Texture handle GLuint textureId; + GLuint vertexObject, indexObject; + } UserData; /// @@ -111,7 +113,27 @@ int Init ( ESContext *esContext ) // Load the texture userData->textureId = CreateSimpleTexture2D (); - glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); + // Setup the vertex data + GLfloat vVertices[] = { -0.5, 0.5, 0.0, // Position 0 + 0.0, 0.0, // TexCoord 0 + -0.5, -0.5, 0.0, // Position 1 + 0.0, 1.0, // TexCoord 1 + 0.5, -0.5, 0.0, // Position 2 + 1.0, 1.0, // TexCoord 2 + 0.5, 0.5, 0.0, // Position 3 + 1.0, 0.0 // TexCoord 3 + }; + GLushort indices[] = { 0, 1, 2, 0, 2, 3 }; + + glGenBuffers(1, &userData->vertexObject); + glBindBuffer(GL_ARRAY_BUFFER, userData->vertexObject ); + glBufferData(GL_ARRAY_BUFFER, sizeof(vVertices), vVertices, GL_STATIC_DRAW ); + + glGenBuffers(1, &userData->indexObject); + glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, userData->indexObject ); + glBufferData ( GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW ); + + glClearColor ( 0.0f, 0.0f, 0.0f, 1.0f ); return GL_TRUE; } @@ -119,34 +141,26 @@ int Init ( ESContext *esContext ) // Draw a triangle using the shader pair created in Init() // void Draw ( ESContext *esContext ) -{ - UserData *userData = esContext->userData; - GLfloat vVertices[] = { -0.5f, 0.5f, 0.0f, // Position 0 - 0.0f, 0.0f, // TexCoord 0 - -0.5f, -0.5f, 0.0f, // Position 1 - 0.0f, 1.0f, // TexCoord 1 - 0.5f, -0.5f, 0.0f, // Position 2 - 1.0f, 1.0f, // TexCoord 2 - 0.5f, 0.5f, 0.0f, // Position 3 - 1.0f, 0.0f // TexCoord 3 - }; - GLushort indices[] = { 0, 1, 2, 0, 2, 3 }; - +{ // Set the viewport glViewport ( 0, 0, esContext->width, esContext->height ); // Clear the color buffer glClear ( GL_COLOR_BUFFER_BIT ); + UserData *userData = esContext->userData; + // Use the program object glUseProgram ( userData->programObject ); // Load the vertex position - glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT, - GL_FALSE, 5 * sizeof(GLfloat), vVertices ); + glBindBuffer (GL_ARRAY_BUFFER, userData->vertexObject ); + glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT, + GL_FALSE, 5 * 4, 0 ); // Load the texture coordinate glVertexAttribPointer ( userData->texCoordLoc, 2, GL_FLOAT, - GL_FALSE, 5 * sizeof(GLfloat), &vVertices[3] ); + GL_FALSE, 5 * 4, + 3 * 4 ); glEnableVertexAttribArray ( userData->positionLoc ); glEnableVertexAttribArray ( userData->texCoordLoc ); @@ -158,8 +172,8 @@ void Draw ( ESContext *esContext ) // Set the sampler texture unit to 0 glUniform1i ( userData->samplerLoc, 0 ); - glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices ); - + glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, userData->indexObject ); + glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0 ); } /// diff --git a/tests/glbook/Makefile b/tests/glbook/Makefile index 386d5717..6220fc3c 100644 --- a/tests/glbook/Makefile +++ b/tests/glbook/Makefile @@ -24,7 +24,7 @@ default: all all: ./Chapter_2/Hello_Triangle/CH02_HelloTriangle.bc \ ./Chapter_8/Simple_VertexShader/CH08_SimpleVertexShader.bc \ -# ./Chapter_9/Simple_Texture2D/CH09_SimpleTexture2D.bc \ + ./Chapter_9/Simple_Texture2D/CH09_SimpleTexture2D.bc \ # ./Chapter_9/MipMap2D/CH09_MipMap2D.bc \ # ./Chapter_9/Simple_TextureCubemap/CH09_TextureCubemap.bc \ # ./Chapter_9/TextureWrap/CH09_TextureWrap.bc \ diff --git a/tests/runner.py b/tests/runner.py index 93c50b20..c340d3a5 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -6878,8 +6878,9 @@ elif 'browser' in str(sys.argv): def test_glbook(self): programs = self.get_library('glbook', [ - os.path.join('Chapter_2/Hello_Triangle/CH02_HelloTriangle.bc'), - os.path.join('Chapter_8/Simple_VertexShader/CH08_SimpleVertexShader.bc') + os.path.join('Chapter_2', 'Hello_Triangle', 'CH02_HelloTriangle.bc'), + os.path.join('Chapter_8', 'Simple_VertexShader', 'CH08_SimpleVertexShader.bc'), + os.path.join('Chapter_9/Simple_Texture2D/CH09_SimpleTexture2D.bc'), ], configure=None) for program in programs: print program diff --git a/tools/clean_webconsole.py b/tools/clean_webconsole.py new file mode 100644 index 00000000..550ace9a --- /dev/null +++ b/tools/clean_webconsole.py @@ -0,0 +1,15 @@ +''' +Removes timestamp and line info from a webgl log +''' + +import os, sys + +lines = sys.stdin.read().split('\n') + +for line in lines: + if line.startswith('['): + line = line[15:] + if line.startswith(('[', ']')): + line = line.split('@')[0] + print line + |