Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions engine/l_studio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions engine/matsys_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" ) )
Expand Down
4 changes: 3 additions & 1 deletion public/togles/linuxwin/cglmbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions public/togles/linuxwin/glfuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
48 changes: 48 additions & 0 deletions togles/linuxwin/cglmbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 );

Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
Expand Down
70 changes: 50 additions & 20 deletions togles/linuxwin/cglmtex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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:
Expand All @@ -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; y<height; y+=4) {
for (int x=0; x<width; x+=4) {
Expand All @@ -3442,15 +3460,15 @@ GLvoid *uncompressDXTc(GLsizei width, GLsizei height, GLenum format, GLsizei ima
case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
DecompressBlockDXT1(x, y, width, (uint8_t*)src, transparent0, simpleAlpha, complexAlpha, (uint32_t*)pixels);
DecompressBlockDXT1(x, y, paddedWidth, (uint8_t*)src, transparent0, simpleAlpha, complexAlpha, (uint32_t*)pixels);
break;
case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
DecompressBlockDXT3(x, y, width, (uint8_t*)src, transparent0, simpleAlpha, complexAlpha, (uint32_t*)pixels);
DecompressBlockDXT3(x, y, paddedWidth, (uint8_t*)src, transparent0, simpleAlpha, complexAlpha, (uint32_t*)pixels);
break;
case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
DecompressBlockDXT5(x, y, width, (uint8_t*)src, transparent0, simpleAlpha, complexAlpha, (uint32_t*)pixels);
DecompressBlockDXT5(x, y, paddedWidth, (uint8_t*)src, transparent0, simpleAlpha, complexAlpha, (uint32_t*)pixels);
break;
}
src+=blocksize;
Expand Down Expand Up @@ -3495,7 +3513,19 @@ void CompressedTexImage2D(GLenum target, GLint level, GLenum internalformat,
intformat = hasAlpha ? GL_SRGB8_ALPHA8 : GL_SRGB8;
}

const bool decoded = pixels && pixels != data;
if( decoded )
{
// decoded texels are tightly packed rows of whole 4x4 blocks
gGL->glPixelStorei( 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);
}
Expand Down Expand Up @@ -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() )
{
Expand All @@ -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
Expand Down Expand Up @@ -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..)
Expand Down Expand Up @@ -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;
Expand Down
Loading