Skip to content

Commit 788f63a

Browse files
Cellcotegithubawn
authored andcommitted
perf(terrain): Hoist global light ray computation out of per-vertex loop
1 parent e760b36 commit 788f63a

3 files changed

Lines changed: 8 additions & 9 deletions

File tree

Core/GameEngineDevice/Include/W3DDevice/GameClient/BaseHeightMap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ class BaseHeightMapRenderObjClass : public RenderObjClass, public DX8_CleanupHoo
157157
void updateMacroTexture(AsciiString textureName);
158158
void doTextures(Bool flag) {m_disableTextures = !flag;};
159159
/// Update the diffuse value from static light info for one vertex.
160-
void doTheLight(VERTEX_FORMAT *vb, Vector3*light, Vector3*normal, RefRenderObjListIterator *pLightsIterator, UnsignedByte alpha);
160+
void doTheLight(VERTEX_FORMAT *vb, const Vector3*light, Vector3*normal, RefRenderObjListIterator *pLightsIterator, UnsignedByte alpha);
161161
void addScorch(Vector3 location, Real radius, Scorches type);
162162
void addTree(DrawableID id, Coord3D location, Real scale, Real angle,
163163
Real randomScaleAmount, const W3DTreeDrawModuleData *data);

Core/GameEngineDevice/Source/W3DDevice/GameClient/BaseHeightMap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ static lights into account as well. It is possible to just use the normal in th
504504
vertex and let D3D do the lighting, but it is slower to render, and can only
505505
handle 4 lights at this point. */
506506
//=============================================================================
507-
void BaseHeightMapRenderObjClass::doTheLight(VERTEX_FORMAT *vb, Vector3*light, Vector3*normal, RefRenderObjListIterator *pLightsIterator, UnsignedByte alpha)
507+
void BaseHeightMapRenderObjClass::doTheLight(VERTEX_FORMAT *vb, const Vector3*light, Vector3*normal, RefRenderObjListIterator *pLightsIterator, UnsignedByte alpha)
508508
{
509509
#ifdef USE_NORMALS
510510
vb->nx = normal->X;

Core/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,6 @@ Int HeightMapRenderObjClass::updateVB(DX8VertexBufferClass *pVB, VERTEX_FORMAT *
306306
{
307307
Int i,j;
308308
Vector3 lightRay[MAX_GLOBAL_LIGHTS];
309-
const Coord3D *lightPos;
310309
Int xCoord, yCoord;
311310
Int vn0,un0,vp1,up1;
312311
Vector3 l2r,n2f,normalAtTexel;
@@ -320,6 +319,12 @@ Int HeightMapRenderObjClass::updateVB(DX8VertexBufferClass *pVB, VERTEX_FORMAT *
320319
assert(x0 >= originX && y0 >= originY && x1>x0 && y1>y0 && x1<=originX+VERTEX_BUFFER_TILE_LENGTH && y1<=originY+VERTEX_BUFFER_TILE_LENGTH);
321320
#endif
322321

322+
for (Int lightIndex=0; lightIndex < TheGlobalData->m_numGlobalLights; lightIndex++)
323+
{
324+
const Coord3D& lightPos = TheGlobalData->m_terrainLightPos[lightIndex];
325+
lightRay[lightIndex].Set(-lightPos.x, -lightPos.y, -lightPos.z);
326+
}
327+
323328
DX8VertexBufferClass::WriteLockClass lockVtxBuffer(pVB);
324329
VERTEX_FORMAT *vbHardware = (VERTEX_FORMAT*)lockVtxBuffer.Get_Vertex_Array();
325330
VERTEX_FORMAT *vBase = data;
@@ -362,12 +367,6 @@ Int HeightMapRenderObjClass::updateVB(DX8VertexBufferClass *pVB, VERTEX_FORMAT *
362367
pMap->getUVData(mapX, mapY, U, V);
363368
pMap->getAlphaUVData(mapX, mapY, UA, VA, alpha, &flipForBlend);
364369

365-
for (Int lightIndex=0; lightIndex < TheGlobalData->m_numGlobalLights; lightIndex++)
366-
{
367-
lightPos=&TheGlobalData->m_terrainLightPos[lightIndex];
368-
lightRay[lightIndex].Set(-lightPos->x,-lightPos->y, -lightPos->z);
369-
}
370-
371370
//top-left sample
372371
l2r.Set(2*MAP_XY_FACTOR,0,MAP_HEIGHT_SCALE*(pMap->getDisplayHeight(mapX+cellOffset, mapY) - pMap->getDisplayHeight(un0, mapY)));
373372
n2f.Set(0,2*MAP_XY_FACTOR,MAP_HEIGHT_SCALE*(pMap->getDisplayHeight(mapX, (mapY+cellOffset)) - pMap->getDisplayHeight(mapX, vn0)));

0 commit comments

Comments
 (0)