Skip to content

Commit

Permalink
Updated ShaderMacroHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Sep 14, 2023
1 parent 22b4953 commit bc9c75c
Show file tree
Hide file tree
Showing 2 changed files with 395 additions and 22 deletions.
93 changes: 71 additions & 22 deletions Graphics/GraphicsTools/interface/ShaderMacroHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,6 @@
* of the possibility of such damages.
*/

//--------------------------------------------------------------------------------------
// Copyright 2013 Intel Corporation
// All Rights Reserved
//
// Permission is granted to use, copy, distribute and prepare derivative works of this
// software for any purpose and without fee, provided, that the above copyright notice
// and this statement appear in all copies. Intel makes no representations about the
// suitability of this software for any purpose. THIS SOFTWARE IS PROVIDED "AS IS."
// INTEL SPECIFICALLY DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, AND ALL LIABILITY,
// INCLUDING CONSEQUENTIAL AND OTHER INDIRECT DAMAGES, FOR THE USE OF THIS SOFTWARE,
// INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PROPRIETARY RIGHTS, AND INCLUDING THE
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Intel does not
// assume any responsibility for any errors which may appear in this software nor any
// responsibility to update it.
//--------------------------------------------------------------------------------------
#pragma once

#include <set>
Expand All @@ -49,7 +34,6 @@
#include "../../GraphicsEngine/interface/Shader.h"
#include "../../../Platforms/Basic/interface/DebugUtilities.hpp"


namespace Diligent
{

Expand All @@ -64,7 +48,7 @@ class ShaderMacroHelper
{
if (strcmp(m_Macros[i].Name, Name) == 0)
{
UNEXPECTED("Macro '", Name, "' already exists. Use UpdateMacro() to update the macro value.");
UNEXPECTED("Macro '", Name, "' already exists. Use Update() to update the macro value.");
}
}
#endif
Expand All @@ -73,6 +57,12 @@ class ShaderMacroHelper
return AddShaderMacro<const Char*>(Name, ss.str().c_str());
}

template <typename DefinitionType>
ShaderMacroHelper& Add(const Char* Name, DefinitionType Definition)
{
return AddShaderMacro(Name, Definition);
}

ShaderMacroHelper() = default;

// NB: we need to make sure that string pointers in m_Macros don't become invalid
Expand Down Expand Up @@ -112,7 +102,7 @@ class ShaderMacroHelper
};
}

void RemoveMacro(const Char* Name)
ShaderMacroHelper& RemoveMacro(const Char* Name)
{
size_t i = 0;
while (i < m_Macros.size() && m_Macros[i].Definition != nullptr)
Expand All @@ -127,6 +117,13 @@ class ShaderMacroHelper
++i;
}
}

return *this;
}

ShaderMacroHelper& Remove(const Char* Name)
{
return RemoveMacro(Name);
}

template <typename DefinitionType>
Expand All @@ -136,6 +133,49 @@ class ShaderMacroHelper
return AddShaderMacro(Name, Definition);
}

template <typename DefinitionType>
ShaderMacroHelper& Update(const Char* Name, DefinitionType Definition)
{
return UpdateMacro(Name, Definition);
}

ShaderMacroHelper& Add(const ShaderMacro& Macro)
{
const auto* PooledDefinition = m_StringPool.insert(Macro.Definition).first->c_str();
const auto* PooledName = m_StringPool.insert(Macro.Name).first->c_str();
m_Macros.emplace_back(PooledName, PooledDefinition);
return *this;
}

ShaderMacroHelper& operator+=(const ShaderMacroHelper& Macros)
{
for (const auto& Macro : Macros.m_Macros)
Add(Macro);

return *this;
}

ShaderMacroHelper& operator+=(const ShaderMacro& Macro)
{
return Add(Macro);
}

ShaderMacroHelper& operator+=(const ShaderMacroArray& Macros)
{
for (Uint32 i = 0; i < Macros.Count; ++i)
Add(Macros[i]);

return *this;
}

template <typename T>
ShaderMacroHelper operator+(const T& Macros) const
{
ShaderMacroHelper NewMacros{*this};
NewMacros += Macros;
return NewMacros;
}

private:
std::vector<ShaderMacro> m_Macros;
std::set<std::string> m_StringPool;
Expand All @@ -144,10 +184,7 @@ class ShaderMacroHelper
template <>
inline ShaderMacroHelper& ShaderMacroHelper::AddShaderMacro(const Char* Name, const Char* Definition)
{
const auto* PooledDefinition = m_StringPool.insert(Definition).first->c_str();
const auto* PooledName = m_StringPool.insert(Name).first->c_str();
m_Macros.emplace_back(PooledName, PooledDefinition);
return *this;
return Add(ShaderMacro{Name, Definition});
}

template <>
Expand Down Expand Up @@ -180,12 +217,24 @@ inline ShaderMacroHelper& ShaderMacroHelper::AddShaderMacro(const Char* Name, Ui
return AddShaderMacro<const Char*>(Name, ss.str().c_str());
}

template <>
inline ShaderMacroHelper& ShaderMacroHelper::AddShaderMacro(const Char* Name, Uint16 Definition)
{
return AddShaderMacro(Name, Uint32{Definition});
}

template <>
inline ShaderMacroHelper& ShaderMacroHelper::AddShaderMacro(const Char* Name, Uint8 Definition)
{
return AddShaderMacro(Name, Uint32{Definition});
}

template <>
inline ShaderMacroHelper& ShaderMacroHelper::AddShaderMacro(const Char* Name, Int8 Definition)
{
return AddShaderMacro(Name, Int32{Definition});
}

#define ADD_SHADER_MACRO_ENUM_VALUE(Helper, EnumValue) Helper.AddShaderMacro(#EnumValue, static_cast<int>(EnumValue));

} // namespace Diligent
Loading

0 comments on commit bc9c75c

Please sign in to comment.