svga: Set the appropriate flags when creating sampler/surface views.
[mesa.git] / src / gallium / drivers / svga / svga_sampler_view.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "svga_cmd.h"
27
28 #include "pipe/p_state.h"
29 #include "pipe/p_defines.h"
30 #include "util/u_inlines.h"
31 #include "os/os_thread.h"
32 #include "util/u_format.h"
33 #include "util/u_math.h"
34 #include "util/u_memory.h"
35
36 #include "svga_screen.h"
37 #include "svga_context.h"
38 #include "svga_resource_texture.h"
39 #include "svga_sampler_view.h"
40 #include "svga_debug.h"
41 #include "svga_surface.h"
42
43
44 struct svga_sampler_view *
45 svga_get_tex_sampler_view(struct pipe_context *pipe,
46 struct pipe_resource *pt,
47 unsigned min_lod, unsigned max_lod)
48 {
49 struct svga_screen *ss = svga_screen(pt->screen);
50 struct svga_texture *tex = svga_texture(pt);
51 struct svga_sampler_view *sv = NULL;
52 SVGA3dSurfaceFlags flags = SVGA3D_SURFACE_HINT_TEXTURE;
53 SVGA3dSurfaceFormat format = svga_translate_format(pt->format);
54 boolean view = TRUE;
55
56 assert(pt);
57 assert(min_lod >= 0);
58 assert(min_lod <= max_lod);
59 assert(max_lod <= pt->last_level);
60
61
62 /* Is a view needed */
63 {
64 /*
65 * Can't control max lod. For first level views and when we only
66 * look at one level we disable mip filtering to achive the same
67 * results as a view.
68 */
69 if (min_lod == 0 && max_lod >= pt->last_level)
70 view = FALSE;
71
72 if (util_format_is_s3tc(pt->format) && view) {
73 format = svga_translate_format_render(pt->format);
74 }
75
76 if (ss->debug.no_sampler_view)
77 view = FALSE;
78
79 if (ss->debug.force_sampler_view)
80 view = TRUE;
81 }
82
83 /* First try the cache */
84 if (view) {
85 pipe_mutex_lock(ss->tex_mutex);
86 if (tex->cached_view &&
87 tex->cached_view->min_lod == min_lod &&
88 tex->cached_view->max_lod == max_lod) {
89 svga_sampler_view_reference(&sv, tex->cached_view);
90 pipe_mutex_unlock(ss->tex_mutex);
91 SVGA_DBG(DEBUG_VIEWS, "svga: Sampler view: reuse %p, %u %u, last %u\n",
92 pt, min_lod, max_lod, pt->last_level);
93 svga_validate_sampler_view(svga_context(pipe), sv);
94 return sv;
95 }
96 pipe_mutex_unlock(ss->tex_mutex);
97 }
98
99 sv = CALLOC_STRUCT(svga_sampler_view);
100 pipe_reference_init(&sv->reference, 1);
101 pipe_resource_reference(&sv->texture, pt);
102 sv->min_lod = min_lod;
103 sv->max_lod = max_lod;
104
105 /* No view needed just use the whole texture */
106 if (!view) {
107 SVGA_DBG(DEBUG_VIEWS,
108 "svga: Sampler view: no %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
109 pt, min_lod, max_lod,
110 max_lod - min_lod + 1,
111 pt->width0,
112 pt->height0,
113 pt->depth0,
114 pt->last_level);
115 sv->key.cachable = 0;
116 sv->handle = tex->handle;
117 return sv;
118 }
119
120 SVGA_DBG(DEBUG_VIEWS,
121 "svga: Sampler view: yes %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
122 pt, min_lod, max_lod,
123 max_lod - min_lod + 1,
124 pt->width0,
125 pt->height0,
126 pt->depth0,
127 pt->last_level);
128
129 sv->age = tex->age;
130 sv->handle = svga_texture_view_surface(pipe, tex, flags, format,
131 min_lod,
132 max_lod - min_lod + 1,
133 -1, -1,
134 &sv->key);
135
136 if (!sv->handle) {
137 assert(0);
138 sv->key.cachable = 0;
139 sv->handle = tex->handle;
140 return sv;
141 }
142
143 pipe_mutex_lock(ss->tex_mutex);
144 svga_sampler_view_reference(&tex->cached_view, sv);
145 pipe_mutex_unlock(ss->tex_mutex);
146
147 return sv;
148 }
149
150 void
151 svga_validate_sampler_view(struct svga_context *svga, struct svga_sampler_view *v)
152 {
153 struct svga_texture *tex = svga_texture(v->texture);
154 unsigned numFaces;
155 unsigned age = 0;
156 int i, k;
157
158 assert(svga);
159
160 if (v->handle == tex->handle)
161 return;
162
163 age = tex->age;
164
165 if(tex->b.b.target == PIPE_TEXTURE_CUBE)
166 numFaces = 6;
167 else
168 numFaces = 1;
169
170 for (i = v->min_lod; i <= v->max_lod; i++) {
171 for (k = 0; k < numFaces; k++) {
172 if (v->age < tex->view_age[i])
173 svga_texture_copy_handle(svga,
174 tex->handle, 0, 0, 0, i, k,
175 v->handle, 0, 0, 0, i - v->min_lod, k,
176 u_minify(tex->b.b.width0, i),
177 u_minify(tex->b.b.height0, i),
178 u_minify(tex->b.b.depth0, i));
179 }
180 }
181
182 v->age = age;
183 }
184
185 void
186 svga_destroy_sampler_view_priv(struct svga_sampler_view *v)
187 {
188 struct svga_texture *tex = svga_texture(v->texture);
189
190 if(v->handle != tex->handle) {
191 struct svga_screen *ss = svga_screen(v->texture->screen);
192 SVGA_DBG(DEBUG_DMA, "unref sid %p (sampler view)\n", v->handle);
193 svga_screen_surface_destroy(ss, &v->key, &v->handle);
194 }
195 pipe_resource_reference(&v->texture, NULL);
196 FREE(v);
197 }