draw: introduce sampler num samples + stride members
[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
75 struct draw_llvm_image_dynamic_state
76 {
77 struct lp_sampler_dynamic_state base;
78
79 const struct draw_image_static_state *static_state;
80 };
81
82 struct draw_llvm_image_soa
83 {
84 struct lp_build_image_soa base;
85
86 struct draw_llvm_image_dynamic_state dynamic_state;
87 };
88
89 /**
90 * Fetch the specified member of the lp_jit_texture structure.
91 * \param emit_load if TRUE, emit the LLVM load instruction to actually
92 * fetch the field's value. Otherwise, just emit the
93 * GEP code to address the field.
94 *
95 * @sa http://llvm.org/docs/GetElementPtr.html
96 */
97 static LLVMValueRef
98 draw_llvm_texture_member(const struct lp_sampler_dynamic_state *base,
99 struct gallivm_state *gallivm,
100 LLVMValueRef context_ptr,
101 unsigned texture_unit,
102 unsigned member_index,
103 const char *member_name,
104 boolean emit_load)
105 {
106 LLVMBuilderRef builder = gallivm->builder;
107 LLVMValueRef indices[4];
108 LLVMValueRef ptr;
109 LLVMValueRef res;
110
111 debug_assert(texture_unit < PIPE_MAX_SHADER_SAMPLER_VIEWS);
112
113 /* context[0] */
114 indices[0] = lp_build_const_int32(gallivm, 0);
115 /* context[0].textures */
116 indices[1] = lp_build_const_int32(gallivm, DRAW_JIT_CTX_TEXTURES);
117 /* context[0].textures[unit] */
118 indices[2] = lp_build_const_int32(gallivm, texture_unit);
119 /* context[0].textures[unit].member */
120 indices[3] = lp_build_const_int32(gallivm, member_index);
121
122 ptr = LLVMBuildGEP(builder, context_ptr, indices, ARRAY_SIZE(indices), "");
123
124 if (emit_load)
125 res = LLVMBuildLoad(builder, ptr, "");
126 else
127 res = ptr;
128
129 lp_build_name(res, "context.texture%u.%s", texture_unit, member_name);
130
131 return res;
132 }
133
134
135 /**
136 * Fetch the specified member of the lp_jit_sampler structure.
137 * \param emit_load if TRUE, emit the LLVM load instruction to actually
138 * fetch the field's value. Otherwise, just emit the
139 * GEP code to address the field.
140 *
141 * @sa http://llvm.org/docs/GetElementPtr.html
142 */
143 static LLVMValueRef
144 draw_llvm_sampler_member(const struct lp_sampler_dynamic_state *base,
145 struct gallivm_state *gallivm,
146 LLVMValueRef context_ptr,
147 unsigned sampler_unit,
148 unsigned member_index,
149 const char *member_name,
150 boolean emit_load)
151 {
152 LLVMBuilderRef builder = gallivm->builder;
153 LLVMValueRef indices[4];
154 LLVMValueRef ptr;
155 LLVMValueRef res;
156
157 debug_assert(sampler_unit < PIPE_MAX_SAMPLERS);
158
159 /* context[0] */
160 indices[0] = lp_build_const_int32(gallivm, 0);
161 /* context[0].samplers */
162 indices[1] = lp_build_const_int32(gallivm, DRAW_JIT_CTX_SAMPLERS);
163 /* context[0].samplers[unit] */
164 indices[2] = lp_build_const_int32(gallivm, sampler_unit);
165 /* context[0].samplers[unit].member */
166 indices[3] = lp_build_const_int32(gallivm, member_index);
167
168 ptr = LLVMBuildGEP(builder, context_ptr, indices, ARRAY_SIZE(indices), "");
169
170 if (emit_load)
171 res = LLVMBuildLoad(builder, ptr, "");
172 else
173 res = ptr;
174
175 lp_build_name(res, "context.sampler%u.%s", sampler_unit, member_name);
176
177 return res;
178 }
179
180 /**
181 * Fetch the specified member of the lp_jit_texture structure.
182 * \param emit_load if TRUE, emit the LLVM load instruction to actually
183 * fetch the field's value. Otherwise, just emit the
184 * GEP code to address the field.
185 *
186 * @sa http://llvm.org/docs/GetElementPtr.html
187 */
188 static LLVMValueRef
189 draw_llvm_image_member(const struct lp_sampler_dynamic_state *base,
190 struct gallivm_state *gallivm,
191 LLVMValueRef context_ptr,
192 unsigned image_unit,
193 unsigned member_index,
194 const char *member_name,
195 boolean emit_load)
196 {
197 LLVMBuilderRef builder = gallivm->builder;
198 LLVMValueRef indices[4];
199 LLVMValueRef ptr;
200 LLVMValueRef res;
201
202 debug_assert(image_unit < PIPE_MAX_SHADER_IMAGES);
203
204 /* context[0] */
205 indices[0] = lp_build_const_int32(gallivm, 0);
206 /* context[0].textures */
207 indices[1] = lp_build_const_int32(gallivm, DRAW_JIT_CTX_IMAGES);
208 /* context[0].textures[unit] */
209 indices[2] = lp_build_const_int32(gallivm, image_unit);
210 /* context[0].textures[unit].member */
211 indices[3] = lp_build_const_int32(gallivm, member_index);
212
213 ptr = LLVMBuildGEP(builder, context_ptr, indices, ARRAY_SIZE(indices), "");
214
215 if (emit_load)
216 res = LLVMBuildLoad(builder, ptr, "");
217 else
218 res = ptr;
219
220 lp_build_name(res, "context.image%u.%s", image_unit, member_name);
221
222 return res;
223 }
224
225 /**
226 * Helper macro to instantiate the functions that generate the code to
227 * fetch the members of lp_jit_texture to fulfill the sampler code
228 * generator requests.
229 *
230 * This complexity is the price we have to pay to keep the texture
231 * sampler code generator a reusable module without dependencies to
232 * llvmpipe internals.
233 */
234 #define DRAW_LLVM_TEXTURE_MEMBER(_name, _index, _emit_load) \
235 static LLVMValueRef \
236 draw_llvm_texture_##_name( const struct lp_sampler_dynamic_state *base, \
237 struct gallivm_state *gallivm, \
238 LLVMValueRef context_ptr, \
239 unsigned texture_unit) \
240 { \
241 return draw_llvm_texture_member(base, gallivm, context_ptr, \
242 texture_unit, _index, #_name, _emit_load ); \
243 }
244
245
246 DRAW_LLVM_TEXTURE_MEMBER(width, DRAW_JIT_TEXTURE_WIDTH, TRUE)
247 DRAW_LLVM_TEXTURE_MEMBER(height, DRAW_JIT_TEXTURE_HEIGHT, TRUE)
248 DRAW_LLVM_TEXTURE_MEMBER(depth, DRAW_JIT_TEXTURE_DEPTH, TRUE)
249 DRAW_LLVM_TEXTURE_MEMBER(first_level,DRAW_JIT_TEXTURE_FIRST_LEVEL, TRUE)
250 DRAW_LLVM_TEXTURE_MEMBER(last_level, DRAW_JIT_TEXTURE_LAST_LEVEL, TRUE)
251 DRAW_LLVM_TEXTURE_MEMBER(base_ptr, DRAW_JIT_TEXTURE_BASE, TRUE)
252 DRAW_LLVM_TEXTURE_MEMBER(row_stride, DRAW_JIT_TEXTURE_ROW_STRIDE, FALSE)
253 DRAW_LLVM_TEXTURE_MEMBER(img_stride, DRAW_JIT_TEXTURE_IMG_STRIDE, FALSE)
254 DRAW_LLVM_TEXTURE_MEMBER(mip_offsets, DRAW_JIT_TEXTURE_MIP_OFFSETS, FALSE)
255 DRAW_LLVM_TEXTURE_MEMBER(num_samples, DRAW_JIT_TEXTURE_NUM_SAMPLES, TRUE)
256 DRAW_LLVM_TEXTURE_MEMBER(sample_stride, DRAW_JIT_TEXTURE_SAMPLE_STRIDE, TRUE)
257
258 #define DRAW_LLVM_SAMPLER_MEMBER(_name, _index, _emit_load) \
259 static LLVMValueRef \
260 draw_llvm_sampler_##_name( const struct lp_sampler_dynamic_state *base, \
261 struct gallivm_state *gallivm, \
262 LLVMValueRef context_ptr, \
263 unsigned sampler_unit) \
264 { \
265 return draw_llvm_sampler_member(base, gallivm, context_ptr, \
266 sampler_unit, _index, #_name, _emit_load ); \
267 }
268
269
270 DRAW_LLVM_SAMPLER_MEMBER(min_lod, DRAW_JIT_SAMPLER_MIN_LOD, TRUE)
271 DRAW_LLVM_SAMPLER_MEMBER(max_lod, DRAW_JIT_SAMPLER_MAX_LOD, TRUE)
272 DRAW_LLVM_SAMPLER_MEMBER(lod_bias, DRAW_JIT_SAMPLER_LOD_BIAS, TRUE)
273 DRAW_LLVM_SAMPLER_MEMBER(border_color, DRAW_JIT_SAMPLER_BORDER_COLOR, FALSE)
274
275 #define DRAW_LLVM_IMAGE_MEMBER(_name, _index, _emit_load) \
276 static LLVMValueRef \
277 draw_llvm_image_##_name( const struct lp_sampler_dynamic_state *base, \
278 struct gallivm_state *gallivm, \
279 LLVMValueRef context_ptr, \
280 unsigned image_unit) \
281 { \
282 return draw_llvm_image_member(base, gallivm, context_ptr, \
283 image_unit, _index, #_name, _emit_load ); \
284 }
285
286
287 DRAW_LLVM_IMAGE_MEMBER(width, DRAW_JIT_IMAGE_WIDTH, TRUE)
288 DRAW_LLVM_IMAGE_MEMBER(height, DRAW_JIT_IMAGE_HEIGHT, TRUE)
289 DRAW_LLVM_IMAGE_MEMBER(depth, DRAW_JIT_IMAGE_DEPTH, TRUE)
290 DRAW_LLVM_IMAGE_MEMBER(base_ptr, DRAW_JIT_IMAGE_BASE, TRUE)
291 DRAW_LLVM_IMAGE_MEMBER(row_stride, DRAW_JIT_IMAGE_ROW_STRIDE, TRUE)
292 DRAW_LLVM_IMAGE_MEMBER(img_stride, DRAW_JIT_IMAGE_IMG_STRIDE, TRUE)
293
294 static void
295 draw_llvm_sampler_soa_destroy(struct lp_build_sampler_soa *sampler)
296 {
297 FREE(sampler);
298 }
299
300
301 /**
302 * Fetch filtered values from texture.
303 * The 'texel' parameter returns four vectors corresponding to R, G, B, A.
304 */
305 static void
306 draw_llvm_sampler_soa_emit_fetch_texel(const struct lp_build_sampler_soa *base,
307 struct gallivm_state *gallivm,
308 const struct lp_sampler_params *params)
309 {
310 struct draw_llvm_sampler_soa *sampler = (struct draw_llvm_sampler_soa *)base;
311 unsigned texture_index = params->texture_index;
312 unsigned sampler_index = params->sampler_index;
313
314 assert(texture_index < PIPE_MAX_SHADER_SAMPLER_VIEWS);
315 assert(sampler_index < PIPE_MAX_SAMPLERS);
316
317 lp_build_sample_soa(&sampler->dynamic_state.static_state[texture_index].texture_state,
318 &sampler->dynamic_state.static_state[sampler_index].sampler_state,
319 &sampler->dynamic_state.base,
320 gallivm, params);
321 }
322
323
324 /**
325 * Fetch the texture size.
326 */
327 static void
328 draw_llvm_sampler_soa_emit_size_query(const struct lp_build_sampler_soa *base,
329 struct gallivm_state *gallivm,
330 const struct lp_sampler_size_query_params *params)
331 {
332 struct draw_llvm_sampler_soa *sampler = (struct draw_llvm_sampler_soa *)base;
333
334 assert(params->texture_unit < PIPE_MAX_SHADER_SAMPLER_VIEWS);
335
336 lp_build_size_query_soa(gallivm,
337 &sampler->dynamic_state.static_state[params->texture_unit].texture_state,
338 &sampler->dynamic_state.base,
339 params);
340 }
341
342 struct lp_build_sampler_soa *
343 draw_llvm_sampler_soa_create(const struct draw_sampler_static_state *static_state)
344 {
345 struct draw_llvm_sampler_soa *sampler;
346
347 sampler = CALLOC_STRUCT(draw_llvm_sampler_soa);
348 if (!sampler)
349 return NULL;
350
351 sampler->base.destroy = draw_llvm_sampler_soa_destroy;
352 sampler->base.emit_tex_sample = draw_llvm_sampler_soa_emit_fetch_texel;
353 sampler->base.emit_size_query = draw_llvm_sampler_soa_emit_size_query;
354 sampler->dynamic_state.base.width = draw_llvm_texture_width;
355 sampler->dynamic_state.base.height = draw_llvm_texture_height;
356 sampler->dynamic_state.base.depth = draw_llvm_texture_depth;
357 sampler->dynamic_state.base.first_level = draw_llvm_texture_first_level;
358 sampler->dynamic_state.base.last_level = draw_llvm_texture_last_level;
359 sampler->dynamic_state.base.row_stride = draw_llvm_texture_row_stride;
360 sampler->dynamic_state.base.img_stride = draw_llvm_texture_img_stride;
361 sampler->dynamic_state.base.base_ptr = draw_llvm_texture_base_ptr;
362 sampler->dynamic_state.base.mip_offsets = draw_llvm_texture_mip_offsets;
363 sampler->dynamic_state.base.num_samples = draw_llvm_texture_num_samples;
364 sampler->dynamic_state.base.sample_stride = draw_llvm_texture_sample_stride;
365 sampler->dynamic_state.base.min_lod = draw_llvm_sampler_min_lod;
366 sampler->dynamic_state.base.max_lod = draw_llvm_sampler_max_lod;
367 sampler->dynamic_state.base.lod_bias = draw_llvm_sampler_lod_bias;
368 sampler->dynamic_state.base.border_color = draw_llvm_sampler_border_color;
369 sampler->dynamic_state.static_state = static_state;
370
371 return &sampler->base;
372 }
373
374 static void
375 draw_llvm_image_soa_emit_op(const struct lp_build_image_soa *base,
376 struct gallivm_state *gallivm,
377 const struct lp_img_params *params)
378 {
379 struct draw_llvm_image_soa *image = (struct draw_llvm_image_soa *)base;
380 unsigned image_index = params->image_index;
381 assert(image_index < PIPE_MAX_SHADER_IMAGES);
382
383 lp_build_img_op_soa(&image->dynamic_state.static_state[image_index].image_state,
384 &image->dynamic_state.base,
385 gallivm, params);
386 }
387 /**
388 * Fetch the texture size.
389 */
390 static void
391 draw_llvm_image_soa_emit_size_query(const struct lp_build_image_soa *base,
392 struct gallivm_state *gallivm,
393 const struct lp_sampler_size_query_params *params)
394 {
395 struct draw_llvm_image_soa *image = (struct draw_llvm_image_soa *)base;
396
397 assert(params->texture_unit < PIPE_MAX_SHADER_IMAGES);
398
399 lp_build_size_query_soa(gallivm,
400 &image->dynamic_state.static_state[params->texture_unit].image_state,
401 &image->dynamic_state.base,
402 params);
403 }
404 static void
405 draw_llvm_image_soa_destroy(struct lp_build_image_soa *image)
406 {
407 FREE(image);
408 }
409
410 struct lp_build_image_soa *
411 draw_llvm_image_soa_create(const struct draw_image_static_state *static_state)
412 {
413 struct draw_llvm_image_soa *image;
414
415 image = CALLOC_STRUCT(draw_llvm_image_soa);
416 if (!image)
417 return NULL;
418
419 image->base.destroy = draw_llvm_image_soa_destroy;
420 image->base.emit_op = draw_llvm_image_soa_emit_op;
421 image->base.emit_size_query = draw_llvm_image_soa_emit_size_query;
422
423 image->dynamic_state.base.width = draw_llvm_image_width;
424 image->dynamic_state.base.height = draw_llvm_image_height;
425
426 image->dynamic_state.base.depth = draw_llvm_image_depth;
427 image->dynamic_state.base.base_ptr = draw_llvm_image_base_ptr;
428 image->dynamic_state.base.row_stride = draw_llvm_image_row_stride;
429 image->dynamic_state.base.img_stride = draw_llvm_image_img_stride;
430
431 image->dynamic_state.static_state = static_state;
432
433 return &image->base;
434 }