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 getReturnType(void) const46 const VariableType &getReturnType(void) const 47 { 48 return m_returnType; 49 } setReturnType(const VariableType & type)50 void setReturnType(const VariableType &type) 51 { 52 m_returnType = type; 53 } 54 55 void addParameter(Variable *variable); 56 getBody(void)57 BlockStatement &getBody(void) 58 { 59 return m_functionBlock; 60 } getBody(void) const61 const BlockStatement &getBody(void) const 62 { 63 return m_functionBlock; 64 } 65 66 void tokenize(GeneratorState &state, TokenStream &stream) const; 67 68 private: 69 std::string m_name; 70 std::vector<Variable *> m_parameters; 71 VariableType m_returnType; 72 73 BlockStatement m_functionBlock; 74 }; 75 76 class ShaderInput 77 { 78 public: 79 ShaderInput(const Variable *variable, ConstValueRangeAccess valueRange); ~ShaderInput(void)80 ~ShaderInput(void) 81 { 82 } 83 getVariable(void) const84 const Variable *getVariable(void) const 85 { 86 return m_variable; 87 } getValueRange(void) const88 ConstValueRangeAccess getValueRange(void) const 89 { 90 return ConstValueRangeAccess(m_variable->getType(), &m_min[0], &m_max[0]); 91 } getValueRange(void)92 ValueRangeAccess getValueRange(void) 93 { 94 return ValueRangeAccess(m_variable->getType(), &m_min[0], &m_max[0]); 95 } 96 97 private: 98 const Variable *m_variable; 99 std::vector<Scalar> m_min; 100 std::vector<Scalar> m_max; 101 }; 102 103 class Shader 104 { 105 public: 106 enum Type 107 { 108 TYPE_VERTEX = 0, 109 TYPE_FRAGMENT, 110 111 TYPE_LAST 112 }; 113 114 Shader(Type type); 115 ~Shader(void); 116 getType(void) const117 Type getType(void) const 118 { 119 return m_type; 120 } getSource(void) const121 const char *getSource(void) const 122 { 123 return m_source.c_str(); 124 } 125 126 void execute(ExecutionContext &execCtx) const; 127 128 // For generator implementation only getMain(void)129 Function &getMain(void) 130 { 131 return m_mainFunction; 132 } 133 Function &allocateFunction(void); 134 getGlobalScope(void)135 VariableScope &getGlobalScope(void) 136 { 137 return m_globalScope; 138 } getGlobalStatements(void)139 std::vector<Statement *> &getGlobalStatements(void) 140 { 141 return m_globalStatements; 142 } 143 144 void tokenize(GeneratorState &state, TokenStream &str) const; setSource(const char * source)145 void setSource(const char *source) 146 { 147 m_source = source; 148 } 149 getInputs(void)150 std::vector<ShaderInput *> &getInputs(void) 151 { 152 return m_inputs; 153 } getUniforms(void)154 std::vector<ShaderInput *> &getUniforms(void) 155 { 156 return m_uniforms; 157 } 158 159 // For executor getInputs(void) const160 const std::vector<ShaderInput *> &getInputs(void) const 161 { 162 return m_inputs; 163 } getUniforms(void) const164 const std::vector<ShaderInput *> &getUniforms(void) const 165 { 166 return m_uniforms; 167 } 168 void getOutputs(std::vector<const Variable *> &outputs) const; 169 170 private: 171 Type m_type; 172 173 VariableScope m_globalScope; 174 std::vector<Statement *> m_globalStatements; 175 176 std::vector<ShaderInput *> m_inputs; 177 std::vector<ShaderInput *> m_uniforms; 178 179 std::vector<Function *> m_functions; 180 Function m_mainFunction; 181 182 std::string m_source; 183 }; 184 185 } // namespace rsg 186 187 #endif // _RSGSHADER_HPP 188