gallium: rename 'state tracker' to 'frontend'
[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 "frontend/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 sample_stride = lp_tex->sample_stride;
287
288 for (j = first_level; j <= last_level; j++) {
289 mip_offsets[j] = lp_tex->mip_offsets[j];
290 row_stride[j] = lp_tex->row_stride[j];
291 img_stride[j] = lp_tex->img_stride[j];
292 }
293 if (tex->target == PIPE_TEXTURE_1D_ARRAY ||
294 tex->target == PIPE_TEXTURE_2D_ARRAY ||
295 tex->target == PIPE_TEXTURE_CUBE ||
296 tex->target == PIPE_TEXTURE_CUBE_ARRAY) {
297 num_layers = view->u.tex.last_layer - view->u.tex.first_layer + 1;
298 for (j = first_level; j <= last_level; j++) {
299 mip_offsets[j] += view->u.tex.first_layer *
300 lp_tex->img_stride[j];
301 }
302 if (view->target == PIPE_TEXTURE_CUBE ||
303 view->target == PIPE_TEXTURE_CUBE_ARRAY) {
304 assert(num_layers % 6 == 0);
305 }
306 assert(view->u.tex.first_layer <= view->u.tex.last_layer);
307 assert(view->u.tex.last_layer < res->array_size);
308 }
309 }
310 else {
311 unsigned view_blocksize = util_format_get_blocksize(view->format);
312 addr = lp_tex->data;
313 /* probably don't really need to fill that out */
314 mip_offsets[0] = 0;
315 row_stride[0] = 0;
316 img_stride[0] = 0;
317
318 /* everything specified in number of elements here. */
319 width0 = view->u.buf.size / view_blocksize;
320 addr = (uint8_t *)addr + view->u.buf.offset;
321 assert(view->u.buf.offset + view->u.buf.size <= res->width0);
322 }
323 }
324 else {
325 /* display target texture/surface */
326 /*
327 * XXX: Where should this be unmapped?
328 */
329 struct llvmpipe_screen *screen = llvmpipe_screen(tex->screen);
330 struct sw_winsys *winsys = screen->winsys;
331 addr = winsys->displaytarget_map(winsys, lp_tex->dt,
332 PIPE_TRANSFER_READ);
333 row_stride[0] = lp_tex->row_stride[0];
334 img_stride[0] = lp_tex->img_stride[0];
335 mip_offsets[0] = 0;
336 assert(addr);
337 }
338 draw_set_mapped_texture(lp->draw,
339 shader_type,
340 i,
341 width0, tex->height0, num_layers,
342 first_level, last_level,
343 num_samples, sample_stride,
344 addr,
345 row_stride, img_stride, mip_offsets);
346 }
347 }
348 }
349
350
351 /**
352 * Called whenever we're about to draw (no dirty flag, FIXME?).
353 */
354 void
355 llvmpipe_prepare_vertex_sampling(struct llvmpipe_context *lp,
356 unsigned num,
357 struct pipe_sampler_view **views)
358 {
359 prepare_shader_sampling(lp, num, views, PIPE_SHADER_VERTEX);
360 }
361
362
363 /**
364 * Called whenever we're about to draw (no dirty flag, FIXME?).
365 */
366 void
367 llvmpipe_prepare_geometry_sampling(struct llvmpipe_context *lp,
368 unsigned num,
369 struct pipe_sampler_view **views)
370 {
371 prepare_shader_sampling(lp, num, views, PIPE_SHADER_GEOMETRY);
372 }
373
374 /**
375 * Called whenever we're about to draw (no dirty flag, FIXME?).
376 */
377 void
378 llvmpipe_prepare_tess_ctrl_sampling(struct llvmpipe_context *lp,
379 unsigned num,
380 struct pipe_sampler_view **views)
381 {
382 prepare_shader_sampling(lp, num, views, PIPE_SHADER_TESS_CTRL);
383 }
384
385 /**
386 * Called whenever we're about to draw (no dirty flag, FIXME?).
387 */
388 void
389 llvmpipe_prepare_tess_eval_sampling(struct llvmpipe_context *lp,
390 unsigned num,
391 struct pipe_sampler_view **views)
392 {
393 prepare_shader_sampling(lp, num, views, PIPE_SHADER_TESS_EVAL);
394 }
395
396 static void
397 prepare_shader_images(
398 struct llvmpipe_context *lp,
399 unsigned num,
400 struct pipe_image_view *views,
401 enum pipe_shader_type shader_type)
402 {
403
404 unsigned i;
405 uint32_t row_stride;
406 uint32_t img_stride;
407 uint32_t sample_stride;
408 const void *addr;
409
410 assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
411 if (!num)
412 return;
413
414 for (i = 0; i < num; i++) {
415 struct pipe_image_view *view = i < num ? &views[i] : NULL;
416
417 if (view) {
418 struct pipe_resource *img = view->resource;
419 struct llvmpipe_resource *lp_img = llvmpipe_resource(img);
420 if (!img)
421 continue;
422
423 unsigned width = u_minify(img->width0, view->u.tex.level);
424 unsigned height = u_minify(img->height0, view->u.tex.level);
425 unsigned num_layers = img->depth0;
426 unsigned num_samples = img->nr_samples;
427
428 if (!lp_img->dt) {
429 /* regular texture - setup array of mipmap level offsets */
430 struct pipe_resource *res = view->resource;
431
432 if (llvmpipe_resource_is_texture(res)) {
433 uint32_t mip_offset = lp_img->mip_offsets[view->u.tex.level];
434 addr = lp_img->tex_data;
435
436 if (img->target == PIPE_TEXTURE_1D_ARRAY ||
437 img->target == PIPE_TEXTURE_2D_ARRAY ||
438 img->target == PIPE_TEXTURE_3D ||
439 img->target == PIPE_TEXTURE_CUBE ||
440 img->target == PIPE_TEXTURE_CUBE_ARRAY) {
441 num_layers = view->u.tex.last_layer - view->u.tex.first_layer + 1;
442 assert(view->u.tex.first_layer <= view->u.tex.last_layer);
443 mip_offset += view->u.tex.first_layer * lp_img->img_stride[view->u.tex.level];
444 }
445
446 row_stride = lp_img->row_stride[view->u.tex.level];
447 img_stride = lp_img->img_stride[view->u.tex.level];
448 sample_stride = lp_img->sample_stride;
449 addr = (uint8_t *)addr + mip_offset;
450 }
451 else {
452 unsigned view_blocksize = util_format_get_blocksize(view->format);
453 addr = lp_img->data;
454 /* probably don't really need to fill that out */
455 row_stride = 0;
456 img_stride = 0;
457 sample_stride = 0;
458
459 /* everything specified in number of elements here. */
460 width = view->u.buf.size / view_blocksize;
461 addr = (uint8_t *)addr + view->u.buf.offset;
462 assert(view->u.buf.offset + view->u.buf.size <= res->width0);
463 }
464 }
465 else {
466 /* display target texture/surface */
467 /*
468 * XXX: Where should this be unmapped?
469 */
470 struct llvmpipe_screen *screen = llvmpipe_screen(img->screen);
471 struct sw_winsys *winsys = screen->winsys;
472 addr = winsys->displaytarget_map(winsys, lp_img->dt,
473 PIPE_TRANSFER_READ);
474 row_stride = lp_img->row_stride[0];
475 img_stride = lp_img->img_stride[0];
476 sample_stride = 0;
477 assert(addr);
478 }
479 draw_set_mapped_image(lp->draw,
480 shader_type,
481 i,
482 width, height, num_layers,
483 addr,
484 row_stride, img_stride,
485 num_samples, sample_stride);
486 }
487 }
488 }
489
490
491 /**
492 * Called whenever we're about to draw (no dirty flag, FIXME?).
493 */
494 void
495 llvmpipe_prepare_vertex_images(struct llvmpipe_context *lp,
496 unsigned num,
497 struct pipe_image_view *views)
498 {
499 prepare_shader_images(lp, num, views, PIPE_SHADER_VERTEX);
500 }
501
502
503 /**
504 * Called whenever we're about to draw (no dirty flag, FIXME?).
505 */
506 void
507 llvmpipe_prepare_geometry_images(struct llvmpipe_context *lp,
508 unsigned num,
509 struct pipe_image_view *views)
510 {
511 prepare_shader_images(lp, num, views, PIPE_SHADER_GEOMETRY);
512 }
513
514 /**
515 * Called whenever we're about to draw (no dirty flag, FIXME?).
516 */
517 void
518 llvmpipe_prepare_tess_ctrl_images(struct llvmpipe_context *lp,
519 unsigned num,
520 struct pipe_image_view *views)
521 {
522 prepare_shader_images(lp, num, views, PIPE_SHADER_TESS_CTRL);
523 }
524
525 /**
526 * Called whenever we're about to draw (no dirty flag, FIXME?).
527 */
528 void
529 llvmpipe_prepare_tess_eval_images(struct llvmpipe_context *lp,
530 unsigned num,
531 struct pipe_image_view *views)
532 {
533 prepare_shader_images(lp, num, views, PIPE_SHADER_TESS_EVAL);
534 }
535
536 void
537 llvmpipe_init_sampler_funcs(struct llvmpipe_context *llvmpipe)
538 {
539 llvmpipe->pipe.create_sampler_state = llvmpipe_create_sampler_state;
540
541 llvmpipe->pipe.bind_sampler_states = llvmpipe_bind_sampler_states;
542 llvmpipe->pipe.create_sampler_view = llvmpipe_create_sampler_view;
543 llvmpipe->pipe.set_sampler_views = llvmpipe_set_sampler_views;
544 llvmpipe->pipe.sampler_view_destroy = llvmpipe_sampler_view_destroy;
545 llvmpipe->pipe.delete_sampler_state = llvmpipe_delete_sampler_state;
546 }