Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/Vulkan/StateMappingVulkan.cpp
3186 views
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 <algorithm>
19
20
#include "Common/GPU/Vulkan/VulkanLoader.h"
21
#include "Common/GPU/Vulkan/VulkanRenderManager.h"
22
23
#include "GPU/GPUState.h"
24
#include "GPU/ge_constants.h"
25
#include "GPU/Common/GPUStateUtils.h"
26
#include "Core/Config.h"
27
#include "GPU/Vulkan/FramebufferManagerVulkan.h"
28
#include "GPU/Vulkan/ShaderManagerVulkan.h"
29
#include "GPU/Vulkan/DrawEngineVulkan.h"
30
31
// These tables all fit into u8s.
32
static const VkBlendFactor vkBlendFactorLookup[(size_t)BlendFactor::COUNT] = {
33
VK_BLEND_FACTOR_ZERO,
34
VK_BLEND_FACTOR_ONE,
35
VK_BLEND_FACTOR_SRC_COLOR,
36
VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR,
37
VK_BLEND_FACTOR_DST_COLOR,
38
VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR,
39
VK_BLEND_FACTOR_SRC_ALPHA,
40
VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA,
41
VK_BLEND_FACTOR_DST_ALPHA,
42
VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA,
43
VK_BLEND_FACTOR_CONSTANT_COLOR,
44
VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR,
45
VK_BLEND_FACTOR_CONSTANT_ALPHA,
46
VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA,
47
VK_BLEND_FACTOR_SRC1_COLOR,
48
VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR,
49
VK_BLEND_FACTOR_SRC1_ALPHA,
50
VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA,
51
VK_BLEND_FACTOR_MAX_ENUM,
52
};
53
54
static const VkBlendOp vkBlendEqLookup[(size_t)BlendEq::COUNT] = {
55
VK_BLEND_OP_ADD,
56
VK_BLEND_OP_SUBTRACT,
57
VK_BLEND_OP_REVERSE_SUBTRACT,
58
VK_BLEND_OP_MIN,
59
VK_BLEND_OP_MAX,
60
};
61
62
static const VkCullModeFlagBits cullingMode[] = {
63
VK_CULL_MODE_BACK_BIT,
64
VK_CULL_MODE_FRONT_BIT,
65
};
66
67
static const VkCompareOp compareOps[] = {
68
VK_COMPARE_OP_NEVER,
69
VK_COMPARE_OP_ALWAYS,
70
VK_COMPARE_OP_EQUAL,
71
VK_COMPARE_OP_NOT_EQUAL,
72
VK_COMPARE_OP_LESS,
73
VK_COMPARE_OP_LESS_OR_EQUAL,
74
VK_COMPARE_OP_GREATER,
75
VK_COMPARE_OP_GREATER_OR_EQUAL,
76
};
77
78
static const VkStencilOp stencilOps[] = {
79
VK_STENCIL_OP_KEEP,
80
VK_STENCIL_OP_ZERO,
81
VK_STENCIL_OP_REPLACE,
82
VK_STENCIL_OP_INVERT,
83
VK_STENCIL_OP_INCREMENT_AND_CLAMP,
84
VK_STENCIL_OP_DECREMENT_AND_CLAMP,
85
VK_STENCIL_OP_KEEP, // reserved
86
VK_STENCIL_OP_KEEP, // reserved
87
};
88
89
static const VkPrimitiveTopology primToVulkan[8] = {
90
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, // We convert points to triangles.
91
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, // We convert lines to triangles.
92
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, // We convert line strips to triangles.
93
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
94
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
95
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN,
96
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, // Vulkan doesn't do quads. We could do strips with restart-index though. We could also do RECT primitives in the geometry shader.
97
};
98
99
// These are actually the same exact values/order/etc. as the GE ones, but for clarity...
100
static const VkLogicOp logicOps[] = {
101
VK_LOGIC_OP_CLEAR,
102
VK_LOGIC_OP_AND,
103
VK_LOGIC_OP_AND_REVERSE,
104
VK_LOGIC_OP_COPY,
105
VK_LOGIC_OP_AND_INVERTED,
106
VK_LOGIC_OP_NO_OP,
107
VK_LOGIC_OP_XOR,
108
VK_LOGIC_OP_OR,
109
VK_LOGIC_OP_NOR,
110
VK_LOGIC_OP_EQUIVALENT,
111
VK_LOGIC_OP_INVERT,
112
VK_LOGIC_OP_OR_REVERSE,
113
VK_LOGIC_OP_COPY_INVERTED,
114
VK_LOGIC_OP_OR_INVERTED,
115
VK_LOGIC_OP_NAND,
116
VK_LOGIC_OP_SET,
117
};
118
119
// In Vulkan, we simply collect all the state together into a "pipeline key" - we don't actually set any state here
120
// (the caller is responsible for setting the little dynamic state that is supported, dynState).
121
void DrawEngineVulkan::ConvertStateToVulkanKey(FramebufferManagerVulkan &fbManager, ShaderManagerVulkan *shaderManager, int prim, VulkanPipelineRasterStateKey &key, VulkanDynamicState &dynState) {
122
key.topology = primToVulkan[prim];
123
124
bool useBufferedRendering = framebufferManager_->UseBufferedRendering();
125
126
if (gstate_c.IsDirty(DIRTY_BLEND_STATE)) {
127
if (gstate.isModeClear()) {
128
key.logicOpEnable = false;
129
key.logicOp = VK_LOGIC_OP_CLEAR;
130
key.blendEnable = false;
131
key.blendOpColor = VK_BLEND_OP_ADD;
132
key.blendOpAlpha = VK_BLEND_OP_ADD;
133
key.srcColor = VK_BLEND_FACTOR_ONE;
134
key.srcAlpha = VK_BLEND_FACTOR_ONE;
135
key.destColor = VK_BLEND_FACTOR_ZERO;
136
key.destAlpha = VK_BLEND_FACTOR_ZERO;
137
dynState.useBlendColor = false;
138
139
// Color Mask
140
bool colorMask = gstate.isClearModeColorMask();
141
bool alphaMask = gstate.isClearModeAlphaMask();
142
key.colorWriteMask = (colorMask ? (VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT) : 0) | (alphaMask ? VK_COLOR_COMPONENT_A_BIT : 0);
143
} else {
144
pipelineState_.Convert(draw_->GetShaderLanguageDesc().bitwiseOps, gstate_c.Use(GPU_USE_SHADER_BLENDING));
145
GenericMaskState &maskState = pipelineState_.maskState;
146
GenericBlendState &blendState = pipelineState_.blendState;
147
GenericLogicState &logicState = pipelineState_.logicState;
148
149
if (pipelineState_.FramebufferRead() && useBufferedRendering) {
150
ApplyFramebufferRead(&fboTexBindState_);
151
// The shader takes over the responsibility for blending, so recompute.
152
// We might still end up using blend to write something to alpha.
153
ApplyStencilReplaceAndLogicOpIgnoreBlend(blendState.replaceAlphaWithStencil, blendState);
154
dirtyRequiresRecheck_ |= DIRTY_FRAGMENTSHADER_STATE;
155
gstate_c.Dirty(DIRTY_FRAGMENTSHADER_STATE);
156
} else {
157
if (fboTexBound_) {
158
boundSecondary_ = VK_NULL_HANDLE;
159
fboTexBound_ = false;
160
dirtyRequiresRecheck_ |= DIRTY_FRAGMENTSHADER_STATE;
161
gstate_c.Dirty(DIRTY_FRAGMENTSHADER_STATE);
162
}
163
}
164
165
if (blendState.blendEnabled) {
166
key.blendEnable = true;
167
key.blendOpColor = vkBlendEqLookup[(size_t)blendState.eqColor];
168
key.blendOpAlpha = vkBlendEqLookup[(size_t)blendState.eqAlpha];
169
key.srcColor = vkBlendFactorLookup[(size_t)blendState.srcColor];
170
key.srcAlpha = vkBlendFactorLookup[(size_t)blendState.srcAlpha];
171
key.destColor = vkBlendFactorLookup[(size_t)blendState.dstColor];
172
key.destAlpha = vkBlendFactorLookup[(size_t)blendState.dstAlpha];
173
if (blendState.dirtyShaderBlendFixValues) {
174
dirtyRequiresRecheck_ |= DIRTY_SHADERBLEND;
175
gstate_c.Dirty(DIRTY_SHADERBLEND);
176
}
177
dynState.useBlendColor = blendState.useBlendColor;
178
if (blendState.useBlendColor) {
179
dynState.blendColor = blendState.blendColor;
180
}
181
} else {
182
key.blendEnable = false;
183
key.blendOpColor = VK_BLEND_OP_ADD;
184
key.blendOpAlpha = VK_BLEND_OP_ADD;
185
key.srcColor = VK_BLEND_FACTOR_ONE;
186
key.srcAlpha = VK_BLEND_FACTOR_ONE;
187
key.destColor = VK_BLEND_FACTOR_ZERO;
188
key.destAlpha = VK_BLEND_FACTOR_ZERO;
189
dynState.useBlendColor = false;
190
}
191
192
key.colorWriteMask = maskState.channelMask; // flags match
193
194
if (logicState.logicOpEnabled) {
195
key.logicOpEnable = true;
196
key.logicOp = logicOps[(int)logicState.logicOp];
197
} else {
198
key.logicOpEnable = false;
199
key.logicOp = VK_LOGIC_OP_COPY;
200
}
201
202
// Workaround proposed in #10421, for bug where the color write mask is not applied correctly on Adreno.
203
if ((gstate.pmskc & 0x00FFFFFF) == 0x00FFFFFF && g_Config.bVendorBugChecksEnabled && draw_->GetBugs().Has(Draw::Bugs::COLORWRITEMASK_BROKEN_WITH_DEPTHTEST)) {
204
key.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
205
if (!key.blendEnable) {
206
bool writeAlpha = maskState.channelMask & 8;
207
key.blendEnable = true;
208
key.blendOpAlpha = VK_BLEND_OP_ADD;
209
key.srcAlpha = writeAlpha ? VK_BLEND_FACTOR_ONE : VK_BLEND_FACTOR_ZERO;
210
key.destAlpha = writeAlpha ? VK_BLEND_FACTOR_ZERO : VK_BLEND_FACTOR_ONE;
211
}
212
key.blendOpColor = VK_BLEND_OP_ADD;
213
key.srcColor = VK_BLEND_FACTOR_ZERO;
214
key.destColor = VK_BLEND_FACTOR_ONE;
215
}
216
}
217
}
218
219
if (gstate_c.IsDirty(DIRTY_RASTER_STATE)) {
220
bool wantCull = !gstate.isModeClear() && prim != GE_PRIM_RECTANGLES && prim > GE_PRIM_LINE_STRIP && gstate.isCullEnabled();
221
key.cullMode = wantCull ? (gstate.getCullMode() ? VK_CULL_MODE_FRONT_BIT : VK_CULL_MODE_BACK_BIT) : VK_CULL_MODE_NONE;
222
223
if (gstate.isModeClear() || gstate.isModeThrough()) {
224
// TODO: Might happen in clear mode if not through...
225
key.depthClampEnable = false;
226
} else {
227
if (gstate.getDepthRangeMin() == 0 || gstate.getDepthRangeMax() == 65535) {
228
// TODO: Still has a bug where we clamp to depth range if one is not the full range.
229
// But the alternate is not clamping in either direction...
230
key.depthClampEnable = gstate.isDepthClampEnabled() && gstate_c.Use(GPU_USE_DEPTH_CLAMP);
231
} else {
232
// We just want to clip in this case, the clamp would be clipped anyway.
233
key.depthClampEnable = false;
234
}
235
}
236
}
237
238
if (gstate_c.IsDirty(DIRTY_DEPTHSTENCIL_STATE)) {
239
GenericStencilFuncState stencilState;
240
ConvertStencilFuncState(stencilState);
241
242
if (gstate.isModeClear()) {
243
key.depthTestEnable = true;
244
key.depthCompareOp = VK_COMPARE_OP_ALWAYS;
245
key.depthWriteEnable = gstate.isClearModeDepthMask();
246
247
// Stencil Test
248
bool alphaMask = gstate.isClearModeAlphaMask();
249
if (alphaMask) {
250
key.stencilTestEnable = true;
251
key.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
252
key.stencilPassOp = VK_STENCIL_OP_REPLACE;
253
key.stencilFailOp = VK_STENCIL_OP_REPLACE;
254
key.stencilDepthFailOp = VK_STENCIL_OP_REPLACE;
255
dynState.useStencil = true;
256
// In clear mode, the stencil value is set to the alpha value of the vertex.
257
// A normal clear will be 2 points, the second point has the color.
258
// We override this value in the pipeline from software transform for clear rectangles.
259
dynState.stencilRef = 0xFF;
260
// But we still apply the stencil write mask.
261
dynState.stencilWriteMask = stencilState.writeMask;
262
} else {
263
key.stencilTestEnable = false;
264
key.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
265
key.stencilPassOp = VK_STENCIL_OP_REPLACE;
266
key.stencilFailOp = VK_STENCIL_OP_REPLACE;
267
key.stencilDepthFailOp = VK_STENCIL_OP_REPLACE;
268
dynState.useStencil = false;
269
}
270
} else {
271
// Depth Test
272
if (!IsDepthTestEffectivelyDisabled()) {
273
key.depthTestEnable = true;
274
key.depthCompareOp = compareOps[gstate.getDepthTestFunction()];
275
key.depthWriteEnable = gstate.isDepthWriteEnabled();
276
UpdateEverUsedEqualDepth(gstate.getDepthTestFunction());
277
} else {
278
key.depthTestEnable = false;
279
key.depthWriteEnable = false;
280
key.depthCompareOp = VK_COMPARE_OP_ALWAYS;
281
}
282
283
// Stencil Test
284
if (stencilState.enabled) {
285
key.stencilTestEnable = true;
286
key.stencilCompareOp = compareOps[stencilState.testFunc];
287
key.stencilPassOp = stencilOps[stencilState.zPass];
288
key.stencilFailOp = stencilOps[stencilState.sFail];
289
key.stencilDepthFailOp = stencilOps[stencilState.zFail];
290
dynState.useStencil = true;
291
dynState.stencilRef = stencilState.testRef;
292
dynState.stencilCompareMask = stencilState.testMask;
293
dynState.stencilWriteMask = stencilState.writeMask;
294
295
// Nasty special case for Spongebob and similar where it tries to write zeros to alpha/stencil during
296
// depth-fail. We can't write to alpha then because the pixel is killed. However, we can invert the depth
297
// test and modify the alpha function...
298
if (SpongebobDepthInverseConditions(stencilState)) {
299
key.blendEnable = true;
300
key.blendOpAlpha = VK_BLEND_OP_ADD;
301
key.blendOpColor = VK_BLEND_OP_ADD;
302
key.srcColor = VK_BLEND_FACTOR_ZERO;
303
key.destColor = VK_BLEND_FACTOR_ZERO;
304
key.logicOpEnable = false;
305
key.srcAlpha = VK_BLEND_FACTOR_ZERO;
306
key.destAlpha = VK_BLEND_FACTOR_ZERO;
307
key.colorWriteMask = VK_COLOR_COMPONENT_A_BIT;
308
key.depthCompareOp = VK_COMPARE_OP_LESS; // Inverse of GREATER_EQUAL
309
key.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
310
// Invert
311
key.stencilPassOp = VK_STENCIL_OP_ZERO;
312
key.stencilFailOp = VK_STENCIL_OP_ZERO;
313
key.stencilDepthFailOp = VK_STENCIL_OP_KEEP;
314
315
dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE;
316
gstate_c.Dirty(DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE);
317
}
318
} else {
319
key.stencilTestEnable = false;
320
key.stencilCompareOp = VK_COMPARE_OP_ALWAYS;
321
key.stencilPassOp = VK_STENCIL_OP_REPLACE;
322
key.stencilFailOp = VK_STENCIL_OP_REPLACE;
323
key.stencilDepthFailOp = VK_STENCIL_OP_REPLACE;
324
dynState.useStencil = false;
325
}
326
}
327
}
328
329
if (gstate_c.IsDirty(DIRTY_VIEWPORTSCISSOR_STATE)) {
330
ViewportAndScissor vpAndScissor;
331
ConvertViewportAndScissor(useBufferedRendering,
332
fbManager.GetRenderWidth(), fbManager.GetRenderHeight(),
333
fbManager.GetTargetBufferWidth(), fbManager.GetTargetBufferHeight(),
334
vpAndScissor);
335
UpdateCachedViewportState(vpAndScissor);
336
337
float depthMin = vpAndScissor.depthRangeMin;
338
float depthMax = vpAndScissor.depthRangeMax;
339
340
if (depthMin < 0.0f) depthMin = 0.0f;
341
if (depthMax > 1.0f) depthMax = 1.0f;
342
343
VkViewport &vp = dynState.viewport;
344
vp.x = vpAndScissor.viewportX;
345
vp.y = vpAndScissor.viewportY;
346
vp.width = vpAndScissor.viewportW;
347
vp.height = vpAndScissor.viewportH;
348
vp.minDepth = vpAndScissor.depthRangeMin;
349
vp.maxDepth = vpAndScissor.depthRangeMax;
350
351
ScissorRect &scissor = dynState.scissor;
352
scissor.x = vpAndScissor.scissorX;
353
scissor.y = vpAndScissor.scissorY;
354
scissor.width = std::max(0, vpAndScissor.scissorW);
355
scissor.height = std::max(0, vpAndScissor.scissorH);
356
}
357
}
358
359
void DrawEngineVulkan::BindShaderBlendTex() {
360
// TODO: At this point, we know if the vertices are full alpha or not.
361
// Set the nearest/linear here (since we correctly know if alpha/color tests are needed)?
362
if (!gstate.isModeClear()) {
363
if (fboTexBindState_ == FBO_TEX_COPY_BIND_TEX) {
364
VirtualFramebuffer *curRenderVfb = framebufferManager_->GetCurrentRenderVFB();
365
bool bindResult = framebufferManager_->BindFramebufferAsColorTexture(1, curRenderVfb, BINDFBCOLOR_MAY_COPY | BINDFBCOLOR_UNCACHED, Draw::ALL_LAYERS);
366
_dbg_assert_(bindResult);
367
boundSecondary_ = (VkImageView)draw_->GetNativeObject(Draw::NativeObject::BOUND_TEXTURE1_IMAGEVIEW);
368
fboTexBound_ = true;
369
fboTexBindState_ = FBO_TEX_NONE;
370
371
// Must dirty blend state here so we re-copy next time. Example: Lunar's spell effects.
372
dirtyRequiresRecheck_ |= DIRTY_BLEND_STATE;
373
} else {
374
boundSecondary_ = VK_NULL_HANDLE;
375
}
376
} else {
377
boundSecondary_ = VK_NULL_HANDLE;
378
}
379
}
380
381
void DrawEngineVulkan::ApplyDrawStateLate(VulkanRenderManager *renderManager, bool applyStencilRef, uint8_t stencilRef, bool useBlendConstant) {
382
if (gstate_c.IsDirty(DIRTY_VIEWPORTSCISSOR_STATE)) {
383
renderManager->SetScissor(dynState_.scissor.x, dynState_.scissor.y, dynState_.scissor.width, dynState_.scissor.height);
384
renderManager->SetViewport(dynState_.viewport);
385
}
386
if ((gstate_c.IsDirty(DIRTY_DEPTHSTENCIL_STATE) && dynState_.useStencil) || applyStencilRef) {
387
renderManager->SetStencilParams(dynState_.stencilWriteMask, dynState_.stencilCompareMask, applyStencilRef ? stencilRef : dynState_.stencilRef);
388
}
389
if (gstate_c.IsDirty(DIRTY_BLEND_STATE) && useBlendConstant) {
390
renderManager->SetBlendFactor(dynState_.blendColor);
391
}
392
}
393
394