draw_llvm.h revision af69d88d
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
43struct draw_llvm;
44struct llvm_vertex_shader;
45struct llvm_geometry_shader;
46
47struct 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
61struct 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
73struct draw_jit_sampler
74{
75   float min_lod;
76   float max_lod;
77   float lod_bias;
78   float border_color[4];
79};
80
81
82enum {
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
96enum {
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
105enum {
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 */
123struct 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
134enum {
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
181enum {
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 */
205struct 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
222enum {
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
268typedef 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
279
280typedef int
281(*draw_jit_vert_func_elts)(struct draw_jit_context *context,
282                           struct vertex_header *io,
283                           const struct draw_vertex_buffer vbuffers[PIPE_MAX_ATTRIBS],
284                           const unsigned *fetch_elts,
285                           unsigned fetch_max_elt,
286                           unsigned fetch_count,
287                           unsigned stride,
288                           struct pipe_vertex_buffer *vertex_buffers,
289                           unsigned instance_id,
290                           unsigned vertex_id_offset);
291
292
293typedef int
294(*draw_gs_jit_func)(struct draw_gs_jit_context *context,
295                    float inputs[6][PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS][TGSI_NUM_CHANNELS],
296                    struct vertex_header *output,
297                    unsigned num_prims,
298                    unsigned instance_id,
299                    int *prim_ids);
300
301struct draw_llvm_variant_key
302{
303   unsigned nr_vertex_elements:8;
304   unsigned nr_samplers:8;
305   unsigned nr_sampler_views:8;
306   unsigned clamp_vertex_color:1;
307   unsigned clip_xy:1;
308   unsigned clip_z:1;
309   unsigned clip_user:1;
310   unsigned clip_halfz:1;
311   unsigned bypass_viewport:1;
312   unsigned need_edgeflags:1;
313   unsigned has_gs:1;
314   unsigned num_outputs:8;
315   /*
316    * it is important there are no holes in this struct
317    * (and all padding gets zeroed).
318    */
319   unsigned ucp_enable:PIPE_MAX_CLIP_PLANES;
320   unsigned pad1:24-PIPE_MAX_CLIP_PLANES;
321
322   /* Variable number of vertex elements:
323    */
324   struct pipe_vertex_element vertex_element[1];
325
326   /* Followed by variable number of samplers:
327    */
328/*   struct draw_sampler_static_state sampler; */
329};
330
331struct draw_gs_llvm_variant_key
332{
333   unsigned nr_samplers:8;
334   unsigned nr_sampler_views:8;
335   unsigned num_outputs:8;
336
337   struct draw_sampler_static_state samplers[1];
338};
339
340#define DRAW_LLVM_MAX_VARIANT_KEY_SIZE \
341   (sizeof(struct draw_llvm_variant_key) +	\
342    PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state) +	\
343    (PIPE_MAX_ATTRIBS-1) * sizeof(struct pipe_vertex_element))
344
345#define DRAW_GS_LLVM_MAX_VARIANT_KEY_SIZE \
346   (sizeof(struct draw_gs_llvm_variant_key) +	\
347    PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state))
348
349
350static INLINE size_t
351draw_llvm_variant_key_size(unsigned nr_vertex_elements,
352                           unsigned nr_samplers)
353{
354   return (sizeof(struct draw_llvm_variant_key) +
355           nr_samplers * sizeof(struct draw_sampler_static_state) +
356           (nr_vertex_elements - 1) * sizeof(struct pipe_vertex_element));
357}
358
359
360static INLINE size_t
361draw_gs_llvm_variant_key_size(unsigned nr_samplers)
362{
363   return (sizeof(struct draw_gs_llvm_variant_key) +
364           (nr_samplers - 1) * sizeof(struct draw_sampler_static_state));
365}
366
367
368static INLINE struct draw_sampler_static_state *
369draw_llvm_variant_key_samplers(struct draw_llvm_variant_key *key)
370{
371   return (struct draw_sampler_static_state *)
372      &key->vertex_element[key->nr_vertex_elements];
373}
374
375
376struct draw_llvm_variant_list_item
377{
378   struct draw_llvm_variant *base;
379   struct draw_llvm_variant_list_item *next, *prev;
380};
381
382struct draw_gs_llvm_variant_list_item
383{
384   struct draw_gs_llvm_variant *base;
385   struct draw_gs_llvm_variant_list_item *next, *prev;
386};
387
388
389struct draw_llvm_variant
390{
391   struct gallivm_state *gallivm;
392
393   /* LLVM JIT builder types */
394   LLVMTypeRef context_ptr_type;
395   LLVMTypeRef buffer_ptr_type;
396   LLVMTypeRef vb_ptr_type;
397   LLVMTypeRef vertex_header_ptr_type;
398
399   LLVMValueRef function;
400   LLVMValueRef function_elts;
401   draw_jit_vert_func jit_func;
402   draw_jit_vert_func_elts jit_func_elts;
403
404   struct llvm_vertex_shader *shader;
405
406   struct draw_llvm *llvm;
407   struct draw_llvm_variant_list_item list_item_global;
408   struct draw_llvm_variant_list_item list_item_local;
409
410   /* key is variable-sized, must be last */
411   struct draw_llvm_variant_key key;
412};
413
414
415struct draw_gs_llvm_variant
416{
417   struct gallivm_state *gallivm;
418
419   /* LLVM JIT builder types */
420   LLVMTypeRef context_ptr_type;
421   LLVMTypeRef vertex_header_ptr_type;
422   LLVMTypeRef input_array_type;
423
424   LLVMValueRef context_ptr;
425   LLVMValueRef io_ptr;
426   LLVMValueRef num_prims;
427   LLVMValueRef function;
428   draw_gs_jit_func jit_func;
429
430   struct llvm_geometry_shader *shader;
431
432   struct draw_llvm *llvm;
433   struct draw_gs_llvm_variant_list_item list_item_global;
434   struct draw_gs_llvm_variant_list_item list_item_local;
435
436   /* key is variable-sized, must be last */
437   struct draw_gs_llvm_variant_key key;
438};
439
440struct llvm_vertex_shader {
441   struct draw_vertex_shader base;
442
443   unsigned variant_key_size;
444   struct draw_llvm_variant_list_item variants;
445   unsigned variants_created;
446   unsigned variants_cached;
447};
448
449struct llvm_geometry_shader {
450   struct draw_geometry_shader base;
451
452   unsigned variant_key_size;
453   struct draw_gs_llvm_variant_list_item variants;
454   unsigned variants_created;
455   unsigned variants_cached;
456};
457
458
459struct draw_llvm {
460   struct draw_context *draw;
461
462   struct draw_jit_context jit_context;
463   struct draw_gs_jit_context gs_jit_context;
464
465   struct draw_llvm_variant_list_item vs_variants_list;
466   int nr_variants;
467
468   struct draw_gs_llvm_variant_list_item gs_variants_list;
469   int nr_gs_variants;
470};
471
472
473static INLINE struct llvm_vertex_shader *
474llvm_vertex_shader(struct draw_vertex_shader *vs)
475{
476   return (struct llvm_vertex_shader *)vs;
477}
478
479static INLINE struct llvm_geometry_shader *
480llvm_geometry_shader(struct draw_geometry_shader *gs)
481{
482   return (struct llvm_geometry_shader *)gs;
483}
484
485
486
487
488struct draw_llvm *
489draw_llvm_create(struct draw_context *draw);
490
491void
492draw_llvm_destroy(struct draw_llvm *llvm);
493
494struct draw_llvm_variant *
495draw_llvm_create_variant(struct draw_llvm *llvm,
496                         unsigned num_vertex_header_attribs,
497                         const struct draw_llvm_variant_key *key);
498
499void
500draw_llvm_destroy_variant(struct draw_llvm_variant *variant);
501
502struct draw_llvm_variant_key *
503draw_llvm_make_variant_key(struct draw_llvm *llvm, char *store);
504
505void
506draw_llvm_dump_variant_key(struct draw_llvm_variant_key *key);
507
508
509struct draw_gs_llvm_variant *
510draw_gs_llvm_create_variant(struct draw_llvm *llvm,
511                            unsigned num_vertex_header_attribs,
512                            const struct draw_gs_llvm_variant_key *key);
513
514void
515draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant *variant);
516
517struct draw_gs_llvm_variant_key *
518draw_gs_llvm_make_variant_key(struct draw_llvm *llvm, char *store);
519
520void
521draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key *key);
522
523struct lp_build_sampler_soa *
524draw_llvm_sampler_soa_create(const struct draw_sampler_static_state *static_state,
525                             LLVMValueRef context_ptr);
526
527void
528draw_llvm_set_sampler_state(struct draw_context *draw, unsigned shader_stage);
529
530void
531draw_llvm_set_mapped_texture(struct draw_context *draw,
532                             unsigned shader_stage,
533                             unsigned sview_idx,
534                             uint32_t width, uint32_t height, uint32_t depth,
535                             uint32_t first_level, uint32_t last_level,
536                             const void *base_ptr,
537                             uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
538                             uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
539                             uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS]);
540
541#endif
542