e0285c70e5b8d730f9d1843ad702211aab702380
[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 };
80
81 struct llvmpipe_image_dynamic_state
82 {
83 struct lp_sampler_dynamic_state base;
84
85 const struct lp_image_static_state *static_state;
86 };
87
88 /**
89 * This is the bridge between our sampler and the TGSI translator.
90 */
91 struct lp_llvm_image_soa
92 {
93 struct lp_build_image_soa base;
94
95 struct llvmpipe_image_dynamic_state dynamic_state;
96 };
97
98
99 /**
100 * Fetch the specified member of the lp_jit_texture structure.
101 * \param emit_load if TRUE, emit the LLVM load instruction to actually
102 * fetch the field's value. Otherwise, just emit the
103 * GEP code to address the field.
104 *
105 * @sa http://llvm.org/docs/GetElementPtr.html
106 */
107 static LLVMValueRef
108 lp_llvm_texture_member(const struct lp_sampler_dynamic_state *base,
109 struct gallivm_state *gallivm,
110 LLVMValueRef context_ptr,
111 unsigned texture_unit,
112 unsigned member_index,
113 const char *member_name,
114 boolean emit_load)
115 {
116 LLVMBuilderRef builder = gallivm->builder;
117 LLVMValueRef indices[4];
118 LLVMValueRef ptr;
119 LLVMValueRef res;
120
121 assert(texture_unit < PIPE_MAX_SHADER_SAMPLER_VIEWS);
122
123 /* context[0] */
124 indices[0] = lp_build_const_int32(gallivm, 0);
125 /* context[0].textures */
126 indices[1] = lp_build_const_int32(gallivm, LP_JIT_CTX_TEXTURES);
127 /* context[0].textures[unit] */
128 indices[2] = lp_build_const_int32(gallivm, texture_unit);
129 /* context[0].textures[unit].member */
130 indices[3] = lp_build_const_int32(gallivm, member_index);
131
132 ptr = LLVMBuildGEP(builder, context_ptr, indices, ARRAY_SIZE(indices), "");
133
134 if (emit_load)
135 res = LLVMBuildLoad(builder, ptr, "");
136 else
137 res = ptr;
138
139 lp_build_name(res, "context.texture%u.%s", texture_unit, member_name);
140
141 return res;
142 }
143
144
145 /**
146 * Helper macro to instantiate the functions that generate the code to
147 * fetch the members of lp_jit_texture to fulfill the sampler code
148 * generator requests.
149 *
150 * This complexity is the price we have to pay to keep the texture
151 * sampler code generator a reusable module without dependencies to
152 * llvmpipe internals.
153 */
154 #define LP_LLVM_TEXTURE_MEMBER(_name, _index, _emit_load) \
155 static LLVMValueRef \
156 lp_llvm_texture_##_name( const struct lp_sampler_dynamic_state *base, \
157 struct gallivm_state *gallivm, \
158 LLVMValueRef context_ptr, \
159 unsigned texture_unit) \
160 { \
161 return lp_llvm_texture_member(base, gallivm, context_ptr, \
162 texture_unit, _index, #_name, _emit_load ); \
163 }
164
165
166 LP_LLVM_TEXTURE_MEMBER(width, LP_JIT_TEXTURE_WIDTH, TRUE)
167 LP_LLVM_TEXTURE_MEMBER(height, LP_JIT_TEXTURE_HEIGHT, TRUE)
168 LP_LLVM_TEXTURE_MEMBER(depth, LP_JIT_TEXTURE_DEPTH, TRUE)
169 LP_LLVM_TEXTURE_MEMBER(first_level, LP_JIT_TEXTURE_FIRST_LEVEL, TRUE)
170 LP_LLVM_TEXTURE_MEMBER(last_level, LP_JIT_TEXTURE_LAST_LEVEL, TRUE)
171 LP_LLVM_TEXTURE_MEMBER(base_ptr, LP_JIT_TEXTURE_BASE, TRUE)
172 LP_LLVM_TEXTURE_MEMBER(row_stride, LP_JIT_TEXTURE_ROW_STRIDE, FALSE)
173 LP_LLVM_TEXTURE_MEMBER(img_stride, LP_JIT_TEXTURE_IMG_STRIDE, FALSE)
174 LP_LLVM_TEXTURE_MEMBER(mip_offsets, LP_JIT_TEXTURE_MIP_OFFSETS, FALSE)
175 LP_LLVM_TEXTURE_MEMBER(num_samples, LP_JIT_TEXTURE_NUM_SAMPLES, TRUE)
176 LP_LLVM_TEXTURE_MEMBER(sample_stride, LP_JIT_TEXTURE_SAMPLE_STRIDE, TRUE)
177
178
179 /**
180 * Fetch the specified member of the lp_jit_sampler structure.
181 * \param emit_load if TRUE, emit the LLVM load instruction to actually
182 * fetch the field's value. Otherwise, just emit the
183 * GEP code to address the field.
184 *
185 * @sa http://llvm.org/docs/GetElementPtr.html
186 */
187 static LLVMValueRef
188 lp_llvm_sampler_member(const struct lp_sampler_dynamic_state *base,
189 struct gallivm_state *gallivm,
190 LLVMValueRef context_ptr,
191 unsigned sampler_unit,
192 unsigned member_index,
193 const char *member_name,
194 boolean emit_load)
195 {
196 LLVMBuilderRef builder = gallivm->builder;
197 LLVMValueRef indices[4];
198 LLVMValueRef ptr;
199 LLVMValueRef res;
200
201 assert(sampler_unit < PIPE_MAX_SAMPLERS);
202
203 /* context[0] */
204 indices[0] = lp_build_const_int32(gallivm, 0);
205 /* context[0].samplers */
206 indices[1] = lp_build_const_int32(gallivm, LP_JIT_CTX_SAMPLERS);
207 /* context[0].samplers[unit] */
208 indices[2] = lp_build_const_int32(gallivm, sampler_unit);
209 /* context[0].samplers[unit].member */
210 indices[3] = lp_build_const_int32(gallivm, member_index);
211
212 ptr = LLVMBuildGEP(builder, context_ptr, indices, ARRAY_SIZE(indices), "");
213
214 if (emit_load)
215 res = LLVMBuildLoad(builder, ptr, "");
216 else
217 res = ptr;
218
219 lp_build_name(res, "context.sampler%u.%s", sampler_unit, member_name);
220
221 return res;
222 }
223
224
225 #define LP_LLVM_SAMPLER_MEMBER(_name, _index, _emit_load) \
226 static LLVMValueRef \
227 lp_llvm_sampler_##_name( const struct lp_sampler_dynamic_state *base, \
228 struct gallivm_state *gallivm, \
229 LLVMValueRef context_ptr, \
230 unsigned sampler_unit) \
231 { \
232 return lp_llvm_sampler_member(base, gallivm, context_ptr, \
233 sampler_unit, _index, #_name, _emit_load ); \
234 }
235
236
237 LP_LLVM_SAMPLER_MEMBER(min_lod, LP_JIT_SAMPLER_MIN_LOD, TRUE)
238 LP_LLVM_SAMPLER_MEMBER(max_lod, LP_JIT_SAMPLER_MAX_LOD, TRUE)
239 LP_LLVM_SAMPLER_MEMBER(lod_bias, LP_JIT_SAMPLER_LOD_BIAS, TRUE)
240 LP_LLVM_SAMPLER_MEMBER(border_color, LP_JIT_SAMPLER_BORDER_COLOR, FALSE)
241
242
243 /**
244 * Fetch the specified member of the lp_jit_image structure.
245 * \param emit_load if TRUE, emit the LLVM load instruction to actually
246 * fetch the field's value. Otherwise, just emit the
247 * GEP code to address the field.
248 *
249 * @sa http://llvm.org/docs/GetElementPtr.html
250 */
251 static LLVMValueRef
252 lp_llvm_image_member(const struct lp_sampler_dynamic_state *base,
253 struct gallivm_state *gallivm,
254 LLVMValueRef context_ptr,
255 unsigned image_unit,
256 unsigned member_index,
257 const char *member_name,
258 boolean emit_load)
259 {
260 LLVMBuilderRef builder = gallivm->builder;
261 LLVMValueRef indices[4];
262 LLVMValueRef ptr;
263 LLVMValueRef res;
264
265 assert(image_unit < PIPE_MAX_SHADER_IMAGES);
266
267 /* context[0] */
268 indices[0] = lp_build_const_int32(gallivm, 0);
269 /* context[0].images */
270 indices[1] = lp_build_const_int32(gallivm, LP_JIT_CTX_IMAGES);
271 /* context[0].images[unit] */
272 indices[2] = lp_build_const_int32(gallivm, image_unit);
273 /* context[0].images[unit].member */
274 indices[3] = lp_build_const_int32(gallivm, member_index);
275
276 ptr = LLVMBuildGEP(builder, context_ptr, indices, ARRAY_SIZE(indices), "");
277
278 if (emit_load)
279 res = LLVMBuildLoad(builder, ptr, "");
280 else
281 res = ptr;
282
283 lp_build_name(res, "context.image%u.%s", image_unit, member_name);
284
285 return res;
286 }
287
288
289 /**
290 * Helper macro to instantiate the functions that generate the code to
291 * fetch the members of lp_jit_image to fulfill the sampler code
292 * generator requests.
293 *
294 * This complexity is the price we have to pay to keep the image
295 * sampler code generator a reusable module without dependencies to
296 * llvmpipe internals.
297 */
298 #define LP_LLVM_IMAGE_MEMBER(_name, _index, _emit_load) \
299 static LLVMValueRef \
300 lp_llvm_image_##_name( const struct lp_sampler_dynamic_state *base, \
301 struct gallivm_state *gallivm, \
302 LLVMValueRef context_ptr, \
303 unsigned image_unit) \
304 { \
305 return lp_llvm_image_member(base, gallivm, context_ptr, \
306 image_unit, _index, #_name, _emit_load ); \
307 }
308
309
310 LP_LLVM_IMAGE_MEMBER(width, LP_JIT_IMAGE_WIDTH, TRUE)
311 LP_LLVM_IMAGE_MEMBER(height, LP_JIT_IMAGE_HEIGHT, TRUE)
312 LP_LLVM_IMAGE_MEMBER(depth, LP_JIT_IMAGE_DEPTH, TRUE)
313 LP_LLVM_IMAGE_MEMBER(base_ptr, LP_JIT_IMAGE_BASE, TRUE)
314 LP_LLVM_IMAGE_MEMBER(row_stride, LP_JIT_IMAGE_ROW_STRIDE, TRUE)
315 LP_LLVM_IMAGE_MEMBER(img_stride, LP_JIT_IMAGE_IMG_STRIDE, TRUE)
316 LP_LLVM_IMAGE_MEMBER(num_samples, LP_JIT_IMAGE_NUM_SAMPLES, TRUE)
317 LP_LLVM_IMAGE_MEMBER(sample_stride, LP_JIT_IMAGE_SAMPLE_STRIDE, TRUE)
318
319 #if LP_USE_TEXTURE_CACHE
320 static LLVMValueRef
321 lp_llvm_texture_cache_ptr(const struct lp_sampler_dynamic_state *base,
322 struct gallivm_state *gallivm,
323 LLVMValueRef thread_data_ptr,
324 unsigned unit)
325 {
326 /* We use the same cache for all units */
327 (void)unit;
328
329 return lp_jit_thread_data_cache(gallivm, thread_data_ptr);
330 }
331 #endif
332
333
334 static void
335 lp_llvm_sampler_soa_destroy(struct lp_build_sampler_soa *sampler)
336 {
337 FREE(sampler);
338 }
339
340
341 /**
342 * Fetch filtered values from texture.
343 * The 'texel' parameter returns four vectors corresponding to R, G, B, A.
344 */
345 static void
346 lp_llvm_sampler_soa_emit_fetch_texel(const struct lp_build_sampler_soa *base,
347 struct gallivm_state *gallivm,
348 const struct lp_sampler_params *params)
349 {
350 struct lp_llvm_sampler_soa *sampler = (struct lp_llvm_sampler_soa *)base;
351 unsigned texture_index = params->texture_index;
352 unsigned sampler_index = params->sampler_index;
353
354 assert(sampler_index < PIPE_MAX_SAMPLERS);
355 assert(texture_index < PIPE_MAX_SHADER_SAMPLER_VIEWS);
356
357 if (LP_PERF & PERF_NO_TEX) {
358 lp_build_sample_nop(gallivm, params->type, params->coords, params->texel);
359 return;
360 }
361
362 lp_build_sample_soa(&sampler->dynamic_state.static_state[texture_index].texture_state,
363 &sampler->dynamic_state.static_state[sampler_index].sampler_state,
364 &sampler->dynamic_state.base,
365 gallivm, params);
366 }
367
368 /**
369 * Fetch the texture size.
370 */
371 static void
372 lp_llvm_sampler_soa_emit_size_query(const struct lp_build_sampler_soa *base,
373 struct gallivm_state *gallivm,
374 const struct lp_sampler_size_query_params *params)
375 {
376 struct lp_llvm_sampler_soa *sampler = (struct lp_llvm_sampler_soa *)base;
377
378 assert(params->texture_unit < PIPE_MAX_SHADER_SAMPLER_VIEWS);
379
380 lp_build_size_query_soa(gallivm,
381 &sampler->dynamic_state.static_state[params->texture_unit].texture_state,
382 &sampler->dynamic_state.base,
383 params);
384 }
385
386
387 struct lp_build_sampler_soa *
388 lp_llvm_sampler_soa_create(const struct lp_sampler_static_state *static_state)
389 {
390 struct lp_llvm_sampler_soa *sampler;
391
392 sampler = CALLOC_STRUCT(lp_llvm_sampler_soa);
393 if (!sampler)
394 return NULL;
395
396 sampler->base.destroy = lp_llvm_sampler_soa_destroy;
397 sampler->base.emit_tex_sample = lp_llvm_sampler_soa_emit_fetch_texel;
398 sampler->base.emit_size_query = lp_llvm_sampler_soa_emit_size_query;
399 sampler->dynamic_state.base.width = lp_llvm_texture_width;
400 sampler->dynamic_state.base.height = lp_llvm_texture_height;
401 sampler->dynamic_state.base.depth = lp_llvm_texture_depth;
402 sampler->dynamic_state.base.first_level = lp_llvm_texture_first_level;
403 sampler->dynamic_state.base.last_level = lp_llvm_texture_last_level;
404 sampler->dynamic_state.base.base_ptr = lp_llvm_texture_base_ptr;
405 sampler->dynamic_state.base.row_stride = lp_llvm_texture_row_stride;
406 sampler->dynamic_state.base.img_stride = lp_llvm_texture_img_stride;
407 sampler->dynamic_state.base.mip_offsets = lp_llvm_texture_mip_offsets;
408 sampler->dynamic_state.base.num_samples = lp_llvm_texture_num_samples;
409 sampler->dynamic_state.base.sample_stride = lp_llvm_texture_sample_stride;
410 sampler->dynamic_state.base.min_lod = lp_llvm_sampler_min_lod;
411 sampler->dynamic_state.base.max_lod = lp_llvm_sampler_max_lod;
412 sampler->dynamic_state.base.lod_bias = lp_llvm_sampler_lod_bias;
413 sampler->dynamic_state.base.border_color = lp_llvm_sampler_border_color;
414
415 #if LP_USE_TEXTURE_CACHE
416 sampler->dynamic_state.base.cache_ptr = lp_llvm_texture_cache_ptr;
417 #endif
418
419 sampler->dynamic_state.static_state = static_state;
420
421 return &sampler->base;
422 }
423
424 static void
425 lp_llvm_image_soa_destroy(struct lp_build_image_soa *image)
426 {
427 FREE(image);
428 }
429
430 static void
431 lp_llvm_image_soa_emit_op(const struct lp_build_image_soa *base,
432 struct gallivm_state *gallivm,
433 const struct lp_img_params *params)
434 {
435 struct lp_llvm_image_soa *image = (struct lp_llvm_image_soa *)base;
436 unsigned image_index = params->image_index;
437 assert(image_index < PIPE_MAX_SHADER_IMAGES);
438
439 lp_build_img_op_soa(&image->dynamic_state.static_state[image_index].image_state,
440 &image->dynamic_state.base,
441 gallivm, params);
442 }
443
444 /**
445 * Fetch the texture size.
446 */
447 static void
448 lp_llvm_image_soa_emit_size_query(const struct lp_build_image_soa *base,
449 struct gallivm_state *gallivm,
450 const struct lp_sampler_size_query_params *params)
451 {
452 struct lp_llvm_image_soa *image = (struct lp_llvm_image_soa *)base;
453
454 assert(params->texture_unit < PIPE_MAX_SHADER_IMAGES);
455
456 lp_build_size_query_soa(gallivm,
457 &image->dynamic_state.static_state[params->texture_unit].image_state,
458 &image->dynamic_state.base,
459 params);
460 }
461
462 struct lp_build_image_soa *
463 lp_llvm_image_soa_create(const struct lp_image_static_state *static_state)
464 {
465 struct lp_llvm_image_soa *image;
466
467 image = CALLOC_STRUCT(lp_llvm_image_soa);
468 if (!image)
469 return NULL;
470
471 image->base.destroy = lp_llvm_image_soa_destroy;
472 image->base.emit_op = lp_llvm_image_soa_emit_op;
473 image->base.emit_size_query = lp_llvm_image_soa_emit_size_query;
474
475 image->dynamic_state.base.width = lp_llvm_image_width;
476 image->dynamic_state.base.height = lp_llvm_image_height;
477
478 image->dynamic_state.base.depth = lp_llvm_image_depth;
479 image->dynamic_state.base.base_ptr = lp_llvm_image_base_ptr;
480 image->dynamic_state.base.row_stride = lp_llvm_image_row_stride;
481 image->dynamic_state.base.img_stride = lp_llvm_image_img_stride;
482 image->dynamic_state.base.num_samples = lp_llvm_image_num_samples;
483 image->dynamic_state.base.sample_stride = lp_llvm_image_sample_stride;
484
485 image->dynamic_state.static_state = static_state;
486
487 return &image->base;
488 }