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