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 "main/framebuffer.h"
34 * Return pointer to current color drawing region, or NULL.
37 intel_drawbuf_region(struct intel_context
*intel
)
39 struct intel_renderbuffer
*irbColor
=
40 intel_renderbuffer(intel
->ctx
.DrawBuffer
->_ColorDrawBuffers
[0]);
42 return irbColor
->region
;
48 * Return pointer to current color reading region, or NULL.
51 intel_readbuf_region(struct intel_context
*intel
)
53 struct intel_renderbuffer
*irb
54 = intel_renderbuffer(intel
->ctx
.ReadBuffer
->_ColorReadBuffer
);
62 * Check if we're about to draw into the front color buffer.
63 * If so, set the intel->front_buffer_dirty field to true.
66 intel_check_front_buffer_rendering(struct intel_context
*intel
)
68 const struct gl_framebuffer
*fb
= intel
->ctx
.DrawBuffer
;
70 /* drawing to window system buffer */
71 if (fb
->_NumColorDrawBuffers
> 0) {
72 if (fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
) {
73 intel
->front_buffer_dirty
= GL_TRUE
;
81 * Update the hardware state for drawing into a window or framebuffer object.
83 * Called by glDrawBuffer, glBindFramebufferEXT, MakeCurrent, and other
84 * places within the driver.
86 * Basically, this needs to be called any time the current framebuffer
87 * changes, the renderbuffers change, or we need to draw into different
91 intel_draw_buffer(GLcontext
* ctx
, struct gl_framebuffer
*fb
)
93 struct intel_context
*intel
= intel_context(ctx
);
94 struct intel_region
*colorRegions
[MAX_DRAW_BUFFERS
], *depthRegion
= NULL
;
95 struct intel_renderbuffer
*irbDepth
= NULL
, *irbStencil
= NULL
;
98 /* this can happen during the initial context initialization */
102 /* Do this here, not core Mesa, since this function is called from
103 * many places within the driver.
105 if (ctx
->NewState
& _NEW_BUFFERS
) {
106 /* this updates the DrawBuffer->_NumColorDrawBuffers fields, etc */
107 _mesa_update_framebuffer(ctx
);
108 /* this updates the DrawBuffer's Width/Height if it's a FBO */
109 _mesa_update_draw_buffer_bounds(ctx
);
112 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
113 /* this may occur when we're called by glBindFrameBuffer() during
114 * the process of someone setting up renderbuffers, etc.
116 /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/
120 /* How many color buffers are we drawing into?
122 * If there are zero buffers or the buffer is too big, don't configure any
123 * regions for hardware drawing. We'll fallback to software below. Not
124 * having regions set makes some of the software fallback paths faster.
126 if ((fb
->Width
> ctx
->Const
.MaxRenderbufferSize
)
127 || (fb
->Height
> ctx
->Const
.MaxRenderbufferSize
)
128 || (fb
->_NumColorDrawBuffers
== 0)) {
130 colorRegions
[0] = NULL
;
132 else if (fb
->_NumColorDrawBuffers
> 1) {
134 struct intel_renderbuffer
*irb
;
136 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
137 irb
= intel_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
138 colorRegions
[i
] = irb
? irb
->region
: NULL
;
142 /* Get the intel_renderbuffer for the single colorbuffer we're drawing
146 /* drawing to window system buffer */
147 if (fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
)
148 colorRegions
[0] = intel_get_rb_region(fb
, BUFFER_FRONT_LEFT
);
150 colorRegions
[0] = intel_get_rb_region(fb
, BUFFER_BACK_LEFT
);
153 /* drawing to user-created FBO */
154 struct intel_renderbuffer
*irb
;
155 irb
= intel_renderbuffer(fb
->_ColorDrawBuffers
[0]);
156 colorRegions
[0] = (irb
&& irb
->region
) ? irb
->region
: NULL
;
160 if (!colorRegions
[0]) {
161 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_TRUE
);
164 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_FALSE
);
168 *** Get depth buffer region and check if we need a software fallback.
169 *** Note that the depth buffer is usually a DEPTH_STENCIL buffer.
171 if (fb
->_DepthBuffer
&& fb
->_DepthBuffer
->Wrapped
) {
172 irbDepth
= intel_renderbuffer(fb
->_DepthBuffer
->Wrapped
);
173 if (irbDepth
&& irbDepth
->region
) {
174 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_FALSE
);
175 depthRegion
= irbDepth
->region
;
178 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_TRUE
);
183 /* not using depth buffer */
184 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_FALSE
);
190 *** This can only be hardware accelerated if we're using a
191 *** combined DEPTH_STENCIL buffer.
193 if (fb
->_StencilBuffer
&& fb
->_StencilBuffer
->Wrapped
) {
194 irbStencil
= intel_renderbuffer(fb
->_StencilBuffer
->Wrapped
);
195 if (irbStencil
&& irbStencil
->region
) {
196 ASSERT(irbStencil
->Base
.Format
== MESA_FORMAT_S8_Z24
);
197 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_FALSE
);
200 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_TRUE
);
204 /* XXX FBO: instead of FALSE, pass ctx->Stencil._Enabled ??? */
205 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_FALSE
);
208 /* If we have a (packed) stencil buffer attached but no depth buffer,
209 * we still need to set up the shared depth/stencil state so we can use it.
211 if (depthRegion
== NULL
&& irbStencil
&& irbStencil
->region
)
212 depthRegion
= irbStencil
->region
;
215 * Update depth and stencil test state
217 if (ctx
->Driver
.Enable
) {
218 ctx
->Driver
.Enable(ctx
, GL_DEPTH_TEST
,
219 (ctx
->Depth
.Test
&& fb
->Visual
.depthBits
> 0));
220 ctx
->Driver
.Enable(ctx
, GL_STENCIL_TEST
,
221 (ctx
->Stencil
.Enabled
&& fb
->Visual
.stencilBits
> 0));
224 /* Mesa's Stencil._Enabled field is updated when
225 * _NEW_BUFFERS | _NEW_STENCIL, but i965 code assumes that the value
226 * only changes with _NEW_STENCIL (which seems sensible). So flag it
227 * here since this is the _NEW_BUFFERS path.
229 intel
->NewGLState
|= (_NEW_DEPTH
| _NEW_STENCIL
);
232 intel
->vtbl
.set_draw_region(intel
, colorRegions
, depthRegion
,
233 fb
->_NumColorDrawBuffers
);
235 /* update viewport since it depends on window size */
237 intelCalcViewport(ctx
);
239 intel
->NewGLState
|= _NEW_VIEWPORT
;
241 /* Set state we know depends on drawable parameters:
243 if (ctx
->Driver
.Scissor
)
244 ctx
->Driver
.Scissor(ctx
, ctx
->Scissor
.X
, ctx
->Scissor
.Y
,
245 ctx
->Scissor
.Width
, ctx
->Scissor
.Height
);
246 intel
->NewGLState
|= _NEW_SCISSOR
;
248 if (ctx
->Driver
.DepthRange
)
249 ctx
->Driver
.DepthRange(ctx
,
253 /* Update culling direction which changes depending on the
254 * orientation of the buffer:
256 if (ctx
->Driver
.FrontFace
)
257 ctx
->Driver
.FrontFace(ctx
, ctx
->Polygon
.FrontFace
);
259 intel
->NewGLState
|= _NEW_POLYGON
;
264 intelDrawBuffer(GLcontext
* ctx
, GLenum mode
)
266 if ((ctx
->DrawBuffer
!= NULL
) && (ctx
->DrawBuffer
->Name
== 0)) {
267 struct intel_context
*const intel
= intel_context(ctx
);
268 const GLboolean was_front_buffer_rendering
=
269 intel
->is_front_buffer_rendering
;
271 intel
->is_front_buffer_rendering
= (mode
== GL_FRONT_LEFT
)
272 || (mode
== GL_FRONT
);
274 /* If we weren't front-buffer rendering before but we are now,
275 * invalidate our DRI drawable so we'll ask for new buffers
276 * (including the fake front) before we start rendering again.
278 if (!was_front_buffer_rendering
&& intel
->is_front_buffer_rendering
)
279 dri2InvalidateDrawable(intel
->driContext
->driDrawablePriv
);
282 intel_draw_buffer(ctx
, ctx
->DrawBuffer
);
287 intelReadBuffer(GLcontext
* ctx
, GLenum mode
)
289 if ((ctx
->DrawBuffer
!= NULL
) && (ctx
->DrawBuffer
->Name
== 0)) {
290 struct intel_context
*const intel
= intel_context(ctx
);
291 const GLboolean was_front_buffer_reading
=
292 intel
->is_front_buffer_reading
;
294 intel
->is_front_buffer_reading
= (mode
== GL_FRONT_LEFT
)
295 || (mode
== GL_FRONT
);
297 /* If we weren't front-buffer reading before but we are now,
298 * invalidate our DRI drawable so we'll ask for new buffers
299 * (including the fake front) before we start reading again.
301 if (!was_front_buffer_reading
&& intel
->is_front_buffer_reading
)
302 dri2InvalidateDrawable(intel
->driContext
->driReadablePriv
);
305 if (ctx
->ReadBuffer
== ctx
->DrawBuffer
) {
306 /* This will update FBO completeness status.
307 * A framebuffer will be incomplete if the GL_READ_BUFFER setting
308 * refers to a missing renderbuffer. Calling glReadBuffer can set
309 * that straight and can make the drawing buffer complete.
311 intel_draw_buffer(ctx
, ctx
->DrawBuffer
);
313 /* Generally, functions which read pixels (glReadPixels, glCopyPixels, etc)
314 * reference ctx->ReadBuffer and do appropriate state checks.
320 intelInitBufferFuncs(struct dd_function_table
*functions
)
322 functions
->DrawBuffer
= intelDrawBuffer
;
323 functions
->ReadBuffer
= intelReadBuffer
;