Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
ShaderProgram.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018 DIVIDE-Studio
3 Copyright (c) 2009 Ionut Cava
4
5 This file is part of DIVIDE Framework.
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software
9 and associated documentation files (the "Software"), to deal in the Software
10 without restriction,
11 including without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so,
15 subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED,
22 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23 PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY,
26 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27 IN CONNECTION WITH THE SOFTWARE
28 OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 */
31
32#pragma once
33#ifndef DVD_SHADER_PROGRAM_H_
34#define DVD_SHADER_PROGRAM_H_
35
36#include "ShaderProgramFwd.h"
37#include "ShaderDataUploader.h"
38
42
43namespace FW
44{
45 class FileWatcher;
46};
47
48namespace Divide
49{
50
51 class Kernel;
52 class Camera;
53 class Material;
54 class ResourceCache;
55
56 struct Configuration;
57
58 enum class FileUpdateEvent : U8;
59
60 using SpvWord = U32;
61
62 class ShaderProgram;
63
64 namespace TypeUtil
65 {
66 [[nodiscard]] const char* DescriptorSetUsageToString( DescriptorSetUsage setUsage ) noexcept;
67 [[nodiscard]] DescriptorSetUsage StringToDescriptorSetUsage( const string& name );
68 };
69
71
73 {
74 protected:
76
77 // 10 seconds should be enough
79
80 public:
81 explicit ShaderModule( GFXDevice& context, const std::string_view name, U32 generation );
82 virtual ~ShaderModule() override;
83
86
88 PROPERTY_R( bool, valid, false );
89 PROPERTY_R( bool, inUse, true );
90 PROPERTY_R( U32, generation, 0u);
91 PROPERTY_R( U64, lastUsedFrame, U64_MAX - MAX_FRAME_LIFETIME - 1u);
92
93
94 public:
95 // ======================= static data ========================= //
97 static ShaderModule* GetShader( const std::string_view name );
98
99 static void Idle(bool fast);
100 static void InitStaticData();
101 static void DestroyStaticData();
102
103 protected:
104 static ShaderModule* GetShaderLocked( const std::string_view name );
105
106 protected:
108 eastl::fixed_vector<ShaderProgram*, 4, true> _parents;
109
110 protected:
112 static std::atomic_bool s_modulesRemoved;
115 };
116
118 public GraphicsResource
119 {
120 public:
121 static constexpr const char* UNIFORM_BLOCK_NAME = "dvd_uniforms";
122
124
125 // one per shader type!
126 struct LoadData
127 {
128 enum class SourceCodeSource : U8
129 {
130 SOURCE_FILES,
131 TEXT_CACHE,
132 SPIRV_CACHE,
133 COUNT
134 };
135 enum class ShaderCacheType : U8
136 {
137 GLSL,
138 SPIRV,
140 COUNT
141 };
142
144 std::vector<SpvWord> _sourceCodeSpirV;
146 Str<256> _sourceName{};
147 Str<256> _shaderName{};
148 Str<256> _sourceFile{};
149 size_t _definesHash{ 0u };
150 ShaderType _type{ ShaderType::COUNT };
151 string _uniformBlock{};
152 Reflection::Data _reflectionData{};
153 bool _compiled{ false };
154 };
155
157 {
158 ShaderProgram* _program{nullptr};
159 U32 _queueDelay{0u};
160 U32 _queueDelayHighWaterMark{1u};
161 };
162
163 using RenderTargets = std::array<bool, to_base( RTColourAttachmentSlot::COUNT )>;
164 using ShaderLoadData = std::array<LoadData, to_base( ShaderType::COUNT )>;
165
166 using ShaderProgramMap = eastl::fixed_vector<ShaderProgram*, U16_MAX, true>;
167
168 using AtomMap = hashMap<U64 /*name hash*/, string>;
169 using AtomInclusionMap = hashMap<U64 /*name hash*/, eastl::set<U64>>;
170 using ShaderQueue = eastl::stack<ShaderQueueEntry, vector<ShaderQueueEntry>>;
171
173 {
174 BaseType<ShaderStageVisibility> _visibility{ to_base( ShaderStageVisibility::NONE ) };
175 DescriptorSetBindingType _type{ DescriptorSetBindingType::COUNT };
177 };
178 using BindingsPerSetArray = std::array<BindingsPerSet, MAX_BINDINGS_PER_DESCRIPTOR_SET>;
179 using SetUsageData = std::array<bool, to_base(DescriptorSetUsage::COUNT)>;
180 using BindingSetData = std::array<BindingsPerSetArray, to_base( DescriptorSetUsage::COUNT )>;
181
182 public:
183 explicit ShaderProgram( PlatformContext& context, const ResourceDescriptor<ShaderProgram>& descriptor );
184
185 ~ShaderProgram() override;
186
187 bool load( PlatformContext& context ) override;
188 bool postLoad() override;
189 bool unload() override;
190
191 inline bool recompile()
192 {
193 bool skipped = false;
194 return recompile( skipped );
195 }
196
197 bool recompile( bool& skipped );
198
199 virtual ShaderResult validatePreBind( bool rebind = true );
200
201 [[nodiscard]] bool uploadUniformData( const UniformData& data, DescriptorSet& set, GFX::MemoryBarrierCommand& memCmdInOut );
202
203 //==================== static methods ===============================//
204 static void Idle( PlatformContext& platformContext, bool fast );
205 static void InitStaticData();
206 static void DestroyStaticData();
207
208 [[nodiscard]] static ErrorCode OnStartup( PlatformContext& context);
209 [[nodiscard]] static bool OnShutdown();
210 static void OnBeginFrame( GFXDevice& gfx );
211 static void OnEndFrame( GFXDevice& gfx );
212
214 static bool RecompileShaderProgram( const std::string_view name );
216 static bool UnregisterShaderProgram( ShaderProgram* shaderProgram );
218 static void RegisterShaderProgram( ShaderProgram* shaderProgram );
219
220 static void RebuildAllShaders();
221
222 [[nodiscard]] static vector<ResourcePath> GetAllAtomLocations();
223
224 [[nodiscard]] static I32 ShaderProgramCount() noexcept
225 {
226 return s_shaderCount.load( std::memory_order_relaxed );
227 }
228
229 [[nodiscard]] inline const ShaderProgramDescriptor& descriptor() const noexcept
230 {
231 return _descriptor;
232 }
233
234 static void OnAtomChange( std::string_view atomName, FileUpdateEvent evt );
235
236 [[nodiscard]] static U8 GetGLBindingForDescriptorSlot( DescriptorSetUsage usage, U8 slot ) noexcept;
237 [[nodiscard]] static std::pair<DescriptorSetUsage, U8> GetDescriptorSlotForGLBinding( U8 binding, DescriptorSetBindingType type ) noexcept;
238
239 [[nodiscard]] static BindingSetData& GetBindingSetData() noexcept;
240
241 static void RegisterSetLayoutBinding( DescriptorSetUsage usage, U8 slot, DescriptorSetBindingType type, ShaderStageVisibility visibility );
242 static ErrorCode SubmitSetLayouts(GFXDevice& gfx);
243 static U32 GetBindingCount( DescriptorSetUsage usage, DescriptorSetBindingType type);
244
245 PROPERTY_RW( bool, highPriority, true );
246 PROPERTY_RW( bool, useShaderCache, true );
247 PROPERTY_R_IW( BindingsPerSetArray, perDrawDescriptorSetLayout );
248 PROPERTY_R_IW( RenderTargets, fragmentOutputs );
250
251 static Mutex g_cacheLock;
252
253 protected:
254
255 static bool SaveToCache( LoadData::ShaderCacheType cache, const LoadData& dataIn, const eastl::set<U64>& atomIDsIn );
256 static bool LoadFromCache( LoadData::ShaderCacheType cache, LoadData& dataInOut, eastl::set<U64>& atomIDsOut );
257
258 protected:
259 static ShaderQueue s_recompileQueue;
260 static ShaderQueue s_recompileFailedQueue;
262 static ShaderProgramMap s_shaderPrograms;
263 static eastl::fixed_vector<ShaderProgram*, U16_MAX, false> s_usedShaderPrograms;
264
266 {
267 ShaderProgram* _program{ nullptr };
268 Handle<ShaderProgram> _handle{ INVALID_HANDLE<ShaderProgram> };
269 };
272
273 protected:
274 virtual bool loadInternal( hashMap<U64, PerFileShaderData>& fileData, bool overwrite );
275
276 bool loadSourceCode( const ModuleDefines& defines,
277 bool reloadExisting,
278 LoadData& loadDataInOut,
279 Reflection::UniformsSet& previousUniformsInOut,
280 U8& blockIndexInOut );
281
282 void loadAndParseGLSL( const ModuleDefines& defines,
283 LoadData& loadDataInOut,
284 Reflection::UniformsSet& previousUniformsInOut,
285 U8& blockIndexInOut,
286 eastl::set<U64>& atomIDsInOut );
287
288 void initDrawDescriptorSetLayout( const PerFileShaderData& loadData );
289 void initUniformUploader( const PerFileShaderData& loadData );
290
291
292 private:
293 static void EraseAtom(const U64 atomHash);
294 static void EraseAtomLocked(const U64 atomHash);
295
296 static const string& ShaderFileRead( const ResourcePath& filePath, std::string_view atomName, bool recurse, eastl::set<U64>& foundAtomIDsInOut, bool& wasParsed );
297 static const string& ShaderFileReadLocked( const ResourcePath& filePath, std::string_view atomName, bool recurse, eastl::set<U64>& foundAtomIDsInOut, bool& wasParsed );
298
299 static void PreprocessIncludes( std::string_view name,
300 string& sourceInOut,
301 I32 level,
302 eastl::set<U64>& foundAtomIDsInOut,
303 bool lock );
304 protected:
305 template <typename T>
306 friend class ImplResourceLoader;
307
309
310 protected:
311 friend class PlatformContext;
312 static void OnThreadCreated( const GFXDevice& gfx, const std::thread::id& threadID, bool isMainRenderThread );
313
314 protected:
316 eastl::set<U64> _usedAtomIDs;
317
318 protected:
319 static std::atomic_int s_shaderCount;
320
322
327
328 //extra entry for "common" location
329 static ResourcePath shaderAtomLocationPrefix[to_base( ShaderType::COUNT ) + 1];
330 static Str<8> shaderAtomExtensionName[to_base( ShaderType::COUNT ) + 1];
331 static U64 shaderAtomExtensionHash[to_base( ShaderType::COUNT ) + 1];
332
334 };
335
337 {
338 string _programName{};
341 };
342
343}; // namespace Divide
344
345#endif //DVD_SHADER_PROGRAM_H_
#define PROPERTY_RW(...)
Convenience method to add a class member with public read access and write access.
#define PROPERTY_R_IW(...)
Convenience method to add a class member with public read access but protected write access including...
#define FWD_DECLARE_MANAGED_CLASS(T)
#define NOINITVTABLE
Rough around the edges Adapter pattern abstracting the actual rendering API and access to the GPU.
Definition: GFXDevice.h:215
Utility class that adds basic GUID management to objects.
Definition: GUIDWrapper.h:44
GFXDevice & context() const noexcept
virtual ~ShaderModule() override
PROPERTY_R(U64, lastUsedFrame, U64_MAX - MAX_FRAME_LIFETIME - 1u)
void deregisterParent(ShaderProgram *parent)
PROPERTY_R(Str< 256 >, name)
static constexpr U32 MAX_FRAME_LIFETIME
Definition: ShaderProgram.h:78
PROPERTY_R(bool, inUse, true)
static void Idle(bool fast)
eastl::fixed_vector< ShaderProgram *, 4, true > _parents
static void DestroyStaticData()
static ShaderModule * GetShader(const std::string_view name)
Returns a reference to an already loaded shader, null otherwise.
static void InitStaticData()
static ShaderModule * GetShaderLocked(const std::string_view name)
hashMap< U64, ShaderModule_uptr > ShaderMap
Definition: ShaderProgram.h:75
PROPERTY_R(bool, valid, false)
PROPERTY_R(U32, generation, 0u)
void registerParent(ShaderProgram *parent)
static std::atomic_bool s_modulesRemoved
Shader cache.
static SharedMutex s_shaderNameLock
static ShaderMap s_shaderNameMap
std::array< BindingsPerSetArray, to_base(DescriptorSetUsage::COUNT)> BindingSetData
static SharedMutex s_programLock
std::array< bool, to_base(RTColourAttachmentSlot::COUNT)> RenderTargets
static std::atomic_int s_shaderCount
std::array< BindingsPerSet, MAX_BINDINGS_PER_DESCRIPTOR_SET > BindingsPerSetArray
static Mutex s_atomLock
Shaders loaded from files are kept as atoms.
eastl::stack< ShaderQueueEntry, vector< ShaderQueueEntry > > ShaderQueue
static AtomMap s_atoms
static LastRequestedShader s_lastRequestedShaderProgram
std::array< LoadData, to_base(ShaderType::COUNT)> ShaderLoadData
static AtomInclusionMap s_atomIncludes
std::array< bool, to_base(DescriptorSetUsage::COUNT)> SetUsageData
const ShaderProgramDescriptor _descriptor
hashMap< U64, string > AtomMap
static BindingSetData s_bindingsPerSet
hashMap< U64, eastl::set< U64 > > AtomInclusionMap
eastl::fixed_vector< ShaderProgram *, U16_MAX, true > ShaderProgramMap
static I64 s_shaderFileWatcherID
const ShaderProgramDescriptor & descriptor() const noexcept
static I32 ShaderProgramCount() noexcept
vector< UniformBlockUploader > _uniformBlockBuffers
eastl::set< U64 > _usedAtomIDs
constexpr U16 TARGET_FRAME_RATE
Application desired framerate for physics and input simulations.
Definition: config.h:97
eastl::set< UniformDeclaration, UniformCompare > UniformsSet
DescriptorSetUsage StringToDescriptorSetUsage(const string &name)
const char * DescriptorSetUsageToString(DescriptorSetUsage setUsage) noexcept
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
bool load() override
constexpr U64 U64_MAX
std::mutex Mutex
Definition: SharedMutex.h:40
bool unload() override
int32_t I32
uint8_t U8
vector< ModuleDefine > ModuleDefines
U32 SpvWord
Definition: ShaderProgram.h:60
std::shared_mutex SharedMutex
Definition: SharedMutex.h:43
eastl::vector< Type > vector
Definition: Vector.h:42
hashAlg::unordered_map< K, V, HashFun, Predicate > hashMap
Definition: HashMap.h:55
DescriptorSetBindingType
constexpr U8 INVALID_TEXTURE_BINDING
Project & parent
Definition: DefaultScene.h:41
std::underlying_type_t< Type > BaseType
ShaderStageVisibility
ShaderType
Available shader stages.
DescriptorSetUsage
int64_t I64
uint32_t U32
uint64_t U64
constexpr auto to_base(const Type value) -> Type
ShaderProgram::ShaderLoadData _loadData
vector< ShaderModuleDescriptor > _modules
Reflection::UniformsSet _uniforms
std::vector< SpvWord > _sourceCodeSpirV