blob: 351e12e8060565ba6fee896ecb063053c5cee003 [file] [log] [blame]
Nicolas Capens0bac2852016-05-07 06:09:58 -04001// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
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
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
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.
14
15#ifndef sw_PixelProgram_hpp
16#define sw_PixelProgram_hpp
17
18#include "PixelRoutine.hpp"
19#include "SamplerCore.hpp"
20
Ben Claytond951f192019-02-11 20:59:19 +000021#include <unordered_map>
22
Nicolas Capens0bac2852016-05-07 06:09:58 -040023namespace sw
24{
25 class PixelProgram : public PixelRoutine
26 {
27 public:
Ben Claytond951f192019-02-11 20:59:19 +000028 PixelProgram(const PixelProcessor::State &state, const PixelShader *shader);
Nicolas Capens0bac2852016-05-07 06:09:58 -040029 virtual ~PixelProgram() {}
30
31 protected:
32 virtual void setBuiltins(Int &x, Int &y, Float4(&z)[4], Float4 &w);
33 virtual void applyShader(Int cMask[4]);
34 virtual Bool alphaTest(Int cMask[4]);
35 virtual void rasterOperation(Float4 &fog, Pointer<Byte> cBuffer[4], Int &x, Int sMask[4], Int zMask[4], Int cMask[4]);
36
37 private:
38 // Temporary registers
Alexis Hetu329747c2018-04-09 13:47:34 -040039 RegisterArray<NUM_TEMPORARY_REGISTERS> r;
Nicolas Capens0bac2852016-05-07 06:09:58 -040040
41 // Color outputs
42 Vector4f c[RENDERTARGETS];
43 RegisterArray<RENDERTARGETS, true> oC;
44
45 // Shader variables
46 Vector4f vPos;
47 Vector4f vFace;
48
49 // DX9 specific variables
50 Vector4f p0;
Ben Claytond951f192019-02-11 20:59:19 +000051 Array<Int> aL; // loop counter register
52 Array<Int> increment; // increment value per loop
53 Array<Int> iteration; // iteration count
Nicolas Capens0bac2852016-05-07 06:09:58 -040054
55 Int loopDepth; // FIXME: Add support for switch
56 Int stackIndex; // FIXME: Inc/decrement callStack
Ben Claytond951f192019-02-11 20:59:19 +000057 Array<UInt> callStack;
Nicolas Capens0bac2852016-05-07 06:09:58 -040058
59 // Per pixel based on conditions reached
60 Int enableIndex;
Alexis Hetu48d47a42019-01-10 14:04:26 -050061 Array<Int4, MAX_SHADER_ENABLE_STACK_SIZE> enableStack;
Nicolas Capens0bac2852016-05-07 06:09:58 -040062 Int4 enableBreak;
63 Int4 enableContinue;
64 Int4 enableLeave;
65
Nicolas Capensa0b57832017-11-07 13:07:53 -050066 Vector4f sampleTexture(const Src &sampler, Vector4f &uvwq, Float4 &bias, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function);
67 Vector4f sampleTexture(int samplerIndex, Vector4f &uvwq, Float4 &bias, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function);
Nicolas Capens0bac2852016-05-07 06:09:58 -040068
69 // Raster operations
70 void clampColor(Vector4f oC[RENDERTARGETS]);
71
72 Int4 enableMask(const Shader::Instruction *instruction);
73
74 Vector4f fetchRegister(const Src &src, unsigned int offset = 0);
75 Vector4f readConstant(const Src &src, unsigned int offset = 0);
76 RValue<Pointer<Byte>> uniformAddress(int bufferIndex, unsigned int index);
77 RValue<Pointer<Byte>> uniformAddress(int bufferIndex, unsigned int index, Int& offset);
Nicolas Capens4b743732018-05-28 13:22:07 -040078 Int relativeAddress(const Shader::Relative &rel, int bufferIndex = -1);
79 Int4 dynamicAddress(const Shader::Relative &rel);
Nicolas Capens0bac2852016-05-07 06:09:58 -040080
81 Float4 linearToSRGB(const Float4 &x);
82
83 // Instructions
84 typedef Shader::Control Control;
85
86 void M3X2(Vector4f &dst, Vector4f &src0, const Src &src1);
87 void M3X3(Vector4f &dst, Vector4f &src0, const Src &src1);
88 void M3X4(Vector4f &dst, Vector4f &src0, const Src &src1);
89 void M4X3(Vector4f &dst, Vector4f &src0, const Src &src1);
90 void M4X4(Vector4f &dst, Vector4f &src0, const Src &src1);
Nicolas Capensa0b57832017-11-07 13:07:53 -050091 void TEX(Vector4f &dst, Vector4f &src0, const Src &src1, bool project, bool bias);
92 void TEXLOD(Vector4f &dst, Vector4f &src0, const Src &src1, Float4 &lod);
93 void TEXBIAS(Vector4f &dst, Vector4f &src0, const Src &src1, Float4 &bias);
Nicolas Capens0bac2852016-05-07 06:09:58 -040094 void TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1);
95 void TEXKILL(Int cMask[4], Vector4f &src, unsigned char mask);
Nicolas Capensa0b57832017-11-07 13:07:53 -050096 void TEXOFFSET(Vector4f &dst, Vector4f &src0, const Src &src1, Vector4f &offset);
97 void TEXOFFSETBIAS(Vector4f &dst, Vector4f &src0, const Src &src1, Vector4f &offset, Float4 &bias);
98 void TEXLODOFFSET(Vector4f &dst, Vector4f &src0, const Src &src1, Vector4f &offset, Float4 &lod);
99 void TEXELFETCH(Vector4f &dst, Vector4f &src, const Src &, Float4 &lod);
100 void TEXELFETCHOFFSET(Vector4f &dst, Vector4f &src, const Src &, Vector4f &offset, Float4 &lod);
101 void TEXGRAD(Vector4f &dst, Vector4f &src0, const Src &src1, Vector4f &dsx, Vector4f &dsy);
102 void TEXGRADOFFSET(Vector4f &dst, Vector4f &src, const Src &, Vector4f &dsx, Vector4f &dsy, Vector4f &offset);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400103 void DISCARD(Int cMask[4], const Shader::Instruction *instruction);
104 void DFDX(Vector4f &dst, Vector4f &src);
105 void DFDY(Vector4f &dst, Vector4f &src);
106 void FWIDTH(Vector4f &dst, Vector4f &src);
107 void BREAK();
108 void BREAKC(Vector4f &src0, Vector4f &src1, Control);
109 void BREAKP(const Src &predicateRegister);
110 void BREAK(Int4 &condition);
111 void CONTINUE();
112 void TEST();
Nicolas Capens6e8ec332018-11-06 11:56:21 -0500113 void SCALAR();
Nicolas Capens0bac2852016-05-07 06:09:58 -0400114 void CALL(int labelIndex, int callSiteIndex);
115 void CALLNZ(int labelIndex, int callSiteIndex, const Src &src);
116 void CALLNZb(int labelIndex, int callSiteIndex, const Src &boolRegister);
117 void CALLNZp(int labelIndex, int callSiteIndex, const Src &predicateRegister);
118 void ELSE();
119 void ENDIF();
120 void ENDLOOP();
121 void ENDREP();
122 void ENDWHILE();
Alexis Hetu9aa83a92016-05-02 17:34:46 -0400123 void ENDSWITCH();
Nicolas Capens0bac2852016-05-07 06:09:58 -0400124 void IF(const Src &src);
125 void IFb(const Src &boolRegister);
126 void IFp(const Src &predicateRegister);
127 void IFC(Vector4f &src0, Vector4f &src1, Control);
128 void IF(Int4 &condition);
129 void LABEL(int labelIndex);
130 void LOOP(const Src &integerRegister);
131 void REP(const Src &integerRegister);
132 void WHILE(const Src &temporaryRegister);
Alexis Hetu9aa83a92016-05-02 17:34:46 -0400133 void SWITCH();
Nicolas Capens0bac2852016-05-07 06:09:58 -0400134 void RET();
135 void LEAVE();
136
Ben Clayton0a608182019-02-19 18:50:29 +0000137 int ifDepth = 0;
138 int loopRepDepth = 0;
139 int currentLabel = -1;
Nicolas Capens6e8ec332018-11-06 11:56:21 -0500140 bool scalar = false;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400141
Ben Claytond951f192019-02-11 20:59:19 +0000142 std::vector<BasicBlock*> ifFalseBlock;
143 std::vector<BasicBlock*> loopRepTestBlock;
144 std::vector<BasicBlock*> loopRepEndBlock;
145 std::vector<BasicBlock*> labelBlock;
146 std::unordered_map<unsigned int, std::vector<BasicBlock*>> callRetBlock; // label -> list of call sites
Nicolas Capensc8b67a42016-09-25 15:02:52 -0400147 BasicBlock *returnBlock;
Ben Claytond951f192019-02-11 20:59:19 +0000148 std::vector<bool> isConditionalIf;
Nicolas Capens2f490f02018-11-01 16:53:36 -0400149 std::vector<Int4> restoreContinue;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400150 };
151}
152
153#endif