Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

D3D9 state cache cleanup #15723

Merged
merged 3 commits into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Remove unnecessary complexity from the D3D9 state cache
  • Loading branch information
hrydgard committed Jul 24, 2022
commit e758506bdbc3953e37bf6e9ca92800450160dc90
4 changes: 1 addition & 3 deletions Common/GPU/D3D9/D3D9StateCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ void DirectXState::Restore() {
stencilTest.restore(); count++;
stencilOp.restore(); count++;
stencilFunc.restore(); count++;
stencilMask.restore(); count++;

dither.restore(); count++;
stencilWriteMask.restore(); count++;

texMinFilter.restore(); count++;
texMagFilter.restore(); count++;
Expand Down
98 changes: 3 additions & 95 deletions Common/GPU/D3D9/D3D9StateCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,66 +288,6 @@ class DirectXState {
}
};

class SavedColorMask {
DWORD mask;
public:
SavedColorMask() {
mask = D3DCOLORWRITEENABLE_RED | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_ALPHA;
DirectXState::state_count++;
}

inline void set(bool r, bool g, bool b, bool a) {
DWORD newmask = 0;
if (r) {
newmask |= D3DCOLORWRITEENABLE_RED;
}
if (g) {
newmask |= D3DCOLORWRITEENABLE_GREEN;
}
if (b) {
newmask |= D3DCOLORWRITEENABLE_BLUE;
}
if (a) {
newmask |= D3DCOLORWRITEENABLE_ALPHA;
}
if (mask != newmask) {
mask = newmask;
restore();
}
}
void force(bool r, bool g, bool b, bool a) {
DWORD old = mask;
set(r, g, b, a);
mask = old;
}
inline void restore() {
pD3Ddevice->SetRenderState(D3DRS_COLORWRITEENABLE, mask);
}
};


class BoolUnused {
public:
BoolUnused() {
DirectXState::state_count++;
}
inline void set(bool) {
// Nothing.
}
void force(bool) {
// Nothing.
}
inline void restore() {
// Nothing.
}
inline void enable() {
set(true);
}
inline void disable() {
set(false);
}
};

class StateVp {
D3DVIEWPORT9 viewport;
public:
Expand Down Expand Up @@ -404,36 +344,6 @@ class DirectXState {
}
};

class CullMode {
DWORD cull;
public:
CullMode() : cull (D3DCULL_NONE) {
}

inline void set(int wantcull, int cullmode) {
DWORD newcull;
if (!wantcull) {
// disable
newcull = D3DCULL_NONE;
} else {
// add front face ...
newcull = cullmode==0 ? D3DCULL_CW:D3DCULL_CCW;
}
if (cull != newcull) {
cull = newcull;
restore();
}
}
void force(int wantcull, int cullmode) {
DWORD old = cull;
set(wantcull, cullmode);
cull = old;
}
inline void restore() {
pD3Ddevice->SetRenderState(D3DRS_CULLMODE, cull);
}
};

bool initialized;

public:
Expand All @@ -451,9 +361,7 @@ class DirectXState {

BoolState<D3DRS_SCISSORTESTENABLE, false> scissorTest;

BoolUnused dither;

CullMode cullMode;
DxState1<D3DRS_CULLMODE, D3DCULL_NONE> cullMode;
DxState1<D3DRS_SHADEMODE, D3DSHADE_GOURAUD> shadeMode;

BoolState<D3DRS_ZENABLE, false> depthTest;
Expand All @@ -465,7 +373,7 @@ class DirectXState {
DxState1<D3DRS_ZFUNC, D3DCMP_LESSEQUAL> depthFunc;
DxState1<D3DRS_ZWRITEENABLE, TRUE> depthWrite;

SavedColorMask colorMask;
DxState1<D3DRS_COLORWRITEENABLE, 0xF> colorMask;

StateVp viewport;
StateScissor scissorRect;
Expand All @@ -474,7 +382,7 @@ class DirectXState {

DxState3<D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP, D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP, D3DRS_STENCILPASS, D3DSTENCILOP_KEEP> stencilOp;
DxState3<D3DRS_STENCILFUNC, D3DCMP_ALWAYS, D3DRS_STENCILREF, 0, D3DRS_STENCILMASK, 0xFFFFFFFF> stencilFunc;
DxState1<D3DRS_STENCILWRITEMASK, 0xFFFFFFFF> stencilMask;
DxState1<D3DRS_STENCILWRITEMASK, 0xFFFFFFFF> stencilWriteMask;

DxSampler0State1<D3DSAMP_MINFILTER, D3DTEXF_POINT> texMinFilter;
DxSampler0State1<D3DSAMP_MAGFILTER, D3DTEXF_POINT> texMagFilter;
Expand Down
6 changes: 3 additions & 3 deletions GPU/Directx9/FramebufferManagerDX9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ static const D3DVERTEXELEMENT9 g_FramebufferVertexElements[] = {
dxstate.texMipLodBias.set(0.0f);
dxstate.texMaxMipLevel.set(0);
dxstate.blend.disable();
dxstate.cullMode.set(false, false);
dxstate.cullMode.set(D3DCULL_NONE);
dxstate.depthTest.disable();
dxstate.scissorTest.disable();
dxstate.stencilTest.disable();
dxstate.colorMask.set(true, true, true, true);
dxstate.stencilMask.set(0xFF);
dxstate.colorMask.set(0xF);
dxstate.stencilWriteMask.set(0xFF);
HRESULT hr = device_->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, coord, 5 * sizeof(float));
if (FAILED(hr)) {
ERROR_LOG_REPORT(G3D, "DrawActiveTexture() failed: %08x", (uint32_t)hr);
Expand Down
4 changes: 2 additions & 2 deletions GPU/Directx9/GPU_DX9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ void GPU_DX9::DeviceRestore() {
void GPU_DX9::InitClear() {
if (!framebufferManager_->UseBufferedRendering()) {
dxstate.depthWrite.set(true);
dxstate.colorMask.set(true, true, true, true);
dxstate.colorMask.set(0xF);
device_->Clear(0, NULL, D3DCLEAR_STENCIL|D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.f, 0);
}
}
Expand Down Expand Up @@ -298,7 +298,7 @@ void GPU_DX9::SetDisplayFramebuffer(u32 framebuf, u32 stride, GEBufferFormat for

void GPU_DX9::CopyDisplayToOutput(bool reallyDirty) {
dxstate.depthWrite.set(true);
dxstate.colorMask.set(true, true, true, true);
dxstate.colorMask.set(0xF);

drawEngine_.Flush();

Expand Down
37 changes: 24 additions & 13 deletions GPU/Directx9/StateMappingDX9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,15 @@ void DrawEngineDX9::ApplyDrawState(int prim) {
gstate_c.SetAllowFramebufferRead(false);
if (gstate.isModeClear()) {
dxstate.blend.disable();

// Color Mask
bool colorMask = gstate.isClearModeColorMask();
bool alphaMask = gstate.isClearModeAlphaMask();
dxstate.colorMask.set(colorMask, colorMask, colorMask, alphaMask);
u32 mask = 0;
if (gstate.isClearModeColorMask()) {
mask |= 7;
}
if (gstate.isClearModeAlphaMask()) {
mask |= 8;
}
dxstate.colorMask.set(mask);
} else {
GenericMaskState maskState;
ConvertMaskState(maskState, gstate_c.allowFramebufferRead);
Expand Down Expand Up @@ -158,20 +162,27 @@ void DrawEngineDX9::ApplyDrawState(int prim) {
dxstate.blend.disable();
}

dxstate.colorMask.set(maskState.rgba[0], maskState.rgba[1], maskState.rgba[2], maskState.rgba[3]);
u32 mask = 0;
for (int i = 0; i < 4; i++) {
if (maskState.rgba[i])
mask |= 1 << i;
}
dxstate.colorMask.set(mask);
}
}

if (gstate_c.IsDirty(DIRTY_RASTER_STATE)) {
gstate_c.Clean(DIRTY_RASTER_STATE);
// Set Dither
if (gstate.isDitherEnabled()) {
dxstate.dither.enable();
bool wantCull = !gstate.isModeClear() && prim != GE_PRIM_RECTANGLES && prim > GE_PRIM_LINE_STRIP && gstate.isCullEnabled();
if (wantCull) {
if (gstate.getCullMode() == 1) {
dxstate.cullMode.set(D3DCULL_CCW);
} else {
dxstate.cullMode.set(D3DCULL_CW);
}
} else {
dxstate.dither.disable();
dxstate.cullMode.set(D3DCULL_NONE);
}
bool wantCull = !gstate.isModeClear() && prim != GE_PRIM_RECTANGLES && prim > GE_PRIM_LINE_STRIP && gstate.isCullEnabled();
dxstate.cullMode.set(wantCull, gstate.getCullMode());
if (gstate.isModeClear()) {
// Well, probably doesn't matter...
dxstate.shadeMode.set(D3DSHADE_GOURAUD);
Expand Down Expand Up @@ -201,7 +212,7 @@ void DrawEngineDX9::ApplyDrawState(int prim) {
dxstate.stencilTest.enable();
dxstate.stencilOp.set(D3DSTENCILOP_REPLACE, D3DSTENCILOP_REPLACE, D3DSTENCILOP_REPLACE);
dxstate.stencilFunc.set(D3DCMP_ALWAYS, 255, 0xFF);
dxstate.stencilMask.set(stencilState.writeMask);
dxstate.stencilWriteMask.set(stencilState.writeMask);
} else {
dxstate.stencilTest.disable();
}
Expand All @@ -224,7 +235,7 @@ void DrawEngineDX9::ApplyDrawState(int prim) {
dxstate.stencilTest.enable();
dxstate.stencilFunc.set(ztests[stencilState.testFunc], stencilState.testRef, stencilState.testMask);
dxstate.stencilOp.set(stencilOps[stencilState.sFail], stencilOps[stencilState.zFail], stencilOps[stencilState.zPass]);
dxstate.stencilMask.set(stencilState.writeMask);
dxstate.stencilWriteMask.set(stencilState.writeMask);
} else {
dxstate.stencilTest.disable();
}
Expand Down
12 changes: 6 additions & 6 deletions GPU/Directx9/StencilBufferDX9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ bool FramebufferManagerDX9::NotifyStencilUpload(u32 addr, int size, StencilUploa

// Let's not bother with the shader if it's just zero.
dxstate.scissorTest.disable();
dxstate.colorMask.set(false, false, false, true);
dxstate.colorMask.set(0x8);
// TODO: Verify this clears only stencil/alpha.
device_->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_STENCIL, D3DCOLOR_RGBA(0, 0, 0, 0), 0.0f, 0);

Expand Down Expand Up @@ -181,7 +181,7 @@ bool FramebufferManagerDX9::NotifyStencilUpload(u32 addr, int size, StencilUploa

shaderManager_->DirtyLastShader();

dxstate.colorMask.set(false, false, false, true);
dxstate.colorMask.set(0x8);
dxstate.stencilTest.enable();
dxstate.stencilOp.set(D3DSTENCILOP_REPLACE, D3DSTENCILOP_REPLACE, D3DSTENCILOP_REPLACE);

Expand Down Expand Up @@ -228,15 +228,15 @@ bool FramebufferManagerDX9::NotifyStencilUpload(u32 addr, int size, StencilUploa
continue;
}
if (dstBuffer->format == GE_FORMAT_4444) {
dxstate.stencilMask.set(i | (i << 4));
dxstate.stencilWriteMask.set(i | (i << 4));
const float f[4] = {i * (16.0f / 255.0f)};
device_->SetPixelShaderConstantF(CONST_PS_STENCILVALUE, f, 1);
} else if (dstBuffer->format == GE_FORMAT_5551) {
dxstate.stencilMask.set(0xFF);
dxstate.stencilWriteMask.set(0xFF);
const float f[4] = {i * (128.0f / 255.0f)};
device_->SetPixelShaderConstantF(CONST_PS_STENCILVALUE, f, 1);
} else {
dxstate.stencilMask.set(i);
dxstate.stencilWriteMask.set(i);
const float f[4] = {i * (1.0f / 255.0f)};
device_->SetPixelShaderConstantF(CONST_PS_STENCILVALUE, f, 1);
}
Expand All @@ -247,7 +247,7 @@ bool FramebufferManagerDX9::NotifyStencilUpload(u32 addr, int size, StencilUploa
}

tex->Release();
dxstate.stencilMask.set(0xFF);
dxstate.stencilWriteMask.set(0xFF);
dxstate.viewport.restore();
RebindFramebuffer("RebindFramebuffer stencil");
gstate_c.Dirty(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_BLEND_STATE | DIRTY_RASTER_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS);
Expand Down