1 /**************************************************************************
3 * Copyright 2010 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
31 #include "draw/draw_private.h"
33 #include "draw/draw_vs.h"
34 #include "draw/draw_gs.h"
36 #include "gallivm/lp_bld_sample.h"
37 #include "gallivm/lp_bld_limits.h"
39 #include "pipe/p_context.h"
40 #include "util/simple_list.h"
44 struct llvm_vertex_shader
;
45 struct llvm_geometry_shader
;
47 struct draw_jit_texture
53 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
];
54 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
];
57 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
];
61 struct draw_sampler_static_state
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.
68 struct lp_static_sampler_state sampler_state
;
69 struct lp_static_texture_state texture_state
;
72 struct draw_image_static_state
74 struct lp_static_texture_state image_state
;
78 struct draw_jit_sampler
83 float border_color
[4];
98 DRAW_JIT_TEXTURE_WIDTH
= 0,
99 DRAW_JIT_TEXTURE_HEIGHT
,
100 DRAW_JIT_TEXTURE_DEPTH
,
101 DRAW_JIT_TEXTURE_BASE
,
102 DRAW_JIT_TEXTURE_ROW_STRIDE
,
103 DRAW_JIT_TEXTURE_IMG_STRIDE
,
104 DRAW_JIT_TEXTURE_FIRST_LEVEL
,
105 DRAW_JIT_TEXTURE_LAST_LEVEL
,
106 DRAW_JIT_TEXTURE_MIP_OFFSETS
,
107 DRAW_JIT_TEXTURE_NUM_FIELDS
/* number of fields above */
112 DRAW_JIT_SAMPLER_MIN_LOD
,
113 DRAW_JIT_SAMPLER_MAX_LOD
,
114 DRAW_JIT_SAMPLER_LOD_BIAS
,
115 DRAW_JIT_SAMPLER_BORDER_COLOR
,
116 DRAW_JIT_SAMPLER_NUM_FIELDS
/* number of fields above */
121 DRAW_JIT_VERTEX_VERTEX_ID
= 0,
122 DRAW_JIT_VERTEX_CLIP_POS
,
127 DRAW_JIT_IMAGE_WIDTH
= 0,
128 DRAW_JIT_IMAGE_HEIGHT
,
129 DRAW_JIT_IMAGE_DEPTH
,
131 DRAW_JIT_IMAGE_ROW_STRIDE
,
132 DRAW_JIT_IMAGE_IMG_STRIDE
,
133 DRAW_JIT_IMAGE_NUM_FIELDS
/* number of fields above */
137 * This structure is passed directly to the generated vertex shader.
139 * It contains the derived state.
141 * Changes here must be reflected in the draw_jit_context_* macros.
142 * Changes to the ordering should be avoided.
144 * Only use types with a clear size and padding here, in particular prefer the
145 * stdint.h types to the basic integer types.
147 struct draw_jit_context
149 const float *vs_constants
[LP_MAX_TGSI_CONST_BUFFERS
];
150 int num_vs_constants
[LP_MAX_TGSI_CONST_BUFFERS
];
151 float (*planes
) [DRAW_TOTAL_CLIP_PLANES
][4];
152 struct pipe_viewport_state
*viewports
;
154 struct draw_jit_texture textures
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
155 struct draw_jit_sampler samplers
[PIPE_MAX_SAMPLERS
];
156 struct draw_jit_image images
[PIPE_MAX_SHADER_IMAGES
];
158 const uint32_t *vs_ssbos
[LP_MAX_TGSI_SHADER_BUFFERS
];
159 int num_vs_ssbos
[LP_MAX_TGSI_SHADER_BUFFERS
];
163 DRAW_JIT_CTX_CONSTANTS
= 0,
164 DRAW_JIT_CTX_NUM_CONSTANTS
= 1,
165 DRAW_JIT_CTX_PLANES
= 2,
166 DRAW_JIT_CTX_VIEWPORT
= 3,
167 DRAW_JIT_CTX_TEXTURES
= 4,
168 DRAW_JIT_CTX_SAMPLERS
= 5,
169 DRAW_JIT_CTX_IMAGES
= 6,
170 DRAW_JIT_CTX_SSBOS
= 7,
171 DRAW_JIT_CTX_NUM_SSBOS
= 8,
172 DRAW_JIT_CTX_NUM_FIELDS
175 #define draw_jit_context_vs_constants(_gallivm, _ptr) \
176 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_CONSTANTS, "vs_constants")
178 #define draw_jit_context_num_vs_constants(_gallivm, _ptr) \
179 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_NUM_CONSTANTS, "num_vs_constants")
181 #define draw_jit_context_planes(_gallivm, _ptr) \
182 lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_CTX_PLANES, "planes")
184 #define draw_jit_context_viewports(_gallivm, _ptr) \
185 lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_CTX_VIEWPORT, "viewports")
187 #define draw_jit_context_textures(_gallivm, _ptr) \
188 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_TEXTURES, "textures")
190 #define draw_jit_context_samplers(_gallivm, _ptr) \
191 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_SAMPLERS, "samplers")
193 #define draw_jit_context_images(_gallivm, _ptr) \
194 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_IMAGES, "images")
196 #define draw_jit_context_vs_ssbos(_gallivm, _ptr) \
197 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_SSBOS, "vs_ssbos")
199 #define draw_jit_context_num_vs_ssbos(_gallivm, _ptr) \
200 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_NUM_SSBOS, "num_vs_ssbos")
203 #define draw_jit_header_id(_gallivm, _ptr) \
204 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_VERTEX_ID, "id")
206 #define draw_jit_header_clip_pos(_gallivm, _ptr) \
207 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_CLIP_POS, "clip_pos")
209 #define draw_jit_header_data(_gallivm, _ptr) \
210 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_DATA, "data")
213 #define draw_jit_vbuffer_stride(_gallivm, _ptr) \
214 lp_build_struct_get(_gallivm, _ptr, 0, "stride")
216 #define draw_jit_vbuffer_offset(_gallivm, _ptr) \
217 lp_build_struct_get(_gallivm, _ptr, 2, "buffer_offset")
220 DRAW_JIT_DVBUFFER_MAP
= 0,
221 DRAW_JIT_DVBUFFER_SIZE
,
222 DRAW_JIT_DVBUFFER_NUM_FIELDS
/* number of fields above */
225 #define draw_jit_dvbuffer_map(_gallivm, _ptr) \
226 lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_DVBUFFER_MAP, "map")
228 #define draw_jit_dvbuffer_size(_gallivm, _ptr) \
229 lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_DVBUFFER_SIZE, "size")
233 * This structure is passed directly to the generated geometry shader.
235 * It contains the derived state.
237 * Changes here must be reflected in the draw_gs_jit_context_* macros.
238 * Changes to the ordering should be avoided.
240 * Only use types with a clear size and padding here, in particular prefer the
241 * stdint.h types to the basic integer types.
243 struct draw_gs_jit_context
245 const float *constants
[LP_MAX_TGSI_CONST_BUFFERS
];
246 int num_constants
[LP_MAX_TGSI_CONST_BUFFERS
];
247 float (*planes
) [DRAW_TOTAL_CLIP_PLANES
][4];
248 struct pipe_viewport_state
*viewports
;
250 /* There two need to be exactly at DRAW_JIT_CTX_TEXTURES and
251 * DRAW_JIT_CTX_SAMPLERS positions in the struct */
252 struct draw_jit_texture textures
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
253 struct draw_jit_sampler samplers
[PIPE_MAX_SAMPLERS
];
254 struct draw_jit_image images
[PIPE_MAX_SHADER_IMAGES
];
257 int *emitted_vertices
;
259 const uint32_t *ssbos
[LP_MAX_TGSI_SHADER_BUFFERS
];
260 int num_ssbos
[LP_MAX_TGSI_SHADER_BUFFERS
];
265 DRAW_GS_JIT_CTX_CONSTANTS
= 0,
266 DRAW_GS_JIT_CTX_NUM_CONSTANTS
= 1,
267 DRAW_GS_JIT_CTX_PLANES
= 2,
268 DRAW_GS_JIT_CTX_VIEWPORT
= 3,
269 /* Textures and samples are reserved for DRAW_JIT_CTX_TEXTURES
270 * and DRAW_JIT_CTX_SAMPLERS, because they both need
271 * to be at exactly the same locations as they are in the
272 * VS ctx structure for sampling to work. */
273 DRAW_GS_JIT_CTX_TEXTURES
= DRAW_JIT_CTX_TEXTURES
,
274 DRAW_GS_JIT_CTX_SAMPLERS
= DRAW_JIT_CTX_SAMPLERS
,
275 DRAW_GS_JIT_CTX_IMAGES
= DRAW_JIT_CTX_IMAGES
,
276 DRAW_GS_JIT_CTX_PRIM_LENGTHS
= 7,
277 DRAW_GS_JIT_CTX_EMITTED_VERTICES
= 8,
278 DRAW_GS_JIT_CTX_EMITTED_PRIMS
= 9,
279 DRAW_GS_JIT_CTX_SSBOS
= 10,
280 DRAW_GS_JIT_CTX_NUM_SSBOS
= 11,
281 DRAW_GS_JIT_CTX_NUM_FIELDS
= 12
284 #define draw_gs_jit_context_constants(_gallivm, _ptr) \
285 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_CONSTANTS, "constants")
287 #define draw_gs_jit_context_num_constants(_gallivm, _ptr) \
288 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_NUM_CONSTANTS, "num_constants")
290 #define draw_gs_jit_context_planes(_gallivm, _ptr) \
291 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_PLANES, "planes")
293 #define draw_gs_jit_context_viewports(_gallivm, _ptr) \
294 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_VIEWPORT, "viewports")
296 #define draw_gs_jit_context_textures(_gallivm, _ptr) \
297 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_TEXTURES, "textures")
299 #define draw_gs_jit_context_samplers(_gallivm, _ptr) \
300 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_SAMPLERS, "samplers")
302 #define draw_gs_jit_context_images(_gallivm, _ptr) \
303 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_IMAGES, "images")
305 #define draw_gs_jit_prim_lengths(_gallivm, _ptr) \
306 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_PRIM_LENGTHS, "prim_lengths")
308 #define draw_gs_jit_emitted_vertices(_gallivm, _ptr) \
309 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_EMITTED_VERTICES, "emitted_vertices")
311 #define draw_gs_jit_emitted_prims(_gallivm, _ptr) \
312 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_EMITTED_PRIMS, "emitted_prims")
314 #define draw_gs_jit_context_ssbos(_gallivm, _ptr) \
315 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_SSBOS, "ssbos")
317 #define draw_gs_jit_context_num_ssbos(_gallivm, _ptr) \
318 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_NUM_SSBOS, "num_ssbos")
321 (*draw_jit_vert_func
)(struct draw_jit_context
*context
,
322 struct vertex_header
*io
,
323 const struct draw_vertex_buffer vbuffers
[PIPE_MAX_ATTRIBS
],
325 unsigned start_or_maxelt
,
327 struct pipe_vertex_buffer
*vertex_buffers
,
328 unsigned instance_id
,
329 unsigned vertex_id_offset
,
330 unsigned start_instance
,
331 const unsigned *fetch_elts
,
336 (*draw_gs_jit_func
)(struct draw_gs_jit_context
*context
,
337 float inputs
[6][PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
][TGSI_NUM_CHANNELS
],
338 struct vertex_header
**output
,
340 unsigned instance_id
,
342 unsigned invocation_id
);
344 struct draw_llvm_variant_key
346 unsigned nr_vertex_elements
:8;
347 unsigned nr_samplers
:8;
348 unsigned nr_sampler_views
:8;
349 unsigned nr_images
:8;
350 unsigned clamp_vertex_color
:1;
353 unsigned clip_user
:1;
354 unsigned clip_halfz
:1;
355 unsigned bypass_viewport
:1;
356 unsigned need_edgeflags
:1;
358 unsigned num_outputs
:8;
359 unsigned ucp_enable
:PIPE_MAX_CLIP_PLANES
;
360 /* note padding here - must use memset */
362 /* Variable number of vertex elements:
364 struct pipe_vertex_element vertex_element
[1];
366 /* Followed by variable number of samplers:
368 /* struct draw_sampler_static_state sampler; */
369 /* Followed by variable number of images
373 struct draw_gs_llvm_variant_key
375 unsigned nr_samplers
:8;
376 unsigned nr_sampler_views
:8;
377 unsigned nr_images
:8;
378 unsigned num_outputs
:8;
379 /* note padding here - must use memset */
381 struct draw_sampler_static_state samplers
[1];
382 /* Followed by variable number of images.*/
385 #define DRAW_LLVM_MAX_VARIANT_KEY_SIZE \
386 (sizeof(struct draw_llvm_variant_key) + \
387 PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state) + \
388 PIPE_MAX_SHADER_IMAGES * sizeof(struct draw_image_static_state) + \
389 (PIPE_MAX_ATTRIBS-1) * sizeof(struct pipe_vertex_element))
391 #define DRAW_GS_LLVM_MAX_VARIANT_KEY_SIZE \
392 (sizeof(struct draw_gs_llvm_variant_key) + \
393 PIPE_MAX_SHADER_IMAGES * sizeof(struct draw_image_static_state) + \
394 PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state))
398 draw_llvm_variant_key_size(unsigned nr_vertex_elements
,
399 unsigned nr_samplers
, unsigned nr_images
)
401 return (sizeof(struct draw_llvm_variant_key
) +
402 nr_samplers
* sizeof(struct draw_sampler_static_state
) +
403 nr_images
* sizeof(struct draw_image_static_state
) +
404 (nr_vertex_elements
- 1) * sizeof(struct pipe_vertex_element
));
409 draw_gs_llvm_variant_key_size(unsigned nr_samplers
, unsigned nr_images
)
411 return (sizeof(struct draw_gs_llvm_variant_key
) +
412 (nr_images
) * sizeof(struct draw_sampler_static_state
) +
413 (nr_samplers
- 1) * sizeof(struct draw_sampler_static_state
));
417 static inline struct draw_sampler_static_state
*
418 draw_llvm_variant_key_samplers(struct draw_llvm_variant_key
*key
)
420 return (struct draw_sampler_static_state
*)
421 &key
->vertex_element
[key
->nr_vertex_elements
];
424 static inline struct draw_image_static_state
*
425 draw_llvm_variant_key_images(struct draw_llvm_variant_key
*key
)
427 struct draw_sampler_static_state
*samplers
= (struct draw_sampler_static_state
*)
428 (&key
->vertex_element
[key
->nr_vertex_elements
]);
429 return (struct draw_image_static_state
*)
430 &samplers
[key
->nr_samplers
];
433 static inline struct draw_image_static_state
*
434 draw_gs_llvm_variant_key_images(struct draw_gs_llvm_variant_key
*key
)
436 return (struct draw_image_static_state
*)
437 &key
->samplers
[key
->nr_samplers
];
440 struct draw_llvm_variant_list_item
442 struct draw_llvm_variant
*base
;
443 struct draw_llvm_variant_list_item
*next
, *prev
;
446 struct draw_gs_llvm_variant_list_item
448 struct draw_gs_llvm_variant
*base
;
449 struct draw_gs_llvm_variant_list_item
*next
, *prev
;
453 struct draw_llvm_variant
455 struct gallivm_state
*gallivm
;
457 /* LLVM JIT builder types */
458 LLVMTypeRef context_ptr_type
;
459 LLVMTypeRef buffer_ptr_type
;
460 LLVMTypeRef vb_ptr_type
;
461 LLVMTypeRef vertex_header_ptr_type
;
463 LLVMValueRef function
;
464 draw_jit_vert_func jit_func
;
466 struct llvm_vertex_shader
*shader
;
468 struct draw_llvm
*llvm
;
469 struct draw_llvm_variant_list_item list_item_global
;
470 struct draw_llvm_variant_list_item list_item_local
;
472 /* key is variable-sized, must be last */
473 struct draw_llvm_variant_key key
;
477 struct draw_gs_llvm_variant
479 struct gallivm_state
*gallivm
;
481 /* LLVM JIT builder types */
482 LLVMTypeRef context_ptr_type
;
483 LLVMTypeRef vertex_header_ptr_type
;
484 LLVMTypeRef input_array_type
;
486 LLVMValueRef context_ptr
;
488 LLVMValueRef num_prims
;
489 LLVMValueRef function
;
490 draw_gs_jit_func jit_func
;
492 struct llvm_geometry_shader
*shader
;
494 struct draw_llvm
*llvm
;
495 struct draw_gs_llvm_variant_list_item list_item_global
;
496 struct draw_gs_llvm_variant_list_item list_item_local
;
498 /* key is variable-sized, must be last */
499 struct draw_gs_llvm_variant_key key
;
502 struct llvm_vertex_shader
{
503 struct draw_vertex_shader base
;
505 unsigned variant_key_size
;
506 struct draw_llvm_variant_list_item variants
;
507 unsigned variants_created
;
508 unsigned variants_cached
;
511 struct llvm_geometry_shader
{
512 struct draw_geometry_shader base
;
514 unsigned variant_key_size
;
515 struct draw_gs_llvm_variant_list_item variants
;
516 unsigned variants_created
;
517 unsigned variants_cached
;
522 struct draw_context
*draw
;
524 LLVMContextRef context
;
525 boolean context_owned
;
527 struct draw_jit_context jit_context
;
528 struct draw_gs_jit_context gs_jit_context
;
530 struct draw_llvm_variant_list_item vs_variants_list
;
533 struct draw_gs_llvm_variant_list_item gs_variants_list
;
538 static inline struct llvm_vertex_shader
*
539 llvm_vertex_shader(struct draw_vertex_shader
*vs
)
541 return (struct llvm_vertex_shader
*)vs
;
544 static inline struct llvm_geometry_shader
*
545 llvm_geometry_shader(struct draw_geometry_shader
*gs
)
547 return (struct llvm_geometry_shader
*)gs
;
554 draw_llvm_create(struct draw_context
*draw
, LLVMContextRef llvm_context
);
557 draw_llvm_destroy(struct draw_llvm
*llvm
);
559 struct draw_llvm_variant
*
560 draw_llvm_create_variant(struct draw_llvm
*llvm
,
561 unsigned num_vertex_header_attribs
,
562 const struct draw_llvm_variant_key
*key
);
565 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
);
567 struct draw_llvm_variant_key
*
568 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
);
571 draw_llvm_dump_variant_key(struct draw_llvm_variant_key
*key
);
574 struct draw_gs_llvm_variant
*
575 draw_gs_llvm_create_variant(struct draw_llvm
*llvm
,
576 unsigned num_vertex_header_attribs
,
577 const struct draw_gs_llvm_variant_key
*key
);
580 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant
*variant
);
582 struct draw_gs_llvm_variant_key
*
583 draw_gs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
);
586 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key
*key
);
588 struct lp_build_sampler_soa
*
589 draw_llvm_sampler_soa_create(const struct draw_sampler_static_state
*static_state
);
591 struct lp_build_image_soa
*
592 draw_llvm_image_soa_create(const struct draw_image_static_state
*static_state
);
595 draw_llvm_set_sampler_state(struct draw_context
*draw
,
596 enum pipe_shader_type shader_stage
);
599 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
600 enum pipe_shader_type shader_stage
,
602 uint32_t width
, uint32_t height
, uint32_t depth
,
603 uint32_t first_level
, uint32_t last_level
,
604 const void *base_ptr
,
605 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
606 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
607 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
]);
610 draw_llvm_set_mapped_image(struct draw_context
*draw
,
611 enum pipe_shader_type shader_stage
,
613 uint32_t width
, uint32_t height
, uint32_t depth
,
614 const void *base_ptr
,
616 uint32_t img_stride
);