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