swr: [rasterizer scripts] Knob scripts tweaks
[mesa.git] / src / gallium / drivers / swr / swr_state.h
1 /****************************************************************************
2 * Copyright (C) 2015 Intel Corporation. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 ***************************************************************************/
23
24 #ifndef SWR_STATE_H
25 #define SWR_STATE_H
26
27 #include "pipe/p_defines.h"
28 #include "tgsi/tgsi_scan.h"
29 #include "tgsi/tgsi_parse.h"
30 #include "tgsi/tgsi_dump.h"
31 #include "gallivm/lp_bld_init.h"
32 #include "gallivm/lp_bld_tgsi.h"
33 #include "util/u_hash.h"
34 #include "api.h"
35 #include "swr_tex_sample.h"
36 #include "swr_shader.h"
37 #include <unordered_map>
38
39 template <typename T>
40 struct ShaderVariant {
41 struct gallivm_state *gallivm;
42 T shader;
43
44 ShaderVariant(struct gallivm_state *gs, T code) : gallivm(gs), shader(code) {}
45 ~ShaderVariant() { gallivm_destroy(gallivm); }
46 };
47
48 typedef ShaderVariant<PFN_VERTEX_FUNC> VariantVS;
49 typedef ShaderVariant<PFN_PIXEL_KERNEL> VariantFS;
50
51 /* skeleton */
52 struct swr_vertex_shader {
53 struct pipe_shader_state pipe;
54 struct lp_tgsi_info info;
55 unsigned linkageMask;
56 std::unordered_map<swr_jit_vs_key, std::unique_ptr<VariantVS>> map;
57 SWR_STREAMOUT_STATE soState;
58 PFN_SO_FUNC soFunc[PIPE_PRIM_MAX] {0};
59 };
60
61 struct swr_fragment_shader {
62 struct pipe_shader_state pipe;
63 struct lp_tgsi_info info;
64 uint32_t constantMask;
65 uint32_t pointSpriteMask;
66 std::unordered_map<swr_jit_fs_key, std::unique_ptr<VariantFS>> map;
67 };
68
69 /* Vertex element state */
70 struct swr_vertex_element_state {
71 FETCH_COMPILE_STATE fsState;
72 PFN_FETCH_FUNC fsFunc;
73 uint32_t stream_pitch[PIPE_MAX_ATTRIBS];
74 };
75
76 struct swr_blend_state {
77 struct pipe_blend_state pipe;
78 SWR_BLEND_STATE blendState;
79 RENDER_TARGET_BLEND_COMPILE_STATE compileState[PIPE_MAX_COLOR_BUFS];
80 };
81
82 /*
83 * Derived SWR API DrawState
84 * For convenience of making simple changes without re-deriving state.
85 */
86 struct swr_derived_state {
87 SWR_RASTSTATE rastState;
88 SWR_VIEWPORT vp;
89 SWR_VIEWPORT_MATRIX vpm;
90 };
91
92 void swr_update_derived(struct pipe_context *,
93 const struct pipe_draw_info * = nullptr);
94
95 /*
96 * Conversion functions: Convert mesa state defines to SWR.
97 */
98
99 static INLINE SWR_LOGIC_OP
100 swr_convert_logic_op(const UINT op)
101 {
102 switch (op) {
103 case PIPE_LOGICOP_CLEAR:
104 return LOGICOP_CLEAR;
105 case PIPE_LOGICOP_NOR:
106 return LOGICOP_NOR;
107 case PIPE_LOGICOP_AND_INVERTED:
108 return LOGICOP_CLEAR;
109 case PIPE_LOGICOP_COPY_INVERTED:
110 return LOGICOP_COPY_INVERTED;
111 case PIPE_LOGICOP_AND_REVERSE:
112 return LOGICOP_AND_REVERSE;
113 case PIPE_LOGICOP_INVERT:
114 return LOGICOP_INVERT;
115 case PIPE_LOGICOP_XOR:
116 return LOGICOP_XOR;
117 case PIPE_LOGICOP_NAND:
118 return LOGICOP_NAND;
119 case PIPE_LOGICOP_AND:
120 return LOGICOP_AND;
121 case PIPE_LOGICOP_EQUIV:
122 return LOGICOP_EQUIV;
123 case PIPE_LOGICOP_NOOP:
124 return LOGICOP_NOOP;
125 case PIPE_LOGICOP_OR_INVERTED:
126 return LOGICOP_OR_INVERTED;
127 case PIPE_LOGICOP_COPY:
128 return LOGICOP_COPY;
129 case PIPE_LOGICOP_OR_REVERSE:
130 return LOGICOP_OR_REVERSE;
131 case PIPE_LOGICOP_OR:
132 return LOGICOP_OR;
133 case PIPE_LOGICOP_SET:
134 return LOGICOP_SET;
135 default:
136 assert(0 && "Unsupported logic op");
137 return LOGICOP_NOOP;
138 }
139 }
140
141 static INLINE SWR_STENCILOP
142 swr_convert_stencil_op(const UINT op)
143 {
144 switch (op) {
145 case PIPE_STENCIL_OP_KEEP:
146 return STENCILOP_KEEP;
147 case PIPE_STENCIL_OP_ZERO:
148 return STENCILOP_ZERO;
149 case PIPE_STENCIL_OP_REPLACE:
150 return STENCILOP_REPLACE;
151 case PIPE_STENCIL_OP_INCR:
152 return STENCILOP_INCRSAT;
153 case PIPE_STENCIL_OP_DECR:
154 return STENCILOP_DECRSAT;
155 case PIPE_STENCIL_OP_INCR_WRAP:
156 return STENCILOP_INCR;
157 case PIPE_STENCIL_OP_DECR_WRAP:
158 return STENCILOP_DECR;
159 case PIPE_STENCIL_OP_INVERT:
160 return STENCILOP_INVERT;
161 default:
162 assert(0 && "Unsupported stencil op");
163 return STENCILOP_KEEP;
164 }
165 }
166
167 static INLINE SWR_FORMAT
168 swr_convert_index_type(const UINT index_size)
169 {
170 switch (index_size) {
171 case sizeof(unsigned char):
172 return R8_UINT;
173 case sizeof(unsigned short):
174 return R16_UINT;
175 case sizeof(unsigned int):
176 return R32_UINT;
177 default:
178 assert(0 && "Unsupported index type");
179 return R32_UINT;
180 }
181 }
182
183
184 static INLINE SWR_ZFUNCTION
185 swr_convert_depth_func(const UINT pipe_func)
186 {
187 switch (pipe_func) {
188 case PIPE_FUNC_NEVER:
189 return ZFUNC_NEVER;
190 case PIPE_FUNC_LESS:
191 return ZFUNC_LT;
192 case PIPE_FUNC_EQUAL:
193 return ZFUNC_EQ;
194 case PIPE_FUNC_LEQUAL:
195 return ZFUNC_LE;
196 case PIPE_FUNC_GREATER:
197 return ZFUNC_GT;
198 case PIPE_FUNC_NOTEQUAL:
199 return ZFUNC_NE;
200 case PIPE_FUNC_GEQUAL:
201 return ZFUNC_GE;
202 case PIPE_FUNC_ALWAYS:
203 return ZFUNC_ALWAYS;
204 default:
205 assert(0 && "Unsupported depth func");
206 return ZFUNC_ALWAYS;
207 }
208 }
209
210
211 static INLINE SWR_CULLMODE
212 swr_convert_cull_mode(const UINT cull_face)
213 {
214 switch (cull_face) {
215 case PIPE_FACE_NONE:
216 return SWR_CULLMODE_NONE;
217 case PIPE_FACE_FRONT:
218 return SWR_CULLMODE_FRONT;
219 case PIPE_FACE_BACK:
220 return SWR_CULLMODE_BACK;
221 case PIPE_FACE_FRONT_AND_BACK:
222 return SWR_CULLMODE_BOTH;
223 default:
224 assert(0 && "Invalid cull mode");
225 return SWR_CULLMODE_NONE;
226 }
227 }
228
229 static INLINE SWR_BLEND_OP
230 swr_convert_blend_func(const UINT blend_func)
231 {
232 switch (blend_func) {
233 case PIPE_BLEND_ADD:
234 return BLENDOP_ADD;
235 case PIPE_BLEND_SUBTRACT:
236 return BLENDOP_SUBTRACT;
237 case PIPE_BLEND_REVERSE_SUBTRACT:
238 return BLENDOP_REVSUBTRACT;
239 case PIPE_BLEND_MIN:
240 return BLENDOP_MIN;
241 case PIPE_BLEND_MAX:
242 return BLENDOP_MAX;
243 default:
244 assert(0 && "Invalid blend func");
245 return BLENDOP_ADD;
246 }
247 }
248
249 static INLINE SWR_BLEND_FACTOR
250 swr_convert_blend_factor(const UINT blend_factor)
251 {
252 switch (blend_factor) {
253 case PIPE_BLENDFACTOR_ONE:
254 return BLENDFACTOR_ONE;
255 case PIPE_BLENDFACTOR_SRC_COLOR:
256 return BLENDFACTOR_SRC_COLOR;
257 case PIPE_BLENDFACTOR_SRC_ALPHA:
258 return BLENDFACTOR_SRC_ALPHA;
259 case PIPE_BLENDFACTOR_DST_ALPHA:
260 return BLENDFACTOR_DST_ALPHA;
261 case PIPE_BLENDFACTOR_DST_COLOR:
262 return BLENDFACTOR_DST_COLOR;
263 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
264 return BLENDFACTOR_SRC_ALPHA_SATURATE;
265 case PIPE_BLENDFACTOR_CONST_COLOR:
266 return BLENDFACTOR_CONST_COLOR;
267 case PIPE_BLENDFACTOR_CONST_ALPHA:
268 return BLENDFACTOR_CONST_ALPHA;
269 case PIPE_BLENDFACTOR_SRC1_COLOR:
270 return BLENDFACTOR_SRC1_COLOR;
271 case PIPE_BLENDFACTOR_SRC1_ALPHA:
272 return BLENDFACTOR_SRC1_ALPHA;
273 case PIPE_BLENDFACTOR_ZERO:
274 return BLENDFACTOR_ZERO;
275 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
276 return BLENDFACTOR_INV_SRC_COLOR;
277 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
278 return BLENDFACTOR_INV_SRC_ALPHA;
279 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
280 return BLENDFACTOR_INV_DST_ALPHA;
281 case PIPE_BLENDFACTOR_INV_DST_COLOR:
282 return BLENDFACTOR_INV_DST_COLOR;
283 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
284 return BLENDFACTOR_INV_CONST_COLOR;
285 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
286 return BLENDFACTOR_INV_CONST_ALPHA;
287 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
288 return BLENDFACTOR_INV_SRC1_COLOR;
289 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
290 return BLENDFACTOR_INV_SRC1_ALPHA;
291 default:
292 assert(0 && "Invalid blend factor");
293 return BLENDFACTOR_ONE;
294 }
295 }
296
297 static INLINE enum SWR_SURFACE_TYPE
298 swr_convert_target_type(const enum pipe_texture_target target)
299 {
300 switch (target) {
301 case PIPE_BUFFER:
302 return SURFACE_BUFFER;
303 case PIPE_TEXTURE_1D:
304 case PIPE_TEXTURE_1D_ARRAY:
305 return SURFACE_1D;
306 case PIPE_TEXTURE_2D:
307 case PIPE_TEXTURE_2D_ARRAY:
308 case PIPE_TEXTURE_RECT:
309 return SURFACE_2D;
310 case PIPE_TEXTURE_3D:
311 return SURFACE_3D;
312 case PIPE_TEXTURE_CUBE:
313 case PIPE_TEXTURE_CUBE_ARRAY:
314 return SURFACE_CUBE;
315 default:
316 assert(0);
317 return SURFACE_NULL;
318 }
319 }
320 #endif