1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program EGL Module
3 * ---------------------------------------
5 * Copyright 2014 The Android Open Source Project
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * \brief Rendering tests for different config and api combinations.
22 * \todo [2013-03-19 pyry] GLES1 and VG support.
23 *//*--------------------------------------------------------------------*/
25 #include "teglRenderTests.hpp"
26 #include "teglRenderCase.hpp"
28 #include "tcuRenderTarget.hpp"
29 #include "tcuTestLog.hpp"
30 #include "tcuImageCompare.hpp"
31 #include "tcuTextureUtil.hpp"
32 #include "tcuSurface.hpp"
34 #include "egluDefs.hpp"
35 #include "egluUtil.hpp"
37 #include "eglwLibrary.hpp"
38 #include "eglwEnums.hpp"
40 #include "gluShaderProgram.hpp"
42 #include "glwFunctions.hpp"
43 #include "glwEnums.hpp"
45 #include "deRandom.hpp"
46 #include "deSharedPtr.hpp"
47 #include "deSemaphore.hpp"
48 #include "deThread.hpp"
51 #include "rrRenderer.hpp"
52 #include "rrFragmentOperations.hpp"
75 static const tcu::Vec4 CLEAR_COLOR = tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f);
76 static const float CLEAR_DEPTH = 1.0f;
77 static const int CLEAR_STENCIL = 0;
84 PRIMITIVETYPE_TRIANGLE = 0, //!< Triangles, requires 3 coordinates per primitive
85 // PRIMITIVETYPE_POINT, //!< Points, requires 1 coordinate per primitive (w is used as size)
86 // PRIMITIVETYPE_LINE, //!< Lines, requires 2 coordinates per primitive
93 BLENDMODE_NONE = 0, //!< No blending
94 BLENDMODE_ADDITIVE, //!< Blending with ONE, ONE
95 BLENDMODE_SRC_OVER, //!< Blending with SRC_ALPHA, ONE_MINUS_SRC_ALPHA
102 DEPTHMODE_NONE = 0, //!< No depth test or depth writes
103 DEPTHMODE_LESS, //!< Depth test with less & depth write
110 STENCILMODE_NONE = 0, //!< No stencil test or write
111 STENCILMODE_LEQUAL_INC, //!< Stencil test with LEQUAL, increment on pass
116 struct DrawPrimitiveOp
120 vector<Vec4> positions;
128 static bool isANarrowScreenSpaceTriangle (const tcu::Vec4& p0, const tcu::Vec4& p1, const tcu::Vec4& p2)
131 const tcu::Vec2 csp0 = p0.swizzle(0, 1) / p0.w();
132 const tcu::Vec2 csp1 = p1.swizzle(0, 1) / p1.w();
133 const tcu::Vec2 csp2 = p2.swizzle(0, 1) / p2.w();
135 const tcu::Vec2 e01 = (csp1 - csp0);
136 const tcu::Vec2 e02 = (csp2 - csp0);
138 const float minimumVisibleArea = 0.4f; // must cover at least 10% of the surface
139 const float visibleArea = de::abs(e01.x() * e02.y() - e02.x() * e01.y()) * 0.5f;
141 return visibleArea < minimumVisibleArea;
144 void randomizeDrawOp (de::Random& rnd, DrawPrimitiveOp& drawOp)
146 const int minStencilRef = 0;
147 const int maxStencilRef = 8;
148 const int minPrimitives = 2;
149 const int maxPrimitives = 4;
151 const float maxTriOffset = 1.0f;
152 const float minDepth = -1.0f; // \todo [pyry] Reference doesn't support Z clipping yet
153 const float maxDepth = 1.0f;
155 const float minRGB = 0.2f;
156 const float maxRGB = 0.9f;
157 const float minAlpha = 0.3f;
158 const float maxAlpha = 1.0f;
160 drawOp.type = (PrimitiveType)rnd.getInt(0, PRIMITIVETYPE_LAST-1);
161 drawOp.count = rnd.getInt(minPrimitives, maxPrimitives);
162 drawOp.blend = (BlendMode)rnd.getInt(0, BLENDMODE_LAST-1);
163 drawOp.depth = (DepthMode)rnd.getInt(0, DEPTHMODE_LAST-1);
164 drawOp.stencil = (StencilMode)rnd.getInt(0, STENCILMODE_LAST-1);
165 drawOp.stencilRef = rnd.getInt(minStencilRef, maxStencilRef);
167 if (drawOp.type == PRIMITIVETYPE_TRIANGLE)
169 drawOp.positions.resize(drawOp.count*3);
170 drawOp.colors.resize(drawOp.count*3);
172 for (int triNdx = 0; triNdx < drawOp.count; triNdx++)
174 const float cx = rnd.getFloat(-1.0f, 1.0f);
175 const float cy = rnd.getFloat(-1.0f, 1.0f);
177 for (int coordNdx = 0; coordNdx < 3; coordNdx++)
179 tcu::Vec4& position = drawOp.positions[triNdx*3 + coordNdx];
180 tcu::Vec4& color = drawOp.colors[triNdx*3 + coordNdx];
182 position.x() = cx + rnd.getFloat(-maxTriOffset, maxTriOffset);
183 position.y() = cy + rnd.getFloat(-maxTriOffset, maxTriOffset);
184 position.z() = rnd.getFloat(minDepth, maxDepth);
187 color.x() = rnd.getFloat(minRGB, maxRGB);
188 color.y() = rnd.getFloat(minRGB, maxRGB);
189 color.z() = rnd.getFloat(minRGB, maxRGB);
190 color.w() = rnd.getFloat(minAlpha, maxAlpha);
193 // avoid generating narrow triangles
195 const int maxAttempts = 40;
197 tcu::Vec4& p0 = drawOp.positions[triNdx*3 + 0];
198 tcu::Vec4& p1 = drawOp.positions[triNdx*3 + 1];
199 tcu::Vec4& p2 = drawOp.positions[triNdx*3 + 2];
201 while (isANarrowScreenSpaceTriangle(p0, p1, p2))
203 p1.x() = cx + rnd.getFloat(-maxTriOffset, maxTriOffset);
204 p1.y() = cy + rnd.getFloat(-maxTriOffset, maxTriOffset);
205 p1.z() = rnd.getFloat(minDepth, maxDepth);
208 p2.x() = cx + rnd.getFloat(-maxTriOffset, maxTriOffset);
209 p2.y() = cy + rnd.getFloat(-maxTriOffset, maxTriOffset);
210 p2.z() = rnd.getFloat(minDepth, maxDepth);
213 if (++numAttempts > maxAttempts)
226 // Reference rendering code
228 class ReferenceShader : public rr::VertexShader, public rr::FragmentShader
237 : rr::VertexShader (2, 1) // color and pos in => color out
238 , rr::FragmentShader(1, 1) // color in => color out
240 this->rr::VertexShader::m_inputs[0].type = rr::GENERICVECTYPE_FLOAT;
241 this->rr::VertexShader::m_inputs[1].type = rr::GENERICVECTYPE_FLOAT;
243 this->rr::VertexShader::m_outputs[0].type = rr::GENERICVECTYPE_FLOAT;
244 this->rr::VertexShader::m_outputs[0].flatshade = false;
246 this->rr::FragmentShader::m_inputs[0].type = rr::GENERICVECTYPE_FLOAT;
247 this->rr::FragmentShader::m_inputs[0].flatshade = false;
249 this->rr::FragmentShader::m_outputs[0].type = rr::GENERICVECTYPE_FLOAT;
252 void shadeVertices (const rr::VertexAttrib* inputs, rr::VertexPacket* const* packets, const int numPackets) const
254 for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx)
256 const int positionAttrLoc = 0;
257 const int colorAttrLoc = 1;
259 rr::VertexPacket& packet = *packets[packetNdx];
261 // Transform to position
262 packet.position = rr::readVertexAttribFloat(inputs[positionAttrLoc], packet.instanceNdx, packet.vertexNdx);
265 packet.outputs[VaryingLoc_Color] = rr::readVertexAttribFloat(inputs[colorAttrLoc], packet.instanceNdx, packet.vertexNdx);
269 void shadeFragments (rr::FragmentPacket* packets, const int numPackets, const rr::FragmentShadingContext& context) const
271 for (int packetNdx = 0; packetNdx < numPackets; ++packetNdx)
273 rr::FragmentPacket& packet = packets[packetNdx];
275 for (int fragNdx = 0; fragNdx < 4; ++fragNdx)
276 rr::writeFragmentOutput(context, packetNdx, fragNdx, 0, rr::readVarying<float>(packet, context, VaryingLoc_Color, fragNdx));
281 void toReferenceRenderState (rr::RenderState& state, const DrawPrimitiveOp& drawOp)
283 state.cullMode = rr::CULLMODE_NONE;
285 if (drawOp.blend != BLENDMODE_NONE)
287 state.fragOps.blendMode = rr::BLENDMODE_STANDARD;
289 switch (drawOp.blend)
291 case BLENDMODE_ADDITIVE:
292 state.fragOps.blendRGBState.srcFunc = rr::BLENDFUNC_ONE;
293 state.fragOps.blendRGBState.dstFunc = rr::BLENDFUNC_ONE;
294 state.fragOps.blendRGBState.equation = rr::BLENDEQUATION_ADD;
295 state.fragOps.blendAState = state.fragOps.blendRGBState;
298 case BLENDMODE_SRC_OVER:
299 state.fragOps.blendRGBState.srcFunc = rr::BLENDFUNC_SRC_ALPHA;
300 state.fragOps.blendRGBState.dstFunc = rr::BLENDFUNC_ONE_MINUS_SRC_ALPHA;
301 state.fragOps.blendRGBState.equation = rr::BLENDEQUATION_ADD;
302 state.fragOps.blendAState = state.fragOps.blendRGBState;
310 if (drawOp.depth != DEPTHMODE_NONE)
312 state.fragOps.depthTestEnabled = true;
314 DE_ASSERT(drawOp.depth == DEPTHMODE_LESS);
315 state.fragOps.depthFunc = rr::TESTFUNC_LESS;
318 if (drawOp.stencil != STENCILMODE_NONE)
320 state.fragOps.stencilTestEnabled = true;
322 DE_ASSERT(drawOp.stencil == STENCILMODE_LEQUAL_INC);
323 state.fragOps.stencilStates[0].func = rr::TESTFUNC_LEQUAL;
324 state.fragOps.stencilStates[0].sFail = rr::STENCILOP_KEEP;
325 state.fragOps.stencilStates[0].dpFail = rr::STENCILOP_INCR;
326 state.fragOps.stencilStates[0].dpPass = rr::STENCILOP_INCR;
327 state.fragOps.stencilStates[0].ref = drawOp.stencilRef;
328 state.fragOps.stencilStates[1] = state.fragOps.stencilStates[0];
332 tcu::TextureFormat getColorFormat (const tcu::PixelFormat& colorBits)
334 using tcu::TextureFormat;
336 DE_ASSERT(de::inBounds(colorBits.redBits, 0, 0xff) &&
337 de::inBounds(colorBits.greenBits, 0, 0xff) &&
338 de::inBounds(colorBits.blueBits, 0, 0xff) &&
339 de::inBounds(colorBits.alphaBits, 0, 0xff));
341 #define PACK_FMT(R, G, B, A) (((R) << 24) | ((G) << 16) | ((B) << 8) | (A))
343 // \note [pyry] This may not hold true on some implementations - best effort guess only.
344 switch (PACK_FMT(colorBits.redBits, colorBits.greenBits, colorBits.blueBits, colorBits.alphaBits))
346 case PACK_FMT(8,8,8,8): return TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8);
347 case PACK_FMT(8,8,8,0): return TextureFormat(TextureFormat::RGB, TextureFormat::UNORM_INT8);
348 case PACK_FMT(4,4,4,4): return TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_SHORT_4444);
349 case PACK_FMT(5,5,5,1): return TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_SHORT_5551);
350 case PACK_FMT(5,6,5,0): return TextureFormat(TextureFormat::RGB, TextureFormat::UNORM_SHORT_565);
352 // \note Defaults to RGBA8
353 default: return TextureFormat(TextureFormat::RGBA, TextureFormat::UNORM_INT8);
359 tcu::TextureFormat getDepthFormat (const int depthBits)
363 case 0: return tcu::TextureFormat();
364 case 8: return tcu::TextureFormat(tcu::TextureFormat::D, tcu::TextureFormat::UNORM_INT8);
365 case 16: return tcu::TextureFormat(tcu::TextureFormat::D, tcu::TextureFormat::UNORM_INT16);
366 case 24: return tcu::TextureFormat(tcu::TextureFormat::D, tcu::TextureFormat::UNORM_INT24);
368 default: return tcu::TextureFormat(tcu::TextureFormat::D, tcu::TextureFormat::FLOAT);
372 tcu::TextureFormat getStencilFormat (int stencilBits)
376 case 0: return tcu::TextureFormat();
378 default: return tcu::TextureFormat(tcu::TextureFormat::S, tcu::TextureFormat::UNSIGNED_INT8);
382 void renderReference (const tcu::PixelBufferAccess& dst, const vector<DrawPrimitiveOp>& drawOps, const tcu::PixelFormat& colorBits, const int depthBits, const int stencilBits, const int numSamples)
384 const int width = dst.getWidth();
385 const int height = dst.getHeight();
387 tcu::TextureLevel colorBuffer;
388 tcu::TextureLevel depthBuffer;
389 tcu::TextureLevel stencilBuffer;
391 rr::Renderer referenceRenderer;
392 rr::VertexAttrib attributes[2];
393 const ReferenceShader shader;
395 attributes[0].type = rr::VERTEXATTRIBTYPE_FLOAT;
396 attributes[0].size = 4;
397 attributes[0].stride = 0;
398 attributes[0].instanceDivisor = 0;
400 attributes[1].type = rr::VERTEXATTRIBTYPE_FLOAT;
401 attributes[1].size = 4;
402 attributes[1].stride = 0;
403 attributes[1].instanceDivisor = 0;
405 // Initialize buffers.
406 colorBuffer.setStorage(getColorFormat(colorBits), numSamples, width, height);
407 rr::clearMultisampleColorBuffer(colorBuffer, CLEAR_COLOR, rr::WindowRectangle(0, 0, width, height));
411 depthBuffer.setStorage(getDepthFormat(depthBits), numSamples, width, height);
412 rr::clearMultisampleDepthBuffer(depthBuffer, CLEAR_DEPTH, rr::WindowRectangle(0, 0, width, height));
417 stencilBuffer.setStorage(getStencilFormat(stencilBits), numSamples, width, height);
418 rr::clearMultisampleStencilBuffer(stencilBuffer, CLEAR_STENCIL, rr::WindowRectangle(0, 0, width, height));
421 const rr::RenderTarget renderTarget(rr::MultisamplePixelBufferAccess::fromMultisampleAccess(colorBuffer.getAccess()),
422 rr::MultisamplePixelBufferAccess::fromMultisampleAccess(depthBuffer.getAccess()),
423 rr::MultisamplePixelBufferAccess::fromMultisampleAccess(stencilBuffer.getAccess()));
425 for (vector<DrawPrimitiveOp>::const_iterator drawOp = drawOps.begin(); drawOp != drawOps.end(); drawOp++)
428 rr::RenderState renderState((rr::ViewportState)(rr::MultisamplePixelBufferAccess::fromMultisampleAccess(colorBuffer.getAccess())));
429 toReferenceRenderState(renderState, *drawOp);
431 DE_ASSERT(drawOp->type == PRIMITIVETYPE_TRIANGLE);
433 attributes[0].pointer = &drawOp->positions[0];
434 attributes[1].pointer = &drawOp->colors[0];
436 referenceRenderer.draw(
440 rr::Program(static_cast<const rr::VertexShader*>(&shader), static_cast<const rr::FragmentShader*>(&shader)),
443 rr::PrimitiveList(rr::PRIMITIVETYPE_TRIANGLES, drawOp->count * 3, 0)));
446 rr::resolveMultisampleColorBuffer(dst, rr::MultisamplePixelBufferAccess::fromMultisampleAccess(colorBuffer.getAccess()));
449 // API rendering code
455 virtual ~Program (void) {}
457 virtual void setup (void) const = DE_NULL;
460 typedef de::SharedPtr<Program> ProgramSp;
462 static glu::ProgramSources getProgramSourcesES2 (void)
464 static const char* s_vertexSrc =
465 "attribute highp vec4 a_position;\n"
466 "attribute mediump vec4 a_color;\n"
467 "varying mediump vec4 v_color;\n"
470 " gl_Position = a_position;\n"
471 " v_color = a_color;\n"
474 static const char* s_fragmentSrc =
475 "varying mediump vec4 v_color;\n"
478 " gl_FragColor = v_color;\n"
481 return glu::ProgramSources() << glu::VertexSource(s_vertexSrc) << glu::FragmentSource(s_fragmentSrc);
484 class GLES2Program : public Program
487 GLES2Program (const glw::Functions& gl)
489 , m_program (gl, getProgramSourcesES2())
494 m_positionLoc = m_gl.getAttribLocation(m_program.getProgram(), "a_position");
495 m_colorLoc = m_gl.getAttribLocation(m_program.getProgram(), "a_color");
502 void setup (void) const
504 m_gl.useProgram(m_program.getProgram());
505 m_gl.enableVertexAttribArray(m_positionLoc);
506 m_gl.enableVertexAttribArray(m_colorLoc);
507 GLU_CHECK_GLW_MSG(m_gl, "Program setup failed");
510 int getPositionLoc (void) const { return m_positionLoc; }
511 int getColorLoc (void) const { return m_colorLoc; }
514 const glw::Functions& m_gl;
515 glu::ShaderProgram m_program;
520 void clearGLES2 (const glw::Functions& gl, const tcu::Vec4& color, const float depth, const int stencil)
522 gl.clearColor(color.x(), color.y(), color.z(), color.w());
523 gl.clearDepthf(depth);
524 gl.clearStencil(stencil);
525 gl.clear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
528 void drawGLES2 (const glw::Functions& gl, const Program& program, const DrawPrimitiveOp& drawOp)
530 const GLES2Program& gles2Program = dynamic_cast<const GLES2Program&>(program);
532 switch (drawOp.blend)
535 gl.disable(GL_BLEND);
538 case BLENDMODE_ADDITIVE:
540 gl.blendFunc(GL_ONE, GL_ONE);
543 case BLENDMODE_SRC_OVER:
545 gl.blendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
552 switch (drawOp.depth)
555 gl.disable(GL_DEPTH_TEST);
559 gl.enable(GL_DEPTH_TEST);
566 switch (drawOp.stencil)
568 case STENCILMODE_NONE:
569 gl.disable(GL_STENCIL_TEST);
572 case STENCILMODE_LEQUAL_INC:
573 gl.enable(GL_STENCIL_TEST);
574 gl.stencilFunc(GL_LEQUAL, drawOp.stencilRef, ~0u);
575 gl.stencilOp(GL_KEEP, GL_INCR, GL_INCR);
582 gl.disable(GL_DITHER);
584 gl.vertexAttribPointer(gles2Program.getPositionLoc(), 4, GL_FLOAT, GL_FALSE, 0, &drawOp.positions[0]);
585 gl.vertexAttribPointer(gles2Program.getColorLoc(), 4, GL_FLOAT, GL_FALSE, 0, &drawOp.colors[0]);
587 DE_ASSERT(drawOp.type == PRIMITIVETYPE_TRIANGLE);
588 gl.drawArrays(GL_TRIANGLES, 0, drawOp.count*3);
591 static void readPixelsGLES2 (const glw::Functions& gl, tcu::Surface& dst)
593 gl.readPixels(0, 0, dst.getWidth(), dst.getHeight(), GL_RGBA, GL_UNSIGNED_BYTE, dst.getAccess().getDataPtr());
596 Program* createProgram (const glw::Functions& gl, EGLint api)
600 case EGL_OPENGL_ES2_BIT: return new GLES2Program(gl);
601 case EGL_OPENGL_ES3_BIT_KHR: return new GLES2Program(gl);
603 throw tcu::NotSupportedError("Unsupported API");
607 void draw (const glw::Functions& gl, EGLint api, const Program& program, const DrawPrimitiveOp& drawOp)
611 case EGL_OPENGL_ES2_BIT: drawGLES2(gl, program, drawOp); break;
612 case EGL_OPENGL_ES3_BIT_KHR: drawGLES2(gl, program, drawOp); break;
614 throw tcu::NotSupportedError("Unsupported API");
618 void clear (const glw::Functions& gl, EGLint api, const tcu::Vec4& color, const float depth, const int stencil)
622 case EGL_OPENGL_ES2_BIT: clearGLES2(gl, color, depth, stencil); break;
623 case EGL_OPENGL_ES3_BIT_KHR: clearGLES2(gl, color, depth, stencil); break;
625 throw tcu::NotSupportedError("Unsupported API");
629 static void readPixels (const glw::Functions& gl, EGLint api, tcu::Surface& dst)
633 case EGL_OPENGL_ES2_BIT: readPixelsGLES2(gl, dst); break;
634 case EGL_OPENGL_ES3_BIT_KHR: readPixelsGLES2(gl, dst); break;
636 throw tcu::NotSupportedError("Unsupported API");
640 static void finish (const glw::Functions& gl, EGLint api)
644 case EGL_OPENGL_ES2_BIT:
645 case EGL_OPENGL_ES3_BIT_KHR:
650 throw tcu::NotSupportedError("Unsupported API");
654 tcu::PixelFormat getPixelFormat (const Library& egl, EGLDisplay display, EGLConfig config)
656 tcu::PixelFormat fmt;
657 fmt.redBits = eglu::getConfigAttribInt(egl, display, config, EGL_RED_SIZE);
658 fmt.greenBits = eglu::getConfigAttribInt(egl, display, config, EGL_GREEN_SIZE);
659 fmt.blueBits = eglu::getConfigAttribInt(egl, display, config, EGL_BLUE_SIZE);
660 fmt.alphaBits = eglu::getConfigAttribInt(egl, display, config, EGL_ALPHA_SIZE);
666 // SingleThreadRenderCase
668 class SingleThreadRenderCase : public MultiContextRenderCase
671 SingleThreadRenderCase (EglTestContext& eglTestCtx, const char* name, const char* description, EGLint api, EGLint surfaceType, const eglu::FilterList& filters, int numContextsPerApi);
676 virtual void executeForContexts (EGLDisplay display, EGLSurface surface, const Config& config, const std::vector<std::pair<EGLint, EGLContext> >& contexts);
681 // SingleThreadColorClearCase
683 SingleThreadRenderCase::SingleThreadRenderCase (EglTestContext& eglTestCtx, const char* name, const char* description, EGLint api, EGLint surfaceType, const eglu::FilterList& filters, int numContextsPerApi)
684 : MultiContextRenderCase(eglTestCtx, name, description, api, surfaceType, filters, numContextsPerApi)
688 void SingleThreadRenderCase::init (void)
690 MultiContextRenderCase::init();
691 m_eglTestCtx.initGLFunctions(&m_gl, glu::ApiType::es(2,0));
694 void SingleThreadRenderCase::executeForContexts (EGLDisplay display, EGLSurface surface, const Config& config, const std::vector<std::pair<EGLint, EGLContext> >& contexts)
696 const Library& egl = m_eglTestCtx.getLibrary();
697 const int width = eglu::querySurfaceInt(egl, display, surface, EGL_WIDTH);
698 const int height = eglu::querySurfaceInt(egl, display, surface, EGL_HEIGHT);
699 const int numContexts = (int)contexts.size();
700 const int drawsPerCtx = 2;
701 const int numIters = 2;
702 const float threshold = 0.02f;
704 const tcu::PixelFormat pixelFmt = getPixelFormat(egl, display, config.config);
705 const int depthBits = eglu::getConfigAttribInt(egl, display, config.config, EGL_DEPTH_SIZE);
706 const int stencilBits = eglu::getConfigAttribInt(egl, display, config.config, EGL_STENCIL_SIZE);
707 const int numSamples = eglu::getConfigAttribInt(egl, display, config.config, EGL_SAMPLES);
709 TestLog& log = m_testCtx.getLog();
711 tcu::Surface refFrame (width, height);
712 tcu::Surface frame (width, height);
714 de::Random rnd (deStringHash(getName()) ^ deInt32Hash(numContexts));
715 vector<ProgramSp> programs (contexts.size());
716 vector<DrawPrimitiveOp> drawOps;
718 // Log basic information about config.
719 log << TestLog::Message << "EGL_RED_SIZE = " << pixelFmt.redBits << TestLog::EndMessage;
720 log << TestLog::Message << "EGL_GREEN_SIZE = " << pixelFmt.greenBits << TestLog::EndMessage;
721 log << TestLog::Message << "EGL_BLUE_SIZE = " << pixelFmt.blueBits << TestLog::EndMessage;
722 log << TestLog::Message << "EGL_ALPHA_SIZE = " << pixelFmt.alphaBits << TestLog::EndMessage;
723 log << TestLog::Message << "EGL_DEPTH_SIZE = " << depthBits << TestLog::EndMessage;
724 log << TestLog::Message << "EGL_STENCIL_SIZE = " << stencilBits << TestLog::EndMessage;
725 log << TestLog::Message << "EGL_SAMPLES = " << numSamples << TestLog::EndMessage;
727 // Generate draw ops.
728 drawOps.resize(numContexts*drawsPerCtx*numIters);
729 for (vector<DrawPrimitiveOp>::iterator drawOp = drawOps.begin(); drawOp != drawOps.end(); ++drawOp)
730 randomizeDrawOp(rnd, *drawOp);
732 // Create and setup programs per context
733 for (int ctxNdx = 0; ctxNdx < numContexts; ctxNdx++)
735 EGLint api = contexts[ctxNdx].first;
736 EGLContext context = contexts[ctxNdx].second;
738 EGLU_CHECK_CALL(egl, makeCurrent(display, surface, surface, context));
740 programs[ctxNdx] = ProgramSp(createProgram(m_gl, api));
741 programs[ctxNdx]->setup();
744 // Clear to black using first context.
746 EGLint api = contexts[0].first;
747 EGLContext context = contexts[0].second;
749 EGLU_CHECK_CALL(egl, makeCurrent(display, surface, surface, context));
751 clear(m_gl, api, CLEAR_COLOR, CLEAR_DEPTH, CLEAR_STENCIL);
756 for (int iterNdx = 0; iterNdx < numIters; iterNdx++)
758 for (int ctxNdx = 0; ctxNdx < numContexts; ctxNdx++)
760 EGLint api = contexts[ctxNdx].first;
761 EGLContext context = contexts[ctxNdx].second;
763 EGLU_CHECK_CALL(egl, makeCurrent(display, surface, surface, context));
765 for (int drawNdx = 0; drawNdx < drawsPerCtx; drawNdx++)
767 const DrawPrimitiveOp& drawOp = drawOps[iterNdx*numContexts*drawsPerCtx + ctxNdx*drawsPerCtx + drawNdx];
768 draw(m_gl, api, *programs[ctxNdx], drawOp);
775 // Read pixels using first context. \todo [pyry] Randomize?
777 EGLint api = contexts[0].first;
778 EGLContext context = contexts[0].second;
780 EGLU_CHECK_CALL(egl, makeCurrent(display, surface, surface, context));
782 readPixels(m_gl, api, frame);
785 EGLU_CHECK_CALL(egl, makeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
788 // \note Reference image is always generated using single-sampling.
789 renderReference(refFrame.getAccess(), drawOps, pixelFmt, depthBits, stencilBits, 1);
793 bool imagesOk = tcu::fuzzyCompare(log, "ComparisonResult", "Image comparison result", refFrame, frame, threshold, tcu::COMPARE_LOG_RESULT);
796 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Image comparison failed");
800 // MultiThreadRenderCase
802 class MultiThreadRenderCase : public MultiContextRenderCase
805 MultiThreadRenderCase (EglTestContext& eglTestCtx, const char* name, const char* description, EGLint api, EGLint surfaceType, const eglu::FilterList& filters, int numContextsPerApi);
810 virtual void executeForContexts (EGLDisplay display, EGLSurface surface, const Config& config, const std::vector<std::pair<EGLint, EGLContext> >& contexts);
815 class RenderTestThread;
817 typedef de::SharedPtr<RenderTestThread> RenderTestThreadSp;
818 typedef de::SharedPtr<de::Semaphore> SemaphoreSp;
828 const DrawPrimitiveOp* drawOps;
834 class RenderTestThread : public de::Thread
837 RenderTestThread (const Library& egl, EGLDisplay display, EGLSurface surface, EGLContext context, EGLint api, const glw::Functions& gl, const Program& program, const std::vector<DrawOpPacket>& packets)
839 , m_display (display)
840 , m_surface (surface)
841 , m_context (context)
844 , m_program (program)
845 , m_packets (packets)
851 for (std::vector<DrawOpPacket>::const_iterator packetIter = m_packets.begin(); packetIter != m_packets.end(); packetIter++)
853 // Wait until it is our turn.
854 packetIter->wait->decrement();
857 EGLU_CHECK_CALL(m_egl, makeCurrent(m_display, m_surface, m_surface, m_context));
859 // Execute rendering.
860 for (int ndx = 0; ndx < packetIter->numOps; ndx++)
861 draw(m_gl, m_api, m_program, packetIter->drawOps[ndx]);
866 EGLU_CHECK_CALL(m_egl, makeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
868 // Signal completion.
869 packetIter->signal->increment();
874 const Library& m_egl;
875 EGLDisplay m_display;
876 EGLSurface m_surface;
877 EGLContext m_context;
879 const glw::Functions& m_gl;
880 const Program& m_program;
881 const std::vector<DrawOpPacket>& m_packets;
884 MultiThreadRenderCase::MultiThreadRenderCase (EglTestContext& eglTestCtx, const char* name, const char* description, EGLint api, EGLint surfaceType, const eglu::FilterList& filters, int numContextsPerApi)
885 : MultiContextRenderCase(eglTestCtx, name, description, api, surfaceType, filters, numContextsPerApi)
889 void MultiThreadRenderCase::init (void)
891 MultiContextRenderCase::init();
892 m_eglTestCtx.initGLFunctions(&m_gl, glu::ApiType::es(2,0));
895 void MultiThreadRenderCase::executeForContexts (EGLDisplay display, EGLSurface surface, const Config& config, const std::vector<std::pair<EGLint, EGLContext> >& contexts)
897 const Library& egl = m_eglTestCtx.getLibrary();
898 const int width = eglu::querySurfaceInt(egl, display, surface, EGL_WIDTH);
899 const int height = eglu::querySurfaceInt(egl, display, surface, EGL_HEIGHT);
900 const int numContexts = (int)contexts.size();
901 const int opsPerPacket = 2;
902 const int packetsPerThread = 2;
903 const int numThreads = numContexts;
904 const int numPackets = numThreads * packetsPerThread;
905 const float threshold = 0.02f;
907 const tcu::PixelFormat pixelFmt = getPixelFormat(egl, display, config.config);
908 const int depthBits = eglu::getConfigAttribInt(egl, display, config.config, EGL_DEPTH_SIZE);
909 const int stencilBits = eglu::getConfigAttribInt(egl, display, config.config, EGL_STENCIL_SIZE);
910 const int numSamples = eglu::getConfigAttribInt(egl, display, config.config, EGL_SAMPLES);
912 TestLog& log = m_testCtx.getLog();
914 tcu::Surface refFrame (width, height);
915 tcu::Surface frame (width, height);
917 de::Random rnd (deStringHash(getName()) ^ deInt32Hash(numContexts));
919 // Resources that need cleanup
920 vector<ProgramSp> programs (numContexts);
921 vector<SemaphoreSp> semaphores (numPackets+1);
922 vector<DrawPrimitiveOp> drawOps (numPackets*opsPerPacket);
923 vector<vector<DrawOpPacket> > packets (numThreads);
924 vector<RenderTestThreadSp> threads (numThreads);
926 // Log basic information about config.
927 log << TestLog::Message << "EGL_RED_SIZE = " << pixelFmt.redBits << TestLog::EndMessage;
928 log << TestLog::Message << "EGL_GREEN_SIZE = " << pixelFmt.greenBits << TestLog::EndMessage;
929 log << TestLog::Message << "EGL_BLUE_SIZE = " << pixelFmt.blueBits << TestLog::EndMessage;
930 log << TestLog::Message << "EGL_ALPHA_SIZE = " << pixelFmt.alphaBits << TestLog::EndMessage;
931 log << TestLog::Message << "EGL_DEPTH_SIZE = " << depthBits << TestLog::EndMessage;
932 log << TestLog::Message << "EGL_STENCIL_SIZE = " << stencilBits << TestLog::EndMessage;
933 log << TestLog::Message << "EGL_SAMPLES = " << numSamples << TestLog::EndMessage;
935 // Initialize semaphores.
936 for (vector<SemaphoreSp>::iterator sem = semaphores.begin(); sem != semaphores.end(); ++sem)
937 *sem = SemaphoreSp(new de::Semaphore(0));
940 for (vector<DrawPrimitiveOp>::iterator drawOp = drawOps.begin(); drawOp != drawOps.end(); ++drawOp)
941 randomizeDrawOp(rnd, *drawOp);
944 for (int threadNdx = 0; threadNdx < numThreads; threadNdx++)
946 packets[threadNdx].resize(packetsPerThread);
948 for (int packetNdx = 0; packetNdx < packetsPerThread; packetNdx++)
950 DrawOpPacket& packet = packets[threadNdx][packetNdx];
952 // Threads take turns with packets.
953 packet.wait = semaphores[packetNdx*numThreads + threadNdx];
954 packet.signal = semaphores[packetNdx*numThreads + threadNdx + 1];
955 packet.numOps = opsPerPacket;
956 packet.drawOps = &drawOps[(packetNdx*numThreads + threadNdx)*opsPerPacket];
960 // Create and setup programs per context
961 for (int ctxNdx = 0; ctxNdx < numContexts; ctxNdx++)
963 EGLint api = contexts[ctxNdx].first;
964 EGLContext context = contexts[ctxNdx].second;
966 EGLU_CHECK_CALL(egl, makeCurrent(display, surface, surface, context));
968 programs[ctxNdx] = ProgramSp(createProgram(m_gl, api));
969 programs[ctxNdx]->setup();
972 EGLU_CHECK_CALL(egl, makeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
975 // Clear to black using first context.
977 EGLint api = contexts[0].first;
978 EGLContext context = contexts[0].second;
980 EGLU_CHECK_CALL(egl, makeCurrent(display, surface, surface, context));
982 clear(m_gl, api, CLEAR_COLOR, CLEAR_DEPTH, CLEAR_STENCIL);
986 EGLU_CHECK_CALL(egl, makeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
989 // Create and launch threads (actual rendering starts once first semaphore is signaled).
990 for (int threadNdx = 0; threadNdx < numThreads; threadNdx++)
992 threads[threadNdx] = RenderTestThreadSp(new RenderTestThread(egl, display, surface, contexts[threadNdx].second, contexts[threadNdx].first, m_gl, *programs[threadNdx], packets[threadNdx]));
993 threads[threadNdx]->start();
996 // Signal start and wait until complete.
997 semaphores.front()->increment();
998 semaphores.back()->decrement();
1000 // Read pixels using first context. \todo [pyry] Randomize?
1002 EGLint api = contexts[0].first;
1003 EGLContext context = contexts[0].second;
1005 EGLU_CHECK_CALL(egl, makeCurrent(display, surface, surface, context));
1007 readPixels(m_gl, api, frame);
1010 EGLU_CHECK_CALL(egl, makeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT));
1013 for (int threadNdx = 0; threadNdx < numThreads; threadNdx++)
1014 threads[threadNdx]->join();
1016 // Render reference.
1017 renderReference(refFrame.getAccess(), drawOps, pixelFmt, depthBits, stencilBits, 1);
1021 bool imagesOk = tcu::fuzzyCompare(log, "ComparisonResult", "Image comparison result", refFrame, frame, threshold, tcu::COMPARE_LOG_RESULT);
1024 m_testCtx.setTestResult(QP_TEST_RESULT_FAIL, "Image comparison failed");
1028 RenderTests::RenderTests (EglTestContext& eglTestCtx)
1029 : TestCaseGroup(eglTestCtx, "render", "Basic rendering with different client APIs")
1033 RenderTests::~RenderTests (void)
1037 struct RenderGroupSpec
1042 eglu::ConfigFilter baseFilter;
1043 int numContextsPerApi;
1046 template <deUint32 Bits>
1047 static bool renderable (const eglu::CandidateConfig& c)
1049 return (c.renderableType() & Bits) == Bits;
1052 template <class RenderClass>
1053 static void createRenderGroups (EglTestContext& eglTestCtx, tcu::TestCaseGroup* group, const RenderGroupSpec* first, const RenderGroupSpec* last)
1055 for (const RenderGroupSpec* groupIter = first; groupIter != last; groupIter++)
1057 tcu::TestCaseGroup* configGroup = new tcu::TestCaseGroup(eglTestCtx.getTestContext(), groupIter->name, groupIter->desc);
1058 group->addChild(configGroup);
1060 vector<RenderFilterList> filterLists;
1061 eglu::FilterList baseFilters;
1062 baseFilters << groupIter->baseFilter;
1063 getDefaultRenderFilterLists(filterLists, baseFilters);
1065 for (vector<RenderFilterList>::const_iterator listIter = filterLists.begin(); listIter != filterLists.end(); listIter++)
1066 configGroup->addChild(new RenderClass(eglTestCtx, listIter->getName(), "", groupIter->apiBits, listIter->getSurfaceTypeMask(), *listIter, groupIter->numContextsPerApi));
1070 void RenderTests::init (void)
1072 static const RenderGroupSpec singleContextCases[] =
1076 "Primitive rendering using GLES2",
1078 renderable<EGL_OPENGL_ES2_BIT>,
1083 "Primitive rendering using GLES3",
1085 renderable<EGL_OPENGL_ES3_BIT>,
1090 static const RenderGroupSpec multiContextCases[] =
1094 "Primitive rendering using multiple GLES2 contexts to shared surface",
1096 renderable<EGL_OPENGL_ES2_BIT>,
1101 "Primitive rendering using multiple GLES3 contexts to shared surface",
1103 renderable<EGL_OPENGL_ES3_BIT>,
1108 "Primitive rendering using multiple APIs to shared surface",
1109 EGL_OPENGL_ES2_BIT|EGL_OPENGL_ES3_BIT,
1110 renderable<EGL_OPENGL_ES2_BIT|EGL_OPENGL_ES3_BIT>,
1115 tcu::TestCaseGroup* singleContextGroup = new tcu::TestCaseGroup(m_testCtx, "single_context", "Single-context rendering");
1116 addChild(singleContextGroup);
1117 createRenderGroups<SingleThreadRenderCase>(m_eglTestCtx, singleContextGroup, &singleContextCases[0], &singleContextCases[DE_LENGTH_OF_ARRAY(singleContextCases)]);
1119 tcu::TestCaseGroup* multiContextGroup = new tcu::TestCaseGroup(m_testCtx, "multi_context", "Multi-context rendering with shared surface");
1120 addChild(multiContextGroup);
1121 createRenderGroups<SingleThreadRenderCase>(m_eglTestCtx, multiContextGroup, &multiContextCases[0], &multiContextCases[DE_LENGTH_OF_ARRAY(multiContextCases)]);
1123 tcu::TestCaseGroup* multiThreadGroup = new tcu::TestCaseGroup(m_testCtx, "multi_thread", "Multi-thread rendering with shared surface");
1124 addChild(multiThreadGroup);
1125 createRenderGroups<MultiThreadRenderCase>(m_eglTestCtx, multiThreadGroup, &multiContextCases[0], &multiContextCases[DE_LENGTH_OF_ARRAY(multiContextCases)]);