Merge commit 'origin/7.8'
[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_winsys.h"
41 #include "svga_debug.h"
42 #include "svga_surface.h"
43
44 #include <util/u_string.h>
45
46
47 struct svga_sampler_view *
48 svga_get_tex_sampler_view(struct pipe_context *pipe,
49 struct pipe_resource *pt,
50 unsigned min_lod, unsigned max_lod)
51 {
52 struct svga_screen *ss = svga_screen(pt->screen);
53 struct svga_texture *tex = svga_texture(pt);
54 struct svga_sampler_view *sv = NULL;
55 SVGA3dSurfaceFormat format = svga_translate_format(pt->format);
56 boolean view = TRUE;
57
58 assert(pt);
59 assert(min_lod >= 0);
60 assert(min_lod <= max_lod);
61 assert(max_lod <= pt->last_level);
62
63
64 /* Is a view needed */
65 {
66 /*
67 * Can't control max lod. For first level views and when we only
68 * look at one level we disable mip filtering to achive the same
69 * results as a view.
70 */
71 if (min_lod == 0 && max_lod >= pt->last_level)
72 view = FALSE;
73
74 if (util_format_is_s3tc(pt->format) && view) {
75 format = svga_translate_format_render(pt->format);
76 }
77
78 if (ss->debug.no_sampler_view)
79 view = FALSE;
80
81 if (ss->debug.force_sampler_view)
82 view = TRUE;
83 }
84
85 /* First try the cache */
86 if (view) {
87 pipe_mutex_lock(ss->tex_mutex);
88 if (tex->cached_view &&
89 tex->cached_view->min_lod == min_lod &&
90 tex->cached_view->max_lod == max_lod) {
91 svga_sampler_view_reference(&sv, tex->cached_view);
92 pipe_mutex_unlock(ss->tex_mutex);
93 SVGA_DBG(DEBUG_VIEWS, "svga: Sampler view: reuse %p, %u %u, last %u\n",
94 pt, min_lod, max_lod, pt->last_level);
95 svga_validate_sampler_view(svga_context(pipe), sv);
96 return sv;
97 }
98 pipe_mutex_unlock(ss->tex_mutex);
99 }
100
101 sv = CALLOC_STRUCT(svga_sampler_view);
102 pipe_reference_init(&sv->reference, 1);
103 pipe_resource_reference(&sv->texture, pt);
104 sv->min_lod = min_lod;
105 sv->max_lod = max_lod;
106
107 /* No view needed just use the whole texture */
108 if (!view) {
109 SVGA_DBG(DEBUG_VIEWS,
110 "svga: Sampler view: no %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
111 pt, min_lod, max_lod,
112 max_lod - min_lod + 1,
113 pt->width0,
114 pt->height0,
115 pt->depth0,
116 pt->last_level);
117 sv->key.cachable = 0;
118 sv->handle = tex->handle;
119 return sv;
120 }
121
122 SVGA_DBG(DEBUG_VIEWS,
123 "svga: Sampler view: yes %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
124 pt, min_lod, max_lod,
125 max_lod - min_lod + 1,
126 pt->width0,
127 pt->height0,
128 pt->depth0,
129 pt->last_level);
130
131 sv->age = tex->age;
132 sv->handle = svga_texture_view_surface(pipe, tex, format,
133 min_lod,
134 max_lod - min_lod + 1,
135 -1, -1,
136 &sv->key);
137
138 if (!sv->handle) {
139 assert(0);
140 sv->key.cachable = 0;
141 sv->handle = tex->handle;
142 return sv;
143 }
144
145 pipe_mutex_lock(ss->tex_mutex);
146 svga_sampler_view_reference(&tex->cached_view, sv);
147 pipe_mutex_unlock(ss->tex_mutex);
148
149 return sv;
150 }
151
152 void
153 svga_validate_sampler_view(struct svga_context *svga, struct svga_sampler_view *v)
154 {
155 struct svga_texture *tex = svga_texture(v->texture);
156 unsigned numFaces;
157 unsigned age = 0;
158 int i, k;
159
160 assert(svga);
161
162 if (v->handle == tex->handle)
163 return;
164
165 age = tex->age;
166
167 if(tex->b.b.target == PIPE_TEXTURE_CUBE)
168 numFaces = 6;
169 else
170 numFaces = 1;
171
172 for (i = v->min_lod; i <= v->max_lod; i++) {
173 for (k = 0; k < numFaces; k++) {
174 if (v->age < tex->view_age[i])
175 svga_texture_copy_handle(svga, NULL,
176 tex->handle, 0, 0, 0, i, k,
177 v->handle, 0, 0, 0, i - v->min_lod, k,
178 u_minify(tex->b.b.width0, i),
179 u_minify(tex->b.b.height0, i),
180 u_minify(tex->b.b.depth0, i));
181 }
182 }
183
184 v->age = age;
185 }
186
187 void
188 svga_destroy_sampler_view_priv(struct svga_sampler_view *v)
189 {
190 struct svga_texture *tex = svga_texture(v->texture);
191
192 if(v->handle != tex->handle) {
193 struct svga_screen *ss = svga_screen(v->texture->screen);
194 SVGA_DBG(DEBUG_DMA, "unref sid %p (sampler view)\n", v->handle);
195 svga_screen_surface_destroy(ss, &v->key, &v->handle);
196 }
197 pipe_resource_reference(&v->texture, NULL);
198 FREE(v);
199 }