Merge remote-tracking branch 'mesa-public/master' into vulkan
[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
82 /**
83 * Fetch the specified member of the lp_jit_texture structure.
84 * \param emit_load if TRUE, emit the LLVM load instruction to actually
85 * fetch the field's value. Otherwise, just emit the
86 * GEP code to address the field.
87 *
88 * @sa http://llvm.org/docs/GetElementPtr.html
89 */
90 static LLVMValueRef
91 lp_llvm_texture_member(const struct lp_sampler_dynamic_state *base,
92 struct gallivm_state *gallivm,
93 LLVMValueRef context_ptr,
94 unsigned texture_unit,
95 unsigned member_index,
96 const char *member_name,
97 boolean emit_load)
98 {
99 LLVMBuilderRef builder = gallivm->builder;
100 LLVMValueRef indices[4];
101 LLVMValueRef ptr;
102 LLVMValueRef res;
103
104 assert(texture_unit < PIPE_MAX_SHADER_SAMPLER_VIEWS);
105
106 /* context[0] */
107 indices[0] = lp_build_const_int32(gallivm, 0);
108 /* context[0].textures */
109 indices[1] = lp_build_const_int32(gallivm, LP_JIT_CTX_TEXTURES);
110 /* context[0].textures[unit] */
111 indices[2] = lp_build_const_int32(gallivm, texture_unit);
112 /* context[0].textures[unit].member */
113 indices[3] = lp_build_const_int32(gallivm, member_index);
114
115 ptr = LLVMBuildGEP(builder, context_ptr, indices, Elements(indices), "");
116
117 if (emit_load)
118 res = LLVMBuildLoad(builder, ptr, "");
119 else
120 res = ptr;
121
122 lp_build_name(res, "context.texture%u.%s", texture_unit, member_name);
123
124 return res;
125 }
126
127
128 /**
129 * Helper macro to instantiate the functions that generate the code to
130 * fetch the members of lp_jit_texture to fulfill the sampler code
131 * generator requests.
132 *
133 * This complexity is the price we have to pay to keep the texture
134 * sampler code generator a reusable module without dependencies to
135 * llvmpipe internals.
136 */
137 #define LP_LLVM_TEXTURE_MEMBER(_name, _index, _emit_load) \
138 static LLVMValueRef \
139 lp_llvm_texture_##_name( const struct lp_sampler_dynamic_state *base, \
140 struct gallivm_state *gallivm, \
141 LLVMValueRef context_ptr, \
142 unsigned texture_unit) \
143 { \
144 return lp_llvm_texture_member(base, gallivm, context_ptr, \
145 texture_unit, _index, #_name, _emit_load ); \
146 }
147
148
149 LP_LLVM_TEXTURE_MEMBER(width, LP_JIT_TEXTURE_WIDTH, TRUE)
150 LP_LLVM_TEXTURE_MEMBER(height, LP_JIT_TEXTURE_HEIGHT, TRUE)
151 LP_LLVM_TEXTURE_MEMBER(depth, LP_JIT_TEXTURE_DEPTH, TRUE)
152 LP_LLVM_TEXTURE_MEMBER(first_level, LP_JIT_TEXTURE_FIRST_LEVEL, TRUE)
153 LP_LLVM_TEXTURE_MEMBER(last_level, LP_JIT_TEXTURE_LAST_LEVEL, TRUE)
154 LP_LLVM_TEXTURE_MEMBER(base_ptr, LP_JIT_TEXTURE_BASE, TRUE)
155 LP_LLVM_TEXTURE_MEMBER(row_stride, LP_JIT_TEXTURE_ROW_STRIDE, FALSE)
156 LP_LLVM_TEXTURE_MEMBER(img_stride, LP_JIT_TEXTURE_IMG_STRIDE, FALSE)
157 LP_LLVM_TEXTURE_MEMBER(mip_offsets, LP_JIT_TEXTURE_MIP_OFFSETS, FALSE)
158
159
160 /**
161 * Fetch the specified member of the lp_jit_sampler structure.
162 * \param emit_load if TRUE, emit the LLVM load instruction to actually
163 * fetch the field's value. Otherwise, just emit the
164 * GEP code to address the field.
165 *
166 * @sa http://llvm.org/docs/GetElementPtr.html
167 */
168 static LLVMValueRef
169 lp_llvm_sampler_member(const struct lp_sampler_dynamic_state *base,
170 struct gallivm_state *gallivm,
171 LLVMValueRef context_ptr,
172 unsigned sampler_unit,
173 unsigned member_index,
174 const char *member_name,
175 boolean emit_load)
176 {
177 LLVMBuilderRef builder = gallivm->builder;
178 LLVMValueRef indices[4];
179 LLVMValueRef ptr;
180 LLVMValueRef res;
181
182 assert(sampler_unit < PIPE_MAX_SAMPLERS);
183
184 /* context[0] */
185 indices[0] = lp_build_const_int32(gallivm, 0);
186 /* context[0].samplers */
187 indices[1] = lp_build_const_int32(gallivm, LP_JIT_CTX_SAMPLERS);
188 /* context[0].samplers[unit] */
189 indices[2] = lp_build_const_int32(gallivm, sampler_unit);
190 /* context[0].samplers[unit].member */
191 indices[3] = lp_build_const_int32(gallivm, member_index);
192
193 ptr = LLVMBuildGEP(builder, context_ptr, indices, Elements(indices), "");
194
195 if (emit_load)
196 res = LLVMBuildLoad(builder, ptr, "");
197 else
198 res = ptr;
199
200 lp_build_name(res, "context.sampler%u.%s", sampler_unit, member_name);
201
202 return res;
203 }
204
205
206 #define LP_LLVM_SAMPLER_MEMBER(_name, _index, _emit_load) \
207 static LLVMValueRef \
208 lp_llvm_sampler_##_name( const struct lp_sampler_dynamic_state *base, \
209 struct gallivm_state *gallivm, \
210 LLVMValueRef context_ptr, \
211 unsigned sampler_unit) \
212 { \
213 return lp_llvm_sampler_member(base, gallivm, context_ptr, \
214 sampler_unit, _index, #_name, _emit_load ); \
215 }
216
217
218 LP_LLVM_SAMPLER_MEMBER(min_lod, LP_JIT_SAMPLER_MIN_LOD, TRUE)
219 LP_LLVM_SAMPLER_MEMBER(max_lod, LP_JIT_SAMPLER_MAX_LOD, TRUE)
220 LP_LLVM_SAMPLER_MEMBER(lod_bias, LP_JIT_SAMPLER_LOD_BIAS, TRUE)
221 LP_LLVM_SAMPLER_MEMBER(border_color, LP_JIT_SAMPLER_BORDER_COLOR, FALSE)
222
223
224 #if LP_USE_TEXTURE_CACHE
225 static LLVMValueRef
226 lp_llvm_texture_cache_ptr(const struct lp_sampler_dynamic_state *base,
227 struct gallivm_state *gallivm,
228 LLVMValueRef thread_data_ptr,
229 unsigned unit)
230 {
231 /* We use the same cache for all units */
232 (void)unit;
233
234 return lp_jit_thread_data_cache(gallivm, thread_data_ptr);
235 }
236 #endif
237
238
239 static void
240 lp_llvm_sampler_soa_destroy(struct lp_build_sampler_soa *sampler)
241 {
242 FREE(sampler);
243 }
244
245
246 /**
247 * Fetch filtered values from texture.
248 * The 'texel' parameter returns four vectors corresponding to R, G, B, A.
249 */
250 static void
251 lp_llvm_sampler_soa_emit_fetch_texel(const struct lp_build_sampler_soa *base,
252 struct gallivm_state *gallivm,
253 const struct lp_sampler_params *params)
254 {
255 struct lp_llvm_sampler_soa *sampler = (struct lp_llvm_sampler_soa *)base;
256 unsigned texture_index = params->texture_index;
257 unsigned sampler_index = params->sampler_index;
258
259 assert(sampler_index < PIPE_MAX_SAMPLERS);
260 assert(texture_index < PIPE_MAX_SHADER_SAMPLER_VIEWS);
261
262 if (LP_PERF & PERF_NO_TEX) {
263 lp_build_sample_nop(gallivm, params->type, params->coords, params->texel);
264 return;
265 }
266
267 lp_build_sample_soa(&sampler->dynamic_state.static_state[texture_index].texture_state,
268 &sampler->dynamic_state.static_state[sampler_index].sampler_state,
269 &sampler->dynamic_state.base,
270 gallivm, params);
271 }
272
273 /**
274 * Fetch the texture size.
275 */
276 static void
277 lp_llvm_sampler_soa_emit_size_query(const struct lp_build_sampler_soa *base,
278 struct gallivm_state *gallivm,
279 struct lp_type type,
280 unsigned texture_unit,
281 unsigned target,
282 LLVMValueRef context_ptr,
283 boolean is_sviewinfo,
284 enum lp_sampler_lod_property lod_property,
285 LLVMValueRef explicit_lod, /* optional */
286 LLVMValueRef *sizes_out)
287 {
288 struct lp_llvm_sampler_soa *sampler = (struct lp_llvm_sampler_soa *)base;
289
290 assert(texture_unit < PIPE_MAX_SHADER_SAMPLER_VIEWS);
291
292 lp_build_size_query_soa(gallivm,
293 &sampler->dynamic_state.static_state[texture_unit].texture_state,
294 &sampler->dynamic_state.base,
295 type,
296 texture_unit,
297 target,
298 context_ptr,
299 is_sviewinfo,
300 lod_property,
301 explicit_lod,
302 sizes_out);
303 }
304
305
306 struct lp_build_sampler_soa *
307 lp_llvm_sampler_soa_create(const struct lp_sampler_static_state *static_state)
308 {
309 struct lp_llvm_sampler_soa *sampler;
310
311 sampler = CALLOC_STRUCT(lp_llvm_sampler_soa);
312 if(!sampler)
313 return NULL;
314
315 sampler->base.destroy = lp_llvm_sampler_soa_destroy;
316 sampler->base.emit_tex_sample = lp_llvm_sampler_soa_emit_fetch_texel;
317 sampler->base.emit_size_query = lp_llvm_sampler_soa_emit_size_query;
318 sampler->dynamic_state.base.width = lp_llvm_texture_width;
319 sampler->dynamic_state.base.height = lp_llvm_texture_height;
320 sampler->dynamic_state.base.depth = lp_llvm_texture_depth;
321 sampler->dynamic_state.base.first_level = lp_llvm_texture_first_level;
322 sampler->dynamic_state.base.last_level = lp_llvm_texture_last_level;
323 sampler->dynamic_state.base.base_ptr = lp_llvm_texture_base_ptr;
324 sampler->dynamic_state.base.row_stride = lp_llvm_texture_row_stride;
325 sampler->dynamic_state.base.img_stride = lp_llvm_texture_img_stride;
326 sampler->dynamic_state.base.mip_offsets = lp_llvm_texture_mip_offsets;
327 sampler->dynamic_state.base.min_lod = lp_llvm_sampler_min_lod;
328 sampler->dynamic_state.base.max_lod = lp_llvm_sampler_max_lod;
329 sampler->dynamic_state.base.lod_bias = lp_llvm_sampler_lod_bias;
330 sampler->dynamic_state.base.border_color = lp_llvm_sampler_border_color;
331
332 #if LP_USE_TEXTURE_CACHE
333 sampler->dynamic_state.base.cache_ptr = lp_llvm_texture_cache_ptr;
334 #endif
335
336 sampler->dynamic_state.static_state = static_state;
337
338 return &sampler->base;
339 }
340