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 Class.
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "rsgShader.hpp"
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyryusing std::vector;
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace rsg
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytemplate <typename T>
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid deleteVectorElements (std::vector<T*>& vec)
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (typename std::vector<T*>::iterator i = vec.begin(); i != vec.end(); i++)
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		delete *i;
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	vec.clear();
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // anonymous
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
443c827367444ee418f129b2c238299f49d3264554Jarkko PoyryFunction::Function (void)
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
483c827367444ee418f129b2c238299f49d3264554Jarkko PoyryFunction::Function (const char* name)
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	: m_name(name)
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
533c827367444ee418f129b2c238299f49d3264554Jarkko PoyryFunction::~Function (void)
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deleteVectorElements(m_parameters);
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
583c827367444ee418f129b2c238299f49d3264554Jarkko PoyryShaderInput::ShaderInput (const Variable* variable, ConstValueRangeAccess valueRange)
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	: m_variable	(variable)
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_min			(variable->getType().getScalarSize())
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_max			(variable->getType().getScalarSize())
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ValueAccess(variable->getType(), &m_min[0]) = valueRange.getMin().value();
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	ValueAccess(variable->getType(), &m_max[0]) = valueRange.getMax().value();
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
673c827367444ee418f129b2c238299f49d3264554Jarkko PoyryShader::Shader (Type type)
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	: m_type			(type)
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	, m_mainFunction	("main")
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
733c827367444ee418f129b2c238299f49d3264554Jarkko PoyryShader::~Shader (void)
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deleteVectorElements(m_functions);
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deleteVectorElements(m_globalStatements);
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deleteVectorElements(m_inputs);
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	deleteVectorElements(m_uniforms);
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid Shader::getOutputs (vector<const Variable*>& outputs) const
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	outputs.clear();
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const vector<Variable*>& globalVars = m_globalScope.getDeclaredVariables();
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (vector<Variable*>::const_iterator i = globalVars.begin(); i != globalVars.end(); i++)
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		const Variable* var = *i;
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (var->getStorage() == Variable::STORAGE_SHADER_OUT)
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			outputs.push_back(var);
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid Shader::tokenize (GeneratorState& state, TokenStream& str) const
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Add default precision for float in fragment shaders \todo [pyry] Proper precision handling
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (state.getShader().getType() == Shader::TYPE_FRAGMENT)
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		str << Token::PRECISION << Token::MEDIUM_PRECISION << Token::FLOAT << Token::SEMICOLON << Token::NEWLINE;
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Tokenize global declaration statements
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int ndx = (int)m_globalStatements.size()-1; ndx >= 0; ndx--)
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_globalStatements[ndx]->tokenize(state, str);
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Tokenize all functions
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (int ndx = (int)m_functions.size()-1; ndx >= 0; ndx--)
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		str << Token::NEWLINE;
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_functions[ndx]->tokenize(state, str);
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Tokenize main
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	str << Token::NEWLINE;
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_mainFunction.tokenize(state, str);
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid Shader::execute (ExecutionContext& execCtx) const
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Execute global statements (declarations)
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (vector<Statement*>::const_reverse_iterator i = m_globalStatements.rbegin(); i != m_globalStatements.rend(); i++)
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		(*i)->execute(execCtx);
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// \todo [2011-03-08 pyry] Proper function calls
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_mainFunction.getBody().execute(execCtx);
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid Function::tokenize (GeneratorState& state, TokenStream& str) const
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Return type
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_returnType.tokenizeShortType(str);
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Function name
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DE_ASSERT(m_name != "");
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	str << Token(m_name.c_str());
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Parameters
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	str << Token::LEFT_PAREN;
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	for (vector<Variable*>::const_iterator i = m_parameters.begin(); i != m_parameters.end(); i++)
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (i != m_parameters.begin())
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			str << Token::COMMA;
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		(*i)->tokenizeDeclaration(state, str);
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	str << Token::RIGHT_PAREN << Token::NEWLINE;
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Tokenize body
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	m_functionBlock.tokenize(state, str);
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // rsg
151