blob: cea417d238b39f92b4dac9e402b620fb1a90f6c8 [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_PixelProcessor_hpp
16#define sw_PixelProcessor_hpp
17
18#include "Context.hpp"
19#include "RoutineCache.hpp"
20
21namespace sw
22{
23 class PixelShader;
24 class Rasterizer;
25 struct Texture;
26 struct DrawData;
27
28 class PixelProcessor
29 {
30 public:
31 struct States
32 {
33 unsigned int computeHash();
34
35 int shaderID;
36
Nicolas Capensac6d5052018-01-05 15:34:00 -050037 bool depthOverride : 1; // TODO: Eliminate by querying shader.
38 bool shaderContainsKill : 1; // TODO: Eliminate by querying shader.
Nicolas Capens0bac2852016-05-07 06:09:58 -040039
40 DepthCompareMode depthCompareMode : BITS(DEPTH_LAST);
41 AlphaCompareMode alphaCompareMode : BITS(ALPHA_LAST);
42 bool depthWriteEnable : 1;
43 bool quadLayoutDepthBuffer : 1;
44
45 bool stencilActive : 1;
46 StencilCompareMode stencilCompareMode : BITS(STENCIL_LAST);
47 StencilOperation stencilFailOperation : BITS(OPERATION_LAST);
48 StencilOperation stencilPassOperation : BITS(OPERATION_LAST);
49 StencilOperation stencilZFailOperation : BITS(OPERATION_LAST);
50 bool noStencilMask : 1;
51 bool noStencilWriteMask : 1;
52 bool stencilWriteMasked : 1;
53 bool twoSidedStencil : 1;
54 StencilCompareMode stencilCompareModeCCW : BITS(STENCIL_LAST);
55 StencilOperation stencilFailOperationCCW : BITS(OPERATION_LAST);
56 StencilOperation stencilPassOperationCCW : BITS(OPERATION_LAST);
57 StencilOperation stencilZFailOperationCCW : BITS(OPERATION_LAST);
58 bool noStencilMaskCCW : 1;
59 bool noStencilWriteMaskCCW : 1;
60 bool stencilWriteMaskedCCW : 1;
61
62 bool depthTestActive : 1;
63 bool fogActive : 1;
64 FogMode pixelFogMode : BITS(FOG_LAST);
65 bool specularAdd : 1;
66 bool occlusionEnabled : 1;
67 bool wBasedFog : 1;
68 bool perspective : 1;
Nicolas Capens3cbeac52017-09-15 11:49:31 -040069 bool depthClamp : 1;
Nicolas Capens0bac2852016-05-07 06:09:58 -040070
71 bool alphaBlendActive : 1;
72 BlendFactor sourceBlendFactor : BITS(BLEND_LAST);
73 BlendFactor destBlendFactor : BITS(BLEND_LAST);
74 BlendOperation blendOperation : BITS(BLENDOP_LAST);
75 BlendFactor sourceBlendFactorAlpha : BITS(BLEND_LAST);
76 BlendFactor destBlendFactorAlpha : BITS(BLEND_LAST);
77 BlendOperation blendOperationAlpha : BITS(BLENDOP_LAST);
78
79 unsigned int colorWriteMask : RENDERTARGETS * 4; // Four component bit masks
80 Format targetFormat[RENDERTARGETS];
81 bool writeSRGB : 1;
82 unsigned int multiSample : 3;
83 unsigned int multiSampleMask : 4;
84 TransparencyAntialiasing transparencyAntialiasing : BITS(TRANSPARENCY_LAST);
85 bool centroid : 1;
86
87 LogicalOperation logicalOperation : BITS(LOGICALOP_LAST);
88
89 Sampler::State sampler[TEXTURE_IMAGE_UNITS];
90 TextureStage::State textureStage[8];
91
92 struct Interpolant
93 {
94 unsigned char component : 4;
95 unsigned char flat : 4;
96 unsigned char project : 2;
97 bool centroid : 1;
98 };
99
100 union
101 {
102 struct
103 {
104 Interpolant color[2];
105 Interpolant texture[8];
Nicolas Capens3b4c93f2016-05-18 12:51:37 -0400106 Interpolant fog;
Nicolas Capens0bac2852016-05-07 06:09:58 -0400107 };
108
Nicolas Capens3b4c93f2016-05-18 12:51:37 -0400109 Interpolant interpolant[MAX_FRAGMENT_INPUTS];
Nicolas Capens0bac2852016-05-07 06:09:58 -0400110 };
Nicolas Capens0bac2852016-05-07 06:09:58 -0400111 };
112
113 struct State : States
114 {
115 State();
116
117 bool operator==(const State &state) const;
118
119 int colorWriteActive(int index) const
120 {
121 return (colorWriteMask >> (index * 4)) & 0xF;
122 }
123
124 bool alphaTestActive() const
125 {
Alexis Hetua9beed32016-12-13 16:48:01 -0500126 return (alphaCompareMode != ALPHA_ALWAYS) || (transparencyAntialiasing != TRANSPARENCY_NONE);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400127 }
128
129 bool pixelFogActive() const
130 {
131 return pixelFogMode != FOG_NONE;
132 }
133
134 unsigned int hash;
135 };
136
137 struct Stencil
138 {
139 int64_t testMaskQ;
140 int64_t referenceMaskedQ;
141 int64_t referenceMaskedSignedQ;
142 int64_t writeMaskQ;
143 int64_t invWriteMaskQ;
144 int64_t referenceQ;
145
146 void set(int reference, int testMask, int writeMask)
147 {
148 referenceQ = replicate(reference);
149 testMaskQ = replicate(testMask);
150 writeMaskQ = replicate(writeMask);
151 invWriteMaskQ = ~writeMaskQ;
152 referenceMaskedQ = referenceQ & testMaskQ;
Nicolas Capensa28d04b2017-09-11 12:19:51 -0400153 referenceMaskedSignedQ = replicate(((reference & testMask) + 0x80) & 0xFF);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400154 }
155
156 static int64_t replicate(int b)
157 {
158 int64_t w = b & 0xFF;
159
160 return (w << 0) | (w << 8) | (w << 16) | (w << 24) | (w << 32) | (w << 40) | (w << 48) | (w << 56);
161 }
162 };
163
164 struct Fog
165 {
166 float4 scale;
167 float4 offset;
168 word4 color4[3];
169 float4 colorF[3];
170 float4 densityE;
171 float4 density2E;
172 };
173
174 struct Factor
175 {
176 word4 textureFactor4[4];
177
178 word4 alphaReference4;
179
180 word4 blendConstant4W[4];
181 float4 blendConstant4F[4];
182 word4 invBlendConstant4W[4];
183 float4 invBlendConstant4F[4];
184 };
185
186 public:
187 typedef void (*RoutinePointer)(const Primitive *primitive, int count, int thread, DrawData *draw);
188
189 PixelProcessor(Context *context);
190
191 virtual ~PixelProcessor();
192
Alexis Hetuc634fb62016-06-02 10:53:13 -0400193 void setFloatConstant(unsigned int index, const float value[4]);
194 void setIntegerConstant(unsigned int index, const int value[4]);
195 void setBooleanConstant(unsigned int index, int boolean);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400196
Alexis Hetuc634fb62016-06-02 10:53:13 -0400197 void setUniformBuffer(int index, sw::Resource* buffer, int offset);
198 void lockUniformBuffers(byte** u, sw::Resource* uniformBuffers[]);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400199
Nicolas Capens8af24c52017-12-11 14:45:01 -0500200 void setRenderTarget(int index, Surface *renderTarget, unsigned int layer = 0);
201 void setDepthBuffer(Surface *depthBuffer, unsigned int layer = 0);
202 void setStencilBuffer(Surface *stencilBuffer, unsigned int layer = 0);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400203
Alexis Hetuc634fb62016-06-02 10:53:13 -0400204 void setTexCoordIndex(unsigned int stage, int texCoordIndex);
205 void setStageOperation(unsigned int stage, TextureStage::StageOperation stageOperation);
206 void setFirstArgument(unsigned int stage, TextureStage::SourceArgument firstArgument);
207 void setSecondArgument(unsigned int stage, TextureStage::SourceArgument secondArgument);
208 void setThirdArgument(unsigned int stage, TextureStage::SourceArgument thirdArgument);
209 void setStageOperationAlpha(unsigned int stage, TextureStage::StageOperation stageOperationAlpha);
210 void setFirstArgumentAlpha(unsigned int stage, TextureStage::SourceArgument firstArgumentAlpha);
211 void setSecondArgumentAlpha(unsigned int stage, TextureStage::SourceArgument secondArgumentAlpha);
212 void setThirdArgumentAlpha(unsigned int stage, TextureStage::SourceArgument thirdArgumentAlpha);
213 void setFirstModifier(unsigned int stage, TextureStage::ArgumentModifier firstModifier);
214 void setSecondModifier(unsigned int stage, TextureStage::ArgumentModifier secondModifier);
215 void setThirdModifier(unsigned int stage, TextureStage::ArgumentModifier thirdModifier);
216 void setFirstModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier firstModifierAlpha);
217 void setSecondModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier secondModifierAlpha);
218 void setThirdModifierAlpha(unsigned int stage, TextureStage::ArgumentModifier thirdModifierAlpha);
219 void setDestinationArgument(unsigned int stage, TextureStage::DestinationArgument destinationArgument);
220 void setConstantColor(unsigned int stage, const Color<float> &constantColor);
221 void setBumpmapMatrix(unsigned int stage, int element, float value);
222 void setLuminanceScale(unsigned int stage, float value);
223 void setLuminanceOffset(unsigned int stage, float value);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400224
Alexis Hetuc634fb62016-06-02 10:53:13 -0400225 void setTextureFilter(unsigned int sampler, FilterType textureFilter);
226 void setMipmapFilter(unsigned int sampler, MipmapType mipmapFilter);
227 void setGatherEnable(unsigned int sampler, bool enable);
228 void setAddressingModeU(unsigned int sampler, AddressingMode addressingMode);
229 void setAddressingModeV(unsigned int sampler, AddressingMode addressingMode);
230 void setAddressingModeW(unsigned int sampler, AddressingMode addressingMode);
231 void setReadSRGB(unsigned int sampler, bool sRGB);
232 void setMipmapLOD(unsigned int sampler, float bias);
233 void setBorderColor(unsigned int sampler, const Color<float> &borderColor);
234 void setMaxAnisotropy(unsigned int sampler, float maxAnisotropy);
Alexis Hetu010a4642017-07-18 14:33:04 -0400235 void setHighPrecisionFiltering(unsigned int sampler, bool highPrecisionFiltering);
Alexis Hetuc634fb62016-06-02 10:53:13 -0400236 void setSwizzleR(unsigned int sampler, SwizzleType swizzleR);
237 void setSwizzleG(unsigned int sampler, SwizzleType swizzleG);
238 void setSwizzleB(unsigned int sampler, SwizzleType swizzleB);
239 void setSwizzleA(unsigned int sampler, SwizzleType swizzleA);
Nicolas Capensf878d502017-11-06 15:29:46 -0500240 void setCompareFunc(unsigned int sampler, CompareFunc compare);
Alexis Hetuc634fb62016-06-02 10:53:13 -0400241 void setBaseLevel(unsigned int sampler, int baseLevel);
242 void setMaxLevel(unsigned int sampler, int maxLevel);
243 void setMinLod(unsigned int sampler, float minLod);
244 void setMaxLod(unsigned int sampler, float maxLod);
Alexis Hetu88482c32018-06-05 17:05:17 -0400245 void setSyncRequired(unsigned int sampler, bool isSincRequired);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400246
Alexis Hetuc634fb62016-06-02 10:53:13 -0400247 void setWriteSRGB(bool sRGB);
248 void setDepthBufferEnable(bool depthBufferEnable);
249 void setDepthCompare(DepthCompareMode depthCompareMode);
250 void setAlphaCompare(AlphaCompareMode alphaCompareMode);
251 void setDepthWriteEnable(bool depthWriteEnable);
252 void setAlphaTestEnable(bool alphaTestEnable);
253 void setCullMode(CullMode cullMode);
254 void setColorWriteMask(int index, int rgbaMask);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400255
Alexis Hetuc634fb62016-06-02 10:53:13 -0400256 void setColorLogicOpEnabled(bool colorLogicOpEnabled);
257 void setLogicalOperation(LogicalOperation logicalOperation);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400258
Alexis Hetuc634fb62016-06-02 10:53:13 -0400259 void setStencilEnable(bool stencilEnable);
260 void setStencilCompare(StencilCompareMode stencilCompareMode);
261 void setStencilReference(int stencilReference);
262 void setStencilMask(int stencilMask);
263 void setStencilFailOperation(StencilOperation stencilFailOperation);
264 void setStencilPassOperation(StencilOperation stencilPassOperation);
265 void setStencilZFailOperation(StencilOperation stencilZFailOperation);
266 void setStencilWriteMask(int stencilWriteMask);
267 void setTwoSidedStencil(bool enable);
268 void setStencilCompareCCW(StencilCompareMode stencilCompareMode);
269 void setStencilReferenceCCW(int stencilReference);
270 void setStencilMaskCCW(int stencilMask);
271 void setStencilFailOperationCCW(StencilOperation stencilFailOperation);
272 void setStencilPassOperationCCW(StencilOperation stencilPassOperation);
273 void setStencilZFailOperationCCW(StencilOperation stencilZFailOperation);
274 void setStencilWriteMaskCCW(int stencilWriteMask);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400275
Alexis Hetuc634fb62016-06-02 10:53:13 -0400276 void setTextureFactor(const Color<float> &textureFactor);
277 void setBlendConstant(const Color<float> &blendConstant);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400278
Alexis Hetuc634fb62016-06-02 10:53:13 -0400279 void setFillMode(FillMode fillMode);
280 void setShadingMode(ShadingMode shadingMode);
Nicolas Capens3b4c93f2016-05-18 12:51:37 -0400281
Alexis Hetuc634fb62016-06-02 10:53:13 -0400282 void setAlphaBlendEnable(bool alphaBlendEnable);
283 void setSourceBlendFactor(BlendFactor sourceBlendFactor);
284 void setDestBlendFactor(BlendFactor destBlendFactor);
285 void setBlendOperation(BlendOperation blendOperation);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400286
Alexis Hetuc634fb62016-06-02 10:53:13 -0400287 void setSeparateAlphaBlendEnable(bool separateAlphaBlendEnable);
288 void setSourceBlendFactorAlpha(BlendFactor sourceBlendFactorAlpha);
289 void setDestBlendFactorAlpha(BlendFactor destBlendFactorAlpha);
290 void setBlendOperationAlpha(BlendOperation blendOperationAlpha);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400291
Alexis Hetuc634fb62016-06-02 10:53:13 -0400292 void setAlphaReference(float alphaReference);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400293
Alexis Hetuc634fb62016-06-02 10:53:13 -0400294 void setGlobalMipmapBias(float bias);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400295
Alexis Hetuc634fb62016-06-02 10:53:13 -0400296 void setFogStart(float start);
297 void setFogEnd(float end);
298 void setFogColor(Color<float> fogColor);
299 void setFogDensity(float fogDensity);
300 void setPixelFogMode(FogMode fogMode);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400301
Alexis Hetuc634fb62016-06-02 10:53:13 -0400302 void setPerspectiveCorrection(bool perspectiveCorrection);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400303
Alexis Hetuc634fb62016-06-02 10:53:13 -0400304 void setOcclusionEnabled(bool enable);
Nicolas Capens0bac2852016-05-07 06:09:58 -0400305
306 protected:
307 const State update() const;
308 Routine *routine(const State &state);
309 void setRoutineCacheSize(int routineCacheSize);
310
311 // Shader constants
312 word4 cW[8][4];
313 float4 c[FRAGMENT_UNIFORM_VECTORS];
314 int4 i[16];
315 bool b[16];
316
317 // Other semi-constants
318 Stencil stencil;
319 Stencil stencilCCW;
320 Fog fog;
321 Factor factor;
322
323 private:
324 struct UniformBufferInfo
325 {
326 UniformBufferInfo();
327
328 Resource* buffer;
329 int offset;
330 };
331 UniformBufferInfo uniformBufferInfo[MAX_UNIFORM_BUFFER_BINDINGS];
332
333 void setFogRanges(float start, float end);
334
335 Context *const context;
336
337 RoutineCache<State> *routineCache;
338 };
339}
340
341#endif // sw_PixelProcessor_hpp