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_format.h"
37 #include "svga_screen.h"
38 #include "svga_context.h"
39 #include "svga_resource_texture.h"
40 #include "svga_surface.h"
41 #include "svga_debug.h"
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
)
54 struct svga_surface dst
, src
;
56 SVGA3dCopyBox box
, *boxes
;
60 src
.handle
= src_handle
;
61 src
.real_level
= src_level
;
62 src
.real_face
= src_face
;
65 dst
.handle
= dst_handle
;
66 dst
.real_level
= dst_level
;
67 dst
.real_face
= dst_face
;
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);
86 ret
= SVGA3D_BeginSurfaceCopy(svga
->swc
,
91 svga_context_flush(svga
, NULL
);
92 ret
= SVGA3D_BeginSurfaceCopy(svga
->swc
,
96 assert(ret
== PIPE_OK
);
99 SVGA_FIFOCommitAll(svga
->swc
);
103 struct svga_winsys_surface
*
104 svga_texture_view_surface(struct svga_context
*svga
,
105 struct svga_texture
*tex
,
106 SVGA3dSurfaceFlags flags
,
107 SVGA3dSurfaceFormat format
,
112 struct svga_host_surface_cache_key
*key
) /* OUT */
114 struct svga_screen
*ss
= svga_screen(svga
->pipe
.screen
);
115 struct svga_winsys_surface
*handle
;
117 unsigned z_offset
= 0;
120 "svga: Create surface view: face %d zslice %d mips %d..%d\n",
121 face_pick
, zslice_pick
, start_mip
, start_mip
+num_mip
-1);
124 key
->format
= format
;
125 key
->numMipLevels
= num_mip
;
126 key
->size
.width
= u_minify(tex
->b
.b
.width0
, start_mip
);
127 key
->size
.height
= u_minify(tex
->b
.b
.height0
, start_mip
);
128 key
->size
.depth
= zslice_pick
< 0 ? u_minify(tex
->b
.b
.depth0
, start_mip
) : 1;
130 assert(key
->size
.depth
== 1);
132 if (tex
->b
.b
.target
== PIPE_TEXTURE_CUBE
&& face_pick
< 0) {
133 key
->flags
|= SVGA3D_SURFACE_CUBEMAP
;
139 if (key
->format
== SVGA3D_FORMAT_INVALID
) {
144 SVGA_DBG(DEBUG_DMA
, "surface_create for texture view\n");
145 handle
= svga_screen_surface_create(ss
, key
);
151 SVGA_DBG(DEBUG_DMA
, " --> got sid %p (texture view)\n", handle
);
156 if (zslice_pick
>= 0)
157 z_offset
= zslice_pick
;
159 for (i
= 0; i
< key
->numMipLevels
; i
++) {
160 for (j
= 0; j
< key
->numFaces
; j
++) {
161 if (svga_is_texture_level_defined(tex
, j
+ face_pick
, i
+ start_mip
)) {
162 unsigned depth
= (zslice_pick
< 0 ?
163 u_minify(tex
->b
.b
.depth0
, i
+ start_mip
) :
166 svga_texture_copy_handle(svga
,
171 handle
, 0, 0, 0, i
, j
,
172 u_minify(tex
->b
.b
.width0
, i
+ start_mip
),
173 u_minify(tex
->b
.b
.height0
, i
+ start_mip
),
183 static struct pipe_surface
*
184 svga_create_surface(struct pipe_context
*pipe
,
185 struct pipe_resource
*pt
,
186 const struct pipe_surface
*surf_tmpl
)
188 struct svga_context
*svga
= svga_context(pipe
);
189 struct svga_texture
*tex
= svga_texture(pt
);
190 struct pipe_screen
*screen
= pipe
->screen
;
191 struct svga_screen
*ss
= svga_screen(screen
);
192 struct svga_surface
*s
;
193 unsigned face
, zslice
;
194 boolean view
= FALSE
;
195 SVGA3dSurfaceFlags flags
;
196 SVGA3dSurfaceFormat format
;
198 assert(surf_tmpl
->u
.tex
.first_layer
== surf_tmpl
->u
.tex
.last_layer
);
200 s
= CALLOC_STRUCT(svga_surface
);
204 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
205 face
= surf_tmpl
->u
.tex
.first_layer
;
210 zslice
= surf_tmpl
->u
.tex
.first_layer
;
213 pipe_reference_init(&s
->base
.reference
, 1);
214 pipe_resource_reference(&s
->base
.texture
, pt
);
215 s
->base
.context
= pipe
;
216 s
->base
.format
= surf_tmpl
->format
;
217 s
->base
.width
= u_minify(pt
->width0
, surf_tmpl
->u
.tex
.level
);
218 s
->base
.height
= u_minify(pt
->height0
, surf_tmpl
->u
.tex
.level
);
219 s
->base
.u
.tex
.level
= surf_tmpl
->u
.tex
.level
;
220 s
->base
.u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
221 s
->base
.u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
223 if (util_format_is_depth_or_stencil(surf_tmpl
->format
)) {
224 flags
= SVGA3D_SURFACE_HINT_DEPTHSTENCIL
;
227 flags
= SVGA3D_SURFACE_HINT_RENDERTARGET
;
230 format
= svga_translate_format(ss
, surf_tmpl
->format
, 0);
231 assert(format
!= SVGA3D_FORMAT_INVALID
);
233 if (svga_screen(screen
)->debug
.force_surface_view
)
236 /* Currently only used for compressed textures */
237 if (format
!= svga_translate_format(ss
, surf_tmpl
->format
, 0)) {
241 if (surf_tmpl
->u
.tex
.level
!= 0 &&
242 svga_screen(screen
)->debug
.force_level_surface_view
)
245 if (pt
->target
== PIPE_TEXTURE_3D
)
248 if (svga_screen(screen
)->debug
.no_surface_view
)
252 SVGA_DBG(DEBUG_VIEWS
, "svga: Surface view: yes %p, level %u face %u z %u, %p\n",
253 pt
, surf_tmpl
->u
.tex
.level
, face
, zslice
, s
);
255 s
->handle
= svga_texture_view_surface(svga
, tex
, flags
, format
,
256 surf_tmpl
->u
.tex
.level
,
257 1, face
, zslice
, &s
->key
);
262 SVGA_DBG(DEBUG_VIEWS
, "svga: Surface view: no %p, level %u, face %u, z %u, %p\n",
263 pt
, surf_tmpl
->u
.tex
.level
, face
, zslice
, s
);
265 memset(&s
->key
, 0, sizeof s
->key
);
266 s
->handle
= tex
->handle
;
268 s
->real_zslice
= zslice
;
269 s
->real_level
= surf_tmpl
->u
.tex
.level
;
277 svga_surface_destroy(struct pipe_context
*pipe
,
278 struct pipe_surface
*surf
)
280 struct svga_surface
*s
= svga_surface(surf
);
281 struct svga_texture
*t
= svga_texture(surf
->texture
);
282 struct svga_screen
*ss
= svga_screen(surf
->texture
->screen
);
284 if (s
->handle
!= t
->handle
) {
285 SVGA_DBG(DEBUG_DMA
, "unref sid %p (tex surface)\n", s
->handle
);
286 svga_screen_surface_destroy(ss
, &s
->key
, &s
->handle
);
289 pipe_resource_reference(&surf
->texture
, NULL
);
295 svga_mark_surface_dirty(struct pipe_surface
*surf
)
297 struct svga_surface
*s
= svga_surface(surf
);
300 struct svga_texture
*tex
= svga_texture(surf
->texture
);
304 if (s
->handle
== tex
->handle
) {
305 /* hmm so 3d textures always have all their slices marked ? */
306 if (surf
->texture
->target
== PIPE_TEXTURE_CUBE
)
307 svga_define_texture_level(tex
, surf
->u
.tex
.first_layer
,
310 svga_define_texture_level(tex
, 0, surf
->u
.tex
.level
);
313 /* this will happen later in svga_propagate_surface */
316 /* Increment the view_age and texture age for this surface's mipmap
317 * level so that any sampler views into the texture are re-validated too.
319 svga_age_texture_view(tex
, surf
->u
.tex
.level
);
325 svga_mark_surfaces_dirty(struct svga_context
*svga
)
327 struct svga_screen
*svgascreen
= svga_screen(svga
->pipe
.screen
);
330 for (i
= 0; i
< svgascreen
->max_color_buffers
; i
++) {
331 if (svga
->curr
.framebuffer
.cbufs
[i
])
332 svga_mark_surface_dirty(svga
->curr
.framebuffer
.cbufs
[i
]);
334 if (svga
->curr
.framebuffer
.zsbuf
)
335 svga_mark_surface_dirty(svga
->curr
.framebuffer
.zsbuf
);
340 * Progagate any changes from surfaces to texture.
341 * pipe is optional context to inline the blit command in.
344 svga_propagate_surface(struct svga_context
*svga
, struct pipe_surface
*surf
)
346 struct svga_surface
*s
= svga_surface(surf
);
347 struct svga_texture
*tex
= svga_texture(surf
->texture
);
348 struct svga_screen
*ss
= svga_screen(surf
->texture
->screen
);
349 unsigned zslice
, face
;
354 if (surf
->texture
->target
== PIPE_TEXTURE_CUBE
) {
356 face
= surf
->u
.tex
.first_layer
;
359 zslice
= surf
->u
.tex
.first_layer
;
364 ss
->texture_timestamp
++;
365 svga_age_texture_view(tex
, surf
->u
.tex
.level
);
367 if (s
->handle
!= tex
->handle
) {
368 SVGA_DBG(DEBUG_VIEWS
,
369 "svga: Surface propagate: tex %p, level %u, from %p\n",
370 tex
, surf
->u
.tex
.level
, surf
);
371 svga_texture_copy_handle(svga
,
372 s
->handle
, 0, 0, 0, s
->real_level
, s
->real_face
,
373 tex
->handle
, 0, 0, zslice
, surf
->u
.tex
.level
, face
,
374 u_minify(tex
->b
.b
.width0
, surf
->u
.tex
.level
),
375 u_minify(tex
->b
.b
.height0
, surf
->u
.tex
.level
), 1);
376 svga_define_texture_level(tex
, face
, surf
->u
.tex
.level
);
382 * Check if we should call svga_propagate_surface on the surface.
385 svga_surface_needs_propagation(const struct pipe_surface
*surf
)
387 const struct svga_surface
*s
= svga_surface_const(surf
);
388 struct svga_texture
*tex
= svga_texture(surf
->texture
);
390 return s
->dirty
&& s
->handle
!= tex
->handle
;
396 svga_init_surface_functions(struct svga_context
*svga
)
398 svga
->pipe
.create_surface
= svga_create_surface
;
399 svga
->pipe
.surface_destroy
= svga_surface_destroy
;