lp_state_sampler.c revision df87fb59136eb302d72eac4b58fd8ffb25989ed5
1/**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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
44static void *
45llvmpipe_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
67static void
68llvmpipe_bind_sampler_states(struct pipe_context *pipe,
69                             unsigned shader,
70                             unsigned num, void **samplers)
71{
72   struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
73   unsigned i;
74
75   assert(num <= PIPE_MAX_SAMPLERS);
76
77   /* Check for no-op */
78   if (num == llvmpipe->num_samplers[shader] &&
79       !memcmp(llvmpipe->samplers[shader], samplers, num * sizeof(void *)))
80      return;
81
82   draw_flush(llvmpipe->draw);
83
84   for (i = 0; i < num; ++i)
85      llvmpipe->samplers[shader][i] = samplers[i];
86   for (i = num; i < PIPE_MAX_SAMPLERS; ++i)
87      llvmpipe->samplers[shader][i] = NULL;
88
89   llvmpipe->num_samplers[shader] = num;
90
91   if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
92      draw_set_samplers(llvmpipe->draw,
93                        shader,
94                        llvmpipe->samplers[shader],
95                        llvmpipe->num_samplers[shader]);
96   }
97
98   llvmpipe->dirty |= LP_NEW_SAMPLER;
99}
100
101
102static void
103llvmpipe_bind_fragment_sampler_states(struct pipe_context *pipe,
104                                      unsigned num, void **samplers)
105{
106   llvmpipe_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, num, samplers);
107}
108
109
110static void
111llvmpipe_bind_vertex_sampler_states(struct pipe_context *pipe,
112                                    unsigned num, void **samplers)
113{
114   llvmpipe_bind_sampler_states(pipe, PIPE_SHADER_VERTEX, num, samplers);
115}
116
117
118static void
119llvmpipe_bind_geometry_sampler_states(struct pipe_context *pipe,
120                                      unsigned num, void **samplers)
121{
122   llvmpipe_bind_sampler_states(pipe, PIPE_SHADER_GEOMETRY, num, samplers);
123}
124
125static void
126llvmpipe_set_sampler_views(struct pipe_context *pipe,
127                           unsigned shader,
128                           unsigned num,
129                           struct pipe_sampler_view **views)
130{
131   struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
132   uint i;
133
134   assert(num <= PIPE_MAX_SAMPLERS);
135
136   /* Check for no-op */
137   if (num == llvmpipe->num_sampler_views[shader] &&
138       !memcmp(llvmpipe->sampler_views[shader], views, num * sizeof(struct pipe_sampler_view *)))
139      return;
140
141   draw_flush(llvmpipe->draw);
142
143   for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
144      struct pipe_sampler_view *view = i < num ? views[i] : NULL;
145
146      pipe_sampler_view_reference(&llvmpipe->sampler_views[shader][i], view);
147   }
148
149   llvmpipe->num_sampler_views[shader] = num;
150
151   if (shader == PIPE_SHADER_VERTEX || shader == PIPE_SHADER_GEOMETRY) {
152      draw_set_sampler_views(llvmpipe->draw,
153                             shader,
154                             llvmpipe->sampler_views[shader],
155                             llvmpipe->num_sampler_views[shader]);
156   }
157
158   llvmpipe->dirty |= LP_NEW_SAMPLER_VIEW;
159}
160
161
162static void
163llvmpipe_set_fragment_sampler_views(struct pipe_context *pipe,
164                                    unsigned num,
165                                    struct pipe_sampler_view **views)
166{
167   llvmpipe_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, num, views);
168}
169
170
171static void
172llvmpipe_set_vertex_sampler_views(struct pipe_context *pipe,
173                                  unsigned num,
174                                  struct pipe_sampler_view **views)
175{
176   llvmpipe_set_sampler_views(pipe, PIPE_SHADER_VERTEX, num, views);
177}
178
179
180static void
181llvmpipe_set_geometry_sampler_views(struct pipe_context *pipe,
182                                    unsigned num,
183                                    struct pipe_sampler_view **views)
184{
185   llvmpipe_set_sampler_views(pipe, PIPE_SHADER_GEOMETRY, num, views);
186}
187
188static struct pipe_sampler_view *
189llvmpipe_create_sampler_view(struct pipe_context *pipe,
190                            struct pipe_resource *texture,
191                            const struct pipe_sampler_view *templ)
192{
193   struct pipe_sampler_view *view = CALLOC_STRUCT(pipe_sampler_view);
194
195   if (view) {
196      *view = *templ;
197      view->reference.count = 1;
198      view->texture = NULL;
199      pipe_resource_reference(&view->texture, texture);
200      view->context = pipe;
201   }
202
203   return view;
204}
205
206
207static void
208llvmpipe_sampler_view_destroy(struct pipe_context *pipe,
209                              struct pipe_sampler_view *view)
210{
211   pipe_resource_reference(&view->texture, NULL);
212   FREE(view);
213}
214
215
216static void
217llvmpipe_delete_sampler_state(struct pipe_context *pipe,
218                              void *sampler)
219{
220   FREE( sampler );
221}
222
223
224/**
225 * Called during state validation when LP_NEW_SAMPLER_VIEW is set.
226 */
227void
228llvmpipe_prepare_vertex_sampling(struct llvmpipe_context *lp,
229                                 unsigned num,
230                                 struct pipe_sampler_view **views)
231{
232   unsigned i;
233   uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
234   uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
235   const void *data[PIPE_MAX_TEXTURE_LEVELS];
236
237   assert(num <= PIPE_MAX_VERTEX_SAMPLERS);
238   if (!num)
239      return;
240
241   for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
242      struct pipe_sampler_view *view = i < num ? views[i] : NULL;
243
244      if (view) {
245         struct pipe_resource *tex = view->texture;
246         struct llvmpipe_resource *lp_tex = llvmpipe_resource(tex);
247
248         /* We're referencing the texture's internal data, so save a
249          * reference to it.
250          */
251         pipe_resource_reference(&lp->mapped_vs_tex[i], tex);
252
253         if (!lp_tex->dt) {
254            /* regular texture - setup array of mipmap level pointers */
255            int j;
256            for (j = view->u.tex.first_level; j <= tex->last_level; j++) {
257               data[j] =
258                  llvmpipe_get_texture_image_all(lp_tex, j, LP_TEX_USAGE_READ,
259                                                 LP_TEX_LAYOUT_LINEAR);
260               row_stride[j] = lp_tex->row_stride[j];
261               img_stride[j] = lp_tex->img_stride[j];
262            }
263         }
264         else {
265            /* display target texture/surface */
266            /*
267             * XXX: Where should this be unmapped?
268             */
269            struct llvmpipe_screen *screen = llvmpipe_screen(tex->screen);
270            struct sw_winsys *winsys = screen->winsys;
271            data[0] = winsys->displaytarget_map(winsys, lp_tex->dt,
272                                                PIPE_TRANSFER_READ);
273            row_stride[0] = lp_tex->row_stride[0];
274            img_stride[0] = lp_tex->img_stride[0];
275            assert(data[0]);
276         }
277         draw_set_mapped_texture(lp->draw,
278                                 PIPE_SHADER_VERTEX,
279                                 i,
280                                 tex->width0, tex->height0, tex->depth0,
281                                 view->u.tex.first_level, tex->last_level,
282                                 row_stride, img_stride, data);
283      }
284   }
285}
286
287void
288llvmpipe_cleanup_vertex_sampling(struct llvmpipe_context *ctx)
289{
290   unsigned i;
291   for (i = 0; i < Elements(ctx->mapped_vs_tex); i++) {
292      pipe_resource_reference(&ctx->mapped_vs_tex[i], NULL);
293   }
294}
295
296void
297llvmpipe_init_sampler_funcs(struct llvmpipe_context *llvmpipe)
298{
299   llvmpipe->pipe.create_sampler_state = llvmpipe_create_sampler_state;
300
301   llvmpipe->pipe.bind_fragment_sampler_states  = llvmpipe_bind_fragment_sampler_states;
302   llvmpipe->pipe.bind_vertex_sampler_states  = llvmpipe_bind_vertex_sampler_states;
303   llvmpipe->pipe.bind_geometry_sampler_states  = llvmpipe_bind_geometry_sampler_states;
304   llvmpipe->pipe.set_fragment_sampler_views = llvmpipe_set_fragment_sampler_views;
305   llvmpipe->pipe.set_vertex_sampler_views = llvmpipe_set_vertex_sampler_views;
306   llvmpipe->pipe.set_geometry_sampler_views = llvmpipe_set_geometry_sampler_views;
307   llvmpipe->pipe.create_sampler_view = llvmpipe_create_sampler_view;
308   llvmpipe->pipe.sampler_view_destroy = llvmpipe_sampler_view_destroy;
309   llvmpipe->pipe.delete_sampler_state = llvmpipe_delete_sampler_state;
310}
311