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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*-------------------------------------------------------------------------
 * 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 Variable manager.
 *//*--------------------------------------------------------------------*/
 
#include "rsgVariableManager.hpp"
 
#include <algorithm>
#include <map>
#include <set>
 
using std::vector;
using std::set;
using std::map;
 
namespace rsg
{
 
class SubValueRangeIterator
{
public:
                                   SubValueRangeIterator            (const ConstValueRangeAccess& valueRange);
                                   ~SubValueRangeIterator            (void) {}
 
   bool                            hasItem                            (void) const;
   ConstValueRangeAccess            getItem                            (void) const;
   void                            next                            (void);
 
private:
 
   vector<ConstValueRangeAccess>    m_stack;
};
 
SubValueRangeIterator::SubValueRangeIterator (const ConstValueRangeAccess& valueRange)
{
   m_stack.push_back(valueRange);
}
 
inline bool SubValueRangeIterator::hasItem (void) const
{
   return !m_stack.empty();
}
 
inline ConstValueRangeAccess SubValueRangeIterator::getItem (void) const
{
   return m_stack[m_stack.size()-1];
}
 
void SubValueRangeIterator::next (void)
{
   ConstValueRangeAccess curItem = getItem();
   m_stack.pop_back(); // Remove current
 
   switch (curItem.getType().getBaseType())
   {
       case VariableType::TYPE_ARRAY:
       {
           int numElements = curItem.getType().getNumElements();
           for (int ndx = 0; ndx < numElements; ndx++)
               m_stack.push_back(curItem.member(ndx));
           break;
       }
 
       case VariableType::TYPE_STRUCT:
       {
           int numMembers = (int)curItem.getType().getMembers().size();
           for (int ndx = 0; ndx < numMembers; ndx++)
               m_stack.push_back(curItem.member(ndx));
           break;
       }
 
       default:
           break; // \todo [2011-02-03 pyry] Swizzle control?
   }
}
 
ValueEntry::ValueEntry (const Variable* variable)
   : m_variable    (variable)
   , m_valueRange    (variable->getType())
{
}
 
VariableScope::VariableScope (void)
{
}
 
VariableScope::~VariableScope (void)
{
   for (vector<Variable*>::iterator i = m_declaredVariables.begin(); i != m_declaredVariables.end(); i++)
       delete *i;
 
   for (vector<Variable*>::iterator i = m_liveVariables.begin(); i != m_liveVariables.end(); i++)
       delete *i;
}
 
Variable* VariableScope::allocate (const VariableType& type, Variable::Storage storage, const char* name)
{
   Variable* variable = new Variable(type, storage, name);
   try
   {
       m_liveVariables.push_back(variable);
       return variable;
   }
   catch (const std::exception&)
   {
       delete variable;
       throw;
   }
}
 
void VariableScope::declare (Variable* variable)
{
   m_declaredVariables.push_back(variable);
   removeLive(variable);
}
 
void VariableScope::removeLive (const Variable* variable)
{
   vector<Variable*>::iterator pos = std::find(m_liveVariables.begin(), m_liveVariables.end(), variable);
   DE_ASSERT(pos != m_liveVariables.end());
 
   // \todo [pyry] Not so efficient
   m_liveVariables.erase(pos);
}
 
ValueScope::ValueScope (void)
{
}
 
ValueScope::~ValueScope (void)
{
   clear();
}
 
void ValueScope::clear (void)
{
   for (vector<ValueEntry*>::iterator i = m_entries.begin(); i != m_entries.end(); i++)
       delete *i;
   m_entries.clear();
}
 
ValueEntry* ValueScope::allocate (const Variable* variable)
{
   ValueEntry* entry = new ValueEntry(variable);
   try
   {
       m_entries.push_back(entry);
       return entry;
   }
   catch (const std::exception&)
   {
       delete entry;
       throw;
   }
}
 
class CompareEntryVariable
{
public:
   CompareEntryVariable (const Variable* variable)
       : m_variable(variable)
   {
   }
 
   bool operator== (const ValueEntry* entry) const
   {
       return entry->getVariable() == m_variable;
   }
 
private:
   const Variable* m_variable;
};
 
bool operator== (const ValueEntry* entry, const CompareEntryVariable& cmp)
{
   return cmp == entry;
}
 
ValueEntry* ValueScope::findEntry (const Variable* variable) const
{
   vector<ValueEntry*>::const_iterator pos = std::find(m_entries.begin(), m_entries.end(), CompareEntryVariable(variable));
   return pos != m_entries.end() ? *pos : DE_NULL;
}
 
void ValueScope::setValue (const Variable* variable, ConstValueRangeAccess value)
{
   ValueEntry* entry = findEntry(variable);
   DE_ASSERT(entry);
 
   ValueRangeAccess dst = entry->getValueRange();
   dst.getMin() = value.getMin().value();
   dst.getMax() = value.getMax().value();
}
 
void ValueScope::removeValue (const Variable* variable)
{
   vector<ValueEntry*>::iterator pos = std::find(m_entries.begin(), m_entries.end(), CompareEntryVariable(variable));
   if (pos != m_entries.end())
   {
       ValueEntry* entry = *pos;
       m_entries.erase(pos);
       delete entry;
   }
}
 
VariableManager::VariableManager (NameAllocator& nameAllocator)
   : m_numAllocatedScalars                (0)
   , m_numAllocatedShaderInScalars        (0)
   , m_numAllocatedShaderInVariables    (0)
   , m_numAllocatedUniformScalars        (0)
   , m_nameAllocator                    (nameAllocator)
{
}
 
VariableManager::~VariableManager (void)
{
}
 
Variable* VariableManager::allocate (const VariableType& type)
{
   return allocate(type, Variable::STORAGE_LOCAL, m_nameAllocator.allocate().c_str());
}
 
Variable* VariableManager::allocate (const VariableType& type, Variable::Storage storage, const char* name)
{
   VariableScope&    varScope    = getCurVariableScope();
   ValueScope&        valueScope    = getCurValueScope();
   int                numScalars    = type.getScalarSize();
 
   // Allocate in current scope
   Variable* variable = varScope.allocate(type, Variable::STORAGE_LOCAL, name);
 
   // Allocate value entry
   ValueEntry* valueEntry = valueScope.allocate(variable);
 
   // Add to cache
   m_entryCache.push_back(valueEntry);
 
   m_numAllocatedScalars += numScalars;
 
   // Set actual storage - affects uniform/shader in allocations.
   setStorage(variable, storage);
 
   return variable;
}
 
void VariableManager::setStorage (Variable* variable, Variable::Storage storage)
{
   int numScalars = variable->getType().getScalarSize();
 
   // Decrement old.
   if (variable->getStorage() == Variable::STORAGE_SHADER_IN)
   {
       m_numAllocatedShaderInScalars    -= numScalars;
       m_numAllocatedShaderInVariables    -= 1;
   }
   else if (variable->getStorage() == Variable::STORAGE_UNIFORM)
       m_numAllocatedUniformScalars -= numScalars;
 
   // Add new.
   if (storage == Variable::STORAGE_SHADER_IN)
   {
       m_numAllocatedShaderInScalars    += numScalars;
       m_numAllocatedShaderInVariables    += 1;
   }
   else if (storage == Variable::STORAGE_UNIFORM)
       m_numAllocatedUniformScalars += numScalars;
 
   variable->setStorage(storage);
}
 
bool VariableManager::canDeclareInCurrentScope (const Variable* variable) const
{
   const vector<Variable*>& curLiveVars = getCurVariableScope().getLiveVariables();
   return std::find(curLiveVars.begin(), curLiveVars.end(), variable) != curLiveVars.end();
}
 
const vector<Variable*>& VariableManager::getLiveVariables (void) const
{
   return getCurVariableScope().getLiveVariables();
}
 
void VariableManager::declareVariable (Variable* variable)
{
   // Remove from cache if exists in there.
   std::vector<const ValueEntry*>::iterator pos = std::find(m_entryCache.begin(), m_entryCache.end(), CompareEntryVariable(variable));
   if (pos != m_entryCache.end())
       m_entryCache.erase(pos);
 
   DE_ASSERT(std::find(m_entryCache.begin(), m_entryCache.end(), CompareEntryVariable(variable)) == m_entryCache.end());
 
   // Remove from scope stack.
   for (vector<ValueScope*>::const_iterator stackIter = m_valueScopeStack.begin(); stackIter != m_valueScopeStack.end(); stackIter++)
   {
       ValueScope* scope = *stackIter;
       scope->removeValue(variable);
   }
 
   // Declare in current scope.
   getCurVariableScope().declare(variable);
}
 
const ValueEntry* VariableManager::getValue (const Variable* variable) const
{
   vector<const ValueEntry*>::const_iterator pos = std::find(m_entryCache.begin(), m_entryCache.end(), CompareEntryVariable(variable));
   return pos != m_entryCache.end() ? *pos : DE_NULL;
}
 
void VariableManager::removeValueFromCurrentScope (const Variable* variable)
{
   // Remove from cache
   std::vector<const ValueEntry*>::iterator pos = std::find(m_entryCache.begin(), m_entryCache.end(), CompareEntryVariable(variable));
   DE_ASSERT(pos != m_entryCache.end());
   m_entryCache.erase(pos);
 
   // Remove from current scope \note May not exist in there.
   getCurValueScope().removeValue(variable);
}
 
const ValueEntry* VariableManager::getParentValue (const Variable* variable) const
{
   if (m_valueScopeStack.size() < 2)
       return DE_NULL; // Only single value scope
 
   for (vector<ValueScope*>::const_reverse_iterator i = m_valueScopeStack.rbegin()+1; i != m_valueScopeStack.rend(); i++)
   {
       const ValueScope*    scope    = *i;
       ValueEntry*            entry    = scope->findEntry(variable);
 
       if (entry)
           return entry;
   }
 
   return DE_NULL; // Not found in stack
}
 
void VariableManager::setValue (const Variable* variable, ConstValueRangeAccess value)
{
   ValueScope& curScope = getCurValueScope();
 
   if (!curScope.findEntry(variable))
   {
       // New value, allocate and update cache.
       ValueEntry*                                    newEntry    = curScope.allocate(variable);
       std::vector<const ValueEntry*>::iterator    cachePos    = std::find(m_entryCache.begin(), m_entryCache.end(), CompareEntryVariable(variable));
 
       if (cachePos != m_entryCache.end())
           *cachePos = newEntry;
       else
           m_entryCache.push_back(newEntry);
   }
 
   curScope.setValue(variable, value);
}
 
void VariableManager::reserve (ReservedScalars& store, int numScalars)
{
   DE_ASSERT(store.numScalars == 0);
   store.numScalars         = numScalars;
   m_numAllocatedScalars    += numScalars;
}
 
void VariableManager::release (ReservedScalars& store)
{
   m_numAllocatedScalars    -= store.numScalars;
   store.numScalars         = 0;
}
 
void VariableManager::pushVariableScope (VariableScope& scope)
{
   // Expects emtpy scope
   DE_ASSERT(scope.getDeclaredVariables().size() == 0);
   DE_ASSERT(scope.getLiveVariables().size() == 0);
 
   m_variableScopeStack.push_back(&scope);
}
 
void VariableManager::popVariableScope (void)
{
   VariableScope& curScope = getCurVariableScope();
 
   // Migrate live variables to parent scope.
   // Variables allocated in child scopes can be declared in any parent scope but not the other way around.
   if (m_variableScopeStack.size() > 1)
   {
       VariableScope&        parentScope        = *m_variableScopeStack[m_variableScopeStack.size()-2];
       vector<Variable*>&    curLiveVars        = curScope.getLiveVariables();
       vector<Variable*>&    parenLiveVars    = parentScope.getLiveVariables();
 
       while (!curLiveVars.empty())
       {
           Variable* liveVar = curLiveVars.back();
           parenLiveVars.push_back(liveVar);
           curLiveVars.pop_back();
       }
   }
 
   // All variables should be either migrated to parent or declared (in case of root scope).
   DE_ASSERT(curScope.getLiveVariables().size() == 0);
 
   m_variableScopeStack.pop_back();
}
 
void VariableManager::pushValueScope (ValueScope& scope)
{
   // Value scope should be empty
   DE_ASSERT(scope.getValues().size() == 0);
 
   m_valueScopeStack.push_back(&scope);
}
 
void VariableManager::popValueScope (void)
{
   ValueScope& oldScope = getCurValueScope();
 
   // Pop scope and clear cache.
   m_valueScopeStack.pop_back();
   m_entryCache.clear();
 
   // Re-build entry cache.
   if (!m_valueScopeStack.empty())
   {
       ValueScope& newTopScope = getCurValueScope();
 
       // Speed up computing intersections.
       map<const Variable*, const ValueEntry*>    oldValues;
       const vector<ValueEntry*>&                oldEntries = oldScope.getValues();
 
       for (vector<ValueEntry*>::const_iterator valueIter = oldEntries.begin(); valueIter != oldEntries.end(); valueIter++)
           oldValues[(*valueIter)->getVariable()] = *valueIter;
 
       set<const Variable*> addedVars;
 
       // Re-build based on current stack.
       for (vector<ValueScope*>::reverse_iterator scopeIter = m_valueScopeStack.rbegin(); scopeIter != m_valueScopeStack.rend(); scopeIter++)
       {
           const ValueScope*            scope            = *scopeIter;
           const vector<ValueEntry*>&    valueEntries    = scope->getValues();
 
           for (vector<ValueEntry*>::const_iterator valueIter = valueEntries.begin(); valueIter != valueEntries.end(); valueIter++)
           {
               const ValueEntry*    entry    = *valueIter;
               const Variable*        var        = entry->getVariable();
 
               if (addedVars.find(var) != addedVars.end())
                   continue; // Already in cache, set deeper in scope stack.
 
               DE_ASSERT(std::find(m_entryCache.begin(), m_entryCache.end(), CompareEntryVariable(var)) == m_entryCache.end());
 
               if (oldValues.find(var) != oldValues.end())
               {
                   const ValueEntry* oldEntry = oldValues[var];
 
                   // Build new intersected value and store into current scope.
                   ValueRange intersectedValue(var->getType());
                   DE_ASSERT(oldEntry->getValueRange().intersects(entry->getValueRange())); // Must intersect
                   ValueRange::computeIntersection(intersectedValue, entry->getValueRange(), oldEntry->getValueRange());
 
                   if (!newTopScope.findEntry(var))
                       newTopScope.allocate(var);
 
                   newTopScope.setValue(var, intersectedValue.asAccess());
 
                   // Add entry from top scope to cache.
                   m_entryCache.push_back(newTopScope.findEntry(var));
               }
               else
                   m_entryCache.push_back(entry); // Just add to cache.
 
               addedVars.insert(var); // Record as cached variable.
           }
       }
 
       // Copy entries from popped scope that don't yet exist in the stack.
       for (vector<ValueEntry*>::const_iterator valueIter = oldEntries.begin(); valueIter != oldEntries.end(); valueIter++)
       {
           const ValueEntry*    oldEntry    = *valueIter;
           const Variable*        var            = oldEntry->getVariable();
 
           if (addedVars.find(var) == addedVars.end())
               setValue(var, oldEntry->getValueRange());
       }
   }
}
 
} // rsg