723e472720ede98fdffd7f7ada51adafe8db492d
[mesa.git] / src / gallium / drivers / llvmpipe / lp_state_sampler.c
1 /**************************************************************************
2 *
3 * Copyright 2007 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 /* Authors:
29 * Brian Paul
30 */
31
32 #include "util/u_inlines.h"
33 #include "util/u_memory.h"
34
35 #include "draw/draw_context.h"
36
37 #include "lp_context.h"
38 #include "lp_screen.h"
39 #include "lp_state.h"
40 #include "lp_debug.h"
41 #include "state_tracker/sw_winsys.h"
42
43
44 static void *
45 llvmpipe_create_sampler_state(struct pipe_context *pipe,
46 const struct pipe_sampler_state *sampler)
47 {
48 struct pipe_sampler_state *state = mem_dup(sampler, sizeof *sampler);
49
50 if (LP_PERF & PERF_NO_MIP_LINEAR) {
51 if (state->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR)
52 state->min_mip_filter = PIPE_TEX_MIPFILTER_NEAREST;
53 }
54
55 if (LP_PERF & PERF_NO_MIPMAPS)
56 state->min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
57
58 if (LP_PERF & PERF_NO_LINEAR) {
59 state->mag_img_filter = PIPE_TEX_FILTER_NEAREST;
60 state->min_img_filter = PIPE_TEX_FILTER_NEAREST;
61 }
62
63 return state;
64 }
65
66
67 static void
68 llvmpipe_bind_sampler_states(struct pipe_context *pipe,
69 enum pipe_shader_type shader,
70 unsigned start,
71 unsigned num,
72 void **samplers)
73 {
74 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
75 unsigned i;
76
77 assert(shader < PIPE_SHADER_TYPES);
78 assert(start + num <= ARRAY_SIZE(llvmpipe->samplers[shader]));
79
80 draw_flush(llvmpipe->draw);
81
82 /* set the new samplers */
83 for (i = 0; i < num; i++) {
84 llvmpipe->samplers[shader][start + i] = samplers[i];
85 }
86
87 /* find highest non-null samplers[] entry */
88 {
89 unsigned j = MAX2(llvmpipe->num_samplers[shader], start + num);
90 while (j > 0 && llvmpipe->samplers[shader][j - 1] == NULL)
91 j--;
92 llvmpipe->num_samplers[shader] = j;
93 }
94
95 if (shader == PIPE_SHADER_VERTEX ||
96 shader == PIPE_SHADER_GEOMETRY ||
97 shader == PIPE_SHADER_TESS_CTRL ||
98 shader == PIPE_SHADER_TESS_EVAL) {
99 draw_set_samplers(llvmpipe->draw,
100 shader,
101 llvmpipe->samplers[shader],
102 llvmpipe->num_samplers[shader]);
103 }
104 else if (shader == PIPE_SHADER_COMPUTE) {
105 llvmpipe->cs_dirty |= LP_CSNEW_SAMPLER;
106 } else {
107 llvmpipe->dirty |= LP_NEW_SAMPLER;
108 }
109 }
110
111
112 static void
113 llvmpipe_set_sampler_views(struct pipe_context *pipe,
114 enum pipe_shader_type shader,
115 unsigned start,
116 unsigned num,
117 struct pipe_sampler_view **views)
118 {
119 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
120 uint i;
121
122 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
123
124 assert(shader < PIPE_SHADER_TYPES);
125 assert(start + num <= ARRAY_SIZE(llvmpipe->sampler_views[shader]));
126
127 draw_flush(llvmpipe->draw);
128
129 /* set the new sampler views */
130 for (i = 0; i < num; i++) {
131 /*
132 * Warn if someone tries to set a view created in a different context
133 * (which is why we need the hack above in the first place).
134 * An assert would be better but st/mesa relies on it...
135 */
136 if (views[i] && views[i]->context != pipe) {
137 debug_printf("Illegal setting of sampler_view %d created in another "
138 "context\n", i);
139 }
140 pipe_sampler_view_reference(&llvmpipe->sampler_views[shader][start + i],
141 views[i]);
142 }
143
144 /* find highest non-null sampler_views[] entry */
145 {
146 unsigned j = MAX2(llvmpipe->num_sampler_views[shader], start + num);
147 while (j > 0 && llvmpipe->sampler_views[shader][j - 1] == NULL)
148 j--;
149 llvmpipe->num_sampler_views[shader] = j;
150 }
151
152 if (shader == PIPE_SHADER_VERTEX ||
153 shader == PIPE_SHADER_GEOMETRY ||
154 shader == PIPE_SHADER_TESS_CTRL ||
155 shader == PIPE_SHADER_TESS_EVAL) {
156 draw_set_sampler_views(llvmpipe->draw,
157 shader,
158 llvmpipe->sampler_views[shader],
159 llvmpipe->num_sampler_views[shader]);
160 }
161 else if (shader == PIPE_SHADER_COMPUTE) {
162 llvmpipe->cs_dirty |= LP_CSNEW_SAMPLER_VIEW;
163 } else {
164 llvmpipe->dirty |= LP_NEW_SAMPLER_VIEW;
165 }
166 }
167
168
169 static struct pipe_sampler_view *
170 llvmpipe_create_sampler_view(struct pipe_context *pipe,
171 struct pipe_resource *texture,
172 const struct pipe_sampler_view *templ)
173 {
174 struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
175 /*
176 * XXX: bind flags from OpenGL state tracker are notoriously unreliable.
177 * This looks unfixable, so fix the bind flags instead when it happens.
178 */
179 if (!(texture->bind & PIPE_BIND_SAMPLER_VIEW)) {
180 debug_printf("Illegal sampler view creation without bind flag\n");
181 texture->bind |= PIPE_BIND_SAMPLER_VIEW;
182 }
183
184 if (view) {
185 *view = *templ;
186 view->reference.count = 1;
187 view->texture = NULL;
188 pipe_resource_reference(&view->texture, texture);
189 view->context = pipe;
190
191 #ifdef DEBUG
192 /*
193 * This is possibly too lenient, but the primary reason is just
194 * to catch state trackers which forget to initialize this, so
195 * it only catches clearly impossible view targets.
196 */
197 if (view->target != texture->target) {
198 if (view->target == PIPE_TEXTURE_1D)
199 assert(texture->target == PIPE_TEXTURE_1D_ARRAY);
200 else if (view->target == PIPE_TEXTURE_1D_ARRAY)
201 assert(texture->target == PIPE_TEXTURE_1D);
202 else if (view->target == PIPE_TEXTURE_2D)
203 assert(texture->target == PIPE_TEXTURE_2D_ARRAY ||
204 texture->target == PIPE_TEXTURE_CUBE ||
205 texture->target == PIPE_TEXTURE_CUBE_ARRAY);
206 else if (view->target == PIPE_TEXTURE_2D_ARRAY)
207 assert(texture->target == PIPE_TEXTURE_2D ||
208 texture->target == PIPE_TEXTURE_CUBE ||
209 texture->target == PIPE_TEXTURE_CUBE_ARRAY);
210 else if (view->target == PIPE_TEXTURE_CUBE)
211 assert(texture->target == PIPE_TEXTURE_CUBE_ARRAY ||
212 texture->target == PIPE_TEXTURE_2D_ARRAY);
213 else if (view->target == PIPE_TEXTURE_CUBE_ARRAY)
214 assert(texture->target == PIPE_TEXTURE_CUBE ||
215 texture->target == PIPE_TEXTURE_2D_ARRAY);
216 else
217 assert(0);
218 }
219 #endif
220 }
221
222 return view;
223 }
224
225
226 static void
227 llvmpipe_sampler_view_destroy(struct pipe_context *pipe,
228 struct pipe_sampler_view *view)
229 {
230 pipe_resource_reference(&view->texture, NULL);
231 FREE(view);
232 }
233
234
235 static void
236 llvmpipe_delete_sampler_state(struct pipe_context *pipe,
237 void *sampler)
238 {
239 FREE( sampler );
240 }
241
242
243 static void
244 prepare_shader_sampling(
245 struct llvmpipe_context *lp,
246 unsigned num,
247 struct pipe_sampler_view **views,
248 enum pipe_shader_type shader_type)
249 {
250
251 unsigned i;
252 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
253 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
254 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS];
255 const void *addr;
256
257 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
258 if (!num)
259 return;
260
261 for (i = 0; i < num; i++) {
262 struct pipe_sampler_view *view = i < num ? views[i] : NULL;
263
264 if (view) {
265 struct pipe_resource *tex = view->texture;
266 struct llvmpipe_resource *lp_tex = llvmpipe_resource(tex);
267 unsigned width0 = tex->width0;
268 unsigned num_layers = tex->depth0;
269 unsigned first_level = 0;
270 unsigned last_level = 0;
271 unsigned sample_stride = 0;
272 unsigned num_samples = tex->nr_samples;
273
274 if (!lp_tex->dt) {
275 /* regular texture - setup array of mipmap level offsets */
276 struct pipe_resource *res = view->texture;
277 int j;
278
279 if (llvmpipe_resource_is_texture(res)) {
280 first_level = view->u.tex.first_level;
281 last_level = view->u.tex.last_level;
282 assert(first_level <= last_level);
283 assert(last_level <= res->last_level);
284 addr = lp_tex->tex_data;
285
286 for (j = first_level; j <= last_level; j++) {
287 mip_offsets[j] = lp_tex->mip_offsets[j];
288 row_stride[j] = lp_tex->row_stride[j];
289 img_stride[j] = lp_tex->img_stride[j];
290 }
291 if (tex->target == PIPE_TEXTURE_1D_ARRAY ||
292 tex->target == PIPE_TEXTURE_2D_ARRAY ||
293 tex->target == PIPE_TEXTURE_CUBE ||
294 tex->target == PIPE_TEXTURE_CUBE_ARRAY) {
295 num_layers = view->u.tex.last_layer - view->u.tex.first_layer + 1;
296 for (j = first_level; j <= last_level; j++) {
297 mip_offsets[j] += view->u.tex.first_layer *
298 lp_tex->img_stride[j];
299 }
300 if (view->target == PIPE_TEXTURE_CUBE ||
301 view->target == PIPE_TEXTURE_CUBE_ARRAY) {
302 assert(num_layers % 6 == 0);
303 }
304 assert(view->u.tex.first_layer <= view->u.tex.last_layer);
305 assert(view->u.tex.last_layer < res->array_size);
306 }
307 }
308 else {
309 unsigned view_blocksize = util_format_get_blocksize(view->format);
310 addr = lp_tex->data;
311 /* probably don't really need to fill that out */
312 mip_offsets[0] = 0;
313 row_stride[0] = 0;
314 img_stride[0] = 0;
315
316 /* everything specified in number of elements here. */
317 width0 = view->u.buf.size / view_blocksize;
318 addr = (uint8_t *)addr + view->u.buf.offset;
319 assert(view->u.buf.offset + view->u.buf.size <= res->width0);
320 }
321 }
322 else {
323 /* display target texture/surface */
324 /*
325 * XXX: Where should this be unmapped?
326 */
327 struct llvmpipe_screen *screen = llvmpipe_screen(tex->screen);
328 struct sw_winsys *winsys = screen->winsys;
329 addr = winsys->displaytarget_map(winsys, lp_tex->dt,
330 PIPE_TRANSFER_READ);
331 row_stride[0] = lp_tex->row_stride[0];
332 img_stride[0] = lp_tex->img_stride[0];
333 mip_offsets[0] = 0;
334 assert(addr);
335 }
336 draw_set_mapped_texture(lp->draw,
337 shader_type,
338 i,
339 width0, tex->height0, num_layers,
340 first_level, last_level,
341 num_samples, sample_stride,
342 addr,
343 row_stride, img_stride, mip_offsets);
344 }
345 }
346 }
347
348
349 /**
350 * Called whenever we're about to draw (no dirty flag, FIXME?).
351 */
352 void
353 llvmpipe_prepare_vertex_sampling(struct llvmpipe_context *lp,
354 unsigned num,
355 struct pipe_sampler_view **views)
356 {
357 prepare_shader_sampling(lp, num, views, PIPE_SHADER_VERTEX);
358 }
359
360
361 /**
362 * Called whenever we're about to draw (no dirty flag, FIXME?).
363 */
364 void
365 llvmpipe_prepare_geometry_sampling(struct llvmpipe_context *lp,
366 unsigned num,
367 struct pipe_sampler_view **views)
368 {
369 prepare_shader_sampling(lp, num, views, PIPE_SHADER_GEOMETRY);
370 }
371
372 /**
373 * Called whenever we're about to draw (no dirty flag, FIXME?).
374 */
375 void
376 llvmpipe_prepare_tess_ctrl_sampling(struct llvmpipe_context *lp,
377 unsigned num,
378 struct pipe_sampler_view **views)
379 {
380 prepare_shader_sampling(lp, num, views, PIPE_SHADER_TESS_CTRL);
381 }
382
383 /**
384 * Called whenever we're about to draw (no dirty flag, FIXME?).
385 */
386 void
387 llvmpipe_prepare_tess_eval_sampling(struct llvmpipe_context *lp,
388 unsigned num,
389 struct pipe_sampler_view **views)
390 {
391 prepare_shader_sampling(lp, num, views, PIPE_SHADER_TESS_EVAL);
392 }
393
394 static void
395 prepare_shader_images(
396 struct llvmpipe_context *lp,
397 unsigned num,
398 struct pipe_image_view *views,
399 enum pipe_shader_type shader_type)
400 {
401
402 unsigned i;
403 uint32_t row_stride;
404 uint32_t img_stride;
405 const void *addr;
406
407 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
408 if (!num)
409 return;
410
411 for (i = 0; i < num; i++) {
412 struct pipe_image_view *view = i < num ? &views[i] : NULL;
413
414 if (view) {
415 struct pipe_resource *img = view->resource;
416 struct llvmpipe_resource *lp_img = llvmpipe_resource(img);
417 if (!img)
418 continue;
419
420 unsigned width = u_minify(img->width0, view->u.tex.level);
421 unsigned height = u_minify(img->height0, view->u.tex.level);
422 unsigned num_layers = img->depth0;
423
424 if (!lp_img->dt) {
425 /* regular texture - setup array of mipmap level offsets */
426 struct pipe_resource *res = view->resource;
427
428 if (llvmpipe_resource_is_texture(res)) {
429 uint32_t mip_offset = lp_img->mip_offsets[view->u.tex.level];
430 addr = lp_img->tex_data;
431
432 if (img->target == PIPE_TEXTURE_1D_ARRAY ||
433 img->target == PIPE_TEXTURE_2D_ARRAY ||
434 img->target == PIPE_TEXTURE_3D ||
435 img->target == PIPE_TEXTURE_CUBE ||
436 img->target == PIPE_TEXTURE_CUBE_ARRAY) {
437 num_layers = view->u.tex.last_layer - view->u.tex.first_layer + 1;
438 assert(view->u.tex.first_layer <= view->u.tex.last_layer);
439 mip_offset += view->u.tex.first_layer * lp_img->img_stride[view->u.tex.level];
440 }
441
442 row_stride = lp_img->row_stride[view->u.tex.level];
443 img_stride = lp_img->img_stride[view->u.tex.level];
444 addr = (uint8_t *)addr + mip_offset;
445 }
446 else {
447 unsigned view_blocksize = util_format_get_blocksize(view->format);
448 addr = lp_img->data;
449 /* probably don't really need to fill that out */
450 row_stride = 0;
451 img_stride = 0;
452
453 /* everything specified in number of elements here. */
454 width = view->u.buf.size / view_blocksize;
455 addr = (uint8_t *)addr + view->u.buf.offset;
456 assert(view->u.buf.offset + view->u.buf.size <= res->width0);
457 }
458 }
459 else {
460 /* display target texture/surface */
461 /*
462 * XXX: Where should this be unmapped?
463 */
464 struct llvmpipe_screen *screen = llvmpipe_screen(img->screen);
465 struct sw_winsys *winsys = screen->winsys;
466 addr = winsys->displaytarget_map(winsys, lp_img->dt,
467 PIPE_TRANSFER_READ);
468 row_stride = lp_img->row_stride[0];
469 img_stride = lp_img->img_stride[0];
470 assert(addr);
471 }
472 draw_set_mapped_image(lp->draw,
473 shader_type,
474 i,
475 width, height, num_layers,
476 addr,
477 row_stride, img_stride, 0, 0);
478 }
479 }
480 }
481
482
483 /**
484 * Called whenever we're about to draw (no dirty flag, FIXME?).
485 */
486 void
487 llvmpipe_prepare_vertex_images(struct llvmpipe_context *lp,
488 unsigned num,
489 struct pipe_image_view *views)
490 {
491 prepare_shader_images(lp, num, views, PIPE_SHADER_VERTEX);
492 }
493
494
495 /**
496 * Called whenever we're about to draw (no dirty flag, FIXME?).
497 */
498 void
499 llvmpipe_prepare_geometry_images(struct llvmpipe_context *lp,
500 unsigned num,
501 struct pipe_image_view *views)
502 {
503 prepare_shader_images(lp, num, views, PIPE_SHADER_GEOMETRY);
504 }
505
506 /**
507 * Called whenever we're about to draw (no dirty flag, FIXME?).
508 */
509 void
510 llvmpipe_prepare_tess_ctrl_images(struct llvmpipe_context *lp,
511 unsigned num,
512 struct pipe_image_view *views)
513 {
514 prepare_shader_images(lp, num, views, PIPE_SHADER_TESS_CTRL);
515 }
516
517 /**
518 * Called whenever we're about to draw (no dirty flag, FIXME?).
519 */
520 void
521 llvmpipe_prepare_tess_eval_images(struct llvmpipe_context *lp,
522 unsigned num,
523 struct pipe_image_view *views)
524 {
525 prepare_shader_images(lp, num, views, PIPE_SHADER_TESS_EVAL);
526 }
527
528 void
529 llvmpipe_init_sampler_funcs(struct llvmpipe_context *llvmpipe)
530 {
531 llvmpipe->pipe.create_sampler_state = llvmpipe_create_sampler_state;
532
533 llvmpipe->pipe.bind_sampler_states = llvmpipe_bind_sampler_states;
534 llvmpipe->pipe.create_sampler_view = llvmpipe_create_sampler_view;
535 llvmpipe->pipe.set_sampler_views = llvmpipe_set_sampler_views;
536 llvmpipe->pipe.sampler_view_destroy = llvmpipe_sampler_view_destroy;
537 llvmpipe->pipe.delete_sampler_state = llvmpipe_delete_sampler_state;
538 }