Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #ifndef INCLUDED_RENDERER_BACKEND_VULKAN_PIPELINESTATE 19 : #define INCLUDED_RENDERER_BACKEND_VULKAN_PIPELINESTATE 20 : 21 : #include "graphics/Color.h" 22 : #include "renderer/backend/PipelineState.h" 23 : #include "renderer/backend/vulkan/DeviceObjectUID.h" 24 : 25 : #include <cstddef> 26 : #include <glad/vulkan.h> 27 : #include <memory> 28 : #include <unordered_map> 29 : 30 : namespace Renderer::Backend::Vulkan { class CDevice; } 31 : namespace Renderer::Backend::Vulkan { class CFramebuffer; } 32 : namespace Renderer::Backend::Vulkan { class CVertexInputLayout; } 33 : 34 : namespace Renderer 35 : { 36 : 37 : namespace Backend 38 : { 39 : 40 : namespace Vulkan 41 : { 42 : 43 : class CGraphicsPipelineState final : public IGraphicsPipelineState 44 : { 45 : public: 46 : ~CGraphicsPipelineState() override; 47 : 48 : IDevice* GetDevice() override; 49 : 50 0 : IShaderProgram* GetShaderProgram() const override { return m_Desc.shaderProgram; } 51 : 52 : const SGraphicsPipelineStateDesc& GetDesc() const { return m_Desc; } 53 : 54 : VkPipeline GetOrCreatePipeline( 55 : const CVertexInputLayout* vertexInputLayout, CFramebuffer* framebuffer); 56 : 57 0 : DeviceObjectUID GetUID() const { return m_UID; } 58 : 59 : private: 60 : friend class CDevice; 61 : 62 : static std::unique_ptr<CGraphicsPipelineState> Create( 63 : CDevice* device, const SGraphicsPipelineStateDesc& desc); 64 : 65 0 : CGraphicsPipelineState() = default; 66 : 67 : CDevice* m_Device = nullptr; 68 : 69 : DeviceObjectUID m_UID{INVALID_DEVICE_OBJECT_UID}; 70 : 71 : SGraphicsPipelineStateDesc m_Desc{}; 72 : 73 : struct CacheKey 74 : { 75 : DeviceObjectUID vertexInputLayoutUID; 76 : // TODO: try to replace the UID by the only required parameters. 77 : DeviceObjectUID framebufferUID; 78 : }; 79 : struct CacheKeyHash 80 : { 81 : size_t operator()(const CacheKey& cacheKey) const; 82 : }; 83 : struct CacheKeyEqual 84 : { 85 : bool operator()(const CacheKey& lhs, const CacheKey& rhs) const; 86 : }; 87 : std::unordered_map<CacheKey, VkPipeline, CacheKeyHash, CacheKeyEqual> m_PipelineMap; 88 : }; 89 : 90 : class CComputePipelineState final : public IComputePipelineState 91 : { 92 : public: 93 : ~CComputePipelineState() override; 94 : 95 : IDevice* GetDevice() override; 96 : 97 0 : IShaderProgram* GetShaderProgram() const override { return m_Desc.shaderProgram; } 98 : 99 : const SComputePipelineStateDesc& GetDesc() const { return m_Desc; } 100 : 101 0 : VkPipeline GetPipeline() { return m_Pipeline; } 102 : 103 0 : DeviceObjectUID GetUID() const { return m_UID; } 104 : 105 : private: 106 : friend class CDevice; 107 : 108 : static std::unique_ptr<CComputePipelineState> Create( 109 : CDevice* device, const SComputePipelineStateDesc& desc); 110 : 111 0 : CComputePipelineState() = default; 112 : 113 : CDevice* m_Device{nullptr}; 114 : 115 : DeviceObjectUID m_UID{INVALID_DEVICE_OBJECT_UID}; 116 : 117 : SComputePipelineStateDesc m_Desc{}; 118 : 119 : VkPipeline m_Pipeline{VK_NULL_HANDLE}; 120 : }; 121 : 122 : } // namespace Vulkan 123 : 124 : } // namespace Backend 125 : 126 : } // namespace Renderer 127 : 128 : #endif // INCLUDED_RENDERER_BACKEND_VULKAN_PIPELINESTATE