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