mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-21 00:41:57 +09:00
[Fix] (Espryt): bound usampler2DMS fetches by the bound texture's real sample count
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
using namespace MobileGL;
|
||||
using MobileGL::MG_Backend::DirectGLES::PrgramImpl::BakeImageFormatQualifiers;
|
||||
using MobileGL::MG_Backend::DirectGLES::PrgramImpl::BuildPassthroughTessControlEssl;
|
||||
using MobileGL::MG_Backend::DirectGLES::PrgramImpl::ClampUnsignedMultisampleFetches;
|
||||
using MobileGL::MG_Backend::DirectGLES::PrgramImpl::ExtractPerVertexBlockMembers;
|
||||
using MobileGL::MG_Backend::DirectGLES::PrgramImpl::ForceFlatIntegerVaryings;
|
||||
using MobileGL::MG_Backend::DirectGLES::PrgramImpl::IMAGE_ARRAY_ELEMENT_PREFIX;
|
||||
@@ -1763,3 +1764,87 @@ void main() { gl_Position = gl_in[0].gl_Position; EmitVertex(); }
|
||||
EXPECT_EQ(RedeclareClipCullDistanceInputsInPerVertexBlock(readsPointSize, GL_VERTEX_SHADER, producer),
|
||||
readsPointSize);
|
||||
}
|
||||
|
||||
// The runtime half of the deferred usampler2DMS clamp: the sample argument of every fetch that
|
||||
// names an unsigned multisample uniform is bounded by that sampler's own uniform, declared right
|
||||
// after it. A float multisample sampler and a non-multisample one are none of its business.
|
||||
TEST(ClampUnsignedMultisampleFetchesTest, BoundsTheSampleOfEveryUnsignedMultisampleFetch) {
|
||||
const String essl = "#version 320 es\n"
|
||||
"precision highp float;\n"
|
||||
"layout(binding = 0) uniform highp usampler2DMS stencilTex;\n"
|
||||
"uniform highp sampler2DMS colorMs;\n"
|
||||
"uniform highp usampler2D plain;\n"
|
||||
"layout(location = 0) out highp vec4 color;\n"
|
||||
"void main() {\n"
|
||||
" uint a = texelFetch(stencilTex, ivec2(0), 3).x;\n"
|
||||
" uint b = texelFetch(stencilTex, ivec2(0), int(gl_FragCoord.x)).x;\n"
|
||||
" vec4 c = texelFetch(colorMs, ivec2(0), 3);\n"
|
||||
" uint d = texelFetch(plain, ivec2(0), 0).x;\n"
|
||||
" color = vec4(float(a), float(b), c.x, float(d));\n"
|
||||
"}\n";
|
||||
const String out = ClampUnsignedMultisampleFetches(essl);
|
||||
EXPECT_TRUE(Contains(out, "texelFetch(stencilTex, ivec2(0), min(3, mg_msSampleMax_stencilTex))")) << out;
|
||||
EXPECT_TRUE(Contains(out, "texelFetch(stencilTex, ivec2(0), min(int(gl_FragCoord.x), mg_msSampleMax_stencilTex))"))
|
||||
<< out;
|
||||
EXPECT_TRUE(Contains(out, "uniform highp usampler2DMS stencilTex;\nuniform highp int mg_msSampleMax_stencilTex;"))
|
||||
<< out;
|
||||
EXPECT_EQ(CountOf(out, "uniform highp int mg_msSampleMax_"), 1u) << out;
|
||||
EXPECT_EQ(CountOf(out, "min("), 2u) << out;
|
||||
|
||||
const String untouched = "#version 320 es\nuniform highp sampler2DMS colorMs;\n"
|
||||
"void main() { highp vec4 c = texelFetch(colorMs, ivec2(0), 3); }\n";
|
||||
EXPECT_EQ(ClampUnsignedMultisampleFetches(untouched), untouched);
|
||||
}
|
||||
|
||||
// End to end, the way the program build runs it: the SPIR-V pass defers the direct usampler2DMS
|
||||
// fetches of KHR-GL4x.texture_stencil8.multisample's checker, SPIRV-Cross prints them with their
|
||||
// literal samples intact, and the text pass bounds each one by the runtime uniform.
|
||||
TEST(ClampUnsignedMultisampleFetchesTest, BoundsTheStencilCheckerAfterTheDeferredSpirvClamp) {
|
||||
using namespace MG_Util::ShaderTranspiler;
|
||||
MobileGL::Initialize();
|
||||
const char* vertexSource = R"(#version 330 core
|
||||
void main() { gl_Position = vec4(0.0, 0.0, 0.0, 1.0); }
|
||||
)";
|
||||
const char* fragmentSource = R"(#version 330 core
|
||||
uniform usampler2DMS stencilTex;
|
||||
out vec4 color;
|
||||
void main() {
|
||||
uint s0 = texelFetch(stencilTex, ivec2(0), 0).r;
|
||||
uint s1 = texelFetch(stencilTex, ivec2(0), 1).r;
|
||||
uint s2 = texelFetch(stencilTex, ivec2(0), 2).r;
|
||||
uint s3 = texelFetch(stencilTex, ivec2(0), 3).r;
|
||||
color = vec4(float(s0), float(s1), float(s2), float(s3)) / 255.0;
|
||||
}
|
||||
)";
|
||||
Vector<SharedPtr<glslang::TShader>> shaders;
|
||||
const Vector<Pair<GLenum, const char*>> stages{{GL_VERTEX_SHADER, vertexSource},
|
||||
{GL_FRAGMENT_SHADER, fragmentSource}};
|
||||
for (const auto& [stage, text] : stages) {
|
||||
ShaderAttrib attrib{.shaderType = stage, .sourceStr = text};
|
||||
auto compiled = ShaderCompiler::CompileShader(attrib);
|
||||
ASSERT_TRUE(compiled) << compiled.error().log;
|
||||
shaders.push_back(compiled.value());
|
||||
}
|
||||
ProgramAttrib programAttrib{.shaders = shaders};
|
||||
auto program = ShaderCompiler::LinkProgram(programAttrib);
|
||||
ASSERT_TRUE(program) << program.error().log;
|
||||
ProgramBinaryAttrib binaryAttrib{.shaderTypes = {GL_VERTEX_SHADER, GL_FRAGMENT_SHADER},
|
||||
.program = *program.value()};
|
||||
auto modules = ShaderCompiler::GetSpirvBinaryFromProgram(binaryAttrib);
|
||||
ASSERT_TRUE(modules) << modules.error().log;
|
||||
ASSERT_EQ(modules.value().size(), 2u);
|
||||
|
||||
Vector<uint32_t> deferred;
|
||||
ASSERT_TRUE(ShaderCompiler::ClampMultisampleFetchesForEssl(modules.value()[1], deferred, 4, 1, 4, 4, true,
|
||||
/*deferDirectUnsignedFetches=*/true));
|
||||
const Vector<uint32_t>& fragmentModule = deferred.empty() ? modules.value()[1] : deferred;
|
||||
SpvcSession session(fragmentModule, SessionUsageBit::Transpile);
|
||||
auto essl = ShaderCompiler::DecompileShader(session);
|
||||
ASSERT_TRUE(essl) << essl.error().log;
|
||||
|
||||
const String out = ClampUnsignedMultisampleFetches(essl.value());
|
||||
for (Int sample = 1; sample <= 3; ++sample) {
|
||||
EXPECT_TRUE(Contains(out, "min(" + std::to_string(sample) + ", mg_msSampleMax_stencilTex)")) << out;
|
||||
}
|
||||
EXPECT_EQ(CountOf(out, "uniform highp int mg_msSampleMax_stencilTex;"), 1u) << out;
|
||||
}
|
||||
|
||||
@@ -381,3 +381,67 @@ TEST_F(ClampMultisampleFetchTest, ClampsAColorSampleWithAMinimum) {
|
||||
ASSERT_EQ(constants.count(minimum->operandIds[1]), 1u) << dis;
|
||||
EXPECT_EQ(constants.at(minimum->operandIds[1]), 1u) << dis;
|
||||
}
|
||||
|
||||
namespace {
|
||||
// KHR-GL4x.texture_stencil8.multisample in miniature: a four-sample STENCIL_INDEX8 texture
|
||||
// read through a usampler2DMS with literal sample indices. Beside it, the same sampler
|
||||
// reached through a function parameter and a signed integer sampler - neither of which the
|
||||
// runtime bound can name, so both must keep the static integer clamp.
|
||||
const char* const kStencilMultisampleFetch = R"(#version 410 core
|
||||
uniform usampler2DMS stencilTex;
|
||||
uniform isampler2DMS signedMs;
|
||||
out vec4 fragColor;
|
||||
uint fetchThrough(usampler2DMS s, int i) { return texelFetch(s, ivec2(0), i).r; }
|
||||
void main() {
|
||||
uint direct = texelFetch(stencilTex, ivec2(0), 3).r;
|
||||
uint viaParameter = fetchThrough(stencilTex, 3);
|
||||
int signedValue = texelFetch(signedMs, ivec2(0), 3).r;
|
||||
fragColor = vec4(float(direct), float(viaParameter), float(signedValue), 1.0);
|
||||
}
|
||||
)";
|
||||
|
||||
// How many Sample operands of the module's multisample fetches are the constant `value`.
|
||||
SizeT CountSampleOperandsEqualTo(const Vector<Uint32>& spirv, Uint32 value) {
|
||||
const std::map<Uint32, Uint32> constants = CollectScalarConstants(spirv);
|
||||
SizeT count = 0;
|
||||
for (const ImageFetch& fetch : CollectImageFetches(spirv)) {
|
||||
if ((fetch.mask & static_cast<Uint32>(spv::ImageOperandsMask::Sample)) == 0u) continue;
|
||||
const auto constant = constants.find(fetch.maskOperandIds.back());
|
||||
if (constant != constants.end() && constant->second == value) ++count;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// Integer colour squeezed to one sample but depth/stencil at four (Adreno): a usampler2DMS fetch
|
||||
// straight from its uniform is handed to the runtime bound and keeps sample 3 - a stencil texture
|
||||
// really has it - while the fetch through a parameter and the signed one still read sample 0.
|
||||
TEST_F(ClampMultisampleFetchTest, DefersADirectUnsignedFetchToTheRuntimeBound) {
|
||||
const Vector<Uint32> input = CompileFragment(kStencilMultisampleFetch);
|
||||
ASSERT_FALSE(input.empty());
|
||||
|
||||
Vector<Uint32> output;
|
||||
ASSERT_TRUE(ShaderCompiler::ClampMultisampleFetchesForEssl(
|
||||
input, output, /*maxColorSamples=*/4, /*maxIntegerSamples=*/1, /*maxDepthSamples=*/4,
|
||||
kAdvertisedMaxSamples, true, /*deferDirectUnsignedFetches=*/true));
|
||||
ASSERT_FALSE(output.empty());
|
||||
const String dis = Disassemble(output);
|
||||
ASSERT_TRUE(Validates(output)) << dis;
|
||||
EXPECT_EQ(CountSampleOperandsEqualTo(output, 3u), 1u) << dis;
|
||||
EXPECT_EQ(CountSampleOperandsEqualTo(output, 0u), 2u) << dis;
|
||||
}
|
||||
|
||||
// Without the runtime bound nothing is deferred: the pre-existing static clamp, all three to 0.
|
||||
TEST_F(ClampMultisampleFetchTest, WithoutTheRuntimeBoundEveryIntegerFetchKeepsTheStaticClamp) {
|
||||
const Vector<Uint32> input = CompileFragment(kStencilMultisampleFetch);
|
||||
ASSERT_FALSE(input.empty());
|
||||
|
||||
Vector<Uint32> output;
|
||||
ASSERT_TRUE(ShaderCompiler::ClampMultisampleFetchesForEssl(
|
||||
input, output, /*maxColorSamples=*/4, /*maxIntegerSamples=*/1, /*maxDepthSamples=*/4,
|
||||
kAdvertisedMaxSamples, true));
|
||||
ASSERT_FALSE(output.empty());
|
||||
const String dis = Disassemble(output);
|
||||
EXPECT_EQ(CountSampleOperandsEqualTo(output, 3u), 0u) << dis;
|
||||
EXPECT_EQ(CountSampleOperandsEqualTo(output, 0u), 3u) << dis;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user