1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL ES 3.0 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 Object lifetime tests.
22 *//*--------------------------------------------------------------------*/
24 #include "es3fLifetimeTests.hpp"
26 #include "deRandom.hpp"
27 #include "deUniquePtr.hpp"
28 #include "tcuRenderTarget.hpp"
29 #include "tcuSurface.hpp"
30 #include "gluDrawUtil.hpp"
31 #include "gluObjectWrapper.hpp"
32 #include "gluPixelTransfer.hpp"
33 #include "gluShaderProgram.hpp"
34 #include "glsLifetimeTests.hpp"
35 #include "glwEnums.hpp"
36 #include "glwFunctions.hpp"
52 using tcu::RenderTarget;
54 using tcu::TestContext;
56 using glu::CallLogWrapper;
57 using glu::RenderContext;
58 using glu::ProgramSources;
59 using glu::VertexArray;
61 namespace lt = gls::LifetimeTests;
64 typedef TestCase::IterateResult IterateResult;
66 enum { VIEWPORT_SIZE = 128 };
68 class ScaleProgram : public glu::ShaderProgram
71 ScaleProgram (lt::Context& ctx);
72 void draw (GLuint vao, GLfloat scale, bool tf, Surface* dst);
73 void setPos (GLuint buffer, GLuint vao);
76 ProgramSources getSources (void);
78 const RenderContext& m_renderCtx;
83 enum { NUM_COMPONENTS = 4, NUM_VERTICES = 3 };
85 ScaleProgram::ScaleProgram (lt::Context& ctx)
86 : glu::ShaderProgram (ctx.getRenderContext(), getSources())
87 , m_renderCtx (ctx.getRenderContext())
89 const Functions& gl = m_renderCtx.getFunctions();
91 m_scaleLoc = gl.getUniformLocation(getProgram(), "scale");
92 m_posLoc = gl.getAttribLocation(getProgram(), "pos");
95 #define GLSL(VERSION, BODY) ("#version " #VERSION "\n" #BODY "\n")
97 static const char* const s_vertexShaderSrc = GLSL(
103 gl_Position = vec4(scale * pos.xy, pos.zw);
107 static const char* const s_fragmentShaderSrc = GLSL(
111 gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0);
115 ProgramSources ScaleProgram::getSources (void)
118 ProgramSources sources;
119 sources << VertexSource(s_vertexShaderSrc)
120 << FragmentSource(s_fragmentShaderSrc)
121 << TransformFeedbackMode(GL_INTERLEAVED_ATTRIBS)
122 << TransformFeedbackVarying("gl_Position");
126 void ScaleProgram::draw (GLuint vao, GLfloat scale, bool tf, Surface* dst)
128 const Functions& gl = m_renderCtx.getFunctions();
129 de::Random rnd (vao);
130 Rectangle viewport = randomViewport(m_renderCtx,
131 VIEWPORT_SIZE, VIEWPORT_SIZE, rnd);
132 setViewport(m_renderCtx, viewport);
133 gl.clearColor(0, 0, 0, 1);
134 gl.clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
136 gl.bindVertexArray(vao);
137 gl.enableVertexAttribArray(m_posLoc);
138 GLU_CHECK_CALL_ERROR(gl.useProgram(getProgram()),
141 gl.uniform1f(m_scaleLoc, scale);
144 gl.beginTransformFeedback(GL_TRIANGLES);
145 GLU_CHECK_CALL_ERROR(gl.drawArrays(GL_TRIANGLES, 0, 3), gl.getError());
147 gl.endTransformFeedback();
150 readRectangle(m_renderCtx, viewport, *dst);
152 gl.bindVertexArray(0);
155 void ScaleProgram::setPos (GLuint buffer, GLuint vao)
157 const Functions& gl = m_renderCtx.getFunctions();
159 gl.bindBuffer(GL_ARRAY_BUFFER, buffer);
160 gl.bindVertexArray(vao);
161 GLU_CHECK_CALL_ERROR(
162 gl.vertexAttribPointer(m_posLoc, NUM_COMPONENTS, GL_FLOAT, false, 0, DE_NULL),
164 gl.bindVertexArray(0);
165 gl.bindBuffer(GL_ARRAY_BUFFER, 0);
166 GLU_CHECK_ERROR(gl.getError());
169 class VertexArrayBinder : public SimpleBinder
172 VertexArrayBinder (lt::Context& ctx)
173 : SimpleBinder (ctx, 0, GL_NONE, GL_VERTEX_ARRAY_BINDING, true) {}
174 void bind (GLuint name) { glBindVertexArray(name); }
177 class SamplerBinder : public Binder
180 SamplerBinder (lt::Context& ctx) : Binder(ctx) {}
181 void bind (GLuint name) { glBindSampler(0, name); }
182 GLuint getBinding (void)
185 glGetIntegerv(GL_SAMPLER_BINDING, arr);
186 log() << TestLog::Message << "// First output integer: " << arr[0]
187 << TestLog::EndMessage;
190 bool genRequired (void) const { return true; }
193 class QueryBinder : public Binder
196 QueryBinder (lt::Context& ctx) : Binder(ctx) {}
197 void bind (GLuint name)
200 glBeginQuery(GL_ANY_SAMPLES_PASSED, name);
202 glEndQuery(GL_ANY_SAMPLES_PASSED);
204 GLuint getBinding (void) { return 0; }
207 class BufferVAOAttacher : public Attacher
210 BufferVAOAttacher (lt::Context& ctx, Type& elementType,
211 Type& varrType, ScaleProgram& program)
212 : Attacher (ctx, elementType, varrType)
213 , m_program (program) {}
214 void initAttachment (GLuint seed, GLuint element);
215 void attach (GLuint element, GLuint container);
216 void detach (GLuint element, GLuint container);
217 bool canAttachDeleted (void) const { return false; }
218 ScaleProgram& getProgram (void) { return m_program; }
219 GLuint getAttachment (GLuint container);
222 ScaleProgram& m_program;
225 static const GLfloat s_varrData[NUM_VERTICES * NUM_COMPONENTS] =
232 void initBuffer (const Functions& gl, GLuint seed, GLenum usage, GLuint buffer)
234 gl.bindBuffer(GL_ARRAY_BUFFER, buffer);
236 gl.bufferData(GL_ARRAY_BUFFER, sizeof(s_varrData), s_varrData, usage);
240 GLfloat data[DE_LENGTH_OF_ARRAY(s_varrData)];
242 for (int ndx = 0; ndx < NUM_VERTICES; ndx++)
244 GLfloat* vertex = &data[ndx * NUM_COMPONENTS];
245 vertex[0] = 2.0f * (rnd.getFloat() - 0.5f);
246 vertex[1] = 2.0f * (rnd.getFloat() - 0.5f);
247 DE_STATIC_ASSERT(NUM_COMPONENTS == 4);
251 gl.bufferData(GL_ARRAY_BUFFER, sizeof(data), data, usage);
253 gl.bindBuffer(GL_ARRAY_BUFFER, 0);
254 GLU_CHECK_ERROR(gl.getError());
257 void BufferVAOAttacher::initAttachment (GLuint seed, GLuint buffer)
259 initBuffer(gl(), seed, GL_STATIC_DRAW, buffer);
260 log() << TestLog::Message << "// Initialized buffer " << buffer << " from seed " << seed
261 << TestLog::EndMessage;
264 void BufferVAOAttacher::attach (GLuint buffer, GLuint vao)
266 m_program.setPos(buffer, vao);
267 log() << TestLog::Message
268 << "// Set the `pos` attribute in VAO " << vao << " to buffer " << buffer
269 << TestLog::EndMessage;
272 void BufferVAOAttacher::detach (GLuint buffer, GLuint varr)
278 GLuint BufferVAOAttacher::getAttachment (GLuint varr)
281 gl().bindVertexArray(varr);
282 gl().getVertexAttribiv(0, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &name);
283 gl().bindVertexArray(0);
284 GLU_CHECK_ERROR(gl().getError());
288 class BufferVAOInputAttacher : public InputAttacher
291 BufferVAOInputAttacher (BufferVAOAttacher& attacher)
292 : InputAttacher (attacher)
293 , m_program (attacher.getProgram()) {}
294 void drawContainer (GLuint container, Surface& dst);
297 ScaleProgram& m_program;
300 void BufferVAOInputAttacher::drawContainer (GLuint vao, Surface& dst)
302 m_program.draw(vao, 1.0, false, &dst);
303 log() << TestLog::Message << "// Drew an output image with VAO " << vao
304 << TestLog::EndMessage;
307 class BufferTfAttacher : public Attacher
310 BufferTfAttacher (lt::Context& ctx, Type& bufferType, Type& tfType)
311 : Attacher (ctx, bufferType, tfType) {}
312 void initAttachment (GLuint seed, GLuint element);
313 void attach (GLuint buffer, GLuint tf);
314 void detach (GLuint buffer, GLuint tf);
315 bool canAttachDeleted (void) const { return false; }
316 GLuint getAttachment (GLuint tf);
319 void BufferTfAttacher::initAttachment (GLuint seed, GLuint buffer)
321 initBuffer(gl(), seed, GL_DYNAMIC_READ, buffer);
322 log() << TestLog::Message << "// Initialized buffer " << buffer << " from seed " << seed
323 << TestLog::EndMessage;
326 void BufferTfAttacher::attach (GLuint buffer, GLuint tf)
328 glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, tf);
329 glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, buffer);
330 glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);
331 GLU_CHECK_ERROR(gl().getError());
334 void BufferTfAttacher::detach (GLuint buffer, GLuint tf)
337 glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, tf);
338 glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
339 glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);
340 GLU_CHECK_ERROR(gl().getError());
343 GLuint BufferTfAttacher::getAttachment (GLuint tf)
346 gl().bindTransformFeedback(GL_TRANSFORM_FEEDBACK, tf);
347 gl().getIntegeri_v(GL_TRANSFORM_FEEDBACK_BUFFER_BINDING, 0, &ret);
348 gl().bindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);
349 GLU_CHECK_ERROR(gl().getError());
353 class BufferTfOutputAttacher : public OutputAttacher
356 BufferTfOutputAttacher (BufferTfAttacher& attacher, ScaleProgram& program)
357 : OutputAttacher (attacher)
358 , m_program (program) {}
359 void setupContainer (GLuint seed, GLuint container);
360 void drawAttachment (GLuint attachment, Surface& dst);
363 ScaleProgram& m_program;
366 void BufferTfOutputAttacher::drawAttachment (GLuint buffer, Surface& dst)
368 VertexArray vao(getRenderContext());
370 m_program.setPos(buffer, *vao);
371 m_program.draw(*vao, 1.0, false, &dst);
372 log() << TestLog::Message
373 << "// Drew output image with vertices from buffer " << buffer
374 << TestLog::EndMessage;
375 GLU_CHECK_ERROR(gl().getError());
378 void BufferTfOutputAttacher::setupContainer (GLuint seed, GLuint tf)
380 Buffer posBuf (getRenderContext());
381 VertexArray vao (getRenderContext());
383 initBuffer(gl(), seed, GL_STATIC_DRAW, *posBuf);
384 m_program.setPos(*posBuf, *vao);
386 glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, tf);
387 m_program.draw(*vao, -1.0, true, DE_NULL);
388 log() << TestLog::Message
389 << "// Drew an image with seed " << seed << " with transform feedback to " << tf
390 << TestLog::EndMessage;
391 glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);
392 GLU_CHECK_ERROR(gl().getError());
395 class ES3Types : public ES2Types
398 ES3Types (lt::Context& ctx);
400 ScaleProgram m_program;
401 QueryBinder m_queryBind;
402 SimpleType m_queryType;
403 SimpleBinder m_tfBind;
405 VertexArrayBinder m_varrBind;
406 SimpleType m_varrType;
407 SamplerBinder m_samplerBind;
408 SimpleType m_samplerType;
409 BufferVAOAttacher m_bufVarrAtt;
410 BufferVAOInputAttacher m_bufVarrInAtt;
411 BufferTfAttacher m_bufTfAtt;
412 BufferTfOutputAttacher m_bufTfOutAtt;
415 ES3Types::ES3Types (lt::Context& ctx)
419 , m_queryType (ctx, "query", &CallLogWrapper::glGenQueries,
420 &CallLogWrapper::glDeleteQueries,
421 &CallLogWrapper::glIsQuery, &m_queryBind)
422 , m_tfBind (ctx, &CallLogWrapper::glBindTransformFeedback, GL_TRANSFORM_FEEDBACK,
423 GL_TRANSFORM_FEEDBACK_BINDING, true)
424 , m_tfType (ctx, "transform_feedback", &CallLogWrapper::glGenTransformFeedbacks,
425 &CallLogWrapper::glDeleteTransformFeedbacks,
426 &CallLogWrapper::glIsTransformFeedback, &m_tfBind)
428 , m_varrType (ctx, "vertex_array", &CallLogWrapper::glGenVertexArrays,
429 &CallLogWrapper::glDeleteVertexArrays,
430 &CallLogWrapper::glIsVertexArray, &m_varrBind)
431 , m_samplerBind (ctx)
432 , m_samplerType (ctx, "sampler", &CallLogWrapper::glGenSamplers,
433 &CallLogWrapper::glDeleteSamplers,
434 &CallLogWrapper::glIsSampler, &m_samplerBind, true)
435 , m_bufVarrAtt (ctx, m_bufferType, m_varrType, m_program)
436 , m_bufVarrInAtt(m_bufVarrAtt)
437 , m_bufTfAtt (ctx, m_bufferType, m_tfType)
438 , m_bufTfOutAtt (m_bufTfAtt, m_program)
440 Type* types[] = { &m_queryType, &m_tfType, &m_varrType, &m_samplerType };
441 m_types.insert(m_types.end(), DE_ARRAY_BEGIN(types), DE_ARRAY_END(types));
443 m_attachers.push_back(&m_bufVarrAtt);
444 m_attachers.push_back(&m_bufTfAtt);
446 m_inAttachers.push_back(&m_bufVarrInAtt);
447 m_outAttachers.push_back(&m_bufTfOutAtt);
450 class TfDeleteActiveTest : public TestCase, private CallLogWrapper
453 TfDeleteActiveTest (gles3::Context& context,
454 const char* name, const char* description);
455 IterateResult iterate (void);
458 TfDeleteActiveTest::TfDeleteActiveTest (gles3::Context& context,
459 const char* name, const char* description)
460 : TestCase (context, name, description)
461 , CallLogWrapper (context.getRenderContext().getFunctions(),
462 context.getTestContext().getLog())
467 class ScopedTransformFeedbackFeedback
470 ScopedTransformFeedbackFeedback (glu::CallLogWrapper& gl, GLenum type);
471 ~ScopedTransformFeedbackFeedback (void);
474 glu::CallLogWrapper& m_gl;
477 ScopedTransformFeedbackFeedback::ScopedTransformFeedbackFeedback (glu::CallLogWrapper& gl, GLenum type)
480 m_gl.glBeginTransformFeedback(type);
481 GLU_EXPECT_NO_ERROR(m_gl.glGetError(), "glBeginTransformFeedback");
484 ScopedTransformFeedbackFeedback::~ScopedTransformFeedbackFeedback (void)
486 m_gl.glEndTransformFeedback();
489 IterateResult TfDeleteActiveTest::iterate (void)
491 static const char* const s_xfbVertexSource = "#version 300 es\n"
494 " gl_Position = vec4(float(gl_VertexID) / 2.0, float(gl_VertexID % 2) / 2.0, 0.0, 1.0);\n"
496 static const char* const s_xfbFragmentSource = "#version 300 es\n"
497 "layout(location=0) out mediump vec4 dEQP_FragColor;\n"
500 " dEQP_FragColor = vec4(1.0, 1.0, 0.0, 1.0);\n"
503 glu::Buffer buf (m_context.getRenderContext());
505 glu::ShaderProgram program (m_context.getRenderContext(),
506 glu::ProgramSources()
507 << glu::VertexSource(s_xfbVertexSource)
508 << glu::FragmentSource(s_xfbFragmentSource)
509 << glu::TransformFeedbackMode(GL_INTERLEAVED_ATTRIBS)
510 << glu::TransformFeedbackVarying("gl_Position"));
514 m_testCtx.getLog() << program;
515 throw tcu::TestError("failed to build program");
520 GLU_CHECK_CALL(glUseProgram(program.getProgram()));
521 GLU_CHECK_CALL(glGenTransformFeedbacks(1, &tf));
522 GLU_CHECK_CALL(glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, tf));
523 GLU_CHECK_CALL(glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, *buf));
524 GLU_CHECK_CALL(glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER, 3 * sizeof(glw::GLfloat[4]), DE_NULL, GL_DYNAMIC_COPY));
527 ScopedTransformFeedbackFeedback xfb(static_cast<glu::CallLogWrapper&>(*this), GL_TRIANGLES);
529 glDeleteTransformFeedbacks(1, &tf);
531 GLenum err = glGetError();
532 if (err != GL_INVALID_OPERATION)
533 getTestContext().setTestResult(
535 "Deleting active transform feedback did not produce GL_INVALID_OPERATION");
537 getTestContext().setTestResult(QP_TEST_RESULT_PASS, "Pass");
540 GLU_CHECK(); // ScopedTransformFeedbackFeedback::dtor might modify error state
542 GLU_CHECK_CALL(glDeleteTransformFeedbacks(1, &tf));
544 catch (const glu::Error&)
546 glDeleteTransformFeedbacks(1, &tf);
553 class TestGroup : public TestCaseGroup
556 TestGroup (gles3::Context& context)
557 : TestCaseGroup (context, "lifetime", "Object lifetime tests")
561 MovePtr<Types> m_types;
564 void TestGroup::init (void)
566 gles3::Context& ctx = getContext();
567 lt::Context ltCtx (ctx.getRenderContext(), ctx.getTestContext());
569 m_types = MovePtr<Types>(new ES3Types(ltCtx));
571 addTestCases(*this, *m_types);
573 TestCaseGroup* deleteActiveGroup =
574 new TestCaseGroup(ctx, "delete_active", "Delete active object");
575 addChild(deleteActiveGroup);
576 deleteActiveGroup->addChild(
577 new TfDeleteActiveTest(ctx, "transform_feedback", "Transform Feedback"));
582 TestCaseGroup* createLifetimeTests (Context& context)
584 return new TestGroup(context);