draw/softpipe: add clip vertex support. (v2)
[mesa.git] / src / gallium / auxiliary / draw / draw_llvm.h
1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 #ifndef DRAW_LLVM_H
29 #define DRAW_LLVM_H
30
31 #include "draw/draw_private.h"
32
33 #include "draw/draw_vs.h"
34 #include "gallivm/lp_bld_sample.h"
35
36 #include "pipe/p_context.h"
37 #include "util/u_simple_list.h"
38
39 #include <llvm-c/Core.h>
40 #include <llvm-c/Analysis.h>
41 #include <llvm-c/Target.h>
42 #include <llvm-c/ExecutionEngine.h>
43
44
45 struct draw_llvm;
46 struct llvm_vertex_shader;
47
48 struct draw_jit_texture
49 {
50 uint32_t width;
51 uint32_t height;
52 uint32_t depth;
53 uint32_t first_level;
54 uint32_t last_level;
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];
58 float min_lod;
59 float max_lod;
60 float lod_bias;
61 float border_color[4];
62 };
63
64 enum {
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 */
78 };
79
80 enum {
81 DRAW_JIT_VERTEX_VERTEX_ID = 0,
82 DRAW_JIT_VERTEX_CLIP,
83 DRAW_JIT_VERTEX_PRE_CLIP_POS,
84 DRAW_JIT_VERTEX_DATA
85 };
86
87 /**
88 * This structure is passed directly to the generated vertex shader.
89 *
90 * It contains the derived state.
91 *
92 * Changes here must be reflected in the draw_jit_context_* macros.
93 * Changes to the ordering should be avoided.
94 *
95 * Only use types with a clear size and padding here, in particular prefer the
96 * stdint.h types to the basic integer types.
97 */
98 struct draw_jit_context
99 {
100 const float *vs_constants;
101 const float *gs_constants;
102 float (*planes) [DRAW_TOTAL_CLIP_PLANES][4];
103 float *viewport;
104
105 struct draw_jit_texture textures[PIPE_MAX_VERTEX_SAMPLERS];
106 };
107
108
109 #define draw_jit_context_vs_constants(_gallivm, _ptr) \
110 lp_build_struct_get(_gallivm, _ptr, 0, "vs_constants")
111
112 #define draw_jit_context_gs_constants(_gallivm, _ptr) \
113 lp_build_struct_get(_gallivm, _ptr, 1, "gs_constants")
114
115 #define draw_jit_context_planes(_gallivm, _ptr) \
116 lp_build_struct_get(_gallivm, _ptr, 2, "planes")
117
118 #define draw_jit_context_viewport(_gallivm, _ptr) \
119 lp_build_struct_get(_gallivm, _ptr, 3, "viewport")
120
121 #define DRAW_JIT_CTX_TEXTURES 4
122
123 #define draw_jit_context_textures(_gallivm, _ptr) \
124 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_TEXTURES, "textures")
125
126 #define draw_jit_header_id(_gallivm, _ptr) \
127 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_VERTEX_ID, "id")
128
129 #define draw_jit_header_clip(_gallivm, _ptr) \
130 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_CLIP, "clip")
131
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")
134
135 #define draw_jit_header_data(_gallivm, _ptr) \
136 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_DATA, "data")
137
138
139 #define draw_jit_vbuffer_stride(_gallivm, _ptr) \
140 lp_build_struct_get(_gallivm, _ptr, 0, "stride")
141
142 #define draw_jit_vbuffer_offset(_gallivm, _ptr) \
143 lp_build_struct_get(_gallivm, _ptr, 1, "buffer_offset")
144
145
146 typedef int
147 (*draw_jit_vert_func)(struct draw_jit_context *context,
148 struct vertex_header *io,
149 const char *vbuffers[PIPE_MAX_ATTRIBS],
150 unsigned start,
151 unsigned count,
152 unsigned stride,
153 struct pipe_vertex_buffer *vertex_buffers,
154 unsigned instance_id);
155
156
157 typedef int
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,
163 unsigned stride,
164 struct pipe_vertex_buffer *vertex_buffers,
165 unsigned instance_id);
166
167 struct draw_llvm_variant_key
168 {
169 unsigned nr_vertex_elements:8;
170 unsigned nr_samplers:8;
171 unsigned clamp_vertex_color:1;
172 unsigned clip_xy:1;
173 unsigned clip_z: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;
180
181 /* Variable number of vertex elements:
182 */
183 struct pipe_vertex_element vertex_element[1];
184
185 /* Followed by variable number of samplers:
186 */
187 /* struct lp_sampler_static_state sampler; */
188 };
189
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))
194
195
196 static INLINE size_t
197 draw_llvm_variant_key_size(unsigned nr_vertex_elements,
198 unsigned nr_samplers)
199 {
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));
203 }
204
205
206 static INLINE struct lp_sampler_static_state *
207 draw_llvm_variant_key_samplers(struct draw_llvm_variant_key *key)
208 {
209 return (struct lp_sampler_static_state *)
210 &key->vertex_element[key->nr_vertex_elements];
211 }
212
213
214
215 struct draw_llvm_variant_list_item
216 {
217 struct draw_llvm_variant *base;
218 struct draw_llvm_variant_list_item *next, *prev;
219 };
220
221 struct draw_llvm_variant
222 {
223 LLVMValueRef function;
224 LLVMValueRef function_elts;
225 draw_jit_vert_func jit_func;
226 draw_jit_vert_func_elts jit_func_elts;
227
228 struct llvm_vertex_shader *shader;
229
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;
233
234 /* key is variable-sized, must be last */
235 struct draw_llvm_variant_key key;
236 };
237
238 struct llvm_vertex_shader {
239 struct draw_vertex_shader base;
240
241 unsigned variant_key_size;
242 struct draw_llvm_variant_list_item variants;
243 unsigned variants_created;
244 unsigned variants_cached;
245 };
246
247 struct draw_llvm {
248 struct draw_context *draw;
249
250 struct draw_jit_context jit_context;
251
252 struct gallivm_state *gallivm;
253
254 struct draw_llvm_variant_list_item vs_variants_list;
255 int nr_variants;
256
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;
262 };
263
264
265 static INLINE struct llvm_vertex_shader *
266 llvm_vertex_shader(struct draw_vertex_shader *vs)
267 {
268 return (struct llvm_vertex_shader *)vs;
269 }
270
271
272 struct draw_llvm *
273 draw_llvm_create(struct draw_context *draw, struct gallivm_state *gallivm);
274
275 void
276 draw_llvm_destroy(struct draw_llvm *llvm);
277
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);
282
283 void
284 draw_llvm_destroy_variant(struct draw_llvm_variant *variant);
285
286 struct draw_llvm_variant_key *
287 draw_llvm_make_variant_key(struct draw_llvm *llvm, char *store);
288
289 LLVMValueRef
290 draw_llvm_translate_from(struct gallivm_state *gallivm,
291 LLVMValueRef vbuffer,
292 enum pipe_format from_format);
293
294 struct lp_build_sampler_soa *
295 draw_llvm_sampler_soa_create(const struct lp_sampler_static_state *static_state,
296 LLVMValueRef context_ptr);
297
298 void
299 draw_llvm_set_sampler_state(struct draw_context *draw);
300
301 void
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]);
309
310 #endif