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_context.h"
29 #include "intel_buffers.h"
30 #include "intel_fbo.h"
31 #include "intel_regions.h"
32 #include "intel_batchbuffer.h"
33 #include "main/framebuffer.h"
34 #include "drirenderbuffer.h"
38 * XXX move this into a new dri/common/cliprects.c file.
41 intel_intersect_cliprects(drm_clip_rect_t
* dst
,
42 const drm_clip_rect_t
* a
,
43 const drm_clip_rect_t
* b
)
47 GLint bw
= b
->x2
- bx
;
48 GLint bh
= b
->y2
- by
;
51 bw
-= a
->x1
- bx
, bx
= a
->x1
;
53 bh
-= a
->y1
- by
, by
= a
->y1
;
72 * Return pointer to current color drawing region, or NULL.
75 intel_drawbuf_region(struct intel_context
*intel
)
77 struct intel_renderbuffer
*irbColor
=
78 intel_renderbuffer(intel
->ctx
.DrawBuffer
->_ColorDrawBuffers
[0]);
80 return irbColor
->region
;
86 * Return pointer to current color reading region, or NULL.
89 intel_readbuf_region(struct intel_context
*intel
)
91 struct intel_renderbuffer
*irb
92 = intel_renderbuffer(intel
->ctx
.ReadBuffer
->_ColorReadBuffer
);
100 intel_get_cliprects(struct intel_context
*intel
,
101 struct drm_clip_rect
**cliprects
,
102 unsigned int *num_cliprects
,
103 int *x_off
, int *y_off
)
105 __DRIdrawablePrivate
*dPriv
= intel
->driDrawable
;
107 if (intel
->constant_cliprect
) {
108 /* FBO or DRI2 rendering, which can just use the fb's size. */
109 intel
->fboRect
.x1
= 0;
110 intel
->fboRect
.y1
= 0;
111 intel
->fboRect
.x2
= intel
->ctx
.DrawBuffer
->Width
;
112 intel
->fboRect
.y2
= intel
->ctx
.DrawBuffer
->Height
;
114 *cliprects
= &intel
->fboRect
;
118 } else if (intel
->front_cliprects
|| dPriv
->numBackClipRects
== 0) {
119 /* use the front clip rects */
120 *cliprects
= dPriv
->pClipRects
;
121 *num_cliprects
= dPriv
->numClipRects
;
126 /* use the back clip rects */
127 *num_cliprects
= dPriv
->numBackClipRects
;
128 *cliprects
= dPriv
->pBackClipRects
;
129 *x_off
= dPriv
->backX
;
130 *y_off
= dPriv
->backY
;
136 * Check if we're about to draw into the front color buffer.
137 * If so, set the intel->front_buffer_dirty field to true.
140 intel_check_front_buffer_rendering(struct intel_context
*intel
)
142 const struct gl_framebuffer
*fb
= intel
->ctx
.DrawBuffer
;
144 /* drawing to window system buffer */
145 if (fb
->_NumColorDrawBuffers
> 0) {
146 if (fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
) {
147 intel
->front_buffer_dirty
= GL_TRUE
;
155 * Update the hardware state for drawing into a window or framebuffer object.
157 * Called by glDrawBuffer, glBindFramebufferEXT, MakeCurrent, and other
158 * places within the driver.
160 * Basically, this needs to be called any time the current framebuffer
161 * changes, the renderbuffers change, or we need to draw into different
165 intel_draw_buffer(GLcontext
* ctx
, struct gl_framebuffer
*fb
)
167 struct intel_context
*intel
= intel_context(ctx
);
168 struct intel_region
*colorRegions
[MAX_DRAW_BUFFERS
], *depthRegion
= NULL
;
169 struct intel_renderbuffer
*irbDepth
= NULL
, *irbStencil
= NULL
;
172 /* this can happen during the initial context initialization */
176 /* Do this here, not core Mesa, since this function is called from
177 * many places within the driver.
179 if (ctx
->NewState
& _NEW_BUFFERS
) {
180 /* this updates the DrawBuffer->_NumColorDrawBuffers fields, etc */
181 _mesa_update_framebuffer(ctx
);
182 /* this updates the DrawBuffer's Width/Height if it's a FBO */
183 _mesa_update_draw_buffer_bounds(ctx
);
186 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
187 /* this may occur when we're called by glBindFrameBuffer() during
188 * the process of someone setting up renderbuffers, etc.
190 /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/
195 * How many color buffers are we drawing into?
197 if (fb
->_NumColorDrawBuffers
== 0) {
199 colorRegions
[0] = NULL
;
200 intel
->constant_cliprect
= GL_TRUE
;
202 else if (fb
->_NumColorDrawBuffers
> 1) {
204 struct intel_renderbuffer
*irb
;
206 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
207 irb
= intel_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
208 colorRegions
[i
] = irb
? irb
->region
: NULL
;
210 intel
->constant_cliprect
= GL_TRUE
;
213 /* Get the intel_renderbuffer for the single colorbuffer we're drawing
214 * into, and set up cliprects if it's a DRI1 window front buffer.
217 intel
->constant_cliprect
= intel
->driScreen
->dri2
.enabled
;
218 /* drawing to window system buffer */
219 if (fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
) {
220 if (!intel
->constant_cliprect
&& !intel
->front_cliprects
)
221 intel_batchbuffer_flush(intel
->batch
);
222 intel
->front_cliprects
= GL_TRUE
;
223 colorRegions
[0] = intel_get_rb_region(fb
, BUFFER_FRONT_LEFT
);
226 if (!intel
->constant_cliprect
&& intel
->front_cliprects
)
227 intel_batchbuffer_flush(intel
->batch
);
228 intel
->front_cliprects
= GL_FALSE
;
229 colorRegions
[0] = intel_get_rb_region(fb
, BUFFER_BACK_LEFT
);
233 /* drawing to user-created FBO */
234 struct intel_renderbuffer
*irb
;
235 irb
= intel_renderbuffer(fb
->_ColorDrawBuffers
[0]);
236 colorRegions
[0] = (irb
&& irb
->region
) ? irb
->region
: NULL
;
237 intel
->constant_cliprect
= GL_TRUE
;
241 if (!colorRegions
[0]) {
242 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_TRUE
);
245 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_FALSE
);
249 *** Get depth buffer region and check if we need a software fallback.
250 *** Note that the depth buffer is usually a DEPTH_STENCIL buffer.
252 if (fb
->_DepthBuffer
&& fb
->_DepthBuffer
->Wrapped
) {
253 irbDepth
= intel_renderbuffer(fb
->_DepthBuffer
->Wrapped
);
254 if (irbDepth
&& irbDepth
->region
) {
255 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_FALSE
);
256 depthRegion
= irbDepth
->region
;
259 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_TRUE
);
264 /* not using depth buffer */
265 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_FALSE
);
271 *** This can only be hardware accelerated if we're using a
272 *** combined DEPTH_STENCIL buffer.
274 if (fb
->_StencilBuffer
&& fb
->_StencilBuffer
->Wrapped
) {
275 irbStencil
= intel_renderbuffer(fb
->_StencilBuffer
->Wrapped
);
276 if (irbStencil
&& irbStencil
->region
) {
277 ASSERT(irbStencil
->Base
.Format
== MESA_FORMAT_S8_Z24
);
278 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_FALSE
);
281 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_TRUE
);
285 /* XXX FBO: instead of FALSE, pass ctx->Stencil._Enabled ??? */
286 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_FALSE
);
290 * Update depth and stencil test state
292 if (ctx
->Driver
.Enable
) {
293 ctx
->Driver
.Enable(ctx
, GL_DEPTH_TEST
,
294 (ctx
->Depth
.Test
&& fb
->Visual
.depthBits
> 0));
295 ctx
->Driver
.Enable(ctx
, GL_STENCIL_TEST
,
296 (ctx
->Stencil
.Enabled
&& fb
->Visual
.stencilBits
> 0));
299 /* Mesa's Stencil._Enabled field is updated when
300 * _NEW_BUFFERS | _NEW_STENCIL, but i965 code assumes that the value
301 * only changes with _NEW_STENCIL (which seems sensible). So flag it
302 * here since this is the _NEW_BUFFERS path.
304 ctx
->NewState
|= (_NEW_DEPTH
| _NEW_STENCIL
);
307 intel
->vtbl
.set_draw_region(intel
, colorRegions
, depthRegion
,
308 fb
->_NumColorDrawBuffers
);
310 /* update viewport since it depends on window size */
312 intelCalcViewport(ctx
);
314 ctx
->NewState
|= _NEW_VIEWPORT
;
316 /* Set state we know depends on drawable parameters:
318 if (ctx
->Driver
.Scissor
)
319 ctx
->Driver
.Scissor(ctx
, ctx
->Scissor
.X
, ctx
->Scissor
.Y
,
320 ctx
->Scissor
.Width
, ctx
->Scissor
.Height
);
321 intel
->NewGLState
|= _NEW_SCISSOR
;
323 if (ctx
->Driver
.DepthRange
)
324 ctx
->Driver
.DepthRange(ctx
,
328 /* Update culling direction which changes depending on the
329 * orientation of the buffer:
331 if (ctx
->Driver
.FrontFace
)
332 ctx
->Driver
.FrontFace(ctx
, ctx
->Polygon
.FrontFace
);
334 ctx
->NewState
|= _NEW_POLYGON
;
339 intelDrawBuffer(GLcontext
* ctx
, GLenum mode
)
341 if ((ctx
->DrawBuffer
!= NULL
) && (ctx
->DrawBuffer
->Name
== 0)) {
342 struct intel_context
*const intel
= intel_context(ctx
);
343 const GLboolean was_front_buffer_rendering
=
344 intel
->is_front_buffer_rendering
;
346 intel
->is_front_buffer_rendering
= (mode
== GL_FRONT_LEFT
)
347 || (mode
== GL_FRONT
);
349 /* If we weren't front-buffer rendering before but we are now, make sure
350 * that the front-buffer has actually been allocated.
352 if (!was_front_buffer_rendering
&& intel
->is_front_buffer_rendering
) {
353 intel_update_renderbuffers(intel
->driContext
,
354 intel
->driContext
->driDrawablePriv
);
358 intel_draw_buffer(ctx
, ctx
->DrawBuffer
);
363 intelReadBuffer(GLcontext
* ctx
, GLenum mode
)
365 if ((ctx
->DrawBuffer
!= NULL
) && (ctx
->DrawBuffer
->Name
== 0)) {
366 struct intel_context
*const intel
= intel_context(ctx
);
367 const GLboolean was_front_buffer_reading
=
368 intel
->is_front_buffer_reading
;
370 intel
->is_front_buffer_reading
= (mode
== GL_FRONT_LEFT
)
371 || (mode
== GL_FRONT
);
373 /* If we weren't front-buffer reading before but we are now, make sure
374 * that the front-buffer has actually been allocated.
376 if (!was_front_buffer_reading
&& intel
->is_front_buffer_reading
) {
377 intel_update_renderbuffers(intel
->driContext
,
378 intel
->driContext
->driDrawablePriv
);
382 if (ctx
->ReadBuffer
== ctx
->DrawBuffer
) {
383 /* This will update FBO completeness status.
384 * A framebuffer will be incomplete if the GL_READ_BUFFER setting
385 * refers to a missing renderbuffer. Calling glReadBuffer can set
386 * that straight and can make the drawing buffer complete.
388 intel_draw_buffer(ctx
, ctx
->DrawBuffer
);
390 /* Generally, functions which read pixels (glReadPixels, glCopyPixels, etc)
391 * reference ctx->ReadBuffer and do appropriate state checks.
397 intelInitBufferFuncs(struct dd_function_table
*functions
)
399 functions
->DrawBuffer
= intelDrawBuffer
;
400 functions
->ReadBuffer
= intelReadBuffer
;