1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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"
42 #include "drirenderbuffer.h"
46 /* This block can be removed when libdrm >= 2.3.1 is required */
48 #ifndef DRM_IOCTL_I915_FLIP
50 #define DRM_VBLANK_FLIP 0x8000000
52 typedef struct drm_i915_flip
{
56 #undef DRM_IOCTL_I915_FLIP
57 #define DRM_IOCTL_I915_FLIP DRM_IOW(DRM_COMMAND_BASE + DRM_I915_FLIP, \
62 #define FILE_DEBUG_FLAG DEBUG_BLIT
65 * XXX move this into a new dri/common/cliprects.c file.
68 intel_intersect_cliprects(drm_clip_rect_t
* dst
,
69 const drm_clip_rect_t
* a
,
70 const drm_clip_rect_t
* b
)
74 GLint bw
= b
->x2
- bx
;
75 GLint bh
= b
->y2
- by
;
78 bw
-= a
->x1
- bx
, bx
= a
->x1
;
80 bh
-= a
->y1
- by
, by
= a
->y1
;
99 * Return pointer to current color drawing region, or NULL.
101 struct intel_region
*
102 intel_drawbuf_region(struct intel_context
*intel
)
104 struct intel_renderbuffer
*irbColor
=
105 intel_renderbuffer(intel
->ctx
.DrawBuffer
->_ColorDrawBuffers
[0]);
107 return irbColor
->region
;
113 * Return pointer to current color reading region, or NULL.
115 struct intel_region
*
116 intel_readbuf_region(struct intel_context
*intel
)
118 struct intel_renderbuffer
*irb
119 = intel_renderbuffer(intel
->ctx
.ReadBuffer
->_ColorReadBuffer
);
129 * Update the following fields for rendering to a user-created FBO:
130 * intel->numClipRects
136 intelSetRenderbufferClipRects(struct intel_context
*intel
)
138 /* If the batch contents require looping over cliprects, flush them before
139 * we go changing which cliprects get referenced when that happens.
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
);
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
;
160 * As above, but for rendering to front buffer of a window.
161 * \sa intelSetRenderbufferClipRects
164 intelSetFrontClipRects(struct intel_context
*intel
)
166 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
171 /* If the batch contents require looping over cliprects, flush them before
172 * we go changing which cliprects get referenced when that happens.
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
;
185 * As above, but for rendering to back buffer of a window.
188 intelSetBackClipRects(struct intel_context
*intel
)
190 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
191 struct intel_framebuffer
*intel_fb
;
196 intel_fb
= dPriv
->driverPrivate
;
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
);
204 intel
->numClipRects
= dPriv
->numClipRects
;
205 intel
->pClipRects
= dPriv
->pClipRects
;
206 intel
->drawX
= dPriv
->x
;
207 intel
->drawY
= dPriv
->y
;
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
);
215 intel
->numClipRects
= dPriv
->numBackClipRects
;
216 intel
->pClipRects
= dPriv
->pBackClipRects
;
217 intel
->drawX
= dPriv
->backX
;
218 intel
->drawY
= dPriv
->backY
;
223 intelUpdatePageFlipping(struct intel_context
*intel
,
224 GLint areaA
, GLint areaB
)
226 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
227 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
231 /* Update page flipping info */
240 intel_fb
->pf_current_page
= (intel
->sarea
->pf_current_page
>>
241 (intel_fb
->pf_planes
& 0x2)) & 0x3;
243 intel_fb
->pf_num_pages
= intel
->intelScreen
->third
.handle
? 3 : 2;
245 pf_active
= pf_planes
&& (pf_planes
& intel
->sarea
->pf_active
) == pf_planes
;
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");
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
;
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.
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;
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
;
281 drmCommandWrite(intel
->driFd
, DRM_I915_FLIP
, &flip
, sizeof(flip
));
284 intel_fb
->pf_planes
= pf_planes
;
287 intel_fb
->pf_active
= pf_active
;
288 intel_flip_renderbuffers(intel_fb
);
289 intel_draw_buffer(&intel
->ctx
, intel
->ctx
.DrawBuffer
);
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).
297 intelWindowMoved(struct intel_context
*intel
)
299 GLcontext
*ctx
= &intel
->ctx
;
300 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
301 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
303 if (!intel
->ctx
.DrawBuffer
) {
304 /* when would this happen? -BP */
305 intelSetFrontClipRects(intel
);
307 else if (intel
->ctx
.DrawBuffer
->Name
!= 0) {
308 /* drawing to user-created FBO - do nothing */
309 /* Cliprects would be set from intelDrawBuffer() */
312 /* drawing to a window */
313 switch (intel_fb
->Base
._ColorDrawBufferIndexes
[0]) {
314 case BUFFER_FRONT_LEFT
:
315 intelSetFrontClipRects(intel
);
317 case BUFFER_BACK_LEFT
:
318 intelSetBackClipRects(intel
);
321 intelSetFrontClipRects(intel
);
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
;
341 intelUpdatePageFlipping(intel
, areaA
, areaB
);
343 /* Update vblank info
345 if (areaB
> areaA
|| (areaA
== areaB
&& areaB
> 0)) {
346 flags
= dPriv
->vblFlags
| VBLANK_FLAG_SECONDARY
;
348 flags
= dPriv
->vblFlags
& ~VBLANK_FLAG_SECONDARY
;
351 /* Check to see if we changed pipes */
352 if (flags
!= dPriv
->vblFlags
&& dPriv
->vblFlags
&&
353 !(dPriv
->vblFlags
& VBLANK_FLAG_NO_IRQ
)) {
359 * Deal with page flipping
361 vbl
.request
.type
= DRM_VBLANK_ABSOLUTE
;
363 if ( dPriv
->vblFlags
& VBLANK_FLAG_SECONDARY
) {
364 vbl
.request
.type
|= DRM_VBLANK_SECONDARY
;
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
) <=
373 vbl
.request
.sequence
= intel_fb
->color_rb
[i
]->vbl_pending
;
374 drmWaitVBlank(intel
->driFd
, &vbl
);
378 * Update msc_base from old pipe
380 driDrawableGetMSC32(dPriv
->driScreenPriv
, dPriv
, &count
);
381 dPriv
->msc_base
= count
;
383 * Then get new vblank_base and vblSeq values
385 dPriv
->vblFlags
= flags
;
386 driGetCurrentVBlank(dPriv
);
387 dPriv
->vblank_base
= dPriv
->vblSeq
;
389 intel_fb
->vbl_waited
= dPriv
->vblSeq
;
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
;
397 dPriv
->vblFlags
&= ~VBLANK_FLAG_SECONDARY
;
400 /* Update Mesa's notion of window size */
401 driUpdateFramebufferSize(ctx
, dPriv
);
402 intel_fb
->Base
.Initialized
= GL_TRUE
; /* XXX remove someday */
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
);
410 /* Re-calculate viewport related state */
411 if (ctx
->Driver
.DepthRange
!= NULL
)
412 ctx
->Driver
.DepthRange( ctx
, ctx
->Viewport
.Near
, ctx
->Viewport
.Far
);
417 /* A true meta version of this would be very simple and additionally
418 * machine independent. Maybe we'll get there one day.
421 intelClearWithTris(struct intel_context
*intel
, GLbitfield mask
)
423 GLcontext
*ctx
= &intel
->ctx
;
424 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
427 intel
->vtbl
.install_meta_state(intel
);
429 /* Back and stencil cliprects are the same. Try and do both
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
);
438 intel
->vtbl
.meta_draw_region(intel
, backRegion
, depthRegion
);
440 if (mask
& BUFFER_BIT_BACK_LEFT
)
441 intel
->vtbl
.meta_color_mask(intel
, GL_TRUE
);
443 intel
->vtbl
.meta_color_mask(intel
, GL_FALSE
);
445 if (mask
& BUFFER_BIT_STENCIL
)
446 intel
->vtbl
.meta_stencil_replace(intel
,
447 intel
->ctx
.Stencil
.WriteMask
[0],
448 intel
->ctx
.Stencil
.Clear
);
450 intel
->vtbl
.meta_no_stencil_write(intel
);
452 if (mask
& BUFFER_BIT_DEPTH
)
453 intel
->vtbl
.meta_depth_replace(intel
);
455 intel
->vtbl
.meta_no_depth_write(intel
);
457 intel
->vtbl
.meta_draw_quad(intel
,
462 intel
->ctx
.Depth
.Clear
,
463 intel
->ClearColor8888
,
464 0, 0, 0, 0); /* texcoords */
466 mask
&= ~(BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_STENCIL
| BUFFER_BIT_DEPTH
);
469 /* clear the remaining (color) renderbuffers */
470 for (buf
= 0; buf
< BUFFER_COUNT
&& mask
; buf
++) {
471 const GLuint bufBit
= 1 << buf
;
473 struct intel_renderbuffer
*irbColor
=
474 intel_renderbuffer(fb
->Attachment
[buf
].Renderbuffer
);
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
);
483 intel
->vtbl
.meta_draw_quad(intel
,
488 0, intel
->ClearColor8888
,
489 0, 0, 0, 0); /* texcoords */
495 intel
->vtbl
.leave_meta_state(intel
);
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",
521 * Called by ctx->Driver.Clear.
524 intelClear(GLcontext
*ctx
, GLbitfield mask
)
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
;
535 fprintf(stderr
, "%s\n", __FUNCTION__
);
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
);
544 /* glColorMask in effect */
545 tri_mask
|= (mask
& BUFFER_BITS_COLOR
);
549 if (mask
& BUFFER_BIT_STENCIL
) {
550 const struct intel_region
*stencilRegion
551 = intel_get_rb_region(fb
, BUFFER_STENCIL
);
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.
560 tri_mask
|= BUFFER_BIT_STENCIL
;
563 /* clearing all stencil bits, use blitting */
564 blit_mask
|= BUFFER_BIT_STENCIL
;
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
;
576 blit_mask
|= BUFFER_BIT_DEPTH
;
579 /* SW fallback clearing */
580 swrast_mask
= mask
& ~tri_mask
& ~blit_mask
;
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
;
588 swrast_mask
|= bufBit
;
594 if (INTEL_DEBUG
& DEBUG_BLIT
) {
596 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
597 if (blit_mask
& (1 << i
))
598 DBG(" %s", buffer_names
[i
]);
602 intelClearWithBlit(ctx
, blit_mask
);
606 if (INTEL_DEBUG
& DEBUG_BLIT
) {
608 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
609 if (tri_mask
& (1 << i
))
610 DBG(" %s", buffer_names
[i
]);
614 intelClearWithTris(intel
, tri_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
]);
626 _swrast_Clear(ctx
, swrast_mask
);
631 /* Emit wait for pending flips */
633 intel_wait_flips(struct intel_context
*intel
)
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
:
643 if (intel
->intelScreen
->driScrnPriv
->dri2
.enabled
)
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
;
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
)
655 OUT_BATCH(pf_planes
& 0x2 ? (MI_WAIT_FOR_EVENT
| MI_WAIT_FOR_PLANE_B_FLIP
)
659 intel_rb
->pf_pending
--;
664 /* Flip the front & back buffers
667 intelPageFlip(const __DRIdrawablePrivate
* dPriv
)
669 struct intel_context
*intel
;
671 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
673 if (INTEL_DEBUG
& DEBUG_IOCTL
)
674 fprintf(stderr
, "%s\n", __FUNCTION__
);
677 assert(dPriv
->driContextPriv
);
678 assert(dPriv
->driContextPriv
->driverPrivate
);
680 intel
= (struct intel_context
*) dPriv
->driContextPriv
->driverPrivate
;
682 if (intel
->intelScreen
->drmMinor
< 9)
685 intelFlush(&intel
->ctx
);
689 LOCK_HARDWARE(intel
);
691 if (dPriv
->numClipRects
&& intel_fb
->pf_active
) {
692 drm_i915_flip_t flip
;
694 flip
.pipes
= intel_fb
->pf_planes
;
696 ret
= drmCommandWrite(intel
->driFd
, DRM_I915_FLIP
, &flip
, sizeof(flip
));
699 UNLOCK_HARDWARE(intel
);
701 if (ret
|| !intel_fb
->pf_active
)
704 if (!dPriv
->numClipRects
) {
705 usleep(10000); /* throttle invisible client 10ms */
708 intel_fb
->pf_current_page
= (intel
->sarea
->pf_current_page
>>
709 (intel_fb
->pf_planes
& 0x2)) & 0x3;
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
=
717 intel_flip_renderbuffers(intel_fb
);
718 intel_draw_buffer(&intel
->ctx
, &intel_fb
->Base
);
724 intelScheduleSwap(__DRIdrawablePrivate
* dPriv
, GLboolean
*missed_target
)
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
;
732 drm_i915_vblank_swap_t swap
;
735 if (!dPriv
->vblFlags
||
736 (dPriv
->vblFlags
& VBLANK_FLAG_NO_IRQ
) ||
737 intelScreen
->drmMinor
< (intel_fb
->pf_active
? 9 : 6))
740 interval
= driGetVBlankInterval(dPriv
);
742 swap
.seqtype
= DRM_VBLANK_ABSOLUTE
;
744 if (dPriv
->vblFlags
& VBLANK_FLAG_SYNC
) {
745 swap
.seqtype
|= DRM_VBLANK_NEXTONMISS
;
746 } else if (interval
== 0)
749 swap
.drawable
= dPriv
->hHWDrawable
;
750 target
= swap
.sequence
= dPriv
->vblSeq
+ interval
;
752 if ( dPriv
->vblFlags
& VBLANK_FLAG_SECONDARY
) {
753 swap
.seqtype
|= DRM_VBLANK_SECONDARY
;
756 LOCK_HARDWARE(intel
);
758 intel_batchbuffer_flush(intel
->batch
);
760 if ( intel_fb
->pf_active
) {
761 swap
.seqtype
|= DRM_VBLANK_FLIP
;
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
;
768 if (!drmCommandWriteRead(intel
->driFd
, DRM_I915_VBLANK_SWAP
, &swap
,
770 dPriv
->vblSeq
= swap
.sequence
;
771 swap
.sequence
-= target
;
772 *missed_target
= swap
.sequence
> 0 && swap
.sequence
<= (1 << 23);
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
=
779 if (swap
.seqtype
& DRM_VBLANK_FLIP
) {
780 intel_flip_renderbuffers(intel_fb
);
781 intel_draw_buffer(&intel
->ctx
, intel
->ctx
.DrawBuffer
);
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
;
795 UNLOCK_HARDWARE(intel
);
801 intelSwapBuffers(__DRIdrawablePrivate
* dPriv
)
803 __DRIscreenPrivate
*psp
= dPriv
->driScreenPriv
;
805 if (dPriv
->driContextPriv
&& dPriv
->driContextPriv
->driverPrivate
) {
806 GET_CURRENT_CONTEXT(ctx
);
807 struct intel_context
*intel
;
812 intel
= intel_context(ctx
);
814 if (ctx
->Visual
.doubleBufferMode
) {
815 GLboolean missed_target
;
816 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
819 _mesa_notifySwapBuffers(ctx
); /* flush pending rendering comands */
821 if (!intelScheduleSwap(dPriv
, &missed_target
)) {
822 driWaitForVBlank(dPriv
, &missed_target
);
825 * Update each buffer's vbl_pending so we don't get too out of
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
=
833 if (!intelPageFlip(dPriv
)) {
834 intelCopyBuffer(dPriv
, NULL
);
838 intel_fb
->swap_count
++;
839 (*psp
->systemTime
->getUST
) (&ust
);
841 intel_fb
->swap_missed_count
++;
842 intel_fb
->swap_missed_ust
= ust
- intel_fb
->swap_ust
;
845 intel_fb
->swap_ust
= ust
;
847 drmCommandNone(intel
->driFd
, DRM_I915_GEM_THROTTLE
);
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__
);
857 intelCopySubBuffer(__DRIdrawablePrivate
* dPriv
, int x
, int y
, int w
, int h
)
859 if (dPriv
->driContextPriv
&& dPriv
->driContextPriv
->driverPrivate
) {
860 struct intel_context
*intel
=
861 (struct intel_context
*) dPriv
->driContextPriv
->driverPrivate
;
862 GLcontext
*ctx
= &intel
->ctx
;
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
);
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__
);
882 * Update the hardware state for drawing into a window or framebuffer object.
884 * Called by glDrawBuffer, glBindFramebufferEXT, MakeCurrent, and other
885 * places within the driver.
887 * Basically, this needs to be called any time the current framebuffer
888 * changes, the renderbuffers change, or we need to draw into different
892 intel_draw_buffer(GLcontext
* ctx
, struct gl_framebuffer
*fb
)
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? */
900 /* this can happen during the initial context initialization */
904 /* Do this here, note core Mesa, since this function is called from
905 * many places within the driver.
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
);
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.
918 /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/
923 intel_validate_paired_depth_stencil(ctx
, fb
);
926 * How many color buffers are we drawing into?
928 if (fb
->_NumColorDrawBuffers
== 0) {
930 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_TRUE
);
931 colorRegions
[0] = NULL
;
934 intelSetRenderbufferClipRects(intel
);
935 } else if (fb
->_NumColorDrawBuffers
> 1) {
937 struct intel_renderbuffer
*irb
;
938 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_FALSE
);
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
;
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
) {
956 * Get the intel_renderbuffer for the colorbuffer we're drawing into.
957 * And set up cliprects.
960 /* drawing to window system buffer */
962 intelSetFrontClipRects(intel
);
963 colorRegions
[0] = intel_get_rb_region(fb
, BUFFER_FRONT_LEFT
);
966 intelSetBackClipRects(intel
);
967 colorRegions
[0]= intel_get_rb_region(fb
, BUFFER_BACK_LEFT
);
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
;
979 /* Update culling direction which changes depending on the
980 * orientation of the buffer:
982 if (ctx
->Driver
.FrontFace
)
983 ctx
->Driver
.FrontFace(ctx
, ctx
->Polygon
.FrontFace
);
985 ctx
->NewState
|= _NEW_POLYGON
;
987 if (!colorRegions
[0]) {
988 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_TRUE
);
991 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_FALSE
);
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.
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
;
1005 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_TRUE
);
1010 /* not using depth buffer */
1011 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_FALSE
);
1017 *** This can only be hardware accelerated if we're using a
1018 *** combined DEPTH_STENCIL buffer (for now anyway).
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
);
1029 ctx
->NewState
|= _NEW_STENCIL
;
1031 depthRegion
= irbStencil
->region
;
1034 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_TRUE
);
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
);
1044 ctx
->NewState
|= _NEW_STENCIL
;
1048 * Update depth test state
1050 if (ctx
->Driver
.Enable
) {
1051 if (ctx
->Depth
.Test
&& fb
->Visual
.depthBits
> 0) {
1052 ctx
->Driver
.Enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1054 ctx
->Driver
.Enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
1057 ctx
->NewState
|= _NEW_DEPTH
;
1060 intel
->vtbl
.set_draw_region(intel
, colorRegions
, depthRegion
,
1061 fb
->_NumColorDrawBuffers
);
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
);
1068 ctx
->NewState
|= _NEW_VIEWPORT
;
1071 /* Set state we know depends on drawable parameters:
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
;
1078 if (ctx
->Driver
.DepthRange
)
1079 ctx
->Driver
.DepthRange(ctx
,
1086 intelDrawBuffer(GLcontext
* ctx
, GLenum mode
)
1088 intel_draw_buffer(ctx
, ctx
->DrawBuffer
);
1093 intelReadBuffer(GLcontext
* ctx
, GLenum mode
)
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.
1101 intel_draw_buffer(ctx
, ctx
->DrawBuffer
);
1103 /* Generally, functions which read pixels (glReadPixels, glCopyPixels, etc)
1104 * reference ctx->ReadBuffer and do appropriate state checks.
1110 intelInitBufferFuncs(struct dd_function_table
*functions
)
1112 functions
->Clear
= intelClear
;
1113 functions
->DrawBuffer
= intelDrawBuffer
;
1114 functions
->ReadBuffer
= intelReadBuffer
;