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"
35 #include "gallivm/lp_bld_limits.h"
37 #include "pipe/p_context.h"
38 #include "util/u_simple_list.h"
42 struct llvm_vertex_shader
;
44 struct draw_jit_texture
52 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
];
53 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
];
54 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
];
58 struct draw_sampler_static_state
61 * These attributes are effectively interleaved for more sane key handling.
62 * However, there might be lots of null space if the amount of samplers and
63 * textures isn't the same.
65 struct lp_static_sampler_state sampler_state
;
66 struct lp_static_texture_state texture_state
;
70 struct draw_jit_sampler
75 float border_color
[4];
80 DRAW_JIT_TEXTURE_WIDTH
= 0,
81 DRAW_JIT_TEXTURE_HEIGHT
,
82 DRAW_JIT_TEXTURE_DEPTH
,
83 DRAW_JIT_TEXTURE_FIRST_LEVEL
,
84 DRAW_JIT_TEXTURE_LAST_LEVEL
,
85 DRAW_JIT_TEXTURE_BASE
,
86 DRAW_JIT_TEXTURE_ROW_STRIDE
,
87 DRAW_JIT_TEXTURE_IMG_STRIDE
,
88 DRAW_JIT_TEXTURE_MIP_OFFSETS
,
89 DRAW_JIT_TEXTURE_NUM_FIELDS
/* number of fields above */
94 DRAW_JIT_SAMPLER_MIN_LOD
,
95 DRAW_JIT_SAMPLER_MAX_LOD
,
96 DRAW_JIT_SAMPLER_LOD_BIAS
,
97 DRAW_JIT_SAMPLER_BORDER_COLOR
,
98 DRAW_JIT_SAMPLER_NUM_FIELDS
/* number of fields above */
103 DRAW_JIT_VERTEX_VERTEX_ID
= 0,
104 DRAW_JIT_VERTEX_CLIP
,
105 DRAW_JIT_VERTEX_PRE_CLIP_POS
,
109 #define DRAW_JIT_CTX_TEXTURES 4
110 #define DRAW_JIT_CTX_SAMPLERS 5
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 const float *gs_constants
[LP_MAX_TGSI_CONST_BUFFERS
];
127 float (*planes
) [DRAW_TOTAL_CLIP_PLANES
][4];
130 struct draw_jit_texture textures
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
131 struct draw_jit_sampler samplers
[PIPE_MAX_SAMPLERS
];
135 #define draw_jit_context_vs_constants(_gallivm, _ptr) \
136 lp_build_struct_get_ptr(_gallivm, _ptr, 0, "vs_constants")
138 #define draw_jit_context_gs_constants(_gallivm, _ptr) \
139 lp_build_struct_get_ptr(_gallivm, _ptr, 1, "gs_constants")
141 #define draw_jit_context_planes(_gallivm, _ptr) \
142 lp_build_struct_get(_gallivm, _ptr, 2, "planes")
144 #define draw_jit_context_viewport(_gallivm, _ptr) \
145 lp_build_struct_get(_gallivm, _ptr, 3, "viewport")
147 #define DRAW_JIT_CTX_TEXTURES 4
148 #define DRAW_JIT_CTX_SAMPLERS 5
150 #define draw_jit_context_textures(_gallivm, _ptr) \
151 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_TEXTURES, "textures")
153 #define draw_jit_context_samplers(_gallivm, _ptr) \
154 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_SAMPLERS, "samplers")
156 #define draw_jit_header_id(_gallivm, _ptr) \
157 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_VERTEX_ID, "id")
159 #define draw_jit_header_clip(_gallivm, _ptr) \
160 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_CLIP, "clip")
162 #define draw_jit_header_pre_clip_pos(_gallivm, _ptr) \
163 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_PRE_CLIP_POS, "pre_clip_pos")
165 #define draw_jit_header_data(_gallivm, _ptr) \
166 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_DATA, "data")
169 #define draw_jit_vbuffer_stride(_gallivm, _ptr) \
170 lp_build_struct_get(_gallivm, _ptr, 0, "stride")
172 #define draw_jit_vbuffer_offset(_gallivm, _ptr) \
173 lp_build_struct_get(_gallivm, _ptr, 1, "buffer_offset")
177 (*draw_jit_vert_func
)(struct draw_jit_context
*context
,
178 struct vertex_header
*io
,
179 const char *vbuffers
[PIPE_MAX_ATTRIBS
],
183 struct pipe_vertex_buffer
*vertex_buffers
,
184 unsigned instance_id
);
188 (*draw_jit_vert_func_elts
)(struct draw_jit_context
*context
,
189 struct vertex_header
*io
,
190 const char *vbuffers
[PIPE_MAX_ATTRIBS
],
191 const unsigned *fetch_elts
,
192 unsigned fetch_count
,
194 struct pipe_vertex_buffer
*vertex_buffers
,
195 unsigned instance_id
);
197 struct draw_llvm_variant_key
199 unsigned nr_vertex_elements
:8;
200 unsigned nr_samplers
:8;
201 unsigned nr_sampler_views
:8;
202 unsigned clamp_vertex_color
:1;
205 unsigned clip_user
:1;
206 unsigned clip_halfz
:1;
207 unsigned bypass_viewport
:1;
208 unsigned need_edgeflags
:1;
209 unsigned ucp_enable
:PIPE_MAX_CLIP_PLANES
;
210 unsigned pad
:33-PIPE_MAX_CLIP_PLANES
;
212 /* Variable number of vertex elements:
214 struct pipe_vertex_element vertex_element
[1];
216 /* Followed by variable number of samplers:
218 /* struct draw_sampler_static_state sampler; */
221 #define DRAW_LLVM_MAX_VARIANT_KEY_SIZE \
222 (sizeof(struct draw_llvm_variant_key) + \
223 PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state) + \
224 (PIPE_MAX_ATTRIBS-1) * sizeof(struct pipe_vertex_element))
228 draw_llvm_variant_key_size(unsigned nr_vertex_elements
,
229 unsigned nr_samplers
)
231 return (sizeof(struct draw_llvm_variant_key
) +
232 nr_samplers
* sizeof(struct draw_sampler_static_state
) +
233 (nr_vertex_elements
- 1) * sizeof(struct pipe_vertex_element
));
237 static INLINE
struct draw_sampler_static_state
*
238 draw_llvm_variant_key_samplers(struct draw_llvm_variant_key
*key
)
240 return (struct draw_sampler_static_state
*)
241 &key
->vertex_element
[key
->nr_vertex_elements
];
245 struct draw_llvm_variant_list_item
247 struct draw_llvm_variant
*base
;
248 struct draw_llvm_variant_list_item
*next
, *prev
;
251 struct draw_llvm_variant
253 struct gallivm_state
*gallivm
;
255 /* LLVM JIT builder types */
256 LLVMTypeRef context_ptr_type
;
257 LLVMTypeRef buffer_ptr_type
;
258 LLVMTypeRef vb_ptr_type
;
259 LLVMTypeRef vertex_header_ptr_type
;
261 LLVMValueRef function
;
262 LLVMValueRef function_elts
;
263 draw_jit_vert_func jit_func
;
264 draw_jit_vert_func_elts jit_func_elts
;
266 struct llvm_vertex_shader
*shader
;
268 struct draw_llvm
*llvm
;
269 struct draw_llvm_variant_list_item list_item_global
;
270 struct draw_llvm_variant_list_item list_item_local
;
272 /* key is variable-sized, must be last */
273 struct draw_llvm_variant_key key
;
276 struct llvm_vertex_shader
{
277 struct draw_vertex_shader base
;
279 unsigned variant_key_size
;
280 struct draw_llvm_variant_list_item variants
;
281 unsigned variants_created
;
282 unsigned variants_cached
;
286 struct draw_context
*draw
;
288 struct draw_jit_context jit_context
;
290 struct draw_llvm_variant_list_item vs_variants_list
;
295 static INLINE
struct llvm_vertex_shader
*
296 llvm_vertex_shader(struct draw_vertex_shader
*vs
)
298 return (struct llvm_vertex_shader
*)vs
;
303 draw_llvm_create(struct draw_context
*draw
);
306 draw_llvm_destroy(struct draw_llvm
*llvm
);
308 struct draw_llvm_variant
*
309 draw_llvm_create_variant(struct draw_llvm
*llvm
,
310 unsigned num_vertex_header_attribs
,
311 const struct draw_llvm_variant_key
*key
);
314 draw_llvm_destroy_variant(struct draw_llvm_variant
*variant
);
316 struct draw_llvm_variant_key
*
317 draw_llvm_make_variant_key(struct draw_llvm
*llvm
, char *store
);
320 draw_llvm_dump_variant_key(struct draw_llvm_variant_key
*key
);
322 struct lp_build_sampler_soa
*
323 draw_llvm_sampler_soa_create(const struct draw_sampler_static_state
*static_state
,
324 LLVMValueRef context_ptr
);
327 draw_llvm_set_sampler_state(struct draw_context
*draw
);
330 draw_llvm_set_mapped_texture(struct draw_context
*draw
,
331 unsigned sampler_idx
,
332 uint32_t width
, uint32_t height
, uint32_t depth
,
333 uint32_t first_level
, uint32_t last_level
,
334 const void *base_ptr
,
335 uint32_t row_stride
[PIPE_MAX_TEXTURE_LEVELS
],
336 uint32_t img_stride
[PIPE_MAX_TEXTURE_LEVELS
],
337 uint32_t mip_offsets
[PIPE_MAX_TEXTURE_LEVELS
]);