gallium/llvm: implement geometry shaders in the llvm paths
[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 float (*planes) [DRAW_TOTAL_CLIP_PLANES][4];
127 float *viewport;
128
129 struct draw_jit_texture textures[PIPE_MAX_SHADER_SAMPLER_VIEWS];
130 struct draw_jit_sampler samplers[PIPE_MAX_SAMPLERS];
131 };
132
133
134 #define draw_jit_context_vs_constants(_gallivm, _ptr) \
135 lp_build_struct_get_ptr(_gallivm, _ptr, 0, "vs_constants")
136
137 #define draw_jit_context_planes(_gallivm, _ptr) \
138 lp_build_struct_get(_gallivm, _ptr, 1, "planes")
139
140 #define draw_jit_context_viewport(_gallivm, _ptr) \
141 lp_build_struct_get(_gallivm, _ptr, 2, "viewport")
142
143 #define DRAW_JIT_CTX_TEXTURES 3
144 #define DRAW_JIT_CTX_SAMPLERS 4
145
146 #define draw_jit_context_textures(_gallivm, _ptr) \
147 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_TEXTURES, "textures")
148
149 #define draw_jit_context_samplers(_gallivm, _ptr) \
150 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_SAMPLERS, "samplers")
151
152 #define draw_jit_header_id(_gallivm, _ptr) \
153 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_VERTEX_ID, "id")
154
155 #define draw_jit_header_clip(_gallivm, _ptr) \
156 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_CLIP, "clip")
157
158 #define draw_jit_header_pre_clip_pos(_gallivm, _ptr) \
159 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_PRE_CLIP_POS, "pre_clip_pos")
160
161 #define draw_jit_header_data(_gallivm, _ptr) \
162 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_DATA, "data")
163
164
165 #define draw_jit_vbuffer_stride(_gallivm, _ptr) \
166 lp_build_struct_get(_gallivm, _ptr, 0, "stride")
167
168 #define draw_jit_vbuffer_offset(_gallivm, _ptr) \
169 lp_build_struct_get(_gallivm, _ptr, 1, "buffer_offset")
170
171
172 /**
173 * This structure is passed directly to the generated geometry shader.
174 *
175 * It contains the derived state.
176 *
177 * Changes here must be reflected in the draw_gs_jit_context_* macros.
178 * Changes to the ordering should be avoided.
179 *
180 * Only use types with a clear size and padding here, in particular prefer the
181 * stdint.h types to the basic integer types.
182 */
183 struct draw_gs_jit_context
184 {
185 const float *constants[LP_MAX_TGSI_CONST_BUFFERS];
186 float (*planes) [DRAW_TOTAL_CLIP_PLANES][4];
187 float *viewport;
188
189 int **prim_lengths;
190 int *emitted_vertices;
191 int *emitted_prims;
192
193 struct draw_jit_texture textures[PIPE_MAX_SHADER_SAMPLER_VIEWS];
194 struct draw_jit_sampler samplers[PIPE_MAX_SAMPLERS];
195 };
196
197
198 #define draw_gs_jit_context_constants(_gallivm, _ptr) \
199 lp_build_struct_get_ptr(_gallivm, _ptr, 0, "constants")
200
201 #define draw_gs_jit_context_planes(_gallivm, _ptr) \
202 lp_build_struct_get(_gallivm, _ptr, 1, "planes")
203
204 #define draw_gs_jit_context_viewport(_gallivm, _ptr) \
205 lp_build_struct_get(_gallivm, _ptr, 2, "viewport")
206
207 #define draw_gs_jit_prim_lengths(_gallivm, _ptr) \
208 lp_build_struct_get(_gallivm, _ptr, 3, "prim_lengths")
209
210 #define draw_gs_jit_emitted_vertices(_gallivm, _ptr) \
211 lp_build_struct_get(_gallivm, _ptr, 4, "emitted_vertices")
212
213 #define draw_gs_jit_emitted_prims(_gallivm, _ptr) \
214 lp_build_struct_get(_gallivm, _ptr, 5, "emitted_prims")
215
216 #define DRAW_GS_JIT_CTX_TEXTURES 6
217 #define DRAW_GS_JIT_CTX_SAMPLERS 7
218
219 #define draw_gs_jit_context_textures(_gallivm, _ptr) \
220 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_TEXTURES, "textures")
221
222 #define draw_gs_jit_context_samplers(_gallivm, _ptr) \
223 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_SAMPLERS, "samplers")
224
225
226
227 typedef int
228 (*draw_jit_vert_func)(struct draw_jit_context *context,
229 struct vertex_header *io,
230 const char *vbuffers[PIPE_MAX_ATTRIBS],
231 unsigned start,
232 unsigned count,
233 unsigned stride,
234 struct pipe_vertex_buffer *vertex_buffers,
235 unsigned instance_id);
236
237
238 typedef int
239 (*draw_jit_vert_func_elts)(struct draw_jit_context *context,
240 struct vertex_header *io,
241 const char *vbuffers[PIPE_MAX_ATTRIBS],
242 const unsigned *fetch_elts,
243 unsigned fetch_count,
244 unsigned stride,
245 struct pipe_vertex_buffer *vertex_buffers,
246 unsigned instance_id);
247
248
249 typedef int
250 (*draw_gs_jit_func)(struct draw_gs_jit_context *context,
251 float inputs[6][PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS][TGSI_NUM_CHANNELS],
252 struct vertex_header *output,
253 unsigned num_prims,
254 unsigned instance_id);
255
256 struct draw_llvm_variant_key
257 {
258 unsigned nr_vertex_elements:8;
259 unsigned nr_samplers:8;
260 unsigned nr_sampler_views:8;
261 unsigned clamp_vertex_color:1;
262 unsigned clip_xy:1;
263 unsigned clip_z:1;
264 unsigned clip_user:1;
265 unsigned clip_halfz:1;
266 unsigned bypass_viewport:1;
267 unsigned need_edgeflags:1;
268 unsigned has_gs:1;
269 /*
270 * it is important there are no holes in this struct
271 * (and all padding gets zeroed).
272 */
273 unsigned ucp_enable:PIPE_MAX_CLIP_PLANES;
274 unsigned pad1:32-PIPE_MAX_CLIP_PLANES;
275
276 /* Variable number of vertex elements:
277 */
278 struct pipe_vertex_element vertex_element[1];
279
280 /* Followed by variable number of samplers:
281 */
282 /* struct draw_sampler_static_state sampler; */
283 };
284
285 struct draw_gs_llvm_variant_key
286 {
287 unsigned nr_samplers:8;
288 unsigned nr_sampler_views:8;
289
290 struct draw_sampler_static_state samplers[1];
291 };
292
293 #define DRAW_LLVM_MAX_VARIANT_KEY_SIZE \
294 (sizeof(struct draw_llvm_variant_key) + \
295 PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state) + \
296 (PIPE_MAX_ATTRIBS-1) * sizeof(struct pipe_vertex_element))
297
298 #define DRAW_GS_LLVM_MAX_VARIANT_KEY_SIZE \
299 (sizeof(struct draw_gs_llvm_variant_key) + \
300 PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state))
301
302
303 static INLINE size_t
304 draw_llvm_variant_key_size(unsigned nr_vertex_elements,
305 unsigned nr_samplers)
306 {
307 return (sizeof(struct draw_llvm_variant_key) +
308 nr_samplers * sizeof(struct draw_sampler_static_state) +
309 (nr_vertex_elements - 1) * sizeof(struct pipe_vertex_element));
310 }
311
312
313 static INLINE size_t
314 draw_gs_llvm_variant_key_size(unsigned nr_samplers)
315 {
316 return (sizeof(struct draw_gs_llvm_variant_key) +
317 (nr_samplers - 1) * sizeof(struct draw_sampler_static_state));
318 }
319
320
321 static INLINE struct draw_sampler_static_state *
322 draw_llvm_variant_key_samplers(struct draw_llvm_variant_key *key)
323 {
324 return (struct draw_sampler_static_state *)
325 &key->vertex_element[key->nr_vertex_elements];
326 }
327
328
329 struct draw_llvm_variant_list_item
330 {
331 struct draw_llvm_variant *base;
332 struct draw_llvm_variant_list_item *next, *prev;
333 };
334
335 struct draw_gs_llvm_variant_list_item
336 {
337 struct draw_gs_llvm_variant *base;
338 struct draw_gs_llvm_variant_list_item *next, *prev;
339 };
340
341
342 struct draw_llvm_variant
343 {
344 struct gallivm_state *gallivm;
345
346 /* LLVM JIT builder types */
347 LLVMTypeRef context_ptr_type;
348 LLVMTypeRef buffer_ptr_type;
349 LLVMTypeRef vb_ptr_type;
350 LLVMTypeRef vertex_header_ptr_type;
351
352 LLVMValueRef function;
353 LLVMValueRef function_elts;
354 draw_jit_vert_func jit_func;
355 draw_jit_vert_func_elts jit_func_elts;
356
357 struct llvm_vertex_shader *shader;
358
359 struct draw_llvm *llvm;
360 struct draw_llvm_variant_list_item list_item_global;
361 struct draw_llvm_variant_list_item list_item_local;
362
363 /* key is variable-sized, must be last */
364 struct draw_llvm_variant_key key;
365 };
366
367
368 struct draw_gs_llvm_variant
369 {
370 struct gallivm_state *gallivm;
371
372 /* LLVM JIT builder types */
373 LLVMTypeRef context_ptr_type;
374 LLVMTypeRef vertex_header_ptr_type;
375 LLVMTypeRef input_array_type;
376
377 LLVMValueRef context_ptr;
378 LLVMValueRef io_ptr;
379 LLVMValueRef num_prims;
380 LLVMValueRef function;
381 draw_gs_jit_func jit_func;
382
383 struct llvm_geometry_shader *shader;
384
385 struct draw_llvm *llvm;
386 struct draw_gs_llvm_variant_list_item list_item_global;
387 struct draw_gs_llvm_variant_list_item list_item_local;
388
389 /* key is variable-sized, must be last */
390 struct draw_gs_llvm_variant_key key;
391 };
392
393 struct llvm_vertex_shader {
394 struct draw_vertex_shader base;
395
396 unsigned variant_key_size;
397 struct draw_llvm_variant_list_item variants;
398 unsigned variants_created;
399 unsigned variants_cached;
400 };
401
402 struct llvm_geometry_shader {
403 struct draw_geometry_shader base;
404
405 unsigned variant_key_size;
406 struct draw_gs_llvm_variant_list_item variants;
407 unsigned variants_created;
408 unsigned variants_cached;
409 };
410
411
412 struct draw_llvm {
413 struct draw_context *draw;
414
415 struct draw_jit_context jit_context;
416 struct draw_gs_jit_context gs_jit_context;
417
418 struct draw_llvm_variant_list_item vs_variants_list;
419 int nr_variants;
420
421 struct draw_gs_llvm_variant_list_item gs_variants_list;
422 int nr_gs_variants;
423 };
424
425
426 static INLINE struct llvm_vertex_shader *
427 llvm_vertex_shader(struct draw_vertex_shader *vs)
428 {
429 return (struct llvm_vertex_shader *)vs;
430 }
431
432 static INLINE struct llvm_geometry_shader *
433 llvm_geometry_shader(struct draw_geometry_shader *gs)
434 {
435 return (struct llvm_geometry_shader *)gs;
436 }
437
438
439
440
441 struct draw_llvm *
442 draw_llvm_create(struct draw_context *draw);
443
444 void
445 draw_llvm_destroy(struct draw_llvm *llvm);
446
447 struct draw_llvm_variant *
448 draw_llvm_create_variant(struct draw_llvm *llvm,
449 unsigned num_vertex_header_attribs,
450 const struct draw_llvm_variant_key *key);
451
452 void
453 draw_llvm_destroy_variant(struct draw_llvm_variant *variant);
454
455 struct draw_llvm_variant_key *
456 draw_llvm_make_variant_key(struct draw_llvm *llvm, char *store);
457
458 void
459 draw_llvm_dump_variant_key(struct draw_llvm_variant_key *key);
460
461
462 struct draw_gs_llvm_variant *
463 draw_gs_llvm_create_variant(struct draw_llvm *llvm,
464 unsigned num_vertex_header_attribs,
465 const struct draw_gs_llvm_variant_key *key);
466
467 void
468 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant *variant);
469
470 struct draw_gs_llvm_variant_key *
471 draw_gs_llvm_make_variant_key(struct draw_llvm *llvm, char *store);
472
473 void
474 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key *key);
475
476 struct lp_build_sampler_soa *
477 draw_llvm_sampler_soa_create(const struct draw_sampler_static_state *static_state,
478 LLVMValueRef context_ptr);
479
480 void
481 draw_llvm_set_sampler_state(struct draw_context *draw);
482
483 void
484 draw_llvm_set_mapped_texture(struct draw_context *draw,
485 unsigned shader_stage,
486 unsigned sview_idx,
487 uint32_t width, uint32_t height, uint32_t depth,
488 uint32_t first_level, uint32_t last_level,
489 const void *base_ptr,
490 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
491 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
492 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS]);
493
494 #endif