Skip to content

Commit

Permalink
Merge pull request #12790 from unknownbrackets/gpu-minor
Browse files Browse the repository at this point in the history
Use consistent state for render mode and hw transform
  • Loading branch information
hrydgard authored Apr 4, 2020
2 parents a26f8ac + ac60e2e commit 350589d
Show file tree
Hide file tree
Showing 37 changed files with 121 additions and 100 deletions.
18 changes: 18 additions & 0 deletions GPU/Common/DrawEngineCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ DrawEngineCommon::DrawEngineCommon() : decoderMap_(16) {
decJitCache_ = new VertexDecoderJitCache();
transformed = (TransformedVertex *)AllocateMemoryPages(TRANSFORMED_VERTEX_BUFFER_SIZE, MEM_PROT_READ | MEM_PROT_WRITE);
transformedExpanded = (TransformedVertex *)AllocateMemoryPages(3 * TRANSFORMED_VERTEX_BUFFER_SIZE, MEM_PROT_READ | MEM_PROT_WRITE);
useHWTransform_ = g_Config.bHardwareTransform;
useHWTessellation_ = UpdateUseHWTessellation(g_Config.bHardwareTessellation);
}

DrawEngineCommon::~DrawEngineCommon() {
Expand Down Expand Up @@ -171,6 +173,9 @@ void DrawEngineCommon::Resized() {
});
decoderMap_.Clear();
ClearTrackedVertexArrays();

useHWTransform_ = g_Config.bHardwareTransform;
useHWTessellation_ = UpdateUseHWTessellation(g_Config.bHardwareTessellation);
}

u32 DrawEngineCommon::NormalizeVertices(u8 *outPtr, u8 *bufPtr, const u8 *inPtr, int lowerBound, int upperBound, u32 vertType, int *vertexSize) {
Expand Down Expand Up @@ -737,6 +742,19 @@ void DrawEngineCommon::SubmitPrim(void *verts, void *inds, GEPrimitiveType prim,
}
}

bool DrawEngineCommon::CanUseHardwareTransform(int prim) {
if (!useHWTransform_)
return false;
return !gstate.isModeThrough() && prim != GE_PRIM_RECTANGLES;
}

bool DrawEngineCommon::CanUseHardwareTessellation(GEPatchPrimType prim) {
if (useHWTessellation_) {
return CanUseHardwareTransform(PatchPrimToPrim(prim));
}
return false;
}

void TessellationDataTransfer::CopyControlPoints(float *pos, float *tex, float *col, int posStride, int texStride, int colStride, const SimpleVertex *const *points, int size, u32 vertType) {
bool hasColor = (vertType & GE_VTYPE_COL_MASK) != 0;
bool hasTexCoord = (vertType & GE_VTYPE_TC_MASK) != 0;
Expand Down
7 changes: 7 additions & 0 deletions GPU/Common/DrawEngineCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ class DrawEngineCommon {
void SubmitCurve(const void *control_points, const void *indices, Surface &surface, u32 vertType, int *bytesRead, const char *scope);
void ClearSplineBezierWeights();

bool CanUseHardwareTransform(int prim);
bool CanUseHardwareTessellation(GEPatchPrimType prim);

std::vector<std::string> DebugGetVertexLoaderIDs();
std::string DebugGetVertexLoaderString(std::string id, DebugShaderStringType stringType);

Expand All @@ -101,6 +104,7 @@ class DrawEngineCommon {
VertexDecoder *GetVertexDecoder(u32 vtype);

protected:
virtual bool UpdateUseHWTessellation(bool enabled) { return enabled; }
virtual void ClearTrackedVertexArrays() {}

int ComputeNumVertsToDecode() const;
Expand Down Expand Up @@ -128,6 +132,9 @@ class DrawEngineCommon {
return 1;
}

bool useHWTransform_ = false;
bool useHWTessellation_ = false;

// Vertex collector buffers
u8 *decoded = nullptr;
u16 *decIndex = nullptr;
Expand Down
4 changes: 4 additions & 0 deletions GPU/Common/FramebufferCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ class FramebufferManagerCommon {
return displayFramebuf_ ? displayFormat_ : GE_FORMAT_INVALID;
}

bool UseBufferedRendering() {
return useBufferedRendering_;
}

bool MayIntersectFramebuffer(u32 start) {
// Clear the cache/kernel bits.
start = start & 0x3FFFFFFF;
Expand Down
6 changes: 0 additions & 6 deletions GPU/Common/GPUStateUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@

#include "GPU/Common/GPUStateUtils.h"

bool CanUseHardwareTransform(int prim) {
if (!g_Config.bHardwareTransform)
return false;
return !gstate.isModeThrough() && prim != GE_PRIM_RECTANGLES;
}

bool IsStencilTestOutputDisabled() {
// The mask applies on all stencil ops.
if (gstate.isStencilTestEnabled() && (gstate.pmska & 0xFF) != 0xFF) {
Expand Down
1 change: 0 additions & 1 deletion GPU/Common/GPUStateUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ StencilValueType ReplaceAlphaWithStencilType();
ReplaceAlphaType ReplaceAlphaWithStencil(ReplaceBlendType replaceBlend);
ReplaceBlendType ReplaceBlendWithShader(bool allowShaderBlend, GEBufferFormat bufferFormat);

bool CanUseHardwareTransform(int prim);
LogicOpReplaceType ReplaceLogicOpType();


Expand Down
4 changes: 2 additions & 2 deletions GPU/Common/ShaderId.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ std::string VertexShaderDesc(const VShaderID &id) {
return desc.str();
}

void ComputeVertexShaderID(VShaderID *id_out, u32 vertType, bool useHWTransform) {
void ComputeVertexShaderID(VShaderID *id_out, u32 vertType, bool useHWTransform, bool useHWTessellation) {
bool isModeThrough = (vertType & GE_VTYPE_THROUGH) != 0;
bool doTexture = gstate.isTextureMapEnabled() && !gstate.isModeClear();
bool doTextureTransform = gstate.getUVGenMode() == GE_TEXMAP_TEXTURE_MATRIX;
Expand Down Expand Up @@ -131,7 +131,7 @@ void ComputeVertexShaderID(VShaderID *id_out, u32 vertType, bool useHWTransform)
id.SetBit(VS_BIT_NORM_REVERSE, gstate.areNormalsReversed());
id.SetBit(VS_BIT_HAS_TEXCOORD, hasTexcoord);

if (g_Config.bHardwareTessellation) {
if (useHWTessellation) {
id.SetBit(VS_BIT_BEZIER, doBezier);
id.SetBit(VS_BIT_SPLINE, doSpline);
if (doBezier || doSpline) {
Expand Down
2 changes: 1 addition & 1 deletion GPU/Common/ShaderId.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ class Bugs;
}


void ComputeVertexShaderID(VShaderID *id, uint32_t vertexType, bool useHWTransform);
void ComputeVertexShaderID(VShaderID *id, uint32_t vertexType, bool useHWTransform, bool useHWTessellation);
// Generates a compact string that describes the shader. Useful in a list to get an overview
// of the current flora of shaders.
std::string VertexShaderDesc(const VShaderID &id);
Expand Down
8 changes: 3 additions & 5 deletions GPU/Common/ShaderUniforms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include "GPU/Common/FramebufferCommon.h"
#include "GPU/Common/GPUStateUtils.h"
#include "GPU/Math3D.h"
#include "Core/Reporting.h"
#include "Core/Config.h"

using namespace Lin;

Expand Down Expand Up @@ -91,7 +89,7 @@ void CalcCullRange(float minValues[4], float maxValues[4], bool flipViewport, bo
maxValues[3] = NAN;
}

void BaseUpdateUniforms(UB_VS_FS_Base *ub, uint64_t dirtyUniforms, bool flipViewport) {
void BaseUpdateUniforms(UB_VS_FS_Base *ub, uint64_t dirtyUniforms, bool flipViewport, bool useBufferedRendering) {
if (dirtyUniforms & DIRTY_TEXENV) {
Uint8x3ToFloat4(ub->texEnvColor, gstate.texenvcolor);
}
Expand Down Expand Up @@ -149,7 +147,7 @@ void BaseUpdateUniforms(UB_VS_FS_Base *ub, uint64_t dirtyUniforms, bool flipView
ConvertProjMatrixToVulkan(flippedMatrix);
}

if (g_Config.iRenderingMode == FB_NON_BUFFERED_MODE && g_display_rotation != DisplayRotation::ROTATE_0) {
if (!useBufferedRendering && g_display_rotation != DisplayRotation::ROTATE_0) {
flippedMatrix = flippedMatrix * g_display_rot_matrix;
}
CopyMatrix4x4(ub->proj, flippedMatrix.getReadPtr());
Expand All @@ -162,7 +160,7 @@ void BaseUpdateUniforms(UB_VS_FS_Base *ub, uint64_t dirtyUniforms, bool flipView
} else {
proj_through.setOrthoVulkan(0.0f, gstate_c.curRTWidth, 0, gstate_c.curRTHeight, 0, 1);
}
if (g_Config.iRenderingMode == FB_NON_BUFFERED_MODE && g_display_rotation != DisplayRotation::ROTATE_0) {
if (!useBufferedRendering && g_display_rotation != DisplayRotation::ROTATE_0) {
proj_through = proj_through * g_display_rot_matrix;
}
CopyMatrix4x4(ub->proj_through, proj_through.getReadPtr());
Expand Down
2 changes: 1 addition & 1 deletion GPU/Common/ShaderUniforms.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ R"( float4x3 u_bone[8];

void CalcCullRange(float minValues[4], float maxValues[4], bool flipViewport, bool hasNegZ);

void BaseUpdateUniforms(UB_VS_FS_Base *ub, uint64_t dirtyUniforms, bool flipViewport);
void BaseUpdateUniforms(UB_VS_FS_Base *ub, uint64_t dirtyUniforms, bool flipViewport, bool useBufferedRendering);
void LightUpdateUniforms(UB_VS_Lights *ub, uint64_t dirtyUniforms);
void BoneUpdateUniforms(UB_VS_Bones *ub, uint64_t dirtyUniforms);

2 changes: 1 addition & 1 deletion GPU/Common/SoftwareTransformCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ void SoftwareTransform(
numTrans = 0;
drawIndexed = false;

bool useBufferedRendering = g_Config.iRenderingMode != FB_NON_BUFFERED_MODE;
bool useBufferedRendering = fbman->UseBufferedRendering();

bool flippedY = g_Config.iGPUBackend == (int)GPUBackend::OPENGL && !useBufferedRendering;

Expand Down
7 changes: 0 additions & 7 deletions GPU/Common/SplineCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@
#include "GPU/ge_constants.h"
#include "GPU/GPUState.h" // only needed for UVScale stuff

bool CanUseHardwareTessellation(GEPatchPrimType prim) {
if (g_Config.bHardwareTessellation && !g_Config.bSoftwareRendering) {
return CanUseHardwareTransform(PatchPrimToPrim(prim));
}
return false;
}

class SimpleBufferManager {
private:
u8 *buf_;
Expand Down
2 changes: 0 additions & 2 deletions GPU/Common/SplineCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,6 @@ void SoftwareTessellation(OutputBuffers &output, const Surface &surface, u32 ori

} // namespace Spline

bool CanUseHardwareTessellation(GEPatchPrimType prim);

// Define function object for TemplateParameterDispatcher
#define TEMPLATE_PARAMETER_DISPATCHER_FUNCTION(NAME, FUNCNAME, FUNCTYPE) \
struct NAME { \
Expand Down
7 changes: 3 additions & 4 deletions GPU/Common/TextureCacheCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ bool TextureCacheCommon::AttachFramebuffer(TexCacheEntry *entry, u32 address, Vi
}
} else {
// Apply to buffered mode only.
if (!(g_Config.iRenderingMode == FB_BUFFERED_MODE))
if (!framebufferManager_->UseBufferedRendering())
return false;

const bool clutFormat =
Expand Down Expand Up @@ -868,8 +868,7 @@ void TextureCacheCommon::SetTextureFramebuffer(TexCacheEntry *entry, VirtualFram
_dbg_assert_msg_(G3D, framebuffer != nullptr, "Framebuffer must not be null.");

framebuffer->usageFlags |= FB_USAGE_TEXTURE;
bool useBufferedRendering = g_Config.iRenderingMode != FB_NON_BUFFERED_MODE;
if (useBufferedRendering) {
if (framebufferManager_->UseBufferedRendering()) {
const u64 cachekey = entry->CacheKey();
const auto &fbInfo = fbTexInfo_[cachekey];

Expand Down Expand Up @@ -910,7 +909,7 @@ void TextureCacheCommon::SetTextureFramebuffer(TexCacheEntry *entry, VirtualFram
}

bool TextureCacheCommon::SetOffsetTexture(u32 offset) {
if (g_Config.iRenderingMode != FB_BUFFERED_MODE) {
if (!framebufferManager_->UseBufferedRendering()) {
return false;
}
u32 texaddr = gstate.getTextureAddress(0);
Expand Down
8 changes: 4 additions & 4 deletions GPU/D3D11/DrawEngineD3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,11 +514,11 @@ void DrawEngineD3D11::DoFlush() {

D3D11VertexShader *vshader;
D3D11FragmentShader *fshader;
shaderManager_->GetShaders(prim, lastVType_, &vshader, &fshader, useHWTransform);
shaderManager_->GetShaders(prim, lastVType_, &vshader, &fshader, useHWTransform, useHWTessellation_);
ID3D11InputLayout *inputLayout = SetupDecFmtForDraw(vshader, dec_->GetDecVtxFmt(), dec_->VertexType());
context_->PSSetShader(fshader->GetShader(), nullptr, 0);
context_->VSSetShader(vshader->GetShader(), nullptr, 0);
shaderManager_->UpdateUniforms();
shaderManager_->UpdateUniforms(framebufferManager_->UseBufferedRendering());
shaderManager_->BindUniforms();

context_->IASetInputLayout(inputLayout);
Expand Down Expand Up @@ -598,10 +598,10 @@ void DrawEngineD3D11::DoFlush() {

D3D11VertexShader *vshader;
D3D11FragmentShader *fshader;
shaderManager_->GetShaders(prim, lastVType_, &vshader, &fshader, false);
shaderManager_->GetShaders(prim, lastVType_, &vshader, &fshader, false, false);
context_->PSSetShader(fshader->GetShader(), nullptr, 0);
context_->VSSetShader(vshader->GetShader(), nullptr, 0);
shaderManager_->UpdateUniforms();
shaderManager_->UpdateUniforms(framebufferManager_->UseBufferedRendering());
shaderManager_->BindUniforms();

// We really do need a vertex layout for each vertex shader (or at least check its ID bits for what inputs it uses)!
Expand Down
3 changes: 1 addition & 2 deletions GPU/D3D11/GPU_D3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@ void GPU_D3D11::DeviceRestore() {
}

void GPU_D3D11::InitClear() {
bool useNonBufferedRendering = g_Config.iRenderingMode == FB_NON_BUFFERED_MODE;
if (useNonBufferedRendering) {
if (!framebufferManager_->UseBufferedRendering()) {
// device_->Clear(0, NULL, D3DCLEAR_STENCIL | D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.f, 0);
}
}
Expand Down
8 changes: 4 additions & 4 deletions GPU/D3D11/ShaderManagerD3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ void ShaderManagerD3D11::DirtyLastShader() {
gstate_c.Dirty(DIRTY_VERTEXSHADER_STATE | DIRTY_FRAGMENTSHADER_STATE);
}

uint64_t ShaderManagerD3D11::UpdateUniforms() {
uint64_t ShaderManagerD3D11::UpdateUniforms(bool useBufferedRendering) {
uint64_t dirty = gstate_c.GetDirtyUniforms();
if (dirty != 0) {
D3D11_MAPPED_SUBRESOURCE map;
if (dirty & DIRTY_BASE_UNIFORMS) {
BaseUpdateUniforms(&ub_base, dirty, true);
BaseUpdateUniforms(&ub_base, dirty, true, useBufferedRendering);
context_->Map(push_base, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
memcpy(map.pData, &ub_base, sizeof(ub_base));
context_->Unmap(push_base, 0);
Expand Down Expand Up @@ -178,13 +178,13 @@ void ShaderManagerD3D11::BindUniforms() {
context_->PSSetConstantBuffers(0, 1, ps_cbs);
}

void ShaderManagerD3D11::GetShaders(int prim, u32 vertType, D3D11VertexShader **vshader, D3D11FragmentShader **fshader, bool useHWTransform) {
void ShaderManagerD3D11::GetShaders(int prim, u32 vertType, D3D11VertexShader **vshader, D3D11FragmentShader **fshader, bool useHWTransform, bool useHWTessellation) {
VShaderID VSID;
FShaderID FSID;

if (gstate_c.IsDirty(DIRTY_VERTEXSHADER_STATE)) {
gstate_c.Clean(DIRTY_VERTEXSHADER_STATE);
ComputeVertexShaderID(&VSID, vertType, useHWTransform);
ComputeVertexShaderID(&VSID, vertType, useHWTransform, useHWTessellation);
} else {
VSID = lastVSID_;
}
Expand Down
4 changes: 2 additions & 2 deletions GPU/D3D11/ShaderManagerD3D11.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class ShaderManagerD3D11 : public ShaderManagerCommon {
ShaderManagerD3D11(Draw::DrawContext *draw, ID3D11Device *device, ID3D11DeviceContext *context, D3D_FEATURE_LEVEL featureLevel);
~ShaderManagerD3D11();

void GetShaders(int prim, u32 vertType, D3D11VertexShader **vshader, D3D11FragmentShader **fshader, bool useHWTransform);
void GetShaders(int prim, u32 vertType, D3D11VertexShader **vshader, D3D11FragmentShader **fshader, bool useHWTransform, bool useHWTessellation);
void ClearShaders();
void DirtyLastShader() override;

Expand All @@ -94,7 +94,7 @@ class ShaderManagerD3D11 : public ShaderManagerCommon {
std::vector<std::string> DebugGetShaderIDs(DebugShaderType type);
std::string DebugGetShaderString(std::string id, DebugShaderType type, DebugShaderStringType stringType);

uint64_t UpdateUniforms();
uint64_t UpdateUniforms(bool useBufferedRendering);
void BindUniforms();

// TODO: Avoid copying these buffers if same as last draw, can still point to it assuming we're still in the same pushbuffer.
Expand Down
2 changes: 1 addition & 1 deletion GPU/D3D11/StateMappingD3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void DrawEngineD3D11::ApplyDrawState(int prim) {
return;
}

bool useBufferedRendering = g_Config.iRenderingMode != FB_NON_BUFFERED_MODE;
bool useBufferedRendering = framebufferManager_->UseBufferedRendering();
// Blend
if (gstate_c.IsDirty(DIRTY_BLEND_STATE)) {
gstate_c.SetAllowShaderBlend(!g_Config.bDisableSlowFramebufEffects);
Expand Down
6 changes: 3 additions & 3 deletions GPU/Directx9/DrawEngineDX9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ void DrawEngineDX9::DoFlush() {
GEPrimitiveType prim = prevPrim_;
ApplyDrawState(prim);

VSShader *vshader = shaderManager_->ApplyShader(prim, lastVType_);
VSShader *vshader = shaderManager_->ApplyShader(CanUseHardwareTransform(prim), useHWTessellation_, lastVType_);

if (vshader->UseHWTransform()) {
LPDIRECT3DVERTEXBUFFER9 vb_ = NULL;
Expand Down Expand Up @@ -491,7 +491,7 @@ void DrawEngineDX9::DoFlush() {
}

ApplyDrawStateLate();
vshader = shaderManager_->ApplyShader(prim, lastVType_);
vshader = shaderManager_->ApplyShader(CanUseHardwareTransform(prim), useHWTessellation_, lastVType_);
IDirect3DVertexDeclaration9 *pHardwareVertexDecl = SetupDecFmtForDraw(vshader, dec_->GetDecVtxFmt(), dec_->VertexType());

if (pHardwareVertexDecl) {
Expand Down Expand Up @@ -552,7 +552,7 @@ void DrawEngineDX9::DoFlush() {
maxIndex, drawBuffer, numTrans, drawIndexed, &params, &result);

ApplyDrawStateLate();
vshader = shaderManager_->ApplyShader(prim, lastVType_);
vshader = shaderManager_->ApplyShader(false, false, lastVType_);

if (result.action == SW_DRAW_PRIMITIVES) {
if (result.setStencil) {
Expand Down
4 changes: 4 additions & 0 deletions GPU/Directx9/DrawEngineDX9.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ class DrawEngineDX9 : public DrawEngineCommon {

void DispatchFlush() override { Flush(); }

protected:
// Not currently supported.
bool UpdateUseHWTessellation(bool enable) override { return false; }

private:
void DoFlush();

Expand Down
4 changes: 1 addition & 3 deletions GPU/Directx9/GPU_DX9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ GPU_DX9::GPU_DX9(GraphicsContext *gfxCtx, Draw::DrawContext *draw)

if (g_Config.bHardwareTessellation) {
// Disable hardware tessellation bacause DX9 is still unsupported.
g_Config.bHardwareTessellation = false;
ERROR_LOG(G3D, "Hardware Tessellation is unsupported, falling back to software tessellation");
auto gr = GetI18NCategory("Graphics");
host->NotifyUserMessage(gr->T("Turn off Hardware Tessellation - unsupported"), 2.5f, 0xFF3030FF);
Expand Down Expand Up @@ -256,8 +255,7 @@ void GPU_DX9::DeviceRestore() {
}

void GPU_DX9::InitClear() {
bool useNonBufferedRendering = g_Config.iRenderingMode == FB_NON_BUFFERED_MODE;
if (useNonBufferedRendering) {
if (!framebufferManager_->UseBufferedRendering()) {
dxstate.depthWrite.set(true);
dxstate.colorMask.set(true, true, true, true);
device_->Clear(0, NULL, D3DCLEAR_STENCIL|D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.f, 0);
Expand Down
Loading

0 comments on commit 350589d

Please sign in to comment.