Home | History | Annotate | Download | only in randomshaders
      1 /*-------------------------------------------------------------------------
      2  * drawElements Quality Program Random Shader Generator
      3  * ----------------------------------------------------
      4  *
      5  * Copyright 2014 The Android Open Source Project
      6  *
      7  * Licensed under the Apache License, Version 2.0 (the "License");
      8  * you may not use this file except in compliance with the License.
      9  * You may obtain a copy of the License at
     10  *
     11  *      http://www.apache.org/licenses/LICENSE-2.0
     12  *
     13  * Unless required by applicable law or agreed to in writing, software
     14  * distributed under the License is distributed on an "AS IS" BASIS,
     15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16  * See the License for the specific language governing permissions and
     17  * limitations under the License.
     18  *
     19  *//*!
     20  * \file
     21  * \brief Shader Evaluation Context.
     22  *//*--------------------------------------------------------------------*/
     23 
     24 #include "rsgExecutionContext.hpp"
     25 
     26 namespace rsg
     27 {
     28 
     29 ExecMaskStorage::ExecMaskStorage (bool initVal)
     30 {
     31 	for (int i = 0; i < EXEC_VEC_WIDTH; i++)
     32 		m_data[i].as<bool>() = initVal;
     33 }
     34 
     35 ExecValueAccess ExecMaskStorage::getValue (void)
     36 {
     37 	return ExecValueAccess(VariableType::getScalarType(VariableType::TYPE_BOOL), m_data);
     38 }
     39 
     40 ExecConstValueAccess ExecMaskStorage::getValue (void) const
     41 {
     42 	return ExecConstValueAccess(VariableType::getScalarType(VariableType::TYPE_BOOL), m_data);
     43 }
     44 
     45 ExecutionContext::ExecutionContext (const Sampler2DMap& samplers2D, const SamplerCubeMap& samplersCube)
     46 	: m_samplers2D		(samplers2D)
     47 	, m_samplersCube	(samplersCube)
     48 {
     49 	// Initialize execution mask to true
     50 	ExecMaskStorage initVal(true);
     51 	pushExecutionMask(initVal.getValue());
     52 }
     53 
     54 ExecutionContext::~ExecutionContext (void)
     55 {
     56 	for (VarValueMap::iterator i = m_varValues.begin(); i != m_varValues.end(); i++)
     57 		delete i->second;
     58 	m_varValues.clear();
     59 }
     60 
     61 ExecValueAccess ExecutionContext::getValue (const Variable* variable)
     62 {
     63 	ExecValueStorage* storage = m_varValues[variable];
     64 
     65 	if (!storage)
     66 	{
     67 		storage = new ExecValueStorage(variable->getType());
     68 		m_varValues[variable] = storage;
     69 	}
     70 
     71 	return storage->getValue(variable->getType());
     72 }
     73 
     74 const Sampler2D& ExecutionContext::getSampler2D (const Variable* sampler) const
     75 {
     76 	const ExecValueStorage* samplerVal = m_varValues.find(sampler)->second;
     77 
     78 	int samplerNdx = samplerVal->getValue(sampler->getType()).asInt(0);
     79 
     80 	return m_samplers2D.find(samplerNdx)->second;
     81 }
     82 
     83 const SamplerCube& ExecutionContext::getSamplerCube (const Variable* sampler) const
     84 {
     85 	const ExecValueStorage* samplerVal = m_varValues.find(sampler)->second;
     86 
     87 	int samplerNdx = samplerVal->getValue(sampler->getType()).asInt(0);
     88 
     89 	return m_samplersCube.find(samplerNdx)->second;
     90 }
     91 
     92 void ExecutionContext::andExecutionMask (ExecConstValueAccess value)
     93 {
     94 	ExecMaskStorage			tmp;
     95 	ExecValueAccess			newValue	= tmp.getValue();
     96 	ExecConstValueAccess	oldValue	= getExecutionMask();
     97 
     98 	for (int i = 0; i < EXEC_VEC_WIDTH; i++)
     99 		newValue.asBool(i) = oldValue.asBool(i) && value.asBool(i);
    100 
    101 	pushExecutionMask(newValue);
    102 }
    103 
    104 void ExecutionContext::pushExecutionMask (ExecConstValueAccess value)
    105 {
    106 	ExecMaskStorage tmp;
    107 	tmp.getValue() = value.value();
    108 	m_execMaskStack.push_back(tmp);
    109 }
    110 
    111 void ExecutionContext::popExecutionMask (void)
    112 {
    113 	m_execMaskStack.pop_back();
    114 }
    115 
    116 ExecConstValueAccess ExecutionContext::getExecutionMask (void) const
    117 {
    118 	return m_execMaskStack[m_execMaskStack.size()-1].getValue();
    119 }
    120 
    121 void assignMasked (ExecValueAccess dst, ExecConstValueAccess src, ExecConstValueAccess mask)
    122 {
    123 	const VariableType& type = dst.getType();
    124 
    125 	switch (type.getBaseType())
    126 	{
    127 		case VariableType::TYPE_ARRAY:
    128 		{
    129 			int numElems = type.getNumElements();
    130 			for (int elemNdx = 0; elemNdx < numElems; elemNdx++)
    131 				assignMasked(dst.arrayElement(elemNdx), src.arrayElement(elemNdx), mask);
    132 
    133 			break;
    134 		}
    135 
    136 		case VariableType::TYPE_STRUCT:
    137 		{
    138 			int numMembers = (int)type.getMembers().size();
    139 			for (int memberNdx = 0; memberNdx < numMembers; memberNdx++)
    140 				assignMasked(dst.member(memberNdx), src.member(memberNdx), mask);
    141 
    142 			break;
    143 		}
    144 
    145 		case VariableType::TYPE_FLOAT:
    146 		case VariableType::TYPE_INT:
    147 		case VariableType::TYPE_BOOL:
    148 		case VariableType::TYPE_SAMPLER_2D:
    149 		case VariableType::TYPE_SAMPLER_CUBE:
    150 		{
    151 			for (int elemNdx = 0; elemNdx < type.getNumElements(); elemNdx++)
    152 			{
    153 				ExecValueAccess			dstElem		= dst.component(elemNdx);
    154 				ExecConstValueAccess	srcElem		= src.component(elemNdx);
    155 
    156 				for (int compNdx = 0; compNdx < EXEC_VEC_WIDTH; compNdx++)
    157 				{
    158 					if (mask.asBool(compNdx))
    159 						dstElem.asScalar(compNdx) = srcElem.asScalar(compNdx);
    160 				}
    161 			}
    162 
    163 			break;
    164 		}
    165 
    166 		default:
    167 			DE_ASSERT(!"Unsupported");
    168 			break;
    169 	}
    170 }
    171 
    172 } // rsg
    173