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