Add ASTC DDS support to texture_replacements (#226)

This commit is contained in:
Luke Street
2026-06-06 07:40:41 -06:00
committed by GitHub
parent c31d6dadcb
commit b3afea435a
7 changed files with 190 additions and 45 deletions
+56
View File
@@ -121,6 +121,62 @@ std::optional<wgpu::TextureFormat> resolve_dx10_format(uint32_t dxgiFormat) noex
return wgpu::TextureFormat::BC5RGUnorm;
case 98:
return wgpu::TextureFormat::BC7RGBAUnorm;
case 134:
return wgpu::TextureFormat::ASTC4x4Unorm;
case 135:
return wgpu::TextureFormat::ASTC4x4UnormSrgb;
case 138:
return wgpu::TextureFormat::ASTC5x4Unorm;
case 139:
return wgpu::TextureFormat::ASTC5x4UnormSrgb;
case 142:
return wgpu::TextureFormat::ASTC5x5Unorm;
case 143:
return wgpu::TextureFormat::ASTC5x5UnormSrgb;
case 146:
return wgpu::TextureFormat::ASTC6x5Unorm;
case 147:
return wgpu::TextureFormat::ASTC6x5UnormSrgb;
case 150:
return wgpu::TextureFormat::ASTC6x6Unorm;
case 151:
return wgpu::TextureFormat::ASTC6x6UnormSrgb;
case 154:
return wgpu::TextureFormat::ASTC8x5Unorm;
case 155:
return wgpu::TextureFormat::ASTC8x5UnormSrgb;
case 158:
return wgpu::TextureFormat::ASTC8x6Unorm;
case 159:
return wgpu::TextureFormat::ASTC8x6UnormSrgb;
case 162:
return wgpu::TextureFormat::ASTC8x8Unorm;
case 163:
return wgpu::TextureFormat::ASTC8x8UnormSrgb;
case 166:
return wgpu::TextureFormat::ASTC10x5Unorm;
case 167:
return wgpu::TextureFormat::ASTC10x5UnormSrgb;
case 170:
return wgpu::TextureFormat::ASTC10x6Unorm;
case 171:
return wgpu::TextureFormat::ASTC10x6UnormSrgb;
case 174:
return wgpu::TextureFormat::ASTC10x8Unorm;
case 175:
return wgpu::TextureFormat::ASTC10x8UnormSrgb;
case 178:
return wgpu::TextureFormat::ASTC10x10Unorm;
case 179:
return wgpu::TextureFormat::ASTC10x10UnormSrgb;
case 182:
return wgpu::TextureFormat::ASTC12x10Unorm;
case 183:
return wgpu::TextureFormat::ASTC12x10UnormSrgb;
case 186:
return wgpu::TextureFormat::ASTC12x12Unorm;
case 187:
return wgpu::TextureFormat::ASTC12x12UnormSrgb;
default:
return std::nullopt;
}
-38
View File
@@ -13,7 +13,6 @@
#include <utility>
#include <fmt/format.h>
#include <magic_enum.hpp>
#include <tracy/Tracy.hpp>
#include <webgpu/webgpu_cpp.h>
@@ -24,8 +23,6 @@ using webgpu::g_queue;
namespace {
Module Log("aurora::gfx");
constexpr u32 div_ceil(u32 value, u32 divisor) noexcept { return (value + divisor - 1) / divisor; }
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;
@@ -56,41 +53,6 @@ bool setup_swizzle(wgpu::TextureComponentSwizzleDescriptor& swizzle, u32 format)
}
} // namespace
TextureFormatInfo format_info(wgpu::TextureFormat format) noexcept {
switch (format) {
DEFAULT_FATAL("unimplemented texture format {}", magic_enum::enum_name(format));
case wgpu::TextureFormat::R8Unorm:
return {1, 1, 1, false};
case wgpu::TextureFormat::RG8Unorm:
case wgpu::TextureFormat::R16Sint:
return {1, 1, 2, false};
case wgpu::TextureFormat::RGBA8Unorm:
case wgpu::TextureFormat::BGRA8Unorm:
case wgpu::TextureFormat::R32Float:
return {1, 1, 4, false};
case wgpu::TextureFormat::BC1RGBAUnorm:
return {4, 4, 8, true};
case wgpu::TextureFormat::BC3RGBAUnorm:
case wgpu::TextureFormat::BC5RGUnorm:
case wgpu::TextureFormat::BC7RGBAUnorm:
return {4, 4, 16, true};
}
}
uint64_t calc_texture_size(wgpu::TextureFormat format, u32 width, u32 height, u32 mips) noexcept {
const auto info = format_info(format);
uint64_t total = 0;
for (uint32_t mip = 0; mip < mips; ++mip) {
const uint32_t mipWidth = std::max(width >> mip, 1u);
const uint32_t mipHeight = std::max(height >> mip, 1u);
const uint64_t widthBlocks = div_ceil(mipWidth, info.blockWidth);
const uint64_t heightBlocks = div_ceil(mipHeight, info.blockHeight);
const uint64_t mipBytes = widthBlocks * heightBlocks * info.blockSize;
total += mipBytes;
}
return total;
}
TextureHandle new_static_texture_2d(uint32_t width, uint32_t height, uint32_t mips, u32 format, ArrayRef<uint8_t> data,
bool tlut, const char* label) noexcept {
ZoneScoped;
+93
View File
@@ -0,0 +1,93 @@
#include "texture.hpp"
#include "../internal.hpp"
#include <algorithm>
#include <cstdint>
#include <magic_enum.hpp>
namespace aurora::gfx {
namespace {
Module Log("aurora::gfx");
constexpr u32 div_ceil(u32 value, u32 divisor) noexcept { return (value + divisor - 1) / divisor; }
} // namespace
TextureFormatInfo format_info(wgpu::TextureFormat format) noexcept {
switch (format) {
DEFAULT_FATAL("unimplemented texture format {}", magic_enum::enum_name(format));
case wgpu::TextureFormat::R8Unorm:
return {1, 1, 1, false};
case wgpu::TextureFormat::RG8Unorm:
case wgpu::TextureFormat::R16Sint:
return {1, 1, 2, false};
case wgpu::TextureFormat::RGBA8Unorm:
case wgpu::TextureFormat::BGRA8Unorm:
case wgpu::TextureFormat::R32Float:
return {1, 1, 4, false};
case wgpu::TextureFormat::BC1RGBAUnorm:
return {4, 4, 8, true};
case wgpu::TextureFormat::BC3RGBAUnorm:
case wgpu::TextureFormat::BC5RGUnorm:
case wgpu::TextureFormat::BC7RGBAUnorm:
return {4, 4, 16, true};
case wgpu::TextureFormat::ASTC4x4Unorm:
case wgpu::TextureFormat::ASTC4x4UnormSrgb:
return {4, 4, 16, true};
case wgpu::TextureFormat::ASTC5x4Unorm:
case wgpu::TextureFormat::ASTC5x4UnormSrgb:
return {5, 4, 16, true};
case wgpu::TextureFormat::ASTC5x5Unorm:
case wgpu::TextureFormat::ASTC5x5UnormSrgb:
return {5, 5, 16, true};
case wgpu::TextureFormat::ASTC6x5Unorm:
case wgpu::TextureFormat::ASTC6x5UnormSrgb:
return {6, 5, 16, true};
case wgpu::TextureFormat::ASTC6x6Unorm:
case wgpu::TextureFormat::ASTC6x6UnormSrgb:
return {6, 6, 16, true};
case wgpu::TextureFormat::ASTC8x5Unorm:
case wgpu::TextureFormat::ASTC8x5UnormSrgb:
return {8, 5, 16, true};
case wgpu::TextureFormat::ASTC8x6Unorm:
case wgpu::TextureFormat::ASTC8x6UnormSrgb:
return {8, 6, 16, true};
case wgpu::TextureFormat::ASTC8x8Unorm:
case wgpu::TextureFormat::ASTC8x8UnormSrgb:
return {8, 8, 16, true};
case wgpu::TextureFormat::ASTC10x5Unorm:
case wgpu::TextureFormat::ASTC10x5UnormSrgb:
return {10, 5, 16, true};
case wgpu::TextureFormat::ASTC10x6Unorm:
case wgpu::TextureFormat::ASTC10x6UnormSrgb:
return {10, 6, 16, true};
case wgpu::TextureFormat::ASTC10x8Unorm:
case wgpu::TextureFormat::ASTC10x8UnormSrgb:
return {10, 8, 16, true};
case wgpu::TextureFormat::ASTC10x10Unorm:
case wgpu::TextureFormat::ASTC10x10UnormSrgb:
return {10, 10, 16, true};
case wgpu::TextureFormat::ASTC12x10Unorm:
case wgpu::TextureFormat::ASTC12x10UnormSrgb:
return {12, 10, 16, true};
case wgpu::TextureFormat::ASTC12x12Unorm:
case wgpu::TextureFormat::ASTC12x12UnormSrgb:
return {12, 12, 16, true};
}
}
uint64_t calc_texture_size(wgpu::TextureFormat format, u32 width, u32 height, u32 mips) noexcept {
const auto info = format_info(format);
uint64_t total = 0;
for (uint32_t mip = 0; mip < mips; ++mip) {
const uint32_t mipWidth = std::max(width >> mip, 1u);
const uint32_t mipHeight = std::max(height >> mip, 1u);
const uint64_t widthBlocks = div_ceil(mipWidth, info.blockWidth);
const uint64_t heightBlocks = div_ceil(mipHeight, info.blockHeight);
const uint64_t mipBytes = widthBlocks * heightBlocks * info.blockSize;
total += mipBytes;
}
return total;
}
} // namespace aurora::gfx
+29
View File
@@ -418,6 +418,35 @@ constexpr bool is_unsupported_texture_format(wgpu::TextureFormat format) {
case wgpu::TextureFormat::BC7RGBAUnorm:
case wgpu::TextureFormat::BC7RGBAUnormSrgb:
return !aurora::webgpu::g_bcTexturesSupported;
case wgpu::TextureFormat::ASTC4x4Unorm:
case wgpu::TextureFormat::ASTC4x4UnormSrgb:
case wgpu::TextureFormat::ASTC5x4Unorm:
case wgpu::TextureFormat::ASTC5x4UnormSrgb:
case wgpu::TextureFormat::ASTC5x5Unorm:
case wgpu::TextureFormat::ASTC5x5UnormSrgb:
case wgpu::TextureFormat::ASTC6x5Unorm:
case wgpu::TextureFormat::ASTC6x5UnormSrgb:
case wgpu::TextureFormat::ASTC6x6Unorm:
case wgpu::TextureFormat::ASTC6x6UnormSrgb:
case wgpu::TextureFormat::ASTC8x5Unorm:
case wgpu::TextureFormat::ASTC8x5UnormSrgb:
case wgpu::TextureFormat::ASTC8x6Unorm:
case wgpu::TextureFormat::ASTC8x6UnormSrgb:
case wgpu::TextureFormat::ASTC8x8Unorm:
case wgpu::TextureFormat::ASTC8x8UnormSrgb:
case wgpu::TextureFormat::ASTC10x5Unorm:
case wgpu::TextureFormat::ASTC10x5UnormSrgb:
case wgpu::TextureFormat::ASTC10x6Unorm:
case wgpu::TextureFormat::ASTC10x6UnormSrgb:
case wgpu::TextureFormat::ASTC10x8Unorm:
case wgpu::TextureFormat::ASTC10x8UnormSrgb:
case wgpu::TextureFormat::ASTC10x10Unorm:
case wgpu::TextureFormat::ASTC10x10UnormSrgb:
case wgpu::TextureFormat::ASTC12x10Unorm:
case wgpu::TextureFormat::ASTC12x10UnormSrgb:
case wgpu::TextureFormat::ASTC12x12Unorm:
case wgpu::TextureFormat::ASTC12x12UnormSrgb:
return !aurora::webgpu::g_astcTexturesSupported;
default:
return false;
}
+10 -7
View File
@@ -56,8 +56,9 @@ static wgpu::Adapter g_adapter;
wgpu::Instance g_instance;
static wgpu::AdapterInfo g_adapterInfo;
static wgpu::SurfaceCapabilities g_surfaceCapabilities;
bool g_bcTexturesSupported;
bool g_textureComponentSwizzleSupported;
bool g_bcTexturesSupported = false;
bool g_astcTexturesSupported = false;
bool g_textureComponentSwizzleSupported = false;
namespace {
@@ -237,7 +238,8 @@ TextureWithSampler create_render_texture(uint32_t width, uint32_t height, bool m
sampleCount = g_graphicsConfig.msaaSamples;
}
if (width == 0 || height == 0) {
Log.fatal("Invalid render texture size! {}x{}, multisampled {}, format {}", width, height, static_cast<uint32_t>(format), multisampled);
Log.fatal("Invalid render texture size! {}x{}, multisampled {}, format {}", width, height,
static_cast<uint32_t>(format), multisampled);
}
const wgpu::TextureDescriptor textureDescriptor{
.label = "Render texture",
@@ -296,9 +298,7 @@ void set_resampler(AuroraSampler sampler) noexcept {
}
}
AuroraSampler get_resampler() noexcept {
return g_Resampler;
}
AuroraSampler get_resampler() noexcept { return g_Resampler; }
Viewport calculate_present_viewport(uint32_t surface_width, uint32_t surface_height, uint32_t content_width,
uint32_t content_height) noexcept {
@@ -806,15 +806,18 @@ bool initialize(AuroraBackend auroraBackend, bool allowCpu) {
requiredLimits.minUniformBufferOffsetAlignment, requiredLimits.minStorageBufferOffsetAlignment);
std::vector<wgpu::FeatureName> requiredFeatures;
g_bcTexturesSupported = false;
g_astcTexturesSupported = false;
g_textureComponentSwizzleSupported = false;
wgpu::SupportedFeatures supportedFeatures;
g_adapter.GetFeatures(&supportedFeatures);
for (size_t i = 0; i < supportedFeatures.featureCount; ++i) {
const auto feature = supportedFeatures.features[i];
if (feature == wgpu::FeatureName::TextureCompressionBC ||
if (feature == wgpu::FeatureName::TextureCompressionBC || feature == wgpu::FeatureName::TextureCompressionASTC ||
feature == wgpu::FeatureName::TextureComponentSwizzle) {
if (feature == wgpu::FeatureName::TextureCompressionBC) {
g_bcTexturesSupported = true;
} else if (feature == wgpu::FeatureName::TextureCompressionASTC) {
g_astcTexturesSupported = true;
} else if (feature == wgpu::FeatureName::TextureComponentSwizzle) {
g_textureComponentSwizzleSupported = true;
}
+1
View File
@@ -51,6 +51,7 @@ extern wgpu::RenderPipeline g_CopyPipeline;
extern wgpu::BindGroup g_CopyBindGroup;
extern wgpu::Instance g_instance;
extern bool g_bcTexturesSupported;
extern bool g_astcTexturesSupported;
extern bool g_textureComponentSwizzleSupported;
bool initialize(AuroraBackend backend, bool allowCpu);