blob: 995eb166c7b3797e827623bc2f6562c8b78cf021 [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#include "compiler/OutputASM.h"
13
14#include "common/debug.h"
15#include "compiler/InfoSink.h"
16
17#include "libGLESv2/Shader.h"
18
19#define GL_APICALL
20#include <GLES2/gl2.h>
21
22namespace sh
23{
John Bauman66b8ab22014-05-06 15:57:45 -040024 // Integer to TString conversion
25 TString str(int i)
26 {
27 char buffer[20];
28 sprintf(buffer, "%d", i);
29 return buffer;
30 }
31
32 class Temporary : public TIntermSymbol
33 {
34 public:
35 Temporary(OutputASM *assembler) : TIntermSymbol(0, "tmp", TType(EbtFloat, EbpHigh, EvqTemporary, 4, false, false)), assembler(assembler)
36 {
37 }
38
39 ~Temporary()
40 {
41 assembler->freeTemporary(this);
42 }
43
44 private:
45 OutputASM *const assembler;
46 };
47
48 class Constant : public TIntermConstantUnion
49 {
50 public:
51 Constant(float x, float y, float z, float w) : TIntermConstantUnion(constants, TType(EbtFloat, EbpHigh, EvqConst, 4, false, false))
52 {
53 constants[0].setFConst(x);
54 constants[1].setFConst(y);
55 constants[2].setFConst(z);
56 constants[3].setFConst(w);
57 }
58
59 Constant(bool b) : TIntermConstantUnion(constants, TType(EbtBool, EbpHigh, EvqConst, 1, false, false))
60 {
61 constants[0].setBConst(b);
62 }
63
64 Constant(int i) : TIntermConstantUnion(constants, TType(EbtInt, EbpHigh, EvqConst, 1, false, false))
65 {
66 constants[0].setIConst(i);
67 }
68
69 ~Constant()
70 {
71 }
72
73 private:
74 ConstantUnion constants[4];
75 };
76
John Baumand4ae8632014-05-06 16:18:33 -040077 Uniform::Uniform(GLenum type, GLenum precision, const std::string &name, int arraySize, int registerIndex)
John Bauman66b8ab22014-05-06 15:57:45 -040078 {
79 this->type = type;
John Baumand4ae8632014-05-06 16:18:33 -040080 this->precision = precision;
John Bauman66b8ab22014-05-06 15:57:45 -040081 this->name = name;
82 this->arraySize = arraySize;
83 this->registerIndex = registerIndex;
84 }
85
86 Attribute::Attribute()
87 {
88 type = GL_NONE;
89 arraySize = 0;
90 registerIndex = 0;
91 }
92
93 Attribute::Attribute(GLenum type, const std::string &name, int arraySize, int registerIndex)
94 {
95 this->type = type;
96 this->name = name;
97 this->arraySize = arraySize;
98 this->registerIndex = registerIndex;
99 }
100
101 OutputASM::OutputASM(TParseContext &context, gl::Shader *shaderObject) : TIntermTraverser(true, true, true), mContext(context), shaderObject(shaderObject)
102 {
103 shader = 0;
104 pixelShader = 0;
105 vertexShader = 0;
106
107 if(shaderObject)
108 {
109 shader = shaderObject->getShader();
110 pixelShader = shaderObject->getPixelShader();
111 vertexShader = shaderObject->getVertexShader();
112 }
113
114 functionArray.push_back(Function(0, "main(", 0, 0));
115 currentFunction = 0;
116 }
117
118 OutputASM::~OutputASM()
119 {
120 }
121
122 void OutputASM::output()
123 {
124 if(shader)
125 {
126 emitShader(GLOBAL);
127
128 if(functionArray.size() > 1) // Only call main() when there are other functions
129 {
130 Instruction *callMain = emit(sw::Shader::OPCODE_CALL);
131 callMain->dst.type = sw::Shader::PARAMETER_LABEL;
132 callMain->dst.index = 0; // main()
133
134 emit(sw::Shader::OPCODE_RET);
135 }
136
137 emitShader(FUNCTION);
138 }
139 }
140
141 void OutputASM::emitShader(Scope scope)
142 {
143 emitScope = scope;
144 currentScope = GLOBAL;
145 mContext.treeRoot->traverse(this);
146 }
147
148 void OutputASM::freeTemporary(Temporary *temporary)
149 {
150 free(temporaries, temporary);
151 }
152
153 void OutputASM::visitSymbol(TIntermSymbol *symbol)
154 {
155 if(symbol->getQualifier() == EvqVaryingOut || symbol->getQualifier() == EvqInvariantVaryingOut)
156 {
157 // Vertex varyings don't have to be actively used to successfully link
158 // against pixel shaders that use them. So make sure they're declared.
159 declareVarying(symbol, -1);
160 }
161 }
162
163 bool OutputASM::visitBinary(Visit visit, TIntermBinary *node)
164 {
165 if(currentScope != emitScope)
166 {
167 return false;
168 }
169
170 TIntermTyped *result = node;
171 TIntermTyped *left = node->getLeft();
172 TIntermTyped *right = node->getRight();
173 const TType &leftType = left->getType();
174 const TType &rightType = right->getType();
175 const TType &resultType = node->getType();
176
177 switch(node->getOp())
178 {
179 case EOpAssign:
180 if(visit == PostVisit)
181 {
182 assignLvalue(left, right);
183 copy(result, right);
184 }
185 break;
186 case EOpInitialize:
187 if(visit == PostVisit)
188 {
189 copy(left, right);
190 }
191 break;
192 case EOpMatrixTimesScalarAssign:
193 if(visit == PostVisit)
194 {
195 for(int i = 0; i < leftType.getNominalSize(); i++)
196 {
197 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, left, right);
198 mul->dst.index += i;
199 argument(mul->src[0], left, i);
200 }
201
202 assignLvalue(left, result);
203 }
204 break;
205 case EOpVectorTimesMatrixAssign:
206 if(visit == PostVisit)
207 {
208 int size = leftType.getNominalSize();
209
210 for(int i = 0; i < size; i++)
211 {
212 Instruction *dot = emit(sw::Shader::OPCODE_DP(size), result, left, right);
213 dot->dst.mask = 1 << i;
214 argument(dot->src[1], right, i);
215 }
216
217 assignLvalue(left, result);
218 }
219 break;
220 case EOpMatrixTimesMatrixAssign:
221 if(visit == PostVisit)
222 {
223 int dim = leftType.getNominalSize();
224
225 for(int i = 0; i < dim; i++)
226 {
227 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, left, right);
228 mul->dst.index += i;
229 argument(mul->src[1], right, i);
230 mul->src[1].swizzle = 0x00;
231
232 for(int j = 1; j < dim; j++)
233 {
234 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, left, right, result);
235 mad->dst.index += i;
236 argument(mad->src[0], left, j);
237 argument(mad->src[1], right, i);
238 mad->src[1].swizzle = j * 0x55;
239 argument(mad->src[2], result, i);
240 }
241 }
242
243 assignLvalue(left, result);
244 }
245 break;
246 case EOpIndexDirect:
247 if(visit == PostVisit)
248 {
249 int index = right->getAsConstantUnion()->getUnionArrayPointer()->getIConst();
250
251 if(result->isMatrix() || result->isStruct())
252 {
253 ASSERT(left->isArray());
254 copy(result, left, index * left->elementRegisterCount());
255 }
256 else if(result->isRegister())
257 {
258 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, left);
259
260 if(left->isRegister())
261 {
262 mov->src[0].swizzle = index;
263 }
264 else if(left->isArray())
265 {
266 argument(mov->src[0], left, index * left->elementRegisterCount());
267 }
268 else if(left->isMatrix())
269 {
270 ASSERT(index < left->getNominalSize()); // FIXME: Report semantic error
271 argument(mov->src[0], left, index);
272 }
273 else UNREACHABLE();
274 }
275 else UNREACHABLE();
276 }
277 break;
278 case EOpIndexIndirect:
279 if(visit == PostVisit)
280 {
281 if(left->isArray() || left->isMatrix())
282 {
283 for(int index = 0; index < result->totalRegisterCount(); index++)
284 {
285 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, left);
286
287 mov->dst.index += index;
288 argument(mov->src[0], left, index);
289
290 if(left->totalRegisterCount() > 1)
291 {
292 sw::Shader::SourceParameter relativeRegister;
293 argument(relativeRegister, right);
294
295 mov->src[0].rel.type = relativeRegister.type;
296 mov->src[0].rel.index = relativeRegister.index;
297 mov->src[0].rel.scale = result->totalRegisterCount();
298 mov->src[0].rel.deterministic = !(vertexShader && left->getQualifier() == EvqUniform);
299 }
300 }
301 }
302 else if(left->isRegister())
303 {
304 emit(sw::Shader::OPCODE_EXTRACT, result, left, right);
305 }
306 else UNREACHABLE();
307 }
308 break;
309 case EOpIndexDirectStruct:
310 if(visit == PostVisit)
311 {
312 ASSERT(leftType.isStruct());
313
314 const TTypeList *structure = leftType.getStruct();
315 const TString &fieldName = rightType.getFieldName();
316 int fieldOffset = 0;
317
318 for(size_t i = 0; i < structure->size(); i++)
319 {
320 const TType &fieldType = *(*structure)[i].type;
321
322 if(fieldType.getFieldName() == fieldName)
323 {
324 break;
325 }
326
327 fieldOffset += fieldType.totalRegisterCount();
328 }
329
330 copy(result, left, fieldOffset);
331 }
332 break;
333 case EOpVectorSwizzle:
334 if(visit == PostVisit)
335 {
336 int swizzle = 0;
337 TIntermAggregate *components = right->getAsAggregate();
338
339 if(components)
340 {
341 TIntermSequence &sequence = components->getSequence();
342 int component = 0;
343
344 for(TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
345 {
346 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
347
348 if(element)
349 {
350 int i = element->getUnionArrayPointer()[0].getIConst();
351 swizzle |= i << (component * 2);
352 component++;
353 }
354 else UNREACHABLE();
355 }
356 }
357 else UNREACHABLE();
358
359 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, left);
360 mov->src[0].swizzle = swizzle;
361 }
362 break;
John Baumand4ae8632014-05-06 16:18:33 -0400363 case EOpAddAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_ADD, result, left, left, right); break;
364 case EOpAdd: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_ADD, result, left, right); break;
365 case EOpSubAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_SUB, result, left, left, right); break;
366 case EOpSub: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_SUB, result, left, right); break;
367 case EOpMulAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_MUL, result, left, left, right); break;
368 case EOpMul: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_MUL, result, left, right); break;
369 case EOpDivAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_DIV, result, left, left, right); break;
370 case EOpDiv: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_DIV, result, left, right); break;
John Bauman66b8ab22014-05-06 15:57:45 -0400371 case EOpEqual:
372 if(visit == PostVisit)
373 {
374 emitCmp(sw::Shader::CONTROL_EQ, result, left, right);
375
376 for(int index = 1; index < left->totalRegisterCount(); index++)
377 {
378 Temporary equal(this);
379 emitCmp(sw::Shader::CONTROL_EQ, &equal, left, right, index);
380 emit(sw::Shader::OPCODE_AND, result, result, &equal);
381 }
382 }
383 break;
384 case EOpNotEqual:
385 if(visit == PostVisit)
386 {
387 emitCmp(sw::Shader::CONTROL_NE, result, left, right);
388
389 for(int index = 1; index < left->totalRegisterCount(); index++)
390 {
391 Temporary notEqual(this);
392 emitCmp(sw::Shader::CONTROL_NE, &notEqual, left, right, index);
393 emit(sw::Shader::OPCODE_OR, result, result, &notEqual);
394 }
395 }
396 break;
397 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, left, right); break;
398 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, left, right); break;
399 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, left, right); break;
400 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, left, right); break;
401 case EOpVectorTimesScalarAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_MUL, result, left, left, right); break;
402 case EOpVectorTimesScalar: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, left, right); break;
403 case EOpMatrixTimesScalar:
404 if(visit == PostVisit)
405 {
406 for(int i = 0; i < leftType.getNominalSize(); i++)
407 {
408 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, left, right);
409 mul->dst.index += i;
410 argument(mul->src[0], left, i);
411 }
412 }
413 break;
414 case EOpVectorTimesMatrix:
415 if(visit == PostVisit)
416 {
417 int size = leftType.getNominalSize();
418
419 for(int i = 0; i < size; i++)
420 {
421 Instruction *dot = emit(sw::Shader::OPCODE_DP(size), result, left, right);
422 dot->dst.mask = 1 << i;
423 argument(dot->src[1], right, i);
424 }
425 }
426 break;
427 case EOpMatrixTimesVector:
428 if(visit == PostVisit)
429 {
430 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, left, right);
431 mul->src[1].swizzle = 0x00;
432
433 for(int i = 1; i < leftType.getNominalSize(); i++)
434 {
435 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, left, right, result);
436 argument(mad->src[0], left, i);
437 mad->src[1].swizzle = i * 0x55;
438 }
439 }
440 break;
441 case EOpMatrixTimesMatrix:
442 if(visit == PostVisit)
443 {
444 int dim = leftType.getNominalSize();
445
446 for(int i = 0; i < dim; i++)
447 {
448 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, left, right);
449 mul->dst.index += i;
450 argument(mul->src[1], right, i);
451 mul->src[1].swizzle = 0x00;
452
453 for(int j = 1; j < dim; j++)
454 {
455 Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, left, right, result);
456 mad->dst.index += i;
457 argument(mad->src[0], left, j);
458 argument(mad->src[1], right, i);
459 mad->src[1].swizzle = j * 0x55;
460 argument(mad->src[2], result, i);
461 }
462 }
463 }
464 break;
465 case EOpLogicalOr:
466 if(trivial(right, 6))
467 {
468 if(visit == PostVisit)
469 {
470 emit(sw::Shader::OPCODE_OR, result, left, right);
471 }
472 }
473 else // Short-circuit evaluation
474 {
475 if(visit == InVisit)
476 {
477 emit(sw::Shader::OPCODE_MOV, result, left);
478 Instruction *ifnot = emit(sw::Shader::OPCODE_IF, 0, result);
479 ifnot->src[0].modifier = sw::Shader::MODIFIER_NOT;
480 }
481 else if(visit == PostVisit)
482 {
483 emit(sw::Shader::OPCODE_MOV, result, right);
484 emit(sw::Shader::OPCODE_ENDIF);
485 }
486 }
487 break;
488 case EOpLogicalXor: if(visit == PostVisit) emit(sw::Shader::OPCODE_XOR, result, left, right); break;
489 case EOpLogicalAnd:
490 if(trivial(right, 6))
491 {
492 if(visit == PostVisit)
493 {
494 emit(sw::Shader::OPCODE_AND, result, left, right);
495 }
496 }
497 else // Short-circuit evaluation
498 {
499 if(visit == InVisit)
500 {
501 emit(sw::Shader::OPCODE_MOV, result, left);
502 emit(sw::Shader::OPCODE_IF, 0, result);
503 }
504 else if(visit == PostVisit)
505 {
506 emit(sw::Shader::OPCODE_MOV, result, right);
507 emit(sw::Shader::OPCODE_ENDIF);
508 }
509 }
510 break;
511 default: UNREACHABLE();
512 }
513
514 return true;
515 }
516
517 bool OutputASM::visitUnary(Visit visit, TIntermUnary *node)
518 {
519 if(currentScope != emitScope)
520 {
521 return false;
522 }
523
524 Constant one(1.0f, 1.0f, 1.0f, 1.0f);
525 Constant rad(1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f, 1.74532925e-2f);
526 Constant deg(5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f, 5.72957795e+1f);
527
528 TIntermTyped *result = node;
529 TIntermTyped *arg = node->getOperand();
530
531 switch(node->getOp())
532 {
533 case EOpNegative:
534 if(visit == PostVisit)
535 {
536 for(int index = 0; index < arg->totalRegisterCount(); index++)
537 {
538 Instruction *neg = emit(sw::Shader::OPCODE_MOV, result, arg);
539 neg->dst.index += index;
540 argument(neg->src[0], arg, index);
541 neg->src[0].modifier = sw::Shader::MODIFIER_NEGATE;
542 }
543 }
544 break;
545 case EOpVectorLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
546 case EOpLogicalNot: if(visit == PostVisit) emit(sw::Shader::OPCODE_NOT, result, arg); break;
547 case EOpPostIncrement:
548 if(visit == PostVisit)
549 {
550 copy(result, arg);
551
552 for(int index = 0; index < arg->totalRegisterCount(); index++)
553 {
554 Instruction *add = emit(sw::Shader::OPCODE_ADD, arg, arg, &one);
555 add->dst.index += index;
556 argument(add->src[0], arg, index);
557 }
558
559 assignLvalue(arg, arg);
560 }
561 break;
562 case EOpPostDecrement:
563 if(visit == PostVisit)
564 {
565 copy(result, arg);
566
567 for(int index = 0; index < arg->totalRegisterCount(); index++)
568 {
569 Instruction *sub = emit(sw::Shader::OPCODE_SUB, arg, arg, &one);
570 sub->dst.index += index;
571 argument(sub->src[0], arg, index);
572 }
573
574 assignLvalue(arg, arg);
575 }
576 break;
577 case EOpPreIncrement:
578 if(visit == PostVisit)
579 {
580 for(int index = 0; index < arg->totalRegisterCount(); index++)
581 {
582 Instruction *add = emit(sw::Shader::OPCODE_ADD, result, arg, &one);
583 add->dst.index += index;
584 argument(add->src[0], arg, index);
585 }
586
587 assignLvalue(arg, result);
588 }
589 break;
590 case EOpPreDecrement:
591 if(visit == PostVisit)
592 {
593 for(int index = 0; index < arg->totalRegisterCount(); index++)
594 {
595 Instruction *sub = emit(sw::Shader::OPCODE_SUB, result, arg, &one);
596 sub->dst.index += index;
597 argument(sub->src[0], arg, index);
598 }
599
600 assignLvalue(arg, result);
601 }
602 break;
603 case EOpConvIntToBool: if(visit == PostVisit) emit(sw::Shader::OPCODE_F2B, result, arg); break; // Integers are implemented as float
604 case EOpConvFloatToBool: if(visit == PostVisit) emit(sw::Shader::OPCODE_F2B, result, arg); break;
605 case EOpConvBoolToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_B2F, result, arg); break;
606 case EOpConvIntToFloat: if(visit == PostVisit) emit(sw::Shader::OPCODE_MOV, result, arg); break; // Integers are implemented as float
607 case EOpConvFloatToInt: if(visit == PostVisit) emit(sw::Shader::OPCODE_TRUNC, result, arg); break; // Integers are implemented as float
608 case EOpConvBoolToInt: if(visit == PostVisit) emit(sw::Shader::OPCODE_B2F, result, arg); break; // Integers are implemented as float
609 case EOpRadians: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &rad); break;
610 case EOpDegrees: if(visit == PostVisit) emit(sw::Shader::OPCODE_MUL, result, arg, &deg); break;
611 case EOpSin: if(visit == PostVisit) emit(sw::Shader::OPCODE_SIN, result, arg); break;
612 case EOpCos: if(visit == PostVisit) emit(sw::Shader::OPCODE_COS, result, arg); break;
613 case EOpTan: if(visit == PostVisit) emit(sw::Shader::OPCODE_TAN, result, arg); break;
614 case EOpAsin: if(visit == PostVisit) emit(sw::Shader::OPCODE_ASIN, result, arg); break;
615 case EOpAcos: if(visit == PostVisit) emit(sw::Shader::OPCODE_ACOS, result, arg); break;
616 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN, result, arg); break;
617 case EOpExp: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP, result, arg); break;
618 case EOpLog: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG, result, arg); break;
619 case EOpExp2: if(visit == PostVisit) emit(sw::Shader::OPCODE_EXP2, result, arg); break;
620 case EOpLog2: if(visit == PostVisit) emit(sw::Shader::OPCODE_LOG2, result, arg); break;
621 case EOpSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_SQRT, result, arg); break;
622 case EOpInverseSqrt: if(visit == PostVisit) emit(sw::Shader::OPCODE_RSQ, result, arg); break;
623 case EOpAbs: if(visit == PostVisit) emit(sw::Shader::OPCODE_ABS, result, arg); break;
624 case EOpSign: if(visit == PostVisit) emit(sw::Shader::OPCODE_SGN, result, arg); break;
625 case EOpFloor: if(visit == PostVisit) emit(sw::Shader::OPCODE_FLOOR, result, arg); break;
626 case EOpCeil: if(visit == PostVisit) emit(sw::Shader::OPCODE_CEIL, result, arg, result); break;
627 case EOpFract: if(visit == PostVisit) emit(sw::Shader::OPCODE_FRC, result, arg); break;
628 case EOpLength: if(visit == PostVisit) emit(sw::Shader::OPCODE_LEN(dim(arg)), result, arg); break;
629 case EOpNormalize: if(visit == PostVisit) emit(sw::Shader::OPCODE_NRM(dim(arg)), result, arg); break;
630 case EOpDFdx: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDX, result, arg); break;
631 case EOpDFdy: if(visit == PostVisit) emit(sw::Shader::OPCODE_DFDY, result, arg); break;
632 case EOpFwidth: if(visit == PostVisit) emit(sw::Shader::OPCODE_FWIDTH, result, arg); break;
633 case EOpAny: if(visit == PostVisit) emit(sw::Shader::OPCODE_ANY, result, arg); break;
634 case EOpAll: if(visit == PostVisit) emit(sw::Shader::OPCODE_ALL, result, arg); break;
635 default: UNREACHABLE();
636 }
637
638 return true;
639 }
640
641 bool OutputASM::visitAggregate(Visit visit, TIntermAggregate *node)
642 {
643 if(currentScope != emitScope && node->getOp() != EOpFunction && node->getOp() != EOpSequence)
644 {
645 return false;
646 }
647
648 TIntermTyped *result = node;
649 const TType &resultType = node->getType();
650 TIntermSequence &arg = node->getSequence();
651 int argumentCount = arg.size();
652
653 switch(node->getOp())
654 {
655 case EOpSequence: break;
656 case EOpDeclaration: break;
657 case EOpPrototype: break;
658 case EOpComma:
659 if(visit == PostVisit)
660 {
661 copy(result, arg[1]);
662 }
663 break;
664 case EOpFunction:
665 if(visit == PreVisit)
666 {
667 const TString &name = node->getName();
668
669 if(emitScope == FUNCTION)
670 {
671 if(functionArray.size() > 1) // No need for a label when there's only main()
672 {
673 Instruction *label = emit(sw::Shader::OPCODE_LABEL);
674 label->dst.type = sw::Shader::PARAMETER_LABEL;
675
676 const Function &function = findFunction(name);
677 label->dst.index = function.label;
678 currentFunction = function.label;
679 }
680 }
681 else if(emitScope == GLOBAL)
682 {
683 if(name != "main(")
684 {
685 TIntermSequence &arguments = node->getSequence()[0]->getAsAggregate()->getSequence();
686 functionArray.push_back(Function(functionArray.size(), name, &arguments, node));
687 }
688 }
689 else UNREACHABLE();
690
691 currentScope = FUNCTION;
692 }
693 else if(visit == PostVisit)
694 {
695 if(emitScope == FUNCTION)
696 {
697 if(functionArray.size() > 1) // No need to return when there's only main()
698 {
699 emit(sw::Shader::OPCODE_RET);
700 }
701 }
702
703 currentScope = GLOBAL;
704 }
705 break;
706 case EOpFunctionCall:
707 if(visit == PostVisit)
708 {
709 if(node->isUserDefined())
710 {
711 const TString &name = node->getName();
712 const Function &function = findFunction(name);
713 TIntermSequence &arguments = *function.arg;
714
715 for(int i = 0; i < argumentCount; i++)
716 {
717 TIntermTyped *in = arguments[i]->getAsTyped();
718
719 if(in->getQualifier() == EvqIn ||
720 in->getQualifier() == EvqInOut ||
721 in->getQualifier() == EvqConstReadOnly)
722 {
723 copy(in, arg[i]);
724 }
725 }
726
727 Instruction *call = emit(sw::Shader::OPCODE_CALL);
728 call->dst.type = sw::Shader::PARAMETER_LABEL;
729 call->dst.index = function.label;
730
731 if(function.ret && function.ret->getType().getBasicType() != EbtVoid)
732 {
733 copy(result, function.ret);
734 }
735
736 for(int i = 0; i < argumentCount; i++)
737 {
738 TIntermTyped *argument = arguments[i]->getAsTyped();
739 TIntermTyped *out = arg[i]->getAsTyped();
740
741 if(argument->getQualifier() == EvqOut ||
742 argument->getQualifier() == EvqInOut)
743 {
744 copy(out, argument);
745 }
746 }
747 }
748 else
749 {
750 TString name = TFunction::unmangleName(node->getName());
751
752 if(name == "texture2D" || name == "textureCube")
753 {
754 if(argumentCount == 2)
755 {
756 emit(sw::Shader::OPCODE_TEX, result, arg[1], arg[0]);
757 }
758 else if(argumentCount == 3) // bias
759 {
760 Temporary uvwb(this);
761 emit(sw::Shader::OPCODE_MOV, &uvwb, arg[1]);
762 Instruction *bias = emit(sw::Shader::OPCODE_MOV, &uvwb, arg[2]);
763 bias->dst.mask = 0x8;
764
765 Instruction *tex = emit(sw::Shader::OPCODE_TEX, result, &uvwb, arg[0]); // FIXME: Implement an efficient TEXLDB instruction
766 tex->bias = true;
767 }
768 else UNREACHABLE();
769 }
770 else if(name == "texture2DProj")
771 {
772 TIntermTyped *t = arg[1]->getAsTyped();
773
774 if(argumentCount == 2)
775 {
776 Instruction *tex = emit(sw::Shader::OPCODE_TEX, result, arg[1], arg[0]);
777 tex->project = true;
778
779 if(t->getNominalSize() == 3)
780 {
781 tex->src[0].swizzle = 0xA4;
782 }
783 else ASSERT(t->getNominalSize() == 4);
784 }
785 else if(argumentCount == 3) // bias
786 {
787 Temporary proj(this);
John Bauman66b8ab22014-05-06 15:57:45 -0400788
789 if(t->getNominalSize() == 3)
790 {
Nicolas Capens49e3cb52014-05-06 23:50:46 -0400791 Instruction *div = emit(sw::Shader::OPCODE_DIV, &proj, arg[1], arg[1]);
792 div->src[1].swizzle = 0xAA;
John Bauman66b8ab22014-05-06 15:57:45 -0400793 div->dst.mask = 0x3;
794 }
795 else if(t->getNominalSize() == 4)
796 {
Nicolas Capens49e3cb52014-05-06 23:50:46 -0400797 Instruction *div = emit(sw::Shader::OPCODE_DIV, &proj, arg[1], arg[1]);
798 div->src[1].swizzle = 0xFF;
John Bauman66b8ab22014-05-06 15:57:45 -0400799 div->dst.mask = 0x3;
800 }
801 else UNREACHABLE();
802
803 Instruction *bias = emit(sw::Shader::OPCODE_MOV, &proj, arg[2]);
804 bias->dst.mask = 0x8;
805
806 Instruction *tex = emit(sw::Shader::OPCODE_TEX, result, &proj, arg[0]);
807 tex->bias = true;
808 }
809 else UNREACHABLE();
810 }
811 else if(name == "texture2DLod" || name == "textureCubeLod")
812 {
813 Temporary uvwb(this);
814 emit(sw::Shader::OPCODE_MOV, &uvwb, arg[1]);
815 Instruction *lod = emit(sw::Shader::OPCODE_MOV, &uvwb, arg[2]);
816 lod->dst.mask = 0x8;
817
818 emit(sw::Shader::OPCODE_TEXLDL, result, &uvwb, arg[0]);
819 }
820 else if(name == "texture2DProjLod")
821 {
822 TIntermTyped *t = arg[1]->getAsTyped();
John Bauman66b8ab22014-05-06 15:57:45 -0400823 Temporary proj(this);
John Bauman66b8ab22014-05-06 15:57:45 -0400824
825 if(t->getNominalSize() == 3)
826 {
Nicolas Capens49e3cb52014-05-06 23:50:46 -0400827 Instruction *div = emit(sw::Shader::OPCODE_DIV, &proj, arg[1], arg[1]);
828 div->src[1].swizzle = 0xAA;
John Bauman66b8ab22014-05-06 15:57:45 -0400829 div->dst.mask = 0x3;
830 }
831 else if(t->getNominalSize() == 4)
832 {
Nicolas Capens49e3cb52014-05-06 23:50:46 -0400833 Instruction *div = emit(sw::Shader::OPCODE_DIV, &proj, arg[1], arg[1]);
834 div->src[1].swizzle = 0xFF;
John Bauman66b8ab22014-05-06 15:57:45 -0400835 div->dst.mask = 0x3;
836 }
837 else UNREACHABLE();
838
839 Instruction *lod = emit(sw::Shader::OPCODE_MOV, &proj, arg[2]);
840 lod->dst.mask = 0x8;
841
842 emit(sw::Shader::OPCODE_TEXLDL, result, &proj, arg[0]);
843 }
844 else UNREACHABLE();
845 }
846 }
847 break;
848 case EOpParameters: break;
849 case EOpConstructFloat:
850 case EOpConstructVec2:
851 case EOpConstructVec3:
852 case EOpConstructVec4:
853 case EOpConstructBool:
854 case EOpConstructBVec2:
855 case EOpConstructBVec3:
856 case EOpConstructBVec4:
857 case EOpConstructInt:
858 case EOpConstructIVec2:
859 case EOpConstructIVec3:
860 case EOpConstructIVec4:
861 if(visit == PostVisit)
862 {
863 int component = 0;
864
865 for(int i = 0; i < argumentCount; i++)
866 {
867 TIntermTyped *argi = arg[i]->getAsTyped();
868 int size = argi->getNominalSize();
869 ASSERT(!argi->isMatrix());
870
871 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, argi);
872 mov->dst.mask = (0xF << component) & 0xF;
Nicolas Capens059bece2014-05-06 16:44:23 -0400873 mov->src[0].swizzle = readSwizzle(argi, size) << (component * 2);
John Bauman66b8ab22014-05-06 15:57:45 -0400874
875 component += size;
876 }
877 }
878 break;
879 case EOpConstructMat2:
880 case EOpConstructMat3:
881 case EOpConstructMat4:
882 if(visit == PostVisit)
883 {
884 TIntermTyped *arg0 = arg[0]->getAsTyped();
885 const int dim = result->getNominalSize();
886
887 if(arg0->isMatrix())
888 {
889 for(int i = 0; i < dim; i++)
890 {
891 if(dim > dim2(arg0))
892 {
893 // Initialize to identity matrix
894 Constant col((i == 0 ? 1.0f : 0.0f), (i == 1 ? 1.0f : 0.0f), (i == 2 ? 1.0f : 0.0f), (i == 3 ? 1.0f : 0.0f));
895 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, &col);
896 mov->dst.index += i;
897 }
898
899 if(i < dim2(arg0))
900 {
901 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, arg0);
902 mov->dst.index += i;
903 mov->dst.mask = 0xF >> (4 - dim2(arg0));
904 argument(mov->src[0], arg0, i);
905 }
906 }
907 }
908 else
909 {
910 int column = 0;
911 int row = 0;
912
913 for(int i = 0; i < argumentCount; i++)
914 {
915 TIntermTyped *argi = arg[i]->getAsTyped();
916 int size = argi->getNominalSize();
917 int element = 0;
918
919 while(element < size)
920 {
921 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, argi);
922 mov->dst.index += column;
923 mov->dst.mask = (0xF << row) & 0xF;
Nicolas Capens059bece2014-05-06 16:44:23 -0400924 mov->src[0].swizzle = (readSwizzle(argi, size) << (row * 2)) + 0x55 * element;
John Bauman66b8ab22014-05-06 15:57:45 -0400925
926 int end = row + size - element;
927 column = end >= dim ? column + 1 : column;
928 element = element + dim - row;
929 row = end >= dim ? 0 : end;
930 }
931 }
932 }
933 }
934 break;
935 case EOpConstructStruct:
936 if(visit == PostVisit)
937 {
938 int offset = 0;
939 for(int i = 0; i < argumentCount; i++)
940 {
941 TIntermTyped *argi = arg[i]->getAsTyped();
942 int size = argi->totalRegisterCount();
943
944 for(int index = 0; index < size; index++)
945 {
946 Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, argi);
947 mov->dst.index += index + offset;
948 mov->dst.mask = writeMask(result, offset + index);
949 argument(mov->src[0], argi, index);
950 }
951
952 offset += size;
953 }
954 }
955 break;
956 case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, arg[0], arg[1]); break;
957 case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, arg[0], arg[1]); break;
958 case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, arg[0], arg[1]); break;
959 case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, arg[0], arg[1]); break;
960 case EOpVectorEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_EQ, result, arg[0], arg[1]); break;
961 case EOpVectorNotEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_NE, result, arg[0], arg[1]); break;
962 case EOpMod: if(visit == PostVisit) emit(sw::Shader::OPCODE_MOD, result, arg[0], arg[1]); break;
963 case EOpPow: if(visit == PostVisit) emit(sw::Shader::OPCODE_POW, result, arg[0], arg[1]); break;
964 case EOpAtan: if(visit == PostVisit) emit(sw::Shader::OPCODE_ATAN2, result, arg[0], arg[1]); break;
965 case EOpMin: if(visit == PostVisit) emit(sw::Shader::OPCODE_MIN, result, arg[0], arg[1]); break;
966 case EOpMax: if(visit == PostVisit) emit(sw::Shader::OPCODE_MAX, result, arg[0], arg[1]); break;
967 case EOpClamp:
968 if(visit == PostVisit)
969 {
970 emit(sw::Shader::OPCODE_MAX, result, arg[0], arg[1]);
971 emit(sw::Shader::OPCODE_MIN, result, result, arg[2]);
972 }
973 break;
974 case EOpMix: if(visit == PostVisit) emit(sw::Shader::OPCODE_LRP, result, arg[2], arg[1], arg[0]); break;
975 case EOpStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_STEP, result, arg[0], arg[1]); break;
976 case EOpSmoothStep: if(visit == PostVisit) emit(sw::Shader::OPCODE_SMOOTH, result, arg[0], arg[1], arg[2]); break;
977 case EOpDistance: if(visit == PostVisit) emit(sw::Shader::OPCODE_DIST(dim(arg[0])), result, arg[0], arg[1]); break;
978 case EOpDot: if(visit == PostVisit) emit(sw::Shader::OPCODE_DP(dim(arg[0])), result, arg[0], arg[1]); break;
979 case EOpCross: if(visit == PostVisit) emit(sw::Shader::OPCODE_CRS, result, arg[0], arg[1]); break;
980 case EOpFaceForward: if(visit == PostVisit) emit(sw::Shader::OPCODE_FORWARD(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
981 case EOpReflect: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFLECT(dim(arg[0])), result, arg[0], arg[1]); break;
982 case EOpRefract: if(visit == PostVisit) emit(sw::Shader::OPCODE_REFRACT(dim(arg[0])), result, arg[0], arg[1], arg[2]); break;
983 case EOpMul:
984 if(visit == PostVisit)
985 {
986 ASSERT(dim2(arg[0]) == dim2(arg[1]));
987
988 for(int i = 0; i < dim2(arg[0]); i++)
989 {
990 Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, arg[0], arg[1]);
991 mul->dst.index += i;
992 argument(mul->src[0], arg[0], i);
993 argument(mul->src[1], arg[1], i);
994 }
995 }
996 break;
997 default: UNREACHABLE();
998 }
999
1000 return true;
1001 }
1002
1003 bool OutputASM::visitSelection(Visit visit, TIntermSelection *node)
1004 {
1005 if(currentScope != emitScope)
1006 {
1007 return false;
1008 }
1009
1010 TIntermTyped *condition = node->getCondition();
1011 TIntermNode *trueBlock = node->getTrueBlock();
1012 TIntermNode *falseBlock = node->getFalseBlock();
1013 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
1014
1015 condition->traverse(this);
1016
1017 if(node->usesTernaryOperator())
1018 {
1019 if(constantCondition)
1020 {
1021 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1022
1023 if(trueCondition)
1024 {
1025 trueBlock->traverse(this);
1026 copy(node, trueBlock);
1027 }
1028 else
1029 {
1030 falseBlock->traverse(this);
1031 copy(node, falseBlock);
1032 }
1033 }
1034 else if(trivial(node, 6)) // Fast to compute both potential results and no side effects
1035 {
1036 trueBlock->traverse(this);
1037 falseBlock->traverse(this);
1038 emit(sw::Shader::OPCODE_SELECT, node, condition, trueBlock, falseBlock);
1039 }
1040 else
1041 {
1042 emit(sw::Shader::OPCODE_IF, 0, condition);
1043
1044 if(trueBlock)
1045 {
1046 trueBlock->traverse(this);
1047 copy(node, trueBlock);
1048 }
1049
1050 if(falseBlock)
1051 {
1052 emit(sw::Shader::OPCODE_ELSE);
1053 falseBlock->traverse(this);
1054 copy(node, falseBlock);
1055 }
1056
1057 emit(sw::Shader::OPCODE_ENDIF);
1058 }
1059 }
1060 else // if/else statement
1061 {
1062 if(constantCondition)
1063 {
1064 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1065
1066 if(trueCondition)
1067 {
1068 if(trueBlock)
1069 {
1070 trueBlock->traverse(this);
1071 }
1072 }
1073 else
1074 {
1075 if(falseBlock)
1076 {
1077 falseBlock->traverse(this);
1078 }
1079 }
1080 }
1081 else
1082 {
1083 emit(sw::Shader::OPCODE_IF, 0, condition);
1084
1085 if(trueBlock)
1086 {
1087 trueBlock->traverse(this);
1088 }
1089
1090 if(falseBlock)
1091 {
1092 emit(sw::Shader::OPCODE_ELSE);
1093 falseBlock->traverse(this);
1094 }
1095
1096 emit(sw::Shader::OPCODE_ENDIF);
1097 }
1098 }
1099
1100 return false;
1101 }
1102
1103 bool OutputASM::visitLoop(Visit visit, TIntermLoop *node)
1104 {
1105 if(currentScope != emitScope)
1106 {
1107 return false;
1108 }
1109
John Baumand4ae8632014-05-06 16:18:33 -04001110 unsigned int iterations = loopCount(node);
1111
1112 if(iterations == 0)
1113 {
1114 return false;
1115 }
1116
1117 bool unroll = (iterations <= 4);
1118
1119 if(unroll)
1120 {
1121 DetectLoopDiscontinuity detectLoopDiscontinuity;
1122 unroll = !detectLoopDiscontinuity.traverse(node);
1123 }
1124
John Bauman66b8ab22014-05-06 15:57:45 -04001125 TIntermNode *init = node->getInit();
1126 TIntermTyped *condition = node->getCondition();
1127 TIntermTyped *expression = node->getExpression();
1128 TIntermNode *body = node->getBody();
1129
1130 if(node->getType() == ELoopDoWhile)
1131 {
1132 Temporary iterate(this);
1133 Constant True(true);
1134 emit(sw::Shader::OPCODE_MOV, &iterate, &True);
1135
1136 emit(sw::Shader::OPCODE_WHILE, 0, &iterate); // FIXME: Implement real do-while
1137
1138 if(body)
1139 {
1140 body->traverse(this);
1141 }
1142
1143 emit(sw::Shader::OPCODE_TEST);
1144
1145 condition->traverse(this);
1146 emit(sw::Shader::OPCODE_MOV, &iterate, condition);
1147
1148 emit(sw::Shader::OPCODE_ENDWHILE);
1149 }
1150 else
1151 {
1152 if(init)
1153 {
1154 init->traverse(this);
1155 }
1156
John Baumand4ae8632014-05-06 16:18:33 -04001157 if(unroll)
John Bauman66b8ab22014-05-06 15:57:45 -04001158 {
John Baumand4ae8632014-05-06 16:18:33 -04001159 for(unsigned int i = 0; i < iterations; i++)
1160 {
1161 // condition->traverse(this); // Condition could contain statements, but not in an unrollable loop
1162
1163 if(body)
1164 {
1165 body->traverse(this);
1166 }
1167
1168 if(expression)
1169 {
1170 expression->traverse(this);
1171 }
1172 }
John Bauman66b8ab22014-05-06 15:57:45 -04001173 }
John Baumand4ae8632014-05-06 16:18:33 -04001174 else
John Bauman66b8ab22014-05-06 15:57:45 -04001175 {
John Baumand4ae8632014-05-06 16:18:33 -04001176 condition->traverse(this);
1177
1178 emit(sw::Shader::OPCODE_WHILE, 0, condition);
1179
1180 if(body)
1181 {
1182 body->traverse(this);
1183 }
1184
1185 emit(sw::Shader::OPCODE_TEST);
1186
1187 if(expression)
1188 {
1189 expression->traverse(this);
1190 }
1191
1192 condition->traverse(this);
1193
1194 emit(sw::Shader::OPCODE_ENDWHILE);
John Bauman66b8ab22014-05-06 15:57:45 -04001195 }
John Bauman66b8ab22014-05-06 15:57:45 -04001196 }
1197
1198 return false;
1199 }
1200
1201 bool OutputASM::visitBranch(Visit visit, TIntermBranch *node)
1202 {
1203 if(currentScope != emitScope)
1204 {
1205 return false;
1206 }
1207
1208 switch(node->getFlowOp())
1209 {
1210 case EOpKill: if(visit == PostVisit) emit(sw::Shader::OPCODE_DISCARD); break;
1211 case EOpBreak: if(visit == PostVisit) emit(sw::Shader::OPCODE_BREAK); break;
1212 case EOpContinue: if(visit == PostVisit) emit(sw::Shader::OPCODE_CONTINUE); break;
1213 case EOpReturn:
1214 if(visit == PostVisit)
1215 {
1216 TIntermTyped *value = node->getExpression();
1217
1218 if(value)
1219 {
1220 copy(functionArray[currentFunction].ret, value);
1221 }
1222
1223 emit(sw::Shader::OPCODE_LEAVE);
1224 }
1225 break;
1226 default: UNREACHABLE();
1227 }
1228
1229 return true;
1230 }
1231
John Baumand4ae8632014-05-06 16:18:33 -04001232 Instruction *OutputASM::emit(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2, int index)
John Bauman66b8ab22014-05-06 15:57:45 -04001233 {
1234 if(dst && registerType(dst) == sw::Shader::PARAMETER_SAMPLER)
1235 {
John Baumand4ae8632014-05-06 16:18:33 -04001236 op = sw::Shader::OPCODE_NULL; // Can't assign to a sampler, but this is hit when indexing sampler arrays
John Bauman66b8ab22014-05-06 15:57:45 -04001237 }
1238
1239 Instruction *instruction = new Instruction(op);
1240
1241 if(dst)
1242 {
1243 instruction->dst.type = registerType(dst);
John Baumand4ae8632014-05-06 16:18:33 -04001244 instruction->dst.index = registerIndex(dst) + index;
John Bauman66b8ab22014-05-06 15:57:45 -04001245 instruction->dst.mask = writeMask(dst);
1246 instruction->dst.integer = (dst->getBasicType() == EbtInt);
1247 }
1248
John Baumand4ae8632014-05-06 16:18:33 -04001249 argument(instruction->src[0], src0, index);
1250 argument(instruction->src[1], src1, index);
1251 argument(instruction->src[2], src2, index);
John Bauman66b8ab22014-05-06 15:57:45 -04001252
1253 shader->append(instruction);
1254
1255 return instruction;
1256 }
1257
John Baumand4ae8632014-05-06 16:18:33 -04001258 void OutputASM::emitBinary(sw::Shader::Opcode op, TIntermTyped *dst, TIntermNode *src0, TIntermNode *src1, TIntermNode *src2)
1259 {
1260 for(int index = 0; index < dst->elementRegisterCount(); index++)
1261 {
1262 emit(op, dst, src0, src1, src2, index);
1263 }
1264 }
1265
John Bauman66b8ab22014-05-06 15:57:45 -04001266 void OutputASM::emitAssign(sw::Shader::Opcode op, TIntermTyped *result, TIntermTyped *lhs, TIntermTyped *src0, TIntermTyped *src1)
1267 {
John Baumand4ae8632014-05-06 16:18:33 -04001268 emitBinary(op, result, src0, src1);
John Bauman66b8ab22014-05-06 15:57:45 -04001269 assignLvalue(lhs, result);
1270 }
1271
1272 void OutputASM::emitCmp(sw::Shader::Control cmpOp, TIntermTyped *dst, TIntermNode *left, TIntermNode *right, int index)
1273 {
1274 bool boolean = (left->getAsTyped()->getBasicType() == EbtBool);
1275 sw::Shader::Opcode opcode = boolean ? sw::Shader::OPCODE_ICMP : sw::Shader::OPCODE_CMP;
1276
1277 Instruction *cmp = emit(opcode, dst, left, right);
1278 cmp->control = cmpOp;
1279 argument(cmp->src[0], left, index);
1280 argument(cmp->src[1], right, index);
1281 }
1282
1283 int componentCount(const TType &type, int registers)
1284 {
1285 if(registers == 0)
1286 {
1287 return 0;
1288 }
1289
1290 if(type.isArray() && registers >= type.elementRegisterCount())
1291 {
1292 int index = registers / type.elementRegisterCount();
1293 registers -= index * type.elementRegisterCount();
1294 return index * type.getElementSize() + componentCount(type, registers);
1295 }
1296
1297 if(type.isStruct())
1298 {
1299 TTypeList *structure = type.getStruct();
1300 int elements = 0;
1301
1302 for(TTypeList::const_iterator field = structure->begin(); field != structure->end(); field++)
1303 {
1304 const TType &fieldType = *field->type;
1305
1306 if(fieldType.totalRegisterCount() <= registers)
1307 {
1308 registers -= fieldType.totalRegisterCount();
1309 elements += fieldType.getObjectSize();
1310 }
1311 else // Register within this field
1312 {
1313 return elements + componentCount(fieldType, registers);
1314 }
1315 }
1316 }
1317 else if(type.isMatrix())
1318 {
1319 return registers * type.getNominalSize();
1320 }
1321
1322 UNREACHABLE();
1323 return 0;
1324 }
1325
1326 int registerSize(const TType &type, int registers)
1327 {
1328 if(registers == 0)
1329 {
1330 if(type.isStruct())
1331 {
1332 return registerSize(*type.getStruct()->begin()->type, 0);
1333 }
1334
1335 return type.getNominalSize();
1336 }
1337
1338 if(type.isArray() && registers >= type.elementRegisterCount())
1339 {
1340 int index = registers / type.elementRegisterCount();
1341 registers -= index * type.elementRegisterCount();
1342 return registerSize(type, registers);
1343 }
1344
1345 if(type.isStruct())
1346 {
1347 TTypeList *structure = type.getStruct();
1348 int elements = 0;
1349
1350 for(TTypeList::const_iterator field = structure->begin(); field != structure->end(); field++)
1351 {
1352 const TType &fieldType = *field->type;
1353
1354 if(fieldType.totalRegisterCount() <= registers)
1355 {
1356 registers -= fieldType.totalRegisterCount();
1357 elements += fieldType.getObjectSize();
1358 }
1359 else // Register within this field
1360 {
1361 return registerSize(fieldType, registers);
1362 }
1363 }
1364 }
1365 else if(type.isMatrix())
1366 {
1367 return registerSize(type, 0);
1368 }
1369
1370 UNREACHABLE();
1371 return 0;
1372 }
1373
1374 void OutputASM::argument(sw::Shader::SourceParameter &parameter, TIntermNode *argument, int index)
1375 {
1376 if(argument)
1377 {
1378 TIntermTyped *arg = argument->getAsTyped();
1379 const TType &type = arg->getType();
1380 const TTypeList *structure = type.getStruct();
1381 ASSERT(index < arg->totalRegisterCount());
1382
1383 int size = registerSize(type, index);
1384
1385 parameter.type = registerType(arg);
1386
1387 if(arg->getQualifier() == EvqConst)
1388 {
1389 int component = componentCount(type, index);
1390 ConstantUnion *constants = arg->getAsConstantUnion()->getUnionArrayPointer();
1391
1392 for(int i = 0; i < 4; i++)
1393 {
1394 if(size == 1) // Replicate
1395 {
1396 parameter.value[i] = constants[component + 0].getAsFloat();
1397 }
1398 else if(i < size)
1399 {
1400 parameter.value[i] = constants[component + i].getAsFloat();
1401 }
1402 else
1403 {
1404 parameter.value[i] = 0.0f;
1405 }
1406 }
1407 }
1408 else
1409 {
1410 parameter.index = registerIndex(arg) + index;
1411
1412 if(registerType(arg) == sw::Shader::PARAMETER_SAMPLER)
1413 {
1414 TIntermBinary *binary = argument->getAsBinaryNode();
1415
1416 if(binary)
1417 {
1418 TIntermTyped *left = binary->getLeft();
1419 TIntermTyped *right = binary->getRight();
1420
1421 if(binary->getOp() == EOpIndexDirect)
1422 {
1423 parameter.index += right->getAsConstantUnion()->getUnionArrayPointer()->getIConst();
1424 }
1425 else if(binary->getOp() == EOpIndexIndirect)
1426 {
1427 if(left->getArraySize() > 1)
1428 {
1429 parameter.rel.type = registerType(binary->getRight());
1430 parameter.rel.index = registerIndex(binary->getRight());
1431 parameter.rel.scale = 1;
1432 parameter.rel.deterministic = true;
1433 }
1434 }
1435 else UNREACHABLE();
1436 }
1437 }
1438 }
1439
1440 if(!IsSampler(arg->getBasicType()))
1441 {
Nicolas Capens059bece2014-05-06 16:44:23 -04001442 parameter.swizzle = readSwizzle(arg, size);
John Bauman66b8ab22014-05-06 15:57:45 -04001443 }
1444 }
1445 }
1446
1447 void OutputASM::copy(TIntermTyped *dst, TIntermNode *src, int offset)
1448 {
1449 for(int index = 0; index < dst->totalRegisterCount(); index++)
1450 {
1451 Instruction *mov = emit(sw::Shader::OPCODE_MOV, dst, src);
1452 mov->dst.index += index;
1453 mov->dst.mask = writeMask(dst, index);
1454 argument(mov->src[0], src, offset + index);
1455 }
1456 }
1457
1458 int swizzleElement(int swizzle, int index)
1459 {
1460 return (swizzle >> (index * 2)) & 0x03;
1461 }
1462
1463 int swizzleSwizzle(int leftSwizzle, int rightSwizzle)
1464 {
1465 return (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 0)) << 0) |
1466 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 1)) << 2) |
1467 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 2)) << 4) |
1468 (swizzleElement(leftSwizzle, swizzleElement(rightSwizzle, 3)) << 6);
1469 }
1470
1471 void OutputASM::assignLvalue(TIntermTyped *dst, TIntermNode *src)
1472 {
1473 TIntermBinary *binary = dst->getAsBinaryNode();
1474
1475 if(binary && binary->getOp() == EOpIndexIndirect && dst->isScalar())
1476 {
1477 Instruction *insert = new Instruction(sw::Shader::OPCODE_INSERT);
1478
1479 Temporary address(this);
1480 lvalue(insert->dst, address, dst);
1481
1482 insert->src[0].type = insert->dst.type;
1483 insert->src[0].index = insert->dst.index;
1484 insert->src[0].rel = insert->dst.rel;
1485 argument(insert->src[1], src);
1486 argument(insert->src[2], binary->getRight());
1487
1488 shader->append(insert);
1489 }
1490 else
1491 {
1492 for(int offset = 0; offset < dst->totalRegisterCount(); offset++)
1493 {
1494 Instruction *mov = new Instruction(sw::Shader::OPCODE_MOV);
1495
1496 Temporary address(this);
1497 int swizzle = lvalue(mov->dst, address, dst);
1498 mov->dst.index += offset;
1499
1500 if(offset > 0)
1501 {
1502 mov->dst.mask = writeMask(dst, offset);
1503 }
1504
1505 argument(mov->src[0], src, offset);
1506 mov->src[0].swizzle = swizzleSwizzle(mov->src[0].swizzle, swizzle);
1507
1508 shader->append(mov);
1509 }
1510 }
1511 }
1512
1513 int OutputASM::lvalue(sw::Shader::DestinationParameter &dst, Temporary &address, TIntermTyped *node)
1514 {
1515 TIntermTyped *result = node;
1516 TIntermBinary *binary = node->getAsBinaryNode();
1517 TIntermSymbol *symbol = node->getAsSymbolNode();
1518
1519 if(binary)
1520 {
1521 TIntermTyped *left = binary->getLeft();
1522 TIntermTyped *right = binary->getRight();
1523
1524 int leftSwizzle = lvalue(dst, address, left); // Resolve the l-value of the left side
1525
1526 switch(binary->getOp())
1527 {
1528 case EOpIndexDirect:
1529 {
1530 int rightIndex = right->getAsConstantUnion()->getUnionArrayPointer()->getIConst();
1531
1532 if(left->isRegister())
1533 {
1534 int leftMask = dst.mask;
1535
1536 dst.mask = 1;
1537 while((leftMask & dst.mask) == 0)
1538 {
1539 dst.mask = dst.mask << 1;
1540 }
1541
1542 int element = swizzleElement(leftSwizzle, rightIndex);
1543 dst.mask = 1 << element;
1544
1545 return element;
1546 }
1547 else if(left->isArray() || left->isMatrix())
1548 {
1549 dst.index += rightIndex * result->totalRegisterCount();
1550 return 0xE4;
1551 }
1552 else UNREACHABLE();
1553 }
1554 break;
1555 case EOpIndexIndirect:
1556 {
1557 if(left->isRegister())
1558 {
1559 // Requires INSERT instruction (handled by calling function)
1560 }
1561 else if(left->isArray() || left->isMatrix())
1562 {
1563 int scale = result->totalRegisterCount();
1564
1565 if(dst.rel.type == sw::Shader::PARAMETER_VOID) // Use the index register as the relative address directly
1566 {
1567 if(left->totalRegisterCount() > 1)
1568 {
1569 sw::Shader::SourceParameter relativeRegister;
1570 argument(relativeRegister, right);
1571
1572 dst.rel.index = relativeRegister.index;
1573 dst.rel.type = relativeRegister.type;
1574 dst.rel.scale = scale;
1575 dst.rel.deterministic = !(vertexShader && left->getQualifier() == EvqUniform);
1576 }
1577 }
1578 else if(dst.rel.index != registerIndex(&address)) // Move the previous index register to the address register
1579 {
1580 if(scale == 1)
1581 {
1582 Constant oldScale((int)dst.rel.scale);
1583 Instruction *mad = emit(sw::Shader::OPCODE_MAD, &address, &address, &oldScale, right);
1584 mad->src[0].index = dst.rel.index;
1585 mad->src[0].type = dst.rel.type;
1586 }
1587 else
1588 {
1589 Constant oldScale((int)dst.rel.scale);
1590 Instruction *mul = emit(sw::Shader::OPCODE_MUL, &address, &address, &oldScale);
1591 mul->src[0].index = dst.rel.index;
1592 mul->src[0].type = dst.rel.type;
1593
1594 Constant newScale(scale);
1595 emit(sw::Shader::OPCODE_MAD, &address, right, &newScale, &address);
1596 }
1597
1598 dst.rel.type = sw::Shader::PARAMETER_TEMP;
1599 dst.rel.index = registerIndex(&address);
1600 dst.rel.scale = 1;
1601 }
1602 else // Just add the new index to the address register
1603 {
1604 if(scale == 1)
1605 {
1606 emit(sw::Shader::OPCODE_ADD, &address, &address, right);
1607 }
1608 else
1609 {
1610 Constant newScale(scale);
1611 emit(sw::Shader::OPCODE_MAD, &address, right, &newScale, &address);
1612 }
1613 }
1614 }
1615 else UNREACHABLE();
1616 }
1617 break;
1618 case EOpIndexDirectStruct:
1619 {
1620 const TTypeList *structure = left->getType().getStruct();
1621 const TString &fieldName = right->getType().getFieldName();
1622
1623 int offset = 0;
1624 for(TTypeList::const_iterator field = structure->begin(); field != structure->end(); field++)
1625 {
1626 if(field->type->getFieldName() == fieldName)
1627 {
1628 dst.type = registerType(left);
1629 dst.index += offset;
1630 dst.mask = writeMask(right);
1631
1632 return 0xE4;
1633 }
1634
1635 offset += field->type->totalRegisterCount();
1636 }
1637 }
1638 break;
1639 case EOpVectorSwizzle:
1640 {
1641 ASSERT(left->isRegister());
1642
1643 int leftMask = dst.mask;
1644
1645 int swizzle = 0;
1646 int rightMask = 0;
1647
1648 TIntermSequence &sequence = right->getAsAggregate()->getSequence();
1649
1650 for(unsigned int i = 0; i < sequence.size(); i++)
1651 {
1652 int index = sequence[i]->getAsConstantUnion()->getUnionArrayPointer()->getIConst();
1653
1654 int element = swizzleElement(leftSwizzle, index);
1655 rightMask = rightMask | (1 << element);
1656 swizzle = swizzle | swizzleElement(leftSwizzle, i) << (element * 2);
1657 }
1658
1659 dst.mask = leftMask & rightMask;
1660
1661 return swizzle;
1662 }
1663 break;
1664 default:
1665 UNREACHABLE(); // Not an l-value operator
1666 break;
1667 }
1668 }
1669 else if(symbol)
1670 {
1671 dst.type = registerType(symbol);
1672 dst.index = registerIndex(symbol);
1673 dst.mask = writeMask(symbol);
1674 return 0xE4;
1675 }
1676
1677 return 0xE4;
1678 }
1679
1680 sw::Shader::ParameterType OutputASM::registerType(TIntermTyped *operand)
1681 {
1682 if(IsSampler(operand->getBasicType()) && (operand->getQualifier() == EvqUniform || operand->getQualifier() == EvqTemporary)) // Function parameters are temporaries
1683 {
1684 return sw::Shader::PARAMETER_SAMPLER;
1685 }
1686
1687 switch(operand->getQualifier())
1688 {
1689 case EvqTemporary: return sw::Shader::PARAMETER_TEMP;
1690 case EvqGlobal: return sw::Shader::PARAMETER_TEMP;
1691 case EvqConst: return sw::Shader::PARAMETER_FLOAT4LITERAL; // All converted to float
1692 case EvqAttribute: return sw::Shader::PARAMETER_INPUT;
1693 case EvqVaryingIn: return sw::Shader::PARAMETER_INPUT;
1694 case EvqVaryingOut: return sw::Shader::PARAMETER_OUTPUT;
1695 case EvqInvariantVaryingIn: return sw::Shader::PARAMETER_INPUT; // FIXME: Guarantee invariance at the backend
1696 case EvqInvariantVaryingOut: return sw::Shader::PARAMETER_OUTPUT; // FIXME: Guarantee invariance at the backend
1697 case EvqUniform: return sw::Shader::PARAMETER_CONST;
1698 case EvqIn: return sw::Shader::PARAMETER_TEMP;
1699 case EvqOut: return sw::Shader::PARAMETER_TEMP;
1700 case EvqInOut: return sw::Shader::PARAMETER_TEMP;
1701 case EvqConstReadOnly: return sw::Shader::PARAMETER_TEMP;
1702 case EvqPosition: return sw::Shader::PARAMETER_OUTPUT;
1703 case EvqPointSize: return sw::Shader::PARAMETER_OUTPUT;
1704 case EvqFragCoord: return sw::Shader::PARAMETER_MISCTYPE;
1705 case EvqFrontFacing: return sw::Shader::PARAMETER_MISCTYPE;
1706 case EvqPointCoord: return sw::Shader::PARAMETER_INPUT;
1707 case EvqFragColor: return sw::Shader::PARAMETER_COLOROUT;
1708 case EvqFragData: return sw::Shader::PARAMETER_COLOROUT;
1709 default: UNREACHABLE();
1710 }
1711
1712 return sw::Shader::PARAMETER_VOID;
1713 }
1714
1715 int OutputASM::registerIndex(TIntermTyped *operand)
1716 {
1717 if(registerType(operand) == sw::Shader::PARAMETER_SAMPLER)
1718 {
1719 return samplerRegister(operand);
1720 }
1721
1722 switch(operand->getQualifier())
1723 {
1724 case EvqTemporary: return temporaryRegister(operand);
1725 case EvqGlobal: return temporaryRegister(operand);
1726 case EvqConst: UNREACHABLE();
1727 case EvqAttribute: return attributeRegister(operand);
1728 case EvqVaryingIn: return varyingRegister(operand);
1729 case EvqVaryingOut: return varyingRegister(operand);
1730 case EvqInvariantVaryingIn: return varyingRegister(operand);
1731 case EvqInvariantVaryingOut: return varyingRegister(operand);
1732 case EvqUniform: return uniformRegister(operand);
1733 case EvqIn: return temporaryRegister(operand);
1734 case EvqOut: return temporaryRegister(operand);
1735 case EvqInOut: return temporaryRegister(operand);
1736 case EvqConstReadOnly: return temporaryRegister(operand);
1737 case EvqPosition: return varyingRegister(operand);
1738 case EvqPointSize: return varyingRegister(operand);
1739 case EvqFragCoord: pixelShader->vPosDeclared = true; return 0;
1740 case EvqFrontFacing: pixelShader->vFaceDeclared = true; return 1;
1741 case EvqPointCoord: return varyingRegister(operand);
1742 case EvqFragColor: return 0;
1743 case EvqFragData: return 0;
1744 default: UNREACHABLE();
1745 }
1746
1747 return 0;
1748 }
1749
1750 int OutputASM::writeMask(TIntermTyped *destination, int index)
1751 {
1752 if(destination->getQualifier() == EvqPointSize)
1753 {
1754 return 0x2; // Point size stored in the y component
1755 }
1756
1757 return 0xF >> (4 - registerSize(destination->getType(), index));
1758 }
1759
Nicolas Capens059bece2014-05-06 16:44:23 -04001760 int OutputASM::readSwizzle(TIntermTyped *argument, int size)
1761 {
1762 if(argument->getQualifier() == EvqPointSize)
1763 {
1764 return 0x55; // Point size stored in the y component
1765 }
1766
1767 static const unsigned char swizzleSize[5] = {0x00, 0x00, 0x54, 0xA4, 0xE4}; // (void), xxxx, xyyy, xyzz, xyzw
1768
1769 return swizzleSize[size];
1770 }
1771
John Bauman66b8ab22014-05-06 15:57:45 -04001772 // Conservatively checks whether an expression is fast to compute and has no side effects
1773 bool OutputASM::trivial(TIntermTyped *expression, int budget)
1774 {
1775 if(!expression->isRegister())
1776 {
1777 return false;
1778 }
1779
1780 return cost(expression, budget) >= 0;
1781 }
1782
1783 // Returns the remaining computing budget (if < 0 the expression is too expensive or has side effects)
1784 int OutputASM::cost(TIntermNode *expression, int budget)
1785 {
1786 if(budget < 0)
1787 {
1788 return budget;
1789 }
1790
1791 if(expression->getAsSymbolNode())
1792 {
1793 return budget;
1794 }
1795 else if(expression->getAsConstantUnion())
1796 {
1797 return budget;
1798 }
1799 else if(expression->getAsBinaryNode())
1800 {
1801 TIntermBinary *binary = expression->getAsBinaryNode();
1802
1803 switch(binary->getOp())
1804 {
1805 case EOpVectorSwizzle:
1806 case EOpIndexDirect:
1807 case EOpIndexDirectStruct:
1808 return cost(binary->getLeft(), budget - 0);
1809 case EOpAdd:
1810 case EOpSub:
1811 case EOpMul:
1812 return cost(binary->getLeft(), cost(binary->getRight(), budget - 1));
1813 default:
1814 return -1;
1815 }
1816 }
1817 else if(expression->getAsUnaryNode())
1818 {
1819 TIntermUnary *unary = expression->getAsUnaryNode();
1820
1821 switch(unary->getOp())
1822 {
1823 case EOpAbs:
1824 case EOpNegative:
1825 return cost(unary->getOperand(), budget - 1);
1826 default:
1827 return -1;
1828 }
1829 }
1830 else if(expression->getAsSelectionNode())
1831 {
1832 TIntermSelection *selection = expression->getAsSelectionNode();
1833
1834 if(selection->usesTernaryOperator())
1835 {
1836 TIntermTyped *condition = selection->getCondition();
1837 TIntermNode *trueBlock = selection->getTrueBlock();
1838 TIntermNode *falseBlock = selection->getFalseBlock();
1839 TIntermConstantUnion *constantCondition = condition->getAsConstantUnion();
1840
1841 if(constantCondition)
1842 {
1843 bool trueCondition = constantCondition->getUnionArrayPointer()->getBConst();
1844
1845 if(trueCondition)
1846 {
1847 return cost(trueBlock, budget - 0);
1848 }
1849 else
1850 {
1851 return cost(falseBlock, budget - 0);
1852 }
1853 }
1854 else
1855 {
1856 return cost(trueBlock, cost(falseBlock, budget - 2));
1857 }
1858 }
1859 }
1860
1861 return -1;
1862 }
1863
1864 const Function &OutputASM::findFunction(const TString &name)
1865 {
1866 for(unsigned int f = 0; f < functionArray.size(); f++)
1867 {
1868 if(functionArray[f].name == name)
1869 {
1870 return functionArray[f];
1871 }
1872 }
1873
1874 UNREACHABLE();
1875 return functionArray[0];
1876 }
1877
1878 int OutputASM::temporaryRegister(TIntermTyped *temporary)
1879 {
1880 return allocate(temporaries, temporary);
1881 }
1882
1883 int OutputASM::varyingRegister(TIntermTyped *varying)
1884 {
1885 int var = lookup(varyings, varying);
1886
1887 if(var == -1)
1888 {
1889 var = allocate(varyings, varying);
1890 int componentCount = varying->getNominalSize();
1891 int registerCount = varying->totalRegisterCount();
1892
1893 if(pixelShader && (var + registerCount) <= sw::PixelShader::MAX_INPUT_VARYINGS)
1894 {
1895 if(varying->getQualifier() == EvqPointCoord)
1896 {
1897 ASSERT(varying->isRegister());
1898 if(componentCount >= 1) pixelShader->semantic[var][0] = sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, var);
1899 if(componentCount >= 2) pixelShader->semantic[var][1] = sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, var);
1900 if(componentCount >= 3) pixelShader->semantic[var][2] = sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, var);
1901 if(componentCount >= 4) pixelShader->semantic[var][3] = sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, var);
1902 }
1903 else
1904 {
1905 for(int i = 0; i < varying->totalRegisterCount(); i++)
1906 {
1907 if(componentCount >= 1) pixelShader->semantic[var + i][0] = sw::Shader::Semantic(sw::Shader::USAGE_COLOR, var + i);
1908 if(componentCount >= 2) pixelShader->semantic[var + i][1] = sw::Shader::Semantic(sw::Shader::USAGE_COLOR, var + i);
1909 if(componentCount >= 3) pixelShader->semantic[var + i][2] = sw::Shader::Semantic(sw::Shader::USAGE_COLOR, var + i);
1910 if(componentCount >= 4) pixelShader->semantic[var + i][3] = sw::Shader::Semantic(sw::Shader::USAGE_COLOR, var + i);
1911 }
1912 }
1913 }
1914 else if(vertexShader && (var + registerCount) <= sw::VertexShader::MAX_OUTPUT_VARYINGS)
1915 {
1916 if(varying->getQualifier() == EvqPosition)
1917 {
1918 ASSERT(varying->isRegister());
1919 vertexShader->output[var][0] = sw::Shader::Semantic(sw::Shader::USAGE_POSITION, 0);
1920 vertexShader->output[var][1] = sw::Shader::Semantic(sw::Shader::USAGE_POSITION, 0);
1921 vertexShader->output[var][2] = sw::Shader::Semantic(sw::Shader::USAGE_POSITION, 0);
1922 vertexShader->output[var][3] = sw::Shader::Semantic(sw::Shader::USAGE_POSITION, 0);
1923 vertexShader->positionRegister = var;
1924 }
1925 else if(varying->getQualifier() == EvqPointSize)
1926 {
1927 ASSERT(varying->isRegister());
1928 vertexShader->output[var][0] = sw::Shader::Semantic(sw::Shader::USAGE_PSIZE, 0);
1929 vertexShader->output[var][1] = sw::Shader::Semantic(sw::Shader::USAGE_PSIZE, 0);
1930 vertexShader->output[var][2] = sw::Shader::Semantic(sw::Shader::USAGE_PSIZE, 0);
1931 vertexShader->output[var][3] = sw::Shader::Semantic(sw::Shader::USAGE_PSIZE, 0);
1932 vertexShader->pointSizeRegister = var;
1933 }
1934 else
1935 {
1936 // Semantic indexes for user varyings will be assigned during program link to match the pixel shader
1937 }
1938 }
1939 else UNREACHABLE();
1940
1941 declareVarying(varying, var);
1942 }
1943
1944 return var;
1945 }
1946
1947 void OutputASM::declareVarying(TIntermTyped *varying, int reg)
1948 {
1949 if(varying->getQualifier() != EvqPointCoord) // gl_PointCoord does not need linking
1950 {
1951 const TType &type = varying->getType();
1952 const char *name = varying->getAsSymbolNode()->getSymbol().c_str();
1953 gl::VaryingList &activeVaryings = shaderObject->varyings;
1954
1955 // Check if this varying has been declared before without having a register assigned
1956 for(gl::VaryingList::iterator v = activeVaryings.begin(); v != activeVaryings.end(); v++)
1957 {
1958 if(v->name == name)
1959 {
1960 if(reg >= 0)
1961 {
1962 ASSERT(v->reg < 0 || v->reg == reg);
1963 v->reg = reg;
1964 }
1965
1966 return;
1967 }
1968 }
1969
1970 activeVaryings.push_back(gl::Varying(glVariableType(type), name, varying->getArraySize(), reg, 0));
1971 }
1972 }
1973
1974 int OutputASM::uniformRegister(TIntermTyped *uniform)
1975 {
1976 const TType &type = uniform->getType();
1977 ASSERT(!IsSampler(type.getBasicType()));
1978 TIntermSymbol *symbol = uniform->getAsSymbolNode();
1979 ASSERT(symbol);
1980
1981 if(symbol)
1982 {
1983 int index = lookup(uniforms, uniform);
1984
1985 if(index == -1)
1986 {
1987 index = allocate(uniforms, uniform);
1988 const TString &name = symbol->getSymbol().c_str();
1989
1990 declareUniform(type, name, index);
1991 }
1992
1993 return index;
1994 }
1995
1996 return 0;
1997 }
1998
1999 int OutputASM::attributeRegister(TIntermTyped *attribute)
2000 {
2001 ASSERT(!attribute->isArray());
2002 ASSERT(attribute->getBasicType() == EbtFloat);
2003
2004 int index = lookup(attributes, attribute);
2005
2006 if(index == -1)
2007 {
2008 TIntermSymbol *symbol = attribute->getAsSymbolNode();
2009 ASSERT(symbol);
2010
2011 if(symbol)
2012 {
2013 index = allocate(attributes, attribute);
2014 const TType &type = attribute->getType();
2015 int registerCount = attribute->totalRegisterCount();
2016
2017 if(vertexShader && (index + registerCount) <= sw::VertexShader::MAX_INPUT_ATTRIBUTES)
2018 {
2019 for(int i = 0; i < registerCount; i++)
2020 {
2021 vertexShader->input[index + i] = sw::Shader::Semantic(sw::Shader::USAGE_TEXCOORD, index + i);
2022 }
2023 }
2024
2025 ActiveAttributes &activeAttributes = shaderObject->activeAttributes;
2026
2027 const char *name = symbol->getSymbol().c_str();
2028 activeAttributes.push_back(Attribute(glVariableType(type), name, 0, index));
2029 }
2030 }
2031
2032 return index;
2033 }
2034
2035 int OutputASM::samplerRegister(TIntermTyped *sampler)
2036 {
2037 const TType &type = sampler->getType();
2038 ASSERT(IsSampler(type.getBasicType()));
2039 TIntermSymbol *symbol = sampler->getAsSymbolNode();
2040 TIntermBinary *binary = sampler->getAsBinaryNode();
2041
2042 if(symbol)
2043 {
2044 int index = lookup(samplers, sampler);
2045
2046 if(index == -1)
2047 {
2048 index = allocate(samplers, sampler);
2049 ActiveUniforms &activeUniforms = shaderObject->activeUniforms;
2050 const char *name = symbol->getSymbol().c_str();
John Baumand4ae8632014-05-06 16:18:33 -04002051 activeUniforms.push_back(Uniform(glVariableType(type), glVariablePrecision(type), name, sampler->getArraySize(), index));
John Bauman66b8ab22014-05-06 15:57:45 -04002052
2053 for(int i = 0; i < sampler->totalRegisterCount(); i++)
2054 {
2055 shader->declareSampler(index + i);
2056 }
2057 }
2058
2059 return index;
2060 }
2061 else if(binary)
2062 {
2063 ASSERT(binary->getOp() == EOpIndexDirect || binary->getOp() == EOpIndexIndirect);
2064
2065 return samplerRegister(binary->getLeft()); // Index added later
2066 }
2067 else UNREACHABLE();
2068
2069 return 0;
2070 }
2071
2072 int OutputASM::lookup(VariableArray &list, TIntermTyped *variable)
2073 {
2074 for(unsigned int i = 0; i < list.size(); i++)
2075 {
2076 if(list[i] == variable)
2077 {
2078 return i; // Pointer match
2079 }
2080 }
2081
2082 TIntermSymbol *varSymbol = variable->getAsSymbolNode();
2083
2084 if(varSymbol)
2085 {
2086 for(unsigned int i = 0; i < list.size(); i++)
2087 {
2088 if(list[i])
2089 {
2090 TIntermSymbol *listSymbol = list[i]->getAsSymbolNode();
2091
2092 if(listSymbol)
2093 {
2094 if(listSymbol->getId() == varSymbol->getId())
2095 {
2096 ASSERT(listSymbol->getSymbol() == varSymbol->getSymbol());
2097 ASSERT(listSymbol->getType() == varSymbol->getType());
2098 ASSERT(listSymbol->getQualifier() == varSymbol->getQualifier());
2099
2100 return i;
2101 }
2102 }
2103 }
2104 }
2105 }
2106
2107 return -1;
2108 }
2109
2110 int OutputASM::allocate(VariableArray &list, TIntermTyped *variable)
2111 {
2112 int index = lookup(list, variable);
2113
2114 if(index == -1)
2115 {
2116 unsigned int registerCount = variable->totalRegisterCount();
2117
2118 for(unsigned int i = 0; i < list.size(); i++)
2119 {
2120 if(list[i] == 0)
2121 {
2122 unsigned int j = 1;
2123 for( ; j < registerCount && (i + j) < list.size(); j++)
2124 {
2125 if(list[i + j] != 0)
2126 {
2127 break;
2128 }
2129 }
2130
2131 if(j == registerCount) // Found free slots
2132 {
2133 for(unsigned int j = 0; j < registerCount; j++)
2134 {
2135 list[i + j] = variable;
2136 }
2137
2138 return i;
2139 }
2140 }
2141 }
2142
2143 index = list.size();
2144
2145 for(unsigned int i = 0; i < registerCount; i++)
2146 {
2147 list.push_back(variable);
2148 }
2149 }
2150
2151 return index;
2152 }
2153
2154 void OutputASM::free(VariableArray &list, TIntermTyped *variable)
2155 {
2156 int index = lookup(list, variable);
2157
2158 if(index >= 0)
2159 {
2160 list[index] = 0;
2161 }
2162 }
2163
2164 void OutputASM::declareUniform(const TType &type, const TString &name, int index)
2165 {
2166 const TTypeList *structure = type.getStruct();
2167 ActiveUniforms &activeUniforms = shaderObject->activeUniforms;
2168
2169 if(!structure)
2170 {
John Baumand4ae8632014-05-06 16:18:33 -04002171 activeUniforms.push_back(Uniform(glVariableType(type), glVariablePrecision(type), name.c_str(), type.getArraySize(), index));
John Bauman66b8ab22014-05-06 15:57:45 -04002172 }
2173 else
2174 {
2175 if(type.isArray())
2176 {
2177 int elementIndex = index;
2178
2179 for(int i = 0; i < type.getArraySize(); i++)
2180 {
2181 for(size_t j = 0; j < structure->size(); j++)
2182 {
2183 const TType &fieldType = *(*structure)[j].type;
2184 const TString &fieldName = fieldType.getFieldName();
2185
2186 const TString uniformName = name + "[" + str(i) + "]." + fieldName;
2187 declareUniform(fieldType, uniformName, elementIndex);
2188 elementIndex += fieldType.totalRegisterCount();
2189 }
2190 }
2191 }
2192 else
2193 {
2194 int fieldIndex = index;
2195
2196 for(size_t i = 0; i < structure->size(); i++)
2197 {
2198 const TType &fieldType = *(*structure)[i].type;
2199 const TString &fieldName = fieldType.getFieldName();
2200
2201 const TString uniformName = name + "." + fieldName;
2202 declareUniform(fieldType, uniformName, fieldIndex);
2203 fieldIndex += fieldType.totalRegisterCount();
2204 }
2205 }
2206 }
2207 }
2208
2209 GLenum OutputASM::glVariableType(const TType &type)
2210 {
2211 if(type.getBasicType() == EbtFloat)
2212 {
2213 if(type.isScalar())
2214 {
2215 return GL_FLOAT;
2216 }
2217 else if(type.isVector())
2218 {
2219 switch(type.getNominalSize())
2220 {
2221 case 2: return GL_FLOAT_VEC2;
2222 case 3: return GL_FLOAT_VEC3;
2223 case 4: return GL_FLOAT_VEC4;
2224 default: UNREACHABLE();
2225 }
2226 }
2227 else if(type.isMatrix())
2228 {
2229 switch(type.getNominalSize())
2230 {
2231 case 2: return GL_FLOAT_MAT2;
2232 case 3: return GL_FLOAT_MAT3;
2233 case 4: return GL_FLOAT_MAT4;
2234 default: UNREACHABLE();
2235 }
2236 }
2237 else UNREACHABLE();
2238 }
2239 else if(type.getBasicType() == EbtInt)
2240 {
2241 if(type.isScalar())
2242 {
2243 return GL_INT;
2244 }
2245 else if(type.isVector())
2246 {
2247 switch(type.getNominalSize())
2248 {
2249 case 2: return GL_INT_VEC2;
2250 case 3: return GL_INT_VEC3;
2251 case 4: return GL_INT_VEC4;
2252 default: UNREACHABLE();
2253 }
2254 }
2255 else UNREACHABLE();
2256 }
2257 else if(type.getBasicType() == EbtBool)
2258 {
2259 if(type.isScalar())
2260 {
2261 return GL_BOOL;
2262 }
2263 else if(type.isVector())
2264 {
2265 switch(type.getNominalSize())
2266 {
2267 case 2: return GL_BOOL_VEC2;
2268 case 3: return GL_BOOL_VEC3;
2269 case 4: return GL_BOOL_VEC4;
2270 default: UNREACHABLE();
2271 }
2272 }
2273 else UNREACHABLE();
2274 }
2275 else if(type.getBasicType() == EbtSampler2D)
2276 {
2277 return GL_SAMPLER_2D;
2278 }
2279 else if(type.getBasicType() == EbtSamplerCube)
2280 {
2281 return GL_SAMPLER_CUBE;
2282 }
2283 else UNREACHABLE();
2284
2285 return GL_NONE;
2286 }
2287
John Baumand4ae8632014-05-06 16:18:33 -04002288 GLenum OutputASM::glVariablePrecision(const TType &type)
2289 {
2290 if(type.getBasicType() == EbtFloat)
2291 {
2292 switch(type.getPrecision())
2293 {
2294 case EbpHigh: return GL_HIGH_FLOAT;
2295 case EbpMedium: return GL_MEDIUM_FLOAT;
2296 case EbpLow: return GL_LOW_FLOAT;
2297 case EbpUndefined:
2298 // Should be defined as the default precision by the parser
2299 default: UNREACHABLE();
2300 }
2301 }
2302 else if (type.getBasicType() == EbtInt)
2303 {
2304 switch (type.getPrecision())
2305 {
2306 case EbpHigh: return GL_HIGH_INT;
2307 case EbpMedium: return GL_MEDIUM_INT;
2308 case EbpLow: return GL_LOW_INT;
2309 case EbpUndefined:
2310 // Should be defined as the default precision by the parser
2311 default: UNREACHABLE();
2312 }
2313 }
2314
2315 // Other types (boolean, sampler) don't have a precision
2316 return GL_NONE;
2317 }
2318
John Bauman66b8ab22014-05-06 15:57:45 -04002319 int OutputASM::dim(TIntermNode *v)
2320 {
2321 TIntermTyped *vector = v->getAsTyped();
2322 ASSERT(vector && vector->isRegister());
2323 return vector->getNominalSize();
2324 }
2325
2326 int OutputASM::dim2(TIntermNode *m)
2327 {
2328 TIntermTyped *matrix = m->getAsTyped();
2329 ASSERT(matrix && matrix->isMatrix() && !matrix->isArray());
2330 return matrix->getNominalSize();
2331 }
John Baumand4ae8632014-05-06 16:18:33 -04002332
2333 // Returns ~0 if no loop count could be determined
2334 unsigned int OutputASM::loopCount(TIntermLoop *node)
2335 {
2336 // Parse loops of the form:
2337 // for(int index = initial; index [comparator] limit; index += increment)
2338 TIntermSymbol *index = 0;
2339 TOperator comparator = EOpNull;
2340 int initial = 0;
2341 int limit = 0;
2342 int increment = 0;
2343
2344 // Parse index name and intial value
2345 if(node->getInit())
2346 {
2347 TIntermAggregate *init = node->getInit()->getAsAggregate();
2348
2349 if(init)
2350 {
2351 TIntermSequence &sequence = init->getSequence();
2352 TIntermTyped *variable = sequence[0]->getAsTyped();
2353
2354 if(variable && variable->getQualifier() == EvqTemporary)
2355 {
2356 TIntermBinary *assign = variable->getAsBinaryNode();
2357
2358 if(assign->getOp() == EOpInitialize)
2359 {
2360 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2361 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2362
2363 if(symbol && constant)
2364 {
2365 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
2366 {
2367 index = symbol;
2368 initial = constant->getUnionArrayPointer()[0].getIConst();
2369 }
2370 }
2371 }
2372 }
2373 }
2374 }
2375
2376 // Parse comparator and limit value
2377 if(index && node->getCondition())
2378 {
2379 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
2380
2381 if(test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2382 {
2383 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2384
2385 if(constant)
2386 {
2387 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
2388 {
2389 comparator = test->getOp();
2390 limit = constant->getUnionArrayPointer()[0].getIConst();
2391 }
2392 }
2393 }
2394 }
2395
2396 // Parse increment
2397 if(index && comparator != EOpNull && node->getExpression())
2398 {
2399 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2400 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
2401
2402 if(binaryTerminal)
2403 {
2404 TOperator op = binaryTerminal->getOp();
2405 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2406
2407 if(constant)
2408 {
2409 if(constant->getBasicType() == EbtInt && constant->getNominalSize() == 1)
2410 {
2411 int value = constant->getUnionArrayPointer()[0].getIConst();
2412
2413 switch(op)
2414 {
2415 case EOpAddAssign: increment = value; break;
2416 case EOpSubAssign: increment = -value; break;
2417 default: UNIMPLEMENTED();
2418 }
2419 }
2420 }
2421 }
2422 else if(unaryTerminal)
2423 {
2424 TOperator op = unaryTerminal->getOp();
2425
2426 switch(op)
2427 {
2428 case EOpPostIncrement: increment = 1; break;
2429 case EOpPostDecrement: increment = -1; break;
2430 case EOpPreIncrement: increment = 1; break;
2431 case EOpPreDecrement: increment = -1; break;
2432 default: UNIMPLEMENTED();
2433 }
2434 }
2435 }
2436
2437 if(index && comparator != EOpNull && increment != 0)
2438 {
2439 if(comparator == EOpLessThanEqual)
2440 {
2441 comparator = EOpLessThan;
2442 limit += 1;
2443 }
2444
2445 if(comparator == EOpLessThan)
2446 {
2447 int iterations = (limit - initial) / increment;
2448
2449 if(iterations <= 0)
2450 {
2451 iterations = 0;
2452 }
2453
2454 return iterations;
2455 }
2456 else UNIMPLEMENTED(); // Falls through
2457 }
2458
2459 return ~0;
2460 }
2461
2462 bool DetectLoopDiscontinuity::traverse(TIntermNode *node)
2463 {
2464 loopDepth = 0;
2465 loopDiscontinuity = false;
2466
2467 node->traverse(this);
2468
2469 return loopDiscontinuity;
2470 }
2471
2472 bool DetectLoopDiscontinuity::visitLoop(Visit visit, TIntermLoop *loop)
2473 {
2474 if(visit == PreVisit)
2475 {
2476 loopDepth++;
2477 }
2478 else if(visit == PostVisit)
2479 {
2480 loopDepth++;
2481 }
2482
2483 return true;
2484 }
2485
2486 bool DetectLoopDiscontinuity::visitBranch(Visit visit, TIntermBranch *node)
2487 {
2488 if(loopDiscontinuity)
2489 {
2490 return false;
2491 }
2492
2493 if(!loopDepth)
2494 {
2495 return true;
2496 }
2497
2498 switch(node->getFlowOp())
2499 {
2500 case EOpKill:
2501 break;
2502 case EOpBreak:
2503 case EOpContinue:
2504 case EOpReturn:
2505 loopDiscontinuity = true;
2506 break;
2507 default: UNREACHABLE();
2508 }
2509
2510 return !loopDiscontinuity;
2511 }
2512
2513 bool DetectLoopDiscontinuity::visitAggregate(Visit visit, TIntermAggregate *node)
2514 {
2515 return !loopDiscontinuity;
2516 }
John Bauman66b8ab22014-05-06 15:57:45 -04002517}