Skip to content

Commit

Permalink
FX Shader Factory: use Shader Source Factory Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Oct 6, 2023
1 parent 40fdf1b commit 02cccd2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 109 deletions.
8 changes: 1 addition & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,7 @@ if(NOT FILE2STRING_PATH STREQUAL "")

set(SHADERS_LIST_FILE shaders_inc/shaders_list.h)
file(WRITE ${SHADERS_LIST_FILE}
"struct ShaderIncInfo\n"
"{\n"
" const char* const FileName;\n"
" const char* const Source;\n"
"};\n"
"\n"
"static const ShaderIncInfo g_Shaders[] =\n"
"static const MemoryShaderSourceFileInfo g_Shaders[] =\n"
"{"
)

Expand Down
53 changes: 5 additions & 48 deletions Utilities/include/DiligentFXShaderSourceStreamFactory.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 Diligent Graphics LLC
* Copyright 2019-2023 Diligent Graphics LLC
* Copyright 2015-2019 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -27,64 +27,21 @@

#pragma once

#include <unordered_map>
#include "BasicFileStream.hpp"
#include "Shader.h"
#include "HashUtils.hpp"
#include "DummyReferenceCounters.hpp"
#include "RefCntAutoPtr.hpp"

namespace Diligent
{

class DiligentFXShaderSourceStreamFactory final : public IShaderSourceInputStreamFactory
class DiligentFXShaderSourceStreamFactory final
{
public:
static DiligentFXShaderSourceStreamFactory& GetInstance();

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

virtual void DILIGENT_CALL_TYPE CreateInputStream2(const Char* Name,
CREATE_SHADER_SOURCE_INPUT_STREAM_FLAGS Flags,
IFileStream** ppStream) override final;

virtual void DILIGENT_CALL_TYPE QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface) override final
{
if (ppInterface == nullptr)
return;

*ppInterface = nullptr;
if (IID == IID_Unknown || IID == IID_IShaderSourceInputStreamFactory)
{
*ppInterface = this;
(*ppInterface)->AddRef();
}
}

virtual ReferenceCounterValueType DILIGENT_CALL_TYPE AddRef() override final
{
return m_RefCounters.AddStrongRef();
}

virtual ReferenceCounterValueType DILIGENT_CALL_TYPE Release() override final
{
return m_RefCounters.ReleaseStrongRef();
}

virtual IReferenceCounters* DILIGENT_CALL_TYPE GetReferenceCounters() const override final
{
return const_cast<IReferenceCounters*>(static_cast<const IReferenceCounters*>(&m_RefCounters));
}
static IShaderSourceInputStreamFactory& GetInstance();

private:
DiligentFXShaderSourceStreamFactory();

using NameToSourceMapType = std::unordered_map<HashMapStringKey, const Char*>;
static NameToSourceMapType InitNameToSourceMap();

private:
DummyReferenceCounters<DiligentFXShaderSourceStreamFactory> m_RefCounters;

const NameToSourceMapType m_NameToSourceMap;
RefCntAutoPtr<IShaderSourceInputStreamFactory> m_pFactory;
};

} // namespace Diligent
56 changes: 9 additions & 47 deletions Utilities/src/DiligentFXShaderSourceStreamFactory.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 Diligent Graphics LLC
* Copyright 2019-2023 Diligent Graphics LLC
* Copyright 2015-2019 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -26,62 +26,24 @@
*/

#include "../include/DiligentFXShaderSourceStreamFactory.hpp"
#include "MemoryFileStream.hpp"
#include "StringDataBlobImpl.hpp"
#include "RefCntAutoPtr.hpp"
#include "../../../shaders_inc/shaders_list.h"
#include "ShaderSourceFactoryUtils.h"

namespace Diligent
{

DiligentFXShaderSourceStreamFactory::NameToSourceMapType DiligentFXShaderSourceStreamFactory::InitNameToSourceMap()
{
NameToSourceMapType NameToSourceMap;
for (size_t i = 0; i < _countof(g_Shaders); ++i)
{
NameToSourceMap.emplace(g_Shaders[i].FileName, g_Shaders[i].Source);
}
return NameToSourceMap;
}

DiligentFXShaderSourceStreamFactory& DiligentFXShaderSourceStreamFactory::GetInstance()
{
static DiligentFXShaderSourceStreamFactory TheFactory;
return TheFactory;
}
#include "../../../shaders_inc/shaders_list.h"

DiligentFXShaderSourceStreamFactory::DiligentFXShaderSourceStreamFactory() :
m_RefCounters{*this},
m_NameToSourceMap{InitNameToSourceMap()}
DiligentFXShaderSourceStreamFactory::DiligentFXShaderSourceStreamFactory()
{
}
MemoryShaderSourceFactoryCreateInfo CI{g_Shaders, _countof(g_Shaders), false};

void DiligentFXShaderSourceStreamFactory::CreateInputStream(const Char* Name,
IFileStream** ppStream)
{
CreateInputStream2(Name, CREATE_SHADER_SOURCE_INPUT_STREAM_FLAG_NONE, ppStream);
CreateMemoryShaderSourceFactory(CI, &m_pFactory);
}

void DiligentFXShaderSourceStreamFactory::CreateInputStream2(const Char* Name,
CREATE_SHADER_SOURCE_INPUT_STREAM_FLAGS Flags,
IFileStream** ppStream)
IShaderSourceInputStreamFactory& DiligentFXShaderSourceStreamFactory::GetInstance()
{
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);
}
}
static DiligentFXShaderSourceStreamFactory TheFactory;
return *TheFactory.m_pFactory;
}

} // namespace Diligent
8 changes: 1 addition & 7 deletions shaders_inc/shaders_list.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
struct ShaderIncInfo
{
const char* const FileName;
const char* const Source;
};

static const ShaderIncInfo g_Shaders[] =
static const MemoryShaderSourceFileInfo g_Shaders[] =
{
{
"EnvMap.psh",
Expand Down

0 comments on commit 02cccd2

Please sign in to comment.