summaryrefslogtreecommitdiffstats
path: root/src/mesa/state_tracker/st_cb_clear.c
blob: bd050ca2cdc1f06fb8ff8f098c0ee7eda30c6572 (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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/**************************************************************************
 * 
 * Copyright 2007 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.
 * 
 **************************************************************************/

 /*
  * Authors:
  *   Keith Whitwell <keith@tungstengraphics.com>
  *   Brian Paul
  */

#include "main/glheader.h"
#include "main/macros.h"
#include "shader/prog_instruction.h"
#include "st_atom.h"
#include "st_context.h"
#include "st_cb_clear.h"
#include "st_cb_fbo.h"
#include "st_draw.h"
#include "st_program.h"
#include "st_public.h"

#include "pipe/p_context.h"
#include "pipe/p_state.h"
#include "pipe/p_defines.h"
#include "pipe/p_winsys.h"

#include "pipe/tgsi/mesa/mesa_to_tgsi.h"

#include "vf/vf.h"




static GLuint
color_value(GLuint pipeFormat, const GLfloat color[4])
{
   GLubyte r, g, b, a;

   UNCLAMPED_FLOAT_TO_UBYTE(r, color[0]);
   UNCLAMPED_FLOAT_TO_UBYTE(g, color[1]);
   UNCLAMPED_FLOAT_TO_UBYTE(b, color[2]);
   UNCLAMPED_FLOAT_TO_UBYTE(a, color[3]);

   switch (pipeFormat) {
   case PIPE_FORMAT_U_R8_G8_B8_A8:
      return (r << 24) | (g << 16) | (b << 8) | a;
   case PIPE_FORMAT_U_A8_R8_G8_B8:
      return (a << 24) | (r << 16) | (g << 8) | b;
   case PIPE_FORMAT_U_R5_G6_B5:
      return ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | (b >> 3);
   default:
      return 0;
   }
}
 

static GLuint
depth_value(GLuint pipeFormat, GLfloat value)
{
   GLuint val;
   switch (pipeFormat) {
   case PIPE_FORMAT_U_Z16:
      val = (GLuint) (value * 0xffffff);
      break;
   case PIPE_FORMAT_U_Z32:
      val = (GLuint) (value * 0xffffffff);
      break;
   case PIPE_FORMAT_S8_Z24:
   /*case PIPE_FORMAT_Z24_S8:*/
      val = (GLuint) (value * 0xffffff);
      break;
   default:
      assert(0);
   }
   return val;
}


static GLboolean
is_depth_stencil_format(GLuint pipeFormat)
{
   switch (pipeFormat) {
   case PIPE_FORMAT_S8_Z24:
   /*case PIPE_FORMAT_Z24_S8:*/
      return GL_TRUE;
   default:
      return GL_FALSE;
   }
}



/**
 * Create a simple fragment shader that just passes through the fragment color.
 */
static struct st_fragment_program *
make_color_shader(struct st_context *st)
{
   GLcontext *ctx = st->ctx;
   struct st_fragment_program *stfp;
   struct gl_program *p;
   GLboolean b;

   p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
   if (!p)
      return NULL;

   p->NumInstructions = 2;
   p->Instructions = _mesa_alloc_instructions(2);
   if (!p->Instructions) {
      ctx->Driver.DeleteProgram(ctx, p);
      return NULL;
   }
   _mesa_init_instructions(p->Instructions, 2);
   /* MOV result.color, fragment.color; */
   p->Instructions[0].Opcode = OPCODE_MOV;
   p->Instructions[0].DstReg.File = PROGRAM_OUTPUT;
   p->Instructions[0].DstReg.Index = FRAG_RESULT_COLR;
   p->Instructions[0].SrcReg[0].File = PROGRAM_INPUT;
   p->Instructions[0].SrcReg[0].Index = FRAG_ATTRIB_COL0;
   /* END; */
   p->Instructions[1].Opcode = OPCODE_END;

   p->InputsRead = FRAG_BIT_COL0;
   p->OutputsWritten = (1 << FRAG_RESULT_COLR);

   stfp = (struct st_fragment_program *) p;
   /* compile into tgsi format */
   b = tgsi_mesa_compile_fp_program(&stfp->Base,
                                    stfp->tokens, ST_FP_MAX_TOKENS);
   assert(b);

   return stfp;
}


/**
 * Create a simple vertex shader that just passes through the
 * vertex position and color.
 */
static struct st_vertex_program *
make_vertex_shader(struct st_context *st)
{
   GLcontext *ctx = st->ctx;
   struct st_vertex_program *stvp;
   struct gl_program *p;
   GLboolean b;

   p = ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0);
   if (!p)
      return NULL;

   p->NumInstructions = 3;
   p->Instructions = _mesa_alloc_instructions(3);
   if (!p->Instructions) {
      ctx->Driver.DeleteProgram(ctx, p);
      return NULL;
   }
   _mesa_init_instructions(p->Instructions, 3);
   /* MOV result.pos, vertex.pos; */
   p->Instructions[0].Opcode = OPCODE_MOV;
   p->Instructions[0].DstReg.File = PROGRAM_OUTPUT;
   p->Instructions[0].DstReg.Index = VERT_RESULT_HPOS;
   p->Instructions[0].SrcReg[0].File = PROGRAM_INPUT;
   p->Instructions[0].SrcReg[0].Index = VERT_ATTRIB_POS;
   /* MOV result.color, vertex.color; */
   p->Instructions[1].Opcode = OPCODE_MOV;
   p->Instructions[1].DstReg.File = PROGRAM_OUTPUT;
   p->Instructions[1].DstReg.Index = VERT_RESULT_COL0;
   p->Instructions[1].SrcReg[0].File = PROGRAM_INPUT;
   p->Instructions[1].SrcReg[0].Index = VERT_ATTRIB_COLOR0;
   /* END; */
   p->Instructions[2].Opcode = OPCODE_END;

   p->InputsRead = VERT_BIT_POS | VERT_BIT_COLOR0;
   p->OutputsWritten = ((1 << VERT_RESULT_COL0) |
                        (1 << VERT_RESULT_HPOS));

   stvp = (struct st_vertex_program *) p;
   /* compile into tgsi format */
   b = tgsi_mesa_compile_vp_program(&stvp->Base,
                                    stvp->tokens, ST_FP_MAX_TOKENS);
   assert(b);

   return stvp;
}



/**
 * Draw a screen-aligned quadrilateral.
 * Coords are window coords.
 * XXX need to emit clip coords when using a vertex program!
 */
static void
draw_quad(GLcontext *ctx,
          float x0, float y0, float x1, float y1, GLfloat z,
          const GLfloat color[4])
{
   static const GLuint attribs[2] = {
      0, /* pos */
      3  /* color */
   };
   GLfloat verts[4][2][4]; /* four verts, two attribs, XYZW */
   GLuint i;

   /* positions */
   verts[0][0][0] = x0;
   verts[0][0][1] = y0;

   verts[1][0][0] = x1;
   verts[1][0][1] = y0;

   verts[2][0][0] = x1;
   verts[2][0][1] = y1;

   verts[3][0][0] = x0;
   verts[3][0][1] = y1;

   /* same for all verts: */
   for (i = 0; i < 4; i++) {
      verts[i][0][2] = z;
      verts[i][0][3] = 1.0;
      verts[i][1][0] = color[0];
      verts[i][1][1] = color[1];
      verts[i][1][2] = color[2];
      verts[i][1][3] = color[3];
   }

   st_draw_vertices(ctx, PIPE_PRIM_QUADS, 4, (float *) verts, 2, attribs);
}



/**
 * Do glClear by drawing a quadrilateral.
 * The vertices of the quad will be computed from the
 * ctx->DrawBuffer->_X/Ymin/max fields.
 */
static void
clear_with_quad(GLcontext *ctx,
                GLboolean color, GLboolean depth, GLboolean stencil)
{
   struct st_context *st = ctx->st;
   const GLfloat x0 = ctx->DrawBuffer->_Xmin;
   const GLfloat y0 = ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymax;
   const GLfloat x1 = ctx->DrawBuffer->_Xmax;
   const GLfloat y1 = ctx->DrawBuffer->Height - ctx->DrawBuffer->_Ymin;

   /* alpha state: disabled */
   {
      struct pipe_alpha_test_state alpha_test;
      memset(&alpha_test, 0, sizeof(alpha_test));
      st->pipe->set_alpha_test_state(st->pipe, &alpha_test);
   }

   /* blend state: RGBA masking */
   {
      struct pipe_blend_state blend;
      memset(&blend, 0, sizeof(blend));
      if (color) {
         if (ctx->Color.ColorMask[0])
            blend.colormask |= PIPE_MASK_R;
         if (ctx->Color.ColorMask[1])
            blend.colormask |= PIPE_MASK_G;
         if (ctx->Color.ColorMask[2])
            blend.colormask |= PIPE_MASK_B;
         if (ctx->Color.ColorMask[3])
            blend.colormask |= PIPE_MASK_A;
         if (st->ctx->Color.DitherFlag)
            blend.dither = 1;
      }
      st->pipe->set_blend_state(st->pipe, &blend);
   }

   /* depth state: always pass */
   {
      struct pipe_depth_state depth_test;
      memset(&depth_test, 0, sizeof(depth_test));
      if (depth) {
         depth_test.enabled = 1;
         depth_test.writemask = 1;
         depth_test.func = PIPE_FUNC_ALWAYS;
      }
      st->pipe->set_depth_state(st->pipe, &depth_test);
   }

   /* setup state: nothing */
   {
      struct pipe_setup_state setup;
      memset(&setup, 0, sizeof(setup));
      if (ctx->Scissor.Enabled)
         setup.scissor = 1;
      st->pipe->set_setup_state(st->pipe, &setup);
   }

   /* stencil state: always set to ref value */
   {
      struct pipe_stencil_state stencil_test;
      memset(&stencil_test, 0, sizeof(stencil_test));
      if (stencil) {
         stencil_test.front_enabled = 1;
         stencil_test.front_func = PIPE_FUNC_ALWAYS;
         stencil_test.front_fail_op = PIPE_STENCIL_OP_REPLACE;
         stencil_test.front_zpass_op = PIPE_STENCIL_OP_REPLACE;
         stencil_test.front_zfail_op = PIPE_STENCIL_OP_REPLACE;
         stencil_test.ref_value[0] = ctx->Stencil.Clear;
         stencil_test.value_mask[0] = 0xff;
         stencil_test.write_mask[0] = ctx->Stencil.WriteMask[0] & 0xff;
      }
      st->pipe->set_stencil_state(st->pipe, &stencil_test);
   }

   /* fragment shader state: color pass-through program */
   {
      static struct st_fragment_program *stfp = NULL;
      struct pipe_shader_state fs;
      if (!stfp) {
         stfp = make_color_shader(st);
      }
      memset(&fs, 0, sizeof(fs));
      fs.inputs_read = stfp->Base.Base.InputsRead;
      fs.tokens = &stfp->tokens[0];
      fs.constants = NULL;
      st->pipe->set_fs_state(st->pipe, &fs);
   }

   /* vertex shader state: color/position pass-through */
   {
      static struct st_vertex_program *stvp = NULL;
      struct pipe_shader_state vs;
      if (!stvp) {
         stvp = make_vertex_shader(st);
      }
      memset(&vs, 0, sizeof(vs));
      vs.inputs_read = stvp->Base.Base.InputsRead;
      vs.outputs_written = stvp->Base.Base.OutputsWritten;
      vs.tokens = &stvp->tokens[0];
      vs.constants = NULL;
      st->pipe->set_vs_state(st->pipe, &vs);
   }

   /* draw quad matching scissor rect (XXX verify coord round-off) */
   draw_quad(ctx, x0, y0, x1, y1, ctx->Depth.Clear, ctx->Color.ClearColor);

   /* Restore pipe state */
   st->pipe->set_alpha_test_state(st->pipe, &st->state.alpha_test);
   st->pipe->set_blend_state(st->pipe, &st->state.blend);
   st->pipe->set_depth_state(st->pipe, &st->state.depth);
   st->pipe->set_fs_state(st->pipe, &st->state.fs);
   st->pipe->set_vs_state(st->pipe, &st->state.vs);
   st->pipe->set_setup_state(st->pipe, &st->state.setup);
   st->pipe->set_stencil_state(st->pipe, &st->state.stencil);
   /* OR:
   st_invalidate_state(ctx, _NEW_COLOR | _NEW_DEPTH | _NEW_STENCIL);
   */
}


static void
clear_color_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
{
   struct st_renderbuffer *strb = st_renderbuffer(rb);

   if (ctx->Color.ColorMask[0] &&
       ctx->Color.ColorMask[1] &&
       ctx->Color.ColorMask[2] &&
       ctx->Color.ColorMask[3] &&
       !ctx->Scissor.Enabled)
   {
      /* clear whole buffer w/out masking */
      GLuint clearValue
         = color_value(strb->surface->format, ctx->Color.ClearColor);
      ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
   }
   else {
      /* masking or scissoring */
      clear_with_quad(ctx, GL_TRUE, GL_FALSE, GL_FALSE);
   }
}


static void
clear_accum_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
{
   struct st_renderbuffer *strb = st_renderbuffer(rb);

   if (!ctx->Scissor.Enabled) {
      /* clear whole buffer w/out masking */
      GLuint clearValue
         = color_value(strb->surface->format, ctx->Accum.ClearColor);
      /* Note that clearValue is 32 bits but the accum buffer will
       * typically be 64bpp...
       */
      ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
   }
   else {
      /* scissoring */
      /* XXX point framebuffer.cbufs[0] at the accum buffer */
      clear_with_quad(ctx, GL_TRUE, GL_FALSE, GL_FALSE);
   }
}


static void
clear_depth_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
{
   struct st_renderbuffer *strb = st_renderbuffer(rb);
   const GLboolean isDS = is_depth_stencil_format(strb->surface->format);

   assert(strb->surface->format);

   if (ctx->Scissor.Enabled ||
       (isDS && ctx->DrawBuffer->Visual.stencilBits > 0)) {
      /* scissoring or we have a combined depth/stencil buffer */
      clear_with_quad(ctx, GL_FALSE, GL_TRUE, GL_FALSE);
   }
   else {
      /* simple clear of whole buffer */
      GLuint clearValue = depth_value(strb->surface->format, ctx->Depth.Clear);
      ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
   }
}


static void
clear_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
{
   struct st_renderbuffer *strb = st_renderbuffer(rb);
   const GLboolean isDS = is_depth_stencil_format(strb->surface->format);
   const GLuint stencilMax = (1 << rb->StencilBits) - 1;
   GLboolean maskStencil = ctx->Stencil.WriteMask[0] != stencilMax;

   if (maskStencil ||
       ctx->Scissor.Enabled ||
       (isDS && ctx->DrawBuffer->Visual.depthBits > 0)) {
      /* masking or scissoring or combined depth/stencil buffer */
      clear_with_quad(ctx, GL_FALSE, GL_FALSE, GL_TRUE);
   }
   else {
      /* simple clear of whole buffer */
      GLuint clearValue = ctx->Stencil.Clear;
      ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
   }
}


static void
clear_depth_stencil_buffer(GLcontext *ctx, struct gl_renderbuffer *rb)
{
   struct st_renderbuffer *strb = st_renderbuffer(rb);
   const GLuint stencilMax = 1 << rb->StencilBits;
   GLboolean maskStencil = ctx->Stencil.WriteMask[0] != stencilMax;

   assert(is_depth_stencil_format(strb->surface->format));

   if (!maskStencil && !ctx->Scissor.Enabled) {
      /* clear whole buffer w/out masking */
      GLuint clearValue = depth_value(strb->surface->format, ctx->Depth.Clear);

      switch (strb->surface->format) {
      case PIPE_FORMAT_S8_Z24:
         clearValue |= ctx->Stencil.Clear << 24;
         break;
#if 0
      case PIPE_FORMAT_Z24_S8:
         clearValue = (clearValue << 8) | clearVal;
         break;
#endif
      default:
         assert(0);
      }  

      ctx->st->pipe->clear(ctx->st->pipe, strb->surface, clearValue);
   }
   else {
      /* masking or scissoring */
      clear_with_quad(ctx, GL_FALSE, GL_TRUE, GL_TRUE);
   }
}



/**
 * Called via ctx->Driver.Clear()
 * XXX: doesn't pick up the differences between front/back/left/right
 * clears.  Need to sort that out...
 */
static void st_clear(GLcontext *ctx, GLbitfield mask)
{
   static const GLbitfield BUFFER_BITS_DS
      = (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL);
   struct st_context *st = ctx->st;
   struct gl_renderbuffer *depthRb
      = ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
   struct gl_renderbuffer *stencilRb
      = ctx->DrawBuffer->Attachment[BUFFER_STENCIL].Renderbuffer;

   /* This makes sure the softpipe has the latest scissor, etc values */
   st_validate_state( st );

   /*
    * XXX TO-DO:
    * If we're going to use clear_with_quad() for any reason, use it to
    * clear as many other buffers as possible.
    * As it is now, we sometimes call clear_with_quad() three times to clear
    * color/depth/stencil individually...
    */

   if (mask & BUFFER_BITS_COLOR) {
      GLuint b;
      for (b = 0; b < BUFFER_COUNT; b++) {
         if (BUFFER_BITS_COLOR & mask & (1 << b)) {
            struct gl_renderbuffer *rb
               = ctx->DrawBuffer->Attachment[b].Renderbuffer;
            assert(rb);
            clear_color_buffer(ctx, rb);
         }
      }
   }

   if (mask & BUFFER_BIT_ACCUM) {
      clear_accum_buffer(ctx,
                     ctx->DrawBuffer->Attachment[BUFFER_ACCUM].Renderbuffer);
   }

   if ((mask & BUFFER_BITS_DS) == BUFFER_BITS_DS && depthRb == stencilRb) {
      /* clearing combined depth + stencil */
      clear_depth_stencil_buffer(ctx, depthRb);
   }
   else {
      /* separate depth/stencil clears */
      if (mask & BUFFER_BIT_DEPTH) {
         clear_depth_buffer(ctx, depthRb);
      }
      if (mask & BUFFER_BIT_STENCIL) {
         clear_stencil_buffer(ctx, stencilRb);
      }
   }
}


void st_init_clear_functions(struct dd_function_table *functions)
{
   functions->Clear = st_clear;
}