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_depthstencil.h"
33 #include "intel_fbo.h"
34 #include "intel_tris.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, \
64 * XXX move this into a new dri/common/cliprects.c file.
67 intel_intersect_cliprects(drm_clip_rect_t
* dst
,
68 const drm_clip_rect_t
* a
,
69 const drm_clip_rect_t
* b
)
73 GLint bw
= b
->x2
- bx
;
74 GLint bh
= b
->y2
- by
;
77 bw
-= a
->x1
- bx
, bx
= a
->x1
;
79 bh
-= a
->y1
- by
, by
= a
->y1
;
98 * Return pointer to current color drawing region, or NULL.
100 struct intel_region
*
101 intel_drawbuf_region(struct intel_context
*intel
)
103 struct intel_renderbuffer
*irbColor
=
104 intel_renderbuffer(intel
->ctx
.DrawBuffer
->_ColorDrawBuffers
[0][0]);
106 return irbColor
->region
;
112 * Return pointer to current color reading region, or NULL.
114 struct intel_region
*
115 intel_readbuf_region(struct intel_context
*intel
)
117 struct intel_renderbuffer
*irb
118 = intel_renderbuffer(intel
->ctx
.ReadBuffer
->_ColorReadBuffer
);
128 * Update the following fields for rendering to a user-created FBO:
129 * intel->numClipRects
135 intelSetRenderbufferClipRects(struct intel_context
*intel
)
137 assert(intel
->ctx
.DrawBuffer
->Width
> 0);
138 assert(intel
->ctx
.DrawBuffer
->Height
> 0);
139 intel
->fboRect
.x1
= 0;
140 intel
->fboRect
.y1
= 0;
141 intel
->fboRect
.x2
= intel
->ctx
.DrawBuffer
->Width
;
142 intel
->fboRect
.y2
= intel
->ctx
.DrawBuffer
->Height
;
143 intel
->numClipRects
= 1;
144 intel
->pClipRects
= &intel
->fboRect
;
151 * As above, but for rendering to front buffer of a window.
152 * \sa intelSetRenderbufferClipRects
155 intelSetFrontClipRects(struct intel_context
*intel
)
157 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
162 intel
->numClipRects
= dPriv
->numClipRects
;
163 intel
->pClipRects
= dPriv
->pClipRects
;
164 intel
->drawX
= dPriv
->x
;
165 intel
->drawY
= dPriv
->y
;
170 * As above, but for rendering to back buffer of a window.
173 intelSetBackClipRects(struct intel_context
*intel
)
175 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
176 struct intel_framebuffer
*intel_fb
;
181 intel_fb
= dPriv
->driverPrivate
;
183 if (intel_fb
->pf_active
|| dPriv
->numBackClipRects
== 0) {
184 /* use the front clip rects */
185 intel
->numClipRects
= dPriv
->numClipRects
;
186 intel
->pClipRects
= dPriv
->pClipRects
;
187 intel
->drawX
= dPriv
->x
;
188 intel
->drawY
= dPriv
->y
;
191 /* use the back clip rects */
192 intel
->numClipRects
= dPriv
->numBackClipRects
;
193 intel
->pClipRects
= dPriv
->pBackClipRects
;
194 intel
->drawX
= dPriv
->backX
;
195 intel
->drawY
= dPriv
->backY
;
201 * This will be called whenever the currently bound window is moved/resized.
202 * XXX: actually, it seems to NOT be called when the window is only moved (BP).
205 intelWindowMoved(struct intel_context
*intel
)
207 GLcontext
*ctx
= &intel
->ctx
;
208 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
209 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
211 if (!intel
->ctx
.DrawBuffer
) {
212 /* when would this happen? -BP */
213 intelSetFrontClipRects(intel
);
215 else if (intel
->ctx
.DrawBuffer
->Name
!= 0) {
216 /* drawing to user-created FBO - do nothing */
217 /* Cliprects would be set from intelDrawBuffer() */
220 /* drawing to a window */
221 switch (intel_fb
->Base
._ColorDrawBufferMask
[0]) {
222 case BUFFER_BIT_FRONT_LEFT
:
223 intelSetFrontClipRects(intel
);
225 case BUFFER_BIT_BACK_LEFT
:
226 intelSetBackClipRects(intel
);
229 /* glDrawBuffer(GL_NONE or GL_FRONT_AND_BACK): software fallback */
230 intelSetFrontClipRects(intel
);
234 if (intel
->intelScreen
->driScrnPriv
->ddx_version
.minor
>= 7) {
235 drmI830Sarea
*sarea
= intel
->sarea
;
236 drm_clip_rect_t drw_rect
= { .x1
= dPriv
->x
, .x2
= dPriv
->x
+ dPriv
->w
,
237 .y1
= dPriv
->y
, .y2
= dPriv
->y
+ dPriv
->h
};
238 drm_clip_rect_t planeA_rect
= { .x1
= sarea
->planeA_x
, .y1
= sarea
->planeA_y
,
239 .x2
= sarea
->planeA_x
+ sarea
->planeA_w
,
240 .y2
= sarea
->planeA_y
+ sarea
->planeA_h
};
241 drm_clip_rect_t planeB_rect
= { .x1
= sarea
->planeB_x
, .y1
= sarea
->planeB_y
,
242 .x2
= sarea
->planeB_x
+ sarea
->planeB_w
,
243 .y2
= sarea
->planeB_y
+ sarea
->planeB_h
};
244 GLint areaA
= driIntersectArea( drw_rect
, planeA_rect
);
245 GLint areaB
= driIntersectArea( drw_rect
, planeB_rect
);
246 GLuint flags
= dPriv
->vblFlags
;
250 /* Update page flipping info
260 intel_fb
->pf_current_page
= (intel
->sarea
->pf_current_page
>>
261 (intel_fb
->pf_planes
& 0x2)) & 0x3;
263 intel_fb
->pf_num_pages
= intel
->intelScreen
->third
.handle
? 3 : 2;
265 pf_active
= pf_planes
&& (pf_planes
& intel
->sarea
->pf_active
) == pf_planes
;
267 if (INTEL_DEBUG
& DEBUG_LOCK
)
268 if (pf_active
!= intel_fb
->pf_active
)
269 _mesa_printf("%s - Page flipping %sactive\n", __progname
,
270 pf_active
? "" : "in");
273 /* Sync pages between planes if flipping on both at the same time */
274 if (pf_planes
== 0x3 && pf_planes
!= intel_fb
->pf_planes
&&
275 (intel
->sarea
->pf_current_page
& 0x3) !=
276 (((intel
->sarea
->pf_current_page
) >> 2) & 0x3)) {
277 drm_i915_flip_t flip
;
279 if (intel_fb
->pf_current_page
==
280 (intel
->sarea
->pf_current_page
& 0x3)) {
281 /* XXX: This is ugly, but emitting two flips 'in a row' can cause
282 * lockups for unknown reasons.
284 intel
->sarea
->pf_current_page
=
285 intel
->sarea
->pf_current_page
& 0x3;
286 intel
->sarea
->pf_current_page
|=
287 ((intel_fb
->pf_current_page
+ intel_fb
->pf_num_pages
- 1) %
288 intel_fb
->pf_num_pages
) << 2;
292 intel
->sarea
->pf_current_page
=
293 intel
->sarea
->pf_current_page
& (0x3 << 2);
294 intel
->sarea
->pf_current_page
|=
295 (intel_fb
->pf_current_page
+ intel_fb
->pf_num_pages
- 1) %
296 intel_fb
->pf_num_pages
;
301 drmCommandWrite(intel
->driFd
, DRM_I915_FLIP
, &flip
, sizeof(flip
));
304 intel_fb
->pf_planes
= pf_planes
;
307 intel_fb
->pf_active
= pf_active
;
308 intel_flip_renderbuffers(intel_fb
);
309 intel_draw_buffer(&intel
->ctx
, intel
->ctx
.DrawBuffer
);
311 /* Update vblank info
313 if (areaB
> areaA
|| (areaA
== areaB
&& areaB
> 0)) {
314 flags
= dPriv
->vblFlags
| VBLANK_FLAG_SECONDARY
;
316 flags
= dPriv
->vblFlags
& ~VBLANK_FLAG_SECONDARY
;
319 /* Check to see if we changed pipes */
320 if (flags
!= dPriv
->vblFlags
&& dPriv
->vblFlags
&&
321 !(dPriv
->vblFlags
& VBLANK_FLAG_NO_IRQ
)) {
327 * Deal with page flipping
329 vbl
.request
.type
= DRM_VBLANK_ABSOLUTE
;
331 if ( dPriv
->vblFlags
& VBLANK_FLAG_SECONDARY
) {
332 vbl
.request
.type
|= DRM_VBLANK_SECONDARY
;
335 for (i
= 0; i
< intel_fb
->pf_num_pages
; i
++) {
336 if (!intel_fb
->color_rb
[i
] ||
337 (intel_fb
->vbl_waited
- intel_fb
->color_rb
[i
]->vbl_pending
) <=
341 vbl
.request
.sequence
= intel_fb
->color_rb
[i
]->vbl_pending
;
342 drmWaitVBlank(intel
->driFd
, &vbl
);
346 * Update msc_base from old pipe
348 driDrawableGetMSC32(dPriv
->driScreenPriv
, dPriv
, &count
);
349 dPriv
->msc_base
= count
;
351 * Then get new vblank_base and vblSeq values
353 dPriv
->vblFlags
= flags
;
354 driGetCurrentVBlank(dPriv
);
355 dPriv
->vblank_base
= dPriv
->vblSeq
;
357 intel_fb
->vbl_waited
= dPriv
->vblSeq
;
359 for (i
= 0; i
< intel_fb
->pf_num_pages
; i
++) {
360 if (intel_fb
->color_rb
[i
])
361 intel_fb
->color_rb
[i
]->vbl_pending
= intel_fb
->vbl_waited
;
365 dPriv
->vblFlags
&= ~VBLANK_FLAG_SECONDARY
;
368 /* Update Mesa's notion of window size */
369 driUpdateFramebufferSize(ctx
, dPriv
);
370 intel_fb
->Base
.Initialized
= GL_TRUE
; /* XXX remove someday */
372 /* Update hardware scissor */
373 ctx
->Driver
.Scissor(ctx
, ctx
->Scissor
.X
, ctx
->Scissor
.Y
,
374 ctx
->Scissor
.Width
, ctx
->Scissor
.Height
);
376 /* Re-calculate viewport related state */
377 ctx
->Driver
.DepthRange( ctx
, ctx
->Viewport
.Near
, ctx
->Viewport
.Far
);
382 /* A true meta version of this would be very simple and additionally
383 * machine independent. Maybe we'll get there one day.
386 intelClearWithTris(struct intel_context
*intel
, GLbitfield mask
)
388 GLcontext
*ctx
= &intel
->ctx
;
389 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
390 drm_clip_rect_t clear
;
392 if (INTEL_DEBUG
& DEBUG_BLIT
)
393 _mesa_printf("%s 0x%x\n", __FUNCTION__
, mask
);
395 LOCK_HARDWARE(intel
);
397 /* XXX FBO: was: intel->driDrawable->numClipRects */
398 if (intel
->numClipRects
) {
399 GLint cx
, cy
, cw
, ch
;
402 intel
->vtbl
.install_meta_state(intel
);
404 /* Get clear bounds after locking */
410 /* note: regardless of 'all', cx, cy, cw, ch are now correct */
416 /* Back and stencil cliprects are the same. Try and do both
420 (BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_STENCIL
| BUFFER_BIT_DEPTH
)) {
421 struct intel_region
*backRegion
=
422 intel_get_rb_region(fb
, BUFFER_BACK_LEFT
);
423 struct intel_region
*depthRegion
=
424 intel_get_rb_region(fb
, BUFFER_DEPTH
);
425 const GLuint clearColor
= (backRegion
&& backRegion
->cpp
== 4)
426 ? intel
->ClearColor8888
: intel
->ClearColor565
;
428 intel
->vtbl
.meta_draw_region(intel
, backRegion
, depthRegion
);
430 if (mask
& BUFFER_BIT_BACK_LEFT
)
431 intel
->vtbl
.meta_color_mask(intel
, GL_TRUE
);
433 intel
->vtbl
.meta_color_mask(intel
, GL_FALSE
);
435 if (mask
& BUFFER_BIT_STENCIL
)
436 intel
->vtbl
.meta_stencil_replace(intel
,
437 intel
->ctx
.Stencil
.WriteMask
[0],
438 intel
->ctx
.Stencil
.Clear
);
440 intel
->vtbl
.meta_no_stencil_write(intel
);
442 if (mask
& BUFFER_BIT_DEPTH
)
443 intel
->vtbl
.meta_depth_replace(intel
);
445 intel
->vtbl
.meta_no_depth_write(intel
);
447 /* XXX: Using INTEL_BATCH_NO_CLIPRECTS here is dangerous as the
448 * drawing origin may not be correctly emitted.
450 intel_meta_draw_quad(intel
, clear
.x1
, clear
.x2
, clear
.y1
, clear
.y2
, intel
->ctx
.Depth
.Clear
, clearColor
, 0, 0, 0, 0); /* texcoords */
453 ~(BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_STENCIL
| BUFFER_BIT_DEPTH
);
456 /* clear the remaining (color) renderbuffers */
457 for (buf
= 0; buf
< BUFFER_COUNT
&& mask
; buf
++) {
458 const GLuint bufBit
= 1 << buf
;
460 struct intel_renderbuffer
*irbColor
=
461 intel_renderbuffer(fb
->Attachment
[buf
].Renderbuffer
);
462 GLuint color
= (irbColor
->region
->cpp
== 4)
463 ? intel
->ClearColor8888
: intel
->ClearColor565
;
467 intel
->vtbl
.meta_no_depth_write(intel
);
468 intel
->vtbl
.meta_no_stencil_write(intel
);
469 intel
->vtbl
.meta_color_mask(intel
, GL_TRUE
);
470 intel
->vtbl
.meta_draw_region(intel
, irbColor
->region
, NULL
);
472 /* XXX: Using INTEL_BATCH_NO_CLIPRECTS here is dangerous as the
473 * drawing origin may not be correctly emitted.
475 intel_meta_draw_quad(intel
, clear
.x1
, clear
.x2
, clear
.y1
, clear
.y2
, 0, /* depth clear val */
476 color
, 0, 0, 0, 0); /* texcoords */
482 intel
->vtbl
.leave_meta_state(intel
);
483 intel_batchbuffer_flush(intel
->batch
);
485 UNLOCK_HARDWARE(intel
);
489 * Called by ctx->Driver.Clear.
492 intelClear(GLcontext
*ctx
, GLbitfield mask
)
494 struct intel_context
*intel
= intel_context(ctx
);
495 const GLuint colorMask
= *((GLuint
*) & ctx
->Color
.ColorMask
);
496 GLbitfield tri_mask
= 0;
497 GLbitfield blit_mask
= 0;
498 GLbitfield swrast_mask
= 0;
499 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
503 fprintf(stderr
, "%s\n", __FUNCTION__
);
505 /* HW color buffers (front, back, aux, generic FBO, etc) */
506 if (colorMask
== ~0) {
507 /* clear all R,G,B,A */
508 /* XXX FBO: need to check if colorbuffers are software RBOs! */
509 blit_mask
|= (mask
& BUFFER_BITS_COLOR
);
512 /* glColorMask in effect */
513 tri_mask
|= (mask
& BUFFER_BITS_COLOR
);
517 if (mask
& BUFFER_BIT_STENCIL
) {
518 const struct intel_region
*stencilRegion
519 = intel_get_rb_region(fb
, BUFFER_STENCIL
);
521 /* have hw stencil */
522 if ((ctx
->Stencil
.WriteMask
[0] & 0xff) != 0xff) {
523 /* not clearing all stencil bits, so use triangle clearing */
524 tri_mask
|= BUFFER_BIT_STENCIL
;
527 /* clearing all stencil bits, use blitting */
528 blit_mask
|= BUFFER_BIT_STENCIL
;
534 if (mask
& BUFFER_BIT_DEPTH
) {
535 /* clear depth with whatever method is used for stencil (see above) */
536 if (tri_mask
& BUFFER_BIT_STENCIL
)
537 tri_mask
|= BUFFER_BIT_DEPTH
;
539 blit_mask
|= BUFFER_BIT_DEPTH
;
542 /* SW fallback clearing */
543 swrast_mask
= mask
& ~tri_mask
& ~blit_mask
;
545 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
546 GLuint bufBit
= 1 << i
;
547 if ((blit_mask
| tri_mask
) & bufBit
) {
548 if (!fb
->Attachment
[i
].Renderbuffer
->ClassID
) {
549 blit_mask
&= ~bufBit
;
551 swrast_mask
|= bufBit
;
557 intelFlush(ctx
); /* XXX intelClearWithBlit also does this */
560 intelClearWithBlit(ctx
, blit_mask
);
563 intelClearWithTris(intel
, tri_mask
);
566 _swrast_Clear(ctx
, swrast_mask
);
570 /* Emit wait for pending flips */
572 intel_wait_flips(struct intel_context
*intel
, GLuint batch_flags
)
574 struct intel_framebuffer
*intel_fb
=
575 (struct intel_framebuffer
*) intel
->ctx
.DrawBuffer
;
576 struct intel_renderbuffer
*intel_rb
=
577 intel_get_renderbuffer(&intel_fb
->Base
,
578 intel_fb
->Base
._ColorDrawBufferMask
[0] ==
579 BUFFER_BIT_FRONT_LEFT
? BUFFER_FRONT_LEFT
:
582 if (intel_fb
->Base
.Name
== 0 && intel_rb
->pf_pending
== intel_fb
->pf_seq
) {
583 GLint pf_planes
= intel_fb
->pf_planes
;
586 /* Wait for pending flips to take effect */
587 BEGIN_BATCH(2, batch_flags
);
588 OUT_BATCH(pf_planes
& 0x1 ? (MI_WAIT_FOR_EVENT
| MI_WAIT_FOR_PLANE_A_FLIP
)
590 OUT_BATCH(pf_planes
& 0x2 ? (MI_WAIT_FOR_EVENT
| MI_WAIT_FOR_PLANE_B_FLIP
)
594 intel_rb
->pf_pending
--;
599 /* Flip the front & back buffers
602 intelPageFlip(const __DRIdrawablePrivate
* dPriv
)
604 struct intel_context
*intel
;
606 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
608 if (INTEL_DEBUG
& DEBUG_IOCTL
)
609 fprintf(stderr
, "%s\n", __FUNCTION__
);
612 assert(dPriv
->driContextPriv
);
613 assert(dPriv
->driContextPriv
->driverPrivate
);
615 intel
= (struct intel_context
*) dPriv
->driContextPriv
->driverPrivate
;
617 if (intel
->intelScreen
->drmMinor
< 9)
620 intelFlush(&intel
->ctx
);
624 LOCK_HARDWARE(intel
);
626 if (dPriv
->numClipRects
&& intel_fb
->pf_active
) {
627 drm_i915_flip_t flip
;
629 flip
.pipes
= intel_fb
->pf_planes
;
631 ret
= drmCommandWrite(intel
->driFd
, DRM_I915_FLIP
, &flip
, sizeof(flip
));
634 UNLOCK_HARDWARE(intel
);
636 if (ret
|| !intel_fb
->pf_active
)
639 if (!dPriv
->numClipRects
) {
640 usleep(10000); /* throttle invisible client 10ms */
643 intel_fb
->pf_current_page
= (intel
->sarea
->pf_current_page
>>
644 (intel_fb
->pf_planes
& 0x2)) & 0x3;
646 if (dPriv
->numClipRects
!= 0) {
647 intel_get_renderbuffer(&intel_fb
->Base
, BUFFER_FRONT_LEFT
)->pf_pending
=
648 intel_get_renderbuffer(&intel_fb
->Base
, BUFFER_BACK_LEFT
)->pf_pending
=
652 intel_flip_renderbuffers(intel_fb
);
653 intel_draw_buffer(&intel
->ctx
, &intel_fb
->Base
);
660 intelSwapBuffers(__DRIdrawablePrivate
* dPriv
)
662 if (dPriv
->driverPrivate
) {
663 const struct gl_framebuffer
*fb
664 = (struct gl_framebuffer
*) dPriv
->driverPrivate
;
665 if (fb
->Visual
.doubleBufferMode
) {
666 GET_CURRENT_CONTEXT(ctx
);
667 if (ctx
&& ctx
->DrawBuffer
== fb
) {
668 _mesa_notifySwapBuffers(ctx
); /* flush pending rendering */
670 if (intel
->doPageFlip
) {
671 intelPageFlip(dPriv
);
674 intelCopyBuffer(dPriv
);
680 "dPriv has no gl_framebuffer pointer in intelSwapBuffers");
688 intelScheduleSwap(__DRIdrawablePrivate
* dPriv
, GLboolean
*missed_target
)
690 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
691 unsigned int interval
;
692 struct intel_context
*intel
=
693 intelScreenContext(dPriv
->driScreenPriv
->private);
694 const intelScreenPrivate
*intelScreen
= intel
->intelScreen
;
696 drm_i915_vblank_swap_t swap
;
699 if (!dPriv
->vblFlags
||
700 (dPriv
->vblFlags
& VBLANK_FLAG_NO_IRQ
) ||
701 intelScreen
->drmMinor
< (intel_fb
->pf_active
? 9 : 6))
704 interval
= driGetVBlankInterval(dPriv
);
706 swap
.seqtype
= DRM_VBLANK_ABSOLUTE
;
708 if (dPriv
->vblFlags
& VBLANK_FLAG_SYNC
) {
709 swap
.seqtype
|= DRM_VBLANK_NEXTONMISS
;
710 } else if (interval
== 0) {
714 swap
.drawable
= dPriv
->hHWDrawable
;
715 target
= swap
.sequence
= dPriv
->vblSeq
+ interval
;
717 if ( dPriv
->vblFlags
& VBLANK_FLAG_SECONDARY
) {
718 swap
.seqtype
|= DRM_VBLANK_SECONDARY
;
721 LOCK_HARDWARE(intel
);
723 intel_batchbuffer_flush(intel
->batch
);
725 if ( intel_fb
->pf_active
) {
726 swap
.seqtype
|= DRM_VBLANK_FLIP
;
728 intel_fb
->pf_current_page
= (((intel
->sarea
->pf_current_page
>>
729 (intel_fb
->pf_planes
& 0x2)) & 0x3) + 1) %
730 intel_fb
->pf_num_pages
;
733 if (!drmCommandWriteRead(intel
->driFd
, DRM_I915_VBLANK_SWAP
, &swap
,
735 dPriv
->vblSeq
= swap
.sequence
;
736 swap
.sequence
-= target
;
737 *missed_target
= swap
.sequence
> 0 && swap
.sequence
<= (1 << 23);
739 intel_get_renderbuffer(&intel_fb
->Base
, BUFFER_BACK_LEFT
)->vbl_pending
=
740 intel_get_renderbuffer(&intel_fb
->Base
,
741 BUFFER_FRONT_LEFT
)->vbl_pending
=
744 if (swap
.seqtype
& DRM_VBLANK_FLIP
) {
745 intel_flip_renderbuffers(intel_fb
);
746 intel_draw_buffer(&intel
->ctx
, intel
->ctx
.DrawBuffer
);
751 if (swap
.seqtype
& DRM_VBLANK_FLIP
) {
752 intel_fb
->pf_current_page
= ((intel
->sarea
->pf_current_page
>>
753 (intel_fb
->pf_planes
& 0x2)) & 0x3) %
754 intel_fb
->pf_num_pages
;
760 UNLOCK_HARDWARE(intel
);
766 intelSwapBuffers(__DRIdrawablePrivate
* dPriv
)
768 if (dPriv
->driContextPriv
&& dPriv
->driContextPriv
->driverPrivate
) {
769 GET_CURRENT_CONTEXT(ctx
);
770 struct intel_context
*intel
;
775 intel
= intel_context(ctx
);
777 if (ctx
->Visual
.doubleBufferMode
) {
778 intelScreenPrivate
*screen
= intel
->intelScreen
;
779 GLboolean missed_target
;
780 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
783 _mesa_notifySwapBuffers(ctx
); /* flush pending rendering comands */
785 if (!intelScheduleSwap(dPriv
, &missed_target
)) {
786 driWaitForVBlank(dPriv
, &missed_target
);
788 if (!intelPageFlip(dPriv
)) {
789 intelCopyBuffer(dPriv
, NULL
);
793 intel_fb
->swap_count
++;
794 (*dri_interface
->getUST
) (&ust
);
796 intel_fb
->swap_missed_count
++;
797 intel_fb
->swap_missed_ust
= ust
- intel_fb
->swap_ust
;
800 intel_fb
->swap_ust
= ust
;
804 /* XXX this shouldn't be an error but we can't handle it for now */
805 fprintf(stderr
, "%s: drawable has no context!\n", __FUNCTION__
);
811 intelCopySubBuffer(__DRIdrawablePrivate
* dPriv
, int x
, int y
, int w
, int h
)
813 if (dPriv
->driContextPriv
&& dPriv
->driContextPriv
->driverPrivate
) {
814 struct intel_context
*intel
=
815 (struct intel_context
*) dPriv
->driContextPriv
->driverPrivate
;
816 GLcontext
*ctx
= &intel
->ctx
;
818 if (ctx
->Visual
.doubleBufferMode
) {
819 drm_clip_rect_t rect
;
820 rect
.x1
= x
+ dPriv
->x
;
821 rect
.y1
= (dPriv
->h
- y
- h
) + dPriv
->y
;
822 rect
.x2
= rect
.x1
+ w
;
823 rect
.y2
= rect
.y1
+ h
;
824 _mesa_notifySwapBuffers(ctx
); /* flush pending rendering comands */
825 intelCopyBuffer(dPriv
, &rect
);
829 /* XXX this shouldn't be an error but we can't handle it for now */
830 fprintf(stderr
, "%s: drawable has no context!\n", __FUNCTION__
);
836 * Update the hardware state for drawing into a window or framebuffer object.
838 * Called by glDrawBuffer, glBindFramebufferEXT, MakeCurrent, and other
839 * places within the driver.
841 * Basically, this needs to be called any time the current framebuffer
842 * changes, the renderbuffers change, or we need to draw into different
846 intel_draw_buffer(GLcontext
* ctx
, struct gl_framebuffer
*fb
)
848 struct intel_context
*intel
= intel_context(ctx
);
849 struct intel_region
*colorRegion
, *depthRegion
= NULL
;
850 struct intel_renderbuffer
*irbDepth
= NULL
, *irbStencil
= NULL
;
851 int front
= 0; /* drawing to front color buffer? */
854 /* this can happen during the initial context initialization */
858 /* Do this here, note core Mesa, since this function is called from
859 * many places within the driver.
861 if (ctx
->NewState
& (_NEW_BUFFERS
| _NEW_COLOR
| _NEW_PIXEL
)) {
862 /* this updates the DrawBuffer->_NumColorDrawBuffers fields, etc */
863 _mesa_update_framebuffer(ctx
);
864 /* this updates the DrawBuffer's Width/Height if it's a FBO */
865 _mesa_update_draw_buffer_bounds(ctx
);
868 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
869 /* this may occur when we're called by glBindFrameBuffer() during
870 * the process of someone setting up renderbuffers, etc.
872 /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/
877 intel_validate_paired_depth_stencil(ctx
, fb
);
880 * How many color buffers are we drawing into?
882 if (fb
->_NumColorDrawBuffers
[0] != 1
884 /* XXX FBO temporary - always use software rendering */
888 /* writing to 0 or 2 or 4 color buffers */
889 /*_mesa_debug(ctx, "Software rendering\n");*/
890 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_TRUE
);
891 front
= 1; /* might not have back color buffer */
894 /* draw to exactly one color buffer */
895 /*_mesa_debug(ctx, "Hardware rendering\n");*/
896 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_FALSE
);
897 if (fb
->_ColorDrawBufferMask
[0] == BUFFER_BIT_FRONT_LEFT
) {
903 * Get the intel_renderbuffer for the colorbuffer we're drawing into.
904 * And set up cliprects.
907 /* drawing to window system buffer */
909 intelSetFrontClipRects(intel
);
910 colorRegion
= intel_get_rb_region(fb
, BUFFER_FRONT_LEFT
);
913 intelSetBackClipRects(intel
);
914 colorRegion
= intel_get_rb_region(fb
, BUFFER_BACK_LEFT
);
918 /* drawing to user-created FBO */
919 struct intel_renderbuffer
*irb
;
920 intelSetRenderbufferClipRects(intel
);
921 irb
= intel_renderbuffer(fb
->_ColorDrawBuffers
[0][0]);
922 colorRegion
= (irb
&& irb
->region
) ? irb
->region
: NULL
;
925 /* Update culling direction which changes depending on the
926 * orientation of the buffer:
928 if (ctx
->Driver
.FrontFace
)
929 ctx
->Driver
.FrontFace(ctx
, ctx
->Polygon
.FrontFace
);
931 ctx
->NewState
|= _NEW_POLYGON
;
934 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_TRUE
);
937 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_FALSE
);
941 *** Get depth buffer region and check if we need a software fallback.
942 *** Note that the depth buffer is usually a DEPTH_STENCIL buffer.
944 if (fb
->_DepthBuffer
&& fb
->_DepthBuffer
->Wrapped
) {
945 irbDepth
= intel_renderbuffer(fb
->_DepthBuffer
->Wrapped
);
946 if (irbDepth
&& irbDepth
->region
) {
947 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_FALSE
);
948 depthRegion
= irbDepth
->region
;
951 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_TRUE
);
956 /* not using depth buffer */
957 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_FALSE
);
963 *** This can only be hardware accelerated if we're using a
964 *** combined DEPTH_STENCIL buffer (for now anyway).
966 if (fb
->_StencilBuffer
&& fb
->_StencilBuffer
->Wrapped
) {
967 irbStencil
= intel_renderbuffer(fb
->_StencilBuffer
->Wrapped
);
968 if (irbStencil
&& irbStencil
->region
) {
969 ASSERT(irbStencil
->Base
._ActualFormat
== GL_DEPTH24_STENCIL8_EXT
);
970 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_FALSE
);
971 /* need to re-compute stencil hw state */
972 ctx
->Driver
.Enable(ctx
, GL_STENCIL_TEST
, ctx
->Stencil
.Enabled
);
974 depthRegion
= irbStencil
->region
;
977 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_TRUE
);
981 /* XXX FBO: instead of FALSE, pass ctx->Stencil.Enabled ??? */
982 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_FALSE
);
983 /* need to re-compute stencil hw state */
984 ctx
->Driver
.Enable(ctx
, GL_STENCIL_TEST
, ctx
->Stencil
.Enabled
);
988 * Update depth test state
990 if (ctx
->Depth
.Test
&& fb
->Visual
.depthBits
> 0) {
991 ctx
->Driver
.Enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
994 ctx
->Driver
.Enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
998 ** Release old regions, reference new regions
1000 #if 0 /* XXX FBO: this seems to be redundant with i915_state_draw_region() */
1001 if (intel
->draw_region
!= colorRegion
) {
1002 intel_region_release(&intel
->draw_region
);
1003 intel_region_reference(&intel
->draw_region
, colorRegion
);
1005 if (intel
->intelScreen
->depth_region
!= depthRegion
) {
1006 intel_region_release(&intel
->intelScreen
->depth_region
);
1007 intel_region_reference(&intel
->intelScreen
->depth_region
, depthRegion
);
1011 intel
->vtbl
.set_draw_region(intel
, colorRegion
, depthRegion
);
1013 /* update viewport since it depends on window size */
1014 ctx
->Driver
.Viewport(ctx
, ctx
->Viewport
.X
, ctx
->Viewport
.Y
,
1015 ctx
->Viewport
.Width
, ctx
->Viewport
.Height
);
1017 /* Update hardware scissor */
1018 ctx
->Driver
.Scissor(ctx
, ctx
->Scissor
.X
, ctx
->Scissor
.Y
,
1019 ctx
->Scissor
.Width
, ctx
->Scissor
.Height
);
1024 intelDrawBuffer(GLcontext
* ctx
, GLenum mode
)
1026 intel_draw_buffer(ctx
, ctx
->DrawBuffer
);
1031 intelReadBuffer(GLcontext
* ctx
, GLenum mode
)
1033 if (ctx
->ReadBuffer
== ctx
->DrawBuffer
) {
1034 /* This will update FBO completeness status.
1035 * A framebuffer will be incomplete if the GL_READ_BUFFER setting
1036 * refers to a missing renderbuffer. Calling glReadBuffer can set
1037 * that straight and can make the drawing buffer complete.
1039 intel_draw_buffer(ctx
, ctx
->DrawBuffer
);
1041 /* Generally, functions which read pixels (glReadPixels, glCopyPixels, etc)
1042 * reference ctx->ReadBuffer and do appropriate state checks.
1048 intelInitBufferFuncs(struct dd_function_table
*functions
)
1050 functions
->Clear
= intelClear
;
1051 functions
->DrawBuffer
= intelDrawBuffer
;
1052 functions
->ReadBuffer
= intelReadBuffer
;