summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/dri/sis/sis_clear.c
blob: fb92d06c7355db63087531a0da379d5ec12e4abc (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/**************************************************************************

Copyright 2000 Silicon Integrated Systems Corp, Inc., HsinChu, Taiwan.
Copyright 2003 Eric Anholt
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
on 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
ERIC ANHOLT OR SILICON INTEGRATED SYSTEMS CORP 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.

**************************************************************************/
/* $XFree86: xc/lib/GL/mesa/src/drv/sis/sis_clear.c,v 1.5 2000/09/26 15:56:48 tsi Exp $ */

/*
 * Authors:
 *   Sung-Ching Lin <sclin@sis.com.tw>
 *   Eric Anholt <anholt@FreeBSD.org>
 */

#include "sis_context.h"
#include "sis_state.h"
#include "sis_lock.h"

#include "swrast/swrast.h"
#include "macros.h"

static GLbitfield sis_3D_Clear( GLcontext * ctx, GLbitfield mask,
				GLint x, GLint y, GLint width,
				GLint height );
static void sis_clear_color_buffer( GLcontext *ctx, GLenum mask, GLint x,
				    GLint y, GLint width, GLint height );
static void sis_clear_z_stencil_buffer( GLcontext * ctx,
					GLbitfield mask, GLint x,
					GLint y, GLint width,
					GLint height );

static void
set_color_pattern( sisContextPtr smesa, GLubyte red, GLubyte green,
		   GLubyte blue, GLubyte alpha )
{
   /* XXX only RGB565 and ARGB8888 */
   switch (smesa->colorFormat)
   {
   case DST_FORMAT_ARGB_8888:
      smesa->clearColorPattern = (alpha << 24) +
	 (red << 16) + (green << 8) + (blue);
      break;
   case DST_FORMAT_RGB_565:
      smesa->clearColorPattern = ((red >> 3) << 11) +
	 ((green >> 2) << 5) + (blue >> 3);
      smesa->clearColorPattern |= smesa->clearColorPattern << 16;
      break;
   default:
      sis_fatal_error("Bad dst color format\n");
   }
}

void
sisUpdateZStencilPattern( sisContextPtr smesa, GLclampd z, GLint stencil )
{
   GLuint zPattern;

   switch (smesa->zFormat)
   {
   case SiS_ZFORMAT_Z16:
      CLAMPED_FLOAT_TO_USHORT(zPattern, z);
      zPattern |= zPattern << 16;
      break;
   case SiS_ZFORMAT_S8Z24:
      zPattern = FLOAT_TO_UINT(z) >> 8;
      zPattern |= stencil << 24;
      break;
   case SiS_ZFORMAT_Z32:
      zPattern = FLOAT_TO_UINT(z);
      break;
   default:
      sis_fatal_error("Bad Z format\n");
   }
   smesa->clearZStencilPattern = zPattern;
}

void
sisDDClear( GLcontext * ctx, GLbitfield mask )
{
   sisContextPtr smesa = SIS_CONTEXT(ctx);

   GLint x1, y1, width1, height1;

   /* get region after locking: */
   x1 = ctx->DrawBuffer->_Xmin;
   y1 = ctx->DrawBuffer->_Ymin;
   width1 = ctx->DrawBuffer->_Xmax - x1;
   height1 = ctx->DrawBuffer->_Ymax - y1;
   y1 = Y_FLIP(y1 + height1 - 1);

   /* Mask out any non-existent buffers */
   if (ctx->Visual.depthBits == 0 || !ctx->Depth.Mask)
      mask &= ~BUFFER_BIT_DEPTH;
   if (ctx->Visual.stencilBits == 0)
      mask &= ~BUFFER_BIT_STENCIL;

   LOCK_HARDWARE();

   /* The 3d clear code is use for masked clears because apparently the SiS
    * 300-series can't do write masks for 2d blits.  3d isn't used in general
    * because it's slower, even in the case of clearing multiple buffers.
    */
   /* XXX: Appears to be broken with stencil. */
   if ((smesa->current.hwCapEnable2 & (MASK_AlphaMaskWriteEnable |
      MASK_ColorMaskWriteEnable) &&
      (mask & (BUFFER_BIT_BACK_LEFT | BUFFER_BIT_FRONT_LEFT)) != 0) ||
      ((ctx->Stencil.WriteMask[0] & 0xff) != 0xff && 
       (mask & BUFFER_BIT_STENCIL) != 0) )
   {
      mask = sis_3D_Clear( ctx, mask, x1, y1, width1, height1 );
   }

   if ( mask & BUFFER_BIT_FRONT_LEFT || mask & BUFFER_BIT_BACK_LEFT) {
      sis_clear_color_buffer( ctx, mask, x1, y1, width1, height1 );
      mask &= ~(BUFFER_BIT_FRONT_LEFT | BUFFER_BIT_BACK_LEFT);
   }

   if (mask & (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL)) {
      if (smesa->depth.offset != 0)
         sis_clear_z_stencil_buffer( ctx, mask, x1, y1, width1, height1 );
      mask &= ~(BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
   }

   UNLOCK_HARDWARE();

   if (mask != 0)
      _swrast_Clear( ctx, mask);
}


void
sisDDClearColor( GLcontext * ctx, const GLfloat color[4] )
{
   sisContextPtr smesa = SIS_CONTEXT(ctx);
   GLubyte c[4];

   CLAMPED_FLOAT_TO_UBYTE(c[0], color[0]);
   CLAMPED_FLOAT_TO_UBYTE(c[1], color[1]);
   CLAMPED_FLOAT_TO_UBYTE(c[2], color[2]);
   CLAMPED_FLOAT_TO_UBYTE(c[3], color[3]);

   set_color_pattern( smesa, c[0], c[1], c[2], c[3] );
}

void
sisDDClearDepth( GLcontext * ctx, GLclampd d )
{
   sisContextPtr smesa = SIS_CONTEXT(ctx);

   sisUpdateZStencilPattern( smesa, d, ctx->Stencil.Clear );
}

void
sisDDClearStencil( GLcontext * ctx, GLint s )
{
   sisContextPtr smesa = SIS_CONTEXT(ctx);

   sisUpdateZStencilPattern( smesa, ctx->Depth.Clear, s );
}

static GLbitfield
sis_3D_Clear( GLcontext * ctx, GLbitfield mask,
	      GLint x, GLint y, GLint width, GLint height )
{
   sisContextPtr smesa = SIS_CONTEXT(ctx);

   __GLSiSHardware *current = &smesa->current;

   float left, top, right, bottom, zClearVal;
   GLboolean bClrColor, bClrDepth, bClrStencil;
   GLint dwPrimitiveSet;
   GLint dwEnable1 = 0, dwEnable2 = MASK_ColorMaskWriteEnable;
   GLint dwDepthMask = 0, dwSten1 = 0, dwSten2 = 0;
   GLint dirtyflags = GFLAG_ENABLESETTING | GFLAG_ENABLESETTING2 |
      GFLAG_CLIPPING | GFLAG_DESTSETTING;
   int count;
   drm_clip_rect_t *pExtents;

   bClrColor = (mask & (BUFFER_BIT_BACK_LEFT | BUFFER_BIT_FRONT_LEFT)) != 0;
   bClrDepth = (mask & BUFFER_BIT_DEPTH) != 0;
   bClrStencil = (mask & BUFFER_BIT_STENCIL) != 0;

   if (smesa->GlobalFlag & GFLAG_RENDER_STATES)
      sis_update_render_state( smesa );

   if (bClrStencil) {
      dwSten1 = STENCIL_FORMAT_8 | SiS_STENCIL_ALWAYS |
         ((ctx->Stencil.Clear & 0xff) << 8) | 0xff;
      dwSten2 = SiS_SFAIL_REPLACE | SiS_SPASS_ZFAIL_REPLACE |
         SiS_SPASS_ZPASS_REPLACE;
      dwEnable1 = MASK_ZWriteEnable | MASK_StencilWriteEnable |
	MASK_StencilTestEnable;
      dwEnable2 |= MASK_ZMaskWriteEnable;
      dwDepthMask |= (ctx->Stencil.WriteMask[0] & 0xff) << 24;
   } else if (bClrDepth) {
      dwEnable1 = MASK_ZWriteEnable;
      dwEnable2 |= MASK_ZMaskWriteEnable;
   }

   if (bClrDepth) {
      zClearVal = ctx->Depth.Clear;
      if (ctx->Visual.depthBits != 32)
         dwDepthMask |= 0x00ffffff;
      else
         dwDepthMask = 0xffffffff;
   } else
      zClearVal = 0.0;

   mWait3DCmdQueue(9);
   MMIO(REG_3D_TEnable, dwEnable1);
   MMIO(REG_3D_TEnable2, dwEnable2);
   if (bClrDepth || bClrStencil) {
      MMIO(REG_3D_ZSet, (current->hwZ & ~MASK_ZTestMode) | SiS_Z_COMP_ALWAYS);
      dirtyflags |= GFLAG_ZSETTING;
   }
   if (bClrColor) {
      MMIO(REG_3D_DstSet, (current->hwDstSet & ~MASK_ROP2) | LOP_COPY);
   } else {
      MMIO(REG_3D_DstAlphaWriteMask, 0L);
   }
   if (bClrStencil) {
      MMIO(REG_3D_StencilSet, dwSten1);
      MMIO(REG_3D_StencilSet2, dwSten2);
      dirtyflags |= GFLAG_STENCILSETTING;
   }

   if (mask & BUFFER_BIT_FRONT_LEFT) {
      pExtents = smesa->driDrawable->pClipRects;
      count = smesa->driDrawable->numClipRects;
   } else {
      pExtents = NULL;
      count = 1;
   }

   while(count--) {
      left = x;
      right = x + width;
      top = y;
      bottom = y + height;

      if (pExtents != NULL) {
         GLuint x1, y1, x2, y2;

         x1 = pExtents->x1 - smesa->driDrawable->x;
         y1 = pExtents->y1 - smesa->driDrawable->y;
         x2 = pExtents->x2 - smesa->driDrawable->x - 1;
         y2 = pExtents->y2 - smesa->driDrawable->y - 1;

         left = (left > x1) ? left : x1;
         right = (right > x2) ? x2 : right;
         top = (top > y1) ? top : y1;
         bottom = (bottom > y2) ? y2 : bottom;
         pExtents++;
         if (left > right || top > bottom)
            continue;
      }

      mWait3DCmdQueue(20);

      MMIO(REG_3D_ClipTopBottom, ((GLint)top << 13) | (GLint)bottom);
      MMIO(REG_3D_ClipLeftRight, ((GLint)left << 13) | (GLint)right);

      /* the first triangle */
      dwPrimitiveSet = OP_3D_TRIANGLE_DRAW | OP_3D_FIRE_TSARGBc | 
                        SHADE_FLAT_VertexC;
      MMIO(REG_3D_PrimitiveSet, dwPrimitiveSet);

      MMIO(REG_3D_TSZa, *(GLint *) &zClearVal);
      MMIO(REG_3D_TSXa, *(GLint *) &right);
      MMIO(REG_3D_TSYa, *(GLint *) &top);
      MMIO(REG_3D_TSARGBa, smesa->clearColorPattern);

      MMIO(REG_3D_TSZb, *(GLint *) &zClearVal);
      MMIO(REG_3D_TSXb, *(GLint *) &left);
      MMIO(REG_3D_TSYb, *(GLint *) &top);
      MMIO(REG_3D_TSARGBb, smesa->clearColorPattern);

      MMIO(REG_3D_TSZc, *(GLint *) &zClearVal);
      MMIO(REG_3D_TSXc, *(GLint *) &left);
      MMIO(REG_3D_TSYc, *(GLint *) &bottom);
      MMIO(REG_3D_TSARGBc, smesa->clearColorPattern);

      /* second triangle */
      dwPrimitiveSet = OP_3D_TRIANGLE_DRAW | OP_3D_FIRE_TSARGBb |
                        SHADE_FLAT_VertexB;
      MMIO(REG_3D_PrimitiveSet, dwPrimitiveSet);

      MMIO(REG_3D_TSZb, *(GLint *) &zClearVal);
      MMIO(REG_3D_TSXb, *(GLint *) &right);
      MMIO(REG_3D_TSYb, *(GLint *) &bottom);
      MMIO(REG_3D_TSARGBb, smesa->clearColorPattern);
   }

   mEndPrimitive();

   /* If BUFFER_BIT_FRONT_LEFT is set, we've only cleared the front buffer so far */
   if ((mask & BUFFER_BIT_FRONT_LEFT) != 0 && (mask & BUFFER_BIT_BACK_LEFT) != 0)
      sis_3D_Clear( ctx, BUFFER_BIT_BACK_LEFT, x, y, width, height );

   smesa->GlobalFlag |= dirtyflags;

   return mask & ~(BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL | BUFFER_BIT_BACK_LEFT |
      BUFFER_BIT_FRONT_LEFT);
}

static void
sis_clear_color_buffer( GLcontext *ctx, GLenum mask, GLint x, GLint y,
			GLint width, GLint height )
{
   sisContextPtr smesa = SIS_CONTEXT(ctx);
   int count;
   drm_clip_rect_t *pExtents = NULL;
   GLint xx, yy;
   GLint x0, y0, width0, height0;

   /* Clear back buffer */
   if (mask & BUFFER_BIT_BACK_LEFT) {
      mWait3DCmdQueue (8);
      MMIO(REG_SRC_PITCH, (smesa->bytesPerPixel == 4) ? 
			   BLIT_DEPTH_32 : BLIT_DEPTH_16);
      MMIO(REG_DST_X_Y, (x << 16) | y);
      MMIO(REG_DST_ADDR, smesa->back.offset);
      MMIO(REG_DST_PITCH_HEIGHT, (smesa->virtualY << 16) | smesa->back.pitch);
      MMIO(REG_WIDTH_HEIGHT, (height << 16) | width);
      MMIO(REG_PATFG, smesa->clearColorPattern);
      MMIO(REG_BLIT_CMD, CMD_DIR_X_INC | CMD_DIR_Y_INC | CMD_ROP_PAT);
      MMIO(REG_CommandQueue, -1);
   }
  
   if ((mask & BUFFER_BIT_FRONT_LEFT) == 0)
      return;

   /* Clear front buffer */
   x0 = x;
   y0 = y;
   width0 = width;
   height0 = height;

   pExtents = smesa->driDrawable->pClipRects;
   count = smesa->driDrawable->numClipRects;

   while (count--) {
      GLint x2 = pExtents->x1 - smesa->driDrawable->x;
      GLint y2 = pExtents->y1 - smesa->driDrawable->y;
      GLint xx2 = pExtents->x2 - smesa->driDrawable->x;
      GLint yy2 = pExtents->y2 - smesa->driDrawable->y;

      x = (x0 > x2) ? x0 : x2;
      y = (y0 > y2) ? y0 : y2;
      xx = ((x0 + width0) > (xx2)) ? xx2 : x0 + width0;
      yy = ((y0 + height0) > (yy2)) ? yy2 : y0 + height0;
      width = xx - x;
      height = yy - y;
      pExtents++;

      if (width <= 0 || height <= 0)
	continue;

      mWait3DCmdQueue (8);
      MMIO(REG_SRC_PITCH, (smesa->bytesPerPixel == 4) ? 
			   BLIT_DEPTH_32 : BLIT_DEPTH_16);
      MMIO(REG_DST_X_Y, (x << 16) | y);
      MMIO(REG_DST_ADDR, smesa->front.offset);
      MMIO(REG_DST_PITCH_HEIGHT, (smesa->virtualY << 16) | smesa->front.pitch);
      MMIO(REG_WIDTH_HEIGHT, (height << 16) | width);
      MMIO(REG_PATFG, smesa->clearColorPattern);
      MMIO(REG_BLIT_CMD, CMD_DIR_X_INC | CMD_DIR_Y_INC | CMD_ROP_PAT);
      MMIO(REG_CommandQueue, -1);
   }
}

static void
sis_clear_z_stencil_buffer( GLcontext * ctx, GLbitfield mask,
			    GLint x, GLint y, GLint width, GLint height )
{
   sisContextPtr smesa = SIS_CONTEXT(ctx);
   int cmd;

   mWait3DCmdQueue (8);
   MMIO(REG_SRC_PITCH, (smesa->zFormat == SiS_ZFORMAT_Z16) ?
			BLIT_DEPTH_16 : BLIT_DEPTH_32);
   MMIO(REG_DST_X_Y, (x << 16) | y);
   MMIO(REG_DST_ADDR, smesa->depth.offset);
   MMIO(REG_DST_PITCH_HEIGHT, (smesa->virtualY << 16) | smesa->depth.pitch);
   MMIO(REG_WIDTH_HEIGHT, (height << 16) | width);
   MMIO(REG_PATFG, smesa->clearZStencilPattern);
   MMIO(REG_BLIT_CMD, CMD_DIR_X_INC | CMD_DIR_Y_INC | CMD_ROP_PAT);
   MMIO(REG_CommandQueue, -1);
}