17ca3047594b9feff3f0ec674c53508d34700f56
[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 #include "gallivm/lp_bld_limits.h"
36
37 #include "pipe/p_context.h"
38 #include "util/u_simple_list.h"
39
40
41 struct draw_llvm;
42 struct llvm_vertex_shader;
43
44 struct draw_jit_texture
45 {
46 uint32_t width;
47 uint32_t height;
48 uint32_t depth;
49 uint32_t first_level;
50 uint32_t last_level;
51 const void *base;
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];
55 };
56
57
58 struct draw_sampler_static_state
59 {
60 /*
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.
64 */
65 struct lp_static_sampler_state sampler_state;
66 struct lp_static_texture_state texture_state;
67 };
68
69
70 struct draw_jit_sampler
71 {
72 float min_lod;
73 float max_lod;
74 float lod_bias;
75 float border_color[4];
76 };
77
78
79 enum {
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 */
90 };
91
92
93 enum {
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 */
99 };
100
101
102 enum {
103 DRAW_JIT_VERTEX_VERTEX_ID = 0,
104 DRAW_JIT_VERTEX_CLIP,
105 DRAW_JIT_VERTEX_PRE_CLIP_POS,
106 DRAW_JIT_VERTEX_DATA
107 };
108
109 #define DRAW_JIT_CTX_TEXTURES 4
110 #define DRAW_JIT_CTX_SAMPLERS 5
111
112 /**
113 * This structure is passed directly to the generated vertex shader.
114 *
115 * It contains the derived state.
116 *
117 * Changes here must be reflected in the draw_jit_context_* macros.
118 * Changes to the ordering should be avoided.
119 *
120 * Only use types with a clear size and padding here, in particular prefer the
121 * stdint.h types to the basic integer types.
122 */
123 struct draw_jit_context
124 {
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];
128 float *viewport;
129
130 struct draw_jit_texture textures[PIPE_MAX_SHADER_SAMPLER_VIEWS];
131 struct draw_jit_sampler samplers[PIPE_MAX_SAMPLERS];
132 };
133
134
135 #define draw_jit_context_vs_constants(_gallivm, _ptr) \
136 lp_build_struct_get_ptr(_gallivm, _ptr, 0, "vs_constants")
137
138 #define draw_jit_context_gs_constants(_gallivm, _ptr) \
139 lp_build_struct_get_ptr(_gallivm, _ptr, 1, "gs_constants")
140
141 #define draw_jit_context_planes(_gallivm, _ptr) \
142 lp_build_struct_get(_gallivm, _ptr, 2, "planes")
143
144 #define draw_jit_context_viewport(_gallivm, _ptr) \
145 lp_build_struct_get(_gallivm, _ptr, 3, "viewport")
146
147 #define DRAW_JIT_CTX_TEXTURES 4
148 #define DRAW_JIT_CTX_SAMPLERS 5
149
150 #define draw_jit_context_textures(_gallivm, _ptr) \
151 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_TEXTURES, "textures")
152
153 #define draw_jit_context_samplers(_gallivm, _ptr) \
154 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_SAMPLERS, "samplers")
155
156 #define draw_jit_header_id(_gallivm, _ptr) \
157 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_VERTEX_ID, "id")
158
159 #define draw_jit_header_clip(_gallivm, _ptr) \
160 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_CLIP, "clip")
161
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")
164
165 #define draw_jit_header_data(_gallivm, _ptr) \
166 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_DATA, "data")
167
168
169 #define draw_jit_vbuffer_stride(_gallivm, _ptr) \
170 lp_build_struct_get(_gallivm, _ptr, 0, "stride")
171
172 #define draw_jit_vbuffer_offset(_gallivm, _ptr) \
173 lp_build_struct_get(_gallivm, _ptr, 1, "buffer_offset")
174
175
176 typedef int
177 (*draw_jit_vert_func)(struct draw_jit_context *context,
178 struct vertex_header *io,
179 const char *vbuffers[PIPE_MAX_ATTRIBS],
180 unsigned start,
181 unsigned count,
182 unsigned stride,
183 struct pipe_vertex_buffer *vertex_buffers,
184 unsigned instance_id);
185
186
187 typedef int
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,
193 unsigned stride,
194 struct pipe_vertex_buffer *vertex_buffers,
195 unsigned instance_id);
196
197 struct draw_llvm_variant_key
198 {
199 unsigned nr_vertex_elements:8;
200 unsigned nr_samplers:8;
201 unsigned nr_sampler_views:8;
202 unsigned clamp_vertex_color:1;
203 unsigned clip_xy:1;
204 unsigned clip_z:1;
205 unsigned clip_user:1;
206 unsigned clip_halfz:1;
207 unsigned bypass_viewport:1;
208 unsigned need_edgeflags:1;
209 /*
210 * it is important there are no holes in this struct
211 * (and all padding gets zeroed).
212 */
213 unsigned pad1:1;
214 unsigned ucp_enable:PIPE_MAX_CLIP_PLANES;
215 unsigned pad2:32-PIPE_MAX_CLIP_PLANES;
216
217 /* Variable number of vertex elements:
218 */
219 struct pipe_vertex_element vertex_element[1];
220
221 /* Followed by variable number of samplers:
222 */
223 /* struct draw_sampler_static_state sampler; */
224 };
225
226 #define DRAW_LLVM_MAX_VARIANT_KEY_SIZE \
227 (sizeof(struct draw_llvm_variant_key) + \
228 PIPE_MAX_SHADER_SAMPLER_VIEWS * sizeof(struct draw_sampler_static_state) + \
229 (PIPE_MAX_ATTRIBS-1) * sizeof(struct pipe_vertex_element))
230
231
232 static INLINE size_t
233 draw_llvm_variant_key_size(unsigned nr_vertex_elements,
234 unsigned nr_samplers)
235 {
236 return (sizeof(struct draw_llvm_variant_key) +
237 nr_samplers * sizeof(struct draw_sampler_static_state) +
238 (nr_vertex_elements - 1) * sizeof(struct pipe_vertex_element));
239 }
240
241
242 static INLINE struct draw_sampler_static_state *
243 draw_llvm_variant_key_samplers(struct draw_llvm_variant_key *key)
244 {
245 return (struct draw_sampler_static_state *)
246 &key->vertex_element[key->nr_vertex_elements];
247 }
248
249
250 struct draw_llvm_variant_list_item
251 {
252 struct draw_llvm_variant *base;
253 struct draw_llvm_variant_list_item *next, *prev;
254 };
255
256 struct draw_llvm_variant
257 {
258 struct gallivm_state *gallivm;
259
260 /* LLVM JIT builder types */
261 LLVMTypeRef context_ptr_type;
262 LLVMTypeRef buffer_ptr_type;
263 LLVMTypeRef vb_ptr_type;
264 LLVMTypeRef vertex_header_ptr_type;
265
266 LLVMValueRef function;
267 LLVMValueRef function_elts;
268 draw_jit_vert_func jit_func;
269 draw_jit_vert_func_elts jit_func_elts;
270
271 struct llvm_vertex_shader *shader;
272
273 struct draw_llvm *llvm;
274 struct draw_llvm_variant_list_item list_item_global;
275 struct draw_llvm_variant_list_item list_item_local;
276
277 /* key is variable-sized, must be last */
278 struct draw_llvm_variant_key key;
279 };
280
281 struct llvm_vertex_shader {
282 struct draw_vertex_shader base;
283
284 unsigned variant_key_size;
285 struct draw_llvm_variant_list_item variants;
286 unsigned variants_created;
287 unsigned variants_cached;
288 };
289
290 struct draw_llvm {
291 struct draw_context *draw;
292
293 struct draw_jit_context jit_context;
294
295 struct draw_llvm_variant_list_item vs_variants_list;
296 int nr_variants;
297 };
298
299
300 static INLINE struct llvm_vertex_shader *
301 llvm_vertex_shader(struct draw_vertex_shader *vs)
302 {
303 return (struct llvm_vertex_shader *)vs;
304 }
305
306
307 struct draw_llvm *
308 draw_llvm_create(struct draw_context *draw);
309
310 void
311 draw_llvm_destroy(struct draw_llvm *llvm);
312
313 struct draw_llvm_variant *
314 draw_llvm_create_variant(struct draw_llvm *llvm,
315 unsigned num_vertex_header_attribs,
316 const struct draw_llvm_variant_key *key);
317
318 void
319 draw_llvm_destroy_variant(struct draw_llvm_variant *variant);
320
321 struct draw_llvm_variant_key *
322 draw_llvm_make_variant_key(struct draw_llvm *llvm, char *store);
323
324 void
325 draw_llvm_dump_variant_key(struct draw_llvm_variant_key *key);
326
327 struct lp_build_sampler_soa *
328 draw_llvm_sampler_soa_create(const struct draw_sampler_static_state *static_state,
329 LLVMValueRef context_ptr);
330
331 void
332 draw_llvm_set_sampler_state(struct draw_context *draw);
333
334 void
335 draw_llvm_set_mapped_texture(struct draw_context *draw,
336 unsigned sampler_idx,
337 uint32_t width, uint32_t height, uint32_t depth,
338 uint32_t first_level, uint32_t last_level,
339 const void *base_ptr,
340 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
341 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
342 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS]);
343
344 #endif