draw: add support for indirect texture access
[mesa.git] / src / gallium / auxiliary / draw / draw_llvm_sample.c
1 /**************************************************************************
2 *
3 * Copyright 2010 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 * @author Jose Fonseca <jfonseca@vmware.com>
31 */
32
33 #include "pipe/p_defines.h"
34 #include "pipe/p_shader_tokens.h"
35 #include "gallivm/lp_bld_const.h"
36 #include "gallivm/lp_bld_debug.h"
37 #include "gallivm/lp_bld_type.h"
38 #include "gallivm/lp_bld_sample.h"
39 #include "gallivm/lp_bld_tgsi.h"
40
41
42 #include "util/u_debug.h"
43 #include "util/u_memory.h"
44 #include "util/u_pointer.h"
45 #include "util/u_string.h"
46
47 #include "draw_llvm.h"
48
49
50 /**
51 * This provides the bridge between the sampler state store in
52 * lp_jit_context and lp_jit_texture and the sampler code
53 * generator. It provides the texture layout information required by
54 * the texture sampler code generator in terms of the state stored in
55 * lp_jit_context and lp_jit_texture in runtime.
56 */
57 struct draw_llvm_sampler_dynamic_state
58 {
59 struct lp_sampler_dynamic_state base;
60
61 const struct draw_sampler_static_state *static_state;
62 };
63
64
65 /**
66 * This is the bridge between our sampler and the TGSI translator.
67 */
68 struct draw_llvm_sampler_soa
69 {
70 struct lp_build_sampler_soa base;
71
72 struct draw_llvm_sampler_dynamic_state dynamic_state;
73
74 unsigned nr_samplers;
75 };
76
77 struct draw_llvm_image_dynamic_state
78 {
79 struct lp_sampler_dynamic_state base;
80
81 const struct draw_image_static_state *static_state;
82 };
83
84 struct draw_llvm_image_soa
85 {
86 struct lp_build_image_soa base;
87
88 struct draw_llvm_image_dynamic_state dynamic_state;
89 };
90
91 /**
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.
96 *
97 * @sa http://llvm.org/docs/GetElementPtr.html
98 */
99 static LLVMValueRef
100 draw_llvm_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,
106 boolean emit_load)
107 {
108 LLVMBuilderRef builder = gallivm->builder;
109 LLVMValueRef indices[4];
110 LLVMValueRef ptr;
111 LLVMValueRef res;
112
113 debug_assert(texture_unit < PIPE_MAX_SHADER_SAMPLER_VIEWS);
114
115 /* context[0] */
116 indices[0] = lp_build_const_int32(gallivm, 0);
117 /* context[0].textures */
118 indices[1] = lp_build_const_int32(gallivm, DRAW_JIT_CTX_TEXTURES);
119 /* context[0].textures[unit] */
120 indices[2] = lp_build_const_int32(gallivm, texture_unit);
121 /* context[0].textures[unit].member */
122 indices[3] = lp_build_const_int32(gallivm, member_index);
123
124 ptr = LLVMBuildGEP(builder, context_ptr, indices, ARRAY_SIZE(indices), "");
125
126 if (emit_load)
127 res = LLVMBuildLoad(builder, ptr, "");
128 else
129 res = ptr;
130
131 lp_build_name(res, "context.texture%u.%s", texture_unit, member_name);
132
133 return res;
134 }
135
136
137 /**
138 * Fetch the specified member of the lp_jit_sampler structure.
139 * \param emit_load if TRUE, emit the LLVM load instruction to actually
140 * fetch the field's value. Otherwise, just emit the
141 * GEP code to address the field.
142 *
143 * @sa http://llvm.org/docs/GetElementPtr.html
144 */
145 static LLVMValueRef
146 draw_llvm_sampler_member(const struct lp_sampler_dynamic_state *base,
147 struct gallivm_state *gallivm,
148 LLVMValueRef context_ptr,
149 unsigned sampler_unit,
150 unsigned member_index,
151 const char *member_name,
152 boolean emit_load)
153 {
154 LLVMBuilderRef builder = gallivm->builder;
155 LLVMValueRef indices[4];
156 LLVMValueRef ptr;
157 LLVMValueRef res;
158
159 debug_assert(sampler_unit < PIPE_MAX_SAMPLERS);
160
161 /* context[0] */
162 indices[0] = lp_build_const_int32(gallivm, 0);
163 /* context[0].samplers */
164 indices[1] = lp_build_const_int32(gallivm, DRAW_JIT_CTX_SAMPLERS);
165 /* context[0].samplers[unit] */
166 indices[2] = lp_build_const_int32(gallivm, sampler_unit);
167 /* context[0].samplers[unit].member */
168 indices[3] = lp_build_const_int32(gallivm, member_index);
169
170 ptr = LLVMBuildGEP(builder, context_ptr, indices, ARRAY_SIZE(indices), "");
171
172 if (emit_load)
173 res = LLVMBuildLoad(builder, ptr, "");
174 else
175 res = ptr;
176
177 lp_build_name(res, "context.sampler%u.%s", sampler_unit, member_name);
178
179 return res;
180 }
181
182 /**
183 * Fetch the specified member of the lp_jit_texture structure.
184 * \param emit_load if TRUE, emit the LLVM load instruction to actually
185 * fetch the field's value. Otherwise, just emit the
186 * GEP code to address the field.
187 *
188 * @sa http://llvm.org/docs/GetElementPtr.html
189 */
190 static LLVMValueRef
191 draw_llvm_image_member(const struct lp_sampler_dynamic_state *base,
192 struct gallivm_state *gallivm,
193 LLVMValueRef context_ptr,
194 unsigned image_unit,
195 unsigned member_index,
196 const char *member_name,
197 boolean emit_load)
198 {
199 LLVMBuilderRef builder = gallivm->builder;
200 LLVMValueRef indices[4];
201 LLVMValueRef ptr;
202 LLVMValueRef res;
203
204 debug_assert(image_unit < PIPE_MAX_SHADER_IMAGES);
205
206 /* context[0] */
207 indices[0] = lp_build_const_int32(gallivm, 0);
208 /* context[0].textures */
209 indices[1] = lp_build_const_int32(gallivm, DRAW_JIT_CTX_IMAGES);
210 /* context[0].textures[unit] */
211 indices[2] = lp_build_const_int32(gallivm, image_unit);
212 /* context[0].textures[unit].member */
213 indices[3] = lp_build_const_int32(gallivm, member_index);
214
215 ptr = LLVMBuildGEP(builder, context_ptr, indices, ARRAY_SIZE(indices), "");
216
217 if (emit_load)
218 res = LLVMBuildLoad(builder, ptr, "");
219 else
220 res = ptr;
221
222 lp_build_name(res, "context.image%u.%s", image_unit, member_name);
223
224 return res;
225 }
226
227 /**
228 * Helper macro to instantiate the functions that generate the code to
229 * fetch the members of lp_jit_texture to fulfill the sampler code
230 * generator requests.
231 *
232 * This complexity is the price we have to pay to keep the texture
233 * sampler code generator a reusable module without dependencies to
234 * llvmpipe internals.
235 */
236 #define DRAW_LLVM_TEXTURE_MEMBER(_name, _index, _emit_load) \
237 static LLVMValueRef \
238 draw_llvm_texture_##_name( const struct lp_sampler_dynamic_state *base, \
239 struct gallivm_state *gallivm, \
240 LLVMValueRef context_ptr, \
241 unsigned texture_unit) \
242 { \
243 return draw_llvm_texture_member(base, gallivm, context_ptr, \
244 texture_unit, _index, #_name, _emit_load ); \
245 }
246
247
248 DRAW_LLVM_TEXTURE_MEMBER(width, DRAW_JIT_TEXTURE_WIDTH, TRUE)
249 DRAW_LLVM_TEXTURE_MEMBER(height, DRAW_JIT_TEXTURE_HEIGHT, TRUE)
250 DRAW_LLVM_TEXTURE_MEMBER(depth, DRAW_JIT_TEXTURE_DEPTH, TRUE)
251 DRAW_LLVM_TEXTURE_MEMBER(first_level,DRAW_JIT_TEXTURE_FIRST_LEVEL, TRUE)
252 DRAW_LLVM_TEXTURE_MEMBER(last_level, DRAW_JIT_TEXTURE_LAST_LEVEL, TRUE)
253 DRAW_LLVM_TEXTURE_MEMBER(base_ptr, DRAW_JIT_TEXTURE_BASE, TRUE)
254 DRAW_LLVM_TEXTURE_MEMBER(row_stride, DRAW_JIT_TEXTURE_ROW_STRIDE, FALSE)
255 DRAW_LLVM_TEXTURE_MEMBER(img_stride, DRAW_JIT_TEXTURE_IMG_STRIDE, FALSE)
256 DRAW_LLVM_TEXTURE_MEMBER(mip_offsets, DRAW_JIT_TEXTURE_MIP_OFFSETS, FALSE)
257 DRAW_LLVM_TEXTURE_MEMBER(num_samples, DRAW_JIT_TEXTURE_NUM_SAMPLES, TRUE)
258 DRAW_LLVM_TEXTURE_MEMBER(sample_stride, DRAW_JIT_TEXTURE_SAMPLE_STRIDE, TRUE)
259
260 #define DRAW_LLVM_SAMPLER_MEMBER(_name, _index, _emit_load) \
261 static LLVMValueRef \
262 draw_llvm_sampler_##_name( const struct lp_sampler_dynamic_state *base, \
263 struct gallivm_state *gallivm, \
264 LLVMValueRef context_ptr, \
265 unsigned sampler_unit) \
266 { \
267 return draw_llvm_sampler_member(base, gallivm, context_ptr, \
268 sampler_unit, _index, #_name, _emit_load ); \
269 }
270
271
272 DRAW_LLVM_SAMPLER_MEMBER(min_lod, DRAW_JIT_SAMPLER_MIN_LOD, TRUE)
273 DRAW_LLVM_SAMPLER_MEMBER(max_lod, DRAW_JIT_SAMPLER_MAX_LOD, TRUE)
274 DRAW_LLVM_SAMPLER_MEMBER(lod_bias, DRAW_JIT_SAMPLER_LOD_BIAS, TRUE)
275 DRAW_LLVM_SAMPLER_MEMBER(border_color, DRAW_JIT_SAMPLER_BORDER_COLOR, FALSE)
276
277 #define DRAW_LLVM_IMAGE_MEMBER(_name, _index, _emit_load) \
278 static LLVMValueRef \
279 draw_llvm_image_##_name( const struct lp_sampler_dynamic_state *base, \
280 struct gallivm_state *gallivm, \
281 LLVMValueRef context_ptr, \
282 unsigned image_unit) \
283 { \
284 return draw_llvm_image_member(base, gallivm, context_ptr, \
285 image_unit, _index, #_name, _emit_load ); \
286 }
287
288
289 DRAW_LLVM_IMAGE_MEMBER(width, DRAW_JIT_IMAGE_WIDTH, TRUE)
290 DRAW_LLVM_IMAGE_MEMBER(height, DRAW_JIT_IMAGE_HEIGHT, TRUE)
291 DRAW_LLVM_IMAGE_MEMBER(depth, DRAW_JIT_IMAGE_DEPTH, TRUE)
292 DRAW_LLVM_IMAGE_MEMBER(base_ptr, DRAW_JIT_IMAGE_BASE, TRUE)
293 DRAW_LLVM_IMAGE_MEMBER(row_stride, DRAW_JIT_IMAGE_ROW_STRIDE, TRUE)
294 DRAW_LLVM_IMAGE_MEMBER(img_stride, DRAW_JIT_IMAGE_IMG_STRIDE, TRUE)
295 DRAW_LLVM_IMAGE_MEMBER(num_samples, DRAW_JIT_IMAGE_NUM_SAMPLES, TRUE)
296 DRAW_LLVM_IMAGE_MEMBER(sample_stride, DRAW_JIT_IMAGE_SAMPLE_STRIDE, TRUE)
297
298 static void
299 draw_llvm_sampler_soa_destroy(struct lp_build_sampler_soa *sampler)
300 {
301 FREE(sampler);
302 }
303
304
305 /**
306 * Fetch filtered values from texture.
307 * The 'texel' parameter returns four vectors corresponding to R, G, B, A.
308 */
309 static void
310 draw_llvm_sampler_soa_emit_fetch_texel(const struct lp_build_sampler_soa *base,
311 struct gallivm_state *gallivm,
312 const struct lp_sampler_params *params)
313 {
314 struct draw_llvm_sampler_soa *sampler = (struct draw_llvm_sampler_soa *)base;
315 unsigned texture_index = params->texture_index;
316 unsigned sampler_index = params->sampler_index;
317
318 assert(texture_index < PIPE_MAX_SHADER_SAMPLER_VIEWS);
319 assert(sampler_index < PIPE_MAX_SAMPLERS);
320
321 if (params->texture_index_offset) {
322 struct lp_build_sample_array_switch switch_info;
323 memset(&switch_info, 0, sizeof(switch_info));
324 LLVMValueRef unit = LLVMBuildAdd(gallivm->builder, params->texture_index_offset,
325 lp_build_const_int32(gallivm, texture_index), "");
326 lp_build_sample_array_init_soa(&switch_info, gallivm, params, unit,
327 0, sampler->nr_samplers);
328
329 for (unsigned i = 0; i < sampler->nr_samplers; i++) {
330 lp_build_sample_array_case_soa(&switch_info, i,
331 &sampler->dynamic_state.static_state[i].texture_state,
332 &sampler->dynamic_state.static_state[i].sampler_state,
333 &sampler->dynamic_state.base);
334 }
335 lp_build_sample_array_fini_soa(&switch_info);
336 } else {
337 lp_build_sample_soa(&sampler->dynamic_state.static_state[texture_index].texture_state,
338 &sampler->dynamic_state.static_state[sampler_index].sampler_state,
339 &sampler->dynamic_state.base,
340 gallivm, params);
341 }
342 }
343
344
345 /**
346 * Fetch the texture size.
347 */
348 static void
349 draw_llvm_sampler_soa_emit_size_query(const struct lp_build_sampler_soa *base,
350 struct gallivm_state *gallivm,
351 const struct lp_sampler_size_query_params *params)
352 {
353 struct draw_llvm_sampler_soa *sampler = (struct draw_llvm_sampler_soa *)base;
354
355 assert(params->texture_unit < PIPE_MAX_SHADER_SAMPLER_VIEWS);
356
357 lp_build_size_query_soa(gallivm,
358 &sampler->dynamic_state.static_state[params->texture_unit].texture_state,
359 &sampler->dynamic_state.base,
360 params);
361 }
362
363 struct lp_build_sampler_soa *
364 draw_llvm_sampler_soa_create(const struct draw_sampler_static_state *static_state,
365 unsigned nr_samplers)
366 {
367 struct draw_llvm_sampler_soa *sampler;
368
369 sampler = CALLOC_STRUCT(draw_llvm_sampler_soa);
370 if (!sampler)
371 return NULL;
372
373 sampler->base.destroy = draw_llvm_sampler_soa_destroy;
374 sampler->base.emit_tex_sample = draw_llvm_sampler_soa_emit_fetch_texel;
375 sampler->base.emit_size_query = draw_llvm_sampler_soa_emit_size_query;
376 sampler->dynamic_state.base.width = draw_llvm_texture_width;
377 sampler->dynamic_state.base.height = draw_llvm_texture_height;
378 sampler->dynamic_state.base.depth = draw_llvm_texture_depth;
379 sampler->dynamic_state.base.first_level = draw_llvm_texture_first_level;
380 sampler->dynamic_state.base.last_level = draw_llvm_texture_last_level;
381 sampler->dynamic_state.base.row_stride = draw_llvm_texture_row_stride;
382 sampler->dynamic_state.base.img_stride = draw_llvm_texture_img_stride;
383 sampler->dynamic_state.base.base_ptr = draw_llvm_texture_base_ptr;
384 sampler->dynamic_state.base.mip_offsets = draw_llvm_texture_mip_offsets;
385 sampler->dynamic_state.base.num_samples = draw_llvm_texture_num_samples;
386 sampler->dynamic_state.base.sample_stride = draw_llvm_texture_sample_stride;
387 sampler->dynamic_state.base.min_lod = draw_llvm_sampler_min_lod;
388 sampler->dynamic_state.base.max_lod = draw_llvm_sampler_max_lod;
389 sampler->dynamic_state.base.lod_bias = draw_llvm_sampler_lod_bias;
390 sampler->dynamic_state.base.border_color = draw_llvm_sampler_border_color;
391 sampler->dynamic_state.static_state = static_state;
392
393 sampler->nr_samplers = nr_samplers;
394 return &sampler->base;
395 }
396
397 static void
398 draw_llvm_image_soa_emit_op(const struct lp_build_image_soa *base,
399 struct gallivm_state *gallivm,
400 const struct lp_img_params *params)
401 {
402 struct draw_llvm_image_soa *image = (struct draw_llvm_image_soa *)base;
403 unsigned image_index = params->image_index;
404 assert(image_index < PIPE_MAX_SHADER_IMAGES);
405
406 lp_build_img_op_soa(&image->dynamic_state.static_state[image_index].image_state,
407 &image->dynamic_state.base,
408 gallivm, params);
409 }
410 /**
411 * Fetch the texture size.
412 */
413 static void
414 draw_llvm_image_soa_emit_size_query(const struct lp_build_image_soa *base,
415 struct gallivm_state *gallivm,
416 const struct lp_sampler_size_query_params *params)
417 {
418 struct draw_llvm_image_soa *image = (struct draw_llvm_image_soa *)base;
419
420 assert(params->texture_unit < PIPE_MAX_SHADER_IMAGES);
421
422 lp_build_size_query_soa(gallivm,
423 &image->dynamic_state.static_state[params->texture_unit].image_state,
424 &image->dynamic_state.base,
425 params);
426 }
427 static void
428 draw_llvm_image_soa_destroy(struct lp_build_image_soa *image)
429 {
430 FREE(image);
431 }
432
433 struct lp_build_image_soa *
434 draw_llvm_image_soa_create(const struct draw_image_static_state *static_state)
435 {
436 struct draw_llvm_image_soa *image;
437
438 image = CALLOC_STRUCT(draw_llvm_image_soa);
439 if (!image)
440 return NULL;
441
442 image->base.destroy = draw_llvm_image_soa_destroy;
443 image->base.emit_op = draw_llvm_image_soa_emit_op;
444 image->base.emit_size_query = draw_llvm_image_soa_emit_size_query;
445
446 image->dynamic_state.base.width = draw_llvm_image_width;
447 image->dynamic_state.base.height = draw_llvm_image_height;
448
449 image->dynamic_state.base.depth = draw_llvm_image_depth;
450 image->dynamic_state.base.base_ptr = draw_llvm_image_base_ptr;
451 image->dynamic_state.base.row_stride = draw_llvm_image_row_stride;
452 image->dynamic_state.base.img_stride = draw_llvm_image_img_stride;
453 image->dynamic_state.base.num_samples = draw_llvm_image_num_samples;
454 image->dynamic_state.base.sample_stride = draw_llvm_image_sample_stride;
455
456 image->dynamic_state.static_state = static_state;
457
458 return &image->base;
459 }