Merge pull request #1394 from gjonn/feat/agc-r10g11b11-float-images

feat(agc): load and store R10_G11_B11 float images
This commit is contained in:
Adrià
2026-10-08 15:25:08 +02:00
committed by GitHub
4 changed files with 94 additions and 11 deletions
@@ -96,7 +96,7 @@ constexpr auto MakeFormatLookupTable() {
constexpr auto kFormatLookupTable = MakeFormatLookupTable();
std::uint32_t remapGuestFormat(std::uint32_t guestFormat) {
return guestFormat == 30 || guestFormat == 34 ? 20 : guestFormat;
return guestFormat == 30 || guestFormat == 34 || guestFormat == 43 ? 20 : guestFormat;
}
const FormatEntry& findFormatEntry(std::uint32_t guestFormat) {
@@ -34,6 +34,8 @@ constexpr std::uint32_t LoadGroups = LoadWidth / Threads;
constexpr std::uint32_t Type2D = 9;
constexpr std::uint32_t UnormFormat = 30;
constexpr std::uint32_t UintFormat = 34;
constexpr std::uint32_t FloatFormat = 43;
constexpr std::size_t FloatOffset = 40960;
constexpr std::uint32_t SwizzleXYZ1 = 0x3acu;
constexpr std::uint32_t SwizzleXZY1 = 0x374u;
constexpr std::uint32_t SwizzleZYX1 = 0x32eu;
@@ -249,14 +251,85 @@ void CheckStore(AgcDriver::VulkanDevice& device, std::uint8_t* texels, std::span
}
}
void RequireRefused(AgcDriver::VulkanDevice& device, std::span<const std::uint32_t> code, const std::uint8_t* texels, std::uint32_t swizzle, const std::array<std::uint32_t, 4>& extra, const std::string& reason, const std::string& what) {
constexpr std::array<float, 11> FloatValues{0.0f, 1.0f, 0.5f, 2.0f, 0.25f, 3.0f, 1.5f, 10.0f, 0.75f, 6.5f, 0.125f};
constexpr std::array<std::array<std::uint32_t, 4>, 7> FloatStoreEdges{{
{0xbf800000u, 0x80000000u, 0x7fc00000u, 0xfc000000u},
{0x7f800000u, 0x7149f2cau, 0x477fff00u, 0xf7feffe0u},
{0x3f830000u, 0x3f818000u, 0x35800000u, 0x002f01e0u},
{0x00000001u, 0xff800000u, 0xffc00000u, 0xfc000000u},
{0x477c0000u, 0x4788b800u, 0x33000000u, 0x001effdfu},
{0x36400000u, 0x477e0000u, 0x3dcccccdu, 0x5cdefc01u},
{0x7f800001u, 0x7f810000u, 0x7fa00000u, 0xfa1f07e1u},
}};
constexpr std::array<std::array<std::uint32_t, 4>, 3> FloatLoadEdges{{
{0x3e0u | (0x7c0u << 10u) | (0x03fu << 21u), 0x7f800000u, 0x7f800000u, 0x387c0000u},
{0x001u | (0x041u << 10u) | (0x7bfu << 21u), 0x36000000u, 0x38820000u, 0x477e0000u},
{0x3dfu | (0x001u << 10u), 0x477c0000u, 0x35800000u, 0x00000000u},
}};
std::uint32_t SmallFloat(float value, std::uint32_t mantissaBits) {
if (value == 0.0f) return 0u;
const auto bits = std::bit_cast<std::uint32_t>(value);
const auto exponent = ((bits >> 23u) & 0xffu) - 127u + 15u;
return (exponent << mantissaBits) | ((bits & 0x7fffffu) >> (23u - mantissaBits));
}
std::uint32_t FloatTexel(std::uint32_t index) {
return SmallFloat(FloatValues[index % 11u], 5u) | (SmallFloat(FloatValues[(index + 4u) % 11u], 6u) << 10u) | (SmallFloat(FloatValues[(index + 8u) % 11u], 6u) << 21u);
}
void CheckFloatLoad(AgcDriver::VulkanDevice& device, std::uint8_t* texels) {
for (std::uint32_t index = 0; index < LoadWidth; ++index) {
const auto texel = index < FloatLoadEdges.size() ? FloatLoadEdges[index][0] : FloatTexel(index);
std::memcpy(texels + index * 4u, &texel, 4u);
}
Run(device, LoadXyzw, TextureDescriptor(texels, FloatFormat, SwizzleXYZ1, LoadWidth), {}, LoadGroups);
for (std::uint32_t index = 0; index < LoadWidth; ++index) {
std::array<std::uint32_t, 4> expected{std::bit_cast<std::uint32_t>(FloatValues[index % 11u]), std::bit_cast<std::uint32_t>(FloatValues[(index + 4u) % 11u]), std::bit_cast<std::uint32_t>(FloatValues[(index + 8u) % 11u]), std::bit_cast<std::uint32_t>(1.0f)};
if (index < FloatLoadEdges.size()) {
expected = {FloatLoadEdges[index][1], FloatLoadEdges[index][2], FloatLoadEdges[index][3], std::bit_cast<std::uint32_t>(1.0f)};
}
for (std::uint32_t component = 0; component < 4u; ++component) {
const auto actual = Output[index * 4u + component];
Require(actual == expected[component], "image_load of R10_G11_B11_FLOAT: texel " + std::to_string(index) + " component " + std::to_string(component) + " is " + Hex(actual) + ", expected " + Hex(expected[component]));
}
}
}
void CheckFloatStore(AgcDriver::VulkanDevice& device, std::uint8_t* texels) {
std::fill(texels, texels + Threads * 4u, static_cast<std::uint8_t>(0xa5u));
Input.fill(0u);
for (std::uint32_t tid = 0; tid < Threads; ++tid) {
Input[tid * 4u + 0u] = std::bit_cast<std::uint32_t>(FloatValues[tid % 11u]);
Input[tid * 4u + 1u] = std::bit_cast<std::uint32_t>(FloatValues[(tid + 4u) % 11u]);
Input[tid * 4u + 2u] = std::bit_cast<std::uint32_t>(FloatValues[(tid + 8u) % 11u]);
if (tid < FloatStoreEdges.size()) {
for (std::uint32_t component = 0; component < 3u; ++component) {
Input[tid * 4u + component] = FloatStoreEdges[tid][component];
}
}
}
Run(device, StoreXyz, TextureDescriptor(texels, FloatFormat, SwizzleXYZ1, Threads), BufferDescriptor(Input.data(), static_cast<std::uint32_t>(Input.size() * 4u)), 1);
AgcDriver::Graphics::StorageTexture::FlushPending(reinterpret_cast<std::uintptr_t>(texels), Threads * 4u, nullptr, "test");
device.WaitIdle();
for (std::uint32_t tid = 0; tid < Threads; ++tid) {
std::uint32_t actual = 0;
std::memcpy(&actual, texels + tid * 4u, 4u);
const auto expected = tid < FloatStoreEdges.size() ? FloatStoreEdges[tid][3] : FloatTexel(tid);
Require(actual == expected, "image_store of R10_G11_B11_FLOAT: texel " + std::to_string(tid) + " is " + Hex(actual) + ", expected " + Hex(expected));
}
}
void RequireRefused(AgcDriver::VulkanDevice& device, std::span<const std::uint32_t> code, const std::uint8_t* texels, std::uint32_t swizzle, const std::array<std::uint32_t, 4>& extra, const std::string& reason, const std::string& what, std::uint32_t format = UnormFormat) {
std::string refusal;
try {
Run(device, code, TextureDescriptor(texels, UnormFormat, swizzle, LoadWidth), extra, 1);
Run(device, code, TextureDescriptor(texels, format, swizzle, LoadWidth), extra, 1);
} catch (const std::exception& error) {
refusal = error.what();
}
Require(refusal.find(reason) != std::string::npos, what + " of a 10_11_11_UNORM image was not refused: " + refusal);
Require(refusal.find(reason) != std::string::npos, what + " of a converted image was not refused: " + refusal);
}
}
@@ -279,6 +352,9 @@ int main() {
CheckRoundTrip(*device, texels);
CheckStore(*device, texels + StoreOffset, StoreXyz, 0x7u, "image_store dmask:0x7");
CheckStore(*device, texels + StoreOffset, StoreXz, 0x5u, "image_store dmask:0x5");
CheckFloatStore(*device, texels + FloatOffset);
CheckFloatLoad(*device, texels + FloatOffset);
RequireRefused(*device, SampleLz, texels + FloatOffset, SwizzleXYZ1, PointSampler, "samples or gathers a converted float image", "image_sample_lz of R10_G11_B11_FLOAT", FloatFormat);
RequireRefused(*device, SampleLz, texels, SwizzleXYZ1, PointSampler, "samples or gathers a converted unorm image", "image_sample_lz");
RequireRefused(*device, Gather4Lz, texels, SwizzleXYZ1, PointSampler, "samples or gathers a converted unorm image", "image_gather4_lz");
RequireRefused(*device, GetLod, texels, SwizzleXYZ1, PointSampler, "queries the level of detail of a converted unorm image", "image_get_lod");
@@ -13,7 +13,7 @@ struct FormatInfo {
bool sint32;
};
constexpr std::array<FormatInfo, 79> kFormatInfoTable {{
constexpr std::array<FormatInfo, 80> kFormatInfoTable {{
{IrBufferFormat::Format8UNorm, true, false, false},
{IrBufferFormat::Format8SNorm, false, false, false},
{IrBufferFormat::Format8UInt, true, true, false},
@@ -38,6 +38,7 @@ constexpr std::array<FormatInfo, 79> kFormatInfoTable {{
{IrBufferFormat::Format11_11_10UNorm, true, true, false},
{IrBufferFormat::Format11_11_10UInt, true, true, false},
{IrBufferFormat::Format11_11_10Float, true, false, false},
{IrBufferFormat::Format10_11_11Float, true, true, false},
{IrBufferFormat::Format10_10_10_2UNorm, true, false, false},
{IrBufferFormat::Format10_10_10_2UInt, true, true, false},
{IrBufferFormat::Format8_8_8_8UNorm, true, false, false},
@@ -120,7 +121,7 @@ IrTextureNumericClass SampledTextureNumericClass(IrBufferFormat format) {
}
IrBufferFormat RemapTextureFormat(IrBufferFormat format) {
return format == IrBufferFormat::Format11_11_10UNorm || format == IrBufferFormat::Format11_11_10UInt ? IrBufferFormat::Format32UInt : format;
return format == IrBufferFormat::Format11_11_10UNorm || format == IrBufferFormat::Format11_11_10UInt || format == IrBufferFormat::Format10_11_11Float ? IrBufferFormat::Format32UInt : format;
}
std::uint32_t DepthBitsTextureWidth(std::uint32_t word1, std::uint32_t word3) {
@@ -479,15 +479,15 @@ SpirvBufferFormatInfo ImageConversionFormat(const ImageResource& image) {
return {};
}
const auto info = GetFormatInfo(format);
if (SampledTextureNumericClass(format) != IrTextureNumericClass::Uint || RemapTextureFormat(format) == format || (info.type != SpirvFormatComponentType::Uint && info.type != SpirvFormatComponentType::Unorm) || !info.packedBitfield || info.byteSize != sizeof(std::uint32_t) || info.componentCount == 0u || info.componentCount > 4u) {
throw std::runtime_error("image conversion format is not a packed 32-bit unsigned integer or unorm format");
if (SampledTextureNumericClass(format) != IrTextureNumericClass::Uint || RemapTextureFormat(format) == format || (info.type != SpirvFormatComponentType::Uint && info.type != SpirvFormatComponentType::Unorm && info.type != SpirvFormatComponentType::Float) || !info.packedBitfield || info.byteSize != sizeof(std::uint32_t) || info.componentCount == 0u || info.componentCount > 4u) {
throw std::runtime_error("image conversion format is not a packed 32-bit unsigned integer, unorm or float format");
}
return info;
}
void RequireConvertedUnormAccess(SpirvValueEmitContext& ctx, const ImageEmitAccess& access, const SpirvBufferFormatInfo& info) {
if (access.mem.dataBits == 16u) {
ctx.Fail(access.inst, "reads or writes a converted unorm image with 16-bit data, which is not implemented");
ctx.Fail(access.inst, info.type == SpirvFormatComponentType::Float ? "reads or writes a converted float image with 16-bit data, which is not implemented" : "reads or writes a converted unorm image with 16-bit data, which is not implemented");
}
for (std::uint32_t component = 0; component < 4u; component++) {
const auto selector = (access.image.shaderSwizzle >> (component * 3u)) & 7u;
@@ -506,7 +506,7 @@ std::uint32_t UnpackImageTexel(SpirvValueEmitContext& ctx, const ImageEmitAccess
if (info.format == IrBufferFormat::Invalid) {
return texel;
}
const bool unorm = info.type == SpirvFormatComponentType::Unorm;
const bool unorm = info.type == SpirvFormatComponentType::Unorm || info.type == SpirvFormatComponentType::Float;
if (unorm) {
RequireConvertedUnormAccess(ctx, access, info);
}
@@ -676,7 +676,7 @@ std::uint32_t PackImageTexel(SpirvValueEmitContext& ctx, const ImageEmitAccess&
if (info.format == IrBufferFormat::Invalid) {
return texel;
}
const bool unorm = info.type == SpirvFormatComponentType::Unorm;
const bool unorm = info.type == SpirvFormatComponentType::Unorm || info.type == SpirvFormatComponentType::Float;
if (unorm) {
RequireConvertedUnormAccess(ctx, access, info);
}
@@ -840,6 +840,9 @@ void EmitQueryLodOp(SpirvValueEmitContext& ctx, const ImageEmitAccess& access) {
if (ImageConversionFormat(access.image).type == SpirvFormatComponentType::Unorm) {
ctx.Fail(access.inst, "queries the level of detail of a converted unorm image, which is not implemented");
}
if (ImageConversionFormat(access.image).type == SpirvFormatComponentType::Float) {
ctx.Fail(access.inst, "queries the level of detail of a converted float image, which is not implemented");
}
state.module.EmitCapability(spv::CapabilityImageQuery);
const auto sampled = MakeSampledImage(state, access.mem.resource, access.mem.sampler, access.slot);
const auto coord = CoordF32(ctx, access, 0, RdnaImageDimensionInfoFor(access.image.dimension).spatialComponents, AddressDimension(access).spatialComponents);
@@ -962,6 +965,9 @@ SampleSetup MakeSampleSetup(SpirvValueEmitContext& ctx, const ImageEmitAccess& a
if (ImageConversionFormat(access.image).type == SpirvFormatComponentType::Unorm) {
ctx.Fail(access.inst, "samples or gathers a converted unorm image, which needs filtering in the shader and is not implemented");
}
if (ImageConversionFormat(access.image).type == SpirvFormatComponentType::Float) {
ctx.Fail(access.inst, "samples or gathers a converted float image, which needs filtering in the shader and is not implemented");
}
const auto coord = CoordF32(ctx, access, layout.coord, dimensionInfo.coordinateComponents, AddressDimension(access).coordinateComponents);
return {dimensionInfo, layout, access.image.numericClass, dref, coord};
}