1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Random Shader Generator
3 * ----------------------------------------------------
5 * Copyright 2014 The Android Open Source Project
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * \brief Shader Class.
22 *//*--------------------------------------------------------------------*/
24 #include "rsgShader.hpp"
35 void deleteVectorElements (std::vector<T*>& vec)
37 for (typename std::vector<T*>::iterator i = vec.begin(); i != vec.end(); i++)
44 Function::Function (void)
48 Function::Function (const char* name)
53 Function::~Function (void)
55 deleteVectorElements(m_parameters);
58 ShaderInput::ShaderInput (const Variable* variable, ConstValueRangeAccess valueRange)
59 : m_variable (variable)
60 , m_min (variable->getType().getScalarSize())
61 , m_max (variable->getType().getScalarSize())
63 ValueAccess(variable->getType(), &m_min[0]) = valueRange.getMin().value();
64 ValueAccess(variable->getType(), &m_max[0]) = valueRange.getMax().value();
67 Shader::Shader (Type type)
69 , m_mainFunction ("main")
73 Shader::~Shader (void)
75 deleteVectorElements(m_functions);
76 deleteVectorElements(m_globalStatements);
77 deleteVectorElements(m_inputs);
78 deleteVectorElements(m_uniforms);
81 void Shader::getOutputs (vector<const Variable*>& outputs) const
84 const vector<Variable*>& globalVars = m_globalScope.getDeclaredVariables();
85 for (vector<Variable*>::const_iterator i = globalVars.begin(); i != globalVars.end(); i++)
87 const Variable* var = *i;
88 if (var->getStorage() == Variable::STORAGE_SHADER_OUT)
89 outputs.push_back(var);
93 void Shader::tokenize (GeneratorState& state, TokenStream& str) const
95 // Add default precision for float in fragment shaders \todo [pyry] Proper precision handling
96 if (state.getShader().getType() == Shader::TYPE_FRAGMENT)
97 str << Token::PRECISION << Token::MEDIUM_PRECISION << Token::FLOAT << Token::SEMICOLON << Token::NEWLINE;
99 // Tokenize global declaration statements
100 for (int ndx = (int)m_globalStatements.size()-1; ndx >= 0; ndx--)
101 m_globalStatements[ndx]->tokenize(state, str);
103 // Tokenize all functions
104 for (int ndx = (int)m_functions.size()-1; ndx >= 0; ndx--)
106 str << Token::NEWLINE;
107 m_functions[ndx]->tokenize(state, str);
111 str << Token::NEWLINE;
112 m_mainFunction.tokenize(state, str);
115 void Shader::execute (ExecutionContext& execCtx) const
117 // Execute global statements (declarations)
118 for (vector<Statement*>::const_reverse_iterator i = m_globalStatements.rbegin(); i != m_globalStatements.rend(); i++)
119 (*i)->execute(execCtx);
121 // \todo [2011-03-08 pyry] Proper function calls
122 m_mainFunction.getBody().execute(execCtx);
125 void Function::tokenize (GeneratorState& state, TokenStream& str) const
128 m_returnType.tokenizeShortType(str);
131 DE_ASSERT(m_name != "");
132 str << Token(m_name.c_str());
135 str << Token::LEFT_PAREN;
137 for (vector<Variable*>::const_iterator i = m_parameters.begin(); i != m_parameters.end(); i++)
139 if (i != m_parameters.begin())
141 (*i)->tokenizeDeclaration(state, str);
144 str << Token::RIGHT_PAREN << Token::NEWLINE;
147 m_functionBlock.tokenize(state, str);