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