Merge commit 'origin/master' into gallium-msaa
[mesa.git] / src / gallium / drivers / svga / svga_surface.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_surface.h"
40 #include "svga_winsys.h"
41 #include "svga_debug.h"
42
43
44 void
45 svga_texture_copy_handle(struct svga_context *svga,
46 struct svga_winsys_surface *src_handle,
47 unsigned src_x, unsigned src_y, unsigned src_z,
48 unsigned src_level, unsigned src_face,
49 struct svga_winsys_surface *dst_handle,
50 unsigned dst_x, unsigned dst_y, unsigned dst_z,
51 unsigned dst_level, unsigned dst_face,
52 unsigned width, unsigned height, unsigned depth)
53 {
54 struct svga_surface dst, src;
55 enum pipe_error ret;
56 SVGA3dCopyBox box, *boxes;
57
58 assert(svga);
59
60 src.handle = src_handle;
61 src.real_level = src_level;
62 src.real_face = src_face;
63 src.real_zslice = 0;
64
65 dst.handle = dst_handle;
66 dst.real_level = dst_level;
67 dst.real_face = dst_face;
68 dst.real_zslice = 0;
69
70 box.x = dst_x;
71 box.y = dst_y;
72 box.z = dst_z;
73 box.w = width;
74 box.h = height;
75 box.d = depth;
76 box.srcx = src_x;
77 box.srcy = src_y;
78 box.srcz = src_z;
79
80 /*
81 SVGA_DBG(DEBUG_VIEWS, "mipcopy src: %p %u (%ux%ux%u), dst: %p %u (%ux%ux%u)\n",
82 src_handle, src_level, src_x, src_y, src_z,
83 dst_handle, dst_level, dst_x, dst_y, dst_z);
84 */
85
86 ret = SVGA3D_BeginSurfaceCopy(svga->swc,
87 &src.base,
88 &dst.base,
89 &boxes, 1);
90 if(ret != PIPE_OK) {
91 svga_context_flush(svga, NULL);
92 ret = SVGA3D_BeginSurfaceCopy(svga->swc,
93 &src.base,
94 &dst.base,
95 &boxes, 1);
96 assert(ret == PIPE_OK);
97 }
98 *boxes = box;
99 SVGA_FIFOCommitAll(svga->swc);
100 }
101
102
103 struct svga_winsys_surface *
104 svga_texture_view_surface(struct pipe_context *pipe,
105 struct svga_texture *tex,
106 SVGA3dSurfaceFormat format,
107 unsigned start_mip,
108 unsigned num_mip,
109 int face_pick,
110 int zslice_pick,
111 struct svga_host_surface_cache_key *key) /* OUT */
112 {
113 struct svga_screen *ss = svga_screen(pipe->screen);
114 struct svga_winsys_surface *handle;
115 uint32_t i, j;
116 unsigned z_offset = 0;
117
118 SVGA_DBG(DEBUG_PERF,
119 "svga: Create surface view: face %d zslice %d mips %d..%d\n",
120 face_pick, zslice_pick, start_mip, start_mip+num_mip-1);
121
122 key->flags = 0;
123 key->format = format;
124 key->numMipLevels = num_mip;
125 key->size.width = u_minify(tex->b.b.width0, start_mip);
126 key->size.height = u_minify(tex->b.b.height0, start_mip);
127 key->size.depth = zslice_pick < 0 ? u_minify(tex->b.b.depth0, start_mip) : 1;
128 key->cachable = 1;
129 assert(key->size.depth == 1);
130
131 if(tex->b.b.target == PIPE_TEXTURE_CUBE && face_pick < 0) {
132 key->flags |= SVGA3D_SURFACE_CUBEMAP;
133 key->numFaces = 6;
134 } else {
135 key->numFaces = 1;
136 }
137
138 if(key->format == SVGA3D_FORMAT_INVALID) {
139 key->cachable = 0;
140 return NULL;
141 }
142
143 SVGA_DBG(DEBUG_DMA, "surface_create for texture view\n");
144 handle = svga_screen_surface_create(ss, key);
145 if (!handle) {
146 key->cachable = 0;
147 return NULL;
148 }
149
150 SVGA_DBG(DEBUG_DMA, " --> got sid %p (texture view)\n", handle);
151
152 if (face_pick < 0)
153 face_pick = 0;
154
155 if (zslice_pick >= 0)
156 z_offset = zslice_pick;
157
158 for (i = 0; i < key->numMipLevels; i++) {
159 for (j = 0; j < key->numFaces; j++) {
160 if(tex->defined[j + face_pick][i + start_mip]) {
161 unsigned depth = (zslice_pick < 0 ?
162 u_minify(tex->b.b.depth0, i + start_mip) :
163 1);
164
165 svga_texture_copy_handle(svga_context(pipe),
166 tex->handle,
167 0, 0, z_offset,
168 i + start_mip,
169 j + face_pick,
170 handle, 0, 0, 0, i, j,
171 u_minify(tex->b.b.width0, i + start_mip),
172 u_minify(tex->b.b.height0, i + start_mip),
173 depth);
174 }
175 }
176 }
177
178 return handle;
179 }
180
181
182 static struct pipe_surface *
183 svga_get_tex_surface(struct pipe_screen *screen,
184 struct pipe_resource *pt,
185 unsigned face, unsigned level, unsigned zslice,
186 unsigned flags)
187 {
188 struct svga_texture *tex = svga_texture(pt);
189 struct svga_surface *s;
190 boolean render = (flags & (PIPE_BIND_RENDER_TARGET |
191 PIPE_BIND_DEPTH_STENCIL)) ? TRUE : FALSE;
192 boolean view = FALSE;
193 SVGA3dSurfaceFormat format;
194
195 s = CALLOC_STRUCT(svga_surface);
196 if (!s)
197 return NULL;
198
199 pipe_reference_init(&s->base.reference, 1);
200 pipe_resource_reference(&s->base.texture, pt);
201 s->base.format = pt->format;
202 s->base.width = u_minify(pt->width0, level);
203 s->base.height = u_minify(pt->height0, level);
204 s->base.usage = flags;
205 s->base.level = level;
206 s->base.face = face;
207 s->base.zslice = zslice;
208
209 if (!render)
210 format = svga_translate_format(pt->format);
211 else
212 format = svga_translate_format_render(pt->format);
213
214 assert(format != SVGA3D_FORMAT_INVALID);
215
216 if (svga_screen(screen)->debug.force_surface_view)
217 view = TRUE;
218
219 /* Currently only used for compressed textures */
220 if (render &&
221 format != svga_translate_format(pt->format)) {
222 view = TRUE;
223 }
224
225 if (level != 0 &&
226 svga_screen(screen)->debug.force_level_surface_view)
227 view = TRUE;
228
229 if (pt->target == PIPE_TEXTURE_3D)
230 view = TRUE;
231
232 if (svga_screen(screen)->debug.no_surface_view)
233 view = FALSE;
234
235 if (view) {
236 SVGA_DBG(DEBUG_VIEWS, "svga: Surface view: yes %p, level %u face %u z %u, %p\n",
237 pt, level, face, zslice, s);
238
239 s->handle = svga_texture_view_surface(NULL, tex, format, level, 1, face, zslice,
240 &s->key);
241 s->real_face = 0;
242 s->real_level = 0;
243 s->real_zslice = 0;
244 } else {
245 SVGA_DBG(DEBUG_VIEWS, "svga: Surface view: no %p, level %u, face %u, z %u, %p\n",
246 pt, level, face, zslice, s);
247
248 memset(&s->key, 0, sizeof s->key);
249 s->handle = tex->handle;
250 s->real_face = face;
251 s->real_level = level;
252 s->real_zslice = zslice;
253 }
254
255 return &s->base;
256 }
257
258
259 static void
260 svga_tex_surface_destroy(struct pipe_surface *surf)
261 {
262 struct svga_surface *s = svga_surface(surf);
263 struct svga_texture *t = svga_texture(surf->texture);
264 struct svga_screen *ss = svga_screen(surf->texture->screen);
265
266 if(s->handle != t->handle) {
267 SVGA_DBG(DEBUG_DMA, "unref sid %p (tex surface)\n", s->handle);
268 svga_screen_surface_destroy(ss, &s->key, &s->handle);
269 }
270
271 pipe_resource_reference(&surf->texture, NULL);
272 FREE(surf);
273 }
274
275
276 static INLINE void
277 svga_mark_surface_dirty(struct pipe_surface *surf)
278 {
279 struct svga_surface *s = svga_surface(surf);
280
281 if(!s->dirty) {
282 struct svga_texture *tex = svga_texture(surf->texture);
283
284 s->dirty = TRUE;
285
286 if (s->handle == tex->handle)
287 tex->defined[surf->face][surf->level] = TRUE;
288 else {
289 /* this will happen later in svga_propagate_surface */
290 }
291 }
292 }
293
294
295 void svga_mark_surfaces_dirty(struct svga_context *svga)
296 {
297 unsigned i;
298
299 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
300 if (svga->curr.framebuffer.cbufs[i])
301 svga_mark_surface_dirty(svga->curr.framebuffer.cbufs[i]);
302 }
303 if (svga->curr.framebuffer.zsbuf)
304 svga_mark_surface_dirty(svga->curr.framebuffer.zsbuf);
305 }
306
307
308 /**
309 * Progagate any changes from surfaces to texture.
310 * pipe is optional context to inline the blit command in.
311 */
312 void
313 svga_propagate_surface(struct pipe_context *pipe, struct pipe_surface *surf)
314 {
315 struct svga_surface *s = svga_surface(surf);
316 struct svga_texture *tex = svga_texture(surf->texture);
317 struct svga_screen *ss = svga_screen(surf->texture->screen);
318
319 if (!s->dirty)
320 return;
321
322 s->dirty = FALSE;
323 ss->texture_timestamp++;
324 tex->view_age[surf->level] = ++(tex->age);
325
326 if (s->handle != tex->handle) {
327 SVGA_DBG(DEBUG_VIEWS, "svga: Surface propagate: tex %p, level %u, from %p\n", tex, surf->level, surf);
328 svga_texture_copy_handle(svga_context(pipe),
329 s->handle, 0, 0, 0, s->real_level, s->real_face,
330 tex->handle, 0, 0, surf->zslice, surf->level, surf->face,
331 u_minify(tex->b.b.width0, surf->level),
332 u_minify(tex->b.b.height0, surf->level), 1);
333 tex->defined[surf->face][surf->level] = TRUE;
334 }
335 }
336
337 /**
338 * Check if we should call svga_propagate_surface on the surface.
339 */
340 boolean
341 svga_surface_needs_propagation(struct pipe_surface *surf)
342 {
343 struct svga_surface *s = svga_surface(surf);
344 struct svga_texture *tex = svga_texture(surf->texture);
345
346 return s->dirty && s->handle != tex->handle;
347 }
348
349
350
351
352
353
354 void
355 svga_screen_init_surface_functions(struct pipe_screen *screen)
356 {
357 screen->get_tex_surface = svga_get_tex_surface;
358 screen->tex_surface_destroy = svga_tex_surface_destroy;
359 }
360