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_Context_hpp
16 #define sw_Context_hpp
17 
18 #include "Sampler.hpp"
19 #include "TextureStage.hpp"
20 #include "Stream.hpp"
21 #include "Point.hpp"
22 #include "Vertex.hpp"
23 
24 namespace sw
25 {
26 	class Sampler;
27 	class Surface;
28 	class PixelShader;
29 	class VertexShader;
30 	struct Triangle;
31 	struct Primitive;
32 	struct Vertex;
33 	class Resource;
34 
35 	enum In   // Default input stream semantic
36 	{
37 		Position = 0,
38 		BlendWeight = 1,
39 		BlendIndices = 2,
40 		Normal = 3,
41 		PointSize = 4,
42 		Color0 = 5,
43 		Color1 = 6,
44 		TexCoord0 = 7,
45 		TexCoord1 = 8,
46 		TexCoord2 = 9,
47 		TexCoord3 = 10,
48 		TexCoord4 = 11,
49 		TexCoord5 = 12,
50 		TexCoord6 = 13,
51 		TexCoord7 = 14,
52 		PositionT = 15
53 	};
54 
55 	enum DrawType : unsigned int
56 	{
57 		// These types must stay ordered by vertices per primitive. Also, if these basic types
58 		// are modified, verify the value assigned to task->verticesPerPrimitive in Renderer.cpp
59 		DRAW_POINTLIST     = 0x00,
60 		DRAW_LINELIST      = 0x01,
61 		DRAW_LINESTRIP     = 0x02,
62 		DRAW_LINELOOP      = 0x03,
63 		DRAW_TRIANGLELIST  = 0x04,
64 		DRAW_TRIANGLESTRIP = 0x05,
65 		DRAW_TRIANGLEFAN   = 0x06,
66 		DRAW_QUADLIST      = 0x07,
67 
68 		DRAW_NONINDEXED = 0x00,
69 		DRAW_INDEXED8   = 0x10,
70 		DRAW_INDEXED16  = 0x20,
71 		DRAW_INDEXED32  = 0x30,
72 
73 		DRAW_INDEXEDPOINTLIST8 = DRAW_POINTLIST | DRAW_INDEXED8,
74 		DRAW_INDEXEDLINELIST8  = DRAW_LINELIST  | DRAW_INDEXED8,
75 		DRAW_INDEXEDLINESTRIP8 = DRAW_LINESTRIP | DRAW_INDEXED8,
76 		DRAW_INDEXEDLINELOOP8  = DRAW_LINELOOP  | DRAW_INDEXED8,
77 		DRAW_INDEXEDTRIANGLELIST8  = DRAW_TRIANGLELIST  | DRAW_INDEXED8,
78 		DRAW_INDEXEDTRIANGLESTRIP8 = DRAW_TRIANGLESTRIP | DRAW_INDEXED8,
79 		DRAW_INDEXEDTRIANGLEFAN8   = DRAW_TRIANGLEFAN   | DRAW_INDEXED8,
80 
81 		DRAW_INDEXEDPOINTLIST16 = DRAW_POINTLIST | DRAW_INDEXED16,
82 		DRAW_INDEXEDLINELIST16  = DRAW_LINELIST  | DRAW_INDEXED16,
83 		DRAW_INDEXEDLINESTRIP16 = DRAW_LINESTRIP | DRAW_INDEXED16,
84 		DRAW_INDEXEDLINELOOP16  = DRAW_LINELOOP  | DRAW_INDEXED16,
85 		DRAW_INDEXEDTRIANGLELIST16  = DRAW_TRIANGLELIST  | DRAW_INDEXED16,
86 		DRAW_INDEXEDTRIANGLESTRIP16 = DRAW_TRIANGLESTRIP | DRAW_INDEXED16,
87 		DRAW_INDEXEDTRIANGLEFAN16   = DRAW_TRIANGLEFAN   | DRAW_INDEXED16,
88 
89 		DRAW_INDEXEDPOINTLIST32 = DRAW_POINTLIST | DRAW_INDEXED32,
90 		DRAW_INDEXEDLINELIST32  = DRAW_LINELIST  | DRAW_INDEXED32,
91 		DRAW_INDEXEDLINESTRIP32 = DRAW_LINESTRIP | DRAW_INDEXED32,
92 		DRAW_INDEXEDLINELOOP32  = DRAW_LINELOOP  | DRAW_INDEXED32,
93 		DRAW_INDEXEDTRIANGLELIST32  = DRAW_TRIANGLELIST  | DRAW_INDEXED32,
94 		DRAW_INDEXEDTRIANGLESTRIP32 = DRAW_TRIANGLESTRIP | DRAW_INDEXED32,
95 		DRAW_INDEXEDTRIANGLEFAN32   = DRAW_TRIANGLEFAN   | DRAW_INDEXED32,
96 
97 		DRAW_LAST = DRAW_INDEXEDTRIANGLEFAN32
98 	};
99 
100 	enum FillMode : unsigned int
101 	{
102 		FILL_SOLID,
103 		FILL_WIREFRAME,
104 		FILL_VERTEX,
105 
106 		FILL_LAST = FILL_VERTEX
107 	};
108 
109 	enum ShadingMode : unsigned int
110 	{
111 		SHADING_FLAT,
112 		SHADING_GOURAUD,
113 
114 		SHADING_LAST = SHADING_GOURAUD
115 	};
116 
117 	enum DepthCompareMode : unsigned int
118 	{
119 		DEPTH_ALWAYS,
120 		DEPTH_NEVER,
121 		DEPTH_EQUAL,
122 		DEPTH_NOTEQUAL,
123 		DEPTH_LESS,
124 		DEPTH_LESSEQUAL,
125 		DEPTH_GREATER,
126 		DEPTH_GREATEREQUAL,
127 
128 		DEPTH_LAST = DEPTH_GREATEREQUAL
129 	};
130 
131 	enum StencilCompareMode : unsigned int
132 	{
133 		STENCIL_ALWAYS,
134 		STENCIL_NEVER,
135 		STENCIL_EQUAL,
136 		STENCIL_NOTEQUAL,
137 		STENCIL_LESS,
138 		STENCIL_LESSEQUAL,
139 		STENCIL_GREATER,
140 		STENCIL_GREATEREQUAL,
141 
142 		STENCIL_LAST = STENCIL_GREATEREQUAL
143 	};
144 
145 	enum StencilOperation : unsigned int
146 	{
147 		OPERATION_KEEP,
148 		OPERATION_ZERO,
149 		OPERATION_REPLACE,
150 		OPERATION_INCRSAT,
151 		OPERATION_DECRSAT,
152 		OPERATION_INVERT,
153 		OPERATION_INCR,
154 		OPERATION_DECR,
155 
156 		OPERATION_LAST = OPERATION_DECR
157 	};
158 
159 	enum AlphaCompareMode : unsigned int
160 	{
161 		ALPHA_ALWAYS,
162 		ALPHA_NEVER,
163 		ALPHA_EQUAL,
164 		ALPHA_NOTEQUAL,
165 		ALPHA_LESS,
166 		ALPHA_LESSEQUAL,
167 		ALPHA_GREATER,
168 		ALPHA_GREATEREQUAL,
169 
170 		ALPHA_LAST = ALPHA_GREATEREQUAL
171 	};
172 
173 	enum CullMode : unsigned int
174 	{
175 		CULL_NONE,
176 		CULL_CLOCKWISE,
177 		CULL_COUNTERCLOCKWISE,
178 
179 		CULL_LAST = CULL_COUNTERCLOCKWISE
180 	};
181 
182 	enum BlendFactor : unsigned int
183 	{
184 		BLEND_ZERO,
185 		BLEND_ONE,
186 		BLEND_SOURCE,
187 		BLEND_INVSOURCE,
188 		BLEND_DEST,
189 		BLEND_INVDEST,
190 		BLEND_SOURCEALPHA,
191 		BLEND_INVSOURCEALPHA,
192 		BLEND_DESTALPHA,
193 		BLEND_INVDESTALPHA,
194 		BLEND_SRCALPHASAT,
195 		BLEND_CONSTANT,
196 		BLEND_INVCONSTANT,
197 		BLEND_CONSTANTALPHA,
198 		BLEND_INVCONSTANTALPHA,
199 
200 		BLEND_LAST = BLEND_INVCONSTANT
201 	};
202 
203 	enum BlendOperation : unsigned int
204 	{
205 		BLENDOP_ADD,
206 		BLENDOP_SUB,
207 		BLENDOP_INVSUB,
208 		BLENDOP_MIN,
209 		BLENDOP_MAX,
210 
211 		BLENDOP_SOURCE,   // Copy source
212 		BLENDOP_DEST,     // Copy dest
213 		BLENDOP_NULL,     // Nullify result
214 
215 		BLENDOP_LAST = BLENDOP_NULL
216 	};
217 
218 	enum LogicalOperation : unsigned int
219 	{
220 		LOGICALOP_CLEAR,
221 		LOGICALOP_SET,
222 		LOGICALOP_COPY,
223 		LOGICALOP_COPY_INVERTED,
224 		LOGICALOP_NOOP,
225 		LOGICALOP_INVERT,
226 		LOGICALOP_AND,
227 		LOGICALOP_NAND,
228 		LOGICALOP_OR,
229 		LOGICALOP_NOR,
230 		LOGICALOP_XOR,
231 		LOGICALOP_EQUIV,
232 		LOGICALOP_AND_REVERSE,
233 		LOGICALOP_AND_INVERTED,
234 		LOGICALOP_OR_REVERSE,
235 		LOGICALOP_OR_INVERTED,
236 
237 		LOGICALOP_LAST = LOGICALOP_OR_INVERTED
238 	};
239 
240 	enum MaterialSource : unsigned int
241 	{
242 		MATERIAL_MATERIAL,
243 		MATERIAL_COLOR1,
244 		MATERIAL_COLOR2,
245 
246 		MATERIAL_LAST = MATERIAL_COLOR2
247 	};
248 
249 	enum FogMode : unsigned int
250 	{
251 		FOG_NONE,
252 		FOG_LINEAR,
253 		FOG_EXP,
254 		FOG_EXP2,
255 
256 		FOG_LAST = FOG_EXP2
257 	};
258 
259 	enum TexGen : unsigned int
260 	{
261 		TEXGEN_PASSTHRU,
262 		TEXGEN_NORMAL,
263 		TEXGEN_POSITION,
264 		TEXGEN_REFLECTION,
265 		TEXGEN_SPHEREMAP,
266 		TEXGEN_NONE,
267 
268 		TEXGEN_LAST = TEXGEN_NONE
269 	};
270 
271 	enum TransparencyAntialiasing : unsigned int
272 	{
273 		TRANSPARENCY_NONE,
274 		TRANSPARENCY_ALPHA_TO_COVERAGE,
275 
276 		TRANSPARENCY_LAST = TRANSPARENCY_ALPHA_TO_COVERAGE
277 	};
278 
279 	class Context
280 	{
281 	public:
282 		Context();
283 
284 		~Context();
285 
286 		void *operator new(size_t bytes);
287 		void operator delete(void *pointer, size_t bytes);
288 
289 		bool isDrawPoint(bool fillModeAware = false) const;
290 		bool isDrawLine(bool fillModeAware = false) const;
291 		bool isDrawTriangle(bool fillModeAware = false) const;
292 
293 		void init();
294 
295 		const float &exp2Bias();   // NOTE: Needs address for JIT
296 
297 		const Point &getLightPosition(int light);
298 
299 		void setGlobalMipmapBias(float bias);
300 
301 		// Set fixed-function vertex pipeline states
302 		void setLightingEnable(bool lightingEnable);
303 		void setSpecularEnable(bool specularEnable);
304 		void setLightEnable(int light, bool lightEnable);
305 		void setLightPosition(int light, Point worldLightPosition);
306 
307 		void setColorVertexEnable(bool colorVertexEnable);
308 		void setAmbientMaterialSource(MaterialSource ambientMaterialSource);
309 		void setDiffuseMaterialSource(MaterialSource diffuseMaterialSource);
310 		void setSpecularMaterialSource(MaterialSource specularMaterialSource);
311 		void setEmissiveMaterialSource(MaterialSource emissiveMaterialSource);
312 
313 		void setPointSpriteEnable(bool pointSpriteEnable);
314 		void setPointScaleEnable(bool pointScaleEnable);
315 
316 		// Set fixed-function pixel pipeline states, return true when modified
317 		bool setDepthBufferEnable(bool depthBufferEnable);
318 
319 		bool setAlphaBlendEnable(bool alphaBlendEnable);
320 		bool setSourceBlendFactor(BlendFactor sourceBlendFactor);
321 		bool setDestBlendFactor(BlendFactor destBlendFactor);
322 		bool setBlendOperation(BlendOperation blendOperation);
323 
324 		bool setSeparateAlphaBlendEnable(bool separateAlphaBlendEnable);
325 		bool setSourceBlendFactorAlpha(BlendFactor sourceBlendFactorAlpha);
326 		bool setDestBlendFactorAlpha(BlendFactor destBlendFactorAlpha);
327 		bool setBlendOperationAlpha(BlendOperation blendOperationAlpha);
328 
329 		bool setColorWriteMask(int index, int colorWriteMask);
330 		bool setWriteSRGB(bool sRGB);
331 
332 		bool setColorLogicOpEnabled(bool colorLogicOpEnabled);
333 		bool setLogicalOperation(LogicalOperation logicalOperation);
334 
335 		// Active fixed-function pixel pipeline states
336 		bool fogActive();
337 		bool pointSizeActive();
338 		FogMode pixelFogActive();
339 		bool depthWriteActive();
340 		bool alphaTestActive();
341 		bool depthBufferActive();
342 		bool stencilActive();
343 
344 		bool perspectiveActive();
345 
346 		// Active fixed-function vertex pipeline states
347 		bool vertexLightingActive();
348 		bool texCoordActive(int coordinate, int component);
349 		bool texCoordActive(int coordinate);
350 		bool isProjectionComponent(unsigned int coordinate, int component);
351 		bool vertexSpecularInputActive();
352 		bool vertexSpecularActive();
353 		bool vertexNormalActive();
354 		bool vertexLightActive();
355 		bool vertexLightActive(int i);
356 		MaterialSource vertexDiffuseMaterialSourceActive();
357 		MaterialSource vertexSpecularMaterialSourceActive();
358 		MaterialSource vertexAmbientMaterialSourceActive();
359 		MaterialSource vertexEmissiveMaterialSourceActive();
360 
361 		bool pointSpriteActive();
362 		bool pointScaleActive();
363 
364 		bool alphaBlendActive();
365 		BlendFactor sourceBlendFactor();
366 		BlendFactor destBlendFactor();
367 		BlendOperation blendOperation();
368 
369 		BlendFactor sourceBlendFactorAlpha();
370 		BlendFactor destBlendFactorAlpha();
371 		BlendOperation blendOperationAlpha();
372 
373 		LogicalOperation colorLogicOp();
374 		LogicalOperation indexLogicOp();
375 
376 		bool indexedVertexBlendActive();
377 		int vertexBlendMatrixCountActive();
378 		bool localViewerActive();
379 		bool normalizeNormalsActive();
380 		FogMode vertexFogModeActive();
381 		bool rangeFogActive();
382 
383 		TexGen texGenActive(int stage);
384 		int textureTransformCountActive(int stage);
385 		int texCoordIndexActive(int stage);
386 
387 		// Active context states
388 		bool diffuseUsed();     // Used by pixel processor but not provided by vertex processor
389 		bool diffuseUsed(int component);     // Used by pixel processor but not provided by vertex processor
390 		bool diffuseActive();
391 		bool diffuseActive(int component);
392 		bool specularUsed();
393 		bool specularUsed(int component);
394 		bool specularActive();
395 		bool specularActive(int component);
396 		bool colorActive(int color, int component);
397 		bool textureActive();
398 		bool textureActive(int coordinate);
399 		bool textureActive(int coordinate, int component);
400 
401 		unsigned short pixelShaderVersion() const;
402 		unsigned short vertexShaderVersion() const;
403 
404 		int getMultiSampleCount() const;
405 		int getSuperSampleCount() const;
406 
407 		DrawType drawType;
408 
409 		bool stencilEnable;
410 		StencilCompareMode stencilCompareMode;
411 		int stencilReference;
412 		int stencilMask;
413 		StencilOperation stencilFailOperation;
414 		StencilOperation stencilPassOperation;
415 		StencilOperation stencilZFailOperation;
416 		int stencilWriteMask;
417 
418 		bool twoSidedStencil;
419 		StencilCompareMode stencilCompareModeCCW;
420 		int stencilReferenceCCW;
421 		int stencilMaskCCW;
422 		StencilOperation stencilFailOperationCCW;
423 		StencilOperation stencilPassOperationCCW;
424 		StencilOperation stencilZFailOperationCCW;
425 		int stencilWriteMaskCCW;
426 
427 		// Pixel processor states
428 		AlphaCompareMode alphaCompareMode;
429 		bool alphaTestEnable;
430 		FillMode fillMode;
431 		ShadingMode shadingMode;
432 
433 		CullMode cullMode;
434 		float alphaReference;
435 
436 		TextureStage textureStage[8];
437 		Sampler sampler[TOTAL_IMAGE_UNITS];
438 
439 		Format renderTargetInternalFormat(int index);
440 		int colorWriteActive();
441 		int colorWriteActive(int index);
442 		bool colorUsed();
443 
444 		Resource *texture[TOTAL_IMAGE_UNITS];
445 		Stream input[MAX_VERTEX_INPUTS];
446 		Resource *indexBuffer;
447 
448 		bool preTransformed;   // FIXME: Private
449 
450 		float fogStart;
451 		float fogEnd;
452 
453 		void computeIllumination();
454 
455 		bool textureWrapActive;
456 		unsigned char textureWrap[TEXTURE_IMAGE_UNITS];
457 		TexGen texGen[8];
458 		bool localViewer;
459 		bool normalizeNormals;
460 		int textureTransformCount[8];
461 		bool textureTransformProject[8];
462 
463 		Surface *renderTarget[RENDERTARGETS];
464 		Surface *depthBuffer;
465 		Surface *stencilBuffer;
466 
467 		// Fog
468 		bool fogEnable;
469 		FogMode pixelFogMode;
470 		FogMode vertexFogMode;
471 		bool wBasedFog;
472 		bool rangeFogEnable;
473 
474 		// Vertex blending
475 		bool indexedVertexBlendEnable;
476 		int vertexBlendMatrixCount;
477 
478 		// Shaders
479 		const PixelShader *pixelShader;
480 		const VertexShader *vertexShader;
481 
482 		// Global mipmap bias
483 		float bias;
484 
485 		// Instancing
486 		int instanceID;
487 
488 		// Fixed-function vertex pipeline state
489 		bool lightingEnable;
490 		bool specularEnable;
491 		bool lightEnable[8];
492 		Point worldLightPosition[8];
493 
494 		MaterialSource ambientMaterialSource;
495 		MaterialSource diffuseMaterialSource;
496 		MaterialSource specularMaterialSource;
497 		MaterialSource emissiveMaterialSource;
498 		bool colorVertexEnable;
499 
500 		bool occlusionEnabled;
501 		bool transformFeedbackQueryEnabled;
502 		uint64_t transformFeedbackEnabled;
503 
504 		// Pixel processor states
505 		bool rasterizerDiscard;
506 		bool depthBufferEnable;
507 		DepthCompareMode depthCompareMode;
508 		bool depthWriteEnable;
509 
510 		bool alphaBlendEnable;
511 		BlendFactor sourceBlendFactorState;
512 		BlendFactor destBlendFactorState;
513 		BlendOperation blendOperationState;
514 
515 		bool separateAlphaBlendEnable;
516 		BlendFactor sourceBlendFactorStateAlpha;
517 		BlendFactor destBlendFactorStateAlpha;
518 		BlendOperation blendOperationStateAlpha;
519 
520 		bool pointSpriteEnable;
521 		bool pointScaleEnable;
522 		float lineWidth;
523 
524 		int colorWriteMask[RENDERTARGETS];   // RGBA
525 		bool writeSRGB;
526 		unsigned int sampleMask;
527 		unsigned int multiSampleMask;
528 
529 		bool colorLogicOpEnabled;
530 		LogicalOperation logicalOperation;
531 	};
532 }
533 
534 #endif   // sw_Context_hpp
535