14#include <glm/gtc/packing.hpp>
15#include <glm/vec2.hpp>
16#include <glm/vec4.hpp>
52 if (status != wgpu::MapAsyncStatus::Success)
54 Log::Error(fmt::format(
"Failed to map buffer: {}", std::string_view(message.data, message.length)));
59 auto mapped =
static_cast<uint8_t *
>(buf.getMappedRange(0, buf.getSize()));
61 for (
size_t i = 0; i < size; i++)
71 case wgpu::TextureFormat::RGBA8UnormSrgb:
72 case wgpu::TextureFormat::RGBA8Unorm:
73 pixel.r = mapped[i * 4 + 0];
74 pixel.g = mapped[i * 4 + 1];
75 pixel.b = mapped[i * 4 + 2];
76 pixel.a = mapped[i * 4 + 3];
78 case wgpu::TextureFormat::BGRA8UnormSrgb:
79 case wgpu::TextureFormat::BGRA8Unorm:
80 pixel.b = mapped[i * 4 + 0];
81 pixel.g = mapped[i * 4 + 1];
82 pixel.r = mapped[i * 4 + 2];
83 pixel.a = mapped[i * 4 + 3];
85 case wgpu::TextureFormat::RGBA16Float: {
86 auto r = std::bit_cast<uint16_t>(std::array<uint8_t, 2>{mapped[i * 8 + 0], mapped[i * 8 + 1]});
87 auto g = std::bit_cast<uint16_t>(std::array<uint8_t, 2>{mapped[i * 8 + 2], mapped[i * 8 + 3]});
88 auto b = std::bit_cast<uint16_t>(std::array<uint8_t, 2>{mapped[i * 8 + 4], mapped[i * 8 + 5]});
89 auto a = std::bit_cast<uint16_t>(std::array<uint8_t, 2>{mapped[i * 8 + 6], mapped[i * 8 + 7]});
90 pixel.r =
static_cast<uint8_t
>(std::clamp(glm::unpackHalf1x16(r), 0.0f, 1.0f) * 255.0f);
91 pixel.g =
static_cast<uint8_t
>(std::clamp(glm::unpackHalf1x16(g), 0.0f, 1.0f) * 255.0f);
92 pixel.b =
static_cast<uint8_t
>(std::clamp(glm::unpackHalf1x16(b), 0.0f, 1.0f) * 255.0f);
93 pixel.a =
static_cast<uint8_t
>(std::clamp(glm::unpackHalf1x16(a), 0.0f, 1.0f) * 255.0f);
96 case wgpu::TextureFormat::Depth32Float: {
97 auto depth = std::bit_cast<float>(
98 std::array<uint8_t, 4>{mapped[i * 4 + 0], mapped[i * 4 + 1], mapped[i * 4 + 2], mapped[i * 4 + 3]});
99 auto depthByte =
static_cast<uint8_t
>(std::clamp(depth, 0.0f, 1.0f) * 255.0f);
100 pixel = glm::u8vec4(depthByte, depthByte, depthByte, 255);
105 data->data.pixels.push_back(pixel);
112 Texture(std::string_view name, wgpu::Texture texture,
bool ownsResources =
true)
119 :
Texture(std::string(descriptor.label.data, descriptor.label.length),
120 context.deviceContext.GetDevice()->createTexture(descriptor))
127 Write(context, image);
131 const std::function<glm::u8vec4(glm::uvec2 pos)> &callback)
152 other._webgpuTexture =
nullptr;
153 other._defaultView =
nullptr;
155 other._ownsResources =
false;
172 _name = std::move(other._name);
175 other._webgpuTexture =
nullptr;
176 other._defaultView =
nullptr;
178 other._ownsResources =
false;
188 if (image.
width != this->_webgpuTexture.getWidth() || image.
height != this->_webgpuTexture.getHeight())
190 Log::Warning(
"Image data size does not match texture size.");
194 wgpu::TexelCopyTextureInfo destination;
196 destination.mipLevel = 0;
197 destination.origin = {0, 0, 0};
198 destination.aspect = wgpu::TextureAspect::All;
200 wgpu::TexelCopyBufferLayout source;
202 source.bytesPerRow = image.
channels * textureSize.width;
203 source.rowsPerImage = textureSize.height;
205 const wgpu::Queue &queue = context.
queue.value();
206 const uint32_t rowBytes = source.bytesPerRow;
207 const uint32_t alignedRowBytes = (rowBytes + 255u) & ~255u;
208 if (alignedRowBytes == rowBytes)
210 queue.writeTexture(destination, image.
pixels.data(), rowBytes * source.rowsPerImage, source, textureSize);
214 std::vector<uint8_t> padded(alignedRowBytes * textureSize.height, 0u);
215 const auto *
const src =
reinterpret_cast<const std::byte *
>(image.
pixels.data());
216 for (uint32_t row = 0; row < textureSize.height; ++row)
218 std::memcpy(padded.data() + row * alignedRowBytes, src + row * rowBytes, rowBytes);
221 source.bytesPerRow = alignedRowBytes;
222 queue.writeTexture(destination, padded.data(), alignedRowBytes * source.rowsPerImage, source, textureSize);
236 const wgpu::Queue &queue = context.
queue.value();
242 const uint32_t bytesPerRow = (copySize.width *
_GetBytesPerPixel() + 255) / 256 * 256;
244 bufDesc.size = copySize.height * bytesPerRow;
245 bufDesc.usage = wgpu::BufferUsage::CopyDst | wgpu::BufferUsage::MapRead;
251 srcView.mipLevel = 0;
252 srcView.origin = {0, 0, 0};
253 if (
_webgpuTexture.getFormat() == wgpu::TextureFormat::Depth24Plus ||
254 _webgpuTexture.getFormat() == wgpu::TextureFormat::Depth24PlusStencil8 ||
255 _webgpuTexture.getFormat() == wgpu::TextureFormat::Depth32Float ||
256 _webgpuTexture.getFormat() == wgpu::TextureFormat::Depth32FloatStencil8)
258 srcView.aspect = wgpu::TextureAspect::DepthOnly;
261 srcView.aspect = wgpu::TextureAspect::All;
264 dstView.buffer = readbackBuffer;
265 dstView.layout.offset = 0;
266 dstView.layout.bytesPerRow = bytesPerRow;
267 dstView.layout.rowsPerImage = copySize.height;
269 encoder.copyTextureToBuffer(srcView, dstView, copySize);
270 auto cmd = encoder.finish();
271 auto cmdName = fmt::format(
"{} Readback Command",
_name);
272 queue.submit(1, &cmd);
281 cbInfo.mode = wgpu::CallbackMode::AllowSpontaneous;
283 cbInfo.userdata1 = &cbData;
284 cbInfo.userdata2 =
nullptr;
285 readbackBuffer.mapAsync(wgpu::MapMode::Read, 0, readbackBuffer.getSize(), cbInfo);
289 std::this_thread::sleep_for(std::chrono::milliseconds(100));
296 inline wgpu::TextureView
CreateView(
const wgpu::TextureViewDescriptor &descriptor)
const
315 textureDesc.dimension = wgpu::TextureDimension::_2D;
316 textureDesc.mipLevelCount = 1;
317 textureDesc.sampleCount = 1;
318 textureDesc.format = wgpu::TextureFormat::RGBA8UnormSrgb;
319 textureDesc.usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::RenderAttachment |
320 wgpu::TextureUsage::CopySrc | wgpu::TextureUsage::CopyDst;
321 textureDesc.viewFormats =
nullptr;
322 textureDesc.viewFormatCount = 0;
Definition UnsupportedTextureFormatError.hpp:7
DeviceContext deviceContext
Definition Context.hpp:43
std::optional< wgpu::Queue > queue
Definition Context.hpp:44
Texture(const Context &context, const wgpu::TextureDescriptor &descriptor)
Definition Texture.hpp:118
wgpu::TextureView GetDefaultView() const
Definition Texture.hpp:294
Texture & operator=(Texture &&other) noexcept
Definition Texture.hpp:158
wgpu::Texture _webgpuTexture
Definition Texture.hpp:328
Texture & operator=(const Texture &)=delete
static wgpu::TextureDescriptor _BuildDescriptor(std::string_view name, const Image &image)
Definition Texture.hpp:310
bool OwnsResources() const
Definition Texture.hpp:305
std::string _name
Definition Texture.hpp:330
void TakeOwnership()
Definition Texture.hpp:303
void Write(const Context &context, const Image &image)
Definition Texture.hpp:186
wgpu::TextureView CreateView(const wgpu::TextureViewDescriptor &descriptor) const
Definition Texture.hpp:296
Texture(std::string_view name, wgpu::Texture texture, bool ownsResources=true)
Definition Texture.hpp:112
Texture(const Texture &)=delete
uint32_t _GetBytesPerPixel() const
Definition Texture.hpp:326
Texture(Texture &&other) noexcept
Definition Texture.hpp:148
bool _ownsResources
Definition Texture.hpp:331
Texture(const Context &context, std::string_view name, const glm::uvec2 &size, const std::function< glm::u8vec4(glm::uvec2 pos)> &callback)
Definition Texture.hpp:130
Image RetrieveImage(const Context &context) const
Reads back the GPU texture and returns it as an Image.
Definition Texture.hpp:234
Texture(const Context &context, std::string_view name, const Image &image)
Definition Texture.hpp:124
glm::uvec2 GetSize() const
Definition Texture.hpp:183
void ReleaseOwnership()
Definition Texture.hpp:301
~Texture()
Definition Texture.hpp:136
wgpu::TextureView _defaultView
Definition Texture.hpp:329
Definition AGPUBuffer.hpp:6
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:49
uint32_t GetBytesPerPixel(wgpu::TextureFormat format)
Determines the number of bytes per pixel for a given wgpu texture format.
Definition GetBytesPerPixel.cpp:13
void Warning(const T &msg) noexcept
Definition Logger.hpp:49
void Error(const T &msg) noexcept
Definition Logger.hpp:51
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:21
bool done
Definition Texture.hpp:26
Image data
Definition Texture.hpp:23
uint32_t bytesPerRow
Definition Texture.hpp:24
wgpu::TextureFormat format
Definition Texture.hpp:25
wgpu::Buffer buffer
Definition Texture.hpp:22
auto & GetDevice()
Definition DeviceContext.hpp:13
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