1e5c31af7Sopenharmony_ci/*-------------------------------------------------------------------------
2e5c31af7Sopenharmony_ci * drawElements Quality Program Random Shader Generator
3e5c31af7Sopenharmony_ci * ----------------------------------------------------
4e5c31af7Sopenharmony_ci *
5e5c31af7Sopenharmony_ci * Copyright 2014 The Android Open Source Project
6e5c31af7Sopenharmony_ci *
7e5c31af7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
8e5c31af7Sopenharmony_ci * you may not use this file except in compliance with the License.
9e5c31af7Sopenharmony_ci * You may obtain a copy of the License at
10e5c31af7Sopenharmony_ci *
11e5c31af7Sopenharmony_ci *      http://www.apache.org/licenses/LICENSE-2.0
12e5c31af7Sopenharmony_ci *
13e5c31af7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
14e5c31af7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
15e5c31af7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16e5c31af7Sopenharmony_ci * See the License for the specific language governing permissions and
17e5c31af7Sopenharmony_ci * limitations under the License.
18e5c31af7Sopenharmony_ci *
19e5c31af7Sopenharmony_ci *//*!
20e5c31af7Sopenharmony_ci * \file
21e5c31af7Sopenharmony_ci * \brief Expression generator.
22e5c31af7Sopenharmony_ci *//*--------------------------------------------------------------------*/
23e5c31af7Sopenharmony_ci
24e5c31af7Sopenharmony_ci#include "rsgFunctionGenerator.hpp"
25e5c31af7Sopenharmony_ci#include "rsgUtils.hpp"
26e5c31af7Sopenharmony_ci
27e5c31af7Sopenharmony_ciusing std::vector;
28e5c31af7Sopenharmony_ci
29e5c31af7Sopenharmony_cinamespace rsg
30e5c31af7Sopenharmony_ci{
31e5c31af7Sopenharmony_ci
32e5c31af7Sopenharmony_ciFunctionGenerator::FunctionGenerator (GeneratorState& state, Function& function)
33e5c31af7Sopenharmony_ci	: m_state		(state)
34e5c31af7Sopenharmony_ci	, m_function	(function)
35e5c31af7Sopenharmony_ci{
36e5c31af7Sopenharmony_ci}
37e5c31af7Sopenharmony_ci
38e5c31af7Sopenharmony_ciFunctionGenerator::~FunctionGenerator (void)
39e5c31af7Sopenharmony_ci{
40e5c31af7Sopenharmony_ci}
41e5c31af7Sopenharmony_ci
42e5c31af7Sopenharmony_civoid FunctionGenerator::generate (void)
43e5c31af7Sopenharmony_ci{
44e5c31af7Sopenharmony_ci	std::vector<Statement*>	statementStack;
45e5c31af7Sopenharmony_ci
46e5c31af7Sopenharmony_ci	// Initialize
47e5c31af7Sopenharmony_ci	m_state.setStatementStack(statementStack);
48e5c31af7Sopenharmony_ci	statementStack.push_back(&m_function.getBody());
49e5c31af7Sopenharmony_ci	m_function.getBody().init(m_state);
50e5c31af7Sopenharmony_ci
51e5c31af7Sopenharmony_ci	// Process until statement stack is empty
52e5c31af7Sopenharmony_ci	while (!statementStack.empty())
53e5c31af7Sopenharmony_ci	{
54e5c31af7Sopenharmony_ci		DE_ASSERT((int)statementStack.size() <= m_state.getShaderParameters().maxStatementDepth);
55e5c31af7Sopenharmony_ci
56e5c31af7Sopenharmony_ci		Statement*	curStatement	= statementStack.back();
57e5c31af7Sopenharmony_ci		Statement*	childStatement	= curStatement->createNextChild(m_state);
58e5c31af7Sopenharmony_ci
59e5c31af7Sopenharmony_ci		if (childStatement)
60e5c31af7Sopenharmony_ci			statementStack.push_back(childStatement);
61e5c31af7Sopenharmony_ci		else
62e5c31af7Sopenharmony_ci			statementStack.pop_back();
63e5c31af7Sopenharmony_ci	}
64e5c31af7Sopenharmony_ci
65e5c31af7Sopenharmony_ci	// Create assignments if variables have bound value range
66e5c31af7Sopenharmony_ci	for (vector<Variable*>::iterator i = m_requiredAssignments.begin(); i != m_requiredAssignments.end(); i++)
67e5c31af7Sopenharmony_ci	{
68e5c31af7Sopenharmony_ci		Variable*				variable		= *i;
69e5c31af7Sopenharmony_ci		const ValueEntry*		entry			= m_state.getVariableManager().getValue(variable);
70e5c31af7Sopenharmony_ci		ValueRange				valueRange(variable->getType());
71e5c31af7Sopenharmony_ci
72e5c31af7Sopenharmony_ci		valueRange.getMin() = entry->getValueRange().getMin().value();
73e5c31af7Sopenharmony_ci		valueRange.getMax() = entry->getValueRange().getMax().value();
74e5c31af7Sopenharmony_ci
75e5c31af7Sopenharmony_ci		// Remove value entry from this scope. After this entry ptr is invalid.
76e5c31af7Sopenharmony_ci		m_state.getVariableManager().removeValueFromCurrentScope(variable);
77e5c31af7Sopenharmony_ci
78e5c31af7Sopenharmony_ci		if (!isUndefinedValueRange(valueRange.asAccess()))
79e5c31af7Sopenharmony_ci			m_function.getBody().addChild(new AssignStatement(m_state, variable, valueRange.asAccess()));
80e5c31af7Sopenharmony_ci	}
81e5c31af7Sopenharmony_ci}
82e5c31af7Sopenharmony_ci
83e5c31af7Sopenharmony_ci} // rsg
84