llvmpipe: Reuse llvmpipes LLVMContext in the draw context.
[mesa.git] / src / gallium / auxiliary / draw / draw_llvm.h
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #ifndef DRAW_LLVM_H
29 #define DRAW_LLVM_H
30
31 #include "draw/draw_private.h"
32
33 #include "draw/draw_vs.h"
34 #include "draw/draw_gs.h"
35
36 #include "gallivm/lp_bld_sample.h"
37 #include "gallivm/lp_bld_limits.h"
38
39 #include "pipe/p_context.h"
40 #include "util/u_simple_list.h"
41
42
43 struct draw_llvm;
44 struct llvm_vertex_shader;
45 struct llvm_geometry_shader;
46
47 struct draw_jit_texture
48 {
49 uint32_t width;
50 uint32_t height;
51 uint32_t depth;
52 uint32_t first_level;
53 uint32_t last_level;
54 const void *base;
55 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
56 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
57 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS];
58 };
59
60
61 struct draw_sampler_static_state
62 {
63 /*
64 * These attributes are effectively interleaved for more sane key handling.
65 * However, there might be lots of null space if the amount of samplers and
66 * textures isn't the same.
67 */
68 struct lp_static_sampler_state sampler_state;
69 struct lp_static_texture_state texture_state;
70 };
71
72
73 struct draw_jit_sampler
74 {
75 float min_lod;
76 float max_lod;
77 float lod_bias;
78 float border_color[4];
79 };
80
81
82 enum {
83 DRAW_JIT_TEXTURE_WIDTH = 0,
84 DRAW_JIT_TEXTURE_HEIGHT,
85 DRAW_JIT_TEXTURE_DEPTH,
86 DRAW_JIT_TEXTURE_FIRST_LEVEL,
87 DRAW_JIT_TEXTURE_LAST_LEVEL,
88 DRAW_JIT_TEXTURE_BASE,
89 DRAW_JIT_TEXTURE_ROW_STRIDE,
90 DRAW_JIT_TEXTURE_IMG_STRIDE,
91 DRAW_JIT_TEXTURE_MIP_OFFSETS,
92 DRAW_JIT_TEXTURE_NUM_FIELDS /* number of fields above */
93 };
94
95
96 enum {
97 DRAW_JIT_SAMPLER_MIN_LOD,
98 DRAW_JIT_SAMPLER_MAX_LOD,
99 DRAW_JIT_SAMPLER_LOD_BIAS,
100 DRAW_JIT_SAMPLER_BORDER_COLOR,
101 DRAW_JIT_SAMPLER_NUM_FIELDS /* number of fields above */
102 };
103
104
105 enum {
106 DRAW_JIT_VERTEX_VERTEX_ID = 0,
107 DRAW_JIT_VERTEX_CLIP,
108 DRAW_JIT_VERTEX_PRE_CLIP_POS,
109 DRAW_JIT_VERTEX_DATA
110 };
111
112 /**
113 * This structure is passed directly to the generated vertex shader.
114 *
115 * It contains the derived state.
116 *
117 * Changes here must be reflected in the draw_jit_context_* macros.
118 * Changes to the ordering should be avoided.
119 *
120 * Only use types with a clear size and padding here, in particular prefer the
121 * stdint.h types to the basic integer types.
122 */
123 struct draw_jit_context
124 {
125 const float *vs_constants[LP_MAX_TGSI_CONST_BUFFERS];
126 int num_vs_constants[LP_MAX_TGSI_CONST_BUFFERS];
127 float (*planes) [DRAW_TOTAL_CLIP_PLANES][4];
128 float *viewport;
129
130 struct draw_jit_texture textures[PIPE_MAX_SHADER_SAMPLER_VIEWS];
131 struct draw_jit_sampler samplers[PIPE_MAX_SAMPLERS];
132 };
133
134 enum {
135 DRAW_JIT_CTX_CONSTANTS = 0,
136 DRAW_JIT_CTX_NUM_CONSTANTS = 1,
137 DRAW_JIT_CTX_PLANES = 2,
138 DRAW_JIT_CTX_VIEWPORT = 3,
139 DRAW_JIT_CTX_TEXTURES = 4,
140 DRAW_JIT_CTX_SAMPLERS = 5,
141 DRAW_JIT_CTX_NUM_FIELDS
142 };
143
144 #define draw_jit_context_vs_constants(_gallivm, _ptr) \
145 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_CONSTANTS, "vs_constants")
146
147 #define draw_jit_context_num_vs_constants(_gallivm, _ptr) \
148 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_NUM_CONSTANTS, "num_vs_constants")
149
150 #define draw_jit_context_planes(_gallivm, _ptr) \
151 lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_CTX_PLANES, "planes")
152
153 #define draw_jit_context_viewport(_gallivm, _ptr) \
154 lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_CTX_VIEWPORT, "viewport")
155
156 #define draw_jit_context_textures(_gallivm, _ptr) \
157 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_TEXTURES, "textures")
158
159 #define draw_jit_context_samplers(_gallivm, _ptr) \
160 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_SAMPLERS, "samplers")
161
162 #define draw_jit_header_id(_gallivm, _ptr) \
163 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_VERTEX_ID, "id")
164
165 #define draw_jit_header_clip(_gallivm, _ptr) \
166 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_CLIP, "clip")
167
168 #define draw_jit_header_pre_clip_pos(_gallivm, _ptr) \
169 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_PRE_CLIP_POS, "pre_clip_pos")
170
171 #define draw_jit_header_data(_gallivm, _ptr) \
172 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_DATA, "data")
173
174
175 #define draw_jit_vbuffer_stride(_gallivm, _ptr) \
176 lp_build_struct_get(_gallivm, _ptr, 0, "stride")
177
178 #define draw_jit_vbuffer_offset(_gallivm, _ptr) \
179 lp_build_struct_get(_gallivm, _ptr, 1, "buffer_offset")
180
181 enum {
182 DRAW_JIT_DVBUFFER_MAP = 0,
183 DRAW_JIT_DVBUFFER_SIZE,
184 DRAW_JIT_DVBUFFER_NUM_FIELDS /* number of fields above */
185 };
186
187 #define draw_jit_dvbuffer_map(_gallivm, _ptr) \
188 lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_DVBUFFER_MAP, "map")
189
190 #define draw_jit_dvbuffer_size(_gallivm, _ptr) \
191 lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_DVBUFFER_SIZE, "size")
192
193
194 /**
195 * This structure is passed directly to the generated geometry shader.
196 *
197 * It contains the derived state.
198 *
199 * Changes here must be reflected in the draw_gs_jit_context_* macros.
200 * Changes to the ordering should be avoided.
201 *
202 * Only use types with a clear size and padding here, in particular prefer the
203 * stdint.h types to the basic integer types.
204 */
205 struct draw_gs_jit_context
206 {
207 const float *constants[LP_MAX_TGSI_CONST_BUFFERS];
208 int num_constants[LP_MAX_TGSI_CONST_BUFFERS];
209 float (*planes) [DRAW_TOTAL_CLIP_PLANES][4];
210 float *viewport;
211
212 /* There two need to be exactly at DRAW_JIT_CTX_TEXTURES and
213 * DRAW_JIT_CTX_SAMPLERS positions in the struct */
214 struct draw_jit_texture textures[PIPE_MAX_SHADER_SAMPLER_VIEWS];
215 struct draw_jit_sampler samplers[PIPE_MAX_SAMPLERS];
216
217 int **prim_lengths;
218 int *emitted_vertices;
219 int *emitted_prims;
220 };
221
222 enum {
223 DRAW_GS_JIT_CTX_CONSTANTS = 0,
224 DRAW_GS_JIT_CTX_NUM_CONSTANTS = 1,
225 DRAW_GS_JIT_CTX_PLANES = 2,
226 DRAW_GS_JIT_CTX_VIEWPORT = 3,
227 /* Textures and samples are reserved for DRAW_JIT_CTX_TEXTURES
228 * and DRAW_JIT_CTX_SAMPLERS, because they both need
229 * to be at exactly the same locations as they are in the
230 * VS ctx structure for sampling to work. */
231 DRAW_GS_JIT_CTX_TEXTURES = DRAW_JIT_CTX_TEXTURES,
232 DRAW_GS_JIT_CTX_SAMPLERS = DRAW_JIT_CTX_SAMPLERS,
233 DRAW_GS_JIT_CTX_PRIM_LENGTHS = 6,
234 DRAW_GS_JIT_CTX_EMITTED_VERTICES = 7,
235 DRAW_GS_JIT_CTX_EMITTED_PRIMS = 8,
236 DRAW_GS_JIT_CTX_NUM_FIELDS = 9
237 };
238
239 #define draw_gs_jit_context_constants(_gallivm, _ptr) \
240 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_CONSTANTS, "constants")
241
242 #define draw_gs_jit_context_num_constants(_gallivm, _ptr) \
243 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_NUM_CONSTANTS, "num_constants")
244
245 #define draw_gs_jit_context_planes(_gallivm, _ptr) \
246 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_PLANES, "planes")
247
248 #define draw_gs_jit_context_viewport(_gallivm, _ptr) \
249 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_VIEWPORT, "viewport")
250
251 #define draw_gs_jit_context_textures(_gallivm, _ptr) \
252 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_TEXTURES, "textures")
253
254 #define draw_gs_jit_context_samplers(_gallivm, _ptr) \
255 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_SAMPLERS, "samplers")
256
257 #define draw_gs_jit_prim_lengths(_gallivm, _ptr) \
258 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_PRIM_LENGTHS, "prim_lengths")
259
260 #define draw_gs_jit_emitted_vertices(_gallivm, _ptr) \
261 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_EMITTED_VERTICES, "emitted_vertices")
262
263 #define draw_gs_jit_emitted_prims(_gallivm, _ptr) \
264 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_EMITTED_PRIMS, "emitted_prims")
265
266
267
268 typedef int
269 (*draw_jit_vert_func)(struct draw_jit_context *context,
270 struct vertex_header *io,
271 const struct draw_vertex_buffer vbuffers[PIPE_MAX_ATTRIBS],
272 unsigned start,
273 unsigned count,
274 unsigned stride,
275 struct pipe_vertex_buffer *vertex_buffers,
276 unsigned instance_id,
277 unsigned vertex_id_offset,
278 unsigned start_instance);
279
280
281 typedef int
282 (*draw_jit_vert_func_elts)(struct draw_jit_context *context,
283 struct vertex_header *io,
284 const struct draw_vertex_buffer vbuffers[PIPE_MAX_ATTRIBS],
285 const unsigned *fetch_elts,
286 unsigned fetch_max_elt,
287 unsigned fetch_count,
288 unsigned stride,
289 struct pipe_vertex_buffer *vertex_buffers,
290 unsigned instance_id,
291 unsigned vertex_id_offset,
292 unsigned start_instance);
293
294
295 typedef int
296 (*draw_gs_jit_func)(struct draw_gs_jit_context *context,
297 float inputs[6][PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS][TGSI_NUM_CHANNELS],
298 struct vertex_header *output,
299 unsigned num_prims,
300 unsigned instance_id,
301 int *prim_ids);
302
303 struct draw_llvm_variant_key
304 {
305 unsigned nr_vertex_elements:8;
306 unsigned nr_samplers:8;
307 unsigned nr_sampler_views:8;
308 unsigned clamp_vertex_color:1;
309 unsigned clip_xy:1;
310 unsigned clip_z:1;
311 unsigned clip_user:1;
312 unsigned clip_halfz:1;
313 unsigned bypass_viewport:1;
314 unsigned need_edgeflags:1;
315 unsigned has_gs:1;
316 unsigned num_outputs:8;
317 /*
318 * it is important there are no holes in this struct
319 * (and all padding gets zeroed).
320 */
321 unsigned ucp_enable:PIPE_MAX_CLIP_PLANES;
322 unsigned pad1:24-PIPE_MAX_CLIP_PLANES;
323
324 /* Variable number of vertex elements:
325 */
326 struct pipe_vertex_element vertex_element[1];
327
328 /* Followed by variable number of samplers:
329 */
330 /* struct draw_sampler_static_state sampler; */
331 };
332
333 struct draw_gs_llvm_variant_key
334 {
335 unsigned nr_samplers:8;
336 unsigned nr_sampler_views:8;
337 unsigned num_outputs:8;
338
339 struct draw_sampler_static_state samplers[1];
340 };
341
342 #define DRAW_LLVM_MAX_VARIANT_KEY_SIZE \
343 (sizeof(struct draw_llvm_variant_key) + \
344 PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state) + \
345 (PIPE_MAX_ATTRIBS-1) * sizeof(struct pipe_vertex_element))
346
347 #define DRAW_GS_LLVM_MAX_VARIANT_KEY_SIZE \
348 (sizeof(struct draw_gs_llvm_variant_key) + \
349 PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state))
350
351
352 static INLINE size_t
353 draw_llvm_variant_key_size(unsigned nr_vertex_elements,
354 unsigned nr_samplers)
355 {
356 return (sizeof(struct draw_llvm_variant_key) +
357 nr_samplers * sizeof(struct draw_sampler_static_state) +
358 (nr_vertex_elements - 1) * sizeof(struct pipe_vertex_element));
359 }
360
361
362 static INLINE size_t
363 draw_gs_llvm_variant_key_size(unsigned nr_samplers)
364 {
365 return (sizeof(struct draw_gs_llvm_variant_key) +
366 (nr_samplers - 1) * sizeof(struct draw_sampler_static_state));
367 }
368
369
370 static INLINE struct draw_sampler_static_state *
371 draw_llvm_variant_key_samplers(struct draw_llvm_variant_key *key)
372 {
373 return (struct draw_sampler_static_state *)
374 &key->vertex_element[key->nr_vertex_elements];
375 }
376
377
378 struct draw_llvm_variant_list_item
379 {
380 struct draw_llvm_variant *base;
381 struct draw_llvm_variant_list_item *next, *prev;
382 };
383
384 struct draw_gs_llvm_variant_list_item
385 {
386 struct draw_gs_llvm_variant *base;
387 struct draw_gs_llvm_variant_list_item *next, *prev;
388 };
389
390
391 struct draw_llvm_variant
392 {
393 struct gallivm_state *gallivm;
394
395 /* LLVM JIT builder types */
396 LLVMTypeRef context_ptr_type;
397 LLVMTypeRef buffer_ptr_type;
398 LLVMTypeRef vb_ptr_type;
399 LLVMTypeRef vertex_header_ptr_type;
400
401 LLVMValueRef function;
402 LLVMValueRef function_elts;
403 draw_jit_vert_func jit_func;
404 draw_jit_vert_func_elts jit_func_elts;
405
406 struct llvm_vertex_shader *shader;
407
408 struct draw_llvm *llvm;
409 struct draw_llvm_variant_list_item list_item_global;
410 struct draw_llvm_variant_list_item list_item_local;
411
412 /* key is variable-sized, must be last */
413 struct draw_llvm_variant_key key;
414 };
415
416
417 struct draw_gs_llvm_variant
418 {
419 struct gallivm_state *gallivm;
420
421 /* LLVM JIT builder types */
422 LLVMTypeRef context_ptr_type;
423 LLVMTypeRef vertex_header_ptr_type;
424 LLVMTypeRef input_array_type;
425
426 LLVMValueRef context_ptr;
427 LLVMValueRef io_ptr;
428 LLVMValueRef num_prims;
429 LLVMValueRef function;
430 draw_gs_jit_func jit_func;
431
432 struct llvm_geometry_shader *shader;
433
434 struct draw_llvm *llvm;
435 struct draw_gs_llvm_variant_list_item list_item_global;
436 struct draw_gs_llvm_variant_list_item list_item_local;
437
438 /* key is variable-sized, must be last */
439 struct draw_gs_llvm_variant_key key;
440 };
441
442 struct llvm_vertex_shader {
443 struct draw_vertex_shader base;
444
445 unsigned variant_key_size;
446 struct draw_llvm_variant_list_item variants;
447 unsigned variants_created;
448 unsigned variants_cached;
449 };
450
451 struct llvm_geometry_shader {
452 struct draw_geometry_shader base;
453
454 unsigned variant_key_size;
455 struct draw_gs_llvm_variant_list_item variants;
456 unsigned variants_created;
457 unsigned variants_cached;
458 };
459
460
461 struct draw_llvm {
462 struct draw_context *draw;
463
464 LLVMContextRef context;
465 boolean context_owned;
466
467 struct draw_jit_context jit_context;
468 struct draw_gs_jit_context gs_jit_context;
469
470 struct draw_llvm_variant_list_item vs_variants_list;
471 int nr_variants;
472
473 struct draw_gs_llvm_variant_list_item gs_variants_list;
474 int nr_gs_variants;
475 };
476
477
478 static INLINE struct llvm_vertex_shader *
479 llvm_vertex_shader(struct draw_vertex_shader *vs)
480 {
481 return (struct llvm_vertex_shader *)vs;
482 }
483
484 static INLINE struct llvm_geometry_shader *
485 llvm_geometry_shader(struct draw_geometry_shader *gs)
486 {
487 return (struct llvm_geometry_shader *)gs;
488 }
489
490
491
492
493 struct draw_llvm *
494 draw_llvm_create(struct draw_context *draw, LLVMContextRef llvm_context);
495
496 void
497 draw_llvm_destroy(struct draw_llvm *llvm);
498
499 struct draw_llvm_variant *
500 draw_llvm_create_variant(struct draw_llvm *llvm,
501 unsigned num_vertex_header_attribs,
502 const struct draw_llvm_variant_key *key);
503
504 void
505 draw_llvm_destroy_variant(struct draw_llvm_variant *variant);
506
507 struct draw_llvm_variant_key *
508 draw_llvm_make_variant_key(struct draw_llvm *llvm, char *store);
509
510 void
511 draw_llvm_dump_variant_key(struct draw_llvm_variant_key *key);
512
513
514 struct draw_gs_llvm_variant *
515 draw_gs_llvm_create_variant(struct draw_llvm *llvm,
516 unsigned num_vertex_header_attribs,
517 const struct draw_gs_llvm_variant_key *key);
518
519 void
520 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant *variant);
521
522 struct draw_gs_llvm_variant_key *
523 draw_gs_llvm_make_variant_key(struct draw_llvm *llvm, char *store);
524
525 void
526 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key *key);
527
528 struct lp_build_sampler_soa *
529 draw_llvm_sampler_soa_create(const struct draw_sampler_static_state *static_state,
530 LLVMValueRef context_ptr);
531
532 void
533 draw_llvm_set_sampler_state(struct draw_context *draw, unsigned shader_stage);
534
535 void
536 draw_llvm_set_mapped_texture(struct draw_context *draw,
537 unsigned shader_stage,
538 unsigned sview_idx,
539 uint32_t width, uint32_t height, uint32_t depth,
540 uint32_t first_level, uint32_t last_level,
541 const void *base_ptr,
542 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
543 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
544 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS]);
545
546 #endif