#include "common.hpp" #include "../internal.hpp" #include "../webgpu/gpu.hpp" #include "aurora/aurora.h" #include "texture.hpp" #include "texture_convert.hpp" #include "../gx/gx_fmt.hpp" #include #include #include #include #include #include #include namespace aurora::gfx { using webgpu::g_device; using webgpu::g_queue; namespace { Module Log("aurora::gfx"); wgpu::Extent3D physical_size(wgpu::Extent3D size, TextureFormatInfo info) { const uint32_t width = ((size.width + info.blockWidth - 1) / info.blockWidth) * info.blockWidth; const uint32_t height = ((size.height + info.blockHeight - 1) / info.blockHeight) * info.blockHeight; return {.width = width, .height = height, .depthOrArrayLayers = size.depthOrArrayLayers}; } bool setup_swizzle(wgpu::TextureComponentSwizzleDescriptor& swizzle, u32 format) noexcept { if (!webgpu::g_textureComponentSwizzleSupported) { return false; } switch (format) { case GX_TF_R8_PC: swizzle.swizzle.r = wgpu::ComponentSwizzle::R; swizzle.swizzle.g = wgpu::ComponentSwizzle::R; swizzle.swizzle.b = wgpu::ComponentSwizzle::R; swizzle.swizzle.a = wgpu::ComponentSwizzle::R; return true; case GX_TF_RG8_PC: swizzle.swizzle.r = wgpu::ComponentSwizzle::R; swizzle.swizzle.g = wgpu::ComponentSwizzle::R; swizzle.swizzle.b = wgpu::ComponentSwizzle::R; swizzle.swizzle.a = wgpu::ComponentSwizzle::G; return true; default: return false; } } } // namespace TextureHandle new_static_texture_2d(uint32_t width, uint32_t height, uint32_t mips, u32 format, ArrayRef data, bool tlut, const char* label) noexcept { ZoneScoped; auto handle = new_dynamic_texture_2d(width, height, mips, format, label); auto& ref = *handle; ConvertedTexture converted; if (ref.gxFormat != InvalidTextureFormat) { if (tlut) { CHECK(ref.size.height == 1, "new_static_texture_2d[{}]: expected tlut height 1, got {}", label, ref.size.height); CHECK(ref.mipCount == 1, "new_static_texture_2d[{}]: expected tlut mipCount 1, got {}", label, ref.mipCount); converted = convert_tlut(ref.gxFormat, ref.size.width, data); } else { converted = convert_texture(ref.gxFormat, ref.size.width, ref.size.height, ref.mipCount, data); } if (!converted.data.empty()) { data = converted.data; ref.hasArbitraryMips = converted.hasArbitraryMips; } } uint32_t offset = 0; for (uint32_t mip = 0; mip < mips; ++mip) { const wgpu::Extent3D mipSize{ .width = std::max(ref.size.width >> mip, 1u), .height = std::max(ref.size.height >> mip, 1u), .depthOrArrayLayers = ref.size.depthOrArrayLayers, }; const auto info = format_info(ref.format); const auto physicalSize = physical_size(mipSize, info); const uint32_t widthBlocks = physicalSize.width / info.blockWidth; const uint32_t heightBlocks = physicalSize.height / info.blockHeight; const uint32_t bytesPerRow = widthBlocks * info.blockSize; const uint32_t dataSize = bytesPerRow * heightBlocks * mipSize.depthOrArrayLayers; CHECK(offset + dataSize <= data.size(), "new_static_texture_2d[{}]: expected at least {} bytes, got {}", label, offset + dataSize, data.size()); const wgpu::TexelCopyTextureInfo dstView{ .texture = ref.texture, .mipLevel = mip, }; if constexpr (UseTextureBuffer) { queue_texture_upload_data(data.data() + offset, dataSize, bytesPerRow, heightBlocks, std::move(dstView), physicalSize); } else { const wgpu::TexelCopyBufferLayout dataLayout{ .bytesPerRow = bytesPerRow, .rowsPerImage = heightBlocks, }; g_queue.WriteTexture(&dstView, data.data() + offset, dataSize, &dataLayout, &physicalSize); } offset += dataSize; } if (data.size() != UINT32_MAX && offset < data.size()) { Log.warn("new_static_texture_2d[{}]: texture used {} bytes, but given {} bytes", label, offset, data.size()); } return handle; } TextureHandle new_dynamic_texture_2d(uint32_t width, uint32_t height, uint32_t mips, u32 gxFormat, const char* label) noexcept { ZoneScopedS(3); const auto wgpuFormat = to_wgpu(gxFormat); const wgpu::Extent3D size{ .width = width, .height = height, .depthOrArrayLayers = 1, }; const wgpu::TextureDescriptor textureDescriptor{ .label = label, .usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::CopyDst, .dimension = wgpu::TextureDimension::e2D, .size = size, .format = wgpuFormat, .mipLevelCount = mips, .sampleCount = 1, }; auto texture = g_device.CreateTexture(&textureDescriptor); const auto viewLabel = fmt::format("{} view", label); wgpu::TextureViewDescriptor textureViewDescriptor{ .label = viewLabel.c_str(), .format = wgpuFormat, .dimension = wgpu::TextureViewDimension::e2D, .mipLevelCount = mips, }; wgpu::TextureComponentSwizzleDescriptor swizzle; if (setup_swizzle(swizzle, gxFormat)) { textureViewDescriptor.nextInChain = &swizzle; } auto textureView = texture.CreateView(&textureViewDescriptor); return std::make_shared(std::move(texture), std::move(textureView), wgpu::TextureView{}, size, wgpuFormat, mips, gxFormat); } TextureHandle new_render_texture(uint32_t width, uint32_t height, u32 gxFormat, const char* label) noexcept { ZoneScoped; const auto wgpuFormat = webgpu::g_graphicsConfig.surfaceConfiguration.format; const wgpu::Extent3D size{ .width = width, .height = height, .depthOrArrayLayers = 1, }; const wgpu::TextureDescriptor textureDescriptor{ .label = label, .usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::CopyDst | wgpu::TextureUsage::RenderAttachment, .dimension = wgpu::TextureDimension::e2D, .size = size, .format = wgpuFormat, .mipLevelCount = 1, .sampleCount = 1, }; auto texture = g_device.CreateTexture(&textureDescriptor); // Create texture view for color attachments const auto viewLabel = fmt::format("{} view", label); wgpu::TextureViewDescriptor textureViewDescriptor{ .label = viewLabel.c_str(), .format = wgpuFormat, .dimension = wgpu::TextureViewDimension::e2D, }; auto attachmentTextureView = texture.CreateView(&textureViewDescriptor); wgpu::TextureView sampleTextureView = attachmentTextureView; return std::make_shared(std::move(texture), std::move(sampleTextureView), std::move(attachmentTextureView), size, wgpuFormat, 1, gxFormat); } TextureHandle new_conv_texture(uint32_t width, uint32_t height, u32 gxFormat, const char* label) noexcept { ZoneScoped; const auto wgpuFormat = to_wgpu(gxFormat); const wgpu::Extent3D size{ .width = width, .height = height, .depthOrArrayLayers = 1, }; const wgpu::TextureDescriptor textureDescriptor{ .label = label, .usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::RenderAttachment, .dimension = wgpu::TextureDimension::e2D, .size = size, .format = wgpuFormat, .mipLevelCount = 1, .sampleCount = 1, }; auto texture = g_device.CreateTexture(&textureDescriptor); // Create texture view for color attachments const auto viewLabel = fmt::format("{} view", label); wgpu::TextureViewDescriptor textureViewDescriptor{ .label = viewLabel.c_str(), .format = wgpuFormat, .dimension = wgpu::TextureViewDimension::e2D, }; auto attachmentTextureView = texture.CreateView(&textureViewDescriptor); wgpu::TextureView sampleTextureView = attachmentTextureView; return std::make_shared(std::move(texture), std::move(sampleTextureView), std::move(attachmentTextureView), size, wgpuFormat, 1, gxFormat); } void write_texture(TextureRef& ref, ArrayRef data) noexcept { ZoneScoped; ConvertedTexture converted; if (ref.gxFormat != InvalidTextureFormat) { converted = convert_texture(ref.gxFormat, ref.size.width, ref.size.height, ref.mipCount, data); ref.hasArbitraryMips = converted.hasArbitraryMips; if (!converted.data.empty()) { data = converted.data; } } uint32_t offset = 0; for (uint32_t mip = 0; mip < ref.mipCount; ++mip) { const wgpu::Extent3D mipSize{ .width = std::max(ref.size.width >> mip, 1u), .height = std::max(ref.size.height >> mip, 1u), .depthOrArrayLayers = ref.size.depthOrArrayLayers, }; const auto info = format_info(ref.format); const auto physicalSize = physical_size(mipSize, info); const uint32_t widthBlocks = physicalSize.width / info.blockWidth; const uint32_t heightBlocks = physicalSize.height / info.blockHeight; const uint32_t bytesPerRow = widthBlocks * info.blockSize; const uint32_t dataSize = bytesPerRow * heightBlocks * mipSize.depthOrArrayLayers; CHECK(offset + dataSize <= data.size(), "write_texture: expected at least {} bytes, got {}", offset + dataSize, data.size()); const wgpu::TexelCopyTextureInfo dstView{ .texture = ref.texture, .mipLevel = mip, }; if constexpr (UseTextureBuffer) { queue_texture_upload_data(data.data() + offset, dataSize, bytesPerRow, heightBlocks, std::move(dstView), physicalSize); } else { const wgpu::TexelCopyBufferLayout dataLayout{ .bytesPerRow = bytesPerRow, .rowsPerImage = heightBlocks, }; g_queue.WriteTexture(&dstView, data.data() + offset, dataSize, &dataLayout, &physicalSize); } offset += dataSize; } if (data.size() != UINT32_MAX && offset < data.size()) { Log.warn("write_texture: texture used {} bytes, but given {} bytes", offset, data.size()); } } } // namespace aurora::gfx