8e01e32e2803043556ee7704e1784deb51bb7f12
[mesa.git] / src / gallium / drivers / swr / swr_tex_sample.cpp
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 * Largely a copy of llvmpipe's lp_tex_sample.c
30 */
31
32 /**
33 * Texture sampling code generation
34 *
35 * This file is nothing more than ugly glue between three largely independent
36 * entities:
37 * - TGSI -> LLVM translation (i.e., lp_build_tgsi_soa)
38 * - texture sampling code generation (i.e., lp_build_sample_soa)
39 * - SWR driver
40 *
41 * All interesting code is in the functions mentioned above. There is really
42 * nothing to see here.
43 *
44 * @author Jose Fonseca <jfonseca@vmware.com>
45 */
46
47 #include "state.h"
48 #include "JitManager.h"
49 #include "state_llvm.h"
50
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"
59
60 #include "swr_tex_sample.h"
61 #include "swr_context_llvm.h"
62
63
64 /**
65 * This provides the bridge between the sampler state store in
66 * lp_jit_context and lp_jit_texture and the sampler code
67 * generator. It provides the texture layout information required by
68 * the texture sampler code generator in terms of the state stored in
69 * lp_jit_context and lp_jit_texture in runtime.
70 */
71 struct swr_sampler_dynamic_state {
72 struct lp_sampler_dynamic_state base;
73
74 const struct swr_sampler_static_state *static_state;
75 };
76
77
78 /**
79 * This is the bridge between our sampler and the TGSI translator.
80 */
81 struct swr_sampler_soa {
82 struct lp_build_sampler_soa base;
83
84 struct swr_sampler_dynamic_state dynamic_state;
85 };
86
87
88 /**
89 * Fetch the specified member of the lp_jit_texture structure.
90 * \param emit_load if TRUE, emit the LLVM load instruction to actually
91 * fetch the field's value. Otherwise, just emit the
92 * GEP code to address the field.
93 *
94 * @sa http://llvm.org/docs/GetElementPtr.html
95 */
96 static LLVMValueRef
97 swr_texture_member(const struct lp_sampler_dynamic_state *base,
98 struct gallivm_state *gallivm,
99 LLVMValueRef context_ptr,
100 unsigned texture_unit,
101 unsigned member_index,
102 const char *member_name,
103 boolean emit_load)
104 {
105 LLVMBuilderRef builder = gallivm->builder;
106 LLVMValueRef indices[4];
107 LLVMValueRef ptr;
108 LLVMValueRef res;
109
110 assert(texture_unit < PIPE_MAX_SHADER_SAMPLER_VIEWS);
111
112 /* context[0] */
113 indices[0] = lp_build_const_int32(gallivm, 0);
114 /* context[0].textures */
115 indices[1] = lp_build_const_int32(gallivm, swr_draw_context_texturesFS);
116 /* context[0].textures[unit] */
117 indices[2] = lp_build_const_int32(gallivm, texture_unit);
118 /* context[0].textures[unit].member */
119 indices[3] = lp_build_const_int32(gallivm, member_index);
120
121 ptr = LLVMBuildGEP(builder, context_ptr, indices, Elements(indices), "");
122
123 if (emit_load)
124 res = LLVMBuildLoad(builder, ptr, "");
125 else
126 res = ptr;
127
128 lp_build_name(res, "context.texture%u.%s", texture_unit, member_name);
129
130 return res;
131 }
132
133
134 /**
135 * Helper macro to instantiate the functions that generate the code to
136 * fetch the members of lp_jit_texture to fulfill the sampler code
137 * generator requests.
138 *
139 * This complexity is the price we have to pay to keep the texture
140 * sampler code generator a reusable module without dependencies to
141 * swr internals.
142 */
143 #define SWR_TEXTURE_MEMBER(_name, _emit_load) \
144 static LLVMValueRef swr_texture_##_name( \
145 const struct lp_sampler_dynamic_state *base, \
146 struct gallivm_state *gallivm, \
147 LLVMValueRef context_ptr, \
148 unsigned texture_unit) \
149 { \
150 return swr_texture_member(base, \
151 gallivm, \
152 context_ptr, \
153 texture_unit, \
154 swr_jit_texture_##_name, \
155 #_name, \
156 _emit_load); \
157 }
158
159
160 SWR_TEXTURE_MEMBER(width, TRUE)
161 SWR_TEXTURE_MEMBER(height, TRUE)
162 SWR_TEXTURE_MEMBER(depth, TRUE)
163 SWR_TEXTURE_MEMBER(first_level, TRUE)
164 SWR_TEXTURE_MEMBER(last_level, TRUE)
165 SWR_TEXTURE_MEMBER(base_ptr, TRUE)
166 SWR_TEXTURE_MEMBER(row_stride, FALSE)
167 SWR_TEXTURE_MEMBER(img_stride, FALSE)
168 SWR_TEXTURE_MEMBER(mip_offsets, FALSE)
169
170
171 /**
172 * Fetch the specified member of the lp_jit_sampler structure.
173 * \param emit_load if TRUE, emit the LLVM load instruction to actually
174 * fetch the field's value. Otherwise, just emit the
175 * GEP code to address the field.
176 *
177 * @sa http://llvm.org/docs/GetElementPtr.html
178 */
179 static LLVMValueRef
180 swr_sampler_member(const struct lp_sampler_dynamic_state *base,
181 struct gallivm_state *gallivm,
182 LLVMValueRef context_ptr,
183 unsigned sampler_unit,
184 unsigned member_index,
185 const char *member_name,
186 boolean emit_load)
187 {
188 LLVMBuilderRef builder = gallivm->builder;
189 LLVMValueRef indices[4];
190 LLVMValueRef ptr;
191 LLVMValueRef res;
192
193 assert(sampler_unit < PIPE_MAX_SAMPLERS);
194
195 /* context[0] */
196 indices[0] = lp_build_const_int32(gallivm, 0);
197 /* context[0].samplers */
198 indices[1] = lp_build_const_int32(gallivm, swr_draw_context_samplersFS);
199 /* context[0].samplers[unit] */
200 indices[2] = lp_build_const_int32(gallivm, sampler_unit);
201 /* context[0].samplers[unit].member */
202 indices[3] = lp_build_const_int32(gallivm, member_index);
203
204 ptr = LLVMBuildGEP(builder, context_ptr, indices, Elements(indices), "");
205
206 if (emit_load)
207 res = LLVMBuildLoad(builder, ptr, "");
208 else
209 res = ptr;
210
211 lp_build_name(res, "context.sampler%u.%s", sampler_unit, member_name);
212
213 return res;
214 }
215
216
217 #define SWR_SAMPLER_MEMBER(_name, _emit_load) \
218 static LLVMValueRef swr_sampler_##_name( \
219 const struct lp_sampler_dynamic_state *base, \
220 struct gallivm_state *gallivm, \
221 LLVMValueRef context_ptr, \
222 unsigned sampler_unit) \
223 { \
224 return swr_sampler_member(base, \
225 gallivm, \
226 context_ptr, \
227 sampler_unit, \
228 swr_jit_sampler_##_name, \
229 #_name, \
230 _emit_load); \
231 }
232
233
234 SWR_SAMPLER_MEMBER(min_lod, TRUE)
235 SWR_SAMPLER_MEMBER(max_lod, TRUE)
236 SWR_SAMPLER_MEMBER(lod_bias, TRUE)
237 SWR_SAMPLER_MEMBER(border_color, FALSE)
238
239
240 static void
241 swr_sampler_soa_destroy(struct lp_build_sampler_soa *sampler)
242 {
243 FREE(sampler);
244 }
245
246
247 /**
248 * Fetch filtered values from texture.
249 * The 'texel' parameter returns four vectors corresponding to R, G, B, A.
250 */
251 static void
252 swr_sampler_soa_emit_fetch_texel(const struct lp_build_sampler_soa *base,
253 struct gallivm_state *gallivm,
254 const struct lp_sampler_params *params)
255 {
256 struct swr_sampler_soa *sampler = (struct swr_sampler_soa *)base;
257 unsigned texture_index = params->texture_index;
258 unsigned sampler_index = params->sampler_index;
259
260 assert(sampler_index < PIPE_MAX_SAMPLERS);
261 assert(texture_index < PIPE_MAX_SHADER_SAMPLER_VIEWS);
262
263 #if 0
264 lp_build_sample_nop(gallivm, params->type, params->coords, params->texel);
265 #else
266 lp_build_sample_soa(
267 &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,
271 params);
272 #endif
273 }
274
275 /**
276 * Fetch the texture size.
277 */
278 static void
279 swr_sampler_soa_emit_size_query(const struct lp_build_sampler_soa *base,
280 struct gallivm_state *gallivm,
281 struct lp_type type,
282 unsigned texture_unit,
283 unsigned target,
284 LLVMValueRef context_ptr,
285 boolean is_sviewinfo,
286 enum lp_sampler_lod_property lod_property,
287 LLVMValueRef explicit_lod, /* optional */
288 LLVMValueRef *sizes_out)
289 {
290 struct swr_sampler_soa *sampler = (struct swr_sampler_soa *)base;
291
292 assert(texture_unit < PIPE_MAX_SHADER_SAMPLER_VIEWS);
293
294 lp_build_size_query_soa(
295 gallivm,
296 &sampler->dynamic_state.static_state[texture_unit].texture_state,
297 &sampler->dynamic_state.base,
298 type,
299 texture_unit,
300 target,
301 context_ptr,
302 is_sviewinfo,
303 lod_property,
304 explicit_lod,
305 sizes_out);
306 }
307
308
309 struct lp_build_sampler_soa *
310 swr_sampler_soa_create(const struct swr_sampler_static_state *static_state)
311 {
312 struct swr_sampler_soa *sampler;
313
314 sampler = CALLOC_STRUCT(swr_sampler_soa);
315 if (!sampler)
316 return NULL;
317
318 sampler->base.destroy = swr_sampler_soa_destroy;
319 sampler->base.emit_tex_sample = swr_sampler_soa_emit_fetch_texel;
320 sampler->base.emit_size_query = swr_sampler_soa_emit_size_query;
321 sampler->dynamic_state.base.width = swr_texture_width;
322 sampler->dynamic_state.base.height = swr_texture_height;
323 sampler->dynamic_state.base.depth = swr_texture_depth;
324 sampler->dynamic_state.base.first_level = swr_texture_first_level;
325 sampler->dynamic_state.base.last_level = swr_texture_last_level;
326 sampler->dynamic_state.base.base_ptr = swr_texture_base_ptr;
327 sampler->dynamic_state.base.row_stride = swr_texture_row_stride;
328 sampler->dynamic_state.base.img_stride = swr_texture_img_stride;
329 sampler->dynamic_state.base.mip_offsets = swr_texture_mip_offsets;
330 sampler->dynamic_state.base.min_lod = swr_sampler_min_lod;
331 sampler->dynamic_state.base.max_lod = swr_sampler_max_lod;
332 sampler->dynamic_state.base.lod_bias = swr_sampler_lod_bias;
333 sampler->dynamic_state.base.border_color = swr_sampler_border_color;
334
335 sampler->dynamic_state.static_state = static_state;
336
337 return &sampler->base;
338 }