Path: blob/main_old/src/tests/gl_tests/BuiltinVariableTest.cpp
1693 views
//1// Copyright 2016 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// BuiltinVariableTest:6// Tests the correctness of the builtin GLSL variables.7//89#include "test_utils/ANGLETest.h"10#include "test_utils/gl_raii.h"1112using namespace angle;1314class BuiltinVariableVertexIdTest : public ANGLETest15{16protected:17BuiltinVariableVertexIdTest()18{19setWindowWidth(64);20setWindowHeight(64);21setConfigRedBits(8);22setConfigGreenBits(8);23setConfigBlueBits(8);24setConfigAlphaBits(8);25setConfigDepthBits(24);26}2728void testSetUp() override29{30constexpr char kVS[] =31"#version 300 es\n"32"precision highp float;\n"33"in vec4 position;\n"34"in int expectedID;"35"out vec4 color;\n"36"\n"37"void main()\n"38"{\n"39" gl_Position = position;\n"40" color = vec4(gl_VertexID != expectedID, gl_VertexID == expectedID, 0.0, 1.0);"41"}\n";4243constexpr char kFS[] =44"#version 300 es\n"45"precision highp float;\n"46"in vec4 color;\n"47"out vec4 fragColor;\n"48"void main()\n"49"{\n"50" fragColor = color;\n"51"}\n";5253mProgram = CompileProgram(kVS, kFS);54ASSERT_NE(0u, mProgram);5556mPositionLocation = glGetAttribLocation(mProgram, "position");57ASSERT_NE(-1, mPositionLocation);58mExpectedIdLocation = glGetAttribLocation(mProgram, "expectedID");59ASSERT_NE(-1, mExpectedIdLocation);6061static const float positions[] = {0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, -0.5};62glGenBuffers(1, &mPositionBuffer);63glBindBuffer(GL_ARRAY_BUFFER, mPositionBuffer);64glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);6566ASSERT_GL_NO_ERROR();67}6869void testTearDown() override70{71glDeleteBuffers(1, &mPositionBuffer);72glDeleteBuffers(1, &mExpectedIdBuffer);73glDeleteBuffers(1, &mIndexBuffer);74glDeleteProgram(mProgram);75}7677// Renders a primitive using the specified mode, each vertex color will78// be green if gl_VertexID is correct, red otherwise.79void runTest(GLuint drawMode, const std::vector<GLint> &indices, int count)80{81glClearColor(0.0, 0.0, 0.0, 1.0);82glClear(GL_COLOR_BUFFER_BIT);8384glGenBuffers(1, &mIndexBuffer);85glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);86glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLint) * indices.size(), indices.data(),87GL_STATIC_DRAW);8889std::vector<GLint> expectedIds = makeRange(count);9091glGenBuffers(1, &mExpectedIdBuffer);92glBindBuffer(GL_ARRAY_BUFFER, mExpectedIdBuffer);93glBufferData(GL_ARRAY_BUFFER, sizeof(GLint) * expectedIds.size(), expectedIds.data(),94GL_STATIC_DRAW);9596glBindBuffer(GL_ARRAY_BUFFER, mPositionBuffer);97glVertexAttribPointer(mPositionLocation, 2, GL_FLOAT, GL_FALSE, 0, 0);98glEnableVertexAttribArray(mPositionLocation);99100glBindBuffer(GL_ARRAY_BUFFER, mExpectedIdBuffer);101glVertexAttribIPointer(mExpectedIdLocation, 1, GL_INT, 0, 0);102glEnableVertexAttribArray(mExpectedIdLocation);103104glUseProgram(mProgram);105glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);106glDrawElements(drawMode, count, GL_UNSIGNED_INT, 0);107108std::vector<GLColor> pixels(getWindowWidth() * getWindowHeight());109glReadPixels(0, 0, getWindowWidth(), getWindowHeight(), GL_RGBA, GL_UNSIGNED_BYTE,110pixels.data());111112ASSERT_GL_NO_ERROR();113114const GLColor green(0, 255, 0, 255);115const GLColor black(0, 0, 0, 255);116117for (const auto &pixel : pixels)118{119EXPECT_TRUE(pixel == green || pixel == black);120}121}122123std::vector<GLint> makeRange(int n) const124{125std::vector<GLint> result;126for (int i = 0; i < n; i++)127{128result.push_back(i);129}130131return result;132}133134GLuint mPositionBuffer = 0;135GLuint mExpectedIdBuffer = 0;136GLuint mIndexBuffer = 0;137138GLuint mProgram = 0;139GLint mPositionLocation = -1;140GLint mExpectedIdLocation = -1;141};142143// Test gl_VertexID when rendering points144TEST_P(BuiltinVariableVertexIdTest, Points)145{146runTest(GL_POINTS, makeRange(4), 4);147}148149// Test gl_VertexID when rendering line strips150TEST_P(BuiltinVariableVertexIdTest, LineStrip)151{152runTest(GL_LINE_STRIP, makeRange(4), 4);153}154155// Test gl_VertexID when rendering line loops156TEST_P(BuiltinVariableVertexIdTest, LineLoop)157{158runTest(GL_LINE_LOOP, makeRange(4), 4);159}160161// Test gl_VertexID when rendering lines162TEST_P(BuiltinVariableVertexIdTest, Lines)163{164runTest(GL_LINES, makeRange(4), 4);165}166167// Test gl_VertexID when rendering triangle strips168TEST_P(BuiltinVariableVertexIdTest, TriangleStrip)169{170runTest(GL_TRIANGLE_STRIP, makeRange(4), 4);171}172173// Test gl_VertexID when rendering triangle fans174TEST_P(BuiltinVariableVertexIdTest, TriangleFan)175{176std::vector<GLint> indices;177indices.push_back(0);178indices.push_back(1);179indices.push_back(3);180indices.push_back(2);181runTest(GL_TRIANGLE_FAN, indices, 4);182}183184// Test gl_VertexID when rendering triangles185TEST_P(BuiltinVariableVertexIdTest, Triangles)186{187std::vector<GLint> indices;188indices.push_back(0);189indices.push_back(1);190indices.push_back(2);191indices.push_back(1);192indices.push_back(2);193indices.push_back(3);194runTest(GL_TRIANGLES, indices, 6);195}196197GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BuiltinVariableVertexIdTest);198ANGLE_INSTANTIATE_TEST_ES3(BuiltinVariableVertexIdTest);199200class BuiltinVariableFragDepthClampingFloatRBOTest : public ANGLETest201{202protected:203void testSetUp() override204{205// Writes a fixed detph value and green.206// Section 15.2.3 of the GL 4.5 specification says that conversion is not207// done but clamping is so the output depth should be in [0.0, 1.0]208constexpr char kFS[] =209R"(#version 300 es210precision highp float;211layout(location = 0) out vec4 fragColor;212uniform float u_depth;213void main(){214gl_FragDepth = u_depth;215fragColor = vec4(0.0, 1.0, 0.0, 1.0);216})";217218mProgram = CompileProgram(essl3_shaders::vs::Simple(), kFS);219ASSERT_NE(0u, mProgram);220221mDepthLocation = glGetUniformLocation(mProgram, "u_depth");222ASSERT_NE(-1, mDepthLocation);223224glBindTexture(GL_TEXTURE_2D, mColorTexture);225glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 1, 1);226glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);227glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);228229glBindTexture(GL_TEXTURE_2D, mDepthTexture);230glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT32F, 1, 1);231glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);232glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);233234glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);235glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mColorTexture,2360);237glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, mDepthTexture,2380);239240ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));241ASSERT_GL_NO_ERROR();242}243244void testTearDown() override { glDeleteProgram(mProgram); }245246void CheckDepthWritten(float expectedDepth, float fsDepth)247{248glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);249glUseProgram(mProgram);250251// Clear to red, the FS will write green on success252glClearColor(1.0f, 0.0f, 0.0f, 0.0f);253// Clear to the expected depth so it will be compared to the FS depth with254// DepthFunc(GL_EQUAL)255glClearDepthf(expectedDepth);256glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);257258glUniform1f(mDepthLocation, fsDepth);259glDepthFunc(GL_EQUAL);260glEnable(GL_DEPTH_TEST);261262drawQuad(mProgram, "a_position", 0.0f);263EXPECT_GL_NO_ERROR();264265EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);266}267268private:269GLuint mProgram;270GLint mDepthLocation;271272GLTexture mColorTexture;273GLTexture mDepthTexture;274GLFramebuffer mFramebuffer;275};276277// Test that gl_FragDepth is clamped above 0278TEST_P(BuiltinVariableFragDepthClampingFloatRBOTest, Above0)279{280CheckDepthWritten(0.0f, -1.0f);281}282283// Test that gl_FragDepth is clamped below 1284TEST_P(BuiltinVariableFragDepthClampingFloatRBOTest, Below1)285{286// TODO(anglebug.com/5360): Failing on ARM-based Apple DTKs.287ANGLE_SKIP_TEST_IF(IsOSX() && IsARM64() && IsDesktopOpenGL());288289CheckDepthWritten(1.0f, 42.0f);290}291292GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BuiltinVariableFragDepthClampingFloatRBOTest);293ANGLE_INSTANTIATE_TEST(BuiltinVariableFragDepthClampingFloatRBOTest,294ES3_D3D11(),295ES3_OPENGL(),296ES3_OPENGLES());297298299