16#include <glm/gtc/packing.hpp>
17#include <glm/vec2.hpp>
18#include <glm/vec4.hpp>
54 if (status != wgpu::MapAsyncStatus::Success)
56 Log::Error(fmt::format(
"Failed to map buffer: {}", std::string_view(message.data, message.length)));
61 auto mapped =
static_cast<uint8_t *
>(buf.getMappedRange(0, buf.getSize()));
63 for (
size_t i = 0; i < size; i++)
73 case wgpu::TextureFormat::RGBA8UnormSrgb:
74 case wgpu::TextureFormat::RGBA8Unorm:
75 pixel.r = mapped[i * 4 + 0];
76 pixel.g = mapped[i * 4 + 1];
77 pixel.b = mapped[i * 4 + 2];
78 pixel.a = mapped[i * 4 + 3];
80 case wgpu::TextureFormat::BGRA8UnormSrgb:
81 case wgpu::TextureFormat::BGRA8Unorm:
82 pixel.b = mapped[i * 4 + 0];
83 pixel.g = mapped[i * 4 + 1];
84 pixel.r = mapped[i * 4 + 2];
85 pixel.a = mapped[i * 4 + 3];
87 case wgpu::TextureFormat::RGBA16Float: {
88 auto r = std::bit_cast<uint16_t>(std::array<uint8_t, 2>{mapped[i * 8 + 0], mapped[i * 8 + 1]});
89 auto g = std::bit_cast<uint16_t>(std::array<uint8_t, 2>{mapped[i * 8 + 2], mapped[i * 8 + 3]});
90 auto b = std::bit_cast<uint16_t>(std::array<uint8_t, 2>{mapped[i * 8 + 4], mapped[i * 8 + 5]});
91 auto a = std::bit_cast<uint16_t>(std::array<uint8_t, 2>{mapped[i * 8 + 6], mapped[i * 8 + 7]});
92 pixel.r =
static_cast<uint8_t
>(std::clamp(glm::unpackHalf1x16(r), 0.0f, 1.0f) * 255.0f);
93 pixel.g =
static_cast<uint8_t
>(std::clamp(glm::unpackHalf1x16(g), 0.0f, 1.0f) * 255.0f);
94 pixel.b =
static_cast<uint8_t
>(std::clamp(glm::unpackHalf1x16(b), 0.0f, 1.0f) * 255.0f);
95 pixel.a =
static_cast<uint8_t
>(std::clamp(glm::unpackHalf1x16(a), 0.0f, 1.0f) * 255.0f);
98 case wgpu::TextureFormat::Depth32Float: {
99 auto depth = std::bit_cast<float>(
100 std::array<uint8_t, 4>{mapped[i * 4 + 0], mapped[i * 4 + 1], mapped[i * 4 + 2], mapped[i * 4 + 3]});
101 auto depthByte =
static_cast<uint8_t
>(std::clamp(depth, 0.0f, 1.0f) * 255.0f);
102 pixel = glm::u8vec4(depthByte, depthByte, depthByte, 255);
107 data->data.pixels.push_back(pixel);
114 Texture(std::string_view name, wgpu::Texture texture,
bool ownsResources =
true)
121 :
Texture(std::string(descriptor.label.data, descriptor.label.length),
122 deviceContext.GetDevice()->createTexture(descriptor))
129 Write(deviceContext, image, queue);
133 const std::function<glm::u8vec4(glm::uvec2 pos)> &callback)
134 :
Texture(deviceContext, queue, name,
Image(size, callback))
153 other._webgpuTexture =
nullptr;
155 other._ownsResources =
false;
169 _name = std::move(other._name);
172 other._webgpuTexture =
nullptr;
174 other._ownsResources =
false;
184 if (image.
width != this->_webgpuTexture.getWidth() || image.
height != this->_webgpuTexture.getHeight())
186 Log::Warning(
"Image data size does not match texture size.");
190 wgpu::TexelCopyTextureInfo destination;
192 destination.mipLevel = 0;
193 destination.origin = {0, 0, 0};
194 destination.aspect = wgpu::TextureAspect::All;
196 wgpu::TexelCopyBufferLayout source;
198 source.bytesPerRow = image.
channels * textureSize.width;
199 source.rowsPerImage = textureSize.height;
201 const uint32_t rowBytes = source.bytesPerRow;
202 const uint32_t alignedRowBytes = (rowBytes + 255u) & ~255u;
203 if (alignedRowBytes == rowBytes)
205 queue->writeTexture(destination, image.
pixels.data(), rowBytes * source.rowsPerImage, source, textureSize);
209 std::vector<uint8_t> padded(alignedRowBytes * textureSize.height, 0u);
210 const auto *
const src =
reinterpret_cast<const std::byte *
>(image.
pixels.data());
211 for (uint32_t row = 0; row < textureSize.height; ++row)
213 std::memcpy(padded.data() + row * alignedRowBytes, src + row * rowBytes, rowBytes);
216 source.bytesPerRow = alignedRowBytes;
217 queue->writeTexture(destination, padded.data(), alignedRowBytes * source.rowsPerImage, source, textureSize);
233 wgpu::CommandEncoder encoder = deviceContext.
GetDevice()->createCommandEncoder();
235 const uint32_t bytesPerRow = (copySize.width *
_GetBytesPerPixel() + 255) / 256 * 256;
237 bufDesc.size = copySize.height * bytesPerRow;
238 bufDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::MapRead;
240 wgpu::Buffer readbackBuffer = deviceContext.
GetDevice()->createBuffer(bufDesc);
244 srcView.mipLevel = 0;
245 srcView.origin = {0, 0, 0};
246 if (
_webgpuTexture.getFormat() == wgpu::TextureFormat::Depth24Plus ||
247 _webgpuTexture.getFormat() == wgpu::TextureFormat::Depth24PlusStencil8 ||
248 _webgpuTexture.getFormat() == wgpu::TextureFormat::Depth32Float ||
249 _webgpuTexture.getFormat() == wgpu::TextureFormat::Depth32FloatStencil8)
251 srcView.aspect = wgpu::TextureAspect::DepthOnly;
254 srcView.aspect = wgpu::TextureAspect::All;
257 dstView.buffer = readbackBuffer;
258 dstView.layout.offset = 0;
259 dstView.layout.bytesPerRow = bytesPerRow;
260 dstView.layout.rowsPerImage = copySize.height;
262 encoder.copyTextureToBuffer(srcView, dstView, copySize);
263 auto cmd = encoder.finish();
265 auto cmdName = fmt::format(
"{} Readback Command",
_name);
266 queue->submit(1, &cmd);
275 cbInfo.mode = wgpu::CallbackMode::AllowSpontaneous;
277 cbInfo.userdata1 = &cbData;
278 cbInfo.userdata2 =
nullptr;
279 readbackBuffer.mapAsync(wgpu::MapMode::Read, 0, readbackBuffer.getSize(), cbInfo);
282 deviceContext.
GetDevice()->poll(
false,
nullptr);
283 std::this_thread::sleep_for(std::chrono::milliseconds(100));
285 readbackBuffer.release();
310 textureDesc.dimension = wgpu::TextureDimension::_2D;
311 textureDesc.mipLevelCount = 1;
312 textureDesc.sampleCount = 1;
313 textureDesc.format = wgpu::TextureFormat::RGBA8UnormSrgb;
314 textureDesc.usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::RenderAttachment |
315 wgpu::TextureUsage::CopySrc | wgpu::TextureUsage::CopyDst;
316 textureDesc.viewFormats =
nullptr;
317 textureDesc.viewFormatCount = 0;
Definition UnsupportedTextureFormatError.hpp:7
Definition TextureView.hpp:21
Texture & operator=(Texture &&other) noexcept
Definition Texture.hpp:158
wgpu::Texture _webgpuTexture
Definition Texture.hpp:323
Texture & operator=(const Texture &)=delete
static wgpu::TextureDescriptor _BuildDescriptor(std::string_view name, const Image &image)
Definition Texture.hpp:305
bool OwnsResources() const
Definition Texture.hpp:300
Texture(const DeviceContext &deviceContext, const wgpu::TextureDescriptor &descriptor)
Definition Texture.hpp:120
std::string _name
Definition Texture.hpp:325
Texture(const DeviceContext &deviceContext, const Queue &queue, std::string_view name, const Image &image)
Definition Texture.hpp:126
void TakeOwnership()
Definition Texture.hpp:298
Texture(std::string_view name, wgpu::Texture texture, bool ownsResources=true)
Definition Texture.hpp:114
Texture(const Texture &)=delete
uint32_t _GetBytesPerPixel() const
Definition Texture.hpp:321
Texture(Texture &&other) noexcept
Definition Texture.hpp:149
bool _ownsResources
Definition Texture.hpp:326
const Resource::TextureView & GetDefaultView() const
Definition Texture.hpp:289
Texture(const DeviceContext &deviceContext, const Queue &queue, std::string_view name, const glm::uvec2 &size, const std::function< glm::u8vec4(glm::uvec2 pos)> &callback)
Definition Texture.hpp:132
Image RetrieveImage(const DeviceContext &deviceContext, const Queue &queue) const
Reads back the GPU texture and returns it as an Image.
Definition Texture.hpp:229
Resource::TextureView _defaultView
Definition Texture.hpp:324
glm::uvec2 GetSize() const
Definition Texture.hpp:179
void ReleaseOwnership()
Definition Texture.hpp:296
~Texture()
Definition Texture.hpp:138
void Write(const DeviceContext &deviceContext, const Image &image, const Queue &queue)
Definition Texture.hpp:182
Resource::TextureView CreateView(const wgpu::TextureViewDescriptor &descriptor) const
Definition Texture.hpp:291
static void TextureRetrieveCallback(WGPUMapAsyncStatus status, WGPUStringView message, void *userdata1, void *userdata2)
Callback invoked when a readback buffer mapping completes; converts mapped texture data into RGBA8 pi...
Definition Texture.hpp:51
uint32_t GetBytesPerPixel(wgpu::TextureFormat format)
Determines the number of bytes per pixel for a given wgpu texture format.
Definition GetBytesPerPixel.cpp:13
void Error(const T &msg) noexcept(false)
Definition Logger.hpp:34
void Warning(const T &msg) noexcept(false)
Definition Logger.hpp:32
constexpr DefaultFlag Default
Definition webgpu.hpp:78
StringView(const std::string_view &cpp)
Definition webgpu.hpp:618
Extent3D(uint32_t width, uint32_t height, uint32_t depthOrArrayLayers)
Definition webgpu.hpp:643
Definition Texture.hpp:23
bool done
Definition Texture.hpp:28
Image data
Definition Texture.hpp:25
uint32_t bytesPerRow
Definition Texture.hpp:26
wgpu::TextureFormat format
Definition Texture.hpp:27
wgpu::Buffer buffer
Definition Texture.hpp:24
Definition DeviceContext.hpp:7
auto & GetDevice()
Definition DeviceContext.hpp:19
uint32_t height
Definition Image.hpp:20
uint32_t width
Definition Image.hpp:19
int channels
Definition Image.hpp:21
std::vector< glm::u8vec4 > pixels
Definition Image.hpp:22