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