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_SHADERTECHNIQUE 19 : #define INCLUDED_SHADERTECHNIQUE 20 : 21 : #include "graphics/ShaderDefines.h" 22 : #include "graphics/ShaderProgramPtr.h" 23 : #include "lib/code_annotation.h" 24 : #include "lib/file/vfs/vfs_path.h" 25 : #include "renderer/backend/PipelineState.h" 26 : 27 : #include <functional> 28 : #include <memory> 29 : #include <vector> 30 : 31 : namespace Renderer::Backend { class IShaderProgram; } 32 : 33 : /** 34 : * Implements a render pass consisting of a pipeline state and a shader, 35 : * used by CShaderTechnique. 36 : */ 37 : class CShaderPass 38 : { 39 : public: 40 : CShaderPass( 41 : std::unique_ptr<Renderer::Backend::IGraphicsPipelineState> pipelineState, 42 : const CShaderProgramPtr& shader); 43 0 : MOVABLE(CShaderPass); 44 : 45 : const CShaderProgramPtr& GetShaderProgram() const noexcept { return m_Shader; } 46 : 47 : Renderer::Backend::IGraphicsPipelineState* 48 0 : GetPipelineState() const noexcept { return m_PipelineState.get(); } 49 : 50 : private: 51 : CShaderProgramPtr m_Shader; 52 : 53 : std::unique_ptr<Renderer::Backend::IGraphicsPipelineState> m_PipelineState; 54 : }; 55 : 56 : /** 57 : * Implements a render technique consisting of a sequence of passes. 58 : * CShaderManager loads these from shader effect XML files. 59 : */ 60 : class CShaderTechnique 61 : { 62 : public: 63 : using PipelineStateDescCallback = 64 : std::function<void(Renderer::Backend::SGraphicsPipelineStateDesc& pipelineStateDesc)>; 65 : 66 : CShaderTechnique(const VfsPath& path, const CShaderDefines& defines, const PipelineStateDescCallback& callback); 67 : 68 : void SetPasses(std::vector<CShaderPass>&& passes); 69 : void SetComputePipelineState( 70 : std::unique_ptr<Renderer::Backend::IComputePipelineState> pipelineState, 71 : const CShaderProgramPtr& computeShader); 72 : 73 : int GetNumPasses() const; 74 : 75 : Renderer::Backend::IShaderProgram* GetShader(int pass = 0) const; 76 : 77 : Renderer::Backend::IGraphicsPipelineState* 78 : GetGraphicsPipelineState(int pass = 0) const; 79 : 80 : Renderer::Backend::IComputePipelineState* 81 : GetComputePipelineState() const; 82 : 83 : /** 84 : * Whether this technique uses alpha blending that requires objects to be 85 : * drawn from furthest to nearest. 86 : */ 87 : bool GetSortByDistance() const; 88 : 89 : void SetSortByDistance(bool enable); 90 : 91 390 : const VfsPath& GetPath() { return m_Path; } 92 : 93 0 : const CShaderDefines& GetShaderDefines() { return m_Defines; } 94 : 95 0 : const PipelineStateDescCallback& GetPipelineStateDescCallback() const { return m_PipelineStateDescCallback; }; 96 : 97 : private: 98 : std::vector<CShaderPass> m_Passes; 99 : 100 : bool m_SortByDistance = false; 101 : 102 : // We need additional data to reload the technique. 103 : VfsPath m_Path; 104 : CShaderDefines m_Defines; 105 : 106 : PipelineStateDescCallback m_PipelineStateDescCallback; 107 : 108 : std::unique_ptr<Renderer::Backend::IComputePipelineState> m_ComputePipelineState; 109 : CShaderProgramPtr m_ComputeShader; 110 : }; 111 : 112 : #endif // INCLUDED_SHADERTECHNIQUE