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