ronnie
2022-10-14 1504bb53e29d3d46222c0b3ea994fc494b48e153
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*-------------------------------------------------------------------------
 * drawElements Quality Program Random Shader Generator
 * ----------------------------------------------------
 *
 * Copyright 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *//*!
 * \file
 * \brief Shader Class.
 *//*--------------------------------------------------------------------*/
 
#include "rsgShader.hpp"
 
using std::vector;
 
namespace rsg
{
 
namespace
{
 
template <typename T>
void deleteVectorElements (std::vector<T*>& vec)
{
   for (typename std::vector<T*>::iterator i = vec.begin(); i != vec.end(); i++)
       delete *i;
   vec.clear();
}
 
} // anonymous
 
Function::Function (void)
{
}
 
Function::Function (const char* name)
   : m_name(name)
{
}
 
Function::~Function (void)
{
   deleteVectorElements(m_parameters);
}
 
ShaderInput::ShaderInput (const Variable* variable, ConstValueRangeAccess valueRange)
   : m_variable    (variable)
   , m_min            (variable->getType().getScalarSize())
   , m_max            (variable->getType().getScalarSize())
{
   ValueAccess(variable->getType(), &m_min[0]) = valueRange.getMin().value();
   ValueAccess(variable->getType(), &m_max[0]) = valueRange.getMax().value();
}
 
Shader::Shader (Type type)
   : m_type            (type)
   , m_mainFunction    ("main")
{
}
 
Shader::~Shader (void)
{
   deleteVectorElements(m_functions);
   deleteVectorElements(m_globalStatements);
   deleteVectorElements(m_inputs);
   deleteVectorElements(m_uniforms);
}
 
void Shader::getOutputs (vector<const Variable*>& outputs) const
{
   outputs.clear();
   const vector<Variable*>& globalVars = m_globalScope.getDeclaredVariables();
   for (vector<Variable*>::const_iterator i = globalVars.begin(); i != globalVars.end(); i++)
   {
       const Variable* var = *i;
       if (var->getStorage() == Variable::STORAGE_SHADER_OUT)
           outputs.push_back(var);
   }
}
 
void Shader::tokenize (GeneratorState& state, TokenStream& str) const
{
   // Add default precision for float in fragment shaders \todo [pyry] Proper precision handling
   if (state.getShader().getType() == Shader::TYPE_FRAGMENT)
       str << Token::PRECISION << Token::MEDIUM_PRECISION << Token::FLOAT << Token::SEMICOLON << Token::NEWLINE;
 
   // Tokenize global declaration statements
   for (int ndx = (int)m_globalStatements.size()-1; ndx >= 0; ndx--)
       m_globalStatements[ndx]->tokenize(state, str);
 
   // Tokenize all functions
   for (int ndx = (int)m_functions.size()-1; ndx >= 0; ndx--)
   {
       str << Token::NEWLINE;
       m_functions[ndx]->tokenize(state, str);
   }
 
   // Tokenize main
   str << Token::NEWLINE;
   m_mainFunction.tokenize(state, str);
}
 
void Shader::execute (ExecutionContext& execCtx) const
{
   // Execute global statements (declarations)
   for (vector<Statement*>::const_reverse_iterator i = m_globalStatements.rbegin(); i != m_globalStatements.rend(); i++)
       (*i)->execute(execCtx);
 
   // \todo [2011-03-08 pyry] Proper function calls
   m_mainFunction.getBody().execute(execCtx);
}
 
void Function::tokenize (GeneratorState& state, TokenStream& str) const
{
   // Return type
   m_returnType.tokenizeShortType(str);
 
   // Function name
   DE_ASSERT(m_name != "");
   str << Token(m_name.c_str());
 
   // Parameters
   str << Token::LEFT_PAREN;
 
   for (vector<Variable*>::const_iterator i = m_parameters.begin(); i != m_parameters.end(); i++)
   {
       if (i != m_parameters.begin())
           str << Token::COMMA;
       (*i)->tokenizeDeclaration(state, str);
   }
 
   str << Token::RIGHT_PAREN << Token::NEWLINE;
 
   // Tokenize body
   m_functionBlock.tokenize(state, str);
}
 
} // rsg