Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / drivers / i965 / brw_pipe_clear.c
1 /**************************************************************************
2  * 
3  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4  * All Rights Reserved.
5  * 
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:
13  * 
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  * 
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.
25  * 
26  **************************************************************************/
27
28 #include "util/u_pack_color.h"
29 #include "util/u_math.h"
30
31 #include "pipe/p_state.h"
32
33 #include "brw_batchbuffer.h"
34 #include "brw_screen.h"
35 #include "brw_context.h"
36
37 #define MASK16 0xffff
38 #define MASK24 0xffffff
39
40
41 /**
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.
46  */
47 static enum pipe_error
48 try_clear( struct brw_context *brw,
49            struct brw_surface *surface,
50            unsigned value,
51            unsigned rgba_mask)
52 {
53    uint32_t BR13, CMD;
54    int x1 = 0;
55    int y1 = 0;
56    int x2 = surface->base.width;
57    int y2 = surface->base.height;
58    int pitch = surface->pitch;
59    int cpp = surface->cpp;
60
61    if (x2 == 0 || y2 == 0)
62       return 0;
63
64    debug_printf("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n",
65                 __FUNCTION__,
66                 (void *)surface->bo, pitch * cpp,
67                 surface->offset,
68                 x1, y1, x2 - x1, y2 - y1);
69
70    BR13 = 0xf0 << 16;
71    CMD = XY_COLOR_BLT_CMD | rgba_mask;
72
73    /* Setup the blit command */
74    if (cpp == 4) {
75       BR13 |= BR13_8888;
76    }
77    else {
78       assert(cpp == 2);
79       BR13 |= BR13_565;
80    }
81
82    /* XXX: nasty hack for clearing depth buffers
83     */
84    if (surface->tiling == BRW_TILING_Y) {
85       x2 = pitch;
86    }
87
88    if (surface->tiling == BRW_TILING_X) {
89       CMD |= XY_DST_TILED;
90       pitch /= 4;
91    }
92
93    BR13 |= (pitch * cpp);
94
95    BEGIN_BATCH(6, 0);
96    OUT_BATCH(CMD);
97    OUT_BATCH(BR13);
98    OUT_BATCH((y1 << 16) | x1);
99    OUT_BATCH((y2 << 16) | x2);
100    OUT_RELOC(surface->bo,
101              BRW_USAGE_BLIT_DEST,
102              surface->offset);
103    OUT_BATCH(value);
104    ADVANCE_BATCH();
105
106    return 0;
107 }
108
109
110
111
112 static void color_clear(struct brw_context *brw, 
113                         struct brw_surface *bsurface,
114                         const float *rgba )
115 {
116    enum pipe_error ret;
117    union util_color value;
118
119    util_pack_color( rgba, bsurface->base.format, &value );
120
121    if (bsurface->cpp == 2)
122       value.ui |= value.ui << 16;
123
124    ret = try_clear( brw, bsurface, value.ui,
125                     XY_BLT_WRITE_RGB | XY_BLT_WRITE_ALPHA );
126
127    if (ret != 0) {
128       brw_context_flush( brw );
129       ret = try_clear( brw, bsurface, value.ui,
130                        XY_BLT_WRITE_RGB | XY_BLT_WRITE_ALPHA );
131       assert( ret == 0 );
132    }
133 }
134
135 static void zstencil_clear(struct brw_context *brw,
136                            struct brw_surface *bsurface,
137                            unsigned clear_flags,
138                            double depth,
139                            unsigned stencil )
140 {
141    enum pipe_error ret;
142    unsigned value;
143    unsigned mask = 0;
144    union fi tmp;
145
146    if (clear_flags & PIPE_CLEAR_DEPTH)
147       mask |= XY_BLT_WRITE_RGB;
148
149    switch (bsurface->base.format) {
150    case PIPE_FORMAT_Z32_FLOAT:
151       tmp.f = (float)depth;
152       value = tmp.ui;
153       break;
154    case PIPE_FORMAT_Z24X8_UNORM:
155    case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
156       value = ((unsigned)(depth * MASK24) & MASK24);
157       break;
158    case PIPE_FORMAT_Z16_UNORM:
159       value = ((unsigned)(depth * MASK16) & MASK16);
160       break;
161    default:
162       assert(0);
163       return;
164    }
165
166    switch (bsurface->base.format) {
167    case PIPE_FORMAT_Z32_FLOAT:
168       mask |= XY_BLT_WRITE_ALPHA;
169       break;
170    case PIPE_FORMAT_Z24X8_UNORM:
171       value = value | (stencil << 24);
172       mask |= XY_BLT_WRITE_ALPHA;
173       break;
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;
178       break;
179    case PIPE_FORMAT_Z16_UNORM:
180       value = value | (value << 16);
181       mask |= XY_BLT_WRITE_ALPHA;
182       break;
183    default:
184       break;
185    }
186
187    ret = try_clear( brw, bsurface, value, mask );
188
189    if (ret != 0) {
190       brw_context_flush( brw );
191       ret = try_clear( brw, bsurface, value, mask );
192       assert( ret == 0 );
193    }
194 }
195
196
197
198 /**
199  * Clear the given surface to the specified value.
200  * No masking, no scissor (clear entire buffer).
201  */
202 static void brw_clear(struct pipe_context *pipe, 
203                       unsigned buffers,
204                       const float *rgba,
205                       double depth,
206                       unsigned stencil)
207 {
208    struct brw_context *brw = brw_context( pipe );
209    int i;
210
211    if (buffers & PIPE_CLEAR_COLOR) {
212       for (i = 0; i < brw->curr.fb.nr_cbufs; i++) {
213          color_clear( brw, 
214                       brw_surface(brw->curr.fb.cbufs[i]),
215                       rgba );
216       }
217    }
218
219    if (buffers & PIPE_CLEAR_DEPTHSTENCIL) {
220       if (brw->curr.fb.zsbuf) {
221          zstencil_clear( brw,
222                          brw_surface(brw->curr.fb.zsbuf),
223                          buffers & PIPE_CLEAR_DEPTHSTENCIL,
224                          depth, stencil );
225       }
226    }
227 }
228
229 /* XXX should respect region */
230 static void brw_clear_render_target(struct pipe_context *pipe,
231                                     struct pipe_surface *dst,
232                                     const float *rgba,
233                                     unsigned dstx, unsigned dsty,
234                                     unsigned width, unsigned height)
235 {
236    struct brw_context *brw = brw_context( pipe );
237
238    color_clear( brw,
239                 brw_surface(dst),
240                 rgba );
241 }
242
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,
247                                     double depth,
248                                     unsigned stencil,
249                                     unsigned dstx, unsigned dsty,
250                                     unsigned width, unsigned height)
251 {
252    struct brw_context *brw = brw_context( pipe );
253
254    zstencil_clear( brw,
255                    brw_surface(dst),
256                    clear_flags,
257                    depth, stencil );
258 }
259
260 void brw_pipe_clear_init( struct brw_context *brw )
261 {
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;
265 }
266
267
268 void brw_pipe_clear_cleanup( struct brw_context *brw )
269 {
270 }