1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 * Texture sampling code generation
31 * This file is nothing more than ugly glue between three largely independent
33 * - TGSI -> LLVM translation (i.e., lp_build_tgsi_soa)
34 * - texture sampling code generation (i.e., lp_build_sample_soa)
37 * All interesting code is in the functions mentioned above. There is really
38 * nothing to see here.
40 * @author Jose Fonseca <jfonseca@vmware.com>
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"
51 #include "lp_tex_sample.h"
52 #include "lp_state_fs.h"
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.
63 struct llvmpipe_sampler_dynamic_state
65 struct lp_sampler_dynamic_state base
;
67 const struct lp_sampler_static_state
*static_state
;
72 * This is the bridge between our sampler and the TGSI translator.
74 struct lp_llvm_sampler_soa
76 struct lp_build_sampler_soa base
;
78 struct llvmpipe_sampler_dynamic_state dynamic_state
;
81 struct llvmpipe_image_dynamic_state
83 struct lp_sampler_dynamic_state base
;
85 const struct lp_image_static_state
*static_state
;
89 * This is the bridge between our sampler and the TGSI translator.
91 struct lp_llvm_image_soa
93 struct lp_build_image_soa base
;
95 struct llvmpipe_image_dynamic_state dynamic_state
;
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.
105 * @sa http://llvm.org/docs/GetElementPtr.html
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
,
116 LLVMBuilderRef builder
= gallivm
->builder
;
117 LLVMValueRef indices
[4];
121 assert(texture_unit
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
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
);
132 ptr
= LLVMBuildGEP(builder
, context_ptr
, indices
, ARRAY_SIZE(indices
), "");
135 res
= LLVMBuildLoad(builder
, ptr
, "");
139 lp_build_name(res
, "context.texture%u.%s", texture_unit
, member_name
);
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.
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.
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) \
161 return lp_llvm_texture_member(base, gallivm, context_ptr, \
162 texture_unit, _index, #_name, _emit_load ); \
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
)
178 * Fetch the specified member of the lp_jit_sampler structure.
179 * \param emit_load if TRUE, emit the LLVM load instruction to actually
180 * fetch the field's value. Otherwise, just emit the
181 * GEP code to address the field.
183 * @sa http://llvm.org/docs/GetElementPtr.html
186 lp_llvm_sampler_member(const struct lp_sampler_dynamic_state
*base
,
187 struct gallivm_state
*gallivm
,
188 LLVMValueRef context_ptr
,
189 unsigned sampler_unit
,
190 unsigned member_index
,
191 const char *member_name
,
194 LLVMBuilderRef builder
= gallivm
->builder
;
195 LLVMValueRef indices
[4];
199 assert(sampler_unit
< PIPE_MAX_SAMPLERS
);
202 indices
[0] = lp_build_const_int32(gallivm
, 0);
203 /* context[0].samplers */
204 indices
[1] = lp_build_const_int32(gallivm
, LP_JIT_CTX_SAMPLERS
);
205 /* context[0].samplers[unit] */
206 indices
[2] = lp_build_const_int32(gallivm
, sampler_unit
);
207 /* context[0].samplers[unit].member */
208 indices
[3] = lp_build_const_int32(gallivm
, member_index
);
210 ptr
= LLVMBuildGEP(builder
, context_ptr
, indices
, ARRAY_SIZE(indices
), "");
213 res
= LLVMBuildLoad(builder
, ptr
, "");
217 lp_build_name(res
, "context.sampler%u.%s", sampler_unit
, member_name
);
223 #define LP_LLVM_SAMPLER_MEMBER(_name, _index, _emit_load) \
224 static LLVMValueRef \
225 lp_llvm_sampler_##_name( const struct lp_sampler_dynamic_state *base, \
226 struct gallivm_state *gallivm, \
227 LLVMValueRef context_ptr, \
228 unsigned sampler_unit) \
230 return lp_llvm_sampler_member(base, gallivm, context_ptr, \
231 sampler_unit, _index, #_name, _emit_load ); \
235 LP_LLVM_SAMPLER_MEMBER(min_lod
, LP_JIT_SAMPLER_MIN_LOD
, TRUE
)
236 LP_LLVM_SAMPLER_MEMBER(max_lod
, LP_JIT_SAMPLER_MAX_LOD
, TRUE
)
237 LP_LLVM_SAMPLER_MEMBER(lod_bias
, LP_JIT_SAMPLER_LOD_BIAS
, TRUE
)
238 LP_LLVM_SAMPLER_MEMBER(border_color
, LP_JIT_SAMPLER_BORDER_COLOR
, FALSE
)
242 * Fetch the specified member of the lp_jit_image structure.
243 * \param emit_load if TRUE, emit the LLVM load instruction to actually
244 * fetch the field's value. Otherwise, just emit the
245 * GEP code to address the field.
247 * @sa http://llvm.org/docs/GetElementPtr.html
250 lp_llvm_image_member(const struct lp_sampler_dynamic_state
*base
,
251 struct gallivm_state
*gallivm
,
252 LLVMValueRef context_ptr
,
254 unsigned member_index
,
255 const char *member_name
,
258 LLVMBuilderRef builder
= gallivm
->builder
;
259 LLVMValueRef indices
[4];
263 assert(image_unit
< PIPE_MAX_SHADER_IMAGES
);
266 indices
[0] = lp_build_const_int32(gallivm
, 0);
267 /* context[0].images */
268 indices
[1] = lp_build_const_int32(gallivm
, LP_JIT_CTX_IMAGES
);
269 /* context[0].images[unit] */
270 indices
[2] = lp_build_const_int32(gallivm
, image_unit
);
271 /* context[0].images[unit].member */
272 indices
[3] = lp_build_const_int32(gallivm
, member_index
);
274 ptr
= LLVMBuildGEP(builder
, context_ptr
, indices
, ARRAY_SIZE(indices
), "");
277 res
= LLVMBuildLoad(builder
, ptr
, "");
281 lp_build_name(res
, "context.image%u.%s", image_unit
, member_name
);
288 * Helper macro to instantiate the functions that generate the code to
289 * fetch the members of lp_jit_image to fulfill the sampler code
290 * generator requests.
292 * This complexity is the price we have to pay to keep the image
293 * sampler code generator a reusable module without dependencies to
294 * llvmpipe internals.
296 #define LP_LLVM_IMAGE_MEMBER(_name, _index, _emit_load) \
297 static LLVMValueRef \
298 lp_llvm_image_##_name( const struct lp_sampler_dynamic_state *base, \
299 struct gallivm_state *gallivm, \
300 LLVMValueRef context_ptr, \
301 unsigned image_unit) \
303 return lp_llvm_image_member(base, gallivm, context_ptr, \
304 image_unit, _index, #_name, _emit_load ); \
308 LP_LLVM_IMAGE_MEMBER(width
, LP_JIT_IMAGE_WIDTH
, TRUE
)
309 LP_LLVM_IMAGE_MEMBER(height
, LP_JIT_IMAGE_HEIGHT
, TRUE
)
310 LP_LLVM_IMAGE_MEMBER(depth
, LP_JIT_IMAGE_DEPTH
, TRUE
)
311 LP_LLVM_IMAGE_MEMBER(base_ptr
, LP_JIT_IMAGE_BASE
, TRUE
)
312 LP_LLVM_IMAGE_MEMBER(row_stride
, LP_JIT_IMAGE_ROW_STRIDE
, TRUE
)
313 LP_LLVM_IMAGE_MEMBER(img_stride
, LP_JIT_IMAGE_IMG_STRIDE
, TRUE
)
315 #if LP_USE_TEXTURE_CACHE
317 lp_llvm_texture_cache_ptr(const struct lp_sampler_dynamic_state
*base
,
318 struct gallivm_state
*gallivm
,
319 LLVMValueRef thread_data_ptr
,
322 /* We use the same cache for all units */
325 return lp_jit_thread_data_cache(gallivm
, thread_data_ptr
);
331 lp_llvm_sampler_soa_destroy(struct lp_build_sampler_soa
*sampler
)
338 * Fetch filtered values from texture.
339 * The 'texel' parameter returns four vectors corresponding to R, G, B, A.
342 lp_llvm_sampler_soa_emit_fetch_texel(const struct lp_build_sampler_soa
*base
,
343 struct gallivm_state
*gallivm
,
344 const struct lp_sampler_params
*params
)
346 struct lp_llvm_sampler_soa
*sampler
= (struct lp_llvm_sampler_soa
*)base
;
347 unsigned texture_index
= params
->texture_index
;
348 unsigned sampler_index
= params
->sampler_index
;
350 assert(sampler_index
< PIPE_MAX_SAMPLERS
);
351 assert(texture_index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
353 if (LP_PERF
& PERF_NO_TEX
) {
354 lp_build_sample_nop(gallivm
, params
->type
, params
->coords
, params
->texel
);
358 lp_build_sample_soa(&sampler
->dynamic_state
.static_state
[texture_index
].texture_state
,
359 &sampler
->dynamic_state
.static_state
[sampler_index
].sampler_state
,
360 &sampler
->dynamic_state
.base
,
365 * Fetch the texture size.
368 lp_llvm_sampler_soa_emit_size_query(const struct lp_build_sampler_soa
*base
,
369 struct gallivm_state
*gallivm
,
370 const struct lp_sampler_size_query_params
*params
)
372 struct lp_llvm_sampler_soa
*sampler
= (struct lp_llvm_sampler_soa
*)base
;
374 assert(params
->texture_unit
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
376 lp_build_size_query_soa(gallivm
,
377 &sampler
->dynamic_state
.static_state
[params
->texture_unit
].texture_state
,
378 &sampler
->dynamic_state
.base
,
383 struct lp_build_sampler_soa
*
384 lp_llvm_sampler_soa_create(const struct lp_sampler_static_state
*static_state
)
386 struct lp_llvm_sampler_soa
*sampler
;
388 sampler
= CALLOC_STRUCT(lp_llvm_sampler_soa
);
392 sampler
->base
.destroy
= lp_llvm_sampler_soa_destroy
;
393 sampler
->base
.emit_tex_sample
= lp_llvm_sampler_soa_emit_fetch_texel
;
394 sampler
->base
.emit_size_query
= lp_llvm_sampler_soa_emit_size_query
;
395 sampler
->dynamic_state
.base
.width
= lp_llvm_texture_width
;
396 sampler
->dynamic_state
.base
.height
= lp_llvm_texture_height
;
397 sampler
->dynamic_state
.base
.depth
= lp_llvm_texture_depth
;
398 sampler
->dynamic_state
.base
.first_level
= lp_llvm_texture_first_level
;
399 sampler
->dynamic_state
.base
.last_level
= lp_llvm_texture_last_level
;
400 sampler
->dynamic_state
.base
.base_ptr
= lp_llvm_texture_base_ptr
;
401 sampler
->dynamic_state
.base
.row_stride
= lp_llvm_texture_row_stride
;
402 sampler
->dynamic_state
.base
.img_stride
= lp_llvm_texture_img_stride
;
403 sampler
->dynamic_state
.base
.mip_offsets
= lp_llvm_texture_mip_offsets
;
404 sampler
->dynamic_state
.base
.min_lod
= lp_llvm_sampler_min_lod
;
405 sampler
->dynamic_state
.base
.max_lod
= lp_llvm_sampler_max_lod
;
406 sampler
->dynamic_state
.base
.lod_bias
= lp_llvm_sampler_lod_bias
;
407 sampler
->dynamic_state
.base
.border_color
= lp_llvm_sampler_border_color
;
409 #if LP_USE_TEXTURE_CACHE
410 sampler
->dynamic_state
.base
.cache_ptr
= lp_llvm_texture_cache_ptr
;
413 sampler
->dynamic_state
.static_state
= static_state
;
415 return &sampler
->base
;
419 lp_llvm_image_soa_destroy(struct lp_build_image_soa
*image
)
425 lp_llvm_image_soa_emit_op(const struct lp_build_image_soa
*base
,
426 struct gallivm_state
*gallivm
,
427 const struct lp_img_params
*params
)
429 struct lp_llvm_image_soa
*image
= (struct lp_llvm_image_soa
*)base
;
430 unsigned image_index
= params
->image_index
;
431 assert(image_index
< PIPE_MAX_SHADER_IMAGES
);
433 lp_build_img_op_soa(&image
->dynamic_state
.static_state
[image_index
].image_state
,
434 &image
->dynamic_state
.base
,
439 * Fetch the texture size.
442 lp_llvm_image_soa_emit_size_query(const struct lp_build_image_soa
*base
,
443 struct gallivm_state
*gallivm
,
444 const struct lp_sampler_size_query_params
*params
)
446 struct lp_llvm_image_soa
*image
= (struct lp_llvm_image_soa
*)base
;
448 assert(params
->texture_unit
< PIPE_MAX_SHADER_IMAGES
);
450 lp_build_size_query_soa(gallivm
,
451 &image
->dynamic_state
.static_state
[params
->texture_unit
].image_state
,
452 &image
->dynamic_state
.base
,
456 struct lp_build_image_soa
*
457 lp_llvm_image_soa_create(const struct lp_image_static_state
*static_state
)
459 struct lp_llvm_image_soa
*image
;
461 image
= CALLOC_STRUCT(lp_llvm_image_soa
);
465 image
->base
.destroy
= lp_llvm_image_soa_destroy
;
466 image
->base
.emit_op
= lp_llvm_image_soa_emit_op
;
467 image
->base
.emit_size_query
= lp_llvm_image_soa_emit_size_query
;
469 image
->dynamic_state
.base
.width
= lp_llvm_image_width
;
470 image
->dynamic_state
.base
.height
= lp_llvm_image_height
;
472 image
->dynamic_state
.base
.depth
= lp_llvm_image_depth
;
473 image
->dynamic_state
.base
.base_ptr
= lp_llvm_image_base_ptr
;
474 image
->dynamic_state
.base
.row_stride
= lp_llvm_image_row_stride
;
475 image
->dynamic_state
.base
.img_stride
= lp_llvm_image_img_stride
;
477 image
->dynamic_state
.static_state
= static_state
;