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