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"
40 #include "drirenderbuffer.h"
41 #include "framebuffer.h"
42 #include "swrast/swrast.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 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
;
152 * As above, but for rendering to front buffer of a window.
153 * \sa intelSetRenderbufferClipRects
156 intelSetFrontClipRects(struct intel_context
*intel
)
158 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
163 intel
->numClipRects
= dPriv
->numClipRects
;
164 intel
->pClipRects
= dPriv
->pClipRects
;
165 intel
->drawX
= dPriv
->x
;
166 intel
->drawY
= dPriv
->y
;
171 * As above, but for rendering to back buffer of a window.
174 intelSetBackClipRects(struct intel_context
*intel
)
176 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
177 struct intel_framebuffer
*intel_fb
;
182 intel_fb
= dPriv
->driverPrivate
;
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
;
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
;
202 intelUpdatePageFlipping(struct intel_context
*intel
,
203 GLint areaA
, GLint areaB
)
205 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
206 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
210 /* Update page flipping info */
219 intel_fb
->pf_current_page
= (intel
->sarea
->pf_current_page
>>
220 (intel_fb
->pf_planes
& 0x2)) & 0x3;
222 intel_fb
->pf_num_pages
= intel
->intelScreen
->third
.handle
? 3 : 2;
224 pf_active
= pf_planes
&& (pf_planes
& intel
->sarea
->pf_active
) == pf_planes
;
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");
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
;
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.
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;
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
;
260 drmCommandWrite(intel
->driFd
, DRM_I915_FLIP
, &flip
, sizeof(flip
));
263 intel_fb
->pf_planes
= pf_planes
;
266 intel_fb
->pf_active
= pf_active
;
267 intel_flip_renderbuffers(intel_fb
);
268 intel_draw_buffer(&intel
->ctx
, intel
->ctx
.DrawBuffer
);
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).
277 intelWindowMoved(struct intel_context
*intel
)
279 GLcontext
*ctx
= &intel
->ctx
;
280 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
281 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
283 if (!intel
->ctx
.DrawBuffer
) {
284 /* when would this happen? -BP */
285 intelSetFrontClipRects(intel
);
287 else if (intel
->ctx
.DrawBuffer
->Name
!= 0) {
288 /* drawing to user-created FBO - do nothing */
289 /* Cliprects would be set from intelDrawBuffer() */
292 /* drawing to a window */
293 switch (intel_fb
->Base
._ColorDrawBufferIndexes
[0]) {
294 case BUFFER_FRONT_LEFT
:
295 intelSetFrontClipRects(intel
);
297 case BUFFER_BACK_LEFT
:
298 intelSetBackClipRects(intel
);
301 intelSetFrontClipRects(intel
);
306 if (!intel
->intelScreen
->driScrnPriv
->dri2
.enabled
&&
307 intel
->intelScreen
->driScrnPriv
->ddx_version
.minor
>= 7) {
308 volatile struct drm_i915_sarea
*sarea
= intel
->sarea
;
309 drm_clip_rect_t drw_rect
= { .x1
= dPriv
->x
, .x2
= dPriv
->x
+ dPriv
->w
,
310 .y1
= dPriv
->y
, .y2
= dPriv
->y
+ dPriv
->h
};
311 drm_clip_rect_t planeA_rect
= { .x1
= sarea
->planeA_x
, .y1
= sarea
->planeA_y
,
312 .x2
= sarea
->planeA_x
+ sarea
->planeA_w
,
313 .y2
= sarea
->planeA_y
+ sarea
->planeA_h
};
314 drm_clip_rect_t planeB_rect
= { .x1
= sarea
->planeB_x
, .y1
= sarea
->planeB_y
,
315 .x2
= sarea
->planeB_x
+ sarea
->planeB_w
,
316 .y2
= sarea
->planeB_y
+ sarea
->planeB_h
};
317 GLint areaA
= driIntersectArea( drw_rect
, planeA_rect
);
318 GLint areaB
= driIntersectArea( drw_rect
, planeB_rect
);
319 GLuint flags
= dPriv
->vblFlags
;
322 intelUpdatePageFlipping(intel
, areaA
, areaB
);
325 /* Update vblank info
327 if (areaB
> areaA
|| (areaA
== areaB
&& areaB
> 0)) {
328 flags
= dPriv
->vblFlags
| VBLANK_FLAG_SECONDARY
;
330 flags
= dPriv
->vblFlags
& ~VBLANK_FLAG_SECONDARY
;
333 /* Check to see if we changed pipes */
334 if (flags
!= dPriv
->vblFlags
&& dPriv
->vblFlags
&&
335 !(dPriv
->vblFlags
& VBLANK_FLAG_NO_IRQ
)) {
341 * Deal with page flipping
343 vbl
.request
.type
= DRM_VBLANK_ABSOLUTE
;
345 if ( dPriv
->vblFlags
& VBLANK_FLAG_SECONDARY
) {
346 vbl
.request
.type
|= DRM_VBLANK_SECONDARY
;
349 for (i
= 0; i
< intel_fb
->pf_num_pages
; i
++) {
350 if (!intel_fb
->color_rb
[i
] ||
351 (intel_fb
->vbl_waited
- intel_fb
->color_rb
[i
]->vbl_pending
) <=
355 vbl
.request
.sequence
= intel_fb
->color_rb
[i
]->vbl_pending
;
356 drmWaitVBlank(intel
->driFd
, &vbl
);
360 * Update msc_base from old pipe
362 driDrawableGetMSC32(dPriv
->driScreenPriv
, dPriv
, &count
);
363 dPriv
->msc_base
= count
;
365 * Then get new vblank_base and vblSeq values
367 dPriv
->vblFlags
= flags
;
368 driGetCurrentVBlank(dPriv
);
369 dPriv
->vblank_base
= dPriv
->vblSeq
;
371 intel_fb
->vbl_waited
= dPriv
->vblSeq
;
373 for (i
= 0; i
< intel_fb
->pf_num_pages
; i
++) {
374 if (intel_fb
->color_rb
[i
])
375 intel_fb
->color_rb
[i
]->vbl_pending
= intel_fb
->vbl_waited
;
379 dPriv
->vblFlags
&= ~VBLANK_FLAG_SECONDARY
;
382 /* Update Mesa's notion of window size */
383 driUpdateFramebufferSize(ctx
, dPriv
);
384 intel_fb
->Base
.Initialized
= GL_TRUE
; /* XXX remove someday */
386 /* Update hardware scissor */
387 if (ctx
->Driver
.Scissor
!= NULL
) {
388 ctx
->Driver
.Scissor(ctx
, ctx
->Scissor
.X
, ctx
->Scissor
.Y
,
389 ctx
->Scissor
.Width
, ctx
->Scissor
.Height
);
392 /* Re-calculate viewport related state */
393 if (ctx
->Driver
.DepthRange
!= NULL
)
394 ctx
->Driver
.DepthRange( ctx
, ctx
->Viewport
.Near
, ctx
->Viewport
.Far
);
399 /* A true meta version of this would be very simple and additionally
400 * machine independent. Maybe we'll get there one day.
403 intelClearWithTris(struct intel_context
*intel
, GLbitfield mask
)
405 GLcontext
*ctx
= &intel
->ctx
;
406 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
409 intel
->vtbl
.install_meta_state(intel
);
411 /* Back and stencil cliprects are the same. Try and do both
414 if (mask
& (BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_STENCIL
| BUFFER_BIT_DEPTH
)) {
415 struct intel_region
*backRegion
=
416 intel_get_rb_region(fb
, BUFFER_BACK_LEFT
);
417 struct intel_region
*depthRegion
=
418 intel_get_rb_region(fb
, BUFFER_DEPTH
);
420 intel
->vtbl
.meta_draw_region(intel
, backRegion
, depthRegion
);
422 if (mask
& BUFFER_BIT_BACK_LEFT
)
423 intel
->vtbl
.meta_color_mask(intel
, GL_TRUE
);
425 intel
->vtbl
.meta_color_mask(intel
, GL_FALSE
);
427 if (mask
& BUFFER_BIT_STENCIL
)
428 intel
->vtbl
.meta_stencil_replace(intel
,
429 intel
->ctx
.Stencil
.WriteMask
[0],
430 intel
->ctx
.Stencil
.Clear
);
432 intel
->vtbl
.meta_no_stencil_write(intel
);
434 if (mask
& BUFFER_BIT_DEPTH
)
435 intel
->vtbl
.meta_depth_replace(intel
);
437 intel
->vtbl
.meta_no_depth_write(intel
);
439 intel
->vtbl
.meta_draw_quad(intel
,
444 intel
->ctx
.Depth
.Clear
,
445 intel
->ClearColor8888
,
446 0, 0, 0, 0); /* texcoords */
448 mask
&= ~(BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_STENCIL
| BUFFER_BIT_DEPTH
);
451 /* clear the remaining (color) renderbuffers */
452 for (buf
= 0; buf
< BUFFER_COUNT
&& mask
; buf
++) {
453 const GLuint bufBit
= 1 << buf
;
455 struct intel_renderbuffer
*irbColor
=
456 intel_renderbuffer(fb
->Attachment
[buf
].Renderbuffer
);
460 intel
->vtbl
.meta_no_depth_write(intel
);
461 intel
->vtbl
.meta_no_stencil_write(intel
);
462 intel
->vtbl
.meta_color_mask(intel
, GL_TRUE
);
463 intel
->vtbl
.meta_draw_region(intel
, irbColor
->region
, NULL
);
465 intel
->vtbl
.meta_draw_quad(intel
,
470 0, intel
->ClearColor8888
,
471 0, 0, 0, 0); /* texcoords */
477 intel
->vtbl
.leave_meta_state(intel
);
480 static const char *buffer_names
[] = {
481 [BUFFER_FRONT_LEFT
] = "front",
482 [BUFFER_BACK_LEFT
] = "back",
483 [BUFFER_FRONT_RIGHT
] = "front right",
484 [BUFFER_BACK_RIGHT
] = "back right",
485 [BUFFER_AUX0
] = "aux0",
486 [BUFFER_AUX1
] = "aux1",
487 [BUFFER_AUX2
] = "aux2",
488 [BUFFER_AUX3
] = "aux3",
489 [BUFFER_DEPTH
] = "depth",
490 [BUFFER_STENCIL
] = "stencil",
491 [BUFFER_ACCUM
] = "accum",
492 [BUFFER_COLOR0
] = "color0",
493 [BUFFER_COLOR1
] = "color1",
494 [BUFFER_COLOR2
] = "color2",
495 [BUFFER_COLOR3
] = "color3",
496 [BUFFER_COLOR4
] = "color4",
497 [BUFFER_COLOR5
] = "color5",
498 [BUFFER_COLOR6
] = "color6",
499 [BUFFER_COLOR7
] = "color7",
503 * Called by ctx->Driver.Clear.
506 intelClear(GLcontext
*ctx
, GLbitfield mask
)
508 struct intel_context
*intel
= intel_context(ctx
);
509 const GLuint colorMask
= *((GLuint
*) & ctx
->Color
.ColorMask
);
510 GLbitfield tri_mask
= 0;
511 GLbitfield blit_mask
= 0;
512 GLbitfield swrast_mask
= 0;
513 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
517 fprintf(stderr
, "%s\n", __FUNCTION__
);
519 /* HW color buffers (front, back, aux, generic FBO, etc) */
520 if (colorMask
== ~0) {
521 /* clear all R,G,B,A */
522 /* XXX FBO: need to check if colorbuffers are software RBOs! */
523 blit_mask
|= (mask
& BUFFER_BITS_COLOR
);
526 /* glColorMask in effect */
527 tri_mask
|= (mask
& BUFFER_BITS_COLOR
);
531 if (mask
& BUFFER_BIT_STENCIL
) {
532 const struct intel_region
*stencilRegion
533 = intel_get_rb_region(fb
, BUFFER_STENCIL
);
535 /* have hw stencil */
536 if (IS_965(intel
->intelScreen
->deviceID
) ||
537 (ctx
->Stencil
.WriteMask
[0] & 0xff) != 0xff) {
538 /* We have to use the 3D engine if we're clearing a partial mask
539 * of the stencil buffer, or if we're on a 965 which has a tiled
540 * depth/stencil buffer in a layout we can't blit to.
542 tri_mask
|= BUFFER_BIT_STENCIL
;
545 /* clearing all stencil bits, use blitting */
546 blit_mask
|= BUFFER_BIT_STENCIL
;
552 if (mask
& BUFFER_BIT_DEPTH
) {
553 /* clear depth with whatever method is used for stencil (see above) */
554 if (IS_965(intel
->intelScreen
->deviceID
) ||
555 tri_mask
& BUFFER_BIT_STENCIL
)
556 tri_mask
|= BUFFER_BIT_DEPTH
;
558 blit_mask
|= BUFFER_BIT_DEPTH
;
561 /* SW fallback clearing */
562 swrast_mask
= mask
& ~tri_mask
& ~blit_mask
;
564 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
565 GLuint bufBit
= 1 << i
;
566 if ((blit_mask
| tri_mask
) & bufBit
) {
567 if (!fb
->Attachment
[i
].Renderbuffer
->ClassID
) {
568 blit_mask
&= ~bufBit
;
570 swrast_mask
|= bufBit
;
576 if (INTEL_DEBUG
& DEBUG_BLIT
) {
578 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
579 if (blit_mask
& (1 << i
))
580 DBG(" %s", buffer_names
[i
]);
584 intelClearWithBlit(ctx
, blit_mask
);
588 if (INTEL_DEBUG
& DEBUG_BLIT
) {
590 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
591 if (tri_mask
& (1 << i
))
592 DBG(" %s", buffer_names
[i
]);
596 intelClearWithTris(intel
, tri_mask
);
600 if (INTEL_DEBUG
& DEBUG_BLIT
) {
601 DBG("swrast clear:");
602 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
603 if (swrast_mask
& (1 << i
))
604 DBG(" %s", buffer_names
[i
]);
608 _swrast_Clear(ctx
, swrast_mask
);
613 /* Emit wait for pending flips */
615 intel_wait_flips(struct intel_context
*intel
)
617 struct intel_framebuffer
*intel_fb
=
618 (struct intel_framebuffer
*) intel
->ctx
.DrawBuffer
;
619 struct intel_renderbuffer
*intel_rb
=
620 intel_get_renderbuffer(&intel_fb
->Base
,
621 intel_fb
->Base
._ColorDrawBufferIndexes
[0] ==
622 BUFFER_FRONT_LEFT
? BUFFER_FRONT_LEFT
:
625 if (intel_fb
->Base
.Name
== 0 && intel_rb
&&
626 intel_rb
->pf_pending
== intel_fb
->pf_seq
) {
627 GLint pf_planes
= intel_fb
->pf_planes
;
630 /* Wait for pending flips to take effect */
631 BEGIN_BATCH(2, NO_LOOP_CLIPRECTS
);
632 OUT_BATCH(pf_planes
& 0x1 ? (MI_WAIT_FOR_EVENT
| MI_WAIT_FOR_PLANE_A_FLIP
)
634 OUT_BATCH(pf_planes
& 0x2 ? (MI_WAIT_FOR_EVENT
| MI_WAIT_FOR_PLANE_B_FLIP
)
638 intel_rb
->pf_pending
--;
643 /* Flip the front & back buffers
646 intelPageFlip(const __DRIdrawablePrivate
* dPriv
)
649 struct intel_context
*intel
;
651 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
653 if (INTEL_DEBUG
& DEBUG_IOCTL
)
654 fprintf(stderr
, "%s\n", __FUNCTION__
);
657 assert(dPriv
->driContextPriv
);
658 assert(dPriv
->driContextPriv
->driverPrivate
);
660 intel
= (struct intel_context
*) dPriv
->driContextPriv
->driverPrivate
;
662 if (intel
->intelScreen
->drmMinor
< 9)
665 intelFlush(&intel
->ctx
);
669 LOCK_HARDWARE(intel
);
671 if (dPriv
->numClipRects
&& intel_fb
->pf_active
) {
672 drm_i915_flip_t flip
;
674 flip
.pipes
= intel_fb
->pf_planes
;
676 ret
= drmCommandWrite(intel
->driFd
, DRM_I915_FLIP
, &flip
, sizeof(flip
));
679 UNLOCK_HARDWARE(intel
);
681 if (ret
|| !intel_fb
->pf_active
)
684 if (!dPriv
->numClipRects
) {
685 usleep(10000); /* throttle invisible client 10ms */
688 intel_fb
->pf_current_page
= (intel
->sarea
->pf_current_page
>>
689 (intel_fb
->pf_planes
& 0x2)) & 0x3;
691 if (dPriv
->numClipRects
!= 0) {
692 intel_get_renderbuffer(&intel_fb
->Base
, BUFFER_FRONT_LEFT
)->pf_pending
=
693 intel_get_renderbuffer(&intel_fb
->Base
, BUFFER_BACK_LEFT
)->pf_pending
=
697 intel_flip_renderbuffers(intel_fb
);
698 intel_draw_buffer(&intel
->ctx
, &intel_fb
->Base
);
708 intelSwapBuffers(__DRIdrawablePrivate
* dPriv
)
710 if (dPriv
->driverPrivate
) {
711 const struct gl_framebuffer
*fb
712 = (struct gl_framebuffer
*) dPriv
->driverPrivate
;
713 if (fb
->Visual
.doubleBufferMode
) {
714 GET_CURRENT_CONTEXT(ctx
);
715 if (ctx
&& ctx
->DrawBuffer
== fb
) {
716 _mesa_notifySwapBuffers(ctx
); /* flush pending rendering */
718 if (intel
->doPageFlip
) {
719 intelPageFlip(dPriv
);
722 intelCopyBuffer(dPriv
);
728 "dPriv has no gl_framebuffer pointer in intelSwapBuffers");
736 intelScheduleSwap(__DRIdrawablePrivate
* dPriv
, GLboolean
*missed_target
)
738 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
739 unsigned int interval
;
740 struct intel_context
*intel
=
741 intelScreenContext(dPriv
->driScreenPriv
->private);
742 const intelScreenPrivate
*intelScreen
= intel
->intelScreen
;
744 drm_i915_vblank_swap_t swap
;
747 if (!dPriv
->vblFlags
||
748 (dPriv
->vblFlags
& VBLANK_FLAG_NO_IRQ
) ||
749 intelScreen
->drmMinor
< (intel_fb
->pf_active
? 9 : 6))
752 interval
= driGetVBlankInterval(dPriv
);
754 swap
.seqtype
= DRM_VBLANK_ABSOLUTE
;
756 if (dPriv
->vblFlags
& VBLANK_FLAG_SYNC
) {
757 swap
.seqtype
|= DRM_VBLANK_NEXTONMISS
;
758 } else if (interval
== 0) {
762 swap
.drawable
= dPriv
->hHWDrawable
;
763 target
= swap
.sequence
= dPriv
->vblSeq
+ interval
;
765 if ( dPriv
->vblFlags
& VBLANK_FLAG_SECONDARY
) {
766 swap
.seqtype
|= DRM_VBLANK_SECONDARY
;
769 LOCK_HARDWARE(intel
);
771 intel_batchbuffer_flush(intel
->batch
);
773 if ( intel_fb
->pf_active
) {
774 swap
.seqtype
|= DRM_VBLANK_FLIP
;
776 intel_fb
->pf_current_page
= (((intel
->sarea
->pf_current_page
>>
777 (intel_fb
->pf_planes
& 0x2)) & 0x3) + 1) %
778 intel_fb
->pf_num_pages
;
781 if (!drmCommandWriteRead(intel
->driFd
, DRM_I915_VBLANK_SWAP
, &swap
,
783 dPriv
->vblSeq
= swap
.sequence
;
784 swap
.sequence
-= target
;
785 *missed_target
= swap
.sequence
> 0 && swap
.sequence
<= (1 << 23);
787 intel_get_renderbuffer(&intel_fb
->Base
, BUFFER_BACK_LEFT
)->vbl_pending
=
788 intel_get_renderbuffer(&intel_fb
->Base
,
789 BUFFER_FRONT_LEFT
)->vbl_pending
=
792 if (swap
.seqtype
& DRM_VBLANK_FLIP
) {
793 intel_flip_renderbuffers(intel_fb
);
794 intel_draw_buffer(&intel
->ctx
, intel
->ctx
.DrawBuffer
);
799 if (swap
.seqtype
& DRM_VBLANK_FLIP
) {
800 intel_fb
->pf_current_page
= ((intel
->sarea
->pf_current_page
>>
801 (intel_fb
->pf_planes
& 0x2)) & 0x3) %
802 intel_fb
->pf_num_pages
;
808 UNLOCK_HARDWARE(intel
);
814 intelSwapBuffers(__DRIdrawablePrivate
* dPriv
)
816 __DRIscreenPrivate
*psp
= dPriv
->driScreenPriv
;
818 if (dPriv
->driContextPriv
&& dPriv
->driContextPriv
->driverPrivate
) {
819 GET_CURRENT_CONTEXT(ctx
);
820 struct intel_context
*intel
;
825 intel
= intel_context(ctx
);
827 if (ctx
->Visual
.doubleBufferMode
) {
828 GLboolean missed_target
;
829 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
832 _mesa_notifySwapBuffers(ctx
); /* flush pending rendering comands */
834 if (!intelScheduleSwap(dPriv
, &missed_target
)) {
835 driWaitForVBlank(dPriv
, &missed_target
);
837 if (!intelPageFlip(dPriv
)) {
838 intelCopyBuffer(dPriv
, NULL
);
842 intel_fb
->swap_count
++;
843 (*psp
->systemTime
->getUST
) (&ust
);
845 intel_fb
->swap_missed_count
++;
846 intel_fb
->swap_missed_ust
= ust
- intel_fb
->swap_ust
;
849 intel_fb
->swap_ust
= ust
;
853 /* XXX this shouldn't be an error but we can't handle it for now */
854 fprintf(stderr
, "%s: drawable has no context!\n", __FUNCTION__
);
860 intelCopySubBuffer(__DRIdrawablePrivate
* dPriv
, int x
, int y
, int w
, int h
)
862 if (dPriv
->driContextPriv
&& dPriv
->driContextPriv
->driverPrivate
) {
863 struct intel_context
*intel
=
864 (struct intel_context
*) dPriv
->driContextPriv
->driverPrivate
;
865 GLcontext
*ctx
= &intel
->ctx
;
867 if (ctx
->Visual
.doubleBufferMode
) {
868 drm_clip_rect_t rect
;
869 rect
.x1
= x
+ dPriv
->x
;
870 rect
.y1
= (dPriv
->h
- y
- h
) + dPriv
->y
;
871 rect
.x2
= rect
.x1
+ w
;
872 rect
.y2
= rect
.y1
+ h
;
873 _mesa_notifySwapBuffers(ctx
); /* flush pending rendering comands */
874 intelCopyBuffer(dPriv
, &rect
);
878 /* XXX this shouldn't be an error but we can't handle it for now */
879 fprintf(stderr
, "%s: drawable has no context!\n", __FUNCTION__
);
885 * Update the hardware state for drawing into a window or framebuffer object.
887 * Called by glDrawBuffer, glBindFramebufferEXT, MakeCurrent, and other
888 * places within the driver.
890 * Basically, this needs to be called any time the current framebuffer
891 * changes, the renderbuffers change, or we need to draw into different
895 intel_draw_buffer(GLcontext
* ctx
, struct gl_framebuffer
*fb
)
897 struct intel_context
*intel
= intel_context(ctx
);
898 struct intel_region
*colorRegions
[MAX_DRAW_BUFFERS
], *depthRegion
= NULL
;
899 struct intel_renderbuffer
*irbDepth
= NULL
, *irbStencil
= NULL
;
900 int front
= 0; /* drawing to front color buffer? */
903 /* this can happen during the initial context initialization */
907 /* Do this here, note core Mesa, since this function is called from
908 * many places within the driver.
910 if (ctx
->NewState
& (_NEW_BUFFERS
| _NEW_COLOR
| _NEW_PIXEL
)) {
911 /* this updates the DrawBuffer->_NumColorDrawBuffers fields, etc */
912 _mesa_update_framebuffer(ctx
);
913 /* this updates the DrawBuffer's Width/Height if it's a FBO */
914 _mesa_update_draw_buffer_bounds(ctx
);
917 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
918 /* this may occur when we're called by glBindFrameBuffer() during
919 * the process of someone setting up renderbuffers, etc.
921 /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/
926 intel_validate_paired_depth_stencil(ctx
, fb
);
928 /* If the batch contents require looping over cliprects, flush them before
929 * we go changing which cliprects get referenced when that happens.
931 if (intel
->batch
->cliprect_mode
== LOOP_CLIPRECTS
)
932 intel_batchbuffer_flush(intel
->batch
);
935 * How many color buffers are we drawing into?
937 if (fb
->_NumColorDrawBuffers
== 0) {
939 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_TRUE
);
940 colorRegions
[0] = NULL
;
943 intelSetRenderbufferClipRects(intel
);
944 } else if (fb
->_NumColorDrawBuffers
> 1) {
946 struct intel_renderbuffer
*irb
;
947 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_FALSE
);
950 intelSetRenderbufferClipRects(intel
);
951 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
952 irb
= intel_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
953 colorRegions
[i
] = (irb
&& irb
->region
) ? irb
->region
: NULL
;
957 /* draw to exactly one color buffer */
958 /*_mesa_debug(ctx, "Hardware rendering\n");*/
959 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_FALSE
);
960 if (fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
) {
965 * Get the intel_renderbuffer for the colorbuffer we're drawing into.
966 * And set up cliprects.
969 /* drawing to window system buffer */
971 intelSetFrontClipRects(intel
);
972 colorRegions
[0] = intel_get_rb_region(fb
, BUFFER_FRONT_LEFT
);
975 intelSetBackClipRects(intel
);
976 colorRegions
[0]= intel_get_rb_region(fb
, BUFFER_BACK_LEFT
);
980 /* drawing to user-created FBO */
981 struct intel_renderbuffer
*irb
;
982 intelSetRenderbufferClipRects(intel
);
983 irb
= intel_renderbuffer(fb
->_ColorDrawBuffers
[0]);
984 colorRegions
[0] = (irb
&& irb
->region
) ? irb
->region
: NULL
;
988 /* Update culling direction which changes depending on the
989 * orientation of the buffer:
991 if (ctx
->Driver
.FrontFace
)
992 ctx
->Driver
.FrontFace(ctx
, ctx
->Polygon
.FrontFace
);
994 ctx
->NewState
|= _NEW_POLYGON
;
996 if (!colorRegions
[0]) {
997 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_TRUE
);
1000 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_FALSE
);
1004 *** Get depth buffer region and check if we need a software fallback.
1005 *** Note that the depth buffer is usually a DEPTH_STENCIL buffer.
1007 if (fb
->_DepthBuffer
&& fb
->_DepthBuffer
->Wrapped
) {
1008 irbDepth
= intel_renderbuffer(fb
->_DepthBuffer
->Wrapped
);
1009 if (irbDepth
&& irbDepth
->region
) {
1010 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_FALSE
);
1011 depthRegion
= irbDepth
->region
;
1014 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_TRUE
);
1019 /* not using depth buffer */
1020 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_FALSE
);
1026 *** This can only be hardware accelerated if we're using a
1027 *** combined DEPTH_STENCIL buffer (for now anyway).
1029 if (fb
->_StencilBuffer
&& fb
->_StencilBuffer
->Wrapped
) {
1030 irbStencil
= intel_renderbuffer(fb
->_StencilBuffer
->Wrapped
);
1031 if (irbStencil
&& irbStencil
->region
) {
1032 ASSERT(irbStencil
->Base
._ActualFormat
== GL_DEPTH24_STENCIL8_EXT
);
1033 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_FALSE
);
1034 /* need to re-compute stencil hw state */
1035 if (ctx
->Driver
.Enable
!= NULL
)
1036 ctx
->Driver
.Enable(ctx
, GL_STENCIL_TEST
, ctx
->Stencil
.Enabled
);
1038 ctx
->NewState
|= _NEW_STENCIL
;
1040 depthRegion
= irbStencil
->region
;
1043 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_TRUE
);
1047 /* XXX FBO: instead of FALSE, pass ctx->Stencil.Enabled ??? */
1048 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_FALSE
);
1049 /* need to re-compute stencil hw state */
1050 if (ctx
->Driver
.Enable
!= NULL
)
1051 ctx
->Driver
.Enable(ctx
, GL_STENCIL_TEST
, ctx
->Stencil
.Enabled
);
1053 ctx
->NewState
|= _NEW_STENCIL
;
1057 * Update depth test state
1059 if (ctx
->Driver
.Enable
) {
1060 if (ctx
->Depth
.Test
&& fb
->Visual
.depthBits
> 0) {
1061 ctx
->Driver
.Enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1063 ctx
->Driver
.Enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
1066 ctx
->NewState
|= _NEW_DEPTH
;
1069 intel
->vtbl
.set_draw_region(intel
, colorRegions
, depthRegion
,
1070 fb
->_NumColorDrawBuffers
);
1072 /* update viewport since it depends on window size */
1073 if (ctx
->Driver
.Viewport
) {
1074 ctx
->Driver
.Viewport(ctx
, ctx
->Viewport
.X
, ctx
->Viewport
.Y
,
1075 ctx
->Viewport
.Width
, ctx
->Viewport
.Height
);
1077 ctx
->NewState
|= _NEW_VIEWPORT
;
1080 /* Set state we know depends on drawable parameters:
1082 if (ctx
->Driver
.Scissor
)
1083 ctx
->Driver
.Scissor(ctx
, ctx
->Scissor
.X
, ctx
->Scissor
.Y
,
1084 ctx
->Scissor
.Width
, ctx
->Scissor
.Height
);
1085 intel
->NewGLState
|= _NEW_SCISSOR
;
1087 if (ctx
->Driver
.DepthRange
)
1088 ctx
->Driver
.DepthRange(ctx
,
1095 intelDrawBuffer(GLcontext
* ctx
, GLenum mode
)
1097 intel_draw_buffer(ctx
, ctx
->DrawBuffer
);
1102 intelReadBuffer(GLcontext
* ctx
, GLenum mode
)
1104 if (ctx
->ReadBuffer
== ctx
->DrawBuffer
) {
1105 /* This will update FBO completeness status.
1106 * A framebuffer will be incomplete if the GL_READ_BUFFER setting
1107 * refers to a missing renderbuffer. Calling glReadBuffer can set
1108 * that straight and can make the drawing buffer complete.
1110 intel_draw_buffer(ctx
, ctx
->DrawBuffer
);
1112 /* Generally, functions which read pixels (glReadPixels, glCopyPixels, etc)
1113 * reference ctx->ReadBuffer and do appropriate state checks.
1119 intelInitBufferFuncs(struct dd_function_table
*functions
)
1121 functions
->Clear
= intelClear
;
1122 functions
->DrawBuffer
= intelDrawBuffer
;
1123 functions
->ReadBuffer
= intelReadBuffer
;