Path: blob/main_old/src/tests/compiler_tests/EmulateGLBaseVertexBaseInstance_test.cpp
1693 views
//1// Copyright 2019 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_base_vertex_base_instance.cpp:6// Test for ANGLE_base_vertex_base_instance 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 EmulateGLBaseVertexBaseInstanceTest : public MatchOutputCodeTest18{19public:20EmulateGLBaseVertexBaseInstanceTest()21: MatchOutputCodeTest(GL_VERTEX_SHADER, SH_VARIABLES, SH_GLSL_COMPATIBILITY_OUTPUT)22{23getResources()->ANGLE_base_vertex_base_instance = 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_GLES3_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_BaseVertex and gl_BaseInstance43// is not set44TEST_F(EmulateGLBaseVertexBaseInstanceTest, RequiresEmulation)45{46CheckCompileFailure(47"#extension GL_ANGLE_base_vertex_base_instance : require\n"48"void main() {\n"49" gl_Position = vec4(float(gl_BaseVertex), float(gl_BaseInstance), 0.0, 1.0);\n"50"}\n",51"extension is not supported");52}5354// Check that compiling with emulation with gl_BaseVertex and gl_BaseInstance works with different55// shader versions56TEST_F(EmulateGLBaseVertexBaseInstanceTest, CheckCompile)57{58const std::string shaderString =59"#extension GL_ANGLE_base_vertex_base_instance : require\n"60"void main() {\n"61" gl_Position = vec4(float(gl_BaseVertex), float(gl_BaseInstance), 0.0, 1.0);\n"62"}\n";6364compile("#version 300 es\n" + shaderString,65SH_OBJECT_CODE | SH_VARIABLES | SH_EMULATE_GL_BASE_VERTEX_BASE_INSTANCE);66}6768// Check that gl_BaseVertex and gl_BaseInstance is properly emulated69TEST_F(EmulateGLBaseVertexBaseInstanceTest, EmulatesUniform)70{71addOutputType(SH_GLSL_COMPATIBILITY_OUTPUT);72addOutputType(SH_ESSL_OUTPUT);73#ifdef ANGLE_ENABLE_VULKAN74addOutputType(SH_SPIRV_VULKAN_OUTPUT);75#endif76#ifdef ANGLE_ENABLE_HLSL77addOutputType(SH_HLSL_3_0_OUTPUT);78addOutputType(SH_HLSL_3_0_OUTPUT);79#endif8081const std::string &shaderString =82"#version 300 es\n"83"#extension GL_ANGLE_base_vertex_base_instance : require\n"84"void main() {\n"85" gl_Position = vec4(float(gl_BaseVertex), float(gl_BaseInstance), 0.0, 1.0);\n"86"}\n";8788compile(shaderString, SH_OBJECT_CODE | SH_VARIABLES | SH_EMULATE_GL_BASE_VERTEX_BASE_INSTANCE);8990EXPECT_TRUE(notFoundInCode("gl_BaseVertex"));91EXPECT_TRUE(foundInCode("angle_BaseVertex"));92EXPECT_TRUE(notFoundInCode("gl_BaseInstance"));93EXPECT_TRUE(foundInCode("angle_BaseInstance"));94EXPECT_TRUE(notFoundInCode("GL_ANGLE_base_vertex_base_instance"));9596EXPECT_TRUE(foundInCode(SH_GLSL_COMPATIBILITY_OUTPUT, "uniform int angle_BaseVertex"));97EXPECT_TRUE(foundInCode(SH_GLSL_COMPATIBILITY_OUTPUT, "uniform int angle_BaseInstance"));98EXPECT_TRUE(foundInCode(SH_ESSL_OUTPUT, "uniform highp int angle_BaseVertex"));99EXPECT_TRUE(foundInCode(SH_ESSL_OUTPUT, "uniform highp int angle_BaseInstance"));100101#ifdef ANGLE_ENABLE_HLSL102EXPECT_TRUE(foundInCode(SH_HLSL_3_0_OUTPUT, "uniform int angle_BaseVertex : register"));103EXPECT_TRUE(foundInCode(SH_HLSL_3_0_OUTPUT, "uniform int angle_BaseInstance : register"));104#endif105}106107// Check that a user-defined "gl_BaseVertex" or "gl_BaseInstance" is not permitted108TEST_F(EmulateGLBaseVertexBaseInstanceTest, DisallowsUserDefinedGLDrawID)109{110// Check that it is not permitted without the extension111CheckCompileFailure(112"#version 300 es\n"113"uniform int gl_BaseVertex;\n"114"void main() {\n"115" gl_Position = vec4(float(gl_BaseVertex), 0.0, 0.0, 1.0);\n"116"}\n",117"reserved built-in name");118119CheckCompileFailure(120"#version 300 es\n"121"uniform int gl_BaseInstance;\n"122"void main() {\n"123" gl_Position = vec4(float(gl_BaseInstance), 0.0, 0.0, 1.0);\n"124"}\n",125"reserved built-in name");126127CheckCompileFailure(128"#version 300 es\n"129"void main() {\n"130" int gl_BaseVertex = 0;\n"131" gl_Position = vec4(float(gl_BaseVertex), 0.0, 0.0, 1.0);\n"132"}\n",133"reserved built-in name");134135CheckCompileFailure(136"#version 300 es\n"137"void main() {\n"138" int gl_BaseInstance = 0;\n"139" gl_Position = vec4(float(gl_BaseInstance), 0.0, 0.0, 1.0);\n"140"}\n",141"reserved built-in name");142143// Check that it is not permitted with the extension144CheckCompileFailure(145"#version 300 es\n"146"#extension GL_ANGLE_base_vertex_base_instance : require\n"147"uniform int gl_BaseVertex;\n"148"void main() {\n"149" gl_Position = vec4(float(gl_BaseVertex), 0.0, 0.0, 1.0);\n"150"}\n",151"reserved built-in name");152153CheckCompileFailure(154"#version 300 es\n"155"#extension GL_ANGLE_base_vertex_base_instance : require\n"156"uniform int gl_BaseInstance;\n"157"void main() {\n"158" gl_Position = vec4(float(gl_BaseInstance), 0.0, 0.0, 1.0);\n"159"}\n",160"reserved built-in name");161162CheckCompileFailure(163"#version 300 es\n"164"#extension GL_ANGLE_base_vertex_base_instance : require\n"165"void main() {\n"166" int gl_BaseVertex = 0;\n"167" gl_Position = vec4(float(gl_BaseVertex), 0.0, 0.0, 1.0);\n"168"}\n",169"reserved built-in name");170171CheckCompileFailure(172"#version 300 es\n"173"#extension GL_ANGLE_base_vertex_base_instance : require\n"174"void main() {\n"175" int gl_BaseInstance = 0;\n"176" gl_Position = vec4(float(gl_BaseInstance), 0.0, 0.0, 1.0);\n"177"}\n",178"reserved built-in name");179}180181// gl_BaseVertex and gl_BaseInstance are translated to angle_BaseVertex and angle_BaseInstance182// internally. Check that a user-defined angle_BaseVertex or angle_BaseInstance is permitted183TEST_F(EmulateGLBaseVertexBaseInstanceTest, AllowsUserDefinedANGLEDrawID)184{185addOutputType(SH_GLSL_COMPATIBILITY_OUTPUT);186addOutputType(SH_ESSL_OUTPUT);187#ifdef ANGLE_ENABLE_VULKAN188addOutputType(SH_SPIRV_VULKAN_OUTPUT);189#endif190#ifdef ANGLE_ENABLE_HLSL191addOutputType(SH_HLSL_3_0_OUTPUT);192addOutputType(SH_HLSL_3_0_OUTPUT);193#endif194195const std::string &shaderString =196"#version 300 es\n"197"#extension GL_ANGLE_base_vertex_base_instance : require\n"198"uniform int angle_BaseVertex;\n"199"uniform int angle_BaseInstance;\n"200"void main() {\n"201" gl_Position = vec4(\n"202" float(angle_BaseVertex + gl_BaseVertex),\n"203" float(angle_BaseInstance + gl_BaseInstance),\n"204" 0.0, 1.0);\n"205"}\n";206207compile(shaderString, SH_OBJECT_CODE | SH_VARIABLES | SH_EMULATE_GL_BASE_VERTEX_BASE_INSTANCE);208209// " angle_BaseVertex" (note the space) should appear exactly twice:210// once in the declaration and once in the body.211// The user-defined angle_BaseVertex should be decorated212EXPECT_TRUE(foundInCode(" angle_BaseVertex", 2));213EXPECT_TRUE(foundInCode(" angle_BaseInstance", 2));214}215216217