summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/i965/brw_pipe_clear.c
blob: 7bf3ea6994a8d694e7a96a592147e98ccb29c631 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**************************************************************************
 * 
 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 **************************************************************************/

#include "util/u_pack_color.h"
#include "util/u_math.h"

#include "pipe/p_state.h"

#include "brw_batchbuffer.h"
#include "brw_screen.h"
#include "brw_context.h"

#define MASK16 0xffff
#define MASK24 0xffffff


/**
 * Use blitting to clear the renderbuffers named by 'flags'.
 * Note: we can't use the ctx->DrawBuffer->_ColorDrawBufferIndexes field
 * since that might include software renderbuffers or renderbuffers
 * which we're clearing with triangles.
 */
static enum pipe_error
try_clear( struct brw_context *brw,
           struct brw_surface *surface,
           unsigned value,
           unsigned rgba_mask)
{
   uint32_t BR13, CMD;
   int x1 = 0;
   int y1 = 0;
   int x2 = surface->base.width;
   int y2 = surface->base.height;
   int pitch = surface->pitch;
   int cpp = surface->cpp;

   if (x2 == 0 || y2 == 0)
      return 0;

   debug_printf("%s dst:buf(%p)/%d+%d %d,%d sz:%dx%d\n",
                __FUNCTION__,
                (void *)surface->bo, pitch * cpp,
                surface->offset,
                x1, y1, x2 - x1, y2 - y1);

   BR13 = 0xf0 << 16;
   CMD = XY_COLOR_BLT_CMD | rgba_mask;

   /* Setup the blit command */
   if (cpp == 4) {
      BR13 |= BR13_8888;
   }
   else {
      assert(cpp == 2);
      BR13 |= BR13_565;
   }

   /* XXX: nasty hack for clearing depth buffers
    */
   if (surface->tiling == BRW_TILING_Y) {
      x2 = pitch;
   }

   if (surface->tiling == BRW_TILING_X) {
      CMD |= XY_DST_TILED;
      pitch /= 4;
   }

   BR13 |= (pitch * cpp);

   BEGIN_BATCH(6, 0);
   OUT_BATCH(CMD);
   OUT_BATCH(BR13);
   OUT_BATCH((y1 << 16) | x1);
   OUT_BATCH((y2 << 16) | x2);
   OUT_RELOC(surface->bo,
             BRW_USAGE_BLIT_DEST,
             surface->offset);
   OUT_BATCH(value);
   ADVANCE_BATCH();

   return 0;
}




static void color_clear(struct brw_context *brw, 
                        struct brw_surface *bsurface,
                        const float *rgba )
{
   enum pipe_error ret;
   union util_color value;

   util_pack_color( rgba, bsurface->base.format, &value );

   if (bsurface->cpp == 2)
      value.ui |= value.ui << 16;

   ret = try_clear( brw, bsurface, value.ui,
                    XY_BLT_WRITE_RGB | XY_BLT_WRITE_ALPHA );

   if (ret != 0) {
      brw_context_flush( brw );
      ret = try_clear( brw, bsurface, value.ui,
                       XY_BLT_WRITE_RGB | XY_BLT_WRITE_ALPHA );
      assert( ret == 0 );
   }
}

static void zstencil_clear(struct brw_context *brw,
                           struct brw_surface *bsurface,
                           unsigned clear_flags,
                           double depth,
                           unsigned stencil )
{
   enum pipe_error ret;
   unsigned value;
   unsigned mask = 0;
   union fi tmp;

   if (clear_flags & PIPE_CLEAR_DEPTH)
      mask |= XY_BLT_WRITE_RGB;

   switch (bsurface->base.format) {
   case PIPE_FORMAT_Z32_FLOAT:
      tmp.f = (float)depth;
      value = tmp.ui;
      break;
   case PIPE_FORMAT_Z24X8_UNORM:
   case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
      value = ((unsigned)(depth * MASK24) & MASK24);
      break;
   case PIPE_FORMAT_Z16_UNORM:
      value = ((unsigned)(depth * MASK16) & MASK16);
      break;
   default:
      assert(0);
      return;
   }

   switch (bsurface->base.format) {
   case PIPE_FORMAT_Z32_FLOAT:
      mask |= XY_BLT_WRITE_ALPHA;
      break;
   case PIPE_FORMAT_Z24X8_UNORM:
      value = value | (stencil << 24);
      mask |= XY_BLT_WRITE_ALPHA;
      break;
   case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
      value = value | (stencil << 24);
      if (clear_flags & PIPE_CLEAR_STENCIL)
         mask |= XY_BLT_WRITE_ALPHA;
      break;
   case PIPE_FORMAT_Z16_UNORM:
      value = value | (value << 16);
      mask |= XY_BLT_WRITE_ALPHA;
      break;
   default:
      break;
   }

   ret = try_clear( brw, bsurface, value, mask );

   if (ret != 0) {
      brw_context_flush( brw );
      ret = try_clear( brw, bsurface, value, mask );
      assert( ret == 0 );
   }
}



/**
 * Clear the given surface to the specified value.
 * No masking, no scissor (clear entire buffer).
 */
static void brw_clear(struct pipe_context *pipe, 
                      unsigned buffers,
                      const float *rgba,
                      double depth,
                      unsigned stencil)
{
   struct brw_context *brw = brw_context( pipe );
   int i;

   if (buffers & PIPE_CLEAR_COLOR) {
      for (i = 0; i < brw->curr.fb.nr_cbufs; i++) {
         color_clear( brw, 
                      brw_surface(brw->curr.fb.cbufs[i]),
                      rgba );
      }
   }

   if (buffers & PIPE_CLEAR_DEPTHSTENCIL) {
      if (brw->curr.fb.zsbuf) {
         zstencil_clear( brw,
                         brw_surface(brw->curr.fb.zsbuf),
                         buffers & PIPE_CLEAR_DEPTHSTENCIL,
                         depth, stencil );
      }
   }
}

/* XXX should respect region */
static void brw_clear_render_target(struct pipe_context *pipe,
                                    struct pipe_surface *dst,
                                    const float *rgba,
                                    unsigned dstx, unsigned dsty,
                                    unsigned width, unsigned height)
{
   struct brw_context *brw = brw_context( pipe );

   color_clear( brw,
                brw_surface(dst),
                rgba );
}

/* XXX should respect region */
static void brw_clear_depth_stencil(struct pipe_context *pipe,
                                    struct pipe_surface *dst,
                                    unsigned clear_flags,
                                    double depth,
                                    unsigned stencil,
                                    unsigned dstx, unsigned dsty,
                                    unsigned width, unsigned height)
{
   struct brw_context *brw = brw_context( pipe );

   zstencil_clear( brw,
                   brw_surface(dst),
                   clear_flags,
                   depth, stencil );
}

void brw_pipe_clear_init( struct brw_context *brw )
{
   brw->base.clear = brw_clear;
   brw->base.clear_render_target = brw_clear_render_target;
   brw->base.clear_depth_stencil = brw_clear_depth_stencil;
}


void brw_pipe_clear_cleanup( struct brw_context *brw )
{
}