John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 1 | // 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 |
|
| 27 | namespace gl
|
| 28 | {
|
| 29 | class Shader;
|
| 30 | }
|
| 31 |
|
| 32 | namespace sh
|
| 33 | {
|
| 34 | struct Uniform
|
| 35 | {
|
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame^] | 36 | Uniform(GLenum type, GLenum precision, const std::string &name, int arraySize, int registerIndex);
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 37 |
|
| 38 | GLenum type;
|
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame^] | 39 | GLenum precision;
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 40 | 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 Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame^] | 91 | private:
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 92 | 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 Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame^] | 109 | Instruction *emit(sw::Shader::Opcode op, TIntermTyped *dst = 0, TIntermNode *src0 = 0, TIntermNode *src1 = 0, TIntermNode *src2 = 0, int index = 0);
|
| 110 | void emitBinary(sw::Shader::Opcode op, TIntermTyped *dst = 0, TIntermNode *src0 = 0, TIntermNode *src1 = 0, TIntermNode *src2 = 0);
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 111 | void emitAssign(sw::Shader::Opcode op, TIntermTyped *result, TIntermTyped *lhs, TIntermTyped *src0, TIntermTyped *src1 = 0);
|
| 112 | void emitCmp(sw::Shader::Control cmpOp, TIntermTyped *dst, TIntermNode *left, TIntermNode *right, int index = 0);
|
| 113 | void argument(sw::Shader::SourceParameter ¶meter, TIntermNode *argument, int index = 0);
|
| 114 | void copy(TIntermTyped *dst, TIntermNode *src, int offset = 0);
|
| 115 | void assignLvalue(TIntermTyped *dst, TIntermNode *src);
|
| 116 | int lvalue(sw::Shader::DestinationParameter &dst, Temporary &address, TIntermTyped *node);
|
| 117 | sw::Shader::ParameterType registerType(TIntermTyped *operand);
|
| 118 | int registerIndex(TIntermTyped *operand);
|
| 119 | int writeMask(TIntermTyped *destination, int index = 0);
|
| 120 | bool trivial(TIntermTyped *expression, int budget); // Fast to compute and no side effects
|
| 121 | int cost(TIntermNode *expression, int budget);
|
| 122 | const Function &findFunction(const TString &name);
|
| 123 |
|
| 124 | int temporaryRegister(TIntermTyped *temporary);
|
| 125 | int varyingRegister(TIntermTyped *varying);
|
| 126 | void declareVarying(TIntermTyped *varying, int reg);
|
| 127 | int uniformRegister(TIntermTyped *uniform);
|
| 128 | int attributeRegister(TIntermTyped *attribute);
|
| 129 | int samplerRegister(TIntermTyped *sampler);
|
| 130 |
|
| 131 | typedef std::vector<TIntermTyped*> VariableArray;
|
| 132 |
|
| 133 | int lookup(VariableArray &list, TIntermTyped *variable);
|
| 134 | int allocate(VariableArray &list, TIntermTyped *variable);
|
| 135 | void free(VariableArray &list, TIntermTyped *variable);
|
| 136 |
|
| 137 | void declareUniform(const TType &type, const TString &name, int index);
|
| 138 | GLenum glVariableType(const TType &type);
|
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame^] | 139 | GLenum glVariablePrecision(const TType &type);
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 140 |
|
| 141 | static int dim(TIntermNode *v);
|
| 142 | static int dim2(TIntermNode *m);
|
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame^] | 143 | static unsigned int loopCount(TIntermLoop *node);
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 144 |
|
| 145 | gl::Shader *const shaderObject;
|
| 146 | sw::Shader *shader;
|
| 147 | sw::PixelShader *pixelShader;
|
| 148 | sw::VertexShader *vertexShader;
|
| 149 |
|
| 150 | VariableArray temporaries;
|
| 151 | VariableArray uniforms;
|
| 152 | VariableArray varyings;
|
| 153 | VariableArray attributes;
|
| 154 | VariableArray samplers;
|
| 155 |
|
| 156 | Scope emitScope;
|
| 157 | Scope currentScope;
|
| 158 |
|
| 159 | int currentFunction;
|
| 160 | std::vector<Function> functionArray;
|
| 161 |
|
| 162 | TParseContext &mContext;
|
| 163 | };
|
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame^] | 164 |
|
| 165 | // Checks whether a loop can run for a variable number of iterations
|
| 166 | class DetectLoopDiscontinuity : public TIntermTraverser
|
| 167 | {
|
| 168 | public:
|
| 169 | bool traverse(TIntermNode *node);
|
| 170 |
|
| 171 | private:
|
| 172 | bool visitBranch(Visit visit, TIntermBranch *node);
|
| 173 | bool visitLoop(Visit visit, TIntermLoop *loop);
|
| 174 | bool visitAggregate(Visit visit, TIntermAggregate *node);
|
| 175 |
|
| 176 | int loopDepth;
|
| 177 | bool loopDiscontinuity;
|
| 178 | };
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 179 | }
|
| 180 |
|
| 181 | #endif // COMPILER_OUTPUTASM_H_
|