xref: /aosp_15_r20/external/deqp/framework/randomshaders/rsgExpressionGenerator.cpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1 /*-------------------------------------------------------------------------
2  * drawElements Quality Program Random Shader Generator
3  * ----------------------------------------------------
4  *
5  * Copyright 2014 The Android Open Source Project
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  *//*!
20  * \file
21  * \brief Expression generator.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "rsgExpressionGenerator.hpp"
25 
26 namespace rsg
27 {
28 
ExpressionGenerator(GeneratorState & state)29 ExpressionGenerator::ExpressionGenerator(GeneratorState &state) : m_state(state)
30 {
31 }
32 
~ExpressionGenerator(void)33 ExpressionGenerator::~ExpressionGenerator(void)
34 {
35 }
36 
generate(const ValueRange & valueRange,int initialDepth)37 Expression *ExpressionGenerator::generate(const ValueRange &valueRange, int initialDepth)
38 {
39     // Create root
40     m_state.setExpressionDepth(initialDepth);
41     Expression *root = Expression::createRandom(m_state, valueRange);
42 
43     try
44     {
45         // Generate full expression
46         generate(root);
47     }
48     catch (const std::exception &)
49     {
50         delete root;
51         m_expressionStack.clear();
52         throw;
53     }
54 
55     return root;
56 }
57 
generate(Expression * root)58 void ExpressionGenerator::generate(Expression *root)
59 {
60     DE_ASSERT(m_expressionStack.empty());
61 
62     // Initialize stack
63     m_expressionStack.push_back(root);
64     m_state.setExpressionDepth(m_state.getExpressionDepth() + 1);
65 
66     // Process until done
67     while (!m_expressionStack.empty())
68     {
69         DE_ASSERT(m_state.getExpressionDepth() <= m_state.getShaderParameters().maxExpressionDepth);
70 
71         Expression *curExpr = m_expressionStack[m_expressionStack.size() - 1];
72         Expression *child   = curExpr->createNextChild(m_state);
73 
74         if (child)
75         {
76             m_expressionStack.push_back(child);
77             m_state.setExpressionDepth(m_state.getExpressionDepth() + 1);
78         }
79         else
80         {
81             m_expressionStack.pop_back();
82             m_state.setExpressionDepth(m_state.getExpressionDepth() - 1);
83         }
84     }
85 }
86 
87 } // namespace rsg
88