blob: f47f4bae7d56d0738d858b907fd3f1a553a4adf9 [file] [log] [blame]
John Bauman66b8ab22014-05-06 15:57:45 -04001// SwiftShader Software Renderer
2//
3// Copyright(c) 2005-2013 TransGaming Inc.
4//
5// All rights reserved. No part of this software may be copied, distributed, transmitted,
6// transcribed, stored in a retrieval system, translated into any human or computer
7// language by any means, or disclosed to third parties without the explicit written
8// agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
9// or implied, including but not limited to any patent rights, are granted to you.
10//
11
12#ifndef COMPILER_OUTPUTASM_H_
13#define COMPILER_OUTPUTASM_H_
14
15#include "intermediate.h"
16#include "ParseHelper.h"
17#include "Shader/PixelShader.hpp"
18#include "Shader/VertexShader.hpp"
19
20#define GL_APICALL
21#include <GLES2/gl2.h>
22
23#include <list>
24#include <set>
25#include <map>
26
27namespace gl
28{
29 class Shader;
30}
31
32namespace sh
33{
34 struct Uniform
35 {
John Baumand4ae8632014-05-06 16:18:33 -040036 Uniform(GLenum type, GLenum precision, const std::string &name, int arraySize, int registerIndex);
John Bauman66b8ab22014-05-06 15:57:45 -040037
38 GLenum type;
John Baumand4ae8632014-05-06 16:18:33 -040039 GLenum precision;
John Bauman66b8ab22014-05-06 15:57:45 -040040 std::string name;
41 int arraySize;
42
43 int registerIndex;
44 };
45
46 typedef std::vector<Uniform> ActiveUniforms;
47
48 struct Attribute
49 {
50 Attribute();
51 Attribute(GLenum type, const std::string &name, int arraySize, int registerIndex);
52
53 GLenum type;
54 std::string name;
55 int arraySize;
56
57 int registerIndex;
58 };
59
60 struct Function
61 {
62 Function(int label, const char *name, TIntermSequence *arg, TIntermTyped *ret) : label(label), name(name), arg(arg), ret(ret)
63 {
64 }
65
66 Function(int label, const TString &name, TIntermSequence *arg, TIntermTyped *ret) : label(label), name(name), arg(arg), ret(ret)
67 {
68 }
69
70 int label;
71 TString name;
72 TIntermSequence *arg;
73 TIntermTyped *ret;
74 };
75
76 typedef sw::Shader::Instruction Instruction;
77 typedef std::vector<Attribute> ActiveAttributes;
78
79 class Temporary;
80
81 class OutputASM : public TIntermTraverser
82 {
83 public:
84 explicit OutputASM(TParseContext &context, gl::Shader *shaderObject);
85 ~OutputASM();
86
87 void output();
88
89 void freeTemporary(Temporary *temporary);
90
John Baumand4ae8632014-05-06 16:18:33 -040091 private:
John Bauman66b8ab22014-05-06 15:57:45 -040092 enum Scope
93 {
94 GLOBAL,
95 FUNCTION
96 };
97
98 void emitShader(Scope scope);
99
100 // Visit AST nodes and output their code to the body stream
101 virtual void visitSymbol(TIntermSymbol*);
102 virtual bool visitBinary(Visit visit, TIntermBinary*);
103 virtual bool visitUnary(Visit visit, TIntermUnary*);
104 virtual bool visitSelection(Visit visit, TIntermSelection*);
105 virtual bool visitAggregate(Visit visit, TIntermAggregate*);
106 virtual bool visitLoop(Visit visit, TIntermLoop*);
107 virtual bool visitBranch(Visit visit, TIntermBranch*);
108
John Baumand4ae8632014-05-06 16:18:33 -0400109 Instruction *emit(sw::Shader::Opcode op, TIntermTyped *dst = 0, TIntermNode *src0 = 0, TIntermNode *src1 = 0, TIntermNode *src2 = 0, int index = 0);
Nicolas Capens20608662014-06-12 14:05:19 -0400110 Instruction *emitCast(TIntermTyped *dst, TIntermTyped *src);
John Baumand4ae8632014-05-06 16:18:33 -0400111 void emitBinary(sw::Shader::Opcode op, TIntermTyped *dst = 0, TIntermNode *src0 = 0, TIntermNode *src1 = 0, TIntermNode *src2 = 0);
John Bauman66b8ab22014-05-06 15:57:45 -0400112 void emitAssign(sw::Shader::Opcode op, TIntermTyped *result, TIntermTyped *lhs, TIntermTyped *src0, TIntermTyped *src1 = 0);
113 void emitCmp(sw::Shader::Control cmpOp, TIntermTyped *dst, TIntermNode *left, TIntermNode *right, int index = 0);
114 void argument(sw::Shader::SourceParameter &parameter, TIntermNode *argument, int index = 0);
115 void copy(TIntermTyped *dst, TIntermNode *src, int offset = 0);
116 void assignLvalue(TIntermTyped *dst, TIntermNode *src);
117 int lvalue(sw::Shader::DestinationParameter &dst, Temporary &address, TIntermTyped *node);
118 sw::Shader::ParameterType registerType(TIntermTyped *operand);
119 int registerIndex(TIntermTyped *operand);
120 int writeMask(TIntermTyped *destination, int index = 0);
Nicolas Capens059bece2014-05-06 16:44:23 -0400121 int readSwizzle(TIntermTyped *argument, int size);
John Bauman66b8ab22014-05-06 15:57:45 -0400122 bool trivial(TIntermTyped *expression, int budget); // Fast to compute and no side effects
123 int cost(TIntermNode *expression, int budget);
124 const Function &findFunction(const TString &name);
125
126 int temporaryRegister(TIntermTyped *temporary);
127 int varyingRegister(TIntermTyped *varying);
128 void declareVarying(TIntermTyped *varying, int reg);
129 int uniformRegister(TIntermTyped *uniform);
130 int attributeRegister(TIntermTyped *attribute);
131 int samplerRegister(TIntermTyped *sampler);
132
133 typedef std::vector<TIntermTyped*> VariableArray;
134
135 int lookup(VariableArray &list, TIntermTyped *variable);
136 int allocate(VariableArray &list, TIntermTyped *variable);
137 void free(VariableArray &list, TIntermTyped *variable);
138
139 void declareUniform(const TType &type, const TString &name, int index);
140 GLenum glVariableType(const TType &type);
John Baumand4ae8632014-05-06 16:18:33 -0400141 GLenum glVariablePrecision(const TType &type);
John Bauman66b8ab22014-05-06 15:57:45 -0400142
143 static int dim(TIntermNode *v);
144 static int dim2(TIntermNode *m);
John Baumand4ae8632014-05-06 16:18:33 -0400145 static unsigned int loopCount(TIntermLoop *node);
John Bauman66b8ab22014-05-06 15:57:45 -0400146
147 gl::Shader *const shaderObject;
148 sw::Shader *shader;
149 sw::PixelShader *pixelShader;
150 sw::VertexShader *vertexShader;
151
152 VariableArray temporaries;
153 VariableArray uniforms;
154 VariableArray varyings;
155 VariableArray attributes;
156 VariableArray samplers;
157
158 Scope emitScope;
159 Scope currentScope;
160
161 int currentFunction;
162 std::vector<Function> functionArray;
163
164 TParseContext &mContext;
165 };
John Baumand4ae8632014-05-06 16:18:33 -0400166
167 // Checks whether a loop can run for a variable number of iterations
168 class DetectLoopDiscontinuity : public TIntermTraverser
169 {
170 public:
171 bool traverse(TIntermNode *node);
172
173 private:
174 bool visitBranch(Visit visit, TIntermBranch *node);
175 bool visitLoop(Visit visit, TIntermLoop *loop);
176 bool visitAggregate(Visit visit, TIntermAggregate *node);
177
178 int loopDepth;
179 bool loopDiscontinuity;
180 };
John Bauman66b8ab22014-05-06 15:57:45 -0400181}
182
183#endif // COMPILER_OUTPUTASM_H_