40731aceb970b4227b540b03dc671db921fa34a3
[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 "gen_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 "gen_surf_state_llvm.h"
62 #include "gen_swr_context_llvm.h"
63
64 using namespace SwrJit;
65
66 /**
67 * This provides the bridge between the sampler state store in
68 * lp_jit_context and lp_jit_texture and the sampler code
69 * generator. It provides the texture layout information required by
70 * the texture sampler code generator in terms of the state stored in
71 * lp_jit_context and lp_jit_texture in runtime.
72 */
73 struct swr_sampler_dynamic_state {
74 struct lp_sampler_dynamic_state base;
75
76 const struct swr_sampler_static_state *static_state;
77
78 enum pipe_shader_type shader_type;
79 };
80
81
82 /**
83 * This is the bridge between our sampler and the TGSI translator.
84 */
85 struct swr_sampler_soa {
86 struct lp_build_sampler_soa base;
87
88 struct swr_sampler_dynamic_state dynamic_state;
89 };
90
91
92 /**
93 * Fetch the specified member of the lp_jit_texture structure.
94 * \param emit_load if TRUE, emit the LLVM load instruction to actually
95 * fetch the field's value. Otherwise, just emit the
96 * GEP code to address the field.
97 *
98 * @sa http://llvm.org/docs/GetElementPtr.html
99 */
100 static LLVMValueRef
101 swr_texture_member(const struct lp_sampler_dynamic_state *base,
102 struct gallivm_state *gallivm,
103 LLVMValueRef context_ptr,
104 unsigned texture_unit,
105 unsigned member_index,
106 const char *member_name,
107 boolean emit_load)
108 {
109 LLVMBuilderRef builder = gallivm->builder;
110 LLVMValueRef indices[4];
111 LLVMValueRef ptr;
112 LLVMValueRef res;
113
114 assert(texture_unit < PIPE_MAX_SHADER_SAMPLER_VIEWS);
115
116 /* context[0] */
117 indices[0] = lp_build_const_int32(gallivm, 0);
118 /* context[0].textures */
119 auto dynamic = (const struct swr_sampler_dynamic_state *)base;
120 switch (dynamic->shader_type) {
121 case PIPE_SHADER_FRAGMENT:
122 indices[1] = lp_build_const_int32(gallivm, swr_draw_context_texturesFS);
123 break;
124 case PIPE_SHADER_VERTEX:
125 indices[1] = lp_build_const_int32(gallivm, swr_draw_context_texturesVS);
126 break;
127 case PIPE_SHADER_GEOMETRY:
128 indices[1] = lp_build_const_int32(gallivm, swr_draw_context_texturesGS);
129 break;
130 case PIPE_SHADER_TESS_CTRL:
131 indices[1] = lp_build_const_int32(gallivm, swr_draw_context_texturesTCS);
132 break;
133 case PIPE_SHADER_TESS_EVAL:
134 indices[1] = lp_build_const_int32(gallivm, swr_draw_context_texturesTES);
135 break;
136 default:
137 assert(0 && "unsupported shader type");
138 break;
139 }
140 /* context[0].textures[unit] */
141 indices[2] = lp_build_const_int32(gallivm, texture_unit);
142 /* context[0].textures[unit].member */
143 indices[3] = lp_build_const_int32(gallivm, member_index);
144
145 ptr = LLVMBuildGEP(builder, context_ptr, indices, ARRAY_SIZE(indices), "");
146
147 if (emit_load)
148 res = LLVMBuildLoad(builder, ptr, "");
149 else
150 res = ptr;
151
152 lp_build_name(res, "context.texture%u.%s", texture_unit, member_name);
153
154 return res;
155 }
156
157
158 /**
159 * Helper macro to instantiate the functions that generate the code to
160 * fetch the members of lp_jit_texture to fulfill the sampler code
161 * generator requests.
162 *
163 * This complexity is the price we have to pay to keep the texture
164 * sampler code generator a reusable module without dependencies to
165 * swr internals.
166 */
167 #define SWR_TEXTURE_MEMBER(_name, _emit_load) \
168 static LLVMValueRef swr_texture_##_name( \
169 const struct lp_sampler_dynamic_state *base, \
170 struct gallivm_state *gallivm, \
171 LLVMValueRef context_ptr, \
172 unsigned texture_unit) \
173 { \
174 return swr_texture_member(base, \
175 gallivm, \
176 context_ptr, \
177 texture_unit, \
178 swr_jit_texture_##_name, \
179 #_name, \
180 _emit_load); \
181 }
182
183
184 SWR_TEXTURE_MEMBER(width, TRUE)
185 SWR_TEXTURE_MEMBER(height, TRUE)
186 SWR_TEXTURE_MEMBER(depth, TRUE)
187 SWR_TEXTURE_MEMBER(first_level, TRUE)
188 SWR_TEXTURE_MEMBER(last_level, TRUE)
189 SWR_TEXTURE_MEMBER(base_ptr, TRUE)
190 SWR_TEXTURE_MEMBER(num_samples, TRUE)
191 SWR_TEXTURE_MEMBER(sample_stride, TRUE)
192 SWR_TEXTURE_MEMBER(row_stride, FALSE)
193 SWR_TEXTURE_MEMBER(img_stride, FALSE)
194 SWR_TEXTURE_MEMBER(mip_offsets, FALSE)
195
196
197 /**
198 * Fetch the specified member of the lp_jit_sampler structure.
199 * \param emit_load if TRUE, emit the LLVM load instruction to actually
200 * fetch the field's value. Otherwise, just emit the
201 * GEP code to address the field.
202 *
203 * @sa http://llvm.org/docs/GetElementPtr.html
204 */
205 static LLVMValueRef
206 swr_sampler_member(const struct lp_sampler_dynamic_state *base,
207 struct gallivm_state *gallivm,
208 LLVMValueRef context_ptr,
209 unsigned sampler_unit,
210 unsigned member_index,
211 const char *member_name,
212 boolean emit_load)
213 {
214 LLVMBuilderRef builder = gallivm->builder;
215 LLVMValueRef indices[4];
216 LLVMValueRef ptr;
217 LLVMValueRef res;
218
219 assert(sampler_unit < PIPE_MAX_SAMPLERS);
220
221 /* context[0] */
222 indices[0] = lp_build_const_int32(gallivm, 0);
223 /* context[0].samplers */
224 auto dynamic = (const struct swr_sampler_dynamic_state *)base;
225 switch (dynamic->shader_type) {
226 case PIPE_SHADER_FRAGMENT:
227 indices[1] = lp_build_const_int32(gallivm, swr_draw_context_samplersFS);
228 break;
229 case PIPE_SHADER_VERTEX:
230 indices[1] = lp_build_const_int32(gallivm, swr_draw_context_samplersVS);
231 break;
232 case PIPE_SHADER_GEOMETRY:
233 indices[1] = lp_build_const_int32(gallivm, swr_draw_context_samplersGS);
234 break;
235 case PIPE_SHADER_TESS_CTRL:
236 indices[1] = lp_build_const_int32(gallivm, swr_draw_context_samplersTCS);
237 break;
238 case PIPE_SHADER_TESS_EVAL:
239 indices[1] = lp_build_const_int32(gallivm, swr_draw_context_samplersTES);
240 break;
241 default:
242 assert(0 && "unsupported shader type");
243 break;
244 }
245 /* context[0].samplers[unit] */
246 indices[2] = lp_build_const_int32(gallivm, sampler_unit);
247 /* context[0].samplers[unit].member */
248 indices[3] = lp_build_const_int32(gallivm, member_index);
249
250 ptr = LLVMBuildGEP(builder, context_ptr, indices, ARRAY_SIZE(indices), "");
251
252 if (emit_load)
253 res = LLVMBuildLoad(builder, ptr, "");
254 else
255 res = ptr;
256
257 lp_build_name(res, "context.sampler%u.%s", sampler_unit, member_name);
258
259 return res;
260 }
261
262
263 #define SWR_SAMPLER_MEMBER(_name, _emit_load) \
264 static LLVMValueRef swr_sampler_##_name( \
265 const struct lp_sampler_dynamic_state *base, \
266 struct gallivm_state *gallivm, \
267 LLVMValueRef context_ptr, \
268 unsigned sampler_unit) \
269 { \
270 return swr_sampler_member(base, \
271 gallivm, \
272 context_ptr, \
273 sampler_unit, \
274 swr_jit_sampler_##_name, \
275 #_name, \
276 _emit_load); \
277 }
278
279
280 SWR_SAMPLER_MEMBER(min_lod, TRUE)
281 SWR_SAMPLER_MEMBER(max_lod, TRUE)
282 SWR_SAMPLER_MEMBER(lod_bias, TRUE)
283 SWR_SAMPLER_MEMBER(border_color, FALSE)
284
285
286 static void
287 swr_sampler_soa_destroy(struct lp_build_sampler_soa *sampler)
288 {
289 FREE(sampler);
290 }
291
292
293 /**
294 * Fetch filtered values from texture.
295 * The 'texel' parameter returns four vectors corresponding to R, G, B, A.
296 */
297 static void
298 swr_sampler_soa_emit_fetch_texel(const struct lp_build_sampler_soa *base,
299 struct gallivm_state *gallivm,
300 const struct lp_sampler_params *params)
301 {
302 struct swr_sampler_soa *sampler = (struct swr_sampler_soa *)base;
303 unsigned texture_index = params->texture_index;
304 unsigned sampler_index = params->sampler_index;
305
306 assert(sampler_index < PIPE_MAX_SAMPLERS);
307 assert(texture_index < PIPE_MAX_SHADER_SAMPLER_VIEWS);
308
309 #if 0
310 lp_build_sample_nop(gallivm, params->type, params->coords, params->texel);
311 #else
312 lp_build_sample_soa(
313 &sampler->dynamic_state.static_state[texture_index].texture_state,
314 &sampler->dynamic_state.static_state[sampler_index].sampler_state,
315 &sampler->dynamic_state.base,
316 gallivm,
317 params);
318 #endif
319 }
320
321 /**
322 * Fetch the texture size.
323 */
324 static void
325 swr_sampler_soa_emit_size_query(const struct lp_build_sampler_soa *base,
326 struct gallivm_state *gallivm,
327 const struct lp_sampler_size_query_params *params)
328 {
329 struct swr_sampler_soa *sampler = (struct swr_sampler_soa *)base;
330
331 assert(params->texture_unit < PIPE_MAX_SHADER_SAMPLER_VIEWS);
332
333 lp_build_size_query_soa(
334 gallivm,
335 &sampler->dynamic_state.static_state[params->texture_unit].texture_state,
336 &sampler->dynamic_state.base,
337 params);
338 }
339
340
341 struct lp_build_sampler_soa *
342 swr_sampler_soa_create(const struct swr_sampler_static_state *static_state,
343 enum pipe_shader_type shader_type)
344 {
345 struct swr_sampler_soa *sampler;
346
347 sampler = CALLOC_STRUCT(swr_sampler_soa);
348 if (!sampler)
349 return NULL;
350
351 sampler->base.destroy = swr_sampler_soa_destroy;
352 sampler->base.emit_tex_sample = swr_sampler_soa_emit_fetch_texel;
353 sampler->base.emit_size_query = swr_sampler_soa_emit_size_query;
354 sampler->dynamic_state.base.width = swr_texture_width;
355 sampler->dynamic_state.base.height = swr_texture_height;
356 sampler->dynamic_state.base.depth = swr_texture_depth;
357 sampler->dynamic_state.base.first_level = swr_texture_first_level;
358 sampler->dynamic_state.base.last_level = swr_texture_last_level;
359 sampler->dynamic_state.base.base_ptr = swr_texture_base_ptr;
360 sampler->dynamic_state.base.row_stride = swr_texture_row_stride;
361 sampler->dynamic_state.base.img_stride = swr_texture_img_stride;
362 sampler->dynamic_state.base.mip_offsets = swr_texture_mip_offsets;
363 sampler->dynamic_state.base.num_samples = swr_texture_num_samples;
364 sampler->dynamic_state.base.sample_stride = swr_texture_sample_stride;
365 sampler->dynamic_state.base.min_lod = swr_sampler_min_lod;
366 sampler->dynamic_state.base.max_lod = swr_sampler_max_lod;
367 sampler->dynamic_state.base.lod_bias = swr_sampler_lod_bias;
368 sampler->dynamic_state.base.border_color = swr_sampler_border_color;
369
370 sampler->dynamic_state.static_state = static_state;
371
372 sampler->dynamic_state.shader_type = shader_type;
373
374 return &sampler->base;
375 }