Skip to content

Commit

Permalink
Merge pull request #10158 from hrydgard/misc-refactor
Browse files Browse the repository at this point in the history
Some minor refactors in the graphics code
  • Loading branch information
hrydgard authored Nov 19, 2017
2 parents f428f6f + 713afdf commit 7ab403e
Show file tree
Hide file tree
Showing 16 changed files with 159 additions and 210 deletions.
46 changes: 46 additions & 0 deletions GPU/Common/DrawEngineCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,52 @@ VertexDecoder *DrawEngineCommon::GetVertexDecoder(u32 vtype) {
return dec;
}

int DrawEngineCommon::ComputeNumVertsToDecode() const {
int vertsToDecode = 0;
if (drawCalls[0].indexType == GE_VTYPE_IDX_NONE >> GE_VTYPE_IDX_SHIFT) {
for (int i = 0; i < numDrawCalls; i++) {
const DeferredDrawCall &dc = drawCalls[i];
vertsToDecode += dc.vertexCount;
}
} else {
// TODO: Share this computation with DecodeVertsStep?
for (int i = 0; i < numDrawCalls; i++) {
const DeferredDrawCall &dc = drawCalls[i];
int lastMatch = i;
const int total = numDrawCalls;
int indexLowerBound = dc.indexLowerBound;
int indexUpperBound = dc.indexUpperBound;
for (int j = i + 1; j < total; ++j) {
if (drawCalls[j].verts != dc.verts)
break;

indexLowerBound = std::min(indexLowerBound, (int)drawCalls[j].indexLowerBound);
indexUpperBound = std::max(indexUpperBound, (int)drawCalls[j].indexUpperBound);
lastMatch = j;
}
vertsToDecode += indexUpperBound - indexLowerBound + 1;
i = lastMatch;
}
}
return vertsToDecode;
}

void DrawEngineCommon::DecodeVerts(u8 *dest) {
const UVScale origUV = gstate_c.uv;
for (; decodeCounter_ < numDrawCalls; decodeCounter_++) {
gstate_c.uv = uvScale[decodeCounter_];
DecodeVertsStep(dest, decodeCounter_, decodedVerts_); // NOTE! DecodeVertsStep can modify decodeCounter_!
}
gstate_c.uv = origUV;

// Sanity check
if (indexGen.Prim() < 0) {
ERROR_LOG_REPORT(G3D, "DecodeVerts: Failed to deduce prim: %i", indexGen.Prim());
// Force to points (0)
indexGen.AddPrim(GE_PRIM_POINTS, 0);
}
}

std::vector<std::string> DrawEngineCommon::DebugGetVertexLoaderIDs() {
std::vector<std::string> ids;
decoderMap_.Iterate([&](const uint32_t vtype, VertexDecoder *decoder) {
Expand Down
3 changes: 3 additions & 0 deletions GPU/Common/DrawEngineCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ class DrawEngineCommon {
protected:
virtual void ClearTrackedVertexArrays() {}

int ComputeNumVertsToDecode() const;
void DecodeVerts(u8 *dest);

// Preprocessing for spline/bezier
u32 NormalizeVertices(u8 *outPtr, u8 *bufPtr, const u8 *inPtr, int lowerBound, int upperBound, u32 vertType, int *vertexSize = nullptr);

Expand Down
30 changes: 7 additions & 23 deletions GPU/D3D11/DrawEngineD3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,22 +315,6 @@ void DrawEngineD3D11::SubmitPrim(void *verts, void *inds, GEPrimitiveType prim,
}
}

void DrawEngineD3D11::DecodeVerts() {
const UVScale origUV = gstate_c.uv;
for (; decodeCounter_ < numDrawCalls; decodeCounter_++) {
gstate_c.uv = uvScale[decodeCounter_];
DecodeVertsStep(decoded, decodeCounter_, decodedVerts_);
}
gstate_c.uv = origUV;

// Sanity check
if (indexGen.Prim() < 0) {
ERROR_LOG_REPORT(G3D, "DecodeVerts: Failed to deduce prim: %i", indexGen.Prim());
// Force to points (0)
indexGen.AddPrim(GE_PRIM_POINTS, 0);
}
}

void DrawEngineD3D11::MarkUnreliable(VertexArrayInfoD3D11 *vai) {
vai->status = VertexArrayInfoD3D11::VAI_UNRELIABLE;
if (vai->vbo) {
Expand Down Expand Up @@ -438,7 +422,7 @@ void DrawEngineD3D11::DoFlush() {
vai->minihash = ComputeMiniHash();
vai->status = VertexArrayInfoD3D11::VAI_HASHING;
vai->drawsUntilNextFullHash = 0;
DecodeVerts(); // writes to indexGen
DecodeVerts(decoded); // writes to indexGen
vai->numVerts = indexGen.VertexCount();
vai->prim = indexGen.Prim();
vai->maxIndex = indexGen.MaxIndex();
Expand All @@ -463,7 +447,7 @@ void DrawEngineD3D11::DoFlush() {
}
if (newMiniHash != vai->minihash || newHash != vai->hash) {
MarkUnreliable(vai);
DecodeVerts();
DecodeVerts(decoded);
goto rotateVBO;
}
if (vai->numVerts > 64) {
Expand All @@ -482,13 +466,13 @@ void DrawEngineD3D11::DoFlush() {
u32 newMiniHash = ComputeMiniHash();
if (newMiniHash != vai->minihash) {
MarkUnreliable(vai);
DecodeVerts();
DecodeVerts(decoded);
goto rotateVBO;
}
}

if (vai->vbo == 0) {
DecodeVerts();
DecodeVerts(decoded);
vai->numVerts = indexGen.VertexCount();
vai->prim = indexGen.Prim();
vai->maxIndex = indexGen.MaxIndex();
Expand Down Expand Up @@ -554,14 +538,14 @@ void DrawEngineD3D11::DoFlush() {
if (vai->lastFrame != gpuStats.numFlips) {
vai->numFrames++;
}
DecodeVerts();
DecodeVerts(decoded);
goto rotateVBO;
}
}

vai->lastFrame = gpuStats.numFlips;
} else {
DecodeVerts();
DecodeVerts(decoded);
rotateVBO:
gpuStats.numUncachedVertsDrawn += indexGen.VertexCount();
useElements = !indexGen.SeenOnlyPurePrims() || prim == GE_PRIM_TRIANGLE_FAN;
Expand Down Expand Up @@ -632,7 +616,7 @@ void DrawEngineD3D11::DoFlush() {
}
}
} else {
DecodeVerts();
DecodeVerts(decoded);
bool hasColor = (lastVType_ & GE_VTYPE_COL_MASK) != GE_VTYPE_COL_NONE;
if (gstate.isModeThrough()) {
gstate_c.vertexFullAlpha = gstate_c.vertexFullAlpha && (hasColor || gstate.getMaterialAmbientA() == 255);
Expand Down
3 changes: 1 addition & 2 deletions GPU/D3D11/DrawEngineD3D11.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class DrawEngineD3D11 : public DrawEngineCommon {
void FinishDeferred() {
if (!numDrawCalls)
return;
DecodeVerts();
DecodeVerts(decoded);
}

void DispatchFlush() override { Flush(); }
Expand All @@ -146,7 +146,6 @@ class DrawEngineD3D11 : public DrawEngineCommon {
void ClearInputLayoutMap();

private:
void DecodeVerts();
void DoFlush();

void ApplyDrawState(int prim);
Expand Down
30 changes: 7 additions & 23 deletions GPU/Directx9/DrawEngineDX9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,22 +296,6 @@ void DrawEngineDX9::SubmitPrim(void *verts, void *inds, GEPrimitiveType prim, in
}
}

void DrawEngineDX9::DecodeVerts() {
const UVScale origUV = gstate_c.uv;
for (; decodeCounter_ < numDrawCalls; decodeCounter_++) {
gstate_c.uv = uvScale[decodeCounter_];
DecodeVertsStep(decoded, decodeCounter_, decodedVerts_);
}
gstate_c.uv = origUV;

// Sanity check
if (indexGen.Prim() < 0) {
ERROR_LOG_REPORT(G3D, "DecodeVerts: Failed to deduce prim: %i", indexGen.Prim());
// Force to points (0)
indexGen.AddPrim(GE_PRIM_POINTS, 0);
}
}

void DrawEngineDX9::MarkUnreliable(VertexArrayInfoDX9 *vai) {
vai->status = VertexArrayInfoDX9::VAI_UNRELIABLE;
if (vai->vbo) {
Expand Down Expand Up @@ -424,7 +408,7 @@ void DrawEngineDX9::DoFlush() {
vai->minihash = ComputeMiniHash();
vai->status = VertexArrayInfoDX9::VAI_HASHING;
vai->drawsUntilNextFullHash = 0;
DecodeVerts(); // writes to indexGen
DecodeVerts(decoded); // writes to indexGen
vai->numVerts = indexGen.VertexCount();
vai->prim = indexGen.Prim();
vai->maxIndex = indexGen.MaxIndex();
Expand All @@ -450,7 +434,7 @@ void DrawEngineDX9::DoFlush() {
}
if (newMiniHash != vai->minihash || newHash != vai->hash) {
MarkUnreliable(vai);
DecodeVerts();
DecodeVerts(decoded);
goto rotateVBO;
}
if (vai->numVerts > 64) {
Expand All @@ -469,13 +453,13 @@ void DrawEngineDX9::DoFlush() {
u32 newMiniHash = ComputeMiniHash();
if (newMiniHash != vai->minihash) {
MarkUnreliable(vai);
DecodeVerts();
DecodeVerts(decoded);
goto rotateVBO;
}
}

if (vai->vbo == 0) {
DecodeVerts();
DecodeVerts(decoded);
vai->numVerts = indexGen.VertexCount();
vai->prim = indexGen.Prim();
vai->maxIndex = indexGen.MaxIndex();
Expand Down Expand Up @@ -544,14 +528,14 @@ void DrawEngineDX9::DoFlush() {
if (vai->lastFrame != gpuStats.numFlips) {
vai->numFrames++;
}
DecodeVerts();
DecodeVerts(decoded);
goto rotateVBO;
}
}

vai->lastFrame = gpuStats.numFlips;
} else {
DecodeVerts();
DecodeVerts(decoded);
rotateVBO:
gpuStats.numUncachedVertsDrawn += indexGen.VertexCount();
useElements = !indexGen.SeenOnlyPurePrims();
Expand Down Expand Up @@ -596,7 +580,7 @@ void DrawEngineDX9::DoFlush() {
}
}
} else {
DecodeVerts();
DecodeVerts(decoded);
bool hasColor = (lastVType_ & GE_VTYPE_COL_MASK) != GE_VTYPE_COL_NONE;
if (gstate.isModeThrough()) {
gstate_c.vertexFullAlpha = gstate_c.vertexFullAlpha && (hasColor || gstate.getMaterialAmbientA() == 255);
Expand Down
3 changes: 1 addition & 2 deletions GPU/Directx9/DrawEngineDX9.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class DrawEngineDX9 : public DrawEngineCommon {
void FinishDeferred() {
if (!numDrawCalls)
return;
DecodeVerts();
DecodeVerts(decoded);
}

void DispatchFlush() override { Flush(); }
Expand All @@ -139,7 +139,6 @@ class DrawEngineDX9 : public DrawEngineCommon {
}

private:
void DecodeVerts();
void DoFlush();

void ApplyDrawState(int prim);
Expand Down
29 changes: 7 additions & 22 deletions GPU/GLES/DrawEngineGLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,21 +314,6 @@ void DrawEngineGLES::SubmitPrim(void *verts, void *inds, GEPrimitiveType prim, i
}
}

void DrawEngineGLES::DecodeVerts() {
const UVScale origUV = gstate_c.uv;
for (; decodeCounter_ < numDrawCalls; decodeCounter_++) {
gstate_c.uv = uvScale[decodeCounter_];
DecodeVertsStep(decoded, decodeCounter_, decodedVerts_);
}
gstate_c.uv = origUV;
// Sanity check
if (indexGen.Prim() < 0) {
ERROR_LOG_REPORT(G3D, "DecodeVerts: Failed to deduce prim: %i", indexGen.Prim());
// Force to points (0)
indexGen.AddPrim(GE_PRIM_POINTS, 0);
}
}

void DrawEngineGLES::MarkUnreliable(VertexArrayInfo *vai) {
vai->status = VertexArrayInfo::VAI_UNRELIABLE;
if (vai->vbo) {
Expand Down Expand Up @@ -495,7 +480,7 @@ void DrawEngineGLES::DoFlush() {
vai->minihash = ComputeMiniHash();
vai->status = VertexArrayInfo::VAI_HASHING;
vai->drawsUntilNextFullHash = 0;
DecodeVerts(); // writes to indexGen
DecodeVerts(decoded); // writes to indexGen
vai->numVerts = indexGen.VertexCount();
vai->prim = indexGen.Prim();
vai->maxIndex = indexGen.MaxIndex();
Expand All @@ -521,7 +506,7 @@ void DrawEngineGLES::DoFlush() {
}
if (newMiniHash != vai->minihash || newHash != vai->hash) {
MarkUnreliable(vai);
DecodeVerts();
DecodeVerts(decoded);
goto rotateVBO;
}
if (vai->numVerts > 64) {
Expand All @@ -540,13 +525,13 @@ void DrawEngineGLES::DoFlush() {
u32 newMiniHash = ComputeMiniHash();
if (newMiniHash != vai->minihash) {
MarkUnreliable(vai);
DecodeVerts();
DecodeVerts(decoded);
goto rotateVBO;
}
}

if (vai->vbo == 0) {
DecodeVerts();
DecodeVerts(decoded);
vai->numVerts = indexGen.VertexCount();
vai->prim = indexGen.Prim();
vai->maxIndex = indexGen.MaxIndex();
Expand Down Expand Up @@ -615,14 +600,14 @@ void DrawEngineGLES::DoFlush() {
if (vai->lastFrame != gpuStats.numFlips) {
vai->numFrames++;
}
DecodeVerts();
DecodeVerts(decoded);
goto rotateVBO;
}
}

vai->lastFrame = gpuStats.numFlips;
} else {
DecodeVerts();
DecodeVerts(decoded);

rotateVBO:
gpuStats.numUncachedVertsDrawn += indexGen.VertexCount();
Expand Down Expand Up @@ -667,7 +652,7 @@ void DrawEngineGLES::DoFlush() {
glDrawArrays(glprim[prim], 0, vertexCount);
}
} else {
DecodeVerts();
DecodeVerts(decoded);
bool hasColor = (lastVType_ & GE_VTYPE_COL_MASK) != GE_VTYPE_COL_NONE;
if (gstate.isModeThrough()) {
gstate_c.vertexFullAlpha = gstate_c.vertexFullAlpha && (hasColor || gstate.getMaterialAmbientA() == 255);
Expand Down
3 changes: 1 addition & 2 deletions GPU/GLES/DrawEngineGLES.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class DrawEngineGLES : public DrawEngineCommon, public GfxResourceHolder {
void FinishDeferred() {
if (!numDrawCalls)
return;
DecodeVerts();
DecodeVerts(decoded);
}

bool IsCodePtrVertexDecoder(const u8 *ptr) const;
Expand All @@ -154,7 +154,6 @@ class DrawEngineGLES : public DrawEngineCommon, public GfxResourceHolder {
void DecimateBuffers();

private:
void DecodeVerts();
void DoFlush();
void ApplyDrawState(int prim);
void ApplyDrawStateLate();
Expand Down
Loading

0 comments on commit 7ab403e

Please sign in to comment.