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 * Largely a copy of llvmpipe's lp_tex_sample.c
33 * Texture sampling code generation
35 * This file is nothing more than ugly glue between three largely independent
37 * - TGSI -> LLVM translation (i.e., lp_build_tgsi_soa)
38 * - texture sampling code generation (i.e., lp_build_sample_soa)
41 * All interesting code is in the functions mentioned above. There is really
42 * nothing to see here.
44 * @author Jose Fonseca <jfonseca@vmware.com>
48 #include "JitManager.h"
49 #include "state_llvm.h"
51 #include "pipe/p_defines.h"
52 #include "pipe/p_shader_tokens.h"
53 #include "gallivm/lp_bld_debug.h"
54 #include "gallivm/lp_bld_const.h"
55 #include "gallivm/lp_bld_type.h"
56 #include "gallivm/lp_bld_sample.h"
57 #include "gallivm/lp_bld_tgsi.h"
58 #include "util/u_memory.h"
60 #include "swr_tex_sample.h"
61 #include "swr_context_llvm.h"
63 using namespace SwrJit
;
66 * This provides the bridge between the sampler state store in
67 * lp_jit_context and lp_jit_texture and the sampler code
68 * generator. It provides the texture layout information required by
69 * the texture sampler code generator in terms of the state stored in
70 * lp_jit_context and lp_jit_texture in runtime.
72 struct swr_sampler_dynamic_state
{
73 struct lp_sampler_dynamic_state base
;
75 const struct swr_sampler_static_state
*static_state
;
82 * This is the bridge between our sampler and the TGSI translator.
84 struct swr_sampler_soa
{
85 struct lp_build_sampler_soa base
;
87 struct swr_sampler_dynamic_state dynamic_state
;
92 * Fetch the specified member of the lp_jit_texture structure.
93 * \param emit_load if TRUE, emit the LLVM load instruction to actually
94 * fetch the field's value. Otherwise, just emit the
95 * GEP code to address the field.
97 * @sa http://llvm.org/docs/GetElementPtr.html
100 swr_texture_member(const struct lp_sampler_dynamic_state
*base
,
101 struct gallivm_state
*gallivm
,
102 LLVMValueRef context_ptr
,
103 unsigned texture_unit
,
104 unsigned member_index
,
105 const char *member_name
,
108 LLVMBuilderRef builder
= gallivm
->builder
;
109 LLVMValueRef indices
[4];
113 assert(texture_unit
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
116 indices
[0] = lp_build_const_int32(gallivm
, 0);
117 /* context[0].textures */
118 auto dynamic
= (const struct swr_sampler_dynamic_state
*)base
;
119 switch (dynamic
->shader_type
) {
120 case PIPE_SHADER_FRAGMENT
:
121 indices
[1] = lp_build_const_int32(gallivm
, swr_draw_context_texturesFS
);
123 case PIPE_SHADER_VERTEX
:
124 indices
[1] = lp_build_const_int32(gallivm
, swr_draw_context_texturesVS
);
127 assert(0 && "unsupported shader type");
130 /* context[0].textures[unit] */
131 indices
[2] = lp_build_const_int32(gallivm
, texture_unit
);
132 /* context[0].textures[unit].member */
133 indices
[3] = lp_build_const_int32(gallivm
, member_index
);
135 ptr
= LLVMBuildGEP(builder
, context_ptr
, indices
, ARRAY_SIZE(indices
), "");
138 res
= LLVMBuildLoad(builder
, ptr
, "");
142 lp_build_name(res
, "context.texture%u.%s", texture_unit
, member_name
);
149 * Helper macro to instantiate the functions that generate the code to
150 * fetch the members of lp_jit_texture to fulfill the sampler code
151 * generator requests.
153 * This complexity is the price we have to pay to keep the texture
154 * sampler code generator a reusable module without dependencies to
157 #define SWR_TEXTURE_MEMBER(_name, _emit_load) \
158 static LLVMValueRef swr_texture_##_name( \
159 const struct lp_sampler_dynamic_state *base, \
160 struct gallivm_state *gallivm, \
161 LLVMValueRef context_ptr, \
162 unsigned texture_unit) \
164 return swr_texture_member(base, \
168 swr_jit_texture_##_name, \
174 SWR_TEXTURE_MEMBER(width
, TRUE
)
175 SWR_TEXTURE_MEMBER(height
, TRUE
)
176 SWR_TEXTURE_MEMBER(depth
, TRUE
)
177 SWR_TEXTURE_MEMBER(first_level
, TRUE
)
178 SWR_TEXTURE_MEMBER(last_level
, TRUE
)
179 SWR_TEXTURE_MEMBER(base_ptr
, TRUE
)
180 SWR_TEXTURE_MEMBER(row_stride
, FALSE
)
181 SWR_TEXTURE_MEMBER(img_stride
, FALSE
)
182 SWR_TEXTURE_MEMBER(mip_offsets
, FALSE
)
186 * Fetch the specified member of the lp_jit_sampler structure.
187 * \param emit_load if TRUE, emit the LLVM load instruction to actually
188 * fetch the field's value. Otherwise, just emit the
189 * GEP code to address the field.
191 * @sa http://llvm.org/docs/GetElementPtr.html
194 swr_sampler_member(const struct lp_sampler_dynamic_state
*base
,
195 struct gallivm_state
*gallivm
,
196 LLVMValueRef context_ptr
,
197 unsigned sampler_unit
,
198 unsigned member_index
,
199 const char *member_name
,
202 LLVMBuilderRef builder
= gallivm
->builder
;
203 LLVMValueRef indices
[4];
207 assert(sampler_unit
< PIPE_MAX_SAMPLERS
);
210 indices
[0] = lp_build_const_int32(gallivm
, 0);
211 /* context[0].samplers */
212 auto dynamic
= (const struct swr_sampler_dynamic_state
*)base
;
213 switch (dynamic
->shader_type
) {
214 case PIPE_SHADER_FRAGMENT
:
215 indices
[1] = lp_build_const_int32(gallivm
, swr_draw_context_samplersFS
);
217 case PIPE_SHADER_VERTEX
:
218 indices
[1] = lp_build_const_int32(gallivm
, swr_draw_context_samplersVS
);
221 assert(0 && "unsupported shader type");
224 /* context[0].samplers[unit] */
225 indices
[2] = lp_build_const_int32(gallivm
, sampler_unit
);
226 /* context[0].samplers[unit].member */
227 indices
[3] = lp_build_const_int32(gallivm
, member_index
);
229 ptr
= LLVMBuildGEP(builder
, context_ptr
, indices
, ARRAY_SIZE(indices
), "");
232 res
= LLVMBuildLoad(builder
, ptr
, "");
236 lp_build_name(res
, "context.sampler%u.%s", sampler_unit
, member_name
);
242 #define SWR_SAMPLER_MEMBER(_name, _emit_load) \
243 static LLVMValueRef swr_sampler_##_name( \
244 const struct lp_sampler_dynamic_state *base, \
245 struct gallivm_state *gallivm, \
246 LLVMValueRef context_ptr, \
247 unsigned sampler_unit) \
249 return swr_sampler_member(base, \
253 swr_jit_sampler_##_name, \
259 SWR_SAMPLER_MEMBER(min_lod
, TRUE
)
260 SWR_SAMPLER_MEMBER(max_lod
, TRUE
)
261 SWR_SAMPLER_MEMBER(lod_bias
, TRUE
)
262 SWR_SAMPLER_MEMBER(border_color
, FALSE
)
266 swr_sampler_soa_destroy(struct lp_build_sampler_soa
*sampler
)
273 * Fetch filtered values from texture.
274 * The 'texel' parameter returns four vectors corresponding to R, G, B, A.
277 swr_sampler_soa_emit_fetch_texel(const struct lp_build_sampler_soa
*base
,
278 struct gallivm_state
*gallivm
,
279 const struct lp_sampler_params
*params
)
281 struct swr_sampler_soa
*sampler
= (struct swr_sampler_soa
*)base
;
282 unsigned texture_index
= params
->texture_index
;
283 unsigned sampler_index
= params
->sampler_index
;
285 assert(sampler_index
< PIPE_MAX_SAMPLERS
);
286 assert(texture_index
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
289 lp_build_sample_nop(gallivm
, params
->type
, params
->coords
, params
->texel
);
292 &sampler
->dynamic_state
.static_state
[texture_index
].texture_state
,
293 &sampler
->dynamic_state
.static_state
[sampler_index
].sampler_state
,
294 &sampler
->dynamic_state
.base
,
301 * Fetch the texture size.
304 swr_sampler_soa_emit_size_query(const struct lp_build_sampler_soa
*base
,
305 struct gallivm_state
*gallivm
,
306 const struct lp_sampler_size_query_params
*params
)
308 struct swr_sampler_soa
*sampler
= (struct swr_sampler_soa
*)base
;
310 assert(params
->texture_unit
< PIPE_MAX_SHADER_SAMPLER_VIEWS
);
312 lp_build_size_query_soa(
314 &sampler
->dynamic_state
.static_state
[params
->texture_unit
].texture_state
,
315 &sampler
->dynamic_state
.base
,
320 struct lp_build_sampler_soa
*
321 swr_sampler_soa_create(const struct swr_sampler_static_state
*static_state
,
322 unsigned shader_type
)
324 struct swr_sampler_soa
*sampler
;
326 sampler
= CALLOC_STRUCT(swr_sampler_soa
);
330 sampler
->base
.destroy
= swr_sampler_soa_destroy
;
331 sampler
->base
.emit_tex_sample
= swr_sampler_soa_emit_fetch_texel
;
332 sampler
->base
.emit_size_query
= swr_sampler_soa_emit_size_query
;
333 sampler
->dynamic_state
.base
.width
= swr_texture_width
;
334 sampler
->dynamic_state
.base
.height
= swr_texture_height
;
335 sampler
->dynamic_state
.base
.depth
= swr_texture_depth
;
336 sampler
->dynamic_state
.base
.first_level
= swr_texture_first_level
;
337 sampler
->dynamic_state
.base
.last_level
= swr_texture_last_level
;
338 sampler
->dynamic_state
.base
.base_ptr
= swr_texture_base_ptr
;
339 sampler
->dynamic_state
.base
.row_stride
= swr_texture_row_stride
;
340 sampler
->dynamic_state
.base
.img_stride
= swr_texture_img_stride
;
341 sampler
->dynamic_state
.base
.mip_offsets
= swr_texture_mip_offsets
;
342 sampler
->dynamic_state
.base
.min_lod
= swr_sampler_min_lod
;
343 sampler
->dynamic_state
.base
.max_lod
= swr_sampler_max_lod
;
344 sampler
->dynamic_state
.base
.lod_bias
= swr_sampler_lod_bias
;
345 sampler
->dynamic_state
.base
.border_color
= swr_sampler_border_color
;
347 sampler
->dynamic_state
.static_state
= static_state
;
349 sampler
->dynamic_state
.shader_type
= shader_type
;
351 return &sampler
->base
;