gallivm/nir: refactor image operations for indirect support.
[mesa.git] / src / gallium / drivers / llvmpipe / lp_tex_sample.c
1 /**************************************************************************
2 *
3 * Copyright 2009 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 *
31 * This file is nothing more than ugly glue between three largely independent
32 * entities:
33 * - TGSI -> LLVM translation (i.e., lp_build_tgsi_soa)
34 * - texture sampling code generation (i.e., lp_build_sample_soa)
35 * - LLVM pipe driver
36 *
37 * All interesting code is in the functions mentioned above. There is really
38 * nothing to see here.
39 *
40 * @author Jose Fonseca <jfonseca@vmware.com>
41 */
42
43 #include "pipe/p_defines.h"
44 #include "pipe/p_shader_tokens.h"
45 #include "gallivm/lp_bld_debug.h"
46 #include "gallivm/lp_bld_const.h"
47 #include "gallivm/lp_bld_type.h"
48 #include "gallivm/lp_bld_sample.h"
49 #include "gallivm/lp_bld_tgsi.h"
50 #include "lp_jit.h"
51 #include "lp_tex_sample.h"
52 #include "lp_state_fs.h"
53 #include "lp_debug.h"
54
55
56 /**
57 * This provides the bridge between the sampler state store in
58 * lp_jit_context and lp_jit_texture and the sampler code
59 * generator. It provides the texture layout information required by
60 * the texture sampler code generator in terms of the state stored in
61 * lp_jit_context and lp_jit_texture in runtime.
62 */
63 struct llvmpipe_sampler_dynamic_state
64 {
65 struct lp_sampler_dynamic_state base;
66
67 const struct lp_sampler_static_state *static_state;
68 };
69
70
71 /**
72 * This is the bridge between our sampler and the TGSI translator.
73 */
74 struct lp_llvm_sampler_soa
75 {
76 struct lp_build_sampler_soa base;
77
78 struct llvmpipe_sampler_dynamic_state dynamic_state;
79 unsigned nr_samplers;
80 };
81
82 struct llvmpipe_image_dynamic_state
83 {
84 struct lp_sampler_dynamic_state base;
85
86 const struct lp_image_static_state *static_state;
87 };
88
89 /**
90 * This is the bridge between our sampler and the TGSI translator.
91 */
92 struct lp_llvm_image_soa
93 {
94 struct lp_build_image_soa base;
95
96 struct llvmpipe_image_dynamic_state dynamic_state;
97 unsigned nr_images;
98 };
99
100
101 /**
102 * Fetch the specified member of the lp_jit_texture structure.
103 * \param emit_load if TRUE, emit the LLVM load instruction to actually
104 * fetch the field's value. Otherwise, just emit the
105 * GEP code to address the field.
106 *
107 * @sa http://llvm.org/docs/GetElementPtr.html
108 */
109 static LLVMValueRef
110 lp_llvm_texture_member(const struct lp_sampler_dynamic_state *base,
111 struct gallivm_state *gallivm,
112 LLVMValueRef context_ptr,
113 unsigned texture_unit,
114 LLVMValueRef texture_unit_offset,
115 unsigned member_index,
116 const char *member_name,
117 boolean emit_load)
118 {
119 LLVMBuilderRef builder = gallivm->builder;
120 LLVMValueRef indices[4];
121 LLVMValueRef ptr;
122 LLVMValueRef res;
123
124 assert(texture_unit < PIPE_MAX_SHADER_SAMPLER_VIEWS);
125
126 /* context[0] */
127 indices[0] = lp_build_const_int32(gallivm, 0);
128 /* context[0].textures */
129 indices[1] = lp_build_const_int32(gallivm, LP_JIT_CTX_TEXTURES);
130 /* context[0].textures[unit] */
131 indices[2] = lp_build_const_int32(gallivm, texture_unit);
132 if (texture_unit_offset) {
133 indices[2] = LLVMBuildAdd(gallivm->builder, indices[2], texture_unit_offset, "");
134 LLVMValueRef cond = LLVMBuildICmp(gallivm->builder, LLVMIntULT, indices[2], lp_build_const_int32(gallivm, PIPE_MAX_SHADER_SAMPLER_VIEWS), "");
135 indices[2] = LLVMBuildSelect(gallivm->builder, cond, indices[2], lp_build_const_int32(gallivm, texture_unit), "");
136 }
137 /* context[0].textures[unit].member */
138 indices[3] = lp_build_const_int32(gallivm, member_index);
139
140 ptr = LLVMBuildGEP(builder, context_ptr, indices, ARRAY_SIZE(indices), "");
141
142 if (emit_load)
143 res = LLVMBuildLoad(builder, ptr, "");
144 else
145 res = ptr;
146
147 lp_build_name(res, "context.texture%u.%s", texture_unit, member_name);
148
149 return res;
150 }
151
152
153 /**
154 * Helper macro to instantiate the functions that generate the code to
155 * fetch the members of lp_jit_texture to fulfill the sampler code
156 * generator requests.
157 *
158 * This complexity is the price we have to pay to keep the texture
159 * sampler code generator a reusable module without dependencies to
160 * llvmpipe internals.
161 */
162 #define LP_LLVM_TEXTURE_MEMBER(_name, _index, _emit_load) \
163 static LLVMValueRef \
164 lp_llvm_texture_##_name( const struct lp_sampler_dynamic_state *base, \
165 struct gallivm_state *gallivm, \
166 LLVMValueRef context_ptr, \
167 unsigned texture_unit, \
168 LLVMValueRef texture_unit_offset) \
169 { \
170 return lp_llvm_texture_member(base, gallivm, context_ptr, \
171 texture_unit, texture_unit_offset, \
172 _index, #_name, _emit_load ); \
173 }
174
175
176 LP_LLVM_TEXTURE_MEMBER(width, LP_JIT_TEXTURE_WIDTH, TRUE)
177 LP_LLVM_TEXTURE_MEMBER(height, LP_JIT_TEXTURE_HEIGHT, TRUE)
178 LP_LLVM_TEXTURE_MEMBER(depth, LP_JIT_TEXTURE_DEPTH, TRUE)
179 LP_LLVM_TEXTURE_MEMBER(first_level, LP_JIT_TEXTURE_FIRST_LEVEL, TRUE)
180 LP_LLVM_TEXTURE_MEMBER(last_level, LP_JIT_TEXTURE_LAST_LEVEL, TRUE)
181 LP_LLVM_TEXTURE_MEMBER(base_ptr, LP_JIT_TEXTURE_BASE, TRUE)
182 LP_LLVM_TEXTURE_MEMBER(row_stride, LP_JIT_TEXTURE_ROW_STRIDE, FALSE)
183 LP_LLVM_TEXTURE_MEMBER(img_stride, LP_JIT_TEXTURE_IMG_STRIDE, FALSE)
184 LP_LLVM_TEXTURE_MEMBER(mip_offsets, LP_JIT_TEXTURE_MIP_OFFSETS, FALSE)
185 LP_LLVM_TEXTURE_MEMBER(num_samples, LP_JIT_TEXTURE_NUM_SAMPLES, TRUE)
186 LP_LLVM_TEXTURE_MEMBER(sample_stride, LP_JIT_TEXTURE_SAMPLE_STRIDE, TRUE)
187
188
189 /**
190 * Fetch the specified member of the lp_jit_sampler structure.
191 * \param emit_load if TRUE, emit the LLVM load instruction to actually
192 * fetch the field's value. Otherwise, just emit the
193 * GEP code to address the field.
194 *
195 * @sa http://llvm.org/docs/GetElementPtr.html
196 */
197 static LLVMValueRef
198 lp_llvm_sampler_member(const struct lp_sampler_dynamic_state *base,
199 struct gallivm_state *gallivm,
200 LLVMValueRef context_ptr,
201 unsigned sampler_unit,
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 assert(sampler_unit < PIPE_MAX_SAMPLERS);
212
213 /* context[0] */
214 indices[0] = lp_build_const_int32(gallivm, 0);
215 /* context[0].samplers */
216 indices[1] = lp_build_const_int32(gallivm, LP_JIT_CTX_SAMPLERS);
217 /* context[0].samplers[unit] */
218 indices[2] = lp_build_const_int32(gallivm, sampler_unit);
219 /* context[0].samplers[unit].member */
220 indices[3] = lp_build_const_int32(gallivm, member_index);
221
222 ptr = LLVMBuildGEP(builder, context_ptr, indices, ARRAY_SIZE(indices), "");
223
224 if (emit_load)
225 res = LLVMBuildLoad(builder, ptr, "");
226 else
227 res = ptr;
228
229 lp_build_name(res, "context.sampler%u.%s", sampler_unit, member_name);
230
231 return res;
232 }
233
234
235 #define LP_LLVM_SAMPLER_MEMBER(_name, _index, _emit_load) \
236 static LLVMValueRef \
237 lp_llvm_sampler_##_name( const struct lp_sampler_dynamic_state *base, \
238 struct gallivm_state *gallivm, \
239 LLVMValueRef context_ptr, \
240 unsigned sampler_unit) \
241 { \
242 return lp_llvm_sampler_member(base, gallivm, context_ptr, \
243 sampler_unit, _index, #_name, _emit_load ); \
244 }
245
246
247 LP_LLVM_SAMPLER_MEMBER(min_lod, LP_JIT_SAMPLER_MIN_LOD, TRUE)
248 LP_LLVM_SAMPLER_MEMBER(max_lod, LP_JIT_SAMPLER_MAX_LOD, TRUE)
249 LP_LLVM_SAMPLER_MEMBER(lod_bias, LP_JIT_SAMPLER_LOD_BIAS, TRUE)
250 LP_LLVM_SAMPLER_MEMBER(border_color, LP_JIT_SAMPLER_BORDER_COLOR, FALSE)
251
252
253 /**
254 * Fetch the specified member of the lp_jit_image structure.
255 * \param emit_load if TRUE, emit the LLVM load instruction to actually
256 * fetch the field's value. Otherwise, just emit the
257 * GEP code to address the field.
258 *
259 * @sa http://llvm.org/docs/GetElementPtr.html
260 */
261 static LLVMValueRef
262 lp_llvm_image_member(const struct lp_sampler_dynamic_state *base,
263 struct gallivm_state *gallivm,
264 LLVMValueRef context_ptr,
265 unsigned image_unit,
266 LLVMValueRef image_unit_offset,
267 unsigned member_index,
268 const char *member_name,
269 boolean emit_load)
270 {
271 LLVMBuilderRef builder = gallivm->builder;
272 LLVMValueRef indices[4];
273 LLVMValueRef ptr;
274 LLVMValueRef res;
275
276 assert(image_unit < PIPE_MAX_SHADER_IMAGES);
277
278 /* context[0] */
279 indices[0] = lp_build_const_int32(gallivm, 0);
280 /* context[0].images */
281 indices[1] = lp_build_const_int32(gallivm, LP_JIT_CTX_IMAGES);
282 /* context[0].images[unit] */
283 indices[2] = lp_build_const_int32(gallivm, image_unit);
284 if (image_unit_offset) {
285 indices[2] = LLVMBuildAdd(gallivm->builder, indices[2], image_unit_offset, "");
286 LLVMValueRef cond = LLVMBuildICmp(gallivm->builder, LLVMIntULT, indices[2], lp_build_const_int32(gallivm, PIPE_MAX_SHADER_IMAGES), "");
287 indices[2] = LLVMBuildSelect(gallivm->builder, cond, indices[2], lp_build_const_int32(gallivm, image_unit), "");
288 }
289 /* context[0].images[unit].member */
290 indices[3] = lp_build_const_int32(gallivm, member_index);
291
292 ptr = LLVMBuildGEP(builder, context_ptr, indices, ARRAY_SIZE(indices), "");
293
294 if (emit_load)
295 res = LLVMBuildLoad(builder, ptr, "");
296 else
297 res = ptr;
298
299 lp_build_name(res, "context.image%u.%s", image_unit, member_name);
300
301 return res;
302 }
303
304
305 /**
306 * Helper macro to instantiate the functions that generate the code to
307 * fetch the members of lp_jit_image to fulfill the sampler code
308 * generator requests.
309 *
310 * This complexity is the price we have to pay to keep the image
311 * sampler code generator a reusable module without dependencies to
312 * llvmpipe internals.
313 */
314 #define LP_LLVM_IMAGE_MEMBER(_name, _index, _emit_load) \
315 static LLVMValueRef \
316 lp_llvm_image_##_name( const struct lp_sampler_dynamic_state *base, \
317 struct gallivm_state *gallivm, \
318 LLVMValueRef context_ptr, \
319 unsigned image_unit, LLVMValueRef image_unit_offset) \
320 { \
321 return lp_llvm_image_member(base, gallivm, context_ptr, \
322 image_unit, image_unit_offset, \
323 _index, #_name, _emit_load ); \
324 }
325
326
327 LP_LLVM_IMAGE_MEMBER(width, LP_JIT_IMAGE_WIDTH, TRUE)
328 LP_LLVM_IMAGE_MEMBER(height, LP_JIT_IMAGE_HEIGHT, TRUE)
329 LP_LLVM_IMAGE_MEMBER(depth, LP_JIT_IMAGE_DEPTH, TRUE)
330 LP_LLVM_IMAGE_MEMBER(base_ptr, LP_JIT_IMAGE_BASE, TRUE)
331 LP_LLVM_IMAGE_MEMBER(row_stride, LP_JIT_IMAGE_ROW_STRIDE, TRUE)
332 LP_LLVM_IMAGE_MEMBER(img_stride, LP_JIT_IMAGE_IMG_STRIDE, TRUE)
333 LP_LLVM_IMAGE_MEMBER(num_samples, LP_JIT_IMAGE_NUM_SAMPLES, TRUE)
334 LP_LLVM_IMAGE_MEMBER(sample_stride, LP_JIT_IMAGE_SAMPLE_STRIDE, TRUE)
335
336 #if LP_USE_TEXTURE_CACHE
337 static LLVMValueRef
338 lp_llvm_texture_cache_ptr(const struct lp_sampler_dynamic_state *base,
339 struct gallivm_state *gallivm,
340 LLVMValueRef thread_data_ptr,
341 unsigned unit)
342 {
343 /* We use the same cache for all units */
344 (void)unit;
345
346 return lp_jit_thread_data_cache(gallivm, thread_data_ptr);
347 }
348 #endif
349
350
351 static void
352 lp_llvm_sampler_soa_destroy(struct lp_build_sampler_soa *sampler)
353 {
354 FREE(sampler);
355 }
356
357
358 /**
359 * Fetch filtered values from texture.
360 * The 'texel' parameter returns four vectors corresponding to R, G, B, A.
361 */
362 static void
363 lp_llvm_sampler_soa_emit_fetch_texel(const struct lp_build_sampler_soa *base,
364 struct gallivm_state *gallivm,
365 const struct lp_sampler_params *params)
366 {
367 struct lp_llvm_sampler_soa *sampler = (struct lp_llvm_sampler_soa *)base;
368 unsigned texture_index = params->texture_index;
369 unsigned sampler_index = params->sampler_index;
370
371 assert(sampler_index < PIPE_MAX_SAMPLERS);
372 assert(texture_index < PIPE_MAX_SHADER_SAMPLER_VIEWS);
373
374 if (LP_PERF & PERF_NO_TEX) {
375 lp_build_sample_nop(gallivm, params->type, params->coords, params->texel);
376 return;
377 }
378
379 if (params->texture_index_offset) {
380 struct lp_build_sample_array_switch switch_info;
381 memset(&switch_info, 0, sizeof(switch_info));
382 LLVMValueRef unit = LLVMBuildAdd(gallivm->builder, params->texture_index_offset,
383 lp_build_const_int32(gallivm, texture_index), "");
384 lp_build_sample_array_init_soa(&switch_info, gallivm, params, unit,
385 0, sampler->nr_samplers);
386
387 for (unsigned i = 0; i < sampler->nr_samplers; i++) {
388 lp_build_sample_array_case_soa(&switch_info, i,
389 &sampler->dynamic_state.static_state[i].texture_state,
390 &sampler->dynamic_state.static_state[i].sampler_state,
391 &sampler->dynamic_state.base);
392 }
393 lp_build_sample_array_fini_soa(&switch_info);
394 } else {
395 lp_build_sample_soa(&sampler->dynamic_state.static_state[texture_index].texture_state,
396 &sampler->dynamic_state.static_state[sampler_index].sampler_state,
397 &sampler->dynamic_state.base,
398 gallivm, params);
399 }
400 }
401
402 /**
403 * Fetch the texture size.
404 */
405 static void
406 lp_llvm_sampler_soa_emit_size_query(const struct lp_build_sampler_soa *base,
407 struct gallivm_state *gallivm,
408 const struct lp_sampler_size_query_params *params)
409 {
410 struct lp_llvm_sampler_soa *sampler = (struct lp_llvm_sampler_soa *)base;
411
412 assert(params->texture_unit < PIPE_MAX_SHADER_SAMPLER_VIEWS);
413
414 lp_build_size_query_soa(gallivm,
415 &sampler->dynamic_state.static_state[params->texture_unit].texture_state,
416 &sampler->dynamic_state.base,
417 params);
418 }
419
420
421 struct lp_build_sampler_soa *
422 lp_llvm_sampler_soa_create(const struct lp_sampler_static_state *static_state,
423 unsigned nr_samplers)
424 {
425 struct lp_llvm_sampler_soa *sampler;
426
427 sampler = CALLOC_STRUCT(lp_llvm_sampler_soa);
428 if (!sampler)
429 return NULL;
430
431 sampler->base.destroy = lp_llvm_sampler_soa_destroy;
432 sampler->base.emit_tex_sample = lp_llvm_sampler_soa_emit_fetch_texel;
433 sampler->base.emit_size_query = lp_llvm_sampler_soa_emit_size_query;
434 sampler->dynamic_state.base.width = lp_llvm_texture_width;
435 sampler->dynamic_state.base.height = lp_llvm_texture_height;
436 sampler->dynamic_state.base.depth = lp_llvm_texture_depth;
437 sampler->dynamic_state.base.first_level = lp_llvm_texture_first_level;
438 sampler->dynamic_state.base.last_level = lp_llvm_texture_last_level;
439 sampler->dynamic_state.base.base_ptr = lp_llvm_texture_base_ptr;
440 sampler->dynamic_state.base.row_stride = lp_llvm_texture_row_stride;
441 sampler->dynamic_state.base.img_stride = lp_llvm_texture_img_stride;
442 sampler->dynamic_state.base.mip_offsets = lp_llvm_texture_mip_offsets;
443 sampler->dynamic_state.base.num_samples = lp_llvm_texture_num_samples;
444 sampler->dynamic_state.base.sample_stride = lp_llvm_texture_sample_stride;
445 sampler->dynamic_state.base.min_lod = lp_llvm_sampler_min_lod;
446 sampler->dynamic_state.base.max_lod = lp_llvm_sampler_max_lod;
447 sampler->dynamic_state.base.lod_bias = lp_llvm_sampler_lod_bias;
448 sampler->dynamic_state.base.border_color = lp_llvm_sampler_border_color;
449
450 #if LP_USE_TEXTURE_CACHE
451 sampler->dynamic_state.base.cache_ptr = lp_llvm_texture_cache_ptr;
452 #endif
453
454 sampler->dynamic_state.static_state = static_state;
455
456 sampler->nr_samplers = nr_samplers;
457 return &sampler->base;
458 }
459
460 static void
461 lp_llvm_image_soa_destroy(struct lp_build_image_soa *image)
462 {
463 FREE(image);
464 }
465
466 static void
467 lp_llvm_image_soa_emit_op(const struct lp_build_image_soa *base,
468 struct gallivm_state *gallivm,
469 const struct lp_img_params *params)
470 {
471 struct lp_llvm_image_soa *image = (struct lp_llvm_image_soa *)base;
472 unsigned image_index = params->image_index;
473 assert(image_index < PIPE_MAX_SHADER_IMAGES);
474
475 lp_build_img_op_soa(&image->dynamic_state.static_state[image_index].image_state,
476 &image->dynamic_state.base,
477 gallivm, params, params->outdata);
478 }
479
480 /**
481 * Fetch the texture size.
482 */
483 static void
484 lp_llvm_image_soa_emit_size_query(const struct lp_build_image_soa *base,
485 struct gallivm_state *gallivm,
486 const struct lp_sampler_size_query_params *params)
487 {
488 struct lp_llvm_image_soa *image = (struct lp_llvm_image_soa *)base;
489
490 assert(params->texture_unit < PIPE_MAX_SHADER_IMAGES);
491
492 lp_build_size_query_soa(gallivm,
493 &image->dynamic_state.static_state[params->texture_unit].image_state,
494 &image->dynamic_state.base,
495 params);
496 }
497
498 struct lp_build_image_soa *
499 lp_llvm_image_soa_create(const struct lp_image_static_state *static_state,
500 unsigned nr_images)
501 {
502 struct lp_llvm_image_soa *image;
503
504 image = CALLOC_STRUCT(lp_llvm_image_soa);
505 if (!image)
506 return NULL;
507
508 image->base.destroy = lp_llvm_image_soa_destroy;
509 image->base.emit_op = lp_llvm_image_soa_emit_op;
510 image->base.emit_size_query = lp_llvm_image_soa_emit_size_query;
511
512 image->dynamic_state.base.width = lp_llvm_image_width;
513 image->dynamic_state.base.height = lp_llvm_image_height;
514
515 image->dynamic_state.base.depth = lp_llvm_image_depth;
516 image->dynamic_state.base.base_ptr = lp_llvm_image_base_ptr;
517 image->dynamic_state.base.row_stride = lp_llvm_image_row_stride;
518 image->dynamic_state.base.img_stride = lp_llvm_image_img_stride;
519 image->dynamic_state.base.num_samples = lp_llvm_image_num_samples;
520 image->dynamic_state.base.sample_stride = lp_llvm_image_sample_stride;
521
522 image->dynamic_state.static_state = static_state;
523
524 image->nr_images = nr_images;
525 return &image->base;
526 }