1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES 2.0 Module
3 * -------------------------------------------------
4 *
5 * Copyright 2014 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 *//*!
20 * \file
21 * \brief Varying interpolation accuracy tests.
22 *//*--------------------------------------------------------------------*/
23
24 #include "es2aVaryingInterpolationTests.hpp"
25 #include "gluPixelTransfer.hpp"
26 #include "gluShaderProgram.hpp"
27 #include "gluShaderUtil.hpp"
28 #include "tcuStringTemplate.hpp"
29 #include "gluContextInfo.hpp"
30 #include "glsTextureTestUtil.hpp"
31 #include "tcuVector.hpp"
32 #include "tcuVectorUtil.hpp"
33 #include "tcuTestLog.hpp"
34 #include "tcuFloat.hpp"
35 #include "tcuImageCompare.hpp"
36 #include "tcuRenderTarget.hpp"
37 #include "deRandom.hpp"
38 #include "deStringUtil.hpp"
39 #include "deString.h"
40
41 #include "glw.h"
42
43 using tcu::TestLog;
44 using tcu::Vec3;
45 using tcu::Vec4;
46 using std::string;
47 using std::vector;
48 using std::map;
49 using deqp::gls::TextureTestUtil::SurfaceAccess;
50
51 namespace deqp
52 {
53 namespace gles2
54 {
55 namespace Accuracy
56 {
57
projectedTriInterpolate(const tcu::Vec3 & s,const tcu::Vec3 & w,float nx,float ny)58 static inline float projectedTriInterpolate (const tcu::Vec3& s, const tcu::Vec3& w, float nx, float ny)
59 {
60 return (s[0]*(1.0f-nx-ny)/w[0] + s[1]*ny/w[1] + s[2]*nx/w[2]) / ((1.0f-nx-ny)/w[0] + ny/w[1] + nx/w[2]);
61 }
62
renderReference(const SurfaceAccess & dst,const float coords[4* 3],const Vec4 & wCoord,const Vec3 & scale,const Vec3 & bias)63 static void renderReference (const SurfaceAccess& dst, const float coords[4*3], const Vec4& wCoord, const Vec3& scale, const Vec3& bias)
64 {
65 float dstW = (float)dst.getWidth();
66 float dstH = (float)dst.getHeight();
67
68 Vec3 triR[2] = { Vec3(coords[0*3+0], coords[1*3+0], coords[2*3+0]), Vec3(coords[3*3+0], coords[2*3+0], coords[1*3+0]) };
69 Vec3 triG[2] = { Vec3(coords[0*3+1], coords[1*3+1], coords[2*3+1]), Vec3(coords[3*3+1], coords[2*3+1], coords[1*3+1]) };
70 Vec3 triB[2] = { Vec3(coords[0*3+2], coords[1*3+2], coords[2*3+2]), Vec3(coords[3*3+2], coords[2*3+2], coords[1*3+2]) };
71 tcu::Vec3 triW[2] = { wCoord.swizzle(0, 1, 2), wCoord.swizzle(3, 2, 1) };
72
73 for (int py = 0; py < dst.getHeight(); py++)
74 {
75 for (int px = 0; px < dst.getWidth(); px++)
76 {
77 float wx = (float)px + 0.5f;
78 float wy = (float)py + 0.5f;
79 float nx = wx / dstW;
80 float ny = wy / dstH;
81
82 int triNdx = nx + ny >= 1.0f ? 1 : 0;
83 float triNx = triNdx ? 1.0f - nx : nx;
84 float triNy = triNdx ? 1.0f - ny : ny;
85
86 float r = projectedTriInterpolate(triR[triNdx], triW[triNdx], triNx, triNy) * scale[0] + bias[0];
87 float g = projectedTriInterpolate(triG[triNdx], triW[triNdx], triNx, triNy) * scale[1] + bias[1];
88 float b = projectedTriInterpolate(triB[triNdx], triW[triNdx], triNx, triNy) * scale[2] + bias[2];
89
90 Vec4 color = Vec4(r, g, b, 1.0f);
91
92 dst.setPixel(color, px, py);
93 }
94 }
95 }
96
97 class InterpolationCase : public TestCase
98 {
99 public:
100 InterpolationCase (Context& context, const char* name, const char* desc, glu::Precision precision, const tcu::Vec3& minVal, const tcu::Vec3& maxVal, bool projective);
101 ~InterpolationCase (void);
102
103 IterateResult iterate (void);
104
105 private:
106 glu::Precision m_precision;
107 tcu::Vec3 m_min;
108 tcu::Vec3 m_max;
109 bool m_projective;
110 };
111
InterpolationCase(Context & context,const char * name,const char * desc,glu::Precision precision,const tcu::Vec3 & minVal,const tcu::Vec3 & maxVal,bool projective)112 InterpolationCase::InterpolationCase (Context& context, const char* name, const char* desc, glu::Precision precision, const tcu::Vec3& minVal, const tcu::Vec3& maxVal, bool projective)
113 : TestCase (context, tcu::NODETYPE_ACCURACY, name, desc)
114 , m_precision (precision)
115 , m_min (minVal)
116 , m_max (maxVal)
117 , m_projective (projective)
118 {
119 }
120
~InterpolationCase(void)121 InterpolationCase::~InterpolationCase (void)
122 {
123 }
124
isValidFloat(glu::Precision precision,float val)125 static bool isValidFloat (glu::Precision precision, float val)
126 {
127 if (precision == glu::PRECISION_MEDIUMP)
128 {
129 tcu::Float16 fp16(val);
130 return !fp16.isDenorm() && !fp16.isInf() && !fp16.isNaN();
131 }
132 else
133 {
134 tcu::Float32 fp32(val);
135 return !fp32.isDenorm() && !fp32.isInf() && !fp32.isNaN();
136 }
137 }
138
139 template <int Size>
isValidFloatVec(glu::Precision precision,const tcu::Vector<float,Size> & vec)140 static bool isValidFloatVec (glu::Precision precision, const tcu::Vector<float, Size>& vec)
141 {
142 for (int ndx = 0; ndx < Size; ndx++)
143 {
144 if (!isValidFloat(precision, vec[ndx]))
145 return false;
146 }
147 return true;
148 }
149
iterate(void)150 InterpolationCase::IterateResult InterpolationCase::iterate (void)
151 {
152 TestLog& log = m_testCtx.getLog();
153 de::Random rnd (deStringHash(getName()));
154 int viewportWidth = 128;
155 int viewportHeight = 128;
156
157 if (m_context.getRenderTarget().getWidth() < viewportWidth ||
158 m_context.getRenderTarget().getHeight() < viewportHeight)
159 throw tcu::NotSupportedError("Too small viewport", "", __FILE__, __LINE__);
160
161 int viewportX = rnd.getInt(0, m_context.getRenderTarget().getWidth() - viewportWidth);
162 int viewportY = rnd.getInt(0, m_context.getRenderTarget().getHeight() - viewportHeight);
163
164 static const char* s_vertShaderTemplate =
165 "attribute highp vec4 a_position;\n"
166 "attribute ${PRECISION} vec3 a_coords;\n"
167 "varying ${PRECISION} vec3 v_coords;\n"
168 "\n"
169 "void main (void)\n"
170 "{\n"
171 " gl_Position = a_position;\n"
172 " v_coords = a_coords;\n"
173 "}\n";
174 static const char* s_fragShaderTemplate =
175 "varying ${PRECISION} vec3 v_coords;\n"
176 "uniform ${PRECISION} vec3 u_scale;\n"
177 "uniform ${PRECISION} vec3 u_bias;\n"
178 "\n"
179 "void main (void)\n"
180 "{\n"
181 " gl_FragColor = vec4(v_coords * u_scale + u_bias, 1.0);\n"
182 "}\n";
183
184 map<string, string> templateParams;
185 templateParams["PRECISION"] = glu::getPrecisionName(m_precision);
186
187 glu::ShaderProgram program(m_context.getRenderContext(),
188 glu::makeVtxFragSources(tcu::StringTemplate(s_vertShaderTemplate).specialize(templateParams),
189 tcu::StringTemplate(s_fragShaderTemplate).specialize(templateParams)));
190 log << program;
191 if (!program.isOk())
192 {
193 if (m_precision == glu::PRECISION_HIGHP && !m_context.getContextInfo().isFragmentHighPrecisionSupported())
194 m_testCtx.setTestResult(QP_TEST_RESULT_NOT_SUPPORTED, "Fragment highp not supported");
195 else
196 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Compile failed");
197 return STOP;
198 }
199
200 // Position coordinates.
201 Vec4 wCoord = m_projective ? Vec4(1.3f, 0.8f, 0.6f, 2.0f) : Vec4(1.0f, 1.0f, 1.0f, 1.0f);
202 float positions[] =
203 {
204 -1.0f*wCoord.x(), -1.0f*wCoord.x(), 0.0f, wCoord.x(),
205 -1.0f*wCoord.y(), +1.0f*wCoord.y(), 0.0f, wCoord.y(),
206 +1.0f*wCoord.z(), -1.0f*wCoord.z(), 0.0f, wCoord.z(),
207 +1.0f*wCoord.w(), +1.0f*wCoord.w(), 0.0f, wCoord.w()
208 };
209
210 // Coordinates for interpolation.
211 tcu::Vec3 scale = 1.0f / (m_max - m_min);
212 tcu::Vec3 bias = -1.0f*m_min*scale;
213 float coords[] =
214 {
215 (0.0f - bias[0])/scale[0], (0.5f - bias[1])/scale[1], (1.0f - bias[2])/scale[2],
216 (0.5f - bias[0])/scale[0], (1.0f - bias[1])/scale[1], (0.5f - bias[2])/scale[2],
217 (0.5f - bias[0])/scale[0], (0.0f - bias[1])/scale[1], (0.5f - bias[2])/scale[2],
218 (1.0f - bias[0])/scale[0], (0.5f - bias[1])/scale[1], (0.0f - bias[2])/scale[2]
219 };
220
221 log << TestLog::Message << "a_coords = " << ((tcu::Vec3(0.0f) - bias)/scale) << " -> " << ((tcu::Vec3(1.0f) - bias)/scale) << TestLog::EndMessage;
222 log << TestLog::Message << "u_scale = " << scale << TestLog::EndMessage;
223 log << TestLog::Message << "u_bias = " << bias << TestLog::EndMessage;
224
225 // Verify that none of the inputs are denormalized / inf / nan.
226 TCU_CHECK(isValidFloatVec(m_precision, scale));
227 TCU_CHECK(isValidFloatVec(m_precision, bias));
228 for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(coords); ndx++)
229 {
230 TCU_CHECK(isValidFloat(m_precision, coords[ndx]));
231 TCU_CHECK(isValidFloat(m_precision, coords[ndx] * scale[ndx % 3] + bias[ndx % 3]));
232 }
233
234 // Indices.
235 static const deUint16 indices[] = { 0, 1, 2, 2, 1, 3 };
236
237 {
238 const int posLoc = glGetAttribLocation(program.getProgram(), "a_position");
239 const int coordLoc = glGetAttribLocation(program.getProgram(), "a_coords");
240
241 glEnableVertexAttribArray(posLoc);
242 glVertexAttribPointer(posLoc, 4, GL_FLOAT, GL_FALSE, 0, &positions[0]);
243
244 glEnableVertexAttribArray(coordLoc);
245 glVertexAttribPointer(coordLoc, 3, GL_FLOAT, GL_FALSE, 0, &coords[0]);
246 }
247
248 glUseProgram(program.getProgram());
249 glUniform3f(glGetUniformLocation(program.getProgram(), "u_scale"), scale.x(), scale.y(), scale.z());
250 glUniform3f(glGetUniformLocation(program.getProgram(), "u_bias"), bias.x(), bias.y(), bias.z());
251
252 GLU_CHECK_MSG("After program setup");
253
254 // Frames.
255 tcu::Surface rendered (viewportWidth, viewportHeight);
256 tcu::Surface reference (viewportWidth, viewportHeight);
257
258 // Render with GL.
259 glViewport(viewportX, viewportY, viewportWidth, viewportHeight);
260 glDrawElements(GL_TRIANGLES, DE_LENGTH_OF_ARRAY(indices), GL_UNSIGNED_SHORT, &indices[0]);
261
262 // Render reference \note While GPU is hopefully doing our draw call.
263 renderReference(SurfaceAccess(reference, m_context.getRenderTarget().getPixelFormat()), coords, wCoord, scale, bias);
264
265 glu::readPixels(m_context.getRenderContext(), viewportX, viewportY, rendered.getAccess());
266
267 // Compute difference.
268 const int bestScoreDiff = 16;
269 const int worstScoreDiff = 300;
270 int score = tcu::measurePixelDiffAccuracy(log, "Result", "Image comparison result", reference, rendered, bestScoreDiff, worstScoreDiff, tcu::COMPARE_LOG_EVERYTHING);
271
272 m_testCtx.setTestResult(QP_TEST_RESULT_PASS, de::toString(score).c_str());
273 return STOP;
274 }
275
VaryingInterpolationTests(Context & context)276 VaryingInterpolationTests::VaryingInterpolationTests (Context& context)
277 : TestCaseGroup(context, "interpolation", "Varying Interpolation Accuracy Tests")
278 {
279 }
280
~VaryingInterpolationTests(void)281 VaryingInterpolationTests::~VaryingInterpolationTests (void)
282 {
283 }
284
init(void)285 void VaryingInterpolationTests::init (void)
286 {
287 DE_STATIC_ASSERT(glu::PRECISION_LOWP+1 == glu::PRECISION_MEDIUMP);
288 DE_STATIC_ASSERT(glu::PRECISION_MEDIUMP+1 == glu::PRECISION_HIGHP);
289
290 // Exp = Emax-3, Mantissa = 0
291 float minF32 = tcu::Float32((0u<<31) | (0xfcu<<23) | 0x0u).asFloat();
292 float maxF32 = tcu::Float32((1u<<31) | (0xfcu<<23) | 0x0u).asFloat();
293 float minF16 = tcu::Float16((deUint16)((0u<<15) | (0x1cu<<10) | 0x0u)).asFloat();
294 float maxF16 = tcu::Float16((deUint16)((1u<<15) | (0x1cu<<10) | 0x0u)).asFloat();
295
296 static const struct
297 {
298 const char* name;
299 Vec3 minVal;
300 Vec3 maxVal;
301 glu::Precision minPrecision;
302 } coordRanges[] =
303 {
304 { "zero_to_one", Vec3( 0.0f, 0.0f, 0.0f), Vec3( 1.0f, 1.0f, 1.0f), glu::PRECISION_LOWP },
305 { "zero_to_minus_one", Vec3( 0.0f, 0.0f, 0.0f), Vec3( -1.0f, -1.0f, -1.0f), glu::PRECISION_LOWP },
306 { "minus_one_to_one", Vec3( -1.0f, -1.0f, -1.0f), Vec3( 1.0f, 1.0f, 1.0f), glu::PRECISION_LOWP },
307 { "minus_ten_to_ten", Vec3(-10.0f, -10.0f, -10.0f), Vec3( 10.0f, 10.0f, 10.0f), glu::PRECISION_MEDIUMP },
308 { "thousands", Vec3( -5e3f, 1e3f, 1e3f), Vec3( 3e3f, -1e3f, 7e3f), glu::PRECISION_MEDIUMP },
309 { "full_mediump", Vec3(minF16, minF16, minF16), Vec3(maxF16, maxF16, maxF16), glu::PRECISION_MEDIUMP },
310 { "full_highp", Vec3(minF32, minF32, minF32), Vec3(maxF32, maxF32, maxF32), glu::PRECISION_HIGHP },
311 };
312
313 for (int precision = glu::PRECISION_LOWP; precision <= glu::PRECISION_HIGHP; precision++)
314 {
315 for (int coordNdx = 0; coordNdx < DE_LENGTH_OF_ARRAY(coordRanges); coordNdx++)
316 {
317 if (precision < (int)coordRanges[coordNdx].minPrecision)
318 continue;
319
320 string baseName = string(glu::getPrecisionName((glu::Precision)precision)) + "_" + coordRanges[coordNdx].name;
321
322 addChild(new InterpolationCase(m_context, baseName.c_str(), "", (glu::Precision)precision, coordRanges[coordNdx].minVal, coordRanges[coordNdx].maxVal, false));
323 addChild(new InterpolationCase(m_context, (baseName + "_proj").c_str(), "", (glu::Precision)precision, coordRanges[coordNdx].minVal, coordRanges[coordNdx].maxVal, true));
324 }
325 }
326 }
327
328 } // Accuracy
329 } // gles2
330 } // deqp
331