Skip to content

Commit

Permalink
ShaderSourceFactoryUtils: added MemoryShaderSourceFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Oct 6, 2023
1 parent e9d856b commit fb3a0b0
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 2 deletions.
71 changes: 69 additions & 2 deletions Graphics/GraphicsTools/interface/ShaderSourceFactoryUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ struct ShaderSourceFileSubstitueInfo
{}
#endif
};
typedef struct ShaderSourceFileSubstitueInfo ShaderSourceFileSubstitueInfo;


/// Compound shader source factory create info.
struct CompoundShaderSourceFactoryCreateInfo
Expand Down Expand Up @@ -86,7 +88,7 @@ struct CompoundShaderSourceFactoryCreateInfo
#endif
};
typedef struct CompoundShaderSourceFactoryCreateInfo CompoundShaderSourceFactoryCreateInfo;
// clang-format on


/// Creates a compound shader source factory.
///
Expand All @@ -100,7 +102,72 @@ typedef struct CompoundShaderSourceFactoryCreateInfo CompoundShaderSourceFactory
///
/// The factory also allows substituting source file names. This is useful when the same shader source
/// is used for multiple shaders, but some of them require a modified version of the source.
void DILIGENT_GLOBAL_FUNCTION(CreateCompoundShaderSourceFactory)(const CompoundShaderSourceFactoryCreateInfo REF CreateInfo, IShaderSourceInputStreamFactory** ppFactory);
void DILIGENT_GLOBAL_FUNCTION(CreateCompoundShaderSourceFactory)(const CompoundShaderSourceFactoryCreateInfo REF CreateInfo,
IShaderSourceInputStreamFactory** ppFactory);


/// Shader source file info.
struct MemoryShaderSourceFileInfo
{
/// File name.
const Char* Name DEFAULT_INITIALIZER(nullptr);

/// Shader source.
const Char* pData DEFAULT_INITIALIZER(nullptr);

/// Shader source length. If 0, the length will be calculated automatically
/// assuming that the source is null-terminated.
Uint32 Lenth DEFAULT_INITIALIZER(0);

Check failure on line 120 in Graphics/GraphicsTools/interface/ShaderSourceFactoryUtils.h

View workflow job for this annotation

GitHub Actions / Linux -> Pre-checks

Lenth ==> Length

#if DILIGENT_CPP_INTERFACE
constexpr MemoryShaderSourceFileInfo() noexcept
{}

constexpr MemoryShaderSourceFileInfo(const Char* _Name,
const Char* _pData,
Uint32 _Length = 0) noexcept :
Name{_Name},
pData{_pData},
Lenth{_Length}

Check failure on line 131 in Graphics/GraphicsTools/interface/ShaderSourceFactoryUtils.h

View workflow job for this annotation

GitHub Actions / Linux -> Pre-checks

Lenth ==> Length
{}
#endif
};
typedef struct MemoryShaderSourceFileInfo MemoryShaderSourceFileInfo;


/// Memory shader source factory create info.
struct MemoryShaderSourceFactoryCreateInfo
{
/// An array of shader source files.
MemoryShaderSourceFileInfo* pSources DEFAULT_INITIALIZER(nullptr);

/// The number of files in pSources array.
Uint32 NumSources DEFAULT_INITIALIZER(0);

/// Whether to copy shader sources. If false, the factory will assume that
/// the source data will remain valid for the lifetime of the factory.
Bool CopySources DEFAULT_INITIALIZER(False);

#if DILIGENT_CPP_INTERFACE
constexpr MemoryShaderSourceFactoryCreateInfo() noexcept
{}

constexpr MemoryShaderSourceFactoryCreateInfo(MemoryShaderSourceFileInfo* _pSources,
Uint32 _NumSources) noexcept :
pSources{_pSources},
NumSources{_NumSources}
{}
#endif
};
typedef struct MemoryShaderSourceFactoryCreateInfo MemoryShaderSourceFactoryCreateInfo;

/// Crates a memory shader source factory.
///
/// \param [in] CreateInfo - Memory shader source factory create info, see Diligent::MemoryShaderSourceFactoryCreateInfo.
/// \param [out] ppFactory - Address of the memory location where the pointer to the created factory will be written.
void DILIGENT_GLOBAL_FUNCTION(CreateMemoryShaderSourceFactory)(const MemoryShaderSourceFactoryCreateInfo REF CreateInfo,
IShaderSourceInputStreamFactory** ppFactory);


#include "../../Primitives/interface/UndefGlobalFuncHelperMacros.h"

Expand Down
92 changes: 92 additions & 0 deletions Graphics/GraphicsTools/src/ShaderSourceFactoryUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@

#include <vector>
#include <unordered_map>
#include <string>

#include "ObjectBase.hpp"
#include "HashUtils.hpp"
#include "RefCntAutoPtr.hpp"
#include "StringDataBlobImpl.hpp"
#include "MemoryFileStream.hpp"

namespace Diligent
{
Expand Down Expand Up @@ -112,6 +115,89 @@ void CreateCompoundShaderSourceFactory(const CompoundShaderSourceFactoryCreateIn
pFactory->QueryInterface(IID_IShaderSourceInputStreamFactory, reinterpret_cast<IObject**>(ppFactory));
}




class MemoryShaderSourceFactory final : public ObjectBase<IShaderSourceInputStreamFactory>
{
public:
using TBase = ObjectBase<IShaderSourceInputStreamFactory>;

static RefCntAutoPtr<IShaderSourceInputStreamFactory> Create(const MemoryShaderSourceFactoryCreateInfo& CreateInfo)
{
return RefCntAutoPtr<IShaderSourceInputStreamFactory>{MakeNewRCObj<MemoryShaderSourceFactory>()(CreateInfo)};
}

MemoryShaderSourceFactory(IReferenceCounters* pRefCounters,
const MemoryShaderSourceFactoryCreateInfo& CI) :
TBase{pRefCounters}
{
if (CI.CopySources)
{
m_Sources.resize(CI.NumSources);
for (Uint32 i = 0; i < CI.NumSources; ++i)
{
const auto& Source = CI.pSources[i];
if (Source.Lenth > 0)

Check failure on line 141 in Graphics/GraphicsTools/src/ShaderSourceFactoryUtils.cpp

View workflow job for this annotation

GitHub Actions / Linux -> Pre-checks

Lenth ==> Length
{
m_Sources[i].assign(Source.pData, Source.Lenth);

Check failure on line 143 in Graphics/GraphicsTools/src/ShaderSourceFactoryUtils.cpp

View workflow job for this annotation

GitHub Actions / Linux -> Pre-checks

Lenth ==> Length
}
else
{
m_Sources[i] = Source.pData;
}
}
}

for (Uint32 i = 0; i < CI.NumSources; ++i)
{
const auto& Source = CI.pSources[i];
m_NameToSourceMap.emplace(HashMapStringKey{Source.Name, true}, CI.CopySources ? m_Sources[i].c_str() : Source.pData);
}
}

virtual void DILIGENT_CALL_TYPE CreateInputStream(const Char* Name, IFileStream** ppStream) override final
{
CreateInputStream2(Name, CREATE_SHADER_SOURCE_INPUT_STREAM_FLAG_NONE, ppStream);
}

virtual void DILIGENT_CALL_TYPE CreateInputStream2(const Char* Name,
CREATE_SHADER_SOURCE_INPUT_STREAM_FLAGS Flags,
IFileStream** ppStream) override final
{
auto SourceIt = m_NameToSourceMap.find(Name);
if (SourceIt != m_NameToSourceMap.end())
{
RefCntAutoPtr<StringDataBlobImpl> pDataBlob{MakeNewRCObj<StringDataBlobImpl>()(SourceIt->second)};
RefCntAutoPtr<MemoryFileStream> pMemStream{MakeNewRCObj<MemoryFileStream>()(pDataBlob)};

pMemStream->QueryInterface(IID_FileStream, reinterpret_cast<IObject**>(ppStream));
}
else
{
*ppStream = nullptr;
if ((Flags & CREATE_SHADER_SOURCE_INPUT_STREAM_FLAG_SILENT) == 0)
{
LOG_ERROR("Failed to create input stream for source file ", Name);
}
}
}

IMPLEMENT_QUERY_INTERFACE_IN_PLACE(IID_IShaderSourceInputStreamFactory, TBase)

private:
std::vector<std::string> m_Sources;

std::unordered_map<HashMapStringKey, const Char*> m_NameToSourceMap;
};

void CreateMemoryShaderSourceFactory(const MemoryShaderSourceFactoryCreateInfo& CreateInfo, IShaderSourceInputStreamFactory** ppFactory)
{
auto pFactory = MemoryShaderSourceFactory::Create(CreateInfo);
pFactory->QueryInterface(IID_IShaderSourceInputStreamFactory, reinterpret_cast<IObject**>(ppFactory));
}


} // namespace Diligent

extern "C"
Expand All @@ -121,4 +207,10 @@ extern "C"
{
Diligent::CreateCompoundShaderSourceFactory(CreateInfo, ppFactory);
}

void Diligent_CreateMemoryShaderSourceFactory(const Diligent::MemoryShaderSourceFactoryCreateInfo& CreateInfo,
Diligent::IShaderSourceInputStreamFactory** ppFactory)
{
Diligent::CreateMemoryShaderSourceFactory(CreateInfo, ppFactory);
}
}

0 comments on commit fb3a0b0

Please sign in to comment.