• 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 "state_tracker/sw_winsys.h"
42 
43 
44 static void *
llvmpipe_create_sampler_state(struct pipe_context * pipe,const struct pipe_sampler_state * sampler)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
llvmpipe_bind_sampler_states(struct pipe_context * pipe,enum pipe_shader_type shader,unsigned start,unsigned num,void ** samplers)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 || shader == PIPE_SHADER_GEOMETRY) {
96       draw_set_samplers(llvmpipe->draw,
97                         shader,
98                         llvmpipe->samplers[shader],
99                         llvmpipe->num_samplers[shader]);
100    }
101    else {
102       llvmpipe->dirty |= LP_NEW_SAMPLER;
103    }
104 }
105 
106 
107 static void
llvmpipe_set_sampler_views(struct pipe_context * pipe,enum pipe_shader_type shader,unsigned start,unsigned num,struct pipe_sampler_view ** views)108 llvmpipe_set_sampler_views(struct pipe_context *pipe,
109                            enum pipe_shader_type shader,
110                            unsigned start,
111                            unsigned num,
112                            struct pipe_sampler_view **views)
113 {
114    struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
115    uint i;
116 
117    assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
118 
119    assert(shader < PIPE_SHADER_TYPES);
120    assert(start + num <= ARRAY_SIZE(llvmpipe->sampler_views[shader]));
121 
122    draw_flush(llvmpipe->draw);
123 
124    /* set the new sampler views */
125    for (i = 0; i < num; i++) {
126       /* Note: we're using pipe_sampler_view_release() here to work around
127        * a possible crash when the old view belongs to another context that
128        * was already destroyed.
129        */
130       pipe_sampler_view_release(pipe,
131                                 &llvmpipe->sampler_views[shader][start + i]);
132       /*
133        * Warn if someone tries to set a view created in a different context
134        * (which is why we need the hack above in the first place).
135        * An assert would be better but st/mesa relies on it...
136        */
137       if (views[i] && views[i]->context != pipe) {
138          debug_printf("Illegal setting of sampler_view %d created in another "
139                       "context\n", i);
140       }
141       pipe_sampler_view_reference(&llvmpipe->sampler_views[shader][start + i],
142                                   views[i]);
143    }
144 
145    /* find highest non-null sampler_views[] entry */
146    {
147       unsigned j = MAX2(llvmpipe->num_sampler_views[shader], start + num);
148       while (j > 0 && llvmpipe->sampler_views[shader][j - 1] == NULL)
149          j--;
150       llvmpipe->num_sampler_views[shader] = j;
151    }
152 
153    if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
154       draw_set_sampler_views(llvmpipe->draw,
155                              shader,
156                              llvmpipe->sampler_views[shader],
157                              llvmpipe->num_sampler_views[shader]);
158    }
159    else {
160       llvmpipe->dirty |= LP_NEW_SAMPLER_VIEW;
161    }
162 }
163 
164 
165 static struct pipe_sampler_view *
llvmpipe_create_sampler_view(struct pipe_context * pipe,struct pipe_resource * texture,const struct pipe_sampler_view * templ)166 llvmpipe_create_sampler_view(struct pipe_context *pipe,
167                             struct pipe_resource *texture,
168                             const struct pipe_sampler_view *templ)
169 {
170    struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
171    /*
172     * XXX: bind flags from OpenGL state tracker are notoriously unreliable.
173     * This looks unfixable, so fix the bind flags instead when it happens.
174     */
175    if (!(texture->bind & PIPE_BIND_SAMPLER_VIEW)) {
176       debug_printf("Illegal sampler view creation without bind flag\n");
177       texture->bind |= PIPE_BIND_SAMPLER_VIEW;
178    }
179 
180    if (view) {
181       *view = *templ;
182       view->reference.count = 1;
183       view->texture = NULL;
184       pipe_resource_reference(&view->texture, texture);
185       view->context = pipe;
186 
187 #ifdef DEBUG
188      /*
189       * This is possibly too lenient, but the primary reason is just
190       * to catch state trackers which forget to initialize this, so
191       * it only catches clearly impossible view targets.
192       */
193       if (view->target != texture->target) {
194          if (view->target == PIPE_TEXTURE_1D)
195             assert(texture->target == PIPE_TEXTURE_1D_ARRAY);
196          else if (view->target == PIPE_TEXTURE_1D_ARRAY)
197             assert(texture->target == PIPE_TEXTURE_1D);
198          else if (view->target == PIPE_TEXTURE_2D)
199             assert(texture->target == PIPE_TEXTURE_2D_ARRAY ||
200                    texture->target == PIPE_TEXTURE_CUBE ||
201                    texture->target == PIPE_TEXTURE_CUBE_ARRAY);
202          else if (view->target == PIPE_TEXTURE_2D_ARRAY)
203             assert(texture->target == PIPE_TEXTURE_2D ||
204                    texture->target == PIPE_TEXTURE_CUBE ||
205                    texture->target == PIPE_TEXTURE_CUBE_ARRAY);
206          else if (view->target == PIPE_TEXTURE_CUBE)
207             assert(texture->target == PIPE_TEXTURE_CUBE_ARRAY ||
208                    texture->target == PIPE_TEXTURE_2D_ARRAY);
209          else if (view->target == PIPE_TEXTURE_CUBE_ARRAY)
210             assert(texture->target == PIPE_TEXTURE_CUBE ||
211                    texture->target == PIPE_TEXTURE_2D_ARRAY);
212          else
213             assert(0);
214       }
215 #endif
216    }
217 
218    return view;
219 }
220 
221 
222 static void
llvmpipe_sampler_view_destroy(struct pipe_context * pipe,struct pipe_sampler_view * view)223 llvmpipe_sampler_view_destroy(struct pipe_context *pipe,
224                               struct pipe_sampler_view *view)
225 {
226    pipe_resource_reference(&view->texture, NULL);
227    FREE(view);
228 }
229 
230 
231 static void
llvmpipe_delete_sampler_state(struct pipe_context * pipe,void * sampler)232 llvmpipe_delete_sampler_state(struct pipe_context *pipe,
233                               void *sampler)
234 {
235    FREE( sampler );
236 }
237 
238 
239 static void
prepare_shader_sampling(struct llvmpipe_context * lp,unsigned num,struct pipe_sampler_view ** views,unsigned shader_type)240 prepare_shader_sampling(
241    struct llvmpipe_context *lp,
242    unsigned num,
243    struct pipe_sampler_view **views,
244    unsigned shader_type)
245 {
246 
247    unsigned i;
248    uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
249    uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
250    uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS];
251    const void *addr;
252 
253    assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
254    if (!num)
255       return;
256 
257    for (i = 0; i < num; i++) {
258       struct pipe_sampler_view *view = i < num ? views[i] : NULL;
259 
260       if (view) {
261          struct pipe_resource *tex = view->texture;
262          struct llvmpipe_resource *lp_tex = llvmpipe_resource(tex);
263          unsigned width0 = tex->width0;
264          unsigned num_layers = tex->depth0;
265          unsigned first_level = 0;
266          unsigned last_level = 0;
267 
268          if (!lp_tex->dt) {
269             /* regular texture - setup array of mipmap level offsets */
270             struct pipe_resource *res = view->texture;
271             int j;
272 
273             if (llvmpipe_resource_is_texture(res)) {
274                first_level = view->u.tex.first_level;
275                last_level = view->u.tex.last_level;
276                assert(first_level <= last_level);
277                assert(last_level <= res->last_level);
278                addr = lp_tex->tex_data;
279 
280                for (j = first_level; j <= last_level; j++) {
281                   mip_offsets[j] = lp_tex->mip_offsets[j];
282                   row_stride[j] = lp_tex->row_stride[j];
283                   img_stride[j] = lp_tex->img_stride[j];
284                }
285                if (tex->target == PIPE_TEXTURE_1D_ARRAY ||
286                    tex->target == PIPE_TEXTURE_2D_ARRAY ||
287                    tex->target == PIPE_TEXTURE_CUBE ||
288                    tex->target == PIPE_TEXTURE_CUBE_ARRAY) {
289                   num_layers = view->u.tex.last_layer - view->u.tex.first_layer + 1;
290                   for (j = first_level; j <= last_level; j++) {
291                      mip_offsets[j] += view->u.tex.first_layer *
292                                        lp_tex->img_stride[j];
293                   }
294                   if (view->target == PIPE_TEXTURE_CUBE ||
295                       view->target == PIPE_TEXTURE_CUBE_ARRAY) {
296                      assert(num_layers % 6 == 0);
297                   }
298                   assert(view->u.tex.first_layer <= view->u.tex.last_layer);
299                   assert(view->u.tex.last_layer < res->array_size);
300                }
301             }
302             else {
303                unsigned view_blocksize = util_format_get_blocksize(view->format);
304                addr = lp_tex->data;
305                /* probably don't really need to fill that out */
306                mip_offsets[0] = 0;
307                row_stride[0] = 0;
308                img_stride[0] = 0;
309 
310                /* everything specified in number of elements here. */
311                width0 = view->u.buf.size / view_blocksize;
312                addr = (uint8_t *)addr + view->u.buf.offset;
313                assert(view->u.buf.offset + view->u.buf.size <= res->width0);
314             }
315          }
316          else {
317             /* display target texture/surface */
318             /*
319              * XXX: Where should this be unmapped?
320              */
321             struct llvmpipe_screen *screen = llvmpipe_screen(tex->screen);
322             struct sw_winsys *winsys = screen->winsys;
323             addr = winsys->displaytarget_map(winsys, lp_tex->dt,
324                                                 PIPE_TRANSFER_READ);
325             row_stride[0] = lp_tex->row_stride[0];
326             img_stride[0] = lp_tex->img_stride[0];
327             mip_offsets[0] = 0;
328             assert(addr);
329          }
330          draw_set_mapped_texture(lp->draw,
331                                  shader_type,
332                                  i,
333                                  width0, tex->height0, num_layers,
334                                  first_level, last_level,
335                                  addr,
336                                  row_stride, img_stride, mip_offsets);
337       }
338    }
339 }
340 
341 
342 /**
343  * Called whenever we're about to draw (no dirty flag, FIXME?).
344  */
345 void
llvmpipe_prepare_vertex_sampling(struct llvmpipe_context * lp,unsigned num,struct pipe_sampler_view ** views)346 llvmpipe_prepare_vertex_sampling(struct llvmpipe_context *lp,
347                                  unsigned num,
348                                  struct pipe_sampler_view **views)
349 {
350    prepare_shader_sampling(lp, num, views, PIPE_SHADER_VERTEX);
351 }
352 
353 
354 /**
355  * Called whenever we're about to draw (no dirty flag, FIXME?).
356  */
357 void
llvmpipe_prepare_geometry_sampling(struct llvmpipe_context * lp,unsigned num,struct pipe_sampler_view ** views)358 llvmpipe_prepare_geometry_sampling(struct llvmpipe_context *lp,
359                                    unsigned num,
360                                    struct pipe_sampler_view **views)
361 {
362    prepare_shader_sampling(lp, num, views, PIPE_SHADER_GEOMETRY);
363 }
364 
365 
366 void
llvmpipe_init_sampler_funcs(struct llvmpipe_context * llvmpipe)367 llvmpipe_init_sampler_funcs(struct llvmpipe_context *llvmpipe)
368 {
369    llvmpipe->pipe.create_sampler_state = llvmpipe_create_sampler_state;
370 
371    llvmpipe->pipe.bind_sampler_states = llvmpipe_bind_sampler_states;
372    llvmpipe->pipe.create_sampler_view = llvmpipe_create_sampler_view;
373    llvmpipe->pipe.set_sampler_views = llvmpipe_set_sampler_views;
374    llvmpipe->pipe.sampler_view_destroy = llvmpipe_sampler_view_destroy;
375    llvmpipe->pipe.delete_sampler_state = llvmpipe_delete_sampler_state;
376 }
377