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 "gallivm/lp_bld_sample.h"
36 #include "pipe/p_context.h"
37 #include "util/u_simple_list.h"
39 #include <llvm-c/Core.h>
40 #include <llvm-c/Analysis.h>
41 #include <llvm-c/Target.h>
42 #include <llvm-c/ExecutionEngine.h>
46 struct llvm_vertex_shader
;
48 struct draw_jit_texture
55 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
];
56 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
];
57 const void *data
[PIPE_MAX_TEXTURE_LEVELS
];
61 float border_color
[4];
65 DRAW_JIT_TEXTURE_WIDTH
= 0,
66 DRAW_JIT_TEXTURE_HEIGHT
,
67 DRAW_JIT_TEXTURE_DEPTH
,
68 DRAW_JIT_TEXTURE_FIRST_LEVEL
,
69 DRAW_JIT_TEXTURE_LAST_LEVEL
,
70 DRAW_JIT_TEXTURE_ROW_STRIDE
,
71 DRAW_JIT_TEXTURE_IMG_STRIDE
,
72 DRAW_JIT_TEXTURE_DATA
,
73 DRAW_JIT_TEXTURE_MIN_LOD
,
74 DRAW_JIT_TEXTURE_MAX_LOD
,
75 DRAW_JIT_TEXTURE_LOD_BIAS
,
76 DRAW_JIT_TEXTURE_BORDER_COLOR
,
77 DRAW_JIT_TEXTURE_NUM_FIELDS
/* number of fields above */
81 DRAW_JIT_VERTEX_VERTEX_ID
= 0,
83 DRAW_JIT_VERTEX_PRE_CLIP_POS
,
88 * This structure is passed directly to the generated vertex shader.
90 * It contains the derived state.
92 * Changes here must be reflected in the draw_jit_context_* macros.
93 * Changes to the ordering should be avoided.
95 * Only use types with a clear size and padding here, in particular prefer the
96 * stdint.h types to the basic integer types.
98 struct draw_jit_context
100 const float *vs_constants
;
101 const float *gs_constants
;
102 float (*planes
) [DRAW_TOTAL_CLIP_PLANES
][4];
105 struct draw_jit_texture textures
[PIPE_MAX_VERTEX_SAMPLERS
];
109 #define draw_jit_context_vs_constants(_gallivm, _ptr) \
110 lp_build_struct_get(_gallivm, _ptr, 0, "vs_constants")
112 #define draw_jit_context_gs_constants(_gallivm, _ptr) \
113 lp_build_struct_get(_gallivm, _ptr, 1, "gs_constants")
115 #define draw_jit_context_planes(_gallivm, _ptr) \
116 lp_build_struct_get(_gallivm, _ptr, 2, "planes")
118 #define draw_jit_context_viewport(_gallivm, _ptr) \
119 lp_build_struct_get(_gallivm, _ptr, 3, "viewport")
121 #define DRAW_JIT_CTX_TEXTURES 4
123 #define draw_jit_context_textures(_gallivm, _ptr) \
124 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_TEXTURES, "textures")
126 #define draw_jit_header_id(_gallivm, _ptr) \
127 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_VERTEX_ID, "id")
129 #define draw_jit_header_clip(_gallivm, _ptr) \
130 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_CLIP, "clip")
132 #define draw_jit_header_pre_clip_pos(_gallivm, _ptr) \
133 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_PRE_CLIP_POS, "pre_clip_pos")
135 #define draw_jit_header_data(_gallivm, _ptr) \
136 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_DATA, "data")
139 #define draw_jit_vbuffer_stride(_gallivm, _ptr) \
140 lp_build_struct_get(_gallivm, _ptr, 0, "stride")
142 #define draw_jit_vbuffer_offset(_gallivm, _ptr) \
143 lp_build_struct_get(_gallivm, _ptr, 1, "buffer_offset")
147 (*draw_jit_vert_func
)(struct draw_jit_context
*context
,
148 struct vertex_header
*io
,
149 const char *vbuffers
[PIPE_MAX_ATTRIBS
],
153 struct pipe_vertex_buffer
*vertex_buffers
,
154 unsigned instance_id
);
158 (*draw_jit_vert_func_elts
)(struct draw_jit_context
*context
,
159 struct vertex_header
*io
,
160 const char *vbuffers
[PIPE_MAX_ATTRIBS
],
161 const unsigned *fetch_elts
,
162 unsigned fetch_count
,
164 struct pipe_vertex_buffer
*vertex_buffers
,
165 unsigned instance_id
);
167 struct draw_llvm_variant_key
169 unsigned nr_vertex_elements
:8;
170 unsigned nr_samplers
:8;
171 unsigned clamp_vertex_color
:1;
174 unsigned clip_user
:1;
175 unsigned clip_halfz
:1;
176 unsigned bypass_viewport
:1;
177 unsigned need_edgeflags
:1;
178 unsigned ucp_enable
:PIPE_MAX_CLIP_PLANES
;
179 unsigned pad
:9-PIPE_MAX_CLIP_PLANES
;
181 /* Variable number of vertex elements:
183 struct pipe_vertex_element vertex_element
[1];
185 /* Followed by variable number of samplers:
187 /* struct lp_sampler_static_state sampler; */
190 #define DRAW_LLVM_MAX_VARIANT_KEY_SIZE \
191 (sizeof(struct draw_llvm_variant_key) + \
192 PIPE_MAX_VERTEX_SAMPLERS * sizeof(struct lp_sampler_static_state) + \
193 (PIPE_MAX_ATTRIBS-1) * sizeof(struct pipe_vertex_element))
197 draw_llvm_variant_key_size(unsigned nr_vertex_elements
,
198 unsigned nr_samplers
)
200 return (sizeof(struct draw_llvm_variant_key
) +
201 nr_samplers
* sizeof(struct lp_sampler_static_state
) +
202 (nr_vertex_elements
- 1) * sizeof(struct pipe_vertex_element
));
206 static INLINE
struct lp_sampler_static_state
*
207 draw_llvm_variant_key_samplers(struct draw_llvm_variant_key
*key
)
209 return (struct lp_sampler_static_state
*)
210 &key
->vertex_element
[key
->nr_vertex_elements
];
215 struct draw_llvm_variant_list_item
217 struct draw_llvm_variant
*base
;
218 struct draw_llvm_variant_list_item
*next
, *prev
;
221 struct draw_llvm_variant
223 LLVMValueRef function
;
224 LLVMValueRef function_elts
;
225 draw_jit_vert_func jit_func
;
226 draw_jit_vert_func_elts jit_func_elts
;
228 struct llvm_vertex_shader
*shader
;
230 struct draw_llvm
*llvm
;
231 struct draw_llvm_variant_list_item list_item_global
;
232 struct draw_llvm_variant_list_item list_item_local
;
234 /* key is variable-sized, must be last */
235 struct draw_llvm_variant_key key
;
238 struct llvm_vertex_shader
{
239 struct draw_vertex_shader base
;
241 unsigned variant_key_size
;
242 struct draw_llvm_variant_list_item variants
;
243 unsigned variants_created
;
244 unsigned variants_cached
;
248 struct draw_context
*draw
;
250 struct draw_jit_context jit_context
;
252 struct gallivm_state
*gallivm
;
254 struct draw_llvm_variant_list_item vs_variants_list
;
257 /* LLVM JIT builder types */
258 LLVMTypeRef context_ptr_type
;
259 LLVMTypeRef buffer_ptr_type
;
260 LLVMTypeRef vb_ptr_type
;
261 LLVMTypeRef vertex_header_ptr_type
;
265 static INLINE
struct llvm_vertex_shader
*
266 llvm_vertex_shader(struct draw_vertex_shader
*vs
)
268 return (struct llvm_vertex_shader
*)vs
;
273 draw_llvm_create(struct draw_context
*draw
, struct gallivm_state
*gallivm
);
276 draw_llvm_destroy(struct draw_llvm
*llvm
);
278 struct draw_llvm_variant
*
279 draw_llvm_create_variant(struct draw_llvm
*llvm
,
280 unsigned num_vertex_header_attribs
,
281 const struct draw_llvm_variant_key
*key
);
284 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
);
286 struct draw_llvm_variant_key
*
287 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
);
290 draw_llvm_translate_from(struct gallivm_state
*gallivm
,
291 LLVMValueRef vbuffer
,
292 enum pipe_format from_format
);
294 struct lp_build_sampler_soa
*
295 draw_llvm_sampler_soa_create(const struct lp_sampler_static_state
*static_state
,
296 LLVMValueRef context_ptr
);
299 draw_llvm_set_sampler_state(struct draw_context
*draw
);
302 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
303 unsigned sampler_idx
,
304 uint32_t width
, uint32_t height
, uint32_t depth
,
305 uint32_t first_level
, uint32_t last_level
,
306 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
307 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
308 const void *data
[PIPE_MAX_TEXTURE_LEVELS
]);