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/u_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 float (*planes
) [DRAW_TOTAL_CLIP_PLANES
][4];
129 struct draw_jit_texture textures
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
130 struct draw_jit_sampler samplers
[PIPE_MAX_SAMPLERS
];
134 DRAW_JIT_CTX_CONSTANTS
= 0,
135 DRAW_JIT_CTX_PLANES
= 1,
136 DRAW_JIT_CTX_VIEWPORT
= 2,
137 DRAW_JIT_CTX_TEXTURES
= 3,
138 DRAW_JIT_CTX_SAMPLERS
= 4,
139 DRAW_JIT_CTX_NUM_FIELDS
142 #define draw_jit_context_vs_constants(_gallivm, _ptr) \
143 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_CONSTANTS, "vs_constants")
145 #define draw_jit_context_planes(_gallivm, _ptr) \
146 lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_CTX_PLANES, "planes")
148 #define draw_jit_context_viewport(_gallivm, _ptr) \
149 lp_build_struct_get(_gallivm, _ptr, DRAW_JIT_CTX_VIEWPORT, "viewport")
151 #define draw_jit_context_textures(_gallivm, _ptr) \
152 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_TEXTURES, "textures")
154 #define draw_jit_context_samplers(_gallivm, _ptr) \
155 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_SAMPLERS, "samplers")
157 #define draw_jit_header_id(_gallivm, _ptr) \
158 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_VERTEX_ID, "id")
160 #define draw_jit_header_clip(_gallivm, _ptr) \
161 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_CLIP, "clip")
163 #define draw_jit_header_pre_clip_pos(_gallivm, _ptr) \
164 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_PRE_CLIP_POS, "pre_clip_pos")
166 #define draw_jit_header_data(_gallivm, _ptr) \
167 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_DATA, "data")
170 #define draw_jit_vbuffer_stride(_gallivm, _ptr) \
171 lp_build_struct_get(_gallivm, _ptr, 0, "stride")
173 #define draw_jit_vbuffer_offset(_gallivm, _ptr) \
174 lp_build_struct_get(_gallivm, _ptr, 1, "buffer_offset")
178 * This structure is passed directly to the generated geometry shader.
180 * It contains the derived state.
182 * Changes here must be reflected in the draw_gs_jit_context_* macros.
183 * Changes to the ordering should be avoided.
185 * Only use types with a clear size and padding here, in particular prefer the
186 * stdint.h types to the basic integer types.
188 struct draw_gs_jit_context
190 const float *constants
[LP_MAX_TGSI_CONST_BUFFERS
];
191 float (*planes
) [DRAW_TOTAL_CLIP_PLANES
][4];
194 /* There two need to be exactly at DRAW_JIT_CTX_TEXTURES and
195 * DRAW_JIT_CTX_SAMPLERS positions in the struct */
196 struct draw_jit_texture textures
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
197 struct draw_jit_sampler samplers
[PIPE_MAX_SAMPLERS
];
200 int *emitted_vertices
;
205 DRAW_GS_JIT_CTX_CONSTANTS
= 0,
206 DRAW_GS_JIT_CTX_PLANES
= 1,
207 DRAW_GS_JIT_CTX_VIEWPORT
= 2,
208 /* Textures and samples are reserved for DRAW_JIT_CTX_TEXTURES
209 * and DRAW_JIT_CTX_SAMPLERS, because they both need
210 * to be at exactly the same locations as they are in the
211 * VS ctx structure for sampling to work. */
212 DRAW_GS_JIT_CTX_TEXTURES
= DRAW_JIT_CTX_TEXTURES
,
213 DRAW_GS_JIT_CTX_SAMPLERS
= DRAW_JIT_CTX_SAMPLERS
,
214 DRAW_GS_JIT_CTX_PRIM_LENGTHS
= 5,
215 DRAW_GS_JIT_CTX_EMITTED_VERTICES
= 6,
216 DRAW_GS_JIT_CTX_EMITTED_PRIMS
= 7,
217 DRAW_GS_JIT_CTX_NUM_FIELDS
= 8
220 #define draw_gs_jit_context_constants(_gallivm, _ptr) \
221 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_CONSTANTS, "constants")
223 #define draw_gs_jit_context_planes(_gallivm, _ptr) \
224 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_PLANES, "planes")
226 #define draw_gs_jit_context_viewport(_gallivm, _ptr) \
227 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_VIEWPORT, "viewport")
229 #define draw_gs_jit_context_textures(_gallivm, _ptr) \
230 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_TEXTURES, "textures")
232 #define draw_gs_jit_context_samplers(_gallivm, _ptr) \
233 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_GS_JIT_CTX_SAMPLERS, "samplers")
235 #define draw_gs_jit_prim_lengths(_gallivm, _ptr) \
236 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_PRIM_LENGTHS, "prim_lengths")
238 #define draw_gs_jit_emitted_vertices(_gallivm, _ptr) \
239 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_EMITTED_VERTICES, "emitted_vertices")
241 #define draw_gs_jit_emitted_prims(_gallivm, _ptr) \
242 lp_build_struct_get(_gallivm, _ptr, DRAW_GS_JIT_CTX_EMITTED_PRIMS, "emitted_prims")
247 (*draw_jit_vert_func
)(struct draw_jit_context
*context
,
248 struct vertex_header
*io
,
249 const char *vbuffers
[PIPE_MAX_ATTRIBS
],
253 struct pipe_vertex_buffer
*vertex_buffers
,
254 unsigned instance_id
);
258 (*draw_jit_vert_func_elts
)(struct draw_jit_context
*context
,
259 struct vertex_header
*io
,
260 const char *vbuffers
[PIPE_MAX_ATTRIBS
],
261 const unsigned *fetch_elts
,
262 unsigned fetch_count
,
264 struct pipe_vertex_buffer
*vertex_buffers
,
265 unsigned instance_id
);
269 (*draw_gs_jit_func
)(struct draw_gs_jit_context
*context
,
270 float inputs
[6][PIPE_MAX_SHADER_INPUTS
][TGSI_NUM_CHANNELS
][TGSI_NUM_CHANNELS
],
271 struct vertex_header
*output
,
273 unsigned instance_id
,
276 struct draw_llvm_variant_key
278 unsigned nr_vertex_elements
:8;
279 unsigned nr_samplers
:8;
280 unsigned nr_sampler_views
:8;
281 unsigned clamp_vertex_color
:1;
284 unsigned clip_user
:1;
285 unsigned clip_halfz
:1;
286 unsigned bypass_viewport
:1;
287 unsigned need_edgeflags
:1;
290 * it is important there are no holes in this struct
291 * (and all padding gets zeroed).
293 unsigned ucp_enable
:PIPE_MAX_CLIP_PLANES
;
294 unsigned pad1
:32-PIPE_MAX_CLIP_PLANES
;
296 /* Variable number of vertex elements:
298 struct pipe_vertex_element vertex_element
[1];
300 /* Followed by variable number of samplers:
302 /* struct draw_sampler_static_state sampler; */
305 struct draw_gs_llvm_variant_key
307 unsigned nr_samplers
:8;
308 unsigned nr_sampler_views
:8;
310 struct draw_sampler_static_state samplers
[1];
313 #define DRAW_LLVM_MAX_VARIANT_KEY_SIZE \
314 (sizeof(struct draw_llvm_variant_key) + \
315 PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state) + \
316 (PIPE_MAX_ATTRIBS-1) * sizeof(struct pipe_vertex_element))
318 #define DRAW_GS_LLVM_MAX_VARIANT_KEY_SIZE \
319 (sizeof(struct draw_gs_llvm_variant_key) + \
320 PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state))
324 draw_llvm_variant_key_size(unsigned nr_vertex_elements
,
325 unsigned nr_samplers
)
327 return (sizeof(struct draw_llvm_variant_key
) +
328 nr_samplers
* sizeof(struct draw_sampler_static_state
) +
329 (nr_vertex_elements
- 1) * sizeof(struct pipe_vertex_element
));
334 draw_gs_llvm_variant_key_size(unsigned nr_samplers
)
336 return (sizeof(struct draw_gs_llvm_variant_key
) +
337 (nr_samplers
- 1) * sizeof(struct draw_sampler_static_state
));
341 static INLINE
struct draw_sampler_static_state
*
342 draw_llvm_variant_key_samplers(struct draw_llvm_variant_key
*key
)
344 return (struct draw_sampler_static_state
*)
345 &key
->vertex_element
[key
->nr_vertex_elements
];
349 struct draw_llvm_variant_list_item
351 struct draw_llvm_variant
*base
;
352 struct draw_llvm_variant_list_item
*next
, *prev
;
355 struct draw_gs_llvm_variant_list_item
357 struct draw_gs_llvm_variant
*base
;
358 struct draw_gs_llvm_variant_list_item
*next
, *prev
;
362 struct draw_llvm_variant
364 struct gallivm_state
*gallivm
;
366 /* LLVM JIT builder types */
367 LLVMTypeRef context_ptr_type
;
368 LLVMTypeRef buffer_ptr_type
;
369 LLVMTypeRef vb_ptr_type
;
370 LLVMTypeRef vertex_header_ptr_type
;
372 LLVMValueRef function
;
373 LLVMValueRef function_elts
;
374 draw_jit_vert_func jit_func
;
375 draw_jit_vert_func_elts jit_func_elts
;
377 struct llvm_vertex_shader
*shader
;
379 struct draw_llvm
*llvm
;
380 struct draw_llvm_variant_list_item list_item_global
;
381 struct draw_llvm_variant_list_item list_item_local
;
383 /* key is variable-sized, must be last */
384 struct draw_llvm_variant_key key
;
388 struct draw_gs_llvm_variant
390 struct gallivm_state
*gallivm
;
392 /* LLVM JIT builder types */
393 LLVMTypeRef context_ptr_type
;
394 LLVMTypeRef vertex_header_ptr_type
;
395 LLVMTypeRef input_array_type
;
397 LLVMValueRef context_ptr
;
399 LLVMValueRef num_prims
;
400 LLVMValueRef function
;
401 draw_gs_jit_func jit_func
;
403 struct llvm_geometry_shader
*shader
;
405 struct draw_llvm
*llvm
;
406 struct draw_gs_llvm_variant_list_item list_item_global
;
407 struct draw_gs_llvm_variant_list_item list_item_local
;
409 /* key is variable-sized, must be last */
410 struct draw_gs_llvm_variant_key key
;
413 struct llvm_vertex_shader
{
414 struct draw_vertex_shader base
;
416 unsigned variant_key_size
;
417 struct draw_llvm_variant_list_item variants
;
418 unsigned variants_created
;
419 unsigned variants_cached
;
422 struct llvm_geometry_shader
{
423 struct draw_geometry_shader base
;
425 unsigned variant_key_size
;
426 struct draw_gs_llvm_variant_list_item variants
;
427 unsigned variants_created
;
428 unsigned variants_cached
;
433 struct draw_context
*draw
;
435 struct draw_jit_context jit_context
;
436 struct draw_gs_jit_context gs_jit_context
;
438 struct draw_llvm_variant_list_item vs_variants_list
;
441 struct draw_gs_llvm_variant_list_item gs_variants_list
;
446 static INLINE
struct llvm_vertex_shader
*
447 llvm_vertex_shader(struct draw_vertex_shader
*vs
)
449 return (struct llvm_vertex_shader
*)vs
;
452 static INLINE
struct llvm_geometry_shader
*
453 llvm_geometry_shader(struct draw_geometry_shader
*gs
)
455 return (struct llvm_geometry_shader
*)gs
;
462 draw_llvm_create(struct draw_context
*draw
);
465 draw_llvm_destroy(struct draw_llvm
*llvm
);
467 struct draw_llvm_variant
*
468 draw_llvm_create_variant(struct draw_llvm
*llvm
,
469 unsigned num_vertex_header_attribs
,
470 const struct draw_llvm_variant_key
*key
);
473 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
);
475 struct draw_llvm_variant_key
*
476 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
);
479 draw_llvm_dump_variant_key(struct draw_llvm_variant_key
*key
);
482 struct draw_gs_llvm_variant
*
483 draw_gs_llvm_create_variant(struct draw_llvm
*llvm
,
484 unsigned num_vertex_header_attribs
,
485 const struct draw_gs_llvm_variant_key
*key
);
488 draw_gs_llvm_destroy_variant(struct draw_gs_llvm_variant
*variant
);
490 struct draw_gs_llvm_variant_key
*
491 draw_gs_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
);
494 draw_gs_llvm_dump_variant_key(struct draw_gs_llvm_variant_key
*key
);
496 struct lp_build_sampler_soa
*
497 draw_llvm_sampler_soa_create(const struct draw_sampler_static_state
*static_state
,
498 LLVMValueRef context_ptr
);
501 draw_llvm_set_sampler_state(struct draw_context
*draw
, unsigned shader_stage
);
504 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
505 unsigned shader_stage
,
507 uint32_t width
, uint32_t height
, uint32_t depth
,
508 uint32_t first_level
, uint32_t last_level
,
509 const void *base_ptr
,
510 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
511 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
512 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
]);