Merge remote-tracking branch 'mesa-public/master' into vulkan
[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/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 struct pipe_viewport_state *viewports;
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_viewports(_gallivm, _ptr) \
154 lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_CTX_VIEWPORT, "viewports")
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 struct pipe_viewport_state *viewports;
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_viewports(_gallivm, _ptr) \
249 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_VIEWPORT, "viewports")
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 unsigned invocation_id);
303
304 struct draw_llvm_variant_key
305 {
306 unsigned nr_vertex_elements:8;
307 unsigned nr_samplers:8;
308 unsigned nr_sampler_views:8;
309 unsigned clamp_vertex_color:1;
310 unsigned clip_xy:1;
311 unsigned clip_z:1;
312 unsigned clip_user:1;
313 unsigned clip_halfz:1;
314 unsigned bypass_viewport:1;
315 unsigned need_edgeflags:1;
316 unsigned has_gs:1;
317 unsigned num_outputs:8;
318 /*
319 * it is important there are no holes in this struct
320 * (and all padding gets zeroed).
321 */
322 unsigned ucp_enable:PIPE_MAX_CLIP_PLANES;
323 unsigned pad1:24-PIPE_MAX_CLIP_PLANES;
324
325 /* Variable number of vertex elements:
326 */
327 struct pipe_vertex_element vertex_element[1];
328
329 /* Followed by variable number of samplers:
330 */
331 /* struct draw_sampler_static_state sampler; */
332 };
333
334 struct draw_gs_llvm_variant_key
335 {
336 unsigned nr_samplers:8;
337 unsigned nr_sampler_views:8;
338 unsigned num_outputs:8;
339
340 struct draw_sampler_static_state samplers[1];
341 };
342
343 #define DRAW_LLVM_MAX_VARIANT_KEY_SIZE \
344 (sizeof(struct draw_llvm_variant_key) + \
345 PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state) + \
346 (PIPE_MAX_ATTRIBS-1) * sizeof(struct pipe_vertex_element))
347
348 #define DRAW_GS_LLVM_MAX_VARIANT_KEY_SIZE \
349 (sizeof(struct draw_gs_llvm_variant_key) + \
350 PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state))
351
352
353 static INLINE size_t
354 draw_llvm_variant_key_size(unsigned nr_vertex_elements,
355 unsigned nr_samplers)
356 {
357 return (sizeof(struct draw_llvm_variant_key) +
358 nr_samplers * sizeof(struct draw_sampler_static_state) +
359 (nr_vertex_elements - 1) * sizeof(struct pipe_vertex_element));
360 }
361
362
363 static INLINE size_t
364 draw_gs_llvm_variant_key_size(unsigned nr_samplers)
365 {
366 return (sizeof(struct draw_gs_llvm_variant_key) +
367 (nr_samplers - 1) * sizeof(struct draw_sampler_static_state));
368 }
369
370
371 static INLINE struct draw_sampler_static_state *
372 draw_llvm_variant_key_samplers(struct draw_llvm_variant_key *key)
373 {
374 return (struct draw_sampler_static_state *)
375 &key->vertex_element[key->nr_vertex_elements];
376 }
377
378
379 struct draw_llvm_variant_list_item
380 {
381 struct draw_llvm_variant *base;
382 struct draw_llvm_variant_list_item *next, *prev;
383 };
384
385 struct draw_gs_llvm_variant_list_item
386 {
387 struct draw_gs_llvm_variant *base;
388 struct draw_gs_llvm_variant_list_item *next, *prev;
389 };
390
391
392 struct draw_llvm_variant
393 {
394 struct gallivm_state *gallivm;
395
396 /* LLVM JIT builder types */
397 LLVMTypeRef context_ptr_type;
398 LLVMTypeRef buffer_ptr_type;
399 LLVMTypeRef vb_ptr_type;
400 LLVMTypeRef vertex_header_ptr_type;
401
402 LLVMValueRef function;
403 LLVMValueRef function_elts;
404 draw_jit_vert_func jit_func;
405 draw_jit_vert_func_elts jit_func_elts;
406
407 struct llvm_vertex_shader *shader;
408
409 struct draw_llvm *llvm;
410 struct draw_llvm_variant_list_item list_item_global;
411 struct draw_llvm_variant_list_item list_item_local;
412
413 /* key is variable-sized, must be last */
414 struct draw_llvm_variant_key key;
415 };
416
417
418 struct draw_gs_llvm_variant
419 {
420 struct gallivm_state *gallivm;
421
422 /* LLVM JIT builder types */
423 LLVMTypeRef context_ptr_type;
424 LLVMTypeRef vertex_header_ptr_type;
425 LLVMTypeRef input_array_type;
426
427 LLVMValueRef context_ptr;
428 LLVMValueRef io_ptr;
429 LLVMValueRef num_prims;
430 LLVMValueRef function;
431 draw_gs_jit_func jit_func;
432
433 struct llvm_geometry_shader *shader;
434
435 struct draw_llvm *llvm;
436 struct draw_gs_llvm_variant_list_item list_item_global;
437 struct draw_gs_llvm_variant_list_item list_item_local;
438
439 /* key is variable-sized, must be last */
440 struct draw_gs_llvm_variant_key key;
441 };
442
443 struct llvm_vertex_shader {
444 struct draw_vertex_shader base;
445
446 unsigned variant_key_size;
447 struct draw_llvm_variant_list_item variants;
448 unsigned variants_created;
449 unsigned variants_cached;
450 };
451
452 struct llvm_geometry_shader {
453 struct draw_geometry_shader base;
454
455 unsigned variant_key_size;
456 struct draw_gs_llvm_variant_list_item variants;
457 unsigned variants_created;
458 unsigned variants_cached;
459 };
460
461
462 struct draw_llvm {
463 struct draw_context *draw;
464
465 LLVMContextRef context;
466 boolean context_owned;
467
468 struct draw_jit_context jit_context;
469 struct draw_gs_jit_context gs_jit_context;
470
471 struct draw_llvm_variant_list_item vs_variants_list;
472 int nr_variants;
473
474 struct draw_gs_llvm_variant_list_item gs_variants_list;
475 int nr_gs_variants;
476 };
477
478
479 static INLINE struct llvm_vertex_shader *
480 llvm_vertex_shader(struct draw_vertex_shader *vs)
481 {
482 return (struct llvm_vertex_shader *)vs;
483 }
484
485 static INLINE struct llvm_geometry_shader *
486 llvm_geometry_shader(struct draw_geometry_shader *gs)
487 {
488 return (struct llvm_geometry_shader *)gs;
489 }
490
491
492
493
494 struct draw_llvm *
495 draw_llvm_create(struct draw_context *draw, LLVMContextRef llvm_context);
496
497 void
498 draw_llvm_destroy(struct draw_llvm *llvm);
499
500 struct draw_llvm_variant *
501 draw_llvm_create_variant(struct draw_llvm *llvm,
502 unsigned num_vertex_header_attribs,
503 const struct draw_llvm_variant_key *key);
504
505 void
506 draw_llvm_destroy_variant(struct draw_llvm_variant *variant);
507
508 struct draw_llvm_variant_key *
509 draw_llvm_make_variant_key(struct draw_llvm *llvm, char *store);
510
511 void
512 draw_llvm_dump_variant_key(struct draw_llvm_variant_key *key);
513
514
515 struct draw_gs_llvm_variant *
516 draw_gs_llvm_create_variant(struct draw_llvm *llvm,
517 unsigned num_vertex_header_attribs,
518 const struct draw_gs_llvm_variant_key *key);
519
520 void
521 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant *variant);
522
523 struct draw_gs_llvm_variant_key *
524 draw_gs_llvm_make_variant_key(struct draw_llvm *llvm, char *store);
525
526 void
527 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key *key);
528
529 struct lp_build_sampler_soa *
530 draw_llvm_sampler_soa_create(const struct draw_sampler_static_state *static_state);
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