xref: /aosp_15_r20/external/deqp/modules/gles2/functional/es2fLightAmountTest.cpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1*35238bceSAndroid Build Coastguard Worker /*-------------------------------------------------------------------------
2*35238bceSAndroid Build Coastguard Worker  * drawElements Quality Program OpenGL ES 2.0 Module
3*35238bceSAndroid Build Coastguard Worker  * -------------------------------------------------
4*35238bceSAndroid Build Coastguard Worker  *
5*35238bceSAndroid Build Coastguard Worker  * Copyright 2014 The Android Open Source Project
6*35238bceSAndroid Build Coastguard Worker  *
7*35238bceSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
8*35238bceSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
9*35238bceSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
10*35238bceSAndroid Build Coastguard Worker  *
11*35238bceSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
12*35238bceSAndroid Build Coastguard Worker  *
13*35238bceSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
14*35238bceSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
15*35238bceSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16*35238bceSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
17*35238bceSAndroid Build Coastguard Worker  * limitations under the License.
18*35238bceSAndroid Build Coastguard Worker  *
19*35238bceSAndroid Build Coastguard Worker  *//*!
20*35238bceSAndroid Build Coastguard Worker  * \file
21*35238bceSAndroid Build Coastguard Worker  * \brief Light amount test.
22*35238bceSAndroid Build Coastguard Worker  *//*--------------------------------------------------------------------*/
23*35238bceSAndroid Build Coastguard Worker 
24*35238bceSAndroid Build Coastguard Worker #include "es2fLightAmountTest.hpp"
25*35238bceSAndroid Build Coastguard Worker #include "tcuStringTemplate.hpp"
26*35238bceSAndroid Build Coastguard Worker #include "gluDefs.hpp"
27*35238bceSAndroid Build Coastguard Worker #include "gluShaderProgram.hpp"
28*35238bceSAndroid Build Coastguard Worker #include "tcuTestLog.hpp"
29*35238bceSAndroid Build Coastguard Worker #include "deStringUtil.hpp"
30*35238bceSAndroid Build Coastguard Worker #include "deInt32.h"
31*35238bceSAndroid Build Coastguard Worker #include "deRandom.h"
32*35238bceSAndroid Build Coastguard Worker 
33*35238bceSAndroid Build Coastguard Worker #include <stdio.h>
34*35238bceSAndroid Build Coastguard Worker #include <vector>
35*35238bceSAndroid Build Coastguard Worker 
36*35238bceSAndroid Build Coastguard Worker #include "glw.h"
37*35238bceSAndroid Build Coastguard Worker 
38*35238bceSAndroid Build Coastguard Worker using namespace std;
39*35238bceSAndroid Build Coastguard Worker 
40*35238bceSAndroid Build Coastguard Worker namespace deqp
41*35238bceSAndroid Build Coastguard Worker {
42*35238bceSAndroid Build Coastguard Worker namespace gles2
43*35238bceSAndroid Build Coastguard Worker {
44*35238bceSAndroid Build Coastguard Worker namespace Functional
45*35238bceSAndroid Build Coastguard Worker {
46*35238bceSAndroid Build Coastguard Worker 
47*35238bceSAndroid Build Coastguard Worker const char *s_noLightsVertexShader = "uniform mat4 u_modelviewMatrix;\n"
48*35238bceSAndroid Build Coastguard Worker                                      "uniform mat4 u_modelviewProjectionMatrix;\n"
49*35238bceSAndroid Build Coastguard Worker                                      "uniform mat3 u_normalMatrix;\n"
50*35238bceSAndroid Build Coastguard Worker                                      "\n"
51*35238bceSAndroid Build Coastguard Worker                                      "attribute vec4 a_position;\n"
52*35238bceSAndroid Build Coastguard Worker                                      "attribute vec3 a_normal;\n"
53*35238bceSAndroid Build Coastguard Worker                                      "\n"
54*35238bceSAndroid Build Coastguard Worker                                      "varying vec3 v_color;\n"
55*35238bceSAndroid Build Coastguard Worker                                      "\n"
56*35238bceSAndroid Build Coastguard Worker                                      "void main()\n"
57*35238bceSAndroid Build Coastguard Worker                                      "{\n"
58*35238bceSAndroid Build Coastguard Worker                                      "    v_color = vec3(0.0);\n"
59*35238bceSAndroid Build Coastguard Worker                                      "    gl_Position = u_modelviewProjectionMatrix * a_position;\n"
60*35238bceSAndroid Build Coastguard Worker                                      "}\n";
61*35238bceSAndroid Build Coastguard Worker 
62*35238bceSAndroid Build Coastguard Worker const char *s_vertexShaderTemplate =
63*35238bceSAndroid Build Coastguard Worker     "struct Light\n"
64*35238bceSAndroid Build Coastguard Worker     "{\n"
65*35238bceSAndroid Build Coastguard Worker     "    vec3    position;\n"
66*35238bceSAndroid Build Coastguard Worker     "    vec3    diffuse;\n"
67*35238bceSAndroid Build Coastguard Worker     "    vec3    specular;\n"
68*35238bceSAndroid Build Coastguard Worker     "    vec3    attenuation;\n"
69*35238bceSAndroid Build Coastguard Worker     "};\n"
70*35238bceSAndroid Build Coastguard Worker     "uniform Light u_lights[${NUM_DIR_LIGHTS} + ${NUM_OMNI_LIGHTS}];\n"
71*35238bceSAndroid Build Coastguard Worker     "uniform mat4 u_modelviewMatrix;\n"
72*35238bceSAndroid Build Coastguard Worker     "uniform mat4 u_modelviewProjectionMatrix;\n"
73*35238bceSAndroid Build Coastguard Worker     "uniform mat3 u_normalMatrix;\n"
74*35238bceSAndroid Build Coastguard Worker     "\n"
75*35238bceSAndroid Build Coastguard Worker     "attribute vec4 a_position;\n"
76*35238bceSAndroid Build Coastguard Worker     "attribute vec3 a_normal;\n"
77*35238bceSAndroid Build Coastguard Worker     "\n"
78*35238bceSAndroid Build Coastguard Worker     "varying vec3 v_color;\n"
79*35238bceSAndroid Build Coastguard Worker     "\n"
80*35238bceSAndroid Build Coastguard Worker     "float computeAttenuation(vec3 dirToLight, vec3 attenuation)\n"
81*35238bceSAndroid Build Coastguard Worker     "{\n"
82*35238bceSAndroid Build Coastguard Worker     "    float dist = length(dirToLight);\n"
83*35238bceSAndroid Build Coastguard Worker     "    return 1.0 / (attenuation.x + attenuation.y*dist + attenuation.z*dist*dist);\n"
84*35238bceSAndroid Build Coastguard Worker     "}\n"
85*35238bceSAndroid Build Coastguard Worker     "\n"
86*35238bceSAndroid Build Coastguard Worker     "vec3 computeDirLight(int ndx, vec3 position, vec3 normal)\n"
87*35238bceSAndroid Build Coastguard Worker     "{\n"
88*35238bceSAndroid Build Coastguard Worker     "    Light light = u_lights[ndx];\n"
89*35238bceSAndroid Build Coastguard Worker     "    float cosAngle = dot(light.position, normal);\n"
90*35238bceSAndroid Build Coastguard Worker     "    return cosAngle * light.diffuse;\n"
91*35238bceSAndroid Build Coastguard Worker     "}\n"
92*35238bceSAndroid Build Coastguard Worker     "\n"
93*35238bceSAndroid Build Coastguard Worker     "vec3 computeOmniLight(int ndx, vec3 position, vec3 normal)\n"
94*35238bceSAndroid Build Coastguard Worker     "{\n"
95*35238bceSAndroid Build Coastguard Worker     "    Light light = u_lights[ndx];\n"
96*35238bceSAndroid Build Coastguard Worker     "    vec3 dirToLight = light.position - position;\n"
97*35238bceSAndroid Build Coastguard Worker     "    float cosAngle = dot(normalize(dirToLight), normal);\n"
98*35238bceSAndroid Build Coastguard Worker     "    float atten = computeAttenuation(dirToLight, light.attenuation);\n"
99*35238bceSAndroid Build Coastguard Worker     "    return atten * cosAngle * light.diffuse;\n"
100*35238bceSAndroid Build Coastguard Worker     "}\n"
101*35238bceSAndroid Build Coastguard Worker     "\n"
102*35238bceSAndroid Build Coastguard Worker     "void main()\n"
103*35238bceSAndroid Build Coastguard Worker     "{\n"
104*35238bceSAndroid Build Coastguard Worker     "    vec3 lightSpacePos = vec3(u_modelviewMatrix * a_position);\n"
105*35238bceSAndroid Build Coastguard Worker     "    vec3 lightNormal = normalize(u_normalMatrix * a_normal);\n"
106*35238bceSAndroid Build Coastguard Worker     "    vec3 color = vec3(0.0);\n"
107*35238bceSAndroid Build Coastguard Worker     "    for (int i = 0; i < ${NUM_DIR_LIGHTS}; i++)\n"
108*35238bceSAndroid Build Coastguard Worker     "        color += computeDirLight(i, lightSpacePos, lightNormal);\n"
109*35238bceSAndroid Build Coastguard Worker     "    for (int i = 0; i < ${NUM_OMNI_LIGHTS}; i++)\n"
110*35238bceSAndroid Build Coastguard Worker     "        color += computeOmniLight(${NUM_DIR_LIGHTS}+i, lightSpacePos, lightNormal);\n"
111*35238bceSAndroid Build Coastguard Worker     "    v_color = color;\n"
112*35238bceSAndroid Build Coastguard Worker     "    gl_Position = u_modelviewProjectionMatrix * a_position;\n"
113*35238bceSAndroid Build Coastguard Worker     "}\n";
114*35238bceSAndroid Build Coastguard Worker 
115*35238bceSAndroid Build Coastguard Worker const char *s_fragmentShaderTemplate = "varying highp vec3 v_color;\n"
116*35238bceSAndroid Build Coastguard Worker                                        "\n"
117*35238bceSAndroid Build Coastguard Worker                                        "void main()\n"
118*35238bceSAndroid Build Coastguard Worker                                        "{\n"
119*35238bceSAndroid Build Coastguard Worker                                        "    gl_FragColor = vec4(v_color, 1.0);\n"
120*35238bceSAndroid Build Coastguard Worker                                        "}\n";
121*35238bceSAndroid Build Coastguard Worker 
122*35238bceSAndroid Build Coastguard Worker class LightAmountCase : public TestCase
123*35238bceSAndroid Build Coastguard Worker {
124*35238bceSAndroid Build Coastguard Worker public:
LightAmountCase(Context & context,const char * name,int numDirectionalLights,int numOmniLights,int numSpotLights)125*35238bceSAndroid Build Coastguard Worker     LightAmountCase(Context &context, const char *name, int numDirectionalLights, int numOmniLights, int numSpotLights)
126*35238bceSAndroid Build Coastguard Worker         : TestCase(context, name, name)
127*35238bceSAndroid Build Coastguard Worker         , m_numDirectionalLights(numDirectionalLights)
128*35238bceSAndroid Build Coastguard Worker         , m_numOmniLights(numOmniLights)
129*35238bceSAndroid Build Coastguard Worker         , m_numSpotLights(numSpotLights)
130*35238bceSAndroid Build Coastguard Worker     {
131*35238bceSAndroid Build Coastguard Worker     }
132*35238bceSAndroid Build Coastguard Worker 
133*35238bceSAndroid Build Coastguard Worker     virtual IterateResult iterate(void);
134*35238bceSAndroid Build Coastguard Worker 
135*35238bceSAndroid Build Coastguard Worker private:
136*35238bceSAndroid Build Coastguard Worker     int m_numDirectionalLights;
137*35238bceSAndroid Build Coastguard Worker     int m_numOmniLights;
138*35238bceSAndroid Build Coastguard Worker     int m_numSpotLights;
139*35238bceSAndroid Build Coastguard Worker };
140*35238bceSAndroid Build Coastguard Worker 
iterate(void)141*35238bceSAndroid Build Coastguard Worker TestCase::IterateResult LightAmountCase::iterate(void)
142*35238bceSAndroid Build Coastguard Worker {
143*35238bceSAndroid Build Coastguard Worker     GLU_CHECK_MSG("LightAmountTest::iterate() begin");
144*35238bceSAndroid Build Coastguard Worker 
145*35238bceSAndroid Build Coastguard Worker     string vertexShaderSource;
146*35238bceSAndroid Build Coastguard Worker     string fragmentShaderSource;
147*35238bceSAndroid Build Coastguard Worker 
148*35238bceSAndroid Build Coastguard Worker     // Fill in shader template parameters.
149*35238bceSAndroid Build Coastguard Worker     {
150*35238bceSAndroid Build Coastguard Worker         bool hasAnyLights = ((m_numDirectionalLights + m_numOmniLights + m_numSpotLights) != 0);
151*35238bceSAndroid Build Coastguard Worker 
152*35238bceSAndroid Build Coastguard Worker         tcu::StringTemplate vertexTemplate(hasAnyLights ? s_vertexShaderTemplate : s_noLightsVertexShader);
153*35238bceSAndroid Build Coastguard Worker         tcu::StringTemplate fragmentTemplate(s_fragmentShaderTemplate);
154*35238bceSAndroid Build Coastguard Worker 
155*35238bceSAndroid Build Coastguard Worker         map<string, string> params;
156*35238bceSAndroid Build Coastguard Worker         params.insert(pair<string, string>("NUM_DIR_LIGHTS", de::toString(m_numDirectionalLights)));
157*35238bceSAndroid Build Coastguard Worker         params.insert(pair<string, string>("NUM_OMNI_LIGHTS", de::toString(m_numOmniLights)));
158*35238bceSAndroid Build Coastguard Worker         params.insert(pair<string, string>("NUM_SPOT_LIGHTS", de::toString(m_numSpotLights)));
159*35238bceSAndroid Build Coastguard Worker 
160*35238bceSAndroid Build Coastguard Worker         vertexShaderSource   = vertexTemplate.specialize(params);
161*35238bceSAndroid Build Coastguard Worker         fragmentShaderSource = fragmentTemplate.specialize(params);
162*35238bceSAndroid Build Coastguard Worker     }
163*35238bceSAndroid Build Coastguard Worker 
164*35238bceSAndroid Build Coastguard Worker     // Create shader and program objects.
165*35238bceSAndroid Build Coastguard Worker     glu::ShaderProgram program(m_context.getRenderContext(),
166*35238bceSAndroid Build Coastguard Worker                                glu::makeVtxFragSources(vertexShaderSource, fragmentShaderSource));
167*35238bceSAndroid Build Coastguard Worker     m_testCtx.getLog() << program;
168*35238bceSAndroid Build Coastguard Worker 
169*35238bceSAndroid Build Coastguard Worker     // Draw something? Check results?
170*35238bceSAndroid Build Coastguard Worker     glUseProgram(program.getProgram());
171*35238bceSAndroid Build Coastguard Worker 
172*35238bceSAndroid Build Coastguard Worker     bool testOk = program.isOk();
173*35238bceSAndroid Build Coastguard Worker 
174*35238bceSAndroid Build Coastguard Worker     GLU_CHECK_MSG("LightAmountTest::iterate() end");
175*35238bceSAndroid Build Coastguard Worker 
176*35238bceSAndroid Build Coastguard Worker     m_testCtx.setTestResult(testOk ? QP_TEST_RESULT_PASS : QP_TEST_RESULT_FAIL, testOk ? "Pass" : "Fail");
177*35238bceSAndroid Build Coastguard Worker     return TestCase::STOP;
178*35238bceSAndroid Build Coastguard Worker }
179*35238bceSAndroid Build Coastguard Worker 
180*35238bceSAndroid Build Coastguard Worker //
181*35238bceSAndroid Build Coastguard Worker 
LightAmountTest(Context & context)182*35238bceSAndroid Build Coastguard Worker LightAmountTest::LightAmountTest(Context &context) : TestCaseGroup(context, "light_amount", "Light Amount Stress Tests")
183*35238bceSAndroid Build Coastguard Worker {
184*35238bceSAndroid Build Coastguard Worker }
185*35238bceSAndroid Build Coastguard Worker 
~LightAmountTest(void)186*35238bceSAndroid Build Coastguard Worker LightAmountTest::~LightAmountTest(void)
187*35238bceSAndroid Build Coastguard Worker {
188*35238bceSAndroid Build Coastguard Worker }
189*35238bceSAndroid Build Coastguard Worker 
init(void)190*35238bceSAndroid Build Coastguard Worker void LightAmountTest::init(void)
191*35238bceSAndroid Build Coastguard Worker {
192*35238bceSAndroid Build Coastguard Worker     //                                        name                dir, omni, spot
193*35238bceSAndroid Build Coastguard Worker     addChild(new LightAmountCase(m_context, "none", 0, 0, 0));
194*35238bceSAndroid Build Coastguard Worker     addChild(new LightAmountCase(m_context, "1dir", 1, 0, 0));
195*35238bceSAndroid Build Coastguard Worker     addChild(new LightAmountCase(m_context, "2dir", 2, 0, 0));
196*35238bceSAndroid Build Coastguard Worker     addChild(new LightAmountCase(m_context, "4dir", 4, 0, 0));
197*35238bceSAndroid Build Coastguard Worker     addChild(new LightAmountCase(m_context, "6dir", 6, 0, 0));
198*35238bceSAndroid Build Coastguard Worker     addChild(new LightAmountCase(m_context, "8dir", 8, 0, 0));
199*35238bceSAndroid Build Coastguard Worker     addChild(new LightAmountCase(m_context, "10dir", 10, 0, 0));
200*35238bceSAndroid Build Coastguard Worker     addChild(new LightAmountCase(m_context, "12dir", 12, 0, 0));
201*35238bceSAndroid Build Coastguard Worker     addChild(new LightAmountCase(m_context, "14dir", 14, 0, 0));
202*35238bceSAndroid Build Coastguard Worker     addChild(new LightAmountCase(m_context, "16dir", 16, 0, 0));
203*35238bceSAndroid Build Coastguard Worker     addChild(new LightAmountCase(m_context, "1omni", 0, 1, 0));
204*35238bceSAndroid Build Coastguard Worker     addChild(new LightAmountCase(m_context, "2omni", 0, 2, 0));
205*35238bceSAndroid Build Coastguard Worker     addChild(new LightAmountCase(m_context, "4omni", 0, 4, 0));
206*35238bceSAndroid Build Coastguard Worker     addChild(new LightAmountCase(m_context, "6omni", 0, 6, 0));
207*35238bceSAndroid Build Coastguard Worker     addChild(new LightAmountCase(m_context, "8omni", 0, 8, 0));
208*35238bceSAndroid Build Coastguard Worker     addChild(new LightAmountCase(m_context, "10omni", 0, 10, 0));
209*35238bceSAndroid Build Coastguard Worker     addChild(new LightAmountCase(m_context, "12omni", 0, 12, 0));
210*35238bceSAndroid Build Coastguard Worker     addChild(new LightAmountCase(m_context, "14omni", 0, 14, 0));
211*35238bceSAndroid Build Coastguard Worker     addChild(new LightAmountCase(m_context, "16omni", 0, 16, 0));
212*35238bceSAndroid Build Coastguard Worker     // addChild(new LightAmountCase(m_context, "1spot", 0, 0, 1 ));
213*35238bceSAndroid Build Coastguard Worker     // addChild(new LightAmountCase(m_context, "2spot", 0, 0, 2 ));
214*35238bceSAndroid Build Coastguard Worker     // addChild(new LightAmountCase(m_context, "4spot", 0, 0, 4 ));
215*35238bceSAndroid Build Coastguard Worker     // addChild(new LightAmountCase(m_context, "6spot", 0, 0, 6 ));
216*35238bceSAndroid Build Coastguard Worker     // addChild(new LightAmountCase(m_context, "8spot", 0, 0, 8 ));
217*35238bceSAndroid Build Coastguard Worker     // addChild(new LightAmountCase(m_context, "1dir_1omni", 1, 1, 0 ));
218*35238bceSAndroid Build Coastguard Worker     // addChild(new LightAmountCase(m_context, "2dir_2omni", 2, 2, 0 ));
219*35238bceSAndroid Build Coastguard Worker     // addChild(new LightAmountCase(m_context, "4dir_4omni", 4, 4, 0 ));
220*35238bceSAndroid Build Coastguard Worker     // addChild(new LightAmountCase(m_context, "1dir_1spot", 1, 0, 1 ));
221*35238bceSAndroid Build Coastguard Worker     // addChild(new LightAmountCase(m_context, "2dir_2spot", 2, 0, 2 ));
222*35238bceSAndroid Build Coastguard Worker     // addChild(new LightAmountCase(m_context, "4dir_4spot", 4, 0, 4 ));
223*35238bceSAndroid Build Coastguard Worker     // addChild(new LightAmountCase(m_context, "1omni_1spot", 0, 1, 1 ));
224*35238bceSAndroid Build Coastguard Worker     // addChild(new LightAmountCase(m_context, "2omni_2spot", 0, 2, 2 ));
225*35238bceSAndroid Build Coastguard Worker     // addChild(new LightAmountCase(m_context, "4omni_4spot", 0, 4, 4 ));
226*35238bceSAndroid Build Coastguard Worker     // addChild(new LightAmountCase(m_context, "1dir_1omni_1spot", 1, 1, 1 ));
227*35238bceSAndroid Build Coastguard Worker     // addChild(new LightAmountCase(m_context, "2dir_2omni_2spot", 2, 2, 2 ));
228*35238bceSAndroid Build Coastguard Worker     // addChild(new LightAmountCase(m_context, "4dir_2omni_2spot", 4, 2, 2 ));
229*35238bceSAndroid Build Coastguard Worker     // addChild(new LightAmountCase(m_context, "2dir_4omni_2spot", 2, 4, 2 ));
230*35238bceSAndroid Build Coastguard Worker     // addChild(new LightAmountCase(m_context, "2dir_2omni_4spot", 2, 2, 4 ));
231*35238bceSAndroid Build Coastguard Worker     // addChild(new LightAmountCase(m_context, "4dir_4omni_4spot", 4, 4, 4 ));
232*35238bceSAndroid Build Coastguard Worker }
233*35238bceSAndroid Build Coastguard Worker 
234*35238bceSAndroid Build Coastguard Worker } // namespace Functional
235*35238bceSAndroid Build Coastguard Worker } // namespace gles2
236*35238bceSAndroid Build Coastguard Worker } // namespace deqp
237