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_memory.h"
33#include "util/u_inlines.h"
34#include "util/format/u_format.h"
35
36#include "draw/draw_context.h"
37
38#include "sp_context.h"
39#include "sp_state.h"
40#include "sp_texture.h"
41#include "sp_tex_sample.h"
42#include "sp_tex_tile_cache.h"
43#include "sp_screen.h"
44#include "frontend/sw_winsys.h"
45
46
47/**
48 * Bind a range [start, start+num-1] of samplers for a shader stage.
49 */
50static void
51softpipe_bind_sampler_states(struct pipe_context *pipe,
52                             enum pipe_shader_type shader,
53                             unsigned start,
54                             unsigned num,
55                             void **samplers)
56{
57   struct softpipe_context *softpipe = softpipe_context(pipe);
58   unsigned i;
59
60   assert(shader < PIPE_SHADER_TYPES);
61   assert(start + num <= ARRAY_SIZE(softpipe->samplers[shader]));
62
63   draw_flush(softpipe->draw);
64
65   /* set the new samplers */
66   for (i = 0; i < num; i++) {
67      softpipe->samplers[shader][start + i] = samplers[i];
68   }
69
70   /* find highest non-null samplers[] entry */
71   {
72      unsigned j = MAX2(softpipe->num_samplers[shader], start + num);
73      while (j > 0 && softpipe->samplers[shader][j - 1] == NULL)
74         j--;
75      softpipe->num_samplers[shader] = j;
76   }
77
78   if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
79      draw_set_samplers(softpipe->draw,
80                        shader,
81                        softpipe->samplers[shader],
82                        softpipe->num_samplers[shader]);
83   }
84
85   softpipe->dirty |= SP_NEW_SAMPLER;
86}
87
88
89static void
90softpipe_sampler_view_destroy(struct pipe_context *pipe,
91                              struct pipe_sampler_view *view)
92{
93   pipe_resource_reference(&view->texture, NULL);
94   FREE(view);
95}
96
97
98void
99softpipe_set_sampler_views(struct pipe_context *pipe,
100                           enum pipe_shader_type shader,
101                           unsigned start,
102                           unsigned num,
103                           unsigned unbind_num_trailing_slots,
104                           bool take_ownership,
105                           struct pipe_sampler_view **views)
106{
107   struct softpipe_context *softpipe = softpipe_context(pipe);
108   uint i;
109
110   assert(shader < PIPE_SHADER_TYPES);
111   assert(start + num <= ARRAY_SIZE(softpipe->sampler_views[shader]));
112
113   draw_flush(softpipe->draw);
114
115   /* set the new sampler views */
116   for (i = 0; i < num; i++) {
117      struct sp_sampler_view *sp_sviewsrc;
118      struct sp_sampler_view *sp_sviewdst =
119         &softpipe->tgsi.sampler[shader]->sp_sview[start + i];
120      struct pipe_sampler_view **pview = &softpipe->sampler_views[shader][start + i];
121
122      if (take_ownership) {
123         pipe_sampler_view_reference(pview, NULL);
124         *pview = views[i];
125      } else {
126         pipe_sampler_view_reference(pview, views[i]);
127      }
128      sp_tex_tile_cache_set_sampler_view(softpipe->tex_cache[shader][start + i],
129                                         views[i]);
130      /*
131       * We don't really have variants, however some bits are different per shader,
132       * so just copy?
133       */
134      sp_sviewsrc = (struct sp_sampler_view *)*pview;
135      if (sp_sviewsrc) {
136         memcpy(sp_sviewdst, sp_sviewsrc, sizeof(*sp_sviewsrc));
137         sp_sviewdst->compute_lambda = softpipe_get_lambda_func(&sp_sviewdst->base, shader);
138         sp_sviewdst->compute_lambda_from_grad = softpipe_get_lambda_from_grad_func(&sp_sviewdst->base, shader);
139         sp_sviewdst->cache = softpipe->tex_cache[shader][start + i];
140      }
141      else {
142         memset(sp_sviewdst, 0,  sizeof(*sp_sviewsrc));
143      }
144   }
145   for (; i < num + unbind_num_trailing_slots; i++) {
146      struct pipe_sampler_view **pview = &softpipe->sampler_views[shader][start + i];
147      pipe_sampler_view_reference(pview, NULL);
148      sp_tex_tile_cache_set_sampler_view(softpipe->tex_cache[shader][start + i],
149                                         NULL);
150   }
151
152
153   /* find highest non-null sampler_views[] entry */
154   {
155      unsigned j = MAX2(softpipe->num_sampler_views[shader], start + num);
156      while (j > 0 && softpipe->sampler_views[shader][j - 1] == NULL)
157         j--;
158      softpipe->num_sampler_views[shader] = j;
159   }
160
161   if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
162      draw_set_sampler_views(softpipe->draw,
163                             shader,
164                             softpipe->sampler_views[shader],
165                             softpipe->num_sampler_views[shader]);
166   }
167
168   softpipe->dirty |= SP_NEW_TEXTURE;
169}
170
171
172static void
173softpipe_delete_sampler_state(struct pipe_context *pipe,
174                              void *sampler)
175{
176   FREE( sampler );
177}
178
179
180static void
181prepare_shader_sampling(
182   struct softpipe_context *sp,
183   unsigned num,
184   struct pipe_sampler_view **views,
185   enum pipe_shader_type shader_type,
186   struct pipe_resource *mapped_tex[PIPE_MAX_SHADER_SAMPLER_VIEWS])
187{
188
189   unsigned i;
190   uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
191   uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
192   uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS];
193   const void *addr;
194
195   assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
196   if (!num)
197      return;
198
199   for (i = 0; i < num; i++) {
200      struct pipe_sampler_view *view = views[i];
201
202      if (view) {
203         struct pipe_resource *tex = view->texture;
204         struct softpipe_resource *sp_tex = softpipe_resource(tex);
205         unsigned width0 = tex->width0;
206         unsigned num_layers = tex->depth0;
207         unsigned first_level = 0;
208         unsigned last_level = 0;
209
210         /* We're referencing the texture's internal data, so save a
211          * reference to it.
212          */
213         pipe_resource_reference(&mapped_tex[i], tex);
214
215         if (!sp_tex->dt) {
216            /* regular texture - setup array of mipmap level offsets */
217            ASSERTED struct pipe_resource *res = view->texture;
218            int j;
219
220            if (view->target != PIPE_BUFFER) {
221               first_level = view->u.tex.first_level;
222               last_level = view->u.tex.last_level;
223               assert(first_level <= last_level);
224               assert(last_level <= res->last_level);
225               addr = sp_tex->data;
226
227               for (j = first_level; j <= last_level; j++) {
228                  mip_offsets[j] = sp_tex->level_offset[j];
229                  row_stride[j] = sp_tex->stride[j];
230                  img_stride[j] = sp_tex->img_stride[j];
231               }
232               if (tex->target == PIPE_TEXTURE_1D_ARRAY ||
233                   tex->target == PIPE_TEXTURE_2D_ARRAY ||
234                   tex->target == PIPE_TEXTURE_CUBE ||
235                   tex->target == PIPE_TEXTURE_CUBE_ARRAY) {
236                  num_layers = view->u.tex.last_layer - view->u.tex.first_layer + 1;
237                  for (j = first_level; j <= last_level; j++) {
238                     mip_offsets[j] += view->u.tex.first_layer *
239                                       sp_tex->img_stride[j];
240                  }
241                  if (view->target == PIPE_TEXTURE_CUBE ||
242                      view->target == PIPE_TEXTURE_CUBE_ARRAY) {
243                     assert(num_layers % 6 == 0);
244                  }
245                  assert(view->u.tex.first_layer <= view->u.tex.last_layer);
246                  assert(view->u.tex.last_layer < res->array_size);
247               }
248            }
249            else {
250               unsigned view_blocksize = util_format_get_blocksize(view->format);
251               addr = sp_tex->data;
252               /* probably don't really need to fill that out */
253               mip_offsets[0] = 0;
254               row_stride[0] = 0;
255               img_stride[0] = 0;
256
257               /* everything specified in number of elements here. */
258               width0 = view->u.buf.size / view_blocksize;
259               addr = (uint8_t *)addr + view->u.buf.offset;
260               assert(view->u.buf.offset + view->u.buf.size <= res->width0);
261            }
262         }
263         else {
264            /* display target texture/surface */
265            struct softpipe_screen *screen = softpipe_screen(tex->screen);
266            struct sw_winsys *winsys = screen->winsys;
267            addr = winsys->displaytarget_map(winsys, sp_tex->dt,
268                                             PIPE_MAP_READ);
269            row_stride[0] = sp_tex->stride[0];
270            img_stride[0] = sp_tex->img_stride[0];
271            mip_offsets[0] = 0;
272            assert(addr);
273         }
274         draw_set_mapped_texture(sp->draw,
275                                 shader_type,
276                                 i,
277                                 width0, tex->height0, num_layers,
278                                 first_level, last_level, 0, 0,
279                                 addr,
280                                 row_stride, img_stride, mip_offsets);
281      }
282   }
283}
284
285static void
286sp_sampler_view_display_target_unmap(struct softpipe_context *sp,
287                                     struct pipe_sampler_view *view)
288{
289   if (view) {
290      struct pipe_resource *tex = view->texture;
291      struct softpipe_resource *sp_tex = softpipe_resource(tex);
292      if (sp_tex->dt) {
293         struct softpipe_screen *screen = softpipe_screen(tex->screen);
294         struct sw_winsys *winsys = screen->winsys;
295         winsys->displaytarget_unmap(winsys, sp_tex->dt);
296      }
297   }
298}
299
300/**
301 * Called during state validation when SP_NEW_TEXTURE is set.
302 */
303void
304softpipe_prepare_vertex_sampling(struct softpipe_context *sp,
305                                 unsigned num,
306                                 struct pipe_sampler_view **views)
307{
308   prepare_shader_sampling(sp, num, views, PIPE_SHADER_VERTEX,
309                           sp->mapped_vs_tex);
310}
311
312void
313softpipe_cleanup_vertex_sampling(struct softpipe_context *ctx)
314{
315   unsigned i;
316   for (i = 0; i < ARRAY_SIZE(ctx->mapped_vs_tex); i++) {
317      sp_sampler_view_display_target_unmap(
318         ctx, ctx->sampler_views[PIPE_SHADER_VERTEX][i]);
319      pipe_resource_reference(&ctx->mapped_vs_tex[i], NULL);
320   }
321}
322
323
324/**
325 * Called during state validation when SP_NEW_TEXTURE is set.
326 */
327void
328softpipe_prepare_geometry_sampling(struct softpipe_context *sp,
329                                   unsigned num,
330                                   struct pipe_sampler_view **views)
331{
332   prepare_shader_sampling(sp, num, views, PIPE_SHADER_GEOMETRY,
333                           sp->mapped_gs_tex);
334}
335
336void
337softpipe_cleanup_geometry_sampling(struct softpipe_context *ctx)
338{
339   unsigned i;
340   for (i = 0; i < ARRAY_SIZE(ctx->mapped_gs_tex); i++) {
341      sp_sampler_view_display_target_unmap(
342         ctx, ctx->sampler_views[PIPE_SHADER_GEOMETRY][i]);
343      pipe_resource_reference(&ctx->mapped_gs_tex[i], NULL);
344   }
345}
346
347
348void
349softpipe_init_sampler_funcs(struct pipe_context *pipe)
350{
351   pipe->create_sampler_state = softpipe_create_sampler_state;
352   pipe->bind_sampler_states = softpipe_bind_sampler_states;
353   pipe->delete_sampler_state = softpipe_delete_sampler_state;
354
355   pipe->create_sampler_view = softpipe_create_sampler_view;
356   pipe->set_sampler_views = softpipe_set_sampler_views;
357   pipe->sampler_view_destroy = softpipe_sampler_view_destroy;
358}
359
360