[Gameplay] Reduce loom cost
[0ad.git] / source / graphics / ShaderTechnique.h
blob7eb29cc65c762746fdd80156ae5c4d7d3976b83b
1 /* Copyright (C) 2022 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #ifndef INCLUDED_SHADERTECHNIQUE
19 #define INCLUDED_SHADERTECHNIQUE
21 #include "graphics/ShaderDefines.h"
22 #include "graphics/ShaderProgram.h"
23 #include "graphics/ShaderTechniquePtr.h"
24 #include "lib/code_annotation.h"
25 #include "lib/file/vfs/vfs_path.h"
26 #include "renderer/backend/PipelineState.h"
28 #include <functional>
29 #include <memory>
30 #include <vector>
32 /**
33 * Implements a render pass consisting of a pipeline state and a shader,
34 * used by CShaderTechnique.
36 class CShaderPass
38 public:
39 CShaderPass(
40 std::unique_ptr<Renderer::Backend::IGraphicsPipelineState> pipelineState,
41 const CShaderProgramPtr& shader);
42 MOVABLE(CShaderPass);
44 const CShaderProgramPtr& GetShaderProgram() const noexcept { return m_Shader; }
46 Renderer::Backend::IGraphicsPipelineState*
47 GetPipelineState() const noexcept { return m_PipelineState.get(); }
49 private:
50 CShaderProgramPtr m_Shader;
52 std::unique_ptr<Renderer::Backend::IGraphicsPipelineState> m_PipelineState;
55 /**
56 * Implements a render technique consisting of a sequence of passes.
57 * CShaderManager loads these from shader effect XML files.
59 class CShaderTechnique
61 public:
62 using PipelineStateDescCallback =
63 std::function<void(Renderer::Backend::SGraphicsPipelineStateDesc& pipelineStateDesc)>;
65 CShaderTechnique(const VfsPath& path, const CShaderDefines& defines, const PipelineStateDescCallback& callback);
67 void SetPasses(std::vector<CShaderPass>&& passes);
69 int GetNumPasses() const;
71 Renderer::Backend::IShaderProgram* GetShader(int pass = 0) const;
73 Renderer::Backend::IGraphicsPipelineState*
74 GetGraphicsPipelineState(int pass = 0) const;
76 /**
77 * Whether this technique uses alpha blending that requires objects to be
78 * drawn from furthest to nearest.
80 bool GetSortByDistance() const;
82 void SetSortByDistance(bool enable);
84 const VfsPath& GetPath() { return m_Path; }
86 const CShaderDefines& GetShaderDefines() { return m_Defines; }
88 const PipelineStateDescCallback& GetPipelineStateDescCallback() const { return m_PipelineStateDescCallback; };
90 private:
91 std::vector<CShaderPass> m_Passes;
93 bool m_SortByDistance = false;
95 // We need additional data to reload the technique.
96 VfsPath m_Path;
97 CShaderDefines m_Defines;
99 PipelineStateDescCallback m_PipelineStateDescCallback;
102 #endif // INCLUDED_SHADERTECHNIQUE