mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-18 15:33:22 +09:00
[Feat, Fix, Test] (ShaderTranspiler, DirectGLES): carry the seven normalized image formats as their own codes in rgba16ui
This commit is contained in:
@@ -89,6 +89,7 @@ namespace {
|
||||
struct StorageImageType {
|
||||
Uint32 resultId = 0u;
|
||||
Uint32 format = 0u;
|
||||
Uint32 sampledTypeId = 0u;
|
||||
};
|
||||
|
||||
Vector<StorageImageType> CollectStorageImageTypes(const Vector<Uint32>& spirv) {
|
||||
@@ -96,11 +97,27 @@ namespace {
|
||||
ForEachInstruction(spirv, [&](spv::Op opcode, const Uint32* words, Uint32 wordCount) {
|
||||
if (opcode != spv::Op::OpTypeImage || wordCount < 9u) return;
|
||||
if (words[7] != 2u) return;
|
||||
types.push_back(StorageImageType{words[1], words[8]});
|
||||
types.push_back(StorageImageType{words[1], words[8], words[2]});
|
||||
});
|
||||
return types;
|
||||
}
|
||||
|
||||
// "float" / "uint" / "int" / "" for a scalar numeric type id, which is the one thing that says
|
||||
// whether a declaration is still an image2D or has become a uimage2D.
|
||||
String ScalarTypeSpellingOf(const Vector<Uint32>& spirv, Uint32 typeId) {
|
||||
String spelling;
|
||||
ForEachInstruction(spirv, [&](spv::Op opcode, const Uint32* words, Uint32 wordCount) {
|
||||
if (words[1] != typeId) return;
|
||||
// OpTypeFloat words: 1 result id, 2 width. OpTypeInt adds 3 signedness.
|
||||
if (opcode == spv::Op::OpTypeFloat && wordCount >= 3u) {
|
||||
spelling = "float";
|
||||
} else if (opcode == spv::Op::OpTypeInt && wordCount >= 4u) {
|
||||
spelling = words[3] != 0u ? "int" : "uint";
|
||||
}
|
||||
});
|
||||
return spelling;
|
||||
}
|
||||
|
||||
// OpVectorShuffle words: 0 opcode/count, 1 result type, 2 result id, 3 vector 1, 4 vector 2,
|
||||
// 5.. the component selectors.
|
||||
struct VectorShuffle {
|
||||
@@ -269,15 +286,40 @@ void main() {
|
||||
}
|
||||
)";
|
||||
|
||||
// rg16 is one of the SEVEN with no core carrier at all - core ESSL has no 16-bit normalized
|
||||
// format, so every candidate loses range or changes the component type the texture presents.
|
||||
// It must be left alone and keep the honest "no GLSL ES spelling" diagnostic instead.
|
||||
// rg16: TWO unsigned-normalized 16-bit channels, which core ESSL has no image format of any
|
||||
// width for. Carried as its own CODES in an rgba16ui, so the declaration comes out a
|
||||
// uimage2D and every access is wrapped in GL 4.6 2.3.5 as well as masked.
|
||||
const char* const kRg16LoadStore = R"(#version 430 core
|
||||
layout(rg16, binding = 0) uniform image2D img;
|
||||
out vec4 fragColor;
|
||||
void main() {
|
||||
vec4 texel = imageLoad(img, ivec2(gl_FragCoord.xy));
|
||||
imageStore(img, ivec2(gl_FragCoord.xy), vec4(1.0, 2.0, 3.0, 4.0));
|
||||
imageStore(img, ivec2(gl_FragCoord.xy), vec4(0.25, 0.5, 0.75, 1.0));
|
||||
fragColor = texel;
|
||||
}
|
||||
)";
|
||||
|
||||
// rgba16_snorm: the signed twin, whose code is a two's-complement 16-bit integer sitting in an
|
||||
// UNSIGNED carrier channel - so the load has to sign-extend it back and the store has to mask
|
||||
// it down, neither of which the unsigned conversion does.
|
||||
const char* const kRgba16SnormLoadStore = R"(#version 430 core
|
||||
layout(rgba16_snorm, binding = 0) uniform image2D img;
|
||||
out vec4 fragColor;
|
||||
void main() {
|
||||
vec4 texel = imageLoad(img, ivec2(gl_FragCoord.xy));
|
||||
imageStore(img, ivec2(gl_FragCoord.xy), vec4(1.0, -1.0, 0.5, -0.5));
|
||||
fragColor = texel;
|
||||
}
|
||||
)";
|
||||
|
||||
// rgb10_a2: FOUR normalized channels that are not all the same width, so its denominator is
|
||||
// (1023, 1023, 1023, 3) and one number would be wrong for a quarter of every texel.
|
||||
const char* const kRgb10A2LoadStore = R"(#version 430 core
|
||||
layout(rgb10_a2, binding = 0) uniform image2D img;
|
||||
out vec4 fragColor;
|
||||
void main() {
|
||||
vec4 texel = imageLoad(img, ivec2(gl_FragCoord.xy));
|
||||
imageStore(img, ivec2(gl_FragCoord.xy), vec4(0.25, 0.5, 0.75, 1.0));
|
||||
fragColor = texel;
|
||||
}
|
||||
)";
|
||||
@@ -287,7 +329,7 @@ void main() {
|
||||
// the shader rewrite, the ES texture storage and the glBindImageTexture argument. If it drifts
|
||||
// the three stop agreeing, and a narrow texture read through a wide image goes out of bounds
|
||||
// silently on every driver tested.
|
||||
TEST(WidenImageFormats, NineteenNonCoreFormatsHaveALosslessCoreCarrier) {
|
||||
TEST(WidenImageFormats, TwentySixNonCoreFormatsHaveALosslessCoreCarrier) {
|
||||
struct Case {
|
||||
Uint requested;
|
||||
Uint carrier;
|
||||
@@ -319,6 +361,16 @@ TEST(WidenImageFormats, NineteenNonCoreFormatsHaveALosslessCoreCarrier) {
|
||||
// FOUR channels: 10, 10, 10 and 2 bits of unsigned integer all fit in sixteen, so nothing
|
||||
// is masked at all and only the packed TRANSFER is re-encoded.
|
||||
{0x906F, 0x8D76, 4, "GL_RGB10_A2UI -> GL_RGBA16UI"},
|
||||
// The seven NORMALIZED formats, carried as their own channel CODES in the same rgba16ui.
|
||||
// These are the entries whose carrier changes the shader-visible TYPE as well, which is
|
||||
// why every access through them is wrapped in GL 4.6 2.3.5 rather than only masked.
|
||||
{0x805B, 0x8D76, 4, "GL_RGBA16 -> GL_RGBA16UI"},
|
||||
{0x822C, 0x8D76, 2, "GL_RG16 -> GL_RGBA16UI"},
|
||||
{0x822A, 0x8D76, 1, "GL_R16 -> GL_RGBA16UI"},
|
||||
{0x8059, 0x8D76, 4, "GL_RGB10_A2 -> GL_RGBA16UI"},
|
||||
{0x8F9B, 0x8D76, 4, "GL_RGBA16_SNORM -> GL_RGBA16UI"},
|
||||
{0x8F99, 0x8D76, 2, "GL_RG16_SNORM -> GL_RGBA16UI"},
|
||||
{0x8F98, 0x8D76, 1, "GL_R16_SNORM -> GL_RGBA16UI"},
|
||||
};
|
||||
for (const Case& testCase : cases) {
|
||||
EXPECT_EQ(ShaderCompiler::WidenedCoreEsslImageFormat(testCase.requested), testCase.carrier)
|
||||
@@ -334,7 +386,7 @@ TEST(WidenImageFormats, NineteenNonCoreFormatsHaveALosslessCoreCarrier) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(WidenImageFormats, CoreFormatsAndTheSevenWithoutALosslessCarrierAreRefused) {
|
||||
TEST(WidenImageFormats, CoreFormatsAreRefused) {
|
||||
// The thirteen GLSL ES already has: nothing to carry.
|
||||
for (const Uint coreFormat : {0x8814u /*RGBA32F*/, 0x881Au /*RGBA16F*/, 0x822Eu /*R32F*/,
|
||||
0x8058u /*RGBA8*/, 0x8F97u /*RGBA8_SNORM*/, 0x8D82u /*RGBA32I*/,
|
||||
@@ -344,23 +396,63 @@ TEST(WidenImageFormats, CoreFormatsAndTheSevenWithoutALosslessCarrierAreRefused)
|
||||
EXPECT_EQ(ShaderCompiler::WidenedCoreEsslImageFormat(coreFormat), 0u)
|
||||
<< "core format 0x" << std::hex << coreFormat;
|
||||
}
|
||||
// The seven with no LOSSLESS core carrier: core ESSL has no 16-bit normalized format and no
|
||||
// 10-bit one, so every candidate for these either loses range or changes the component type
|
||||
// the texture presents to anything that samples it. Deliberately left to the honest
|
||||
// diagnostic. r11f_g11f_b10f is NOT among them - rgba16f holds every value it can - and
|
||||
// neither is rgb10_a2ui, whose channels are INTEGER and fit an rgba16ui outright.
|
||||
for (const Uint hardFormat : {0x8059u /*RGB10_A2*/, 0x805Bu /*RGBA16*/, 0x822Cu /*RG16*/,
|
||||
0x822Au /*R16*/, 0x8F9Bu /*RGBA16_SNORM*/, 0x8F99u /*RG16_SNORM*/,
|
||||
0x8F98u /*R16_SNORM*/}) {
|
||||
EXPECT_EQ(ShaderCompiler::WidenedCoreEsslImageFormat(hardFormat), 0u)
|
||||
<< "format without an exact carrier 0x" << std::hex << hardFormat;
|
||||
}
|
||||
// Not an image format at all.
|
||||
EXPECT_EQ(ShaderCompiler::WidenedCoreEsslImageFormat(0x8051 /*GL_RGB8*/), 0u);
|
||||
EXPECT_EQ(ShaderCompiler::ImageFormatChannelCount(0x8051 /*GL_RGB8*/), 0u);
|
||||
EXPECT_EQ(ShaderCompiler::WidenedCoreEsslImageFormat(0), 0u);
|
||||
}
|
||||
|
||||
// The denominators of GL 4.6 2.3.5, which is the whole difference between a carrier that holds a
|
||||
// format's VALUES and one that holds its CODES. Both halves of DirectGLES's transfer read them
|
||||
// (the upload's synthetic alpha and glGetTexImage's divide), and so does the shader rewrite, so a
|
||||
// wrong entry here is wrong in three places at once and consistently - which is exactly the kind
|
||||
// of error a round-trip test cannot see.
|
||||
TEST(WidenImageFormats, OnlyTheNormalizedFormatsCarryCodesAndTheirDenominatorsAreTheFormatsOwn) {
|
||||
struct Case {
|
||||
Uint format;
|
||||
Uint32 channelMax[4];
|
||||
bool isSigned;
|
||||
const char* name;
|
||||
};
|
||||
const Case cases[] = {
|
||||
{0x805B, {65535u, 65535u, 65535u, 65535u}, false, "GL_RGBA16"},
|
||||
{0x822C, {65535u, 65535u, 65535u, 65535u}, false, "GL_RG16"},
|
||||
{0x822A, {65535u, 65535u, 65535u, 65535u}, false, "GL_R16"},
|
||||
// The one format whose channels are not all the same width, and the reason the answer is
|
||||
// four numbers rather than one: a two-bit alpha saturates at 3, not at 1023.
|
||||
{0x8059, {1023u, 1023u, 1023u, 3u}, false, "GL_RGB10_A2"},
|
||||
{0x8F9B, {32767u, 32767u, 32767u, 32767u}, true, "GL_RGBA16_SNORM"},
|
||||
{0x8F99, {32767u, 32767u, 32767u, 32767u}, true, "GL_RG16_SNORM"},
|
||||
{0x8F98, {32767u, 32767u, 32767u, 32767u}, true, "GL_R16_SNORM"},
|
||||
};
|
||||
for (const Case& testCase : cases) {
|
||||
Uint32 channelMax[4] = {0u, 0u, 0u, 0u};
|
||||
bool isSigned = !testCase.isSigned;
|
||||
EXPECT_TRUE(ShaderCompiler::NormalizedImageCarrierCodes(testCase.format, channelMax, isSigned))
|
||||
<< testCase.name;
|
||||
for (Uint channel = 0; channel < 4; ++channel) {
|
||||
EXPECT_EQ(channelMax[channel], testCase.channelMax[channel])
|
||||
<< testCase.name << " channel " << channel;
|
||||
}
|
||||
EXPECT_EQ(isSigned, testCase.isSigned) << testCase.name;
|
||||
}
|
||||
|
||||
// Everything else keeps its own component type in the carrier, so nothing is converted: an
|
||||
// rg8's carrier channel really is an 8-bit unsigned normalized one, and an rgb10_a2ui's
|
||||
// channel really does hold the integer the shader stored.
|
||||
for (const Uint direct : {0x8230u /*RG32F*/, 0x8229u /*R8*/, 0x8F94u /*R8_SNORM*/,
|
||||
0x8232u /*R8UI*/, 0x8C3Au /*R11F_G11F_B10F*/, 0x906Fu /*RGB10_A2UI*/,
|
||||
0x8814u /*RGBA32F*/, 0x8051u /*RGB8, not an image format*/}) {
|
||||
Uint32 channelMax[4] = {7u, 7u, 7u, 7u};
|
||||
bool isSigned = true;
|
||||
EXPECT_FALSE(ShaderCompiler::NormalizedImageCarrierCodes(direct, channelMax, isSigned))
|
||||
<< "format 0x" << std::hex << direct;
|
||||
for (Uint channel = 0; channel < 4; ++channel) {
|
||||
EXPECT_EQ(channelMax[channel], 7u) << "a refused format must leave the output alone";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(WidenImageFormats, TwoChannelFloatImageBecomesRgba32fWithBothAccessesMasked) {
|
||||
const Vector<Uint32> spirv = CompileFragment(kRg32fLoadStore);
|
||||
ASSERT_FALSE(spirv.empty());
|
||||
@@ -679,25 +771,132 @@ TEST(WidenImageFormats, CoreFormatModuleIsHandedBackUntouched) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(WidenImageFormats, FormatWithoutAnExactCarrierIsLeftAlone) {
|
||||
// The normalized carrier, which is the one that does not merely re-DECLARE the image: a 16-bit
|
||||
// normalized channel has no core ESSL format of any width behind it, and no FLOAT carrier is
|
||||
// honest either (a half has eleven mantissa bits against its sixteen), so what the rgba16ui holds
|
||||
// is the format's own CODE. That changes the shader-visible TYPE, which is the thing to check -
|
||||
// an image2D whose format moved to rgba16ui but whose sampled type stayed float is not merely
|
||||
// wrong, it is invalid SPIR-V, and a module that kept the float type while the STORAGE became an
|
||||
// integer texture would read whole texels as garbage.
|
||||
TEST(WidenImageFormats, NormalizedImageBecomesAUimageWhoseAccessesConvertItsCodes) {
|
||||
const Vector<Uint32> spirv = CompileFragment(kRg16LoadStore);
|
||||
ASSERT_FALSE(spirv.empty());
|
||||
ASSERT_TRUE(ShaderCompiler::DeclaresWidenableImageFormat(spirv));
|
||||
|
||||
const auto beforeTypes = CollectStorageImageTypes(spirv);
|
||||
ASSERT_EQ(beforeTypes.size(), 1u);
|
||||
EXPECT_EQ(beforeTypes.front().format, static_cast<Uint32>(spv::ImageFormat::Rg16));
|
||||
|
||||
// rg16 has no core format with 16-bit unsigned-normalized channels behind it. Anything wider
|
||||
// would requantize differently from what the application asked for, so the pass declines and
|
||||
// CollectImageFormatBakeInputs reports the format as unspellable instead.
|
||||
EXPECT_FALSE(ShaderCompiler::DeclaresWidenableImageFormat(spirv));
|
||||
EXPECT_EQ(ScalarTypeSpellingOf(spirv, beforeTypes.front().sampledTypeId), "float");
|
||||
|
||||
Vector<Uint32> widened;
|
||||
ShaderCompiler::WidenImageFormatsForEssl(spirv, widened, /*onlyFormatsSpirvCrossRefusesToPrint=*/false,
|
||||
/*enableSpirvValidation=*/true);
|
||||
if (!widened.empty()) {
|
||||
const auto afterTypes = CollectStorageImageTypes(widened);
|
||||
ASSERT_EQ(afterTypes.size(), 1u);
|
||||
EXPECT_EQ(afterTypes.front().format, static_cast<Uint32>(spv::ImageFormat::Rg16));
|
||||
ASSERT_TRUE(ShaderCompiler::WidenImageFormatsForEssl(spirv, widened, /*onlyFormatsSpirvCrossRefusesToPrint=*/false,
|
||||
/*enableSpirvValidation=*/true));
|
||||
ASSERT_FALSE(widened.empty());
|
||||
EXPECT_TRUE(Validates(widened));
|
||||
EXPECT_FALSE(ShaderCompiler::DeclaresWidenableImageFormat(widened));
|
||||
|
||||
const auto afterTypes = CollectStorageImageTypes(widened);
|
||||
ASSERT_EQ(afterTypes.size(), 1u);
|
||||
EXPECT_EQ(afterTypes.front().format, static_cast<Uint32>(spv::ImageFormat::Rgba16ui));
|
||||
EXPECT_EQ(ScalarTypeSpellingOf(widened, afterTypes.front().sampledTypeId), "uint")
|
||||
<< "the carrier's component type is unsigned integer, and spirv-val requires the image's "
|
||||
"Sampled Type to say so";
|
||||
|
||||
// The masks are still there and still say what a two-channel format's surplus channels are -
|
||||
// the conversion wraps them, it does not replace them.
|
||||
const auto shuffles = CollectVectorShuffles(widened);
|
||||
const auto texelIds = CollectImageWriteTexelIds(widened);
|
||||
ASSERT_EQ(texelIds.size(), 1u);
|
||||
// The texel is now the PACKED value, so the mask is one step further back: find the shuffle
|
||||
// by its component selectors instead.
|
||||
Bool sawTwoChannelMask = false;
|
||||
for (const VectorShuffle& shuffle : shuffles) {
|
||||
sawTwoChannelMask = sawTwoChannelMask || HasComponents(shuffle, {0u, 1u, 6u, 7u});
|
||||
}
|
||||
EXPECT_TRUE(sawTwoChannelMask) << "expected the (r, g, 0, 1) mask a two-channel format needs";
|
||||
|
||||
// ...and the ESSL says the whole story: a uimage2D holding rgba16ui, divided and multiplied
|
||||
// by the format's own 65535.
|
||||
const EsslAttempt after = EmitEssl(widened);
|
||||
ASSERT_TRUE(after.succeeded) << after.error;
|
||||
EXPECT_NE(after.text.find("uimage2D"), String::npos) << after.text;
|
||||
EXPECT_NE(after.text.find("rgba16ui"), String::npos) << after.text;
|
||||
EXPECT_EQ(after.text.find("rg16"), String::npos)
|
||||
<< "the token no ES driver accepts is still in the emitted source:\n"
|
||||
<< after.text;
|
||||
EXPECT_NE(after.text.find("65535.0"), String::npos)
|
||||
<< "the unsigned-normalized denominator is 2^16 - 1:\n"
|
||||
<< after.text;
|
||||
EXPECT_EQ(after.text.find("32767.0"), String::npos)
|
||||
<< "an unsigned format must not take the SIGNED denominator:\n"
|
||||
<< after.text;
|
||||
}
|
||||
|
||||
// The signed half, which needs two things the unsigned one does not: the code is sign-extended
|
||||
// out of the unsigned carrier channel on the way in, and the decode is max(c / 32767, -1) rather
|
||||
// than the bare division - GL clamps -2^15/32767 up to exactly -1.
|
||||
TEST(WidenImageFormats, SignedNormalizedImageSignExtendsItsCodeAndClampsAtMinusOne) {
|
||||
const Vector<Uint32> spirv = CompileFragment(kRgba16SnormLoadStore);
|
||||
ASSERT_FALSE(spirv.empty());
|
||||
ASSERT_TRUE(ShaderCompiler::DeclaresWidenableImageFormat(spirv));
|
||||
|
||||
Vector<Uint32> widened;
|
||||
ASSERT_TRUE(ShaderCompiler::WidenImageFormatsForEssl(spirv, widened, false, true));
|
||||
ASSERT_FALSE(widened.empty());
|
||||
EXPECT_TRUE(Validates(widened));
|
||||
|
||||
const auto afterTypes = CollectStorageImageTypes(widened);
|
||||
ASSERT_EQ(afterTypes.size(), 1u);
|
||||
EXPECT_EQ(afterTypes.front().format, static_cast<Uint32>(spv::ImageFormat::Rgba16ui));
|
||||
EXPECT_EQ(ScalarTypeSpellingOf(widened, afterTypes.front().sampledTypeId), "uint");
|
||||
|
||||
// The sign extension is a shift PAIR, and the arithmetic one is what makes it a sign
|
||||
// extension rather than a zero extension.
|
||||
Bool sawShiftLeft = false;
|
||||
Bool sawArithmeticShiftRight = false;
|
||||
ForEachInstruction(widened, [&](spv::Op opcode, const Uint32*, Uint32) {
|
||||
sawShiftLeft = sawShiftLeft || opcode == spv::Op::OpShiftLeftLogical;
|
||||
sawArithmeticShiftRight = sawArithmeticShiftRight || opcode == spv::Op::OpShiftRightArithmetic;
|
||||
});
|
||||
EXPECT_TRUE(sawShiftLeft);
|
||||
EXPECT_TRUE(sawArithmeticShiftRight)
|
||||
<< "a logical shift right would read every negative code as a large positive one";
|
||||
|
||||
const EsslAttempt after = EmitEssl(widened);
|
||||
ASSERT_TRUE(after.succeeded) << after.error;
|
||||
EXPECT_NE(after.text.find("uimage2D"), String::npos) << after.text;
|
||||
EXPECT_NE(after.text.find("rgba16ui"), String::npos) << after.text;
|
||||
EXPECT_EQ(after.text.find("rgba16_snorm"), String::npos) << after.text;
|
||||
EXPECT_NE(after.text.find("32767.0"), String::npos)
|
||||
<< "the signed-normalized denominator is 2^15 - 1:\n"
|
||||
<< after.text;
|
||||
EXPECT_NE(after.text.find("-1.0"), String::npos)
|
||||
<< "GL clamps the signed decode at -1:\n"
|
||||
<< after.text;
|
||||
}
|
||||
|
||||
// rgb10_a2, whose four channels are 10, 10, 10 and 2 bits: the only entry where one denominator
|
||||
// would be wrong for a channel that IS present, rather than for one the mask discards anyway.
|
||||
TEST(WidenImageFormats, TenTenTenTwoImageTakesAPerChannelDenominator) {
|
||||
const Vector<Uint32> spirv = CompileFragment(kRgb10A2LoadStore);
|
||||
ASSERT_FALSE(spirv.empty());
|
||||
ASSERT_TRUE(ShaderCompiler::DeclaresWidenableImageFormat(spirv));
|
||||
|
||||
Vector<Uint32> widened;
|
||||
ASSERT_TRUE(ShaderCompiler::WidenImageFormatsForEssl(spirv, widened, false, true));
|
||||
ASSERT_FALSE(widened.empty());
|
||||
EXPECT_TRUE(Validates(widened));
|
||||
|
||||
const auto afterTypes = CollectStorageImageTypes(widened);
|
||||
ASSERT_EQ(afterTypes.size(), 1u);
|
||||
EXPECT_EQ(afterTypes.front().format, static_cast<Uint32>(spv::ImageFormat::Rgba16ui));
|
||||
EXPECT_EQ(ScalarTypeSpellingOf(widened, afterTypes.front().sampledTypeId), "uint");
|
||||
|
||||
const EsslAttempt after = EmitEssl(widened);
|
||||
ASSERT_TRUE(after.succeeded) << after.error;
|
||||
EXPECT_NE(after.text.find("1023.0"), String::npos) << after.text;
|
||||
EXPECT_NE(after.text.find("3.0"), String::npos)
|
||||
<< "the two-bit alpha saturates at 3, not at 1023:\n"
|
||||
<< after.text;
|
||||
EXPECT_EQ(after.text.find("rgb10_a2)"), String::npos) << after.text;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user