1 // Copyright (c) 2015- 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 #pragma once 19 20 #include <cstring> 21 #include "Common/Data/Collections/Hashmaps.h" 22 23 #include "GPU/Common/VertexDecoderCommon.h" 24 #include "GPU/Common/ShaderId.h" 25 #include "GPU/Common/ShaderCommon.h" 26 #include "GPU/Vulkan/VulkanUtil.h" 27 #include "GPU/Vulkan/StateMappingVulkan.h" 28 29 #include "GPU/Vulkan/VulkanQueueRunner.h" 30 31 struct VulkanPipelineKey { 32 VulkanPipelineRasterStateKey raster; // prim is included here 33 VkRenderPass renderPass; 34 VkShaderModule vShader; 35 VkShaderModule fShader; 36 uint32_t vtxFmtId; 37 bool useHWTransform; 38 ToStringVulkanPipelineKey39 void ToString(std::string *str) const { 40 str->resize(sizeof(*this)); 41 memcpy(&(*str)[0], this, sizeof(*this)); 42 } FromStringVulkanPipelineKey43 void FromString(const std::string &str) { 44 memcpy(this, &str[0], sizeof(*this)); 45 } 46 std::string GetDescription(DebugShaderStringType stringType) const; 47 }; 48 49 // Simply wraps a Vulkan pipeline, providing some metadata. 50 struct VulkanPipeline { 51 VkPipeline pipeline; 52 int flags; // PipelineFlags enum above. 53 UsesBlendConstantVulkanPipeline54 bool UsesBlendConstant() const { return (flags & PIPELINE_FLAG_USES_BLEND_CONSTANT) != 0; } UsesLinesVulkanPipeline55 bool UsesLines() const { return (flags & PIPELINE_FLAG_USES_LINES) != 0; } UsesDepthStencilVulkanPipeline56 bool UsesDepthStencil() const { return (flags & PIPELINE_FLAG_USES_DEPTH_STENCIL) != 0; } 57 }; 58 59 class VulkanContext; 60 class VulkanVertexShader; 61 class VulkanFragmentShader; 62 class ShaderManagerVulkan; 63 class DrawEngineCommon; 64 65 class PipelineManagerVulkan { 66 public: 67 PipelineManagerVulkan(VulkanContext *ctx); 68 ~PipelineManagerVulkan(); 69 70 VulkanPipeline *GetOrCreatePipeline(VkPipelineLayout layout, VkRenderPass renderPass, const VulkanPipelineRasterStateKey &rasterKey, const DecVtxFormat *decFmt, VulkanVertexShader *vs, VulkanFragmentShader *fs, bool useHwTransform); GetNumPipelines()71 int GetNumPipelines() const { return (int)pipelines_.size(); } 72 73 void Clear(); 74 75 void SetLineWidth(float lw); 76 77 void DeviceLost(); 78 void DeviceRestore(VulkanContext *vulkan); 79 80 std::string DebugGetObjectString(std::string id, DebugShaderType type, DebugShaderStringType stringType); 81 std::vector<std::string> DebugGetObjectIDs(DebugShaderType type); 82 83 // Saves data for faster creation next time. 84 void SaveCache(FILE *file, bool saveRawPipelineCache, ShaderManagerVulkan *shaderManager, Draw::DrawContext *drawContext); 85 bool LoadCache(FILE *file, bool loadRawPipelineCache, ShaderManagerVulkan *shaderManager, Draw::DrawContext *drawContext, VkPipelineLayout layout); 86 void CancelCache(); 87 88 private: 89 DenseHashMap<VulkanPipelineKey, VulkanPipeline *, nullptr> pipelines_; 90 VkPipelineCache pipelineCache_ = VK_NULL_HANDLE; 91 VulkanContext *vulkan_; 92 float lineWidth_ = 1.0f; 93 bool cancelCache_ = false; 94 }; 95