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_debug.h"
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
)
53 struct svga_surface dst
, src
;
55 SVGA3dCopyBox box
, *boxes
;
59 src
.handle
= src_handle
;
60 src
.real_level
= src_level
;
61 src
.real_face
= src_face
;
64 dst
.handle
= dst_handle
;
65 dst
.real_level
= dst_level
;
66 dst
.real_face
= dst_face
;
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);
85 ret
= SVGA3D_BeginSurfaceCopy(svga
->swc
,
90 svga_context_flush(svga
, NULL
);
91 ret
= SVGA3D_BeginSurfaceCopy(svga
->swc
,
95 assert(ret
== PIPE_OK
);
98 SVGA_FIFOCommitAll(svga
->swc
);
102 struct svga_winsys_surface
*
103 svga_texture_view_surface(struct pipe_context
*pipe
,
104 struct svga_texture
*tex
,
105 SVGA3dSurfaceFormat format
,
110 struct svga_host_surface_cache_key
*key
) /* OUT */
112 struct svga_screen
*ss
= svga_screen(pipe
->screen
);
113 struct svga_winsys_surface
*handle
;
115 unsigned z_offset
= 0;
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);
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;
128 assert(key
->size
.depth
== 1);
130 if(tex
->b
.b
.target
== PIPE_TEXTURE_CUBE
&& face_pick
< 0) {
131 key
->flags
|= SVGA3D_SURFACE_CUBEMAP
;
137 if(key
->format
== SVGA3D_FORMAT_INVALID
) {
142 SVGA_DBG(DEBUG_DMA
, "surface_create for texture view\n");
143 handle
= svga_screen_surface_create(ss
, key
);
149 SVGA_DBG(DEBUG_DMA
, " --> got sid %p (texture view)\n", handle
);
154 if (zslice_pick
>= 0)
155 z_offset
= zslice_pick
;
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
) :
164 svga_texture_copy_handle(svga_context(pipe
),
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
),
181 static struct pipe_surface
*
182 svga_get_tex_surface(struct pipe_screen
*screen
,
183 struct pipe_resource
*pt
,
184 unsigned face
, unsigned level
, unsigned zslice
,
187 struct svga_texture
*tex
= svga_texture(pt
);
188 struct svga_surface
*s
;
189 boolean render
= (flags
& (PIPE_BIND_RENDER_TARGET
|
190 PIPE_BIND_DEPTH_STENCIL
)) ? TRUE
: FALSE
;
191 boolean view
= FALSE
;
192 SVGA3dSurfaceFormat format
;
194 s
= CALLOC_STRUCT(svga_surface
);
198 pipe_reference_init(&s
->base
.reference
, 1);
199 pipe_resource_reference(&s
->base
.texture
, pt
);
200 s
->base
.format
= pt
->format
;
201 s
->base
.width
= u_minify(pt
->width0
, level
);
202 s
->base
.height
= u_minify(pt
->height0
, level
);
203 s
->base
.usage
= flags
;
204 s
->base
.level
= level
;
206 s
->base
.zslice
= zslice
;
209 format
= svga_translate_format(pt
->format
);
211 format
= svga_translate_format_render(pt
->format
);
213 assert(format
!= SVGA3D_FORMAT_INVALID
);
215 if (svga_screen(screen
)->debug
.force_surface_view
)
218 /* Currently only used for compressed textures */
220 format
!= svga_translate_format(pt
->format
)) {
225 svga_screen(screen
)->debug
.force_level_surface_view
)
228 if (pt
->target
== PIPE_TEXTURE_3D
)
231 if (svga_screen(screen
)->debug
.no_surface_view
)
235 SVGA_DBG(DEBUG_VIEWS
, "svga: Surface view: yes %p, level %u face %u z %u, %p\n",
236 pt
, level
, face
, zslice
, s
);
238 s
->handle
= svga_texture_view_surface(NULL
, tex
, format
, level
, 1, face
, zslice
,
244 SVGA_DBG(DEBUG_VIEWS
, "svga: Surface view: no %p, level %u, face %u, z %u, %p\n",
245 pt
, level
, face
, zslice
, s
);
247 memset(&s
->key
, 0, sizeof s
->key
);
248 s
->handle
= tex
->handle
;
250 s
->real_level
= level
;
251 s
->real_zslice
= zslice
;
259 svga_tex_surface_destroy(struct pipe_surface
*surf
)
261 struct svga_surface
*s
= svga_surface(surf
);
262 struct svga_texture
*t
= svga_texture(surf
->texture
);
263 struct svga_screen
*ss
= svga_screen(surf
->texture
->screen
);
265 if(s
->handle
!= t
->handle
) {
266 SVGA_DBG(DEBUG_DMA
, "unref sid %p (tex surface)\n", s
->handle
);
267 svga_screen_surface_destroy(ss
, &s
->key
, &s
->handle
);
270 pipe_resource_reference(&surf
->texture
, NULL
);
276 svga_mark_surface_dirty(struct pipe_surface
*surf
)
278 struct svga_surface
*s
= svga_surface(surf
);
281 struct svga_texture
*tex
= svga_texture(surf
->texture
);
285 if (s
->handle
== tex
->handle
)
286 tex
->defined
[surf
->face
][surf
->level
] = TRUE
;
288 /* this will happen later in svga_propagate_surface */
294 void svga_mark_surfaces_dirty(struct svga_context
*svga
)
298 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
299 if (svga
->curr
.framebuffer
.cbufs
[i
])
300 svga_mark_surface_dirty(svga
->curr
.framebuffer
.cbufs
[i
]);
302 if (svga
->curr
.framebuffer
.zsbuf
)
303 svga_mark_surface_dirty(svga
->curr
.framebuffer
.zsbuf
);
308 * Progagate any changes from surfaces to texture.
309 * pipe is optional context to inline the blit command in.
312 svga_propagate_surface(struct pipe_context
*pipe
, struct pipe_surface
*surf
)
314 struct svga_surface
*s
= svga_surface(surf
);
315 struct svga_texture
*tex
= svga_texture(surf
->texture
);
316 struct svga_screen
*ss
= svga_screen(surf
->texture
->screen
);
322 ss
->texture_timestamp
++;
323 tex
->view_age
[surf
->level
] = ++(tex
->age
);
325 if (s
->handle
!= tex
->handle
) {
326 SVGA_DBG(DEBUG_VIEWS
, "svga: Surface propagate: tex %p, level %u, from %p\n", tex
, surf
->level
, surf
);
327 svga_texture_copy_handle(svga_context(pipe
),
328 s
->handle
, 0, 0, 0, s
->real_level
, s
->real_face
,
329 tex
->handle
, 0, 0, surf
->zslice
, surf
->level
, surf
->face
,
330 u_minify(tex
->b
.b
.width0
, surf
->level
),
331 u_minify(tex
->b
.b
.height0
, surf
->level
), 1);
332 tex
->defined
[surf
->face
][surf
->level
] = TRUE
;
337 * Check if we should call svga_propagate_surface on the surface.
340 svga_surface_needs_propagation(struct pipe_surface
*surf
)
342 struct svga_surface
*s
= svga_surface(surf
);
343 struct svga_texture
*tex
= svga_texture(surf
->texture
);
345 return s
->dirty
&& s
->handle
!= tex
->handle
;
354 svga_screen_init_surface_functions(struct pipe_screen
*screen
)
356 screen
->get_tex_surface
= svga_get_tex_surface
;
357 screen
->tex_surface_destroy
= svga_tex_surface_destroy
;