Diligent Engine API Reference
ShaderMacroHelper.h
1 /* Copyright 2015-2018 Egor Yusov
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
12  *
13  * In no event and under no legal theory, whether in tort (including negligence),
14  * contract, or otherwise, unless required by applicable law (such as deliberate
15  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
16  * liable for any damages, including any direct, indirect, special, incidental,
17  * or consequential damages of any character arising as a result of this License or
18  * out of the use or inability to use the software (including but not limited to damages
19  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
20  * all other commercial damages or losses), even if such Contributor has been advised
21  * of the possibility of such damages.
22  */
23 
24 //--------------------------------------------------------------------------------------
25 // Copyright 2013 Intel Corporation
26 // All Rights Reserved
27 //
28 // Permission is granted to use, copy, distribute and prepare derivative works of this
29 // software for any purpose and without fee, provided, that the above copyright notice
30 // and this statement appear in all copies. Intel makes no representations about the
31 // suitability of this software for any purpose. THIS SOFTWARE IS PROVIDED "AS IS."
32 // INTEL SPECIFICALLY DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, AND ALL LIABILITY,
33 // INCLUDING CONSEQUENTIAL AND OTHER INDIRECT DAMAGES, FOR THE USE OF THIS SOFTWARE,
34 // INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PROPRIETARY RIGHTS, AND INCLUDING THE
35 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Intel does not
36 // assume any responsibility for any errors which may appear in this software nor any
37 // responsibility to update it.
38 //--------------------------------------------------------------------------------------
39 #pragma once
40 
41 #include <set>
42 #include <vector>
43 #include <sstream>
44 #include <iomanip>
45 #include "BasicTypes.h"
46 #include "DebugUtilities.h"
47 #include "Shader.h"
48 
49 namespace Diligent
50 {
51 
52 class ShaderMacroHelper
53 {
54 public:
55 
56  template<typename DefintionType>
57  void AddShaderMacro( const Diligent::Char* Name, DefintionType Definition )
58  {
59  std::ostringstream ss;
60  ss << Definition;
61  AddShaderMacro<const Diligent::Char*>( Name, ss.str().c_str() );
62  }
63 
64  void Finalize()
65  {
66  if (!m_bIsFinalized)
67  {
68  m_Macros.emplace_back(nullptr, nullptr);
69  m_bIsFinalized = true;
70  }
71  }
72 
73  void Reopen()
74  {
75  if (m_bIsFinalized)
76  {
77  VERIFY_EXPR(m_Macros.size() > 0 && m_Macros.back().Name == nullptr && m_Macros.back().Definition == nullptr);
78  m_Macros.pop_back();
79  m_bIsFinalized = false;
80  }
81  }
82 
83  void Clear()
84  {
85  m_Macros.clear();
86  m_DefinitionsPool.clear();
87  m_bIsFinalized = false;
88  }
89 
90  operator const ShaderMacro* ()
91  {
92  if (m_Macros.size() > 0 && !m_bIsFinalized)
93  Finalize();
94  return m_Macros.size() ? m_Macros.data() : nullptr;
95  }
96 
97 private:
98 
99  std::vector< ShaderMacro > m_Macros;
100  std::set< std::string > m_DefinitionsPool;
101  bool m_bIsFinalized = false;
102 };
103 
104 template<>
105 inline void ShaderMacroHelper::AddShaderMacro( const Diligent::Char* Name, const Diligent::Char* Definition )
106 {
107  Reopen();
108  auto *PooledDefinition = m_DefinitionsPool.insert(Definition).first->c_str();
109  m_Macros.emplace_back(Name, PooledDefinition);
110 }
111 
112 template<>
113 inline void ShaderMacroHelper::AddShaderMacro( const Diligent::Char* Name, bool Definition )
114 {
115  AddShaderMacro( Name, Definition ? "1" : "0");
116 }
117 
118 template<>
119 inline void ShaderMacroHelper::AddShaderMacro( const Diligent::Char* Name, float Definition )
120 {
121  std::ostringstream ss;
122 
123  // Make sure that when floating point represents integer, it is still
124  // written as float: 1024.0, but not 1024. This is essnetial to
125  // avoid type conversion issues in GLES.
126  if( Definition == static_cast<float>(static_cast<int>(Definition)) )
127  ss << std::fixed << std::setprecision( 1 );
128 
129  ss << Definition;
130  AddShaderMacro<const Diligent::Char*>( Name, ss.str().c_str() );
131 }
132 
133 }
Graphics engine namespace.
Definition: AdaptiveFixedBlockAllocator.h:30