1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
18 #include "Common/Types.hpp"
30 SHADER_PIXEL = 0xFFFF,
31 SHADER_VERTEX = 0xFFFE,
32 SHADER_GEOMETRY = 0xFFFD
37 // Matches order in d3d9types.h
52 OPCODE_EXP2X, // D3DSIO_EXP
53 OPCODE_LOG2X, // D3DSIO_LOG
55 OPCODE_ATT, // D3DSIO_DST
74 OPCODE_NRM3, // D3DSIO_NRM
112 OPCODE_CMP0, // D3DSIO_CMP
115 OPCODE_DFDX, // D3DSIO_DSX
116 OPCODE_DFDY, // D3DSIO_DSY
118 OPCODE_CMP, // D3DSIO_SETP
123 OPCODE_PHASE = 0xFFFD,
124 OPCODE_COMMENT = 0xFFFE,
127 OPCODE_PS_1_0 = 0xFFFF0100,
128 OPCODE_PS_1_1 = 0xFFFF0101,
129 OPCODE_PS_1_2 = 0xFFFF0102,
130 OPCODE_PS_1_3 = 0xFFFF0103,
131 OPCODE_PS_1_4 = 0xFFFF0104,
132 OPCODE_PS_2_0 = 0xFFFF0200,
133 OPCODE_PS_2_x = 0xFFFF0201,
134 OPCODE_PS_3_0 = 0xFFFF0300,
136 OPCODE_VS_1_0 = 0xFFFE0100,
137 OPCODE_VS_1_1 = 0xFFFE0101,
138 OPCODE_VS_2_0 = 0xFFFE0200,
139 OPCODE_VS_2_x = 0xFFFE0201,
140 OPCODE_VS_2_sw = 0xFFFE02FF,
141 OPCODE_VS_3_0 = 0xFFFE0300,
142 OPCODE_VS_3_sw = 0xFFFE03FF,
144 OPCODE_NULL = 0x10000000, // Dead instruction, to be eliminated
185 OPCODE_F2B, // Float to bool
186 OPCODE_B2F, // Bool to float
187 OPCODE_F2I, // Float to int
188 OPCODE_I2F, // Int to float
189 OPCODE_F2U, // Float to uint
190 OPCODE_U2F, // Uint to float
191 OPCODE_I2B, // Int to bool
192 OPCODE_B2I, // Bool to int
212 OPCODE_TEXELFETCHOFFSET,
214 OPCODE_TEXGRADOFFSET,
215 OPCODE_FLOATBITSTOINT,
216 OPCODE_FLOATBITSTOUINT,
217 OPCODE_INTBITSTOFLOAT,
218 OPCODE_UINTBITSTOFLOAT,
219 OPCODE_PACKSNORM2x16,
220 OPCODE_PACKUNORM2x16,
222 OPCODE_UNPACKSNORM2x16,
223 OPCODE_UNPACKUNORM2x16,
224 OPCODE_UNPACKHALF2x16,
244 OPCODE_LEAVE, // Return before the end of the function
246 OPCODE_TEST, // Marks the end of the code that can be skipped by 'continue'
265 // Unsigned integer opcodes
273 static Opcode OPCODE_DP(int);
274 static Opcode OPCODE_LEN(int);
275 static Opcode OPCODE_DIST(int);
276 static Opcode OPCODE_NRM(int);
277 static Opcode OPCODE_FORWARD(int);
278 static Opcode OPCODE_REFLECT(int);
279 static Opcode OPCODE_REFRACT(int);
302 enum Usage // For vertex input/output declarations
305 USAGE_BLENDWEIGHT = 1,
306 USAGE_BLENDINDICES = 2,
312 USAGE_TESSFACTOR = 8,
325 PARAMETER_TEXTURE = 3,
327 PARAMETER_RASTOUT = 4,
328 PARAMETER_ATTROUT = 5,
329 PARAMETER_TEXCRDOUT = 6,
330 PARAMETER_OUTPUT = 6,
331 PARAMETER_CONSTINT = 7,
332 PARAMETER_COLOROUT = 8,
333 PARAMETER_DEPTHOUT = 9,
334 PARAMETER_SAMPLER = 10,
335 PARAMETER_CONST2 = 11,
336 PARAMETER_CONST3 = 12,
337 PARAMETER_CONST4 = 13,
338 PARAMETER_CONSTBOOL = 14,
340 PARAMETER_TEMPFLOAT16 = 16,
341 PARAMETER_MISCTYPE = 17,
342 PARAMETER_LABEL = 18,
343 PARAMETER_PREDICATE = 19,
345 // PARAMETER_FLOAT1LITERAL,
346 // PARAMETER_FLOAT2LITERAL,
347 // PARAMETER_FLOAT3LITERAL,
348 PARAMETER_FLOAT4LITERAL,
349 PARAMETER_BOOL1LITERAL,
350 // PARAMETER_BOOL2LITERAL,
351 // PARAMETER_BOOL3LITERAL,
352 // PARAMETER_BOOL4LITERAL,
353 // PARAMETER_INT1LITERAL,
354 // PARAMETER_INT2LITERAL,
355 // PARAMETER_INT3LITERAL,
356 PARAMETER_INT4LITERAL,
361 enum MiscParameterIndex
374 MODIFIER_BIAS_NEGATE,
376 MODIFIER_SIGN_NEGATE,
389 // Flags indicating whether an instruction is affected by an execution enable mask
390 ANALYSIS_BRANCH = 0x00000001,
391 ANALYSIS_BREAK = 0x00000002,
392 ANALYSIS_CONTINUE = 0x00000004,
393 ANALYSIS_LEAVE = 0x00000008,
402 unsigned int index; // For registers types
406 ParameterType type : 8;
408 unsigned int swizzle : 8;
410 bool deterministic; // Equal accross shader instances run in lockstep (e.g. unrollable loop couters)
414 float value[4]; // For float constants
415 int integer[4]; // For integer constants
416 int boolean[4]; // For boolean constants
420 unsigned int label; // Label index
421 unsigned int callSite; // Call index (per label)
425 Parameter() : index(0), type(PARAMETER_VOID)
427 rel.type = PARAMETER_VOID;
431 rel.deterministic = false;
434 std::string string(ShaderType shaderType, unsigned short version) const;
435 std::string typeString(ShaderType shaderType, unsigned short version) const;
436 std::string relativeString() const;
438 ParameterType type : 8;
441 struct DestinationParameter : Parameter
456 DestinationParameter() : mask(0xF), integer(false), saturate(false), partialPrecision(false), centroid(false), shift(0)
460 std::string modifierString() const;
461 std::string shiftString() const;
462 std::string maskString() const;
466 bool partialPrecision : 1;
468 signed char shift : 4;
471 struct SourceParameter : Parameter
473 SourceParameter() : swizzle(0xE4), modifier(MODIFIER_NONE), bufferIndex(-1)
477 std::string swizzleString() const;
478 std::string preModifierString() const;
479 std::string postModifierString() const;
481 unsigned int swizzle : 8;
482 Modifier modifier : 8;
488 explicit Instruction(Opcode opcode);
489 Instruction(const unsigned long *token, int size, unsigned char majorVersion);
491 virtual ~Instruction();
493 void parseOperationToken(unsigned long token, unsigned char majorVersion);
494 void parseDeclarationToken(unsigned long token);
495 void parseDestinationToken(const unsigned long *token, unsigned char majorVersion);
496 void parseSourceToken(int i, const unsigned long *token, unsigned char majorVersion);
498 std::string string(ShaderType shaderType, unsigned short version) const;
499 static std::string swizzleString(ParameterType type, unsigned char swizzle);
500 std::string operationString(unsigned short version) const;
501 std::string controlString() const;
503 bool isBranch() const;
505 bool isBreak() const;
507 bool isEndLoop() const;
509 bool isPredicated() const;
519 unsigned char project : 1; // D3DSI_TEXLD_PROJECT
520 unsigned char bias : 1; // D3DSI_TEXLD_BIAS
525 bool predicateNot; // Negative predicate
526 unsigned char predicateSwizzle;
529 SamplerType samplerType;
531 unsigned char usageIndex;
533 DestinationParameter dst;
534 SourceParameter src[5];
538 unsigned int analysis;
542 // Keep in sync with Shader::Analysis flags
543 unsigned int analysisBranch : 1;
544 unsigned int analysisBreak : 1;
545 unsigned int analysisContinue : 1;
546 unsigned int analysisLeave : 1;
555 int getSerialID() const;
556 size_t getLength() const;
557 ShaderType getShaderType() const;
558 unsigned short getVersion() const;
560 void append(Instruction *instruction);
561 void declareSampler(int i);
563 const Instruction *getInstruction(size_t i) const;
564 int size(unsigned long opcode) const;
565 static int size(unsigned long opcode, unsigned short version);
567 void print(const char *fileName, ...) const;
568 void printInstruction(int index, const char *fileName) const;
570 static bool maskContainsComponent(int mask, int component);
571 static bool swizzleContainsComponent(int swizzle, int component);
572 static bool swizzleContainsComponentMasked(int swizzle, int component, int mask);
574 bool containsDynamicBranching() const;
575 bool containsBreakInstruction() const;
576 bool containsContinueInstruction() const;
577 bool containsLeaveInstruction() const;
578 bool containsDefineInstruction() const;
579 bool usesSampler(int i) const;
583 Semantic(unsigned char usage = 0xFF, unsigned char index = 0xFF, bool flat = false) : usage(usage), index(index), centroid(false), flat(flat)
587 bool operator==(const Semantic &semantic) const
589 return usage == semantic.usage && index == semantic.index;
594 return usage != 0xFF;
606 unsigned int dirtyConstantsF;
607 unsigned int dirtyConstantsI;
608 unsigned int dirtyConstantsB;
610 bool dynamicallyIndexedTemporaries;
611 bool dynamicallyIndexedInput;
612 bool dynamicallyIndexedOutput;
615 void parse(const unsigned long *token);
617 void optimizeLeave();
621 void analyzeDirtyConstants();
622 void analyzeDynamicBranching();
623 void analyzeSamplers();
624 void analyzeCallSites();
625 void analyzeDynamicIndexing();
626 void markFunctionAnalysis(unsigned int functionLabel, Analysis flag);
628 ShaderType shaderType;
632 unsigned short version;
636 unsigned char minorVersion;
637 unsigned char majorVersion;
641 std::vector<Instruction*> instruction;
643 unsigned short usedSamplers; // Bit flags
647 static volatile int serialCounter;
649 bool dynamicBranching;
651 bool containsContinue;
657 #endif // sw_Shader_hpp