xref: /aosp_15_r20/external/angle/src/tests/gl_tests/ShaderInterpTest.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker //
2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2020 The ANGLE Project Authors. All rights reserved.
3*8975f5c5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker // found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker //
6*8975f5c5SAndroid Build Coastguard Worker // Tests for shader interpolation qualifiers
7*8975f5c5SAndroid Build Coastguard Worker //
8*8975f5c5SAndroid Build Coastguard Worker 
9*8975f5c5SAndroid Build Coastguard Worker #include "common/mathutil.h"
10*8975f5c5SAndroid Build Coastguard Worker #include "test_utils/ANGLETest.h"
11*8975f5c5SAndroid Build Coastguard Worker #include "test_utils/gl_raii.h"
12*8975f5c5SAndroid Build Coastguard Worker 
13*8975f5c5SAndroid Build Coastguard Worker using namespace angle;
14*8975f5c5SAndroid Build Coastguard Worker 
15*8975f5c5SAndroid Build Coastguard Worker constexpr int kPixelColorThreshhold = 8;
16*8975f5c5SAndroid Build Coastguard Worker 
17*8975f5c5SAndroid Build Coastguard Worker class ShaderInterpTest : public ANGLETest<>
18*8975f5c5SAndroid Build Coastguard Worker {
19*8975f5c5SAndroid Build Coastguard Worker   protected:
ShaderInterpTest()20*8975f5c5SAndroid Build Coastguard Worker     ShaderInterpTest() : ANGLETest()
21*8975f5c5SAndroid Build Coastguard Worker     {
22*8975f5c5SAndroid Build Coastguard Worker         setWindowWidth(128);
23*8975f5c5SAndroid Build Coastguard Worker         setWindowHeight(128);
24*8975f5c5SAndroid Build Coastguard Worker     }
25*8975f5c5SAndroid Build Coastguard Worker 
draw(GLuint program,float skew)26*8975f5c5SAndroid Build Coastguard Worker     void draw(GLuint program, float skew)
27*8975f5c5SAndroid Build Coastguard Worker     {
28*8975f5c5SAndroid Build Coastguard Worker         glUseProgram(program);
29*8975f5c5SAndroid Build Coastguard Worker 
30*8975f5c5SAndroid Build Coastguard Worker         std::array<Vector4, 3> vertices;
31*8975f5c5SAndroid Build Coastguard Worker         vertices[0] = {-1.0, -1.0, 0.0, 1.0};
32*8975f5c5SAndroid Build Coastguard Worker         vertices[1] = {1.0, -1.0, 0.0, 1.0};
33*8975f5c5SAndroid Build Coastguard Worker         vertices[2] = {0.0, 1.0 * skew, 0.0, skew};
34*8975f5c5SAndroid Build Coastguard Worker 
35*8975f5c5SAndroid Build Coastguard Worker         std::array<Vector4, 3> colors;
36*8975f5c5SAndroid Build Coastguard Worker         colors[0] = {1.0, 0.0, 0.0, 1.0};
37*8975f5c5SAndroid Build Coastguard Worker         colors[1] = {0.0, 1.0, 0.0, 1.0};
38*8975f5c5SAndroid Build Coastguard Worker         colors[2] = {0.0, 0.0, 1.0, 1.0};
39*8975f5c5SAndroid Build Coastguard Worker 
40*8975f5c5SAndroid Build Coastguard Worker         GLint positionLocation = glGetAttribLocation(program, "position");
41*8975f5c5SAndroid Build Coastguard Worker         GLint colorLocation    = glGetAttribLocation(program, "vertex_color");
42*8975f5c5SAndroid Build Coastguard Worker 
43*8975f5c5SAndroid Build Coastguard Worker         glVertexAttribPointer(positionLocation, 4, GL_FLOAT, GL_FALSE, 0, vertices.data());
44*8975f5c5SAndroid Build Coastguard Worker         glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, 0, colors.data());
45*8975f5c5SAndroid Build Coastguard Worker 
46*8975f5c5SAndroid Build Coastguard Worker         glEnableVertexAttribArray(positionLocation);
47*8975f5c5SAndroid Build Coastguard Worker         glEnableVertexAttribArray(colorLocation);
48*8975f5c5SAndroid Build Coastguard Worker 
49*8975f5c5SAndroid Build Coastguard Worker         glDrawArrays(GL_TRIANGLES, 0, 3);
50*8975f5c5SAndroid Build Coastguard Worker     }
51*8975f5c5SAndroid Build Coastguard Worker };
52*8975f5c5SAndroid Build Coastguard Worker 
53*8975f5c5SAndroid Build Coastguard Worker // Test that regular "smooth" interpolation works correctly
TEST_P(ShaderInterpTest,Smooth)54*8975f5c5SAndroid Build Coastguard Worker TEST_P(ShaderInterpTest, Smooth)
55*8975f5c5SAndroid Build Coastguard Worker {
56*8975f5c5SAndroid Build Coastguard Worker     const char *vertSrc = R"(#version 300 es
57*8975f5c5SAndroid Build Coastguard Worker precision highp float;
58*8975f5c5SAndroid Build Coastguard Worker in vec4 position;
59*8975f5c5SAndroid Build Coastguard Worker in vec4 vertex_color;
60*8975f5c5SAndroid Build Coastguard Worker smooth out vec4 interp_color;
61*8975f5c5SAndroid Build Coastguard Worker 
62*8975f5c5SAndroid Build Coastguard Worker void main()
63*8975f5c5SAndroid Build Coastguard Worker {
64*8975f5c5SAndroid Build Coastguard Worker     gl_Position = position;
65*8975f5c5SAndroid Build Coastguard Worker     interp_color = vertex_color;
66*8975f5c5SAndroid Build Coastguard Worker }
67*8975f5c5SAndroid Build Coastguard Worker )";
68*8975f5c5SAndroid Build Coastguard Worker     const char *fragSrc = R"(#version 300 es
69*8975f5c5SAndroid Build Coastguard Worker precision highp float;
70*8975f5c5SAndroid Build Coastguard Worker smooth in vec4 interp_color;
71*8975f5c5SAndroid Build Coastguard Worker out vec4 fragColor;
72*8975f5c5SAndroid Build Coastguard Worker 
73*8975f5c5SAndroid Build Coastguard Worker void main()
74*8975f5c5SAndroid Build Coastguard Worker {
75*8975f5c5SAndroid Build Coastguard Worker     fragColor = interp_color;
76*8975f5c5SAndroid Build Coastguard Worker }
77*8975f5c5SAndroid Build Coastguard Worker )";
78*8975f5c5SAndroid Build Coastguard Worker 
79*8975f5c5SAndroid Build Coastguard Worker     ANGLE_GL_PROGRAM(program, vertSrc, fragSrc);
80*8975f5c5SAndroid Build Coastguard Worker     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
81*8975f5c5SAndroid Build Coastguard Worker 
82*8975f5c5SAndroid Build Coastguard Worker     glClear(GL_COLOR_BUFFER_BIT);
83*8975f5c5SAndroid Build Coastguard Worker     draw(program, 1.0);
84*8975f5c5SAndroid Build Coastguard Worker     EXPECT_PIXEL_COLOR_NEAR(64, 64, GLColor(62, 64, 128, 255), kPixelColorThreshhold);
85*8975f5c5SAndroid Build Coastguard Worker 
86*8975f5c5SAndroid Build Coastguard Worker     glClear(GL_COLOR_BUFFER_BIT);
87*8975f5c5SAndroid Build Coastguard Worker     draw(program, 2.0);
88*8975f5c5SAndroid Build Coastguard Worker     EXPECT_PIXEL_COLOR_NEAR(64, 64, GLColor(83, 86, 86, 255), kPixelColorThreshhold);
89*8975f5c5SAndroid Build Coastguard Worker }
90*8975f5c5SAndroid Build Coastguard Worker 
91*8975f5c5SAndroid Build Coastguard Worker // Test that uninterpolated "Flat" interpolation works correctly
TEST_P(ShaderInterpTest,Flat)92*8975f5c5SAndroid Build Coastguard Worker TEST_P(ShaderInterpTest, Flat)
93*8975f5c5SAndroid Build Coastguard Worker {
94*8975f5c5SAndroid Build Coastguard Worker     // TODO: anglebug.com/42262721
95*8975f5c5SAndroid Build Coastguard Worker     // No vendors currently support VK_EXT_provoking_vertex, which is necessary for conformant flat
96*8975f5c5SAndroid Build Coastguard Worker     // shading. SwiftShader does technically support this extension, but as it has not yet been
97*8975f5c5SAndroid Build Coastguard Worker     // ratified by Khronos, the vulkan validation layers do not recognize the create info struct,
98*8975f5c5SAndroid Build Coastguard Worker     // causing it to be stripped and thus causing the extension to behave as if it is disabled.
99*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(IsVulkan());
100*8975f5c5SAndroid Build Coastguard Worker 
101*8975f5c5SAndroid Build Coastguard Worker     const char *vertSrc = R"(#version 300 es
102*8975f5c5SAndroid Build Coastguard Worker precision highp float;
103*8975f5c5SAndroid Build Coastguard Worker in vec4 position;
104*8975f5c5SAndroid Build Coastguard Worker in vec4 vertex_color;
105*8975f5c5SAndroid Build Coastguard Worker flat out vec4 interp_color;
106*8975f5c5SAndroid Build Coastguard Worker 
107*8975f5c5SAndroid Build Coastguard Worker void main()
108*8975f5c5SAndroid Build Coastguard Worker {
109*8975f5c5SAndroid Build Coastguard Worker     gl_Position = position;
110*8975f5c5SAndroid Build Coastguard Worker     interp_color = vertex_color;
111*8975f5c5SAndroid Build Coastguard Worker }
112*8975f5c5SAndroid Build Coastguard Worker )";
113*8975f5c5SAndroid Build Coastguard Worker     const char *fragSrc = R"(#version 300 es
114*8975f5c5SAndroid Build Coastguard Worker precision highp float;
115*8975f5c5SAndroid Build Coastguard Worker flat in vec4 interp_color;
116*8975f5c5SAndroid Build Coastguard Worker out vec4 fragColor;
117*8975f5c5SAndroid Build Coastguard Worker 
118*8975f5c5SAndroid Build Coastguard Worker void main()
119*8975f5c5SAndroid Build Coastguard Worker {
120*8975f5c5SAndroid Build Coastguard Worker     fragColor = interp_color;
121*8975f5c5SAndroid Build Coastguard Worker }
122*8975f5c5SAndroid Build Coastguard Worker )";
123*8975f5c5SAndroid Build Coastguard Worker 
124*8975f5c5SAndroid Build Coastguard Worker     ANGLE_GL_PROGRAM(program, vertSrc, fragSrc);
125*8975f5c5SAndroid Build Coastguard Worker     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
126*8975f5c5SAndroid Build Coastguard Worker 
127*8975f5c5SAndroid Build Coastguard Worker     glClear(GL_COLOR_BUFFER_BIT);
128*8975f5c5SAndroid Build Coastguard Worker     draw(program, 1.0);
129*8975f5c5SAndroid Build Coastguard Worker     GLColor smooth_reference;
130*8975f5c5SAndroid Build Coastguard Worker     glReadPixels(64, 64, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &smooth_reference);
131*8975f5c5SAndroid Build Coastguard Worker     EXPECT_PIXEL_COLOR_EQ(64, 64, GLColor(0, 0, 255, 255));
132*8975f5c5SAndroid Build Coastguard Worker }
133*8975f5c5SAndroid Build Coastguard Worker 
134*8975f5c5SAndroid Build Coastguard Worker // Test that "noperspective" interpolation correctly interpolates in screenspace
TEST_P(ShaderInterpTest,NoPerspective)135*8975f5c5SAndroid Build Coastguard Worker TEST_P(ShaderInterpTest, NoPerspective)
136*8975f5c5SAndroid Build Coastguard Worker {
137*8975f5c5SAndroid Build Coastguard Worker     ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_NV_shader_noperspective_interpolation"));
138*8975f5c5SAndroid Build Coastguard Worker 
139*8975f5c5SAndroid Build Coastguard Worker     const char *vertSrcSmooth = R"(#version 300 es
140*8975f5c5SAndroid Build Coastguard Worker precision highp float;
141*8975f5c5SAndroid Build Coastguard Worker in vec4 position;
142*8975f5c5SAndroid Build Coastguard Worker in vec4 vertex_color;
143*8975f5c5SAndroid Build Coastguard Worker smooth out vec4 interp_color;
144*8975f5c5SAndroid Build Coastguard Worker 
145*8975f5c5SAndroid Build Coastguard Worker void main()
146*8975f5c5SAndroid Build Coastguard Worker {
147*8975f5c5SAndroid Build Coastguard Worker     gl_Position = position;
148*8975f5c5SAndroid Build Coastguard Worker     interp_color = vertex_color;
149*8975f5c5SAndroid Build Coastguard Worker }
150*8975f5c5SAndroid Build Coastguard Worker )";
151*8975f5c5SAndroid Build Coastguard Worker     const char *fragSrcSmooth = R"(#version 300 es
152*8975f5c5SAndroid Build Coastguard Worker precision highp float;
153*8975f5c5SAndroid Build Coastguard Worker smooth in vec4 interp_color;
154*8975f5c5SAndroid Build Coastguard Worker out vec4 fragColor;
155*8975f5c5SAndroid Build Coastguard Worker 
156*8975f5c5SAndroid Build Coastguard Worker void main()
157*8975f5c5SAndroid Build Coastguard Worker {
158*8975f5c5SAndroid Build Coastguard Worker     fragColor = interp_color;
159*8975f5c5SAndroid Build Coastguard Worker }
160*8975f5c5SAndroid Build Coastguard Worker )";
161*8975f5c5SAndroid Build Coastguard Worker     ANGLE_GL_PROGRAM(programSmooth, vertSrcSmooth, fragSrcSmooth);
162*8975f5c5SAndroid Build Coastguard Worker     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
163*8975f5c5SAndroid Build Coastguard Worker     glClear(GL_COLOR_BUFFER_BIT);
164*8975f5c5SAndroid Build Coastguard Worker     draw(programSmooth, 1.0);
165*8975f5c5SAndroid Build Coastguard Worker     GLColor smooth_reference;
166*8975f5c5SAndroid Build Coastguard Worker     glReadPixels(64, 64, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &smooth_reference);
167*8975f5c5SAndroid Build Coastguard Worker 
168*8975f5c5SAndroid Build Coastguard Worker     const char *vertSrcNoPerspective = R"(#version 300 es
169*8975f5c5SAndroid Build Coastguard Worker #extension GL_NV_shader_noperspective_interpolation : require
170*8975f5c5SAndroid Build Coastguard Worker 
171*8975f5c5SAndroid Build Coastguard Worker #ifndef GL_NV_shader_noperspective_interpolation
172*8975f5c5SAndroid Build Coastguard Worker #error GL_NV_shader_noperspective_interpolation is not defined
173*8975f5c5SAndroid Build Coastguard Worker #endif
174*8975f5c5SAndroid Build Coastguard Worker 
175*8975f5c5SAndroid Build Coastguard Worker precision highp float;
176*8975f5c5SAndroid Build Coastguard Worker in vec4 position;
177*8975f5c5SAndroid Build Coastguard Worker in vec4 vertex_color;
178*8975f5c5SAndroid Build Coastguard Worker noperspective out vec4 interp_color;
179*8975f5c5SAndroid Build Coastguard Worker 
180*8975f5c5SAndroid Build Coastguard Worker void main()
181*8975f5c5SAndroid Build Coastguard Worker {
182*8975f5c5SAndroid Build Coastguard Worker     gl_Position = position;
183*8975f5c5SAndroid Build Coastguard Worker     interp_color = vertex_color;
184*8975f5c5SAndroid Build Coastguard Worker }
185*8975f5c5SAndroid Build Coastguard Worker )";
186*8975f5c5SAndroid Build Coastguard Worker     const char *fragSrcNoPerspective = R"(#version 300 es
187*8975f5c5SAndroid Build Coastguard Worker #extension GL_NV_shader_noperspective_interpolation : require
188*8975f5c5SAndroid Build Coastguard Worker 
189*8975f5c5SAndroid Build Coastguard Worker #ifndef GL_NV_shader_noperspective_interpolation
190*8975f5c5SAndroid Build Coastguard Worker #error GL_NV_shader_noperspective_interpolation is not defined
191*8975f5c5SAndroid Build Coastguard Worker #endif
192*8975f5c5SAndroid Build Coastguard Worker 
193*8975f5c5SAndroid Build Coastguard Worker precision highp float;
194*8975f5c5SAndroid Build Coastguard Worker noperspective in vec4 interp_color;
195*8975f5c5SAndroid Build Coastguard Worker out vec4 fragColor;
196*8975f5c5SAndroid Build Coastguard Worker 
197*8975f5c5SAndroid Build Coastguard Worker void main()
198*8975f5c5SAndroid Build Coastguard Worker {
199*8975f5c5SAndroid Build Coastguard Worker     fragColor = interp_color;
200*8975f5c5SAndroid Build Coastguard Worker }
201*8975f5c5SAndroid Build Coastguard Worker )";
202*8975f5c5SAndroid Build Coastguard Worker     ANGLE_GL_PROGRAM(programNoPerspective, vertSrcNoPerspective, fragSrcNoPerspective);
203*8975f5c5SAndroid Build Coastguard Worker     glClear(GL_COLOR_BUFFER_BIT);
204*8975f5c5SAndroid Build Coastguard Worker     draw(programNoPerspective, 1.0);
205*8975f5c5SAndroid Build Coastguard Worker     EXPECT_PIXEL_COLOR_EQ(64, 64, smooth_reference);
206*8975f5c5SAndroid Build Coastguard Worker 
207*8975f5c5SAndroid Build Coastguard Worker     glClear(GL_COLOR_BUFFER_BIT);
208*8975f5c5SAndroid Build Coastguard Worker     draw(programNoPerspective, 2.0);
209*8975f5c5SAndroid Build Coastguard Worker     EXPECT_PIXEL_COLOR_EQ(64, 64, smooth_reference);
210*8975f5c5SAndroid Build Coastguard Worker }
211*8975f5c5SAndroid Build Coastguard Worker 
212*8975f5c5SAndroid Build Coastguard Worker GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ShaderInterpTest);
213*8975f5c5SAndroid Build Coastguard Worker ANGLE_INSTANTIATE_TEST_ES3(ShaderInterpTest);