/* ================= idRenderModelOverlay::ReUse ================= */ void idRenderModelOverlay::ReUse() { firstOverlay = 0; nextOverlay = 0; firstDeferredOverlay = 0; nextDeferredOverlay = 0; numOverlayMaterials = 0; for( unsigned int i = 0; i < MAX_OVERLAYS; i++ ) { FreeOverlay( overlays[i] ); } }
DWORD COverlayRenderer::ScheduleThread() { const DWORD eventArraySize = 4; HANDLE handles[eventArraySize]; handles[0] = m_hStopThreadEvent; handles[1] = m_hOverlayTimerIG; handles[2] = m_hOverlayTimerPG; handles[3] = m_hNewOverlayAvailable; DWORD stopThread = WAIT_OBJECT_0; DWORD processOverlayIG = WAIT_OBJECT_0 + 1; DWORD processOverlayPG = WAIT_OBJECT_0 + 2; DWORD newOverlayAvailable = WAIT_OBJECT_0 + 3; while (true) { ScheduleOverlays(); DWORD result = WaitForMultipleObjects(eventArraySize, handles, false, INFINITE); if (result == WAIT_FAILED) return 0; else if (result == stopThread) return 0; else if(result == newOverlayAvailable) { CAutoLock queueLock(&m_csOverlayQueue); ResetEvent(m_hNewOverlayAvailable); #ifdef LOG_DRAWING LogDebug("newOverlayAvailable"); #endif } else if (result == processOverlayIG || result == processOverlayPG) { UINT8 plane = result == processOverlayIG ? BD_OVERLAY_IG : BD_OVERLAY_PG; CAutoLock queueLock(&m_csOverlayQueue); if (!m_overlayQueue[plane].empty()) { ivecOverlayQueue it = m_overlayQueue[plane].begin(); if ((*it)) { #ifdef LOG_DRAWING LogDebug("RENDERING PTS: %6.3f", (CONVERT_90KHz_DS((*it)->pts) + m_rtOffset) / 10000000.0); #endif // close frees all overlays bool freeOverlay = (*it)->cmd != BD_OVERLAY_CLOSE; ProcessOverlay((*it)); if (freeOverlay) FreeOverlay(it); } else FreeOverlay(it); } } } return 0; }
/* ==================== idRenderModelOverlay::CreateOverlayDrawSurf ==================== */ drawSurf_t* idRenderModelOverlay::CreateOverlayDrawSurf( const viewEntity_t* space, const idRenderModel* baseModel, unsigned int index ) { if( index < 0 || index >= numOverlayMaterials ) { return NULL; } // md5 models won't have any surfaces when r_showSkel is set if( baseModel == NULL || baseModel->IsDefaultModel() || baseModel->NumSurfaces() == 0 ) { return NULL; } assert( baseModel->IsDynamicModel() == DM_STATIC ); const idRenderModelStatic* staticModel = static_cast< const idRenderModelStatic* >( baseModel ); const idMaterial* material = overlayMaterials[index]; int maxVerts = 0; int maxIndexes = 0; for( unsigned int i = firstOverlay; i < nextOverlay; i++ ) { const overlay_t& overlay = overlays[i & ( MAX_OVERLAYS - 1 )]; if( overlay.material == material ) { maxVerts += overlay.numVerts; maxIndexes += overlay.numIndexes; } } if( maxVerts == 0 || maxIndexes == 0 ) { return NULL; } // create a new triangle surface in frame memory so it gets automatically disposed of srfTriangles_t* newTri = ( srfTriangles_t* )R_ClearedFrameAlloc( sizeof( *newTri ), FRAME_ALLOC_SURFACE_TRIANGLES ); newTri->staticModelWithJoints = ( staticModel->jointsInverted != NULL ) ? const_cast< idRenderModelStatic* >( staticModel ) : NULL; // allow GPU skinning newTri->ambientCache = vertexCache.AllocVertex( NULL, ALIGN( maxVerts * sizeof( idDrawVert ), VERTEX_CACHE_ALIGN ) ); newTri->indexCache = vertexCache.AllocIndex( NULL, ALIGN( maxIndexes * sizeof( triIndex_t ), INDEX_CACHE_ALIGN ) ); idDrawVert* mappedVerts = ( idDrawVert* )vertexCache.MappedVertexBuffer( newTri->ambientCache ); triIndex_t* mappedIndexes = ( triIndex_t* )vertexCache.MappedIndexBuffer( newTri->indexCache ); int numVerts = 0; int numIndexes = 0; for( unsigned int i = firstOverlay; i < nextOverlay; i++ ) { overlay_t& overlay = overlays[i & ( MAX_OVERLAYS - 1 )]; if( overlay.numVerts == 0 ) { if( i == firstOverlay ) { firstOverlay++; } continue; } if( overlay.material != material ) { continue; } // get the source model surface for this overlay surface const modelSurface_t* baseSurf = ( overlay.surfaceNum < staticModel->NumSurfaces() ) ? staticModel->Surface( overlay.surfaceNum ) : NULL; // if the surface ids no longer match if( baseSurf == NULL || baseSurf->id != overlay.surfaceId ) { // find the surface with the correct id if( staticModel->FindSurfaceWithId( overlay.surfaceId, overlay.surfaceNum ) ) { baseSurf = staticModel->Surface( overlay.surfaceNum ); } else { // the surface with this id no longer exists FreeOverlay( overlay ); if( i == firstOverlay ) { firstOverlay++; } continue; } } // check for out of range vertex references const srfTriangles_t* baseTri = baseSurf->geometry; if( overlay.maxReferencedVertex >= baseTri->numVerts ) { // This can happen when playing a demofile and a model has been changed since it was recorded, so just issue a warning and go on. common->Warning( "idRenderModelOverlay::CreateOverlayDrawSurf: overlay vertex out of range. Model has probably changed since generating the overlay." ); FreeOverlay( overlay ); if( i == firstOverlay ) { firstOverlay++; } continue; } // use SIMD optimized routine to copy the vertices and indices directly to write-combined memory R_CopyOverlaySurface( mappedVerts, numVerts, mappedIndexes, numIndexes, &overlay, baseTri->verts ); numIndexes += overlay.numIndexes; numVerts += overlay.numVerts; } newTri->numVerts = numVerts; newTri->numIndexes = numIndexes; // create the drawsurf drawSurf_t* drawSurf = ( drawSurf_t* )R_FrameAlloc( sizeof( *drawSurf ), FRAME_ALLOC_DRAW_SURFACE ); drawSurf->frontEndGeo = newTri; drawSurf->numIndexes = newTri->numIndexes; drawSurf->ambientCache = newTri->ambientCache; drawSurf->indexCache = newTri->indexCache; drawSurf->shadowCache = 0; drawSurf->space = space; drawSurf->scissorRect = space->scissorRect; drawSurf->extraGLState = 0; drawSurf->renderZFail = 0; R_SetupDrawSurfShader( drawSurf, material, &space->entityDef->parms ); R_SetupDrawSurfJoints( drawSurf, newTri, NULL ); return drawSurf; }
/* ===================== idRenderModelOverlay::CreateOverlay This projects on both front and back sides to avoid seams The material should be clamped, because entire triangles are added, some of which may extend well past the 0.0 to 1.0 texture range ===================== */ void idRenderModelOverlay::CreateOverlay( const idRenderModel* model, const idPlane localTextureAxis[2], const idMaterial* material ) { // count up the maximum possible vertices and indexes per surface int maxVerts = 0; int maxIndexes = 0; for( int surfNum = 0; surfNum < model->NumSurfaces(); surfNum++ ) { const modelSurface_t* surf = model->Surface( surfNum ); if( surf->geometry->numVerts > maxVerts ) { maxVerts = surf->geometry->numVerts; } if( surf->geometry->numIndexes > maxIndexes ) { maxIndexes = surf->geometry->numIndexes; } } maxIndexes += 3 * 16 / sizeof( triIndex_t ); // to allow the index size to be a multiple of 16 bytes // make temporary buffers for the building process idTempArray< byte > cullBits( maxVerts ); idTempArray< halfFloat_t > texCoordS( maxVerts ); idTempArray< halfFloat_t > texCoordT( maxVerts ); idTempArray< triIndex_t > vertexRemap( maxVerts ); idTempArray< overlayVertex_t > overlayVerts( maxVerts ); idTempArray< triIndex_t > overlayIndexes( maxIndexes ); // pull out the triangles we need from the base surfaces for( int surfNum = 0; surfNum < model->NumBaseSurfaces(); surfNum++ ) { const modelSurface_t* surf = model->Surface( surfNum ); if( surf->geometry == NULL || surf->shader == NULL ) { continue; } // some surfaces can explicitly disallow overlays if( !surf->shader->AllowOverlays() ) { continue; } const srfTriangles_t* tri = surf->geometry; // try to cull the whole surface along the first texture axis const float d0 = tri->bounds.PlaneDistance( localTextureAxis[0] ); if( d0 < 0.0f || d0 > 1.0f ) { continue; } // try to cull the whole surface along the second texture axis const float d1 = tri->bounds.PlaneDistance( localTextureAxis[1] ); if( d1 < 0.0f || d1 > 1.0f ) { continue; } if( tri->staticModelWithJoints != NULL && r_useGPUSkinning.GetBool() ) { R_OverlayPointCullSkinned( cullBits.Ptr(), texCoordS.Ptr(), texCoordT.Ptr(), localTextureAxis, tri->verts, tri->numVerts, tri->staticModelWithJoints->jointsInverted ); } else { R_OverlayPointCullStatic( cullBits.Ptr(), texCoordS.Ptr(), texCoordT.Ptr(), localTextureAxis, tri->verts, tri->numVerts ); } // start streaming the indexes idODSStreamedArray< triIndex_t, 256, SBT_QUAD, 3 > indexesODS( tri->indexes, tri->numIndexes ); memset( vertexRemap.Ptr(), -1, vertexRemap.Size() ); int numIndexes = 0; int numVerts = 0; int maxReferencedVertex = 0; // find triangles that need the overlay for( int i = 0; i < tri->numIndexes; ) { const int nextNumIndexes = indexesODS.FetchNextBatch() - 3; for( ; i <= nextNumIndexes; i += 3 ) { const int i0 = indexesODS[i + 0]; const int i1 = indexesODS[i + 1]; const int i2 = indexesODS[i + 2]; // skip triangles completely off one side if( cullBits[i0] & cullBits[i1] & cullBits[i2] ) { continue; } // we could do more precise triangle culling, like a light interaction does, but it's not worth it // keep this triangle for( int j = 0; j < 3; j++ ) { int index = tri->indexes[i + j]; if( vertexRemap[index] == ( triIndex_t ) - 1 ) { vertexRemap[index] = numVerts; overlayVerts[numVerts].vertexNum = index; overlayVerts[numVerts].st[0] = texCoordS[index]; overlayVerts[numVerts].st[1] = texCoordT[index]; numVerts++; maxReferencedVertex = Max( maxReferencedVertex, index ); } overlayIndexes[numIndexes] = vertexRemap[index]; numIndexes++; } } } if( numIndexes == 0 ) { continue; } // add degenerate triangles until the index size is a multiple of 16 bytes for( ; ( ( ( numIndexes * sizeof( triIndex_t ) ) & 15 ) != 0 ); numIndexes += 3 ) { overlayIndexes[numIndexes + 0] = 0; overlayIndexes[numIndexes + 1] = 0; overlayIndexes[numIndexes + 2] = 0; } // allocate a new overlay overlay_t& overlay = overlays[nextOverlay++ & ( MAX_OVERLAYS - 1 )]; FreeOverlay( overlay ); overlay.material = material; overlay.surfaceNum = surfNum; overlay.surfaceId = surf->id; overlay.numIndexes = numIndexes; overlay.indexes = ( triIndex_t* )Mem_Alloc( numIndexes * sizeof( overlay.indexes[0] ), TAG_MODEL ); memcpy( overlay.indexes, overlayIndexes.Ptr(), numIndexes * sizeof( overlay.indexes[0] ) ); overlay.numVerts = numVerts; overlay.verts = ( overlayVertex_t* )Mem_Alloc( numVerts * sizeof( overlay.verts[0] ), TAG_MODEL ); memcpy( overlay.verts, overlayVerts.Ptr(), numVerts * sizeof( overlay.verts[0] ) ); overlay.maxReferencedVertex = maxReferencedVertex; if( nextOverlay - firstOverlay > MAX_OVERLAYS ) { firstOverlay = nextOverlay - MAX_OVERLAYS; } } }