llvmpipe/draw: wire up indirect offset
[mesa.git] / src / gallium / auxiliary / draw / draw_llvm_sample.c
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 /**
29 * Texture sampling code generation
30 * @author Jose Fonseca <jfonseca@vmware.com>
31 */
32
33 #include "pipe/p_defines.h"
34 #include "pipe/p_shader_tokens.h"
35 #include "gallivm/lp_bld_const.h"
36 #include "gallivm/lp_bld_debug.h"
37 #include "gallivm/lp_bld_type.h"
38 #include "gallivm/lp_bld_sample.h"
39 #include "gallivm/lp_bld_tgsi.h"
40
41
42 #include "util/u_debug.h"
43 #include "util/u_memory.h"
44 #include "util/u_pointer.h"
45 #include "util/u_string.h"
46
47 #include "draw_llvm.h"
48
49
50 /**
51 * This provides the bridge between the sampler state store in
52 * lp_jit_context and lp_jit_texture and the sampler code
53 * generator. It provides the texture layout information required by
54 * the texture sampler code generator in terms of the state stored in
55 * lp_jit_context and lp_jit_texture in runtime.
56 */
57 struct draw_llvm_sampler_dynamic_state
58 {
59 struct lp_sampler_dynamic_state base;
60
61 const struct draw_sampler_static_state *static_state;
62 };
63
64
65 /**
66 * This is the bridge between our sampler and the TGSI translator.
67 */
68 struct draw_llvm_sampler_soa
69 {
70 struct lp_build_sampler_soa base;
71
72 struct draw_llvm_sampler_dynamic_state dynamic_state;
73
74 unsigned nr_samplers;
75 };
76
77 struct draw_llvm_image_dynamic_state
78 {
79 struct lp_sampler_dynamic_state base;
80
81 const struct draw_image_static_state *static_state;
82 };
83
84 struct draw_llvm_image_soa
85 {
86 struct lp_build_image_soa base;
87
88 struct draw_llvm_image_dynamic_state dynamic_state;
89 };
90
91 /**
92 * Fetch the specified member of the lp_jit_texture structure.
93 * \param emit_load if TRUE, emit the LLVM load instruction to actually
94 * fetch the field's value. Otherwise, just emit the
95 * GEP code to address the field.
96 *
97 * @sa http://llvm.org/docs/GetElementPtr.html
98 */
99 static LLVMValueRef
100 draw_llvm_texture_member(const struct lp_sampler_dynamic_state *base,
101 struct gallivm_state *gallivm,
102 LLVMValueRef context_ptr,
103 unsigned texture_unit,
104 LLVMValueRef texture_unit_offset,
105 unsigned member_index,
106 const char *member_name,
107 boolean emit_load)
108 {
109 LLVMBuilderRef builder = gallivm->builder;
110 LLVMValueRef indices[4];
111 LLVMValueRef ptr;
112 LLVMValueRef res;
113
114 debug_assert(texture_unit < PIPE_MAX_SHADER_SAMPLER_VIEWS);
115
116 /* context[0] */
117 indices[0] = lp_build_const_int32(gallivm, 0);
118 /* context[0].textures */
119 indices[1] = lp_build_const_int32(gallivm, DRAW_JIT_CTX_TEXTURES);
120 /* context[0].textures[unit] */
121 indices[2] = lp_build_const_int32(gallivm, texture_unit);
122 if (texture_unit_offset) {
123 indices[2] = LLVMBuildAdd(gallivm->builder, indices[2], texture_unit_offset, "");
124 LLVMValueRef cond = LLVMBuildICmp(gallivm->builder, LLVMIntULT, indices[2], lp_build_const_int32(gallivm, PIPE_MAX_SHADER_SAMPLER_VIEWS), "");
125 indices[2] = LLVMBuildSelect(gallivm->builder, cond, indices[2], lp_build_const_int32(gallivm, texture_unit), "");
126 }
127 /* context[0].textures[unit].member */
128 indices[3] = lp_build_const_int32(gallivm, member_index);
129
130 ptr = LLVMBuildGEP(builder, context_ptr, indices, ARRAY_SIZE(indices), "");
131
132 if (emit_load)
133 res = LLVMBuildLoad(builder, ptr, "");
134 else
135 res = ptr;
136
137 lp_build_name(res, "context.texture%u.%s", texture_unit, member_name);
138
139 return res;
140 }
141
142
143 /**
144 * Fetch the specified member of the lp_jit_sampler structure.
145 * \param emit_load if TRUE, emit the LLVM load instruction to actually
146 * fetch the field's value. Otherwise, just emit the
147 * GEP code to address the field.
148 *
149 * @sa http://llvm.org/docs/GetElementPtr.html
150 */
151 static LLVMValueRef
152 draw_llvm_sampler_member(const struct lp_sampler_dynamic_state *base,
153 struct gallivm_state *gallivm,
154 LLVMValueRef context_ptr,
155 unsigned sampler_unit,
156 unsigned member_index,
157 const char *member_name,
158 boolean emit_load)
159 {
160 LLVMBuilderRef builder = gallivm->builder;
161 LLVMValueRef indices[4];
162 LLVMValueRef ptr;
163 LLVMValueRef res;
164
165 debug_assert(sampler_unit < PIPE_MAX_SAMPLERS);
166
167 /* context[0] */
168 indices[0] = lp_build_const_int32(gallivm, 0);
169 /* context[0].samplers */
170 indices[1] = lp_build_const_int32(gallivm, DRAW_JIT_CTX_SAMPLERS);
171 /* context[0].samplers[unit] */
172 indices[2] = lp_build_const_int32(gallivm, sampler_unit);
173 /* context[0].samplers[unit].member */
174 indices[3] = lp_build_const_int32(gallivm, member_index);
175
176 ptr = LLVMBuildGEP(builder, context_ptr, indices, ARRAY_SIZE(indices), "");
177
178 if (emit_load)
179 res = LLVMBuildLoad(builder, ptr, "");
180 else
181 res = ptr;
182
183 lp_build_name(res, "context.sampler%u.%s", sampler_unit, member_name);
184
185 return res;
186 }
187
188 /**
189 * Fetch the specified member of the lp_jit_texture structure.
190 * \param emit_load if TRUE, emit the LLVM load instruction to actually
191 * fetch the field's value. Otherwise, just emit the
192 * GEP code to address the field.
193 *
194 * @sa http://llvm.org/docs/GetElementPtr.html
195 */
196 static LLVMValueRef
197 draw_llvm_image_member(const struct lp_sampler_dynamic_state *base,
198 struct gallivm_state *gallivm,
199 LLVMValueRef context_ptr,
200 unsigned image_unit,
201 LLVMValueRef image_unit_offset,
202 unsigned member_index,
203 const char *member_name,
204 boolean emit_load)
205 {
206 LLVMBuilderRef builder = gallivm->builder;
207 LLVMValueRef indices[4];
208 LLVMValueRef ptr;
209 LLVMValueRef res;
210
211 debug_assert(image_unit < PIPE_MAX_SHADER_IMAGES);
212
213 /* context[0] */
214 indices[0] = lp_build_const_int32(gallivm, 0);
215 /* context[0].textures */
216 indices[1] = lp_build_const_int32(gallivm, DRAW_JIT_CTX_IMAGES);
217 /* context[0].textures[unit] */
218 indices[2] = lp_build_const_int32(gallivm, image_unit);
219 if (image_unit_offset) {
220 indices[2] = LLVMBuildAdd(gallivm->builder, indices[2], image_unit_offset, "");
221 LLVMValueRef cond = LLVMBuildICmp(gallivm->builder, LLVMIntULT, indices[2], lp_build_const_int32(gallivm, PIPE_MAX_SHADER_IMAGES), "");
222 indices[2] = LLVMBuildSelect(gallivm->builder, cond, indices[2], lp_build_const_int32(gallivm, image_unit), "");
223 }
224 /* context[0].textures[unit].member */
225 indices[3] = lp_build_const_int32(gallivm, member_index);
226
227 ptr = LLVMBuildGEP(builder, context_ptr, indices, ARRAY_SIZE(indices), "");
228
229 if (emit_load)
230 res = LLVMBuildLoad(builder, ptr, "");
231 else
232 res = ptr;
233
234 lp_build_name(res, "context.image%u.%s", image_unit, member_name);
235
236 return res;
237 }
238
239 /**
240 * Helper macro to instantiate the functions that generate the code to
241 * fetch the members of lp_jit_texture to fulfill the sampler code
242 * generator requests.
243 *
244 * This complexity is the price we have to pay to keep the texture
245 * sampler code generator a reusable module without dependencies to
246 * llvmpipe internals.
247 */
248 #define DRAW_LLVM_TEXTURE_MEMBER(_name, _index, _emit_load) \
249 static LLVMValueRef \
250 draw_llvm_texture_##_name( const struct lp_sampler_dynamic_state *base, \
251 struct gallivm_state *gallivm, \
252 LLVMValueRef context_ptr, \
253 unsigned texture_unit, \
254 LLVMValueRef texture_unit_offset) \
255 { \
256 return draw_llvm_texture_member(base, gallivm, context_ptr, \
257 texture_unit, texture_unit_offset, \
258 _index, #_name, _emit_load ); \
259 }
260
261
262 DRAW_LLVM_TEXTURE_MEMBER(width, DRAW_JIT_TEXTURE_WIDTH, TRUE)
263 DRAW_LLVM_TEXTURE_MEMBER(height, DRAW_JIT_TEXTURE_HEIGHT, TRUE)
264 DRAW_LLVM_TEXTURE_MEMBER(depth, DRAW_JIT_TEXTURE_DEPTH, TRUE)
265 DRAW_LLVM_TEXTURE_MEMBER(first_level,DRAW_JIT_TEXTURE_FIRST_LEVEL, TRUE)
266 DRAW_LLVM_TEXTURE_MEMBER(last_level, DRAW_JIT_TEXTURE_LAST_LEVEL, TRUE)
267 DRAW_LLVM_TEXTURE_MEMBER(base_ptr, DRAW_JIT_TEXTURE_BASE, TRUE)
268 DRAW_LLVM_TEXTURE_MEMBER(row_stride, DRAW_JIT_TEXTURE_ROW_STRIDE, FALSE)
269 DRAW_LLVM_TEXTURE_MEMBER(img_stride, DRAW_JIT_TEXTURE_IMG_STRIDE, FALSE)
270 DRAW_LLVM_TEXTURE_MEMBER(mip_offsets, DRAW_JIT_TEXTURE_MIP_OFFSETS, FALSE)
271 DRAW_LLVM_TEXTURE_MEMBER(num_samples, DRAW_JIT_TEXTURE_NUM_SAMPLES, TRUE)
272 DRAW_LLVM_TEXTURE_MEMBER(sample_stride, DRAW_JIT_TEXTURE_SAMPLE_STRIDE, TRUE)
273
274 #define DRAW_LLVM_SAMPLER_MEMBER(_name, _index, _emit_load) \
275 static LLVMValueRef \
276 draw_llvm_sampler_##_name( const struct lp_sampler_dynamic_state *base, \
277 struct gallivm_state *gallivm, \
278 LLVMValueRef context_ptr, \
279 unsigned sampler_unit) \
280 { \
281 return draw_llvm_sampler_member(base, gallivm, context_ptr, \
282 sampler_unit, _index, #_name, _emit_load ); \
283 }
284
285
286 DRAW_LLVM_SAMPLER_MEMBER(min_lod, DRAW_JIT_SAMPLER_MIN_LOD, TRUE)
287 DRAW_LLVM_SAMPLER_MEMBER(max_lod, DRAW_JIT_SAMPLER_MAX_LOD, TRUE)
288 DRAW_LLVM_SAMPLER_MEMBER(lod_bias, DRAW_JIT_SAMPLER_LOD_BIAS, TRUE)
289 DRAW_LLVM_SAMPLER_MEMBER(border_color, DRAW_JIT_SAMPLER_BORDER_COLOR, FALSE)
290
291 #define DRAW_LLVM_IMAGE_MEMBER(_name, _index, _emit_load) \
292 static LLVMValueRef \
293 draw_llvm_image_##_name( const struct lp_sampler_dynamic_state *base, \
294 struct gallivm_state *gallivm, \
295 LLVMValueRef context_ptr, \
296 unsigned image_unit, LLVMValueRef image_unit_offset) \
297 { \
298 return draw_llvm_image_member(base, gallivm, context_ptr, \
299 image_unit, image_unit_offset, \
300 _index, #_name, _emit_load ); \
301 }
302
303
304 DRAW_LLVM_IMAGE_MEMBER(width, DRAW_JIT_IMAGE_WIDTH, TRUE)
305 DRAW_LLVM_IMAGE_MEMBER(height, DRAW_JIT_IMAGE_HEIGHT, TRUE)
306 DRAW_LLVM_IMAGE_MEMBER(depth, DRAW_JIT_IMAGE_DEPTH, TRUE)
307 DRAW_LLVM_IMAGE_MEMBER(base_ptr, DRAW_JIT_IMAGE_BASE, TRUE)
308 DRAW_LLVM_IMAGE_MEMBER(row_stride, DRAW_JIT_IMAGE_ROW_STRIDE, TRUE)
309 DRAW_LLVM_IMAGE_MEMBER(img_stride, DRAW_JIT_IMAGE_IMG_STRIDE, TRUE)
310 DRAW_LLVM_IMAGE_MEMBER(num_samples, DRAW_JIT_IMAGE_NUM_SAMPLES, TRUE)
311 DRAW_LLVM_IMAGE_MEMBER(sample_stride, DRAW_JIT_IMAGE_SAMPLE_STRIDE, TRUE)
312
313 static void
314 draw_llvm_sampler_soa_destroy(struct lp_build_sampler_soa *sampler)
315 {
316 FREE(sampler);
317 }
318
319
320 /**
321 * Fetch filtered values from texture.
322 * The 'texel' parameter returns four vectors corresponding to R, G, B, A.
323 */
324 static void
325 draw_llvm_sampler_soa_emit_fetch_texel(const struct lp_build_sampler_soa *base,
326 struct gallivm_state *gallivm,
327 const struct lp_sampler_params *params)
328 {
329 struct draw_llvm_sampler_soa *sampler = (struct draw_llvm_sampler_soa *)base;
330 unsigned texture_index = params->texture_index;
331 unsigned sampler_index = params->sampler_index;
332
333 assert(texture_index < PIPE_MAX_SHADER_SAMPLER_VIEWS);
334 assert(sampler_index < PIPE_MAX_SAMPLERS);
335
336 if (params->texture_index_offset) {
337 struct lp_build_sample_array_switch switch_info;
338 memset(&switch_info, 0, sizeof(switch_info));
339 LLVMValueRef unit = LLVMBuildAdd(gallivm->builder, params->texture_index_offset,
340 lp_build_const_int32(gallivm, texture_index), "");
341 lp_build_sample_array_init_soa(&switch_info, gallivm, params, unit,
342 0, sampler->nr_samplers);
343
344 for (unsigned i = 0; i < sampler->nr_samplers; i++) {
345 lp_build_sample_array_case_soa(&switch_info, i,
346 &sampler->dynamic_state.static_state[i].texture_state,
347 &sampler->dynamic_state.static_state[i].sampler_state,
348 &sampler->dynamic_state.base);
349 }
350 lp_build_sample_array_fini_soa(&switch_info);
351 } else {
352 lp_build_sample_soa(&sampler->dynamic_state.static_state[texture_index].texture_state,
353 &sampler->dynamic_state.static_state[sampler_index].sampler_state,
354 &sampler->dynamic_state.base,
355 gallivm, params);
356 }
357 }
358
359
360 /**
361 * Fetch the texture size.
362 */
363 static void
364 draw_llvm_sampler_soa_emit_size_query(const struct lp_build_sampler_soa *base,
365 struct gallivm_state *gallivm,
366 const struct lp_sampler_size_query_params *params)
367 {
368 struct draw_llvm_sampler_soa *sampler = (struct draw_llvm_sampler_soa *)base;
369
370 assert(params->texture_unit < PIPE_MAX_SHADER_SAMPLER_VIEWS);
371
372 lp_build_size_query_soa(gallivm,
373 &sampler->dynamic_state.static_state[params->texture_unit].texture_state,
374 &sampler->dynamic_state.base,
375 params);
376 }
377
378 struct lp_build_sampler_soa *
379 draw_llvm_sampler_soa_create(const struct draw_sampler_static_state *static_state,
380 unsigned nr_samplers)
381 {
382 struct draw_llvm_sampler_soa *sampler;
383
384 sampler = CALLOC_STRUCT(draw_llvm_sampler_soa);
385 if (!sampler)
386 return NULL;
387
388 sampler->base.destroy = draw_llvm_sampler_soa_destroy;
389 sampler->base.emit_tex_sample = draw_llvm_sampler_soa_emit_fetch_texel;
390 sampler->base.emit_size_query = draw_llvm_sampler_soa_emit_size_query;
391 sampler->dynamic_state.base.width = draw_llvm_texture_width;
392 sampler->dynamic_state.base.height = draw_llvm_texture_height;
393 sampler->dynamic_state.base.depth = draw_llvm_texture_depth;
394 sampler->dynamic_state.base.first_level = draw_llvm_texture_first_level;
395 sampler->dynamic_state.base.last_level = draw_llvm_texture_last_level;
396 sampler->dynamic_state.base.row_stride = draw_llvm_texture_row_stride;
397 sampler->dynamic_state.base.img_stride = draw_llvm_texture_img_stride;
398 sampler->dynamic_state.base.base_ptr = draw_llvm_texture_base_ptr;
399 sampler->dynamic_state.base.mip_offsets = draw_llvm_texture_mip_offsets;
400 sampler->dynamic_state.base.num_samples = draw_llvm_texture_num_samples;
401 sampler->dynamic_state.base.sample_stride = draw_llvm_texture_sample_stride;
402 sampler->dynamic_state.base.min_lod = draw_llvm_sampler_min_lod;
403 sampler->dynamic_state.base.max_lod = draw_llvm_sampler_max_lod;
404 sampler->dynamic_state.base.lod_bias = draw_llvm_sampler_lod_bias;
405 sampler->dynamic_state.base.border_color = draw_llvm_sampler_border_color;
406 sampler->dynamic_state.static_state = static_state;
407
408 sampler->nr_samplers = nr_samplers;
409 return &sampler->base;
410 }
411
412 static void
413 draw_llvm_image_soa_emit_op(const struct lp_build_image_soa *base,
414 struct gallivm_state *gallivm,
415 const struct lp_img_params *params)
416 {
417 struct draw_llvm_image_soa *image = (struct draw_llvm_image_soa *)base;
418 unsigned image_index = params->image_index;
419 assert(image_index < PIPE_MAX_SHADER_IMAGES);
420
421 lp_build_img_op_soa(&image->dynamic_state.static_state[image_index].image_state,
422 &image->dynamic_state.base,
423 gallivm, params);
424 }
425 /**
426 * Fetch the texture size.
427 */
428 static void
429 draw_llvm_image_soa_emit_size_query(const struct lp_build_image_soa *base,
430 struct gallivm_state *gallivm,
431 const struct lp_sampler_size_query_params *params)
432 {
433 struct draw_llvm_image_soa *image = (struct draw_llvm_image_soa *)base;
434
435 assert(params->texture_unit < PIPE_MAX_SHADER_IMAGES);
436
437 lp_build_size_query_soa(gallivm,
438 &image->dynamic_state.static_state[params->texture_unit].image_state,
439 &image->dynamic_state.base,
440 params);
441 }
442 static void
443 draw_llvm_image_soa_destroy(struct lp_build_image_soa *image)
444 {
445 FREE(image);
446 }
447
448 struct lp_build_image_soa *
449 draw_llvm_image_soa_create(const struct draw_image_static_state *static_state)
450 {
451 struct draw_llvm_image_soa *image;
452
453 image = CALLOC_STRUCT(draw_llvm_image_soa);
454 if (!image)
455 return NULL;
456
457 image->base.destroy = draw_llvm_image_soa_destroy;
458 image->base.emit_op = draw_llvm_image_soa_emit_op;
459 image->base.emit_size_query = draw_llvm_image_soa_emit_size_query;
460
461 image->dynamic_state.base.width = draw_llvm_image_width;
462 image->dynamic_state.base.height = draw_llvm_image_height;
463
464 image->dynamic_state.base.depth = draw_llvm_image_depth;
465 image->dynamic_state.base.base_ptr = draw_llvm_image_base_ptr;
466 image->dynamic_state.base.row_stride = draw_llvm_image_row_stride;
467 image->dynamic_state.base.img_stride = draw_llvm_image_img_stride;
468 image->dynamic_state.base.num_samples = draw_llvm_image_num_samples;
469 image->dynamic_state.base.sample_stride = draw_llvm_image_sample_stride;
470
471 image->dynamic_state.static_state = static_state;
472
473 return &image->base;
474 }