LCOV - code coverage report
Current view: top level - source/graphics - ShaderDefines.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 10 14 71.4 %
Date: 2026-12-03 01:19:39 Functions: 0 8 0.0 %

          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_SHADERDEFINES
      19             : #define INCLUDED_SHADERDEFINES
      20             : 
      21             : #include "maths/Vector4D.h"
      22             : #include "ps/CStrIntern.h"
      23             : #include "ps/containers/StaticVector.h"
      24             : 
      25             : #include <cstddef>
      26             : #include <map>
      27             : #include <memory>
      28             : #include <unordered_map>
      29             : #include <utility>
      30             : #include <vector>
      31             : 
      32             : namespace Renderer::Backend { class IDeviceCommandContext; }
      33             : namespace Renderer::Backend { class IShaderProgram; }
      34             : 
      35             : /**
      36             :  * Represents a mapping of name strings to value, for use with
      37             :  * CShaderDefines (values are strings) and CShaderUniforms (values are vec4s).
      38             :  *
      39             :  * Stored as interned vectors of name-value pairs, to support high performance
      40             :  * comparison operators.
      41             :  *
      42             :  * Not thread-safe - must only be used from the main thread.
      43             :  */
      44             : template<typename value_t>
      45             : class CShaderParams
      46             : {
      47             : public:
      48             :     /**
      49             :      * Create an empty map of defines.
      50             :      */
      51             :     CShaderParams();
      52             : 
      53             :     /**
      54             :      * Add a name and associated value to the map of parameters.
      55             :      * If the name is already defined, its value will be replaced.
      56             :      */
      57             :     void Set(CStrIntern name, const value_t& value);
      58             : 
      59             :     /**
      60             :      * Add all the names and values from another set of parameters.
      61             :      * If any name is already defined in this object, its value will be replaced.
      62             :      */
      63             :     void SetMany(const CShaderParams& params);
      64             : 
      65             :     /**
      66             :      * Return a copy of the current name/value mapping.
      67             :      */
      68             :     std::map<CStrIntern, value_t> GetMap() const;
      69             : 
      70             :     /**
      71             :      * Return a hash of the current mapping.
      72             :      */
      73             :     size_t GetHash() const;
      74             : 
      75             :     /**
      76             :      * Compare with some arbitrary total order.
      77             :      * The order may be different each time the application is run
      78             :      * (it is based on interned memory addresses).
      79             :      */
      80           2 :     bool operator<(const CShaderParams& b) const
      81             :     {
      82           2 :         return m_Items < b.m_Items;
      83             :     }
      84             : 
      85             :     /**
      86             :      * Fast equality comparison.
      87             :      */
      88          11 :     bool operator==(const CShaderParams& b) const
      89             :     {
      90          11 :         return m_Items == b.m_Items;
      91             :     }
      92             : 
      93             :     /**
      94             :      * Fast inequality comparison.
      95             :      */
      96           0 :     bool operator!=(const CShaderParams& b) const
      97             :     {
      98           0 :         return m_Items != b.m_Items;
      99             :     }
     100             : 
     101         475 :     struct SItems
     102             :     {
     103             :         // Name/value pair
     104             :         using Item = std::pair<CStrIntern, value_t>;
     105             : 
     106             :         // Sorted by name; no duplicated names. We can use the StaticVector
     107             :         // because we shouldn't have too many shader parameters of a single
     108             :         // type.
     109             :         using ItemsContainers = PS::StaticVector<Item, 32>;
     110             :         ItemsContainers items;
     111             : 
     112             :         size_t hash;
     113             : 
     114             :         void RecalcHash();
     115             : 
     116             :         static bool NameLess(const Item& a, const Item& b);
     117             :     };
     118             : 
     119             :     struct SItemsHash
     120             :     {
     121          89 :         std::size_t operator()(const SItems& items) const
     122             :         {
     123          89 :             return items.hash;
     124             :         }
     125             :     };
     126             : 
     127             : protected:
     128             :     SItems* m_Items; // interned value
     129             : 
     130             : private:
     131             :     using InternedItems_t = std::unordered_map<SItems, std::shared_ptr<SItems>, SItemsHash>;
     132             :     static InternedItems_t s_InternedItems;
     133             : 
     134             :     /**
     135             :      * Returns a pointer to an SItems equal to @p items.
     136             :      * The pointer will be valid forever, and the same pointer will be returned
     137             :      * for any subsequent requests for an equal items list.
     138             :      */
     139             :     static SItems* GetInterned(const SItems& items);
     140             : 
     141             :     CShaderParams(SItems* items);
     142             :     static CShaderParams CreateEmpty();
     143             :     static CShaderParams s_Empty;
     144             : };
     145             : 
     146             : /**
     147             :  * Represents a mapping of name strings to value strings, for use with
     148             :  * \#if and \#ifdef and similar conditionals in shaders.
     149             :  *
     150             :  * Not thread-safe - must only be used from the main thread.
     151             :  */
     152          54 : class CShaderDefines : public CShaderParams<CStrIntern>
     153             : {
     154             : public:
     155             :     /**
     156             :      * Add a name and associated value to the map of defines.
     157             :      * If the name is already defined, its value will be replaced.
     158             :      */
     159             :     void Add(CStrIntern name, CStrIntern value);
     160             : 
     161             :     /**
     162             :      * Return the value for the given name as an integer, or 0 if not defined.
     163             :      */
     164             :     int GetInt(const char* name) const;
     165             : };
     166             : 
     167             : /**
     168             :  * Represents a mapping of name strings to value CVector4Ds, for use with
     169             :  * uniforms in shaders.
     170             :  *
     171             :  * Not thread-safe - must only be used from the main thread.
     172             :  */
     173           4 : class CShaderUniforms : public CShaderParams<CVector4D>
     174             : {
     175             : public:
     176             :     /**
     177             :      * Add a name and associated value to the map of uniforms.
     178             :      * If the name is already defined, its value will be replaced.
     179             :      */
     180             :     void Add(const char* name, const CVector4D& value);
     181             : 
     182             :     /**
     183             :      * Return the value for the given name, or (0,0,0,0) if not defined.
     184             :      */
     185             :     CVector4D GetVector(const char* name) const;
     186             : 
     187             :     /**
     188             :      * Bind the collection of uniforms onto the given shader.
     189             :      */
     190             :     void BindUniforms(
     191             :         Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
     192             :         Renderer::Backend::IShaderProgram* shader) const;
     193             : };
     194             : 
     195             : // Add here the types of queries we can make in the renderer
     196             : enum RENDER_QUERIES
     197             : {
     198             :     RQUERY_TIME,
     199             :     RQUERY_WATER_TEX,
     200             :     RQUERY_SKY_CUBE
     201             : };
     202             : 
     203             : /**
     204             :  * Uniform values that need to be evaluated in the renderer.
     205             :  *
     206             :  * Not thread-safe - must only be used from the main thread.
     207             :  */
     208           3 : class CShaderRenderQueries
     209             : {
     210             : public:
     211             :     using RenderQuery = std::pair<int, CStrIntern>;
     212             : 
     213             :     void Add(const char* name);
     214           0 :     size_t GetSize() const { return m_Items.size(); }
     215           0 :     RenderQuery GetItem(size_t i) const { return m_Items[i]; }
     216             : private:
     217             :     std::vector<RenderQuery> m_Items;
     218             : };
     219             : 
     220             : #endif // INCLUDED_SHADERDEFINES

Generated by: LCOV version 1.16