diff options
30 files changed, 1642 insertions, 73 deletions
@@ -273,6 +273,11 @@ Options that are modified or new in %s include: -v to Clang, and also enable EMCC_DEBUG to details emcc's operations + --remove-duplicates If set, will remove duplicate symbols when + linking. This can be useful because + llvm-link's behavior is not as permissive + as ld is. + The target file, if specified (-o <target>), defines what will be generated: @@ -336,11 +341,10 @@ ASSEMBLY_SUFFIXES = ('.ll',) LIB_PREFIXES = ('', 'lib') seen_names = {} -def unsuffixed_uniquename(name): - ret = unsuffixed_basename(name) +def uniquename(name): if name not in seen_names: seen_names[name] = str(len(seen_names)) - return ret + '_' + seen_names[name] + return unsuffixed(name) + '_' + seen_names[name] + (('.' + suffix(name)) if suffix(name) else '') # ---------------- End configs ------------- @@ -395,7 +399,7 @@ else: temp_dir = tempfile.mkdtemp() def in_temp(name): - return os.path.join(temp_dir, name) + return os.path.join(temp_dir, os.path.basename(name)) try: call = CXX if use_cxx else CC @@ -418,6 +422,7 @@ try: ignore_dynamic_linking = False shell_path = shared.path_from_root('src', 'shell.html') js_libraries = [] + remove_duplicates = False def check_bad_eq(arg): assert '=' not in arg, 'Invalid parameter (do not use "=" with "--" options)' @@ -504,6 +509,9 @@ try: js_libraries.append(newargs[i+1]) newargs[i] = '' newargs[i+1] = '' + elif newargs[i] == '--remove-duplicates': + remove_duplicates = True + newargs[i] = '' newargs = [ arg for arg in newargs if arg is not '' ] if llvm_opts is None: llvm_opts = LLVM_OPT_LEVEL[opt_level] @@ -575,6 +583,8 @@ try: libs.append(arg[2:]) newargs[i] = '' + original_input_files = input_files[:] + newargs = [ arg for arg in newargs if arg is not '' ] # Find library files @@ -640,7 +650,7 @@ try: for input_file in input_files: if input_file.endswith(SOURCE_SUFFIXES): if DEBUG: print >> sys.stderr, 'emcc: compiling source file: ', input_file - output_file = in_temp(unsuffixed_uniquename(input_file) + '.o') + output_file = in_temp(unsuffixed(uniquename(input_file)) + '.o') temp_files.append(output_file) args = newargs + ['-emit-llvm', '-c', input_file, '-o', output_file] if DEBUG: print >> sys.stderr, "emcc running:", call, ' '.join(args) @@ -651,12 +661,12 @@ try: else: # bitcode if input_file.endswith(BITCODE_SUFFIXES): if DEBUG: print >> sys.stderr, 'emcc: copying bitcode file: ', input_file - temp_file = in_temp(unsuffixed_uniquename(input_file) + '.o') + temp_file = in_temp(unsuffixed(uniquename(input_file)) + '.o') shutil.copyfile(input_file, temp_file) temp_files.append(temp_file) elif input_file.endswith(DYNAMICLIB_SUFFIXES) or shared.Building.is_ar(input_file): if DEBUG: print >> sys.stderr, 'emcc: copying library file: ', input_file - temp_file = in_temp(os.path.basename(input_file)) + temp_file = in_temp(uniquename(input_file)) shutil.copyfile(input_file, temp_file) temp_files.append(temp_file) else: #.ll @@ -664,7 +674,7 @@ try: # Note that by assembling the .ll file, then disassembling it later, we will # remove annotations which is a good thing for compilation time if DEBUG: print >> sys.stderr, 'emcc: assembling assembly file: ', input_file - temp_file = in_temp(unsuffixed_uniquename(input_file) + '.o') + temp_file = in_temp(unsuffixed(uniquename(input_file)) + '.o') shared.Building.llvm_as(input_file, temp_file) temp_files.append(temp_file) @@ -676,16 +686,16 @@ try: print >> sys.stderr, 'emcc: warning: -Ox flags ignored, since not generating JavaScript' if not specified_target: for input_file in input_files: - shutil.move(in_temp(unsuffixed_uniquename(input_file) + '.o'), unsuffixed_basename(input_file) + '.' + final_suffix) + shutil.move(in_temp(unsuffixed(uniquename(input_file)) + '.o'), unsuffixed_basename(input_file) + '.' + final_suffix) else: if len(input_files) == 1: - shutil.move(in_temp(unsuffixed_uniquename(input_files[0]) + '.o'), specified_target) + shutil.move(in_temp(unsuffixed(uniquename(input_files[0])) + '.o'), specified_target) else: - assert not has_dash_c, 'fatal error: cannot specify -o with -c with multiple files' + str(sys.argv) + assert len(original_input_files) == 1 or not has_dash_c, 'fatal error: cannot specify -o with -c with multiple files' + str(sys.argv) + ':' + str(original_input_files) # We have a specified target (-o <target>), which is not JavaScript or HTML, and # we have multiple files: Link them if DEBUG: print >> sys.stderr, 'emcc: link: ' + str(temp_files) - shared.Building.link(temp_files, specified_target) + shared.Building.link(temp_files, specified_target, remove_duplicates=remove_duplicates) exit(0) ## Continue on to create JavaScript @@ -777,8 +787,7 @@ try: (not LEAVE_INPUTS_RAW and not (suffix(temp_files[0]) in BITCODE_SUFFIXES or suffix(temp_files[0]) in DYNAMICLIB_SUFFIXES) and shared.Building.is_ar(temp_files[0])): linker_inputs = temp_files + extra_files_to_link if DEBUG: print >> sys.stderr, 'emcc: linking: ', linker_inputs - shared.Building.link(linker_inputs, - in_temp(target_basename + '.bc')) + shared.Building.link(linker_inputs, in_temp(target_basename + '.bc'), remove_duplicates=remove_duplicates) final = in_temp(target_basename + '.bc') else: if not LEAVE_INPUTS_RAW: diff --git a/src/library.js b/src/library.js index cb94fccb..b9c13055 100644 --- a/src/library.js +++ b/src/library.js @@ -1495,7 +1495,7 @@ LibraryManager.library = { lseek: function(fildes, offset, whence) { // off_t lseek(int fildes, off_t offset, int whence); // http://pubs.opengroup.org/onlinepubs/000095399/functions/lseek.html - if (FS.streams[fildes] && !FS.streams[fildes].isDevice) { + if (FS.streams[fildes] && !FS.streams[fildes].object.isDevice) { var stream = FS.streams[fildes]; var position = offset; if (whence === 1) { // SEEK_CUR. diff --git a/src/library_browser.js b/src/library_browser.js index 9283913f..6015168f 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -86,6 +86,7 @@ mergeInto(LibraryManager.library, { requestFullScreen: function() { var canvas = Module.canvas; function fullScreenChange() { + if (Module['onFullScreen']) Module['onFullScreen'](); if (document['webkitFullScreenElement'] === canvas || document['mozFullScreenElement'] === canvas || document['fullScreenElement'] === canvas) { @@ -142,24 +143,55 @@ mergeInto(LibraryManager.library, { 0; }, - asyncLoad: function(url, callback) { + xhrLoad: function(url, onload, onerror) { var xhr = new XMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'arraybuffer'; xhr.onload = function() { - var arrayBuffer = xhr.response; + if (xhr.status == 200) { + onload(xhr.response); + } else { + onerror(); + } + }; + xhr.onerror = onerror; + xhr.send(null); + }, + + asyncLoad: function(url, callback) { + Browser.xhrLoad(url, function(arrayBuffer) { assert(arrayBuffer, 'Loading data file "' + url + '" failed (no arrayBuffer).'); callback(new Uint8Array(arrayBuffer)); removeRunDependency(); - }; - xhr.onerror = function(event) { - assert(arrayBuffer, 'Loading data file "' + url + '" failed.'); - }; - xhr.send(null); + }, function(event) { + throw 'Loading data file "' + url + '" failed.'; + }); addRunDependency(); } }, + emscripten_async_wget: function(url, file, onload, onerror) { + url = Pointer_stringify(url); + + Browser.xhrLoad(url, function(response) { + var absolute = Pointer_stringify(file); + var index = absolute.lastIndexOf('/'); + FS.createDataFile( + absolute.substr(0, index), + absolute.substr(index +1), + new Uint8Array(response), + true, true); + + if (onload) { + FUNCTION_TABLE[onload](file); + } + }, function(event) { + if (onerror) { + FUNCTION_TABLE[onerror](file); + } + }); + }, + emscripten_async_run_script__deps: ['emscripten_run_script'], emscripten_async_run_script: function(script, millis) { Module['noExitRuntime'] = true; diff --git a/src/library_gl.js b/src/library_gl.js index 68f8248b..53e587de 100644 --- a/src/library_gl.js +++ b/src/library_gl.js @@ -23,7 +23,7 @@ var LibraryGL = { unpackAlignment: 4, // default alignment is 4 bytes init: function() { - Browser.moduleContextCreatedCallbacks.push(GL.initCompression); + Browser.moduleContextCreatedCallbacks.push(GL.initExtensions); }, // Linear lookup in one of the tables (buffers, programs, etc.). TODO: consider using a weakmap to make this faster, if it matters @@ -107,14 +107,17 @@ var LibraryGL = { ((height - 1) * alignedRowSize + plainRowSize); }, - initCompression: function() { - if (GL.initCompression.done) return; - GL.initCompression.done = true; + initExtensions: function() { + if (GL.initExtensions.done) return; + GL.initExtensions.done = true; - var ext = Module.ctx.getExtension('WEBGL_compressed_texture_s3tc') || - Module.ctx.getExtension('MOZ_WEBGL_compressed_texture_s3tc') || - Module.ctx.getExtension('WEBKIT_WEBGL_compressed_texture_s3tc'); - if (!ext) Module.printErr('Failed to get texture compression WebGL extension, if compressed textures are used they will fail'); + GL.compressionExt = Module.ctx.getExtension('WEBGL_compressed_texture_s3tc') || + Module.ctx.getExtension('MOZ_WEBGL_compressed_texture_s3tc') || + Module.ctx.getExtension('WEBKIT_WEBGL_compressed_texture_s3tc'); + + GL.anisotropicExt = Module.ctx.getExtension('EXT_texture_filter_anisotropic') || + Module.ctx.getExtension('MOZ_EXT_texture_filter_anisotropic') || + Module.ctx.getExtension('WEBKIT_EXT_texture_filter_anisotropic'); } }, @@ -289,21 +292,23 @@ var LibraryGL = { }, glCompressedTexImage2D: function(target, level, internalformat, width, height, border, imageSize, data) { + assert(GL.compressionExt); if (data) { data = {{{ makeHEAPView('U8', 'data', 'data+imageSize') }}}; } else { data = null; } - Module.ctx.compressedTexImage2D(target, level, internalformat, width, height, border, data); + Module.ctx['compressedTexImage2D'](target, level, internalformat, width, height, border, data); }, glCompressedTexSubImage2D: function(target, level, xoffset, yoffset, width, height, format, imageSize, data) { + assert(GL.compressionExt); if (data) { data = {{{ makeHEAPView('U8', 'data', 'data+imageSize') }}}; } else { data = null; } - Module.ctx.compressedTexSubImage2D(target, level, xoffset, yoffset, width, height, data); + Module.ctx['compressedTexSubImage2D'](target, level, xoffset, yoffset, width, height, data); }, glTexImage2D: function(target, level, internalformat, width, height, border, format, type, pixels) { @@ -909,7 +914,10 @@ var LibraryGL = { // Fog support. Partial, we assume shaders are used that implement fog. We just pass them uniforms fogStart: 0, fogEnd: 1, + fogDensity: 1.0, fogColor: null, + fogMode: 0x0800, // GL_EXP + fogEnabled: false, init: function() { GLEmulation.fogColor = new Float32Array(4); @@ -917,29 +925,48 @@ var LibraryGL = { // Add some emulation workarounds Module.printErr('WARNING: using emscripten GL emulation. This is a collection of limited workarounds, do not expect it to work'); - // XXX some of these ignored capabilities may lead to incorrect rendering, if we do not emulate them in shaders - var ignoredCapabilities = { - 0x0B20: 1, // GL_LINE_SMOOTH - 0x0B60: 1, // GL_FOG - 0x0BA1: 1, // GL_NORMALIZE - 0x0C60: 1, // GL_TEXTURE_GEN_S - 0x0C61: 1, // GL_TEXTURE_GEN_T - 0x0DE1: 1, // GL_TEXTURE_2D - 0x8513: 1, // GL_TEXTURE_CUBE_MAP - 0x2A02: 1 // GL_POLYGON_OFFSET_LINE + // XXX some of the capabilities we don't support may lead to incorrect rendering, if we do not emulate them in shaders + var validCapabilities = { + 0x0B44: 1, // GL_CULL_FACE + 0x0BE2: 1, // GL_BLEND + 0x0BD0: 1, // GL_DITHER, + 0x0B90: 1, // GL_STENCIL_TEST + 0x0B71: 1, // GL_DEPTH_TEST + 0x0C11: 1, // GL_SCISSOR_TEST + 0x8037: 1, // GL_POLYGON_OFFSET_FILL + 0x809E: 1, // GL_SAMPLE_ALPHA_TO_COVERAGE + 0x80A0: 1 // GL_SAMPLE_COVERAGE }; _glEnable = function(cap) { // Clean up the renderer on any change to the rendering state. The optimization of // skipping renderer setup is aimed at the case of multiple glDraw* right after each other if (GL.immediate.lastRenderer) GL.immediate.lastRenderer.cleanup(); - if (cap in ignoredCapabilities) return; + if (cap == 0x0B60 /* GL_FOG */) { + GLEmulation.fogEnabled = true; + return; + } else if (!(cap in validCapabilities)) { + return; + } Module.ctx.enable(cap); }; _glDisable = function(cap) { if (GL.immediate.lastRenderer) GL.immediate.lastRenderer.cleanup(); - if (cap in ignoredCapabilities) return; + if (cap == 0x0B60 /* GL_FOG */) { + GLEmulation.fogEnabled = false; + return; + } else if (!(cap in validCapabilities)) { + return; + } Module.ctx.disable(cap); }; + _glIsEnabled = function(cap) { + if (cap == 0x0B60 /* GL_FOG */) { + return GLEmulation.fogEnabled ? 1 : 0; + } else if (!(cap in validCapabilities)) { + return 0; + } + return Module.ctx.isEnabled(cap); + }; var glGetIntegerv = _glGetIntegerv; _glGetIntegerv = function(pname, params) { @@ -969,7 +996,11 @@ var LibraryGL = { _glGetString = function(name_) { switch(name_) { case 0x1F03 /* GL_EXTENSIONS */: // Add various extensions that we can support - return allocate(intArrayFromString(Module.ctx.getSupportedExtensions().join(' ') + ' GL_EXT_texture_env_combine GL_ARB_texture_env_crossbar GL_ATI_texture_env_combine3 GL_NV_texture_env_combine4 GL_EXT_texture_env_dot3 GL_ARB_multitexture GL_ARB_vertex_buffer_object GL_EXT_framebuffer_object GL_ARB_vertex_program GL_ARB_fragment_program GL_ARB_shading_language_100 GL_ARB_shader_objects GL_ARB_vertex_shader GL_ARB_fragment_shader GL_ARB_texture_cube_map GL_EXT_draw_range_elements GL_ARB_texture_compression GL_EXT_texture_compression_s3tc'), 'i8', ALLOC_NORMAL); + return allocate(intArrayFromString(Module.ctx.getSupportedExtensions().join(' ') + + ' GL_EXT_texture_env_combine GL_ARB_texture_env_crossbar GL_ATI_texture_env_combine3 GL_NV_texture_env_combine4 GL_EXT_texture_env_dot3 GL_ARB_multitexture GL_ARB_vertex_buffer_object GL_EXT_framebuffer_object GL_ARB_vertex_program GL_ARB_fragment_program GL_ARB_shading_language_100 GL_ARB_shader_objects GL_ARB_vertex_shader GL_ARB_fragment_shader GL_ARB_texture_cube_map GL_EXT_draw_range_elements' + + (GL.compressionExt ? ' GL_ARB_texture_compression GL_EXT_texture_compression_s3tc' : '') + + (GL.anisotropicExt ? ' GL_EXT_texture_filter_anisotropic' : '') + ), 'i8', ALLOC_NORMAL); } return glGetString(name_); }; @@ -1089,6 +1120,10 @@ var LibraryGL = { source = 'uniform float u_fogScale; \n' + source.replace(/gl_Fog.scale/g, 'u_fogScale'); } + if (source.indexOf('gl_Fog.density') >= 0) { + source = 'uniform float u_fogDensity; \n' + + source.replace(/gl_Fog.density/g, 'u_fogDensity'); + } if (source.indexOf('gl_FogFragCoord') >= 0) { source = 'varying float v_fogFragCoord; \n' + source.replace(/gl_FogFragCoord/g, 'v_fogFragCoord'); @@ -1182,6 +1217,10 @@ var LibraryGL = { {{{ makeSetValue('params', '0', 'GLEmulation.fogStart', 'float') }}}; } else if (pname == 0x0B64) { // GL_FOG_END {{{ makeSetValue('params', '0', 'GLEmulation.fogEnd', 'float') }}}; + } else if (pname == 0x0B62) { // GL_FOG_DENSITY + {{{ makeSetValue('params', '0', 'GLEmulation.fogDensity', 'float') }}}; + } else if (pname == 0x0B65) { // GL_FOG_MODE + {{{ makeSetValue('params', '0', 'GLEmulation.fogMode', 'float') }}}; } else { glGetFloatv(pname, params); } @@ -1474,7 +1513,14 @@ var LibraryGL = { if (!cacheItem[attribute.type]) cacheItem[attribute.type] = {}; cacheItem = cacheItem[attribute.type]; } - if (GL.currProgram) { + if (GLEmulation.fogEnabled) { + var fogParam = GLEmulation.fogMode; + } else { + var fogParam = 0; // all valid modes are non-zero + } + if (!cacheItem[fogParam]) cacheItem[fogParam] = {}; + cacheItem = cacheItem[fogParam]; + if (GL.currProgram) { // Note the order here; this one is last, and optional if (!cacheItem[GL.currProgram]) cacheItem[GL.currProgram] = {}; cacheItem = cacheItem[GL.currProgram]; } @@ -1526,20 +1572,43 @@ var LibraryGL = { } this.program = GL.programs[GL.currProgram]; } else { + // IMPORTANT NOTE: If you parameterize the shader source based on any runtime values + // in order to create the least expensive shader possible based on the features being + // used, you should also update the code in the beginning of getRenderer to make sure + // that you cache the renderer based on the said parameters. this.vertexShader = Module.ctx.createShader(Module.ctx.VERTEX_SHADER); var zero = positionSize == 2 ? '0, ' : ''; + if (GLEmulation.fogEnabled) { + switch (GLEmulation.fogMode) { + case 0x0801: // GL_EXP2 + // fog = exp(-(gl_Fog.density * gl_FogFragCoord)^2) + var fogFormula = ' float fog = exp(-u_fogDensity * u_fogDensity * ecDistance * ecDistance); \n'; + break; + case 0x2601: // GL_LINEAR + // fog = (gl_Fog.end - gl_FogFragCoord) * gl_fog.scale + var fogFormula = ' float fog = (u_fogEnd - ecDistance) * u_fogScale; \n'; + break; + default: // default to GL_EXP + // fog = exp(-gl_Fog.density * gl_FogFragCoord) + var fogFormula = ' float fog = exp(-u_fogDensity * ecDistance); \n'; + break; + } + } Module.ctx.shaderSource(this.vertexShader, 'attribute vec' + positionSize + ' a_position; \n' + 'attribute vec2 a_texCoord0; \n' + (hasTextures ? 'varying vec2 v_texCoord; \n' : '') + 'varying vec4 v_color; \n' + (colorSize ? 'attribute vec4 a_color; \n': 'uniform vec4 u_color; \n') + + (GLEmulation.fogEnabled ? 'varying float v_fogFragCoord; \n' : '') + 'uniform mat4 u_modelView; \n' + 'uniform mat4 u_projection; \n' + 'void main() \n' + '{ \n' + - ' gl_Position = u_projection * (u_modelView * vec4(a_position, ' + zero + '1.0)); \n' + + ' vec4 ecPosition = (u_modelView * vec4(a_position, ' + zero + '1.0)); \n' + // eye-coordinate position + ' gl_Position = u_projection * ecPosition; \n' + (hasTextures ? 'v_texCoord = a_texCoord0; \n' : '') + (colorSize ? 'v_color = a_color; \n' : 'v_color = u_color; \n') + + (GLEmulation.fogEnabled ? 'v_fogFragCoord = abs(ecPosition.z);\n' : '') + '} \n'); Module.ctx.compileShader(this.vertexShader); @@ -1548,10 +1617,23 @@ var LibraryGL = { 'varying vec2 v_texCoord; \n' + 'uniform sampler2D u_texture; \n' + 'varying vec4 v_color; \n' + + (GLEmulation.fogEnabled ? ( + 'varying float v_fogFragCoord; \n' + + 'uniform vec4 u_fogColor; \n' + + 'uniform float u_fogEnd; \n' + + 'uniform float u_fogScale; \n' + + 'uniform float u_fogDensity; \n' + + 'float ffog(in float ecDistance) { \n' + + fogFormula + + ' fog = clamp(fog, 0.0, 1.0); \n' + + ' return fog; \n' + + '} \n' + ) : '') + 'void main() \n' + '{ \n' + (hasTextures ? 'gl_FragColor = v_color * texture2D( u_texture, v_texCoord );\n' : 'gl_FragColor = v_color;\n') + + (GLEmulation.fogEnabled ? 'gl_FragColor = vec4(mix(u_fogColor.rgb, gl_FragColor.rgb, ffog(v_fogFragCoord)), gl_FragColor.a); \n' : '') + '} \n'); Module.ctx.compileShader(this.fragmentShader); @@ -1591,7 +1673,9 @@ var LibraryGL = { this.fogColorLocation = Module.ctx.getUniformLocation(this.program, 'u_fogColor'); this.fogEndLocation = Module.ctx.getUniformLocation(this.program, 'u_fogEnd'); this.fogScaleLocation = Module.ctx.getUniformLocation(this.program, 'u_fogScale'); - this.hasFog = !!(this.fogColorLocation || this.fogEndLocation || this.fogScaleLocation); + this.fogDensityLocation = Module.ctx.getUniformLocation(this.program, 'u_fogDensity'); + this.hasFog = !!(this.fogColorLocation || this.fogEndLocation || + this.fogScaleLocation || this.fogDensityLocation); }, prepare: function() { @@ -1678,6 +1762,7 @@ var LibraryGL = { if (this.fogColorLocation) Module.ctx.uniform4fv(this.fogColorLocation, GLEmulation.fogColor); if (this.fogEndLocation) Module.ctx.uniform1f(this.fogEndLocation, GLEmulation.fogEnd); if (this.fogScaleLocation) Module.ctx.uniform1f(this.fogScaleLocation, 1/(GLEmulation.fogEnd - GLEmulation.fogStart)); + if (this.fogDensityLocation) Module.ctx.uniform1f(this.fogDensityLocation, GLEmulation.fogDensity); } }, @@ -1996,7 +2081,7 @@ var LibraryGL = { }, glColor4us__deps: ['glColor4f'], glColor4us: function(r, g, b, a) { - _glColor4f((r&65525)/65535, (g&65525)/65535, (b&65525)/65535, (a&65525)/65535); + _glColor4f((r&65535)/65535, (g&65535)/65535, (b&65535)/65535, (a&65535)/65535); }, glColor4ui__deps: ['glColor4f'], glColor4ui: function(r, g, b, a) { @@ -2047,10 +2132,24 @@ var LibraryGL = { GLEmulation.fogStart = param; break; case 0x0B64: // GL_FOG_END GLEmulation.fogEnd = param; break; + case 0x0B62: // GL_FOG_DENSITY + GLEmulation.fogDensity = param; break; + case 0x0B65: // GL_FOG_MODE + switch (param) { + case 0x0801: // GL_EXP2 + case 0x2601: // GL_LINEAR + GLEmulation.fogMode = param; break; + default: // default to GL_EXP + GLEmulation.fogMode = 0x0800 /* GL_EXP */; break; + } + break; } }, - glFogi: function(){}, // TODO - glFogx: function(){}, // TODO + glFogi__deps: ['glFogf'], + glFogi: function(pname, param) { + return _glFogf(pname, param); + }, + glFogfv__deps: ['glFogf'], glFogfv: function(pname, param) { // partial support, TODO switch(pname) { case 0x0B66: // GL_FOG_COLOR @@ -2059,8 +2158,26 @@ var LibraryGL = { GLEmulation.fogColor[2] = {{{ makeGetValue('param', '8', 'float') }}}; GLEmulation.fogColor[3] = {{{ makeGetValue('param', '12', 'float') }}}; break; + case 0x0B63: // GL_FOG_START + case 0x0B64: // GL_FOG_END + _glFogf(pname, {{{ makeGetValue('param', '0', 'float') }}}); break; + } + }, + glFogiv__deps: ['glFogf'], + glFogiv: function(pname, param) { + switch(pname) { + case 0x0B66: // GL_FOG_COLOR + GLEmulation.fogColor[0] = ({{{ makeGetValue('param', '0', 'i32') }}}/2147483647)/2.0+0.5; + GLEmulation.fogColor[1] = ({{{ makeGetValue('param', '4', 'i32') }}}/2147483647)/2.0+0.5; + GLEmulation.fogColor[2] = ({{{ makeGetValue('param', '8', 'i32') }}}/2147483647)/2.0+0.5; + GLEmulation.fogColor[3] = ({{{ makeGetValue('param', '12', 'i32') }}}/2147483647)/2.0+0.5; + break; + default: + _glFogf(pname, {{{ makeGetValue('param', '0', 'i32') }}}); break; } }, + glFogx: 'glFogi', + glFogxv: 'glFogiv', glPolygonMode: function(){}, // TODO diff --git a/src/library_sdl.js b/src/library_sdl.js index 41898056..88649c38 100644 --- a/src/library_sdl.js +++ b/src/library_sdl.js @@ -23,6 +23,10 @@ var LibrarySDL = { audio: null, volume: 1.0 }, + mixerFrequency: 22050, + mixerFormat: 0x8010, // AUDIO_S16LSB + mixerNumChannels: 2, + mixerChunkSize: 1024, keyboardState: null, shiftKey: false, @@ -1065,6 +1069,10 @@ var LibrarySDL = { Mix_OpenAudio: function(frequency, format, channels, chunksize) { SDL.allocateChannels(32); + SDL.mixerFrequency = frequency; + SDL.mixerFormat = format; + SDL.mixerNumChannels = channels; + SDL.mixerChunkSize = chunksize; return 0; }, @@ -1108,13 +1116,32 @@ var LibrarySDL = { return id; }, + Mix_QuickLoad_RAW: function(mem, len) { + var audio = new Audio(); + audio['mozSetup'](SDL.mixerNumChannels, SDL.mixerFrequency); + var numSamples = (len / (SDL.mixerNumChannels * 2)) | 0; + var buffer = new Float32Array(numSamples); + for (var i = 0; i < numSamples; ++i) { + buffer[i] = ({{{ makeGetValue('mem', 'i*2', 'i16', 0, 0) }}}) / 0x8000; // hardcoded 16-bit audio, signed (TODO: reSign if not ta2?) + } + var id = SDL.audios.length; + SDL.audios.push({ + source: '', + audio: audio, + buffer: buffer + }); + return id; + }, + Mix_FreeChunk: function(id) { SDL.audios[id] = null; }, Mix_PlayChannel: function(channel, id, loops) { // TODO: handle loops - var audio = SDL.audios[id].audio; + var info = SDL.audios[id]; + if (!info) return 0; + var audio = info.audio; if (!audio) return 0; if (channel == -1) { channel = 0; @@ -1125,15 +1152,20 @@ var LibrarySDL = { } } } - var info = SDL.channels[channel]; - info.audio = audio.cloneNode(true); + var channelInfo = SDL.channels[channel]; + channelInfo.audio = audio = audio.cloneNode(true); if (SDL.channelFinished) { - info.audio['onended'] = function() { // TODO: cache these + audio['onended'] = function() { // TODO: cache these Runtime.getFuncWrapper(SDL.channelFinished)(channel); } } - info.audio.play(); - info.audio.volume = info.volume; + if (info.buffer) { + audio['mozSetup'](SDL.mixerNumChannels, SDL.mixerFrequency); + audio["mozWriteAudio"](info.buffer); + } else { + audio.play(); + } + audio.volume = channelInfo.volume; return channel; }, Mix_PlayChannelTimed: 'Mix_PlayChannel', // XXX ignore Timing @@ -1176,7 +1208,11 @@ var LibrarySDL = { var audio = SDL.audios[id].audio; if (!audio) return 0; audio.loop = loops != 1; // TODO: handle N loops for finite N - audio.play(); + if (SDL.audios[id].buffer) { + audio["mozWriteAudio"](SDL.audios[id].buffer); + } else { + audio.play(); + } audio.volume = SDL.music.volume; audio['onended'] = _Mix_HaltMusic; // will send callback SDL.music.audio = audio; diff --git a/system/include/emscripten/emscripten.h b/system/include/emscripten/emscripten.h index 48ccd341..5b71ce6a 100644 --- a/system/include/emscripten/emscripten.h +++ b/system/include/emscripten/emscripten.h @@ -103,6 +103,18 @@ float emscripten_random(); //extern void EMSCRIPTEN_COMMENT(const char *text); /* + * Emscripten file system api + */ + +/* + * Load file from url in asynchronous way. + * When file is loaded then 'onload' callback will called. + * If any error occurred 'onerror' will called. + * The callbacks are called with the file as their argument. + */ +void emscripten_async_wget(const char* url, const char* file, void (*onload)(const char*), void (*onerror)(const char*)); + +/* * Profiling tools. * INIT must be called first, with the maximum identifier that * will be used. BEGIN will add some code that marks diff --git a/system/include/libc/sys/_default_fcntl.h b/system/include/libc/sys/_default_fcntl.h index 0f2ffb07..188e25c4 100644 --- a/system/include/libc/sys/_default_fcntl.h +++ b/system/include/libc/sys/_default_fcntl.h @@ -209,6 +209,11 @@ extern int _open64 _PARAMS ((const char *, int, ...)); #define POSIX_FADV_DONTNEED 135 int posix_fadvise(int fd, off_t offset, off_t len, int advice); int posix_fallocate(int fd, off_t offset, off_t len); +#define LOCK_SH 1 +#define LOCK_EX 2 +#define LOCK_UN 4 +#define LOCK_NB 8 +int flock(int fd, int operation); #ifdef __cplusplus } diff --git a/system/include/libc/sys/dirent.h b/system/include/libc/sys/dirent.h index 1fbe2b21..9dcf34d1 100644 --- a/system/include/libc/sys/dirent.h +++ b/system/include/libc/sys/dirent.h @@ -26,6 +26,10 @@ long telldir(DIR *); DIR *readdir(DIR *); int closedir(DIR *dirp); void rewinddir(DIR *dirp); +int scandir(const char *dirp, + struct dirent ***namelist, + int (*filter)(const struct dirent *), + int (*compar)(const struct dirent **, const struct dirent **)); enum { DT_UNKNOWN = 0, diff --git a/system/include/sys/statvfs.h b/system/include/sys/statvfs.h index cf0a8c96..192be153 100644 --- a/system/include/sys/statvfs.h +++ b/system/include/sys/statvfs.h @@ -20,7 +20,7 @@ struct statvfs { int f_namemax; }; -int statvfs(char *path, struct statvfs *s); +int statvfs(const char *path, struct statvfs *s); #ifdef __cplusplus } diff --git a/tests/aniso.c b/tests/aniso.c new file mode 100644 index 00000000..f2735f49 --- /dev/null +++ b/tests/aniso.c @@ -0,0 +1,189 @@ +/******************************************************************* + * * + * Using SDL With OpenGL * + * * + * Tutorial by Kyle Foley (sdw) * + * * + * http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL * + * * + *******************************************************************/ + +/* +THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION +AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN. + +THE ORIGINAL AUTHOR IS KYLE FOLEY. + +THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY +OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF +MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, +ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE +RESULTING FROM THE USE, MODIFICATION, OR +REDISTRIBUTION OF THIS SOFTWARE. +*/ + +#include "SDL/SDL.h" +#include "SDL/SDL_image.h" +#include "SDL/SDL_opengl.h" + +#include <stdio.h> +#include <string.h> +#include <assert.h> + +#define MAX(x, y) ((x) > (y) ? (x) : (y)) + +int hasext(const char *exts, const char *ext) // from cube2, zlib licensed +{ + int len = strlen(ext); + if(len) for(const char *cur = exts; (cur = strstr(cur, ext)); cur += len) + { + if((cur == exts || cur[-1] == ' ') && (cur[len] == ' ' || !cur[len])) return 1; + } + return 0; +} + +int main(int argc, char *argv[]) +{ + SDL_Surface *screen; + + // Slightly different SDL initialization + if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) { + printf("Unable to initialize SDL: %s\n", SDL_GetError()); + return 1; + } + + SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new* + + screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed* + if ( !screen ) { + printf("Unable to set video mode: %s\n", SDL_GetError()); + return 1; + } + + // Check extensions + + const char *exts = (const char *)glGetString(GL_EXTENSIONS); + assert(hasext(exts, "GL_EXT_texture_filter_anisotropic")); + + GLint aniso; + glGetIntegerv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &aniso); + printf("Max anisotropy: %d\n", aniso); + assert(aniso >= 4); + + // Set the OpenGL state after creating the context with SDL_SetVideoMode + + glClearColor( 0, 0, 0, 0 ); + +#if !EMSCRIPTEN + glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL +#endif + + glViewport( 0, 0, 800, 600 ); + + glMatrixMode( GL_PROJECTION ); + GLfloat matrixData[] = { 2.0/640, 0, 0, 0, + 0, -2.0/480, 0, 0, + 0, 0, -2.0/480, 0, + -1, 1, 0, 1 }; + glLoadMatrixf(matrixData); // test loadmatrix + + glMatrixMode( GL_MODELVIEW ); + glLoadIdentity(); + + + // Load the OpenGL texture + + GLuint texture, texture2; + + const int DDS_SIZE = 43920; + FILE *dds = fopen("water.dds", "rb"); + assert(dds); + char *ddsdata = (char*)malloc(DDS_SIZE); + assert(fread(ddsdata, 1, DDS_SIZE, dds) == DDS_SIZE); + fclose(dds); + + { + glGenTextures( 1, &texture ); + glBindTexture( GL_TEXTURE_2D, texture ); + + char *curr = ddsdata + 128; + int level = 0; + int w = 512; + int h = 64; + while (level < 5) { + printf("uploading level %d: %d, %d\n", level, w, h); + assert(!glGetError()); + glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, curr); + assert(!glGetError()); + curr += MAX(w, 4)*MAX(h, 4); + w /= 2; + h /= 2; + level++; + } + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + } + { + glGenTextures( 1, &texture2 ); + glBindTexture( GL_TEXTURE_2D, texture2 ); + + char *curr = ddsdata + 128; + int level = 0; + int w = 512; + int h = 64; + while (level < 5) { + printf("uploading level %d: %d, %d\n", level, w, h); + assert(!glGetError()); + glCompressedTexImage2D(GL_TEXTURE_2D, level, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, w, h, 0, w*h, curr); + assert(!glGetError()); + curr += MAX(w, 4)*MAX(h, 4); + w /= 2; + h /= 2; + level++; + } + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 4); + } + + // Prepare and Render + + // Clear the screen before drawing + glClear( GL_COLOR_BUFFER_BIT ); + + // Bind the texture to which subsequent calls refer to + int w = 8; + int n = 20; + for (int x = 0; x < n; x++) { + for (int y = 0; y < n*2; y++) { + glBindTexture( GL_TEXTURE_2D, texture ); + glBegin( GL_TRIANGLE_STRIP ); + glTexCoord2i( 0, 0 ); glVertex3f( x*w, y*(w), 0 ); + glTexCoord2i( 1, 0 ); glVertex3f( (x+1)*(w-2*y/n), y*(w), 0 ); + glTexCoord2i( 1, 1 ); glVertex3f( (x+1)*(w-2*y/n), (y+1)*(w), 0 ); + glTexCoord2i( 0, 1 ); glVertex3f( x*w, (y+1)*(w), 0 ); + glEnd(); + glBindTexture( GL_TEXTURE_2D, texture2 ); + glBegin( GL_TRIANGLE_STRIP ); + glTexCoord2i( 0, 0 ); glVertex3f( n*w + x*w, y*(w), 0 ); + glTexCoord2i( 1, 0 ); glVertex3f( n*w + (x+1)*(w-2*y/n), y*(w), 0 ); + glTexCoord2i( 1, 1 ); glVertex3f( n*w + (x+1)*(w-2*y/n), (y+1)*(w), 0 ); + glTexCoord2i( 0, 1 ); glVertex3f( n*w + x*w, (y+1)*(w), 0 ); + glEnd(); + } + } + + SDL_GL_SwapBuffers(); + +#if !EMSCRIPTEN + // Wait for 3 seconds to give us a chance to see the image + SDL_Delay(10000); +#endif + + // Now we can delete the OpenGL texture and close down SDL + glDeleteTextures( 1, &texture ); + + SDL_Quit(); + + return 0; +} diff --git a/tests/emscripten_fs_api_browser.cpp b/tests/emscripten_fs_api_browser.cpp new file mode 100644 index 00000000..07469f34 --- /dev/null +++ b/tests/emscripten_fs_api_browser.cpp @@ -0,0 +1,69 @@ +#include<stdio.h> +#include<emscripten.h> +#include<assert.h> +#include <string.h> + +extern "C" { + +int result = 1; +int get_count = 0; + +void wait_wgets() { + if (get_count == 2) { + emscripten_cancel_main_loop(); + REPORT_RESULT(); + } +} + +void onLoaded(const char* file) { + if (strcmp(file, "/tmp/test.html")) { + result = 0; + } + + printf("loaded: %s\n", file); + + if (FILE * f = fopen(file, "r")) { + printf("exists: %s\n", file); + int c = fgetc (f); + if (c == EOF) { + printf("file empty: %s\n", file); + result = 0; + } + fclose(f); + } else { + result = 0; + printf("!exists: %s\n", file); + } + + get_count++; +} + +void onError(const char* file) { + if (strcmp(file, "/tmp/null")) { + result = 0; + } + + printf("error: %s\n", file); + get_count++; +} + +int main() { + emscripten_async_wget( + "http://localhost:8888/this_is_not_a_file", + "/tmp/null", + onLoaded, + onError); + + emscripten_async_wget( + "http://localhost:8888/test.html", + "/tmp/test.html", + onLoaded, + onError); + + emscripten_set_main_loop(wait_wgets, 0); + + return 0; +} + +} + diff --git a/tests/fcntl/src.c b/tests/fcntl/src.c index 5b40ec79..c8c71c8a 100644 --- a/tests/fcntl/src.c +++ b/tests/fcntl/src.c @@ -45,7 +45,7 @@ int main() { printf("\n"); errno = 0; - flock lk; + struct flock lk; lk.l_type = 42; printf("F_GETLK: %d\n", fcntl(f, F_GETLK, &lk)); printf("errno: %d\n", errno); diff --git a/tests/runner.py b/tests/runner.py index d840465d..852928ef 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -6863,6 +6863,30 @@ f.close() Popen(['python', EMCC, os.path.join(self.get_dir(), 'foo', 'main.o'), os.path.join(self.get_dir(), 'bar', 'main.o')]).communicate() self.assertContained('hello there', run_js(os.path.join(self.get_dir(), 'a.out.js'))) + def test_remove_duplicates(self): + # can happen with .a files. we do a best-effort, removing dupes + open(os.path.join(self.get_dir(), 'main.cpp'), 'w').write(''' + #include<stdio.h> + void printey() { printf("bye bye\\n"); } + int main() { + printey(); + return 0; + } + ''') + open(os.path.join(self.get_dir(), 'side.cpp'), 'w').write(''' + #include<stdio.h> + void printey() { printf("bye bye\\n"); } + ''') + + # without --remove-duplicates, we fail + err = Popen(['python', EMCC, os.path.join(self.get_dir(), 'main.cpp'), os.path.join(self.get_dir(), 'side.cpp')], stderr=PIPE).communicate()[1] + assert not os.path.exists('a.out.js') + assert 'multiply' in err + + # with it, we succeed + err = Popen(['python', EMCC, os.path.join(self.get_dir(), 'main.cpp'), os.path.join(self.get_dir(), 'side.cpp'), '--remove-duplicates'], stderr=PIPE).communicate()[1] + self.assertContained('bye bye', run_js(os.path.join(self.get_dir(), 'a.out.js'))) + def test_embed_file(self): open(os.path.join(self.get_dir(), 'somefile.txt'), 'w').write('''hello from a file with lots of data and stuff in it thank you very much''') open(os.path.join(self.get_dir(), 'main.cpp'), 'w').write(r''' @@ -7625,6 +7649,13 @@ elif 'browser' in str(sys.argv): Popen(['python', EMCC, '-O2', '--minify', '0', os.path.join(self.get_dir(), 'sdl_audio.c'), '--preload-file', 'sound.ogg', '--preload-file', 'sound2.wav', '-o', 'page.html', '-s', 'EXPORTED_FUNCTIONS=["_main", "_play", "_play2"]']).communicate() self.run_browser('page.html', '', '/report_result?1') + def test_sdl_audio_quickload(self): + open(os.path.join(self.get_dir(), 'sdl_audio_quickload.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_audio_quickload.c')).read())) + + # use closure to check for a possible bug with closure minifying away newer Audio() attributes + Popen(['python', EMCC, '-O2', '--minify', '0', os.path.join(self.get_dir(), 'sdl_audio_quickload.c'), '-o', 'page.html', '-s', 'EXPORTED_FUNCTIONS=["_main", "_play"]']).communicate() + self.run_browser('page.html', '', '/report_result?1') + def test_sdl_gl_read(self): # SDL, OpenGL, readPixels open(os.path.join(self.get_dir(), 'sdl_gl_read.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_gl_read.c')).read())) @@ -7652,6 +7683,41 @@ elif 'browser' in str(sys.argv): Popen(['python', EMCC, path_from_root('tests', 'sdl_ogl_p.c'), '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png']).communicate() self.run_browser('something.html', 'You should see an image with gray at the top.', '/report_result?0') + def test_sdl_fog_simple(self): + # SDL, OpenGL, textures, fog, immediate mode. Closure for more coverage + shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png')) + self.reftest(path_from_root('tests', 'screenshot-fog-simple.png')) + Popen(['python', EMCC, path_from_root('tests', 'sdl_fog_simple.c'), '-O2', '--minify', '0', '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png']).communicate() + self.run_browser('something.html', 'You should see an image with fog.', '/report_result?0') + + def test_sdl_fog_negative(self): + # SDL, OpenGL, textures, fog, immediate mode. Closure for more coverage + shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png')) + self.reftest(path_from_root('tests', 'screenshot-fog-negative.png')) + Popen(['python', EMCC, path_from_root('tests', 'sdl_fog_negative.c'), '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png']).communicate() + self.run_browser('something.html', 'You should see an image with fog.', '/report_result?0') + + def test_sdl_fog_density(self): + # SDL, OpenGL, textures, fog, immediate mode. Closure for more coverage + shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png')) + self.reftest(path_from_root('tests', 'screenshot-fog-density.png')) + Popen(['python', EMCC, path_from_root('tests', 'sdl_fog_density.c'), '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png']).communicate() + self.run_browser('something.html', 'You should see an image with fog.', '/report_result?0') + + def test_sdl_fog_exp2(self): + # SDL, OpenGL, textures, fog, immediate mode. Closure for more coverage + shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png')) + self.reftest(path_from_root('tests', 'screenshot-fog-exp2.png')) + Popen(['python', EMCC, path_from_root('tests', 'sdl_fog_exp2.c'), '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png']).communicate() + self.run_browser('something.html', 'You should see an image with fog.', '/report_result?0') + + def test_sdl_fog_linear(self): + # SDL, OpenGL, textures, fog, immediate mode. Closure for more coverage + shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png')) + self.reftest(path_from_root('tests', 'screenshot-fog-linear.png')) + Popen(['python', EMCC, path_from_root('tests', 'sdl_fog_linear.c'), '-o', 'something.html', '--pre-js', 'reftest.js', '--preload-file', 'screenshot.png']).communicate() + self.run_browser('something.html', 'You should see an image with fog.', '/report_result?0') + def test_worker(self): # Test running in a web worker output = Popen(['python', EMCC, path_from_root('tests', 'hello_world_worker.cpp'), '-o', 'worker.js'], stdout=PIPE, stderr=PIPE).communicate() @@ -7747,6 +7813,9 @@ elif 'browser' in str(sys.argv): def test_emscripten_api(self): self.btest('emscripten_api_browser.cpp', '1') + def test_emscripten_fs_api(self): + self.btest('emscripten_fs_api_browser.cpp', '1') + def test_gc(self): self.btest('browser_gc.cpp', '1') @@ -7829,12 +7898,18 @@ elif 'browser' in str(sys.argv): def test_s3tc_crunch(self): shutil.copyfile(path_from_root('tests', 'ship.dds'), 'ship.dds') shutil.copyfile(path_from_root('tests', 'bloom.dds'), 'bloom.dds') - Popen(['python', FILE_PACKAGER, 'test.data', '--pre-run', '--crunch', '--preload', 'ship.dds', 'bloom.dds'], stdout=open('pre.js', 'w')).communicate() - assert os.stat('test.data').st_size < 0.5*(os.stat('ship.dds').st_size+os.stat('bloom.dds').st_size), 'Compressed should be smaller than dds' + shutil.copyfile(path_from_root('tests', 'water.dds'), 'water.dds') + Popen(['python', FILE_PACKAGER, 'test.data', '--pre-run', '--crunch', '--preload', 'ship.dds', 'bloom.dds', 'water.dds'], stdout=open('pre.js', 'w')).communicate() + assert os.stat('test.data').st_size < 0.5*(os.stat('ship.dds').st_size+os.stat('bloom.dds').st_size+os.stat('water.dds').st_size), 'Compressed should be smaller than dds' shutil.move('ship.dds', 'ship.donotfindme.dds') # make sure we load from the compressed shutil.move('bloom.dds', 'bloom.donotfindme.dds') # make sure we load from the compressed + shutil.move('water.dds', 'water.donotfindme.dds') # make sure we load from the compressed self.btest('s3tc_crunch.c', reference='s3tc_crunch.png', args=['--pre-js', 'pre.js']) + def zzztest_aniso(self): + shutil.copyfile(path_from_root('tests', 'water.dds'), 'water.dds') + self.btest('aniso.c', reference='aniso.png', args=['--preload-file', 'water.dds', '-s', 'GL_DEBUG=1']) + def test_pre_run_deps(self): # Adding a dependency in preRun will delay run open(os.path.join(self.get_dir(), 'pre.js'), 'w').write(''' diff --git a/tests/s3tc_crunch.c b/tests/s3tc_crunch.c index 1169f462..57974109 100644 --- a/tests/s3tc_crunch.c +++ b/tests/s3tc_crunch.c @@ -90,7 +90,7 @@ int main(int argc, char *argv[]) GLuint texture; { - #define DDS_SIZE 65664 + const int DDS_SIZE = 65664; FILE *dds = fopen("ship.dds", "rb"); assert(dds); char *ddsdata = (char*)malloc(DDS_SIZE); @@ -113,7 +113,7 @@ int main(int argc, char *argv[]) GLuint texture2; { - #define DDS_SIZE 32896 + const int DDS_SIZE = 32896; FILE *dds = fopen("bloom.dds", "rb"); assert(dds); char *ddsdata = (char*)malloc(DDS_SIZE); @@ -131,6 +131,29 @@ int main(int argc, char *argv[]) glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); } + // third, a non-square texture with mipmaps + + GLuint texture3; + + { + const int DDS_SIZE = 43920; + FILE *dds = fopen("water.dds", "rb"); + assert(dds); + char *ddsdata = (char*)malloc(DDS_SIZE); + assert(fread(ddsdata, 1, DDS_SIZE, dds) == DDS_SIZE); + fclose(dds); + + glGenTextures( 1, &texture3 ); + glBindTexture( GL_TEXTURE_2D, texture3 ); + + assert(!glGetError()); + glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT5_EXT, 512, 64, 0, 512*64, ddsdata+128); + assert(!glGetError()); + + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + } + // Prepare and Render // Clear the screen before drawing @@ -154,7 +177,10 @@ int main(int argc, char *argv[]) glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(2, GL_FLOAT, 4*4, &vertexData[2]); - glDrawArrays(GL_QUADS, 0, 8); + glDrawArrays(GL_QUADS, 0, 4); + + glBindTexture( GL_TEXTURE_2D, texture3 ); + glDrawArrays(GL_QUADS, 4, 4); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); diff --git a/tests/s3tc_crunch.png b/tests/s3tc_crunch.png Binary files differindex 920f3915..383d4c5b 100644 --- a/tests/s3tc_crunch.png +++ b/tests/s3tc_crunch.png diff --git a/tests/screenshot-fog-density.png b/tests/screenshot-fog-density.png Binary files differnew file mode 100644 index 00000000..cd1f6f1b --- /dev/null +++ b/tests/screenshot-fog-density.png diff --git a/tests/screenshot-fog-exp2.png b/tests/screenshot-fog-exp2.png Binary files differnew file mode 100644 index 00000000..cd5e6a63 --- /dev/null +++ b/tests/screenshot-fog-exp2.png diff --git a/tests/screenshot-fog-linear.png b/tests/screenshot-fog-linear.png Binary files differnew file mode 100644 index 00000000..57534566 --- /dev/null +++ b/tests/screenshot-fog-linear.png diff --git a/tests/screenshot-fog-negative.png b/tests/screenshot-fog-negative.png Binary files differnew file mode 100644 index 00000000..5b18a201 --- /dev/null +++ b/tests/screenshot-fog-negative.png diff --git a/tests/screenshot-fog-simple.png b/tests/screenshot-fog-simple.png Binary files differnew file mode 100644 index 00000000..527768fc --- /dev/null +++ b/tests/screenshot-fog-simple.png diff --git a/tests/sdl_audio_quickload.c b/tests/sdl_audio_quickload.c new file mode 100644 index 00000000..1525d048 --- /dev/null +++ b/tests/sdl_audio_quickload.c @@ -0,0 +1,44 @@ +#include <stdio.h> +#include <stdlib.h> +#include <SDL/SDL.h> +#include <SDL/SDL_mixer.h> +#include <assert.h> +#include <limits.h> +#include <emscripten.h> + +Mix_Chunk *sound; + +void play() { + int channel = Mix_PlayChannel(-1, sound, 1); + assert(channel == 0); + + int result = 1; + REPORT_RESULT(); +} + +int main(int argc, char **argv) { + SDL_Init(SDL_INIT_AUDIO); + + int ret = Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024); + assert(ret == 0); + + Uint16* buffer = (Uint16*)malloc(10*44100*sizeof(Uint16)); + for (Uint32 i = 0; i < 10*44100; ++i) { + buffer[i] = (i * 5) % UINT32_MAX; + } + sound = Mix_QuickLoad_RAW((Uint8*) buffer, 10*44100*sizeof(Uint16)); + assert(sound); + + play(); + + emscripten_run_script("element = document.createElement('input');" + "element.setAttribute('type', 'button');" + "element.setAttribute('value', 'replay!');" + "element.setAttribute('onclick', 'Module[\"_play\"]()');" + "document.body.appendChild(element);"); + + printf("you should one sounds. press the button to replay!\n"); + + return 0; +} + diff --git a/tests/sdl_fog_density.c b/tests/sdl_fog_density.c new file mode 100644 index 00000000..95773419 --- /dev/null +++ b/tests/sdl_fog_density.c @@ -0,0 +1,183 @@ +/******************************************************************* + * * + * Using SDL With OpenGL * + * * + * Tutorial by Kyle Foley (sdw) * + * * + * http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL * + * * + *******************************************************************/ + +/* +THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION +AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN. + +THE ORIGINAL AUTHOR IS KYLE FOLEY. + +THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY +OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF +MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, +ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE +RESULTING FROM THE USE, MODIFICATION, OR +REDISTRIBUTION OF THIS SOFTWARE. +*/ + +#include "SDL/SDL.h" +#include "SDL/SDL_image.h" +#include "SDL/SDL_opengl.h" + +#include <stdio.h> +#include <string.h> +#include <assert.h> + +int main(int argc, char *argv[]) +{ + SDL_Surface *screen; + + // Slightly different SDL initialization + if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) { + printf("Unable to initialize SDL: %s\n", SDL_GetError()); + return 1; + } + + SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new* + + screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed* + if ( !screen ) { + printf("Unable to set video mode: %s\n", SDL_GetError()); + return 1; + } + + // Set the OpenGL state after creating the context with SDL_SetVideoMode + + glClearColor( 0, 0, 0, 0 ); + +#if !EMSCRIPTEN + glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL +#endif + + glViewport( 0, 0, 640, 480 ); + + glMatrixMode( GL_PROJECTION ); + glPushMatrix(); // just for testing + glLoadIdentity(); + + glOrtho( 0, 640, 480, 0, -1000, 1000 ); + + glMatrixMode( GL_MODELVIEW ); + glLoadIdentity(); + + // Load the OpenGL texture + + GLuint texture; // Texture object handle + SDL_Surface *surface; // Gives us the information to make the texture + + if ( (surface = IMG_Load("screenshot.png")) ) { + + // Check that the image's width is a power of 2 + if ( (surface->w & (surface->w - 1)) != 0 ) { + printf("warning: image.bmp's width is not a power of 2\n"); + } + + // Also check if the height is a power of 2 + if ( (surface->h & (surface->h - 1)) != 0 ) { + printf("warning: image.bmp's height is not a power of 2\n"); + } + + // Have OpenGL generate a texture object handle for us + glGenTextures( 1, &texture ); + + // Bind the texture object + glBindTexture( GL_TEXTURE_2D, texture ); + + // Set the texture's stretching properties + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + + //SDL_LockSurface(surface); + + // Add some greyness + memset(surface->pixels, 0x66, surface->w*surface->h); + + // Edit the texture object's image data using the information SDL_Surface gives us + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, + GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels ); + + //SDL_UnlockSurface(surface); + } + else { + printf("SDL could not load image.bmp: %s\n", SDL_GetError()); + SDL_Quit(); + return 1; + } + + // Free the SDL_Surface only if it was successfully created + if ( surface ) { + SDL_FreeSurface( surface ); + } + + // Clear the screen before drawing + glClear( GL_COLOR_BUFFER_BIT ); + + // Bind the texture to which subsequent calls refer to + glBindTexture( GL_TEXTURE_2D, texture ); + + glEnable(GL_FOG); + GLfloat fogColor[] = { 1.0, 0.5, 0.5, 0.05 }; + glFogfv(GL_FOG_COLOR, fogColor); + glFogf(GL_FOG_DENSITY, 0.2); + + assert(glIsEnabled(GL_FOG)); + + glBegin( GL_QUADS ); + glTexCoord2i( 0, 0 ); glVertex3f( 10, 10, 10 ); + glTexCoord2i( 1, 0 ); glVertex3f( 300, 10, 10 ); + glTexCoord2i( 1, 1 ); glVertex3f( 300, 128, 10 ); + glTexCoord2i( 0, 1 ); glVertex3f( 10, 128, 10 ); + + glTexCoord2f( 0, 0.5 ); glVertex3f( 410, 10, 5 ); + glTexCoord2f( 1, 0.5 ); glVertex3f( 600, 10, 6 ); + glTexCoord2f( 1, 1 ); glVertex3f( 630, 200, 7 ); + glTexCoord2f( 0.5, 1 ); glVertex3f( 310, 250, 8 ); + glEnd(); + + glBegin( GL_TRIANGLE_STRIP ); + glTexCoord2i( 0, 0 ); glVertex3f( 100, 300, 1 ); + glTexCoord2i( 1, 0 ); glVertex3f( 300, 300, 1 ); + glTexCoord2i( 1, 1 ); glVertex3f( 300, 400, 1 ); + glTexCoord2i( 0, 1 ); glVertex3f( 500, 410, 1 ); + glEnd(); + +#if !EMSCRIPTEN + glDisable(GL_TEXTURE_2D); +#endif + + glColor3ub(90, 255, 255); + glBegin( GL_QUADS ); + glVertex3f( 10, 410, 5 ); + glVertex3f( 300, 410, 50 ); + glVertex3f( 300, 480, 100 ); + glVertex3f( 10, 470, 5 ); + glEnd(); + + glBegin( GL_QUADS ); + glColor3f(1.0, 0, 1.0); glVertex3f( 410, 410, 10 ); + glColor3f(0, 1.0, 0); glVertex3f( 600, 410, 10 ); + glColor3f(0, 0, 1.0); glVertex3f( 600, 480, 10 ); + glColor3f(1.0, 1.0, 1.0); glVertex3f( 410, 470, 10 ); + glEnd(); + + SDL_GL_SwapBuffers(); + +#if !EMSCRIPTEN + // Wait for 3 seconds to give us a chance to see the image + SDL_Delay(30000); +#endif + + // Now we can delete the OpenGL texture and close down SDL + glDeleteTextures( 1, &texture ); + + SDL_Quit(); + + return 0; +} diff --git a/tests/sdl_fog_exp2.c b/tests/sdl_fog_exp2.c new file mode 100644 index 00000000..a09a5e3d --- /dev/null +++ b/tests/sdl_fog_exp2.c @@ -0,0 +1,184 @@ +/******************************************************************* + * * + * Using SDL With OpenGL * + * * + * Tutorial by Kyle Foley (sdw) * + * * + * http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL * + * * + *******************************************************************/ + +/* +THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION +AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN. + +THE ORIGINAL AUTHOR IS KYLE FOLEY. + +THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY +OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF +MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, +ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE +RESULTING FROM THE USE, MODIFICATION, OR +REDISTRIBUTION OF THIS SOFTWARE. +*/ + +#include "SDL/SDL.h" +#include "SDL/SDL_image.h" +#include "SDL/SDL_opengl.h" + +#include <stdio.h> +#include <string.h> +#include <assert.h> + +int main(int argc, char *argv[]) +{ + SDL_Surface *screen; + + // Slightly different SDL initialization + if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) { + printf("Unable to initialize SDL: %s\n", SDL_GetError()); + return 1; + } + + SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new* + + screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed* + if ( !screen ) { + printf("Unable to set video mode: %s\n", SDL_GetError()); + return 1; + } + + // Set the OpenGL state after creating the context with SDL_SetVideoMode + + glClearColor( 0, 0, 0, 0 ); + +#if !EMSCRIPTEN + glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL +#endif + + glViewport( 0, 0, 640, 480 ); + + glMatrixMode( GL_PROJECTION ); + glPushMatrix(); // just for testing + glLoadIdentity(); + + glOrtho( 0, 640, 480, 0, -1000, 1000 ); + + glMatrixMode( GL_MODELVIEW ); + glLoadIdentity(); + + // Load the OpenGL texture + + GLuint texture; // Texture object handle + SDL_Surface *surface; // Gives us the information to make the texture + + if ( (surface = IMG_Load("screenshot.png")) ) { + + // Check that the image's width is a power of 2 + if ( (surface->w & (surface->w - 1)) != 0 ) { + printf("warning: image.bmp's width is not a power of 2\n"); + } + + // Also check if the height is a power of 2 + if ( (surface->h & (surface->h - 1)) != 0 ) { + printf("warning: image.bmp's height is not a power of 2\n"); + } + + // Have OpenGL generate a texture object handle for us + glGenTextures( 1, &texture ); + + // Bind the texture object + glBindTexture( GL_TEXTURE_2D, texture ); + + // Set the texture's stretching properties + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + + //SDL_LockSurface(surface); + + // Add some greyness + memset(surface->pixels, 0x66, surface->w*surface->h); + + // Edit the texture object's image data using the information SDL_Surface gives us + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, + GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels ); + + //SDL_UnlockSurface(surface); + } + else { + printf("SDL could not load image.bmp: %s\n", SDL_GetError()); + SDL_Quit(); + return 1; + } + + // Free the SDL_Surface only if it was successfully created + if ( surface ) { + SDL_FreeSurface( surface ); + } + + // Clear the screen before drawing + glClear( GL_COLOR_BUFFER_BIT ); + + // Bind the texture to which subsequent calls refer to + glBindTexture( GL_TEXTURE_2D, texture ); + + glEnable(GL_FOG); + GLfloat fogColor[] = { 1.0, 0.5, 0.5, 0.05 }; + glFogfv(GL_FOG_COLOR, fogColor); + glFogf(GL_FOG_DENSITY, 0.2); + glFogi(GL_FOG_MODE, GL_EXP2); + + assert(glIsEnabled(GL_FOG)); + + glBegin( GL_QUADS ); + glTexCoord2i( 0, 0 ); glVertex3f( 10, 10, 10 ); + glTexCoord2i( 1, 0 ); glVertex3f( 300, 10, 10 ); + glTexCoord2i( 1, 1 ); glVertex3f( 300, 128, 10 ); + glTexCoord2i( 0, 1 ); glVertex3f( 10, 128, 10 ); + + glTexCoord2f( 0, 0.5 ); glVertex3f( 410, 10, 5 ); + glTexCoord2f( 1, 0.5 ); glVertex3f( 600, 10, 6 ); + glTexCoord2f( 1, 1 ); glVertex3f( 630, 200, 7 ); + glTexCoord2f( 0.5, 1 ); glVertex3f( 310, 250, 8 ); + glEnd(); + + glBegin( GL_TRIANGLE_STRIP ); + glTexCoord2i( 0, 0 ); glVertex3f( 100, 300, 1 ); + glTexCoord2i( 1, 0 ); glVertex3f( 300, 300, 1 ); + glTexCoord2i( 1, 1 ); glVertex3f( 300, 400, 1 ); + glTexCoord2i( 0, 1 ); glVertex3f( 500, 410, 1 ); + glEnd(); + +#if !EMSCRIPTEN + glDisable(GL_TEXTURE_2D); +#endif + + glColor3ub(90, 255, 255); + glBegin( GL_QUADS ); + glVertex3f( 10, 410, 5 ); + glVertex3f( 300, 410, 50 ); + glVertex3f( 300, 480, 100 ); + glVertex3f( 10, 470, 5 ); + glEnd(); + + glBegin( GL_QUADS ); + glColor3f(1.0, 0, 1.0); glVertex3f( 410, 410, 10 ); + glColor3f(0, 1.0, 0); glVertex3f( 600, 410, 10 ); + glColor3f(0, 0, 1.0); glVertex3f( 600, 480, 10 ); + glColor3f(1.0, 1.0, 1.0); glVertex3f( 410, 470, 10 ); + glEnd(); + + SDL_GL_SwapBuffers(); + +#if !EMSCRIPTEN + // Wait for 3 seconds to give us a chance to see the image + SDL_Delay(30000); +#endif + + // Now we can delete the OpenGL texture and close down SDL + glDeleteTextures( 1, &texture ); + + SDL_Quit(); + + return 0; +} diff --git a/tests/sdl_fog_linear.c b/tests/sdl_fog_linear.c new file mode 100644 index 00000000..8fc18b7c --- /dev/null +++ b/tests/sdl_fog_linear.c @@ -0,0 +1,185 @@ +/******************************************************************* + * * + * Using SDL With OpenGL * + * * + * Tutorial by Kyle Foley (sdw) * + * * + * http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL * + * * + *******************************************************************/ + +/* +THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION +AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN. + +THE ORIGINAL AUTHOR IS KYLE FOLEY. + +THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY +OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF +MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, +ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE +RESULTING FROM THE USE, MODIFICATION, OR +REDISTRIBUTION OF THIS SOFTWARE. +*/ + +#include "SDL/SDL.h" +#include "SDL/SDL_image.h" +#include "SDL/SDL_opengl.h" + +#include <stdio.h> +#include <string.h> +#include <assert.h> + +int main(int argc, char *argv[]) +{ + SDL_Surface *screen; + + // Slightly different SDL initialization + if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) { + printf("Unable to initialize SDL: %s\n", SDL_GetError()); + return 1; + } + + SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new* + + screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed* + if ( !screen ) { + printf("Unable to set video mode: %s\n", SDL_GetError()); + return 1; + } + + // Set the OpenGL state after creating the context with SDL_SetVideoMode + + glClearColor( 0, 0, 0, 0 ); + +#if !EMSCRIPTEN + glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL +#endif + + glViewport( 0, 0, 640, 480 ); + + glMatrixMode( GL_PROJECTION ); + glPushMatrix(); // just for testing + glLoadIdentity(); + + glOrtho( 0, 640, 480, 0, -1000, 1000 ); + + glMatrixMode( GL_MODELVIEW ); + glLoadIdentity(); + + // Load the OpenGL texture + + GLuint texture; // Texture object handle + SDL_Surface *surface; // Gives us the information to make the texture + + if ( (surface = IMG_Load("screenshot.png")) ) { + + // Check that the image's width is a power of 2 + if ( (surface->w & (surface->w - 1)) != 0 ) { + printf("warning: image.bmp's width is not a power of 2\n"); + } + + // Also check if the height is a power of 2 + if ( (surface->h & (surface->h - 1)) != 0 ) { + printf("warning: image.bmp's height is not a power of 2\n"); + } + + // Have OpenGL generate a texture object handle for us + glGenTextures( 1, &texture ); + + // Bind the texture object + glBindTexture( GL_TEXTURE_2D, texture ); + + // Set the texture's stretching properties + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + + //SDL_LockSurface(surface); + + // Add some greyness + memset(surface->pixels, 0x66, surface->w*surface->h); + + // Edit the texture object's image data using the information SDL_Surface gives us + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, + GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels ); + + //SDL_UnlockSurface(surface); + } + else { + printf("SDL could not load image.bmp: %s\n", SDL_GetError()); + SDL_Quit(); + return 1; + } + + // Free the SDL_Surface only if it was successfully created + if ( surface ) { + SDL_FreeSurface( surface ); + } + + // Clear the screen before drawing + glClear( GL_COLOR_BUFFER_BIT ); + + // Bind the texture to which subsequent calls refer to + glBindTexture( GL_TEXTURE_2D, texture ); + + glEnable(GL_FOG); + GLfloat fogColor[] = { 1.0, 0.5, 0.5, 0.05 }; + glFogfv(GL_FOG_COLOR, fogColor); + glFogi(GL_FOG_START, 8); + glFogi(GL_FOG_END, 13); + glFogi(GL_FOG_MODE, GL_LINEAR); + + assert(glIsEnabled(GL_FOG)); + + glBegin( GL_QUADS ); + glTexCoord2i( 0, 0 ); glVertex3f( 10, 10, 10 ); + glTexCoord2i( 1, 0 ); glVertex3f( 300, 10, 10 ); + glTexCoord2i( 1, 1 ); glVertex3f( 300, 128, 10 ); + glTexCoord2i( 0, 1 ); glVertex3f( 10, 128, 10 ); + + glTexCoord2f( 0, 0.5 ); glVertex3f( 410, 10, 5 ); + glTexCoord2f( 1, 0.5 ); glVertex3f( 600, 10, 6 ); + glTexCoord2f( 1, 1 ); glVertex3f( 630, 200, 7 ); + glTexCoord2f( 0.5, 1 ); glVertex3f( 310, 250, 8 ); + glEnd(); + + glBegin( GL_TRIANGLE_STRIP ); + glTexCoord2i( 0, 0 ); glVertex3f( 100, 300, 1 ); + glTexCoord2i( 1, 0 ); glVertex3f( 300, 300, 1 ); + glTexCoord2i( 1, 1 ); glVertex3f( 300, 400, 1 ); + glTexCoord2i( 0, 1 ); glVertex3f( 500, 410, 1 ); + glEnd(); + +#if !EMSCRIPTEN + glDisable(GL_TEXTURE_2D); +#endif + + glColor3ub(90, 255, 255); + glBegin( GL_QUADS ); + glVertex3f( 10, 410, 5 ); + glVertex3f( 300, 410, 50 ); + glVertex3f( 300, 480, 100 ); + glVertex3f( 10, 470, 5 ); + glEnd(); + + glBegin( GL_QUADS ); + glColor3f(1.0, 0, 1.0); glVertex3f( 410, 410, 10 ); + glColor3f(0, 1.0, 0); glVertex3f( 600, 410, 10 ); + glColor3f(0, 0, 1.0); glVertex3f( 600, 480, 10 ); + glColor3f(1.0, 1.0, 1.0); glVertex3f( 410, 470, 10 ); + glEnd(); + + SDL_GL_SwapBuffers(); + +#if !EMSCRIPTEN + // Wait for 3 seconds to give us a chance to see the image + SDL_Delay(30000); +#endif + + // Now we can delete the OpenGL texture and close down SDL + glDeleteTextures( 1, &texture ); + + SDL_Quit(); + + return 0; +} diff --git a/tests/sdl_fog_negative.c b/tests/sdl_fog_negative.c new file mode 100644 index 00000000..2d589a47 --- /dev/null +++ b/tests/sdl_fog_negative.c @@ -0,0 +1,182 @@ +/******************************************************************* + * * + * Using SDL With OpenGL * + * * + * Tutorial by Kyle Foley (sdw) * + * * + * http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL * + * * + *******************************************************************/ + +/* +THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION +AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN. + +THE ORIGINAL AUTHOR IS KYLE FOLEY. + +THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY +OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF +MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, +ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE +RESULTING FROM THE USE, MODIFICATION, OR +REDISTRIBUTION OF THIS SOFTWARE. +*/ + +#include "SDL/SDL.h" +#include "SDL/SDL_image.h" +#include "SDL/SDL_opengl.h" + +#include <stdio.h> +#include <string.h> +#include <assert.h> + +int main(int argc, char *argv[]) +{ + SDL_Surface *screen; + + // Slightly different SDL initialization + if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) { + printf("Unable to initialize SDL: %s\n", SDL_GetError()); + return 1; + } + + SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new* + + screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed* + if ( !screen ) { + printf("Unable to set video mode: %s\n", SDL_GetError()); + return 1; + } + + // Set the OpenGL state after creating the context with SDL_SetVideoMode + + glClearColor( 0, 0, 0, 0 ); + +#if !EMSCRIPTEN + glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL +#endif + + glViewport( 0, 0, 640, 480 ); + + glMatrixMode( GL_PROJECTION ); + glPushMatrix(); // just for testing + glLoadIdentity(); + + glOrtho( 0, 640, 480, 0, -1000, 1000 ); + + glMatrixMode( GL_MODELVIEW ); + glLoadIdentity(); + + // Load the OpenGL texture + + GLuint texture; // Texture object handle + SDL_Surface *surface; // Gives us the information to make the texture + + if ( (surface = IMG_Load("screenshot.png")) ) { + + // Check that the image's width is a power of 2 + if ( (surface->w & (surface->w - 1)) != 0 ) { + printf("warning: image.bmp's width is not a power of 2\n"); + } + + // Also check if the height is a power of 2 + if ( (surface->h & (surface->h - 1)) != 0 ) { + printf("warning: image.bmp's height is not a power of 2\n"); + } + + // Have OpenGL generate a texture object handle for us + glGenTextures( 1, &texture ); + + // Bind the texture object + glBindTexture( GL_TEXTURE_2D, texture ); + + // Set the texture's stretching properties + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + + //SDL_LockSurface(surface); + + // Add some greyness + memset(surface->pixels, 0x66, surface->w*surface->h); + + // Edit the texture object's image data using the information SDL_Surface gives us + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, + GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels ); + + //SDL_UnlockSurface(surface); + } + else { + printf("SDL could not load image.bmp: %s\n", SDL_GetError()); + SDL_Quit(); + return 1; + } + + // Free the SDL_Surface only if it was successfully created + if ( surface ) { + SDL_FreeSurface( surface ); + } + + // Clear the screen before drawing + glClear( GL_COLOR_BUFFER_BIT ); + + // Bind the texture to which subsequent calls refer to + glBindTexture( GL_TEXTURE_2D, texture ); + + glEnable(GL_FOG); + GLfloat fogColor[] = { 1.0, 0.5, 0.5, 0.05 }; + glFogfv(GL_FOG_COLOR, fogColor); + + assert(glIsEnabled(GL_FOG)); + + glBegin( GL_QUADS ); + glTexCoord2i( 0, 0 ); glVertex3f( 10, 10, -1 ); + glTexCoord2i( 1, 0 ); glVertex3f( 300, 10, -1 ); + glTexCoord2i( 1, 1 ); glVertex3f( 300, 128, -1 ); + glTexCoord2i( 0, 1 ); glVertex3f( 10, 128, -1 ); + + glTexCoord2f( 0, 0.5 ); glVertex3f( 410, 10, -5 ); + glTexCoord2f( 1, 0.5 ); glVertex3f( 600, 10, -6 ); + glTexCoord2f( 1, 1 ); glVertex3f( 630, 200, -7 ); + glTexCoord2f( 0.5, 1 ); glVertex3f( 310, 250, -8 ); + glEnd(); + + glBegin( GL_TRIANGLE_STRIP ); + glTexCoord2i( 0, 0 ); glVertex3f( 100, 300, -1 ); + glTexCoord2i( 1, 0 ); glVertex3f( 300, 300, -1 ); + glTexCoord2i( 1, 1 ); glVertex3f( 300, 400, -1 ); + glTexCoord2i( 0, 1 ); glVertex3f( 500, 410, -1 ); + glEnd(); + +#if !EMSCRIPTEN + glDisable(GL_TEXTURE_2D); +#endif + + glColor3ub(90, 255, 255); + glBegin( GL_QUADS ); + glVertex3f( 10, 410, -5 ); + glVertex3f( 300, 410, -50 ); + glVertex3f( 300, 480, -100 ); + glVertex3f( 10, 470, -5 ); + glEnd(); + + glBegin( GL_QUADS ); + glColor3f(1.0, 0, 1.0); glVertex3f( 410, 410, -10 ); + glColor3f(0, 1.0, 0); glVertex3f( 600, 410, -10 ); + glColor3f(0, 0, 1.0); glVertex3f( 600, 480, -10 ); + glColor3f(1.0, 1.0, 1.0); glVertex3f( 410, 470, -10 ); + glEnd(); + + SDL_GL_SwapBuffers(); + +#if !EMSCRIPTEN + // Wait for 3 seconds to give us a chance to see the image + SDL_Delay(30000); +#endif + + // Now we can delete the OpenGL texture and close down SDL + glDeleteTextures( 1, &texture ); + + SDL_Quit(); + + return 0; +} diff --git a/tests/sdl_fog_simple.c b/tests/sdl_fog_simple.c new file mode 100644 index 00000000..be023593 --- /dev/null +++ b/tests/sdl_fog_simple.c @@ -0,0 +1,182 @@ +/******************************************************************* + * * + * Using SDL With OpenGL * + * * + * Tutorial by Kyle Foley (sdw) * + * * + * http://gpwiki.org/index.php/SDL:Tutorials:Using_SDL_with_OpenGL * + * * + *******************************************************************/ + +/* +THIS WORK, INCLUDING THE SOURCE CODE, DOCUMENTATION +AND RELATED MEDIA AND DATA, IS PLACED INTO THE PUBLIC DOMAIN. + +THE ORIGINAL AUTHOR IS KYLE FOLEY. + +THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY +OF ANY KIND, NOT EVEN THE IMPLIED WARRANTY OF +MERCHANTABILITY. THE AUTHOR OF THIS SOFTWARE, +ASSUMES _NO_ RESPONSIBILITY FOR ANY CONSEQUENCE +RESULTING FROM THE USE, MODIFICATION, OR +REDISTRIBUTION OF THIS SOFTWARE. +*/ + +#include "SDL/SDL.h" +#include "SDL/SDL_image.h" +#include "SDL/SDL_opengl.h" + +#include <stdio.h> +#include <string.h> +#include <assert.h> + +int main(int argc, char *argv[]) +{ + SDL_Surface *screen; + + // Slightly different SDL initialization + if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) { + printf("Unable to initialize SDL: %s\n", SDL_GetError()); + return 1; + } + + SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); // *new* + + screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL ); // *changed* + if ( !screen ) { + printf("Unable to set video mode: %s\n", SDL_GetError()); + return 1; + } + + // Set the OpenGL state after creating the context with SDL_SetVideoMode + + glClearColor( 0, 0, 0, 0 ); + +#if !EMSCRIPTEN + glEnable( GL_TEXTURE_2D ); // Need this to display a texture XXX unnecessary in OpenGL ES 2.0/WebGL +#endif + + glViewport( 0, 0, 640, 480 ); + + glMatrixMode( GL_PROJECTION ); + glPushMatrix(); // just for testing + glLoadIdentity(); + + glOrtho( 0, 640, 480, 0, -1000, 1000 ); + + glMatrixMode( GL_MODELVIEW ); + glLoadIdentity(); + + // Load the OpenGL texture + + GLuint texture; // Texture object handle + SDL_Surface *surface; // Gives us the information to make the texture + + if ( (surface = IMG_Load("screenshot.png")) ) { + + // Check that the image's width is a power of 2 + if ( (surface->w & (surface->w - 1)) != 0 ) { + printf("warning: image.bmp's width is not a power of 2\n"); + } + + // Also check if the height is a power of 2 + if ( (surface->h & (surface->h - 1)) != 0 ) { + printf("warning: image.bmp's height is not a power of 2\n"); + } + + // Have OpenGL generate a texture object handle for us + glGenTextures( 1, &texture ); + + // Bind the texture object + glBindTexture( GL_TEXTURE_2D, texture ); + + // Set the texture's stretching properties + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + + //SDL_LockSurface(surface); + + // Add some greyness + memset(surface->pixels, 0x66, surface->w*surface->h); + + // Edit the texture object's image data using the information SDL_Surface gives us + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, + GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels ); + + //SDL_UnlockSurface(surface); + } + else { + printf("SDL could not load image.bmp: %s\n", SDL_GetError()); + SDL_Quit(); + return 1; + } + + // Free the SDL_Surface only if it was successfully created + if ( surface ) { + SDL_FreeSurface( surface ); + } + + // Clear the screen before drawing + glClear( GL_COLOR_BUFFER_BIT ); + + // Bind the texture to which subsequent calls refer to + glBindTexture( GL_TEXTURE_2D, texture ); + + glEnable(GL_FOG); + GLfloat fogColor[] = { 1.0, 0.5, 0.5, 0.05 }; + glFogfv(GL_FOG_COLOR, fogColor); + + assert(glIsEnabled(GL_FOG)); + + glBegin( GL_QUADS ); + glTexCoord2i( 0, 0 ); glVertex3f( 10, 10, 10 ); + glTexCoord2i( 1, 0 ); glVertex3f( 300, 10, 10 ); + glTexCoord2i( 1, 1 ); glVertex3f( 300, 128, 10 ); + glTexCoord2i( 0, 1 ); glVertex3f( 10, 128, 10 ); + + glTexCoord2f( 0, 0.5 ); glVertex3f( 410, 10, 5 ); + glTexCoord2f( 1, 0.5 ); glVertex3f( 600, 10, 6 ); + glTexCoord2f( 1, 1 ); glVertex3f( 630, 200, 7 ); + glTexCoord2f( 0.5, 1 ); glVertex3f( 310, 250, 8 ); + glEnd(); + + glBegin( GL_TRIANGLE_STRIP ); + glTexCoord2i( 0, 0 ); glVertex3f( 100, 300, 1 ); + glTexCoord2i( 1, 0 ); glVertex3f( 300, 300, 1 ); + glTexCoord2i( 1, 1 ); glVertex3f( 300, 400, 1 ); + glTexCoord2i( 0, 1 ); glVertex3f( 500, 410, 1 ); + glEnd(); + +#if !EMSCRIPTEN + glDisable(GL_TEXTURE_2D); +#endif + + glColor3ub(90, 255, 255); + glBegin( GL_QUADS ); + glVertex3f( 10, 410, 5 ); + glVertex3f( 300, 410, 50 ); + glVertex3f( 300, 480, 100 ); + glVertex3f( 10, 470, 5 ); + glEnd(); + + glBegin( GL_QUADS ); + glColor3f(1.0, 0, 1.0); glVertex3f( 410, 410, 10 ); + glColor3f(0, 1.0, 0); glVertex3f( 600, 410, 10 ); + glColor3f(0, 0, 1.0); glVertex3f( 600, 480, 10 ); + glColor3f(1.0, 1.0, 1.0); glVertex3f( 410, 470, 10 ); + glEnd(); + + SDL_GL_SwapBuffers(); + +#if !EMSCRIPTEN + // Wait for 3 seconds to give us a chance to see the image + SDL_Delay(30000); +#endif + + // Now we can delete the OpenGL texture and close down SDL + glDeleteTextures( 1, &texture ); + + SDL_Quit(); + + return 0; +} diff --git a/tests/water.dds b/tests/water.dds Binary files differnew file mode 100644 index 00000000..2a78a859 --- /dev/null +++ b/tests/water.dds diff --git a/tools/crunch-worker.js b/tools/crunch-worker.js index bc7eb95d..5c48d009 100644 --- a/tools/crunch-worker.js +++ b/tools/crunch-worker.js @@ -66,7 +66,6 @@ if(format != cCRNFmtDXT1 && format != cCRNFmtDXT3 && format != cCRNFmtDXT5) { throw "Unsupported image format " + format + " for " + filename; } - width = Module._crn_get_width(src, srcSize); height = Module._crn_get_height(src, srcSize); levels = Module._crn_get_levels(src, srcSize); @@ -74,9 +73,18 @@ dst = Module._malloc(dstSize); var totalSize = 0; + var bytesPerPixel = format == cCRNFmtDXT1 ? 0.5 : 1; for(i = 0; i < levels; ++i) { - totalSize += Module._crn_get_uncompressed_size(src, srcSize, i); + totalSize += width * height * bytesPerPixel; + width *= 0.5; + height *= 0.5; + width = Math.max(width, 4); + height = Math.max(height, 4); } + + width = Module._crn_get_width(src, srcSize); + height = Module._crn_get_height(src, srcSize); + var ret = new Uint8Array(totalSize); var retIndex = 0; @@ -107,6 +115,7 @@ onmessage = function(msg) { var start = Date.now(); var data = deCrunch(new Uint8Array(msg.data.data), msg.data.filename); postMessage({ + filename: msg.data.filename, data: data, callbackID: msg.data.callbackID, time: Date.now() - start diff --git a/tools/file_packager.py b/tools/file_packager.py index 812def9b..3844b4ae 100644 --- a/tools/file_packager.py +++ b/tools/file_packager.py @@ -27,7 +27,7 @@ TODO: You can also provide .crn files yourself, pre-crunched. With this o import os, sys, shutil -from shared import Compression, execute, suffix, unsuffixed, CRUNCH +from shared import Compression, execute, suffix, unsuffixed import shared from subprocess import Popen, PIPE, STDOUT @@ -70,6 +70,7 @@ for arg in sys.argv[1:]: in_embed = False in_compress = 0 elif arg.startswith('--crunch'): + from shared import CRUNCH crunch = arg.split('=')[1] if '=' in arg else '128' in_preload = False in_embed = False @@ -144,7 +145,7 @@ if crunch: var decrunchCallbacks = []; decrunchWorker.onmessage = function(msg) { decrunchCallbacks[msg.data.callbackID](msg.data.data); - console.log('decrunched in ' + msg.data.time + ' ms'); + console.log('decrunched ' + msg.data.filename + ' in ' + msg.data.time + ' ms, ' + msg.data.data.length + ' bytes'); decrunchCallbacks[msg.data.callbackID] = null; }; function requestDecrunch(filename, data, callback) { @@ -161,6 +162,7 @@ if crunch: if file_['name'].endswith(CRUNCH_INPUT_SUFFIX): # Do not crunch if crunched version exists and is more recent than dds source crunch_name = unsuffixed(file_['name']) + CRUNCH_OUTPUT_SUFFIX + file_['localname'] = crunch_name try: crunch_time = os.stat(crunch_name).st_mtime dds_time = os.stat(file_['name']).st_mtime @@ -187,7 +189,6 @@ if crunch: c.write(open(file_['name'], 'rb').read()[:DDS_HEADER_SIZE]) c.write(crunched) c.close() - file_['localname'] = crunch_name # Set up folders partial_dirs = [] @@ -210,6 +211,7 @@ if has_preloaded: file_['data_start'] = start curr = open(file_['localname'], 'rb').read() file_['data_end'] = start + len(curr) + print >> sys.stderr, 'bundling', file_['name'], file_['localname'], file_['data_start'], file_['data_end'] start += len(curr) data.write(curr) data.close() diff --git a/tools/shared.py b/tools/shared.py index 84924253..e6c0163c 100644 --- a/tools/shared.py +++ b/tools/shared.py @@ -114,6 +114,7 @@ LLVM_DIS=os.path.expanduser(os.path.join(LLVM_ROOT, 'llvm-dis')) LLVM_NM=os.path.expanduser(os.path.join(LLVM_ROOT, 'llvm-nm')) LLVM_INTERPRETER=os.path.expanduser(os.path.join(LLVM_ROOT, 'lli')) LLVM_COMPILER=os.path.expanduser(os.path.join(LLVM_ROOT, 'llc')) +LLVM_EXTRACT=os.path.expanduser(os.path.join(LLVM_ROOT, 'llvm-extract')) COFFEESCRIPT = path_from_root('tools', 'eliminator', 'node_modules', 'coffee-script', 'bin', 'coffee') EMSCRIPTEN = path_from_root('emscripten.py') @@ -530,7 +531,7 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)''' % { 'winfix': '' if not WINDOWS e return generated_libs @staticmethod - def link(files, target): + def link(files, target, remove_duplicates=False): actual_files = [] unresolved_symbols = set() # necessary for .a linking, see below resolved_symbols = set() @@ -579,8 +580,27 @@ set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)''' % { 'winfix': '' if not WINDOWS e finally: os.chdir(cwd) try_delete(target) + + if remove_duplicates: + # Remove duplicate symbols. This is a workaround for how we compile .a files, we try to + # emulate ld behavior which is permissive TODO: cache llvm-nm results + seen_symbols = set() + print >> sys.stderr, actual_files + for actual in actual_files: + symbols = Building.llvm_nm(actual) + dupes = seen_symbols.intersection(symbols.defs) + if len(dupes) > 0: + print >> sys.stderr, 'emcc: warning: removing duplicates in', actual + for dupe in dupes: + print >> sys.stderr, 'emcc: warning: removing duplicate', dupe + Popen([LLVM_EXTRACT, actual, '-delete', '-glob=' + dupe, '-o', actual], stderr=PIPE).communicate() + Popen([LLVM_EXTRACT, actual, '-delete', '-func=' + dupe, '-o', actual], stderr=PIPE).communicate() + Popen([LLVM_EXTRACT, actual, '-delete', '-glob=.str', '-o', actual], stderr=PIPE).communicate() # garbage that appears here + seen_symbols = seen_symbols.union(symbols.defs) + + # Finish link output = Popen([LLVM_LINK] + actual_files + ['-o', target], stdout=PIPE).communicate()[0] - assert os.path.exists(target) and (output is None or 'Could not open input file' not in output), 'Linking error: ' + output + assert os.path.exists(target) and (output is None or 'Could not open input file' not in output), 'Linking error: ' + output + '\nemcc: If you get duplicate symbol errors, try --remove-duplicates' if temp_dir: try_delete(temp_dir) @@ -960,7 +980,11 @@ def execute(cmd, *args, **kw): raise def suffix(name): - return name.split('.')[-1] + parts = name.split('.') + if len(parts) > 1: + return parts[-1] + else: + return None def unsuffixed(name): return '.'.join(name.split('.')[:-1]) |