CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Directx9/StateMappingDX9.cpp
Views: 1401
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include "Common/Profiler/Profiler.h"
19
#include "Common/GPU/D3D9/D3D9ShaderCompiler.h"
20
#include "Common/GPU/D3D9/D3D9StateCache.h"
21
22
#include "Core/System.h"
23
#include "Core/Config.h"
24
25
#include "GPU/Math3D.h"
26
#include "GPU/GPUState.h"
27
#include "GPU/ge_constants.h"
28
29
#include "GPU/Directx9/GPU_DX9.h"
30
#include "GPU/Directx9/ShaderManagerDX9.h"
31
#include "GPU/Directx9/TextureCacheDX9.h"
32
#include "GPU/Directx9/FramebufferManagerDX9.h"
33
34
static const D3DBLEND dxBlendFactorLookup[(size_t)BlendFactor::COUNT] = {
35
D3DBLEND_ZERO,
36
D3DBLEND_ONE,
37
D3DBLEND_SRCCOLOR,
38
D3DBLEND_INVSRCCOLOR,
39
D3DBLEND_DESTCOLOR,
40
D3DBLEND_INVDESTCOLOR,
41
D3DBLEND_SRCALPHA,
42
D3DBLEND_INVSRCALPHA,
43
D3DBLEND_DESTALPHA,
44
D3DBLEND_INVDESTALPHA,
45
D3DBLEND_BLENDFACTOR,
46
D3DBLEND_INVBLENDFACTOR,
47
D3DBLEND_BLENDFACTOR,
48
D3DBLEND_INVBLENDFACTOR,
49
#if 0 // TODO: Requires D3D9Ex
50
D3DBLEND_SRCCOLOR2,
51
D3DBLEND_INVSRCCOLOR2,
52
D3DBLEND_SRCCOLOR2,
53
D3DBLEND_INVSRCCOLOR2,
54
#else
55
D3DBLEND_FORCE_DWORD,
56
D3DBLEND_FORCE_DWORD,
57
#endif
58
D3DBLEND_FORCE_DWORD,
59
};
60
61
static const D3DBLENDOP dxBlendEqLookup[(size_t)BlendEq::COUNT] = {
62
D3DBLENDOP_ADD,
63
D3DBLENDOP_SUBTRACT,
64
D3DBLENDOP_REVSUBTRACT,
65
D3DBLENDOP_MIN,
66
D3DBLENDOP_MAX,
67
};
68
69
static const D3DCULL cullingMode[] = {
70
D3DCULL_CW,
71
D3DCULL_CCW,
72
};
73
74
static const D3DCMPFUNC ztests[] = {
75
D3DCMP_NEVER, D3DCMP_ALWAYS, D3DCMP_EQUAL, D3DCMP_NOTEQUAL,
76
D3DCMP_LESS, D3DCMP_LESSEQUAL, D3DCMP_GREATER, D3DCMP_GREATEREQUAL,
77
};
78
79
static const D3DSTENCILOP stencilOps[] = {
80
D3DSTENCILOP_KEEP,
81
D3DSTENCILOP_ZERO,
82
D3DSTENCILOP_REPLACE,
83
D3DSTENCILOP_INVERT,
84
D3DSTENCILOP_INCRSAT,
85
D3DSTENCILOP_DECRSAT,
86
D3DSTENCILOP_KEEP, // reserved
87
D3DSTENCILOP_KEEP, // reserved
88
};
89
90
void DrawEngineDX9::ApplyDrawState(int prim) {
91
if (!gstate_c.IsDirty(DIRTY_BLEND_STATE | DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS | DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_RASTER_STATE | DIRTY_DEPTHSTENCIL_STATE)) {
92
// nothing to do
93
return;
94
}
95
96
// At this point, we know if the vertices are full alpha or not.
97
// TODO: Set the nearest/linear here (since we correctly know if alpha/color tests are needed)?
98
if (!gstate.isModeClear()) {
99
textureCache_->ApplyTexture();
100
101
if (fboTexBindState_ == FBO_TEX_COPY_BIND_TEX) {
102
// Note that this is positions, not UVs, that we need the copy from.
103
framebufferManager_->BindFramebufferAsColorTexture(1, framebufferManager_->GetCurrentRenderVFB(), BINDFBCOLOR_MAY_COPY, 0);
104
// If we are rendering at a higher resolution, linear is probably best for the dest color.
105
device_->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
106
device_->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
107
fboTexBound_ = true;
108
fboTexBindState_ = FBO_TEX_NONE;
109
}
110
111
// TODO: Test texture?
112
}
113
114
bool useBufferedRendering = framebufferManager_->UseBufferedRendering();
115
116
if (gstate_c.IsDirty(DIRTY_BLEND_STATE)) {
117
if (gstate.isModeClear()) {
118
dxstate.blend.disable();
119
// Color Mask
120
u32 mask = 0;
121
if (gstate.isClearModeColorMask()) {
122
mask |= 7;
123
}
124
if (gstate.isClearModeAlphaMask()) {
125
mask |= 8;
126
}
127
dxstate.colorMask.set(mask);
128
} else {
129
pipelineState_.Convert(draw_->GetShaderLanguageDesc().bitwiseOps);
130
GenericMaskState &maskState = pipelineState_.maskState;
131
GenericBlendState &blendState = pipelineState_.blendState;
132
// We ignore the logicState on D3D since there's no support, the emulation of it is blend-and-shader only.
133
134
if (pipelineState_.FramebufferRead()) {
135
ApplyFramebufferRead(&fboTexBindState_);
136
// The shader takes over the responsibility for blending, so recompute.
137
ApplyStencilReplaceAndLogicOpIgnoreBlend(blendState.replaceAlphaWithStencil, blendState);
138
139
if (fboTexBindState_ == FBO_TEX_COPY_BIND_TEX) {
140
// Note that this is positions, not UVs, that we need the copy from.
141
framebufferManager_->BindFramebufferAsColorTexture(1, framebufferManager_->GetCurrentRenderVFB(), BINDFBCOLOR_MAY_COPY | BINDFBCOLOR_UNCACHED, Draw::ALL_LAYERS);
142
// If we are rendering at a higher resolution, linear is probably best for the dest color.
143
device_->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
144
device_->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
145
fboTexBound_ = true;
146
fboTexBindState_ = FBO_TEX_NONE;
147
dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE;
148
gstate_c.Dirty(DIRTY_BLEND_STATE);
149
} else if (fboTexBindState_ == FBO_TEX_READ_FRAMEBUFFER) {
150
// Not supported.
151
fboTexBindState_ = FBO_TEX_NONE;
152
}
153
154
dirtyRequiresRecheck_ |= DIRTY_FRAGMENTSHADER_STATE;
155
gstate_c.Dirty(DIRTY_FRAGMENTSHADER_STATE);
156
} else {
157
if (fboTexBound_) {
158
dirtyRequiresRecheck_ |= DIRTY_FRAGMENTSHADER_STATE;
159
gstate_c.Dirty(DIRTY_FRAGMENTSHADER_STATE);
160
fboTexBound_ = false;
161
}
162
}
163
164
if (blendState.blendEnabled) {
165
dxstate.blend.enable();
166
dxstate.blendSeparate.enable();
167
dxstate.blendEquation.set(dxBlendEqLookup[(size_t)blendState.eqColor], dxBlendEqLookup[(size_t)blendState.eqAlpha]);
168
dxstate.blendFunc.set(
169
dxBlendFactorLookup[(size_t)blendState.srcColor], dxBlendFactorLookup[(size_t)blendState.dstColor],
170
dxBlendFactorLookup[(size_t)blendState.srcAlpha], dxBlendFactorLookup[(size_t)blendState.dstAlpha]);
171
if (blendState.dirtyShaderBlendFixValues) {
172
dirtyRequiresRecheck_ |= DIRTY_SHADERBLEND;
173
gstate_c.Dirty(DIRTY_SHADERBLEND);
174
}
175
if (blendState.useBlendColor) {
176
dxstate.blendColor.setDWORD(blendState.blendColor);
177
}
178
} else {
179
dxstate.blend.disable();
180
}
181
182
dxstate.colorMask.set(maskState.channelMask);
183
}
184
}
185
186
if (gstate_c.IsDirty(DIRTY_RASTER_STATE)) {
187
bool wantCull = !gstate.isModeClear() && prim != GE_PRIM_RECTANGLES && prim > GE_PRIM_LINE_STRIP && gstate.isCullEnabled();
188
if (wantCull) {
189
if (gstate.getCullMode() == 1) {
190
dxstate.cullMode.set(D3DCULL_CCW);
191
} else {
192
dxstate.cullMode.set(D3DCULL_CW);
193
}
194
} else {
195
dxstate.cullMode.set(D3DCULL_NONE);
196
}
197
if (gstate.isModeClear()) {
198
// Well, probably doesn't matter...
199
dxstate.shadeMode.set(D3DSHADE_GOURAUD);
200
} else {
201
dxstate.shadeMode.set(gstate.getShadeMode() == GE_SHADE_GOURAUD ? D3DSHADE_GOURAUD : D3DSHADE_FLAT);
202
}
203
204
// We use fixed-function user clipping on D3D9, where available, for negative Z clipping.
205
if (draw_->GetDeviceCaps().clipPlanesSupported >= 1) {
206
bool wantClip = !gstate.isModeThrough() && gstate_c.submitType == SubmitType::DRAW;
207
dxstate.clipPlaneEnable.set(wantClip ? 1 : 0);
208
}
209
}
210
211
if (gstate_c.IsDirty(DIRTY_DEPTHSTENCIL_STATE)) {
212
GenericStencilFuncState stencilState;
213
ConvertStencilFuncState(stencilState);
214
215
// Set Stencil/Depth
216
if (gstate.isModeClear()) {
217
// Depth Test
218
dxstate.depthTest.enable();
219
dxstate.depthFunc.set(D3DCMP_ALWAYS);
220
dxstate.depthWrite.set(gstate.isClearModeDepthMask());
221
222
// Stencil Test
223
bool alphaMask = gstate.isClearModeAlphaMask();
224
if (alphaMask) {
225
dxstate.stencilTest.enable();
226
dxstate.stencilOp.set(D3DSTENCILOP_REPLACE, D3DSTENCILOP_REPLACE, D3DSTENCILOP_REPLACE);
227
dxstate.stencilFunc.set(D3DCMP_ALWAYS);
228
dxstate.stencilRef.set(0xFF);
229
dxstate.stencilCompareMask.set(0xFF);
230
dxstate.stencilWriteMask.set(stencilState.writeMask);
231
} else {
232
dxstate.stencilTest.disable();
233
}
234
} else {
235
// Depth Test
236
if (!IsDepthTestEffectivelyDisabled()) {
237
dxstate.depthTest.enable();
238
dxstate.depthFunc.set(ztests[gstate.getDepthTestFunction()]);
239
dxstate.depthWrite.set(gstate.isDepthWriteEnabled());
240
UpdateEverUsedEqualDepth(gstate.getDepthTestFunction());
241
} else {
242
dxstate.depthTest.disable();
243
}
244
245
// Stencil Test
246
if (stencilState.enabled) {
247
dxstate.stencilTest.enable();
248
dxstate.stencilFunc.set(ztests[stencilState.testFunc]);
249
dxstate.stencilRef.set(stencilState.testRef);
250
dxstate.stencilCompareMask.set(stencilState.testMask);
251
dxstate.stencilOp.set(stencilOps[stencilState.sFail], stencilOps[stencilState.zFail], stencilOps[stencilState.zPass]);
252
dxstate.stencilWriteMask.set(stencilState.writeMask);
253
254
// Nasty special case for Spongebob and similar where it tries to write zeros to alpha/stencil during
255
// depth-fail. We can't write to alpha then because the pixel is killed. However, we can invert the depth
256
// test and modify the alpha function...
257
if (SpongebobDepthInverseConditions(stencilState)) {
258
dxstate.blend.set(true);
259
dxstate.blendEquation.set(D3DBLENDOP_ADD, D3DBLENDOP_ADD);
260
dxstate.blendFunc.set(D3DBLEND_ZERO, D3DBLEND_ZERO, D3DBLEND_ZERO, D3DBLEND_ZERO);
261
dxstate.colorMask.set(8);
262
263
dxstate.depthFunc.set(D3DCMP_LESS);
264
dxstate.stencilFunc.set(D3DCMP_ALWAYS);
265
// Invert
266
dxstate.stencilOp.set(D3DSTENCILOP_ZERO, D3DSTENCILOP_KEEP, D3DSTENCILOP_ZERO);
267
268
dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE;
269
gstate_c.Dirty(DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE);
270
}
271
} else {
272
dxstate.stencilTest.disable();
273
}
274
}
275
}
276
277
if (gstate_c.IsDirty(DIRTY_VIEWPORTSCISSOR_STATE)) {
278
ViewportAndScissor vpAndScissor;
279
ConvertViewportAndScissor(useBufferedRendering,
280
framebufferManager_->GetRenderWidth(), framebufferManager_->GetRenderHeight(),
281
framebufferManager_->GetTargetBufferWidth(), framebufferManager_->GetTargetBufferHeight(),
282
vpAndScissor);
283
UpdateCachedViewportState(vpAndScissor);
284
285
dxstate.scissorTest.enable();
286
dxstate.scissorRect.set(vpAndScissor.scissorX, vpAndScissor.scissorY, vpAndScissor.scissorX + vpAndScissor.scissorW, vpAndScissor.scissorY + vpAndScissor.scissorH);
287
288
float depthMin = vpAndScissor.depthRangeMin;
289
float depthMax = vpAndScissor.depthRangeMax;
290
291
dxstate.viewport.set(vpAndScissor.viewportX, vpAndScissor.viewportY, vpAndScissor.viewportW, vpAndScissor.viewportH, depthMin, depthMax);
292
}
293
294
gstate_c.Clean(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_RASTER_STATE | DIRTY_BLEND_STATE);
295
gstate_c.Dirty(dirtyRequiresRecheck_);
296
dirtyRequiresRecheck_ = 0;
297
}
298
299
void DrawEngineDX9::ApplyDrawStateLate() {
300
// At this point, we know if the vertices are full alpha or not.
301
// TODO: Set the nearest/linear here (since we correctly know if alpha/color tests are needed)?
302
}
303
304