8977d26541c1bf8c65da4f1a9a04966009bbaa71
[mesa.git] / src / gallium / drivers / svga / svga_pipe_clear.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_defines.h"
29 #include "util/u_pack_color.h"
30
31 #include "svga_context.h"
32 #include "svga_state.h"
33
34
35 static enum pipe_error
36 try_clear(struct svga_context *svga,
37 unsigned buffers,
38 const float *rgba,
39 double depth,
40 unsigned stencil)
41 {
42 int ret = PIPE_OK;
43 SVGA3dRect rect = { 0, 0, 0, 0 };
44 boolean restore_viewport = FALSE;
45 SVGA3dClearFlag flags = 0;
46 struct pipe_framebuffer_state *fb = &svga->curr.framebuffer;
47 unsigned color = 0;
48
49 ret = svga_update_state(svga, SVGA_STATE_HW_CLEAR);
50 if (ret)
51 return ret;
52
53 if ((buffers & PIPE_CLEAR_COLOR) && fb->cbufs[0]) {
54 flags |= SVGA3D_CLEAR_COLOR;
55 util_pack_color(rgba, PIPE_FORMAT_A8R8G8B8_UNORM, &color);
56
57 rect.w = fb->cbufs[0]->width;
58 rect.h = fb->cbufs[0]->height;
59 }
60
61 if ((buffers & PIPE_CLEAR_DEPTHSTENCIL) && fb->zsbuf) {
62 flags |= SVGA3D_CLEAR_DEPTH;
63
64 if (svga->curr.framebuffer.zsbuf->format == PIPE_FORMAT_Z24S8_UNORM)
65 flags |= SVGA3D_CLEAR_STENCIL;
66
67 rect.w = MAX2(rect.w, fb->zsbuf->width);
68 rect.h = MAX2(rect.h, fb->zsbuf->height);
69 }
70
71 if (memcmp(&rect, &svga->state.hw_clear.viewport, sizeof(rect)) != 0) {
72 restore_viewport = TRUE;
73 ret = SVGA3D_SetViewport(svga->swc, &rect);
74 if (ret)
75 return ret;
76 }
77
78 ret = SVGA3D_ClearRect(svga->swc, flags, color, depth, stencil,
79 rect.x, rect.y, rect.w, rect.h);
80 if (ret != PIPE_OK)
81 return ret;
82
83 if (restore_viewport) {
84 memcpy(&rect, &svga->state.hw_clear.viewport, sizeof rect);
85 ret = SVGA3D_SetViewport(svga->swc, &rect);
86 }
87
88 return ret;
89 }
90
91 /**
92 * Clear the given surface to the specified value.
93 * No masking, no scissor (clear entire buffer).
94 */
95 void
96 svga_clear(struct pipe_context *pipe, unsigned buffers, const float *rgba,
97 double depth, unsigned stencil)
98 {
99 struct svga_context *svga = svga_context( pipe );
100 int ret;
101
102 ret = try_clear( svga, buffers, rgba, depth, stencil );
103
104 if (ret == PIPE_ERROR_OUT_OF_MEMORY) {
105 /* Flush command buffer and retry:
106 */
107 svga_context_flush( svga, NULL );
108
109 ret = try_clear( svga, buffers, rgba, depth, stencil );
110 }
111
112 /*
113 * Mark target surfaces as dirty
114 * TODO Mark only cleared surfaces.
115 */
116 svga_mark_surfaces_dirty(svga);
117
118 assert (ret == PIPE_OK);
119 }