diff --git a/engine/l_studio.cpp b/engine/l_studio.cpp index 6a71336047..8e9dcee34d 100644 --- a/engine/l_studio.cpp +++ b/engine/l_studio.cpp @@ -104,7 +104,16 @@ ConVar r_proplightingfromdisk( "r_proplightingfromdisk", "1", FCVAR_CHEAT, "0=Of #endif static ConVar r_itemblinkmax( "r_itemblinkmax", ".3", FCVAR_CHEAT ); static ConVar r_itemblinkrate( "r_itemblinkrate", "4.5", FCVAR_CHEAT ); +#ifdef __EMSCRIPTEN__ +// The pooled allocator locks only the first N vertices of the shared buffer +// and then writes each prop's colors at a byte offset past that range. That +// relies on D3D handing out a pointer to the whole buffer; the browser GL +// layer stages exactly the locked range, so those writes landed outside it. +// Give every static prop its own color mesh instead. +static ConVar r_proplightingpooling( "r_proplightingpooling", "0", FCVAR_CHEAT, "0 - off, 1 - static prop color meshes are allocated from a single shared vertex buffer (on hardware that supports stream offset)" ); +#else static ConVar r_proplightingpooling( "r_proplightingpooling", "-1.0", FCVAR_CHEAT, "0 - off, 1 - static prop color meshes are allocated from a single shared vertex buffer (on hardware that supports stream offset)" ); +#endif //----------------------------------------------------------------------------- // StudioRender config diff --git a/engine/matsys_interface.cpp b/engine/matsys_interface.cpp index e97bb8b7db..2f5ac74937 100644 --- a/engine/matsys_interface.cpp +++ b/engine/matsys_interface.cpp @@ -666,10 +666,16 @@ static void OverrideMaterialSystemConfigFromCommandLine( MaterialSystem_Config_t config.m_nAAQuality = CommandLine()->ParmValue( "-mat_aaquality", config.m_nAAQuality ); // Clamp the requested dimensions to the display resolution +#ifdef __EMSCRIPTEN__ + // In the browser the GL layer reports a fixed 640x480 "display", which + // squashed the page-sized -w/-h back to 4:3. The canvas is whatever size + // the page asks for, so keep the requested dimensions. +#else MaterialVideoMode_t videoMode; materials->GetDisplayMode( videoMode ); config.m_VideoMode.m_Width = MIN( videoMode.m_Width, config.m_VideoMode.m_Width ); config.m_VideoMode.m_Height = MIN( videoMode.m_Height, config.m_VideoMode.m_Height ); +#endif // safe mode if ( CommandLine()->FindParm( "-safe" ) ) diff --git a/public/togles/linuxwin/cglmbuffer.h b/public/togles/linuxwin/cglmbuffer.h index 10782243b7..621198905a 100644 --- a/public/togles/linuxwin/cglmbuffer.h +++ b/public/togles/linuxwin/cglmbuffer.h @@ -242,7 +242,9 @@ class CGLMBuffer char *m_pActualPseudoBuf; // storage for pseudo buffer char *m_pPseudoBuf; // storage for pseudo buffer char *m_pStaticBuffer; - + char *m_pWebStaging; // browser build: CPU copy standing in for a mapped range + bool m_bWebHasData; // browser build: something was uploaded, so a staged lock must read it back + GLMBuffLockParams m_LockParams; static char ALIGN16 m_StaticBuffers[ GL_MAX_STATIC_BUFFERS ][ GL_STATIC_BUFFER_SIZE ] ALIGN16_POST; diff --git a/public/togles/linuxwin/glfuncs.h b/public/togles/linuxwin/glfuncs.h index b7b9bad049..d1cdb9ab1a 100644 --- a/public/togles/linuxwin/glfuncs.h +++ b/public/togles/linuxwin/glfuncs.h @@ -136,6 +136,7 @@ GL_FUNC(OpenGL,false,void*,glMapBufferRange,(GLenum a,GLintptr b,GLsizeiptr c,GL GL_FUNC_VOID(OpenGL,false,glFlushMappedBufferRange,(GLenum a,GLintptr b,GLsizeiptr c),(a,b,c)) GL_EXT(GL_ARB_vertex_buffer_object,-1,-1) GL_FUNC_VOID(OpenGL,true,glBufferSubData,(GLenum a,GLintptr b,GLsizeiptr c,const GLvoid *d),(a,b,c,d)) +GL_FUNC_VOID(OpenGL,false,glGetBufferSubData,(GLenum a,GLintptr b,GLsizeiptr c,GLvoid *d),(a,b,c,d)) GL_EXT(GL_ARB_occlusion_query,-1,-1) GL_FUNC_VOID(OpenGL,false,glGetQueryObjectuiv,(GLuint a,GLenum b,GLuint *c),(a,b,c)) GL_EXT(GL_APPLE_texture_range,-1,-1) diff --git a/togles/linuxwin/cglmbuffer.cpp b/togles/linuxwin/cglmbuffer.cpp index 91bf92aabb..7f6b5b6e96 100644 --- a/togles/linuxwin/cglmbuffer.cpp +++ b/togles/linuxwin/cglmbuffer.cpp @@ -454,6 +454,8 @@ CGLMBuffer::CGLMBuffer( GLMContext *pCtx, EGLMBufferType type, uint size, uint o m_pLastMappedAddress = NULL; m_pStaticBuffer = NULL; + m_pWebStaging = NULL; + m_bWebHasData = false; m_nPinnedMemoryOfs = -1; m_nPersistentBufferStartOffset = 0; m_bUsingPersistentBuffer = false; @@ -574,6 +576,12 @@ CGLMBuffer::~CGLMBuffer( ) gGL->glDeleteBuffers( 1, &m_nHandle ); } + if ( m_pWebStaging ) + { + free( m_pWebStaging ); + m_pWebStaging = NULL; + } + m_pCtx = NULL; m_nHandle = 0; @@ -846,6 +854,26 @@ void CGLMBuffer::Lock( GLMBuffLockParams *pParams, char **pAddressOut ) // map char *mapPtr; +#ifdef __EMSCRIPTEN__ + // WebGL has no buffer mapping. Emscripten's emulation hands back + // uninitialized malloc memory and uploads all of it on unmap, so any + // byte the engine doesn't rewrite (static mesh modifies, displacement + // index ranges) turned into garbage geometry and colors. Stage the + // range in CPU memory holding the buffer's current contents instead, + // and upload it with glBufferSubData on unlock. + m_pWebStaging = (char*)malloc( pParams->m_nSize ); + if ( m_pWebStaging ) + { + // D3D promises the locked range keeps its contents unless the lock + // discards or promises not to overwrite in-flight data. + if ( !pParams->m_bDiscard && !pParams->m_bNoOverwrite && m_bWebHasData && gGL->glGetBufferSubData ) + gGL->glGetBufferSubData( m_buffGLTarget, pParams->m_nOffset, pParams->m_nSize, m_pWebStaging ); + else + memset( m_pWebStaging, 0, pParams->m_nSize ); + } + mapPtr = m_pWebStaging; +#else + // m_bEnableAsyncMap is actually pParams->m_bNoOverwrite GLbitfield parms = GL_MAP_WRITE_BIT | ( m_bEnableAsyncMap ? GL_MAP_UNSYNCHRONIZED_BIT : 0 ) | ( pParams->m_bDiscard ? GL_MAP_INVALIDATE_BUFFER_BIT : 0 ) | ( m_bEnableExplicitFlush ? GL_MAP_FLUSH_EXPLICIT_BIT : 0 ); @@ -854,6 +882,7 @@ void CGLMBuffer::Lock( GLMBuffLockParams *pParams, char **pAddressOut ) #endif mapPtr = (char*)gGL->glMapBufferRange( m_buffGLTarget, pParams->m_nOffset, pParams->m_nSize, parms); +#endif #ifdef REPORT_LOCK_TIME double flEnd = Plat_FloatTime(); @@ -1052,6 +1081,9 @@ void CGLMBuffer::Unlock( int nActualSize, const void *pActualData ) Assert( nActualSize <= (int)( m_dirtyMaxOffset - m_dirtyMinOffset ) ); glBufferSubDataMaxSize( m_buffGLTarget, m_dirtyMinOffset, nActualSize, pActualData ? pActualData : m_pStaticBuffer ); +#ifdef __EMSCRIPTEN__ + m_bWebHasData = true; +#endif #ifdef REPORT_LOCK_TIME double flEnd = Plat_FloatTime(); @@ -1101,6 +1133,22 @@ void CGLMBuffer::Unlock( int nActualSize, const void *pActualData ) m_pCtx->BindBufferToCtx( m_type, this ); +#ifdef __EMSCRIPTEN__ + if ( m_pWebStaging ) + { + if ( nActualSize ) + { + gGL->glBufferSubData( m_buffGLTarget, m_dirtyMinOffset, nActualSize, m_pWebStaging ); + m_bWebHasData = true; + } + free( m_pWebStaging ); + m_pWebStaging = NULL; + } + m_dirtyMinOffset = m_dirtyMaxOffset = 0; + m_bMapped = false; + return; +#endif + Assert( nActualSize <= (int)( m_dirtyMaxOffset - m_dirtyMinOffset ) ); // time to do explicit flush (currently m_bEnableExplicitFlush is always true) diff --git a/togles/linuxwin/cglmtex.cpp b/togles/linuxwin/cglmtex.cpp index f2d62d839e..5f98bdadc8 100644 --- a/togles/linuxwin/cglmtex.cpp +++ b/togles/linuxwin/cglmtex.cpp @@ -717,6 +717,18 @@ ConVar gl_minimize_rt_tex ( "gl_minimize_rt_tex", "0" ); // if 1, set the GL_TEX ConVar gl_minimize_all_tex ( "gl_minimize_all_tex", "1" ); // if 1, set the GL_TEXTURE_MINIMIZE_STORAGE_APPLE texture parameter to cut off mipmaps for textures which are unmipped ConVar gl_minimize_tex_log ( "gl_minimize_tex_log", "0" ); // if 1, printf the names of the tex that got minimized +#ifdef __EMSCRIPTEN__ +// Dynamic textures (lightmap pages) normally stream through a mapped pixel +// unpack buffer. WebGL can't map buffers: Emscripten's emulation returns fresh +// uninitialized memory on every map and re-uploads the whole slice on unmap, +// so each re-lock of a lightmap page wiped the surfaces written earlier and +// filled the world with garbage lighting. In the browser, dynamic textures +// keep a persistent CPU backing store like managed textures do instead. +#define GLM_DYNAMIC_TEX_USES_PBO( flags ) false +#else +#define GLM_DYNAMIC_TEX_USES_PBO( flags ) ( ( ( flags ) & kGLMTexDynamic ) != 0 ) +#endif + CGLMTex::CGLMTex( GLMContext *ctx, GLMTexLayout *layout, uint levels, const char *debugLabel ) { #if GLMDEBUG @@ -771,7 +783,7 @@ CGLMTex::CGLMTex( GLMContext *ctx, GLMTexLayout *layout, uint levels, const char m_mapped = NULL; m_pbo = 0; - if( m_layout->m_key.m_texFlags & kGLMTexDynamic ) + if( GLM_DYNAMIC_TEX_USES_PBO( m_layout->m_key.m_texFlags ) ) { gGL->glGenBuffers(1, &m_pbo); gGL->glBindBuffer(GL_PIXEL_UNPACK_BUFFER, m_pbo); @@ -3411,14 +3423,8 @@ GLvoid *uncompressDXTc(GLsizei width, GLsizei height, GLenum format, GLsizei ima int pixelsize = 4; if (format == GL_COMPRESSED_RGB_S3TC_DXT1_EXT || format == GL_COMPRESSED_SRGB_S3TC_DXT1_EXT) pixelsize = 3; - // check with the size of the input data stream if the stream is in fact uncompressed - if (imageSize == width*height*pixelsize || data==NULL) { - // uncompressed stream + if (data==NULL) return (GLvoid*)data; - } - // alloc memory - GLvoid *pixels = malloc(((width+3)&~3)*((height+3)&~3)*pixelsize); - // uncompress loop int blocksize; switch (format) { case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: @@ -3427,13 +3433,25 @@ GLvoid *uncompressDXTc(GLsizei width, GLsizei height, GLenum format, GLsizei ima case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT: blocksize = 8; break; - case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT: - case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT: - case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT: - case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT: + default: blocksize = 16; break; } + // Blocks always cover 4x4 texels, so decode into a buffer padded to whole + // blocks and let the caller upload the visible part with that row length. + // (Decoding 2x2 and 1x1 mips with the real width as the row stride made + // neighbouring rows overwrite each other, and a 2x2 DXT5 mip is exactly + // as large as 2x2 RGBA, so it used to be mistaken for uncompressed data.) + const int paddedWidth = (width+3)&~3; + const int paddedHeight = (height+3)&~3; + // check with the size of the input data stream if the stream is in fact uncompressed + if (imageSize != (paddedWidth/4)*(paddedHeight/4)*blocksize && imageSize == width*height*pixelsize) { + // uncompressed stream + return (GLvoid*)data; + } + // alloc memory + GLvoid *pixels = malloc(paddedWidth*paddedHeight*pixelsize); + // uncompress loop uintptr_t src = (uintptr_t) data; for (int y=0; yglPixelStorei( GL_UNPACK_ALIGNMENT, 1 ); + gGL->glPixelStorei( GL_UNPACK_ROW_LENGTH, (width+3)&~3 ); + } gGL->glTexImage2D(target, level, intformat, width, height, border, format, type, pixels); + if( decoded ) + { + gGL->glPixelStorei( GL_UNPACK_ROW_LENGTH, 0 ); + gGL->glPixelStorei( GL_UNPACK_ALIGNMENT, 4 ); + } if( data != pixels ) free(pixels); } @@ -3828,7 +3858,7 @@ void CGLMTex::Lock( GLMTexLockParams *params, char** addressOut, int* yStrideOut // d - the params of the lock request have been saved in the lock table (in the context) // so step 1 is unambiguous. If there's no backing storage, make some. - if (!m_backing && !(m_layout->m_key.m_texFlags & kGLMTexDynamic)) + if (!m_backing && !GLM_DYNAMIC_TEX_USES_PBO( m_layout->m_key.m_texFlags )) { if ( gl_pow2_tempmem.GetBool() ) { @@ -3841,11 +3871,11 @@ void CGLMTex::Lock( GLMTexLockParams *params, char** addressOut, int* yStrideOut unStoragePow2 |= unStoragePow2 >> 8; unStoragePow2 |= unStoragePow2 >> 16; unStoragePow2++; - m_backing = (char *)malloc( unStoragePow2 ); + m_backing = (char *)calloc( 1, unStoragePow2 ); } else { - m_backing = (char *)malloc( m_layout->m_storageTotalSize ); + m_backing = (char *)calloc( 1, m_layout->m_storageTotalSize ); } // clear the kSliceStorageValid bit on all slices @@ -3940,7 +3970,7 @@ void CGLMTex::Lock( GLMTexLockParams *params, char** addressOut, int* yStrideOut desc->m_sliceRegionOffset = offsetInSlice + desc->m_sliceBaseOffset; - if ( (m_layout->m_key.m_texFlags & kGLMTexDynamic) || (params->m_readonly && copyout) ) + if ( GLM_DYNAMIC_TEX_USES_PBO( m_layout->m_key.m_texFlags ) || (params->m_readonly && copyout) ) { // read the whole slice // (odds are we'll never request anything but a whole slice to be read..) @@ -4080,7 +4110,7 @@ void CGLMTex::Unlock( GLMTexLockParams *params ) // because it reuploads the whole thing each slice; we only use 3D textures // for the 32x32x32 colorpsace conversion lookups and debugging the problem // would not save any more memory. - if ( !m_texClientStorage && ( m_texGLTarget == GL_TEXTURE_2D ) && m_backing ) + if ( !m_texClientStorage && ( m_texGLTarget == GL_TEXTURE_2D ) && m_backing && !( m_layout->m_key.m_texFlags & kGLMTexDynamic ) ) { free(m_backing); m_backing = NULL;