Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

perf(ww3d2): skin fill on the job pool, batched shadow decals, cached lights

Olcay Seygan39b7ca043115
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8d8727cc9..78503f593 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -161,6 +161,7 @@ found and fixed — EA's own, not port damage.**
- The radar's under-attack pulse throbs instead of strobing, and no longer ends early.
- The main menu surf rolls at its proper pace, not ten times too fast.
- And it is there at all again. The waves breaking on the main menu's beach had been worked out every frame and then drawn nowhere since v1.0.0, so the shore behind the menu was flat water.
+- Less work per picture in a big battle. Infantry and other bending models are shaped on every core at once instead of one, the shadow blobs and markers on the ground go to the graphics card in one batch instead of two uploads each, and a material or light identical to the one already set is no longer sent again. What is drawn does not change.
## The computer opponent builds a base now
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DProjectedShadow.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DProjectedShadow.cpp
index c9bfaeef4..a90a1d590 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DProjectedShadow.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DProjectedShadow.cpp
@@ -133,8 +133,37 @@ int nShadowDecalVertsInBatch=0;
#define DECAL_AIRBORNE_HEIGHT 1.0f
#define DECAL_AIRBORNE_BLEND 10.0f
#define DECAL_MAX_STRETCH 4.0f
-int SHADOW_DECAL_VERTEX_SIZE=32768;
-int SHADOW_DECAL_INDEX_SIZE=65536;
+enum { SHADOW_DECAL_VERTEX_COUNT = 32768, SHADOW_DECAL_INDEX_COUNT = 65536 };
+int SHADOW_DECAL_VERTEX_SIZE=SHADOW_DECAL_VERTEX_COUNT;
+int SHADOW_DECAL_INDEX_SIZE=SHADOW_DECAL_INDEX_COUNT;
+
+/* A batch of decals is written here and goes to the buffers in one lock when flushDecals draws it.
+ Locking the buffers once per decal, twice for each device, cost more than drawing them: a late
+ game puts a few hundred blobs and markers on the ground in a handful of batches. The batch holds
+ the same vertices and indices the per-decal locks wrote, at the same places relative to its start,
+ so what is drawn does not change. */
+static SHADOW_DECAL_VERTEX decalStageVertices[SHADOW_DECAL_VERTEX_COUNT];
+static UnsignedShort decalStageIndices[SHADOW_DECAL_INDEX_COUNT];
+
+/** Copy one staged range into a streaming buffer and its Direct3D 11 twin. While Direct3D 11
+ presents, Direct3D 9 draws nothing indexed, so its buffer is left alone and the twin is written
+ in place; a twin the device refused keeps the Direct3D 9 copy, as the per-decal locks did. */
+template <class D3D9_BUFFER>
+static Bool uploadDecalRange(D3D9_BUFFER *d3d9Buffer, DX11BufferTwinClass *twin, const void *source,
+ UnsignedInt byteOffset, UnsignedInt byteCount, UnsignedInt lockFlags)
+{
+ const Bool d3d9Draws = !Direct3D11_Present_Is_Enabled() || twin == NULL;
+ void *d3d9Memory = NULL;
+ if (d3d9Draws && d3d9Buffer->Lock(byteOffset, byteCount, &d3d9Memory, lockFlags) != D3D_OK)
+ return FALSE; //a lost device refuses the lock
+ DX11BufferLockClass lock;
+ void *target = lock.Begin(twin, d3d9Memory, byteOffset, byteCount, lockFlags, d3d9Draws);
+ memcpy(target != NULL ? target : d3d9Memory, source, byteCount);
+ lock.End();
+ if (d3d9Draws)
+ d3d9Buffer->Unlock();
+ return TRUE;
+}
class W3DShadowTexture; //forward reference
@@ -767,6 +796,30 @@ void W3DProjectedShadowManager::flushDecals(W3DShadowTexture *texture, ShadowTyp
LPDIRECT3DDEVICE9 m_pDev=DX8Wrapper::_Get_D3D_Device();
if (!m_pDev) return; //no D3D Device to render
+ //The staged batch goes after what this frame already drew, or to the front of a fresh buffer when
+ //it does not fit. renderShadows starts each frame past the end, so its first batch discards.
+ const Int numIndex = nShadowDecalPolysInBatch*3;
+ UnsignedInt vertexFlags = D3DLOCK_NOOVERWRITE;
+ if (nShadowDecalVertsInBuf > (SHADOW_DECAL_VERTEX_SIZE-nShadowDecalVertsInBatch))
+ { nShadowDecalVertsInBuf = 0;
+ vertexFlags = D3DLOCK_DISCARD;
+ }
+ UnsignedInt indexFlags = D3DLOCK_NOOVERWRITE;
+ if (nShadowDecalIndicesInBuf > (SHADOW_DECAL_INDEX_SIZE-numIndex))
+ { nShadowDecalIndicesInBuf = 0;
+ indexFlags = D3DLOCK_DISCARD;
+ }
+ nShadowDecalStartBatchVertex = nShadowDecalVertsInBuf;
+ nShadowDecalStartBatchIndex = nShadowDecalIndicesInBuf;
+ const Bool uploaded =
+ uploadDecalRange(shadowDecalVertexBufferD3D, shadowDecalVertexTwin, decalStageVertices,
+ nShadowDecalVertsInBuf*sizeof(SHADOW_DECAL_VERTEX), nShadowDecalVertsInBatch*sizeof(SHADOW_DECAL_VERTEX),
+ vertexFlags)
+ && uploadDecalRange(shadowDecalIndexBufferD3D, shadowDecalIndexTwin, decalStageIndices,
+ nShadowDecalIndicesInBuf*sizeof(UnsignedShort), numIndex*sizeof(UnsignedShort), indexFlags);
+ nShadowDecalVertsInBuf += nShadowDecalVertsInBatch;
+ nShadowDecalIndicesInBuf += numIndex;
+
VertexMaterialClass *vmat=VertexMaterialClass::Get_Preset(VertexMaterialClass::PRELIT_DIFFUSE);
DX8Wrapper::Set_Material(vmat);
REF_PTR_RELEASE(vmat);
@@ -838,7 +891,7 @@ void W3DProjectedShadowManager::flushDecals(W3DShadowTexture *texture, ShadowTyp
*/
//m_pDev->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE ); //useful to see bounds
- if (DX8Wrapper::_Is_Triangle_Draw_Enabled())
+ if (uploaded && DX8Wrapper::_Is_Triangle_Draw_Enabled())
{
Debug_Statistics::Record_DX8_Polys_And_Vertices(nShadowDecalPolysInBatch,nShadowDecalVertsInBatch,ShaderClass::_PresetOpaqueShader);
if (!Direct3D11_Present_Is_Enabled())
@@ -863,8 +916,6 @@ void W3DProjectedShadowManager::flushDecals(W3DShadowTexture *texture, ShadowTyp
m_pDev->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
m_pDev->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
*/
- nShadowDecalStartBatchVertex=nShadowDecalVertsInBuf;
- nShadowDecalStartBatchIndex=nShadowDecalIndicesInBuf;
nShadowDecalPolysInBatch=0; //reset number of polys in texture batch
nShadowDecalVertsInBatch=0;
}
@@ -1182,37 +1233,13 @@ void W3DProjectedShadowManager::queueDecal(W3DProjectedShadow *shadow, Bool sunC
Int numVerts = vertsPerRow *vertsPerColumn; //number of terrain vertices
Int numIndex=(endX - startX) * (endY-startY)*6; //6 indices per terrain cell (2 triangles).
- SHADOW_DECAL_VERTEX* pvVertices;
- UnsignedShort *pvIndices;
-
- DX11BufferLockClass vertexLock;
- const UnsignedInt vertexStride = sizeof(SHADOW_DECAL_VERTEX);
- UnsignedInt vertexOffset;
- UnsignedInt vertexFlags;
- if (nShadowDecalVertsInBuf > (SHADOW_DECAL_VERTEX_SIZE-numVerts)) //check if room for model verts
- { //flush the buffer by drawing the contents and re-locking again
+ //a batch that cannot take this decal is drawn first
+ if (nShadowDecalVertsInBatch > (SHADOW_DECAL_VERTEX_SIZE-numVerts) ||
+ nShadowDecalPolysInBatch*3 > (SHADOW_DECAL_INDEX_SIZE-numIndex))
flushDecals(shadow->m_shadowTexture[0], shadow->m_type);
- vertexOffset=0;
- vertexFlags=D3DLOCK_DISCARD;
- if (shadowDecalVertexBufferD3D->Lock(0,numVerts*vertexStride,(void**)&pvVertices,D3DLOCK_DISCARD) != D3D_OK)
- return;
-
- nShadowDecalStartBatchVertex=0;
- nShadowDecalPolysInBatch=0; //reset number of polys in texture batch
- nShadowDecalVertsInBatch=0;
- nShadowDecalVertsInBuf=0;
- }
- else
- { vertexOffset=nShadowDecalVertsInBuf*vertexStride;
- vertexFlags=D3DLOCK_NOOVERWRITE;
- if (shadowDecalVertexBufferD3D->Lock(vertexOffset,numVerts*vertexStride, (void**)&pvVertices,D3DLOCK_NOOVERWRITE) != D3D_OK)
- return;
- }
- void *vertexTarget = vertexLock.Begin(shadowDecalVertexTwin, pvVertices, vertexOffset,
- numVerts*vertexStride, vertexFlags);
- if (vertexTarget != NULL)
- pvVertices = (SHADOW_DECAL_VERTEX *)vertexTarget;
+ SHADOW_DECAL_VERTEX* pvVertices = decalStageVertices + nShadowDecalVertsInBatch;
+ UnsignedShort *pvIndices = decalStageIndices + nShadowDecalPolysInBatch*3;
//code to deal with rotated shadows based on sun direction, fix this later. For now shadow rotates with object rotation.
//shadow->m_shadowTexture[0]->getDecalUVAxis(&uVector,&vVector);
@@ -1268,40 +1295,6 @@ void W3DProjectedShadowManager::queueDecal(W3DProjectedShadow *shadow, Bool sunC
}
}
- vertexLock.End();
- shadowDecalVertexBufferD3D->Unlock();
-
- DX11BufferLockClass indexLock;
- UnsignedInt indexOffset;
- UnsignedInt indexFlags;
- if (nShadowDecalIndicesInBuf > (SHADOW_DECAL_INDEX_SIZE-numIndex)) //check if room for model verts
- { //flush the buffer by drawing the contents and re-locking again
- flushDecals(shadow->m_shadowTexture[0], shadow->m_type);
-
- indexOffset=0;
- indexFlags=D3DLOCK_DISCARD;
- if (shadowDecalIndexBufferD3D->Lock(0,numIndex*sizeof(short),(void**)&pvIndices,D3DLOCK_DISCARD) != D3D_OK)
- return;
-
- nShadowDecalStartBatchIndex=0;
- nShadowDecalPolysInBatch=0; //reset number of polys in texture batch
- nShadowDecalVertsInBatch=0;
- nShadowDecalIndicesInBuf=0;
- }
- else
- { indexOffset=nShadowDecalIndicesInBuf*sizeof(short);
- indexFlags=D3DLOCK_NOOVERWRITE;
- if (shadowDecalIndexBufferD3D->Lock(indexOffset,numIndex*sizeof(short), (void**)&pvIndices,D3DLOCK_NOOVERWRITE) != D3D_OK)
- return;
- }
-
- void *indexTarget = indexLock.Begin(shadowDecalIndexTwin, pvIndices, indexOffset,
- numIndex*sizeof(short), indexFlags);
- if (indexTarget != NULL)
- pvIndices = (UnsignedShort *)indexTarget;
-
- try {
- if(pvIndices)
{ //fill each cell's vertex indices
Int rowStart;
for (j=startY,rowStart=0; j<endY; j++,rowStart+=vertsPerRow)
@@ -1329,22 +1322,10 @@ void W3DProjectedShadowManager::queueDecal(W3DProjectedShadow *shadow, Bool sunC
}
}
}
- IndexBufferExceptionFunc();
- } catch(...) {
- IndexBufferExceptionFunc();
- }
- indexLock.End();
- shadowDecalIndexBufferD3D->Unlock();
Int numPolys = (endX - startX)*(endY - startY)*2; //2 triangles per cell
nShadowDecalPolysInBatch += numPolys;
-
- nShadowDecalVertsInBuf += numVerts;
nShadowDecalVertsInBatch += numVerts;
-// nShadowDecalStartBatchVertex=nShadowDecalVertsInBuf;
-
- nShadowDecalIndicesInBuf += numIndex;
-// nShadowDecalStartBatchIndex=nShadowDecalIndicesInBuf;
return;
}
@@ -1389,37 +1370,13 @@ void W3DProjectedShadowManager::queueSimpleDecal(W3DProjectedShadow *shadow)
Int numVerts = 4; //number of decal vertices
Int numIndex=6; //(2 triangles).
- SHADOW_DECAL_VERTEX* pvVertices;
- UnsignedShort *pvIndices;
-
- DX11BufferLockClass vertexLock;
- const UnsignedInt vertexStride = sizeof(SHADOW_DECAL_VERTEX);
- UnsignedInt vertexOffset;
- UnsignedInt vertexFlags;
- if (nShadowDecalVertsInBuf > (SHADOW_DECAL_VERTEX_SIZE-numVerts)) //check if room for model verts
- { //flush the buffer by drawing the contents and re-locking again
+ //see queueDecal
+ if (nShadowDecalVertsInBatch > (SHADOW_DECAL_VERTEX_SIZE-numVerts) ||
+ nShadowDecalPolysInBatch*3 > (SHADOW_DECAL_INDEX_SIZE-numIndex))
flushDecals(shadow->m_shadowTexture[0], shadow->m_type);
- vertexOffset=0;
- vertexFlags=D3DLOCK_DISCARD;
- if (shadowDecalVertexBufferD3D->Lock(0,numVerts*vertexStride,(void**)&pvVertices,D3DLOCK_DISCARD) != D3D_OK)
- return;
-
- nShadowDecalStartBatchVertex=0;
- nShadowDecalPolysInBatch=0; //reset number of polys in texture batch
- nShadowDecalVertsInBatch=0;
- nShadowDecalVertsInBuf=0;
- }
- else
- { vertexOffset=nShadowDecalVertsInBuf*vertexStride;
- vertexFlags=D3DLOCK_NOOVERWRITE;
- if (shadowDecalVertexBufferD3D->Lock(vertexOffset,numVerts*vertexStride, (void**)&pvVertices,D3DLOCK_NOOVERWRITE) != D3D_OK)
- return;
- }
- void *vertexTarget = vertexLock.Begin(shadowDecalVertexTwin, pvVertices, vertexOffset,
- numVerts*vertexStride, vertexFlags);
- if (vertexTarget != NULL)
- pvVertices = (SHADOW_DECAL_VERTEX *)vertexTarget;
+ SHADOW_DECAL_VERTEX* pvVertices = decalStageVertices + nShadowDecalVertsInBatch;
+ UnsignedShort *pvIndices = decalStageIndices + nShadowDecalPolysInBatch*3;
objPos.Z=groundHeight; //force decal to ground level
objPos += groundNormal * 1.0f; //offset decal slightly above terrain to reduce z-fighting.
@@ -1464,63 +1421,16 @@ void W3DProjectedShadowManager::queueSimpleDecal(W3DProjectedShadow *shadow)
pvVertices++;
}
- vertexLock.End();
- shadowDecalVertexBufferD3D->Unlock();
-
- DX11BufferLockClass indexLock;
- UnsignedInt indexOffset;
- UnsignedInt indexFlags;
- if (nShadowDecalIndicesInBuf > (SHADOW_DECAL_INDEX_SIZE-numIndex)) //check if room for model verts
- { //flush the buffer by drawing the contents and re-locking again
- flushDecals(shadow->m_shadowTexture[0],shadow->m_type);
-
- indexOffset=0;
- indexFlags=D3DLOCK_DISCARD;
- if (shadowDecalIndexBufferD3D->Lock(0,numIndex*sizeof(short),(void**)&pvIndices,D3DLOCK_DISCARD) != D3D_OK)
- return;
-
- nShadowDecalStartBatchIndex=0;
- nShadowDecalPolysInBatch=0; //reset number of polys in texture batch
- nShadowDecalVertsInBatch=0;
- nShadowDecalIndicesInBuf=0;
- }
- else
- { indexOffset=nShadowDecalIndicesInBuf*sizeof(short);
- indexFlags=D3DLOCK_NOOVERWRITE;
- if (shadowDecalIndexBufferD3D->Lock(indexOffset,numIndex*sizeof(short), (void**)&pvIndices,D3DLOCK_NOOVERWRITE) != D3D_OK)
- return;
- }
-
- void *indexTarget = indexLock.Begin(shadowDecalIndexTwin, pvIndices, indexOffset,
- numIndex*sizeof(short), indexFlags);
- if (indexTarget != NULL)
- pvIndices = (UnsignedShort *)indexTarget;
-
- try {
- if(pvIndices)
- { pvIndices[0]=nShadowDecalVertsInBatch;
- pvIndices[1]=nShadowDecalVertsInBatch+1;
- pvIndices[2]=nShadowDecalVertsInBatch+2;
- pvIndices[3]=nShadowDecalVertsInBatch;
- pvIndices[4]=nShadowDecalVertsInBatch+2;
- pvIndices[5]=nShadowDecalVertsInBatch+3;
- pvIndices += 6;
- }
- IndexBufferExceptionFunc();
- } catch(...) {
- IndexBufferExceptionFunc();
- }
-
- indexLock.End();
- shadowDecalIndexBufferD3D->Unlock();
+ pvIndices[0]=nShadowDecalVertsInBatch;
+ pvIndices[1]=nShadowDecalVertsInBatch+1;
+ pvIndices[2]=nShadowDecalVertsInBatch+2;
+ pvIndices[3]=nShadowDecalVertsInBatch;
+ pvIndices[4]=nShadowDecalVertsInBatch+2;
+ pvIndices[5]=nShadowDecalVertsInBatch+3;
Int numPolys = 2; //2 triangles per decal
nShadowDecalPolysInBatch += numPolys;
-
- nShadowDecalVertsInBuf += numVerts;
nShadowDecalVertsInBatch += numVerts;
-
- nShadowDecalIndicesInBuf += numIndex;
return;
}
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8renderer.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8renderer.cpp
index f948ef99b..697c1bfe0 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8renderer.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8renderer.cpp
@@ -63,6 +63,7 @@
#include "camera.h"
#include "stripoptimizer.h"
#include "meshgeometry.h"
+#include "sortingrenderer.h"
/*
** Global Instance of the DX8MeshRender
@@ -73,6 +74,7 @@ bool DX8TextureCategoryClass::m_gForceMultiply = false; // Forces opaque materia
static DynamicVectorClass<Vector3> _TempVertexBuffer;
static DynamicVectorClass<Vector3> _TempNormalBuffer;
+static DynamicVectorClass<MeshClass *> _SkinFillMeshes;
static MultiListClass<MeshModelClass> _RegisteredMeshList;
static TextureCategoryList texture_category_delete_list;
@@ -1297,6 +1299,75 @@ void DX8SkinFVFCategoryContainer::Log(bool only_visible)
// ----------------------------------------------------------------------------
+// Skins a thread claims at once; a skinned mesh is a few hundred vertices.
+static const int SKINS_PER_CLAIM=2;
+
+struct SkinFillJob
+{
+ MeshClass **meshes;
+ VertexFormatXYZNDUV2 *dest_verts;
+ Vector3 *loc;
+ Vector3 *norm;
+};
+
+// One skin, deformed and written into its slice of the vertex buffer. Runs on the job pool: it
+// reads the mesh and its pose, writes only from its base vertex offset on, and allocates nothing,
+// so the bytes are the ones the single-threaded fill wrote.
+static void Fill_Skin(int index,void *context)
+{
+ SkinFillJob *job=(SkinFillJob *)context;
+ MeshClass *mesh=job->meshes[index];
+ MeshModelClass *mmc=mesh->Peek_Model();
+ const int mesh_vertex_count=mmc->Get_Vertex_Count();
+ const int vertex_offset=mesh->Get_Base_Vertex_Offset();
+
+ Vector3* loc=job->loc+vertex_offset;
+ Vector3* norm=job->norm+vertex_offset;
+ const Vector2* uv0=mmc->Get_UV_Array_By_Index(0);
+ const Vector2* uv1=mmc->Get_UV_Array_By_Index(1);
+ const unsigned* diffuse=mmc->Get_Color_Array(0,false);
+
+ VertexFormatXYZNDUV2* verts=job->dest_verts+vertex_offset;
+
+ mesh->Get_Deformed_Vertices(loc,norm);
+
+ for (int v=0;v<mesh_vertex_count;++v) {
+ verts[v].x=(*loc)[0];
+ verts[v].y=(*loc)[1];
+ verts[v].z=(*loc)[2];
+ verts[v].nx=(*norm)[0];
+ verts[v].ny=(*norm)[1];
+ verts[v].nz=(*norm)[2];
+ if (diffuse) {
+ verts[v].diffuse=*diffuse++;
+ }
+ else {
+ verts[v].diffuse=0;
+ }
+ if (uv0) {
+ verts[v].u1=(*uv0)[0];
+ verts[v].v1=(*uv0)[1];
+ uv0++;
+ }
+ else {
+ verts[v].u1=0.0f;
+ verts[v].v1=0.0f;
+ }
+ if (uv1) {
+ verts[v].u2=(*uv1)[0];
+ verts[v].v2=(*uv1)[1];
+ uv1++;
+ }
+ else {
+ verts[v].u2=0.0f;
+ verts[v].v2=0.0f;
+ }
+
+ loc++;
+ norm++;
+ }
+}
+
void DX8SkinFVFCategoryContainer::Render(void)
{
SNAPSHOT_SAY(("DX8SkinFVFCategoryContainer::Render()\n"));
@@ -1324,6 +1395,11 @@ void DX8SkinFVFCategoryContainer::Render(void)
unsigned int renderedVertexCount=0;
+ // Every mesh deforms into its own slice of the scratch buffers, so the fill can run on the job
+ // pool; sized here because a job must not allocate.
+ if (_TempVertexBuffer.Length() < (int)maxVertexCount) _TempVertexBuffer.Resize(maxVertexCount);
+ if (_TempNormalBuffer.Length() < (int)maxVertexCount) _TempNormalBuffer.Resize(maxVertexCount);
+
MeshClass * mesh = VisibleSkinHead;
MeshClass * remainingMesh = VisibleSkinHead;
while (renderedVertexCount < VisibleVertexCount)
@@ -1332,6 +1408,7 @@ void DX8SkinFVFCategoryContainer::Render(void)
VertexFormatXYZNDUV2 * dest_verts = l.Get_Formatted_Vertex_Array();
unsigned vertex_offset=0;
remainingMesh = NULL;
+ _SkinFillMeshes.Reset_Active();
while (mesh != NULL) {
@@ -1353,61 +1430,20 @@ void DX8SkinFVFCategoryContainer::Render(void)
WWASSERT((vertex_offset+mesh_vertex_count)<=VisibleVertexCount);
DX8_RECORD_SKIN_RENDER(mesh->Get_Num_Polys(),mesh_vertex_count);
- if (_TempVertexBuffer.Length() < mesh_vertex_count) _TempVertexBuffer.Resize(mesh_vertex_count);
- if (_TempNormalBuffer.Length() < mesh_vertex_count) _TempNormalBuffer.Resize(mesh_vertex_count);
-
- Vector3* loc=&(_TempVertexBuffer[0]);
- Vector3* norm=&(_TempNormalBuffer[0]);
- const Vector2* uv0=mmc->Get_UV_Array_By_Index(0);
- const Vector2* uv1=mmc->Get_UV_Array_By_Index(1);
- const unsigned* diffuse=mmc->Get_Color_Array(0,false);
-
- VertexFormatXYZNDUV2* verts=dest_verts+vertex_offset;
-
- mesh->Get_Deformed_Vertices(loc,norm);
-
- for (int v=0;v<mesh_vertex_count;++v) {
- verts[v].x=(*loc)[0];
- verts[v].y=(*loc)[1];
- verts[v].z=(*loc)[2];
- verts[v].nx=(*norm)[0];
- verts[v].ny=(*norm)[1];
- verts[v].nz=(*norm)[2];
- if (diffuse) {
- verts[v].diffuse=*diffuse++;
- }
- else {
- verts[v].diffuse=0;
- }
- if (uv0) {
- verts[v].u1=(*uv0)[0];
- verts[v].v1=(*uv0)[1];
- uv0++;
- }
- else {
- verts[v].u1=0.0f;
- verts[v].v1=0.0f;
- }
- if (uv1) {
- verts[v].u2=(*uv1)[0];
- verts[v].v2=(*uv1)[1];
- uv1++;
- }
- else {
- verts[v].u2=0.0f;
- verts[v].v2=0.0f;
- }
-
- loc++;
- norm++;
- }
-
mesh->Set_Base_Vertex_Offset(vertex_offset);
+ _SkinFillMeshes.Add(mesh);
vertex_offset+=mesh_vertex_count;
renderedVertexCount += mesh_vertex_count;
-
+
mesh = mesh->Peek_Next_Visible_Skin();
} //while
+
+ SkinFillJob job;
+ job.meshes=&_SkinFillMeshes[0];
+ job.dest_verts=dest_verts;
+ job.loc=&_TempVertexBuffer[0];
+ job.norm=&_TempNormalBuffer[0];
+ SortingRendererClass::Parallel_For(_SkinFillMeshes.Count(),SKINS_PER_CLAIM,Fill_Skin,&job);
}//lock
SNAPSHOT_SAY(("Set vb: %x ib: %x\n",vb,index_buffer));
@@ -2009,6 +2045,7 @@ void DX8MeshRendererClass::Shutdown(void)
Clear_Pending_Delete_Lists();
_TempVertexBuffer.Clear(); //free memory
_TempNormalBuffer.Clear();
+ _SkinFillMeshes.Clear();
}
// ----------------------------------------------------------------------------
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp
index c41a09706..2049d9112 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.cpp
@@ -328,6 +328,10 @@ unsigned DX8Wrapper::texture_stage_state_changes = 0;
unsigned DX8Wrapper::draw_calls = 0;
unsigned DX8Wrapper::_MainThreadID = 0;
bool DX8Wrapper::CurrentDX8LightEnables[4];
+D3DMATERIAL9 DX8Wrapper::CurrentDX8Material;
+bool DX8Wrapper::CurrentDX8MaterialValid = false;
+D3DLIGHT9 DX8Wrapper::CurrentDX8Lights[4];
+unsigned DX8Wrapper::CurrentDX8LightsValid = 0;
bool DX8Wrapper::D3D9StatePending = false;
unsigned DX8Wrapper::PendingRenderStates[256/32];
unsigned DX8Wrapper::PendingTextureStageStates[MAX_TEXTURE_STAGES];
@@ -714,6 +718,8 @@ void DX8Wrapper::Invalidate_Cached_Render_States(void)
// The caches below are about to stop holding the values the device is owed.
Flush_Deferred_D3D9_State();
render_state_changed=0;
+ CurrentDX8MaterialValid=false;
+ CurrentDX8LightsValid=0;
int a;
for (a=0;a<sizeof(RenderStates)/sizeof(unsigned);++a) {
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h
index 2c15ce2a1..805086d16 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h
@@ -1011,6 +1011,14 @@ protected:
static unsigned draw_calls;
static bool CurrentDX8LightEnables[4];
+ // The material and lights last handed to both devices. VertexMaterialClass::Apply and
+ // Set_Light_Environment send the same values mesh after mesh, and a repeat is dropped before it
+ // reaches either device. Invalidate_Cached_Render_States forgets them with the rest.
+ static D3DMATERIAL9 CurrentDX8Material;
+ static bool CurrentDX8MaterialValid;
+ static D3DLIGHT9 CurrentDX8Lights[4];
+ static unsigned CurrentDX8LightsValid;
+
// What the Direct3D 9 device has not been told yet, one bit per state, stage or light; the
// values are the caches above. Defer_D3D9_State says whether a setter marks or calls.
static bool Defer_D3D9_State();
@@ -1220,8 +1228,11 @@ WWINLINE bool DX8Wrapper::Defer_D3D9_State()
WWINLINE void DX8Wrapper::Set_DX8_Material(const D3DMATERIAL9* mat)
{
- DX8_RECORD_MATERIAL_CHANGE();
WWASSERT(mat);
+ if (CurrentDX8MaterialValid && memcmp(&CurrentDX8Material, mat, sizeof(D3DMATERIAL9))==0) return;
+ DX8_RECORD_MATERIAL_CHANGE();
+ CurrentDX8Material=*mat;
+ CurrentDX8MaterialValid=true;
SNAPSHOT_SAY(("DX8 - SetMaterial\n"));
if (Defer_D3D9_State()) {
PendingMaterialValue=*mat;
@@ -1240,6 +1251,11 @@ WWINLINE void DX8Wrapper::Set_DX8_Material(const D3DMATERIAL9* mat)
WWINLINE void DX8Wrapper::Set_DX8_Light(int index, D3DLIGHT9* light)
{
if (light) {
+ // An enabled light holding these exact bits already: the device and the backend have it.
+ if (CurrentDX8LightEnables[index] && (CurrentDX8LightsValid&(1u<<index))
+ && memcmp(&CurrentDX8Lights[index], light, sizeof(D3DLIGHT9))==0) return;
+ CurrentDX8Lights[index]=*light;
+ CurrentDX8LightsValid|=1u<<index;
DX8_RECORD_LIGHT_CHANGE();
if (Defer_D3D9_State()) {
PendingLightValues[index]=*light;
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/sortingrenderer.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/sortingrenderer.cpp
index e905c5e5c..ce0429dca 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/sortingrenderer.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/sortingrenderer.cpp
@@ -439,6 +439,11 @@ static void Run_Parallel(int count,int granularity,void (*work)(int,void*),void*
}
}
+void SortingRendererClass::Parallel_For(int count,int granularity,void (*work)(int,void*),void* context)
+{
+ Run_Parallel(count,granularity,work,context);
+}
+
// nodes a thread claims at once in the depth pass; a particle node is a few hundred quads
static const int NODES_PER_CLAIM=4;
// entries one job copies into a batch at once
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/sortingrenderer.h b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/sortingrenderer.h
index 2114390d0..8bab0f718 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/sortingrenderer.h
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/sortingrenderer.h
@@ -86,6 +86,8 @@ public:
/// the work runs inline. The work never allocates and never touches the device.
typedef void (*ParallelForFunc)(int count, int granularity, void (*work)(int index, void *context), void *context);
static void Set_Parallel_For(ParallelForFunc parallel_for);
+ /// The same pool for the rest of WW3D2 (the skin fill), under the same rules.
+ static void Parallel_For(int count, int granularity, void (*work)(int index, void *context), void *context);
/// A depth as an unsigned key that orders the way the float does, negatives first and -0 just
/// below +0; the flush's radix sort buckets on it. The bits come out through memcpy so no