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
45static void *
46llvmpipe_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
68static void
69llvmpipe_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
117static void
118llvmpipe_set_sampler_views(struct pipe_context *pipe,
119                           enum pipe_shader_type shader,
120                           unsigned start,
121                           unsigned num,
122                           unsigned unbind_num_trailing_slots,
123                           bool take_ownership,
124                           struct pipe_sampler_view **views)
125{
126   struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
127   uint i;
128
129   assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
130
131   assert(shader < PIPE_SHADER_TYPES);
132   assert(start + num <= ARRAY_SIZE(llvmpipe->sampler_views[shader]));
133
134   draw_flush(llvmpipe->draw);
135
136   /* set the new sampler views */
137   for (i = 0; i < num; i++) {
138      struct pipe_sampler_view *view = NULL;
139
140      if (views && views[i])
141	 view = views[i];
142      /*
143       * Warn if someone tries to set a view created in a different context
144       * (which is why we need the hack above in the first place).
145       * An assert would be better but st/mesa relies on it...
146       */
147      if (view && view->context != pipe) {
148         debug_printf("Illegal setting of sampler_view %d created in another "
149                      "context\n", i);
150      }
151
152      if (view)
153         llvmpipe_flush_resource(pipe, view->texture, 0, true, false, false, "sampler_view");
154
155      if (take_ownership) {
156         pipe_sampler_view_reference(&llvmpipe->sampler_views[shader][start + i],
157                                     NULL);
158         llvmpipe->sampler_views[shader][start + i] = view;
159      } else {
160         pipe_sampler_view_reference(&llvmpipe->sampler_views[shader][start + i],
161                                     view);
162      }
163   }
164
165   for (; i < num + unbind_num_trailing_slots; i++) {
166      pipe_sampler_view_reference(&llvmpipe->sampler_views[shader][start + i],
167                                  NULL);
168   }
169
170   /* find highest non-null sampler_views[] entry */
171   {
172      unsigned j = MAX2(llvmpipe->num_sampler_views[shader], start + num);
173      while (j > 0 && llvmpipe->sampler_views[shader][j - 1] == NULL)
174         j--;
175      llvmpipe->num_sampler_views[shader] = j;
176   }
177
178   if (shader == PIPE_SHADER_VERTEX ||
179       shader == PIPE_SHADER_GEOMETRY ||
180       shader == PIPE_SHADER_TESS_CTRL ||
181       shader == PIPE_SHADER_TESS_EVAL) {
182      draw_set_sampler_views(llvmpipe->draw,
183                             shader,
184                             llvmpipe->sampler_views[shader],
185                             llvmpipe->num_sampler_views[shader]);
186   }
187   else if (shader == PIPE_SHADER_COMPUTE) {
188      llvmpipe->cs_dirty |= LP_CSNEW_SAMPLER_VIEW;
189   }
190   else if (shader == PIPE_SHADER_FRAGMENT) {
191      llvmpipe->dirty |= LP_NEW_SAMPLER_VIEW;
192      lp_setup_set_fragment_sampler_views(llvmpipe->setup,
193                                          llvmpipe->num_sampler_views[PIPE_SHADER_FRAGMENT],
194                                          llvmpipe->sampler_views[PIPE_SHADER_FRAGMENT]);
195   }
196}
197
198
199static struct pipe_sampler_view *
200llvmpipe_create_sampler_view(struct pipe_context *pipe,
201                            struct pipe_resource *texture,
202                            const struct pipe_sampler_view *templ)
203{
204   struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
205   /*
206    * XXX: bind flags from OpenGL state tracker are notoriously unreliable.
207    * This looks unfixable, so fix the bind flags instead when it happens.
208    */
209   if (!(texture->bind & PIPE_BIND_SAMPLER_VIEW)) {
210      debug_printf("Illegal sampler view creation without bind flag\n");
211      texture->bind |= PIPE_BIND_SAMPLER_VIEW;
212   }
213
214   if (view) {
215      *view = *templ;
216      view->reference.count = 1;
217      view->texture = NULL;
218      pipe_resource_reference(&view->texture, texture);
219      view->context = pipe;
220
221#ifdef DEBUG
222     /*
223      * This is possibly too lenient, but the primary reason is just
224      * to catch gallium frontends which forget to initialize this, so
225      * it only catches clearly impossible view targets.
226      */
227      if (view->target != texture->target) {
228         if (view->target == PIPE_TEXTURE_1D)
229            assert(texture->target == PIPE_TEXTURE_1D_ARRAY);
230         else if (view->target == PIPE_TEXTURE_1D_ARRAY)
231            assert(texture->target == PIPE_TEXTURE_1D);
232         else if (view->target == PIPE_TEXTURE_2D)
233            assert(texture->target == PIPE_TEXTURE_2D_ARRAY ||
234                   texture->target == PIPE_TEXTURE_3D ||
235                   texture->target == PIPE_TEXTURE_CUBE ||
236                   texture->target == PIPE_TEXTURE_CUBE_ARRAY);
237         else if (view->target == PIPE_TEXTURE_2D_ARRAY)
238            assert(texture->target == PIPE_TEXTURE_2D ||
239                   texture->target == PIPE_TEXTURE_CUBE ||
240                   texture->target == PIPE_TEXTURE_CUBE_ARRAY);
241         else if (view->target == PIPE_TEXTURE_CUBE)
242            assert(texture->target == PIPE_TEXTURE_CUBE_ARRAY ||
243                   texture->target == PIPE_TEXTURE_2D_ARRAY);
244         else if (view->target == PIPE_TEXTURE_CUBE_ARRAY)
245            assert(texture->target == PIPE_TEXTURE_CUBE ||
246                   texture->target == PIPE_TEXTURE_2D_ARRAY);
247         else
248            assert(0);
249      }
250#endif
251   }
252
253   return view;
254}
255
256
257static void
258llvmpipe_sampler_view_destroy(struct pipe_context *pipe,
259                              struct pipe_sampler_view *view)
260{
261   pipe_resource_reference(&view->texture, NULL);
262   FREE(view);
263}
264
265
266static void
267llvmpipe_delete_sampler_state(struct pipe_context *pipe,
268                              void *sampler)
269{
270   FREE( sampler );
271}
272
273
274static void
275prepare_shader_sampling(
276   struct llvmpipe_context *lp,
277   unsigned num,
278   struct pipe_sampler_view **views,
279   enum pipe_shader_type shader_type)
280{
281
282   unsigned i;
283   uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
284   uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
285   uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS];
286   const void *addr;
287
288   assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
289   if (!num)
290      return;
291
292   for (i = 0; i < num; i++) {
293      struct pipe_sampler_view *view = i < num ? views[i] : NULL;
294
295      if (view) {
296         struct pipe_resource *tex = view->texture;
297         struct llvmpipe_resource *lp_tex = llvmpipe_resource(tex);
298         unsigned width0 = tex->width0;
299         unsigned num_layers = tex->depth0;
300         unsigned first_level = 0;
301         unsigned last_level = 0;
302         unsigned sample_stride = 0;
303         unsigned num_samples = tex->nr_samples;
304
305         if (!lp_tex->dt) {
306            /* regular texture - setup array of mipmap level offsets */
307            struct pipe_resource *res = view->texture;
308            int j;
309
310            if (llvmpipe_resource_is_texture(res)) {
311               first_level = view->u.tex.first_level;
312               last_level = view->u.tex.last_level;
313               assert(first_level <= last_level);
314               assert(last_level <= res->last_level);
315               addr = lp_tex->tex_data;
316
317               sample_stride = lp_tex->sample_stride;
318
319               for (j = first_level; j <= last_level; j++) {
320                  mip_offsets[j] = lp_tex->mip_offsets[j];
321                  row_stride[j] = lp_tex->row_stride[j];
322                  img_stride[j] = lp_tex->img_stride[j];
323               }
324               if (tex->target == PIPE_TEXTURE_1D_ARRAY ||
325                   tex->target == PIPE_TEXTURE_2D_ARRAY ||
326                   tex->target == PIPE_TEXTURE_CUBE ||
327                   tex->target == PIPE_TEXTURE_CUBE_ARRAY) {
328                  num_layers = view->u.tex.last_layer - view->u.tex.first_layer + 1;
329                  for (j = first_level; j <= last_level; j++) {
330                     mip_offsets[j] += view->u.tex.first_layer *
331                                       lp_tex->img_stride[j];
332                  }
333                  if (view->target == PIPE_TEXTURE_CUBE ||
334                      view->target == PIPE_TEXTURE_CUBE_ARRAY) {
335                     assert(num_layers % 6 == 0);
336                  }
337                  assert(view->u.tex.first_layer <= view->u.tex.last_layer);
338                  assert(view->u.tex.last_layer < res->array_size);
339               }
340            }
341            else {
342               unsigned view_blocksize = util_format_get_blocksize(view->format);
343               addr = lp_tex->data;
344               /* probably don't really need to fill that out */
345               mip_offsets[0] = 0;
346               row_stride[0] = 0;
347               img_stride[0] = 0;
348
349               /* everything specified in number of elements here. */
350               width0 = view->u.buf.size / view_blocksize;
351               addr = (uint8_t *)addr + view->u.buf.offset;
352               assert(view->u.buf.offset + view->u.buf.size <= res->width0);
353            }
354         }
355         else {
356            /* display target texture/surface */
357            addr = llvmpipe_resource_map(tex, 0, 0, LP_TEX_USAGE_READ);
358            row_stride[0] = lp_tex->row_stride[0];
359            img_stride[0] = lp_tex->img_stride[0];
360            mip_offsets[0] = 0;
361            assert(addr);
362         }
363         draw_set_mapped_texture(lp->draw,
364                                 shader_type,
365                                 i,
366                                 width0, tex->height0, num_layers,
367                                 first_level, last_level,
368                                 num_samples, sample_stride,
369                                 addr,
370                                 row_stride, img_stride, mip_offsets);
371      }
372   }
373}
374
375
376/**
377 * Called whenever we're about to draw (no dirty flag, FIXME?).
378 */
379void
380llvmpipe_prepare_vertex_sampling(struct llvmpipe_context *lp,
381                                 unsigned num,
382                                 struct pipe_sampler_view **views)
383{
384   prepare_shader_sampling(lp, num, views, PIPE_SHADER_VERTEX);
385}
386
387
388/**
389 * Called whenever we're about to draw (no dirty flag, FIXME?).
390 */
391void
392llvmpipe_prepare_geometry_sampling(struct llvmpipe_context *lp,
393                                   unsigned num,
394                                   struct pipe_sampler_view **views)
395{
396   prepare_shader_sampling(lp, num, views, PIPE_SHADER_GEOMETRY);
397}
398
399/**
400 * Called whenever we're about to draw (no dirty flag, FIXME?).
401 */
402void
403llvmpipe_prepare_tess_ctrl_sampling(struct llvmpipe_context *lp,
404				    unsigned num,
405				    struct pipe_sampler_view **views)
406{
407   prepare_shader_sampling(lp, num, views, PIPE_SHADER_TESS_CTRL);
408}
409
410/**
411 * Called whenever we're about to draw (no dirty flag, FIXME?).
412 */
413void
414llvmpipe_prepare_tess_eval_sampling(struct llvmpipe_context *lp,
415				    unsigned num,
416				    struct pipe_sampler_view **views)
417{
418   prepare_shader_sampling(lp, num, views, PIPE_SHADER_TESS_EVAL);
419}
420
421void
422llvmpipe_cleanup_stage_sampling(struct llvmpipe_context *ctx,
423                                enum pipe_shader_type stage)
424{
425   unsigned num, i;
426   struct pipe_sampler_view **views;
427   assert(ctx);
428   assert(stage < ARRAY_SIZE(ctx->num_sampler_views));
429   assert(stage < ARRAY_SIZE(ctx->sampler_views));
430
431   num = ctx->num_sampler_views[stage];
432   views = ctx->sampler_views[stage];
433
434   assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
435
436   for (i = 0; i < num; i++) {
437      struct pipe_sampler_view *view = views[i];
438      if (view) {
439         struct pipe_resource *tex = view->texture;
440         if (tex)
441            llvmpipe_resource_unmap(tex, 0, 0);
442      }
443   }
444}
445
446static void
447prepare_shader_images(
448   struct llvmpipe_context *lp,
449   unsigned num,
450   struct pipe_image_view *views,
451   enum pipe_shader_type shader_type)
452{
453
454   unsigned i;
455   uint32_t row_stride;
456   uint32_t img_stride;
457   uint32_t sample_stride;
458   const void *addr;
459
460   assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
461   if (!num)
462      return;
463
464   for (i = 0; i < num; i++) {
465      struct pipe_image_view *view = i < num ? &views[i] : NULL;
466
467      if (view) {
468         struct pipe_resource *img = view->resource;
469         struct llvmpipe_resource *lp_img = llvmpipe_resource(img);
470         if (!img)
471            continue;
472
473         unsigned width = img->width0;
474         unsigned height = img->height0;
475         unsigned num_layers = img->depth0;
476         unsigned num_samples = img->nr_samples;
477
478         const uint32_t bw = util_format_get_blockwidth(view->resource->format);
479         const uint32_t bh = util_format_get_blockheight(view->resource->format);
480
481         width = DIV_ROUND_UP(width, bw);
482         height = DIV_ROUND_UP(height, bh);
483         width = u_minify(width, view->u.tex.level);
484         height = u_minify(height, view->u.tex.level);
485
486         if (!lp_img->dt) {
487            /* regular texture - setup array of mipmap level offsets */
488            struct pipe_resource *res = view->resource;
489
490            if (llvmpipe_resource_is_texture(res)) {
491               uint32_t mip_offset = lp_img->mip_offsets[view->u.tex.level];
492               addr = lp_img->tex_data;
493
494               if (img->target == PIPE_TEXTURE_1D_ARRAY ||
495                   img->target == PIPE_TEXTURE_2D_ARRAY ||
496                   img->target == PIPE_TEXTURE_3D ||
497                   img->target == PIPE_TEXTURE_CUBE ||
498                   img->target == PIPE_TEXTURE_CUBE_ARRAY) {
499                  num_layers = view->u.tex.last_layer - view->u.tex.first_layer + 1;
500                  assert(view->u.tex.first_layer <= view->u.tex.last_layer);
501                  mip_offset += view->u.tex.first_layer * lp_img->img_stride[view->u.tex.level];
502               }
503
504               row_stride = lp_img->row_stride[view->u.tex.level];
505               img_stride = lp_img->img_stride[view->u.tex.level];
506               sample_stride = lp_img->sample_stride;
507               addr = (uint8_t *)addr + mip_offset;
508            }
509            else {
510               unsigned view_blocksize = util_format_get_blocksize(view->format);
511               addr = lp_img->data;
512               /* probably don't really need to fill that out */
513               row_stride = 0;
514               img_stride = 0;
515               sample_stride = 0;
516
517               /* everything specified in number of elements here. */
518               width = view->u.buf.size / view_blocksize;
519               addr = (uint8_t *)addr + view->u.buf.offset;
520               assert(view->u.buf.offset + view->u.buf.size <= res->width0);
521            }
522         }
523         else {
524            /* display target texture/surface */
525            addr = llvmpipe_resource_map(img, 0, 0, LP_TEX_USAGE_READ);
526            row_stride = lp_img->row_stride[0];
527            img_stride = lp_img->img_stride[0];
528            sample_stride = 0;
529            assert(addr);
530         }
531         draw_set_mapped_image(lp->draw,
532                               shader_type,
533                               i,
534                               width, height, num_layers,
535                               addr,
536                               row_stride, img_stride,
537                               num_samples, sample_stride);
538      }
539   }
540}
541
542
543/**
544 * Called whenever we're about to draw (no dirty flag, FIXME?).
545 */
546void
547llvmpipe_prepare_vertex_images(struct llvmpipe_context *lp,
548                               unsigned num,
549                               struct pipe_image_view *views)
550{
551   prepare_shader_images(lp, num, views, PIPE_SHADER_VERTEX);
552}
553
554
555/**
556 * Called whenever we're about to draw (no dirty flag, FIXME?).
557 */
558void
559llvmpipe_prepare_geometry_images(struct llvmpipe_context *lp,
560                                 unsigned num,
561                                 struct pipe_image_view *views)
562{
563   prepare_shader_images(lp, num, views, PIPE_SHADER_GEOMETRY);
564}
565
566/**
567 * Called whenever we're about to draw (no dirty flag, FIXME?).
568 */
569void
570llvmpipe_prepare_tess_ctrl_images(struct llvmpipe_context *lp,
571                                  unsigned num,
572                                  struct pipe_image_view *views)
573{
574   prepare_shader_images(lp, num, views, PIPE_SHADER_TESS_CTRL);
575}
576
577/**
578 * Called whenever we're about to draw (no dirty flag, FIXME?).
579 */
580void
581llvmpipe_prepare_tess_eval_images(struct llvmpipe_context *lp,
582                                  unsigned num,
583                                  struct pipe_image_view *views)
584{
585   prepare_shader_images(lp, num, views, PIPE_SHADER_TESS_EVAL);
586}
587
588void
589llvmpipe_cleanup_stage_images(struct llvmpipe_context *ctx,
590                              enum pipe_shader_type stage)
591{
592   unsigned num, i;
593   struct pipe_image_view *views;
594   assert(ctx);
595   assert(stage < ARRAY_SIZE(ctx->num_images));
596   assert(stage < ARRAY_SIZE(ctx->images));
597
598   num = ctx->num_images[stage];
599   views = ctx->images[stage];
600
601   assert(num <= LP_MAX_TGSI_SHADER_IMAGES);
602
603   for (i = 0; i < num; i++) {
604      struct pipe_image_view *view = &views[i];
605      assert(view);
606      struct pipe_resource *img = view->resource;
607      if (img)
608         llvmpipe_resource_unmap(img, 0, 0);
609   }
610}
611
612void
613llvmpipe_init_sampler_funcs(struct llvmpipe_context *llvmpipe)
614{
615   llvmpipe->pipe.create_sampler_state = llvmpipe_create_sampler_state;
616
617   llvmpipe->pipe.bind_sampler_states = llvmpipe_bind_sampler_states;
618   llvmpipe->pipe.create_sampler_view = llvmpipe_create_sampler_view;
619   llvmpipe->pipe.set_sampler_views = llvmpipe_set_sampler_views;
620   llvmpipe->pipe.sampler_view_destroy = llvmpipe_sampler_view_destroy;
621   llvmpipe->pipe.delete_sampler_state = llvmpipe_delete_sampler_state;
622}
623