gallivm,llvmpipe,draw: Support multiple constant buffers.
[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 float min_lod;
56 float max_lod;
57 float lod_bias;
58 float border_color[4];
59 };
60
61 enum {
62 DRAW_JIT_TEXTURE_WIDTH = 0,
63 DRAW_JIT_TEXTURE_HEIGHT,
64 DRAW_JIT_TEXTURE_DEPTH,
65 DRAW_JIT_TEXTURE_FIRST_LEVEL,
66 DRAW_JIT_TEXTURE_LAST_LEVEL,
67 DRAW_JIT_TEXTURE_BASE,
68 DRAW_JIT_TEXTURE_ROW_STRIDE,
69 DRAW_JIT_TEXTURE_IMG_STRIDE,
70 DRAW_JIT_TEXTURE_MIP_OFFSETS,
71 DRAW_JIT_TEXTURE_MIN_LOD,
72 DRAW_JIT_TEXTURE_MAX_LOD,
73 DRAW_JIT_TEXTURE_LOD_BIAS,
74 DRAW_JIT_TEXTURE_BORDER_COLOR,
75 DRAW_JIT_TEXTURE_NUM_FIELDS /* number of fields above */
76 };
77
78 enum {
79 DRAW_JIT_VERTEX_VERTEX_ID = 0,
80 DRAW_JIT_VERTEX_CLIP,
81 DRAW_JIT_VERTEX_PRE_CLIP_POS,
82 DRAW_JIT_VERTEX_DATA
83 };
84
85 /**
86 * This structure is passed directly to the generated vertex shader.
87 *
88 * It contains the derived state.
89 *
90 * Changes here must be reflected in the draw_jit_context_* macros.
91 * Changes to the ordering should be avoided.
92 *
93 * Only use types with a clear size and padding here, in particular prefer the
94 * stdint.h types to the basic integer types.
95 */
96 struct draw_jit_context
97 {
98 const float *vs_constants[LP_MAX_TGSI_CONST_BUFFERS];
99 const float *gs_constants[LP_MAX_TGSI_CONST_BUFFERS];
100 float (*planes) [DRAW_TOTAL_CLIP_PLANES][4];
101 float *viewport;
102
103 struct draw_jit_texture textures[PIPE_MAX_SAMPLERS];
104 };
105
106
107 #define draw_jit_context_vs_constants(_gallivm, _ptr) \
108 lp_build_struct_get_ptr(_gallivm, _ptr, 0, "vs_constants")
109
110 #define draw_jit_context_gs_constants(_gallivm, _ptr) \
111 lp_build_struct_get_ptr(_gallivm, _ptr, 1, "gs_constants")
112
113 #define draw_jit_context_planes(_gallivm, _ptr) \
114 lp_build_struct_get(_gallivm, _ptr, 2, "planes")
115
116 #define draw_jit_context_viewport(_gallivm, _ptr) \
117 lp_build_struct_get(_gallivm, _ptr, 3, "viewport")
118
119 #define DRAW_JIT_CTX_TEXTURES 4
120
121 #define draw_jit_context_textures(_gallivm, _ptr) \
122 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_CTX_TEXTURES, "textures")
123
124 #define draw_jit_header_id(_gallivm, _ptr) \
125 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_VERTEX_ID, "id")
126
127 #define draw_jit_header_clip(_gallivm, _ptr) \
128 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_CLIP, "clip")
129
130 #define draw_jit_header_pre_clip_pos(_gallivm, _ptr) \
131 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_PRE_CLIP_POS, "pre_clip_pos")
132
133 #define draw_jit_header_data(_gallivm, _ptr) \
134 lp_build_struct_get_ptr(_gallivm, _ptr, DRAW_JIT_VERTEX_DATA, "data")
135
136
137 #define draw_jit_vbuffer_stride(_gallivm, _ptr) \
138 lp_build_struct_get(_gallivm, _ptr, 0, "stride")
139
140 #define draw_jit_vbuffer_offset(_gallivm, _ptr) \
141 lp_build_struct_get(_gallivm, _ptr, 1, "buffer_offset")
142
143
144 typedef int
145 (*draw_jit_vert_func)(struct draw_jit_context *context,
146 struct vertex_header *io,
147 const char *vbuffers[PIPE_MAX_ATTRIBS],
148 unsigned start,
149 unsigned count,
150 unsigned stride,
151 struct pipe_vertex_buffer *vertex_buffers,
152 unsigned instance_id);
153
154
155 typedef int
156 (*draw_jit_vert_func_elts)(struct draw_jit_context *context,
157 struct vertex_header *io,
158 const char *vbuffers[PIPE_MAX_ATTRIBS],
159 const unsigned *fetch_elts,
160 unsigned fetch_count,
161 unsigned stride,
162 struct pipe_vertex_buffer *vertex_buffers,
163 unsigned instance_id);
164
165 struct draw_llvm_variant_key
166 {
167 unsigned nr_vertex_elements:8;
168 unsigned nr_samplers:8;
169 unsigned clamp_vertex_color:1;
170 unsigned clip_xy:1;
171 unsigned clip_z:1;
172 unsigned clip_user:1;
173 unsigned clip_halfz:1;
174 unsigned bypass_viewport:1;
175 unsigned need_edgeflags:1;
176 unsigned ucp_enable:PIPE_MAX_CLIP_PLANES;
177 unsigned pad:9-PIPE_MAX_CLIP_PLANES;
178
179 /* Variable number of vertex elements:
180 */
181 struct pipe_vertex_element vertex_element[1];
182
183 /* Followed by variable number of samplers:
184 */
185 /* struct lp_sampler_static_state sampler; */
186 };
187
188 #define DRAW_LLVM_MAX_VARIANT_KEY_SIZE \
189 (sizeof(struct draw_llvm_variant_key) + \
190 PIPE_MAX_SAMPLERS * sizeof(struct lp_sampler_static_state) + \
191 (PIPE_MAX_ATTRIBS-1) * sizeof(struct pipe_vertex_element))
192
193
194 static INLINE size_t
195 draw_llvm_variant_key_size(unsigned nr_vertex_elements,
196 unsigned nr_samplers)
197 {
198 return (sizeof(struct draw_llvm_variant_key) +
199 nr_samplers * sizeof(struct lp_sampler_static_state) +
200 (nr_vertex_elements - 1) * sizeof(struct pipe_vertex_element));
201 }
202
203
204 static INLINE struct lp_sampler_static_state *
205 draw_llvm_variant_key_samplers(struct draw_llvm_variant_key *key)
206 {
207 return (struct lp_sampler_static_state *)
208 &key->vertex_element[key->nr_vertex_elements];
209 }
210
211
212
213 struct draw_llvm_variant_list_item
214 {
215 struct draw_llvm_variant *base;
216 struct draw_llvm_variant_list_item *next, *prev;
217 };
218
219 struct draw_llvm_variant
220 {
221 struct gallivm_state *gallivm;
222
223 /* LLVM JIT builder types */
224 LLVMTypeRef context_ptr_type;
225 LLVMTypeRef buffer_ptr_type;
226 LLVMTypeRef vb_ptr_type;
227 LLVMTypeRef vertex_header_ptr_type;
228
229 LLVMValueRef function;
230 LLVMValueRef function_elts;
231 draw_jit_vert_func jit_func;
232 draw_jit_vert_func_elts jit_func_elts;
233
234 struct llvm_vertex_shader *shader;
235
236 struct draw_llvm *llvm;
237 struct draw_llvm_variant_list_item list_item_global;
238 struct draw_llvm_variant_list_item list_item_local;
239
240 /* key is variable-sized, must be last */
241 struct draw_llvm_variant_key key;
242 };
243
244 struct llvm_vertex_shader {
245 struct draw_vertex_shader base;
246
247 unsigned variant_key_size;
248 struct draw_llvm_variant_list_item variants;
249 unsigned variants_created;
250 unsigned variants_cached;
251 };
252
253 struct draw_llvm {
254 struct draw_context *draw;
255
256 struct draw_jit_context jit_context;
257
258 struct draw_llvm_variant_list_item vs_variants_list;
259 int nr_variants;
260 };
261
262
263 static INLINE struct llvm_vertex_shader *
264 llvm_vertex_shader(struct draw_vertex_shader *vs)
265 {
266 return (struct llvm_vertex_shader *)vs;
267 }
268
269
270 struct draw_llvm *
271 draw_llvm_create(struct draw_context *draw);
272
273 void
274 draw_llvm_destroy(struct draw_llvm *llvm);
275
276 struct draw_llvm_variant *
277 draw_llvm_create_variant(struct draw_llvm *llvm,
278 unsigned num_vertex_header_attribs,
279 const struct draw_llvm_variant_key *key);
280
281 void
282 draw_llvm_destroy_variant(struct draw_llvm_variant *variant);
283
284 struct draw_llvm_variant_key *
285 draw_llvm_make_variant_key(struct draw_llvm *llvm, char *store);
286
287 struct lp_build_sampler_soa *
288 draw_llvm_sampler_soa_create(const struct lp_sampler_static_state *static_state,
289 LLVMValueRef context_ptr);
290
291 void
292 draw_llvm_set_sampler_state(struct draw_context *draw);
293
294 void
295 draw_llvm_set_mapped_texture(struct draw_context *draw,
296 unsigned sampler_idx,
297 uint32_t width, uint32_t height, uint32_t depth,
298 uint32_t first_level, uint32_t last_level,
299 const void *base_ptr,
300 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
301 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
302 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS]);
303
304 #endif