RavEngine
Loading...
Searching...
No Matches
Texture.hpp
1#pragma once
2#include "Types.hpp"
3#include "Span.hpp"
4#include <cstdint>
5#include <limits>
6#include <string_view>
7#include <array>
8#include "TextureFormat.hpp"
9#include "SubresourceRange.hpp"
10
11#if RGL_VK_AVAILABLE
12#include <volk.h>
13#endif
14
15#if RGL_WEBGPU_AVAILABLE
16#include <emscripten/html5_webgpu.h>
17#endif
18
19namespace RGL {
20
21#if RGL_MTL_AVAILABLE
22 struct TextureMTL;
23#endif
24
25 struct Dimension {
26 uint32_t width = 0, height = 0;
27 };
28
29 enum class TextureType : uint8_t {
30 T1D,
31 T2D,
32 T3D,
33 };
34
35 enum class TilingMode : uint8_t {
36 Optimal,
37 Linear
38 };
39#if RGL_DX12_AVAILABLE
40 struct TextureD3D12;
41#endif
42
43 struct TextureView {
44#if RGL_VK_AVAILABLE || RGL_DX12_AVAILABLE
45 Dimension viewSize{ 0,0 };
46#endif
47#if RGL_MTL_AVAILABLE
48 struct
49#else
50 union
51#endif
53#if RGL_MTL_AVAILABLE
54 struct mtl_t {
55 const TextureMTL* texture = nullptr;
56 uint32_t mip = 0;
57 constexpr static decltype(mip) ALL_MIPS = std::numeric_limits<decltype(mip)>::max();
58 bool representsBindless = false;
59 } mtl;
60#endif
61#if RGL_WEBGPU_AVAILABLE
62 WGPUTextureView wg;
63 NativeHandles(decltype(wg) wg) : wg(wg) {}
64#endif
65#if RGL_DX12_AVAILABLE
66 struct dx {
67 constexpr static uint32_t unallocated = std::numeric_limits<uint32_t>::max();
68 uint32_t
69 dsvIDX = unallocated,
70 rtvIDX = unallocated,
71 srvIDX = unallocated,
72 uavIDX = unallocated;
73
74 bool representsBindless = false;
75
76 bool dsvAllocated() const {
77 return dsvIDX != unallocated;
78 }
79 bool rtvAllocated() const {
80 return rtvIDX != unallocated;
81 }
82 bool srvAllocated() const {
83 return srvIDX != unallocated;
84 }
85 bool uavAllocated() const {
86 return uavIDX != unallocated;
87 }
88 const TextureD3D12* parentResource;
89 covered_mips_t coveredMips = 0;
90 covered_layers_t coveredLayers = 0;
91 } dx;
92 NativeHandles(const decltype(dx)& dx) : dx(dx) {}
93#endif
94#if RGL_VK_AVAILABLE
95 struct vk {
96 VkImageView view = VK_NULL_HANDLE;
97 covered_mips_t coveredMips = 0;
98 covered_layers_t coveredLayers = 0;
99 struct bindlessInfo {
100 VkDescriptorSet bindlessSet = NULL;
101
102 } bindlessInfo;
103 }
104 vk;
105 NativeHandles(decltype(vk.view) view, decltype(vk.coveredMips) mips, decltype(vk.coveredLayers) layers) : vk{ view, mips, layers } {}
106 NativeHandles(decltype(vk.bindlessInfo) set) : vk{ .bindlessInfo = set } {}
107#endif
108
109 NativeHandles() {}
110
111 } texture;
112
113#if RGL_VK_AVAILABLE
114 const RGL::ITexture* parent = nullptr;
115 TextureView(decltype(parent) parent, VkImageView in_img, covered_mips_t mips, covered_layers_t layers, Dimension dim) : parent(parent), viewSize(dim), texture(in_img, mips, layers) {}
116 TextureView(decltype(texture.vk.bindlessInfo) bindless) : texture(bindless) {}
117#endif
118
119#if RGL_DX12_AVAILABLE
120 TextureView(decltype(texture.dx) dx) : texture(dx) {}
121#endif
122
123#if RGL_WEBGPU_AVAILABLE
124 TextureView(decltype(texture.wg) wg) : texture(wg) {}
125#endif
126
127#if RGL_MTL_AVAILABLE
128 TextureView(const decltype(texture.mtl)& inTexture) {
129 texture.mtl = inTexture;
130 }
131#endif
132 TextureView() {}
133
134 uint32_t GetReadonlyBindlessTextureHandle();
135 };
136
137
139 TextureUsage usage;
140 TextureAspect aspect;
141 uint32_t width = 0, height = 0, depth = 1, mipLevels = 1, arrayLayers = 1;
142 std::array<float, 4> optimizedClearValue{ 0,0,0,0 };
143 TextureType imageType = decltype(imageType)::T2D;
144 TextureFormat format;
145 TilingMode mode = decltype(mode)::Optimal;
146 ResourceLayout initialLayout = ResourceLayout::Undefined;
147 bool isCubemap = false;
148 bool readbackEnabled = false;
149 std::string_view debugName;
150 };
151
153
154 virtual TextureView GetView() const = 0;
155 };
156
158 uint32_t mip = 0;
159 uint32_t layer = 0;
160 };
161
163 untyped_span data{nullptr, 0};
164 TextureUploadData(const decltype(data)& data) : data(data) {}
166 };
167
168 class ITexture {
169 protected:
170 Dimension size;
171 public:
172 ITexture(decltype(size) size) : size(size) {}
173 virtual Dimension GetSize() const = 0;
174 virtual TextureView GetDefaultView() const = 0;
175 virtual TextureView GetViewForMip(uint32_t mip) const = 0;
176 virtual uint8_t GetNumMips() const = 0;
177 virtual RGLCustomTextureViewPtr MakeCustomTextureView(const CustomTextureViewConfig& config) const = 0;
178 };
179
180}
Definition Texture.hpp:152
Definition Texture.hpp:168
Definition Span.hpp:11
uint32 uint32_t
Definition fwd.hpp:131
Definition Texture.hpp:157
Definition Texture.hpp:25
Definition TextureFormat.hpp:99
Definition Texture.hpp:138
Definition D3D12Texture.hpp:17
Definition MTLTexture.hpp:12
Definition Texture.hpp:162
Definition TextureFormat.hpp:88
Definition Texture.hpp:43
Definition Texture.hpp:52