| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "D3D11ShaderTypes.h" |
| 4 |
#include "D3D11VertexFormat.h" |
| 5 |
#include "../RhiTypes.h" |
| 6 |
|
| 7 |
#include <cstdint> |
| 8 |
#include <string> |
| 9 |
|
| 10 |
#include <Containers/ArrayView.h> |
| 11 |
#include <Containers/SmallVector.h> |
| 12 |
#include <Containers/String.h> |
| 13 |
#include <Containers/StringView.h> |
| 14 |
|
| 15 |
using namespace Death::Containers; |
| 16 |
|
| 17 |
// Direct3D 11 interfaces referenced only as opaque pointers here (definitions pulled in by the .cpp) |
| 18 |
struct ID3D11VertexShader; |
| 19 |
struct ID3D11PixelShader; |
| 20 |
struct ID3D11InputLayout; |
| 21 |
|
| 22 |
namespace ShaderCompiler |
| 23 |
{ |
| 24 |
struct ProgramVariant; |
| 25 |
} |
| 26 |
|
| 27 |
namespace nCine::RHI::D3D11 |
| 28 |
{ |
| 29 |
class D3D11ShaderUniforms; |
| 30 |
class D3D11ShaderUniformBlocks; |
| 31 |
|
| 32 |
/** |
| 33 |
@brief Describes one constant buffer slot a shader stage expects, precomputed from bytecode reflection |
| 34 |
|
| 35 |
At draw time the device rebuilds each slot's bytes into a dynamic `ID3D11Buffer` and binds it at |
| 36 |
@ref Register. A `_Globals` slot (loose uniforms) is gathered member-by-member from the program's |
| 37 |
resolved loose-uniform values; a uniform-block slot is copied verbatim from the std140 range the |
| 38 |
pipeline bound (the HLSL emitter laid the cbuffer out to match std140, so no repacking is needed). |
| 39 |
*/ |
| 40 |
struct D3D11CBufferSlot |
| 41 |
{ |
| 42 |
struct GlobalVar |
| 43 |
{ |
| 44 |
std::string Name; // loose-uniform name to resolve |
| 45 |
std::uint32_t Offset; // byte offset within the cbuffer |
| 46 |
std::uint32_t Size; // byte size of the value |
| 47 |
}; |
| 48 |
|
| 49 |
std::uint32_t Register = 0; // cbuffer register (bN) |
| 50 |
std::uint32_t ByteSize = 0; // full cbuffer size (16-aligned), as declared in the bytecode |
| 51 |
bool IsGlobals = false; // true = built from loose uniforms; false = a uniform block |
| 52 |
std::int32_t BlockIndex = -1; // if a block: index into the device's bound-uniform-range table |
| 53 |
// If IsGlobals: the members to gather. Heap-only (no inline storage) because the slot itself lives |
| 54 |
// inside the per-stage slot lists below, where inline elements would bloat every entry. |
| 55 |
SmallVector<GlobalVar, 0> Globals; |
| 56 |
}; |
| 57 |
|
| 58 |
/** |
| 59 |
@brief Constant-buffer slots one shader stage expects |
| 60 |
|
| 61 |
A stage binds one or two cbuffers in practice (the gathered `_Globals` plus at most one uniform |
| 62 |
block), so the inline capacity covers the usual case without a heap allocation, and the draw path |
| 63 |
walks the slots inline. |
| 64 |
*/ |
| 65 |
using CBufferSlotList = SmallVector<D3D11CBufferSlot, 2>; |
| 66 |
|
| 67 |
/** @brief Compiled DXBC bytecode of one shader stage (kilobytes — always heap-allocated) */ |
| 68 |
using ShaderByteCode = SmallVector<std::uint8_t, 0>; |
| 69 |
|
| 70 |
/** |
| 71 |
@brief Shader program of the Direct3D 11 backend (aliased as `RHI::ShaderProgram`) |
| 72 |
|
| 73 |
Carries the offline ShaderCompiler reflection (set with @ref SetReflection() like the OpenGL backend) |
| 74 |
from which it imports uniforms, uniform blocks and attributes, and creates real |
| 75 |
`ID3D11VertexShader`/`ID3D11PixelShader` objects from the reflection's precompiled DXBC bytecode |
| 76 |
(`HlslVsDxbc`/`HlslFsDxbc`, the normal case) — or, when only HLSL text is embedded, by runtime-compiling |
| 77 |
`HlslVsSource`/`HlslFsSource` (matrix packing forced column-major so the emitter's `mul(M,v)` |
| 78 |
column-vector algebra matches the engine's column-major uniform data verbatim; both paths use the same |
| 79 |
contract). It then reflects the constant buffers into @ref D3D11CBufferSlot lists the device rebinds |
| 80 |
each draw, and keeps the VS bytecode for building the input layout of attribute-based (mesh/tilemap) |
| 81 |
shaders. @ref Use() records the program as current on the device. |
| 82 |
*/ |
| 83 |
class D3D11ShaderProgram |
| 84 |
{ |
| 85 |
friend class D3D11ShaderUniforms; |
| 86 |
friend class D3D11ShaderUniformBlocks; |
| 87 |
|
| 88 |
public: |
| 89 |
enum class Introspection |
| 90 |
{ |
| 91 |
Enabled, |
| 92 |
NoUniformsInBlocks, |
| 93 |
Disabled |
| 94 |
}; |
| 95 |
|
| 96 |
enum class Status |
| 97 |
{ |
| 98 |
NotLinked, |
| 99 |
CompilationFailed, |
| 100 |
LinkingFailed, |
| 101 |
Linked, |
| 102 |
LinkedWithDeferredQueries, |
| 103 |
LinkedWithIntrospection |
| 104 |
}; |
| 105 |
|
| 106 |
enum class QueryPhase |
| 107 |
{ |
| 108 |
Immediate, |
| 109 |
Deferred |
| 110 |
}; |
| 111 |
|
| 112 |
/** @brief Default batch size, indicating the shader is not batched */ |
| 113 |
static constexpr std::int32_t DefaultBatchSize = -1; |
| 114 |
|
| 115 |
D3D11ShaderProgram(); |
| 116 |
explicit D3D11ShaderProgram(QueryPhase queryPhase); |
| 117 |
D3D11ShaderProgram(StringView vertexFile, StringView fragmentFile, Introspection introspection, QueryPhase queryPhase); |
| 118 |
D3D11ShaderProgram(StringView vertexFile, StringView fragmentFile, Introspection introspection); |
| 119 |
D3D11ShaderProgram(StringView vertexFile, StringView fragmentFile); |
| 120 |
~D3D11ShaderProgram(); |
| 121 |
|
| 122 |
D3D11ShaderProgram(const D3D11ShaderProgram&) = delete; |
| 123 |
D3D11ShaderProgram& operator=(const D3D11ShaderProgram&) = delete; |
| 124 |
|
| 125 |
/** @brief Returns a backend-neutral identifier uniquely identifying the program (feeds material sort keys) */ |
| 126 |
inline std::uint32_t GetUniqueId() const { |
| 127 |
return _handle; |
| 128 |
} |
| 129 |
inline Status GetStatus() const { |
| 130 |
return _status; |
| 131 |
} |
| 132 |
inline Introspection GetIntrospection() const { |
| 133 |
return _introspection; |
| 134 |
} |
| 135 |
inline QueryPhase GetQueryPhase() const { |
| 136 |
return _queryPhase; |
| 137 |
} |
| 138 |
inline std::uint32_t GetBatchSize() const { |
| 139 |
return _batchSize; |
| 140 |
} |
| 141 |
inline void SetBatchSize(std::uint32_t value) { |
| 142 |
_batchSize = value; |
| 143 |
} |
| 144 |
|
| 145 |
bool IsLinked() const; |
| 146 |
|
| 147 |
std::uint32_t RetrieveInfoLogLength() const { |
| 148 |
return 0; |
| 149 |
} |
| 150 |
void RetrieveInfoLog(std::string& infoLog) const { |
| 151 |
static_cast<void>(infoLog); |
| 152 |
} |
| 153 |
|
| 154 |
inline std::uint32_t GetUniformsSize() const { |
| 155 |
return _uniformsSize; |
| 156 |
} |
| 157 |
inline std::uint32_t GetUniformBlocksSize() const { |
| 158 |
return _uniformBlocksSize; |
| 159 |
} |
| 160 |
|
| 161 |
bool AttachShaderFromFile(ShaderStage stage, StringView filename); |
| 162 |
bool AttachShaderFromString(ShaderStage stage, StringView string); |
| 163 |
bool AttachShaderFromStrings(ShaderStage stage, ArrayView<const StringView> strings); |
| 164 |
bool AttachShaderFromStringsAndFile(ShaderStage stage, ArrayView<const StringView> strings, StringView filename); |
| 165 |
|
| 166 |
/** @brief Sets the offline reflection consumed by @ref Link() to import uniforms/blocks/attributes and compile HLSL */ |
| 167 |
inline void SetReflection(const ShaderCompiler::ProgramVariant* reflection) { |
| 168 |
_reflection = reflection; |
| 169 |
} |
| 170 |
/** |
| 171 |
@brief Records the true (program, variant) identity of the loaded shader |
| 172 |
|
| 173 |
Only the fixed-function console backends consume it (they resolve their generated |
| 174 |
effect tables from this identity instead of the object label); this backend runs |
| 175 |
real shaders and ignores it. |
| 176 |
*/ |
| 177 |
inline void SetProgramIdentity(const char* programName, const char* variantName) { |
| 178 |
static_cast<void>(programName); |
| 179 |
static_cast<void>(variantName); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
bool Link(Introspection introspection); |
| 184 |
void Use(); |
| 185 |
bool Validate() { |
| 186 |
return true; |
| 187 |
} |
| 188 |
bool FinalizeAfterLinking(Introspection introspection); |
| 189 |
|
| 190 |
inline std::uint32_t GetAttributeCount() const { |
| 191 |
return std::uint32_t(_attributes.size()); |
| 192 |
} |
| 193 |
bool HasAttribute(const char* name) const; |
| 194 |
D3D11VertexFormat::Attribute* GetAttribute(const char* name); |
| 195 |
|
| 196 |
inline void DefineVertexFormat(const D3D11BufferObject* vbo) { |
| 197 |
DefineVertexFormat(vbo, nullptr, 0); |
| 198 |
} |
| 199 |
inline void DefineVertexFormat(const D3D11BufferObject* vbo, const D3D11BufferObject* ibo) { |
| 200 |
DefineVertexFormat(vbo, ibo, 0); |
| 201 |
} |
| 202 |
void DefineVertexFormat(const D3D11BufferObject* vbo, const D3D11BufferObject* ibo, std::uint32_t vboOffset); |
| 203 |
|
| 204 |
void Reset(); |
| 205 |
void SetObjectLabel(StringView label); |
| 206 |
|
| 207 |
inline bool GetLogOnErrors() const { |
| 208 |
return _shouldLogOnErrors; |
| 209 |
} |
| 210 |
inline void SetLogOnErrors(bool shouldLogOnErrors) { |
| 211 |
_shouldLogOnErrors = shouldLogOnErrors; |
| 212 |
} |
| 213 |
|
| 214 |
// -- Backend extensions (used by the uniform caches and the device draw path) -- |
| 215 |
|
| 216 |
/** @brief Publishes a committed loose-uniform value pointer for the device to gather into `_Globals` */ |
| 217 |
void SetResolvedUniform(const char* name, const std::uint8_t* data); |
| 218 |
/** @brief Returns the last published value pointer of the named loose uniform, or `nullptr` */ |
| 219 |
const std::uint8_t* ResolveUniform(const char* name) const; |
| 220 |
|
| 221 |
/** @brief Returns the compiled vertex shader, or `nullptr` if not compiled (e.g. runtime shader with no HLSL) */ |
| 222 |
inline ID3D11VertexShader* GetVertexShader() const { |
| 223 |
return _vertexShader; |
| 224 |
} |
| 225 |
/** @brief Returns the compiled pixel shader, or `nullptr` */ |
| 226 |
inline ID3D11PixelShader* GetPixelShader() const { |
| 227 |
return _pixelShader; |
| 228 |
} |
| 229 |
/** @brief Returns/creates the input layout for this program's attributes + bound vertex format; `nullptr` for `SV_VertexID` shaders */ |
| 230 |
ID3D11InputLayout* GetInputLayout(); |
| 231 |
/** @brief Returns `true` if the vertex shader reads vertex attributes (needs an input layout + vertex buffer) */ |
| 232 |
inline bool HasVertexAttributes() const { |
| 233 |
return !_attributes.empty(); |
| 234 |
} |
| 235 |
/** @brief Returns the vertex buffer bound by @ref DefineVertexFormat(), or `nullptr` */ |
| 236 |
inline const D3D11BufferObject* GetBoundVbo() const { |
| 237 |
return _boundVbo; |
| 238 |
} |
| 239 |
/** @brief Returns the index buffer bound by @ref DefineVertexFormat(), or `nullptr` */ |
| 240 |
inline const D3D11BufferObject* GetBoundIbo() const { |
| 241 |
return _boundIbo; |
| 242 |
} |
| 243 |
/** @brief Returns the per-vertex byte stride (from the first enabled attribute of the bound vertex format) */ |
| 244 |
std::uint32_t GetVertexStride() const; |
| 245 |
|
| 246 |
/** @brief Constant-buffer slots the vertex stage expects (rebuilt and bound each draw) */ |
| 247 |
inline const CBufferSlotList& GetVsCBuffers() const { |
| 248 |
return _vsCBuffers; |
| 249 |
} |
| 250 |
/** @brief Constant-buffer slots the pixel stage expects */ |
| 251 |
inline const CBufferSlotList& GetPsCBuffers() const { |
| 252 |
return _psCBuffers; |
| 253 |
} |
| 254 |
/** @brief Bitmask of the texture/sampler registers the vertex stage actually reads (bit N = slot tN/sN) */ |
| 255 |
inline std::uint32_t GetVsTextureMask() const { |
| 256 |
return _vsTextureMask; |
| 257 |
} |
| 258 |
/** @brief Bitmask of the texture/sampler registers the pixel stage actually reads */ |
| 259 |
inline std::uint32_t GetPsTextureMask() const { |
| 260 |
return _psTextureMask; |
| 261 |
} |
| 262 |
|
| 263 |
private: |
| 264 |
static std::uint32_t _nextHandle; |
| 265 |
|
| 266 |
std::uint32_t _handle; |
| 267 |
Status _status; |
| 268 |
Introspection _introspection; |
| 269 |
QueryPhase _queryPhase; |
| 270 |
std::uint32_t _batchSize; |
| 271 |
bool _shouldLogOnErrors; |
| 272 |
std::uint32_t _uniformsSize; |
| 273 |
std::uint32_t _uniformBlocksSize; |
| 274 |
|
| 275 |
SmallVector<D3D11Uniform, 0> _uniforms; |
| 276 |
SmallVector<D3D11UniformBlock, 0> _uniformBlocks; |
| 277 |
SmallVector<D3D11Attribute, 0> _attributes; |
| 278 |
|
| 279 |
const ShaderCompiler::ProgramVariant* _reflection; |
| 280 |
|
| 281 |
D3D11VertexFormat _vertexFormat; |
| 282 |
const D3D11BufferObject* _boundVbo; |
| 283 |
const D3D11BufferObject* _boundIbo; |
| 284 |
|
| 285 |
struct ResolvedUniform |
| 286 |
{ |
| 287 |
String Name; |
| 288 |
const std::uint8_t* Data; |
| 289 |
}; |
| 290 |
SmallVector<ResolvedUniform, 0> _resolvedUniforms; |
| 291 |
|
| 292 |
// Compiled Direct3D 11 objects (owned) |
| 293 |
ID3D11VertexShader* _vertexShader; |
| 294 |
ID3D11PixelShader* _pixelShader; |
| 295 |
ID3D11InputLayout* _inputLayout; |
| 296 |
std::uint64_t _inputLayoutFingerprint; // vertex-format fingerprint the cached input layout was built for |
| 297 |
ShaderByteCode _vsByteCode; // kept for building input layouts |
| 298 |
CBufferSlotList _vsCBuffers; |
| 299 |
CBufferSlotList _psCBuffers; |
| 300 |
std::uint32_t _vsTextureMask = 0; // texture/sampler registers the stage binds (from bytecode reflection) |
| 301 |
std::uint32_t _psTextureMask = 0; |
| 302 |
|
| 303 |
void PerformIntrospection(); |
| 304 |
void ImportReflection(); |
| 305 |
/** @brief Compiles `_reflection`'s HLSL stage sources and reflects their constant buffers (called during introspection) */ |
| 306 |
void CompileHlsl(); |
| 307 |
/** @brief Reflects the constant buffers and used texture/sampler registers of one compiled stage bytecode */ |
| 308 |
void ReflectStageCBuffers(const void* byteCode, std::size_t byteCodeSize, CBufferSlotList& slots, std::uint32_t& textureMask); |
| 309 |
}; |
| 310 |
} |