2013-08-17 09:23:51 +00:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
2020-10-04 08:04:01 +00:00
|
|
|
#include "Common/Profiler/Profiler.h"
|
2020-10-04 21:24:14 +00:00
|
|
|
#include "Common/GPU/D3D9/D3D9ShaderCompiler.h"
|
|
|
|
#include "Common/GPU/D3D9/D3D9StateCache.h"
|
|
|
|
|
2013-08-17 09:23:51 +00:00
|
|
|
#include "Core/System.h"
|
|
|
|
#include "Core/Config.h"
|
|
|
|
#include "Core/Reporting.h"
|
2017-02-05 19:36:00 +00:00
|
|
|
|
2020-10-04 21:24:14 +00:00
|
|
|
#include "GPU/Math3D.h"
|
|
|
|
#include "GPU/GPUState.h"
|
|
|
|
#include "GPU/ge_constants.h"
|
2017-02-05 19:36:00 +00:00
|
|
|
|
2013-09-15 10:46:14 +00:00
|
|
|
#include "GPU/Directx9/GPU_DX9.h"
|
|
|
|
#include "GPU/Directx9/ShaderManagerDX9.h"
|
|
|
|
#include "GPU/Directx9/TextureCacheDX9.h"
|
2020-08-03 21:24:50 +00:00
|
|
|
#include "GPU/Directx9/FramebufferManagerDX9.h"
|
2013-08-17 09:23:51 +00:00
|
|
|
|
2013-09-15 15:53:21 +00:00
|
|
|
namespace DX9 {
|
|
|
|
|
2015-11-08 21:42:39 +00:00
|
|
|
static const D3DBLEND dxBlendFactorLookup[(size_t)BlendFactor::COUNT] = {
|
|
|
|
D3DBLEND_ZERO,
|
|
|
|
D3DBLEND_ONE,
|
2013-08-17 09:23:51 +00:00
|
|
|
D3DBLEND_SRCCOLOR,
|
|
|
|
D3DBLEND_INVSRCCOLOR,
|
2015-11-08 21:42:39 +00:00
|
|
|
D3DBLEND_DESTCOLOR,
|
|
|
|
D3DBLEND_INVDESTCOLOR,
|
2013-08-17 09:23:51 +00:00
|
|
|
D3DBLEND_SRCALPHA,
|
|
|
|
D3DBLEND_INVSRCALPHA,
|
|
|
|
D3DBLEND_DESTALPHA,
|
|
|
|
D3DBLEND_INVDESTALPHA,
|
2015-11-08 21:42:39 +00:00
|
|
|
D3DBLEND_BLENDFACTOR,
|
|
|
|
D3DBLEND_INVBLENDFACTOR,
|
|
|
|
D3DBLEND_BLENDFACTOR,
|
|
|
|
D3DBLEND_INVBLENDFACTOR,
|
|
|
|
#if 0 // TODO: Requires D3D9Ex
|
|
|
|
D3DBLEND_SRCCOLOR2,
|
|
|
|
D3DBLEND_INVSRCCOLOR2,
|
2016-12-26 22:51:17 +00:00
|
|
|
D3DBLEND_SRCCOLOR2,
|
|
|
|
D3DBLEND_INVSRCCOLOR2,
|
2015-11-08 21:42:39 +00:00
|
|
|
#else
|
|
|
|
D3DBLEND_FORCE_DWORD,
|
|
|
|
D3DBLEND_FORCE_DWORD,
|
|
|
|
#endif
|
|
|
|
D3DBLEND_FORCE_DWORD,
|
2013-08-17 09:23:51 +00:00
|
|
|
};
|
|
|
|
|
2015-11-08 21:42:39 +00:00
|
|
|
static const D3DBLENDOP dxBlendEqLookup[(size_t)BlendEq::COUNT] = {
|
2013-08-17 09:23:51 +00:00
|
|
|
D3DBLENDOP_ADD,
|
|
|
|
D3DBLENDOP_SUBTRACT,
|
|
|
|
D3DBLENDOP_REVSUBTRACT,
|
|
|
|
D3DBLENDOP_MIN,
|
|
|
|
D3DBLENDOP_MAX,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const D3DCULL cullingMode[] = {
|
|
|
|
D3DCULL_CW,
|
|
|
|
D3DCULL_CCW,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const D3DCMPFUNC ztests[] = {
|
|
|
|
D3DCMP_NEVER, D3DCMP_ALWAYS, D3DCMP_EQUAL, D3DCMP_NOTEQUAL,
|
|
|
|
D3DCMP_LESS, D3DCMP_LESSEQUAL, D3DCMP_GREATER, D3DCMP_GREATEREQUAL,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const D3DSTENCILOP stencilOps[] = {
|
|
|
|
D3DSTENCILOP_KEEP,
|
|
|
|
D3DSTENCILOP_ZERO,
|
|
|
|
D3DSTENCILOP_REPLACE,
|
|
|
|
D3DSTENCILOP_INVERT,
|
2014-08-26 07:23:34 +00:00
|
|
|
D3DSTENCILOP_INCRSAT,
|
|
|
|
D3DSTENCILOP_DECRSAT,
|
2013-08-17 09:23:51 +00:00
|
|
|
D3DSTENCILOP_KEEP, // reserved
|
|
|
|
D3DSTENCILOP_KEEP, // reserved
|
|
|
|
};
|
|
|
|
|
2020-11-08 12:14:23 +00:00
|
|
|
inline void DrawEngineDX9::ResetFramebufferRead() {
|
2022-08-05 19:11:33 +00:00
|
|
|
fboTexBound_ = false;
|
2014-09-21 19:01:49 +00:00
|
|
|
}
|
|
|
|
|
2016-04-10 08:21:48 +00:00
|
|
|
void DrawEngineDX9::ApplyDrawState(int prim) {
|
2022-08-05 19:11:33 +00:00
|
|
|
if (!gstate_c.IsDirty(DIRTY_BLEND_STATE | DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS | DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_RASTER_STATE | DIRTY_DEPTHSTENCIL_STATE)) {
|
|
|
|
// nothing to do
|
|
|
|
return;
|
|
|
|
}
|
2014-09-21 19:01:49 +00:00
|
|
|
|
2017-01-24 08:41:38 +00:00
|
|
|
if (gstate_c.IsDirty(DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS) && !gstate.isModeClear() && gstate.isTextureMapEnabled()) {
|
2015-11-08 21:42:39 +00:00
|
|
|
textureCache_->SetTexture();
|
2017-01-24 08:41:38 +00:00
|
|
|
gstate_c.Clean(DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS);
|
2018-04-22 18:16:33 +00:00
|
|
|
} else if (gstate.getTextureAddress(0) == ((gstate.getFrameBufRawAddress() | 0x04000000) & 0x3FFFFFFF)) {
|
|
|
|
// This catches the case of clearing a texture.
|
|
|
|
gstate_c.Dirty(DIRTY_TEXTURE_IMAGE);
|
2014-09-21 19:01:49 +00:00
|
|
|
}
|
2014-09-21 19:17:16 +00:00
|
|
|
|
2015-11-12 17:09:28 +00:00
|
|
|
// Start profiling here to skip SetTexture which is already accounted for
|
|
|
|
PROFILE_THIS_SCOPE("applydrawstate");
|
|
|
|
|
2020-04-04 17:51:47 +00:00
|
|
|
bool useBufferedRendering = framebufferManager_->UseBufferedRendering();
|
2015-11-12 17:09:28 +00:00
|
|
|
|
2017-01-30 10:56:14 +00:00
|
|
|
if (gstate_c.IsDirty(DIRTY_BLEND_STATE)) {
|
2020-11-08 11:49:06 +00:00
|
|
|
// Unfortunately, this isn't implemented on DX9 yet.
|
|
|
|
gstate_c.SetAllowFramebufferRead(false);
|
2017-01-30 10:56:14 +00:00
|
|
|
if (gstate.isModeClear()) {
|
|
|
|
dxstate.blend.disable();
|
|
|
|
// Color Mask
|
2022-07-24 19:26:40 +00:00
|
|
|
u32 mask = 0;
|
|
|
|
if (gstate.isClearModeColorMask()) {
|
|
|
|
mask |= 7;
|
|
|
|
}
|
|
|
|
if (gstate.isClearModeAlphaMask()) {
|
|
|
|
mask |= 8;
|
|
|
|
}
|
|
|
|
dxstate.colorMask.set(mask);
|
2017-01-30 10:56:14 +00:00
|
|
|
} else {
|
2020-11-08 22:17:06 +00:00
|
|
|
GenericMaskState maskState;
|
|
|
|
ConvertMaskState(maskState, gstate_c.allowFramebufferRead);
|
|
|
|
|
2022-04-24 22:06:21 +00:00
|
|
|
// Set blend - unless we need to do it in the shader.
|
|
|
|
GenericBlendState blendState;
|
|
|
|
ConvertBlendState(blendState, gstate_c.allowFramebufferRead, maskState.applyFramebufferRead);
|
|
|
|
|
2020-11-08 22:17:06 +00:00
|
|
|
if (blendState.applyFramebufferRead || maskState.applyFramebufferRead) {
|
2022-05-01 10:49:19 +00:00
|
|
|
ApplyFramebufferRead(&fboTexNeedsBind_);
|
|
|
|
// The shader takes over the responsibility for blending, so recompute.
|
|
|
|
ApplyStencilReplaceAndLogicOpIgnoreBlend(blendState.replaceAlphaWithStencil, blendState);
|
2022-08-05 19:11:33 +00:00
|
|
|
|
|
|
|
if (fboTexNeedsBind_) {
|
|
|
|
// Note that this is positions, not UVs, that we need the copy from.
|
|
|
|
framebufferManager_->BindFramebufferAsColorTexture(1, framebufferManager_->GetCurrentRenderVFB(), BINDFBCOLOR_MAY_COPY);
|
|
|
|
// If we are rendering at a higher resolution, linear is probably best for the dest color.
|
|
|
|
device_->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
|
|
|
|
device_->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
|
|
|
|
fboTexBound_ = true;
|
|
|
|
fboTexNeedsBind_ = false;
|
|
|
|
gstate_c.Dirty(DIRTY_BLEND_STATE);
|
|
|
|
}
|
|
|
|
|
2020-11-08 12:14:23 +00:00
|
|
|
gstate_c.Dirty(DIRTY_FRAGMENTSHADER_STATE);
|
2020-11-08 11:49:06 +00:00
|
|
|
} else if (blendState.resetFramebufferRead) {
|
2020-11-08 12:14:23 +00:00
|
|
|
ResetFramebufferRead();
|
2017-01-30 10:56:14 +00:00
|
|
|
}
|
2015-11-14 07:11:06 +00:00
|
|
|
|
2017-01-30 10:56:14 +00:00
|
|
|
if (blendState.enabled) {
|
|
|
|
dxstate.blend.enable();
|
|
|
|
dxstate.blendSeparate.enable();
|
|
|
|
dxstate.blendEquation.set(dxBlendEqLookup[(size_t)blendState.eqColor], dxBlendEqLookup[(size_t)blendState.eqAlpha]);
|
|
|
|
dxstate.blendFunc.set(
|
|
|
|
dxBlendFactorLookup[(size_t)blendState.srcColor], dxBlendFactorLookup[(size_t)blendState.dstColor],
|
|
|
|
dxBlendFactorLookup[(size_t)blendState.srcAlpha], dxBlendFactorLookup[(size_t)blendState.dstAlpha]);
|
2020-11-08 11:49:06 +00:00
|
|
|
if (blendState.dirtyShaderBlendFixValues) {
|
2017-01-30 10:56:14 +00:00
|
|
|
gstate_c.Dirty(DIRTY_SHADERBLEND);
|
|
|
|
}
|
|
|
|
if (blendState.useBlendColor) {
|
|
|
|
dxstate.blendColor.setDWORD(blendState.blendColor);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dxstate.blend.disable();
|
|
|
|
}
|
2015-11-12 17:09:28 +00:00
|
|
|
|
2022-07-24 19:26:40 +00:00
|
|
|
u32 mask = 0;
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if (maskState.rgba[i])
|
|
|
|
mask |= 1 << i;
|
|
|
|
}
|
|
|
|
dxstate.colorMask.set(mask);
|
2014-09-21 19:01:49 +00:00
|
|
|
}
|
2013-08-17 09:23:51 +00:00
|
|
|
}
|
|
|
|
|
2017-01-30 11:07:26 +00:00
|
|
|
if (gstate_c.IsDirty(DIRTY_RASTER_STATE)) {
|
2022-07-24 19:26:40 +00:00
|
|
|
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);
|
|
|
|
}
|
2017-04-03 14:19:10 +00:00
|
|
|
} else {
|
2022-07-24 19:26:40 +00:00
|
|
|
dxstate.cullMode.set(D3DCULL_NONE);
|
2017-04-03 14:19:10 +00:00
|
|
|
}
|
2017-01-30 11:02:14 +00:00
|
|
|
if (gstate.isModeClear()) {
|
2018-04-28 23:31:39 +00:00
|
|
|
// Well, probably doesn't matter...
|
|
|
|
dxstate.shadeMode.set(D3DSHADE_GOURAUD);
|
2017-04-03 14:19:10 +00:00
|
|
|
} else {
|
2018-04-28 23:31:39 +00:00
|
|
|
dxstate.shadeMode.set(gstate.getShadeMode() == GE_SHADE_GOURAUD ? D3DSHADE_GOURAUD : D3DSHADE_FLAT);
|
2017-04-03 14:19:10 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-17 09:23:51 +00:00
|
|
|
|
2017-01-30 11:02:14 +00:00
|
|
|
if (gstate_c.IsDirty(DIRTY_DEPTHSTENCIL_STATE)) {
|
2021-01-24 23:54:13 +00:00
|
|
|
GenericStencilFuncState stencilState;
|
|
|
|
ConvertStencilFuncState(stencilState);
|
|
|
|
|
2017-04-03 14:19:10 +00:00
|
|
|
// Set Stencil/Depth
|
2022-08-01 10:11:42 +00:00
|
|
|
|
|
|
|
if (gstate_c.renderMode == FB_MODE_COLOR_TO_DEPTH) {
|
|
|
|
// Enforce plain depth writing.
|
|
|
|
dxstate.depthTest.enable();
|
|
|
|
dxstate.depthFunc.set(D3DCMP_ALWAYS);
|
|
|
|
dxstate.depthWrite.set(true);
|
|
|
|
dxstate.stencilTest.disable();
|
|
|
|
} else if (gstate.isModeClear()) {
|
2017-01-30 11:02:14 +00:00
|
|
|
// Depth Test
|
2013-08-17 09:23:51 +00:00
|
|
|
dxstate.depthTest.enable();
|
2017-01-30 11:02:14 +00:00
|
|
|
dxstate.depthFunc.set(D3DCMP_ALWAYS);
|
|
|
|
dxstate.depthWrite.set(gstate.isClearModeDepthMask());
|
2017-05-26 08:23:45 +00:00
|
|
|
if (gstate.isClearModeDepthMask()) {
|
2014-09-21 19:47:39 +00:00
|
|
|
framebufferManager_->SetDepthUpdated();
|
|
|
|
}
|
2013-08-17 09:23:51 +00:00
|
|
|
|
2017-01-30 11:02:14 +00:00
|
|
|
// Stencil Test
|
|
|
|
bool alphaMask = gstate.isClearModeAlphaMask();
|
2018-12-14 12:54:03 +00:00
|
|
|
if (alphaMask) {
|
2017-01-30 11:02:14 +00:00
|
|
|
dxstate.stencilTest.enable();
|
|
|
|
dxstate.stencilOp.set(D3DSTENCILOP_REPLACE, D3DSTENCILOP_REPLACE, D3DSTENCILOP_REPLACE);
|
2022-07-24 19:40:04 +00:00
|
|
|
dxstate.stencilFunc.set(D3DCMP_ALWAYS);
|
|
|
|
dxstate.stencilRef.set(0xFF);
|
|
|
|
dxstate.stencilCompareMask.set(0xFF);
|
2022-07-24 19:26:40 +00:00
|
|
|
dxstate.stencilWriteMask.set(stencilState.writeMask);
|
2017-01-30 11:02:14 +00:00
|
|
|
} else {
|
|
|
|
dxstate.stencilTest.disable();
|
|
|
|
}
|
2015-12-31 09:50:18 +00:00
|
|
|
|
2013-08-17 09:23:51 +00:00
|
|
|
} else {
|
2017-01-30 11:02:14 +00:00
|
|
|
// Depth Test
|
|
|
|
if (gstate.isDepthTestEnabled()) {
|
|
|
|
dxstate.depthTest.enable();
|
|
|
|
dxstate.depthFunc.set(ztests[gstate.getDepthTestFunction()]);
|
|
|
|
dxstate.depthWrite.set(gstate.isDepthWriteEnabled());
|
|
|
|
if (gstate.isDepthWriteEnabled()) {
|
|
|
|
framebufferManager_->SetDepthUpdated();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dxstate.depthTest.disable();
|
|
|
|
}
|
2013-08-17 09:23:51 +00:00
|
|
|
|
2017-01-30 11:02:14 +00:00
|
|
|
// Stencil Test
|
|
|
|
if (stencilState.enabled) {
|
|
|
|
dxstate.stencilTest.enable();
|
2022-07-24 19:40:04 +00:00
|
|
|
dxstate.stencilFunc.set(ztests[stencilState.testFunc]);
|
|
|
|
dxstate.stencilRef.set(stencilState.testRef);
|
|
|
|
dxstate.stencilCompareMask.set(stencilState.testMask);
|
2017-01-30 11:02:14 +00:00
|
|
|
dxstate.stencilOp.set(stencilOps[stencilState.sFail], stencilOps[stencilState.zFail], stencilOps[stencilState.zPass]);
|
2022-07-24 19:26:40 +00:00
|
|
|
dxstate.stencilWriteMask.set(stencilState.writeMask);
|
2017-01-30 11:02:14 +00:00
|
|
|
} else {
|
|
|
|
dxstate.stencilTest.disable();
|
|
|
|
}
|
|
|
|
}
|
2015-11-08 17:13:34 +00:00
|
|
|
}
|
2014-12-19 06:16:26 +00:00
|
|
|
|
2017-01-30 11:22:35 +00:00
|
|
|
if (gstate_c.IsDirty(DIRTY_VIEWPORTSCISSOR_STATE)) {
|
2017-01-30 11:02:14 +00:00
|
|
|
ViewportAndScissor vpAndScissor;
|
|
|
|
ConvertViewportAndScissor(useBufferedRendering,
|
|
|
|
framebufferManager_->GetRenderWidth(), framebufferManager_->GetRenderHeight(),
|
|
|
|
framebufferManager_->GetTargetBufferWidth(), framebufferManager_->GetTargetBufferHeight(),
|
|
|
|
vpAndScissor);
|
|
|
|
|
2022-07-24 19:04:54 +00:00
|
|
|
dxstate.scissorTest.enable();
|
|
|
|
dxstate.scissorRect.set(vpAndScissor.scissorX, vpAndScissor.scissorY, vpAndScissor.scissorX + vpAndScissor.scissorW, vpAndScissor.scissorY + vpAndScissor.scissorH);
|
2014-12-19 06:16:26 +00:00
|
|
|
|
2017-01-30 11:02:14 +00:00
|
|
|
float depthMin = vpAndScissor.depthRangeMin;
|
|
|
|
float depthMax = vpAndScissor.depthRangeMax;
|
|
|
|
|
|
|
|
dxstate.viewport.set(vpAndScissor.viewportX, vpAndScissor.viewportY, vpAndScissor.viewportW, vpAndScissor.viewportH, depthMin, depthMax);
|
|
|
|
if (vpAndScissor.dirtyProj) {
|
|
|
|
gstate_c.Dirty(DIRTY_PROJMATRIX);
|
|
|
|
}
|
|
|
|
if (vpAndScissor.dirtyDepth) {
|
|
|
|
gstate_c.Dirty(DIRTY_DEPTHRANGE);
|
|
|
|
}
|
2016-01-03 20:11:05 +00:00
|
|
|
}
|
2022-08-05 19:11:33 +00:00
|
|
|
|
|
|
|
gstate_c.Clean(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_RASTER_STATE | DIRTY_BLEND_STATE);
|
2013-08-17 09:23:51 +00:00
|
|
|
}
|
2013-09-15 15:53:21 +00:00
|
|
|
|
2016-04-10 08:21:48 +00:00
|
|
|
void DrawEngineDX9::ApplyDrawStateLate() {
|
2015-03-18 05:07:09 +00:00
|
|
|
// At this point, we know if the vertices are full alpha or not.
|
|
|
|
// TODO: Set the nearest/linear here (since we correctly know if alpha/color tests are needed)?
|
2016-09-11 18:03:03 +00:00
|
|
|
|
2015-03-18 05:07:09 +00:00
|
|
|
}
|
|
|
|
|
2015-08-26 11:39:52 +00:00
|
|
|
}
|