r300: check buffer sizes in non-tcl case + set correct VRAM limits
[mesa.git] / src / mesa / drivers / dri / radeon / radeon_common.c
1 /**************************************************************************
2
3 Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
4
5 The Weather Channel (TM) funded Tungsten Graphics to develop the
6 initial release of the Radeon 8500 driver under the XFree86 license.
7 This notice must be preserved.
8
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice (including the
18 next paragraph) shall be included in all copies or substantial
19 portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
25 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
29 **************************************************************************/
30
31 /*
32 * Authors:
33 * Keith Whitwell <keith@tungstengraphics.com>
34 */
35
36 /*
37 - Scissor implementation
38 - buffer swap/copy ioctls
39 - finish/flush
40 - state emission
41 - cmdbuffer management
42 */
43
44 #include <errno.h>
45 #include "main/glheader.h"
46 #include "main/imports.h"
47 #include "main/context.h"
48 #include "main/api_arrayelt.h"
49 #include "main/enums.h"
50 #include "main/colormac.h"
51 #include "main/light.h"
52 #include "main/framebuffer.h"
53 #include "main/simple_list.h"
54 #include "main/renderbuffer.h"
55 #include "swrast/swrast.h"
56 #include "vbo/vbo.h"
57 #include "tnl/tnl.h"
58 #include "tnl/t_pipeline.h"
59 #include "swrast_setup/swrast_setup.h"
60
61 #include "main/blend.h"
62 #include "main/bufferobj.h"
63 #include "main/buffers.h"
64 #include "main/depth.h"
65 #include "main/shaders.h"
66 #include "main/texstate.h"
67 #include "main/varray.h"
68 #include "glapi/dispatch.h"
69 #include "swrast/swrast.h"
70 #include "main/stencil.h"
71 #include "main/matrix.h"
72 #include "main/attrib.h"
73 #include "main/enable.h"
74 #include "main/viewport.h"
75
76 #include "dri_util.h"
77 #include "vblank.h"
78
79 #include "radeon_common.h"
80 #include "radeon_bocs_wrapper.h"
81 #include "radeon_lock.h"
82 #include "radeon_drm.h"
83 #include "radeon_mipmap_tree.h"
84
85 #define DEBUG_CMDBUF 0
86
87 /* =============================================================
88 * Scissoring
89 */
90
91 static GLboolean intersect_rect(drm_clip_rect_t * out,
92 drm_clip_rect_t * a, drm_clip_rect_t * b)
93 {
94 *out = *a;
95 if (b->x1 > out->x1)
96 out->x1 = b->x1;
97 if (b->y1 > out->y1)
98 out->y1 = b->y1;
99 if (b->x2 < out->x2)
100 out->x2 = b->x2;
101 if (b->y2 < out->y2)
102 out->y2 = b->y2;
103 if (out->x1 >= out->x2)
104 return GL_FALSE;
105 if (out->y1 >= out->y2)
106 return GL_FALSE;
107 return GL_TRUE;
108 }
109
110 void radeonRecalcScissorRects(radeonContextPtr radeon)
111 {
112 drm_clip_rect_t *out;
113 int i;
114
115 /* Grow cliprect store?
116 */
117 if (radeon->state.scissor.numAllocedClipRects < radeon->numClipRects) {
118 while (radeon->state.scissor.numAllocedClipRects <
119 radeon->numClipRects) {
120 radeon->state.scissor.numAllocedClipRects += 1; /* zero case */
121 radeon->state.scissor.numAllocedClipRects *= 2;
122 }
123
124 if (radeon->state.scissor.pClipRects)
125 FREE(radeon->state.scissor.pClipRects);
126
127 radeon->state.scissor.pClipRects =
128 MALLOC(radeon->state.scissor.numAllocedClipRects *
129 sizeof(drm_clip_rect_t));
130
131 if (radeon->state.scissor.pClipRects == NULL) {
132 radeon->state.scissor.numAllocedClipRects = 0;
133 return;
134 }
135 }
136
137 out = radeon->state.scissor.pClipRects;
138 radeon->state.scissor.numClipRects = 0;
139
140 for (i = 0; i < radeon->numClipRects; i++) {
141 if (intersect_rect(out,
142 &radeon->pClipRects[i],
143 &radeon->state.scissor.rect)) {
144 radeon->state.scissor.numClipRects++;
145 out++;
146 }
147 }
148 }
149
150 void radeon_get_cliprects(radeonContextPtr radeon,
151 struct drm_clip_rect **cliprects,
152 unsigned int *num_cliprects,
153 int *x_off, int *y_off)
154 {
155 __DRIdrawablePrivate *dPriv = radeon->dri.drawable;
156 struct radeon_framebuffer *rfb = dPriv->driverPrivate;
157
158 if (radeon->constant_cliprect) {
159 radeon->fboRect.x1 = 0;
160 radeon->fboRect.y1 = 0;
161 radeon->fboRect.x2 = radeon->glCtx->DrawBuffer->Width;
162 radeon->fboRect.y2 = radeon->glCtx->DrawBuffer->Height;
163
164 *cliprects = &radeon->fboRect;
165 *num_cliprects = 1;
166 *x_off = 0;
167 *y_off = 0;
168 } else if (radeon->front_cliprects ||
169 rfb->pf_active || dPriv->numBackClipRects == 0) {
170 *cliprects = dPriv->pClipRects;
171 *num_cliprects = dPriv->numClipRects;
172 *x_off = dPriv->x;
173 *y_off = dPriv->y;
174 } else {
175 *num_cliprects = dPriv->numBackClipRects;
176 *cliprects = dPriv->pBackClipRects;
177 *x_off = dPriv->backX;
178 *y_off = dPriv->backY;
179 }
180 }
181
182 /**
183 * Update cliprects and scissors.
184 */
185 void radeonSetCliprects(radeonContextPtr radeon)
186 {
187 __DRIdrawablePrivate *const drawable = radeon->dri.drawable;
188 __DRIdrawablePrivate *const readable = radeon->dri.readable;
189 struct radeon_framebuffer *const draw_rfb = drawable->driverPrivate;
190 struct radeon_framebuffer *const read_rfb = readable->driverPrivate;
191 int x_off, y_off;
192
193 radeon_get_cliprects(radeon, &radeon->pClipRects,
194 &radeon->numClipRects, &x_off, &y_off);
195
196 if ((draw_rfb->base.Width != drawable->w) ||
197 (draw_rfb->base.Height != drawable->h)) {
198 _mesa_resize_framebuffer(radeon->glCtx, &draw_rfb->base,
199 drawable->w, drawable->h);
200 draw_rfb->base.Initialized = GL_TRUE;
201 }
202
203 if (drawable != readable) {
204 if ((read_rfb->base.Width != readable->w) ||
205 (read_rfb->base.Height != readable->h)) {
206 _mesa_resize_framebuffer(radeon->glCtx, &read_rfb->base,
207 readable->w, readable->h);
208 read_rfb->base.Initialized = GL_TRUE;
209 }
210 }
211
212 if (radeon->state.scissor.enabled)
213 radeonRecalcScissorRects(radeon);
214
215 }
216
217
218
219 void radeonUpdateScissor( GLcontext *ctx )
220 {
221 radeonContextPtr rmesa = RADEON_CONTEXT(ctx);
222
223 if ( rmesa->dri.drawable ) {
224 __DRIdrawablePrivate *dPriv = rmesa->dri.drawable;
225
226 int x = ctx->Scissor.X;
227 int y = dPriv->h - ctx->Scissor.Y - ctx->Scissor.Height;
228 int w = ctx->Scissor.X + ctx->Scissor.Width - 1;
229 int h = dPriv->h - ctx->Scissor.Y - 1;
230
231 rmesa->state.scissor.rect.x1 = x + dPriv->x;
232 rmesa->state.scissor.rect.y1 = y + dPriv->y;
233 rmesa->state.scissor.rect.x2 = w + dPriv->x + 1;
234 rmesa->state.scissor.rect.y2 = h + dPriv->y + 1;
235
236 radeonRecalcScissorRects( rmesa );
237 }
238 }
239
240 /* =============================================================
241 * Scissoring
242 */
243
244 void radeonScissor(GLcontext* ctx, GLint x, GLint y, GLsizei w, GLsizei h)
245 {
246 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
247 if (ctx->Scissor.Enabled) {
248 /* We don't pipeline cliprect changes */
249 radeon_firevertices(radeon);
250 radeonUpdateScissor(ctx);
251 }
252 }
253
254
255 /* ================================================================
256 * SwapBuffers with client-side throttling
257 */
258
259 static uint32_t radeonGetLastFrame(radeonContextPtr radeon)
260 {
261 drm_radeon_getparam_t gp;
262 int ret;
263 uint32_t frame = 0;
264
265 gp.param = RADEON_PARAM_LAST_FRAME;
266 gp.value = (int *)&frame;
267 ret = drmCommandWriteRead(radeon->dri.fd, DRM_RADEON_GETPARAM,
268 &gp, sizeof(gp));
269 if (ret) {
270 fprintf(stderr, "%s: drmRadeonGetParam: %d\n", __FUNCTION__,
271 ret);
272 exit(1);
273 }
274
275 return frame;
276 }
277
278 uint32_t radeonGetAge(radeonContextPtr radeon)
279 {
280 drm_radeon_getparam_t gp;
281 int ret;
282 uint32_t age;
283
284 gp.param = RADEON_PARAM_LAST_CLEAR;
285 gp.value = (int *)&age;
286 ret = drmCommandWriteRead(radeon->dri.fd, DRM_RADEON_GETPARAM,
287 &gp, sizeof(gp));
288 if (ret) {
289 fprintf(stderr, "%s: drmRadeonGetParam: %d\n", __FUNCTION__,
290 ret);
291 exit(1);
292 }
293
294 return age;
295 }
296
297 static void radeonEmitIrqLocked(radeonContextPtr radeon)
298 {
299 drm_radeon_irq_emit_t ie;
300 int ret;
301
302 ie.irq_seq = &radeon->iw.irq_seq;
303 ret = drmCommandWriteRead(radeon->dri.fd, DRM_RADEON_IRQ_EMIT,
304 &ie, sizeof(ie));
305 if (ret) {
306 fprintf(stderr, "%s: drmRadeonIrqEmit: %d\n", __FUNCTION__,
307 ret);
308 exit(1);
309 }
310 }
311
312 static void radeonWaitIrq(radeonContextPtr radeon)
313 {
314 int ret;
315
316 do {
317 ret = drmCommandWrite(radeon->dri.fd, DRM_RADEON_IRQ_WAIT,
318 &radeon->iw, sizeof(radeon->iw));
319 } while (ret && (errno == EINTR || errno == EBUSY));
320
321 if (ret) {
322 fprintf(stderr, "%s: drmRadeonIrqWait: %d\n", __FUNCTION__,
323 ret);
324 exit(1);
325 }
326 }
327
328 static void radeonWaitForFrameCompletion(radeonContextPtr radeon)
329 {
330 drm_radeon_sarea_t *sarea = radeon->sarea;
331
332 if (radeon->do_irqs) {
333 if (radeonGetLastFrame(radeon) < sarea->last_frame) {
334 if (!radeon->irqsEmitted) {
335 while (radeonGetLastFrame(radeon) <
336 sarea->last_frame) ;
337 } else {
338 UNLOCK_HARDWARE(radeon);
339 radeonWaitIrq(radeon);
340 LOCK_HARDWARE(radeon);
341 }
342 radeon->irqsEmitted = 10;
343 }
344
345 if (radeon->irqsEmitted) {
346 radeonEmitIrqLocked(radeon);
347 radeon->irqsEmitted--;
348 }
349 } else {
350 while (radeonGetLastFrame(radeon) < sarea->last_frame) {
351 UNLOCK_HARDWARE(radeon);
352 if (radeon->do_usleeps)
353 DO_USLEEP(1);
354 LOCK_HARDWARE(radeon);
355 }
356 }
357 }
358
359 /* wait for idle */
360 void radeonWaitForIdleLocked(radeonContextPtr radeon)
361 {
362 int ret;
363 int i = 0;
364
365 do {
366 ret = drmCommandNone(radeon->dri.fd, DRM_RADEON_CP_IDLE);
367 if (ret)
368 DO_USLEEP(1);
369 } while (ret && ++i < 100);
370
371 if (ret < 0) {
372 UNLOCK_HARDWARE(radeon);
373 fprintf(stderr, "Error: R300 timed out... exiting\n");
374 exit(-1);
375 }
376 }
377
378 static void radeonWaitForIdle(radeonContextPtr radeon)
379 {
380 LOCK_HARDWARE(radeon);
381 radeonWaitForIdleLocked(radeon);
382 UNLOCK_HARDWARE(radeon);
383 }
384
385 static void radeon_flip_renderbuffers(struct radeon_framebuffer *rfb)
386 {
387 int current_page = rfb->pf_current_page;
388 int next_page = (current_page + 1) % rfb->pf_num_pages;
389 struct gl_renderbuffer *tmp_rb;
390
391 /* Exchange renderbuffers if necessary but make sure their
392 * reference counts are preserved.
393 */
394 if (rfb->color_rb[current_page] &&
395 rfb->base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer !=
396 &rfb->color_rb[current_page]->base) {
397 tmp_rb = NULL;
398 _mesa_reference_renderbuffer(&tmp_rb,
399 rfb->base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
400 tmp_rb = &rfb->color_rb[current_page]->base;
401 _mesa_reference_renderbuffer(&rfb->base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer, tmp_rb);
402 _mesa_reference_renderbuffer(&tmp_rb, NULL);
403 }
404
405 if (rfb->color_rb[next_page] &&
406 rfb->base.Attachment[BUFFER_BACK_LEFT].Renderbuffer !=
407 &rfb->color_rb[next_page]->base) {
408 tmp_rb = NULL;
409 _mesa_reference_renderbuffer(&tmp_rb,
410 rfb->base.Attachment[BUFFER_BACK_LEFT].Renderbuffer);
411 tmp_rb = &rfb->color_rb[next_page]->base;
412 _mesa_reference_renderbuffer(&rfb->base.Attachment[BUFFER_BACK_LEFT].Renderbuffer, tmp_rb);
413 _mesa_reference_renderbuffer(&tmp_rb, NULL);
414 }
415 }
416
417 /* Copy the back color buffer to the front color buffer.
418 */
419 void radeonCopyBuffer( __DRIdrawablePrivate *dPriv,
420 const drm_clip_rect_t *rect)
421 {
422 radeonContextPtr rmesa;
423 struct radeon_framebuffer *rfb;
424 GLint nbox, i, ret;
425
426 assert(dPriv);
427 assert(dPriv->driContextPriv);
428 assert(dPriv->driContextPriv->driverPrivate);
429
430 rmesa = (radeonContextPtr) dPriv->driContextPriv->driverPrivate;
431
432 LOCK_HARDWARE(rmesa);
433
434 rfb = dPriv->driverPrivate;
435
436 if ( RADEON_DEBUG & DEBUG_IOCTL ) {
437 fprintf( stderr, "\n%s( %p )\n\n", __FUNCTION__, (void *) rmesa->glCtx );
438 }
439
440 nbox = dPriv->numClipRects; /* must be in locked region */
441
442 for ( i = 0 ; i < nbox ; ) {
443 GLint nr = MIN2( i + RADEON_NR_SAREA_CLIPRECTS , nbox );
444 drm_clip_rect_t *box = dPriv->pClipRects;
445 drm_clip_rect_t *b = rmesa->sarea->boxes;
446 GLint n = 0;
447
448 for ( ; i < nr ; i++ ) {
449
450 *b = box[i];
451
452 if (rect)
453 {
454 if (rect->x1 > b->x1)
455 b->x1 = rect->x1;
456 if (rect->y1 > b->y1)
457 b->y1 = rect->y1;
458 if (rect->x2 < b->x2)
459 b->x2 = rect->x2;
460 if (rect->y2 < b->y2)
461 b->y2 = rect->y2;
462
463 if (b->x1 >= b->x2 || b->y1 >= b->y2)
464 continue;
465 }
466
467 b++;
468 n++;
469 }
470 rmesa->sarea->nbox = n;
471
472 if (!n)
473 continue;
474
475 ret = drmCommandNone( rmesa->dri.fd, DRM_RADEON_SWAP );
476
477 if ( ret ) {
478 fprintf( stderr, "DRM_RADEON_SWAP_BUFFERS: return = %d\n", ret );
479 UNLOCK_HARDWARE( rmesa );
480 exit( 1 );
481 }
482 }
483
484 UNLOCK_HARDWARE( rmesa );
485 }
486
487 static int radeonScheduleSwap(__DRIdrawablePrivate *dPriv, GLboolean *missed_target)
488 {
489 radeonContextPtr rmesa;
490
491 rmesa = (radeonContextPtr) dPriv->driContextPriv->driverPrivate;
492 radeon_firevertices(rmesa);
493
494 LOCK_HARDWARE( rmesa );
495
496 if (!dPriv->numClipRects) {
497 UNLOCK_HARDWARE(rmesa);
498 usleep(10000); /* throttle invisible client 10ms */
499 return 0;
500 }
501
502 radeonWaitForFrameCompletion(rmesa);
503
504 UNLOCK_HARDWARE(rmesa);
505 driWaitForVBlank(dPriv, missed_target);
506
507 return 0;
508 }
509
510 static GLboolean radeonPageFlip( __DRIdrawablePrivate *dPriv )
511 {
512 radeonContextPtr radeon;
513 GLint ret;
514 __DRIscreenPrivate *psp;
515 struct radeon_renderbuffer *rrb;
516 struct radeon_framebuffer *rfb;
517
518 assert(dPriv);
519 assert(dPriv->driContextPriv);
520 assert(dPriv->driContextPriv->driverPrivate);
521
522 radeon = (radeonContextPtr) dPriv->driContextPriv->driverPrivate;
523 rfb = dPriv->driverPrivate;
524 rrb = (void *)rfb->base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer;
525
526 psp = dPriv->driScreenPriv;
527
528 LOCK_HARDWARE(radeon);
529
530 if ( RADEON_DEBUG & DEBUG_IOCTL ) {
531 fprintf(stderr, "%s: pfCurrentPage: %d %d\n", __FUNCTION__,
532 radeon->sarea->pfCurrentPage, radeon->sarea->pfState);
533 }
534 drm_clip_rect_t *box = dPriv->pClipRects;
535 drm_clip_rect_t *b = radeon->sarea->boxes;
536 b[0] = box[0];
537 radeon->sarea->nbox = 1;
538
539 ret = drmCommandNone( radeon->dri.fd, DRM_RADEON_FLIP );
540
541 UNLOCK_HARDWARE(radeon);
542
543 if ( ret ) {
544 fprintf( stderr, "DRM_RADEON_FLIP: return = %d\n", ret );
545 return GL_FALSE;
546 }
547
548 if (!rfb->pf_active)
549 return GL_FALSE;
550
551 rfb->pf_current_page = radeon->sarea->pfCurrentPage;
552 radeon_flip_renderbuffers(rfb);
553 radeon_draw_buffer(radeon->glCtx, &rfb->base);
554
555 return GL_TRUE;
556 }
557
558
559 /**
560 * Swap front and back buffer.
561 */
562 void radeonSwapBuffers(__DRIdrawablePrivate * dPriv)
563 {
564 int64_t ust;
565 __DRIscreenPrivate *psp;
566
567 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
568 radeonContextPtr radeon;
569 GLcontext *ctx;
570
571 radeon = (radeonContextPtr) dPriv->driContextPriv->driverPrivate;
572 ctx = radeon->glCtx;
573
574 if (ctx->Visual.doubleBufferMode) {
575 GLboolean missed_target;
576 struct radeon_framebuffer *rfb = dPriv->driverPrivate;
577 _mesa_notifySwapBuffers(ctx);/* flush pending rendering comands */
578
579 radeonScheduleSwap(dPriv, &missed_target);
580
581 if (rfb->pf_active) {
582 radeonPageFlip(dPriv);
583 } else {
584 radeonCopyBuffer(dPriv, NULL);
585 }
586
587 psp = dPriv->driScreenPriv;
588
589 rfb->swap_count++;
590 (*psp->systemTime->getUST)( & ust );
591 if ( missed_target ) {
592 rfb->swap_missed_count++;
593 rfb->swap_missed_ust = ust - rfb->swap_ust;
594 }
595
596 rfb->swap_ust = ust;
597 radeon->hw.all_dirty = GL_TRUE;
598 }
599 } else {
600 /* XXX this shouldn't be an error but we can't handle it for now */
601 _mesa_problem(NULL, "%s: drawable has no context!",
602 __FUNCTION__);
603 }
604 }
605
606 void radeonCopySubBuffer(__DRIdrawablePrivate * dPriv,
607 int x, int y, int w, int h )
608 {
609 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
610 radeonContextPtr radeon;
611 GLcontext *ctx;
612
613 radeon = (radeonContextPtr) dPriv->driContextPriv->driverPrivate;
614 ctx = radeon->glCtx;
615
616 if (ctx->Visual.doubleBufferMode) {
617 drm_clip_rect_t rect;
618 rect.x1 = x + dPriv->x;
619 rect.y1 = (dPriv->h - y - h) + dPriv->y;
620 rect.x2 = rect.x1 + w;
621 rect.y2 = rect.y1 + h;
622 _mesa_notifySwapBuffers(ctx); /* flush pending rendering comands */
623 radeonCopyBuffer(dPriv, &rect);
624 }
625 } else {
626 /* XXX this shouldn't be an error but we can't handle it for now */
627 _mesa_problem(NULL, "%s: drawable has no context!",
628 __FUNCTION__);
629 }
630 }
631
632 void radeon_draw_buffer(GLcontext *ctx, struct gl_framebuffer *fb)
633 {
634 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
635 struct radeon_renderbuffer *rrbDepth = NULL, *rrbStencil = NULL,
636 *rrbColor = NULL;
637 uint32_t offset = 0;
638
639
640 if (!fb) {
641 /* this can happen during the initial context initialization */
642 return;
643 }
644
645 /* radeons only handle 1 color draw so far */
646 if (fb->_NumColorDrawBuffers != 1) {
647 radeon->vtbl.fallback(ctx, RADEON_FALLBACK_DRAW_BUFFER, GL_TRUE);
648 return;
649 }
650
651 /* Do this here, note core Mesa, since this function is called from
652 * many places within the driver.
653 */
654 if (ctx->NewState & (_NEW_BUFFERS | _NEW_COLOR | _NEW_PIXEL)) {
655 /* this updates the DrawBuffer->_NumColorDrawBuffers fields, etc */
656 _mesa_update_framebuffer(ctx);
657 /* this updates the DrawBuffer's Width/Height if it's a FBO */
658 _mesa_update_draw_buffer_bounds(ctx);
659 }
660
661 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
662 /* this may occur when we're called by glBindFrameBuffer() during
663 * the process of someone setting up renderbuffers, etc.
664 */
665 /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/
666 return;
667 }
668
669 if (fb->Name)
670 ;/* do something depthy/stencily TODO */
671
672
673 /* none */
674 if (fb->Name == 0) {
675 if (fb->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT) {
676 rrbColor = radeon_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
677 radeon->front_cliprects = GL_TRUE;
678 } else {
679 rrbColor = radeon_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
680 radeon->front_cliprects = GL_FALSE;
681 }
682 } else {
683 /* user FBO in theory */
684 struct radeon_renderbuffer *rrb;
685 rrb = radeon_renderbuffer(fb->_ColorDrawBuffers[0]);
686 if (rrb) {
687 offset = rrb->draw_offset;
688 rrbColor = rrb;
689 }
690 radeon->constant_cliprect = GL_TRUE;
691 }
692
693 if (rrbColor == NULL)
694 radeon->vtbl.fallback(ctx, RADEON_FALLBACK_DRAW_BUFFER, GL_TRUE);
695 else
696 radeon->vtbl.fallback(ctx, RADEON_FALLBACK_DRAW_BUFFER, GL_FALSE);
697
698
699 if (fb->_DepthBuffer && fb->_DepthBuffer->Wrapped) {
700 rrbDepth = radeon_renderbuffer(fb->_DepthBuffer->Wrapped);
701 if (rrbDepth && rrbDepth->bo) {
702 radeon->vtbl.fallback(ctx, RADEON_FALLBACK_DEPTH_BUFFER, GL_FALSE);
703 } else {
704 radeon->vtbl.fallback(ctx, RADEON_FALLBACK_DEPTH_BUFFER, GL_TRUE);
705 }
706 } else {
707 radeon->vtbl.fallback(ctx, RADEON_FALLBACK_DEPTH_BUFFER, GL_FALSE);
708 rrbDepth = NULL;
709 }
710
711 if (fb->_StencilBuffer && fb->_StencilBuffer->Wrapped) {
712 rrbStencil = radeon_renderbuffer(fb->_DepthBuffer->Wrapped);
713 if (rrbStencil && rrbStencil->bo) {
714 radeon->vtbl.fallback(ctx, RADEON_FALLBACK_STENCIL_BUFFER, GL_FALSE);
715 /* need to re-compute stencil hw state */
716 if (!rrbDepth)
717 rrbDepth = rrbStencil;
718 } else {
719 radeon->vtbl.fallback(ctx, RADEON_FALLBACK_STENCIL_BUFFER, GL_TRUE);
720 }
721 } else {
722 radeon->vtbl.fallback(ctx, RADEON_FALLBACK_STENCIL_BUFFER, GL_FALSE);
723 if (ctx->Driver.Enable != NULL)
724 ctx->Driver.Enable(ctx, GL_STENCIL_TEST, ctx->Stencil.Enabled);
725 else
726 ctx->NewState |= _NEW_STENCIL;
727 }
728
729 /* Update culling direction which changes depending on the
730 * orientation of the buffer:
731 */
732 if (ctx->Driver.FrontFace)
733 ctx->Driver.FrontFace(ctx, ctx->Polygon.FrontFace);
734 else
735 ctx->NewState |= _NEW_POLYGON;
736
737 /*
738 * Update depth test state
739 */
740 if (ctx->Driver.Enable) {
741 ctx->Driver.Enable(ctx, GL_DEPTH_TEST,
742 (ctx->Depth.Test && fb->Visual.depthBits > 0));
743 ctx->Driver.Enable(ctx, GL_STENCIL_TEST,
744 (ctx->Stencil._Enabled && fb->Visual.stencilBits > 0));
745 } else {
746 ctx->NewState |= (_NEW_DEPTH | _NEW_STENCIL);
747 }
748
749 radeon->state.depth.rrb = rrbDepth;
750 radeon->state.color.rrb = rrbColor;
751 radeon->state.color.draw_offset = offset;
752
753 #if 0
754 /* update viewport since it depends on window size */
755 if (ctx->Driver.Viewport) {
756 ctx->Driver.Viewport(ctx, ctx->Viewport.X, ctx->Viewport.Y,
757 ctx->Viewport.Width, ctx->Viewport.Height);
758 } else {
759
760 }
761 #endif
762 ctx->NewState |= _NEW_VIEWPORT;
763
764 /* Set state we know depends on drawable parameters:
765 */
766 if (ctx->Driver.Scissor)
767 ctx->Driver.Scissor(ctx, ctx->Scissor.X, ctx->Scissor.Y,
768 ctx->Scissor.Width, ctx->Scissor.Height);
769 radeon->NewGLState |= _NEW_SCISSOR;
770
771 if (ctx->Driver.DepthRange)
772 ctx->Driver.DepthRange(ctx,
773 ctx->Viewport.Near,
774 ctx->Viewport.Far);
775
776 /* Update culling direction which changes depending on the
777 * orientation of the buffer:
778 */
779 if (ctx->Driver.FrontFace)
780 ctx->Driver.FrontFace(ctx, ctx->Polygon.FrontFace);
781 else
782 ctx->NewState |= _NEW_POLYGON;
783 }
784
785 /**
786 * Called via glDrawBuffer.
787 */
788 void radeonDrawBuffer( GLcontext *ctx, GLenum mode )
789 {
790 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
791
792 if (RADEON_DEBUG & DEBUG_DRI)
793 fprintf(stderr, "%s %s\n", __FUNCTION__,
794 _mesa_lookup_enum_by_nr( mode ));
795
796 radeon_firevertices(radeon); /* don't pipeline cliprect changes */
797
798 radeon_draw_buffer(ctx, ctx->DrawBuffer);
799 }
800
801 void radeonReadBuffer( GLcontext *ctx, GLenum mode )
802 {
803 /* nothing, until we implement h/w glRead/CopyPixels or CopyTexImage */
804 if (ctx->ReadBuffer == ctx->DrawBuffer) {
805 /* This will update FBO completeness status.
806 * A framebuffer will be incomplete if the GL_READ_BUFFER setting
807 * refers to a missing renderbuffer. Calling glReadBuffer can set
808 * that straight and can make the drawing buffer complete.
809 */
810 radeon_draw_buffer(ctx, ctx->DrawBuffer);
811 }
812 }
813
814
815 /* Turn on/off page flipping according to the flags in the sarea:
816 */
817 void radeonUpdatePageFlipping(radeonContextPtr radeon)
818 {
819 struct radeon_framebuffer *rfb = radeon->dri.drawable->driverPrivate;
820
821 rfb->pf_active = radeon->sarea->pfState;
822 rfb->pf_current_page = radeon->sarea->pfCurrentPage;
823 rfb->pf_num_pages = 2;
824 radeon_flip_renderbuffers(rfb);
825 radeon_draw_buffer(radeon->glCtx, radeon->glCtx->DrawBuffer);
826 }
827
828 void radeon_window_moved(radeonContextPtr radeon)
829 {
830 if (!radeon->radeonScreen->driScreen->dri2.enabled) {
831 radeonUpdatePageFlipping(radeon);
832 }
833 radeonSetCliprects(radeon);
834 }
835
836 void radeon_viewport(GLcontext *ctx, GLint x, GLint y, GLsizei width, GLsizei height)
837 {
838 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
839 __DRIcontext *driContext = radeon->dri.context;
840 void (*old_viewport)(GLcontext *ctx, GLint x, GLint y,
841 GLsizei w, GLsizei h);
842
843 if (!driContext->driScreenPriv->dri2.enabled)
844 return;
845
846 radeon_update_renderbuffers(driContext, driContext->driDrawablePriv);
847 if (driContext->driDrawablePriv != driContext->driReadablePriv)
848 radeon_update_renderbuffers(driContext, driContext->driReadablePriv);
849
850 old_viewport = ctx->Driver.Viewport;
851 ctx->Driver.Viewport = NULL;
852 radeon->dri.drawable = driContext->driDrawablePriv;
853 radeon_window_moved(radeon);
854 radeon_draw_buffer(ctx, radeon->glCtx->DrawBuffer);
855 ctx->Driver.Viewport = old_viewport;
856
857
858 }
859 static void radeon_print_state_atom(radeonContextPtr radeon, struct radeon_state_atom *state )
860 {
861 int i;
862 int dwords = (*state->check)(radeon->glCtx, state);
863
864 fprintf(stderr, "emit %s %d/%d\n", state->name, state->cmd_size, dwords);
865
866 if (RADEON_DEBUG & DEBUG_VERBOSE)
867 for (i = 0 ; i < dwords; i++)
868 fprintf(stderr, "\t%s[%d]: %x\n", state->name, i, state->cmd[i]);
869
870 }
871
872 static INLINE void radeonEmitAtoms(radeonContextPtr radeon, GLboolean dirty)
873 {
874 BATCH_LOCALS(radeon);
875 struct radeon_state_atom *atom;
876 int dwords;
877
878 if (radeon->vtbl.pre_emit_atoms)
879 radeon->vtbl.pre_emit_atoms(radeon);
880
881 /* Emit actual atoms */
882 foreach(atom, &radeon->hw.atomlist) {
883 if ((atom->dirty || radeon->hw.all_dirty) == dirty) {
884 dwords = (*atom->check) (radeon->glCtx, atom);
885 if (dwords) {
886 if (DEBUG_CMDBUF && RADEON_DEBUG & DEBUG_STATE) {
887 radeon_print_state_atom(radeon, atom);
888 }
889 if (atom->emit) {
890 (*atom->emit)(radeon->glCtx, atom);
891 } else {
892 BEGIN_BATCH_NO_AUTOSTATE(dwords);
893 OUT_BATCH_TABLE(atom->cmd, dwords);
894 END_BATCH();
895 }
896 atom->dirty = GL_FALSE;
897 } else {
898 if (DEBUG_CMDBUF && RADEON_DEBUG & DEBUG_STATE) {
899 fprintf(stderr, " skip state %s\n",
900 atom->name);
901 }
902 }
903 }
904 }
905
906 COMMIT_BATCH();
907 }
908
909 void radeonEmitState(radeonContextPtr radeon)
910 {
911 if (RADEON_DEBUG & (DEBUG_STATE|DEBUG_PRIMS))
912 fprintf(stderr, "%s\n", __FUNCTION__);
913
914 if (radeon->vtbl.pre_emit_state)
915 radeon->vtbl.pre_emit_state(radeon);
916
917 /* this code used to return here but now it emits zbs */
918 if (radeon->cmdbuf.cs->cdw && !radeon->hw.is_dirty && !radeon->hw.all_dirty)
919 return;
920
921 /* To avoid going across the entire set of states multiple times, just check
922 * for enough space for the case of emitting all state, and inline the
923 * radeonAllocCmdBuf code here without all the checks.
924 */
925 rcommonEnsureCmdBufSpace(radeon, radeon->hw.max_state_size, __FUNCTION__);
926
927 if (!radeon->cmdbuf.cs->cdw) {
928 if (RADEON_DEBUG & DEBUG_STATE)
929 fprintf(stderr, "Begin reemit state\n");
930
931 radeonEmitAtoms(radeon, GL_FALSE);
932 }
933
934 if (RADEON_DEBUG & DEBUG_STATE)
935 fprintf(stderr, "Begin dirty state\n");
936
937 radeonEmitAtoms(radeon, GL_TRUE);
938 radeon->hw.is_dirty = GL_FALSE;
939 radeon->hw.all_dirty = GL_FALSE;
940
941 }
942
943
944 void radeonFlush(GLcontext *ctx)
945 {
946 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
947 if (RADEON_DEBUG & DEBUG_IOCTL)
948 fprintf(stderr, "%s %d\n", __FUNCTION__, radeon->cmdbuf.cs->cdw);
949
950 if (radeon->dma.flush)
951 radeon->dma.flush( ctx );
952
953 radeonEmitState(radeon);
954
955 if (radeon->cmdbuf.cs->cdw)
956 rcommonFlushCmdBuf(radeon, __FUNCTION__);
957 }
958
959 /* Make sure all commands have been sent to the hardware and have
960 * completed processing.
961 */
962 void radeonFinish(GLcontext * ctx)
963 {
964 radeonContextPtr radeon = RADEON_CONTEXT(ctx);
965 struct gl_framebuffer *fb = ctx->DrawBuffer;
966 int i;
967
968 radeonFlush(ctx);
969
970 if (radeon->radeonScreen->kernel_mm) {
971 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
972 struct radeon_renderbuffer *rrb;
973 rrb = radeon_renderbuffer(fb->_ColorDrawBuffers[i]);
974 if (rrb && rrb->bo)
975 radeon_bo_wait(rrb->bo);
976 }
977 {
978 struct radeon_renderbuffer *rrb;
979 rrb = radeon_get_depthbuffer(radeon);
980 if (rrb && rrb->bo)
981 radeon_bo_wait(rrb->bo);
982 }
983 } else if (radeon->do_irqs) {
984 LOCK_HARDWARE(radeon);
985 radeonEmitIrqLocked(radeon);
986 UNLOCK_HARDWARE(radeon);
987 radeonWaitIrq(radeon);
988 } else {
989 radeonWaitForIdle(radeon);
990 }
991 }
992
993 /* cmdbuffer */
994 /**
995 * Send the current command buffer via ioctl to the hardware.
996 */
997 int rcommonFlushCmdBufLocked(radeonContextPtr rmesa, const char *caller)
998 {
999 int ret = 0;
1000
1001 if (rmesa->cmdbuf.flushing) {
1002 fprintf(stderr, "Recursive call into r300FlushCmdBufLocked!\n");
1003 exit(-1);
1004 }
1005 rmesa->cmdbuf.flushing = 1;
1006
1007 if (RADEON_DEBUG & DEBUG_IOCTL) {
1008 fprintf(stderr, "%s from %s - %i cliprects\n",
1009 __FUNCTION__, caller, rmesa->numClipRects);
1010 }
1011
1012 if (rmesa->cmdbuf.cs->cdw) {
1013 ret = radeon_cs_emit(rmesa->cmdbuf.cs);
1014 rmesa->hw.all_dirty = GL_TRUE;
1015 }
1016 radeon_cs_erase(rmesa->cmdbuf.cs);
1017 rmesa->cmdbuf.flushing = 0;
1018 return ret;
1019 }
1020
1021 int rcommonFlushCmdBuf(radeonContextPtr rmesa, const char *caller)
1022 {
1023 int ret;
1024
1025 radeonReleaseDmaRegion(rmesa);
1026
1027 LOCK_HARDWARE(rmesa);
1028 ret = rcommonFlushCmdBufLocked(rmesa, caller);
1029 UNLOCK_HARDWARE(rmesa);
1030
1031 if (ret) {
1032 fprintf(stderr, "drmRadeonCmdBuffer: %d\n", ret);
1033 _mesa_exit(ret);
1034 }
1035
1036 return ret;
1037 }
1038
1039 /**
1040 * Make sure that enough space is available in the command buffer
1041 * by flushing if necessary.
1042 *
1043 * \param dwords The number of dwords we need to be free on the command buffer
1044 */
1045 void rcommonEnsureCmdBufSpace(radeonContextPtr rmesa, int dwords, const char *caller)
1046 {
1047 if ((rmesa->cmdbuf.cs->cdw + dwords + 128) > rmesa->cmdbuf.size ||
1048 radeon_cs_need_flush(rmesa->cmdbuf.cs)) {
1049 rcommonFlushCmdBuf(rmesa, caller);
1050 }
1051 }
1052
1053 void rcommonInitCmdBuf(radeonContextPtr rmesa)
1054 {
1055 GLuint size;
1056 /* Initialize command buffer */
1057 size = 256 * driQueryOptioni(&rmesa->optionCache,
1058 "command_buffer_size");
1059 if (size < 2 * rmesa->hw.max_state_size) {
1060 size = 2 * rmesa->hw.max_state_size + 65535;
1061 }
1062 if (size > 64 * 256)
1063 size = 64 * 256;
1064
1065 if (RADEON_DEBUG & (DEBUG_IOCTL | DEBUG_DMA)) {
1066 fprintf(stderr, "sizeof(drm_r300_cmd_header_t)=%zd\n",
1067 sizeof(drm_r300_cmd_header_t));
1068 fprintf(stderr, "sizeof(drm_radeon_cmd_buffer_t)=%zd\n",
1069 sizeof(drm_radeon_cmd_buffer_t));
1070 fprintf(stderr,
1071 "Allocating %d bytes command buffer (max state is %d bytes)\n",
1072 size * 4, rmesa->hw.max_state_size * 4);
1073 }
1074
1075 if (rmesa->radeonScreen->kernel_mm) {
1076 int fd = rmesa->radeonScreen->driScreen->fd;
1077 rmesa->cmdbuf.csm = radeon_cs_manager_gem_ctor(fd);
1078 } else {
1079 rmesa->cmdbuf.csm = radeon_cs_manager_legacy_ctor(rmesa);
1080 }
1081 if (rmesa->cmdbuf.csm == NULL) {
1082 /* FIXME: fatal error */
1083 return;
1084 }
1085 rmesa->cmdbuf.cs = radeon_cs_create(rmesa->cmdbuf.csm, size);
1086 assert(rmesa->cmdbuf.cs != NULL);
1087 rmesa->cmdbuf.size = size;
1088
1089 if (!rmesa->radeonScreen->kernel_mm) {
1090 radeon_cs_set_limit(rmesa->cmdbuf.cs, RADEON_GEM_DOMAIN_VRAM, rmesa->radeonScreen->texSize[0]);
1091 radeon_cs_set_limit(rmesa->cmdbuf.cs, RADEON_GEM_DOMAIN_GTT, rmesa->radeonScreen->gartTextures.size);
1092 } else {
1093 struct drm_radeon_gem_info mminfo;
1094
1095 if (!drmCommandWriteRead(rmesa->dri.fd, DRM_RADEON_GEM_INFO, &mminfo, sizeof(mminfo)))
1096 {
1097 radeon_cs_set_limit(rmesa->cmdbuf.cs, RADEON_GEM_DOMAIN_VRAM, mminfo.vram_visible);
1098 radeon_cs_set_limit(rmesa->cmdbuf.cs, RADEON_GEM_DOMAIN_GTT, mminfo.gart_size);
1099 }
1100 }
1101
1102 }
1103 /**
1104 * Destroy the command buffer
1105 */
1106 void rcommonDestroyCmdBuf(radeonContextPtr rmesa)
1107 {
1108 radeon_cs_destroy(rmesa->cmdbuf.cs);
1109 if (rmesa->radeonScreen->driScreen->dri2.enabled || rmesa->radeonScreen->kernel_mm) {
1110 radeon_cs_manager_gem_dtor(rmesa->cmdbuf.csm);
1111 } else {
1112 radeon_cs_manager_legacy_dtor(rmesa->cmdbuf.csm);
1113 }
1114 }
1115
1116 void rcommonBeginBatch(radeonContextPtr rmesa, int n,
1117 int dostate,
1118 const char *file,
1119 const char *function,
1120 int line)
1121 {
1122 rcommonEnsureCmdBufSpace(rmesa, n, function);
1123 if (!rmesa->cmdbuf.cs->cdw && dostate) {
1124 if (RADEON_DEBUG & DEBUG_IOCTL)
1125 fprintf(stderr, "Reemit state after flush (from %s)\n", function);
1126 radeonEmitState(rmesa);
1127 }
1128 radeon_cs_begin(rmesa->cmdbuf.cs, n, file, function, line);
1129
1130 if (DEBUG_CMDBUF && RADEON_DEBUG & DEBUG_IOCTL)
1131 fprintf(stderr, "BEGIN_BATCH(%d) at %d, from %s:%i\n",
1132 n, rmesa->cmdbuf.cs->cdw, function, line);
1133
1134 }
1135
1136
1137
1138 static void
1139 radeon_meta_set_passthrough_transform(radeonContextPtr radeon)
1140 {
1141 GLcontext *ctx = radeon->glCtx;
1142
1143 radeon->meta.saved_vp_x = ctx->Viewport.X;
1144 radeon->meta.saved_vp_y = ctx->Viewport.Y;
1145 radeon->meta.saved_vp_width = ctx->Viewport.Width;
1146 radeon->meta.saved_vp_height = ctx->Viewport.Height;
1147 radeon->meta.saved_matrix_mode = ctx->Transform.MatrixMode;
1148
1149 _mesa_Viewport(0, 0, ctx->DrawBuffer->Width, ctx->DrawBuffer->Height);
1150
1151 _mesa_MatrixMode(GL_PROJECTION);
1152 _mesa_PushMatrix();
1153 _mesa_LoadIdentity();
1154 _mesa_Ortho(0, ctx->DrawBuffer->Width, 0, ctx->DrawBuffer->Height, 1, -1);
1155
1156 _mesa_MatrixMode(GL_MODELVIEW);
1157 _mesa_PushMatrix();
1158 _mesa_LoadIdentity();
1159 }
1160
1161 static void
1162 radeon_meta_restore_transform(radeonContextPtr radeon)
1163 {
1164 _mesa_MatrixMode(GL_PROJECTION);
1165 _mesa_PopMatrix();
1166 _mesa_MatrixMode(GL_MODELVIEW);
1167 _mesa_PopMatrix();
1168
1169 _mesa_MatrixMode(radeon->meta.saved_matrix_mode);
1170
1171 _mesa_Viewport(radeon->meta.saved_vp_x, radeon->meta.saved_vp_y,
1172 radeon->meta.saved_vp_width, radeon->meta.saved_vp_height);
1173 }
1174
1175
1176 /**
1177 * Perform glClear where mask contains only color, depth, and/or stencil.
1178 *
1179 * The implementation is based on calling into Mesa to set GL state and
1180 * performing normal triangle rendering. The intent of this path is to
1181 * have as generic a path as possible, so that any driver could make use of
1182 * it.
1183 */
1184
1185
1186 void radeon_clear_tris(GLcontext *ctx, GLbitfield mask)
1187 {
1188 radeonContextPtr rmesa = RADEON_CONTEXT(ctx);
1189 GLfloat vertices[4][3];
1190 GLfloat color[4][4];
1191 GLfloat dst_z;
1192 struct gl_framebuffer *fb = ctx->DrawBuffer;
1193 int i;
1194 GLboolean saved_fp_enable = GL_FALSE, saved_vp_enable = GL_FALSE;
1195 GLboolean saved_shader_program = 0;
1196 unsigned int saved_active_texture;
1197
1198 assert((mask & ~(TRI_CLEAR_COLOR_BITS | BUFFER_BIT_DEPTH |
1199 BUFFER_BIT_STENCIL)) == 0);
1200
1201 _mesa_PushAttrib(GL_COLOR_BUFFER_BIT |
1202 GL_CURRENT_BIT |
1203 GL_DEPTH_BUFFER_BIT |
1204 GL_ENABLE_BIT |
1205 GL_STENCIL_BUFFER_BIT |
1206 GL_TRANSFORM_BIT |
1207 GL_CURRENT_BIT);
1208 _mesa_PushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
1209 saved_active_texture = ctx->Texture.CurrentUnit;
1210
1211 /* Disable existing GL state we don't want to apply to a clear. */
1212 _mesa_Disable(GL_ALPHA_TEST);
1213 _mesa_Disable(GL_BLEND);
1214 _mesa_Disable(GL_CULL_FACE);
1215 _mesa_Disable(GL_FOG);
1216 _mesa_Disable(GL_POLYGON_SMOOTH);
1217 _mesa_Disable(GL_POLYGON_STIPPLE);
1218 _mesa_Disable(GL_POLYGON_OFFSET_FILL);
1219 _mesa_Disable(GL_LIGHTING);
1220 _mesa_Disable(GL_CLIP_PLANE0);
1221 _mesa_Disable(GL_CLIP_PLANE1);
1222 _mesa_Disable(GL_CLIP_PLANE2);
1223 _mesa_Disable(GL_CLIP_PLANE3);
1224 _mesa_Disable(GL_CLIP_PLANE4);
1225 _mesa_Disable(GL_CLIP_PLANE5);
1226 if (ctx->Extensions.ARB_fragment_program && ctx->FragmentProgram.Enabled) {
1227 saved_fp_enable = GL_TRUE;
1228 _mesa_Disable(GL_FRAGMENT_PROGRAM_ARB);
1229 }
1230 if (ctx->Extensions.ARB_vertex_program && ctx->VertexProgram.Enabled) {
1231 saved_vp_enable = GL_TRUE;
1232 _mesa_Disable(GL_VERTEX_PROGRAM_ARB);
1233 }
1234 if (ctx->Extensions.ARB_shader_objects && ctx->Shader.CurrentProgram) {
1235 saved_shader_program = ctx->Shader.CurrentProgram->Name;
1236 _mesa_UseProgramObjectARB(0);
1237 }
1238
1239 if (ctx->Texture._EnabledUnits != 0) {
1240 int i;
1241
1242 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
1243 _mesa_ActiveTextureARB(GL_TEXTURE0 + i);
1244 _mesa_Disable(GL_TEXTURE_1D);
1245 _mesa_Disable(GL_TEXTURE_2D);
1246 _mesa_Disable(GL_TEXTURE_3D);
1247 if (ctx->Extensions.ARB_texture_cube_map)
1248 _mesa_Disable(GL_TEXTURE_CUBE_MAP_ARB);
1249 if (ctx->Extensions.NV_texture_rectangle)
1250 _mesa_Disable(GL_TEXTURE_RECTANGLE_NV);
1251 if (ctx->Extensions.MESA_texture_array) {
1252 _mesa_Disable(GL_TEXTURE_1D_ARRAY_EXT);
1253 _mesa_Disable(GL_TEXTURE_2D_ARRAY_EXT);
1254 }
1255 }
1256 }
1257
1258 radeon_meta_set_passthrough_transform(rmesa);
1259
1260 for (i = 0; i < 4; i++) {
1261 color[i][0] = ctx->Color.ClearColor[0];
1262 color[i][1] = ctx->Color.ClearColor[1];
1263 color[i][2] = ctx->Color.ClearColor[2];
1264 color[i][3] = ctx->Color.ClearColor[3];
1265 }
1266
1267 /* convert clear Z from [0,1] to NDC coord in [-1,1] */
1268
1269 dst_z = -1.0 + 2.0 * ctx->Depth.Clear;
1270 /* Prepare the vertices, which are the same regardless of which buffer we're
1271 * drawing to.
1272 */
1273 vertices[0][0] = fb->_Xmin;
1274 vertices[0][1] = fb->_Ymin;
1275 vertices[0][2] = dst_z;
1276 vertices[1][0] = fb->_Xmax;
1277 vertices[1][1] = fb->_Ymin;
1278 vertices[1][2] = dst_z;
1279 vertices[2][0] = fb->_Xmax;
1280 vertices[2][1] = fb->_Ymax;
1281 vertices[2][2] = dst_z;
1282 vertices[3][0] = fb->_Xmin;
1283 vertices[3][1] = fb->_Ymax;
1284 vertices[3][2] = dst_z;
1285
1286 _mesa_ColorPointer(4, GL_FLOAT, 4 * sizeof(GLfloat), &color);
1287 _mesa_VertexPointer(3, GL_FLOAT, 3 * sizeof(GLfloat), &vertices);
1288 _mesa_Enable(GL_COLOR_ARRAY);
1289 _mesa_Enable(GL_VERTEX_ARRAY);
1290
1291 while (mask != 0) {
1292 GLuint this_mask = 0;
1293 GLuint color_bit;
1294
1295 color_bit = _mesa_ffs(mask & TRI_CLEAR_COLOR_BITS);
1296 if (color_bit != 0)
1297 this_mask |= (1 << (color_bit - 1));
1298
1299 /* Clear depth/stencil in the same pass as color. */
1300 this_mask |= (mask & (BUFFER_BIT_DEPTH | BUFFER_BIT_STENCIL));
1301
1302 /* Select the current color buffer and use the color write mask if
1303 * we have one, otherwise don't write any color channels.
1304 */
1305 if (this_mask & BUFFER_BIT_FRONT_LEFT)
1306 _mesa_DrawBuffer(GL_FRONT_LEFT);
1307 else if (this_mask & BUFFER_BIT_BACK_LEFT)
1308 _mesa_DrawBuffer(GL_BACK_LEFT);
1309 else if (color_bit != 0)
1310 _mesa_DrawBuffer(GL_COLOR_ATTACHMENT0 +
1311 (color_bit - BUFFER_COLOR0 - 1));
1312 else
1313 _mesa_ColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
1314
1315 /* Control writing of the depth clear value to depth. */
1316 if (this_mask & BUFFER_BIT_DEPTH) {
1317 _mesa_DepthFunc(GL_ALWAYS);
1318 _mesa_DepthMask(GL_TRUE);
1319 _mesa_Enable(GL_DEPTH_TEST);
1320 } else {
1321 _mesa_Disable(GL_DEPTH_TEST);
1322 _mesa_DepthMask(GL_FALSE);
1323 }
1324
1325 /* Control writing of the stencil clear value to stencil. */
1326 if (this_mask & BUFFER_BIT_STENCIL) {
1327 _mesa_Enable(GL_STENCIL_TEST);
1328 _mesa_StencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
1329 _mesa_StencilFuncSeparate(GL_FRONT, GL_ALWAYS, ctx->Stencil.Clear,
1330 ctx->Stencil.WriteMask[0]);
1331 } else {
1332 _mesa_Disable(GL_STENCIL_TEST);
1333 }
1334
1335 CALL_DrawArrays(ctx->Exec, (GL_TRIANGLE_FAN, 0, 4));
1336
1337 mask &= ~this_mask;
1338 }
1339
1340 radeon_meta_restore_transform(rmesa);
1341
1342 _mesa_ActiveTextureARB(GL_TEXTURE0 + saved_active_texture);
1343 if (saved_fp_enable)
1344 _mesa_Enable(GL_FRAGMENT_PROGRAM_ARB);
1345 if (saved_vp_enable)
1346 _mesa_Enable(GL_VERTEX_PROGRAM_ARB);
1347
1348 if (saved_shader_program)
1349 _mesa_UseProgramObjectARB(saved_shader_program);
1350
1351 _mesa_PopClientAttrib();
1352 _mesa_PopAttrib();
1353 }