swr: add fetch shader cache
[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/crc32.h"
34 #include "api.h"
35 #include "swr_tex_sample.h"
36 #include "swr_shader.h"
37 #include <unordered_map>
38 #include <memory>
39
40 template <typename T>
41 struct ShaderVariant {
42 struct gallivm_state *gallivm;
43 T shader;
44
45 ShaderVariant(struct gallivm_state *gs, T code) : gallivm(gs), shader(code) {}
46 ~ShaderVariant() { gallivm_destroy(gallivm); }
47 };
48
49 typedef ShaderVariant<PFN_VERTEX_FUNC> VariantVS;
50 typedef ShaderVariant<PFN_PIXEL_KERNEL> VariantFS;
51
52 /* skeleton */
53 struct swr_vertex_shader {
54 struct pipe_shader_state pipe;
55 struct lp_tgsi_info info;
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 flatConstantMask;
66 uint32_t pointSpriteMask;
67 std::unordered_map<swr_jit_fs_key, std::unique_ptr<VariantFS>> map;
68 };
69
70 /* Vertex element state */
71 struct swr_vertex_element_state {
72 FETCH_COMPILE_STATE fsState;
73 PFN_FETCH_FUNC fsFunc {NULL};
74 uint32_t stream_pitch[PIPE_MAX_ATTRIBS] {0};
75 uint32_t min_instance_div[PIPE_MAX_ATTRIBS] {0};
76 uint32_t instanced_bufs {0};
77 std::unordered_map<swr_jit_fetch_key, PFN_FETCH_FUNC> map;
78 };
79
80 struct swr_blend_state {
81 struct pipe_blend_state pipe;
82 SWR_BLEND_STATE blendState;
83 RENDER_TARGET_BLEND_COMPILE_STATE compileState[PIPE_MAX_COLOR_BUFS];
84 };
85
86 /*
87 * Derived SWR API DrawState
88 * For convenience of making simple changes without re-deriving state.
89 */
90 struct swr_derived_state {
91 SWR_RASTSTATE rastState;
92 SWR_VIEWPORT vp;
93 SWR_VIEWPORT_MATRICES vpm;
94 };
95
96 void swr_update_derived(struct pipe_context *,
97 const struct pipe_draw_info * = nullptr);
98
99 /*
100 * Conversion functions: Convert mesa state defines to SWR.
101 */
102
103 static INLINE SWR_LOGIC_OP
104 swr_convert_logic_op(const UINT op)
105 {
106 switch (op) {
107 case PIPE_LOGICOP_CLEAR:
108 return LOGICOP_CLEAR;
109 case PIPE_LOGICOP_NOR:
110 return LOGICOP_NOR;
111 case PIPE_LOGICOP_AND_INVERTED:
112 return LOGICOP_AND_INVERTED;
113 case PIPE_LOGICOP_COPY_INVERTED:
114 return LOGICOP_COPY_INVERTED;
115 case PIPE_LOGICOP_AND_REVERSE:
116 return LOGICOP_AND_REVERSE;
117 case PIPE_LOGICOP_INVERT:
118 return LOGICOP_INVERT;
119 case PIPE_LOGICOP_XOR:
120 return LOGICOP_XOR;
121 case PIPE_LOGICOP_NAND:
122 return LOGICOP_NAND;
123 case PIPE_LOGICOP_AND:
124 return LOGICOP_AND;
125 case PIPE_LOGICOP_EQUIV:
126 return LOGICOP_EQUIV;
127 case PIPE_LOGICOP_NOOP:
128 return LOGICOP_NOOP;
129 case PIPE_LOGICOP_OR_INVERTED:
130 return LOGICOP_OR_INVERTED;
131 case PIPE_LOGICOP_COPY:
132 return LOGICOP_COPY;
133 case PIPE_LOGICOP_OR_REVERSE:
134 return LOGICOP_OR_REVERSE;
135 case PIPE_LOGICOP_OR:
136 return LOGICOP_OR;
137 case PIPE_LOGICOP_SET:
138 return LOGICOP_SET;
139 default:
140 assert(0 && "Unsupported logic op");
141 return LOGICOP_NOOP;
142 }
143 }
144
145 static INLINE SWR_STENCILOP
146 swr_convert_stencil_op(const UINT op)
147 {
148 switch (op) {
149 case PIPE_STENCIL_OP_KEEP:
150 return STENCILOP_KEEP;
151 case PIPE_STENCIL_OP_ZERO:
152 return STENCILOP_ZERO;
153 case PIPE_STENCIL_OP_REPLACE:
154 return STENCILOP_REPLACE;
155 case PIPE_STENCIL_OP_INCR:
156 return STENCILOP_INCRSAT;
157 case PIPE_STENCIL_OP_DECR:
158 return STENCILOP_DECRSAT;
159 case PIPE_STENCIL_OP_INCR_WRAP:
160 return STENCILOP_INCR;
161 case PIPE_STENCIL_OP_DECR_WRAP:
162 return STENCILOP_DECR;
163 case PIPE_STENCIL_OP_INVERT:
164 return STENCILOP_INVERT;
165 default:
166 assert(0 && "Unsupported stencil op");
167 return STENCILOP_KEEP;
168 }
169 }
170
171 static INLINE SWR_FORMAT
172 swr_convert_index_type(const UINT index_size)
173 {
174 switch (index_size) {
175 case sizeof(unsigned char):
176 return R8_UINT;
177 case sizeof(unsigned short):
178 return R16_UINT;
179 case sizeof(unsigned int):
180 return R32_UINT;
181 default:
182 assert(0 && "Unsupported index type");
183 return R32_UINT;
184 }
185 }
186
187
188 static INLINE SWR_ZFUNCTION
189 swr_convert_depth_func(const UINT pipe_func)
190 {
191 switch (pipe_func) {
192 case PIPE_FUNC_NEVER:
193 return ZFUNC_NEVER;
194 case PIPE_FUNC_LESS:
195 return ZFUNC_LT;
196 case PIPE_FUNC_EQUAL:
197 return ZFUNC_EQ;
198 case PIPE_FUNC_LEQUAL:
199 return ZFUNC_LE;
200 case PIPE_FUNC_GREATER:
201 return ZFUNC_GT;
202 case PIPE_FUNC_NOTEQUAL:
203 return ZFUNC_NE;
204 case PIPE_FUNC_GEQUAL:
205 return ZFUNC_GE;
206 case PIPE_FUNC_ALWAYS:
207 return ZFUNC_ALWAYS;
208 default:
209 assert(0 && "Unsupported depth func");
210 return ZFUNC_ALWAYS;
211 }
212 }
213
214
215 static INLINE SWR_CULLMODE
216 swr_convert_cull_mode(const UINT cull_face)
217 {
218 switch (cull_face) {
219 case PIPE_FACE_NONE:
220 return SWR_CULLMODE_NONE;
221 case PIPE_FACE_FRONT:
222 return SWR_CULLMODE_FRONT;
223 case PIPE_FACE_BACK:
224 return SWR_CULLMODE_BACK;
225 case PIPE_FACE_FRONT_AND_BACK:
226 return SWR_CULLMODE_BOTH;
227 default:
228 assert(0 && "Invalid cull mode");
229 return SWR_CULLMODE_NONE;
230 }
231 }
232
233 static INLINE SWR_BLEND_OP
234 swr_convert_blend_func(const UINT blend_func)
235 {
236 switch (blend_func) {
237 case PIPE_BLEND_ADD:
238 return BLENDOP_ADD;
239 case PIPE_BLEND_SUBTRACT:
240 return BLENDOP_SUBTRACT;
241 case PIPE_BLEND_REVERSE_SUBTRACT:
242 return BLENDOP_REVSUBTRACT;
243 case PIPE_BLEND_MIN:
244 return BLENDOP_MIN;
245 case PIPE_BLEND_MAX:
246 return BLENDOP_MAX;
247 default:
248 assert(0 && "Invalid blend func");
249 return BLENDOP_ADD;
250 }
251 }
252
253 static INLINE SWR_BLEND_FACTOR
254 swr_convert_blend_factor(const UINT blend_factor)
255 {
256 switch (blend_factor) {
257 case PIPE_BLENDFACTOR_ONE:
258 return BLENDFACTOR_ONE;
259 case PIPE_BLENDFACTOR_SRC_COLOR:
260 return BLENDFACTOR_SRC_COLOR;
261 case PIPE_BLENDFACTOR_SRC_ALPHA:
262 return BLENDFACTOR_SRC_ALPHA;
263 case PIPE_BLENDFACTOR_DST_ALPHA:
264 return BLENDFACTOR_DST_ALPHA;
265 case PIPE_BLENDFACTOR_DST_COLOR:
266 return BLENDFACTOR_DST_COLOR;
267 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
268 return BLENDFACTOR_SRC_ALPHA_SATURATE;
269 case PIPE_BLENDFACTOR_CONST_COLOR:
270 return BLENDFACTOR_CONST_COLOR;
271 case PIPE_BLENDFACTOR_CONST_ALPHA:
272 return BLENDFACTOR_CONST_ALPHA;
273 case PIPE_BLENDFACTOR_SRC1_COLOR:
274 return BLENDFACTOR_SRC1_COLOR;
275 case PIPE_BLENDFACTOR_SRC1_ALPHA:
276 return BLENDFACTOR_SRC1_ALPHA;
277 case PIPE_BLENDFACTOR_ZERO:
278 return BLENDFACTOR_ZERO;
279 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
280 return BLENDFACTOR_INV_SRC_COLOR;
281 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
282 return BLENDFACTOR_INV_SRC_ALPHA;
283 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
284 return BLENDFACTOR_INV_DST_ALPHA;
285 case PIPE_BLENDFACTOR_INV_DST_COLOR:
286 return BLENDFACTOR_INV_DST_COLOR;
287 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
288 return BLENDFACTOR_INV_CONST_COLOR;
289 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
290 return BLENDFACTOR_INV_CONST_ALPHA;
291 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
292 return BLENDFACTOR_INV_SRC1_COLOR;
293 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA:
294 return BLENDFACTOR_INV_SRC1_ALPHA;
295 default:
296 assert(0 && "Invalid blend factor");
297 return BLENDFACTOR_ONE;
298 }
299 }
300
301 static INLINE enum SWR_SURFACE_TYPE
302 swr_convert_target_type(const enum pipe_texture_target target)
303 {
304 switch (target) {
305 case PIPE_BUFFER:
306 return SURFACE_BUFFER;
307 case PIPE_TEXTURE_1D:
308 case PIPE_TEXTURE_1D_ARRAY:
309 return SURFACE_1D;
310 case PIPE_TEXTURE_2D:
311 case PIPE_TEXTURE_2D_ARRAY:
312 case PIPE_TEXTURE_RECT:
313 return SURFACE_2D;
314 case PIPE_TEXTURE_3D:
315 return SURFACE_3D;
316 case PIPE_TEXTURE_CUBE:
317 case PIPE_TEXTURE_CUBE_ARRAY:
318 return SURFACE_CUBE;
319 default:
320 assert(0);
321 return SURFACE_NULL;
322 }
323 }
324 #endif