summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/dri/sis/sis_clear.c
blob: d34793d833aaf1bcbe79dc182b53cf900b3206e3 (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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/**************************************************************************

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:
      assert(0);
   }
}

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

   switch (smesa->zFormat)
   {
   case SiS_ZFORMAT_Z16:
      zPattern = FLOAT_TO_USHORT(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:
      assert(0);
   }
   smesa->clearZStencilPattern = zPattern;
}

void
sisDDClear( GLcontext * ctx, GLbitfield mask, GLboolean all,
	    GLint x, GLint y, GLint width, GLint height )
{
  sisContextPtr smesa = SIS_CONTEXT(ctx);

  GLint x1, y1, width1, height1;

   if (all) {
      GLframebuffer *buffer = ctx->DrawBuffer;

      x1 = 0;
      y1 = 0;
      width1 = buffer->Width;
      height1 = buffer->Height;
   } else {
      x1 = x;
      y1 = Y_FLIP(y+height-1);
      width1 = width;            
      height1 = height;
   }
   /* XXX: Scissoring */

   LOCK_HARDWARE();

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

   /* The 3d clear code is use for masked clears because I don't know how to do
    * masked clears with the 2d functions.  3d isn't used in general because
    * it's slower, even in the case of clearing multiple buffers
    */
   if ((smesa->current.hwDstMask != 0xffffffff &&
      (mask & (DD_BACK_LEFT_BIT | DD_FRONT_LEFT_BIT)) != 0) ||
      (ctx->Stencil.WriteMask[0] < 0xff && (mask & DD_STENCIL_BIT) != 0) )
   {
      mask = sis_3D_Clear( ctx, mask, x1, y1, width1, height1 );
   }

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

   if (mask & (DD_DEPTH_BIT | DD_STENCIL_BIT)) {
      if (smesa->depthbuffer != NULL)
         sis_clear_z_stencil_buffer( ctx, mask, x1, y1, width1, height1 );
      mask &= ~(DD_DEPTH_BIT | DD_STENCIL_BIT);
   }

   UNLOCK_HARDWARE();

   if (mask != 0)
      _swrast_Clear( ctx, mask, all, x1, y1, width, height );
}


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 & (DD_BACK_LEFT_BIT | DD_FRONT_LEFT_BIT)) != 0;
   bClrDepth = (mask & DD_DEPTH_BIT) != 0;
   bClrStencil = (mask & DD_STENCIL_BIT) != 0;

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

   if (bClrStencil) {
      dwSten1 = STENCIL_FORMAT_8 | SiS_STENCIL_ALWAYS |
         (ctx->Stencil.Clear << 8) | 0xff;
      dwSten2 = SiS_SFAIL_REPLACE | SiS_SPASS_ZFAIL_REPLACE |
         SiS_SPASS_ZPASS_REPLACE;
      dwEnable1 = MASK_ZWriteEnable | MASK_StencilTestEnable;
      dwEnable2 |= MASK_ZMaskWriteEnable;
      dwDepthMask |= ctx->Stencil.WriteMask[0] << 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 & DD_FRONT_LEFT_BIT) {
      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 DD_FRONT_LEFT_BIT is set, we've only cleared the front buffer so far */
   if ((mask & DD_FRONT_LEFT_BIT) != 0 && (mask & DD_BACK_LEFT_BIT) != 0)
      sis_3D_Clear( ctx, DD_BACK_LEFT_BIT, x, y, width, height );

   smesa->GlobalFlag |= dirtyflags;

   return mask & ~(DD_DEPTH_BIT | DD_STENCIL_BIT | DD_BACK_LEFT_BIT |
      DD_FRONT_LEFT_BIT);
}

static void
sis_bitblt_clear_cmd( sisContextPtr smesa, ENGPACKET * pkt )
{
   GLint *lpdwDest, *lpdwSrc;
   int i;

   lpdwSrc = (GLint *) pkt + 1;
   lpdwDest = (GLint *) (GET_IOBase (smesa) + REG_SRC_ADDR) + 1;

   mWait3DCmdQueue (10);

   *lpdwDest++ = *lpdwSrc++;
   lpdwSrc++;
   lpdwDest++;
   for (i = 3; i < 8; i++) {
      *lpdwDest++ = *lpdwSrc++;
   }

   MMIO(REG_CMD0, *(GLint *) & pkt->stdwCmd);
   MMIO(REG_CommandQueue, -1);
}

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;
   GLuint depth = smesa->bytesPerPixel;
   drm_clip_rect_t *pExtents = NULL;
   GLint xx, yy;
   GLint x0, y0, width0, height0;

   ENGPACKET stEngPacket;

   /* Clear back buffer */
   if (mask & DD_BACK_LEFT_BIT) {
      smesa->cbClearPacket.stdwDestPos.wY = y;
      smesa->cbClearPacket.stdwDestPos.wX = x;
      smesa->cbClearPacket.stdwDim.wWidth = (GLshort) width;
      smesa->cbClearPacket.stdwDim.wHeight = (GLshort) height;
      smesa->cbClearPacket.dwFgRopColor = smesa->clearColorPattern;

      sis_bitblt_clear_cmd( smesa, &smesa->cbClearPacket );
   }
  
   if ((mask & DD_FRONT_LEFT_BIT) == 0)
      return;

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

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

   memset( &stEngPacket, 0, sizeof (ENGPACKET) );

   stEngPacket.dwSrcPitch = (depth == 2) ? 0x80000000 : 0xc0000000;
   stEngPacket.dwDestBaseAddr = smesa->frontOffset;
   stEngPacket.wDestPitch = smesa->frontPitch;
   /* TODO: set maximum value? */
   stEngPacket.wDestHeight = smesa->virtualY;
   stEngPacket.stdwCmd.cRop = 0xf0;
   stEngPacket.dwFgRopColor = smesa->clearColorPattern;

   /* for SGRAM Block Write Enable */
   if (smesa->blockWrite)
      stEngPacket.stdwCmd.cCmd0 = CMD0_PAT_FG_COLOR;
   else
      stEngPacket.stdwCmd.cCmd0 = 0;
   stEngPacket.stdwCmd.cCmd1 = CMD1_DIR_X_INC | CMD1_DIR_Y_INC;

   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;

      stEngPacket.stdwDestPos.wY = y;
      stEngPacket.stdwDestPos.wX = x;
      stEngPacket.stdwDim.wWidth = (GLshort)width;
      stEngPacket.stdwDim.wHeight = (GLshort)height;

      sis_bitblt_clear_cmd( smesa, &stEngPacket );
   }
}

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

   /* TODO: consider alignment of width, height? */
   smesa->zClearPacket.stdwDestPos.wY = y;
   smesa->zClearPacket.stdwDestPos.wX = x;
   smesa->zClearPacket.stdwDim.wWidth = (GLshort) width;
   smesa->zClearPacket.stdwDim.wHeight = (GLshort) height;
   smesa->zClearPacket.dwFgRopColor = smesa->clearZStencilPattern;

   sis_bitblt_clear_cmd( smesa, &smesa->zClearPacket );
}