diff --git a/3rdparty/glslang b/3rdparty/glslang index 892c1e3f..7bdd997c 160000 --- a/3rdparty/glslang +++ b/3rdparty/glslang @@ -1 +1 @@ -Subproject commit 892c1e3fe5ee09874c885faf2fa69db2fa246c9e +Subproject commit 7bdd997cca17d08d9bfcf406de95342878d57939 diff --git a/MobileGL/MG_IntegrationTest/Scenarios/DrawParametersScenario.cpp b/MobileGL/MG_IntegrationTest/Scenarios/DrawParametersScenario.cpp index 7c0f4308..7cd1e99e 100644 --- a/MobileGL/MG_IntegrationTest/Scenarios/DrawParametersScenario.cpp +++ b/MobileGL/MG_IntegrationTest/Scenarios/DrawParametersScenario.cpp @@ -30,6 +30,7 @@ #include #include +#include #include #include "../Harness/HeadlessGL.h" @@ -223,6 +224,39 @@ void main() { ExpectParams(image, 0, {0, 5, 0}, "glDrawArraysInstancedBaseInstance(baseInstance=5)"); } + // GL_ARB_shader_draw_parameters needs only OpenGL 3.1, so a 3.30 shader may enable it + // behind the usual `#ifdef` probe and read the ARB built-ins. + TEST_F(DrawParametersScenario, A330ShaderReadsTheArbBuiltIns) { + if (!Ready()) return; + constexpr const char* kVertex330 = R"(#version 330 core +#ifdef GL_ARB_shader_draw_parameters +#extension GL_ARB_shader_draw_parameters : enable +#endif +layout(location = 0) in vec2 aPos; +flat out vec3 vParams; +const int kStep[8] = int[8](0, 8, 16, 24, 32, 40, 48, 56); +void main() { + vParams = (vec3(gl_BaseVertexARB * 8, kStep[gl_BaseInstanceARB], gl_DrawIDARB * 8) + 16.0) / 255.0; + gl_Position = vec4(aPos, 0.0, 1.0); +} +)"; + constexpr const char* kFragment330 = R"(#version 330 core +flat in vec3 vParams; +out vec4 oColor; +void main() { oColor = vec4(vParams, 1.0); } +)"; + std::string error; + GLuint program = CompileProgram(kVertex330, kFragment330, &error); + ASSERT_NE(program, 0u) << error; + // Render draws with m_program, and TearDown deletes whatever m_program holds. + std::swap(m_program, program); + glDeleteProgram(program); + const Image image = + Render([&] { glDrawArraysInstancedBaseInstance(GL_TRIANGLES, kLeftFirst, 2 * kHalfCount, 1, 5); }); + EXPECT_EQ(FirstGLError(), GLenum(GL_NO_ERROR)); + ExpectParams(image, 0, {0, 5, 0}, "#version 330 glDrawArraysInstancedBaseInstance(baseInstance=5)"); + } + // The base instance of one draw must not survive into the next one. This is // the shape that broke on DirectGLES: the emulation uniform is per-program // state, so a draw that never writes it inherits the last writer's value. diff --git a/MobileGL/MG_Test/SanityTest.cpp b/MobileGL/MG_Test/SanityTest.cpp index 2ec865c2..3bf486ae 100644 --- a/MobileGL/MG_Test/SanityTest.cpp +++ b/MobileGL/MG_Test/SanityTest.cpp @@ -1568,6 +1568,46 @@ void main() { mgColor = vec4(float(gl_MaxCullDistances)); } MG_State::pGLContext = Move(previousContext); } +// GL_ARB_shader_draw_parameters needs only OpenGL 3.1: below #version 440 its built-ins are usable +// behind the extension's `#ifdef` probe, and still unreachable without the extension. +TEST(ShaderCompilerSanity, ArbShaderDrawParametersIsUsableBelow440) { + using namespace MobileGL; + auto previousContext = MG_State::pGLContext.Take(); + auto previousBackend = Move(MG_Backend::pActiveBackendObject); + MG_State::pGLContext = MakeUnique(); + MG_Backend::pActiveBackendObject = MakeUnique(MG_Backend::DynamicBackendParameters{}); + const auto env = MG_Util::ShaderTranspiler::CaptureCompileEnv(); + const auto compileVertex = [&env](const String& source) { + return MG_Util::ShaderTranspiler::ShaderCompiler::CompileShader({ + .shaderType = GL_VERTEX_SHADER, + .sourceStr = source, + .env = env.get(), + }); + }; + + for (const char* version : {"140", "330 core", "430 core"}) { + const String probed = String("#version ") + version + R"( +#ifdef GL_ARB_shader_draw_parameters +#extension GL_ARB_shader_draw_parameters : enable +#else +#error GL_ARB_shader_draw_parameters should be defined from 140 up +#endif +const int offsets[4] = int[4](0, 1, 2, 3); +void main() { gl_Position = vec4(float(offsets[gl_BaseInstanceARB] + gl_BaseVertexARB + gl_DrawIDARB)); } +)"; + auto compiled = compileVertex(probed); + EXPECT_TRUE(compiled) << version << ": " << (compiled ? String() : compiled.error().log); + } + + const String withoutExtension = R"(#version 330 core +void main() { gl_Position = vec4(float(gl_BaseInstanceARB)); } +)"; + EXPECT_FALSE(compileVertex(withoutExtension)) << "gl_BaseInstanceARB must require GL_ARB_shader_draw_parameters"; + + MG_Backend::pActiveBackendObject = Move(previousBackend); + MG_State::pGLContext = Move(previousContext); +} + // GL_EXT_shader_integer_mix (KHR-GL4x.shader_integer_mix.*): a 3.30 shader that requires the // extension must see its macro and must be able to mix integer and boolean vectors under a // boolean selector. Both backends carry the result: SPIR-V lowers the call to OpSelect, which