Home | History | Annotate | Download | only in randomshaders
      1 #ifndef _RSGSHADER_HPP
      2 #define _RSGSHADER_HPP
      3 /*-------------------------------------------------------------------------
      4  * drawElements Quality Program Random Shader Generator
      5  * ----------------------------------------------------
      6  *
      7  * Copyright 2014 The Android Open Source Project
      8  *
      9  * Licensed under the Apache License, Version 2.0 (the "License");
     10  * you may not use this file except in compliance with the License.
     11  * You may obtain a copy of the License at
     12  *
     13  *      http://www.apache.org/licenses/LICENSE-2.0
     14  *
     15  * Unless required by applicable law or agreed to in writing, software
     16  * distributed under the License is distributed on an "AS IS" BASIS,
     17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     18  * See the License for the specific language governing permissions and
     19  * limitations under the License.
     20  *
     21  *//*!
     22  * \file
     23  * \brief Shader Class.
     24  *//*--------------------------------------------------------------------*/
     25 
     26 #include "rsgDefs.hpp"
     27 #include "rsgVariable.hpp"
     28 #include "rsgStatement.hpp"
     29 #include "rsgVariableManager.hpp"
     30 #include "rsgToken.hpp"
     31 #include "rsgExecutionContext.hpp"
     32 
     33 #include <vector>
     34 #include <string>
     35 
     36 namespace rsg
     37 {
     38 
     39 class Function
     40 {
     41 public:
     42 								Function			(void);
     43 								Function			(const char* name);
     44 								~Function			(void);
     45 
     46 	const VariableType&			getReturnType		(void) const				{ return m_returnType;		}
     47 	void						setReturnType		(const VariableType& type)	{ m_returnType = type;		}
     48 
     49 	void						addParameter		(Variable* variable);
     50 
     51 	BlockStatement&				getBody				(void)			{ return m_functionBlock;	}
     52 	const BlockStatement&		getBody				(void) const	{ return m_functionBlock;	}
     53 
     54 	void						tokenize			(GeneratorState& state, TokenStream& stream) const;
     55 
     56 private:
     57 	std::string					m_name;
     58 	std::vector<Variable*>		m_parameters;
     59 	VariableType				m_returnType;
     60 
     61 	BlockStatement				m_functionBlock;
     62 };
     63 
     64 class ShaderInput
     65 {
     66 public:
     67 								ShaderInput			(const Variable* variable, ConstValueRangeAccess valueRange);
     68 								~ShaderInput		(void) {}
     69 
     70 	const Variable*				getVariable			(void) const	{ return m_variable;	}
     71 	ConstValueRangeAccess		getValueRange		(void) const	{ return ConstValueRangeAccess(m_variable->getType(), &m_min[0], &m_max[0]);	}
     72 	ValueRangeAccess			getValueRange		(void)			{ return ValueRangeAccess(m_variable->getType(), &m_min[0], &m_max[0]);			}
     73 
     74 private:
     75 	const Variable*				m_variable;
     76 	std::vector<Scalar>			m_min;
     77 	std::vector<Scalar>			m_max;
     78 };
     79 
     80 class Shader
     81 {
     82 public:
     83 	enum Type
     84 	{
     85 		TYPE_VERTEX = 0,
     86 		TYPE_FRAGMENT,
     87 
     88 		TYPE_LAST
     89 	};
     90 
     91 								Shader				(Type type);
     92 								~Shader				(void);
     93 
     94 	Type						getType				(void) const	{ return m_type;				}
     95 	const char*					getSource			(void) const	{ return m_source.c_str();		}
     96 
     97 	void						execute				(ExecutionContext& execCtx) const;
     98 
     99 	// For generator implementation only
    100 	Function&					getMain				(void)			{ return m_mainFunction;		}
    101 	Function&					allocateFunction	(void);
    102 
    103 	VariableScope&				getGlobalScope		(void)			{ return m_globalScope;			}
    104 	std::vector<Statement*>&	getGlobalStatements	(void)			{ return m_globalStatements;	}
    105 
    106 	void						tokenize			(GeneratorState& state, TokenStream& str) const;
    107 	void						setSource			(const char* source) { m_source = source;		}
    108 
    109 	std::vector<ShaderInput*>&	getInputs			(void)			{ return m_inputs;				}
    110 	std::vector<ShaderInput*>&	getUniforms			(void)			{ return m_uniforms;			}
    111 
    112 	// For executor
    113 	const std::vector<ShaderInput*>&	getInputs	(void) const	{ return m_inputs;				}
    114 	const std::vector<ShaderInput*>&	getUniforms	(void) const	{ return m_uniforms;			}
    115 	void								getOutputs	(std::vector<const Variable*>& outputs) const;
    116 
    117 private:
    118 	Type						m_type;
    119 
    120 	VariableScope				m_globalScope;
    121 	std::vector<Statement*>		m_globalStatements;
    122 
    123 	std::vector<ShaderInput*>	m_inputs;
    124 	std::vector<ShaderInput*>	m_uniforms;
    125 
    126 	std::vector<Function*>		m_functions;
    127 	Function					m_mainFunction;
    128 
    129 	std::string					m_source;
    130 };
    131 
    132 } // rsg
    133 
    134 #endif // _RSGSHADER_HPP
    135