Path: blob/main_old/src/tests/compiler_tests/EmulateGLDrawID_test.cpp
1693 views
//1// Copyright 2018 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//5// ANGLE_draw_id.cpp:6// Test for ANGLE_draw_id extension7//89#include "GLSLANG/ShaderLang.h"10#include "angle_gl.h"11#include "compiler/translator/tree_ops/EmulateMultiDrawShaderBuiltins.h"12#include "gtest/gtest.h"13#include "tests/test_utils/compiler_test.h"1415using namespace sh;1617class EmulateGLDrawIDTest : public MatchOutputCodeTest18{19public:20EmulateGLDrawIDTest()21: MatchOutputCodeTest(GL_VERTEX_SHADER, SH_VARIABLES, SH_GLSL_COMPATIBILITY_OUTPUT)22{23getResources()->ANGLE_multi_draw = 1;24}2526protected:27void CheckCompileFailure(const std::string &shaderString, const char *expectedError = nullptr)28{29std::string translatedCode;30std::string infoLog;31bool success = compileTestShader(GL_VERTEX_SHADER, SH_GLES2_SPEC,32SH_GLSL_COMPATIBILITY_OUTPUT, shaderString, getResources(),33SH_VARIABLES, &translatedCode, &infoLog);34EXPECT_FALSE(success);35if (expectedError)36{37EXPECT_TRUE(infoLog.find(expectedError) != std::string::npos);38}39}40};4142// Check that compilation fails if the compile option to emulate gl_DrawID43// is not set44TEST_F(EmulateGLDrawIDTest, RequiresEmulation)45{46CheckCompileFailure(47"#extension GL_ANGLE_multi_draw : require\n"48"void main() {\n"49" gl_Position = vec4(float(gl_DrawID), 0.0, 0.0, 1.0);\n"50"}\n",51"extension is not supported");52}5354// Check that compiling with emulation with gl_DrawID works with different shader versions55TEST_F(EmulateGLDrawIDTest, CheckCompile)56{57const std::string shaderString =58"#extension GL_ANGLE_multi_draw : require\n"59"void main() {\n"60" gl_Position = vec4(float(gl_DrawID), 0.0, 0.0, 1.0);\n"61"}\n";6263compile(shaderString, SH_OBJECT_CODE | SH_VARIABLES | SH_EMULATE_GL_DRAW_ID);64compile("#version 100\n" + shaderString, SH_OBJECT_CODE | SH_VARIABLES | SH_EMULATE_GL_DRAW_ID);65compile("#version 300 es\n" + shaderString,66SH_OBJECT_CODE | SH_VARIABLES | SH_EMULATE_GL_DRAW_ID);67}6869// Check that gl_DrawID is properly emulated70TEST_F(EmulateGLDrawIDTest, EmulatesUniform)71{72addOutputType(SH_GLSL_COMPATIBILITY_OUTPUT);73addOutputType(SH_ESSL_OUTPUT);74#ifdef ANGLE_ENABLE_VULKAN75addOutputType(SH_SPIRV_VULKAN_OUTPUT);76#endif77#ifdef ANGLE_ENABLE_HLSL78addOutputType(SH_HLSL_3_0_OUTPUT);79addOutputType(SH_HLSL_3_0_OUTPUT);80#endif8182const std::string &shaderString =83"#extension GL_ANGLE_multi_draw : require\n"84"void main() {\n"85" gl_Position = vec4(float(gl_DrawID), 0.0, 0.0, 1.0);\n"86"}\n";8788compile(shaderString, SH_OBJECT_CODE | SH_VARIABLES | SH_EMULATE_GL_DRAW_ID);8990EXPECT_TRUE(notFoundInCode("gl_DrawID"));91EXPECT_TRUE(foundInCode("angle_DrawID"));92EXPECT_TRUE(notFoundInCode("GL_ANGLE_multi_draw"));9394EXPECT_TRUE(foundInCode(SH_GLSL_COMPATIBILITY_OUTPUT, "uniform int angle_DrawID"));95EXPECT_TRUE(foundInCode(SH_ESSL_OUTPUT, "uniform highp int angle_DrawID"));9697#ifdef ANGLE_ENABLE_HLSL98EXPECT_TRUE(foundInCode(SH_HLSL_3_0_OUTPUT, "uniform int angle_DrawID : register"));99EXPECT_TRUE(foundInCode(SH_HLSL_3_0_OUTPUT, "uniform int angle_DrawID : register"));100#endif101}102103// Check that a user-defined "gl_DrawID" is not permitted104TEST_F(EmulateGLDrawIDTest, DisallowsUserDefinedGLDrawID)105{106// Check that it is not permitted without the extension107CheckCompileFailure(108"uniform int gl_DrawID;\n"109"void main() {\n"110" gl_Position = vec4(float(gl_DrawID), 0.0, 0.0, 1.0);\n"111"}\n",112"reserved built-in name");113114CheckCompileFailure(115"void main() {\n"116" int gl_DrawID = 0;\n"117" gl_Position = vec4(float(gl_DrawID), 0.0, 0.0, 1.0);\n"118"}\n",119"reserved built-in name");120121// Check that it is not permitted with the extension122CheckCompileFailure(123"#extension GL_ANGLE_multi_draw : require\n"124"uniform int gl_DrawID;\n"125"void main() {\n"126" gl_Position = vec4(float(gl_DrawID), 0.0, 0.0, 1.0);\n"127"}\n",128"reserved built-in name");129130CheckCompileFailure(131"#extension GL_ANGLE_multi_draw : require\n"132"void main() {\n"133" int gl_DrawID = 0;\n"134" gl_Position = vec4(float(gl_DrawID), 0.0, 0.0, 1.0);\n"135"}\n",136"reserved built-in name");137}138139// gl_DrawID is translated to angle_DrawID internally. Check that a user-defined140// angle_DrawID is permitted141TEST_F(EmulateGLDrawIDTest, AllowsUserDefinedANGLEDrawID)142{143addOutputType(SH_GLSL_COMPATIBILITY_OUTPUT);144addOutputType(SH_ESSL_OUTPUT);145#ifdef ANGLE_ENABLE_VULKAN146addOutputType(SH_SPIRV_VULKAN_OUTPUT);147#endif148#ifdef ANGLE_ENABLE_HLSL149addOutputType(SH_HLSL_3_0_OUTPUT);150addOutputType(SH_HLSL_3_0_OUTPUT);151#endif152153const std::string &shaderString =154"#extension GL_ANGLE_multi_draw : require\n"155"uniform int angle_DrawID;\n"156"void main() {\n"157" gl_Position = vec4(float(angle_DrawID + gl_DrawID), 0.0, 0.0, 1.0);\n"158"}\n";159160compile(shaderString, SH_OBJECT_CODE | SH_VARIABLES | SH_EMULATE_GL_DRAW_ID);161162// " angle_DrawID" (note the space) should appear exactly twice:163// once in the declaration and once in the body.164// The user-defined angle_DrawID should be decorated165EXPECT_TRUE(foundInCode(" angle_DrawID", 2));166}167168169