forked from google/swiftshader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOutputASM.cpp
3998 lines (3536 loc) · 113 KB
/
OutputASM.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "OutputASM.h"
#include "Common/Math.hpp"
#include "common/debug.h"
#include "InfoSink.h"
#include "libGLESv2/Shader.h"
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <GLES3/gl3.h>
#include <GL/glcorearb.h>
#include <GL/glext.h>
#include <stdlib.h>
namespace
{
GLenum glVariableType(const TType &type)
{
switch(type.getBasicType())
{
case EbtFloat:
if(type.isScalar())
{
return GL_FLOAT;
}
else if(type.isVector())
{
switch(type.getNominalSize())
{
case 2: return GL_FLOAT_VEC2;
case 3: return GL_FLOAT_VEC3;
case 4: return GL_FLOAT_VEC4;
default: UNREACHABLE(type.getNominalSize());
}
}
else if(type.isMatrix())
{
switch(type.getNominalSize())
{
case 2:
switch(type.getSecondarySize())
{
case 2: return GL_FLOAT_MAT2;
case 3: return GL_FLOAT_MAT2x3;
case 4: return GL_FLOAT_MAT2x4;
default: UNREACHABLE(type.getSecondarySize());
}
case 3:
switch(type.getSecondarySize())
{
case 2: return GL_FLOAT_MAT3x2;
case 3: return GL_FLOAT_MAT3;
case 4: return GL_FLOAT_MAT3x4;
default: UNREACHABLE(type.getSecondarySize());
}
case 4:
switch(type.getSecondarySize())
{
case 2: return GL_FLOAT_MAT4x2;
case 3: return GL_FLOAT_MAT4x3;
case 4: return GL_FLOAT_MAT4;
default: UNREACHABLE(type.getSecondarySize());
}
default: UNREACHABLE(type.getNominalSize());
}
}
else UNREACHABLE(0);
break;
case EbtInt:
if(type.isScalar())
{
return GL_INT;
}
else if(type.isVector())
{
switch(type.getNominalSize())
{
case 2: return GL_INT_VEC2;
case 3: return GL_INT_VEC3;
case 4: return GL_INT_VEC4;
default: UNREACHABLE(type.getNominalSize());
}
}
else UNREACHABLE(0);
break;
case EbtUInt:
if(type.isScalar())
{
return GL_UNSIGNED_INT;
}
else if(type.isVector())
{
switch(type.getNominalSize())
{
case 2: return GL_UNSIGNED_INT_VEC2;
case 3: return GL_UNSIGNED_INT_VEC3;
case 4: return GL_UNSIGNED_INT_VEC4;
default: UNREACHABLE(type.getNominalSize());
}
}
else UNREACHABLE(0);
break;
case EbtBool:
if(type.isScalar())
{
return GL_BOOL;
}
else if(type.isVector())
{
switch(type.getNominalSize())
{
case 2: return GL_BOOL_VEC2;
case 3: return GL_BOOL_VEC3;
case 4: return GL_BOOL_VEC4;
default: UNREACHABLE(type.getNominalSize());
}
}
else UNREACHABLE(0);
break;
case EbtSampler2D:
return GL_SAMPLER_2D;
case EbtISampler2D:
return GL_INT_SAMPLER_2D;
case EbtUSampler2D:
return GL_UNSIGNED_INT_SAMPLER_2D;
case EbtSamplerCube:
return GL_SAMPLER_CUBE;
case EbtSampler2DRect:
return GL_SAMPLER_2D_RECT_ARB;
case EbtISamplerCube:
return GL_INT_SAMPLER_CUBE;
case EbtUSamplerCube:
return GL_UNSIGNED_INT_SAMPLER_CUBE;
case EbtSamplerExternalOES:
return GL_SAMPLER_EXTERNAL_OES;
case EbtSampler3D:
return GL_SAMPLER_3D_OES;
case EbtISampler3D:
return GL_INT_SAMPLER_3D;
case EbtUSampler3D:
return GL_UNSIGNED_INT_SAMPLER_3D;
case EbtSampler2DArray:
return GL_SAMPLER_2D_ARRAY;
case EbtISampler2DArray:
return GL_INT_SAMPLER_2D_ARRAY;
case EbtUSampler2DArray:
return GL_UNSIGNED_INT_SAMPLER_2D_ARRAY;
case EbtSampler2DShadow:
return GL_SAMPLER_2D_SHADOW;
case EbtSamplerCubeShadow:
return GL_SAMPLER_CUBE_SHADOW;
case EbtSampler2DArrayShadow:
return GL_SAMPLER_2D_ARRAY_SHADOW;
default:
UNREACHABLE(type.getBasicType());
break;
}
return GL_NONE;
}
GLenum glVariablePrecision(const TType &type)
{
if(type.getBasicType() == EbtFloat)
{
switch(type.getPrecision())
{
case EbpHigh: return GL_HIGH_FLOAT;
case EbpMedium: return GL_MEDIUM_FLOAT;
case EbpLow: return GL_LOW_FLOAT;
case EbpUndefined:
// Should be defined as the default precision by the parser
default: UNREACHABLE(type.getPrecision());
}
}
else if(type.getBasicType() == EbtInt)
{
switch(type.getPrecision())
{
case EbpHigh: return GL_HIGH_INT;
case EbpMedium: return GL_MEDIUM_INT;
case EbpLow: return GL_LOW_INT;
case EbpUndefined:
// Should be defined as the default precision by the parser
default: UNREACHABLE(type.getPrecision());
}
}
// Other types (boolean, sampler) don't have a precision
return GL_NONE;
}
}
namespace glsl
{
// Integer to TString conversion
TString str(int i)
{
char buffer[20];
sprintf(buffer, "%d", i);
return buffer;
}
class Temporary : public TIntermSymbol
{
public:
Temporary(OutputASM *assembler) : TIntermSymbol(TSymbolTableLevel::nextUniqueId(), "tmp", TType(EbtFloat, EbpHigh, EvqTemporary, 4, 1, false)), assembler(assembler)
{
}
~Temporary()
{
assembler->freeTemporary(this);
}
private:
OutputASM *const assembler;
};
class Constant : public TIntermConstantUnion
{
public:
Constant(float x, float y, float z, float w) : TIntermConstantUnion(constants, TType(EbtFloat, EbpHigh, EvqConstExpr, 4, 1, false))
{
constants[0].setFConst(x);
constants[1].setFConst(y);
constants[2].setFConst(z);
constants[3].setFConst(w);
}
Constant(bool b) : TIntermConstantUnion(constants, TType(EbtBool, EbpHigh, EvqConstExpr, 1, 1, false))
{
constants[0].setBConst(b);
}
Constant(int i) : TIntermConstantUnion(constants, TType(EbtInt, EbpHigh, EvqConstExpr, 1, 1, false))
{
constants[0].setIConst(i);
}
~Constant()
{
}
private:
ConstantUnion constants[4];
};
ShaderVariable::ShaderVariable(const TType& type, const std::string& name, int registerIndex) :
type(type.isStruct() ? GL_NONE : glVariableType(type)), precision(glVariablePrecision(type)),
name(name), arraySize(type.getArraySize()), registerIndex(registerIndex)
{
if(type.isStruct())
{
for(const auto& field : type.getStruct()->fields())
{
fields.push_back(ShaderVariable(*(field->type()), field->name().c_str(), -1));
}
}
}
Uniform::Uniform(const TType& type, const std::string &name, int registerIndex, int blockId, const BlockMemberInfo& blockMemberInfo) :
ShaderVariable(type, name, registerIndex), blockId(blockId), blockInfo(blockMemberInfo)
{
}
UniformBlock::UniformBlock(const std::string& name, unsigned int dataSize, unsigned int arraySize,
TLayoutBlockStorage layout, bool isRowMajorLayout, int registerIndex, int blockId) :
name(name), dataSize(dataSize), arraySize(arraySize), layout(layout),
isRowMajorLayout(isRowMajorLayout), registerIndex(registerIndex), blockId(blockId)
{
}
BlockLayoutEncoder::BlockLayoutEncoder()
: mCurrentOffset(0)
{
}
BlockMemberInfo BlockLayoutEncoder::encodeType(const TType &type)
{
int arrayStride;
int matrixStride;
bool isRowMajor = type.getLayoutQualifier().matrixPacking == EmpRowMajor;
getBlockLayoutInfo(type, type.getArraySize(), isRowMajor, &arrayStride, &matrixStride);
const BlockMemberInfo memberInfo(static_cast<int>(mCurrentOffset * BytesPerComponent),
static_cast<int>(arrayStride * BytesPerComponent),
static_cast<int>(matrixStride * BytesPerComponent),
(matrixStride > 0) && isRowMajor);
advanceOffset(type, type.getArraySize(), isRowMajor, arrayStride, matrixStride);
return memberInfo;
}
// static
size_t BlockLayoutEncoder::getBlockRegister(const BlockMemberInfo &info)
{
return (info.offset / BytesPerComponent) / ComponentsPerRegister;
}
// static
size_t BlockLayoutEncoder::getBlockRegisterElement(const BlockMemberInfo &info)
{
return (info.offset / BytesPerComponent) % ComponentsPerRegister;
}
void BlockLayoutEncoder::nextRegister()
{
mCurrentOffset = sw::align(mCurrentOffset, ComponentsPerRegister);
}
Std140BlockEncoder::Std140BlockEncoder() : BlockLayoutEncoder()
{
}
void Std140BlockEncoder::enterAggregateType()
{
nextRegister();
}
void Std140BlockEncoder::exitAggregateType()
{
nextRegister();
}
void Std140BlockEncoder::getBlockLayoutInfo(const TType &type, unsigned int arraySize, bool isRowMajorMatrix, int *arrayStrideOut, int *matrixStrideOut)
{
size_t baseAlignment = 0;
int matrixStride = 0;
int arrayStride = 0;
if(type.isMatrix())
{
baseAlignment = ComponentsPerRegister;
matrixStride = ComponentsPerRegister;
if(arraySize > 0)
{
const int numRegisters = isRowMajorMatrix ? type.getSecondarySize() : type.getNominalSize();
arrayStride = ComponentsPerRegister * numRegisters;
}
}
else if(arraySize > 0)
{
baseAlignment = ComponentsPerRegister;
arrayStride = ComponentsPerRegister;
}
else
{
const size_t numComponents = type.getElementSize();
baseAlignment = (numComponents == 3 ? 4u : numComponents);
}
mCurrentOffset = sw::align(mCurrentOffset, baseAlignment);
*matrixStrideOut = matrixStride;
*arrayStrideOut = arrayStride;
}
void Std140BlockEncoder::advanceOffset(const TType &type, unsigned int arraySize, bool isRowMajorMatrix, int arrayStride, int matrixStride)
{
if(arraySize > 0)
{
mCurrentOffset += arrayStride * arraySize;
}
else if(type.isMatrix())
{
ASSERT(matrixStride == ComponentsPerRegister);
const int numRegisters = isRowMajorMatrix ? type.getSecondarySize() : type.getNominalSize();
mCurrentOffset += ComponentsPerRegister * numRegisters;
}
else
{
mCurrentOffset += type.getElementSize();
}
}
Attribute::Attribute()
{
type = GL_NONE;
arraySize = 0;
registerIndex = 0;
}
Attribute::Attribute(GLenum type, const std::string &name, int arraySize, int layoutLocation, int registerIndex)
{
this->type = type;
this->name = name;
this->arraySize = arraySize;
this->layoutLocation = layoutLocation;
this->registerIndex = registerIndex;
}
sw::PixelShader *Shader::getPixelShader() const
{
return nullptr;
}
sw::VertexShader *Shader::getVertexShader() const
{
return nullptr;
}
OutputASM::TextureFunction::TextureFunction(const TString& nodeName) : method(IMPLICIT), proj(false), offset(false)
{
TString name = TFunction::unmangleName(nodeName);
if(name == "texture2D" || name == "textureCube" || name == "texture" || name == "texture3D" || name == "texture2DRect")
{
method = IMPLICIT;
}
else if(name == "texture2DProj" || name == "textureProj" || name == "texture2DRectProj")
{
method = IMPLICIT;
proj = true;
}
else if(name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod")
{
method = LOD;
}
else if(name == "texture2DProjLod" || name == "textureProjLod")
{
method = LOD;
proj = true;
}
else if(name == "textureSize")
{
method = SIZE;
}
else if(name == "textureOffset")
{
method = IMPLICIT;
offset = true;
}
else if(name == "textureProjOffset")
{
method = IMPLICIT;
offset = true;
proj = true;
}
else if(name == "textureLodOffset")
{
method = LOD;
offset = true;
}
else if(name == "textureProjLodOffset")
{
method = LOD;
proj = true;
offset = true;
}
else if(name == "texelFetch")
{
method = FETCH;
}
else if(name == "texelFetchOffset")
{
method = FETCH;
offset = true;
}
else if(name == "textureGrad")
{
method = GRAD;
}
else if(name == "textureGradOffset")
{
method = GRAD;
offset = true;
}
else if(name == "textureProjGrad")
{
method = GRAD;
proj = true;
}
else if(name == "textureProjGradOffset")
{
method = GRAD;
proj = true;
offset = true;
}
else UNREACHABLE(0);
}
OutputASM::OutputASM(TParseContext &context, Shader *shaderObject) : TIntermTraverser(true, true, true), shaderObject(shaderObject), mContext(context)
{
shader = nullptr;
pixelShader = nullptr;
vertexShader = nullptr;
if(shaderObject)
{
shader = shaderObject->getShader();
pixelShader = shaderObject->getPixelShader();
vertexShader = shaderObject->getVertexShader();
}
functionArray.push_back(Function(0, "main(", nullptr, nullptr));
currentFunction = 0;
outputQualifier = EvqOutput; // Initialize outputQualifier to any value other than EvqFragColor or EvqFragData
}
OutputASM::~OutputASM()
{
}
void OutputASM::output()
{
if(shader)
{
emitShader(GLOBAL);
if(functionArray.size() > 1) // Only call main() when there are other functions
{
Instruction *callMain = emit(sw::Shader::OPCODE_CALL);
callMain->dst.type = sw::Shader::PARAMETER_LABEL;
callMain->dst.index = 0; // main()
emit(sw::Shader::OPCODE_RET);
}
emitShader(FUNCTION);
}
}
void OutputASM::emitShader(Scope scope)
{
emitScope = scope;
currentScope = GLOBAL;
mContext.getTreeRoot()->traverse(this);
}
void OutputASM::freeTemporary(Temporary *temporary)
{
free(temporaries, temporary);
}
sw::Shader::Opcode OutputASM::getOpcode(sw::Shader::Opcode op, TIntermTyped *in) const
{
TBasicType baseType = in->getType().getBasicType();
switch(op)
{
case sw::Shader::OPCODE_NEG:
switch(baseType)
{
case EbtInt:
case EbtUInt:
return sw::Shader::OPCODE_INEG;
case EbtFloat:
default:
return op;
}
case sw::Shader::OPCODE_ABS:
switch(baseType)
{
case EbtInt:
return sw::Shader::OPCODE_IABS;
case EbtFloat:
default:
return op;
}
case sw::Shader::OPCODE_SGN:
switch(baseType)
{
case EbtInt:
return sw::Shader::OPCODE_ISGN;
case EbtFloat:
default:
return op;
}
case sw::Shader::OPCODE_ADD:
switch(baseType)
{
case EbtInt:
case EbtUInt:
return sw::Shader::OPCODE_IADD;
case EbtFloat:
default:
return op;
}
case sw::Shader::OPCODE_SUB:
switch(baseType)
{
case EbtInt:
case EbtUInt:
return sw::Shader::OPCODE_ISUB;
case EbtFloat:
default:
return op;
}
case sw::Shader::OPCODE_MUL:
switch(baseType)
{
case EbtInt:
case EbtUInt:
return sw::Shader::OPCODE_IMUL;
case EbtFloat:
default:
return op;
}
case sw::Shader::OPCODE_DIV:
switch(baseType)
{
case EbtInt:
return sw::Shader::OPCODE_IDIV;
case EbtUInt:
return sw::Shader::OPCODE_UDIV;
case EbtFloat:
default:
return op;
}
case sw::Shader::OPCODE_IMOD:
return baseType == EbtUInt ? sw::Shader::OPCODE_UMOD : op;
case sw::Shader::OPCODE_ISHR:
return baseType == EbtUInt ? sw::Shader::OPCODE_USHR : op;
case sw::Shader::OPCODE_MIN:
switch(baseType)
{
case EbtInt:
return sw::Shader::OPCODE_IMIN;
case EbtUInt:
return sw::Shader::OPCODE_UMIN;
case EbtFloat:
default:
return op;
}
case sw::Shader::OPCODE_MAX:
switch(baseType)
{
case EbtInt:
return sw::Shader::OPCODE_IMAX;
case EbtUInt:
return sw::Shader::OPCODE_UMAX;
case EbtFloat:
default:
return op;
}
default:
return op;
}
}
void OutputASM::visitSymbol(TIntermSymbol *symbol)
{
// The type of vertex outputs and fragment inputs with the same name must match (validated at link time),
// so declare them but don't assign a register index yet (one will be assigned when referenced in reachable code).
switch(symbol->getQualifier())
{
case EvqVaryingIn:
case EvqVaryingOut:
case EvqInvariantVaryingIn:
case EvqInvariantVaryingOut:
case EvqVertexOut:
case EvqFragmentIn:
if(symbol->getBasicType() != EbtInvariant) // Typeless declarations are not new varyings
{
declareVarying(symbol, -1);
}
break;
case EvqFragmentOut:
declareFragmentOutput(symbol);
break;
default:
break;
}
TInterfaceBlock* block = symbol->getType().getInterfaceBlock();
// OpenGL ES 3.0.4 spec, section 2.12.6 Uniform Variables:
// "All members of a named uniform block declared with a shared or std140 layout qualifier
// are considered active, even if they are not referenced in any shader in the program.
// The uniform block itself is also considered active, even if no member of the block is referenced."
if(block && ((block->blockStorage() == EbsShared) || (block->blockStorage() == EbsStd140)))
{
uniformRegister(symbol);
}
}
bool OutputASM::visitBinary(Visit visit, TIntermBinary *node)
{
if(currentScope != emitScope)
{
return false;
}
TIntermTyped *result = node;
TIntermTyped *left = node->getLeft();
TIntermTyped *right = node->getRight();
const TType &leftType = left->getType();
const TType &rightType = right->getType();
if(isSamplerRegister(result))
{
return false; // Don't traverse, the register index is determined statically
}
switch(node->getOp())
{
case EOpAssign:
assert(visit == PreVisit);
right->traverse(this);
assignLvalue(left, right);
copy(result, right);
return false;
case EOpInitialize:
assert(visit == PreVisit);
// Constant arrays go into the constant register file.
if(leftType.getQualifier() == EvqConstExpr && leftType.isArray() && leftType.getArraySize() > 1)
{
for(int i = 0; i < left->totalRegisterCount(); i++)
{
emit(sw::Shader::OPCODE_DEF, left, i, right, i);
}
}
else
{
right->traverse(this);
copy(left, right);
}
return false;
case EOpMatrixTimesScalarAssign:
assert(visit == PreVisit);
right->traverse(this);
for(int i = 0; i < leftType.getNominalSize(); i++)
{
emit(sw::Shader::OPCODE_MUL, result, i, left, i, right);
}
assignLvalue(left, result);
return false;
case EOpVectorTimesMatrixAssign:
assert(visit == PreVisit);
{
// The left operand may contain a swizzle serving double-duty as
// swizzle and writemask, so it's important that we traverse it
// first. Otherwise we may end up never setting up our left
// operand correctly.
left->traverse(this);
right->traverse(this);
int size = leftType.getNominalSize();
for(int i = 0; i < size; i++)
{
Instruction *dot = emit(sw::Shader::OPCODE_DP(size), result, 0, left, 0, right, i);
dot->dst.mask = 1 << i;
}
assignLvalue(left, result);
}
return false;
case EOpMatrixTimesMatrixAssign:
assert(visit == PreVisit);
{
right->traverse(this);
int dim = leftType.getNominalSize();
for(int i = 0; i < dim; i++)
{
Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
mul->src[1].swizzle = 0x00;
for(int j = 1; j < dim; j++)
{
Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
mad->src[1].swizzle = j * 0x55;
}
}
assignLvalue(left, result);
}
return false;
case EOpIndexDirect:
case EOpIndexIndirect:
case EOpIndexDirectStruct:
case EOpIndexDirectInterfaceBlock:
assert(visit == PreVisit);
evaluateRvalue(node);
return false;
case EOpVectorSwizzle:
if(visit == PostVisit)
{
int swizzle = 0;
TIntermAggregate *components = right->getAsAggregate();
if(components)
{
TIntermSequence &sequence = components->getSequence();
int component = 0;
for(TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++)
{
TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
if(element)
{
int i = element->getUnionArrayPointer()[0].getIConst();
swizzle |= i << (component * 2);
component++;
}
else UNREACHABLE(0);
}
}
else UNREACHABLE(0);
Instruction *mov = emit(sw::Shader::OPCODE_MOV, result, left);
mov->src[0].swizzle = swizzle;
}
break;
case EOpAddAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, left, right); break;
case EOpAdd: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ADD, result), result, left, right); break;
case EOpSubAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, left, right); break;
case EOpSub: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_SUB, result), result, left, right); break;
case EOpMulAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, left, right); break;
case EOpMul: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_MUL, result), result, left, right); break;
case EOpDivAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, left, right); break;
case EOpDiv: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_DIV, result), result, left, right); break;
case EOpIModAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, left, right); break;
case EOpIMod: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_IMOD, result), result, left, right); break;
case EOpBitShiftLeftAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_SHL, result, left, left, right); break;
case EOpBitShiftLeft: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_SHL, result, left, right); break;
case EOpBitShiftRightAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, left, right); break;
case EOpBitShiftRight: if(visit == PostVisit) emitBinary(getOpcode(sw::Shader::OPCODE_ISHR, result), result, left, right); break;
case EOpBitwiseAndAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_AND, result, left, left, right); break;
case EOpBitwiseAnd: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_AND, result, left, right); break;
case EOpBitwiseXorAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_XOR, result, left, left, right); break;
case EOpBitwiseXor: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_XOR, result, left, right); break;
case EOpBitwiseOrAssign: if(visit == PostVisit) emitAssign(sw::Shader::OPCODE_OR, result, left, left, right); break;
case EOpBitwiseOr: if(visit == PostVisit) emitBinary(sw::Shader::OPCODE_OR, result, left, right); break;
case EOpEqual:
if(visit == PostVisit)
{
emitBinary(sw::Shader::OPCODE_EQ, result, left, right);
for(int index = 1; index < left->totalRegisterCount(); index++)
{
Temporary equal(this);
emit(sw::Shader::OPCODE_EQ, &equal, 0, left, index, right, index);
emit(sw::Shader::OPCODE_AND, result, result, &equal);
}
}
break;
case EOpNotEqual:
if(visit == PostVisit)
{
emitBinary(sw::Shader::OPCODE_NE, result, left, right);
for(int index = 1; index < left->totalRegisterCount(); index++)
{
Temporary notEqual(this);
emit(sw::Shader::OPCODE_NE, ¬Equal, 0, left, index, right, index);
emit(sw::Shader::OPCODE_OR, result, result, ¬Equal);
}
}
break;
case EOpLessThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LT, result, left, right); break;
case EOpGreaterThan: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GT, result, left, right); break;
case EOpLessThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_LE, result, left, right); break;
case EOpGreaterThanEqual: if(visit == PostVisit) emitCmp(sw::Shader::CONTROL_GE, result, left, right); break;
case EOpVectorTimesScalarAssign: if(visit == PostVisit) emitAssign(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, left, right); break;
case EOpVectorTimesScalar: if(visit == PostVisit) emit(getOpcode(sw::Shader::OPCODE_MUL, left), result, left, right); break;
case EOpMatrixTimesScalar:
if(visit == PostVisit)
{
if(left->isMatrix())
{
for(int i = 0; i < leftType.getNominalSize(); i++)
{
emit(sw::Shader::OPCODE_MUL, result, i, left, i, right, 0);
}
}
else if(right->isMatrix())
{
for(int i = 0; i < rightType.getNominalSize(); i++)
{
emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
}
}
else UNREACHABLE(0);
}
break;
case EOpVectorTimesMatrix:
if(visit == PostVisit)
{
sw::Shader::Opcode dpOpcode = sw::Shader::OPCODE_DP(leftType.getNominalSize());
int size = rightType.getNominalSize();
for(int i = 0; i < size; i++)
{
Instruction *dot = emit(dpOpcode, result, 0, left, 0, right, i);
dot->dst.mask = 1 << i;
}
}
break;
case EOpMatrixTimesVector:
if(visit == PostVisit)
{
Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, left, right);
mul->src[1].swizzle = 0x00;
int size = rightType.getNominalSize();
for(int i = 1; i < size; i++)
{
Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, 0, left, i, right, 0, result);
mad->src[1].swizzle = i * 0x55;
}
}
break;
case EOpMatrixTimesMatrix:
if(visit == PostVisit)
{
int dim = leftType.getNominalSize();
int size = rightType.getNominalSize();
for(int i = 0; i < size; i++)
{
Instruction *mul = emit(sw::Shader::OPCODE_MUL, result, i, left, 0, right, i);
mul->src[1].swizzle = 0x00;
for(int j = 1; j < dim; j++)
{
Instruction *mad = emit(sw::Shader::OPCODE_MAD, result, i, left, j, right, i, result, i);
mad->src[1].swizzle = j * 0x55;
}
}
}
break;
case EOpLogicalOr:
if(trivial(right, 6))
{
if(visit == PostVisit)
{
emit(sw::Shader::OPCODE_OR, result, left, right);
}
}
else // Short-circuit evaluation
{
if(visit == InVisit)
{
emit(sw::Shader::OPCODE_MOV, result, left);
Instruction *ifnot = emit(sw::Shader::OPCODE_IF, 0, result);
ifnot->src[0].modifier = sw::Shader::MODIFIER_NOT;
}
else if(visit == PostVisit)
{
emit(sw::Shader::OPCODE_MOV, result, right);
emit(sw::Shader::OPCODE_ENDIF);
}
}
break;
case EOpLogicalXor: if(visit == PostVisit) emit(sw::Shader::OPCODE_XOR, result, left, right); break;
case EOpLogicalAnd:
if(trivial(right, 6))
{
if(visit == PostVisit)
{
emit(sw::Shader::OPCODE_AND, result, left, right);
}
}
else // Short-circuit evaluation
{
if(visit == InVisit)
{
emit(sw::Shader::OPCODE_MOV, result, left);
emit(sw::Shader::OPCODE_IF, 0, result);
}
else if(visit == PostVisit)
{
emit(sw::Shader::OPCODE_MOV, result, right);
emit(sw::Shader::OPCODE_ENDIF);
}
}
break;
default: UNREACHABLE(node->getOp());
}
return true;
}
void OutputASM::emitDeterminant(TIntermTyped *result, TIntermTyped *arg, int size, int col, int row, int outCol, int outRow)
{
switch(size)
{
case 1: // Used for cofactor computation only