2015-10-10 14:41:19 +00:00
|
|
|
#include <cstring>
|
2018-03-13 22:22:21 +00:00
|
|
|
#include <memory>
|
2018-03-19 09:55:06 +00:00
|
|
|
#include <set>
|
2020-08-10 12:31:31 +00:00
|
|
|
#include <sstream>
|
2015-10-10 14:41:19 +00:00
|
|
|
|
2020-10-04 08:04:01 +00:00
|
|
|
#include "Common/Profiler/Profiler.h"
|
2017-08-18 11:39:42 +00:00
|
|
|
|
2015-10-10 14:41:19 +00:00
|
|
|
#include "Common/Log.h"
|
2016-01-09 20:19:18 +00:00
|
|
|
#include "Common/StringUtils.h"
|
2020-10-04 21:24:14 +00:00
|
|
|
#include "Common/GPU/Vulkan/VulkanContext.h"
|
2022-11-27 10:39:44 +00:00
|
|
|
#include "Core/Config.h"
|
2015-10-10 14:41:19 +00:00
|
|
|
#include "GPU/Vulkan/VulkanUtil.h"
|
|
|
|
#include "GPU/Vulkan/PipelineManagerVulkan.h"
|
2016-01-24 16:30:26 +00:00
|
|
|
#include "GPU/Vulkan/ShaderManagerVulkan.h"
|
2018-03-13 22:22:21 +00:00
|
|
|
#include "GPU/Common/DrawEngineCommon.h"
|
2022-10-25 22:24:43 +00:00
|
|
|
#include "GPU/Common/ShaderId.h"
|
2020-10-04 21:24:14 +00:00
|
|
|
#include "Common/GPU/thin3d.h"
|
|
|
|
#include "Common/GPU/Vulkan/VulkanRenderManager.h"
|
|
|
|
#include "Common/GPU/Vulkan/VulkanQueueRunner.h"
|
2016-01-03 17:31:03 +00:00
|
|
|
|
2021-08-21 10:39:15 +00:00
|
|
|
using namespace PPSSPP_VK;
|
2021-08-20 09:22:57 +00:00
|
|
|
|
2022-09-07 13:19:20 +00:00
|
|
|
u32 VulkanPipeline::GetVariantsBitmask() const {
|
|
|
|
return pipeline->GetVariantsBitmask();
|
|
|
|
}
|
|
|
|
|
2021-02-15 18:29:34 +00:00
|
|
|
PipelineManagerVulkan::PipelineManagerVulkan(VulkanContext *vulkan) : pipelines_(256), vulkan_(vulkan) {
|
2018-03-13 22:22:21 +00:00
|
|
|
// The pipeline cache is created on demand (or explicitly through Load).
|
2016-01-03 17:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PipelineManagerVulkan::~PipelineManagerVulkan() {
|
2016-01-10 13:24:10 +00:00
|
|
|
Clear();
|
2016-10-09 19:16:42 +00:00
|
|
|
if (pipelineCache_ != VK_NULL_HANDLE)
|
|
|
|
vulkan_->Delete().QueueDeletePipelineCache(pipelineCache_);
|
2022-08-24 03:15:30 +00:00
|
|
|
vulkan_ = nullptr;
|
2016-01-10 13:24:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PipelineManagerVulkan::Clear() {
|
2017-08-20 09:30:19 +00:00
|
|
|
pipelines_.Iterate([&](const VulkanPipelineKey &key, VulkanPipeline *value) {
|
2022-09-07 14:11:15 +00:00
|
|
|
if (!value->pipeline) {
|
|
|
|
// Something went wrong.
|
|
|
|
ERROR_LOG(G3D, "Null pipeline found in PipelineManagerVulkan::Clear - didn't wait for asyncs?");
|
2022-09-07 17:29:54 +00:00
|
|
|
} else {
|
|
|
|
value->pipeline->QueueForDeletion(vulkan_);
|
2019-06-16 18:29:38 +00:00
|
|
|
}
|
2017-08-20 09:30:19 +00:00
|
|
|
delete value;
|
|
|
|
});
|
|
|
|
|
|
|
|
pipelines_.Clear();
|
2016-01-03 17:31:03 +00:00
|
|
|
}
|
2015-10-10 14:41:19 +00:00
|
|
|
|
2022-12-01 22:41:31 +00:00
|
|
|
void PipelineManagerVulkan::InvalidateMSAAPipelines() {
|
|
|
|
pipelines_.Iterate([&](const VulkanPipelineKey &key, VulkanPipeline *value) {
|
|
|
|
value->pipeline->DestroyVariants(vulkan_, true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-09 18:17:03 +00:00
|
|
|
void PipelineManagerVulkan::DeviceLost() {
|
|
|
|
Clear();
|
2016-10-09 18:39:09 +00:00
|
|
|
if (pipelineCache_ != VK_NULL_HANDLE)
|
|
|
|
vulkan_->Delete().QueueDeletePipelineCache(pipelineCache_);
|
2022-08-24 03:15:30 +00:00
|
|
|
vulkan_ = nullptr;
|
2016-10-09 18:17:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PipelineManagerVulkan::DeviceRestore(VulkanContext *vulkan) {
|
|
|
|
vulkan_ = vulkan;
|
2018-03-13 22:22:21 +00:00
|
|
|
// The pipeline cache is created on demand.
|
2016-10-09 18:17:03 +00:00
|
|
|
}
|
|
|
|
|
2015-10-10 14:41:19 +00:00
|
|
|
struct DeclTypeInfo {
|
|
|
|
VkFormat type;
|
2016-02-21 17:05:01 +00:00
|
|
|
const char *name;
|
2015-10-10 14:41:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const DeclTypeInfo VComp[] = {
|
|
|
|
{ VK_FORMAT_UNDEFINED, "NULL" }, // DEC_NONE,
|
|
|
|
{ VK_FORMAT_R32_SFLOAT, "R32_SFLOAT " }, // DEC_FLOAT_1,
|
|
|
|
{ VK_FORMAT_R32G32_SFLOAT, "R32G32_SFLOAT " }, // DEC_FLOAT_2,
|
|
|
|
{ VK_FORMAT_R32G32B32_SFLOAT, "R32G32B32_SFLOAT " }, // DEC_FLOAT_3,
|
|
|
|
{ VK_FORMAT_R32G32B32A32_SFLOAT, "R32G32B32A32_SFLOAT " }, // DEC_FLOAT_4,
|
|
|
|
|
2016-01-09 22:27:53 +00:00
|
|
|
{ VK_FORMAT_R8G8B8A8_SNORM, "R8G8B8A8_SNORM" }, // DEC_S8_3,
|
2015-10-10 14:41:19 +00:00
|
|
|
{ VK_FORMAT_R16G16B16A16_SNORM, "R16G16B16A16_SNORM " }, // DEC_S16_3,
|
2016-01-09 12:31:36 +00:00
|
|
|
|
|
|
|
{ VK_FORMAT_R8G8B8A8_UNORM, "R8G8B8A8_UNORM " }, // DEC_U8_1,
|
|
|
|
{ VK_FORMAT_R8G8B8A8_UNORM, "R8G8B8A8_UNORM " }, // DEC_U8_2,
|
|
|
|
{ VK_FORMAT_R8G8B8A8_UNORM, "R8G8B8A8_UNORM " }, // DEC_U8_3,
|
|
|
|
{ VK_FORMAT_R8G8B8A8_UNORM, "R8G8B8A8_UNORM " }, // DEC_U8_4,
|
2015-10-10 14:41:19 +00:00
|
|
|
{ VK_FORMAT_R16G16_UNORM, "R16G16_UNORM" }, // DEC_U16_1,
|
|
|
|
{ VK_FORMAT_R16G16_UNORM, "R16G16_UNORM" }, // DEC_U16_2,
|
|
|
|
{ VK_FORMAT_R16G16B16A16_UNORM, "R16G16B16A16_UNORM " }, // DEC_U16_3,
|
|
|
|
{ VK_FORMAT_R16G16B16A16_UNORM, "R16G16B16A16_UNORM " }, // DEC_U16_4,
|
|
|
|
};
|
|
|
|
|
2016-03-28 20:14:04 +00:00
|
|
|
static void VertexAttribSetup(VkVertexInputAttributeDescription *attr, int fmt, int offset, PspAttributeLocation location) {
|
2020-08-15 22:38:55 +00:00
|
|
|
_assert_(fmt != DEC_NONE);
|
|
|
|
_assert_(fmt < ARRAY_SIZE(VComp));
|
2015-10-10 14:41:19 +00:00
|
|
|
attr->location = (uint32_t)location;
|
|
|
|
attr->binding = 0;
|
|
|
|
attr->format = VComp[fmt].type;
|
|
|
|
attr->offset = offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the number of attributes that were set.
|
|
|
|
// We could cache these AttributeDescription arrays (with pspFmt as the key), but hardly worth bothering
|
|
|
|
// as we will only call this code when we need to create a new VkPipeline.
|
2016-03-28 20:14:04 +00:00
|
|
|
static int SetupVertexAttribs(VkVertexInputAttributeDescription attrs[], const DecVtxFormat &decFmt) {
|
2015-10-10 14:41:19 +00:00
|
|
|
int count = 0;
|
2018-04-10 09:21:56 +00:00
|
|
|
if (decFmt.w0fmt != 0) {
|
|
|
|
VertexAttribSetup(&attrs[count++], decFmt.w0fmt, decFmt.w0off, PspAttributeLocation::W1);
|
|
|
|
}
|
|
|
|
if (decFmt.w1fmt != 0) {
|
|
|
|
VertexAttribSetup(&attrs[count++], decFmt.w1fmt, decFmt.w1off, PspAttributeLocation::W2);
|
|
|
|
}
|
2015-10-10 14:41:19 +00:00
|
|
|
if (decFmt.uvfmt != 0) {
|
|
|
|
VertexAttribSetup(&attrs[count++], decFmt.uvfmt, decFmt.uvoff, PspAttributeLocation::TEXCOORD);
|
|
|
|
}
|
|
|
|
if (decFmt.c0fmt != 0) {
|
|
|
|
VertexAttribSetup(&attrs[count++], decFmt.c0fmt, decFmt.c0off, PspAttributeLocation::COLOR0);
|
|
|
|
}
|
|
|
|
if (decFmt.c1fmt != 0) {
|
|
|
|
VertexAttribSetup(&attrs[count++], decFmt.c1fmt, decFmt.c1off, PspAttributeLocation::COLOR1);
|
|
|
|
}
|
|
|
|
if (decFmt.nrmfmt != 0) {
|
|
|
|
VertexAttribSetup(&attrs[count++], decFmt.nrmfmt, decFmt.nrmoff, PspAttributeLocation::NORMAL);
|
|
|
|
}
|
|
|
|
// Position is always there.
|
|
|
|
VertexAttribSetup(&attrs[count++], decFmt.posfmt, decFmt.posoff, PspAttributeLocation::POSITION);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2021-10-24 19:07:57 +00:00
|
|
|
static int SetupVertexAttribsPretransformed(VkVertexInputAttributeDescription attrs[], bool needsUV, bool needsColor1, bool needsFog) {
|
2016-03-06 11:00:57 +00:00
|
|
|
int count = 0;
|
2021-10-24 19:07:57 +00:00
|
|
|
VertexAttribSetup(&attrs[count++], DEC_FLOAT_4, offsetof(TransformedVertex, pos), PspAttributeLocation::POSITION);
|
2019-02-04 13:33:01 +00:00
|
|
|
if (needsUV) {
|
2021-10-24 19:07:57 +00:00
|
|
|
VertexAttribSetup(&attrs[count++], DEC_FLOAT_3, offsetof(TransformedVertex, uv), PspAttributeLocation::TEXCOORD);
|
2019-02-04 13:33:01 +00:00
|
|
|
}
|
2021-10-24 19:07:57 +00:00
|
|
|
VertexAttribSetup(&attrs[count++], DEC_U8_4, offsetof(TransformedVertex, color0), PspAttributeLocation::COLOR0);
|
2019-01-25 09:44:50 +00:00
|
|
|
if (needsColor1) {
|
2021-10-24 19:07:57 +00:00
|
|
|
VertexAttribSetup(&attrs[count++], DEC_U8_4, offsetof(TransformedVertex, color1), PspAttributeLocation::COLOR1);
|
|
|
|
}
|
|
|
|
if (needsFog) {
|
|
|
|
VertexAttribSetup(&attrs[count++], DEC_FLOAT_1, offsetof(TransformedVertex, fog), PspAttributeLocation::NORMAL);
|
2019-01-25 09:44:50 +00:00
|
|
|
}
|
2016-03-06 11:00:57 +00:00
|
|
|
return count;
|
2015-10-10 14:41:19 +00:00
|
|
|
}
|
|
|
|
|
2016-04-02 09:06:39 +00:00
|
|
|
static bool UsesBlendConstant(int factor) {
|
2019-10-13 19:15:01 +00:00
|
|
|
switch (factor) {
|
|
|
|
case VK_BLEND_FACTOR_CONSTANT_ALPHA:
|
|
|
|
case VK_BLEND_FACTOR_CONSTANT_COLOR:
|
|
|
|
case VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA:
|
|
|
|
case VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-02 09:06:39 +00:00
|
|
|
}
|
|
|
|
|
2021-03-07 23:12:38 +00:00
|
|
|
static std::string CutFromMain(std::string str) {
|
|
|
|
std::vector<std::string> lines;
|
|
|
|
SplitString(str, '\n', lines);
|
|
|
|
|
|
|
|
std::string rebuilt;
|
|
|
|
bool foundStart = false;
|
|
|
|
int c = 0;
|
|
|
|
for (const std::string &str : lines) {
|
|
|
|
if (startsWith(str, "void main")) {
|
|
|
|
foundStart = true;
|
|
|
|
rebuilt += StringFromFormat("... (cut %d lines)\n", c);
|
|
|
|
}
|
|
|
|
if (foundStart) {
|
|
|
|
rebuilt += str + "\n";
|
|
|
|
}
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
return rebuilt;
|
|
|
|
}
|
|
|
|
|
2019-06-16 18:29:38 +00:00
|
|
|
static VulkanPipeline *CreateVulkanPipeline(VulkanRenderManager *renderManager, VkPipelineCache pipelineCache,
|
2022-11-27 10:39:44 +00:00
|
|
|
VkPipelineLayout layout, PipelineFlags pipelineFlags, VkSampleCountFlagBits sampleCount, const VulkanPipelineRasterStateKey &key,
|
2023-01-13 09:14:29 +00:00
|
|
|
const DecVtxFormat *decFmt, VulkanVertexShader *vs, VulkanFragmentShader *fs, VulkanGeometryShader *gs, bool useHwTransform, u32 variantBitmask, bool cacheLoad) {
|
2022-12-31 10:30:47 +00:00
|
|
|
|
|
|
|
if (!fs->GetModule()) {
|
|
|
|
ERROR_LOG(G3D, "Fragment shader missing in CreateVulkanPipeline");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!vs->GetModule()) {
|
|
|
|
ERROR_LOG(G3D, "Vertex shader missing in CreateVulkanPipeline");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-09-07 14:11:15 +00:00
|
|
|
VulkanPipeline *vulkanPipeline = new VulkanPipeline();
|
2022-12-03 22:52:06 +00:00
|
|
|
vulkanPipeline->desc = new VKRGraphicsPipelineDesc();
|
|
|
|
VKRGraphicsPipelineDesc *desc = vulkanPipeline->desc;
|
2019-06-16 18:29:38 +00:00
|
|
|
desc->pipelineCache = pipelineCache;
|
|
|
|
|
2022-12-31 10:30:47 +00:00
|
|
|
desc->fragmentShader = fs->GetModule();
|
|
|
|
desc->vertexShader = vs->GetModule();
|
|
|
|
desc->geometryShader = gs ? gs->GetModule() : nullptr;
|
|
|
|
|
2018-03-19 16:46:58 +00:00
|
|
|
PROFILE_THIS_SCOPE("pipelinebuild");
|
2017-10-20 16:09:05 +00:00
|
|
|
bool useBlendConstant = false;
|
|
|
|
|
2019-06-16 18:29:38 +00:00
|
|
|
VkPipelineColorBlendAttachmentState &blend0 = desc->blend0;
|
2015-10-10 14:41:19 +00:00
|
|
|
blend0.blendEnable = key.blendEnable;
|
|
|
|
if (key.blendEnable) {
|
2016-01-09 00:23:32 +00:00
|
|
|
blend0.colorBlendOp = (VkBlendOp)key.blendOpColor;
|
|
|
|
blend0.alphaBlendOp = (VkBlendOp)key.blendOpAlpha;
|
|
|
|
blend0.srcColorBlendFactor = (VkBlendFactor)key.srcColor;
|
|
|
|
blend0.srcAlphaBlendFactor = (VkBlendFactor)key.srcAlpha;
|
|
|
|
blend0.dstColorBlendFactor = (VkBlendFactor)key.destColor;
|
|
|
|
blend0.dstAlphaBlendFactor = (VkBlendFactor)key.destAlpha;
|
2015-10-10 14:41:19 +00:00
|
|
|
}
|
|
|
|
blend0.colorWriteMask = key.colorWriteMask;
|
2016-03-13 23:53:57 +00:00
|
|
|
|
2019-06-16 18:29:38 +00:00
|
|
|
VkPipelineColorBlendStateCreateInfo &cbs = desc->cbs;
|
2016-01-09 00:23:32 +00:00
|
|
|
cbs.flags = 0;
|
2015-10-10 14:41:19 +00:00
|
|
|
cbs.pAttachments = &blend0;
|
|
|
|
cbs.attachmentCount = 1;
|
|
|
|
cbs.logicOpEnable = key.logicOpEnable;
|
|
|
|
if (key.logicOpEnable)
|
2016-01-09 00:23:32 +00:00
|
|
|
cbs.logicOp = (VkLogicOp)key.logicOp;
|
2015-10-10 14:41:19 +00:00
|
|
|
else
|
|
|
|
cbs.logicOp = VK_LOGIC_OP_COPY;
|
|
|
|
|
2019-06-16 18:29:38 +00:00
|
|
|
VkPipelineDepthStencilStateCreateInfo &dss = desc->dss;
|
2015-10-10 14:41:19 +00:00
|
|
|
dss.depthBoundsTestEnable = false;
|
|
|
|
dss.stencilTestEnable = key.stencilTestEnable;
|
|
|
|
if (key.stencilTestEnable) {
|
2016-01-09 00:23:32 +00:00
|
|
|
dss.front.compareOp = (VkCompareOp)key.stencilCompareOp;
|
|
|
|
dss.front.passOp = (VkStencilOp)key.stencilPassOp;
|
|
|
|
dss.front.failOp = (VkStencilOp)key.stencilFailOp;
|
|
|
|
dss.front.depthFailOp = (VkStencilOp)key.stencilDepthFailOp;
|
2015-10-10 14:41:19 +00:00
|
|
|
// Back stencil is always the same as front on PSP.
|
|
|
|
memcpy(&dss.back, &dss.front, sizeof(dss.front));
|
|
|
|
}
|
|
|
|
dss.depthTestEnable = key.depthTestEnable;
|
|
|
|
if (key.depthTestEnable) {
|
2016-01-09 00:23:32 +00:00
|
|
|
dss.depthCompareOp = (VkCompareOp)key.depthCompareOp;
|
2015-10-10 14:41:19 +00:00
|
|
|
dss.depthWriteEnable = key.depthWriteEnable;
|
|
|
|
}
|
|
|
|
|
2019-06-16 18:29:38 +00:00
|
|
|
VkDynamicState *dynamicStates = &desc->dynamicStates[0];
|
2015-10-10 14:41:19 +00:00
|
|
|
int numDyn = 0;
|
2016-04-02 09:06:39 +00:00
|
|
|
if (key.blendEnable &&
|
2022-09-21 11:12:58 +00:00
|
|
|
(UsesBlendConstant(key.srcAlpha) || UsesBlendConstant(key.srcColor) || UsesBlendConstant(key.destAlpha) || UsesBlendConstant(key.destColor))) {
|
2015-10-10 14:41:19 +00:00
|
|
|
dynamicStates[numDyn++] = VK_DYNAMIC_STATE_BLEND_CONSTANTS;
|
2017-10-20 16:09:05 +00:00
|
|
|
useBlendConstant = true;
|
2015-10-10 14:41:19 +00:00
|
|
|
}
|
|
|
|
dynamicStates[numDyn++] = VK_DYNAMIC_STATE_SCISSOR;
|
|
|
|
dynamicStates[numDyn++] = VK_DYNAMIC_STATE_VIEWPORT;
|
|
|
|
if (key.stencilTestEnable) {
|
2016-03-27 02:43:42 +00:00
|
|
|
dynamicStates[numDyn++] = VK_DYNAMIC_STATE_STENCIL_WRITE_MASK;
|
2015-10-10 14:41:19 +00:00
|
|
|
dynamicStates[numDyn++] = VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK;
|
|
|
|
dynamicStates[numDyn++] = VK_DYNAMIC_STATE_STENCIL_REFERENCE;
|
|
|
|
}
|
2022-09-21 11:12:58 +00:00
|
|
|
|
2019-06-16 18:29:38 +00:00
|
|
|
VkPipelineDynamicStateCreateInfo &ds = desc->ds;
|
2016-01-09 00:23:32 +00:00
|
|
|
ds.flags = 0;
|
2015-10-10 14:41:19 +00:00
|
|
|
ds.pDynamicStates = dynamicStates;
|
|
|
|
ds.dynamicStateCount = numDyn;
|
2022-09-21 11:12:58 +00:00
|
|
|
|
2019-06-16 18:29:38 +00:00
|
|
|
VkPipelineRasterizationStateCreateInfo &rs = desc->rs;
|
2016-01-09 00:23:32 +00:00
|
|
|
rs.flags = 0;
|
2015-10-10 14:41:19 +00:00
|
|
|
rs.depthBiasEnable = false;
|
|
|
|
rs.cullMode = key.cullMode;
|
|
|
|
rs.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
|
2021-10-31 21:47:21 +00:00
|
|
|
rs.lineWidth = 1.0f;
|
2015-10-10 14:41:19 +00:00
|
|
|
rs.rasterizerDiscardEnable = false;
|
|
|
|
rs.polygonMode = VK_POLYGON_MODE_FILL;
|
2017-12-27 00:35:14 +00:00
|
|
|
rs.depthClampEnable = key.depthClampEnable;
|
2015-10-10 14:41:19 +00:00
|
|
|
|
2022-11-07 21:33:15 +00:00
|
|
|
desc->fragmentShaderSource = fs->GetShaderString(SHADER_STRING_SOURCE_CODE);
|
|
|
|
desc->vertexShaderSource = vs->GetShaderString(SHADER_STRING_SOURCE_CODE);
|
|
|
|
if (gs) {
|
2022-11-28 10:50:28 +00:00
|
|
|
desc->geometryShaderSource = gs->GetShaderString(SHADER_STRING_SOURCE_CODE);
|
2022-11-07 21:33:15 +00:00
|
|
|
}
|
2016-03-12 17:21:13 +00:00
|
|
|
|
2022-12-13 14:16:11 +00:00
|
|
|
desc->topology = (VkPrimitiveTopology)key.topology;
|
2015-10-10 14:41:19 +00:00
|
|
|
|
2022-12-13 14:16:11 +00:00
|
|
|
int vertexStride = 0;
|
2019-06-16 18:29:38 +00:00
|
|
|
VkVertexInputAttributeDescription *attrs = &desc->attrs[0];
|
|
|
|
|
2015-10-10 14:41:19 +00:00
|
|
|
int attributeCount;
|
|
|
|
if (useHwTransform) {
|
2017-11-13 10:16:20 +00:00
|
|
|
attributeCount = SetupVertexAttribs(attrs, *decFmt);
|
|
|
|
vertexStride = decFmt->stride;
|
2015-10-10 14:41:19 +00:00
|
|
|
} else {
|
2023-01-10 14:27:05 +00:00
|
|
|
bool needsUV = true;
|
2019-02-04 13:21:46 +00:00
|
|
|
bool needsColor1 = vs->GetID().Bit(VS_BIT_LMODE);
|
2022-09-26 07:30:54 +00:00
|
|
|
attributeCount = SetupVertexAttribsPretransformed(attrs, needsUV, needsColor1, true);
|
2021-10-24 19:07:57 +00:00
|
|
|
vertexStride = (int)sizeof(TransformedVertex);
|
2015-10-10 14:41:19 +00:00
|
|
|
}
|
|
|
|
|
2019-06-16 18:29:38 +00:00
|
|
|
VkVertexInputBindingDescription &ibd = desc->ibd;
|
2015-10-10 14:41:19 +00:00
|
|
|
ibd.binding = 0;
|
|
|
|
ibd.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
|
|
|
|
ibd.stride = vertexStride;
|
|
|
|
|
2019-06-16 18:29:38 +00:00
|
|
|
VkPipelineVertexInputStateCreateInfo &vis = desc->vis;
|
2016-01-09 00:23:32 +00:00
|
|
|
vis.flags = 0;
|
2015-10-10 14:41:19 +00:00
|
|
|
vis.vertexBindingDescriptionCount = 1;
|
2019-06-16 18:29:38 +00:00
|
|
|
vis.pVertexBindingDescriptions = &desc->ibd;
|
2015-10-10 14:41:19 +00:00
|
|
|
vis.vertexAttributeDescriptionCount = attributeCount;
|
|
|
|
vis.pVertexAttributeDescriptions = attrs;
|
|
|
|
|
2019-06-16 18:29:38 +00:00
|
|
|
VkPipelineViewportStateCreateInfo &views = desc->views;
|
2016-01-28 22:44:05 +00:00
|
|
|
views.flags = 0;
|
|
|
|
views.viewportCount = 1;
|
|
|
|
views.scissorCount = 1;
|
|
|
|
views.pViewports = nullptr; // dynamic
|
|
|
|
views.pScissors = nullptr; // dynamic
|
2016-01-03 17:31:03 +00:00
|
|
|
|
2022-09-06 11:30:18 +00:00
|
|
|
desc->pipelineLayout = layout;
|
2016-01-03 17:31:03 +00:00
|
|
|
|
2022-10-25 22:24:43 +00:00
|
|
|
std::string tag = "game";
|
|
|
|
#ifdef _DEBUG
|
|
|
|
tag = FragmentShaderDesc(fs->GetID()) + " VS " + VertexShaderDesc(vs->GetID());
|
|
|
|
#endif
|
|
|
|
|
2023-01-13 09:14:29 +00:00
|
|
|
VKRGraphicsPipeline *pipeline = renderManager->CreateGraphicsPipeline(desc, pipelineFlags, variantBitmask, sampleCount, cacheLoad, tag.c_str());
|
2015-10-10 14:41:19 +00:00
|
|
|
|
|
|
|
vulkanPipeline->pipeline = pipeline;
|
2022-09-21 11:12:58 +00:00
|
|
|
if (useBlendConstant) {
|
2022-02-19 19:40:27 +00:00
|
|
|
pipelineFlags |= PipelineFlags::USES_BLEND_CONSTANT;
|
2022-09-21 11:12:58 +00:00
|
|
|
}
|
2022-10-02 03:01:23 +00:00
|
|
|
if (gs) {
|
|
|
|
pipelineFlags |= PipelineFlags::USES_GEOMETRY_SHADER;
|
|
|
|
}
|
2020-10-11 09:47:24 +00:00
|
|
|
if (dss.depthTestEnable || dss.stencilTestEnable) {
|
2022-02-19 19:40:27 +00:00
|
|
|
pipelineFlags |= PipelineFlags::USES_DEPTH_STENCIL;
|
2020-10-11 09:47:24 +00:00
|
|
|
}
|
2022-02-19 19:40:27 +00:00
|
|
|
vulkanPipeline->pipelineFlags = pipelineFlags;
|
2015-10-10 14:41:19 +00:00
|
|
|
return vulkanPipeline;
|
|
|
|
}
|
2016-01-09 00:23:32 +00:00
|
|
|
|
2023-01-13 09:14:29 +00:00
|
|
|
VulkanPipeline *PipelineManagerVulkan::GetOrCreatePipeline(VulkanRenderManager *renderManager, VkPipelineLayout layout, const VulkanPipelineRasterStateKey &rasterKey, const DecVtxFormat *decFmt, VulkanVertexShader *vs, VulkanFragmentShader *fs, VulkanGeometryShader *gs, bool useHwTransform, u32 variantBitmask, int multiSampleLevel, bool cacheLoad) {
|
2018-03-13 22:22:21 +00:00
|
|
|
if (!pipelineCache_) {
|
|
|
|
VkPipelineCacheCreateInfo pc{ VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO };
|
|
|
|
VkResult res = vkCreatePipelineCache(vulkan_->GetDevice(), &pc, nullptr, &pipelineCache_);
|
2020-08-15 22:38:55 +00:00
|
|
|
_assert_(VK_SUCCESS == res);
|
2018-03-13 22:22:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 14:18:27 +00:00
|
|
|
VulkanPipelineKey key{};
|
2017-05-22 12:48:20 +00:00
|
|
|
|
2016-01-09 00:23:32 +00:00
|
|
|
key.raster = rasterKey;
|
2016-01-09 10:07:14 +00:00
|
|
|
key.useHWTransform = useHwTransform;
|
2022-06-11 14:24:19 +00:00
|
|
|
key.vShader = vs->GetModule();
|
|
|
|
key.fShader = fs->GetModule();
|
2022-10-02 03:01:23 +00:00
|
|
|
key.gShader = gs ? gs->GetModule() : VK_NULL_HANDLE;
|
2018-03-13 22:22:21 +00:00
|
|
|
key.vtxFmtId = useHwTransform ? decFmt->id : 0;
|
2017-08-20 09:30:19 +00:00
|
|
|
|
|
|
|
auto iter = pipelines_.Get(key);
|
|
|
|
if (iter)
|
|
|
|
return iter;
|
2017-08-18 11:39:42 +00:00
|
|
|
|
2022-02-19 19:40:27 +00:00
|
|
|
PipelineFlags pipelineFlags = (PipelineFlags)0;
|
|
|
|
if (fs->Flags() & FragmentShaderFlags::INPUT_ATTACHMENT) {
|
|
|
|
pipelineFlags |= PipelineFlags::USES_INPUT_ATTACHMENT;
|
|
|
|
}
|
2022-11-28 21:59:42 +00:00
|
|
|
if (fs->Flags() & FragmentShaderFlags::USES_DISCARD) {
|
|
|
|
pipelineFlags |= PipelineFlags::USES_DISCARD;
|
|
|
|
}
|
2022-10-09 18:19:39 +00:00
|
|
|
if (vs->Flags() & VertexShaderFlags::MULTI_VIEW) {
|
|
|
|
pipelineFlags |= PipelineFlags::USES_MULTIVIEW;
|
|
|
|
}
|
2022-02-19 19:40:27 +00:00
|
|
|
|
2022-12-14 13:29:18 +00:00
|
|
|
VkSampleCountFlagBits sampleCount = MultiSampleLevelToFlagBits(multiSampleLevel);
|
2022-11-27 10:39:44 +00:00
|
|
|
|
2016-01-09 00:23:32 +00:00
|
|
|
VulkanPipeline *pipeline = CreateVulkanPipeline(
|
2022-11-27 10:39:44 +00:00
|
|
|
renderManager, pipelineCache_, layout, pipelineFlags, sampleCount,
|
2023-01-13 09:14:29 +00:00
|
|
|
rasterKey, decFmt, vs, fs, gs, useHwTransform, variantBitmask, cacheLoad);
|
2022-12-31 10:30:47 +00:00
|
|
|
|
|
|
|
// If the above failed, we got a null pipeline. We still insert it to keep track.
|
2017-08-20 09:30:19 +00:00
|
|
|
pipelines_.Insert(key, pipeline);
|
2018-03-19 16:46:58 +00:00
|
|
|
|
|
|
|
// Don't return placeholder null pipelines.
|
|
|
|
if (pipeline && pipeline->pipeline) {
|
|
|
|
return pipeline;
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-01-09 00:23:32 +00:00
|
|
|
}
|
2016-01-09 20:19:18 +00:00
|
|
|
|
2022-12-14 13:29:18 +00:00
|
|
|
std::vector<std::string> PipelineManagerVulkan::DebugGetObjectIDs(DebugShaderType type) const {
|
2016-01-09 20:19:18 +00:00
|
|
|
std::vector<std::string> ids;
|
|
|
|
switch (type) {
|
|
|
|
case SHADER_TYPE_PIPELINE:
|
|
|
|
{
|
2017-08-20 09:30:19 +00:00
|
|
|
pipelines_.Iterate([&](const VulkanPipelineKey &key, VulkanPipeline *value) {
|
|
|
|
std::string id;
|
|
|
|
key.ToString(&id);
|
2016-01-09 20:19:18 +00:00
|
|
|
ids.push_back(id);
|
2017-08-20 09:30:19 +00:00
|
|
|
});
|
2016-01-09 20:19:18 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-02-21 17:05:01 +00:00
|
|
|
default:
|
|
|
|
break;
|
2016-01-09 20:19:18 +00:00
|
|
|
}
|
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
2017-11-13 14:27:06 +00:00
|
|
|
static const char *const topologies[8] = {
|
2023-01-08 22:37:38 +00:00
|
|
|
"POINTS",
|
|
|
|
"LINES",
|
2017-11-13 14:27:06 +00:00
|
|
|
"LINESTRIP",
|
2023-01-08 22:37:38 +00:00
|
|
|
"TRIS",
|
2017-11-13 14:27:06 +00:00
|
|
|
"TRISTRIP",
|
|
|
|
"TRIFAN",
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *const blendOps[8] = {
|
|
|
|
"ADD",
|
|
|
|
"SUB",
|
2023-01-08 22:37:38 +00:00
|
|
|
"RSUB",
|
2017-11-13 14:27:06 +00:00
|
|
|
"MIN",
|
|
|
|
"MAX",
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *const compareOps[8] = {
|
|
|
|
"NEVER",
|
|
|
|
"<",
|
|
|
|
"==",
|
|
|
|
"<=",
|
|
|
|
">",
|
|
|
|
">=",
|
|
|
|
"!=",
|
|
|
|
"ALWAYS",
|
|
|
|
};
|
|
|
|
|
2018-03-19 10:19:01 +00:00
|
|
|
static const char *const logicOps[] = {
|
|
|
|
"CLEAR",
|
|
|
|
"AND",
|
|
|
|
"AND_REV",
|
|
|
|
"COPY",
|
|
|
|
"AND_INV",
|
|
|
|
"NOOP",
|
|
|
|
"XOR",
|
|
|
|
"OR",
|
|
|
|
"NOR",
|
|
|
|
"EQUIV",
|
|
|
|
"INVERT",
|
|
|
|
"OR_REV",
|
|
|
|
"COPY_INV",
|
|
|
|
"OR_INV",
|
|
|
|
"NAND",
|
|
|
|
"SET",
|
|
|
|
};
|
|
|
|
|
2017-11-13 14:27:06 +00:00
|
|
|
static const char *const stencilOps[8] = {
|
|
|
|
"KEEP",
|
|
|
|
"ZERO",
|
2023-01-08 22:37:38 +00:00
|
|
|
"REPL",
|
|
|
|
"INC_SAT",
|
|
|
|
"DEC_SAT",
|
2017-11-13 14:27:06 +00:00
|
|
|
"INVERT",
|
|
|
|
"INC_WRAP",
|
|
|
|
"DEC_WRAP",
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *const blendFactors[19] = {
|
|
|
|
"ZERO",
|
|
|
|
"ONE",
|
2023-01-08 22:37:38 +00:00
|
|
|
"SRC_COL",
|
|
|
|
"INV_SRC_COL",
|
|
|
|
"DST_COL",
|
|
|
|
"INV_DST_COL",
|
|
|
|
"SRC_A",
|
|
|
|
"INV_SRC_A",
|
|
|
|
"DST_A",
|
|
|
|
"INV_DST_A",
|
|
|
|
"CONSTANT_COL",
|
|
|
|
"INV_CONST_COL",
|
|
|
|
"CONSTANT_A",
|
|
|
|
"INV_CONST_A",
|
|
|
|
"SRC_A_SAT",
|
|
|
|
"SRC1_COL",
|
|
|
|
"INV_SRC1_COL",
|
|
|
|
"SRC1_A",
|
|
|
|
"INV_SRC1_A",
|
2017-11-13 14:27:06 +00:00
|
|
|
};
|
|
|
|
|
2023-01-08 22:37:38 +00:00
|
|
|
std::string PipelineManagerVulkan::DebugGetObjectString(std::string id, DebugShaderType type, DebugShaderStringType stringType, ShaderManagerVulkan *shaderManager) {
|
2016-01-09 20:19:18 +00:00
|
|
|
if (type != SHADER_TYPE_PIPELINE)
|
|
|
|
return "N/A";
|
|
|
|
|
2017-11-13 14:27:06 +00:00
|
|
|
VulkanPipelineKey pipelineKey;
|
|
|
|
pipelineKey.FromString(id);
|
2016-01-09 20:19:18 +00:00
|
|
|
|
2022-12-13 15:26:38 +00:00
|
|
|
VulkanPipeline *pipeline = pipelines_.Get(pipelineKey);
|
|
|
|
if (!pipeline) {
|
|
|
|
return "N/A (missing)";
|
2016-01-09 20:19:18 +00:00
|
|
|
}
|
2022-12-13 15:26:38 +00:00
|
|
|
u32 variants = pipeline->GetVariantsBitmask();
|
2016-01-09 20:19:18 +00:00
|
|
|
|
2023-01-08 22:37:38 +00:00
|
|
|
std::string keyDescription = pipelineKey.GetDescription(stringType, shaderManager);
|
2022-12-13 15:26:38 +00:00
|
|
|
return StringFromFormat("%s. v: %08x", keyDescription.c_str(), variants);
|
2018-03-19 10:19:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-08 22:37:38 +00:00
|
|
|
std::string VulkanPipelineKey::GetRasterStateDesc(bool lineBreaks) const {
|
|
|
|
std::stringstream str;
|
|
|
|
str << topologies[raster.topology] << " ";
|
|
|
|
if (useHWTransform) {
|
|
|
|
str << "HWX ";
|
|
|
|
}
|
|
|
|
if (vtxFmtId) {
|
|
|
|
str << "Vfmt(" << StringFromFormat("%08x", vtxFmtId) << ") "; // TODO: Format nicer.
|
|
|
|
} else {
|
|
|
|
str << "SWX ";
|
|
|
|
}
|
|
|
|
if (lineBreaks) str << std::endl;
|
|
|
|
if (raster.blendEnable) {
|
|
|
|
str << "Blend(C:" << blendOps[raster.blendOpColor] << "/"
|
|
|
|
<< blendFactors[raster.srcColor] << ":" << blendFactors[raster.destColor] << " ";
|
|
|
|
if (raster.blendOpAlpha != VK_BLEND_OP_ADD ||
|
|
|
|
raster.srcAlpha != VK_BLEND_FACTOR_ONE ||
|
|
|
|
raster.destAlpha != VK_BLEND_FACTOR_ZERO) {
|
|
|
|
str << "A:" << blendOps[raster.blendOpAlpha] << "/"
|
2018-03-19 10:19:01 +00:00
|
|
|
<< blendFactors[raster.srcColor] << ":" << blendFactors[raster.destColor] << " ";
|
2017-11-13 14:27:06 +00:00
|
|
|
}
|
2023-01-08 22:37:38 +00:00
|
|
|
str << ") ";
|
|
|
|
if (lineBreaks) str << std::endl;
|
|
|
|
}
|
|
|
|
if (raster.colorWriteMask != 0xF) {
|
|
|
|
str << "Mask(";
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if (raster.colorWriteMask & (1 << i)) {
|
|
|
|
str << "RGBA"[i];
|
|
|
|
} else {
|
|
|
|
str << "_";
|
2017-11-13 14:27:06 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-08 22:37:38 +00:00
|
|
|
str << ") ";
|
|
|
|
if (lineBreaks) str << std::endl;
|
|
|
|
}
|
|
|
|
if (raster.depthTestEnable) {
|
|
|
|
str << "Z(";
|
|
|
|
if (raster.depthWriteEnable)
|
|
|
|
str << "W, ";
|
|
|
|
if (raster.depthCompareOp)
|
|
|
|
str << compareOps[raster.depthCompareOp & 7];
|
|
|
|
str << ") ";
|
|
|
|
if (lineBreaks) str << std::endl;
|
|
|
|
}
|
|
|
|
if (raster.stencilTestEnable) {
|
|
|
|
str << "Stenc(";
|
|
|
|
str << compareOps[raster.stencilCompareOp & 7] << " ";
|
|
|
|
str << stencilOps[raster.stencilPassOp & 7] << "/";
|
|
|
|
str << stencilOps[raster.stencilFailOp & 7] << "/";
|
|
|
|
str << stencilOps[raster.stencilDepthFailOp & 7];
|
|
|
|
str << ") ";
|
|
|
|
if (lineBreaks) str << std::endl;
|
|
|
|
}
|
|
|
|
if (raster.logicOpEnable) {
|
|
|
|
str << "Logic(" << logicOps[raster.logicOp & 15] << ") ";
|
|
|
|
if (lineBreaks) str << std::endl;
|
|
|
|
}
|
|
|
|
return str.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string VulkanPipelineKey::GetDescription(DebugShaderStringType stringType, ShaderManagerVulkan *shaderManager) const {
|
|
|
|
switch (stringType) {
|
|
|
|
case SHADER_STRING_SHORT_DESC:
|
|
|
|
// Just show the raster state. Also show brief VS/FS IDs?
|
|
|
|
return GetRasterStateDesc(false);
|
2016-01-09 20:19:18 +00:00
|
|
|
|
|
|
|
case SHADER_STRING_SOURCE_CODE:
|
|
|
|
{
|
2023-01-08 22:37:38 +00:00
|
|
|
// More detailed description of all the parts of the pipeline.
|
|
|
|
VkShaderModule fsModule = this->fShader->BlockUntilReady();
|
|
|
|
VkShaderModule vsModule = this->vShader->BlockUntilReady();
|
|
|
|
VkShaderModule gsModule = this->gShader ? this->gShader->BlockUntilReady() : VK_NULL_HANDLE;
|
|
|
|
|
|
|
|
std::stringstream str;
|
|
|
|
str << "VS: " << VertexShaderDesc(shaderManager->GetVertexShaderFromModule(vsModule)->GetID()) << std::endl;
|
|
|
|
str << "FS: " << FragmentShaderDesc(shaderManager->GetFragmentShaderFromModule(fsModule)->GetID()) << std::endl;
|
|
|
|
if (gsModule) {
|
|
|
|
str << "GS: " << GeometryShaderDesc(shaderManager->GetGeometryShaderFromModule(gsModule)->GetID()) << std::endl;
|
|
|
|
}
|
|
|
|
str << GetRasterStateDesc(true);
|
|
|
|
return str.str();
|
2016-01-09 20:19:18 +00:00
|
|
|
}
|
2023-01-08 22:37:38 +00:00
|
|
|
|
2016-01-09 20:19:18 +00:00
|
|
|
default:
|
|
|
|
return "N/A";
|
|
|
|
}
|
|
|
|
}
|
2017-11-12 09:17:49 +00:00
|
|
|
|
2018-03-13 22:22:21 +00:00
|
|
|
// For some reason this struct is only defined in the spec, not in the headers.
|
|
|
|
struct VkPipelineCacheHeader {
|
|
|
|
uint32_t headerSize;
|
|
|
|
VkPipelineCacheHeaderVersion version;
|
|
|
|
uint32_t vendorId;
|
|
|
|
uint32_t deviceId;
|
|
|
|
uint8_t uuid[VK_UUID_SIZE];
|
|
|
|
};
|
|
|
|
|
2018-03-29 12:36:04 +00:00
|
|
|
struct StoredVulkanPipelineKey {
|
|
|
|
VulkanPipelineRasterStateKey raster;
|
|
|
|
VShaderID vShaderID;
|
|
|
|
FShaderID fShaderID;
|
2022-10-02 03:13:30 +00:00
|
|
|
GShaderID gShaderID;
|
2018-03-29 12:36:04 +00:00
|
|
|
uint32_t vtxFmtId;
|
2022-09-07 13:19:20 +00:00
|
|
|
uint32_t variants;
|
|
|
|
bool useHWTransform; // TODO: Still needed?
|
2018-03-29 12:36:04 +00:00
|
|
|
|
|
|
|
// For std::set. Better zero-initialize the struct properly for this to work.
|
|
|
|
bool operator < (const StoredVulkanPipelineKey &other) const {
|
|
|
|
return memcmp(this, &other, sizeof(*this)) < 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-27 08:02:50 +00:00
|
|
|
// If you're looking for how to invalidate the cache, it's done in ShaderManagerVulkan, look for CACHE_VERSION and increment it.
|
|
|
|
// (Header of the same file this is stored in).
|
2022-12-13 18:18:20 +00:00
|
|
|
void PipelineManagerVulkan::SavePipelineCache(FILE *file, bool saveRawPipelineCache, ShaderManagerVulkan *shaderManager, Draw::DrawContext *drawContext) {
|
2018-03-29 12:36:04 +00:00
|
|
|
VulkanRenderManager *rm = (VulkanRenderManager *)drawContext->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
|
|
|
|
VulkanQueueRunner *queueRunner = rm->GetQueueRunner();
|
|
|
|
|
2018-03-13 22:22:21 +00:00
|
|
|
size_t dataSize = 0;
|
|
|
|
uint32_t size;
|
|
|
|
|
|
|
|
if (saveRawPipelineCache) {
|
2019-07-23 15:45:52 +00:00
|
|
|
// WARNING: See comment in LoadCache before using this path.
|
2018-03-13 22:22:21 +00:00
|
|
|
VkResult result = vkGetPipelineCacheData(vulkan_->GetDevice(), pipelineCache_, &dataSize, nullptr);
|
|
|
|
uint32_t size = (uint32_t)dataSize;
|
|
|
|
if (result != VK_SUCCESS) {
|
|
|
|
size = 0;
|
|
|
|
fwrite(&size, sizeof(size), 1, file);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::unique_ptr<uint8_t[]> buffer(new uint8_t[dataSize]);
|
|
|
|
vkGetPipelineCacheData(vulkan_->GetDevice(), pipelineCache_, &dataSize, buffer.get());
|
|
|
|
size = (uint32_t)dataSize;
|
|
|
|
fwrite(&size, sizeof(size), 1, file);
|
|
|
|
fwrite(buffer.get(), 1, size, file);
|
|
|
|
NOTICE_LOG(G3D, "Saved Vulkan pipeline cache (%d bytes).", (int)size);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t seekPosOnFailure = ftell(file);
|
|
|
|
|
|
|
|
bool failed = false;
|
2018-04-14 17:59:41 +00:00
|
|
|
bool writeFailed = false;
|
2018-03-19 09:55:06 +00:00
|
|
|
// Since we don't include the full pipeline key, there can be duplicates,
|
|
|
|
// caused by things like switching from buffered to non-buffered rendering.
|
|
|
|
// Make sure the set of pipelines we write is "unique".
|
|
|
|
std::set<StoredVulkanPipelineKey> keys;
|
|
|
|
|
2018-03-13 22:22:21 +00:00
|
|
|
pipelines_.Iterate([&](const VulkanPipelineKey &pkey, VulkanPipeline *value) {
|
|
|
|
if (failed)
|
|
|
|
return;
|
2022-06-11 14:24:19 +00:00
|
|
|
VulkanVertexShader *vshader = shaderManager->GetVertexShaderFromModule(pkey.vShader->BlockUntilReady());
|
|
|
|
VulkanFragmentShader *fshader = shaderManager->GetFragmentShaderFromModule(pkey.fShader->BlockUntilReady());
|
2022-10-02 03:01:23 +00:00
|
|
|
VulkanGeometryShader *gshader = nullptr;
|
|
|
|
if (pkey.gShader) {
|
|
|
|
gshader = shaderManager->GetGeometryShaderFromModule(pkey.gShader->BlockUntilReady());
|
|
|
|
if (!gshader)
|
|
|
|
failed = true;
|
|
|
|
}
|
|
|
|
if (!vshader || !fshader || failed) {
|
2018-03-13 22:22:21 +00:00
|
|
|
failed = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
StoredVulkanPipelineKey key{};
|
|
|
|
key.raster = pkey.raster;
|
|
|
|
key.useHWTransform = pkey.useHWTransform;
|
|
|
|
key.fShaderID = fshader->GetID();
|
|
|
|
key.vShaderID = vshader->GetID();
|
2022-10-02 03:13:30 +00:00
|
|
|
key.gShaderID = gshader ? gshader->GetID() : GShaderID();
|
2022-09-07 13:19:20 +00:00
|
|
|
key.variants = value->GetVariantsBitmask();
|
2018-03-13 22:22:21 +00:00
|
|
|
if (key.useHWTransform) {
|
|
|
|
// NOTE: This is not a vtype, but a decoded vertex format.
|
|
|
|
key.vtxFmtId = pkey.vtxFmtId;
|
|
|
|
}
|
2018-03-19 09:55:06 +00:00
|
|
|
keys.insert(key);
|
2018-03-13 22:22:21 +00:00
|
|
|
});
|
|
|
|
|
2018-03-19 09:55:06 +00:00
|
|
|
// Write the number of pipelines.
|
|
|
|
size = (uint32_t)keys.size();
|
2018-04-14 17:59:41 +00:00
|
|
|
writeFailed = writeFailed || fwrite(&size, sizeof(size), 1, file) != 1;
|
2018-03-19 09:55:06 +00:00
|
|
|
|
|
|
|
// Write the pipelines.
|
|
|
|
for (auto &key : keys) {
|
2018-04-14 17:59:41 +00:00
|
|
|
writeFailed = writeFailed || fwrite(&key, sizeof(key), 1, file) != 1;
|
2018-03-19 09:55:06 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 22:22:21 +00:00
|
|
|
if (failed) {
|
|
|
|
ERROR_LOG(G3D, "Failed to write pipeline cache, some shader was missing");
|
|
|
|
// Write a zero in the right place so it doesn't try to load the pipelines next time.
|
|
|
|
size = 0;
|
|
|
|
fseek(file, (long)seekPosOnFailure, SEEK_SET);
|
2018-04-14 17:59:41 +00:00
|
|
|
writeFailed = fwrite(&size, sizeof(size), 1, file) != 1;
|
|
|
|
if (writeFailed) {
|
|
|
|
ERROR_LOG(G3D, "Failed to write pipeline cache, disk full?");
|
|
|
|
}
|
2018-03-13 22:22:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-04-14 17:59:41 +00:00
|
|
|
if (writeFailed) {
|
|
|
|
ERROR_LOG(G3D, "Failed to write pipeline cache, disk full?");
|
|
|
|
} else {
|
|
|
|
NOTICE_LOG(G3D, "Saved Vulkan pipeline ID cache (%d unique pipelines/%d).", (int)keys.size(), (int)pipelines_.size());
|
|
|
|
}
|
2018-03-13 22:22:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 13:29:18 +00:00
|
|
|
bool PipelineManagerVulkan::LoadPipelineCache(FILE *file, bool loadRawPipelineCache, ShaderManagerVulkan *shaderManager, Draw::DrawContext *drawContext, VkPipelineLayout layout, int multiSampleLevel) {
|
2018-03-29 12:36:04 +00:00
|
|
|
VulkanRenderManager *rm = (VulkanRenderManager *)drawContext->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
|
|
|
|
VulkanQueueRunner *queueRunner = rm->GetQueueRunner();
|
|
|
|
|
2022-12-13 22:06:57 +00:00
|
|
|
cancelCache_ = false;
|
|
|
|
|
2018-03-13 22:22:21 +00:00
|
|
|
uint32_t size = 0;
|
|
|
|
if (loadRawPipelineCache) {
|
2022-12-13 15:26:38 +00:00
|
|
|
NOTICE_LOG(G3D, "WARNING: Using the badly tested raw pipeline cache path!!!!");
|
2019-07-23 15:45:52 +00:00
|
|
|
// WARNING: Do not use this path until after reading and implementing https://zeux.io/2019/07/17/serializing-pipeline-cache/ !
|
2018-04-14 17:59:41 +00:00
|
|
|
bool success = fread(&size, sizeof(size), 1, file) == 1;
|
|
|
|
if (!size || !success) {
|
2018-03-13 22:22:21 +00:00
|
|
|
WARN_LOG(G3D, "Zero-sized Vulkan pipeline cache.");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
std::unique_ptr<uint8_t[]> buffer(new uint8_t[size]);
|
2018-04-14 17:59:41 +00:00
|
|
|
success = fread(buffer.get(), 1, size, file) == size;
|
2018-03-13 22:22:21 +00:00
|
|
|
// Verify header.
|
|
|
|
VkPipelineCacheHeader *header = (VkPipelineCacheHeader *)buffer.get();
|
2018-04-14 17:59:41 +00:00
|
|
|
if (!success || header->version != VK_PIPELINE_CACHE_HEADER_VERSION_ONE) {
|
2018-03-13 22:22:21 +00:00
|
|
|
// Bad header, don't do anything.
|
|
|
|
WARN_LOG(G3D, "Bad Vulkan pipeline cache header - ignoring");
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-05 09:05:22 +00:00
|
|
|
if (0 != memcmp(header->uuid, vulkan_->GetPhysicalDeviceProperties().properties.pipelineCacheUUID, VK_UUID_SIZE)) {
|
2018-03-13 22:22:21 +00:00
|
|
|
// Wrong hardware/driver/etc.
|
|
|
|
WARN_LOG(G3D, "Bad Vulkan pipeline cache UUID - ignoring");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
VkPipelineCacheCreateInfo pc{ VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO };
|
|
|
|
pc.pInitialData = buffer.get();
|
|
|
|
pc.initialDataSize = size;
|
|
|
|
pc.flags = 0;
|
|
|
|
VkPipelineCache cache;
|
|
|
|
VkResult res = vkCreatePipelineCache(vulkan_->GetDevice(), &pc, nullptr, &cache);
|
|
|
|
if (res != VK_SUCCESS) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!pipelineCache_) {
|
|
|
|
pipelineCache_ = cache;
|
|
|
|
} else {
|
|
|
|
vkMergePipelineCaches(vulkan_->GetDevice(), pipelineCache_, 1, &cache);
|
|
|
|
}
|
2022-12-13 15:26:38 +00:00
|
|
|
NOTICE_LOG(G3D, "Loaded Vulkan binary pipeline cache (%d bytes).", (int)size);
|
|
|
|
// Note that after loading the cache, it's still a good idea to pre-create the various pipelines.
|
2018-03-13 22:22:21 +00:00
|
|
|
} else {
|
|
|
|
if (!pipelineCache_) {
|
|
|
|
VkPipelineCacheCreateInfo pc{ VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO };
|
|
|
|
VkResult res = vkCreatePipelineCache(vulkan_->GetDevice(), &pc, nullptr, &pipelineCache_);
|
2021-02-15 17:28:07 +00:00
|
|
|
if (res != VK_SUCCESS) {
|
2022-12-13 15:26:38 +00:00
|
|
|
WARN_LOG(G3D, "vkCreatePipelineCache failed (%08x), highly unexpected", (u32)res);
|
2021-02-15 17:28:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-13 22:22:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the number of pipelines.
|
2018-04-14 17:59:41 +00:00
|
|
|
bool failed = fread(&size, sizeof(size), 1, file) != 1;
|
2018-03-13 22:22:21 +00:00
|
|
|
|
2022-12-14 14:15:31 +00:00
|
|
|
NOTICE_LOG(G3D, "Creating %d pipelines from cache (%dx MSAA)...", size, (1 << multiSampleLevel));
|
2021-03-07 23:12:38 +00:00
|
|
|
int pipelineCreateFailCount = 0;
|
2022-12-13 22:06:57 +00:00
|
|
|
int shaderFailCount = 0;
|
2018-03-13 22:22:21 +00:00
|
|
|
for (uint32_t i = 0; i < size; i++) {
|
2018-10-31 03:32:12 +00:00
|
|
|
if (failed || cancelCache_) {
|
2018-04-14 17:59:41 +00:00
|
|
|
break;
|
2018-03-13 22:22:21 +00:00
|
|
|
}
|
|
|
|
StoredVulkanPipelineKey key;
|
2018-04-14 17:59:41 +00:00
|
|
|
failed = failed || fread(&key, sizeof(key), 1, file) != 1;
|
|
|
|
if (failed) {
|
2022-12-13 22:06:57 +00:00
|
|
|
ERROR_LOG(G3D, "Truncated Vulkan pipeline cache file, stopping.");
|
|
|
|
break;
|
2018-04-14 17:59:41 +00:00
|
|
|
}
|
2018-03-13 22:22:21 +00:00
|
|
|
VulkanVertexShader *vs = shaderManager->GetVertexShaderFromID(key.vShaderID);
|
|
|
|
VulkanFragmentShader *fs = shaderManager->GetFragmentShaderFromID(key.fShaderID);
|
2022-10-02 03:13:30 +00:00
|
|
|
VulkanGeometryShader *gs = shaderManager->GetGeometryShaderFromID(key.gShaderID);
|
2021-10-26 07:56:14 +00:00
|
|
|
if (!vs || !fs || (!gs && key.gShaderID.Bit(GS_BIT_ENABLED))) {
|
2022-12-13 22:06:57 +00:00
|
|
|
// We just ignore this one, it'll get created later if needed.
|
|
|
|
// Probably some useFlags mismatch.
|
|
|
|
WARN_LOG(G3D, "Failed to find vs or fs in pipeline %d in cache, skipping pipeline", (int)i);
|
2018-03-13 22:22:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-03-29 12:36:04 +00:00
|
|
|
|
2022-12-13 22:06:57 +00:00
|
|
|
// Avoid creating multisampled shaders if it's not enabled, as that results in an invalid combination.
|
2023-01-13 09:14:29 +00:00
|
|
|
// Note that variantsToBuild is NOT directly a RenderPassType! instead, it's a collection of (1 << RenderPassType).
|
2022-12-13 22:06:57 +00:00
|
|
|
u32 variantsToBuild = key.variants;
|
2022-12-14 13:29:18 +00:00
|
|
|
if (multiSampleLevel == 0) {
|
2022-12-13 22:06:57 +00:00
|
|
|
for (u32 i = 0; i < (int)RenderPassType::TYPE_COUNT; i++) {
|
|
|
|
if (RenderPassTypeHasMultisample((RenderPassType)i)) {
|
|
|
|
variantsToBuild &= ~(1 << i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-13 22:22:21 +00:00
|
|
|
DecVtxFormat fmt;
|
|
|
|
fmt.InitializeFromID(key.vtxFmtId);
|
2022-12-13 22:06:57 +00:00
|
|
|
VulkanPipeline *pipeline = GetOrCreatePipeline(
|
2023-01-13 09:14:29 +00:00
|
|
|
rm, layout, key.raster, key.useHWTransform ? &fmt : 0, vs, fs, gs, key.useHWTransform, variantsToBuild, multiSampleLevel, true);
|
2021-03-07 23:12:38 +00:00
|
|
|
if (!pipeline) {
|
|
|
|
pipelineCreateFailCount += 1;
|
|
|
|
}
|
2018-03-13 22:22:21 +00:00
|
|
|
}
|
2022-09-07 13:19:20 +00:00
|
|
|
|
|
|
|
rm->NudgeCompilerThread();
|
|
|
|
|
2021-03-07 23:12:38 +00:00
|
|
|
NOTICE_LOG(G3D, "Recreated Vulkan pipeline cache (%d pipelines, %d failed).", (int)size, pipelineCreateFailCount);
|
2022-12-13 22:06:57 +00:00
|
|
|
// We just ignore any failures.
|
2018-03-13 22:22:21 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-10-31 03:32:12 +00:00
|
|
|
|
|
|
|
void PipelineManagerVulkan::CancelCache() {
|
|
|
|
cancelCache_ = true;
|
|
|
|
}
|