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
;
201 intelUpdatePageFlipping(struct intel_context
*intel
,
202 GLint areaA
, GLint areaB
)
204 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
205 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
209 /* Update page flipping info */
218 intel_fb
->pf_current_page
= (intel
->sarea
->pf_current_page
>>
219 (intel_fb
->pf_planes
& 0x2)) & 0x3;
221 intel_fb
->pf_num_pages
= intel
->intelScreen
->third
.handle
? 3 : 2;
223 pf_active
= pf_planes
&& (pf_planes
& intel
->sarea
->pf_active
) == pf_planes
;
225 if (INTEL_DEBUG
& DEBUG_LOCK
)
226 if (pf_active
!= intel_fb
->pf_active
)
227 _mesa_printf("%s - Page flipping %sactive\n", __progname
,
228 pf_active
? "" : "in");
231 /* Sync pages between planes if flipping on both at the same time */
232 if (pf_planes
== 0x3 && pf_planes
!= intel_fb
->pf_planes
&&
233 (intel
->sarea
->pf_current_page
& 0x3) !=
234 (((intel
->sarea
->pf_current_page
) >> 2) & 0x3)) {
235 drm_i915_flip_t flip
;
237 if (intel_fb
->pf_current_page
==
238 (intel
->sarea
->pf_current_page
& 0x3)) {
239 /* XXX: This is ugly, but emitting two flips 'in a row' can cause
240 * lockups for unknown reasons.
242 intel
->sarea
->pf_current_page
=
243 intel
->sarea
->pf_current_page
& 0x3;
244 intel
->sarea
->pf_current_page
|=
245 ((intel_fb
->pf_current_page
+ intel_fb
->pf_num_pages
- 1) %
246 intel_fb
->pf_num_pages
) << 2;
250 intel
->sarea
->pf_current_page
=
251 intel
->sarea
->pf_current_page
& (0x3 << 2);
252 intel
->sarea
->pf_current_page
|=
253 (intel_fb
->pf_current_page
+ intel_fb
->pf_num_pages
- 1) %
254 intel_fb
->pf_num_pages
;
259 drmCommandWrite(intel
->driFd
, DRM_I915_FLIP
, &flip
, sizeof(flip
));
262 intel_fb
->pf_planes
= pf_planes
;
265 intel_fb
->pf_active
= pf_active
;
266 intel_flip_renderbuffers(intel_fb
);
267 intel_draw_buffer(&intel
->ctx
, intel
->ctx
.DrawBuffer
);
271 * This will be called whenever the currently bound window is moved/resized.
272 * XXX: actually, it seems to NOT be called when the window is only moved (BP).
275 intelWindowMoved(struct intel_context
*intel
)
277 GLcontext
*ctx
= &intel
->ctx
;
278 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
279 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
281 if (!intel
->ctx
.DrawBuffer
) {
282 /* when would this happen? -BP */
283 intelSetFrontClipRects(intel
);
285 else if (intel
->ctx
.DrawBuffer
->Name
!= 0) {
286 /* drawing to user-created FBO - do nothing */
287 /* Cliprects would be set from intelDrawBuffer() */
290 /* drawing to a window */
291 switch (intel_fb
->Base
._ColorDrawBufferIndexes
[0]) {
292 case BUFFER_FRONT_LEFT
:
293 intelSetFrontClipRects(intel
);
295 case BUFFER_BACK_LEFT
:
296 intelSetBackClipRects(intel
);
299 intelSetFrontClipRects(intel
);
304 if (!intel
->intelScreen
->driScrnPriv
->dri2
.enabled
&&
305 intel
->intelScreen
->driScrnPriv
->ddx_version
.minor
>= 7) {
306 volatile struct drm_i915_sarea
*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
;
319 intelUpdatePageFlipping(intel
, areaA
, areaB
);
321 /* Update vblank info
323 if (areaB
> areaA
|| (areaA
== areaB
&& areaB
> 0)) {
324 flags
= dPriv
->vblFlags
| VBLANK_FLAG_SECONDARY
;
326 flags
= dPriv
->vblFlags
& ~VBLANK_FLAG_SECONDARY
;
329 /* Check to see if we changed pipes */
330 if (flags
!= dPriv
->vblFlags
&& dPriv
->vblFlags
&&
331 !(dPriv
->vblFlags
& VBLANK_FLAG_NO_IRQ
)) {
337 * Deal with page flipping
339 vbl
.request
.type
= DRM_VBLANK_ABSOLUTE
;
341 if ( dPriv
->vblFlags
& VBLANK_FLAG_SECONDARY
) {
342 vbl
.request
.type
|= DRM_VBLANK_SECONDARY
;
345 for (i
= 0; i
< intel_fb
->pf_num_pages
; i
++) {
346 if (!intel_fb
->color_rb
[i
] ||
347 (intel_fb
->vbl_waited
- intel_fb
->color_rb
[i
]->vbl_pending
) <=
351 vbl
.request
.sequence
= intel_fb
->color_rb
[i
]->vbl_pending
;
352 drmWaitVBlank(intel
->driFd
, &vbl
);
356 * Update msc_base from old pipe
358 driDrawableGetMSC32(dPriv
->driScreenPriv
, dPriv
, &count
);
359 dPriv
->msc_base
= count
;
361 * Then get new vblank_base and vblSeq values
363 dPriv
->vblFlags
= flags
;
364 driGetCurrentVBlank(dPriv
);
365 dPriv
->vblank_base
= dPriv
->vblSeq
;
367 intel_fb
->vbl_waited
= dPriv
->vblSeq
;
369 for (i
= 0; i
< intel_fb
->pf_num_pages
; i
++) {
370 if (intel_fb
->color_rb
[i
])
371 intel_fb
->color_rb
[i
]->vbl_pending
= intel_fb
->vbl_waited
;
375 dPriv
->vblFlags
&= ~VBLANK_FLAG_SECONDARY
;
378 /* Update Mesa's notion of window size */
379 driUpdateFramebufferSize(ctx
, dPriv
);
380 intel_fb
->Base
.Initialized
= GL_TRUE
; /* XXX remove someday */
382 /* Update hardware scissor */
383 if (ctx
->Driver
.Scissor
!= NULL
) {
384 ctx
->Driver
.Scissor(ctx
, ctx
->Scissor
.X
, ctx
->Scissor
.Y
,
385 ctx
->Scissor
.Width
, ctx
->Scissor
.Height
);
388 /* Re-calculate viewport related state */
389 if (ctx
->Driver
.DepthRange
!= NULL
)
390 ctx
->Driver
.DepthRange( ctx
, ctx
->Viewport
.Near
, ctx
->Viewport
.Far
);
395 /* A true meta version of this would be very simple and additionally
396 * machine independent. Maybe we'll get there one day.
399 intelClearWithTris(struct intel_context
*intel
, GLbitfield mask
)
401 GLcontext
*ctx
= &intel
->ctx
;
402 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
405 intel
->vtbl
.install_meta_state(intel
);
407 /* Back and stencil cliprects are the same. Try and do both
410 if (mask
& (BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_STENCIL
| BUFFER_BIT_DEPTH
)) {
411 struct intel_region
*backRegion
=
412 intel_get_rb_region(fb
, BUFFER_BACK_LEFT
);
413 struct intel_region
*depthRegion
=
414 intel_get_rb_region(fb
, BUFFER_DEPTH
);
416 intel
->vtbl
.meta_draw_region(intel
, backRegion
, depthRegion
);
418 if (mask
& BUFFER_BIT_BACK_LEFT
)
419 intel
->vtbl
.meta_color_mask(intel
, GL_TRUE
);
421 intel
->vtbl
.meta_color_mask(intel
, GL_FALSE
);
423 if (mask
& BUFFER_BIT_STENCIL
)
424 intel
->vtbl
.meta_stencil_replace(intel
,
425 intel
->ctx
.Stencil
.WriteMask
[0],
426 intel
->ctx
.Stencil
.Clear
);
428 intel
->vtbl
.meta_no_stencil_write(intel
);
430 if (mask
& BUFFER_BIT_DEPTH
)
431 intel
->vtbl
.meta_depth_replace(intel
);
433 intel
->vtbl
.meta_no_depth_write(intel
);
435 intel
->vtbl
.meta_draw_quad(intel
,
440 intel
->ctx
.Depth
.Clear
,
441 intel
->ClearColor8888
,
442 0, 0, 0, 0); /* texcoords */
444 mask
&= ~(BUFFER_BIT_BACK_LEFT
| BUFFER_BIT_STENCIL
| BUFFER_BIT_DEPTH
);
447 /* clear the remaining (color) renderbuffers */
448 for (buf
= 0; buf
< BUFFER_COUNT
&& mask
; buf
++) {
449 const GLuint bufBit
= 1 << buf
;
451 struct intel_renderbuffer
*irbColor
=
452 intel_renderbuffer(fb
->Attachment
[buf
].Renderbuffer
);
456 intel
->vtbl
.meta_no_depth_write(intel
);
457 intel
->vtbl
.meta_no_stencil_write(intel
);
458 intel
->vtbl
.meta_color_mask(intel
, GL_TRUE
);
459 intel
->vtbl
.meta_draw_region(intel
, irbColor
->region
, NULL
);
461 intel
->vtbl
.meta_draw_quad(intel
,
466 0, intel
->ClearColor8888
,
467 0, 0, 0, 0); /* texcoords */
473 intel
->vtbl
.leave_meta_state(intel
);
476 static const char *buffer_names
[] = {
477 [BUFFER_FRONT_LEFT
] = "front",
478 [BUFFER_BACK_LEFT
] = "back",
479 [BUFFER_FRONT_RIGHT
] = "front right",
480 [BUFFER_BACK_RIGHT
] = "back right",
481 [BUFFER_AUX0
] = "aux0",
482 [BUFFER_AUX1
] = "aux1",
483 [BUFFER_AUX2
] = "aux2",
484 [BUFFER_AUX3
] = "aux3",
485 [BUFFER_DEPTH
] = "depth",
486 [BUFFER_STENCIL
] = "stencil",
487 [BUFFER_ACCUM
] = "accum",
488 [BUFFER_COLOR0
] = "color0",
489 [BUFFER_COLOR1
] = "color1",
490 [BUFFER_COLOR2
] = "color2",
491 [BUFFER_COLOR3
] = "color3",
492 [BUFFER_COLOR4
] = "color4",
493 [BUFFER_COLOR5
] = "color5",
494 [BUFFER_COLOR6
] = "color6",
495 [BUFFER_COLOR7
] = "color7",
499 * Called by ctx->Driver.Clear.
502 intelClear(GLcontext
*ctx
, GLbitfield mask
)
504 struct intel_context
*intel
= intel_context(ctx
);
505 const GLuint colorMask
= *((GLuint
*) & ctx
->Color
.ColorMask
);
506 GLbitfield tri_mask
= 0;
507 GLbitfield blit_mask
= 0;
508 GLbitfield swrast_mask
= 0;
509 struct gl_framebuffer
*fb
= ctx
->DrawBuffer
;
513 fprintf(stderr
, "%s\n", __FUNCTION__
);
515 /* HW color buffers (front, back, aux, generic FBO, etc) */
516 if (colorMask
== ~0) {
517 /* clear all R,G,B,A */
518 /* XXX FBO: need to check if colorbuffers are software RBOs! */
519 blit_mask
|= (mask
& BUFFER_BITS_COLOR
);
522 /* glColorMask in effect */
523 tri_mask
|= (mask
& BUFFER_BITS_COLOR
);
527 if (mask
& BUFFER_BIT_STENCIL
) {
528 const struct intel_region
*stencilRegion
529 = intel_get_rb_region(fb
, BUFFER_STENCIL
);
531 /* have hw stencil */
532 if (IS_965(intel
->intelScreen
->deviceID
) ||
533 (ctx
->Stencil
.WriteMask
[0] & 0xff) != 0xff) {
534 /* We have to use the 3D engine if we're clearing a partial mask
535 * of the stencil buffer, or if we're on a 965 which has a tiled
536 * depth/stencil buffer in a layout we can't blit to.
538 tri_mask
|= BUFFER_BIT_STENCIL
;
541 /* clearing all stencil bits, use blitting */
542 blit_mask
|= BUFFER_BIT_STENCIL
;
548 if (mask
& BUFFER_BIT_DEPTH
) {
549 /* clear depth with whatever method is used for stencil (see above) */
550 if (IS_965(intel
->intelScreen
->deviceID
) ||
551 tri_mask
& BUFFER_BIT_STENCIL
)
552 tri_mask
|= BUFFER_BIT_DEPTH
;
554 blit_mask
|= BUFFER_BIT_DEPTH
;
557 /* SW fallback clearing */
558 swrast_mask
= mask
& ~tri_mask
& ~blit_mask
;
560 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
561 GLuint bufBit
= 1 << i
;
562 if ((blit_mask
| tri_mask
) & bufBit
) {
563 if (!fb
->Attachment
[i
].Renderbuffer
->ClassID
) {
564 blit_mask
&= ~bufBit
;
566 swrast_mask
|= bufBit
;
572 if (INTEL_DEBUG
& DEBUG_BLIT
) {
574 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
575 if (blit_mask
& (1 << i
))
576 DBG(" %s", buffer_names
[i
]);
580 intelClearWithBlit(ctx
, blit_mask
);
584 if (INTEL_DEBUG
& DEBUG_BLIT
) {
586 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
587 if (tri_mask
& (1 << i
))
588 DBG(" %s", buffer_names
[i
]);
592 intelClearWithTris(intel
, tri_mask
);
596 if (INTEL_DEBUG
& DEBUG_BLIT
) {
597 DBG("swrast clear:");
598 for (i
= 0; i
< BUFFER_COUNT
; i
++) {
599 if (swrast_mask
& (1 << i
))
600 DBG(" %s", buffer_names
[i
]);
604 _swrast_Clear(ctx
, swrast_mask
);
609 /* Emit wait for pending flips */
611 intel_wait_flips(struct intel_context
*intel
)
613 struct intel_framebuffer
*intel_fb
=
614 (struct intel_framebuffer
*) intel
->ctx
.DrawBuffer
;
615 struct intel_renderbuffer
*intel_rb
=
616 intel_get_renderbuffer(&intel_fb
->Base
,
617 intel_fb
->Base
._ColorDrawBufferIndexes
[0] ==
618 BUFFER_FRONT_LEFT
? BUFFER_FRONT_LEFT
:
621 if (intel
->intelScreen
->driScrnPriv
->dri2
.enabled
)
624 if (intel_fb
->Base
.Name
== 0 && intel_rb
&&
625 intel_rb
->pf_pending
== intel_fb
->pf_seq
) {
626 GLint pf_planes
= intel_fb
->pf_planes
;
629 /* Wait for pending flips to take effect */
630 BEGIN_BATCH(2, NO_LOOP_CLIPRECTS
);
631 OUT_BATCH(pf_planes
& 0x1 ? (MI_WAIT_FOR_EVENT
| MI_WAIT_FOR_PLANE_A_FLIP
)
633 OUT_BATCH(pf_planes
& 0x2 ? (MI_WAIT_FOR_EVENT
| MI_WAIT_FOR_PLANE_B_FLIP
)
637 intel_rb
->pf_pending
--;
642 /* Flip the front & back buffers
645 intelPageFlip(const __DRIdrawablePrivate
* dPriv
)
647 struct intel_context
*intel
;
649 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
651 if (INTEL_DEBUG
& DEBUG_IOCTL
)
652 fprintf(stderr
, "%s\n", __FUNCTION__
);
655 assert(dPriv
->driContextPriv
);
656 assert(dPriv
->driContextPriv
->driverPrivate
);
658 intel
= (struct intel_context
*) dPriv
->driContextPriv
->driverPrivate
;
660 if (intel
->intelScreen
->drmMinor
< 9)
663 intelFlush(&intel
->ctx
);
667 LOCK_HARDWARE(intel
);
669 if (dPriv
->numClipRects
&& intel_fb
->pf_active
) {
670 drm_i915_flip_t flip
;
672 flip
.pipes
= intel_fb
->pf_planes
;
674 ret
= drmCommandWrite(intel
->driFd
, DRM_I915_FLIP
, &flip
, sizeof(flip
));
677 UNLOCK_HARDWARE(intel
);
679 if (ret
|| !intel_fb
->pf_active
)
682 if (!dPriv
->numClipRects
) {
683 usleep(10000); /* throttle invisible client 10ms */
686 intel_fb
->pf_current_page
= (intel
->sarea
->pf_current_page
>>
687 (intel_fb
->pf_planes
& 0x2)) & 0x3;
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
=
695 intel_flip_renderbuffers(intel_fb
);
696 intel_draw_buffer(&intel
->ctx
, &intel_fb
->Base
);
702 intelScheduleSwap(__DRIdrawablePrivate
* dPriv
, GLboolean
*missed_target
)
704 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
705 unsigned int interval
;
706 struct intel_context
*intel
=
707 intelScreenContext(dPriv
->driScreenPriv
->private);
708 const intelScreenPrivate
*intelScreen
= intel
->intelScreen
;
710 drm_i915_vblank_swap_t swap
;
713 if (!dPriv
->vblFlags
||
714 (dPriv
->vblFlags
& VBLANK_FLAG_NO_IRQ
) ||
715 intelScreen
->drmMinor
< (intel_fb
->pf_active
? 9 : 6))
718 interval
= driGetVBlankInterval(dPriv
);
720 swap
.seqtype
= DRM_VBLANK_ABSOLUTE
;
722 if (dPriv
->vblFlags
& VBLANK_FLAG_SYNC
) {
723 swap
.seqtype
|= DRM_VBLANK_NEXTONMISS
;
724 } else if (interval
== 0)
727 swap
.drawable
= dPriv
->hHWDrawable
;
728 target
= swap
.sequence
= dPriv
->vblSeq
+ interval
;
730 if ( dPriv
->vblFlags
& VBLANK_FLAG_SECONDARY
) {
731 swap
.seqtype
|= DRM_VBLANK_SECONDARY
;
734 LOCK_HARDWARE(intel
);
736 intel_batchbuffer_flush(intel
->batch
);
738 if ( intel_fb
->pf_active
) {
739 swap
.seqtype
|= DRM_VBLANK_FLIP
;
741 intel_fb
->pf_current_page
= (((intel
->sarea
->pf_current_page
>>
742 (intel_fb
->pf_planes
& 0x2)) & 0x3) + 1) %
743 intel_fb
->pf_num_pages
;
746 if (!drmCommandWriteRead(intel
->driFd
, DRM_I915_VBLANK_SWAP
, &swap
,
748 dPriv
->vblSeq
= swap
.sequence
;
749 swap
.sequence
-= target
;
750 *missed_target
= swap
.sequence
> 0 && swap
.sequence
<= (1 << 23);
752 intel_get_renderbuffer(&intel_fb
->Base
, BUFFER_BACK_LEFT
)->vbl_pending
=
753 intel_get_renderbuffer(&intel_fb
->Base
,
754 BUFFER_FRONT_LEFT
)->vbl_pending
=
757 if (swap
.seqtype
& DRM_VBLANK_FLIP
) {
758 intel_flip_renderbuffers(intel_fb
);
759 intel_draw_buffer(&intel
->ctx
, intel
->ctx
.DrawBuffer
);
764 if (swap
.seqtype
& DRM_VBLANK_FLIP
) {
765 intel_fb
->pf_current_page
= ((intel
->sarea
->pf_current_page
>>
766 (intel_fb
->pf_planes
& 0x2)) & 0x3) %
767 intel_fb
->pf_num_pages
;
773 UNLOCK_HARDWARE(intel
);
779 intelSwapBuffers(__DRIdrawablePrivate
* dPriv
)
781 __DRIscreenPrivate
*psp
= dPriv
->driScreenPriv
;
783 if (dPriv
->driContextPriv
&& dPriv
->driContextPriv
->driverPrivate
) {
784 GET_CURRENT_CONTEXT(ctx
);
785 struct intel_context
*intel
;
790 intel
= intel_context(ctx
);
792 if (ctx
->Visual
.doubleBufferMode
) {
793 GLboolean missed_target
;
794 struct intel_framebuffer
*intel_fb
= dPriv
->driverPrivate
;
797 _mesa_notifySwapBuffers(ctx
); /* flush pending rendering comands */
799 if (!intelScheduleSwap(dPriv
, &missed_target
)) {
800 driWaitForVBlank(dPriv
, &missed_target
);
803 * Update each buffer's vbl_pending so we don't get too out of
806 intel_get_renderbuffer(&intel_fb
->Base
,
807 BUFFER_BACK_LEFT
)->vbl_pending
=
808 intel_get_renderbuffer(&intel_fb
->Base
,
809 BUFFER_FRONT_LEFT
)->vbl_pending
=
811 if (!intelPageFlip(dPriv
)) {
812 intelCopyBuffer(dPriv
, NULL
);
816 intel_fb
->swap_count
++;
817 (*psp
->systemTime
->getUST
) (&ust
);
819 intel_fb
->swap_missed_count
++;
820 intel_fb
->swap_missed_ust
= ust
- intel_fb
->swap_ust
;
823 intel_fb
->swap_ust
= ust
;
825 drmCommandNone(intel
->driFd
, DRM_I915_GEM_THROTTLE
);
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__
);
835 intelCopySubBuffer(__DRIdrawablePrivate
* dPriv
, int x
, int y
, int w
, int h
)
837 if (dPriv
->driContextPriv
&& dPriv
->driContextPriv
->driverPrivate
) {
838 struct intel_context
*intel
=
839 (struct intel_context
*) dPriv
->driContextPriv
->driverPrivate
;
840 GLcontext
*ctx
= &intel
->ctx
;
842 if (ctx
->Visual
.doubleBufferMode
) {
843 drm_clip_rect_t rect
;
844 rect
.x1
= x
+ dPriv
->x
;
845 rect
.y1
= (dPriv
->h
- y
- h
) + dPriv
->y
;
846 rect
.x2
= rect
.x1
+ w
;
847 rect
.y2
= rect
.y1
+ h
;
848 _mesa_notifySwapBuffers(ctx
); /* flush pending rendering comands */
849 intelCopyBuffer(dPriv
, &rect
);
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 * Update the hardware state for drawing into a window or framebuffer object.
862 * Called by glDrawBuffer, glBindFramebufferEXT, MakeCurrent, and other
863 * places within the driver.
865 * Basically, this needs to be called any time the current framebuffer
866 * changes, the renderbuffers change, or we need to draw into different
870 intel_draw_buffer(GLcontext
* ctx
, struct gl_framebuffer
*fb
)
872 struct intel_context
*intel
= intel_context(ctx
);
873 struct intel_region
*colorRegions
[MAX_DRAW_BUFFERS
], *depthRegion
= NULL
;
874 struct intel_renderbuffer
*irbDepth
= NULL
, *irbStencil
= NULL
;
875 int front
= 0; /* drawing to front color buffer? */
878 /* this can happen during the initial context initialization */
882 /* Do this here, note core Mesa, since this function is called from
883 * many places within the driver.
885 if (ctx
->NewState
& (_NEW_BUFFERS
| _NEW_COLOR
| _NEW_PIXEL
)) {
886 /* this updates the DrawBuffer->_NumColorDrawBuffers fields, etc */
887 _mesa_update_framebuffer(ctx
);
888 /* this updates the DrawBuffer's Width/Height if it's a FBO */
889 _mesa_update_draw_buffer_bounds(ctx
);
892 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
893 /* this may occur when we're called by glBindFrameBuffer() during
894 * the process of someone setting up renderbuffers, etc.
896 /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/
901 intel_validate_paired_depth_stencil(ctx
, fb
);
903 /* If the batch contents require looping over cliprects, flush them before
904 * we go changing which cliprects get referenced when that happens.
906 if (intel
->batch
->cliprect_mode
== LOOP_CLIPRECTS
)
907 intel_batchbuffer_flush(intel
->batch
);
910 * How many color buffers are we drawing into?
912 if (fb
->_NumColorDrawBuffers
== 0) {
914 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_TRUE
);
915 colorRegions
[0] = NULL
;
918 intelSetRenderbufferClipRects(intel
);
919 } else if (fb
->_NumColorDrawBuffers
> 1) {
921 struct intel_renderbuffer
*irb
;
922 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_FALSE
);
925 intelSetRenderbufferClipRects(intel
);
926 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
927 irb
= intel_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
928 colorRegions
[i
] = (irb
&& irb
->region
) ? irb
->region
: NULL
;
932 /* draw to exactly one color buffer */
933 /*_mesa_debug(ctx, "Hardware rendering\n");*/
934 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_FALSE
);
935 if (fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
) {
940 * Get the intel_renderbuffer for the colorbuffer we're drawing into.
941 * And set up cliprects.
944 /* drawing to window system buffer */
946 intelSetFrontClipRects(intel
);
947 colorRegions
[0] = intel_get_rb_region(fb
, BUFFER_FRONT_LEFT
);
950 intelSetBackClipRects(intel
);
951 colorRegions
[0]= intel_get_rb_region(fb
, BUFFER_BACK_LEFT
);
955 /* drawing to user-created FBO */
956 struct intel_renderbuffer
*irb
;
957 intelSetRenderbufferClipRects(intel
);
958 irb
= intel_renderbuffer(fb
->_ColorDrawBuffers
[0]);
959 colorRegions
[0] = (irb
&& irb
->region
) ? irb
->region
: NULL
;
963 /* Update culling direction which changes depending on the
964 * orientation of the buffer:
966 if (ctx
->Driver
.FrontFace
)
967 ctx
->Driver
.FrontFace(ctx
, ctx
->Polygon
.FrontFace
);
969 ctx
->NewState
|= _NEW_POLYGON
;
971 if (!colorRegions
[0]) {
972 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_TRUE
);
975 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_FALSE
);
979 *** Get depth buffer region and check if we need a software fallback.
980 *** Note that the depth buffer is usually a DEPTH_STENCIL buffer.
982 if (fb
->_DepthBuffer
&& fb
->_DepthBuffer
->Wrapped
) {
983 irbDepth
= intel_renderbuffer(fb
->_DepthBuffer
->Wrapped
);
984 if (irbDepth
&& irbDepth
->region
) {
985 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_FALSE
);
986 depthRegion
= irbDepth
->region
;
989 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_TRUE
);
994 /* not using depth buffer */
995 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_FALSE
);
1001 *** This can only be hardware accelerated if we're using a
1002 *** combined DEPTH_STENCIL buffer (for now anyway).
1004 if (fb
->_StencilBuffer
&& fb
->_StencilBuffer
->Wrapped
) {
1005 irbStencil
= intel_renderbuffer(fb
->_StencilBuffer
->Wrapped
);
1006 if (irbStencil
&& irbStencil
->region
) {
1007 ASSERT(irbStencil
->Base
._ActualFormat
== GL_DEPTH24_STENCIL8_EXT
);
1008 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_FALSE
);
1009 /* need to re-compute stencil hw state */
1010 if (ctx
->Driver
.Enable
!= NULL
)
1011 ctx
->Driver
.Enable(ctx
, GL_STENCIL_TEST
, ctx
->Stencil
.Enabled
);
1013 ctx
->NewState
|= _NEW_STENCIL
;
1015 depthRegion
= irbStencil
->region
;
1018 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_TRUE
);
1022 /* XXX FBO: instead of FALSE, pass ctx->Stencil.Enabled ??? */
1023 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_FALSE
);
1024 /* need to re-compute stencil hw state */
1025 if (ctx
->Driver
.Enable
!= NULL
)
1026 ctx
->Driver
.Enable(ctx
, GL_STENCIL_TEST
, ctx
->Stencil
.Enabled
);
1028 ctx
->NewState
|= _NEW_STENCIL
;
1032 * Update depth test state
1034 if (ctx
->Driver
.Enable
) {
1035 if (ctx
->Depth
.Test
&& fb
->Visual
.depthBits
> 0) {
1036 ctx
->Driver
.Enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
1038 ctx
->Driver
.Enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
1041 ctx
->NewState
|= _NEW_DEPTH
;
1044 intel
->vtbl
.set_draw_region(intel
, colorRegions
, depthRegion
,
1045 fb
->_NumColorDrawBuffers
);
1047 /* update viewport since it depends on window size */
1048 if (ctx
->Driver
.Viewport
) {
1049 ctx
->Driver
.Viewport(ctx
, ctx
->Viewport
.X
, ctx
->Viewport
.Y
,
1050 ctx
->Viewport
.Width
, ctx
->Viewport
.Height
);
1052 ctx
->NewState
|= _NEW_VIEWPORT
;
1055 /* Set state we know depends on drawable parameters:
1057 if (ctx
->Driver
.Scissor
)
1058 ctx
->Driver
.Scissor(ctx
, ctx
->Scissor
.X
, ctx
->Scissor
.Y
,
1059 ctx
->Scissor
.Width
, ctx
->Scissor
.Height
);
1060 intel
->NewGLState
|= _NEW_SCISSOR
;
1062 if (ctx
->Driver
.DepthRange
)
1063 ctx
->Driver
.DepthRange(ctx
,
1070 intelDrawBuffer(GLcontext
* ctx
, GLenum mode
)
1072 intel_draw_buffer(ctx
, ctx
->DrawBuffer
);
1077 intelReadBuffer(GLcontext
* ctx
, GLenum mode
)
1079 if (ctx
->ReadBuffer
== ctx
->DrawBuffer
) {
1080 /* This will update FBO completeness status.
1081 * A framebuffer will be incomplete if the GL_READ_BUFFER setting
1082 * refers to a missing renderbuffer. Calling glReadBuffer can set
1083 * that straight and can make the drawing buffer complete.
1085 intel_draw_buffer(ctx
, ctx
->DrawBuffer
);
1087 /* Generally, functions which read pixels (glReadPixels, glCopyPixels, etc)
1088 * reference ctx->ReadBuffer and do appropriate state checks.
1094 intelInitBufferFuncs(struct dd_function_table
*functions
)
1096 functions
->Clear
= intelClear
;
1097 functions
->DrawBuffer
= intelDrawBuffer
;
1098 functions
->ReadBuffer
= intelReadBuffer
;