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 |
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 20 | #include <list>
|
| 21 | #include <set>
|
| 22 | #include <map>
|
| 23 |
|
Nicolas Capens | 14ee762 | 2014-10-28 23:48:41 -0400 | [diff] [blame] | 24 | namespace es2
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 25 | {
|
| 26 | class Shader;
|
| 27 | }
|
| 28 |
|
Nicolas Capens | eafa022 | 2015-01-26 00:23:01 -0500 | [diff] [blame] | 29 | typedef unsigned int GLenum;
|
| 30 |
|
Nicolas Capens | 5597eef | 2015-01-22 09:33:04 -0500 | [diff] [blame] | 31 | namespace glsl
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 32 | {
|
| 33 | struct Uniform
|
| 34 | {
|
Alexis Hetu | 5228d3c | 2015-07-22 16:37:02 -0400 | [diff] [blame] | 35 | Uniform(GLenum type, GLenum precision, const std::string &name, int arraySize, int registerIndex, int offset, int blockId);
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 36 |
|
| 37 | GLenum type;
|
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 38 | GLenum precision;
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 39 | std::string name;
|
| 40 | int arraySize;
|
| 41 |
|
| 42 | int registerIndex;
|
Alexis Hetu | 5228d3c | 2015-07-22 16:37:02 -0400 | [diff] [blame] | 43 | int offset;
|
Alexis Hetu | b938c3c | 2015-07-06 15:03:42 -0400 | [diff] [blame] | 44 |
|
| 45 | int blockId;
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 46 | };
|
| 47 |
|
| 48 | typedef std::vector<Uniform> ActiveUniforms;
|
| 49 |
|
Alexis Hetu | b938c3c | 2015-07-06 15:03:42 -0400 | [diff] [blame] | 50 | struct UniformBlock
|
| 51 | {
|
| 52 | UniformBlock(const std::string& name, const std::string& instanceName, unsigned int dataSize, unsigned int arraySize,
|
| 53 | TLayoutBlockStorage layout, bool isRowMajorLayout, int registerIndex, int blockId);
|
| 54 |
|
| 55 | const std::string& getName() const { return (instanceName.length() > 0) ? instanceName : name; }
|
| 56 |
|
| 57 | std::string name;
|
| 58 | std::string instanceName;
|
| 59 | unsigned int dataSize;
|
| 60 | unsigned int arraySize;
|
| 61 | TLayoutBlockStorage layout;
|
| 62 | bool isRowMajorLayout;
|
| 63 | std::vector<int> fields;
|
| 64 |
|
| 65 | int registerIndex;
|
| 66 |
|
| 67 | int blockId;
|
| 68 | };
|
| 69 |
|
| 70 | typedef std::vector<UniformBlock> ActiveUniformBlocks;
|
| 71 |
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 72 | struct Attribute
|
| 73 | {
|
| 74 | Attribute();
|
Alexis Hetu | 910b6b6 | 2015-07-16 11:12:38 -0400 | [diff] [blame] | 75 | Attribute(GLenum type, const std::string &name, int arraySize, int location, int registerIndex);
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 76 |
|
| 77 | GLenum type;
|
| 78 | std::string name;
|
| 79 | int arraySize;
|
Alexis Hetu | 910b6b6 | 2015-07-16 11:12:38 -0400 | [diff] [blame] | 80 | int location;
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 81 |
|
| 82 | int registerIndex;
|
| 83 | };
|
| 84 |
|
Nicolas Capens | 68754bf | 2015-01-22 09:31:59 -0500 | [diff] [blame] | 85 | typedef std::vector<Attribute> ActiveAttributes;
|
| 86 |
|
| 87 | struct Varying
|
| 88 | {
|
| 89 | Varying(GLenum type, const std::string &name, int arraySize, int reg = -1, int col = -1)
|
| 90 | : type(type), name(name), arraySize(arraySize), reg(reg), col(col)
|
| 91 | {
|
| 92 | }
|
| 93 |
|
| 94 | bool isArray() const
|
| 95 | {
|
| 96 | return arraySize >= 1;
|
| 97 | }
|
| 98 |
|
| 99 | int size() const // Unify with es2::Uniform?
|
| 100 | {
|
| 101 | return arraySize > 0 ? arraySize : 1;
|
| 102 | }
|
| 103 |
|
| 104 | GLenum type;
|
| 105 | std::string name;
|
| 106 | int arraySize;
|
| 107 |
|
| 108 | int reg; // First varying register, assigned during link
|
| 109 | int col; // First register element, assigned during link
|
| 110 | };
|
| 111 |
|
| 112 | typedef std::list<Varying> VaryingList;
|
| 113 |
|
| 114 | class Shader
|
| 115 | {
|
| 116 | friend class OutputASM;
|
| 117 | public:
|
Ping-Hao Wu | 6dbd5fe | 2015-03-20 13:21:28 -0700 | [diff] [blame] | 118 | virtual ~Shader() {};
|
Nicolas Capens | 68754bf | 2015-01-22 09:31:59 -0500 | [diff] [blame] | 119 | virtual sw::Shader *getShader() const = 0;
|
| 120 | virtual sw::PixelShader *getPixelShader() const;
|
| 121 | virtual sw::VertexShader *getVertexShader() const;
|
| 122 |
|
| 123 | protected:
|
| 124 | VaryingList varyings;
|
| 125 | ActiveUniforms activeUniforms;
|
| 126 | ActiveAttributes activeAttributes;
|
Alexis Hetu | b938c3c | 2015-07-06 15:03:42 -0400 | [diff] [blame] | 127 | ActiveUniformBlocks activeUniformBlocks;
|
Nicolas Capens | 68754bf | 2015-01-22 09:31:59 -0500 | [diff] [blame] | 128 | };
|
| 129 |
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 130 | struct Function
|
| 131 | {
|
| 132 | Function(int label, const char *name, TIntermSequence *arg, TIntermTyped *ret) : label(label), name(name), arg(arg), ret(ret)
|
| 133 | {
|
| 134 | }
|
| 135 |
|
| 136 | Function(int label, const TString &name, TIntermSequence *arg, TIntermTyped *ret) : label(label), name(name), arg(arg), ret(ret)
|
| 137 | {
|
| 138 | }
|
| 139 |
|
| 140 | int label;
|
| 141 | TString name;
|
| 142 | TIntermSequence *arg;
|
| 143 | TIntermTyped *ret;
|
| 144 | };
|
| 145 |
|
| 146 | typedef sw::Shader::Instruction Instruction;
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 147 |
|
| 148 | class Temporary;
|
| 149 |
|
| 150 | class OutputASM : public TIntermTraverser
|
| 151 | {
|
| 152 | public:
|
Nicolas Capens | 68754bf | 2015-01-22 09:31:59 -0500 | [diff] [blame] | 153 | explicit OutputASM(TParseContext &context, Shader *shaderObject);
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 154 | ~OutputASM();
|
| 155 |
|
| 156 | void output();
|
| 157 |
|
| 158 | void freeTemporary(Temporary *temporary);
|
| 159 |
|
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 160 | private:
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 161 | enum Scope
|
| 162 | {
|
| 163 | GLOBAL,
|
| 164 | FUNCTION
|
| 165 | };
|
| 166 |
|
| 167 | void emitShader(Scope scope);
|
| 168 |
|
| 169 | // Visit AST nodes and output their code to the body stream
|
| 170 | virtual void visitSymbol(TIntermSymbol*);
|
| 171 | virtual bool visitBinary(Visit visit, TIntermBinary*);
|
| 172 | virtual bool visitUnary(Visit visit, TIntermUnary*);
|
| 173 | virtual bool visitSelection(Visit visit, TIntermSelection*);
|
| 174 | virtual bool visitAggregate(Visit visit, TIntermAggregate*);
|
| 175 | virtual bool visitLoop(Visit visit, TIntermLoop*);
|
| 176 | virtual bool visitBranch(Visit visit, TIntermBranch*);
|
| 177 |
|
Alexis Hetu | 02a2bb8 | 2015-08-20 14:10:33 -0400 | [diff] [blame] | 178 | sw::Shader::Opcode getOpcode(sw::Shader::Opcode op, TIntermTyped *in) const;
|
Alexis Hetu | c3d95f3 | 2015-09-23 12:27:32 -0400 | [diff] [blame] | 179 | Instruction *emit(sw::Shader::Opcode op, TIntermTyped *dst = 0, TIntermNode *src0 = 0, TIntermNode *src1 = 0, TIntermNode *src2 = 0, TIntermNode *src3 = 0, int index = 0);
|
Nicolas Capens | 2060866 | 2014-06-12 14:05:19 -0400 | [diff] [blame] | 180 | Instruction *emitCast(TIntermTyped *dst, TIntermTyped *src);
|
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 181 | 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] | 182 | void emitAssign(sw::Shader::Opcode op, TIntermTyped *result, TIntermTyped *lhs, TIntermTyped *src0, TIntermTyped *src1 = 0);
|
| 183 | void emitCmp(sw::Shader::Control cmpOp, TIntermTyped *dst, TIntermNode *left, TIntermNode *right, int index = 0);
|
Alexis Hetu | c3d95f3 | 2015-09-23 12:27:32 -0400 | [diff] [blame] | 184 | void emitDeterminant(TIntermTyped *result, TIntermTyped *arg, int size, int col = -1, int row = -1, int outCol = 0, int outRow = 0);
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 185 | void argument(sw::Shader::SourceParameter ¶meter, TIntermNode *argument, int index = 0);
|
| 186 | void copy(TIntermTyped *dst, TIntermNode *src, int offset = 0);
|
Alexis Hetu | 1763eeb | 2014-11-20 10:47:04 -0500 | [diff] [blame] | 187 | void assignLvalue(TIntermTyped *dst, TIntermTyped *src);
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 188 | int lvalue(sw::Shader::DestinationParameter &dst, Temporary &address, TIntermTyped *node);
|
| 189 | sw::Shader::ParameterType registerType(TIntermTyped *operand);
|
| 190 | int registerIndex(TIntermTyped *operand);
|
| 191 | int writeMask(TIntermTyped *destination, int index = 0);
|
Nicolas Capens | 059bece | 2014-05-06 16:44:23 -0400 | [diff] [blame] | 192 | int readSwizzle(TIntermTyped *argument, int size);
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 193 | bool trivial(TIntermTyped *expression, int budget); // Fast to compute and no side effects
|
| 194 | int cost(TIntermNode *expression, int budget);
|
Nicolas Capens | 014b9a6 | 2014-10-15 10:28:29 -0400 | [diff] [blame] | 195 | const Function *findFunction(const TString &name);
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 196 |
|
| 197 | int temporaryRegister(TIntermTyped *temporary);
|
| 198 | int varyingRegister(TIntermTyped *varying);
|
| 199 | void declareVarying(TIntermTyped *varying, int reg);
|
| 200 | int uniformRegister(TIntermTyped *uniform);
|
| 201 | int attributeRegister(TIntermTyped *attribute);
|
Alexis Hetu | f14891d | 2015-11-20 11:46:41 -0500 | [diff] [blame^] | 202 | int fragmentOutputRegister(TIntermTyped *fragmentOutput);
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 203 | int samplerRegister(TIntermTyped *sampler);
|
Nicolas Capens | cdd2924 | 2015-02-12 15:27:55 -0500 | [diff] [blame] | 204 | int samplerRegister(TIntermSymbol *sampler);
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 205 |
|
| 206 | typedef std::vector<TIntermTyped*> VariableArray;
|
| 207 |
|
| 208 | int lookup(VariableArray &list, TIntermTyped *variable);
|
| 209 | int allocate(VariableArray &list, TIntermTyped *variable);
|
| 210 | void free(VariableArray &list, TIntermTyped *variable);
|
| 211 |
|
Alexis Hetu | 5228d3c | 2015-07-22 16:37:02 -0400 | [diff] [blame] | 212 | void declareUniform(const TType &type, const TString &name, int registerIndex, int offset = 0, int blockId = -1);
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 213 | GLenum glVariableType(const TType &type);
|
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 214 | GLenum glVariablePrecision(const TType &type);
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 215 |
|
| 216 | static int dim(TIntermNode *v);
|
| 217 | static int dim2(TIntermNode *m);
|
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 218 | static unsigned int loopCount(TIntermLoop *node);
|
Alexis Hetu | 5b5d562 | 2014-11-24 16:22:57 -0500 | [diff] [blame] | 219 | static bool isSamplerRegister(TIntermTyped *operand);
|
Nicolas Capens | cdd2924 | 2015-02-12 15:27:55 -0500 | [diff] [blame] | 220 | static bool isSamplerRegister(const TType &type);
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 221 |
|
Nicolas Capens | 68754bf | 2015-01-22 09:31:59 -0500 | [diff] [blame] | 222 | Shader *const shaderObject;
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 223 | sw::Shader *shader;
|
| 224 | sw::PixelShader *pixelShader;
|
| 225 | sw::VertexShader *vertexShader;
|
| 226 |
|
| 227 | VariableArray temporaries;
|
| 228 | VariableArray uniforms;
|
| 229 | VariableArray varyings;
|
| 230 | VariableArray attributes;
|
| 231 | VariableArray samplers;
|
Alexis Hetu | f14891d | 2015-11-20 11:46:41 -0500 | [diff] [blame^] | 232 | VariableArray fragmentOutputs;
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 233 |
|
| 234 | Scope emitScope;
|
| 235 | Scope currentScope;
|
| 236 |
|
| 237 | int currentFunction;
|
| 238 | std::vector<Function> functionArray;
|
| 239 |
|
Alexis Hetu | a82b58e | 2014-11-13 16:43:44 -0500 | [diff] [blame] | 240 | TQualifier outputQualifier;
|
| 241 |
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 242 | TParseContext &mContext;
|
| 243 | };
|
John Bauman | d4ae863 | 2014-05-06 16:18:33 -0400 | [diff] [blame] | 244 |
|
| 245 | // Checks whether a loop can run for a variable number of iterations
|
| 246 | class DetectLoopDiscontinuity : public TIntermTraverser
|
| 247 | {
|
| 248 | public:
|
| 249 | bool traverse(TIntermNode *node);
|
| 250 |
|
| 251 | private:
|
| 252 | bool visitBranch(Visit visit, TIntermBranch *node);
|
| 253 | bool visitLoop(Visit visit, TIntermLoop *loop);
|
| 254 | bool visitAggregate(Visit visit, TIntermAggregate *node);
|
| 255 |
|
| 256 | int loopDepth;
|
| 257 | bool loopDiscontinuity;
|
| 258 | };
|
John Bauman | 66b8ab2 | 2014-05-06 15:57:45 -0400 | [diff] [blame] | 259 | }
|
| 260 |
|
| 261 | #endif // COMPILER_OUTPUTASM_H_
|