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(w3ddevice): classic relights ground and copies stencils at half cost

Olcay Seygan526b3d3b0aff
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 505413088..942a04a07 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -20,6 +20,7 @@ found and fixed, EA's own, not port damage.**
- One click brings the other game back. Options, Graphics, untick Classic Graphics: the High preset fills in every Reforged box on the page, you can move the quality slider from there, and Accept saves it for the next start. Tick it again and the same boxes go off.
- Defaults in Options now means this game too. It used to leave every setting this build added where you had put it, so a Reforged page stayed Reforged after the click. Now it ticks Classic Graphics and puts every row back where a first start has it, the controls and the sound included. Your language, window mode and resolution stay, and unticking the box still gives you High.
- Classic now means EA's picture in a fight too, checked side by side against the real 2003 game on this machine. Every explosion, every muzzle flash and all the black smoke this build retuned goes back to EA's own numbers under Classic: 132 effect lists and 235 particle systems stand as shipped, and the 51 particle systems this build invented draw nothing. So do the slow water, the eased hull turn, tank barrels that tilt to the target and the grade insignia on units, which return to the three chevrons, and a match opens at EA's camera height. Tick off Classic and Reforged has all of it, down to the pixel. Two players on different looks still share a match, and we ran one 3000 frames with no mismatch.
+- Classic was the slower look in a big fight, and the picture never asked for that. Every explosion's light tested every patch of ground for every light on the map, and each shadow was copied a second time for a graphics device that draws nothing. Both are gone and the picture is the same. In a late 8-player match the drawing behind one frame falls from 9.6 ms to 5.4 ms, and the slowest frame in a thousand from 21.25 ms to 17.75 ms.
- A detail preset now means EA's detail settings. Before this, a preset in your settings file switched on team glow, water foam and 4x multisampling behind any box you could see, and not on the very first start, so the first start and the second drew different pictures. Now a Reforged setting comes on from its own line, which the menu writes when you accept.
- Smooth Motion, the blending that made units glide on a fast monitor, had no switch at all. It is a box in Options, Display, off until ticked, and a 60 Hz screen shows no difference either way.
- Already playing? Nothing in your settings file gets rewritten and every line in it still wins. A setting you never touched has no line, so it takes the new default, which for most of you means Classic on your next start. Untick Classic Graphics once and you are back. The settings can differ between players in one match: none of them reaches the game's logic.
diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/HeightMap.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/HeightMap.h
index 75d83e440..c88263b93 100644
--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/HeightMap.h
+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/HeightMap.h
@@ -153,7 +153,7 @@ protected:
DynamicLightPass m_lastRelight; ///< compared with memcmp, so only ever written by memset and memcpy
Bool m_lastRelightValid; ///< false once anything has rewritten the vertex buffers since that relight
- UnsignedInt doTheDynamicLight(VERTEX_FORMAT *vb, VERTEX_FORMAT *vbMirror, Vector3*light, Vector3*normal, const DynamicLightInputs lights[], Int numLights);
+ UnsignedInt doTheDynamicLight(VERTEX_FORMAT *vb, VERTEX_FORMAT *vbMirror, Vector3*light, Vector3*normal, const DynamicLightInputs lights[], const Int lightsHere[], Int numHere);
//
// Gets the index that corresponds to the data. For example, if the columns are shifted by 3,
// index 3 is actually the first row of polygons, or 0. Yes it is confusing, but it makes
@@ -184,8 +184,6 @@ protected:
}
///update vertex diffuse color for dynamic lights inside given rectangle
Int updateVBForLight(DX8VertexBufferClass *pVB, char *data, Int x0, Int y0, Int x1, Int y1, Int originX, Int originY, W3DDynamicLight *pLights[], Int numLights);
- Int updateVBForLightOptimized(DX8VertexBufferClass *pVB, char *data, Int x0, Int y0, Int x1, Int y1, Int originX, Int originY, W3DDynamicLight *pLights[], Int numLights);
- static Bool quadUnderLight(W3DDynamicLight *pLights[], Int numLights, Int xCoord, Int yCoord); ///< updateVBForLight's per-quad test
///update vertex buffer vertices inside given rectangle
Int updateVB(DX8VertexBufferClass *pVB, char *data, Int x0, Int y0, Int x1, Int y1, Int originX, Int originY, WorldHeightMap *pMap, RefRenderObjListIterator *pLightsIterator);
///upate vertex buffers associated with the given rectangle
diff --git a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DShadow.h b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DShadow.h
index 2575f677c..c5b3a1762 100644
--- a/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DShadow.h
+++ b/GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DShadow.h
@@ -30,9 +30,30 @@
#include "matrix4.h"
#include "GameClient/Shadow.h"
+#include "WW3D2/dx8wrapper.h"
class Drawable; //forward reference
+/** Copy one 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. */
+template <class D3D9_BUFFER>
+inline Bool uploadStreamingRange(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;
+}
+
// ShadowManager -------------------------------------------------------------
class W3DShadowManager
{
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp
index 49a321b2a..1d188152f 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp
@@ -109,9 +109,6 @@
//#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
#endif
-#define no_OPTIMIZED_HEIGHTMAP_LIGHTING 01
-// Doesn't work well. jba.
-
const Bool HALF_RES_MESH = false;
HeightMapRenderObjClass *TheHeightMap = NULL;
@@ -183,7 +180,7 @@ Int HeightMapRenderObjClass::freeMapResources(void)
//=============================================================================
/** Calculates the diffuse lighting as affected by dynamic lighting. */
//=============================================================================
-UnsignedInt HeightMapRenderObjClass::doTheDynamicLight(VERTEX_FORMAT *vb, VERTEX_FORMAT *vbMirror, Vector3*light, Vector3*normal, const DynamicLightInputs lights[], Int numLights)
+UnsignedInt HeightMapRenderObjClass::doTheDynamicLight(VERTEX_FORMAT *vb, VERTEX_FORMAT *vbMirror, Vector3*light, Vector3*normal, const DynamicLightInputs lights[], const Int lightsHere[], Int numHere)
{
#ifdef USE_NORMALS
return;
@@ -202,8 +199,8 @@ UnsignedInt HeightMapRenderObjClass::doTheDynamicLight(VERTEX_FORMAT *vb, VERTEX
Int alpha = (diffuse>>24)&0x00FF;
Int k;
- for (k=0; k<numLights; k++) {
- const DynamicLightInputs &dl = lights[k];
+ for (k=0; k<numHere; k++) {
+ const DynamicLightInputs &dl = lights[lightsHere[k]];
if (!dl.enabled) {
continue; // he is turned off.
}
@@ -541,34 +538,8 @@ Int HeightMapRenderObjClass::updateVB(DX8VertexBufferClass *pVB, char *data, Int
/** Update the dynamic lighting values only in a rectangular block of the given Vertex Buffer.
The vertex locations and texture coords are unchanged.
*/
-/** Whether the quad at map cell (xCoord, yCoord) lies under any of the lights, where they are now or
-where they were at the last relight: the test updateVBForLight relights a quad by. */
-Bool HeightMapRenderObjClass::quadUnderLight(W3DDynamicLight *pLights[], Int numLights, Int xCoord, Int yCoord)
-{
- for (Int k=0; k<numLights; k++) {
- if (pLights[k]->m_minX <= xCoord+1 &&
- pLights[k]->m_maxX >= xCoord &&
- pLights[k]->m_minY <= yCoord+1 &&
- pLights[k]->m_maxY >= yCoord) {
- return true;
- }
- if (pLights[k]->m_prevMinX <= xCoord+1 &&
- pLights[k]->m_prevMaxX >= xCoord &&
- pLights[k]->m_prevMinY <= yCoord+1 &&
- pLights[k]->m_prevMaxY >= yCoord) {
- return true;
- }
- }
- return false;
-}
-
Int HeightMapRenderObjClass::updateVBForLight(DX8VertexBufferClass *pVB, char *data, Int x0, Int y0, Int x1, Int y1, Int originX, Int originY, W3DDynamicLight *pLights[], Int numLights)
{
-
-#if (OPTIMIZED_HEIGHTMAP_LIGHTING) // (gth) if optimizations are enabled, jump over to the "optimized" version of this function.
- return updateVBForLightOptimized( pVB, data, x0, y0, x1, y1, originX, originY, pLights, numLights );
-#endif
-
Int i,j,k;
Int vn0,un0,vp1,up1;
Vector3 l2r,n2f,normalAtTexel;
@@ -580,21 +551,53 @@ Int HeightMapRenderObjClass::updateVBForLight(DX8VertexBufferClass *pVB, char *d
assert(x0 >= originX && y0 >= originY && x1>x0 && y1>y0 && x1<=originX+VERTEX_BUFFER_TILE_LENGTH && y1<=originY+VERTEX_BUFFER_TILE_LENGTH);
#endif
+ // The tile's columns and rows each light covers, one bit each, where it is now and where it was
+ // at the last relight. A quad under either rectangle of any light is rewritten, the old rectangle
+ // so it goes back to its own colour. Only the lights whose new rectangle holds a quad can reach
+ // its corners: that rectangle is the light's whole range rounded out to cells. Asking every light
+ // at every quad and every corner was a third of the render on a crowded Classic frame.
+ UnsignedInt colsNow[MAX_ENABLED_DYNAMIC_LIGHTS], colsPrev[MAX_ENABLED_DYNAMIC_LIGHTS];
+ UnsignedInt rowsNow[MAX_ENABLED_DYNAMIC_LIGHTS], rowsPrev[MAX_ENABLED_DYNAMIC_LIGHTS];
+ for (k=0; k<numLights; k++) {
+ colsNow[k] = colsPrev[k] = rowsNow[k] = rowsPrev[k] = 0;
+ }
+ for (i=x0; i<x1; i++)
+ {
+ if (HALF_RES_MESH && (i&1)) continue;
+ Int xCoord = getXWithOrigin(i)+m_map->getDrawOrgX()-m_map->getBorderSizeInline();
+ for (k=0; k<numLights; k++) {
+ if (pLights[k]->m_minX <= xCoord+1 && pLights[k]->m_maxX >= xCoord) colsNow[k] |= 1u<<(i-x0);
+ if (pLights[k]->m_prevMinX <= xCoord+1 && pLights[k]->m_prevMaxX >= xCoord) colsPrev[k] |= 1u<<(i-x0);
+ }
+ }
+ for (j=y0; j<y1; j++)
+ {
+ if (HALF_RES_MESH && (j&1)) continue;
+ Int yCoord = getYWithOrigin(j)+m_map->getDrawOrgY()-m_map->getBorderSizeInline();
+ for (k=0; k<numLights; k++) {
+ if (pLights[k]->m_minY <= yCoord+1 && pLights[k]->m_maxY >= yCoord) rowsNow[k] |= 1u<<(j-y0);
+ if (pLights[k]->m_prevMinY <= yCoord+1 && pLights[k]->m_prevMaxY >= yCoord) rowsPrev[k] |= 1u<<(j-y0);
+ }
+ }
+
// Lock only from the first quad the loop below writes to the end of the last one. The
// whole tile is 4096 vertices, and with Direct3D 11 its unlock copied every byte into the
// D3D9 buffer and again into the D3D11 one for a light a few cells wide. The bytes outside
// the range are untouched, and every copy of them already holds the same thing.
+ UnsignedInt quadsInRow[VERTEX_BUFFER_TILE_LENGTH];
Int firstVertex = -1;
Int endVertex = 0;
for (j=y0; j<y1; j++)
{
- if (HALF_RES_MESH && (j&1)) continue;
- Int yCoord = getYWithOrigin(j)+m_map->getDrawOrgY()-m_map->getBorderSizeInline();
+ UnsignedInt quads = 0;
+ for (k=0; k<numLights; k++) {
+ if (rowsNow[k] & (1u<<(j-y0))) quads |= colsNow[k];
+ if (rowsPrev[k] & (1u<<(j-y0))) quads |= colsPrev[k];
+ }
+ quadsInRow[j-y0] = quads;
for (i=x0; i<x1; i++)
{
- if (HALF_RES_MESH && (i&1)) continue;
- Int xCoord = getXWithOrigin(i)+m_map->getDrawOrgX()-m_map->getBorderSizeInline();
- if (!quadUnderLight(pLights, numLights, xCoord, yCoord)) continue;
+ if (!(quads & (1u<<(i-x0)))) continue;
Int offset = (j-originY)*vertsPerRow+4*(i-originX);
if (HALF_RES_MESH) {
offset = (j-originY)*vertsPerRow/4+2*(i-originX);
@@ -618,22 +621,8 @@ Int HeightMapRenderObjClass::updateVBForLight(DX8VertexBufferClass *pVB, char *d
for (j=y0; j<y1; j++)
{
- if (HALF_RES_MESH) {
- if (j&1) continue;
- }
- Int yCoord = getYWithOrigin(j)+m_map->getDrawOrgY()-m_map->getBorderSizeInline();
- Bool intersect = false;
- for (k=0; k<numLights; k++) {
- if (pLights[k]->m_minY <= yCoord+1 &&
- pLights[k]->m_maxY >= yCoord) {
- intersect = true;
- }
- if (pLights[k]->m_prevMinY <= yCoord+1 &&
- pLights[k]->m_prevMaxY >= yCoord) {
- intersect = true;
- }
- }
- if (!intersect) {
+ const UnsignedInt quads = quadsInRow[j-y0];
+ if (quads == 0) {
continue;
}
vn0 = getYWithOrigin(j)-1;
@@ -645,28 +634,16 @@ Int HeightMapRenderObjClass::updateVBForLight(DX8VertexBufferClass *pVB, char *d
for (i=x0; i<x1; i++)
{
- if (HALF_RES_MESH) {
- if (i&1) continue;
+ if (!(quads & (1u<<(i-x0)))) {
+ continue;
}
- Int xCoord = getXWithOrigin(i)+m_map->getDrawOrgX()-m_map->getBorderSizeInline();
- Bool intersect = false;
+ Int lightsHere[MAX_ENABLED_DYNAMIC_LIGHTS];
+ Int numHere = 0;
for (k=0; k<numLights; k++) {
- if (pLights[k]->m_minX <= xCoord+1 &&
- pLights[k]->m_maxX >= xCoord &&
- pLights[k]->m_minY <= yCoord+1 &&
- pLights[k]->m_maxY >= yCoord) {
- intersect = true;
- }
- if (pLights[k]->m_prevMinX <= xCoord+1 &&
- pLights[k]->m_prevMaxX >= xCoord &&
- pLights[k]->m_prevMinY <= yCoord+1 &&
- pLights[k]->m_prevMaxY >= yCoord) {
- intersect = true;
+ if ((rowsNow[k] & (1u<<(j-y0))) && (colsNow[k] & (1u<<(i-x0)))) {
+ lightsHere[numHere++] = k;
}
}
- if (!intersect) {
- continue;
- }
// vb is the pointer to the vertex in the hardware dx8 vertex buffer.
Int offset = (j-originY)*vertsPerRow+4*(i-originX);
if (HALF_RES_MESH) {
@@ -675,7 +652,7 @@ Int HeightMapRenderObjClass::updateVBForLight(DX8VertexBufferClass *pVB, char *d
vb = vBase + offset; //skip to correct row in vertex buffer
// vbMirror is the pointer to the vertex in our memory based copy.
// The important point is that we can read out of our copy to get the original
- // diffuse color, and xyz location. It is VERY SLOW to read out of the
+ // diffuse color, and xyz location. It is VERY SLOW to read out of the
// hardware vertex buffer, possibly worse... jba.
VERTEX_FORMAT *vbMirror = ((VERTEX_FORMAT*)data) + offset;
un0 = getXWithOrigin(i)-1;
@@ -697,7 +674,7 @@ Int HeightMapRenderObjClass::updateVBForLight(DX8VertexBufferClass *pVB, char *d
Vector3::Normalized_Cross_Product(l2r, n2f, &normalAtTexel);
#endif
- doTheDynamicLight(vb, vbMirror, &lightRay, &normalAtTexel, m_lastRelight.lights, numLights);
+ doTheDynamicLight(vb, vbMirror, &lightRay, &normalAtTexel, m_lastRelight.lights, lightsHere, numHere);
vb++; vbMirror++;
//top-right sample
@@ -710,7 +687,7 @@ Int HeightMapRenderObjClass::updateVBForLight(DX8VertexBufferClass *pVB, char *d
Vector3::Normalized_Cross_Product(l2r, n2f, &normalAtTexel);
#endif
- doTheDynamicLight(vb, vbMirror, &lightRay, &normalAtTexel, m_lastRelight.lights, numLights);
+ doTheDynamicLight(vb, vbMirror, &lightRay, &normalAtTexel, m_lastRelight.lights, lightsHere, numHere);
vb++; vbMirror++;
//bottom-right sample
@@ -723,7 +700,7 @@ Int HeightMapRenderObjClass::updateVBForLight(DX8VertexBufferClass *pVB, char *d
Vector3::Normalized_Cross_Product(l2r, n2f, &normalAtTexel);
#endif
- doTheDynamicLight(vb, vbMirror, &lightRay, &normalAtTexel, m_lastRelight.lights, numLights);
+ doTheDynamicLight(vb, vbMirror, &lightRay, &normalAtTexel, m_lastRelight.lights, lightsHere, numHere);
vb++; vbMirror++;
//bottom-left sample
@@ -736,7 +713,7 @@ Int HeightMapRenderObjClass::updateVBForLight(DX8VertexBufferClass *pVB, char *d
Vector3::Normalized_Cross_Product(l2r, n2f, &normalAtTexel);
#endif
- doTheDynamicLight(vb, vbMirror, &lightRay, &normalAtTexel, m_lastRelight.lights, numLights);
+ doTheDynamicLight(vb, vbMirror, &lightRay, &normalAtTexel, m_lastRelight.lights, lightsHere, numHere);
vb++; vbMirror++;
}
}
@@ -746,203 +723,6 @@ Int HeightMapRenderObjClass::updateVBForLight(DX8VertexBufferClass *pVB, char *d
}
-Int HeightMapRenderObjClass::updateVBForLightOptimized(DX8VertexBufferClass *pVB, char *data, Int x0, Int y0, Int x1, Int y1, Int originX, Int originY, W3DDynamicLight *pLights[], Int numLights)
-{
- Int i,j,k;
- Int vn0,un0,vp1,up1;
- Vector3 l2r,n2f,normalAtTexel;
- Int vertsPerRow=(VERTEX_BUFFER_TILE_LENGTH)*4; //vertices per row of VB
-
- if (m_vertexBufferTiles && m_map)
- {
-#ifdef _DEBUG
- assert(x0 >= originX && y0 >= originY && x1>x0 && y1>y0 && x1<=originX+VERTEX_BUFFER_TILE_LENGTH && y1<=originY+VERTEX_BUFFER_TILE_LENGTH);
-#endif
-
- DX8VertexBufferClass::WriteLockClass lockVtxBuffer(pVB);
- VERTEX_FORMAT *vBase = (VERTEX_FORMAT*)lockVtxBuffer.Get_Vertex_Array();
- // a failed lock - a device that has gone away - hands back nothing to light
- if (vBase == NULL) {
- m_lastRelightValid = false; // nothing written, so the next pass tries again
- return 0;
- }
- VERTEX_FORMAT *vb;
-
- //
- // (gth) the optimization in this function is to take advantage of verts in the same
- // x,y position who have already computed their lighting. To do this, we need to set up
- // some offsets in the vertex buffer. I've computed these offsets to be consistent with
- // the formula's that Generals is using but in the case of the "half-res-mesh" I'm not
- // sure things are correct...
- //
- Int quad_right_offset;
- Int quad_below_offset;
- Int quad_below_right_offset;
-
- if (HALF_RES_MESH == false) {
- // offset = (j-originY)*vertsPerRow+4*(i-originX);
- quad_right_offset = 4;
- quad_below_offset = vertsPerRow;
- quad_below_right_offset = vertsPerRow + 4;
-
- } else {
- // offset = (j-originY)*vertsPerRow/4+2*(i-originX);
- quad_right_offset = 2;
- quad_below_offset = vertsPerRow/4;
- quad_below_right_offset = vertsPerRow/4 + 2;
- }
-
- //
- // i,j loop over the quads affected by the light. Each quad has its *own* 4 vertices. This
- // means that for any vertex position on the map, there are actually 4 copies of the vertex.
- //
- for (j=y0; j<y1; j++)
- {
- if (HALF_RES_MESH) {
- if (j&1) continue;
- }
- Int yCoord = getYWithOrigin(j)+m_map->getDrawOrgY()-m_map->getBorderSizeInline();
- Bool intersect = false;
- for (k=0; k<numLights; k++) {
- if (pLights[k]->m_minY <= yCoord+1 &&
- pLights[k]->m_maxY >= yCoord) {
- intersect = true;
- }
- if (pLights[k]->m_prevMinY <= yCoord+1 &&
- pLights[k]->m_prevMaxY >= yCoord) {
- intersect = true;
- }
- }
- if (!intersect) {
- continue;
- }
- vn0 = getYWithOrigin(j)-1;
- if (vn0 < -m_map->getDrawOrgY())
- vn0=-m_map->getDrawOrgY();
- vp1 = getYWithOrigin(j+1)+1;
- if (vp1 >= m_map->getYExtent()-m_map->getDrawOrgY())
- vp1=m_map->getYExtent()-m_map->getDrawOrgY()-1;
-
- for (i=x0; i<x1; i++)
- {
- if (HALF_RES_MESH) {
- if (i&1) continue;
- }
- Int xCoord = getXWithOrigin(i)+m_map->getDrawOrgX()-m_map->getBorderSizeInline();
- Bool intersect = false;
- for (k=0; k<numLights; k++) {
- if (pLights[k]->m_minX <= xCoord+1 &&
- pLights[k]->m_maxX >= xCoord &&
- pLights[k]->m_minY <= yCoord+1 &&
- pLights[k]->m_maxY >= yCoord) {
- intersect = true;
- }
- if (pLights[k]->m_prevMinX <= xCoord+1 &&
- pLights[k]->m_prevMaxX >= xCoord &&
- pLights[k]->m_prevMinY <= yCoord+1 &&
- pLights[k]->m_prevMaxY >= yCoord) {
- intersect = true;
- }
- }
- if (!intersect) {
- continue;
- }
- // vb is the pointer to the vertex in the hardware dx8 vertex buffer.
- Int offset = (j-originY)*vertsPerRow+4*(i-originX);
- if (HALF_RES_MESH) {
- offset = (j-originY)*vertsPerRow/4+2*(i-originX);
- }
- vb = vBase + offset; //skip to correct row in vertex buffer
- // vbMirror is the pointer to the vertex in our memory based copy.
- // The important point is that we can read out of our copy to get the original
- // diffuse color, and xyz location. It is VERY SLOW to read out of the
- // hardware vertex buffer, possibly worse... jba.
- VERTEX_FORMAT *vbMirror = ((VERTEX_FORMAT*)data) + offset;
- VERTEX_FORMAT *vbaseMirror = ((VERTEX_FORMAT*)data);
- un0 = getXWithOrigin(i)-1;
- if (un0 < -m_map->getDrawOrgX())
- un0=-m_map->getDrawOrgX();
- up1 = getXWithOrigin(i+1)+1;
- if (up1 >= m_map->getXExtent()-m_map->getDrawOrgX())
- up1=m_map->getXExtent()-m_map->getDrawOrgX()-1;
-
- Vector3 lightRay(0,0,0);
-
- //
- // (gth) Following the set of rules below lets us take advantage of lighting values that have
- // been previously computed. The idea is to copy them ahead to future quads that will need them
- // and then not compute them when we get to those quads. This also avoids having to read-back
- // from the vertex buffer but we do jump around in memory... probably bad anyway, maybe we should
- // compute into a temporary buffer and copy all at once...
- //
- unsigned long light_copy;
-
- // top-left sample -> only compute when i==0 and j==0
- if ((i==x0) && (j==y0)) {
- l2r.Set(2*MAP_XY_FACTOR,0,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(getXWithOrigin(i)+1, getYWithOrigin(j)) - m_map->getDisplayHeight(un0, getYWithOrigin(j))));
- n2f.Set(0,2*MAP_XY_FACTOR,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(getXWithOrigin(i), (getYWithOrigin(j)+1)) - m_map->getDisplayHeight(getXWithOrigin(i), vn0)));
- Vector3::Normalized_Cross_Product(l2r, n2f, &normalAtTexel);
- doTheDynamicLight(vb, vbMirror, &lightRay, &normalAtTexel, m_lastRelight.lights, numLights);
- }
- vb++; vbMirror++;
-
- //top-right sample -> compute when j==0, then copy to (right,0)
- if (j==y0) {
- l2r.Set(2*MAP_XY_FACTOR,0,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(up1 , getYWithOrigin(j) ) - m_map->getDisplayHeight(getXWithOrigin(i) , getYWithOrigin(j) )));
- n2f.Set(0,2*MAP_XY_FACTOR,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(getXWithOrigin(i)+1 , (getYWithOrigin(j)+1) ) - m_map->getDisplayHeight(getXWithOrigin(i)+1 , vn0 )));
- Vector3::Normalized_Cross_Product(l2r, n2f, &normalAtTexel);
- light_copy = doTheDynamicLight(vb, vbMirror, &lightRay, &normalAtTexel, m_lastRelight.lights, numLights);
-
- if (i < x1-1) {
- // copy light to (right,0)
- (vBase + offset + quad_right_offset)->diffuse = (light_copy&0x00FFFFFF) | ((vbaseMirror + offset + quad_right_offset)->diffuse&0xff000000) ;
- }
- }
- vb++; vbMirror++;
-
- //bottom-right sample -> always compute, then copy to (right,3), (down,1), (down+right,0)
- l2r.Set(2*MAP_XY_FACTOR,0,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(up1 , (getYWithOrigin(j)+1) ) - m_map->getDisplayHeight(getXWithOrigin(i) , (getYWithOrigin(j)+1) )));
- n2f.Set(0,2*MAP_XY_FACTOR,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(getXWithOrigin(i)+1 , vp1 ) - m_map->getDisplayHeight(getXWithOrigin(i)+1 , getYWithOrigin(j) )));
- Vector3::Normalized_Cross_Product(l2r, n2f, &normalAtTexel);
- light_copy = doTheDynamicLight(vb, vbMirror, &lightRay, &normalAtTexel, m_lastRelight.lights, numLights);
-
- if (i < x1-1) {
- // copy light to (right,3)
- //(vBase + offset + quad_right_offset + 3)->diffuse = light_copy;
- (vBase + offset + quad_right_offset + 3)->diffuse = (light_copy&0x00FFFFFF) | ((vbaseMirror + offset + quad_right_offset + 3)->diffuse&0xff000000) ;
- }
- if (j < y1-1) {
- // copy light to (down,1)
- //(vBase + offset + quad_below_offset + 1)->diffuse = light_copy;
- (vBase + offset + quad_right_offset + 1)->diffuse = (light_copy&0x00FFFFFF) | ((vbaseMirror + offset + quad_right_offset + 1)->diffuse&0xff000000) ;
- }
- if ((i < x1-1) && (j < y1-1)) {
- // copy light to (right+down,0)
- //(vBase + offset + quad_below_right_offset)->diffuse = light_copy;
- (vBase + offset + quad_right_offset)->diffuse = (light_copy&0x00FFFFFF) | ((vbaseMirror + offset + quad_right_offset)->diffuse&0xff000000) ;
- }
- vb++; vbMirror++;
-
- //bottom-left sample -> compute when i==0, otherwise copy from (left,2)
- if (i==x0) {
- l2r.Set(2*MAP_XY_FACTOR,0,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(getXWithOrigin(i)+1 , (getYWithOrigin(j)+1) ) - m_map->getDisplayHeight(un0 , (getYWithOrigin(j)+1) )));
- n2f.Set(0,2*MAP_XY_FACTOR,MAP_HEIGHT_SCALE*(m_map->getDisplayHeight(getXWithOrigin(i) , vp1 ) - m_map->getDisplayHeight(getXWithOrigin(i) , getYWithOrigin(j) )));
- Vector3::Normalized_Cross_Product(l2r, n2f, &normalAtTexel);
- light_copy = doTheDynamicLight(vb, vbMirror, &lightRay, &normalAtTexel, m_lastRelight.lights, numLights);
-
- if (j < y1-1) {
- // copy light to (down,0)
- //(vBase + offset + quad_below_offset)->diffuse = light_copy;
- (vBase + offset + quad_below_offset)->diffuse = (light_copy&0x00FFFFFF) | ((vbaseMirror + offset + quad_below_offset)->diffuse&0xff000000) ;
- }
- }
- vb++; vbMirror++;
- }
- }
- return 0; //success.
- }
- return -1;
-}
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DProjectedShadow.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DProjectedShadow.cpp
index 3a96f6a1f..25960b70d 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DProjectedShadow.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DProjectedShadow.cpp
@@ -146,26 +146,6 @@ int SHADOW_DECAL_INDEX_SIZE=SHADOW_DECAL_INDEX_COUNT;
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
class W3DShadowTextureManager; //forward reference
@@ -813,10 +793,10 @@ void W3DProjectedShadowManager::flushDecals(W3DShadowTexture *texture, ShadowTyp
nShadowDecalStartBatchVertex = nShadowDecalVertsInBuf;
nShadowDecalStartBatchIndex = nShadowDecalIndicesInBuf;
const Bool uploaded =
- uploadDecalRange(shadowDecalVertexBufferD3D, shadowDecalVertexTwin, decalStageVertices,
+ uploadStreamingRange(shadowDecalVertexBufferD3D, shadowDecalVertexTwin, decalStageVertices,
nShadowDecalVertsInBuf*sizeof(SHADOW_DECAL_VERTEX), nShadowDecalVertsInBatch*sizeof(SHADOW_DECAL_VERTEX),
vertexFlags)
- && uploadDecalRange(shadowDecalIndexBufferD3D, shadowDecalIndexTwin, decalStageIndices,
+ && uploadStreamingRange(shadowDecalIndexBufferD3D, shadowDecalIndexTwin, decalStageIndices,
nShadowDecalIndicesInBuf*sizeof(UnsignedShort), numIndex*sizeof(UnsignedShort), indexFlags);
nShadowDecalVertsInBuf += nShadowDecalVertsInBatch;
nShadowDecalIndicesInBuf += numIndex;
diff --git a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
index e07b22c52..917445966 100644
--- a/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
+++ b/GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/Shadow/W3DVolumetricShadow.cpp
@@ -1685,8 +1685,6 @@ void W3DVolumetricShadow::RenderDynamicMeshVolume(Int meshIndex, Int lightIndex,
{
Geometry *geometry;
Int numVerts, numPolys, numIndex;
- SHADOW_DYNAMIC_VOLUME_VERTEX* pvVertices;
- UnsignedShort *pvIndices;
//Get D3D Device used by W3D for quicker access.
LPDIRECT3DDEVICE9 m_pDev=DX8Wrapper::_Get_D3D_Device();
@@ -1713,104 +1711,48 @@ void W3DVolumetricShadow::RenderDynamicMeshVolume(Int meshIndex, Int lightIndex,
return;
- DX11BufferLockClass vertexLock;
+ /* Classic's stencil shadows hand over a volume per mesh, twice a picture, and these were a Direct3D 9
+ lock, a copy into a block, a copy into the D3D9 buffer and a copy into the D3D11 one for each of
+ them, with Direct3D 9 drawing nothing: a quarter of the shadow pass went in d3d9.dll alone. */
const UnsignedInt vertexStride = sizeof(SHADOW_DYNAMIC_VOLUME_VERTEX);
- UnsignedInt vertexOffset;
- UnsignedInt vertexFlags;
- if (nShadowVertsInBuf > (SHADOW_VERTEX_SIZE-numVerts)) //check if room for model verts
- { //flush the buffer by drawing the contents and re-locking again
- vertexOffset=0;
- vertexFlags=D3DLOCK_DISCARD;
- if (shadowVertexBufferD3D->Lock(0,numVerts*vertexStride,(void**)&pvVertices,D3DLOCK_DISCARD) != D3D_OK)
- return;
+ const Bool vertexDiscard = nShadowVertsInBuf > (SHADOW_VERTEX_SIZE-numVerts); //check if room for model verts
+ if (!uploadStreamingRange(shadowVertexBufferD3D, shadowVertexTwin, geometry->GetVertex(0),
+ vertexDiscard ? 0 : nShadowVertsInBuf*vertexStride, numVerts*vertexStride,
+ vertexDiscard ? D3DLOCK_DISCARD : D3DLOCK_NOOVERWRITE))
+ return;
+ if (vertexDiscard)
+ {
nShadowVertsInBuf=0;
nShadowStartBatchVertex=0;
}
- else
- { vertexOffset=nShadowVertsInBuf*vertexStride;
- vertexFlags=D3DLOCK_NOOVERWRITE;
- if (shadowVertexBufferD3D->Lock(vertexOffset,numVerts*vertexStride, (void**)&pvVertices,D3DLOCK_NOOVERWRITE) != D3D_OK)
- return;
- }
- // From here the fill writes into the twin's own block instead of the D3D9 pointer, and the
- // unlock below copies that range into both. Without a twin Begin answers null and the fill
- // carries on writing where it always did.
- void *vertexTarget = vertexLock.Begin(shadowVertexTwin, pvVertices, vertexOffset,
- numVerts*vertexStride, vertexFlags);
- if (vertexTarget != NULL)
- pvVertices = (SHADOW_DYNAMIC_VOLUME_VERTEX *)vertexTarget;
-#ifdef SV_DEBUG
- srand(0x1345465);
-#endif
- if(pvVertices)
+ short firstPolygonIndices[3];
+ const Bool indexDiscard = nShadowIndicesInBuf > (SHADOW_INDEX_SIZE-numIndex); //check if room for model verts
+ if (!uploadStreamingRange(shadowIndexBufferD3D, shadowIndexTwin, geometry->GetPolygonIndex(0,firstPolygonIndices),
+ indexDiscard ? 0 : nShadowIndicesInBuf*sizeof(short), numIndex*sizeof(short),
+ indexDiscard ? D3DLOCK_DISCARD : D3DLOCK_NOOVERWRITE))
+ return;
+ if (indexDiscard)
{
-#ifdef SV_DEBUG
- for (Int i=0; i<numVerts; i++)
- {
- (*((Vector3 *)pvVertices))=*geometry->GetVertex(i); //cast is valid since both start with xyz
- pvVertices->diffuse=(rand()%255) | ((rand()%255)<<8) | ((rand()%255)<<16);
- pvVertices++;
- }
-#else
- memcpy(pvVertices,geometry->GetVertex(0),numVerts*sizeof(SHADOW_DYNAMIC_VOLUME_VERTEX));
-#endif
- }
-
- vertexLock.End();
- shadowVertexBufferD3D->Unlock();
-
- DX11BufferLockClass indexLock;
- UnsignedInt indexOffset;
- UnsignedInt indexFlags;
- if (nShadowIndicesInBuf > (SHADOW_INDEX_SIZE-numIndex)) //check if room for model verts
- { //flush the buffer by drawing the contents and re-locking again
- indexOffset=0;
- indexFlags=D3DLOCK_DISCARD;
- if (shadowIndexBufferD3D->Lock(0,numIndex*sizeof(short),(void**)&pvIndices,D3DLOCK_DISCARD) != D3D_OK)
- return;
nShadowIndicesInBuf=0;
nShadowStartBatchIndex=0;
}
- else
- { indexOffset=nShadowIndicesInBuf*sizeof(short);
- indexFlags=D3DLOCK_NOOVERWRITE;
- if (shadowIndexBufferD3D->Lock(indexOffset,numIndex*sizeof(short), (void**)&pvIndices,D3DLOCK_NOOVERWRITE) != D3D_OK)
- return;
- }
- void *indexTarget = indexLock.Begin(shadowIndexTwin, pvIndices, indexOffset,
- numIndex*sizeof(short), indexFlags);
- if (indexTarget != NULL)
- pvIndices = (UnsignedShort *)indexTarget;
+ Matrix4x4 mWorld(*meshXform);
+ Matrix4x4 mWorldTransposed = mWorld.Transpose();
- try {
- if(pvIndices)
+ // D3D9 takes the base vertex index on the draw call, not here.
+ if (!Direct3D11_Present_Is_Enabled())
{
- memcpy(pvIndices,geometry->GetPolygonIndex(0,(short *)pvIndices),numPolys*3*sizeof(short));
- }
- IndexBufferExceptionFunc();
- } catch(...) {
- IndexBufferExceptionFunc();
+ m_pDev->SetIndices(shadowIndexBufferD3D);
+ m_pDev->SetTransform(D3DTS_WORLD,(D3DMATRIX *)&mWorldTransposed);
+ if (shadowVertexBufferD3D != lastActiveVertexBuffer)
+ { m_pDev->SetStreamSource(0,shadowVertexBufferD3D,0,vertexStride);
+ lastActiveVertexBuffer = shadowVertexBufferD3D;
+ }
}
-
- indexLock.End();
- shadowIndexBufferD3D->Unlock();
-
- // D3D9 takes the base vertex index on the draw call, not here.
- m_pDev->SetIndices(shadowIndexBufferD3D);
Direct3D11_Mirror_Indices(shadowIndexTwin);
-
- Matrix4x4 mWorld(*meshXform);
- Matrix4x4 mWorldTransposed = mWorld.Transpose();
-
- m_pDev->SetTransform(D3DTS_WORLD,(D3DMATRIX *)&mWorldTransposed);
Direct3D11_Mirror_Transform(D3DTS_WORLD,(const float *)&mWorldTransposed);
-
- if (shadowVertexBufferD3D != lastActiveVertexBuffer)
- { m_pDev->SetStreamSource(0,shadowVertexBufferD3D,0,vertexStride);
- lastActiveVertexBuffer = shadowVertexBufferD3D;
- }
Direct3D11_Mirror_Stream_Source(shadowVertexTwin, vertexStride, 0);
if (DX8Wrapper::_Is_Triangle_Draw_Enabled())