diff --git a/public/togles/linuxwin/glmgr.h b/public/togles/linuxwin/glmgr.h index a418d9c120..34d754436b 100644 --- a/public/togles/linuxwin/glmgr.h +++ b/public/togles/linuxwin/glmgr.h @@ -1512,6 +1512,7 @@ class GLMContext IDirect3DVertexDeclaration9 *m_pVertDecl; D3DStreamDesc m_streams[ D3D_MAX_STREAMS ]; uint64 m_vtxAttribMap[2]; + uint m_nBaseVertex; // browser build: base vertex baked into the attrib pointers }; CurAttribs_t m_CurAttribs; @@ -1523,6 +1524,7 @@ class GLMContext memset( m_CurAttribs.m_streams, 0, sizeof( m_CurAttribs.m_streams ) ); m_CurAttribs.m_vtxAttribMap[0] = 0xBBBBBBBBBBBBBBBBULL; m_CurAttribs.m_vtxAttribMap[1] = 0xBBBBBBBBBBBBBBBBULL; + m_CurAttribs.m_nBaseVertex = 0xFFFFFFFF; } FORCEINLINE void ReleasedShader() { NullProgram(); } diff --git a/togles/linuxwin/dxabstract.cpp b/togles/linuxwin/dxabstract.cpp index 19ef1e5dc5..c73dc55dea 100644 --- a/togles/linuxwin/dxabstract.cpp +++ b/togles/linuxwin/dxabstract.cpp @@ -5636,7 +5636,9 @@ HRESULT IDirect3DDevice9::DrawIndexedPrimitive( D3DPRIMITIVETYPE Type,INT BaseVe this->FlushIndexBindings( ); this->FlushVertexBindings( BaseVertexIndex ); - m_ctx->FlushDrawStates( MinVertexIndex, MinVertexIndex + NumVertices - 1, 0 ); + // FlushDrawStates builds the GL vertex attribs from the bound streams, and + // on the web it applies BaseVertexIndex to them (see glmgr_flush.inl). + m_ctx->FlushDrawStates( MinVertexIndex, MinVertexIndex + NumVertices - 1, BaseVertexIndex ); if (gl.m_FogEnable) { diff --git a/togles/linuxwin/glmgr_flush.inl b/togles/linuxwin/glmgr_flush.inl index 21e2f0c0f4..64e69f4fe3 100644 --- a/togles/linuxwin/glmgr_flush.inl +++ b/togles/linuxwin/glmgr_flush.inl @@ -502,7 +502,11 @@ FORCEINLINE void GLMContext::FlushDrawStates( uint nStartIndex, uint nEndIndex, ( m_CurAttribs.m_pVertDecl != m_pDevice->m_pVertDecl ) || ( m_CurAttribs.m_vtxAttribMap[0] != reinterpret_cast(m_pDevice->m_vertexShader->m_vtxAttribMap)[0] ) || ( m_CurAttribs.m_vtxAttribMap[1] != reinterpret_cast(m_pDevice->m_vertexShader->m_vtxAttribMap)[1] ) || - ( memcmp( m_CurAttribs.m_streams, m_pDevice->m_streams, sizeof( m_pDevice->m_streams ) ) != 0 ) ) + ( memcmp( m_CurAttribs.m_streams, m_pDevice->m_streams, sizeof( m_pDevice->m_streams ) ) != 0 ) +#ifdef __EMSCRIPTEN__ + || ( m_CurAttribs.m_nBaseVertex != nBaseVertex ) +#endif + ) { // This branch is taken 52.2% of the time in the L4D2 test1 (long) timedemo. @@ -515,6 +519,7 @@ FORCEINLINE void GLMContext::FlushDrawStates( uint nStartIndex, uint nEndIndex, m_CurAttribs.m_vtxAttribMap[0] = reinterpret_cast(m_pDevice->m_vertexShader->m_vtxAttribMap)[0]; m_CurAttribs.m_vtxAttribMap[1] = reinterpret_cast(m_pDevice->m_vertexShader->m_vtxAttribMap)[1]; memcpy( m_CurAttribs.m_streams, m_pDevice->m_streams, sizeof( m_pDevice->m_streams ) ); + m_CurAttribs.m_nBaseVertex = nBaseVertex; unsigned char *pVertexShaderAttribMap = m_pDevice->m_vertexShader->m_vtxAttribMap; const int nMaxVertexAttributesToCheck = m_drawingProgram[ kGLMVertexProgram ]->m_maxVertexAttrs; @@ -570,6 +575,13 @@ FORCEINLINE void GLMContext::FlushDrawStates( uint nStartIndex, uint nEndIndex, { nBufOffset += pBuf->m_nPersistentBufferStartOffset; } +#ifdef __EMSCRIPTEN__ + // WebGL 2 has no glDrawRangeElementsBaseVertex, so the base vertex + // is applied here by starting every attribute that many vertices + // into its stream. (It used to be dropped entirely: every draw with + // a non-zero base vertex read the wrong vertices.) + nBufOffset += nBaseVertex * pStream->m_stride; +#endif SetBufAndVertexAttribPointer( nIndex, pBuf->GetHandle(), pStream->m_stride, pDeclElem->m_gldecl.m_datatype, pDeclElem->m_gldecl.m_normalized, pDeclElem->m_gldecl.m_nCompCount,