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 "Stream.hpp"
20 #include "Point.hpp"
21 #include "Vertex.hpp"
22 #include "System/Types.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 ENUM_UNDERLYING_TYPE_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_TRIANGLELIST  = 0x03,
63 		DRAW_TRIANGLESTRIP = 0x04,
64 		DRAW_TRIANGLEFAN   = 0x05,
65 
66 		DRAW_NONINDEXED = 0x00,
67 		DRAW_INDEXED16  = 0x20,
68 		DRAW_INDEXED32  = 0x30,
69 
70 		DRAW_INDEXEDPOINTLIST16 = DRAW_POINTLIST | DRAW_INDEXED16,
71 		DRAW_INDEXEDLINELIST16  = DRAW_LINELIST  | DRAW_INDEXED16,
72 		DRAW_INDEXEDLINESTRIP16 = DRAW_LINESTRIP | DRAW_INDEXED16,
73 		DRAW_INDEXEDTRIANGLELIST16  = DRAW_TRIANGLELIST  | DRAW_INDEXED16,
74 		DRAW_INDEXEDTRIANGLESTRIP16 = DRAW_TRIANGLESTRIP | DRAW_INDEXED16,
75 		DRAW_INDEXEDTRIANGLEFAN16   = DRAW_TRIANGLEFAN   | DRAW_INDEXED16,
76 
77 		DRAW_INDEXEDPOINTLIST32 = DRAW_POINTLIST | DRAW_INDEXED32,
78 		DRAW_INDEXEDLINELIST32  = DRAW_LINELIST  | DRAW_INDEXED32,
79 		DRAW_INDEXEDLINESTRIP32 = DRAW_LINESTRIP | DRAW_INDEXED32,
80 		DRAW_INDEXEDTRIANGLELIST32  = DRAW_TRIANGLELIST  | DRAW_INDEXED32,
81 		DRAW_INDEXEDTRIANGLESTRIP32 = DRAW_TRIANGLESTRIP | DRAW_INDEXED32,
82 		DRAW_INDEXEDTRIANGLEFAN32   = DRAW_TRIANGLEFAN   | DRAW_INDEXED32,
83 
84 		DRAW_LAST = DRAW_INDEXEDTRIANGLEFAN32
85 	};
86 
87 	enum CullMode ENUM_UNDERLYING_TYPE_UNSIGNED_INT
88 	{
89 		CULL_NONE,
90 		CULL_CLOCKWISE,
91 		CULL_COUNTERCLOCKWISE,
92 
93 		CULL_LAST = CULL_COUNTERCLOCKWISE
94 	};
95 
96 	enum TransparencyAntialiasing ENUM_UNDERLYING_TYPE_UNSIGNED_INT
97 	{
98 		TRANSPARENCY_NONE,
99 		TRANSPARENCY_ALPHA_TO_COVERAGE,
100 
101 		TRANSPARENCY_LAST = TRANSPARENCY_ALPHA_TO_COVERAGE
102 	};
103 
104 	class Context
105 	{
106 	public:
107 		Context();
108 
109 		~Context();
110 
111 		void *operator new(size_t bytes);
112 		void operator delete(void *pointer, size_t bytes);
113 
114 		void init();
115 
116 		bool isDrawPoint() const;
117 		bool isDrawLine() const;
118 		bool isDrawTriangle() const;
119 
120 		bool setDepthBufferEnable(bool depthBufferEnable);
121 
122 		bool setAlphaBlendEnable(bool alphaBlendEnable);
123 		bool setSourceBlendFactor(VkBlendFactor sourceBlendFactor);
124 		bool setDestBlendFactor(VkBlendFactor destBlendFactor);
125 		bool setBlendOperation(VkBlendOp blendOperation);
126 
127 		bool setSeparateAlphaBlendEnable(bool separateAlphaBlendEnable);
128 		bool setSourceBlendFactorAlpha(VkBlendFactor sourceBlendFactorAlpha);
129 		bool setDestBlendFactorAlpha(VkBlendFactor destBlendFactorAlpha);
130 		bool setBlendOperationAlpha(VkBlendOp blendOperationAlpha);
131 
132 		bool setColorWriteMask(int index, int colorWriteMask);
133 		bool setWriteSRGB(bool sRGB);
134 
135 		bool setColorLogicOpEnabled(bool colorLogicOpEnabled);
136 		bool setLogicalOperation(VkLogicOp logicalOperation);
137 
138 		bool depthWriteActive();
139 		bool alphaTestActive();
140 		bool depthBufferActive();
141 		bool stencilActive();
142 
143 		bool perspectiveActive();
144 
145 		bool alphaBlendActive();
146 		VkBlendFactor sourceBlendFactor();
147 		VkBlendFactor destBlendFactor();
148 		VkBlendOp blendOperation();
149 
150 		VkBlendFactor sourceBlendFactorAlpha();
151 		VkBlendFactor destBlendFactorAlpha();
152 		VkBlendOp blendOperationAlpha();
153 
154 		VkLogicOp colorLogicOp();
155 
156 		unsigned short pixelShaderModel() const;
157 		unsigned short vertexShaderModel() const;
158 
159 		int getMultiSampleCount() const;
160 
161 		DrawType drawType;
162 
163 		bool stencilEnable;
164 		VkCompareOp stencilCompareMode;
165 		int stencilReference;
166 		int stencilMask;
167 		VkStencilOp stencilFailOperation;
168 		VkStencilOp stencilPassOperation;
169 		VkStencilOp stencilZFailOperation;
170 		int stencilWriteMask;
171 
172 		bool twoSidedStencil;
173 		VkCompareOp stencilCompareModeCCW;
174 		int stencilReferenceCCW;
175 		int stencilMaskCCW;
176 		VkStencilOp stencilFailOperationCCW;
177 		VkStencilOp stencilPassOperationCCW;
178 		VkStencilOp stencilZFailOperationCCW;
179 		int stencilWriteMaskCCW;
180 
181 		// Pixel processor states
182 		VkCompareOp alphaCompareMode;
183 		bool alphaTestEnable;
184 
185 		CullMode cullMode;
186 		bool frontFacingCCW;
187 		float alphaReference;
188 
189 		float depthBias;
190 		float slopeDepthBias;
191 
192 		Sampler sampler[TOTAL_IMAGE_UNITS];
193 
194 		VkFormat renderTargetInternalFormat(int index);
195 		int colorWriteActive();
196 		int colorWriteActive(int index);
197 		bool colorUsed();
198 
199 		Resource *texture[TOTAL_IMAGE_UNITS];
200 		Stream input[MAX_VERTEX_INPUTS];
201 		Resource *indexBuffer;
202 
203 		Surface *renderTarget[RENDERTARGETS];
204 		unsigned int renderTargetLayer[RENDERTARGETS];
205 		Surface *depthBuffer;
206 		unsigned int depthBufferLayer;
207 		Surface *stencilBuffer;
208 		unsigned int stencilBufferLayer;
209 
210 		// Shaders
211 		const PixelShader *pixelShader;
212 		const VertexShader *vertexShader;
213 
214 		// Instancing
215 		int instanceID;
216 
217 		bool occlusionEnabled;
218 		bool transformFeedbackQueryEnabled;
219 		uint64_t transformFeedbackEnabled;
220 
221 		// Pixel processor states
222 		bool rasterizerDiscard;
223 		bool depthBufferEnable;
224 		VkCompareOp depthCompareMode;
225 		bool depthWriteEnable;
226 
227 		bool alphaBlendEnable;
228 		VkBlendFactor sourceBlendFactorState;
229 		VkBlendFactor destBlendFactorState;
230 		VkBlendOp blendOperationState;
231 
232 		bool separateAlphaBlendEnable;
233 		VkBlendFactor sourceBlendFactorStateAlpha;
234 		VkBlendFactor destBlendFactorStateAlpha;
235 		VkBlendOp blendOperationStateAlpha;
236 
237 		float lineWidth;
238 
239 		int colorWriteMask[RENDERTARGETS];   // RGBA
240 		bool writeSRGB;
241 		unsigned int sampleMask;
242 		unsigned int multiSampleMask;
243 
244 		bool colorLogicOpEnabled;
245 		VkLogicOp logicalOperation;
246 	};
247 }
248 
249 #endif   // sw_Context_hpp
250