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 sglr-rsg adaptation.
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "glsRandomShaderProgram.hpp"
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "rsgShader.hpp"
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace deqp
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace gls
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing std::vector;
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic rr::GenericVecType mapToGenericVecType (const rsg::VariableType& varType)
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (varType.isFloatOrVec())
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return rr::GENERICVECTYPE_FLOAT;
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else if (varType.isIntOrVec())
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return rr::GENERICVECTYPE_INT32;
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_ASSERT(false);
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return rr::GENERICVECTYPE_LAST;
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic glu::DataType mapToBasicType (const rsg::VariableType& varType)
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (varType.isFloatOrVec() || varType.isIntOrVec() || varType.isBoolOrVec())
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const glu::DataType		scalarType		= varType.isFloatOrVec()	? glu::TYPE_FLOAT	:
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry												  varType.isIntOrVec()		? glu::TYPE_INT		:
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry												  varType.isBoolOrVec()		? glu::TYPE_BOOL	: glu::TYPE_LAST;
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const int				numComps		= varType.getNumElements();
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_ASSERT(de::inRange(numComps, 1, 4));
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return glu::DataType(scalarType + numComps - 1);
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else if (varType.getBaseType() == rsg::VariableType::TYPE_SAMPLER_2D)
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return glu::TYPE_SAMPLER_2D;
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else if (varType.getBaseType() == rsg::VariableType::TYPE_SAMPLER_CUBE)
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return glu::TYPE_SAMPLER_CUBE;
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	else
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_ASSERT(false);
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		return glu::TYPE_LAST;
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic void generateProgramDeclaration (sglr::pdec::ShaderProgramDeclaration& decl, const rsg::Shader& vertexShader, const rsg::Shader& fragmentShader, int numUnifiedUniforms, const rsg::ShaderInput* const* unifiedUniforms)
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	decl << sglr::pdec::VertexSource(vertexShader.getSource())
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		 << sglr::pdec::FragmentSource(fragmentShader.getSource());
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (vector<rsg::ShaderInput*>::const_iterator vtxInIter = vertexShader.getInputs().begin(); vtxInIter != vertexShader.getInputs().end(); ++vtxInIter)
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const rsg::ShaderInput*	vertexInput	= *vtxInIter;
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		decl << sglr::pdec::VertexAttribute(vertexInput->getVariable()->getName(), mapToGenericVecType(vertexInput->getVariable()->getType()));
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (vector<rsg::ShaderInput*>::const_iterator fragInIter = fragmentShader.getInputs().begin(); fragInIter != fragmentShader.getInputs().end(); ++fragInIter)
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const rsg::ShaderInput*	fragInput	= *fragInIter;
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		decl << sglr::pdec::VertexToFragmentVarying(mapToGenericVecType(fragInput->getVariable()->getType()));
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int uniformNdx = 0; uniformNdx < numUnifiedUniforms; uniformNdx++)
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const rsg::ShaderInput*	uniform	= unifiedUniforms[uniformNdx];
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		decl << sglr::pdec::Uniform(uniform->getVariable()->getName(), mapToBasicType(uniform->getVariable()->getType()));
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	decl << sglr::pdec::FragmentOutput(rr::GENERICVECTYPE_FLOAT);
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic sglr::pdec::ShaderProgramDeclaration generateProgramDeclaration (const rsg::Shader& vertexShader, const rsg::Shader& fragmentShader, int numUnifiedUniforms, const rsg::ShaderInput* const* unifiedUniforms)
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	sglr::pdec::ShaderProgramDeclaration decl;
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	generateProgramDeclaration(decl, vertexShader, fragmentShader, numUnifiedUniforms, unifiedUniforms);
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return decl;
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic const rsg::Variable* findShaderOutputByName (const rsg::Shader& shader, const char* name)
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	vector<const rsg::Variable*> outputs;
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	shader.getOutputs(outputs);
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (vector<const rsg::Variable*>::const_iterator iter = outputs.begin(); iter != outputs.end(); ++iter)
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (deStringEqual((*iter)->getName(), name))
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			return *iter;
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return DE_NULL;
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystatic const rsg::Variable* findShaderOutputByLocation (const rsg::Shader& shader, int location)
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	vector<const rsg::Variable*> outputs;
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	shader.getOutputs(outputs);
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (vector<const rsg::Variable*>::const_iterator iter = outputs.begin(); iter != outputs.end(); iter++)
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if ((*iter)->getLayoutLocation() == location)
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			return *iter;
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return DE_NULL;
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1313c827367444ee418f129b2c238299f49d3264554Jarkko PoyryRandomShaderProgram::RandomShaderProgram (const rsg::Shader& vertexShader, const rsg::Shader& fragmentShader, int numUnifiedUniforms, const rsg::ShaderInput* const* unifiedUniforms)
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	: sglr::ShaderProgram	(generateProgramDeclaration(vertexShader, fragmentShader, numUnifiedUniforms, unifiedUniforms))
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_vertexShader		(vertexShader)
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_fragmentShader		(fragmentShader)
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_numUnifiedUniforms	(numUnifiedUniforms)
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_unifiedUniforms		(unifiedUniforms)
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_positionVar			(findShaderOutputByName(vertexShader, "gl_Position"))
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_fragColorVar		(findShaderOutputByLocation(fragmentShader, 0))
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_execCtx				(m_sampler2DMap, m_samplerCubeMap)
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	TCU_CHECK_INTERNAL(m_positionVar && m_positionVar->getType().getBaseType() == rsg::VariableType::TYPE_FLOAT && m_positionVar->getType().getNumElements() == 4);
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	TCU_CHECK_INTERNAL(m_fragColorVar && m_fragColorVar->getType().getBaseType() == rsg::VariableType::TYPE_FLOAT && m_fragColorVar->getType().getNumElements() == 4);
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Build list of vertex outputs.
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (vector<rsg::ShaderInput*>::const_iterator fragInIter = fragmentShader.getInputs().begin(); fragInIter != fragmentShader.getInputs().end(); ++fragInIter)
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const rsg::ShaderInput*	fragInput		= *fragInIter;
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const rsg::Variable*	vertexOutput	= findShaderOutputByName(vertexShader, fragInput->getVariable()->getName());
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		TCU_CHECK_INTERNAL(vertexOutput);
1513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_vertexOutputs.push_back(vertexOutput);
1523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1553c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid RandomShaderProgram::refreshUniforms (void) const
1563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_ASSERT(m_numUnifiedUniforms == (int)m_uniforms.size());
1583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int uniformNdx = 0; uniformNdx < m_numUnifiedUniforms; uniformNdx++)
1603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const rsg::Variable*		uniformVar	= m_unifiedUniforms[uniformNdx]->getVariable();
1623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const rsg::VariableType&	uniformType	= uniformVar->getType();
1633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const sglr::UniformSlot&	uniformSlot	= m_uniforms[uniformNdx];
1643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_execCtx.getValue(uniformVar) = rsg::ConstValueAccess(uniformType, (const rsg::Scalar*)&uniformSlot.value).value();
1663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1693c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid RandomShaderProgram::shadeVertices (const rr::VertexAttrib* inputs, rr::VertexPacket* const* packets, const int numPackets) const
1703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// \todo [2013-12-13 pyry] Do only when necessary.
1723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	refreshUniforms();
1733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int packetOffset = 0;
1753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	while (packetOffset < numPackets)
1773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const int	numToExecute	= de::min(numPackets-packetOffset, (int)rsg::EXEC_VEC_WIDTH);
1793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Fetch attributes.
1813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int attribNdx = 0; attribNdx < (int)m_vertexShader.getInputs().size(); ++attribNdx)
1823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const rsg::Variable*		attribVar		= m_vertexShader.getInputs()[attribNdx]->getVariable();
1843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const rsg::VariableType&	attribType		= attribVar->getType();
1853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const int					numComponents	= attribType.getNumElements();
1863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			rsg::ExecValueAccess		access			= m_execCtx.getValue(attribVar);
1873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_ASSERT(attribType.isFloatOrVec() && de::inRange(numComponents, 1, 4));
1893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			for (int ndx = 0; ndx < numToExecute; ndx++)
1913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
1923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				const int				packetNdx	= ndx+packetOffset;
1933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				const rr::VertexPacket*	packet		= packets[packetNdx];
1943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				const tcu::Vec4			attribValue	= rr::readVertexAttribFloat(inputs[attribNdx], packet->instanceNdx, packet->vertexNdx);
1953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry										access.component(0).asFloat(ndx) = attribValue[0];
1973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				if (numComponents >= 2)	access.component(1).asFloat(ndx) = attribValue[1];
1983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				if (numComponents >= 3)	access.component(2).asFloat(ndx) = attribValue[2];
1993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				if (numComponents >= 4)	access.component(3).asFloat(ndx) = attribValue[3];
2003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
2013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
2023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_vertexShader.execute(m_execCtx);
2043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Store position
2063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
2073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const rsg::ExecConstValueAccess	access	= m_execCtx.getValue(m_positionVar);
2083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			for (int ndx = 0; ndx < numToExecute; ndx++)
2103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
2113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				const int			packetNdx	= ndx+packetOffset;
2123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				rr::VertexPacket*	packet		= packets[packetNdx];
2133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				packet->position[0] = access.component(0).asFloat(ndx);
2153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				packet->position[1] = access.component(1).asFloat(ndx);
2163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				packet->position[2] = access.component(2).asFloat(ndx);
2173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				packet->position[3] = access.component(3).asFloat(ndx);
2183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
2193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
2203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Other varyings
2223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int varNdx = 0; varNdx < (int)m_vertexOutputs.size(); varNdx++)
2233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
2243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const rsg::Variable*			var				= m_vertexOutputs[varNdx];
2253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const rsg::VariableType&		varType			= var->getType();
2263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const int						numComponents	= varType.getNumElements();
2273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const rsg::ExecConstValueAccess	access			= m_execCtx.getValue(var);
2283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_ASSERT(varType.isFloatOrVec() && de::inRange(numComponents, 1, 4));
2303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			for (int ndx = 0; ndx < numToExecute; ndx++)
2323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
2333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				const int				packetNdx	= ndx+packetOffset;
2343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				rr::VertexPacket* const	packet		= packets[packetNdx];
2353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				float* const			dst			= packet->outputs[varNdx].getAccess<float>();
2363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry										dst[0] = access.component(0).asFloat(ndx);
2383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				if (numComponents >= 2) dst[1] = access.component(1).asFloat(ndx);
2393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				if (numComponents >= 3) dst[2] = access.component(2).asFloat(ndx);
2403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				if (numComponents >= 4) dst[3] = access.component(3).asFloat(ndx);
2413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
2423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
2433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		packetOffset += numToExecute;
2453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
2473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2483c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid RandomShaderProgram::shadeFragments (rr::FragmentPacket* packets, const int numPackets, const rr::FragmentShadingContext& context) const
2493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
2503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const rsg::ExecConstValueAccess	fragColorAccess	= m_execCtx.getValue(m_fragColorVar);
2513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int								packetOffset	= 0;
2523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_STATIC_ASSERT(rsg::EXEC_VEC_WIDTH % rr::NUM_FRAGMENTS_PER_PACKET == 0);
2543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	while (packetOffset < numPackets)
2563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
2573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const int	numPacketsToExecute	= de::min(numPackets-packetOffset, (int)rsg::EXEC_VEC_WIDTH / (int)rr::NUM_FRAGMENTS_PER_PACKET);
2583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Interpolate varyings.
2603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int varNdx = 0; varNdx < (int)m_fragmentShader.getInputs().size(); ++varNdx)
2613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
2623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const rsg::Variable*		var				= m_fragmentShader.getInputs()[varNdx]->getVariable();
2633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const rsg::VariableType&	varType			= var->getType();
2643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			const int					numComponents	= varType.getNumElements();
2653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			rsg::ExecValueAccess		access			= m_execCtx.getValue(var);
2663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_ASSERT(varType.isFloatOrVec() && de::inRange(numComponents, 1, 4));
2683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			for (int packetNdx = 0; packetNdx < numPacketsToExecute; packetNdx++)
2703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
2713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				const rr::FragmentPacket&	packet		= packets[packetOffset+packetNdx];
2723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				for (int fragNdx = 0; fragNdx < rr::NUM_FRAGMENTS_PER_PACKET; fragNdx++)
2743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				{
2753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					const tcu::Vec4		varValue	= rr::readVarying<float>(packet, context, varNdx, fragNdx);
2763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					const int			dstNdx		= packetNdx*rr::NUM_FRAGMENTS_PER_PACKET + fragNdx;
2773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry											access.component(0).asFloat(dstNdx) = varValue[0];
2793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					if (numComponents >= 2)	access.component(1).asFloat(dstNdx) = varValue[1];
2803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					if (numComponents >= 3)	access.component(2).asFloat(dstNdx) = varValue[2];
2813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					if (numComponents >= 4)	access.component(3).asFloat(dstNdx) = varValue[3];
2823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				}
2833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
2843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
2853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_fragmentShader.execute(m_execCtx);
2873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		// Store color
2893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (int packetNdx = 0; packetNdx < numPacketsToExecute; packetNdx++)
2903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
2913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			for (int fragNdx = 0; fragNdx < rr::NUM_FRAGMENTS_PER_PACKET; fragNdx++)
2923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
2933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				const int		srcNdx	= packetNdx*rr::NUM_FRAGMENTS_PER_PACKET + fragNdx;
2943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				const tcu::Vec4 color	(fragColorAccess.component(0).asFloat(srcNdx),
2953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry										 fragColorAccess.component(1).asFloat(srcNdx),
2963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry										 fragColorAccess.component(2).asFloat(srcNdx),
2973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry										 fragColorAccess.component(3).asFloat(srcNdx));
2983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				rr::writeFragmentOutput(context, packetOffset+packetNdx, fragNdx, 0, color);
3003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
3013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
3023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		packetOffset += numPacketsToExecute;
3043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
3053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
3063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // gls
3083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // deqp
309