Diligent Engine  v.2.4.g
ShaderMacroHelper.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2021 Diligent Graphics LLC
3  * Copyright 2015-2019 Egor Yusov
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * In no event and under no legal theory, whether in tort (including negligence),
18  * contract, or otherwise, unless required by applicable law (such as deliberate
19  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
20  * liable for any damages, including any direct, indirect, special, incidental,
21  * or consequential damages of any character arising as a result of this License or
22  * out of the use or inability to use the software (including but not limited to damages
23  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
24  * all other commercial damages or losses), even if such Contributor has been advised
25  * of the possibility of such damages.
26  */
27 
28 //--------------------------------------------------------------------------------------
29 // Copyright 2013 Intel Corporation
30 // All Rights Reserved
31 //
32 // Permission is granted to use, copy, distribute and prepare derivative works of this
33 // software for any purpose and without fee, provided, that the above copyright notice
34 // and this statement appear in all copies. Intel makes no representations about the
35 // suitability of this software for any purpose. THIS SOFTWARE IS PROVIDED "AS IS."
36 // INTEL SPECIFICALLY DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, AND ALL LIABILITY,
37 // INCLUDING CONSEQUENTIAL AND OTHER INDIRECT DAMAGES, FOR THE USE OF THIS SOFTWARE,
38 // INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PROPRIETARY RIGHTS, AND INCLUDING THE
39 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Intel does not
40 // assume any responsibility for any errors which may appear in this software nor any
41 // responsibility to update it.
42 //--------------------------------------------------------------------------------------
43 #pragma once
44 
45 #include <set>
46 #include <vector>
47 #include <sstream>
48 #include <iomanip>
49 #include "../../GraphicsEngine/interface/Shader.h"
50 #include "../../../Platforms/Basic/interface/DebugUtilities.hpp"
51 
52 
53 namespace Diligent
54 {
55 
57 {
58 public:
59  template <typename DefintionType>
60  void AddShaderMacro(const Char* Name, DefintionType Definition)
61  {
62 #if DILIGENT_DEBUG
63  for (size_t i = 0; i < m_Macros.size() && m_Macros[i].Definition != nullptr; ++i)
64  {
65  if (strcmp(m_Macros[i].Name, Name) == 0)
66  {
67  UNEXPECTED("Macro '", Name, "' already exists. Use UpdateMacro() to update the macro value.");
68  }
69  }
70 #endif
71  std::ostringstream ss;
72  ss << Definition;
73  AddShaderMacro<const Char*>(Name, ss.str().c_str());
74  }
75 
76  ShaderMacroHelper() = default;
77 
78  // NB: string pointers in m_Macros may become invalid after the
79  // copy or move due to short string optimization in std::string
80  ShaderMacroHelper(const ShaderMacroHelper&) = delete;
84 
85  void Finalize()
86  {
87  if (!m_bIsFinalized)
88  {
89  m_Macros.emplace_back(nullptr, nullptr);
90  m_bIsFinalized = true;
91  }
92  }
93 
94  void Reopen()
95  {
96  if (m_bIsFinalized)
97  {
98  VERIFY_EXPR(m_Macros.size() > 0 && m_Macros.back().Name == nullptr && m_Macros.back().Definition == nullptr);
99  m_Macros.pop_back();
100  m_bIsFinalized = false;
101  }
102  }
103 
104  void Clear()
105  {
106  m_Macros.clear();
107  m_StringPool.clear();
108  m_bIsFinalized = false;
109  }
110 
111  operator const ShaderMacro*()
112  {
113  if (m_Macros.size() > 0 && !m_bIsFinalized)
114  Finalize();
115  return m_Macros.size() ? m_Macros.data() : nullptr;
116  }
117 
118  void RemoveMacro(const Char* Name)
119  {
120  size_t i = 0;
121  while (i < m_Macros.size() && m_Macros[i].Definition != nullptr)
122  {
123  if (strcmp(m_Macros[i].Name, Name) == 0)
124  {
125  m_Macros.erase(m_Macros.begin() + i);
126  break;
127  }
128  else
129  {
130  ++i;
131  }
132  }
133  }
134 
135  template <typename DefintionType>
136  void UpdateMacro(const Char* Name, DefintionType Definition)
137  {
138  RemoveMacro(Name);
139  AddShaderMacro(Name, Definition);
140  }
141 
142 private:
143  std::vector<ShaderMacro> m_Macros;
144  std::set<std::string> m_StringPool;
145  bool m_bIsFinalized = false;
146 };
147 
148 template <>
149 inline void ShaderMacroHelper::AddShaderMacro(const Char* Name, const Char* Definition)
150 {
151  Reopen();
152  const auto* PooledDefinition = m_StringPool.insert(Definition).first->c_str();
153  const auto* PooledName = m_StringPool.insert(Name).first->c_str();
154  m_Macros.emplace_back(PooledName, PooledDefinition);
155 }
156 
157 template <>
158 inline void ShaderMacroHelper::AddShaderMacro(const Char* Name, bool Definition)
159 {
160  AddShaderMacro<const Char*>(Name, Definition ? "1" : "0");
161 }
162 
163 template <>
164 inline void ShaderMacroHelper::AddShaderMacro(const Char* Name, float Definition)
165 {
166  std::ostringstream ss;
167 
168  // Make sure that when floating point represents integer, it is still
169  // written as float: 1024.0, but not 1024. This is essnetial to
170  // avoid type conversion issues in GLES.
171  if (Definition == static_cast<float>(static_cast<int>(Definition)))
172  ss << std::fixed << std::setprecision(1);
173 
174  ss << Definition;
175  AddShaderMacro<const Char*>(Name, ss.str().c_str());
176 }
177 
178 template <>
179 inline void ShaderMacroHelper::AddShaderMacro(const Char* Name, Uint32 Definition)
180 {
181  // Make sure that uint constants have the 'u' suffix to avoid problems in GLES.
182  std::ostringstream ss;
183  ss << Definition << 'u';
184  AddShaderMacro<const Char*>(Name, ss.str().c_str());
185 }
186 
187 template <>
188 inline void ShaderMacroHelper::AddShaderMacro(const Char* Name, Uint8 Definition)
189 {
190  AddShaderMacro(Name, Uint32{Definition});
191 }
192 
193 #define ADD_SHADER_MACRO_ENUM_VALUE(Helper, EnumValue) Helper.AddShaderMacro(#EnumValue, static_cast<int>(EnumValue));
194 
195 } // namespace Diligent
Diligent::ShaderMacroHelper::operator=
ShaderMacroHelper & operator=(const ShaderMacroHelper &)=delete
Diligent::Char
char Char
Definition: BasicTypes.h:64
Diligent::ShaderMacroHelper::ShaderMacroHelper
ShaderMacroHelper()=default
Diligent::ShaderMacroHelper::Reopen
void Reopen()
Definition: ShaderMacroHelper.hpp:94
UNEXPECTED
#define UNEXPECTED(...)
Definition: DebugUtilities.hpp:77
Diligent::ShaderMacro
Definition: Shader.h:159
Diligent::ShaderMacroHelper::UpdateMacro
void UpdateMacro(const Char *Name, DefintionType Definition)
Definition: ShaderMacroHelper.hpp:136
Diligent::ShaderMacroHelper
Definition: ShaderMacroHelper.hpp:56
Diligent::ShaderMacroHelper::Finalize
void Finalize()
Definition: ShaderMacroHelper.hpp:85
Diligent::Uint32
uint32_t Uint32
32-bit unsigned integer
Definition: BasicTypes.h:51
Diligent::ShaderMacroHelper::RemoveMacro
void RemoveMacro(const Char *Name)
Definition: ShaderMacroHelper.hpp:118
Diligent::Uint8
uint8_t Uint8
8-bit unsigned integer
Definition: BasicTypes.h:53
VERIFY_EXPR
#define VERIFY_EXPR(...)
Definition: DebugUtilities.hpp:79
Diligent::ShaderMacroHelper::Clear
void Clear()
Definition: ShaderMacroHelper.hpp:104
Diligent::ShaderMacroHelper::AddShaderMacro
void AddShaderMacro(const Char *Name, DefintionType Definition)
Definition: ShaderMacroHelper.hpp:60
Diligent
The library uses Direct3D-style math:
Definition: AdvancedMath.hpp:37