Skip to content

Commit a5fa20c

Browse files
Added ShaderCreateInfoWrapper
1 parent 799fc52 commit a5fa20c

File tree

5 files changed

+172
-97
lines changed

5 files changed

+172
-97
lines changed

Graphics/Archiver/include/SerializedShaderImpl.hpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@
2626

2727
#pragma once
2828

29-
#include <memory>
3029
#include <array>
3130

3231
#include "SerializedShader.h"
3332
#include "SerializationEngineImplTraits.hpp"
34-
#include "ObjectBase.hpp"
33+
#include "ShaderBase.hpp"
3534
#include "RefCntAutoPtr.hpp"
3635
#include "STDAllocator.hpp"
3736
#include "Serializer.hpp"
@@ -57,7 +56,7 @@ class SerializedShaderImpl final : public ObjectBase<ISerializedShader>
5756

5857
virtual void DILIGENT_CALL_TYPE QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface) override final;
5958

60-
virtual const ShaderDesc& DILIGENT_CALL_TYPE GetDesc() const override final { return m_CreateInfo.Desc; }
59+
virtual const ShaderDesc& DILIGENT_CALL_TYPE GetDesc() const override final { return m_CreateInfo.Get().Desc; }
6160

6261
UNSUPPORTED_CONST_METHOD(Uint32, GetResourceCount)
6362
UNSUPPORTED_CONST_METHOD(void, GetResourceDesc, Uint32 Index, ShaderResourceDesc& ResourceDesc)
@@ -99,12 +98,8 @@ class SerializedShaderImpl final : public ObjectBase<ISerializedShader>
9998
}
10099

101100
private:
102-
void CopyShaderCreateInfo(const ShaderCreateInfo& ShaderCI) noexcept(false);
103-
104-
SerializationDeviceImpl* m_pDevice;
105-
RefCntAutoPtr<IShaderSourceInputStreamFactory> m_pShaderSourceFactory;
106-
ShaderCreateInfo m_CreateInfo;
107-
std::unique_ptr<void, STDDeleterRawMem<void>> m_pRawMemory;
101+
SerializationDeviceImpl* m_pDevice;
102+
ShaderCreateInfoWrapper m_CreateInfo;
108103

109104
std::array<std::unique_ptr<CompiledShader>, static_cast<size_t>(DeviceType::Count)> m_Shaders;
110105

Graphics/Archiver/src/SerializedShaderImpl.cpp

Lines changed: 3 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include <cstring>
3030

3131
#include "SerializationDeviceImpl.hpp"
32-
#include "FixedLinearAllocator.hpp"
3332
#include "EngineMemory.h"
3433
#include "DataBlobImpl.hpp"
3534
#include "PlatformMisc.hpp"
@@ -46,9 +45,7 @@ SerializedShaderImpl::SerializedShaderImpl(IReferenceCounters* pRefCounters
4645
const ShaderCreateInfo& ShaderCI,
4746
const ShaderArchiveInfo& ArchiveInfo) :
4847
TBase{pRefCounters},
49-
m_pDevice{pDevice},
50-
m_pShaderSourceFactory{ShaderCI.pShaderSourceStreamFactory},
51-
m_CreateInfo{ShaderCI}
48+
m_pDevice{pDevice}
5249
{
5350
if (ShaderCI.Desc.Name == nullptr || ShaderCI.Desc.Name[0] == '\0')
5451
{
@@ -66,7 +63,7 @@ SerializedShaderImpl::SerializedShaderImpl(IReferenceCounters* pRefCounters
6663
LOG_ERROR_AND_THROW("Serialized shader must not contain SHADER_COMPILE_FLAG_SKIP_REFLECTION flag");
6764
}
6865

69-
CopyShaderCreateInfo(ShaderCI);
66+
m_CreateInfo = ShaderCreateInfoWrapper{ShaderCI, GetRawAllocator()};
7067

7168
if ((DeviceFlags & ARCHIVE_DEVICE_DATA_FLAG_GL) != 0 && (DeviceFlags & ARCHIVE_DEVICE_DATA_FLAG_GLES) != 0)
7269
{
@@ -124,88 +121,6 @@ SerializedShaderImpl::SerializedShaderImpl(IReferenceCounters* pRefCounters
124121
}
125122
}
126123

127-
void SerializedShaderImpl::CopyShaderCreateInfo(const ShaderCreateInfo& ShaderCI) noexcept(false)
128-
{
129-
m_CreateInfo.ppCompilerOutput = nullptr;
130-
131-
auto& RawAllocator = GetRawAllocator();
132-
FixedLinearAllocator Allocator{RawAllocator};
133-
134-
Allocator.AddSpaceForString(ShaderCI.EntryPoint);
135-
Allocator.AddSpaceForString(ShaderCI.Desc.CombinedSamplerSuffix);
136-
Allocator.AddSpaceForString(ShaderCI.Desc.Name);
137-
138-
if (ShaderCI.ByteCode && ShaderCI.ByteCodeSize > 0)
139-
{
140-
Allocator.AddSpace(ShaderCI.ByteCodeSize, alignof(Uint32));
141-
}
142-
else if (ShaderCI.Source != nullptr)
143-
{
144-
Allocator.AddSpaceForString(ShaderCI.Source, ShaderCI.SourceLength);
145-
}
146-
else if (ShaderCI.FilePath != nullptr && ShaderCI.pShaderSourceStreamFactory != nullptr)
147-
{
148-
Allocator.AddSpaceForString(ShaderCI.FilePath);
149-
}
150-
else
151-
{
152-
LOG_ERROR_AND_THROW("Shader create info must contain Source, Bytecode or FilePath with pShaderSourceStreamFactory");
153-
}
154-
155-
size_t MacroCount = 0;
156-
if (ShaderCI.Macros)
157-
{
158-
for (auto* Macro = ShaderCI.Macros; Macro->Name != nullptr && Macro->Definition != nullptr; ++Macro, ++MacroCount)
159-
{}
160-
Allocator.AddSpace<ShaderMacro>(MacroCount + 1);
161-
162-
for (size_t i = 0; i < MacroCount; ++i)
163-
{
164-
Allocator.AddSpaceForString(ShaderCI.Macros[i].Name);
165-
Allocator.AddSpaceForString(ShaderCI.Macros[i].Definition);
166-
}
167-
}
168-
169-
Allocator.Reserve();
170-
171-
m_pRawMemory = decltype(m_pRawMemory){Allocator.ReleaseOwnership(), STDDeleterRawMem<void>{RawAllocator}};
172-
173-
m_CreateInfo.EntryPoint = Allocator.CopyString(ShaderCI.EntryPoint);
174-
m_CreateInfo.Desc.CombinedSamplerSuffix = Allocator.CopyString(ShaderCI.Desc.CombinedSamplerSuffix);
175-
m_CreateInfo.Desc.Name = Allocator.CopyString(ShaderCI.Desc.Name);
176-
177-
if (m_CreateInfo.Desc.Name == nullptr)
178-
m_CreateInfo.Desc.Name = "";
179-
180-
if (ShaderCI.ByteCode && ShaderCI.ByteCodeSize > 0)
181-
{
182-
m_CreateInfo.ByteCode = Allocator.Copy(ShaderCI.ByteCode, ShaderCI.ByteCodeSize, alignof(Uint32));
183-
}
184-
else if (ShaderCI.Source != nullptr)
185-
{
186-
m_CreateInfo.Source = Allocator.CopyString(ShaderCI.Source, ShaderCI.SourceLength);
187-
m_CreateInfo.SourceLength = ShaderCI.SourceLength;
188-
}
189-
else
190-
{
191-
VERIFY_EXPR(ShaderCI.FilePath != nullptr && ShaderCI.pShaderSourceStreamFactory != nullptr);
192-
m_CreateInfo.FilePath = Allocator.CopyString(ShaderCI.FilePath);
193-
}
194-
195-
if (MacroCount > 0)
196-
{
197-
auto* pMacros = Allocator.ConstructArray<ShaderMacro>(MacroCount + 1);
198-
m_CreateInfo.Macros = pMacros;
199-
for (auto* Macro = ShaderCI.Macros; Macro->Name != nullptr && Macro->Definition != nullptr; ++Macro, ++pMacros)
200-
{
201-
pMacros->Name = Allocator.CopyString(Macro->Name);
202-
pMacros->Definition = Allocator.CopyString(Macro->Definition);
203-
}
204-
pMacros->Name = nullptr;
205-
pMacros->Definition = nullptr;
206-
}
207-
}
208-
209124
SerializedShaderImpl::~SerializedShaderImpl()
210125
{}
211126

@@ -226,7 +141,7 @@ void DILIGENT_CALL_TYPE SerializedShaderImpl::QueryInterface(const INTERFACE_ID&
226141

227142
bool SerializedShaderImpl::operator==(const SerializedShaderImpl& Rhs) const noexcept
228143
{
229-
return m_CreateInfo == Rhs.m_CreateInfo;
144+
return m_CreateInfo.Get() == Rhs.m_CreateInfo.Get();
230145
}
231146

232147
SerializedData SerializedShaderImpl::SerializeCreateInfo(const ShaderCreateInfo& CI)

Graphics/GraphicsEngine/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ set(SOURCE
105105
src/RenderPassBase.cpp
106106
src/ShaderBindingTableBase.cpp
107107
src/SamplerBase.cpp
108+
src/ShaderBase.cpp
108109
src/TextureBase.cpp
109110
src/TopLevelASBase.cpp
110111
)

Graphics/GraphicsEngine/include/ShaderBase.hpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
/// Implementation of the Diligent::ShaderBase template class
3232

3333
#include <vector>
34+
#include <memory>
3435

3536
#include "Shader.h"
3637
#include "DeviceObjectBase.hpp"
@@ -42,6 +43,51 @@
4243
namespace Diligent
4344
{
4445

46+
class ShaderCreateInfoWrapper
47+
{
48+
public:
49+
ShaderCreateInfoWrapper() = default;
50+
51+
ShaderCreateInfoWrapper(const ShaderCreateInfoWrapper&) = delete;
52+
ShaderCreateInfoWrapper& operator=(const ShaderCreateInfoWrapper&) = delete;
53+
54+
ShaderCreateInfoWrapper(ShaderCreateInfoWrapper&& rhs) noexcept :
55+
m_CreateInfo{rhs.m_CreateInfo},
56+
m_SourceFactory{std::move(rhs.m_SourceFactory)},
57+
m_pRawMemory{std::move(rhs.m_pRawMemory)}
58+
{
59+
rhs.m_CreateInfo = {};
60+
}
61+
62+
ShaderCreateInfoWrapper& operator=(ShaderCreateInfoWrapper&& rhs) noexcept
63+
{
64+
m_CreateInfo = rhs.m_CreateInfo;
65+
m_SourceFactory = std::move(rhs.m_SourceFactory);
66+
m_pRawMemory = std::move(rhs.m_pRawMemory);
67+
68+
rhs.m_CreateInfo = {};
69+
70+
return *this;
71+
}
72+
73+
ShaderCreateInfoWrapper(const ShaderCreateInfo& CI, IMemoryAllocator& RawAllocator) noexcept(false);
74+
75+
const ShaderCreateInfo& Get() const
76+
{
77+
return m_CreateInfo;
78+
}
79+
80+
operator const ShaderCreateInfo&() const
81+
{
82+
return m_CreateInfo;
83+
}
84+
85+
private:
86+
ShaderCreateInfo m_CreateInfo;
87+
RefCntAutoPtr<IShaderSourceInputStreamFactory> m_SourceFactory;
88+
std::unique_ptr<void, STDDeleterRawMem<void>> m_pRawMemory;
89+
};
90+
4591
/// Template class implementing base functionality of the shader object
4692

4793
/// \tparam EngineImplTraits - Engine implementation type traits.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2019-2022 Diligent Graphics LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* In no event and under no legal theory, whether in tort (including negligence),
17+
* contract, or otherwise, unless required by applicable law (such as deliberate
18+
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
19+
* liable for any damages, including any direct, indirect, special, incidental,
20+
* or consequential damages of any character arising as a result of this License or
21+
* out of the use or inability to use the software (including but not limited to damages
22+
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
23+
* all other commercial damages or losses), even if such Contributor has been advised
24+
* of the possibility of such damages.
25+
*/
26+
27+
#include "ShaderBase.hpp"
28+
#include "FixedLinearAllocator.hpp"
29+
30+
namespace Diligent
31+
{
32+
33+
ShaderCreateInfoWrapper::ShaderCreateInfoWrapper(const ShaderCreateInfo& ShaderCI,
34+
IMemoryAllocator& RawAllocator) noexcept(false) :
35+
m_CreateInfo{ShaderCI},
36+
m_SourceFactory{ShaderCI.pShaderSourceStreamFactory}
37+
{
38+
m_CreateInfo.ppConversionStream = nullptr;
39+
m_CreateInfo.ppCompilerOutput = nullptr;
40+
41+
FixedLinearAllocator Allocator{RawAllocator};
42+
43+
Allocator.AddSpaceForString(ShaderCI.EntryPoint);
44+
Allocator.AddSpaceForString(ShaderCI.Desc.Name);
45+
Allocator.AddSpaceForString(ShaderCI.Desc.CombinedSamplerSuffix);
46+
47+
if (ShaderCI.ByteCode && ShaderCI.ByteCodeSize > 0)
48+
{
49+
Allocator.AddSpace(ShaderCI.ByteCodeSize, alignof(Uint32));
50+
}
51+
else if (ShaderCI.Source != nullptr)
52+
{
53+
Allocator.AddSpaceForString(ShaderCI.Source, ShaderCI.SourceLength);
54+
}
55+
else if (ShaderCI.FilePath != nullptr && ShaderCI.pShaderSourceStreamFactory != nullptr)
56+
{
57+
Allocator.AddSpaceForString(ShaderCI.FilePath);
58+
}
59+
else
60+
{
61+
LOG_ERROR_AND_THROW("Shader create info must contain Source, Bytecode or FilePath with pShaderSourceStreamFactory");
62+
}
63+
64+
size_t MacroCount = 0;
65+
if (ShaderCI.Macros)
66+
{
67+
for (auto* Macro = ShaderCI.Macros; Macro->Name != nullptr && Macro->Definition != nullptr; ++Macro, ++MacroCount)
68+
{}
69+
Allocator.AddSpace<ShaderMacro>(MacroCount + 1);
70+
71+
for (size_t i = 0; i < MacroCount; ++i)
72+
{
73+
Allocator.AddSpaceForString(ShaderCI.Macros[i].Name);
74+
Allocator.AddSpaceForString(ShaderCI.Macros[i].Definition);
75+
}
76+
}
77+
78+
Allocator.Reserve();
79+
80+
m_pRawMemory = decltype(m_pRawMemory){Allocator.ReleaseOwnership(), STDDeleterRawMem<void>{RawAllocator}};
81+
82+
m_CreateInfo.EntryPoint = Allocator.CopyString(ShaderCI.EntryPoint);
83+
m_CreateInfo.Desc.Name = Allocator.CopyString(ShaderCI.Desc.Name);
84+
m_CreateInfo.Desc.CombinedSamplerSuffix = Allocator.CopyString(ShaderCI.Desc.CombinedSamplerSuffix);
85+
86+
if (m_CreateInfo.Desc.Name == nullptr)
87+
m_CreateInfo.Desc.Name = "";
88+
89+
if (ShaderCI.ByteCode && ShaderCI.ByteCodeSize > 0)
90+
{
91+
m_CreateInfo.ByteCode = Allocator.Copy(ShaderCI.ByteCode, ShaderCI.ByteCodeSize, alignof(Uint32));
92+
}
93+
else if (ShaderCI.Source != nullptr)
94+
{
95+
m_CreateInfo.Source = Allocator.CopyString(ShaderCI.Source, ShaderCI.SourceLength);
96+
m_CreateInfo.SourceLength = ShaderCI.SourceLength;
97+
}
98+
else
99+
{
100+
VERIFY_EXPR(ShaderCI.FilePath != nullptr && ShaderCI.pShaderSourceStreamFactory != nullptr);
101+
m_CreateInfo.FilePath = Allocator.CopyString(ShaderCI.FilePath);
102+
}
103+
104+
if (MacroCount > 0)
105+
{
106+
auto* pMacros = Allocator.ConstructArray<ShaderMacro>(MacroCount + 1);
107+
m_CreateInfo.Macros = pMacros;
108+
for (auto* Macro = ShaderCI.Macros; Macro->Name != nullptr && Macro->Definition != nullptr; ++Macro, ++pMacros)
109+
{
110+
pMacros->Name = Allocator.CopyString(Macro->Name);
111+
pMacros->Definition = Allocator.CopyString(Macro->Definition);
112+
}
113+
pMacros->Name = nullptr;
114+
pMacros->Definition = nullptr;
115+
}
116+
}
117+
118+
} // namespace Diligent

0 commit comments

Comments
 (0)