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 **********************************************************/
27 #include "svga_debug.h"
29 #include "pipe/p_defines.h"
30 #include "util/u_pack_color.h"
32 #include "svga_context.h"
33 #include "svga_state.h"
34 #include "svga_surface.h"
37 static enum pipe_error
38 try_clear(struct svga_context
*svga
,
40 const union pipe_color_union
*color
,
44 enum pipe_error ret
= PIPE_OK
;
45 SVGA3dRect rect
= { 0, 0, 0, 0 };
46 boolean restore_viewport
= FALSE
;
47 SVGA3dClearFlag flags
= 0;
48 struct pipe_framebuffer_state
*fb
= &svga
->curr
.framebuffer
;
49 union util_color uc
= {0};
51 ret
= svga_update_state(svga
, SVGA_STATE_HW_CLEAR
);
55 if (svga
->rebind
.rendertargets
) {
56 ret
= svga_reemit_framebuffer_bindings(svga
);
62 if (buffers
& PIPE_CLEAR_COLOR
) {
63 flags
|= SVGA3D_CLEAR_COLOR
;
64 util_pack_color(color
->f
, PIPE_FORMAT_B8G8R8A8_UNORM
, &uc
);
70 if ((buffers
& PIPE_CLEAR_DEPTHSTENCIL
) && fb
->zsbuf
) {
71 if (buffers
& PIPE_CLEAR_DEPTH
)
72 flags
|= SVGA3D_CLEAR_DEPTH
;
74 if ((svga
->curr
.framebuffer
.zsbuf
->format
== PIPE_FORMAT_S8_UINT_Z24_UNORM
) &&
75 (buffers
& PIPE_CLEAR_STENCIL
))
76 flags
|= SVGA3D_CLEAR_STENCIL
;
78 rect
.w
= MAX2(rect
.w
, fb
->zsbuf
->width
);
79 rect
.h
= MAX2(rect
.h
, fb
->zsbuf
->height
);
82 if (memcmp(&rect
, &svga
->state
.hw_clear
.viewport
, sizeof(rect
)) != 0) {
83 restore_viewport
= TRUE
;
84 ret
= SVGA3D_SetViewport(svga
->swc
, &rect
);
89 ret
= SVGA3D_ClearRect(svga
->swc
, flags
, uc
.ui
[0], (float) depth
, stencil
,
90 rect
.x
, rect
.y
, rect
.w
, rect
.h
);
94 if (restore_viewport
) {
95 memcpy(&rect
, &svga
->state
.hw_clear
.viewport
, sizeof rect
);
96 ret
= SVGA3D_SetViewport(svga
->swc
, &rect
);
103 * Clear the given surface to the specified value.
104 * No masking, no scissor (clear entire buffer).
107 svga_clear(struct pipe_context
*pipe
, unsigned buffers
,
108 const union pipe_color_union
*color
,
109 double depth
, unsigned stencil
)
111 struct svga_context
*svga
= svga_context( pipe
);
114 if (buffers
& PIPE_CLEAR_COLOR
) {
115 struct svga_winsys_surface
*h
= NULL
;
116 if (svga
->curr
.framebuffer
.cbufs
[0]) {
117 h
= svga_surface(svga
->curr
.framebuffer
.cbufs
[0])->handle
;
119 SVGA_DBG(DEBUG_DMA
, "clear sid %p\n", h
);
122 /* flush any queued prims (don't want them to appear after the clear!) */
123 svga_hwtnl_flush_retry(svga
);
125 ret
= try_clear( svga
, buffers
, color
, depth
, stencil
);
127 if (ret
== PIPE_ERROR_OUT_OF_MEMORY
) {
128 /* Flush command buffer and retry:
130 svga_context_flush( svga
, NULL
);
132 ret
= try_clear( svga
, buffers
, color
, depth
, stencil
);
136 * Mark target surfaces as dirty
137 * TODO Mark only cleared surfaces.
139 svga_mark_surfaces_dirty(svga
);
141 assert (ret
== PIPE_OK
);