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
);
335 (*draw_gs_jit_func
)(struct draw_gs_jit_context
*context
,
336 float inputs
[6][PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
][TGSI_NUM_CHANNELS
],
337 struct vertex_header
*output
,
339 unsigned instance_id
,
341 unsigned invocation_id
);
343 struct draw_llvm_variant_key
345 unsigned nr_vertex_elements
:8;
346 unsigned nr_samplers
:8;
347 unsigned nr_sampler_views
:8;
348 unsigned nr_images
:8;
349 unsigned clamp_vertex_color
:1;
352 unsigned clip_user
:1;
353 unsigned clip_halfz
:1;
354 unsigned bypass_viewport
:1;
355 unsigned need_edgeflags
:1;
357 unsigned num_outputs
:8;
358 unsigned ucp_enable
:PIPE_MAX_CLIP_PLANES
;
359 /* note padding here - must use memset */
361 /* Variable number of vertex elements:
363 struct pipe_vertex_element vertex_element
[1];
365 /* Followed by variable number of samplers:
367 /* struct draw_sampler_static_state sampler; */
368 /* Followed by variable number of images
372 struct draw_gs_llvm_variant_key
374 unsigned nr_samplers
:8;
375 unsigned nr_sampler_views
:8;
376 unsigned nr_images
:8;
377 unsigned num_outputs
:8;
378 /* note padding here - must use memset */
380 struct draw_sampler_static_state samplers
[1];
381 /* Followed by variable number of images.*/
384 #define DRAW_LLVM_MAX_VARIANT_KEY_SIZE \
385 (sizeof(struct draw_llvm_variant_key) + \
386 PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state) + \
387 PIPE_MAX_SHADER_IMAGES * sizeof(struct draw_image_static_state) + \
388 (PIPE_MAX_ATTRIBS-1) * sizeof(struct pipe_vertex_element))
390 #define DRAW_GS_LLVM_MAX_VARIANT_KEY_SIZE \
391 (sizeof(struct draw_gs_llvm_variant_key) + \
392 PIPE_MAX_SHADER_IMAGES * sizeof(struct draw_image_static_state) + \
393 PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state))
397 draw_llvm_variant_key_size(unsigned nr_vertex_elements
,
398 unsigned nr_samplers
, unsigned nr_images
)
400 return (sizeof(struct draw_llvm_variant_key
) +
401 nr_samplers
* sizeof(struct draw_sampler_static_state
) +
402 nr_images
* sizeof(struct draw_image_static_state
) +
403 (nr_vertex_elements
- 1) * sizeof(struct pipe_vertex_element
));
408 draw_gs_llvm_variant_key_size(unsigned nr_samplers
, unsigned nr_images
)
410 return (sizeof(struct draw_gs_llvm_variant_key
) +
411 (nr_images
) * sizeof(struct draw_sampler_static_state
) +
412 (nr_samplers
- 1) * sizeof(struct draw_sampler_static_state
));
416 static inline struct draw_sampler_static_state
*
417 draw_llvm_variant_key_samplers(struct draw_llvm_variant_key
*key
)
419 return (struct draw_sampler_static_state
*)
420 &key
->vertex_element
[key
->nr_vertex_elements
];
423 static inline struct draw_image_static_state
*
424 draw_llvm_variant_key_images(struct draw_llvm_variant_key
*key
)
426 struct draw_sampler_static_state
*samplers
= (struct draw_sampler_static_state
*)
427 (&key
->vertex_element
[key
->nr_vertex_elements
]);
428 return (struct draw_image_static_state
*)
429 &samplers
[key
->nr_samplers
];
432 static inline struct draw_image_static_state
*
433 draw_gs_llvm_variant_key_images(struct draw_gs_llvm_variant_key
*key
)
435 return (struct draw_image_static_state
*)
436 &key
->samplers
[key
->nr_samplers
];
439 struct draw_llvm_variant_list_item
441 struct draw_llvm_variant
*base
;
442 struct draw_llvm_variant_list_item
*next
, *prev
;
445 struct draw_gs_llvm_variant_list_item
447 struct draw_gs_llvm_variant
*base
;
448 struct draw_gs_llvm_variant_list_item
*next
, *prev
;
452 struct draw_llvm_variant
454 struct gallivm_state
*gallivm
;
456 /* LLVM JIT builder types */
457 LLVMTypeRef context_ptr_type
;
458 LLVMTypeRef buffer_ptr_type
;
459 LLVMTypeRef vb_ptr_type
;
460 LLVMTypeRef vertex_header_ptr_type
;
462 LLVMValueRef function
;
463 draw_jit_vert_func jit_func
;
465 struct llvm_vertex_shader
*shader
;
467 struct draw_llvm
*llvm
;
468 struct draw_llvm_variant_list_item list_item_global
;
469 struct draw_llvm_variant_list_item list_item_local
;
471 /* key is variable-sized, must be last */
472 struct draw_llvm_variant_key key
;
476 struct draw_gs_llvm_variant
478 struct gallivm_state
*gallivm
;
480 /* LLVM JIT builder types */
481 LLVMTypeRef context_ptr_type
;
482 LLVMTypeRef vertex_header_ptr_type
;
483 LLVMTypeRef input_array_type
;
485 LLVMValueRef context_ptr
;
487 LLVMValueRef num_prims
;
488 LLVMValueRef function
;
489 draw_gs_jit_func jit_func
;
491 struct llvm_geometry_shader
*shader
;
493 struct draw_llvm
*llvm
;
494 struct draw_gs_llvm_variant_list_item list_item_global
;
495 struct draw_gs_llvm_variant_list_item list_item_local
;
497 /* key is variable-sized, must be last */
498 struct draw_gs_llvm_variant_key key
;
501 struct llvm_vertex_shader
{
502 struct draw_vertex_shader base
;
504 unsigned variant_key_size
;
505 struct draw_llvm_variant_list_item variants
;
506 unsigned variants_created
;
507 unsigned variants_cached
;
510 struct llvm_geometry_shader
{
511 struct draw_geometry_shader base
;
513 unsigned variant_key_size
;
514 struct draw_gs_llvm_variant_list_item variants
;
515 unsigned variants_created
;
516 unsigned variants_cached
;
521 struct draw_context
*draw
;
523 LLVMContextRef context
;
524 boolean context_owned
;
526 struct draw_jit_context jit_context
;
527 struct draw_gs_jit_context gs_jit_context
;
529 struct draw_llvm_variant_list_item vs_variants_list
;
532 struct draw_gs_llvm_variant_list_item gs_variants_list
;
537 static inline struct llvm_vertex_shader
*
538 llvm_vertex_shader(struct draw_vertex_shader
*vs
)
540 return (struct llvm_vertex_shader
*)vs
;
543 static inline struct llvm_geometry_shader
*
544 llvm_geometry_shader(struct draw_geometry_shader
*gs
)
546 return (struct llvm_geometry_shader
*)gs
;
553 draw_llvm_create(struct draw_context
*draw
, LLVMContextRef llvm_context
);
556 draw_llvm_destroy(struct draw_llvm
*llvm
);
558 struct draw_llvm_variant
*
559 draw_llvm_create_variant(struct draw_llvm
*llvm
,
560 unsigned num_vertex_header_attribs
,
561 const struct draw_llvm_variant_key
*key
);
564 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
);
566 struct draw_llvm_variant_key
*
567 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
);
570 draw_llvm_dump_variant_key(struct draw_llvm_variant_key
*key
);
573 struct draw_gs_llvm_variant
*
574 draw_gs_llvm_create_variant(struct draw_llvm
*llvm
,
575 unsigned num_vertex_header_attribs
,
576 const struct draw_gs_llvm_variant_key
*key
);
579 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant
*variant
);
581 struct draw_gs_llvm_variant_key
*
582 draw_gs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
);
585 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key
*key
);
587 struct lp_build_sampler_soa
*
588 draw_llvm_sampler_soa_create(const struct draw_sampler_static_state
*static_state
);
590 struct lp_build_image_soa
*
591 draw_llvm_image_soa_create(const struct draw_image_static_state
*static_state
);
594 draw_llvm_set_sampler_state(struct draw_context
*draw
,
595 enum pipe_shader_type shader_stage
);
598 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
599 enum pipe_shader_type shader_stage
,
601 uint32_t width
, uint32_t height
, uint32_t depth
,
602 uint32_t first_level
, uint32_t last_level
,
603 const void *base_ptr
,
604 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
605 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
606 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
]);
609 draw_llvm_set_mapped_image(struct draw_context
*draw
,
610 enum pipe_shader_type shader_stage
,
612 uint32_t width
, uint32_t height
, uint32_t depth
,
613 const void *base_ptr
,
615 uint32_t img_stride
);