1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "util/u_pack_color.h"
29 #include "util/u_math.h"
31 #include "pipe/p_state.h"
33 #include "brw_batchbuffer.h"
34 #include "brw_screen.h"
35 #include "brw_context.h"
38 #define MASK24 0xffffff
42 * Use blitting to clear the renderbuffers named by 'flags'.
43 * Note: we can't use the ctx->DrawBuffer->_ColorDrawBufferIndexes field
44 * since that might include software renderbuffers or renderbuffers
45 * which we're clearing with triangles.
47 static enum pipe_error
48 try_clear( struct brw_context
*brw
,
49 struct brw_surface
*surface
,
56 int x2
= surface
->base
.width
;
57 int y2
= surface
->base
.height
;
58 int pitch
= surface
->pitch
;
59 int cpp
= surface
->cpp
;
61 if (x2
== 0 || y2
== 0)
64 debug_printf("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n",
66 (void *)surface
->bo
, pitch
* cpp
,
68 x1
, y1
, x2
- x1
, y2
- y1
);
71 CMD
= XY_COLOR_BLT_CMD
| rgba_mask
;
73 /* Setup the blit command */
82 /* XXX: nasty hack for clearing depth buffers
84 if (surface
->tiling
== BRW_TILING_Y
) {
88 if (surface
->tiling
== BRW_TILING_X
) {
93 BR13
|= (pitch
* cpp
);
98 OUT_BATCH((y1
<< 16) | x1
);
99 OUT_BATCH((y2
<< 16) | x2
);
100 OUT_RELOC(surface
->bo
,
102 surface
->base
.offset
);
112 static void color_clear(struct brw_context
*brw
,
113 struct brw_surface
*bsurface
,
117 union util_color value
;
119 util_pack_color( rgba
, bsurface
->base
.format
, &value
);
121 if (bsurface
->cpp
== 2)
122 value
.ui
|= value
.ui
<< 16;
124 ret
= try_clear( brw
, bsurface
, value
.ui
,
125 XY_BLT_WRITE_RGB
| XY_BLT_WRITE_ALPHA
);
128 brw_context_flush( brw
);
129 ret
= try_clear( brw
, bsurface
, value
.ui
,
130 XY_BLT_WRITE_RGB
| XY_BLT_WRITE_ALPHA
);
135 static void zstencil_clear(struct brw_context
*brw
,
136 struct brw_surface
*bsurface
,
137 unsigned clear_flags
,
146 if (clear_flags
& PIPE_CLEAR_DEPTH
)
147 mask
|= XY_BLT_WRITE_RGB
;
149 switch (bsurface
->base
.format
) {
150 case PIPE_FORMAT_Z32_FLOAT
:
151 tmp
.f
= (float)depth
;
154 case PIPE_FORMAT_Z24X8_UNORM
:
155 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
156 value
= ((unsigned)(depth
* MASK24
) & MASK24
);
158 case PIPE_FORMAT_Z16_UNORM
:
159 value
= ((unsigned)(depth
* MASK16
) & MASK16
);
166 switch (bsurface
->base
.format
) {
167 case PIPE_FORMAT_Z32_FLOAT
:
168 mask
|= XY_BLT_WRITE_ALPHA
;
170 case PIPE_FORMAT_Z24X8_UNORM
:
171 value
= value
| (stencil
<< 24);
172 mask
|= XY_BLT_WRITE_ALPHA
;
174 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
175 value
= value
| (stencil
<< 24);
176 if (clear_flags
& PIPE_CLEAR_STENCIL
)
177 mask
|= XY_BLT_WRITE_ALPHA
;
179 case PIPE_FORMAT_Z16_UNORM
:
180 value
= value
| (value
<< 16);
181 mask
|= XY_BLT_WRITE_ALPHA
;
187 ret
= try_clear( brw
, bsurface
, value
, mask
);
190 brw_context_flush( brw
);
191 ret
= try_clear( brw
, bsurface
, value
, mask
);
199 * Clear the given surface to the specified value.
200 * No masking, no scissor (clear entire buffer).
202 static void brw_clear(struct pipe_context
*pipe
,
208 struct brw_context
*brw
= brw_context( pipe
);
211 if (buffers
& PIPE_CLEAR_COLOR
) {
212 for (i
= 0; i
< brw
->curr
.fb
.nr_cbufs
; i
++) {
214 brw_surface(brw
->curr
.fb
.cbufs
[i
]),
219 if (buffers
& PIPE_CLEAR_DEPTHSTENCIL
) {
220 if (brw
->curr
.fb
.zsbuf
) {
222 brw_surface(brw
->curr
.fb
.zsbuf
),
223 buffers
& PIPE_CLEAR_DEPTHSTENCIL
,
229 /* XXX should respect region */
230 static void brw_clear_render_target(struct pipe_context
*pipe
,
231 struct pipe_surface
*dst
,
233 unsigned dstx
, unsigned dsty
,
234 unsigned width
, unsigned height
)
236 struct brw_context
*brw
= brw_context( pipe
);
243 /* XXX should respect region */
244 static void brw_clear_depth_stencil(struct pipe_context
*pipe
,
245 struct pipe_surface
*dst
,
246 unsigned clear_flags
,
249 unsigned dstx
, unsigned dsty
,
250 unsigned width
, unsigned height
)
252 struct brw_context
*brw
= brw_context( pipe
);
260 void brw_pipe_clear_init( struct brw_context
*brw
)
262 brw
->base
.clear
= brw_clear
;
263 brw
->base
.clear_render_target
= brw_clear_render_target
;
264 brw
->base
.clear_depth_stencil
= brw_clear_depth_stencil
;
268 void brw_pipe_clear_cleanup( struct brw_context
*brw
)