1 // SwiftShader Software Renderer
\r
3 // Copyright(c) 2005-2013 TransGaming Inc.
\r
5 // All rights reserved. No part of this software may be copied, distributed, transmitted,
\r
6 // transcribed, stored in a retrieval system, translated into any human or computer
\r
7 // language by any means, or disclosed to third parties without the explicit written
\r
8 // agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
\r
9 // or implied, including but not limited to any patent rights, are granted to you.
\r
12 #ifndef COMPILER_OUTPUTASM_H_
\r
13 #define COMPILER_OUTPUTASM_H_
\r
15 #include "intermediate.h"
\r
16 #include "ParseHelper.h"
\r
17 #include "Shader/PixelShader.hpp"
\r
18 #include "Shader/VertexShader.hpp"
\r
21 #include <GLES2/gl2.h>
\r
36 Uniform(GLenum type, GLenum precision, const std::string &name, int arraySize, int registerIndex);
\r
46 typedef std::vector<Uniform> ActiveUniforms;
\r
51 Attribute(GLenum type, const std::string &name, int arraySize, int registerIndex);
\r
62 Function(int label, const char *name, TIntermSequence *arg, TIntermTyped *ret) : label(label), name(name), arg(arg), ret(ret)
\r
66 Function(int label, const TString &name, TIntermSequence *arg, TIntermTyped *ret) : label(label), name(name), arg(arg), ret(ret)
\r
72 TIntermSequence *arg;
\r
76 typedef sw::Shader::Instruction Instruction;
\r
77 typedef std::vector<Attribute> ActiveAttributes;
\r
81 class OutputASM : public TIntermTraverser
\r
84 explicit OutputASM(TParseContext &context, es2::Shader *shaderObject);
\r
89 void freeTemporary(Temporary *temporary);
\r
98 void emitShader(Scope scope);
\r
100 // Visit AST nodes and output their code to the body stream
\r
101 virtual void visitSymbol(TIntermSymbol*);
\r
102 virtual bool visitBinary(Visit visit, TIntermBinary*);
\r
103 virtual bool visitUnary(Visit visit, TIntermUnary*);
\r
104 virtual bool visitSelection(Visit visit, TIntermSelection*);
\r
105 virtual bool visitAggregate(Visit visit, TIntermAggregate*);
\r
106 virtual bool visitLoop(Visit visit, TIntermLoop*);
\r
107 virtual bool visitBranch(Visit visit, TIntermBranch*);
\r
109 Instruction *emit(sw::Shader::Opcode op, TIntermTyped *dst = 0, TIntermNode *src0 = 0, TIntermNode *src1 = 0, TIntermNode *src2 = 0, int index = 0);
\r
110 Instruction *emitCast(TIntermTyped *dst, TIntermTyped *src);
\r
111 void emitBinary(sw::Shader::Opcode op, TIntermTyped *dst = 0, TIntermNode *src0 = 0, TIntermNode *src1 = 0, TIntermNode *src2 = 0);
\r
112 void emitAssign(sw::Shader::Opcode op, TIntermTyped *result, TIntermTyped *lhs, TIntermTyped *src0, TIntermTyped *src1 = 0);
\r
113 void emitCmp(sw::Shader::Control cmpOp, TIntermTyped *dst, TIntermNode *left, TIntermNode *right, int index = 0);
\r
114 void argument(sw::Shader::SourceParameter ¶meter, TIntermNode *argument, int index = 0);
\r
115 void copy(TIntermTyped *dst, TIntermNode *src, int offset = 0);
\r
116 void assignLvalue(TIntermTyped *dst, TIntermTyped *src);
\r
117 int lvalue(sw::Shader::DestinationParameter &dst, Temporary &address, TIntermTyped *node);
\r
118 sw::Shader::ParameterType registerType(TIntermTyped *operand);
\r
119 int registerIndex(TIntermTyped *operand);
\r
120 int writeMask(TIntermTyped *destination, int index = 0);
\r
121 int readSwizzle(TIntermTyped *argument, int size);
\r
122 bool trivial(TIntermTyped *expression, int budget); // Fast to compute and no side effects
\r
123 int cost(TIntermNode *expression, int budget);
\r
124 const Function *findFunction(const TString &name);
\r
126 int temporaryRegister(TIntermTyped *temporary);
\r
127 int varyingRegister(TIntermTyped *varying);
\r
128 void declareVarying(TIntermTyped *varying, int reg);
\r
129 int uniformRegister(TIntermTyped *uniform);
\r
130 int attributeRegister(TIntermTyped *attribute);
\r
131 int samplerRegister(TIntermTyped *sampler);
\r
133 typedef std::vector<TIntermTyped*> VariableArray;
\r
135 int lookup(VariableArray &list, TIntermTyped *variable);
\r
136 int allocate(VariableArray &list, TIntermTyped *variable);
\r
137 void free(VariableArray &list, TIntermTyped *variable);
\r
139 void declareUniform(const TType &type, const TString &name, int index);
\r
140 GLenum glVariableType(const TType &type);
\r
141 GLenum glVariablePrecision(const TType &type);
\r
143 static int dim(TIntermNode *v);
\r
144 static int dim2(TIntermNode *m);
\r
145 static unsigned int loopCount(TIntermLoop *node);
\r
146 static bool isSamplerRegister(TIntermTyped *operand);
\r
148 es2::Shader *const shaderObject;
\r
149 sw::Shader *shader;
\r
150 sw::PixelShader *pixelShader;
\r
151 sw::VertexShader *vertexShader;
\r
153 VariableArray temporaries;
\r
154 VariableArray uniforms;
\r
155 VariableArray varyings;
\r
156 VariableArray attributes;
\r
157 VariableArray samplers;
\r
160 Scope currentScope;
\r
162 int currentFunction;
\r
163 std::vector<Function> functionArray;
\r
165 TQualifier outputQualifier;
\r
167 TParseContext &mContext;
\r
170 // Checks whether a loop can run for a variable number of iterations
\r
171 class DetectLoopDiscontinuity : public TIntermTraverser
\r
174 bool traverse(TIntermNode *node);
\r
177 bool visitBranch(Visit visit, TIntermBranch *node);
\r
178 bool visitLoop(Visit visit, TIntermLoop *loop);
\r
179 bool visitAggregate(Visit visit, TIntermAggregate *node);
\r
182 bool loopDiscontinuity;
\r
186 #endif // COMPILER_OUTPUTASM_H_
\r