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