1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 **********************************************************/
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"
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"
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
)
55 struct svga_surface dst
, src
;
57 SVGA3dCopyBox box
, *boxes
;
61 src
.handle
= src_handle
;
62 src
.real_level
= src_level
;
63 src
.real_face
= src_face
;
66 dst
.handle
= dst_handle
;
67 dst
.real_level
= dst_level
;
68 dst
.real_face
= dst_face
;
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);
88 ret
= SVGA3D_BeginSurfaceCopy(svga
->swc
,
93 svga_context_flush(svga
, NULL
);
94 ret
= SVGA3D_BeginSurfaceCopy(svga
->swc
,
98 assert(ret
== PIPE_OK
);
101 SVGA_FIFOCommitAll(svga
->swc
);
103 pipe_mutex_lock(ss
->swc_mutex
);
104 ret
= SVGA3D_BeginSurfaceCopy(ss
->swc
,
109 ss
->swc
->flush(ss
->swc
, NULL
);
110 ret
= SVGA3D_BeginSurfaceCopy(ss
->swc
,
114 assert(ret
== PIPE_OK
);
117 SVGA_FIFOCommitAll(ss
->swc
);
118 pipe_mutex_unlock(ss
->swc_mutex
);
123 struct svga_winsys_surface
*
124 svga_texture_view_surface(struct pipe_context
*pipe
,
125 struct svga_texture
*tex
,
126 SVGA3dSurfaceFormat format
,
131 struct svga_host_surface_cache_key
*key
) /* OUT */
133 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
134 struct svga_winsys_surface
*handle
;
136 unsigned z_offset
= 0;
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);
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;
149 assert(key
->size
.depth
== 1);
151 if(tex
->b
.b
.target
== PIPE_TEXTURE_CUBE
&& face_pick
< 0) {
152 key
->flags
|= SVGA3D_SURFACE_CUBEMAP
;
158 if(key
->format
== SVGA3D_FORMAT_INVALID
) {
163 SVGA_DBG(DEBUG_DMA
, "surface_create for texture view\n");
164 handle
= svga_screen_surface_create(ss
, key
);
170 SVGA_DBG(DEBUG_DMA
, " --> got sid %p (texture view)\n", handle
);
175 if (zslice_pick
>= 0)
176 z_offset
= zslice_pick
;
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
) :
185 svga_texture_copy_handle(svga_context(pipe
),
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
),
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
,
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
;
216 s
= CALLOC_STRUCT(svga_surface
);
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
;
228 s
->base
.zslice
= zslice
;
231 format
= svga_translate_format(pt
->format
);
233 format
= svga_translate_format_render(pt
->format
);
235 assert(format
!= SVGA3D_FORMAT_INVALID
);
237 if (svga_screen(screen
)->debug
.force_surface_view
)
240 /* Currently only used for compressed textures */
242 format
!= svga_translate_format(pt
->format
)) {
247 svga_screen(screen
)->debug
.force_level_surface_view
)
250 if (pt
->target
== PIPE_TEXTURE_3D
)
253 if (svga_screen(screen
)->debug
.no_surface_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
);
260 s
->handle
= svga_texture_view_surface(NULL
, tex
, format
, level
, 1, face
, zslice
,
266 SVGA_DBG(DEBUG_VIEWS
, "svga: Surface view: no %p, level %u, face %u, z %u, %p\n",
267 pt
, level
, face
, zslice
, s
);
269 memset(&s
->key
, 0, sizeof s
->key
);
270 s
->handle
= tex
->handle
;
272 s
->real_level
= level
;
273 s
->real_zslice
= zslice
;
281 svga_tex_surface_destroy(struct pipe_surface
*surf
)
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
);
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
);
292 pipe_resource_reference(&surf
->texture
, NULL
);
298 svga_mark_surface_dirty(struct pipe_surface
*surf
)
300 struct svga_surface
*s
= svga_surface(surf
);
303 struct svga_texture
*tex
= svga_texture(surf
->texture
);
307 if (s
->handle
== tex
->handle
)
308 tex
->defined
[surf
->face
][surf
->level
] = TRUE
;
310 /* this will happen later in svga_propagate_surface */
316 void svga_mark_surfaces_dirty(struct svga_context
*svga
)
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
]);
324 if (svga
->curr
.framebuffer
.zsbuf
)
325 svga_mark_surface_dirty(svga
->curr
.framebuffer
.zsbuf
);
330 * Progagate any changes from surfaces to texture.
331 * pipe is optional context to inline the blit command in.
334 svga_propagate_surface(struct pipe_context
*pipe
, struct pipe_surface
*surf
)
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
);
344 ss
->texture_timestamp
++;
345 tex
->view_age
[surf
->level
] = ++(tex
->age
);
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
;
359 * Check if we should call svga_propagate_surface on the surface.
362 svga_surface_needs_propagation(struct pipe_surface
*surf
)
364 struct svga_surface
*s
= svga_surface(surf
);
365 struct svga_texture
*tex
= svga_texture(surf
->texture
);
367 return s
->dirty
&& s
->handle
!= tex
->handle
;
376 svga_screen_init_surface_functions(struct pipe_screen
*screen
)
378 screen
->get_tex_surface
= svga_get_tex_surface
;
379 screen
->tex_surface_destroy
= svga_tex_surface_destroy
;