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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*-------------------------------------------------------------------------
 * 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 generator.
 *//*--------------------------------------------------------------------*/
 
#include "rsgShaderGenerator.hpp"
#include "rsgFunctionGenerator.hpp"
#include "rsgToken.hpp"
#include "rsgPrettyPrinter.hpp"
#include "rsgUtils.hpp"
#include "deString.h"
 
#include <iterator>
 
using std::string;
using std::vector;
 
namespace rsg
{
 
ShaderGenerator::ShaderGenerator (GeneratorState& state)
   : m_state        (state)
   , m_varManager    (state.getNameAllocator())
{
   state.setVariableManager(m_varManager);
}
 
ShaderGenerator::~ShaderGenerator (void)
{
}
 
namespace
{
 
const char* getFragColorName (const GeneratorState& state)
{
   switch (state.getProgramParameters().version)
   {
       case VERSION_100:    return "gl_FragColor";
       case VERSION_300:    return "dEQP_FragColor";
       default:
           DE_ASSERT(DE_FALSE);
           return DE_NULL;
   }
}
 
void createAssignment (BlockStatement& block, const Variable* dstVar, const Variable* srcVar)
{
   VariableRead* varRead = new VariableRead(srcVar);
   try
   {
       block.addChild(new AssignStatement( dstVar, varRead));
   }
   catch (const std::exception&)
   {
       delete varRead;
       throw;
   }
}
 
const ValueEntry* findByName (VariableManager& varManager, const char* name)
{
   AnyEntry::Iterator    iter    = varManager.getBegin<AnyEntry>();
   AnyEntry::Iterator    end        = varManager.getEnd<AnyEntry>();
   for (; iter != end; iter++)
   {
       const ValueEntry* entry = *iter;
       if (deStringEqual(entry->getVariable()->getName(), name))
           return entry;
   }
   return DE_NULL;
}
 
void genVertexPassthrough (GeneratorState& state, Shader& shader)
{
   // Create copies from shader inputs to outputs
   vector<const ValueEntry*> entries;
   std::copy(state.getVariableManager().getBegin<AnyEntry>(), state.getVariableManager().getEnd<AnyEntry>(), std::inserter(entries, entries.begin()));
 
   for (vector<const ValueEntry*>::const_iterator i = entries.begin(); i != entries.end(); i++)
   {
       const ValueEntry*    entry        = *i;
       const Variable*        outVar        = entry->getVariable();
       std::string            inVarName;
 
       if (outVar->getStorage() != Variable::STORAGE_SHADER_OUT)
           continue;
 
       // Name: a_[name], remove v_ -prefix if such exists
       inVarName = "a_";
       if (deStringBeginsWith(outVar->getName(), "v_"))
           inVarName += (outVar->getName()+2);
       else
           inVarName += outVar->getName();
 
       Variable* inVar = state.getVariableManager().allocate(outVar->getType(), Variable::STORAGE_SHADER_IN, inVarName.c_str());
 
       // Update value range. This will be stored into shader input info.
       state.getVariableManager().setValue(inVar, entry->getValueRange());
 
       // Add assignment from input to output into main() body
       createAssignment(shader.getMain().getBody(), entry->getVariable(), inVar);
   }
}
 
void genFragmentPassthrough (GeneratorState& state, Shader& shader)
{
   // Add simple gl_FragColor = v_color; assignment
   const ValueEntry* fragColorEntry = findByName(state.getVariableManager(), getFragColorName(state));
   TCU_CHECK(fragColorEntry);
 
   Variable* inColorVariable = state.getVariableManager().allocate(fragColorEntry->getVariable()->getType(), Variable::STORAGE_SHADER_IN, "v_color");
 
   state.getVariableManager().setValue(inColorVariable, fragColorEntry->getValueRange());
   createAssignment(shader.getMain().getBody(), fragColorEntry->getVariable(), inColorVariable);
}
 
// Sets undefined (-inf..inf) components to some meaningful values. Used for sanitizing final shader input value ranges.
void fillUndefinedComponents (ValueRangeAccess valueRange)
{
   VariableType::Type baseType = valueRange.getType().getBaseType();
   TCU_CHECK(baseType == VariableType::TYPE_FLOAT    ||
             baseType == VariableType::TYPE_INT    ||
             baseType == VariableType::TYPE_BOOL);
 
   for (int elemNdx = 0; elemNdx < valueRange.getType().getNumElements(); elemNdx++)
   {
       if (isUndefinedValueRange(valueRange.component(elemNdx)))
       {
           ValueAccess min = valueRange.component(elemNdx).getMin();
           ValueAccess max = valueRange.component(elemNdx).getMax();
 
           switch (baseType)
           {
               case VariableType::TYPE_FLOAT:    min = 0.0f;        max = 1.0f;        break;
               case VariableType::TYPE_INT:    min = 0;        max = 1;        break;
               case VariableType::TYPE_BOOL:    min = false;    max = true;        break;
               default: DE_ASSERT(DE_FALSE);
           }
       }
   }
}
 
void fillUndefinedShaderInputs (vector<ShaderInput*>& inputs)
{
   for (vector<ShaderInput*>::iterator i = inputs.begin(); i != inputs.end(); i++)
   {
       if (!(*i)->getVariable()->getType().isSampler()) // Samplers are assigned at program-level.
           fillUndefinedComponents((*i)->getValueRange());
   }
}
 
} // anonymous
 
void ShaderGenerator::generate (const ShaderParameters& shaderParams, Shader& shader, const vector<ShaderInput*>& outputs)
{
   // Global scopes
   VariableScope&    globalVariableScope    = shader.getGlobalScope();
   ValueScope        globalValueScope;
 
   // Init state
   m_state.setShader(shaderParams, shader);
   DE_ASSERT(m_state.getExpressionFlags() == 0);
 
   // Reserve some scalars for gl_Position & dEQP_Position
   ReservedScalars reservedScalars;
   if (shader.getType() == Shader::TYPE_VERTEX)
       m_state.getVariableManager().reserve(reservedScalars, 4*2);
 
   // Push global scopes
   m_varManager.pushVariableScope(globalVariableScope);
   m_varManager.pushValueScope(globalValueScope);
 
   // Init shader outputs.
   {
       for (vector<ShaderInput*>::const_iterator i = outputs.begin(); i != outputs.end(); i++)
       {
           const ShaderInput*    input        = *i;
           Variable*            variable    = m_state.getVariableManager().allocate(input->getVariable()->getType(), Variable::STORAGE_SHADER_OUT, input->getVariable()->getName());
 
           m_state.getVariableManager().setValue(variable, input->getValueRange());
       }
 
       if (shader.getType() == Shader::TYPE_FRAGMENT)
       {
           // gl_FragColor
           // \todo [2011-11-22 pyry] Multiple outputs from fragment shader!
           Variable*    fragColorVar    = m_state.getVariableManager().allocate(VariableType(VariableType::TYPE_FLOAT, 4), Variable::STORAGE_SHADER_OUT, getFragColorName(m_state));
           ValueRange    valueRange(fragColorVar->getType());
 
           valueRange.getMin() = tcu::Vec4(0.0f, 0.0f, 0.0f, 0.0f);
           valueRange.getMax() = tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f);
 
           fragColorVar->setLayoutLocation(0); // Bind color output to location 0 (applies to GLSL ES 3.0 onwards).
 
           m_state.getVariableManager().setValue(fragColorVar, valueRange.asAccess());
       }
   }
 
   // Construct shader code.
   {
       Function& main = shader.getMain();
       main.setReturnType(VariableType(VariableType::TYPE_VOID));
 
       if (shaderParams.randomize)
       {
           FunctionGenerator funcGen(m_state, main);
 
           // Mandate assignment into to all shader outputs in main()
           const vector<Variable*>& liveVars = globalVariableScope.getLiveVariables();
           for (vector<Variable*>::const_iterator i = liveVars.begin(); i != liveVars.end(); i++)
           {
               Variable* variable = *i;
               if (variable->getStorage() == Variable::STORAGE_SHADER_OUT)
                   funcGen.requireAssignment(variable);
           }
 
           funcGen.generate();
       }
       else
       {
           if (shader.getType() == Shader::TYPE_VERTEX)
               genVertexPassthrough(m_state, shader);
           else
           {
               DE_ASSERT(shader.getType() == Shader::TYPE_FRAGMENT);
               genFragmentPassthrough(m_state, shader);
           }
       }
 
       if (shader.getType() == Shader::TYPE_VERTEX)
       {
           // Add gl_Position = dEQP_Position;
           m_state.getVariableManager().release(reservedScalars);
 
           Variable* glPosVariable = m_state.getVariableManager().allocate(VariableType(VariableType::TYPE_FLOAT, 4), Variable::STORAGE_SHADER_OUT, "gl_Position");
           Variable* qpPosVariable = m_state.getVariableManager().allocate(VariableType(VariableType::TYPE_FLOAT, 4), Variable::STORAGE_SHADER_IN, "dEQP_Position");
 
           ValueRange valueRange(glPosVariable->getType());
 
           valueRange.getMin() = tcu::Vec4(-1.0f, -1.0f, 0.0f, 1.0f);
           valueRange.getMax() = tcu::Vec4( 1.0f,  1.0f, 0.0f, 1.0f);
 
           m_state.getVariableManager().setValue(qpPosVariable, valueRange.asAccess()); // \todo [2011-05-24 pyry] No expression should be able to use gl_Position or dEQP_Position..
 
           createAssignment(main.getBody(), glPosVariable, qpPosVariable);
       }
   }
 
   // Declare live global variables.
   {
       vector<Variable*> liveVariables;
       std::copy(globalVariableScope.getLiveVariables().begin(), globalVariableScope.getLiveVariables().end(), std::inserter(liveVariables, liveVariables.begin()));
 
       vector<Variable*> createDeclarationStatementVars;
 
       for (vector<Variable*>::iterator i = liveVariables.begin(); i != liveVariables.end(); i++)
       {
           Variable*        variable    = *i;
           const char*        name        = variable->getName();
           bool            declare        = !deStringBeginsWith(name, "gl_"); // Do not declare built-in types.
 
           // Create input entries (store value range) if necessary
           vector<ShaderInput*>& inputs    = shader.getInputs();
           vector<ShaderInput*>& uniforms    = shader.getUniforms();
 
           switch (variable->getStorage())
           {
               case Variable::STORAGE_SHADER_IN:
               {
                   const ValueEntry* value = m_state.getVariableManager().getValue(variable);
 
                   inputs.reserve(inputs.size()+1);
                   inputs.push_back(new ShaderInput(variable, value->getValueRange()));
                   break;
               }
 
               case Variable::STORAGE_UNIFORM:
               {
                   const ValueEntry* value = m_state.getVariableManager().getValue(variable);
 
                   uniforms.reserve(uniforms.size()+1);
                   uniforms.push_back(new ShaderInput(variable, value->getValueRange()));
                   break;
               }
 
               default:
                   break;
           }
 
           if (declare)
               createDeclarationStatementVars.push_back(variable);
           else
           {
               // Just move to global scope without declaration statement.
               m_state.getVariableManager().declareVariable(variable);
           }
       }
 
       // All global initializers must be constant expressions, no variable allocation is allowed
       DE_ASSERT(m_state.getExpressionFlags() == 0);
       m_state.pushExpressionFlags(CONST_EXPR|NO_VAR_ALLOCATION);
 
       // Create declaration statements
       for (vector<Variable*>::iterator i = createDeclarationStatementVars.begin(); i != createDeclarationStatementVars.end(); i++)
       {
           shader.getGlobalStatements().reserve(shader.getGlobalStatements().size());
           shader.getGlobalStatements().push_back(new DeclarationStatement(m_state, *i));
       }
 
       m_state.popExpressionFlags();
   }
 
   // Pop global scopes
   m_varManager.popVariableScope();
   m_varManager.popValueScope();
 
   // Fill undefined (unused) components in inputs with dummy values
   fillUndefinedShaderInputs(shader.getInputs());
   fillUndefinedShaderInputs(shader.getUniforms());
 
   // Tokenize shader and write source
   {
       TokenStream tokenStr;
       shader.tokenize(m_state, tokenStr);
 
       std::ostringstream    str;
       PrettyPrinter        printer(str);
 
       // Append #version if necessary.
       if (m_state.getProgramParameters().version == VERSION_300)
           str << "#version 300 es\n";
 
       printer.append(tokenStr);
       shader.setSource(str.str().c_str());
   }
}
 
} // rsg