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
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
];
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
;
73 struct draw_jit_sampler
78 float border_color
[4];
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 */
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 */
106 DRAW_JIT_VERTEX_VERTEX_ID
= 0,
107 DRAW_JIT_VERTEX_CLIP
,
108 DRAW_JIT_VERTEX_PRE_CLIP_POS
,
113 * This structure is passed directly to the generated vertex shader.
115 * It contains the derived state.
117 * Changes here must be reflected in the draw_jit_context_* macros.
118 * Changes to the ordering should be avoided.
120 * Only use types with a clear size and padding here, in particular prefer the
121 * stdint.h types to the basic integer types.
123 struct draw_jit_context
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 struct pipe_viewport_state
*viewports
;
130 struct draw_jit_texture textures
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
131 struct draw_jit_sampler samplers
[PIPE_MAX_SAMPLERS
];
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
144 #define draw_jit_context_vs_constants(_gallivm, _ptr) \
145 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_CONSTANTS, "vs_constants")
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")
150 #define draw_jit_context_planes(_gallivm, _ptr) \
151 lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_CTX_PLANES, "planes")
153 #define draw_jit_context_viewports(_gallivm, _ptr) \
154 lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_CTX_VIEWPORT, "viewports")
156 #define draw_jit_context_textures(_gallivm, _ptr) \
157 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_TEXTURES, "textures")
159 #define draw_jit_context_samplers(_gallivm, _ptr) \
160 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_SAMPLERS, "samplers")
162 #define draw_jit_header_id(_gallivm, _ptr) \
163 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_VERTEX_ID, "id")
165 #define draw_jit_header_clip(_gallivm, _ptr) \
166 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_CLIP, "clip")
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")
171 #define draw_jit_header_data(_gallivm, _ptr) \
172 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_DATA, "data")
175 #define draw_jit_vbuffer_stride(_gallivm, _ptr) \
176 lp_build_struct_get(_gallivm, _ptr, 0, "stride")
178 #define draw_jit_vbuffer_offset(_gallivm, _ptr) \
179 lp_build_struct_get(_gallivm, _ptr, 1, "buffer_offset")
182 DRAW_JIT_DVBUFFER_MAP
= 0,
183 DRAW_JIT_DVBUFFER_SIZE
,
184 DRAW_JIT_DVBUFFER_NUM_FIELDS
/* number of fields above */
187 #define draw_jit_dvbuffer_map(_gallivm, _ptr) \
188 lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_DVBUFFER_MAP, "map")
190 #define draw_jit_dvbuffer_size(_gallivm, _ptr) \
191 lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_DVBUFFER_SIZE, "size")
195 * This structure is passed directly to the generated geometry shader.
197 * It contains the derived state.
199 * Changes here must be reflected in the draw_gs_jit_context_* macros.
200 * Changes to the ordering should be avoided.
202 * Only use types with a clear size and padding here, in particular prefer the
203 * stdint.h types to the basic integer types.
205 struct draw_gs_jit_context
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 struct pipe_viewport_state
*viewports
;
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
];
218 int *emitted_vertices
;
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
239 #define draw_gs_jit_context_constants(_gallivm, _ptr) \
240 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_CONSTANTS, "constants")
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")
245 #define draw_gs_jit_context_planes(_gallivm, _ptr) \
246 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_PLANES, "planes")
248 #define draw_gs_jit_context_viewports(_gallivm, _ptr) \
249 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_VIEWPORT, "viewports")
251 #define draw_gs_jit_context_textures(_gallivm, _ptr) \
252 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_TEXTURES, "textures")
254 #define draw_gs_jit_context_samplers(_gallivm, _ptr) \
255 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_SAMPLERS, "samplers")
257 #define draw_gs_jit_prim_lengths(_gallivm, _ptr) \
258 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_PRIM_LENGTHS, "prim_lengths")
260 #define draw_gs_jit_emitted_vertices(_gallivm, _ptr) \
261 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_EMITTED_VERTICES, "emitted_vertices")
263 #define draw_gs_jit_emitted_prims(_gallivm, _ptr) \
264 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_EMITTED_PRIMS, "emitted_prims")
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
],
275 struct pipe_vertex_buffer
*vertex_buffers
,
276 unsigned instance_id
,
277 unsigned vertex_id_offset
,
278 unsigned start_instance
);
282 (*draw_jit_vert_func_elts
)(struct draw_jit_context
*context
,
283 struct vertex_header
*io
,
284 const struct draw_vertex_buffer vbuffers
[PIPE_MAX_ATTRIBS
],
285 const unsigned *fetch_elts
,
286 unsigned fetch_max_elt
,
287 unsigned fetch_count
,
289 struct pipe_vertex_buffer
*vertex_buffers
,
290 unsigned instance_id
,
291 unsigned vertex_id_offset
,
292 unsigned start_instance
);
296 (*draw_gs_jit_func
)(struct draw_gs_jit_context
*context
,
297 float inputs
[6][PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
][TGSI_NUM_CHANNELS
],
298 struct vertex_header
*output
,
300 unsigned instance_id
,
303 struct draw_llvm_variant_key
305 unsigned nr_vertex_elements
:8;
306 unsigned nr_samplers
:8;
307 unsigned nr_sampler_views
:8;
308 unsigned clamp_vertex_color
:1;
311 unsigned clip_user
:1;
312 unsigned clip_halfz
:1;
313 unsigned bypass_viewport
:1;
314 unsigned need_edgeflags
:1;
316 unsigned num_outputs
:8;
318 * it is important there are no holes in this struct
319 * (and all padding gets zeroed).
321 unsigned ucp_enable
:PIPE_MAX_CLIP_PLANES
;
322 unsigned pad1
:24-PIPE_MAX_CLIP_PLANES
;
324 /* Variable number of vertex elements:
326 struct pipe_vertex_element vertex_element
[1];
328 /* Followed by variable number of samplers:
330 /* struct draw_sampler_static_state sampler; */
333 struct draw_gs_llvm_variant_key
335 unsigned nr_samplers
:8;
336 unsigned nr_sampler_views
:8;
337 unsigned num_outputs
:8;
339 struct draw_sampler_static_state samplers
[1];
342 #define DRAW_LLVM_MAX_VARIANT_KEY_SIZE \
343 (sizeof(struct draw_llvm_variant_key) + \
344 PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state) + \
345 (PIPE_MAX_ATTRIBS-1) * sizeof(struct pipe_vertex_element))
347 #define DRAW_GS_LLVM_MAX_VARIANT_KEY_SIZE \
348 (sizeof(struct draw_gs_llvm_variant_key) + \
349 PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state))
353 draw_llvm_variant_key_size(unsigned nr_vertex_elements
,
354 unsigned nr_samplers
)
356 return (sizeof(struct draw_llvm_variant_key
) +
357 nr_samplers
* sizeof(struct draw_sampler_static_state
) +
358 (nr_vertex_elements
- 1) * sizeof(struct pipe_vertex_element
));
363 draw_gs_llvm_variant_key_size(unsigned nr_samplers
)
365 return (sizeof(struct draw_gs_llvm_variant_key
) +
366 (nr_samplers
- 1) * sizeof(struct draw_sampler_static_state
));
370 static INLINE
struct draw_sampler_static_state
*
371 draw_llvm_variant_key_samplers(struct draw_llvm_variant_key
*key
)
373 return (struct draw_sampler_static_state
*)
374 &key
->vertex_element
[key
->nr_vertex_elements
];
378 struct draw_llvm_variant_list_item
380 struct draw_llvm_variant
*base
;
381 struct draw_llvm_variant_list_item
*next
, *prev
;
384 struct draw_gs_llvm_variant_list_item
386 struct draw_gs_llvm_variant
*base
;
387 struct draw_gs_llvm_variant_list_item
*next
, *prev
;
391 struct draw_llvm_variant
393 struct gallivm_state
*gallivm
;
395 /* LLVM JIT builder types */
396 LLVMTypeRef context_ptr_type
;
397 LLVMTypeRef buffer_ptr_type
;
398 LLVMTypeRef vb_ptr_type
;
399 LLVMTypeRef vertex_header_ptr_type
;
401 LLVMValueRef function
;
402 LLVMValueRef function_elts
;
403 draw_jit_vert_func jit_func
;
404 draw_jit_vert_func_elts jit_func_elts
;
406 struct llvm_vertex_shader
*shader
;
408 struct draw_llvm
*llvm
;
409 struct draw_llvm_variant_list_item list_item_global
;
410 struct draw_llvm_variant_list_item list_item_local
;
412 /* key is variable-sized, must be last */
413 struct draw_llvm_variant_key key
;
417 struct draw_gs_llvm_variant
419 struct gallivm_state
*gallivm
;
421 /* LLVM JIT builder types */
422 LLVMTypeRef context_ptr_type
;
423 LLVMTypeRef vertex_header_ptr_type
;
424 LLVMTypeRef input_array_type
;
426 LLVMValueRef context_ptr
;
428 LLVMValueRef num_prims
;
429 LLVMValueRef function
;
430 draw_gs_jit_func jit_func
;
432 struct llvm_geometry_shader
*shader
;
434 struct draw_llvm
*llvm
;
435 struct draw_gs_llvm_variant_list_item list_item_global
;
436 struct draw_gs_llvm_variant_list_item list_item_local
;
438 /* key is variable-sized, must be last */
439 struct draw_gs_llvm_variant_key key
;
442 struct llvm_vertex_shader
{
443 struct draw_vertex_shader base
;
445 unsigned variant_key_size
;
446 struct draw_llvm_variant_list_item variants
;
447 unsigned variants_created
;
448 unsigned variants_cached
;
451 struct llvm_geometry_shader
{
452 struct draw_geometry_shader base
;
454 unsigned variant_key_size
;
455 struct draw_gs_llvm_variant_list_item variants
;
456 unsigned variants_created
;
457 unsigned variants_cached
;
462 struct draw_context
*draw
;
464 LLVMContextRef context
;
465 boolean context_owned
;
467 struct draw_jit_context jit_context
;
468 struct draw_gs_jit_context gs_jit_context
;
470 struct draw_llvm_variant_list_item vs_variants_list
;
473 struct draw_gs_llvm_variant_list_item gs_variants_list
;
478 static INLINE
struct llvm_vertex_shader
*
479 llvm_vertex_shader(struct draw_vertex_shader
*vs
)
481 return (struct llvm_vertex_shader
*)vs
;
484 static INLINE
struct llvm_geometry_shader
*
485 llvm_geometry_shader(struct draw_geometry_shader
*gs
)
487 return (struct llvm_geometry_shader
*)gs
;
494 draw_llvm_create(struct draw_context
*draw
, LLVMContextRef llvm_context
);
497 draw_llvm_destroy(struct draw_llvm
*llvm
);
499 struct draw_llvm_variant
*
500 draw_llvm_create_variant(struct draw_llvm
*llvm
,
501 unsigned num_vertex_header_attribs
,
502 const struct draw_llvm_variant_key
*key
);
505 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
);
507 struct draw_llvm_variant_key
*
508 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
);
511 draw_llvm_dump_variant_key(struct draw_llvm_variant_key
*key
);
514 struct draw_gs_llvm_variant
*
515 draw_gs_llvm_create_variant(struct draw_llvm
*llvm
,
516 unsigned num_vertex_header_attribs
,
517 const struct draw_gs_llvm_variant_key
*key
);
520 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant
*variant
);
522 struct draw_gs_llvm_variant_key
*
523 draw_gs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
);
526 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key
*key
);
528 struct lp_build_sampler_soa
*
529 draw_llvm_sampler_soa_create(const struct draw_sampler_static_state
*static_state
);
532 draw_llvm_set_sampler_state(struct draw_context
*draw
, unsigned shader_stage
);
535 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
536 unsigned shader_stage
,
538 uint32_t width
, uint32_t height
, uint32_t depth
,
539 uint32_t first_level
, uint32_t last_level
,
540 const void *base_ptr
,
541 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
542 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
543 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
]);