| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "../RhiTypes.h" |
| 4 |
|
| 5 |
#include <cstddef> |
| 6 |
#include <cstdint> |
| 7 |
|
| 8 |
#include <Containers/SmallVector.h> |
| 9 |
#include <Containers/StringView.h> |
| 10 |
|
| 11 |
using namespace Death::Containers; |
| 12 |
|
| 13 |
// Direct3D 11 interfaces referenced only as opaque pointers here (definitions pulled in by the .cpp) |
| 14 |
struct ID3D11Texture2D; |
| 15 |
struct ID3D11ShaderResourceView; |
| 16 |
struct ID3D11SamplerState; |
| 17 |
|
| 18 |
namespace nCine::RHI::D3D11 |
| 19 |
{ |
| 20 |
/** |
| 21 |
@brief Texture object of the Direct3D 11 backend (aliased as `RHI::Texture`) |
| 22 |
|
| 23 |
Wraps a single, tightly-packed level-0 pixel buffer in host memory and exposes the neutral upload |
| 24 |
surface `Texture.cpp` drives (`TexImage2D`, `TexSubImage2D`, `TexStorage2D`, filter/wrap/swizzle |
| 25 |
setters); binding records the texture on the device. The real `ID3D11Texture2D` + |
| 26 |
`ID3D11ShaderResourceView`/`ID3D11RenderTargetView` are created from this same surface. Mip levels |
| 27 |
above 0 and compressed formats are accepted but not stored. |
| 28 |
*/ |
| 29 |
class D3D11Texture |
| 30 |
{ |
| 31 |
public: |
| 32 |
/** @brief Number of texture units tracked by the device */ |
| 33 |
static constexpr std::uint32_t MaxTextureUnits = 8; |
| 34 |
|
| 35 |
explicit D3D11Texture(TextureTarget target); |
| 36 |
~D3D11Texture(); |
| 37 |
|
| 38 |
D3D11Texture(const D3D11Texture&) = delete; |
| 39 |
D3D11Texture& operator=(const D3D11Texture&) = delete; |
| 40 |
|
| 41 |
/** @brief Returns the shader resource view (created lazily from the host pixels / render-target storage) */ |
| 42 |
ID3D11ShaderResourceView* GetSRV() const; |
| 43 |
/** @brief Returns the sampler state matching the current filter/wrap (created lazily) */ |
| 44 |
ID3D11SamplerState* GetSampler() const; |
| 45 |
/** @brief Returns the underlying texture (created lazily); used by @ref D3D11RenderTarget to build its RTV */ |
| 46 |
ID3D11Texture2D* GetOrCreateTexture2D() const; |
| 47 |
/** @brief Releases the GPU texture / view / sampler (on re-allocation and destruction) */ |
| 48 |
void ReleaseGpu() const; |
| 49 |
|
| 50 |
/** @brief Returns a backend-neutral identifier uniquely identifying the texture (feeds material sort keys) */ |
| 51 |
inline std::uint32_t GetUniqueId() const { |
| 52 |
return _handle; |
| 53 |
} |
| 54 |
/** @brief Returns the texture target */ |
| 55 |
inline TextureTarget GetTarget() const { |
| 56 |
return _target; |
| 57 |
} |
| 58 |
|
| 59 |
/** @brief Returns the width of level 0 in texels */ |
| 60 |
inline std::int32_t GetWidth() const { |
| 61 |
return _width; |
| 62 |
} |
| 63 |
/** @brief Returns the height of level 0 in texels */ |
| 64 |
inline std::int32_t GetHeight() const { |
| 65 |
return _height; |
| 66 |
} |
| 67 |
/** @brief Returns the pixel format of the stored texels (after any promotion to the RGBA8 store) */ |
| 68 |
inline PixelFormat GetFormat() const { |
| 69 |
return _format; |
| 70 |
} |
| 71 |
/** @brief Returns the original upload format before promotion (R8/RG8/RGB8 kept) */ |
| 72 |
inline PixelFormat GetUploadFormat() const { |
| 73 |
return _uploadFormat; |
| 74 |
} |
| 75 |
/** @brief Returns the byte distance between two consecutive rows of level 0 */ |
| 76 |
inline std::int32_t GetStrideBytes() const { |
| 77 |
return _strideBytes; |
| 78 |
} |
| 79 |
/** @brief Returns the base pointer of the level-0 texel store (may be `nullptr` before an upload); the single-level store ignores @p level */ |
| 80 |
inline const std::uint8_t* GetPixels(std::int32_t level = 0) const { |
| 81 |
static_cast<void>(level); |
| 82 |
return _pixels.empty() ? nullptr : _pixels.data(); |
| 83 |
} |
| 84 |
/** @brief Returns the horizontal texture-coordinate wrap mode */ |
| 85 |
inline SamplerWrapping GetWrapS() const { |
| 86 |
return _wrap; |
| 87 |
} |
| 88 |
/** @brief Returns the vertical texture-coordinate wrap mode (single stored mode, same as @ref GetWrapS()) */ |
| 89 |
inline SamplerWrapping GetWrapT() const { |
| 90 |
return _wrap; |
| 91 |
} |
| 92 |
/** @brief Returns the four-channel sampling swizzle (identity by default) */ |
| 93 |
inline const SwizzleChannel* GetSwizzle() const { |
| 94 |
return _swizzle; |
| 95 |
} |
| 96 |
/** @brief Returns the magnification filter (alias of @ref GetMagFiltering()) */ |
| 97 |
inline nCine::SamplerFilter GetMagFilter() const { |
| 98 |
return _magFilter; |
| 99 |
} |
| 100 |
/** @brief Returns `true` if the texture is bound as a color render target */ |
| 101 |
inline bool IsRenderTarget() const { |
| 102 |
return _isRenderTarget; |
| 103 |
} |
| 104 |
/** @brief Marks the texture as (or no longer as) a color render target (rebuilds the GPU texture with the render-target bind flag when it changes) */ |
| 105 |
inline void SetRenderTarget(bool isRenderTarget) { |
| 106 |
if (isRenderTarget != _isRenderTarget) { |
| 107 |
_isRenderTarget = isRenderTarget; |
| 108 |
ReleaseGpu(); |
| 109 |
_contentsDirty = true; |
| 110 |
} |
| 111 |
} |
| 112 |
/** @brief Returns a writable base pointer of the level-0 texel store (for render-target output) */ |
| 113 |
inline std::uint8_t* MutablePixels() { |
| 114 |
return _pixels.empty() ? nullptr : _pixels.data(); |
| 115 |
} |
| 116 |
|
| 117 |
/** @brief Binds the texture to the specified texture unit on the device */ |
| 118 |
bool Bind(std::uint32_t textureUnit) const; |
| 119 |
/** @brief Binds the texture to texture unit 0 */ |
| 120 |
inline bool Bind() const { |
| 121 |
return Bind(0); |
| 122 |
} |
| 123 |
/** @brief Unbinds the texture from the unit it was last bound to */ |
| 124 |
bool Unbind() const; |
| 125 |
/** @brief Unbinds any texture from the specified texture unit */ |
| 126 |
static bool Unbind(std::uint32_t textureUnit); |
| 127 |
|
| 128 |
/** @brief Allocates level-0 storage of the given format/size and optionally uploads its texels */ |
| 129 |
void TexImage2D(std::int32_t level, PixelFormat format, bool bgr, std::int32_t width, std::int32_t height, const void* data); |
| 130 |
/** @brief Updates a rectangular subregion of level 0 */ |
| 131 |
void TexSubImage2D(std::int32_t level, std::int32_t xoffset, std::int32_t yoffset, std::int32_t width, std::int32_t height, PixelFormat format, bool bgr, const void* data); |
| 132 |
/** @brief Allocates immutable level-0 storage of the given format/size (no texels yet) */ |
| 133 |
void TexStorage2D(std::int32_t levels, PixelFormat format, std::int32_t width, std::int32_t height); |
| 134 |
/** @brief Compressed upload (unsupported by this backend, accepted as a no-op) */ |
| 135 |
void CompressedTexImage2D(std::int32_t level, PixelFormat format, std::int32_t width, std::int32_t height, std::int32_t imageSize, const void* data); |
| 136 |
/** @brief Compressed sub-upload (unsupported by this backend, accepted as a no-op) */ |
| 137 |
void CompressedTexSubImage2D(std::int32_t level, std::int32_t xoffset, std::int32_t yoffset, std::int32_t width, std::int32_t height, PixelFormat format, std::int32_t imageSize, const void* data); |
| 138 |
/** @brief Reads back level-0 texels into client memory */ |
| 139 |
void GetTexImage(std::int32_t level, PixelFormat format, bool bgr, void* pixels); |
| 140 |
|
| 141 |
/** @brief Sets the minification filter (stored) */ |
| 142 |
void SetMinFiltering(nCine::SamplerFilter filter); |
| 143 |
/** @brief Sets the magnification filter (stored) */ |
| 144 |
void SetMagFiltering(nCine::SamplerFilter filter); |
| 145 |
/** @brief Sets the wrap mode (stored) */ |
| 146 |
void SetWrap(SamplerWrapping wrap); |
| 147 |
/** @brief Sets the sampling swizzle (stored) */ |
| 148 |
void SetSwizzle(SwizzleChannel r, SwizzleChannel g, SwizzleChannel b, SwizzleChannel a); |
| 149 |
/** @brief Sets the highest defined mipmap level (ignored) */ |
| 150 |
void SetMaxLevel(std::int32_t maxLevel); |
| 151 |
/** @brief Sets the client pixel-row alignment of uploads (ignored, uploads are tightly packed) */ |
| 152 |
static void SetUnpackAlignment(std::int32_t alignment); |
| 153 |
|
| 154 |
/** @brief Sets a debug label for the texture (ignored) */ |
| 155 |
void SetObjectLabel(StringView label); |
| 156 |
|
| 157 |
/** @brief Returns the magnification filter */ |
| 158 |
inline nCine::SamplerFilter GetMagFiltering() const { |
| 159 |
return _magFilter; |
| 160 |
} |
| 161 |
|
| 162 |
static bool SupportsImmutableStorage() { |
| 163 |
return false; |
| 164 |
} |
| 165 |
static bool SupportsTextureReadback() { |
| 166 |
return true; |
| 167 |
} |
| 168 |
static void ClearErrors() {} |
| 169 |
static bool CheckErrors() { |
| 170 |
return false; |
| 171 |
} |
| 172 |
static void CheckFormatSupport(PixelFormat format) { |
| 173 |
static_cast<void>(format); |
| 174 |
} |
| 175 |
|
| 176 |
/** @brief Returns the number of bytes occupied by one texel of the given format (0 if unsupported) */ |
| 177 |
static std::int32_t BytesPerPixel(PixelFormat format); |
| 178 |
|
| 179 |
/** @brief Returns the existing SRV without creating one (device shadow-state scrubbing on destroy) */ |
| 180 |
inline ID3D11ShaderResourceView* PeekSRV() const { |
| 181 |
return _srv; |
| 182 |
} |
| 183 |
/** @brief Returns the existing sampler without creating one */ |
| 184 |
inline ID3D11SamplerState* PeekSampler() const { |
| 185 |
return _sampler; |
| 186 |
} |
| 187 |
|
| 188 |
private: |
| 189 |
static std::uint32_t _nextHandle; |
| 190 |
|
| 191 |
std::uint32_t _handle; |
| 192 |
TextureTarget _target; |
| 193 |
PixelFormat _format; |
| 194 |
PixelFormat _uploadFormat; |
| 195 |
std::int32_t _width; |
| 196 |
std::int32_t _height; |
| 197 |
std::int32_t _strideBytes; |
| 198 |
nCine::SamplerFilter _minFilter; |
| 199 |
nCine::SamplerFilter _magFilter; |
| 200 |
SamplerWrapping _wrap; |
| 201 |
SwizzleChannel _swizzle[4]; |
| 202 |
mutable std::uint32_t _textureUnit; |
| 203 |
SmallVector<std::uint8_t, 0> _pixels; |
| 204 |
// Swizzled copy of _pixels uploaded to the GPU when the sampling swizzle is not the identity. D3D11's |
| 205 |
// base SRV has no per-channel swizzle (unlike GL's GL_TEXTURE_SWIZZLE_*), so the swizzle is baked into |
| 206 |
// the texels instead. Rebuilt lazily whenever the contents or swizzle change. |
| 207 |
mutable SmallVector<std::uint8_t, 0> _swizzledPixels; |
| 208 |
bool _isRenderTarget; |
| 209 |
|
| 210 |
// GPU objects, created lazily from the host store on first bind (mutable so the const bind-time |
| 211 |
// accessors can materialize them); `_contentsDirty` forces a refresh after a CPU upload |
| 212 |
mutable ID3D11Texture2D* _gpuTexture; |
| 213 |
mutable ID3D11ShaderResourceView* _srv; |
| 214 |
mutable ID3D11SamplerState* _sampler; |
| 215 |
mutable bool _contentsDirty; |
| 216 |
mutable bool _hasCpuData; |
| 217 |
mutable nCine::SamplerFilter _samplerMinFilter; |
| 218 |
mutable nCine::SamplerFilter _samplerFilter; |
| 219 |
mutable SamplerWrapping _samplerWrap; |
| 220 |
|
| 221 |
void Allocate(PixelFormat format, std::int32_t width, std::int32_t height); |
| 222 |
/** @brief (Re)creates @ref _gpuTexture + @ref _srv from the host store when missing or dirty */ |
| 223 |
void EnsureGpuTexture() const; |
| 224 |
/** @brief Returns `true` if @ref _swizzle is the identity mapping (R,G,B,A) */ |
| 225 |
bool IsIdentitySwizzle() const; |
| 226 |
/** @brief Returns the texels to upload: @ref _pixels for the identity swizzle, otherwise a swizzle-baked copy */ |
| 227 |
const std::uint8_t* SwizzledUploadPixels() const; |
| 228 |
}; |
| 229 |
} |