13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements Quality Program OpenGL (ES) Module
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * -----------------------------------------------
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License");
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License.
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *      http://www.apache.org/licenses/LICENSE-2.0
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS,
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License.
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*!
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Texture buffer test case
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "glsTextureBufferCase.hpp"
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuFormatUtil.hpp"
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuImageCompare.hpp"
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuRenderTarget.hpp"
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuStringTemplate.hpp"
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuSurface.hpp"
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuTestLog.hpp"
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "tcuTextureUtil.hpp"
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "rrRenderer.hpp"
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "rrShaders.hpp"
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "gluObjectWrapper.hpp"
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "gluPixelTransfer.hpp"
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "gluShaderProgram.hpp"
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "gluShaderUtil.hpp"
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "gluStrUtil.hpp"
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "gluTexture.hpp"
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "glwEnums.hpp"
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "glwFunctions.hpp"
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deRandom.hpp"
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deStringUtil.hpp"
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deUniquePtr.hpp"
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deMemory.h"
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deString.h"
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deMath.h"
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <sstream>
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <string>
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <vector>
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing tcu::TestLog;
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing std::map;
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing std::string;
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing std::vector;
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing namespace deqp::gls::TextureBufferCaseUtil;
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace deqp
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace gls
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyryenum
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	MAX_VIEWPORT_WIDTH	= 256,
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	MAX_VIEWPORT_HEIGHT	= 256,
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	MIN_VIEWPORT_WIDTH	= 64,
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	MIN_VIEWPORT_HEIGHT	= 64,
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
823c827367444ee418f129b2c238299f49d3264554Jarkko PoyrydeUint8 extend2BitsToByte (deUint8 bits)
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deUint8 x = 0;
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_ASSERT((bits & (~0x03u)) == 0);
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	x |= bits << 6;
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	x |= bits << 4;
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	x |= bits << 2;
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	x |= bits;
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return x;
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid genRandomCoords (de::Random rng, vector<deUint8>& coords, size_t offset, size_t size)
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const deUint8 bits		= 2;
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const deUint8 bitMask	= deUint8((0x1u << bits) - 1);
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	coords.resize(size);
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < (int)size; i++)
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const deUint8 xBits = deUint8(rng.getUint32() & bitMask);
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		coords[i] = extend2BitsToByte(xBits);
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Fill indices with nice quad
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const deUint8 indices[] =
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			extend2BitsToByte(0x0u),
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			extend2BitsToByte(0x1u),
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			extend2BitsToByte(0x2u),
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			extend2BitsToByte(0x3u)
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		};
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int i = 0; i < DE_LENGTH_OF_ARRAY(indices); i++)
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const deUint8	index	= indices[i];
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const size_t	posX	= (size_t(index) * 2) + 0;
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const size_t	posY	= (size_t(index) * 2) + 1;
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (posX >= offset && posX < offset+size)
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				coords[posX - offset] = ((i % 2) == 0 ? extend2BitsToByte(0x0u) : extend2BitsToByte(0x3u));
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (posY >= offset && posY < offset+size)
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				coords[posY - offset] = ((i / 2) == 1 ? extend2BitsToByte(0x3u) : extend2BitsToByte(0x0u));
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Fill beginning of buffer
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const deUint8 indices[] =
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			extend2BitsToByte(0x0u),
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			extend2BitsToByte(0x3u),
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			extend2BitsToByte(0x1u),
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			extend2BitsToByte(0x1u),
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			extend2BitsToByte(0x2u),
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			extend2BitsToByte(0x0u),
1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			extend2BitsToByte(0x0u),
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			extend2BitsToByte(0x2u),
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			extend2BitsToByte(0x1u),
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			extend2BitsToByte(0x1u),
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			extend2BitsToByte(0x3u),
1513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			extend2BitsToByte(0x0u)
1523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		};
1533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int i = (int)offset; i < DE_LENGTH_OF_ARRAY(indices) && i < (int)(offset + size); i++)
1553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			coords[i-offset] = indices[i];
1563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1593c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass CoordVertexShader : public rr::VertexShader
1603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1613c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
1623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	CoordVertexShader (void)
1633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		: rr::VertexShader(1, 1)
1643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_inputs[0].type	= rr::GENERICVECTYPE_FLOAT;
1663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_outputs[0].type	= rr::GENERICVECTYPE_FLOAT;
1673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void shadeVertices (const rr::VertexAttrib* inputs, rr::VertexPacket* const* packets, const int numPackets) const
1703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int packetNdx = 0; packetNdx < numPackets; packetNdx++)
1723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			rr::VertexPacket* const		packet		= packets[packetNdx];
1743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			tcu::Vec4					position;
1753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			readVertexAttrib(position, inputs[0], packet->instanceNdx, packet->vertexNdx);
1773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			packet->outputs[0]	= tcu::Vec4(1.0f);
1793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			packet->position	= tcu::Vec4(2.0f * (position.x() - 0.5f), 2.0f * (position.y() - 0.5f), 0.0f, 1.0f);
1803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
1833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1843c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass TextureVertexShader : public rr::VertexShader
1853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1863c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
1873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	TextureVertexShader (const tcu::ConstPixelBufferAccess& texture)
1883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		: rr::VertexShader	(1, 1)
1893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		, m_texture			(texture)
1903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_inputs[0].type	= rr::GENERICVECTYPE_FLOAT;
1923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_outputs[0].type	= rr::GENERICVECTYPE_FLOAT;
1933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void shadeVertices (const rr::VertexAttrib* inputs, rr::VertexPacket* const* packets, const int numPackets) const
1963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int packetNdx = 0; packetNdx < numPackets; packetNdx++)
1983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			rr::VertexPacket* const		packet		= packets[packetNdx];
2003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			tcu::Vec4					position;
2013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			tcu::Vec4					texelValue;
2023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			readVertexAttrib(position, inputs[0], packet->instanceNdx, packet->vertexNdx);
2043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			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));
2063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			packet->outputs[0]	= texelValue;
2083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			packet->position	= tcu::Vec4(2.0f * (position.x() - 0.5f), 2.0f * (position.y() - 0.5f), 0.0f, 1.0f);
2093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
2103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2123c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
2133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const tcu::ConstPixelBufferAccess& m_texture;
2143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
2153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2163c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass CoordFragmentShader : public rr::FragmentShader
2173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2183c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
2193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	CoordFragmentShader (void)
2203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		: rr::FragmentShader (1, 1)
2213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
2223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_inputs[0].type	= rr::GENERICVECTYPE_FLOAT;
2233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_outputs[0].type	= rr::GENERICVECTYPE_FLOAT;
2243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void shadeFragments (rr::FragmentPacket* packets, const int numPackets, const rr::FragmentShadingContext& context) const
2283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
2293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int packetNdx = 0; packetNdx < numPackets; packetNdx++)
2303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
2313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			rr::FragmentPacket&	packet		= packets[packetNdx];
2323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::Vec4		vtxColor0	= rr::readVarying<float>(packet, context, 0, 0);
2343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::Vec4		vtxColor1	= rr::readVarying<float>(packet, context, 0, 1);
2353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::Vec4		vtxColor2	= rr::readVarying<float>(packet, context, 0, 2);
2363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::Vec4		vtxColor3	= rr::readVarying<float>(packet, context, 0, 3);
2373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::Vec4		color0		= vtxColor0;
2393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::Vec4		color1		= vtxColor1;
2403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::Vec4		color2		= vtxColor2;
2413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::Vec4		color3		= vtxColor3;
2423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			rr::writeFragmentOutput(context, packetNdx, 0, 0, tcu::Vec4(color0.x() * color0.w(), color0.y() * color0.w(), color0.z() * color0.w(), 1.0f));
2443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			rr::writeFragmentOutput(context, packetNdx, 1, 0, tcu::Vec4(color1.x() * color1.w(), color1.y() * color1.w(), color1.z() * color1.w(), 1.0f));
2453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			rr::writeFragmentOutput(context, packetNdx, 2, 0, tcu::Vec4(color2.x() * color2.w(), color2.y() * color2.w(), color2.z() * color2.w(), 1.0f));
2463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			rr::writeFragmentOutput(context, packetNdx, 3, 0, tcu::Vec4(color3.x() * color3.w(), color3.y() * color3.w(), color3.z() * color3.w(), 1.0f));
2473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
2483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
2503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2513c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass TextureFragmentShader : public rr::FragmentShader
2523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2533c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
2543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	TextureFragmentShader (const tcu::ConstPixelBufferAccess& texture)
2553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		: rr::FragmentShader	(1, 1)
2563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		, m_texture				(texture)
2573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
2583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_inputs[0].type	= rr::GENERICVECTYPE_FLOAT;
2593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_outputs[0].type	= rr::GENERICVECTYPE_FLOAT;
2603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void shadeFragments (rr::FragmentPacket* packets, const int numPackets, const rr::FragmentShadingContext& context) const
2633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
2643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int packetNdx = 0; packetNdx < numPackets; packetNdx++)
2653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
2663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			rr::FragmentPacket&	packet		= packets[packetNdx];
2673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::IVec2	position	= packet.position;
2693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::IVec2	position0	= packet.position + tcu::IVec2(0, 0);
2713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::IVec2	position1	= packet.position + tcu::IVec2(1, 0);
2723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::IVec2	position2	= packet.position + tcu::IVec2(0, 1);
2733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::IVec2	position3	= packet.position + tcu::IVec2(1, 1);
2743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::Vec4		texColor0	= m_texture.getPixel(de::clamp((position0.x() * position0.y()), 0, m_texture.getWidth()-1), 0);
2763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::Vec4		texColor1	= m_texture.getPixel(de::clamp((position1.x() * position1.y()), 0, m_texture.getWidth()-1), 0);
2773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::Vec4		texColor2	= m_texture.getPixel(de::clamp((position2.x() * position2.y()), 0, m_texture.getWidth()-1), 0);
2783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::Vec4		texColor3	= m_texture.getPixel(de::clamp((position3.x() * position3.y()), 0, m_texture.getWidth()-1), 0);
2793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::Vec4		vtxColor0	= rr::readVarying<float>(packet, context, 0, 0);
2813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::Vec4		vtxColor1	= rr::readVarying<float>(packet, context, 0, 1);
2823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::Vec4		vtxColor2	= rr::readVarying<float>(packet, context, 0, 2);
2833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::Vec4		vtxColor3	= rr::readVarying<float>(packet, context, 0, 3);
2843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::Vec4		color0		= 0.5f * (vtxColor0 + texColor0);
2863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::Vec4		color1		= 0.5f * (vtxColor1 + texColor1);
2873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::Vec4		color2		= 0.5f * (vtxColor2 + texColor2);
2883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::Vec4		color3		= 0.5f * (vtxColor3 + texColor3);
2893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			rr::writeFragmentOutput(context, packetNdx, 0, 0, tcu::Vec4(color0.x() * color0.w(), color0.y() * color0.w(), color0.z() * color0.w(), 1.0f));
2913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			rr::writeFragmentOutput(context, packetNdx, 1, 0, tcu::Vec4(color1.x() * color1.w(), color1.y() * color1.w(), color1.z() * color1.w(), 1.0f));
2923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			rr::writeFragmentOutput(context, packetNdx, 2, 0, tcu::Vec4(color2.x() * color2.w(), color2.y() * color2.w(), color2.z() * color2.w(), 1.0f));
2933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			rr::writeFragmentOutput(context, packetNdx, 3, 0, tcu::Vec4(color3.x() * color3.w(), color3.y() * color3.w(), color3.z() * color3.w(), 1.0f));
2943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
2953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2973c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
2983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const tcu::ConstPixelBufferAccess& m_texture;
2993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
3003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3013c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystring generateVertexShaderTemplate (RenderBits renderBits)
3023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	std::ostringstream stream;
3043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	stream <<
3063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		"${VERSION_HEADER}\n";
3073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (renderBits & RENDERBITS_AS_VERTEX_TEXTURE)
3093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		stream << "${TEXTURE_BUFFER_EXT}";
3103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	stream <<
3123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		"${VTX_INPUT} layout(location = 0) ${HIGHP} vec2 i_coord;\n"
3133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		"${VTX_OUTPUT} ${HIGHP} vec4 v_color;\n";
3143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (renderBits & RENDERBITS_AS_VERTEX_TEXTURE)
3163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
3173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		stream <<
3183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			"uniform ${HIGHP} samplerBuffer u_vtxSampler;\n";
3193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
3203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	stream <<
3223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		"\n"
3233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		"void main (void)\n"
3243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		"{\n";
3253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (renderBits & RENDERBITS_AS_VERTEX_TEXTURE)
3273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		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";
3283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else
3293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		stream << "\tv_color = vec4(1.0);\n";
3303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	stream <<
3323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		"\tgl_Position = vec4(2.0 * (i_coord - vec2(0.5)), 0.0, 1.0);\n"
3333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		"}\n";
3343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return stream.str();
3363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3383c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystring generateFragmentShaderTemplate (RenderBits renderBits)
3393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	std::ostringstream stream;
3413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	stream <<
3433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		"${VERSION_HEADER}\n";
3443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (renderBits & RENDERBITS_AS_FRAGMENT_TEXTURE)
3463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		stream << "${TEXTURE_BUFFER_EXT}";
3473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	stream <<
3493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		"${FRAG_OUTPUT} layout(location = 0) ${HIGHP} vec4 dEQP_FragColor;\n"
3503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		"${FRAG_INPUT} ${HIGHP} vec4 v_color;\n";
3513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (renderBits & RENDERBITS_AS_FRAGMENT_TEXTURE)
3533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		stream << "uniform ${HIGHP} samplerBuffer u_fragSampler;\n";
3543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	stream <<
3563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		"\n"
3573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		"void main (void)\n"
3583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		"{\n";
3593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (renderBits & RENDERBITS_AS_FRAGMENT_TEXTURE)
3613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		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";
3623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else
3633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		stream << "\t${HIGHP} vec4 color = v_color;\n";
3643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	stream <<
3663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		"\tdEQP_FragColor = vec4(color.xyz * color.w, 1.0);\n"
3673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		"}\n";
3683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return stream.str();
3703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3723c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystring specializeShader (const string& shaderTemplateString, glu::GLSLVersion glslVersion)
3733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const tcu::StringTemplate	shaderTemplate(shaderTemplateString);
3753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	map<string, string>			parameters;
3763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	parameters["VERSION_HEADER"]		= glu::getGLSLVersionDeclaration(glslVersion);
3783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	parameters["VTX_OUTPUT"]			= "out";
3793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	parameters["VTX_INPUT"]				= "in";
3803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	parameters["FRAG_INPUT"]			= "in";
3813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	parameters["FRAG_OUTPUT"]			= "out";
3823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	parameters["HIGHP"]					= (glslVersion == glu::GLSL_VERSION_330 ? "" : "highp");
3833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	parameters["TEXTURE_BUFFER_EXT"]	= (glslVersion == glu::GLSL_VERSION_330 ? "" : "#extension GL_EXT_texture_buffer : enable\n");
3843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return shaderTemplate.specialize(parameters);
3863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3883c827367444ee418f129b2c238299f49d3264554Jarkko Poyryglu::ShaderProgram* createRenderProgram (glu::RenderContext&	renderContext,
3893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry										 RenderBits				renderBits)
3903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
3913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const string				vertexShaderTemplate	= generateVertexShaderTemplate(renderBits);
3923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const string				fragmentShaderTemplate	= generateFragmentShaderTemplate(renderBits);
3933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const glu::GLSLVersion		glslVersion				= glu::getContextTypeGLSLVersion(renderContext.getType());
3953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const string				vertexShaderSource		= specializeShader(vertexShaderTemplate, glslVersion);
3973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const string				fragmentShaderSource	= specializeShader(fragmentShaderTemplate, glslVersion);
3983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	glu::ShaderProgram* const	program					= new glu::ShaderProgram(renderContext, glu::makeVtxFragSources(vertexShaderSource, fragmentShaderSource));
4003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return program;
4023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4043c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid logModifications (TestLog& log, ModifyBits modifyBits)
4053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
4063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	tcu::ScopedLogSection section(log, "Modify Operations", "Modify Operations");
4073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const struct
4093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
4103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		ModifyBits	bit;
4113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const char*	str;
4123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	} bitInfos[] =
4133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
4143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{ MODIFYBITS_BUFFERDATA,			"Recreate buffer data with glBufferData()."			},
4153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{ MODIFYBITS_BUFFERSUBDATA,			"Modify texture buffer with glBufferSubData()."		},
4163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{ MODIFYBITS_MAPBUFFER_WRITE,		"Map buffer write-only and rewrite data."			},
4173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{ MODIFYBITS_MAPBUFFER_READWRITE,	"Map buffer readw-write check and rewrite data."	}
4183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	};
4193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_ASSERT(modifyBits != 0);
4213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int infoNdx = 0; infoNdx < DE_LENGTH_OF_ARRAY(bitInfos); infoNdx++)
4233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
4243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (modifyBits & bitInfos[infoNdx].bit)
4253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			log << TestLog::Message << bitInfos[infoNdx].str << TestLog::EndMessage;
4263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
4273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4293c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid modifyBufferData (TestLog&				log,
4303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					   de::Random&			rng,
4313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					   glu::TextureBuffer&	texture)
4323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
4333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	vector<deUint8> data;
4343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	genRandomCoords(rng, data, 0, texture.getBufferSize());
4363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	log << TestLog::Message << "BufferData, Size: " << data.size() << TestLog::EndMessage;
4383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	texture.bufferData(&(data[0]), data.size());
4403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	texture.upload();
4413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4433c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid modifyBufferSubData (TestLog&				log,
4443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry						  de::Random&			rng,
4453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry						  const glw::Functions&	gl,
4463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry						  glu::TextureBuffer&	texture)
4473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
4483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const size_t				minSize		= 4*16;
4493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const size_t				size		= de::max<size_t>(minSize, size_t((texture.getSize() != 0 ? texture.getSize() : texture.getBufferSize()) * (0.7 + 0.3 * rng.getFloat())));
4503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const size_t				minOffset	= texture.getOffset();
4513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const size_t				offset		= minOffset + (rng.getUint32() % (texture.getBufferSize() - (size + minOffset)));
4523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	vector<deUint8>				data;
4533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	genRandomCoords(rng, data, offset, size);
4553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	log << TestLog::Message << "BufferSubData, Offset: " << offset << ", Size: " << size << TestLog::EndMessage;
4573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	gl.bindBuffer(GL_TEXTURE_BUFFER, texture.getGLBuffer());
4593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	gl.bufferSubData(GL_TEXTURE_BUFFER, (glw::GLsizei)offset, (glw::GLsizei)data.size(), &(data[0]));
4603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	gl.bindBuffer(GL_TEXTURE_BUFFER, 0);
4613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to update data with glBufferSubData()");
4623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deMemcpy(texture.getRefBuffer() + offset, &(data[0]), int(data.size()));
4643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4663c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid modifyMapWrite (TestLog&				log,
4673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					 de::Random&			rng,
4683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					 const glw::Functions&	gl,
4693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					 glu::TextureBuffer&	texture)
4703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
4713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const size_t				minSize		= 4*16;
4723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const size_t				size		= de::max<size_t>(minSize, size_t((texture.getSize() != 0 ? texture.getSize() : texture.getBufferSize()) * (0.7 + 0.3 * rng.getFloat())));
4733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const size_t				minOffset	= texture.getOffset();
4743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const size_t				offset		= minOffset + (rng.getUint32() % (texture.getBufferSize() - (size + minOffset)));
4753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	vector<deUint8>				data;
4763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	genRandomCoords(rng, data, offset, size);
4783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	log << TestLog::Message << "glMapBufferRange, Write Only, Offset: " << offset << ", Size: " << size << TestLog::EndMessage;
4803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	gl.bindBuffer(GL_TEXTURE_BUFFER, texture.getGLBuffer());
4823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
4833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		deUint8* ptr = (deUint8*)gl.mapBufferRange(GL_TEXTURE_BUFFER, (glw::GLsizei)offset, (glw::GLsizei)size, GL_MAP_WRITE_BIT);
4843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		GLU_EXPECT_NO_ERROR(gl.getError(), "glMapBufferRange()");
4863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		TCU_CHECK(ptr);
4873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int i = 0; i < (int)data.size(); i++)
4893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			ptr[i] = data[i];
4903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		TCU_CHECK(gl.unmapBuffer(GL_TEXTURE_BUFFER));
4923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
4933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	gl.bindBuffer(GL_TEXTURE_BUFFER, 0);
4943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to update data with glMapBufferRange()");
4953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deMemcpy(texture.getRefBuffer()+offset, &(data[0]), int(data.size()));
4973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4993c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid modifyMapReadWrite (TestLog&				log,
5003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry						 tcu::ResultCollector&	resultCollector,
5013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry						 de::Random&			rng,
5023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry						 const glw::Functions&	gl,
5033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry						 glu::TextureBuffer&	texture)
5043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
5053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const size_t				minSize		= 4*16;
5063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const size_t				size		= de::max<size_t>(minSize, size_t((texture.getSize() != 0 ? texture.getSize() : texture.getBufferSize()) * (0.7 + 0.3 * rng.getFloat())));
5073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const size_t				minOffset	= texture.getOffset();
5083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const size_t				offset		= minOffset + (rng.getUint32() % (texture.getBufferSize() - (size + minOffset)));
5093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	vector<deUint8>				data;
5103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	genRandomCoords(rng, data, offset, size);
5123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	log << TestLog::Message << "glMapBufferRange, Read Write, Offset: " << offset << ", Size: " << size << TestLog::EndMessage;
5143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	gl.bindBuffer(GL_TEXTURE_BUFFER, texture.getGLBuffer());
5163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
5173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		size_t			invalidBytes	= 0;
5183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		deUint8* const	ptr				= (deUint8*)gl.mapBufferRange(GL_TEXTURE_BUFFER, (glw::GLsizei)offset, (glw::GLsizei)size, GL_MAP_WRITE_BIT|GL_MAP_READ_BIT);
5193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		GLU_EXPECT_NO_ERROR(gl.getError(), "glMapBufferRange()");
5213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		TCU_CHECK(ptr);
5223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int i = 0; i < (int)data.size(); i++)
5243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
5253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (ptr[i] != texture.getRefBuffer()[offset+i])
5263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
5273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				if (invalidBytes < 24)
5283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					log << TestLog::Message << "Invalid byte in mapped buffer. " << tcu::Format::Hex<2>(data[i]).toString() << " at " << i << ", expected " << tcu::Format::Hex<2>(texture.getRefBuffer()[i]).toString() << TestLog::EndMessage;
5293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				invalidBytes++;
5313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
5323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			ptr[i] = data[i];
5343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
5353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		TCU_CHECK(gl.unmapBuffer(GL_TEXTURE_BUFFER));
5373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (invalidBytes > 0)
5393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
5403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			log << TestLog::Message << "Total of " << invalidBytes << " invalid bytes." << TestLog::EndMessage;
5413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			resultCollector.fail("Invalid data in mapped buffer");
5423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
5433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
5443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	gl.bindBuffer(GL_TEXTURE_BUFFER, 0);
5463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	GLU_EXPECT_NO_ERROR(gl.getError(), "Failed to update data with glMapBufferRange()");
5473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < (int)data.size(); i++)
5493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		texture.getRefBuffer()[offset+i] = data[i];
5503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
5513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5523c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid modify (TestLog&						log,
5533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			 tcu::ResultCollector&			resultCollector,
5543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			 glu::RenderContext&			renderContext,
5553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			 ModifyBits						modifyBits,
5563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			 de::Random&					rng,
5573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			 glu::TextureBuffer&			texture)
5583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
5593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const tcu::ScopedLogSection modifySection(log, "Modifying Texture buffer", "Modifying Texture Buffer");
5603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	logModifications(log, modifyBits);
5623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (modifyBits & MODIFYBITS_BUFFERDATA)
5643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		modifyBufferData(log, rng, texture);
5653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (modifyBits & MODIFYBITS_BUFFERSUBDATA)
5673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		modifyBufferSubData(log, rng, renderContext.getFunctions(), texture);
5683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (modifyBits & MODIFYBITS_MAPBUFFER_WRITE)
5703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		modifyMapWrite(log, rng, renderContext.getFunctions(), texture);
5713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (modifyBits & MODIFYBITS_MAPBUFFER_READWRITE)
5733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		modifyMapReadWrite(log, resultCollector, rng, renderContext.getFunctions(), texture);
5743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
5753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5763c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid renderGL (glu::RenderContext&		renderContext,
5773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			   RenderBits				renderBits,
5783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			   deUint32					coordSeed,
5793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			   int						triangleCount,
5803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			   glu::ShaderProgram&		program,
5813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			   glu::TextureBuffer&		texture)
5823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
5833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const glw::Functions&	gl			= renderContext.getFunctions();
5843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const glu::VertexArray	vao			(renderContext);
5853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const glu::Buffer		coordBuffer	(renderContext);
5863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	gl.useProgram(program.getProgram());
5883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	gl.bindVertexArray(*vao);
5893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	gl.enableVertexAttribArray(0);
5913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
5923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (renderBits & RENDERBITS_AS_VERTEX_ARRAY)
5933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
5943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.bindBuffer(GL_ARRAY_BUFFER, texture.getGLBuffer());
5953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.vertexAttribPointer(0, 2, GL_UNSIGNED_BYTE, true, 0, DE_NULL);
5963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
5973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else
5983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
5993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		de::Random		rng(coordSeed);
6003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		vector<deUint8> coords;
6013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		genRandomCoords(rng, coords, 0, 256*2);
6033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.bindBuffer(GL_ARRAY_BUFFER, *coordBuffer);
6053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.bufferData(GL_ARRAY_BUFFER, (glw::GLsizei)coords.size(), &(coords[0]), GL_STREAM_DRAW);
6063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.vertexAttribPointer(0, 2, GL_UNSIGNED_BYTE, true, 0, DE_NULL);
6073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
6083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (renderBits & RENDERBITS_AS_VERTEX_TEXTURE)
6103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
6113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const deInt32 location = gl.getUniformLocation(program.getProgram(), "u_vtxSampler");
6123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.activeTexture(GL_TEXTURE0);
6143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.bindTexture(GL_TEXTURE_BUFFER, texture.getGLTexture());
6153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.uniform1i(location, 0);
6163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
6173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (renderBits & RENDERBITS_AS_FRAGMENT_TEXTURE)
6193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
6203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const deInt32 location = gl.getUniformLocation(program.getProgram(), "u_fragSampler");
6213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.activeTexture(GL_TEXTURE1);
6233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.bindTexture(GL_TEXTURE_BUFFER, texture.getGLTexture());
6243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.uniform1i(location, 1);
6253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.activeTexture(GL_TEXTURE0);
6263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
6273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (renderBits & RENDERBITS_AS_INDEX_ARRAY)
6293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
6303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.bindBuffer(GL_ELEMENT_ARRAY_BUFFER, texture.getGLBuffer());
6313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.drawElements(GL_TRIANGLES, triangleCount * 3, GL_UNSIGNED_BYTE, DE_NULL);
6323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.bindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
6333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
6343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else
6353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.drawArrays(GL_TRIANGLES, 0, triangleCount * 3);
6363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (renderBits & RENDERBITS_AS_FRAGMENT_TEXTURE)
6383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
6393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.activeTexture(GL_TEXTURE1);
6403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.bindTexture(GL_TEXTURE_BUFFER, 0);
6413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
6423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (renderBits & RENDERBITS_AS_VERTEX_TEXTURE)
6443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
6453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.activeTexture(GL_TEXTURE0);
6463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.bindTexture(GL_TEXTURE_BUFFER, 0);
6473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
6483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	gl.bindBuffer(GL_ARRAY_BUFFER, 0);
6503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	gl.disableVertexAttribArray(0);
6513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	gl.bindVertexArray(0);
6533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	gl.useProgram(0);
6543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	GLU_EXPECT_NO_ERROR(gl.getError(), "Rendering failed");
6553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
6563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6573c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid renderReference (RenderBits					renderBits,
6583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					  deUint32						coordSeed,
6593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					  int							triangleCount,
6603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					  glu::TextureBuffer&			texture,
6613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					  const tcu::PixelBufferAccess&	target)
6623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
6633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const CoordVertexShader			coordVertexShader;
6643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const TextureVertexShader		textureVertexShader		(texture.getRefTexture());
6653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const rr::VertexShader* const	vertexShader			= (renderBits & RENDERBITS_AS_VERTEX_TEXTURE ? static_cast<const rr::VertexShader*>(&textureVertexShader) : &coordVertexShader);
6663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const CoordFragmentShader		coordFragmmentShader;
6683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const TextureFragmentShader		textureFragmentShader	(texture.getRefTexture());
6693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const rr::FragmentShader* const	fragmentShader			= (renderBits & RENDERBITS_AS_FRAGMENT_TEXTURE ? static_cast<const rr::FragmentShader*>(&textureFragmentShader) : &coordFragmmentShader);
6703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const rr::Renderer				renderer;
6723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const rr::RenderState			renderState(rr::ViewportState(rr::WindowRectangle(0, 0, target.getWidth(), target.getHeight())));
6733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const rr::RenderTarget			renderTarget(rr::MultisamplePixelBufferAccess::fromSinglesampleAccess(target));
6743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const rr::Program				program(vertexShader, fragmentShader);
6763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	rr::VertexAttrib				vertexAttribs[1];
6783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	vector<deUint8>					coords;
6793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (renderBits & RENDERBITS_AS_VERTEX_ARRAY)
6813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
6823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		vertexAttribs[0].type			= rr::VERTEXATTRIBTYPE_NONPURE_UNORM8;
6833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		vertexAttribs[0].size			= 2;
6843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		vertexAttribs[0].pointer		= texture.getRefBuffer();
6853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
6863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else
6873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
6883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		de::Random rng(coordSeed);
6893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		genRandomCoords(rng, coords, 0, 256*2);
6913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		vertexAttribs[0].type			= rr::VERTEXATTRIBTYPE_NONPURE_UNORM8;
6933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		vertexAttribs[0].size			= 2;
6943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		vertexAttribs[0].pointer		= &(coords[0]);
6953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
6963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
6973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (renderBits & RENDERBITS_AS_INDEX_ARRAY)
6983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
6993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const rr::PrimitiveList	primitives(rr::PRIMITIVETYPE_TRIANGLES, triangleCount * 3, rr::DrawIndices((const void*)texture.getRefBuffer(), rr::INDEXTYPE_UINT8));
7003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const rr::DrawCommand	cmd(renderState, renderTarget, program, 1, vertexAttribs, primitives);
7013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		renderer.draw(cmd);
7033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
7043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else
7053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
7063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const rr::PrimitiveList	primitives(rr::PRIMITIVETYPE_TRIANGLES, triangleCount * 3, 0);
7073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const rr::DrawCommand	cmd(renderState, renderTarget, program, 1, vertexAttribs, primitives);
7083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		renderer.draw(cmd);
7103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
7113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
7123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7133c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid logRendering (TestLog& log, RenderBits renderBits)
7143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
7153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const struct
7163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
7173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		RenderBits	bit;
7183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const char*	str;
7193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	} bitInfos[] =
7203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
7213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{ RENDERBITS_AS_VERTEX_ARRAY,		"vertex array"		},
7223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{ RENDERBITS_AS_INDEX_ARRAY,		"index array"		},
7233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{ RENDERBITS_AS_VERTEX_TEXTURE,		"vertex texture"	},
7243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{ RENDERBITS_AS_FRAGMENT_TEXTURE,	"fragment texture"	}
7253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	};
7263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	std::ostringstream	stream;
7283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	vector<const char*> usedAs;
7293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_ASSERT(renderBits != 0);
7313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int infoNdx = 0; infoNdx < DE_LENGTH_OF_ARRAY(bitInfos); infoNdx++)
7333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
7343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (renderBits & bitInfos[infoNdx].bit)
7353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			usedAs.push_back(bitInfos[infoNdx].str);
7363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
7373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	stream << "Render using texture buffer as ";
7393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int asNdx = 0; asNdx < (int)usedAs.size(); asNdx++)
7413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
7423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (asNdx+1 == (int)usedAs.size() && (int)usedAs.size() > 1)
7433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			stream << " and ";
7443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		else if (asNdx > 0)
7453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			stream << ", ";
7463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		stream << usedAs[asNdx];
7483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
7493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	stream << ".";
7513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	log << TestLog::Message << stream.str() << TestLog::EndMessage;
7533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
7543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7553c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid render (TestLog&						log,
7563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			 glu::RenderContext&			renderContext,
7573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			 RenderBits						renderBits,
7583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			 de::Random&					rng,
7593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			 glu::ShaderProgram&			program,
7603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			 glu::TextureBuffer&			texture,
7613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			 const tcu::PixelBufferAccess&	target)
7623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
7633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const tcu::ScopedLogSection	renderSection	(log, "Render Texture buffer", "Render Texture Buffer");
7643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const int					triangleCount	= 8;
7653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const deUint32				coordSeed		= rng.getUint32();
7663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	logRendering(log, renderBits);
7683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	renderGL(renderContext, renderBits, coordSeed, triangleCount, program, texture);
7703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	renderReference(renderBits, coordSeed, triangleCount, texture, target);
7713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
7723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7733c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid verifyScreen (TestLog&								log,
7743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				   tcu::ResultCollector&				resultCollector,
7753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				   glu::RenderContext&					renderContext,
7763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				   const tcu::ConstPixelBufferAccess&	referenceTarget)
7773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
7783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const tcu::ScopedLogSection	verifySection	(log, "Verify screen contents", "Verify screen contents");
7793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	tcu::Surface				screen			(referenceTarget.getWidth(), referenceTarget.getHeight());
7803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	glu::readPixels(renderContext, 0, 0, screen.getAccess());
7823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (!tcu::fuzzyCompare(log, "Result of rendering", "Result of rendering", referenceTarget, screen.getAccess(), 0.05f, tcu::COMPARE_LOG_RESULT))
7843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		resultCollector.fail("Rendering failed");
7853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
7863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7873c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid logImplementationInfo (TestLog& log, glu::RenderContext& renderContext)
7883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
7893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const tcu::ScopedLogSection		section	(log, "Implementation Values", "Implementation Values");
7903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	de::UniquePtr<glu::ContextInfo>	info	(glu::ContextInfo::create(renderContext));
7913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const glw::Functions&			gl		= renderContext.getFunctions();
7923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (glu::contextSupports(renderContext.getType(), glu::ApiType(3, 3, glu::PROFILE_CORE)))
7943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
7953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		deInt32 maxTextureSize;
7963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
7973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.getIntegerv(GL_MAX_TEXTURE_BUFFER_SIZE, &maxTextureSize);
7983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		GLU_EXPECT_NO_ERROR(gl.getError(), "glGetIntegerv(GL_MAX_TEXTURE_BUFFER_SIZE)");
7993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		log << TestLog::Message << "GL_MAX_TEXTURE_BUFFER_SIZE : " <<  maxTextureSize << TestLog::EndMessage;
8013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
8023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else if (glu::contextSupports(renderContext.getType(), glu::ApiType(3, 1, glu::PROFILE_ES)) && info->isExtensionSupported("GL_EXT_texture_buffer"))
8033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
8043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
8053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			deInt32 maxTextureSize;
8063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			gl.getIntegerv(GL_MAX_TEXTURE_BUFFER_SIZE, &maxTextureSize);
8083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			GLU_EXPECT_NO_ERROR(gl.getError(), "glGetIntegerv(GL_MAX_TEXTURE_BUFFER_SIZE_EXT)");
8093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			log << TestLog::Message << "GL_MAX_TEXTURE_BUFFER_SIZE_EXT : " <<  maxTextureSize << TestLog::EndMessage;
8113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
8123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
8143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			deInt32 textureBufferAlignment;
8153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			gl.getIntegerv(GL_TEXTURE_BUFFER_OFFSET_ALIGNMENT, &textureBufferAlignment);
8173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			GLU_EXPECT_NO_ERROR(gl.getError(), "glGetIntegerv(GL_TEXTURE_BUFFER_OFFSET_ALIGNMENT_EXT)");
8183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			log << TestLog::Message << "GL_TEXTURE_BUFFER_OFFSET_ALIGNMENT_EXT : " <<  textureBufferAlignment << TestLog::EndMessage;
8203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
8213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
8223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else
8233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_ASSERT(DE_FALSE);
8243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
8253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8263c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid logTextureInfo (TestLog&	log,
8273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					 deUint32	format,
8283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					 size_t		bufferSize,
8293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					 size_t		offset,
8303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					 size_t		size)
8313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
8323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const tcu::ScopedLogSection	section(log, "Texture Info", "Texture Info");
8333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	log << TestLog::Message << "Texture format : " << glu::getPixelFormatStr(format) << TestLog::EndMessage;
8353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	log << TestLog::Message << "Buffer size : " << bufferSize << TestLog::EndMessage;
8363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (offset != 0 || size != 0)
8383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
8393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		log << TestLog::Message << "Buffer range offset: " << offset << TestLog::EndMessage;
8403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		log << TestLog::Message << "Buffer range size: " << size << TestLog::EndMessage;
8413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
8423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
8433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8443c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid runTests (tcu::TestContext&	testCtx,
8453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			   glu::RenderContext&	renderContext,
8463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			   de::Random&			rng,
8473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			   deUint32				format,
8483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			   size_t				bufferSize,
8493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			   size_t				offset,
8503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			   size_t				size,
8513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			   RenderBits			preRender,
8523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			   glu::ShaderProgram*	preRenderProgram,
8533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			   ModifyBits			modifyType,
8543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			   RenderBits			postRender,
8553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			   glu::ShaderProgram*	postRenderProgram)
8563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
8573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const tcu::RenderTarget	renderTarget	(renderContext.getRenderTarget());
8583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const glw::Functions&	gl				= renderContext.getFunctions();
8593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const int				width			= de::min<int>(renderTarget.getWidth(), MAX_VIEWPORT_WIDTH);
8613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const int				height			= de::min<int>(renderTarget.getHeight(), MAX_VIEWPORT_HEIGHT);
8623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const tcu::Vec4			clearColor		(0.25f, 0.5f, 0.75f, 1.0f);
8633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	TestLog&				log				= testCtx.getLog();
8653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	tcu::ResultCollector	resultCollector	(log);
8663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	logImplementationInfo(log, renderContext);
8683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	logTextureInfo(log, format, bufferSize, offset, size);
8693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
8713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		tcu::Surface			referenceTarget	(width, height);
8723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		vector<deUint8>			bufferData;
8733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		genRandomCoords(rng, bufferData, 0, bufferSize);
8753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (deUint8 i = 0; i < 4; i++)
8773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
8783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const deUint8 val = extend2BitsToByte(i);
8793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (val >= offset && val < offset + size)
8813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
8823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				bufferData[val*2 + 0] = (i / 2 == 0 ? extend2BitsToByte(0x2u) : extend2BitsToByte(0x01u));
8833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				bufferData[val*2 + 1] = (i % 2 == 0 ? extend2BitsToByte(0x2u) : extend2BitsToByte(0x01u));
8843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
8853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
8863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
8883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			glu::TextureBuffer texture (renderContext, format, bufferSize, offset, size, &(bufferData[0]));
8893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			TCU_CHECK_MSG(width >= MIN_VIEWPORT_WIDTH || height >= MIN_VIEWPORT_HEIGHT, "Too small viewport");
8913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_ASSERT(preRender == 0 || preRenderProgram);
8933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_ASSERT(postRender == 0 || postRenderProgram);
8943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
8953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			gl.viewport(0, 0, width, height);
8963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			gl.clearColor(clearColor.x(), clearColor.y(), clearColor.z(), clearColor.w());
8973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			gl.clear(GL_COLOR_BUFFER_BIT);
8983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			GLU_EXPECT_NO_ERROR(gl.getError(), "Screen setup failed");
8993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			tcu::clear(referenceTarget.getAccess(), clearColor);
9013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			texture.upload();
9033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (preRender != 0)
9053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				render(log, renderContext, preRender, rng, *preRenderProgram, texture, referenceTarget.getAccess());
9063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (modifyType != 0)
9083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				modify(log, resultCollector, renderContext, modifyType, rng, texture);
9093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (postRender != 0)
9113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				render(log, renderContext, postRender, rng, *postRenderProgram, texture, referenceTarget.getAccess());
9123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
9133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		verifyScreen(log, resultCollector, renderContext, referenceTarget.getAccess());
9153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		resultCollector.setTestContextResult(testCtx);
9173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
9183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
9193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // anonymous
9213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9223c827367444ee418f129b2c238299f49d3264554Jarkko PoyryTextureBufferCase::TextureBufferCase (tcu::TestContext&		testCtx,
9233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry									  glu::RenderContext&	renderCtx,
9243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry									  deUint32				format,
9253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry									  size_t				bufferSize,
9263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry									  size_t				offset,
9273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry									  size_t				size,
9283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry									  RenderBits			preRender,
9293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry									  ModifyBits			modify,
9303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry									  RenderBits			postRender,
9313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry									  const char*			name,
9323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry									  const char*			description)
9333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	: tcu::TestCase				(testCtx, name, description)
9343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_renderCtx				(renderCtx)
9353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_format					(format)
9363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_bufferSize				(bufferSize)
9373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_offset					(offset)
9383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_size					(size)
9393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_preRender				(preRender)
9413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_modify					(modify)
9423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_postRender				(postRender)
9433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_preRenderProgram		(DE_NULL)
9453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_postRenderProgram		(DE_NULL)
9463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
9473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
9483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9493c827367444ee418f129b2c238299f49d3264554Jarkko PoyryTextureBufferCase::~TextureBufferCase (void)
9503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
9513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	TextureBufferCase::deinit();
9523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
9533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9543c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid TextureBufferCase::init (void)
9553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
9563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	de::UniquePtr<glu::ContextInfo> info (glu::ContextInfo::create(m_renderCtx));
9573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (!glu::contextSupports(m_renderCtx.getType(), glu::ApiType(3, 3, glu::PROFILE_CORE))
9593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		&& !(glu::contextSupports(m_renderCtx.getType(), glu::ApiType(3, 1, glu::PROFILE_ES)) && info->isExtensionSupported("GL_EXT_texture_buffer")))
9603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		throw tcu::NotSupportedError("Texture buffers not supported", "", __FILE__, __LINE__);
9613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (m_preRender != 0)
9633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
9643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		TestLog&					log			= m_testCtx.getLog();
9653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const char* const			sectionName	= (m_postRender != 0 ? "Primary render program" : "Render program");
9663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const tcu::ScopedLogSection	section		(log, sectionName, sectionName);
9673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_preRenderProgram = createRenderProgram(m_renderCtx, m_preRender);
9693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_testCtx.getLog() << (*m_preRenderProgram);
9703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		TCU_CHECK(m_preRenderProgram->isOk());
9723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
9733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (m_postRender != 0)
9753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
9763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Reusing program
9773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (m_preRender == m_postRender)
9783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
9793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			m_postRenderProgram = m_preRenderProgram;
9803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
9813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		else
9823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
9833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			TestLog&					log			= m_testCtx.getLog();
9843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const char* const			sectionName	= (m_preRender!= 0 ? "Secondary render program" : "Render program");
9853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const tcu::ScopedLogSection	section		(log, sectionName, sectionName);
9863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			m_postRenderProgram = createRenderProgram(m_renderCtx, m_postRender);
9883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			m_testCtx.getLog() << (*m_postRenderProgram);
9893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			TCU_CHECK(m_postRenderProgram->isOk());
9913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
9923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
9933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
9943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
9953c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid TextureBufferCase::deinit (void)
9963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
9973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (m_preRenderProgram == m_postRenderProgram)
9983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_postRenderProgram = DE_NULL;
9993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
10003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	delete m_preRenderProgram;
10013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_preRenderProgram = DE_NULL;
10023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
10033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	delete m_postRenderProgram;
10043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_postRenderProgram = DE_NULL;
10053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
10063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
10073c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytcu::TestCase::IterateResult TextureBufferCase::iterate (void)
10083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
10093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	de::Random	rng		(deInt32Hash(deStringHash(getName())));
10103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	size_t		offset;
10113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
10123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (m_offset != 0)
10133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
10143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const glw::Functions&	gl			= m_renderCtx.getFunctions();
10153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		deInt32					alignment	= 0;
10163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
10173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		gl.getIntegerv(GL_TEXTURE_BUFFER_OFFSET_ALIGNMENT, &alignment);
10183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		GLU_EXPECT_NO_ERROR(gl.getError(), "glGetIntegerv(GL_TEXTURE_BUFFER_OFFSET_ALIGNMENT)");
10193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
10203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		offset = m_offset * alignment;
10213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
10223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else
10233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		offset = 0;
10243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
10253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	runTests(m_testCtx, m_renderCtx, rng, m_format, m_bufferSize, offset, m_size, m_preRender, m_preRenderProgram, m_modify, m_postRender, m_postRenderProgram);
10263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
10273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return STOP;
10283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
10293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
10303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // gls
10313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // deqp
1032