13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements Quality Program Random Shader Generator
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 Shader Evaluation Context.
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "rsgExecutionContext.hpp"
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace rsg
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
293c827367444ee418f129b2c238299f49d3264554Jarkko PoyryExecMaskStorage::ExecMaskStorage (bool initVal)
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < EXEC_VEC_WIDTH; i++)
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_data[i].as<bool>() = initVal;
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
353c827367444ee418f129b2c238299f49d3264554Jarkko PoyryExecValueAccess ExecMaskStorage::getValue (void)
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return ExecValueAccess(VariableType::getScalarType(VariableType::TYPE_BOOL), m_data);
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
403c827367444ee418f129b2c238299f49d3264554Jarkko PoyryExecConstValueAccess ExecMaskStorage::getValue (void) const
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return ExecConstValueAccess(VariableType::getScalarType(VariableType::TYPE_BOOL), m_data);
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
453c827367444ee418f129b2c238299f49d3264554Jarkko PoyryExecutionContext::ExecutionContext (const Sampler2DMap& samplers2D, const SamplerCubeMap& samplersCube)
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	: m_samplers2D		(samplers2D)
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_samplersCube	(samplersCube)
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Initialize execution mask to true
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ExecMaskStorage initVal(true);
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	pushExecutionMask(initVal.getValue());
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
543c827367444ee418f129b2c238299f49d3264554Jarkko PoyryExecutionContext::~ExecutionContext (void)
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (VarValueMap::iterator i = m_varValues.begin(); i != m_varValues.end(); i++)
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		delete i->second;
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_varValues.clear();
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
613c827367444ee418f129b2c238299f49d3264554Jarkko PoyryExecValueAccess ExecutionContext::getValue (const Variable* variable)
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ExecValueStorage* storage = m_varValues[variable];
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (!storage)
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		storage = new ExecValueStorage(variable->getType());
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_varValues[variable] = storage;
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return storage->getValue(variable->getType());
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyryconst Sampler2D& ExecutionContext::getSampler2D (const Variable* sampler) const
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const ExecValueStorage* samplerVal = m_varValues.find(sampler)->second;
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int samplerNdx = samplerVal->getValue(sampler->getType()).asInt(0);
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return m_samplers2D.find(samplerNdx)->second;
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyryconst SamplerCube& ExecutionContext::getSamplerCube (const Variable* sampler) const
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const ExecValueStorage* samplerVal = m_varValues.find(sampler)->second;
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	int samplerNdx = samplerVal->getValue(sampler->getType()).asInt(0);
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return m_samplersCube.find(samplerNdx)->second;
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid ExecutionContext::andExecutionMask (ExecConstValueAccess value)
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ExecMaskStorage			tmp;
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ExecValueAccess			newValue	= tmp.getValue();
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ExecConstValueAccess	oldValue	= getExecutionMask();
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int i = 0; i < EXEC_VEC_WIDTH; i++)
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		newValue.asBool(i) = oldValue.asBool(i) && value.asBool(i);
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	pushExecutionMask(newValue);
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid ExecutionContext::pushExecutionMask (ExecConstValueAccess value)
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ExecMaskStorage tmp;
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	tmp.getValue() = value.value();
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_execMaskStack.push_back(tmp);
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid ExecutionContext::popExecutionMask (void)
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_execMaskStack.pop_back();
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1163c827367444ee418f129b2c238299f49d3264554Jarkko PoyryExecConstValueAccess ExecutionContext::getExecutionMask (void) const
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return m_execMaskStack[m_execMaskStack.size()-1].getValue();
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid assignMasked (ExecValueAccess dst, ExecConstValueAccess src, ExecConstValueAccess mask)
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const VariableType& type = dst.getType();
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	switch (type.getBaseType())
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		case VariableType::TYPE_ARRAY:
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			int numElems = type.getNumElements();
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			for (int elemNdx = 0; elemNdx < numElems; elemNdx++)
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				assignMasked(dst.arrayElement(elemNdx), src.arrayElement(elemNdx), mask);
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			break;
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		case VariableType::TYPE_STRUCT:
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			int numMembers = (int)type.getMembers().size();
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			for (int memberNdx = 0; memberNdx < numMembers; memberNdx++)
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				assignMasked(dst.member(memberNdx), src.member(memberNdx), mask);
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			break;
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		case VariableType::TYPE_FLOAT:
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		case VariableType::TYPE_INT:
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		case VariableType::TYPE_BOOL:
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		case VariableType::TYPE_SAMPLER_2D:
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		case VariableType::TYPE_SAMPLER_CUBE:
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			for (int elemNdx = 0; elemNdx < type.getNumElements(); elemNdx++)
1523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
1533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				ExecValueAccess			dstElem		= dst.component(elemNdx);
1543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				ExecConstValueAccess	srcElem		= src.component(elemNdx);
1553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				for (int compNdx = 0; compNdx < EXEC_VEC_WIDTH; compNdx++)
1573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				{
1583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					if (mask.asBool(compNdx))
1593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry						dstElem.asScalar(compNdx) = srcElem.asScalar(compNdx);
1603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				}
1613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
1623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			break;
1643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		default:
1673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_ASSERT(!"Unsupported");
1683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			break;
1693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // rsg
173