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_type.h"
47 #include "gallivm/lp_bld_sample.h"
48 #include "gallivm/lp_bld_tgsi.h"
50 #include "lp_tex_sample.h"
54 * This provides the bridge between the sampler state store in lp_jit_context
55 * and lp_jit_texture and the sampler code generator. It provides the
56 * texture layout information required by the texture sampler code generator
57 * in terms of the state stored in lp_jit_context and lp_jit_texture in runtime.
59 struct llvmpipe_sampler_dynamic_state
61 struct lp_sampler_dynamic_state base
;
63 const struct lp_sampler_static_state
*static_state
;
65 LLVMValueRef context_ptr
;
70 * This is the bridge between our sampler and the TGSI translator.
72 struct lp_llvm_sampler_soa
74 struct lp_build_sampler_soa base
;
76 struct llvmpipe_sampler_dynamic_state dynamic_state
;
81 * Fetch the specified member of the lp_jit_texture structure.
83 * @sa http://llvm.org/docs/GetElementPtr.html
86 lp_llvm_texture_member(struct lp_sampler_dynamic_state
*base
,
87 LLVMBuilderRef builder
,
89 unsigned member_index
,
90 const char *member_name
)
92 struct llvmpipe_sampler_dynamic_state
*state
= (struct llvmpipe_sampler_dynamic_state
*)base
;
93 LLVMValueRef indices
[4];
97 assert(unit
< PIPE_MAX_SAMPLERS
);
100 indices
[0] = LLVMConstInt(LLVMInt32Type(), 0, 0);
101 /* context[0].textures */
102 indices
[1] = LLVMConstInt(LLVMInt32Type(), LP_JIT_CONTEXT_TEXTURES_INDEX
, 0);
103 /* context[0].textures[unit] */
104 indices
[2] = LLVMConstInt(LLVMInt32Type(), unit
, 0);
105 /* context[0].textures[unit].member */
106 indices
[3] = LLVMConstInt(LLVMInt32Type(), member_index
, 0);
108 ptr
= LLVMBuildGEP(builder
, state
->context_ptr
, indices
, Elements(indices
), "");
110 res
= LLVMBuildLoad(builder
, ptr
, "");
112 lp_build_name(res
, "context.texture%u.%s", unit
, member_name
);
119 * Helper macro to instantiate the functions that generate the code to fetch
120 * the members of lp_jit_texture to fulfill the sampler code generator requests.
122 * This complexity is the price we have to pay to keep the texture sampler code
123 * generator a reusable module without dependencies to llvmpipe internals.
125 #define LP_LLVM_TEXTURE_MEMBER(_name, _index) \
126 static LLVMValueRef \
127 lp_llvm_texture_##_name( struct lp_sampler_dynamic_state *base, \
128 LLVMBuilderRef builder, \
131 return lp_llvm_texture_member(base, builder, unit, _index, #_name ); \
135 LP_LLVM_TEXTURE_MEMBER(width
, LP_JIT_TEXTURE_WIDTH
)
136 LP_LLVM_TEXTURE_MEMBER(height
, LP_JIT_TEXTURE_HEIGHT
)
137 LP_LLVM_TEXTURE_MEMBER(depth
, LP_JIT_TEXTURE_DEPTH
)
138 LP_LLVM_TEXTURE_MEMBER(last_level
, LP_JIT_TEXTURE_LAST_LEVEL
)
139 LP_LLVM_TEXTURE_MEMBER(stride
, LP_JIT_TEXTURE_STRIDE
)
140 LP_LLVM_TEXTURE_MEMBER(data_ptr
, LP_JIT_TEXTURE_DATA
)
144 lp_llvm_sampler_soa_destroy(struct lp_build_sampler_soa
*sampler
)
151 * Fetch filtered values from texture.
152 * The 'texel' parameter returns four vectors corresponding to R, G, B, A.
155 lp_llvm_sampler_soa_emit_fetch_texel(struct lp_build_sampler_soa
*base
,
156 LLVMBuilderRef builder
,
160 const LLVMValueRef
*coords
,
161 LLVMValueRef lodbias
,
164 struct lp_llvm_sampler_soa
*sampler
= (struct lp_llvm_sampler_soa
*)base
;
166 assert(unit
< PIPE_MAX_SAMPLERS
);
168 lp_build_sample_soa(builder
,
169 &sampler
->dynamic_state
.static_state
[unit
],
170 &sampler
->dynamic_state
.base
,
180 struct lp_build_sampler_soa
*
181 lp_llvm_sampler_soa_create(const struct lp_sampler_static_state
*static_state
,
182 LLVMValueRef context_ptr
)
184 struct lp_llvm_sampler_soa
*sampler
;
186 sampler
= CALLOC_STRUCT(lp_llvm_sampler_soa
);
190 sampler
->base
.destroy
= lp_llvm_sampler_soa_destroy
;
191 sampler
->base
.emit_fetch_texel
= lp_llvm_sampler_soa_emit_fetch_texel
;
192 sampler
->dynamic_state
.base
.width
= lp_llvm_texture_width
;
193 sampler
->dynamic_state
.base
.height
= lp_llvm_texture_height
;
194 sampler
->dynamic_state
.base
.depth
= lp_llvm_texture_depth
;
195 sampler
->dynamic_state
.base
.last_level
= lp_llvm_texture_last_level
;
196 sampler
->dynamic_state
.base
.stride
= lp_llvm_texture_stride
;
197 sampler
->dynamic_state
.base
.data_ptr
= lp_llvm_texture_data_ptr
;
198 sampler
->dynamic_state
.static_state
= static_state
;
199 sampler
->dynamic_state
.context_ptr
= context_ptr
;
201 return &sampler
->base
;