2022-07-27 15:25:25 +00:00
|
|
|
#include "common.hpp"
|
|
|
|
|
|
|
|
#include "../webgpu/gpu.hpp"
|
|
|
|
#include "../internal.hpp"
|
|
|
|
#include "texture.hpp"
|
|
|
|
#include "texture_convert.hpp"
|
|
|
|
|
|
|
|
#include <magic_enum.hpp>
|
|
|
|
|
|
|
|
namespace aurora::gfx {
|
|
|
|
static Module Log("aurora::gfx");
|
|
|
|
|
|
|
|
using webgpu::g_device;
|
|
|
|
using webgpu::g_queue;
|
|
|
|
|
|
|
|
struct TextureFormatInfo {
|
|
|
|
uint8_t blockWidth;
|
|
|
|
uint8_t blockHeight;
|
|
|
|
uint8_t blockSize;
|
|
|
|
bool compressed;
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
static TextureFormatInfo format_info(wgpu::TextureFormat format) {
|
2022-07-27 15:25:25 +00:00
|
|
|
switch (format) {
|
2022-08-09 06:05:33 +00:00
|
|
|
DEFAULT_FATAL("unimplemented texture format {}", magic_enum::enum_name(format));
|
2022-08-02 20:37:56 +00:00
|
|
|
case wgpu::TextureFormat::R8Unorm:
|
2022-07-27 15:25:25 +00:00
|
|
|
return {1, 1, 1, false};
|
2022-08-02 20:37:56 +00:00
|
|
|
case wgpu::TextureFormat::R16Sint:
|
2022-07-27 15:25:25 +00:00
|
|
|
return {1, 1, 2, false};
|
2022-08-02 20:37:56 +00:00
|
|
|
case wgpu::TextureFormat::RGBA8Unorm:
|
|
|
|
case wgpu::TextureFormat::R32Float:
|
2022-07-27 15:25:25 +00:00
|
|
|
return {1, 1, 4, false};
|
2022-08-02 20:37:56 +00:00
|
|
|
case wgpu::TextureFormat::BC1RGBAUnorm:
|
2022-07-27 15:25:25 +00:00
|
|
|
return {4, 4, 8, true};
|
|
|
|
}
|
|
|
|
}
|
2022-08-02 20:37:56 +00:00
|
|
|
static wgpu::Extent3D physical_size(wgpu::Extent3D size, TextureFormatInfo info) {
|
2022-07-27 15:25:25 +00:00
|
|
|
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, height, size.depthOrArrayLayers};
|
|
|
|
}
|
|
|
|
|
2022-08-02 20:37:56 +00:00
|
|
|
TextureHandle new_static_texture_2d(uint32_t width, uint32_t height, uint32_t mips, u32 format, ArrayRef<uint8_t> data,
|
|
|
|
const char* label) noexcept {
|
2022-07-27 15:25:25 +00:00
|
|
|
auto handle = new_dynamic_texture_2d(width, height, mips, format, label);
|
|
|
|
const auto& ref = *handle;
|
|
|
|
|
|
|
|
ByteBuffer buffer;
|
|
|
|
if (ref.gxFormat != InvalidTextureFormat) {
|
|
|
|
buffer = convert_texture(ref.gxFormat, ref.size.width, ref.size.height, ref.mipCount, data);
|
|
|
|
if (!buffer.empty()) {
|
|
|
|
data = {buffer.data(), buffer.size()};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t offset = 0;
|
|
|
|
for (uint32_t mip = 0; mip < mips; ++mip) {
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::Extent3D mipSize{
|
2022-07-27 15:25:25 +00:00
|
|
|
.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;
|
2022-08-09 06:05:33 +00:00
|
|
|
CHECK(offset + dataSize <= data.size(), "new_static_texture_2d[{}]: expected at least {} bytes, got {}", label,
|
|
|
|
offset + dataSize, data.size());
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::ImageCopyTexture dstView{
|
2022-07-27 15:25:25 +00:00
|
|
|
.texture = ref.texture,
|
|
|
|
.mipLevel = mip,
|
|
|
|
};
|
|
|
|
// const auto range = push_texture_data(data.data() + offset, dataSize, bytesPerRow, heightBlocks);
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::TextureDataLayout dataLayout{
|
2022-07-27 15:25:25 +00:00
|
|
|
// .offset = range.offset,
|
|
|
|
.bytesPerRow = bytesPerRow,
|
|
|
|
.rowsPerImage = heightBlocks,
|
|
|
|
};
|
|
|
|
// TODO
|
|
|
|
// g_textureUploads.emplace_back(dataLayout, std::move(dstView), physicalSize);
|
2022-08-02 20:37:56 +00:00
|
|
|
g_queue.WriteTexture(&dstView, data.data() + offset, dataSize, &dataLayout, &physicalSize);
|
2022-07-27 15:25:25 +00:00
|
|
|
offset += dataSize;
|
|
|
|
}
|
|
|
|
if (data.size() != UINT32_MAX && offset < data.size()) {
|
|
|
|
Log.report(LOG_WARNING, FMT_STRING("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 format,
|
|
|
|
const char* label) noexcept {
|
|
|
|
const auto wgpuFormat = to_wgpu(format);
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::Extent3D size{
|
2022-07-27 15:25:25 +00:00
|
|
|
.width = width,
|
|
|
|
.height = height,
|
|
|
|
.depthOrArrayLayers = 1,
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::TextureDescriptor textureDescriptor{
|
2022-07-27 15:25:25 +00:00
|
|
|
.label = label,
|
2022-08-02 20:37:56 +00:00
|
|
|
.usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::CopyDst,
|
|
|
|
.dimension = wgpu::TextureDimension::e2D,
|
2022-07-27 15:25:25 +00:00
|
|
|
.size = size,
|
|
|
|
.format = wgpuFormat,
|
|
|
|
.mipLevelCount = mips,
|
|
|
|
.sampleCount = 1,
|
|
|
|
};
|
|
|
|
const auto viewLabel = fmt::format(FMT_STRING("{} view"), label);
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::TextureViewDescriptor textureViewDescriptor{
|
2022-07-27 15:25:25 +00:00
|
|
|
.label = viewLabel.c_str(),
|
|
|
|
.format = wgpuFormat,
|
2022-08-02 20:37:56 +00:00
|
|
|
.dimension = wgpu::TextureViewDimension::e2D,
|
2022-07-27 15:25:25 +00:00
|
|
|
.mipLevelCount = mips,
|
|
|
|
.arrayLayerCount = WGPU_ARRAY_LAYER_COUNT_UNDEFINED,
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
auto texture = g_device.CreateTexture(&textureDescriptor);
|
|
|
|
auto textureView = texture.CreateView(&textureViewDescriptor);
|
|
|
|
return std::make_shared<TextureRef>(std::move(texture), std::move(textureView), size, wgpuFormat, mips, format,
|
|
|
|
false);
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureHandle new_render_texture(uint32_t width, uint32_t height, u32 fmt, const char* label) noexcept {
|
2022-08-02 20:37:56 +00:00
|
|
|
const auto wgpuFormat = webgpu::g_graphicsConfig.swapChainDescriptor.format;
|
|
|
|
const wgpu::Extent3D size{
|
2022-07-27 15:25:25 +00:00
|
|
|
.width = width,
|
|
|
|
.height = height,
|
|
|
|
.depthOrArrayLayers = 1,
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::TextureDescriptor textureDescriptor{
|
2022-07-27 15:25:25 +00:00
|
|
|
.label = label,
|
2022-08-02 20:37:56 +00:00
|
|
|
.usage = wgpu::TextureUsage::TextureBinding | wgpu::TextureUsage::CopyDst,
|
|
|
|
.dimension = wgpu::TextureDimension::e2D,
|
2022-07-27 15:25:25 +00:00
|
|
|
.size = size,
|
|
|
|
.format = wgpuFormat,
|
|
|
|
.mipLevelCount = 1,
|
|
|
|
.sampleCount = 1,
|
|
|
|
};
|
|
|
|
const auto viewLabel = fmt::format(FMT_STRING("{} view"), label);
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::TextureViewDescriptor textureViewDescriptor{
|
2022-07-27 15:25:25 +00:00
|
|
|
.label = viewLabel.c_str(),
|
|
|
|
.format = wgpuFormat,
|
2022-08-02 20:37:56 +00:00
|
|
|
.dimension = wgpu::TextureViewDimension::e2D,
|
2022-07-27 15:25:25 +00:00
|
|
|
.mipLevelCount = WGPU_MIP_LEVEL_COUNT_UNDEFINED,
|
|
|
|
.arrayLayerCount = WGPU_ARRAY_LAYER_COUNT_UNDEFINED,
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
auto texture = g_device.CreateTexture(&textureDescriptor);
|
|
|
|
auto textureView = texture.CreateView(&textureViewDescriptor);
|
|
|
|
return std::make_shared<TextureRef>(std::move(texture), std::move(textureView), size, wgpuFormat, 1, fmt, true);
|
2022-07-27 15:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void write_texture(const TextureRef& ref, ArrayRef<uint8_t> data) noexcept {
|
|
|
|
ByteBuffer buffer;
|
|
|
|
if (ref.gxFormat != InvalidTextureFormat) {
|
|
|
|
buffer = convert_texture(ref.gxFormat, ref.size.width, ref.size.height, ref.mipCount, data);
|
|
|
|
if (!buffer.empty()) {
|
|
|
|
data = {buffer.data(), buffer.size()};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t offset = 0;
|
|
|
|
for (uint32_t mip = 0; mip < ref.mipCount; ++mip) {
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::Extent3D mipSize{
|
2022-07-27 15:25:25 +00:00
|
|
|
.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;
|
2022-08-09 06:05:33 +00:00
|
|
|
CHECK(offset + dataSize <= data.size(), "write_texture: expected at least {} bytes, got {}", offset + dataSize,
|
|
|
|
data.size());
|
2022-08-02 20:37:56 +00:00
|
|
|
// auto dstView = wgpu::ImageCopyTexture{
|
2022-07-27 15:25:25 +00:00
|
|
|
// .texture = ref.texture,
|
|
|
|
// .mipLevel = mip,
|
|
|
|
// };
|
|
|
|
// const auto range = push_texture_data(data.data() + offset, dataSize, bytesPerRow, heightBlocks);
|
2022-08-02 20:37:56 +00:00
|
|
|
// const auto dataLayout = wgpu::TextureDataLayout{
|
2022-07-27 15:25:25 +00:00
|
|
|
// .offset = range.offset,
|
|
|
|
// .bytesPerRow = bytesPerRow,
|
|
|
|
// .rowsPerImage = heightBlocks,
|
|
|
|
// };
|
|
|
|
// g_textureUploads.emplace_back(dataLayout, std::move(dstView), physicalSize);
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::ImageCopyTexture dstView{
|
2022-07-27 15:25:25 +00:00
|
|
|
.texture = ref.texture,
|
|
|
|
.mipLevel = mip,
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
const wgpu::TextureDataLayout dataLayout{
|
2022-07-27 15:25:25 +00:00
|
|
|
.bytesPerRow = bytesPerRow,
|
|
|
|
.rowsPerImage = heightBlocks,
|
|
|
|
};
|
2022-08-02 20:37:56 +00:00
|
|
|
g_queue.WriteTexture(&dstView, data.data() + offset, dataSize, &dataLayout, &physicalSize);
|
2022-07-27 15:25:25 +00:00
|
|
|
offset += dataSize;
|
|
|
|
}
|
|
|
|
if (data.size() != UINT32_MAX && offset < data.size()) {
|
|
|
|
Log.report(LOG_WARNING, FMT_STRING("write_texture: texture used {} bytes, but given {} bytes"), offset,
|
|
|
|
data.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace aurora::gfx
|