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