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