Skip to content

Commit

Permalink
Optimizations to when rendering api calls are done. Results in 60%+ b…
Browse files Browse the repository at this point in the history
…oost in performance in high actor/mesh count scenarios, and ~20% in real world scenarios when not GPU limited.
  • Loading branch information
xwidghet committed Jun 13, 2017
1 parent c6c9593 commit c4f7ccc
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 111 deletions.
4 changes: 2 additions & 2 deletions src/ActorMultiTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ void ActorMultiTexture::DrawPrimitives()
quadVerticies.top = -m_size.y/2.0f;
quadVerticies.bottom = +m_size.y/2.0f;

DISPLAY->ClearAllTextures();
for( size_t i = 0; i < m_aTextureUnits.size(); ++i )
{
TextureUnit tu = enum_add2(TextureUnit_1, i);
Expand Down Expand Up @@ -130,7 +129,8 @@ void ActorMultiTexture::DrawPrimitives()
for( size_t i = 0; i < m_aTextureUnits.size(); ++i )
DISPLAY->SetTexture( enum_add2(TextureUnit_1, i), 0 );

DISPLAY->SetEffectMode( EffectMode_Normal );
if ( m_EffectMode != EffectMode_Normal )
DISPLAY->SetEffectMode(EffectMode_Normal);
}

bool ActorMultiTexture::EarlyAbortDraw() const
Expand Down
4 changes: 2 additions & 2 deletions src/ActorMultiVertex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ void ActorMultiVertex::DrawPrimitives()
{
Actor::SetGlobalRenderStates(); // set Actor-specified render states

DISPLAY->ClearAllTextures();
DISPLAY->SetTexture( TextureUnit_1, _Texture->GetTexHandle() );

Actor::SetTextureRenderStates();
Expand Down Expand Up @@ -330,7 +329,8 @@ void ActorMultiVertex::DrawInternal( const AMV_TweenState *TS )
break;
}

DISPLAY->SetEffectMode( EffectMode_Normal );
if ( _EffectMode != EffectMode_Normal )
DISPLAY->SetEffectMode(EffectMode_Normal);
}

bool ActorMultiVertex::EarlyAbortDraw() const
Expand Down
2 changes: 0 additions & 2 deletions src/BitmapText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,6 @@ void BitmapText::DrawChars( bool bUseStrokeTexture )
bool haveTextures = false;
int startingPoint = iStartGlyph;

DISPLAY->ClearAllTextures();
for ( int start = iStartGlyph; start < iEndGlyph; )
{
int end = start;
Expand Down Expand Up @@ -465,7 +464,6 @@ void BitmapText::DrawChars( bool bUseStrokeTexture )
DISPLAY->DrawQuads(&m_aVertices[startingPoint * 4], (end - startingPoint) * 4);

// Setup for the next render pass
DISPLAY->ClearAllTextures();
startingPoint = end;
haveTextures = false;
}
Expand Down
3 changes: 0 additions & 3 deletions src/GraphDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class GraphLine: public Actor
{
Actor::SetGlobalRenderStates(); // set Actor-specified render states

DISPLAY->ClearAllTextures();

// Must call this after setting the texture or else texture
// parameters have no effect.
Actor::SetTextureRenderStates();
Expand Down Expand Up @@ -133,7 +131,6 @@ class GraphBody: public Actor
{
Actor::SetGlobalRenderStates(); // set Actor-specified render states

DISPLAY->ClearAllTextures();
DISPLAY->SetTexture( TextureUnit_1, m_pTexture->GetTexHandle() );

// Must call this after setting the texture or else texture
Expand Down
1 change: 0 additions & 1 deletion src/GrooveRadar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ void GrooveRadar::GrooveRadarValueMap::DrawPrimitives()
// draw radar filling
const float fRadius = GetUnzoomedWidth()/2.0f*1.1f;

DISPLAY->ClearAllTextures();
DISPLAY->SetTextureMode( TextureUnit_1, TextureMode_Modulate );
RageSpriteVertex v[12]; // needed to draw 5 fan primitives and 10 strip primitives

Expand Down
6 changes: 0 additions & 6 deletions src/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,6 @@ void Model::DrawPrimitives()
static const RageColor specular( 0.2f,0.2f,0.2f,1 );
static const float shininess = 1;
DISPLAY->SetMaterial( emissive, ambient, diffuse, specular, shininess );
DISPLAY->ClearAllTextures();
DISPLAY->SetSphereEnvironmentMapping( TextureUnit_1, false );
DrawMesh( i );
}
Expand All @@ -445,18 +444,13 @@ void Model::DrawPrimitives()
float shininess = 1;

DISPLAY->SetMaterial( emissive, ambient, diffuse, specular, shininess );
DISPLAY->ClearAllTextures();

if( pMesh->nMaterialIndex != -1 )
{
msMaterial& mat = m_Materials[ pMesh->nMaterialIndex ];
DISPLAY->SetTexture( TextureUnit_1, mat.diffuse.GetCurrentTexture() ? mat.diffuse.GetCurrentTexture()->GetTexHandle() : 0 );
Actor::SetTextureRenderStates(); // set Actor-specified render states
}
else
{
// hey why is this otherwise empty else block here? -aj
}

DrawMesh( i );
}
Expand Down
2 changes: 0 additions & 2 deletions src/NoteDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -789,8 +789,6 @@ void NoteDisplay::DrawHoldPart(vector<Sprite*> &vpSpr,
}
}

DISPLAY->ClearAllTextures();

const float fTexCoordLeft = rect.left;
const float fTexCoordRight = rect.right;
const float fTexCoordCenter = (fTexCoordLeft+fTexCoordRight)/2;
Expand Down
173 changes: 109 additions & 64 deletions src/RageDisplay_D3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ D3DCAPS9 g_DeviceCaps;
D3DDISPLAYMODE g_DesktopMode;
D3DPRESENT_PARAMETERS g_d3dpp;
int g_ModelMatrixCnt=0;
DWORD lastFVF = 0;
static bool g_bSphereMapping[NUM_TextureUnit] = { false, false };

// Need default color and depth buffer to restore them after using render targets
Expand Down Expand Up @@ -715,70 +716,79 @@ const VideoModeParams* RageDisplay_D3D::GetActualVideoModeParams() const

void RageDisplay_D3D::SendCurrentMatrices()
{
RageMatrix m;
RageMatrixMultiply( &m, GetCentering(), GetProjectionTop() );
static RageMatrix Centering;
static RageMatrix Projection;

if (g_bInvertY)
{
RageMatrix flip;
RageMatrixScale(&flip, +1, -1, +1);
RageMatrixMultiply(&m, &flip, &m);
}

// Convert to OpenGL-style "pixel-centered" coords
RageMatrix m2 = GetCenteringMatrix( -0.5f, -0.5f, 0, 0 );
RageMatrix projection;
RageMatrixMultiply( &projection, &m2, &m );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, (D3DMATRIX*)&projection );

g_pd3dDevice->SetTransform( D3DTS_VIEW, (D3DMATRIX*)GetViewTop() );
g_pd3dDevice->SetTransform( D3DTS_WORLD, (D3DMATRIX*)GetWorldTop() );

FOREACH_ENUM( TextureUnit, tu )
if ( Centering != *GetCentering() || Projection != *GetProjectionTop() )
{
// Optimization opportunity: Turn off texture transform if not using texture coords.
g_pd3dDevice->SetTextureStageState( tu, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );
Centering = *GetCentering();
Projection = *GetProjectionTop();

// If no texture is set for this texture unit, don't bother setting it up.
IDirect3DBaseTexture9* pTexture = NULL;
g_pd3dDevice->GetTexture( tu, &pTexture );
if( pTexture == NULL )
continue;
pTexture->Release();
RageMatrix m;
RageMatrixMultiply( &m, GetCentering(), GetProjectionTop() );

if( g_bSphereMapping[tu] )
if ( g_bInvertY )
{
static const RageMatrix tex = RageMatrix
(
0.5f, 0.0f, 0.0f, 0.0f,
0.0f, -0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.0f, 1.0f
);
g_pd3dDevice->SetTransform( (D3DTRANSFORMSTATETYPE)(D3DTS_TEXTURE0+tu), (D3DMATRIX*)&tex );

// Tell D3D to use transformed reflection vectors as texture co-ordinate 0
// and then transform this coordinate by the specified texture matrix.
g_pd3dDevice->SetTextureStageState( tu, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR );
RageMatrix flip;
RageMatrixScale( &flip, +1, -1, +1 );
RageMatrixMultiply( &m, &flip, &m );
}
else

// Convert to OpenGL-style "pixel-centered" coords
RageMatrix m2 = GetCenteringMatrix( -0.5f, -0.5f, 0, 0 );
RageMatrix projection;
RageMatrixMultiply( &projection, &m2, &m );
g_pd3dDevice->SetTransform( D3DTS_PROJECTION, (D3DMATRIX*)&projection );

g_pd3dDevice->SetTransform( D3DTS_VIEW, (D3DMATRIX*)GetViewTop() );
g_pd3dDevice->SetTransform( D3DTS_WORLD, (D3DMATRIX*)GetWorldTop() );

FOREACH_ENUM( TextureUnit, tu )
{
/* Direct3D is expecting a 3x3 matrix loaded into the 4x4 in order
* to transform the 2-component texture coordinates. We currently
* only use translate and scale, and ignore the z component entirely,
* so convert the texture matrix from 4x4 to 3x3 by dropping z. */

const RageMatrix &tex1 = *GetTextureTop();
const RageMatrix tex2 = RageMatrix
(
tex1.m[0][0], tex1.m[0][1], tex1.m[0][3], 0,
tex1.m[1][0], tex1.m[1][1], tex1.m[1][3], 0,
tex1.m[3][0], tex1.m[3][1], tex1.m[3][3], 0,
0, 0, 0, 0
);
g_pd3dDevice->SetTransform( D3DTRANSFORMSTATETYPE(D3DTS_TEXTURE0+tu), (D3DMATRIX*)&tex2 );

g_pd3dDevice->SetTextureStageState( tu, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_PASSTHRU );
// If no texture is set for this texture unit, don't bother setting it up.
IDirect3DBaseTexture9* pTexture = NULL;
g_pd3dDevice->GetTexture( tu, &pTexture );
if (pTexture == NULL)
continue;
pTexture->Release();

// Optimization opportunity: Turn off texture transform if not using texture coords.
g_pd3dDevice->SetTextureStageState(tu, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2);

if ( g_bSphereMapping[tu] )
{
static const RageMatrix tex = RageMatrix
(
0.5f, 0.0f, 0.0f, 0.0f,
0.0f, -0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.0f, 1.0f
);
g_pd3dDevice->SetTransform( (D3DTRANSFORMSTATETYPE)(D3DTS_TEXTURE0 + tu), (D3DMATRIX*)&tex );

// Tell D3D to use transformed reflection vectors as texture co-ordinate 0
// and then transform this coordinate by the specified texture matrix.
g_pd3dDevice->SetTextureStageState( tu, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR );
}
else
{
/* Direct3D is expecting a 3x3 matrix loaded into the 4x4 in order
* to transform the 2-component texture coordinates. We currently
* only use translate and scale, and ignore the z component entirely,
* so convert the texture matrix from 4x4 to 3x3 by dropping z. */

const RageMatrix &tex1 = *GetTextureTop();
const RageMatrix tex2 = RageMatrix
(
tex1.m[0][0], tex1.m[0][1], tex1.m[0][3], 0,
tex1.m[1][0], tex1.m[1][1], tex1.m[1][3], 0,
tex1.m[3][0], tex1.m[3][1], tex1.m[3][3], 0,
0, 0, 0, 0
);
g_pd3dDevice->SetTransform( D3DTRANSFORMSTATETYPE(D3DTS_TEXTURE0 + tu), (D3DMATRIX*)&tex2 );

g_pd3dDevice->SetTextureStageState( tu, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_PASSTHRU );
}
}
}
}
Expand Down Expand Up @@ -825,7 +835,12 @@ class RageCompiledGeometrySWD3D : public RageCompiledGeometry
g_pd3dDevice->SetTransform( D3DTS_TEXTURE0, (D3DMATRIX*)&m );
}

g_pd3dDevice->SetFVF( D3DFVF_RageModelVertex );
if ( lastFVF != D3DFVF_RageModelVertex )
{
lastFVF = D3DFVF_RageModelVertex;
g_pd3dDevice->SetFVF( D3DFVF_RageModelVertex );
}

g_pd3dDevice->DrawIndexedPrimitiveUP(
D3DPT_TRIANGLELIST, // PrimitiveType
meshInfo.iVertexStart, // MinIndex
Expand Down Expand Up @@ -875,7 +890,12 @@ void RageDisplay_D3D::DrawQuadsInternal( const RageSpriteVertex v[], int iNumVer
vIndices[i*6+5] = i*4+0;
}

g_pd3dDevice->SetFVF( D3DFVF_RageSpriteVertex );
if ( lastFVF != D3DFVF_RageSpriteVertex )
{
lastFVF = D3DFVF_RageSpriteVertex;
g_pd3dDevice->SetFVF( D3DFVF_RageSpriteVertex );
}

SendCurrentMatrices();
g_pd3dDevice->DrawIndexedPrimitiveUP(
D3DPT_TRIANGLELIST, // PrimitiveType
Expand Down Expand Up @@ -911,7 +931,12 @@ void RageDisplay_D3D::DrawQuadStripInternal( const RageSpriteVertex v[], int iNu
vIndices[i*6+5] = i*2+3;
}

g_pd3dDevice->SetFVF( D3DFVF_RageSpriteVertex );
if ( lastFVF != D3DFVF_RageSpriteVertex )
{
lastFVF = D3DFVF_RageSpriteVertex;
g_pd3dDevice->SetFVF( D3DFVF_RageSpriteVertex );
}

SendCurrentMatrices();
g_pd3dDevice->DrawIndexedPrimitiveUP(
D3DPT_TRIANGLELIST, // PrimitiveType
Expand Down Expand Up @@ -953,7 +978,12 @@ void RageDisplay_D3D::DrawSymmetricQuadStripInternal( const RageSpriteVertex v[]
vIndices[i*12+11] = i*3+5;
}

g_pd3dDevice->SetFVF( D3DFVF_RageSpriteVertex );
if ( lastFVF != D3DFVF_RageSpriteVertex )
{
lastFVF = D3DFVF_RageSpriteVertex;
g_pd3dDevice->SetFVF( D3DFVF_RageSpriteVertex );
}

SendCurrentMatrices();
g_pd3dDevice->DrawIndexedPrimitiveUP(
D3DPT_TRIANGLELIST, // PrimitiveType
Expand All @@ -969,7 +999,12 @@ void RageDisplay_D3D::DrawSymmetricQuadStripInternal( const RageSpriteVertex v[]

void RageDisplay_D3D::DrawFanInternal( const RageSpriteVertex v[], int iNumVerts )
{
g_pd3dDevice->SetFVF( D3DFVF_RageSpriteVertex );
if ( lastFVF != D3DFVF_RageSpriteVertex )
{
lastFVF = D3DFVF_RageSpriteVertex;
g_pd3dDevice->SetFVF( D3DFVF_RageSpriteVertex );
}

SendCurrentMatrices();
g_pd3dDevice->DrawPrimitiveUP(
D3DPT_TRIANGLEFAN, // PrimitiveType
Expand All @@ -981,7 +1016,12 @@ void RageDisplay_D3D::DrawFanInternal( const RageSpriteVertex v[], int iNumVerts

void RageDisplay_D3D::DrawStripInternal( const RageSpriteVertex v[], int iNumVerts )
{
g_pd3dDevice->SetFVF( D3DFVF_RageSpriteVertex );
if ( lastFVF != D3DFVF_RageSpriteVertex )
{
lastFVF = D3DFVF_RageSpriteVertex;
g_pd3dDevice->SetFVF( D3DFVF_RageSpriteVertex );
}

SendCurrentMatrices();
g_pd3dDevice->DrawPrimitiveUP(
D3DPT_TRIANGLESTRIP, // PrimitiveType
Expand All @@ -993,7 +1033,12 @@ void RageDisplay_D3D::DrawStripInternal( const RageSpriteVertex v[], int iNumVer

void RageDisplay_D3D::DrawTrianglesInternal( const RageSpriteVertex v[], int iNumVerts )
{
g_pd3dDevice->SetFVF( D3DFVF_RageSpriteVertex );
if ( lastFVF != D3DFVF_RageSpriteVertex )
{
lastFVF = D3DFVF_RageSpriteVertex;
g_pd3dDevice->SetFVF( D3DFVF_RageSpriteVertex );
}

SendCurrentMatrices();
g_pd3dDevice->DrawPrimitiveUP(
D3DPT_TRIANGLELIST, // PrimitiveType
Expand Down
Loading

0 comments on commit c4f7ccc

Please sign in to comment.