OSDN Git Service

Implement a Radiance prototype.
[android-x86/external-swiftshader.git] / src / Radiance / compiler / OutputASM.h
1 // SwiftShader Software Renderer\r
2 //\r
3 // Copyright(c) 2005-2013 TransGaming Inc.\r
4 //\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
10 //\r
11 \r
12 #ifndef COMPILER_OUTPUTASM_H_\r
13 #define COMPILER_OUTPUTASM_H_\r
14 \r
15 #include "intermediate.h"\r
16 #include "ParseHelper.h"\r
17 #include "Shader/PixelShader.hpp"\r
18 #include "Shader/VertexShader.hpp"\r
19 \r
20 #define GL_APICALL\r
21 #include <GLES2/gl2.h>\r
22 \r
23 #include <list>\r
24 #include <set>\r
25 #include <map>\r
26 \r
27 namespace es2\r
28 {\r
29         class Shader;\r
30 }\r
31 \r
32 namespace sh\r
33 {\r
34         struct Uniform\r
35         {\r
36                 Uniform(GLenum type, GLenum precision, const std::string &name, int arraySize, int registerIndex);\r
37 \r
38                 GLenum type;\r
39                 GLenum precision;\r
40                 std::string name;\r
41                 int arraySize;\r
42         \r
43                 int registerIndex;\r
44         };\r
45 \r
46         typedef std::vector<Uniform> ActiveUniforms;\r
47 \r
48         struct Attribute\r
49         {\r
50                 Attribute();\r
51                 Attribute(GLenum type, const std::string &name, int arraySize, int registerIndex);\r
52 \r
53                 GLenum type;\r
54                 std::string name;\r
55                 int arraySize;\r
56         \r
57                 int registerIndex;\r
58         };\r
59 \r
60         struct Function\r
61         {\r
62                 Function(int label, const char *name, TIntermSequence *arg, TIntermTyped *ret) : label(label), name(name), arg(arg), ret(ret)\r
63                 {\r
64                 }\r
65 \r
66                 Function(int label, const TString &name, TIntermSequence *arg, TIntermTyped *ret) : label(label), name(name), arg(arg), ret(ret)\r
67                 {\r
68                 }\r
69 \r
70                 int label;\r
71                 TString name;\r
72                 TIntermSequence *arg;\r
73                 TIntermTyped *ret;\r
74         };\r
75         \r
76         typedef sw::Shader::Instruction Instruction;\r
77         typedef std::vector<Attribute> ActiveAttributes;\r
78 \r
79         class Temporary;\r
80 \r
81         class OutputASM : public TIntermTraverser\r
82         {\r
83         public:\r
84                 explicit OutputASM(TParseContext &context, es2::Shader *shaderObject);\r
85                 ~OutputASM();\r
86 \r
87                 void output();\r
88 \r
89                 void freeTemporary(Temporary *temporary);\r
90 \r
91         private:\r
92                 enum Scope\r
93                 {\r
94                         GLOBAL,\r
95                         FUNCTION\r
96                 };\r
97 \r
98                 void emitShader(Scope scope);\r
99 \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
108 \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 &parameter, 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
125 \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
132 \r
133                 typedef std::vector<TIntermTyped*> VariableArray;\r
134 \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
138 \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
142 \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
147 \r
148                 es2::Shader *const shaderObject;\r
149                 sw::Shader *shader;\r
150                 sw::PixelShader *pixelShader;\r
151                 sw::VertexShader *vertexShader;\r
152 \r
153                 VariableArray temporaries;\r
154                 VariableArray uniforms;\r
155                 VariableArray varyings;\r
156                 VariableArray attributes;\r
157                 VariableArray samplers;\r
158 \r
159                 Scope emitScope;\r
160                 Scope currentScope;\r
161 \r
162                 int currentFunction;\r
163                 std::vector<Function> functionArray;\r
164 \r
165                 TQualifier outputQualifier;\r
166 \r
167                 TParseContext &mContext;\r
168         };\r
169 \r
170         // Checks whether a loop can run for a variable number of iterations\r
171         class DetectLoopDiscontinuity : public TIntermTraverser\r
172         {\r
173         public:\r
174                 bool traverse(TIntermNode *node);\r
175 \r
176         private:\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
180 \r
181                 int loopDepth;\r
182                 bool loopDiscontinuity;\r
183         };\r
184 }\r
185 \r
186 #endif   // COMPILER_OUTPUTASM_H_\r