Merge remote branch 'nouveau/gallium-0.1' into gallium-0.2
[mesa.git] / src / mesa / drivers / dri / intel / intel_buffers.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "intel_screen.h"
29 #include "intel_context.h"
30 #include "intel_blit.h"
31 #include "intel_buffers.h"
32 #include "intel_chipset.h"
33 #include "intel_depthstencil.h"
34 #include "intel_fbo.h"
35 #include "intel_regions.h"
36 #include "intel_batchbuffer.h"
37 #include "intel_reg.h"
38 #include "main/context.h"
39 #include "main/framebuffer.h"
40 #include "swrast/swrast.h"
41 #include "utils.h"
42 #include "drirenderbuffer.h"
43 #include "vblank.h"
44 #include "i915_drm.h"
45
46 /* This block can be removed when libdrm >= 2.3.1 is required */
47
48 #ifndef DRM_IOCTL_I915_FLIP
49
50 #define DRM_VBLANK_FLIP 0x8000000
51
52 typedef struct drm_i915_flip {
53 int pipes;
54 } drm_i915_flip_t;
55
56 #undef DRM_IOCTL_I915_FLIP
57 #define DRM_IOCTL_I915_FLIP DRM_IOW(DRM_COMMAND_BASE + DRM_I915_FLIP, \
58 drm_i915_flip_t)
59
60 #endif
61
62 #define FILE_DEBUG_FLAG DEBUG_BLIT
63
64 /**
65 * XXX move this into a new dri/common/cliprects.c file.
66 */
67 GLboolean
68 intel_intersect_cliprects(drm_clip_rect_t * dst,
69 const drm_clip_rect_t * a,
70 const drm_clip_rect_t * b)
71 {
72 GLint bx = b->x1;
73 GLint by = b->y1;
74 GLint bw = b->x2 - bx;
75 GLint bh = b->y2 - by;
76
77 if (bx < a->x1)
78 bw -= a->x1 - bx, bx = a->x1;
79 if (by < a->y1)
80 bh -= a->y1 - by, by = a->y1;
81 if (bx + bw > a->x2)
82 bw = a->x2 - bx;
83 if (by + bh > a->y2)
84 bh = a->y2 - by;
85 if (bw <= 0)
86 return GL_FALSE;
87 if (bh <= 0)
88 return GL_FALSE;
89
90 dst->x1 = bx;
91 dst->y1 = by;
92 dst->x2 = bx + bw;
93 dst->y2 = by + bh;
94
95 return GL_TRUE;
96 }
97
98 /**
99 * Return pointer to current color drawing region, or NULL.
100 */
101 struct intel_region *
102 intel_drawbuf_region(struct intel_context *intel)
103 {
104 struct intel_renderbuffer *irbColor =
105 intel_renderbuffer(intel->ctx.DrawBuffer->_ColorDrawBuffers[0]);
106 if (irbColor)
107 return irbColor->region;
108 else
109 return NULL;
110 }
111
112 /**
113 * Return pointer to current color reading region, or NULL.
114 */
115 struct intel_region *
116 intel_readbuf_region(struct intel_context *intel)
117 {
118 struct intel_renderbuffer *irb
119 = intel_renderbuffer(intel->ctx.ReadBuffer->_ColorReadBuffer);
120 if (irb)
121 return irb->region;
122 else
123 return NULL;
124 }
125
126
127
128 /**
129 * Update the following fields for rendering to a user-created FBO:
130 * intel->numClipRects
131 * intel->pClipRects
132 * intel->drawX
133 * intel->drawY
134 */
135 static void
136 intelSetRenderbufferClipRects(struct intel_context *intel)
137 {
138 /* If the batch contents require looping over cliprects, flush them before
139 * we go changing which cliprects get referenced when that happens.
140 */
141 if (intel->batch->cliprect_mode == LOOP_CLIPRECTS &&
142 (intel->fboRect.x2 != intel->ctx.DrawBuffer->Width ||
143 intel->fboRect.x2 != intel->ctx.DrawBuffer->Height))
144 intel_batchbuffer_flush(intel->batch);
145
146 assert(intel->ctx.DrawBuffer->Width > 0);
147 assert(intel->ctx.DrawBuffer->Height > 0);
148 intel->fboRect.x1 = 0;
149 intel->fboRect.y1 = 0;
150 intel->fboRect.x2 = intel->ctx.DrawBuffer->Width;
151 intel->fboRect.y2 = intel->ctx.DrawBuffer->Height;
152 intel->numClipRects = 1;
153 intel->pClipRects = &intel->fboRect;
154 intel->drawX = 0;
155 intel->drawY = 0;
156 }
157
158
159 /**
160 * As above, but for rendering to front buffer of a window.
161 * \sa intelSetRenderbufferClipRects
162 */
163 static void
164 intelSetFrontClipRects(struct intel_context *intel)
165 {
166 __DRIdrawablePrivate *dPriv = intel->driDrawable;
167
168 if (!dPriv)
169 return;
170
171 /* If the batch contents require looping over cliprects, flush them before
172 * we go changing which cliprects get referenced when that happens.
173 */
174 if (intel->batch->cliprect_mode == LOOP_CLIPRECTS &&
175 intel->pClipRects != dPriv->pClipRects)
176 intel_batchbuffer_flush(intel->batch);
177 intel->numClipRects = dPriv->numClipRects;
178 intel->pClipRects = dPriv->pClipRects;
179 intel->drawX = dPriv->x;
180 intel->drawY = dPriv->y;
181 }
182
183
184 /**
185 * As above, but for rendering to back buffer of a window.
186 */
187 static void
188 intelSetBackClipRects(struct intel_context *intel)
189 {
190 __DRIdrawablePrivate *dPriv = intel->driDrawable;
191 struct intel_framebuffer *intel_fb;
192
193 if (!dPriv)
194 return;
195
196 intel_fb = dPriv->driverPrivate;
197
198 if (intel_fb->pf_active || dPriv->numBackClipRects == 0) {
199 /* use the front clip rects */
200 if (intel->batch->cliprect_mode == LOOP_CLIPRECTS &&
201 intel->pClipRects != dPriv->pClipRects)
202 intel_batchbuffer_flush(intel->batch);
203
204 intel->numClipRects = dPriv->numClipRects;
205 intel->pClipRects = dPriv->pClipRects;
206 intel->drawX = dPriv->x;
207 intel->drawY = dPriv->y;
208 }
209 else {
210 /* use the back clip rects */
211 if (intel->batch->cliprect_mode == LOOP_CLIPRECTS &&
212 intel->pClipRects != dPriv->pBackClipRects)
213 intel_batchbuffer_flush(intel->batch);
214
215 intel->numClipRects = dPriv->numBackClipRects;
216 intel->pClipRects = dPriv->pBackClipRects;
217 intel->drawX = dPriv->backX;
218 intel->drawY = dPriv->backY;
219 }
220 }
221
222 static void
223 intelUpdatePageFlipping(struct intel_context *intel,
224 GLint areaA, GLint areaB)
225 {
226 __DRIdrawablePrivate *dPriv = intel->driDrawable;
227 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
228 GLboolean pf_active;
229 GLint pf_planes;
230
231 /* Update page flipping info */
232 pf_planes = 0;
233
234 if (areaA > 0)
235 pf_planes |= 1;
236
237 if (areaB > 0)
238 pf_planes |= 2;
239
240 intel_fb->pf_current_page = (intel->sarea->pf_current_page >>
241 (intel_fb->pf_planes & 0x2)) & 0x3;
242
243 intel_fb->pf_num_pages = intel->intelScreen->third.handle ? 3 : 2;
244
245 pf_active = pf_planes && (pf_planes & intel->sarea->pf_active) == pf_planes;
246
247 if (INTEL_DEBUG & DEBUG_LOCK)
248 if (pf_active != intel_fb->pf_active)
249 _mesa_printf("%s - Page flipping %sactive\n", __progname,
250 pf_active ? "" : "in");
251
252 if (pf_active) {
253 /* Sync pages between planes if flipping on both at the same time */
254 if (pf_planes == 0x3 && pf_planes != intel_fb->pf_planes &&
255 (intel->sarea->pf_current_page & 0x3) !=
256 (((intel->sarea->pf_current_page) >> 2) & 0x3)) {
257 drm_i915_flip_t flip;
258
259 if (intel_fb->pf_current_page ==
260 (intel->sarea->pf_current_page & 0x3)) {
261 /* XXX: This is ugly, but emitting two flips 'in a row' can cause
262 * lockups for unknown reasons.
263 */
264 intel->sarea->pf_current_page =
265 intel->sarea->pf_current_page & 0x3;
266 intel->sarea->pf_current_page |=
267 ((intel_fb->pf_current_page + intel_fb->pf_num_pages - 1) %
268 intel_fb->pf_num_pages) << 2;
269
270 flip.pipes = 0x2;
271 } else {
272 intel->sarea->pf_current_page =
273 intel->sarea->pf_current_page & (0x3 << 2);
274 intel->sarea->pf_current_page |=
275 (intel_fb->pf_current_page + intel_fb->pf_num_pages - 1) %
276 intel_fb->pf_num_pages;
277
278 flip.pipes = 0x1;
279 }
280
281 drmCommandWrite(intel->driFd, DRM_I915_FLIP, &flip, sizeof(flip));
282 }
283
284 intel_fb->pf_planes = pf_planes;
285 }
286
287 intel_fb->pf_active = pf_active;
288 intel_flip_renderbuffers(intel_fb);
289 intel_draw_buffer(&intel->ctx, intel->ctx.DrawBuffer);
290 }
291
292 /**
293 * This will be called whenever the currently bound window is moved/resized.
294 * XXX: actually, it seems to NOT be called when the window is only moved (BP).
295 */
296 void
297 intelWindowMoved(struct intel_context *intel)
298 {
299 GLcontext *ctx = &intel->ctx;
300 __DRIdrawablePrivate *dPriv = intel->driDrawable;
301 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
302
303 if (!intel->ctx.DrawBuffer) {
304 /* when would this happen? -BP */
305 intelSetFrontClipRects(intel);
306 }
307 else if (intel->ctx.DrawBuffer->Name != 0) {
308 /* drawing to user-created FBO - do nothing */
309 /* Cliprects would be set from intelDrawBuffer() */
310 }
311 else {
312 /* drawing to a window */
313 switch (intel_fb->Base._ColorDrawBufferIndexes[0]) {
314 case BUFFER_FRONT_LEFT:
315 intelSetFrontClipRects(intel);
316 break;
317 case BUFFER_BACK_LEFT:
318 intelSetBackClipRects(intel);
319 break;
320 default:
321 intelSetFrontClipRects(intel);
322 }
323
324 }
325
326 if (!intel->intelScreen->driScrnPriv->dri2.enabled &&
327 intel->intelScreen->driScrnPriv->ddx_version.minor >= 7) {
328 volatile struct drm_i915_sarea *sarea = intel->sarea;
329 drm_clip_rect_t drw_rect = { .x1 = dPriv->x, .x2 = dPriv->x + dPriv->w,
330 .y1 = dPriv->y, .y2 = dPriv->y + dPriv->h };
331 drm_clip_rect_t planeA_rect = { .x1 = sarea->planeA_x, .y1 = sarea->planeA_y,
332 .x2 = sarea->planeA_x + sarea->planeA_w,
333 .y2 = sarea->planeA_y + sarea->planeA_h };
334 drm_clip_rect_t planeB_rect = { .x1 = sarea->planeB_x, .y1 = sarea->planeB_y,
335 .x2 = sarea->planeB_x + sarea->planeB_w,
336 .y2 = sarea->planeB_y + sarea->planeB_h };
337 GLint areaA = driIntersectArea( drw_rect, planeA_rect );
338 GLint areaB = driIntersectArea( drw_rect, planeB_rect );
339 GLuint flags = dPriv->vblFlags;
340
341 intelUpdatePageFlipping(intel, areaA, areaB);
342
343 /* Update vblank info
344 */
345 if (areaB > areaA || (areaA == areaB && areaB > 0)) {
346 flags = dPriv->vblFlags | VBLANK_FLAG_SECONDARY;
347 } else {
348 flags = dPriv->vblFlags & ~VBLANK_FLAG_SECONDARY;
349 }
350
351 /* Check to see if we changed pipes */
352 if (flags != dPriv->vblFlags && dPriv->vblFlags &&
353 !(dPriv->vblFlags & VBLANK_FLAG_NO_IRQ)) {
354 int64_t count;
355 drmVBlank vbl;
356 int i;
357
358 /*
359 * Deal with page flipping
360 */
361 vbl.request.type = DRM_VBLANK_ABSOLUTE;
362
363 if ( dPriv->vblFlags & VBLANK_FLAG_SECONDARY ) {
364 vbl.request.type |= DRM_VBLANK_SECONDARY;
365 }
366
367 for (i = 0; i < intel_fb->pf_num_pages; i++) {
368 if (!intel_fb->color_rb[i] ||
369 (intel_fb->vbl_waited - intel_fb->color_rb[i]->vbl_pending) <=
370 (1<<23))
371 continue;
372
373 vbl.request.sequence = intel_fb->color_rb[i]->vbl_pending;
374 drmWaitVBlank(intel->driFd, &vbl);
375 }
376
377 /*
378 * Update msc_base from old pipe
379 */
380 driDrawableGetMSC32(dPriv->driScreenPriv, dPriv, &count);
381 dPriv->msc_base = count;
382 /*
383 * Then get new vblank_base and vblSeq values
384 */
385 dPriv->vblFlags = flags;
386 driGetCurrentVBlank(dPriv);
387 dPriv->vblank_base = dPriv->vblSeq;
388
389 intel_fb->vbl_waited = dPriv->vblSeq;
390
391 for (i = 0; i < intel_fb->pf_num_pages; i++) {
392 if (intel_fb->color_rb[i])
393 intel_fb->color_rb[i]->vbl_pending = intel_fb->vbl_waited;
394 }
395 }
396 } else {
397 dPriv->vblFlags &= ~VBLANK_FLAG_SECONDARY;
398 }
399
400 /* Update Mesa's notion of window size */
401 driUpdateFramebufferSize(ctx, dPriv);
402 intel_fb->Base.Initialized = GL_TRUE; /* XXX remove someday */
403
404 /* Update hardware scissor */
405 if (ctx->Driver.Scissor != NULL) {
406 ctx->Driver.Scissor(ctx, ctx->Scissor.X, ctx->Scissor.Y,
407 ctx->Scissor.Width, ctx->Scissor.Height);
408 }
409
410 /* Re-calculate viewport related state */
411 if (ctx->Driver.DepthRange != NULL)
412 ctx->Driver.DepthRange( ctx, ctx->Viewport.Near, ctx->Viewport.Far );
413 }
414
415
416
417 /* A true meta version of this would be very simple and additionally
418 * machine independent. Maybe we'll get there one day.
419 */
420 static void
421 intelClearWithTris(struct intel_context *intel, GLbitfield mask)
422 {
423 GLcontext *ctx = &intel->ctx;
424 struct gl_framebuffer *fb = ctx->DrawBuffer;
425 GLuint buf;
426
427 intel->vtbl.install_meta_state(intel);
428
429 /* Back and stencil cliprects are the same. Try and do both
430 * buffers at once:
431 */
432 if (mask & (BUFFER_BIT_BACK_LEFT | BUFFER_BIT_STENCIL | BUFFER_BIT_DEPTH)) {
433 struct intel_region *backRegion =
434 intel_get_rb_region(fb, BUFFER_BACK_LEFT);
435 struct intel_region *depthRegion =
436 intel_get_rb_region(fb, BUFFER_DEPTH);
437
438 intel->vtbl.meta_draw_region(intel, backRegion, depthRegion);
439
440 if (mask & BUFFER_BIT_BACK_LEFT)
441 intel->vtbl.meta_color_mask(intel, GL_TRUE);
442 else
443 intel->vtbl.meta_color_mask(intel, GL_FALSE);
444
445 if (mask & BUFFER_BIT_STENCIL)
446 intel->vtbl.meta_stencil_replace(intel,
447 intel->ctx.Stencil.WriteMask[0],
448 intel->ctx.Stencil.Clear);
449 else
450 intel->vtbl.meta_no_stencil_write(intel);
451
452 if (mask & BUFFER_BIT_DEPTH)
453 intel->vtbl.meta_depth_replace(intel);
454 else
455 intel->vtbl.meta_no_depth_write(intel);
456
457 intel->vtbl.meta_draw_quad(intel,
458 fb->_Xmin,
459 fb->_Xmax,
460 fb->_Ymin,
461 fb->_Ymax,
462 intel->ctx.Depth.Clear,
463 intel->ClearColor8888,
464 0, 0, 0, 0); /* texcoords */
465
466 mask &= ~(BUFFER_BIT_BACK_LEFT | BUFFER_BIT_STENCIL | BUFFER_BIT_DEPTH);
467 }
468
469 /* clear the remaining (color) renderbuffers */
470 for (buf = 0; buf < BUFFER_COUNT && mask; buf++) {
471 const GLuint bufBit = 1 << buf;
472 if (mask & bufBit) {
473 struct intel_renderbuffer *irbColor =
474 intel_renderbuffer(fb->Attachment[buf].Renderbuffer);
475
476 ASSERT(irbColor);
477
478 intel->vtbl.meta_no_depth_write(intel);
479 intel->vtbl.meta_no_stencil_write(intel);
480 intel->vtbl.meta_color_mask(intel, GL_TRUE);
481 intel->vtbl.meta_draw_region(intel, irbColor->region, NULL);
482
483 intel->vtbl.meta_draw_quad(intel,
484 fb->_Xmin,
485 fb->_Xmax,
486 fb->_Ymin,
487 fb->_Ymax,
488 0, intel->ClearColor8888,
489 0, 0, 0, 0); /* texcoords */
490
491 mask &= ~bufBit;
492 }
493 }
494
495 intel->vtbl.leave_meta_state(intel);
496 }
497
498 static const char *buffer_names[] = {
499 [BUFFER_FRONT_LEFT] = "front",
500 [BUFFER_BACK_LEFT] = "back",
501 [BUFFER_FRONT_RIGHT] = "front right",
502 [BUFFER_BACK_RIGHT] = "back right",
503 [BUFFER_AUX0] = "aux0",
504 [BUFFER_AUX1] = "aux1",
505 [BUFFER_AUX2] = "aux2",
506 [BUFFER_AUX3] = "aux3",
507 [BUFFER_DEPTH] = "depth",
508 [BUFFER_STENCIL] = "stencil",
509 [BUFFER_ACCUM] = "accum",
510 [BUFFER_COLOR0] = "color0",
511 [BUFFER_COLOR1] = "color1",
512 [BUFFER_COLOR2] = "color2",
513 [BUFFER_COLOR3] = "color3",
514 [BUFFER_COLOR4] = "color4",
515 [BUFFER_COLOR5] = "color5",
516 [BUFFER_COLOR6] = "color6",
517 [BUFFER_COLOR7] = "color7",
518 };
519
520 /**
521 * Called by ctx->Driver.Clear.
522 */
523 static void
524 intelClear(GLcontext *ctx, GLbitfield mask)
525 {
526 struct intel_context *intel = intel_context(ctx);
527 const GLuint colorMask = *((GLuint *) & ctx->Color.ColorMask);
528 GLbitfield tri_mask = 0;
529 GLbitfield blit_mask = 0;
530 GLbitfield swrast_mask = 0;
531 struct gl_framebuffer *fb = ctx->DrawBuffer;
532 GLuint i;
533
534 if (0)
535 fprintf(stderr, "%s\n", __FUNCTION__);
536
537 /* HW color buffers (front, back, aux, generic FBO, etc) */
538 if (colorMask == ~0) {
539 /* clear all R,G,B,A */
540 /* XXX FBO: need to check if colorbuffers are software RBOs! */
541 blit_mask |= (mask & BUFFER_BITS_COLOR);
542 }
543 else {
544 /* glColorMask in effect */
545 tri_mask |= (mask & BUFFER_BITS_COLOR);
546 }
547
548 /* HW stencil */
549 if (mask & BUFFER_BIT_STENCIL) {
550 const struct intel_region *stencilRegion
551 = intel_get_rb_region(fb, BUFFER_STENCIL);
552 if (stencilRegion) {
553 /* have hw stencil */
554 if (IS_965(intel->intelScreen->deviceID) ||
555 (ctx->Stencil.WriteMask[0] & 0xff) != 0xff) {
556 /* We have to use the 3D engine if we're clearing a partial mask
557 * of the stencil buffer, or if we're on a 965 which has a tiled
558 * depth/stencil buffer in a layout we can't blit to.
559 */
560 tri_mask |= BUFFER_BIT_STENCIL;
561 }
562 else {
563 /* clearing all stencil bits, use blitting */
564 blit_mask |= BUFFER_BIT_STENCIL;
565 }
566 }
567 }
568
569 /* HW depth */
570 if (mask & BUFFER_BIT_DEPTH) {
571 /* clear depth with whatever method is used for stencil (see above) */
572 if (IS_965(intel->intelScreen->deviceID) ||
573 tri_mask & BUFFER_BIT_STENCIL)
574 tri_mask |= BUFFER_BIT_DEPTH;
575 else
576 blit_mask |= BUFFER_BIT_DEPTH;
577 }
578
579 /* SW fallback clearing */
580 swrast_mask = mask & ~tri_mask & ~blit_mask;
581
582 for (i = 0; i < BUFFER_COUNT; i++) {
583 GLuint bufBit = 1 << i;
584 if ((blit_mask | tri_mask) & bufBit) {
585 if (!fb->Attachment[i].Renderbuffer->ClassID) {
586 blit_mask &= ~bufBit;
587 tri_mask &= ~bufBit;
588 swrast_mask |= bufBit;
589 }
590 }
591 }
592
593 if (blit_mask) {
594 if (INTEL_DEBUG & DEBUG_BLIT) {
595 DBG("blit clear:");
596 for (i = 0; i < BUFFER_COUNT; i++) {
597 if (blit_mask & (1 << i))
598 DBG(" %s", buffer_names[i]);
599 }
600 DBG("\n");
601 }
602 intelClearWithBlit(ctx, blit_mask);
603 }
604
605 if (tri_mask) {
606 if (INTEL_DEBUG & DEBUG_BLIT) {
607 DBG("tri clear:");
608 for (i = 0; i < BUFFER_COUNT; i++) {
609 if (tri_mask & (1 << i))
610 DBG(" %s", buffer_names[i]);
611 }
612 DBG("\n");
613 }
614 intelClearWithTris(intel, tri_mask);
615 }
616
617 if (swrast_mask) {
618 if (INTEL_DEBUG & DEBUG_BLIT) {
619 DBG("swrast clear:");
620 for (i = 0; i < BUFFER_COUNT; i++) {
621 if (swrast_mask & (1 << i))
622 DBG(" %s", buffer_names[i]);
623 }
624 DBG("\n");
625 }
626 _swrast_Clear(ctx, swrast_mask);
627 }
628 }
629
630
631 /* Emit wait for pending flips */
632 void
633 intel_wait_flips(struct intel_context *intel)
634 {
635 struct intel_framebuffer *intel_fb =
636 (struct intel_framebuffer *) intel->ctx.DrawBuffer;
637 struct intel_renderbuffer *intel_rb =
638 intel_get_renderbuffer(&intel_fb->Base,
639 intel_fb->Base._ColorDrawBufferIndexes[0] ==
640 BUFFER_FRONT_LEFT ? BUFFER_FRONT_LEFT :
641 BUFFER_BACK_LEFT);
642
643 if (intel->intelScreen->driScrnPriv->dri2.enabled)
644 return;
645
646 if (intel_fb->Base.Name == 0 && intel_rb &&
647 intel_rb->pf_pending == intel_fb->pf_seq) {
648 GLint pf_planes = intel_fb->pf_planes;
649 BATCH_LOCALS;
650
651 /* Wait for pending flips to take effect */
652 BEGIN_BATCH(2, NO_LOOP_CLIPRECTS);
653 OUT_BATCH(pf_planes & 0x1 ? (MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_A_FLIP)
654 : 0);
655 OUT_BATCH(pf_planes & 0x2 ? (MI_WAIT_FOR_EVENT | MI_WAIT_FOR_PLANE_B_FLIP)
656 : 0);
657 ADVANCE_BATCH();
658
659 intel_rb->pf_pending--;
660 }
661 }
662
663
664 /* Flip the front & back buffers
665 */
666 static GLboolean
667 intelPageFlip(const __DRIdrawablePrivate * dPriv)
668 {
669 struct intel_context *intel;
670 int ret;
671 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
672
673 if (INTEL_DEBUG & DEBUG_IOCTL)
674 fprintf(stderr, "%s\n", __FUNCTION__);
675
676 assert(dPriv);
677 assert(dPriv->driContextPriv);
678 assert(dPriv->driContextPriv->driverPrivate);
679
680 intel = (struct intel_context *) dPriv->driContextPriv->driverPrivate;
681
682 if (intel->intelScreen->drmMinor < 9)
683 return GL_FALSE;
684
685 intelFlush(&intel->ctx);
686
687 ret = 0;
688
689 LOCK_HARDWARE(intel);
690
691 if (dPriv->numClipRects && intel_fb->pf_active) {
692 drm_i915_flip_t flip;
693
694 flip.pipes = intel_fb->pf_planes;
695
696 ret = drmCommandWrite(intel->driFd, DRM_I915_FLIP, &flip, sizeof(flip));
697 }
698
699 UNLOCK_HARDWARE(intel);
700
701 if (ret || !intel_fb->pf_active)
702 return GL_FALSE;
703
704 if (!dPriv->numClipRects) {
705 usleep(10000); /* throttle invisible client 10ms */
706 }
707
708 intel_fb->pf_current_page = (intel->sarea->pf_current_page >>
709 (intel_fb->pf_planes & 0x2)) & 0x3;
710
711 if (dPriv->numClipRects != 0) {
712 intel_get_renderbuffer(&intel_fb->Base, BUFFER_FRONT_LEFT)->pf_pending =
713 intel_get_renderbuffer(&intel_fb->Base, BUFFER_BACK_LEFT)->pf_pending =
714 ++intel_fb->pf_seq;
715 }
716
717 intel_flip_renderbuffers(intel_fb);
718 intel_draw_buffer(&intel->ctx, &intel_fb->Base);
719
720 return GL_TRUE;
721 }
722
723 static GLboolean
724 intelScheduleSwap(__DRIdrawablePrivate * dPriv, GLboolean *missed_target)
725 {
726 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
727 unsigned int interval;
728 struct intel_context *intel =
729 intelScreenContext(dPriv->driScreenPriv->private);
730 const intelScreenPrivate *intelScreen = intel->intelScreen;
731 unsigned int target;
732 drm_i915_vblank_swap_t swap;
733 GLboolean ret;
734
735 if (!dPriv->vblFlags ||
736 (dPriv->vblFlags & VBLANK_FLAG_NO_IRQ) ||
737 intelScreen->drmMinor < (intel_fb->pf_active ? 9 : 6))
738 return GL_FALSE;
739
740 interval = driGetVBlankInterval(dPriv);
741
742 swap.seqtype = DRM_VBLANK_ABSOLUTE;
743
744 if (dPriv->vblFlags & VBLANK_FLAG_SYNC) {
745 swap.seqtype |= DRM_VBLANK_NEXTONMISS;
746 } else if (interval == 0)
747 return GL_FALSE;
748
749 swap.drawable = dPriv->hHWDrawable;
750 target = swap.sequence = dPriv->vblSeq + interval;
751
752 if ( dPriv->vblFlags & VBLANK_FLAG_SECONDARY ) {
753 swap.seqtype |= DRM_VBLANK_SECONDARY;
754 }
755
756 LOCK_HARDWARE(intel);
757
758 intel_batchbuffer_flush(intel->batch);
759
760 if ( intel_fb->pf_active ) {
761 swap.seqtype |= DRM_VBLANK_FLIP;
762
763 intel_fb->pf_current_page = (((intel->sarea->pf_current_page >>
764 (intel_fb->pf_planes & 0x2)) & 0x3) + 1) %
765 intel_fb->pf_num_pages;
766 }
767
768 if (!drmCommandWriteRead(intel->driFd, DRM_I915_VBLANK_SWAP, &swap,
769 sizeof(swap))) {
770 dPriv->vblSeq = swap.sequence;
771 swap.sequence -= target;
772 *missed_target = swap.sequence > 0 && swap.sequence <= (1 << 23);
773
774 intel_get_renderbuffer(&intel_fb->Base, BUFFER_BACK_LEFT)->vbl_pending =
775 intel_get_renderbuffer(&intel_fb->Base,
776 BUFFER_FRONT_LEFT)->vbl_pending =
777 dPriv->vblSeq;
778
779 if (swap.seqtype & DRM_VBLANK_FLIP) {
780 intel_flip_renderbuffers(intel_fb);
781 intel_draw_buffer(&intel->ctx, intel->ctx.DrawBuffer);
782 }
783
784 ret = GL_TRUE;
785 } else {
786 if (swap.seqtype & DRM_VBLANK_FLIP) {
787 intel_fb->pf_current_page = ((intel->sarea->pf_current_page >>
788 (intel_fb->pf_planes & 0x2)) & 0x3) %
789 intel_fb->pf_num_pages;
790 }
791
792 ret = GL_FALSE;
793 }
794
795 UNLOCK_HARDWARE(intel);
796
797 return ret;
798 }
799
800 void
801 intelSwapBuffers(__DRIdrawablePrivate * dPriv)
802 {
803 __DRIscreenPrivate *psp = dPriv->driScreenPriv;
804
805 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
806 GET_CURRENT_CONTEXT(ctx);
807 struct intel_context *intel;
808
809 if (ctx == NULL)
810 return;
811
812 intel = intel_context(ctx);
813
814 if (ctx->Visual.doubleBufferMode) {
815 GLboolean missed_target;
816 struct intel_framebuffer *intel_fb = dPriv->driverPrivate;
817 int64_t ust;
818
819 _mesa_notifySwapBuffers(ctx); /* flush pending rendering comands */
820
821 if (!intelScheduleSwap(dPriv, &missed_target)) {
822 driWaitForVBlank(dPriv, &missed_target);
823
824 /*
825 * Update each buffer's vbl_pending so we don't get too out of
826 * sync
827 */
828 intel_get_renderbuffer(&intel_fb->Base,
829 BUFFER_BACK_LEFT)->vbl_pending =
830 intel_get_renderbuffer(&intel_fb->Base,
831 BUFFER_FRONT_LEFT)->vbl_pending =
832 dPriv->vblSeq;
833 if (!intelPageFlip(dPriv)) {
834 intelCopyBuffer(dPriv, NULL);
835 }
836 }
837
838 intel_fb->swap_count++;
839 (*psp->systemTime->getUST) (&ust);
840 if (missed_target) {
841 intel_fb->swap_missed_count++;
842 intel_fb->swap_missed_ust = ust - intel_fb->swap_ust;
843 }
844
845 intel_fb->swap_ust = ust;
846 }
847 drmCommandNone(intel->driFd, DRM_I915_GEM_THROTTLE);
848
849 }
850 else {
851 /* XXX this shouldn't be an error but we can't handle it for now */
852 fprintf(stderr, "%s: drawable has no context!\n", __FUNCTION__);
853 }
854 }
855
856 void
857 intelCopySubBuffer(__DRIdrawablePrivate * dPriv, int x, int y, int w, int h)
858 {
859 if (dPriv->driContextPriv && dPriv->driContextPriv->driverPrivate) {
860 struct intel_context *intel =
861 (struct intel_context *) dPriv->driContextPriv->driverPrivate;
862 GLcontext *ctx = &intel->ctx;
863
864 if (ctx->Visual.doubleBufferMode) {
865 drm_clip_rect_t rect;
866 rect.x1 = x + dPriv->x;
867 rect.y1 = (dPriv->h - y - h) + dPriv->y;
868 rect.x2 = rect.x1 + w;
869 rect.y2 = rect.y1 + h;
870 _mesa_notifySwapBuffers(ctx); /* flush pending rendering comands */
871 intelCopyBuffer(dPriv, &rect);
872 }
873 }
874 else {
875 /* XXX this shouldn't be an error but we can't handle it for now */
876 fprintf(stderr, "%s: drawable has no context!\n", __FUNCTION__);
877 }
878 }
879
880
881 /**
882 * Update the hardware state for drawing into a window or framebuffer object.
883 *
884 * Called by glDrawBuffer, glBindFramebufferEXT, MakeCurrent, and other
885 * places within the driver.
886 *
887 * Basically, this needs to be called any time the current framebuffer
888 * changes, the renderbuffers change, or we need to draw into different
889 * color buffers.
890 */
891 void
892 intel_draw_buffer(GLcontext * ctx, struct gl_framebuffer *fb)
893 {
894 struct intel_context *intel = intel_context(ctx);
895 struct intel_region *colorRegions[MAX_DRAW_BUFFERS], *depthRegion = NULL;
896 struct intel_renderbuffer *irbDepth = NULL, *irbStencil = NULL;
897 int front = 0; /* drawing to front color buffer? */
898
899 if (!fb) {
900 /* this can happen during the initial context initialization */
901 return;
902 }
903
904 /* Do this here, note core Mesa, since this function is called from
905 * many places within the driver.
906 */
907 if (ctx->NewState & (_NEW_BUFFERS | _NEW_COLOR | _NEW_PIXEL)) {
908 /* this updates the DrawBuffer->_NumColorDrawBuffers fields, etc */
909 _mesa_update_framebuffer(ctx);
910 /* this updates the DrawBuffer's Width/Height if it's a FBO */
911 _mesa_update_draw_buffer_bounds(ctx);
912 }
913
914 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
915 /* this may occur when we're called by glBindFrameBuffer() during
916 * the process of someone setting up renderbuffers, etc.
917 */
918 /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/
919 return;
920 }
921
922 if (fb->Name)
923 intel_validate_paired_depth_stencil(ctx, fb);
924
925 /*
926 * How many color buffers are we drawing into?
927 */
928 if (fb->_NumColorDrawBuffers == 0) {
929 /* writing to 0 */
930 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_TRUE);
931 colorRegions[0] = NULL;
932
933 if (fb->Name != 0)
934 intelSetRenderbufferClipRects(intel);
935 } else if (fb->_NumColorDrawBuffers > 1) {
936 int i;
937 struct intel_renderbuffer *irb;
938 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_FALSE);
939
940 if (fb->Name != 0)
941 intelSetRenderbufferClipRects(intel);
942 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
943 irb = intel_renderbuffer(fb->_ColorDrawBuffers[i]);
944 colorRegions[i] = (irb && irb->region) ? irb->region : NULL;
945 }
946 }
947 else {
948 /* draw to exactly one color buffer */
949 /*_mesa_debug(ctx, "Hardware rendering\n");*/
950 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_FALSE);
951 if (fb->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT) {
952 front = 1;
953 }
954
955 /*
956 * Get the intel_renderbuffer for the colorbuffer we're drawing into.
957 * And set up cliprects.
958 */
959 if (fb->Name == 0) {
960 /* drawing to window system buffer */
961 if (front) {
962 intelSetFrontClipRects(intel);
963 colorRegions[0] = intel_get_rb_region(fb, BUFFER_FRONT_LEFT);
964 }
965 else {
966 intelSetBackClipRects(intel);
967 colorRegions[0]= intel_get_rb_region(fb, BUFFER_BACK_LEFT);
968 }
969 }
970 else {
971 /* drawing to user-created FBO */
972 struct intel_renderbuffer *irb;
973 intelSetRenderbufferClipRects(intel);
974 irb = intel_renderbuffer(fb->_ColorDrawBuffers[0]);
975 colorRegions[0] = (irb && irb->region) ? irb->region : NULL;
976 }
977 }
978
979 /* Update culling direction which changes depending on the
980 * orientation of the buffer:
981 */
982 if (ctx->Driver.FrontFace)
983 ctx->Driver.FrontFace(ctx, ctx->Polygon.FrontFace);
984 else
985 ctx->NewState |= _NEW_POLYGON;
986
987 if (!colorRegions[0]) {
988 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_TRUE);
989 }
990 else {
991 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_FALSE);
992 }
993
994 /***
995 *** Get depth buffer region and check if we need a software fallback.
996 *** Note that the depth buffer is usually a DEPTH_STENCIL buffer.
997 ***/
998 if (fb->_DepthBuffer && fb->_DepthBuffer->Wrapped) {
999 irbDepth = intel_renderbuffer(fb->_DepthBuffer->Wrapped);
1000 if (irbDepth && irbDepth->region) {
1001 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_FALSE);
1002 depthRegion = irbDepth->region;
1003 }
1004 else {
1005 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_TRUE);
1006 depthRegion = NULL;
1007 }
1008 }
1009 else {
1010 /* not using depth buffer */
1011 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_FALSE);
1012 depthRegion = NULL;
1013 }
1014
1015 /***
1016 *** Stencil buffer
1017 *** This can only be hardware accelerated if we're using a
1018 *** combined DEPTH_STENCIL buffer (for now anyway).
1019 ***/
1020 if (fb->_StencilBuffer && fb->_StencilBuffer->Wrapped) {
1021 irbStencil = intel_renderbuffer(fb->_StencilBuffer->Wrapped);
1022 if (irbStencil && irbStencil->region) {
1023 ASSERT(irbStencil->Base._ActualFormat == GL_DEPTH24_STENCIL8_EXT);
1024 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_FALSE);
1025 /* need to re-compute stencil hw state */
1026 if (ctx->Driver.Enable != NULL)
1027 ctx->Driver.Enable(ctx, GL_STENCIL_TEST, ctx->Stencil.Enabled);
1028 else
1029 ctx->NewState |= _NEW_STENCIL;
1030 if (!depthRegion)
1031 depthRegion = irbStencil->region;
1032 }
1033 else {
1034 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_TRUE);
1035 }
1036 }
1037 else {
1038 /* XXX FBO: instead of FALSE, pass ctx->Stencil.Enabled ??? */
1039 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_FALSE);
1040 /* need to re-compute stencil hw state */
1041 if (ctx->Driver.Enable != NULL)
1042 ctx->Driver.Enable(ctx, GL_STENCIL_TEST, ctx->Stencil.Enabled);
1043 else
1044 ctx->NewState |= _NEW_STENCIL;
1045 }
1046
1047 /*
1048 * Update depth test state
1049 */
1050 if (ctx->Driver.Enable) {
1051 if (ctx->Depth.Test && fb->Visual.depthBits > 0) {
1052 ctx->Driver.Enable(ctx, GL_DEPTH_TEST, GL_TRUE);
1053 } else {
1054 ctx->Driver.Enable(ctx, GL_DEPTH_TEST, GL_FALSE);
1055 }
1056 } else {
1057 ctx->NewState |= _NEW_DEPTH;
1058 }
1059
1060 intel->vtbl.set_draw_region(intel, colorRegions, depthRegion,
1061 fb->_NumColorDrawBuffers);
1062
1063 /* update viewport since it depends on window size */
1064 if (ctx->Driver.Viewport) {
1065 ctx->Driver.Viewport(ctx, ctx->Viewport.X, ctx->Viewport.Y,
1066 ctx->Viewport.Width, ctx->Viewport.Height);
1067 } else {
1068 ctx->NewState |= _NEW_VIEWPORT;
1069 }
1070
1071 /* Set state we know depends on drawable parameters:
1072 */
1073 if (ctx->Driver.Scissor)
1074 ctx->Driver.Scissor(ctx, ctx->Scissor.X, ctx->Scissor.Y,
1075 ctx->Scissor.Width, ctx->Scissor.Height);
1076 intel->NewGLState |= _NEW_SCISSOR;
1077
1078 if (ctx->Driver.DepthRange)
1079 ctx->Driver.DepthRange(ctx,
1080 ctx->Viewport.Near,
1081 ctx->Viewport.Far);
1082 }
1083
1084
1085 static void
1086 intelDrawBuffer(GLcontext * ctx, GLenum mode)
1087 {
1088 intel_draw_buffer(ctx, ctx->DrawBuffer);
1089 }
1090
1091
1092 static void
1093 intelReadBuffer(GLcontext * ctx, GLenum mode)
1094 {
1095 if (ctx->ReadBuffer == ctx->DrawBuffer) {
1096 /* This will update FBO completeness status.
1097 * A framebuffer will be incomplete if the GL_READ_BUFFER setting
1098 * refers to a missing renderbuffer. Calling glReadBuffer can set
1099 * that straight and can make the drawing buffer complete.
1100 */
1101 intel_draw_buffer(ctx, ctx->DrawBuffer);
1102 }
1103 /* Generally, functions which read pixels (glReadPixels, glCopyPixels, etc)
1104 * reference ctx->ReadBuffer and do appropriate state checks.
1105 */
1106 }
1107
1108
1109 void
1110 intelInitBufferFuncs(struct dd_function_table *functions)
1111 {
1112 functions->Clear = intelClear;
1113 functions->DrawBuffer = intelDrawBuffer;
1114 functions->ReadBuffer = intelReadBuffer;
1115 }