1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program OpenGL (ES) 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 Texture buffer test case
22 *//*--------------------------------------------------------------------*/
24 #include "glsTextureBufferCase.hpp"
26 #include "tcuFormatUtil.hpp"
27 #include "tcuImageCompare.hpp"
28 #include "tcuRenderTarget.hpp"
29 #include "tcuStringTemplate.hpp"
30 #include "tcuSurface.hpp"
31 #include "tcuTestLog.hpp"
32 #include "tcuTextureUtil.hpp"
33 #include "tcuResultCollector.hpp"
35 #include "rrRenderer.hpp"
36 #include "rrShaders.hpp"
38 #include "gluObjectWrapper.hpp"
39 #include "gluPixelTransfer.hpp"
40 #include "gluShaderProgram.hpp"
41 #include "gluShaderUtil.hpp"
42 #include "gluStrUtil.hpp"
43 #include "gluTexture.hpp"
44 #include "gluTextureUtil.hpp"
46 #include "glwEnums.hpp"
47 #include "glwFunctions.hpp"
49 #include "deRandom.hpp"
50 #include "deStringUtil.hpp"
51 #include "deUniquePtr.hpp"
67 using namespace deqp::gls::TextureBufferCaseUtil;
78 MAX_VIEWPORT_WIDTH = 256,
79 MAX_VIEWPORT_HEIGHT = 256,
80 MIN_VIEWPORT_WIDTH = 64,
81 MIN_VIEWPORT_HEIGHT = 64,
84 deUint8 extend2BitsToByte (deUint8 bits)
86 DE_ASSERT((bits & (~0x03u)) == 0);
88 return (deUint8)(bits | (bits << 2) | (bits << 4) | (bits << 6));
91 void genRandomCoords (de::Random rng, vector<deUint8>& coords, size_t offset, size_t size)
93 const deUint8 bits = 2;
94 const deUint8 bitMask = deUint8((0x1u << bits) - 1);
98 for (int i = 0; i < (int)size; i++)
100 const deUint8 xBits = deUint8(rng.getUint32() & bitMask);
101 coords[i] = extend2BitsToByte(xBits);
104 // Fill indices with nice quad
106 const deUint8 indices[] =
108 extend2BitsToByte(0x0u),
109 extend2BitsToByte(0x1u),
110 extend2BitsToByte(0x2u),
111 extend2BitsToByte(0x3u)
114 for (int i = 0; i < DE_LENGTH_OF_ARRAY(indices); i++)
116 const deUint8 index = indices[i];
117 const size_t posX = (size_t(index) * 2) + 0;
118 const size_t posY = (size_t(index) * 2) + 1;
120 if (posX >= offset && posX < offset+size)
121 coords[posX - offset] = ((i % 2) == 0 ? extend2BitsToByte(0x0u) : extend2BitsToByte(0x3u));
123 if (posY >= offset && posY < offset+size)
124 coords[posY - offset] = ((i / 2) == 1 ? extend2BitsToByte(0x3u) : extend2BitsToByte(0x0u));
128 // Fill beginning of buffer
130 const deUint8 indices[] =
132 extend2BitsToByte(0x0u),
133 extend2BitsToByte(0x3u),
134 extend2BitsToByte(0x1u),
136 extend2BitsToByte(0x1u),
137 extend2BitsToByte(0x2u),
138 extend2BitsToByte(0x0u),
140 extend2BitsToByte(0x0u),
141 extend2BitsToByte(0x2u),
142 extend2BitsToByte(0x1u),
144 extend2BitsToByte(0x1u),
145 extend2BitsToByte(0x3u),
146 extend2BitsToByte(0x0u)
149 for (int i = (int)offset; i < DE_LENGTH_OF_ARRAY(indices) && i < (int)(offset + size); i++)
150 coords[i-offset] = indices[i];
154 class CoordVertexShader : public rr::VertexShader
157 CoordVertexShader (void)
158 : rr::VertexShader(1, 1)
160 m_inputs[0].type = rr::GENERICVECTYPE_FLOAT;
161 m_outputs[0].type = rr::GENERICVECTYPE_FLOAT;
164 void shadeVertices (const rr::VertexAttrib* inputs, rr::VertexPacket* const* packets, const int numPackets) const
166 for (int packetNdx = 0; packetNdx < numPackets; packetNdx++)
168 rr::VertexPacket* const packet = packets[packetNdx];
171 readVertexAttrib(position, inputs[0], packet->instanceNdx, packet->vertexNdx);
173 packet->outputs[0] = tcu::Vec4(1.0f);
174 packet->position = tcu::Vec4(2.0f * (position.x() - 0.5f), 2.0f * (position.y() - 0.5f), 0.0f, 1.0f);
179 class TextureVertexShader : public rr::VertexShader
182 TextureVertexShader (const tcu::ConstPixelBufferAccess& texture)
183 : rr::VertexShader (1, 1)
184 , m_texture (texture)
186 m_inputs[0].type = rr::GENERICVECTYPE_FLOAT;
187 m_outputs[0].type = rr::GENERICVECTYPE_FLOAT;
190 void shadeVertices (const rr::VertexAttrib* inputs, rr::VertexPacket* const* packets, const int numPackets) const
192 for (int packetNdx = 0; packetNdx < numPackets; packetNdx++)
194 rr::VertexPacket* const packet = packets[packetNdx];
196 tcu::Vec4 texelValue;
198 readVertexAttrib(position, inputs[0], packet->instanceNdx, packet->vertexNdx);
200 texelValue = tcu::Vec4(m_texture.getPixel(de::clamp<int>((deRoundFloatToInt32(position.x() * 4) + 4) * (deRoundFloatToInt32(position.y() * 4) + 4), 0, m_texture.getWidth()-1), 0));
202 packet->outputs[0] = texelValue;
203 packet->position = tcu::Vec4(2.0f * (position.x() - 0.5f), 2.0f * (position.y() - 0.5f), 0.0f, 1.0f);
208 const tcu::ConstPixelBufferAccess m_texture;
211 class CoordFragmentShader : public rr::FragmentShader
214 CoordFragmentShader (void)
215 : rr::FragmentShader (1, 1)
217 m_inputs[0].type = rr::GENERICVECTYPE_FLOAT;
218 m_outputs[0].type = rr::GENERICVECTYPE_FLOAT;
222 void shadeFragments (rr::FragmentPacket* packets, const int numPackets, const rr::FragmentShadingContext& context) const
224 for (int packetNdx = 0; packetNdx < numPackets; packetNdx++)
226 rr::FragmentPacket& packet = packets[packetNdx];
228 const tcu::Vec4 vtxColor0 = rr::readVarying<float>(packet, context, 0, 0);
229 const tcu::Vec4 vtxColor1 = rr::readVarying<float>(packet, context, 0, 1);
230 const tcu::Vec4 vtxColor2 = rr::readVarying<float>(packet, context, 0, 2);
231 const tcu::Vec4 vtxColor3 = rr::readVarying<float>(packet, context, 0, 3);
233 const tcu::Vec4 color0 = vtxColor0;
234 const tcu::Vec4 color1 = vtxColor1;
235 const tcu::Vec4 color2 = vtxColor2;
236 const tcu::Vec4 color3 = vtxColor3;
238 rr::writeFragmentOutput(context, packetNdx, 0, 0, tcu::Vec4(color0.x() * color0.w(), color0.y() * color0.w(), color0.z() * color0.w(), 1.0f));
239 rr::writeFragmentOutput(context, packetNdx, 1, 0, tcu::Vec4(color1.x() * color1.w(), color1.y() * color1.w(), color1.z() * color1.w(), 1.0f));
240 rr::writeFragmentOutput(context, packetNdx, 2, 0, tcu::Vec4(color2.x() * color2.w(), color2.y() * color2.w(), color2.z() * color2.w(), 1.0f));
241 rr::writeFragmentOutput(context, packetNdx, 3, 0, tcu::Vec4(color3.x() * color3.w(), color3.y() * color3.w(), color3.z() * color3.w(), 1.0f));
246 class TextureFragmentShader : public rr::FragmentShader
249 TextureFragmentShader (const tcu::ConstPixelBufferAccess& texture)
250 : rr::FragmentShader (1, 1)
251 , m_texture (texture)
253 m_inputs[0].type = rr::GENERICVECTYPE_FLOAT;
254 m_outputs[0].type = rr::GENERICVECTYPE_FLOAT;
257 void shadeFragments (rr::FragmentPacket* packets, const int numPackets, const rr::FragmentShadingContext& context) const
259 for (int packetNdx = 0; packetNdx < numPackets; packetNdx++)
261 rr::FragmentPacket& packet = packets[packetNdx];
263 const tcu::IVec2 position0 = packet.position + tcu::IVec2(0, 0);
264 const tcu::IVec2 position1 = packet.position + tcu::IVec2(1, 0);
265 const tcu::IVec2 position2 = packet.position + tcu::IVec2(0, 1);
266 const tcu::IVec2 position3 = packet.position + tcu::IVec2(1, 1);
268 const tcu::Vec4 texColor0 = m_texture.getPixel(de::clamp((position0.x() * position0.y()), 0, m_texture.getWidth()-1), 0);
269 const tcu::Vec4 texColor1 = m_texture.getPixel(de::clamp((position1.x() * position1.y()), 0, m_texture.getWidth()-1), 0);
270 const tcu::Vec4 texColor2 = m_texture.getPixel(de::clamp((position2.x() * position2.y()), 0, m_texture.getWidth()-1), 0);
271 const tcu::Vec4 texColor3 = m_texture.getPixel(de::clamp((position3.x() * position3.y()), 0, m_texture.getWidth()-1), 0);
273 const tcu::Vec4 vtxColor0 = rr::readVarying<float>(packet, context, 0, 0);
274 const tcu::Vec4 vtxColor1 = rr::readVarying<float>(packet, context, 0, 1);
275 const tcu::Vec4 vtxColor2 = rr::readVarying<float>(packet, context, 0, 2);
276 const tcu::Vec4 vtxColor3 = rr::readVarying<float>(packet, context, 0, 3);
278 const tcu::Vec4 color0 = 0.5f * (vtxColor0 + texColor0);
279 const tcu::Vec4 color1 = 0.5f * (vtxColor1 + texColor1);
280 const tcu::Vec4 color2 = 0.5f * (vtxColor2 + texColor2);
281 const tcu::Vec4 color3 = 0.5f * (vtxColor3 + texColor3);
283 rr::writeFragmentOutput(context, packetNdx, 0, 0, tcu::Vec4(color0.x() * color0.w(), color0.y() * color0.w(), color0.z() * color0.w(), 1.0f));
284 rr::writeFragmentOutput(context, packetNdx, 1, 0, tcu::Vec4(color1.x() * color1.w(), color1.y() * color1.w(), color1.z() * color1.w(), 1.0f));
285 rr::writeFragmentOutput(context, packetNdx, 2, 0, tcu::Vec4(color2.x() * color2.w(), color2.y() * color2.w(), color2.z() * color2.w(), 1.0f));
286 rr::writeFragmentOutput(context, packetNdx, 3, 0, tcu::Vec4(color3.x() * color3.w(), color3.y() * color3.w(), color3.z() * color3.w(), 1.0f));
291 const tcu::ConstPixelBufferAccess m_texture;
294 string generateVertexShaderTemplate (RenderBits renderBits)
296 std::ostringstream stream;
299 "${VERSION_HEADER}\n";
301 if (renderBits & RENDERBITS_AS_VERTEX_TEXTURE)
302 stream << "${TEXTURE_BUFFER_EXT}";
305 "${VTX_INPUT} layout(location = 0) ${HIGHP} vec2 i_coord;\n"
306 "${VTX_OUTPUT} ${HIGHP} vec4 v_color;\n";
308 if (renderBits & RENDERBITS_AS_VERTEX_TEXTURE)
311 "uniform ${HIGHP} samplerBuffer u_vtxSampler;\n";
319 if (renderBits & RENDERBITS_AS_VERTEX_TEXTURE)
320 stream << "\tv_color = texelFetch(u_vtxSampler, clamp((int(round(i_coord.x * 4.0)) + 4) * (int(round(i_coord.y * 4.0)) + 4), 0, textureSize(u_vtxSampler)-1));\n";
322 stream << "\tv_color = vec4(1.0);\n";
325 "\tgl_Position = vec4(2.0 * (i_coord - vec2(0.5)), 0.0, 1.0);\n"
331 string generateFragmentShaderTemplate (RenderBits renderBits)
333 std::ostringstream stream;
336 "${VERSION_HEADER}\n";
338 if (renderBits & RENDERBITS_AS_FRAGMENT_TEXTURE)
339 stream << "${TEXTURE_BUFFER_EXT}";
342 "${FRAG_OUTPUT} layout(location = 0) ${HIGHP} vec4 dEQP_FragColor;\n"
343 "${FRAG_INPUT} ${HIGHP} vec4 v_color;\n";
345 if (renderBits & RENDERBITS_AS_FRAGMENT_TEXTURE)
346 stream << "uniform ${HIGHP} samplerBuffer u_fragSampler;\n";
353 if (renderBits & RENDERBITS_AS_FRAGMENT_TEXTURE)
354 stream << "\t${HIGHP} vec4 color = 0.5 * (v_color + texelFetch(u_fragSampler, clamp(int(gl_FragCoord.x) * int(gl_FragCoord.y), 0, textureSize(u_fragSampler)-1)));\n";
356 stream << "\t${HIGHP} vec4 color = v_color;\n";
359 "\tdEQP_FragColor = vec4(color.xyz * color.w, 1.0);\n"
365 string specializeShader (const string& shaderTemplateString, glu::GLSLVersion glslVersion)
367 const tcu::StringTemplate shaderTemplate(shaderTemplateString);
368 map<string, string> parameters;
370 parameters["VERSION_HEADER"] = glu::getGLSLVersionDeclaration(glslVersion);
371 parameters["VTX_OUTPUT"] = "out";
372 parameters["VTX_INPUT"] = "in";
373 parameters["FRAG_INPUT"] = "in";
374 parameters["FRAG_OUTPUT"] = "out";
375 parameters["HIGHP"] = (glslVersion == glu::GLSL_VERSION_330 ? "" : "highp");
376 parameters["TEXTURE_BUFFER_EXT"] = (glslVersion == glu::GLSL_VERSION_330 ? "" : "#extension GL_EXT_texture_buffer : enable\n");
378 return shaderTemplate.specialize(parameters);
381 glu::ShaderProgram* createRenderProgram (glu::RenderContext& renderContext,
382 RenderBits renderBits)
384 const string vertexShaderTemplate = generateVertexShaderTemplate(renderBits);
385 const string fragmentShaderTemplate = generateFragmentShaderTemplate(renderBits);
387 const glu::GLSLVersion glslVersion = glu::getContextTypeGLSLVersion(renderContext.getType());
389 const string vertexShaderSource = specializeShader(vertexShaderTemplate, glslVersion);
390 const string fragmentShaderSource = specializeShader(fragmentShaderTemplate, glslVersion);
392 glu::ShaderProgram* const program = new glu::ShaderProgram(renderContext, glu::makeVtxFragSources(vertexShaderSource, fragmentShaderSource));
397 void logModifications (TestLog& log, ModifyBits modifyBits)
399 tcu::ScopedLogSection section(log, "Modify Operations", "Modify Operations");
407 { MODIFYBITS_BUFFERDATA, "Recreate buffer data with glBufferData()." },
408 { MODIFYBITS_BUFFERSUBDATA, "Modify texture buffer with glBufferSubData()." },
409 { MODIFYBITS_MAPBUFFER_WRITE, "Map buffer write-only and rewrite data." },
410 { MODIFYBITS_MAPBUFFER_READWRITE, "Map buffer readw-write check and rewrite data." }
413 DE_ASSERT(modifyBits != 0);
415 for (int infoNdx = 0; infoNdx < DE_LENGTH_OF_ARRAY(bitInfos); infoNdx++)
417 if (modifyBits & bitInfos[infoNdx].bit)
418 log << TestLog::Message << bitInfos[infoNdx].str << TestLog::EndMessage;
422 void modifyBufferData (TestLog& log,
424 glu::TextureBuffer& texture)
426 vector<deUint8> data;
428 genRandomCoords(rng, data, 0, texture.getBufferSize());
430 log << TestLog::Message << "BufferData, Size: " << data.size() << TestLog::EndMessage;
433 // replace getRefBuffer with a new buffer
434 de::ArrayBuffer<deUint8> buffer(&(data[0]), data.size());
435 texture.getRefBuffer().swap(buffer);
441 void modifyBufferSubData (TestLog& log,
443 const glw::Functions& gl,
444 glu::TextureBuffer& texture)
446 const size_t minSize = 4*16;
447 const size_t size = de::max<size_t>(minSize, size_t((float)(texture.getSize() != 0 ? texture.getSize() : texture.getBufferSize()) * (0.7f + 0.3f * rng.getFloat())));
448 const size_t minOffset = texture.getOffset();
449 const size_t offset = minOffset + (rng.getUint32() % (texture.getBufferSize() - (size + minOffset)));
450 vector<deUint8> data;
452 genRandomCoords(rng, data, offset, size);
454 log << TestLog::Message << "BufferSubData, Offset: " << offset << ", Size: " << size << TestLog::EndMessage;
456 gl.bindBuffer(GL_TEXTURE_BUFFER, texture.getGLBuffer());
457 gl.bufferSubData(GL_TEXTURE_BUFFER, (glw::GLsizei)offset, (glw::GLsizei)data.size(), &(data[0]));
458 gl.bindBuffer(GL_TEXTURE_BUFFER, 0);
459 GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to update data with glBufferSubData()");
461 deMemcpy((deUint8*)texture.getRefBuffer().getPtr() + offset, &(data[0]), int(data.size()));
464 void modifyMapWrite (TestLog& log,
466 const glw::Functions& gl,
467 glu::TextureBuffer& texture)
469 const size_t minSize = 4*16;
470 const size_t size = de::max<size_t>(minSize, size_t((float)(texture.getSize() != 0 ? texture.getSize() : texture.getBufferSize()) * (0.7f + 0.3f * rng.getFloat())));
471 const size_t minOffset = texture.getOffset();
472 const size_t offset = minOffset + (rng.getUint32() % (texture.getBufferSize() - (size + minOffset)));
473 vector<deUint8> data;
475 genRandomCoords(rng, data, offset, size);
477 log << TestLog::Message << "glMapBufferRange, Write Only, Offset: " << offset << ", Size: " << size << TestLog::EndMessage;
479 gl.bindBuffer(GL_TEXTURE_BUFFER, texture.getGLBuffer());
481 deUint8* ptr = (deUint8*)gl.mapBufferRange(GL_TEXTURE_BUFFER, (glw::GLsizei)offset, (glw::GLsizei)size, GL_MAP_WRITE_BIT);
483 GLU_EXPECT_NO_ERROR(gl.getError(), "glMapBufferRange()");
486 for (int i = 0; i < (int)data.size(); i++)
489 TCU_CHECK(gl.unmapBuffer(GL_TEXTURE_BUFFER));
491 gl.bindBuffer(GL_TEXTURE_BUFFER, 0);
492 GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to update data with glMapBufferRange()");
494 deMemcpy((deUint8*)texture.getRefBuffer().getPtr()+offset, &(data[0]), int(data.size()));
497 void modifyMapReadWrite (TestLog& log,
498 tcu::ResultCollector& resultCollector,
500 const glw::Functions& gl,
501 glu::TextureBuffer& texture)
503 const size_t minSize = 4*16;
504 const size_t size = de::max<size_t>(minSize, size_t((float)(texture.getSize() != 0 ? texture.getSize() : texture.getBufferSize()) * (0.7f + 0.3f * rng.getFloat())));
505 const size_t minOffset = texture.getOffset();
506 const size_t offset = minOffset + (rng.getUint32() % (texture.getBufferSize() - (size + minOffset)));
507 deUint8* const refPtr = (deUint8*)texture.getRefBuffer().getPtr() + offset;
508 vector<deUint8> data;
510 genRandomCoords(rng, data, offset, size);
512 log << TestLog::Message << "glMapBufferRange, Read Write, Offset: " << offset << ", Size: " << size << TestLog::EndMessage;
514 gl.bindBuffer(GL_TEXTURE_BUFFER, texture.getGLBuffer());
516 size_t invalidBytes = 0;
517 deUint8* const ptr = (deUint8*)gl.mapBufferRange(GL_TEXTURE_BUFFER, (glw::GLsizei)offset, (glw::GLsizei)size, GL_MAP_WRITE_BIT|GL_MAP_READ_BIT);
519 GLU_EXPECT_NO_ERROR(gl.getError(), "glMapBufferRange()");
522 for (int i = 0; i < (int)data.size(); i++)
524 if (ptr[i] != refPtr[i])
526 if (invalidBytes < 24)
527 log << TestLog::Message << "Invalid byte in mapped buffer. " << tcu::Format::Hex<2>(data[i]).toString() << " at " << i << ", expected " << tcu::Format::Hex<2>(refPtr[i]).toString() << TestLog::EndMessage;
535 TCU_CHECK(gl.unmapBuffer(GL_TEXTURE_BUFFER));
537 if (invalidBytes > 0)
539 log << TestLog::Message << "Total of " << invalidBytes << " invalid bytes." << TestLog::EndMessage;
540 resultCollector.fail("Invalid data in mapped buffer");
544 gl.bindBuffer(GL_TEXTURE_BUFFER, 0);
545 GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to update data with glMapBufferRange()");
547 for (int i = 0; i < (int)data.size(); i++)
551 void modify (TestLog& log,
552 tcu::ResultCollector& resultCollector,
553 glu::RenderContext& renderContext,
554 ModifyBits modifyBits,
556 glu::TextureBuffer& texture)
558 const tcu::ScopedLogSection modifySection(log, "Modifying Texture buffer", "Modifying Texture Buffer");
560 logModifications(log, modifyBits);
562 if (modifyBits & MODIFYBITS_BUFFERDATA)
563 modifyBufferData(log, rng, texture);
565 if (modifyBits & MODIFYBITS_BUFFERSUBDATA)
566 modifyBufferSubData(log, rng, renderContext.getFunctions(), texture);
568 if (modifyBits & MODIFYBITS_MAPBUFFER_WRITE)
569 modifyMapWrite(log, rng, renderContext.getFunctions(), texture);
571 if (modifyBits & MODIFYBITS_MAPBUFFER_READWRITE)
572 modifyMapReadWrite(log, resultCollector, rng, renderContext.getFunctions(), texture);
575 void renderGL (glu::RenderContext& renderContext,
576 RenderBits renderBits,
579 glu::ShaderProgram& program,
580 glu::TextureBuffer& texture)
582 const glw::Functions& gl = renderContext.getFunctions();
583 const glu::VertexArray vao (renderContext);
584 const glu::Buffer coordBuffer (renderContext);
586 gl.useProgram(program.getProgram());
587 gl.bindVertexArray(*vao);
589 gl.enableVertexAttribArray(0);
591 if (renderBits & RENDERBITS_AS_VERTEX_ARRAY)
593 gl.bindBuffer(GL_ARRAY_BUFFER, texture.getGLBuffer());
594 gl.vertexAttribPointer(0, 2, GL_UNSIGNED_BYTE, true, 0, DE_NULL);
598 de::Random rng(coordSeed);
599 vector<deUint8> coords;
601 genRandomCoords(rng, coords, 0, 256*2);
603 gl.bindBuffer(GL_ARRAY_BUFFER, *coordBuffer);
604 gl.bufferData(GL_ARRAY_BUFFER, (glw::GLsizei)coords.size(), &(coords[0]), GL_STREAM_DRAW);
605 gl.vertexAttribPointer(0, 2, GL_UNSIGNED_BYTE, true, 0, DE_NULL);
608 if (renderBits & RENDERBITS_AS_VERTEX_TEXTURE)
610 const deInt32 location = gl.getUniformLocation(program.getProgram(), "u_vtxSampler");
612 gl.activeTexture(GL_TEXTURE0);
613 gl.bindTexture(GL_TEXTURE_BUFFER, texture.getGLTexture());
614 gl.uniform1i(location, 0);
617 if (renderBits & RENDERBITS_AS_FRAGMENT_TEXTURE)
619 const deInt32 location = gl.getUniformLocation(program.getProgram(), "u_fragSampler");
621 gl.activeTexture(GL_TEXTURE1);
622 gl.bindTexture(GL_TEXTURE_BUFFER, texture.getGLTexture());
623 gl.uniform1i(location, 1);
624 gl.activeTexture(GL_TEXTURE0);
627 if (renderBits & RENDERBITS_AS_INDEX_ARRAY)
629 gl.bindBuffer(GL_ELEMENT_ARRAY_BUFFER, texture.getGLBuffer());
630 gl.drawElements(GL_TRIANGLES, triangleCount * 3, GL_UNSIGNED_BYTE, DE_NULL);
631 gl.bindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
634 gl.drawArrays(GL_TRIANGLES, 0, triangleCount * 3);
636 if (renderBits & RENDERBITS_AS_FRAGMENT_TEXTURE)
638 gl.activeTexture(GL_TEXTURE1);
639 gl.bindTexture(GL_TEXTURE_BUFFER, 0);
642 if (renderBits & RENDERBITS_AS_VERTEX_TEXTURE)
644 gl.activeTexture(GL_TEXTURE0);
645 gl.bindTexture(GL_TEXTURE_BUFFER, 0);
648 gl.bindBuffer(GL_ARRAY_BUFFER, 0);
649 gl.disableVertexAttribArray(0);
651 gl.bindVertexArray(0);
653 GLU_EXPECT_NO_ERROR(gl.getError(), "Rendering failed");
656 void renderReference (RenderBits renderBits,
659 const glu::TextureBuffer& texture,
660 int maxTextureBufferSize,
661 const tcu::PixelBufferAccess& target)
663 const tcu::ConstPixelBufferAccess effectiveAccess = glu::getTextureBufferEffectiveRefTexture(texture, maxTextureBufferSize);
665 const CoordVertexShader coordVertexShader;
666 const TextureVertexShader textureVertexShader (effectiveAccess);
667 const rr::VertexShader* const vertexShader = (renderBits & RENDERBITS_AS_VERTEX_TEXTURE ? static_cast<const rr::VertexShader*>(&textureVertexShader) : &coordVertexShader);
669 const CoordFragmentShader coordFragmmentShader;
670 const TextureFragmentShader textureFragmentShader (effectiveAccess);
671 const rr::FragmentShader* const fragmentShader = (renderBits & RENDERBITS_AS_FRAGMENT_TEXTURE ? static_cast<const rr::FragmentShader*>(&textureFragmentShader) : &coordFragmmentShader);
673 const rr::Renderer renderer;
674 const rr::RenderState renderState(rr::ViewportState(rr::WindowRectangle(0, 0, target.getWidth(), target.getHeight())));
675 const rr::RenderTarget renderTarget(rr::MultisamplePixelBufferAccess::fromSinglesampleAccess(target));
677 const rr::Program program(vertexShader, fragmentShader);
679 rr::VertexAttrib vertexAttribs[1];
680 vector<deUint8> coords;
682 if (renderBits & RENDERBITS_AS_VERTEX_ARRAY)
684 vertexAttribs[0].type = rr::VERTEXATTRIBTYPE_NONPURE_UNORM8;
685 vertexAttribs[0].size = 2;
686 vertexAttribs[0].pointer = texture.getRefBuffer().getPtr();
690 de::Random rng(coordSeed);
692 genRandomCoords(rng, coords, 0, 256*2);
694 vertexAttribs[0].type = rr::VERTEXATTRIBTYPE_NONPURE_UNORM8;
695 vertexAttribs[0].size = 2;
696 vertexAttribs[0].pointer = &(coords[0]);
699 if (renderBits & RENDERBITS_AS_INDEX_ARRAY)
701 const rr::PrimitiveList primitives(rr::PRIMITIVETYPE_TRIANGLES, triangleCount * 3, rr::DrawIndices(texture.getRefBuffer().getPtr(), rr::INDEXTYPE_UINT8));
702 const rr::DrawCommand cmd(renderState, renderTarget, program, 1, vertexAttribs, primitives);
708 const rr::PrimitiveList primitives(rr::PRIMITIVETYPE_TRIANGLES, triangleCount * 3, 0);
709 const rr::DrawCommand cmd(renderState, renderTarget, program, 1, vertexAttribs, primitives);
715 void logRendering (TestLog& log, RenderBits renderBits)
723 { RENDERBITS_AS_VERTEX_ARRAY, "vertex array" },
724 { RENDERBITS_AS_INDEX_ARRAY, "index array" },
725 { RENDERBITS_AS_VERTEX_TEXTURE, "vertex texture" },
726 { RENDERBITS_AS_FRAGMENT_TEXTURE, "fragment texture" }
729 std::ostringstream stream;
730 vector<const char*> usedAs;
732 DE_ASSERT(renderBits != 0);
734 for (int infoNdx = 0; infoNdx < DE_LENGTH_OF_ARRAY(bitInfos); infoNdx++)
736 if (renderBits & bitInfos[infoNdx].bit)
737 usedAs.push_back(bitInfos[infoNdx].str);
740 stream << "Render using texture buffer as ";
742 for (int asNdx = 0; asNdx < (int)usedAs.size(); asNdx++)
744 if (asNdx+1 == (int)usedAs.size() && (int)usedAs.size() > 1)
749 stream << usedAs[asNdx];
754 log << TestLog::Message << stream.str() << TestLog::EndMessage;
757 void render (TestLog& log,
758 glu::RenderContext& renderContext,
759 RenderBits renderBits,
761 glu::ShaderProgram& program,
762 glu::TextureBuffer& texture,
763 const tcu::PixelBufferAccess& target)
765 const tcu::ScopedLogSection renderSection (log, "Render Texture buffer", "Render Texture Buffer");
766 const int triangleCount = 8;
767 const deUint32 coordSeed = rng.getUint32();
768 int maxTextureBufferSize = 0;
770 renderContext.getFunctions().getIntegerv(GL_MAX_TEXTURE_BUFFER_SIZE, &maxTextureBufferSize);
771 GLU_EXPECT_NO_ERROR(renderContext.getFunctions().getError(), "query GL_MAX_TEXTURE_BUFFER_SIZE");
772 DE_ASSERT(maxTextureBufferSize > 0); // checked in init()
774 logRendering(log, renderBits);
776 renderGL(renderContext, renderBits, coordSeed, triangleCount, program, texture);
777 renderReference(renderBits, coordSeed, triangleCount, texture, maxTextureBufferSize, target);
780 void verifyScreen (TestLog& log,
781 tcu::ResultCollector& resultCollector,
782 glu::RenderContext& renderContext,
783 const tcu::ConstPixelBufferAccess& referenceTarget)
785 const tcu::ScopedLogSection verifySection (log, "Verify screen contents", "Verify screen contents");
786 tcu::Surface screen (referenceTarget.getWidth(), referenceTarget.getHeight());
788 glu::readPixels(renderContext, 0, 0, screen.getAccess());
790 if (!tcu::fuzzyCompare(log, "Result of rendering", "Result of rendering", referenceTarget, screen.getAccess(), 0.05f, tcu::COMPARE_LOG_RESULT))
791 resultCollector.fail("Rendering failed");
794 void logImplementationInfo (TestLog& log, glu::RenderContext& renderContext)
796 const tcu::ScopedLogSection section (log, "Implementation Values", "Implementation Values");
797 de::UniquePtr<glu::ContextInfo> info (glu::ContextInfo::create(renderContext));
798 const glw::Functions& gl = renderContext.getFunctions();
800 if (glu::contextSupports(renderContext.getType(), glu::ApiType(3, 3, glu::PROFILE_CORE)))
802 deInt32 maxTextureSize = 0;
804 gl.getIntegerv(GL_MAX_TEXTURE_BUFFER_SIZE, &maxTextureSize);
805 GLU_EXPECT_NO_ERROR(gl.getError(), "glGetIntegerv(GL_MAX_TEXTURE_BUFFER_SIZE)");
807 log << TestLog::Message << "GL_MAX_TEXTURE_BUFFER_SIZE : " << maxTextureSize << TestLog::EndMessage;
809 else if (glu::contextSupports(renderContext.getType(), glu::ApiType(3, 1, glu::PROFILE_ES)) && info->isExtensionSupported("GL_EXT_texture_buffer"))
812 deInt32 maxTextureSize = 0;
814 gl.getIntegerv(GL_MAX_TEXTURE_BUFFER_SIZE, &maxTextureSize);
815 GLU_EXPECT_NO_ERROR(gl.getError(), "glGetIntegerv(GL_MAX_TEXTURE_BUFFER_SIZE_EXT)");
817 log << TestLog::Message << "GL_MAX_TEXTURE_BUFFER_SIZE_EXT : " << maxTextureSize << TestLog::EndMessage;
821 deInt32 textureBufferAlignment = 0;
823 gl.getIntegerv(GL_TEXTURE_BUFFER_OFFSET_ALIGNMENT, &textureBufferAlignment);
824 GLU_EXPECT_NO_ERROR(gl.getError(), "glGetIntegerv(GL_TEXTURE_BUFFER_OFFSET_ALIGNMENT_EXT)");
826 log << TestLog::Message << "GL_TEXTURE_BUFFER_OFFSET_ALIGNMENT_EXT : " << textureBufferAlignment << TestLog::EndMessage;
833 void logTextureInfo (TestLog& log,
839 const tcu::ScopedLogSection section(log, "Texture Info", "Texture Info");
841 log << TestLog::Message << "Texture format : " << glu::getTextureFormatStr(format) << TestLog::EndMessage;
842 log << TestLog::Message << "Buffer size : " << bufferSize << TestLog::EndMessage;
844 if (offset != 0 || size != 0)
846 log << TestLog::Message << "Buffer range offset: " << offset << TestLog::EndMessage;
847 log << TestLog::Message << "Buffer range size: " << size << TestLog::EndMessage;
851 void runTests (tcu::TestContext& testCtx,
852 glu::RenderContext& renderContext,
858 RenderBits preRender,
859 glu::ShaderProgram* preRenderProgram,
860 ModifyBits modifyType,
861 RenderBits postRender,
862 glu::ShaderProgram* postRenderProgram)
864 const tcu::RenderTarget renderTarget (renderContext.getRenderTarget());
865 const glw::Functions& gl = renderContext.getFunctions();
867 const int width = de::min<int>(renderTarget.getWidth(), MAX_VIEWPORT_WIDTH);
868 const int height = de::min<int>(renderTarget.getHeight(), MAX_VIEWPORT_HEIGHT);
869 const tcu::Vec4 clearColor (0.25f, 0.5f, 0.75f, 1.0f);
871 TestLog& log = testCtx.getLog();
872 tcu::ResultCollector resultCollector (log);
874 logImplementationInfo(log, renderContext);
875 logTextureInfo(log, format, bufferSize, offset, size);
878 tcu::Surface referenceTarget (width, height);
879 vector<deUint8> bufferData;
881 genRandomCoords(rng, bufferData, 0, bufferSize);
883 for (deUint8 i = 0; i < 4; i++)
885 const deUint8 val = extend2BitsToByte(i);
887 if (val >= offset && val < offset + size)
889 bufferData[val*2 + 0] = (i / 2 == 0 ? extend2BitsToByte(0x2u) : extend2BitsToByte(0x01u));
890 bufferData[val*2 + 1] = (i % 2 == 0 ? extend2BitsToByte(0x2u) : extend2BitsToByte(0x01u));
895 glu::TextureBuffer texture (renderContext, format, bufferSize, offset, size, &(bufferData[0]));
897 TCU_CHECK_MSG(width >= MIN_VIEWPORT_WIDTH || height >= MIN_VIEWPORT_HEIGHT, "Too small viewport");
899 DE_ASSERT(preRender == 0 || preRenderProgram);
900 DE_ASSERT(postRender == 0 || postRenderProgram);
902 gl.viewport(0, 0, width, height);
903 gl.clearColor(clearColor.x(), clearColor.y(), clearColor.z(), clearColor.w());
904 gl.clear(GL_COLOR_BUFFER_BIT);
905 GLU_EXPECT_NO_ERROR(gl.getError(), "Screen setup failed");
907 tcu::clear(referenceTarget.getAccess(), clearColor);
912 render(log, renderContext, preRender, rng, *preRenderProgram, texture, referenceTarget.getAccess());
915 modify(log, resultCollector, renderContext, modifyType, rng, texture);
918 render(log, renderContext, postRender, rng, *postRenderProgram, texture, referenceTarget.getAccess());
921 verifyScreen(log, resultCollector, renderContext, referenceTarget.getAccess());
923 resultCollector.setTestContextResult(testCtx);
929 TextureBufferCase::TextureBufferCase (tcu::TestContext& testCtx,
930 glu::RenderContext& renderCtx,
935 RenderBits preRender,
937 RenderBits postRender,
939 const char* description)
940 : tcu::TestCase (testCtx, name, description)
941 , m_renderCtx (renderCtx)
943 , m_bufferSize (bufferSize)
947 , m_preRender (preRender)
949 , m_postRender (postRender)
951 , m_preRenderProgram (DE_NULL)
952 , m_postRenderProgram (DE_NULL)
956 TextureBufferCase::~TextureBufferCase (void)
958 TextureBufferCase::deinit();
961 void TextureBufferCase::init (void)
963 de::UniquePtr<glu::ContextInfo> info (glu::ContextInfo::create(m_renderCtx));
965 if (!glu::contextSupports(m_renderCtx.getType(), glu::ApiType(3, 3, glu::PROFILE_CORE))
966 && !(glu::contextSupports(m_renderCtx.getType(), glu::ApiType(3, 1, glu::PROFILE_ES)) && info->isExtensionSupported("GL_EXT_texture_buffer")))
967 throw tcu::NotSupportedError("Texture buffers not supported", "", __FILE__, __LINE__);
970 const int maxTextureBufferSize = info->getInt(GL_MAX_TEXTURE_BUFFER_SIZE);
971 if (maxTextureBufferSize <= 0)
972 TCU_THROW(NotSupportedError, "GL_MAX_TEXTURE_BUFFER_SIZE > 0 required");
975 if (m_preRender != 0)
977 TestLog& log = m_testCtx.getLog();
978 const char* const sectionName = (m_postRender != 0 ? "Primary render program" : "Render program");
979 const tcu::ScopedLogSection section (log, sectionName, sectionName);
981 m_preRenderProgram = createRenderProgram(m_renderCtx, m_preRender);
982 m_testCtx.getLog() << (*m_preRenderProgram);
984 TCU_CHECK(m_preRenderProgram->isOk());
987 if (m_postRender != 0)
990 if (m_preRender == m_postRender)
992 m_postRenderProgram = m_preRenderProgram;
996 TestLog& log = m_testCtx.getLog();
997 const char* const sectionName = (m_preRender!= 0 ? "Secondary render program" : "Render program");
998 const tcu::ScopedLogSection section (log, sectionName, sectionName);
1000 m_postRenderProgram = createRenderProgram(m_renderCtx, m_postRender);
1001 m_testCtx.getLog() << (*m_postRenderProgram);
1003 TCU_CHECK(m_postRenderProgram->isOk());
1008 void TextureBufferCase::deinit (void)
1010 if (m_preRenderProgram == m_postRenderProgram)
1011 m_postRenderProgram = DE_NULL;
1013 delete m_preRenderProgram;
1014 m_preRenderProgram = DE_NULL;
1016 delete m_postRenderProgram;
1017 m_postRenderProgram = DE_NULL;
1020 tcu::TestCase::IterateResult TextureBufferCase::iterate (void)
1022 de::Random rng (deInt32Hash(deStringHash(getName())));
1027 const glw::Functions& gl = m_renderCtx.getFunctions();
1028 deInt32 alignment = 0;
1030 gl.getIntegerv(GL_TEXTURE_BUFFER_OFFSET_ALIGNMENT, &alignment);
1031 GLU_EXPECT_NO_ERROR(gl.getError(), "glGetIntegerv(GL_TEXTURE_BUFFER_OFFSET_ALIGNMENT)");
1033 offset = m_offset * alignment;
1038 runTests(m_testCtx, m_renderCtx, rng, m_format, m_bufferSize, offset, m_size, m_preRender, m_preRenderProgram, m_modify, m_postRender, m_postRenderProgram);