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
,
302 unsigned invocation_id
);
304 struct draw_llvm_variant_key
306 unsigned nr_vertex_elements
:8;
307 unsigned nr_samplers
:8;
308 unsigned nr_sampler_views
:8;
309 unsigned clamp_vertex_color
:1;
312 unsigned clip_user
:1;
313 unsigned clip_halfz
:1;
314 unsigned bypass_viewport
:1;
315 unsigned need_edgeflags
:1;
317 unsigned num_outputs
:8;
319 * it is important there are no holes in this struct
320 * (and all padding gets zeroed).
322 unsigned ucp_enable
:PIPE_MAX_CLIP_PLANES
;
323 unsigned pad1
:24-PIPE_MAX_CLIP_PLANES
;
325 /* Variable number of vertex elements:
327 struct pipe_vertex_element vertex_element
[1];
329 /* Followed by variable number of samplers:
331 /* struct draw_sampler_static_state sampler; */
334 struct draw_gs_llvm_variant_key
336 unsigned nr_samplers
:8;
337 unsigned nr_sampler_views
:8;
338 unsigned num_outputs
:8;
340 struct draw_sampler_static_state samplers
[1];
343 #define DRAW_LLVM_MAX_VARIANT_KEY_SIZE \
344 (sizeof(struct draw_llvm_variant_key) + \
345 PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state) + \
346 (PIPE_MAX_ATTRIBS-1) * sizeof(struct pipe_vertex_element))
348 #define DRAW_GS_LLVM_MAX_VARIANT_KEY_SIZE \
349 (sizeof(struct draw_gs_llvm_variant_key) + \
350 PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state))
354 draw_llvm_variant_key_size(unsigned nr_vertex_elements
,
355 unsigned nr_samplers
)
357 return (sizeof(struct draw_llvm_variant_key
) +
358 nr_samplers
* sizeof(struct draw_sampler_static_state
) +
359 (nr_vertex_elements
- 1) * sizeof(struct pipe_vertex_element
));
364 draw_gs_llvm_variant_key_size(unsigned nr_samplers
)
366 return (sizeof(struct draw_gs_llvm_variant_key
) +
367 (nr_samplers
- 1) * sizeof(struct draw_sampler_static_state
));
371 static inline struct draw_sampler_static_state
*
372 draw_llvm_variant_key_samplers(struct draw_llvm_variant_key
*key
)
374 return (struct draw_sampler_static_state
*)
375 &key
->vertex_element
[key
->nr_vertex_elements
];
379 struct draw_llvm_variant_list_item
381 struct draw_llvm_variant
*base
;
382 struct draw_llvm_variant_list_item
*next
, *prev
;
385 struct draw_gs_llvm_variant_list_item
387 struct draw_gs_llvm_variant
*base
;
388 struct draw_gs_llvm_variant_list_item
*next
, *prev
;
392 struct draw_llvm_variant
394 struct gallivm_state
*gallivm
;
396 /* LLVM JIT builder types */
397 LLVMTypeRef context_ptr_type
;
398 LLVMTypeRef buffer_ptr_type
;
399 LLVMTypeRef vb_ptr_type
;
400 LLVMTypeRef vertex_header_ptr_type
;
402 LLVMValueRef function
;
403 LLVMValueRef function_elts
;
404 draw_jit_vert_func jit_func
;
405 draw_jit_vert_func_elts jit_func_elts
;
407 struct llvm_vertex_shader
*shader
;
409 struct draw_llvm
*llvm
;
410 struct draw_llvm_variant_list_item list_item_global
;
411 struct draw_llvm_variant_list_item list_item_local
;
413 /* key is variable-sized, must be last */
414 struct draw_llvm_variant_key key
;
418 struct draw_gs_llvm_variant
420 struct gallivm_state
*gallivm
;
422 /* LLVM JIT builder types */
423 LLVMTypeRef context_ptr_type
;
424 LLVMTypeRef vertex_header_ptr_type
;
425 LLVMTypeRef input_array_type
;
427 LLVMValueRef context_ptr
;
429 LLVMValueRef num_prims
;
430 LLVMValueRef function
;
431 draw_gs_jit_func jit_func
;
433 struct llvm_geometry_shader
*shader
;
435 struct draw_llvm
*llvm
;
436 struct draw_gs_llvm_variant_list_item list_item_global
;
437 struct draw_gs_llvm_variant_list_item list_item_local
;
439 /* key is variable-sized, must be last */
440 struct draw_gs_llvm_variant_key key
;
443 struct llvm_vertex_shader
{
444 struct draw_vertex_shader base
;
446 unsigned variant_key_size
;
447 struct draw_llvm_variant_list_item variants
;
448 unsigned variants_created
;
449 unsigned variants_cached
;
452 struct llvm_geometry_shader
{
453 struct draw_geometry_shader base
;
455 unsigned variant_key_size
;
456 struct draw_gs_llvm_variant_list_item variants
;
457 unsigned variants_created
;
458 unsigned variants_cached
;
463 struct draw_context
*draw
;
465 LLVMContextRef context
;
466 boolean context_owned
;
468 struct draw_jit_context jit_context
;
469 struct draw_gs_jit_context gs_jit_context
;
471 struct draw_llvm_variant_list_item vs_variants_list
;
474 struct draw_gs_llvm_variant_list_item gs_variants_list
;
479 static inline struct llvm_vertex_shader
*
480 llvm_vertex_shader(struct draw_vertex_shader
*vs
)
482 return (struct llvm_vertex_shader
*)vs
;
485 static inline struct llvm_geometry_shader
*
486 llvm_geometry_shader(struct draw_geometry_shader
*gs
)
488 return (struct llvm_geometry_shader
*)gs
;
495 draw_llvm_create(struct draw_context
*draw
, LLVMContextRef llvm_context
);
498 draw_llvm_destroy(struct draw_llvm
*llvm
);
500 struct draw_llvm_variant
*
501 draw_llvm_create_variant(struct draw_llvm
*llvm
,
502 unsigned num_vertex_header_attribs
,
503 const struct draw_llvm_variant_key
*key
);
506 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
);
508 struct draw_llvm_variant_key
*
509 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
);
512 draw_llvm_dump_variant_key(struct draw_llvm_variant_key
*key
);
515 struct draw_gs_llvm_variant
*
516 draw_gs_llvm_create_variant(struct draw_llvm
*llvm
,
517 unsigned num_vertex_header_attribs
,
518 const struct draw_gs_llvm_variant_key
*key
);
521 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant
*variant
);
523 struct draw_gs_llvm_variant_key
*
524 draw_gs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
);
527 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key
*key
);
529 struct lp_build_sampler_soa
*
530 draw_llvm_sampler_soa_create(const struct draw_sampler_static_state
*static_state
);
533 draw_llvm_set_sampler_state(struct draw_context
*draw
, unsigned shader_stage
);
536 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
537 unsigned shader_stage
,
539 uint32_t width
, uint32_t height
, uint32_t depth
,
540 uint32_t first_level
, uint32_t last_level
,
541 const void *base_ptr
,
542 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
543 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
544 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
]);