• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_POS,
108    DRAW_JIT_VERTEX_DATA
109 };
110 
111 /**
112  * This structure is passed directly to the generated vertex shader.
113  *
114  * It contains the derived state.
115  *
116  * Changes here must be reflected in the draw_jit_context_* macros.
117  * Changes to the ordering should be avoided.
118  *
119  * Only use types with a clear size and padding here, in particular prefer the
120  * stdint.h types to the basic integer types.
121  */
122 struct draw_jit_context
123 {
124    const float *vs_constants[LP_MAX_TGSI_CONST_BUFFERS];
125    int num_vs_constants[LP_MAX_TGSI_CONST_BUFFERS];
126    float (*planes) [DRAW_TOTAL_CLIP_PLANES][4];
127    struct pipe_viewport_state *viewports;
128 
129    struct draw_jit_texture textures[PIPE_MAX_SHADER_SAMPLER_VIEWS];
130    struct draw_jit_sampler samplers[PIPE_MAX_SAMPLERS];
131 };
132 
133 enum {
134    DRAW_JIT_CTX_CONSTANTS            = 0,
135    DRAW_JIT_CTX_NUM_CONSTANTS        = 1,
136    DRAW_JIT_CTX_PLANES               = 2,
137    DRAW_JIT_CTX_VIEWPORT             = 3,
138    DRAW_JIT_CTX_TEXTURES             = 4,
139    DRAW_JIT_CTX_SAMPLERS             = 5,
140    DRAW_JIT_CTX_NUM_FIELDS
141 };
142 
143 #define draw_jit_context_vs_constants(_gallivm, _ptr) \
144    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_CONSTANTS, "vs_constants")
145 
146 #define draw_jit_context_num_vs_constants(_gallivm, _ptr) \
147    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_NUM_CONSTANTS, "num_vs_constants")
148 
149 #define draw_jit_context_planes(_gallivm, _ptr) \
150    lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_CTX_PLANES, "planes")
151 
152 #define draw_jit_context_viewports(_gallivm, _ptr) \
153    lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_CTX_VIEWPORT, "viewports")
154 
155 #define draw_jit_context_textures(_gallivm, _ptr) \
156    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_TEXTURES, "textures")
157 
158 #define draw_jit_context_samplers(_gallivm, _ptr) \
159    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_SAMPLERS, "samplers")
160 
161 #define draw_jit_header_id(_gallivm, _ptr)              \
162    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_VERTEX_ID, "id")
163 
164 #define draw_jit_header_clip_pos(_gallivm, _ptr) \
165    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_CLIP_POS, "clip_pos")
166 
167 #define draw_jit_header_data(_gallivm, _ptr)            \
168    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_DATA, "data")
169 
170 
171 #define draw_jit_vbuffer_stride(_gallivm, _ptr)         \
172    lp_build_struct_get(_gallivm, _ptr, 0, "stride")
173 
174 #define draw_jit_vbuffer_offset(_gallivm, _ptr)         \
175    lp_build_struct_get(_gallivm, _ptr, 2, "buffer_offset")
176 
177 enum {
178    DRAW_JIT_DVBUFFER_MAP = 0,
179    DRAW_JIT_DVBUFFER_SIZE,
180    DRAW_JIT_DVBUFFER_NUM_FIELDS  /* number of fields above */
181 };
182 
183 #define draw_jit_dvbuffer_map(_gallivm, _ptr)         \
184    lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_DVBUFFER_MAP, "map")
185 
186 #define draw_jit_dvbuffer_size(_gallivm, _ptr)        \
187    lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_DVBUFFER_SIZE, "size")
188 
189 
190 /**
191  * This structure is passed directly to the generated geometry shader.
192  *
193  * It contains the derived state.
194  *
195  * Changes here must be reflected in the draw_gs_jit_context_* macros.
196  * Changes to the ordering should be avoided.
197  *
198  * Only use types with a clear size and padding here, in particular prefer the
199  * stdint.h types to the basic integer types.
200  */
201 struct draw_gs_jit_context
202 {
203    const float *constants[LP_MAX_TGSI_CONST_BUFFERS];
204    int num_constants[LP_MAX_TGSI_CONST_BUFFERS];
205    float (*planes) [DRAW_TOTAL_CLIP_PLANES][4];
206    struct pipe_viewport_state *viewports;
207 
208    /* There two need to be exactly at DRAW_JIT_CTX_TEXTURES and
209     * DRAW_JIT_CTX_SAMPLERS positions in the struct */
210    struct draw_jit_texture textures[PIPE_MAX_SHADER_SAMPLER_VIEWS];
211    struct draw_jit_sampler samplers[PIPE_MAX_SAMPLERS];
212 
213    int **prim_lengths;
214    int *emitted_vertices;
215    int *emitted_prims;
216 };
217 
218 enum {
219    DRAW_GS_JIT_CTX_CONSTANTS = 0,
220    DRAW_GS_JIT_CTX_NUM_CONSTANTS = 1,
221    DRAW_GS_JIT_CTX_PLANES = 2,
222    DRAW_GS_JIT_CTX_VIEWPORT = 3,
223    /* Textures and samples are reserved for DRAW_JIT_CTX_TEXTURES
224     * and DRAW_JIT_CTX_SAMPLERS, because they both need
225     * to be at exactly the same locations as they are in the
226     * VS ctx structure for sampling to work. */
227    DRAW_GS_JIT_CTX_TEXTURES = DRAW_JIT_CTX_TEXTURES,
228    DRAW_GS_JIT_CTX_SAMPLERS = DRAW_JIT_CTX_SAMPLERS,
229    DRAW_GS_JIT_CTX_PRIM_LENGTHS = 6,
230    DRAW_GS_JIT_CTX_EMITTED_VERTICES = 7,
231    DRAW_GS_JIT_CTX_EMITTED_PRIMS = 8,
232    DRAW_GS_JIT_CTX_NUM_FIELDS = 9
233 };
234 
235 #define draw_gs_jit_context_constants(_gallivm, _ptr) \
236    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_CONSTANTS, "constants")
237 
238 #define draw_gs_jit_context_num_constants(_gallivm, _ptr) \
239    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_NUM_CONSTANTS, "num_constants")
240 
241 #define draw_gs_jit_context_planes(_gallivm, _ptr) \
242    lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_PLANES, "planes")
243 
244 #define draw_gs_jit_context_viewports(_gallivm, _ptr) \
245    lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_VIEWPORT, "viewports")
246 
247 #define draw_gs_jit_context_textures(_gallivm, _ptr) \
248    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_TEXTURES, "textures")
249 
250 #define draw_gs_jit_context_samplers(_gallivm, _ptr) \
251    lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_SAMPLERS, "samplers")
252 
253 #define draw_gs_jit_prim_lengths(_gallivm, _ptr) \
254    lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_PRIM_LENGTHS, "prim_lengths")
255 
256 #define draw_gs_jit_emitted_vertices(_gallivm, _ptr) \
257    lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_EMITTED_VERTICES, "emitted_vertices")
258 
259 #define draw_gs_jit_emitted_prims(_gallivm, _ptr) \
260    lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_EMITTED_PRIMS, "emitted_prims")
261 
262 
263 
264 typedef boolean
265 (*draw_jit_vert_func)(struct draw_jit_context *context,
266                       struct vertex_header *io,
267                       const struct draw_vertex_buffer vbuffers[PIPE_MAX_ATTRIBS],
268                       unsigned count,
269                       unsigned start_or_maxelt,
270                       unsigned stride,
271                       struct pipe_vertex_buffer *vertex_buffers,
272                       unsigned instance_id,
273                       unsigned vertex_id_offset,
274                       unsigned start_instance,
275                       const unsigned *fetch_elts);
276 
277 
278 typedef int
279 (*draw_gs_jit_func)(struct draw_gs_jit_context *context,
280                     float inputs[6][PIPE_MAX_SHADER_INPUTS][TGSI_NUM_CHANNELS][TGSI_NUM_CHANNELS],
281                     struct vertex_header *output,
282                     unsigned num_prims,
283                     unsigned instance_id,
284                     int *prim_ids,
285                     unsigned invocation_id);
286 
287 struct draw_llvm_variant_key
288 {
289    unsigned nr_vertex_elements:8;
290    unsigned nr_samplers:8;
291    unsigned nr_sampler_views:8;
292    unsigned clamp_vertex_color:1;
293    unsigned clip_xy:1;
294    unsigned clip_z:1;
295    unsigned clip_user:1;
296    unsigned clip_halfz:1;
297    unsigned bypass_viewport:1;
298    unsigned need_edgeflags:1;
299    unsigned has_gs:1;
300    unsigned num_outputs:8;
301    unsigned ucp_enable:PIPE_MAX_CLIP_PLANES;
302    /* note padding here - must use memset */
303 
304    /* Variable number of vertex elements:
305     */
306    struct pipe_vertex_element vertex_element[1];
307 
308    /* Followed by variable number of samplers:
309     */
310 /*   struct draw_sampler_static_state sampler; */
311 };
312 
313 struct draw_gs_llvm_variant_key
314 {
315    unsigned nr_samplers:8;
316    unsigned nr_sampler_views:8;
317    unsigned num_outputs:8;
318    /* note padding here - must use memset */
319 
320    struct draw_sampler_static_state samplers[1];
321 };
322 
323 #define DRAW_LLVM_MAX_VARIANT_KEY_SIZE \
324    (sizeof(struct draw_llvm_variant_key) +	\
325     PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state) +	\
326     (PIPE_MAX_ATTRIBS-1) * sizeof(struct pipe_vertex_element))
327 
328 #define DRAW_GS_LLVM_MAX_VARIANT_KEY_SIZE \
329    (sizeof(struct draw_gs_llvm_variant_key) +	\
330     PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state))
331 
332 
333 static inline size_t
draw_llvm_variant_key_size(unsigned nr_vertex_elements,unsigned nr_samplers)334 draw_llvm_variant_key_size(unsigned nr_vertex_elements,
335                            unsigned nr_samplers)
336 {
337    return (sizeof(struct draw_llvm_variant_key) +
338            nr_samplers * sizeof(struct draw_sampler_static_state) +
339            (nr_vertex_elements - 1) * sizeof(struct pipe_vertex_element));
340 }
341 
342 
343 static inline size_t
draw_gs_llvm_variant_key_size(unsigned nr_samplers)344 draw_gs_llvm_variant_key_size(unsigned nr_samplers)
345 {
346    return (sizeof(struct draw_gs_llvm_variant_key) +
347            (nr_samplers - 1) * sizeof(struct draw_sampler_static_state));
348 }
349 
350 
351 static inline struct draw_sampler_static_state *
draw_llvm_variant_key_samplers(struct draw_llvm_variant_key * key)352 draw_llvm_variant_key_samplers(struct draw_llvm_variant_key *key)
353 {
354    return (struct draw_sampler_static_state *)
355       &key->vertex_element[key->nr_vertex_elements];
356 }
357 
358 
359 struct draw_llvm_variant_list_item
360 {
361    struct draw_llvm_variant *base;
362    struct draw_llvm_variant_list_item *next, *prev;
363 };
364 
365 struct draw_gs_llvm_variant_list_item
366 {
367    struct draw_gs_llvm_variant *base;
368    struct draw_gs_llvm_variant_list_item *next, *prev;
369 };
370 
371 
372 struct draw_llvm_variant
373 {
374    struct gallivm_state *gallivm;
375 
376    /* LLVM JIT builder types */
377    LLVMTypeRef context_ptr_type;
378    LLVMTypeRef buffer_ptr_type;
379    LLVMTypeRef vb_ptr_type;
380    LLVMTypeRef vertex_header_ptr_type;
381 
382    LLVMValueRef function;
383    draw_jit_vert_func jit_func;
384 
385    struct llvm_vertex_shader *shader;
386 
387    struct draw_llvm *llvm;
388    struct draw_llvm_variant_list_item list_item_global;
389    struct draw_llvm_variant_list_item list_item_local;
390 
391    /* key is variable-sized, must be last */
392    struct draw_llvm_variant_key key;
393 };
394 
395 
396 struct draw_gs_llvm_variant
397 {
398    struct gallivm_state *gallivm;
399 
400    /* LLVM JIT builder types */
401    LLVMTypeRef context_ptr_type;
402    LLVMTypeRef vertex_header_ptr_type;
403    LLVMTypeRef input_array_type;
404 
405    LLVMValueRef context_ptr;
406    LLVMValueRef io_ptr;
407    LLVMValueRef num_prims;
408    LLVMValueRef function;
409    draw_gs_jit_func jit_func;
410 
411    struct llvm_geometry_shader *shader;
412 
413    struct draw_llvm *llvm;
414    struct draw_gs_llvm_variant_list_item list_item_global;
415    struct draw_gs_llvm_variant_list_item list_item_local;
416 
417    /* key is variable-sized, must be last */
418    struct draw_gs_llvm_variant_key key;
419 };
420 
421 struct llvm_vertex_shader {
422    struct draw_vertex_shader base;
423 
424    unsigned variant_key_size;
425    struct draw_llvm_variant_list_item variants;
426    unsigned variants_created;
427    unsigned variants_cached;
428 };
429 
430 struct llvm_geometry_shader {
431    struct draw_geometry_shader base;
432 
433    unsigned variant_key_size;
434    struct draw_gs_llvm_variant_list_item variants;
435    unsigned variants_created;
436    unsigned variants_cached;
437 };
438 
439 
440 struct draw_llvm {
441    struct draw_context *draw;
442 
443    LLVMContextRef context;
444    boolean context_owned;
445 
446    struct draw_jit_context jit_context;
447    struct draw_gs_jit_context gs_jit_context;
448 
449    struct draw_llvm_variant_list_item vs_variants_list;
450    int nr_variants;
451 
452    struct draw_gs_llvm_variant_list_item gs_variants_list;
453    int nr_gs_variants;
454 };
455 
456 
457 static inline struct llvm_vertex_shader *
llvm_vertex_shader(struct draw_vertex_shader * vs)458 llvm_vertex_shader(struct draw_vertex_shader *vs)
459 {
460    return (struct llvm_vertex_shader *)vs;
461 }
462 
463 static inline struct llvm_geometry_shader *
llvm_geometry_shader(struct draw_geometry_shader * gs)464 llvm_geometry_shader(struct draw_geometry_shader *gs)
465 {
466    return (struct llvm_geometry_shader *)gs;
467 }
468 
469 
470 
471 
472 struct draw_llvm *
473 draw_llvm_create(struct draw_context *draw, LLVMContextRef llvm_context);
474 
475 void
476 draw_llvm_destroy(struct draw_llvm *llvm);
477 
478 struct draw_llvm_variant *
479 draw_llvm_create_variant(struct draw_llvm *llvm,
480                          unsigned num_vertex_header_attribs,
481                          const struct draw_llvm_variant_key *key);
482 
483 void
484 draw_llvm_destroy_variant(struct draw_llvm_variant *variant);
485 
486 struct draw_llvm_variant_key *
487 draw_llvm_make_variant_key(struct draw_llvm *llvm, char *store);
488 
489 void
490 draw_llvm_dump_variant_key(struct draw_llvm_variant_key *key);
491 
492 
493 struct draw_gs_llvm_variant *
494 draw_gs_llvm_create_variant(struct draw_llvm *llvm,
495                             unsigned num_vertex_header_attribs,
496                             const struct draw_gs_llvm_variant_key *key);
497 
498 void
499 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant *variant);
500 
501 struct draw_gs_llvm_variant_key *
502 draw_gs_llvm_make_variant_key(struct draw_llvm *llvm, char *store);
503 
504 void
505 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key *key);
506 
507 struct lp_build_sampler_soa *
508 draw_llvm_sampler_soa_create(const struct draw_sampler_static_state *static_state);
509 
510 void
511 draw_llvm_set_sampler_state(struct draw_context *draw,
512                             enum pipe_shader_type shader_stage);
513 
514 void
515 draw_llvm_set_mapped_texture(struct draw_context *draw,
516                              enum pipe_shader_type shader_stage,
517                              unsigned sview_idx,
518                              uint32_t width, uint32_t height, uint32_t depth,
519                              uint32_t first_level, uint32_t last_level,
520                              const void *base_ptr,
521                              uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
522                              uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
523                              uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS]);
524 
525 #endif
526