| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "D3D11UniformCache.h" |
| 4 |
#include "../RhiFwd.h" |
| 5 |
|
| 6 |
#include <cstdint> |
| 7 |
|
| 8 |
#include <Containers/SmallVector.h> |
| 9 |
|
| 10 |
using namespace Death::Containers; |
| 11 |
|
| 12 |
namespace nCine::RHI::D3D11 |
| 13 |
{ |
| 14 |
class D3D11ShaderProgram; |
| 15 |
|
| 16 |
/** |
| 17 |
@brief Manages the loose-uniform caches of a program (aliased as `RHI::ShaderUniforms`) |
| 18 |
|
| 19 |
Owns a @ref D3D11UniformCache for every active uniform of a program that is not part of a block, |
| 20 |
distributes a shared host data buffer across them, and on @ref CommitUniforms() publishes every dirty |
| 21 |
value to the program. The managed set can be restricted to an include-only or exclude list of |
| 22 |
null-separated names. |
| 23 |
*/ |
| 24 |
class D3D11ShaderUniforms |
| 25 |
{ |
| 26 |
public: |
| 27 |
// The caches are stored in a flat container whose value type is the cache itself (the name lives |
| 28 |
// inside each cache's uniform), so range-iteration yields `D3D11UniformCache&` directly — matching |
| 29 |
// the OpenGL backend's `GetAllUniforms()` iteration semantics that the render batcher relies on. |
| 30 |
using UniformHashMapType = SmallVector<D3D11UniformCache, 0>; |
| 31 |
|
| 32 |
D3D11ShaderUniforms(); |
| 33 |
explicit D3D11ShaderUniforms(D3D11ShaderProgram* shaderProgram); |
| 34 |
D3D11ShaderUniforms(D3D11ShaderProgram* shaderProgram, const char* includeOnly, const char* exclude); |
| 35 |
|
| 36 |
inline void SetProgram(D3D11ShaderProgram* shaderProgram) { |
| 37 |
SetProgram(shaderProgram, nullptr, nullptr); |
| 38 |
} |
| 39 |
void SetProgram(D3D11ShaderProgram* shaderProgram, const char* includeOnly, const char* exclude); |
| 40 |
void SetUniformsDataPointer(std::uint8_t* dataPointer); |
| 41 |
void SetDirty(bool isDirty); |
| 42 |
|
| 43 |
inline std::uint32_t GetUniformCount() const { |
| 44 |
return std::uint32_t(_uniformCaches.size()); |
| 45 |
} |
| 46 |
bool HasUniform(const char* name) const; |
| 47 |
D3D11UniformCache* GetUniform(const char* name); |
| 48 |
/** @brief Returns the container of all managed uniform caches (range-iterates as `D3D11UniformCache&`) */ |
| 49 |
inline const UniformHashMapType& GetAllUniforms() const { |
| 50 |
return _uniformCaches; |
| 51 |
} |
| 52 |
inline void MarkDirty() { |
| 53 |
_maybeDirty = true; |
| 54 |
} |
| 55 |
void CommitUniforms(); |
| 56 |
|
| 57 |
private: |
| 58 |
D3D11ShaderProgram* _shaderProgram; |
| 59 |
bool _maybeDirty; |
| 60 |
|
| 61 |
UniformHashMapType _uniformCaches; |
| 62 |
|
| 63 |
void ImportUniforms(const char* includeOnly, const char* exclude); |
| 64 |
}; |
| 65 |
|
| 66 |
/** |
| 67 |
@brief Manages the uniform-block caches of a program (aliased as `RHI::ShaderUniformBlocks`) |
| 68 |
|
| 69 |
Owns a @ref D3D11UniformBlockCache per active block, distributes a shared host data buffer across |
| 70 |
them, copies the block contents into a suballocated range of the streaming uniform buffer on @ref |
| 71 |
CommitUniformBlocks(), and on @ref Bind() forwards each range to the device. |
| 72 |
*/ |
| 73 |
class D3D11ShaderUniformBlocks |
| 74 |
{ |
| 75 |
public: |
| 76 |
// Flat container whose value type is the block cache itself (the name lives inside each cache's |
| 77 |
// block), so range-iteration yields `D3D11UniformBlockCache&` directly, matching the OpenGL backend. |
| 78 |
using UniformHashMapType = SmallVector<D3D11UniformBlockCache, 0>; |
| 79 |
|
| 80 |
/** @brief Function that suballocates a range of the given size from the streaming uniform buffer */ |
| 81 |
using UniformRangeAllocator = RHI::BufferRange (*)(std::uint32_t bytes); |
| 82 |
|
| 83 |
/** @brief Sets the allocator used by @ref CommitUniformBlocks() (registered by the pipeline at startup) */ |
| 84 |
static void SetUniformRangeAllocator(UniformRangeAllocator allocator); |
| 85 |
|
| 86 |
D3D11ShaderUniformBlocks(); |
| 87 |
explicit D3D11ShaderUniformBlocks(D3D11ShaderProgram* shaderProgram); |
| 88 |
D3D11ShaderUniformBlocks(D3D11ShaderProgram* shaderProgram, const char* includeOnly, const char* exclude); |
| 89 |
|
| 90 |
inline void SetProgram(D3D11ShaderProgram* shaderProgram) { |
| 91 |
SetProgram(shaderProgram, nullptr, nullptr); |
| 92 |
} |
| 93 |
void SetProgram(D3D11ShaderProgram* shaderProgram, const char* includeOnly, const char* exclude); |
| 94 |
void SetUniformsDataPointer(std::uint8_t* dataPointer); |
| 95 |
|
| 96 |
inline std::uint32_t GetUniformBlockCount() const { |
| 97 |
return std::uint32_t(_uniformBlockCaches.size()); |
| 98 |
} |
| 99 |
bool HasUniformBlock(const char* name) const; |
| 100 |
D3D11UniformBlockCache* GetUniformBlock(const char* name); |
| 101 |
/** @brief Returns the container of all managed block caches (range-iterates as `D3D11UniformBlockCache&`) */ |
| 102 |
inline const UniformHashMapType& GetAllUniformBlocks() const { |
| 103 |
return _uniformBlockCaches; |
| 104 |
} |
| 105 |
void CommitUniformBlocks(); |
| 106 |
void Bind(); |
| 107 |
|
| 108 |
private: |
| 109 |
static UniformRangeAllocator _uniformRangeAllocator; |
| 110 |
|
| 111 |
D3D11ShaderProgram* _shaderProgram; |
| 112 |
std::uint8_t* _dataPointer; |
| 113 |
RHI::BufferRange _uboParams; |
| 114 |
|
| 115 |
UniformHashMapType _uniformBlockCaches; |
| 116 |
|
| 117 |
void ImportUniformBlocks(const char* includeOnly, const char* exclude); |
| 118 |
}; |
| 119 |
} |