Line data Source code
1 : /* Copyright (C) 2026 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_MATERIAL 19 : #define INCLUDED_MATERIAL 20 : 21 : #include "graphics/ShaderDefines.h" 22 : #include "graphics/Texture.h" 23 : #include "ps/CStr.h" 24 : #include "ps/CStrIntern.h" 25 : 26 : #include <array> 27 : #include <vector> 28 : 29 : class CVector4D; 30 : 31 : class CMaterial 32 : { 33 : public: 34 : enum class Pass : uint8_t 35 : { 36 : MAIN, 37 : SHADOW_CASTER, 38 : REFLECTIONS, 39 : REFRACTIONS, 40 : SILHOUETTE_OCCLUDER, 41 : SILHOUETTE_CASTER, 42 : WIREFRAME, 43 : WIREFRAME_SOLID, 44 : COUNT 45 : }; 46 : 47 0 : struct TextureSampler 48 : { 49 0 : TextureSampler(const CStr &n, CTexturePtr t) : Name(n), Sampler(t) {} 50 0 : TextureSampler(const CStrIntern &n, CTexturePtr t) : Name(n), Sampler(t) {} 51 : 52 : CStrIntern Name; 53 : CTexturePtr Sampler; 54 : }; 55 : 56 : struct RequiredSampler 57 : { 58 : CStrIntern name; 59 : bool sRGB; 60 : }; 61 : 62 : typedef std::vector<TextureSampler> SamplersVector; 63 : 64 : CMaterial(); 65 : 66 : // Whether this material's shaders use alpha blending, in which case 67 : // models using this material need to be rendered in a special order 68 : // relative to the alpha-blended water plane 69 0 : void SetUsesAlphaBlending(bool flag) { m_AlphaBlending = flag; } 70 0 : bool UsesAlphaBlending() const { return m_AlphaBlending; } 71 : 72 0 : const CTexturePtr& GetDiffuseTexture() const { return m_DiffuseTexture; } 73 : 74 : struct Shader 75 : { 76 : CStrIntern effect; 77 : CShaderDefines defines; 78 : }; 79 : void SetShader(const Pass pass, Shader shaderEffect); 80 : const Shader& GetShader(const Pass pass) const; 81 : 82 : void AddShaderDefine(const Pass pass, CStrIntern key, CStrIntern value); 83 : 84 : void AddStaticUniform(const char* key, const CVector4D& value); 85 0 : const CShaderUniforms& GetStaticUniforms() const { return m_StaticUniforms; } 86 : 87 : void AddSampler(const TextureSampler& texture); 88 0 : const SamplersVector& GetSamplers() const { return m_Samplers; } 89 : 90 : void AddRenderQuery(const char* key); 91 : const CShaderRenderQueries& GetRenderQueries() const { return m_RenderQueries; } 92 : 93 : void AddRequiredSampler(RequiredSampler sampler); 94 1 : const std::vector<RequiredSampler>& GetRequiredSamplers() const { return m_RequiredSamplers; } 95 : 96 : private: 97 : 98 : // This pointer is kept to make it easier for the fixed pipeline to 99 : // access the only texture it's interested in. 100 : CTexturePtr m_DiffuseTexture; 101 : 102 : SamplersVector m_Samplers; 103 : std::vector<RequiredSampler> m_RequiredSamplers; 104 : 105 : std::array<Shader, static_cast<size_t>(Pass::COUNT)> m_Shaders; 106 : CShaderUniforms m_StaticUniforms; 107 : CShaderRenderQueries m_RenderQueries; 108 : 109 : bool m_AlphaBlending; 110 : }; 111 : 112 : #endif // INCLUDED_MATERIAL