| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "../RhiTypes.h" |
| 4 |
|
| 5 |
#include <cstddef> |
| 6 |
#include <cstdint> |
| 7 |
#include <vector> |
| 8 |
|
| 9 |
#include <Containers/StringView.h> |
| 10 |
|
| 11 |
using namespace Death::Containers; |
| 12 |
|
| 13 |
namespace nCine::RHI::Vulkan |
| 14 |
{ |
| 15 |
/** |
| 16 |
@brief Texture object of the Vulkan backend (aliased as `RHI::Texture`) |
| 17 |
|
| 18 |
Wraps a single, tightly-packed level-0 pixel buffer in host memory and exposes the neutral upload |
| 19 |
surface `Texture.cpp` drives (`TexImage2D`, `TexSubImage2D`, `TexStorage2D`, filter/wrap/swizzle |
| 20 |
setters). It materializes the real `VkImage` + `VkImageView` + `VkSampler` from this surface: a |
| 21 |
staging-buffer upload for CPU textures, or a colour-attachment image for render targets. The sampling |
| 22 |
swizzle is applied through the image view's `VkComponentMapping` (no texel baking, unlike D3D11). Mip |
| 23 |
levels above 0 and compressed formats are accepted but not stored. |
| 24 |
*/ |
| 25 |
class VulkanTexture |
| 26 |
{ |
| 27 |
public: |
| 28 |
/** @brief Number of texture units tracked by the device */ |
| 29 |
static constexpr std::uint32_t MaxTextureUnits = 8; |
| 30 |
|
| 31 |
explicit VulkanTexture(TextureTarget target); |
| 32 |
~VulkanTexture(); |
| 33 |
|
| 34 |
VulkanTexture(const VulkanTexture&) = delete; |
| 35 |
VulkanTexture& operator=(const VulkanTexture&) = delete; |
| 36 |
|
| 37 |
// -- GPU accessors (used by the device draw / render-pass path). Handles are opaque |
| 38 |
// integers so this contract header stays free of <vulkan/vulkan.h>; the .cpp reinterprets them. -- |
| 39 |
|
| 40 |
/** @brief Materializes the `VkImage`/view/sampler if missing (does NOT upload texels; see @ref RecordStreamingUpload()) */ |
| 41 |
void EnsureGpu() const; |
| 42 |
/** @brief `true` if CPU texels are pending upload to the GPU image (a streaming update not yet recorded) */ |
| 43 |
inline bool HasPendingUpload() const { |
| 44 |
return _contentsDirty && _hasCpuData && !_isRenderTarget && !_pixels.empty(); |
| 45 |
} |
| 46 |
/** @brief Records the pending CPU->GPU texel upload (staging copy + layout barriers) into @p cmdBuffer (an opaque `VkCommandBuffer`); the device frees the staging buffer once the frame completes */ |
| 47 |
void RecordStreamingUpload(std::uint64_t cmdBuffer) const; |
| 48 |
/** @brief Returns the `VkImage` (as `std::uint64_t`), creating it on demand; `0` if unavailable */ |
| 49 |
std::uint64_t GpuImage() const; |
| 50 |
/** @brief Returns the sampling `VkImageView` (with the swizzle component mapping) */ |
| 51 |
std::uint64_t GpuView() const; |
| 52 |
/** @brief Returns the `VkSampler` matching the current filter/wrap */ |
| 53 |
std::uint64_t GpuSampler() const; |
| 54 |
/** @brief Returns the `VkFormat` of the image */ |
| 55 |
std::uint32_t GpuFormat() const; |
| 56 |
/** @brief Returns the last-known `VkImageLayout` of the image (device layout tracking) */ |
| 57 |
std::uint32_t GetCurrentLayout() const { |
| 58 |
return _currentLayout; |
| 59 |
} |
| 60 |
/** @brief Records the current `VkImageLayout` (the device updates this after each transition) */ |
| 61 |
void SetCurrentLayout(std::uint32_t layout) const { |
| 62 |
_currentLayout = layout; |
| 63 |
} |
| 64 |
/** @brief Releases the GPU image / view / sampler (on re-allocation and destruction) */ |
| 65 |
void ReleaseGpu() const; |
| 66 |
|
| 67 |
/** @brief Returns a backend-neutral identifier uniquely identifying the texture (feeds material sort keys) */ |
| 68 |
inline std::uint32_t GetUniqueId() const { |
| 69 |
return _handle; |
| 70 |
} |
| 71 |
/** @brief Returns the texture target */ |
| 72 |
inline TextureTarget GetTarget() const { |
| 73 |
return _target; |
| 74 |
} |
| 75 |
|
| 76 |
/** @brief Returns the width of level 0 in texels */ |
| 77 |
inline std::int32_t GetWidth() const { |
| 78 |
return _width; |
| 79 |
} |
| 80 |
/** @brief Returns the height of level 0 in texels */ |
| 81 |
inline std::int32_t GetHeight() const { |
| 82 |
return _height; |
| 83 |
} |
| 84 |
/** @brief Returns the pixel format of the stored texels (after any promotion to the RGBA8 store) */ |
| 85 |
inline PixelFormat GetFormat() const { |
| 86 |
return _format; |
| 87 |
} |
| 88 |
/** @brief Returns the original upload format before promotion (R8/RG8/RGB8 kept) */ |
| 89 |
inline PixelFormat GetUploadFormat() const { |
| 90 |
return _uploadFormat; |
| 91 |
} |
| 92 |
/** @brief Returns the byte distance between two consecutive rows of level 0 */ |
| 93 |
inline std::int32_t GetStrideBytes() const { |
| 94 |
return _strideBytes; |
| 95 |
} |
| 96 |
/** @brief Returns the base pointer of the level-0 texel store (may be `nullptr` before an upload); the single-level store ignores @p level */ |
| 97 |
inline const std::uint8_t* GetPixels(std::int32_t level = 0) const { |
| 98 |
static_cast<void>(level); |
| 99 |
return _pixels.empty() ? nullptr : _pixels.data(); |
| 100 |
} |
| 101 |
/** @brief Returns the horizontal texture-coordinate wrap mode */ |
| 102 |
inline SamplerWrapping GetWrapS() const { |
| 103 |
return _wrap; |
| 104 |
} |
| 105 |
/** @brief Returns the vertical texture-coordinate wrap mode (single stored mode, same as @ref GetWrapS()) */ |
| 106 |
inline SamplerWrapping GetWrapT() const { |
| 107 |
return _wrap; |
| 108 |
} |
| 109 |
/** @brief Returns the four-channel sampling swizzle (identity by default) */ |
| 110 |
inline const SwizzleChannel* GetSwizzle() const { |
| 111 |
return _swizzle; |
| 112 |
} |
| 113 |
/** @brief Returns the magnification filter (alias of @ref GetMagFiltering()) */ |
| 114 |
inline nCine::SamplerFilter GetMagFilter() const { |
| 115 |
return _magFilter; |
| 116 |
} |
| 117 |
/** @brief Returns `true` if the texture is bound as a color render target */ |
| 118 |
inline bool IsRenderTarget() const { |
| 119 |
return _isRenderTarget; |
| 120 |
} |
| 121 |
/** @brief Marks the texture as (or no longer as) a color render target (rebuilds the GPU image with the color-attachment usage when it changes) */ |
| 122 |
inline void SetRenderTarget(bool isRenderTarget) { |
| 123 |
if (isRenderTarget != _isRenderTarget) { |
| 124 |
_isRenderTarget = isRenderTarget; |
| 125 |
ReleaseGpu(); |
| 126 |
} |
| 127 |
} |
| 128 |
/** @brief Returns a writable base pointer of the level-0 texel store (for render-target output) */ |
| 129 |
inline std::uint8_t* MutablePixels() { |
| 130 |
return _pixels.empty() ? nullptr : _pixels.data(); |
| 131 |
} |
| 132 |
|
| 133 |
/** @brief Binds the texture to the specified texture unit on the device */ |
| 134 |
bool Bind(std::uint32_t textureUnit) const; |
| 135 |
/** @brief Binds the texture to texture unit 0 */ |
| 136 |
inline bool Bind() const { |
| 137 |
return Bind(0); |
| 138 |
} |
| 139 |
/** @brief Unbinds the texture from the unit it was last bound to */ |
| 140 |
bool Unbind() const; |
| 141 |
/** @brief Unbinds any texture from the specified texture unit */ |
| 142 |
static bool Unbind(std::uint32_t textureUnit); |
| 143 |
|
| 144 |
/** @brief Allocates level-0 storage of the given format/size and optionally uploads its texels */ |
| 145 |
void TexImage2D(std::int32_t level, PixelFormat format, bool bgr, std::int32_t width, std::int32_t height, const void* data); |
| 146 |
/** @brief Updates a rectangular subregion of level 0 */ |
| 147 |
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); |
| 148 |
/** @brief Allocates immutable level-0 storage of the given format/size (no texels yet) */ |
| 149 |
void TexStorage2D(std::int32_t levels, PixelFormat format, std::int32_t width, std::int32_t height); |
| 150 |
/** @brief Compressed upload (unsupported by this backend, accepted as a no-op) */ |
| 151 |
void CompressedTexImage2D(std::int32_t level, PixelFormat format, std::int32_t width, std::int32_t height, std::int32_t imageSize, const void* data); |
| 152 |
/** @brief Compressed sub-upload (unsupported by this backend, accepted as a no-op) */ |
| 153 |
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); |
| 154 |
/** @brief Reads back level-0 texels into client memory */ |
| 155 |
void GetTexImage(std::int32_t level, PixelFormat format, bool bgr, void* pixels); |
| 156 |
|
| 157 |
/** @brief Sets the minification filter (stored) */ |
| 158 |
void SetMinFiltering(nCine::SamplerFilter filter); |
| 159 |
/** @brief Sets the magnification filter (stored) */ |
| 160 |
void SetMagFiltering(nCine::SamplerFilter filter); |
| 161 |
/** @brief Sets the wrap mode (stored) */ |
| 162 |
void SetWrap(SamplerWrapping wrap); |
| 163 |
/** @brief Sets the sampling swizzle (stored) */ |
| 164 |
void SetSwizzle(SwizzleChannel r, SwizzleChannel g, SwizzleChannel b, SwizzleChannel a); |
| 165 |
/** @brief Sets the highest defined mipmap level (ignored) */ |
| 166 |
void SetMaxLevel(std::int32_t maxLevel); |
| 167 |
/** @brief Sets the client pixel-row alignment of uploads (ignored, uploads are tightly packed) */ |
| 168 |
static void SetUnpackAlignment(std::int32_t alignment); |
| 169 |
|
| 170 |
/** @brief Sets a debug label for the texture (ignored) */ |
| 171 |
void SetObjectLabel(StringView label); |
| 172 |
|
| 173 |
/** @brief Returns the magnification filter */ |
| 174 |
inline nCine::SamplerFilter GetMagFiltering() const { |
| 175 |
return _magFilter; |
| 176 |
} |
| 177 |
|
| 178 |
static bool SupportsImmutableStorage() { |
| 179 |
return false; |
| 180 |
} |
| 181 |
static bool SupportsTextureReadback() { |
| 182 |
return true; |
| 183 |
} |
| 184 |
static void ClearErrors() {} |
| 185 |
static bool CheckErrors() { |
| 186 |
return false; |
| 187 |
} |
| 188 |
static void CheckFormatSupport(PixelFormat format) { |
| 189 |
static_cast<void>(format); |
| 190 |
} |
| 191 |
|
| 192 |
/** @brief Returns the number of bytes occupied by one texel of the given format (0 if unsupported) */ |
| 193 |
static std::int32_t BytesPerPixel(PixelFormat format); |
| 194 |
|
| 195 |
private: |
| 196 |
static std::uint32_t _nextHandle; |
| 197 |
|
| 198 |
std::uint32_t _handle; |
| 199 |
TextureTarget _target; |
| 200 |
PixelFormat _format; |
| 201 |
PixelFormat _uploadFormat; |
| 202 |
std::int32_t _width; |
| 203 |
std::int32_t _height; |
| 204 |
std::int32_t _strideBytes; |
| 205 |
nCine::SamplerFilter _minFilter; |
| 206 |
nCine::SamplerFilter _magFilter; |
| 207 |
SamplerWrapping _wrap; |
| 208 |
SwizzleChannel _swizzle[4]; |
| 209 |
mutable std::uint32_t _textureUnit; |
| 210 |
std::vector<std::uint8_t> _pixels; |
| 211 |
bool _isRenderTarget; |
| 212 |
|
| 213 |
// GPU objects, created lazily from the host store (mutable so the const bind-time accessors can |
| 214 |
// materialize them). Opaque integers so this header needs no <vulkan/vulkan.h>. _contentsDirty forces |
| 215 |
// a texel re-upload; _viewDirty rebuilds the view after a swizzle change; _currentLayout tracks the |
| 216 |
// image's VkImageLayout for the device's render-pass / sampling transitions. |
| 217 |
mutable std::uint64_t _gpuImage; |
| 218 |
mutable std::uint64_t _gpuMemory; |
| 219 |
mutable std::uint64_t _gpuView; |
| 220 |
mutable std::uint64_t _gpuSampler; |
| 221 |
mutable std::uint32_t _currentLayout; |
| 222 |
mutable std::uint32_t _gpuFormat; |
| 223 |
mutable bool _contentsDirty; |
| 224 |
mutable bool _viewDirty; |
| 225 |
mutable bool _hasCpuData; |
| 226 |
mutable nCine::SamplerFilter _samplerFilter; |
| 227 |
mutable SamplerWrapping _samplerWrap; |
| 228 |
|
| 229 |
void Allocate(PixelFormat format, std::int32_t width, std::int32_t height); |
| 230 |
}; |
| 231 |
} |