From 02cccd27a506ae49fe6f9985383fad22dcef907f Mon Sep 17 00:00:00 2001 From: assiduous Date: Thu, 5 Oct 2023 23:32:59 -0700 Subject: [PATCH] FX Shader Factory: use Shader Source Factory Utils --- CMakeLists.txt | 8 +-- .../DiligentFXShaderSourceStreamFactory.hpp | 53 ++---------------- .../DiligentFXShaderSourceStreamFactory.cpp | 56 +++---------------- shaders_inc/shaders_list.h | 8 +-- 4 files changed, 16 insertions(+), 109 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 01f79a9c..b9d13ccc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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" "{" ) diff --git a/Utilities/include/DiligentFXShaderSourceStreamFactory.hpp b/Utilities/include/DiligentFXShaderSourceStreamFactory.hpp index 0a6f949b..c2b98a80 100644 --- a/Utilities/include/DiligentFXShaderSourceStreamFactory.hpp +++ b/Utilities/include/DiligentFXShaderSourceStreamFactory.hpp @@ -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"); @@ -27,64 +27,21 @@ #pragma once -#include -#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(static_cast(&m_RefCounters)); - } + static IShaderSourceInputStreamFactory& GetInstance(); private: DiligentFXShaderSourceStreamFactory(); - using NameToSourceMapType = std::unordered_map; - static NameToSourceMapType InitNameToSourceMap(); - -private: - DummyReferenceCounters m_RefCounters; - - const NameToSourceMapType m_NameToSourceMap; + RefCntAutoPtr m_pFactory; }; } // namespace Diligent diff --git a/Utilities/src/DiligentFXShaderSourceStreamFactory.cpp b/Utilities/src/DiligentFXShaderSourceStreamFactory.cpp index f45997fc..88e222ba 100644 --- a/Utilities/src/DiligentFXShaderSourceStreamFactory.cpp +++ b/Utilities/src/DiligentFXShaderSourceStreamFactory.cpp @@ -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"); @@ -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 pDataBlob(MakeNewRCObj()(SourceIt->second)); - RefCntAutoPtr pMemStream(MakeNewRCObj()(pDataBlob)); - - pMemStream->QueryInterface(IID_FileStream, reinterpret_cast(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 diff --git a/shaders_inc/shaders_list.h b/shaders_inc/shaders_list.h index b4b2662e..142e3795 100644 --- a/shaders_inc/shaders_list.h +++ b/shaders_inc/shaders_list.h @@ -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",