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>
44 #define DRAW_MAX_TEXTURE_LEVELS 13 /* 4K x 4K for now */
47 struct llvm_vertex_shader
;
49 struct draw_jit_texture
55 uint32_t row_stride
[DRAW_MAX_TEXTURE_LEVELS
];
56 uint32_t img_stride
[DRAW_MAX_TEXTURE_LEVELS
];
57 const void *data
[DRAW_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_LAST_LEVEL
,
69 DRAW_JIT_TEXTURE_ROW_STRIDE
,
70 DRAW_JIT_TEXTURE_IMG_STRIDE
,
71 DRAW_JIT_TEXTURE_DATA
,
72 DRAW_JIT_TEXTURE_MIN_LOD
,
73 DRAW_JIT_TEXTURE_MAX_LOD
,
74 DRAW_JIT_TEXTURE_LOD_BIAS
,
75 DRAW_JIT_TEXTURE_BORDER_COLOR
,
76 DRAW_JIT_TEXTURE_NUM_FIELDS
/* number of fields above */
80 DRAW_JIT_VERTEX_VERTEX_ID
= 0,
86 * This structure is passed directly to the generated vertex shader.
88 * It contains the derived state.
90 * Changes here must be reflected in the draw_jit_context_* macros.
91 * Changes to the ordering should be avoided.
93 * Only use types with a clear size and padding here, in particular prefer the
94 * stdint.h types to the basic integer types.
96 struct draw_jit_context
98 const float *vs_constants
;
99 const float *gs_constants
;
100 float (*planes
) [12][4];
102 struct draw_jit_texture textures
[PIPE_MAX_VERTEX_SAMPLERS
];
106 #define draw_jit_context_vs_constants(_builder, _ptr) \
107 lp_build_struct_get(_builder, _ptr, 0, "vs_constants")
109 #define draw_jit_context_gs_constants(_builder, _ptr) \
110 lp_build_struct_get(_builder, _ptr, 1, "gs_constants")
112 #define draw_jit_context_planes(_builder, _ptr) \
113 lp_build_struct_get(_builder, _ptr, 2, "planes")
115 #define DRAW_JIT_CTX_TEXTURES 3
117 #define draw_jit_context_textures(_builder, _ptr) \
118 lp_build_struct_get_ptr(_builder, _ptr, DRAW_JIT_CTX_TEXTURES, "textures")
120 #define draw_jit_header_id(_builder, _ptr) \
121 lp_build_struct_get_ptr(_builder, _ptr, 0, "id")
123 #define draw_jit_header_clip(_builder, _ptr) \
124 lp_build_struct_get_ptr(_builder, _ptr, 1, "clip")
126 #define draw_jit_header_data(_builder, _ptr) \
127 lp_build_struct_get_ptr(_builder, _ptr, 2, "data")
130 #define draw_jit_vbuffer_stride(_builder, _ptr) \
131 lp_build_struct_get(_builder, _ptr, 0, "stride")
133 #define draw_jit_vbuffer_max_index(_builder, _ptr) \
134 lp_build_struct_get(_builder, _ptr, 1, "max_index")
136 #define draw_jit_vbuffer_offset(_builder, _ptr) \
137 lp_build_struct_get(_builder, _ptr, 2, "buffer_offset")
141 (*draw_jit_vert_func
)(struct draw_jit_context
*context
,
142 struct vertex_header
*io
,
143 const char *vbuffers
[PIPE_MAX_ATTRIBS
],
147 struct pipe_vertex_buffer
*vertex_buffers
,
148 unsigned instance_id
);
152 (*draw_jit_vert_func_elts
)(struct draw_jit_context
*context
,
153 struct vertex_header
*io
,
154 const char *vbuffers
[PIPE_MAX_ATTRIBS
],
155 const unsigned *fetch_elts
,
156 unsigned fetch_count
,
158 struct pipe_vertex_buffer
*vertex_buffers
,
159 unsigned instance_id
);
161 struct draw_llvm_variant_key
163 unsigned nr_vertex_elements
:8;
164 unsigned nr_samplers
:8;
167 unsigned clip_user
:1;
168 unsigned clip_halfz
:1;
169 unsigned bypass_viewport
:1;
170 unsigned need_edgeflags
:1;
171 unsigned nr_planes
:4;
174 /* Variable number of vertex elements:
176 struct pipe_vertex_element vertex_element
[1];
178 /* Followed by variable number of samplers:
180 /* struct lp_sampler_static_state sampler; */
183 #define DRAW_LLVM_MAX_VARIANT_KEY_SIZE \
184 (sizeof(struct draw_llvm_variant_key) + \
185 PIPE_MAX_VERTEX_SAMPLERS * sizeof(struct lp_sampler_static_state) + \
186 (PIPE_MAX_ATTRIBS-1) * sizeof(struct pipe_vertex_element))
190 draw_llvm_variant_key_size(unsigned nr_vertex_elements
,
191 unsigned nr_samplers
)
193 return (sizeof(struct draw_llvm_variant_key
) +
194 nr_samplers
* sizeof(struct lp_sampler_static_state
) +
195 (nr_vertex_elements
- 1) * sizeof(struct pipe_vertex_element
));
199 static INLINE
struct lp_sampler_static_state
*
200 draw_llvm_variant_key_samplers(struct draw_llvm_variant_key
*key
)
202 return (struct lp_sampler_static_state
*)
203 &key
->vertex_element
[key
->nr_vertex_elements
];
208 struct draw_llvm_variant_list_item
210 struct draw_llvm_variant
*base
;
211 struct draw_llvm_variant_list_item
*next
, *prev
;
214 struct draw_llvm_variant
216 LLVMValueRef function
;
217 LLVMValueRef function_elts
;
218 draw_jit_vert_func jit_func
;
219 draw_jit_vert_func_elts jit_func_elts
;
221 struct llvm_vertex_shader
*shader
;
223 struct draw_llvm
*llvm
;
224 struct draw_llvm_variant_list_item list_item_global
;
225 struct draw_llvm_variant_list_item list_item_local
;
227 /* key is variable-sized, must be last */
228 struct draw_llvm_variant_key key
;
229 /* key is variable-sized, must be last */
232 struct llvm_vertex_shader
{
233 struct draw_vertex_shader base
;
235 unsigned variant_key_size
;
236 struct draw_llvm_variant_list_item variants
;
237 unsigned variants_created
;
238 unsigned variants_cached
;
242 struct draw_context
*draw
;
244 struct draw_jit_context jit_context
;
246 struct draw_llvm_variant_list_item vs_variants_list
;
249 LLVMModuleRef module
;
250 LLVMExecutionEngineRef engine
;
251 LLVMModuleProviderRef provider
;
252 LLVMTargetDataRef target
;
253 LLVMPassManagerRef pass
;
255 LLVMTypeRef context_ptr_type
;
256 LLVMTypeRef vertex_header_ptr_type
;
257 LLVMTypeRef buffer_ptr_type
;
258 LLVMTypeRef vb_ptr_type
;
261 static INLINE
struct llvm_vertex_shader
*
262 llvm_vertex_shader(struct draw_vertex_shader
*vs
)
264 return (struct llvm_vertex_shader
*)vs
;
269 draw_llvm_create(struct draw_context
*draw
);
272 draw_llvm_destroy(struct draw_llvm
*llvm
);
274 struct draw_llvm_variant
*
275 draw_llvm_create_variant(struct draw_llvm
*llvm
,
276 unsigned num_vertex_header_attribs
,
277 const struct draw_llvm_variant_key
*key
);
280 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
);
282 struct draw_llvm_variant_key
*
283 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
);
286 draw_llvm_translate_from(LLVMBuilderRef builder
,
287 LLVMValueRef vbuffer
,
288 enum pipe_format from_format
);
290 struct lp_build_sampler_soa
*
291 draw_llvm_sampler_soa_create(const struct lp_sampler_static_state
*static_state
,
292 LLVMValueRef context_ptr
);
295 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
296 unsigned sampler_idx
,
297 uint32_t width
, uint32_t height
, uint32_t depth
,
299 uint32_t row_stride
[DRAW_MAX_TEXTURE_LEVELS
],
300 uint32_t img_stride
[DRAW_MAX_TEXTURE_LEVELS
],
301 const void *data
[DRAW_MAX_TEXTURE_LEVELS
]);