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_batchbuffer.h"
32 #include "main/framebuffer.h"
35 * Return pointer to current color drawing region, or NULL.
38 intel_drawbuf_region(struct intel_context
*intel
)
40 struct intel_renderbuffer
*irbColor
=
41 intel_renderbuffer(intel
->ctx
.DrawBuffer
->_ColorDrawBuffers
[0]);
43 return irbColor
->region
;
49 * Return pointer to current color reading region, or NULL.
52 intel_readbuf_region(struct intel_context
*intel
)
54 struct intel_renderbuffer
*irb
55 = intel_renderbuffer(intel
->ctx
.ReadBuffer
->_ColorReadBuffer
);
63 * Check if we're about to draw into the front color buffer.
64 * If so, set the intel->front_buffer_dirty field to true.
67 intel_check_front_buffer_rendering(struct intel_context
*intel
)
69 const struct gl_framebuffer
*fb
= intel
->ctx
.DrawBuffer
;
71 /* drawing to window system buffer */
72 if (fb
->_NumColorDrawBuffers
> 0) {
73 if (fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
) {
74 intel
->front_buffer_dirty
= GL_TRUE
;
82 * Update the hardware state for drawing into a window or framebuffer object.
84 * Called by glDrawBuffer, glBindFramebufferEXT, MakeCurrent, and other
85 * places within the driver.
87 * Basically, this needs to be called any time the current framebuffer
88 * changes, the renderbuffers change, or we need to draw into different
92 intel_draw_buffer(GLcontext
* ctx
, struct gl_framebuffer
*fb
)
94 struct intel_context
*intel
= intel_context(ctx
);
95 struct intel_region
*colorRegions
[MAX_DRAW_BUFFERS
], *depthRegion
= NULL
;
96 struct intel_renderbuffer
*irbDepth
= NULL
, *irbStencil
= NULL
;
99 /* this can happen during the initial context initialization */
103 /* Do this here, not core Mesa, since this function is called from
104 * many places within the driver.
106 if (ctx
->NewState
& _NEW_BUFFERS
) {
107 /* this updates the DrawBuffer->_NumColorDrawBuffers fields, etc */
108 _mesa_update_framebuffer(ctx
);
109 /* this updates the DrawBuffer's Width/Height if it's a FBO */
110 _mesa_update_draw_buffer_bounds(ctx
);
113 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
114 /* this may occur when we're called by glBindFrameBuffer() during
115 * the process of someone setting up renderbuffers, etc.
117 /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/
121 /* How many color buffers are we drawing into?
123 * If there are zero buffers or the buffer is too big, don't configure any
124 * regions for hardware drawing. We'll fallback to software below. Not
125 * having regions set makes some of the software fallback paths faster.
127 if ((fb
->Width
> ctx
->Const
.MaxRenderbufferSize
)
128 || (fb
->Height
> ctx
->Const
.MaxRenderbufferSize
)
129 || (fb
->_NumColorDrawBuffers
== 0)) {
131 colorRegions
[0] = NULL
;
133 else if (fb
->_NumColorDrawBuffers
> 1) {
135 struct intel_renderbuffer
*irb
;
137 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
138 irb
= intel_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
139 colorRegions
[i
] = irb
? irb
->region
: NULL
;
143 /* Get the intel_renderbuffer for the single colorbuffer we're drawing
147 /* drawing to window system buffer */
148 if (fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
)
149 colorRegions
[0] = intel_get_rb_region(fb
, BUFFER_FRONT_LEFT
);
151 colorRegions
[0] = intel_get_rb_region(fb
, BUFFER_BACK_LEFT
);
154 /* drawing to user-created FBO */
155 struct intel_renderbuffer
*irb
;
156 irb
= intel_renderbuffer(fb
->_ColorDrawBuffers
[0]);
157 colorRegions
[0] = (irb
&& irb
->region
) ? irb
->region
: NULL
;
161 if (!colorRegions
[0]) {
162 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_TRUE
);
165 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_FALSE
);
169 *** Get depth buffer region and check if we need a software fallback.
170 *** Note that the depth buffer is usually a DEPTH_STENCIL buffer.
172 if (fb
->_DepthBuffer
&& fb
->_DepthBuffer
->Wrapped
) {
173 irbDepth
= intel_renderbuffer(fb
->_DepthBuffer
->Wrapped
);
174 if (irbDepth
&& irbDepth
->region
) {
175 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_FALSE
);
176 depthRegion
= irbDepth
->region
;
179 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_TRUE
);
184 /* not using depth buffer */
185 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_FALSE
);
191 *** This can only be hardware accelerated if we're using a
192 *** combined DEPTH_STENCIL buffer.
194 if (fb
->_StencilBuffer
&& fb
->_StencilBuffer
->Wrapped
) {
195 irbStencil
= intel_renderbuffer(fb
->_StencilBuffer
->Wrapped
);
196 if (irbStencil
&& irbStencil
->region
) {
197 ASSERT(irbStencil
->Base
.Format
== MESA_FORMAT_S8_Z24
);
198 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_FALSE
);
201 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_TRUE
);
205 /* XXX FBO: instead of FALSE, pass ctx->Stencil._Enabled ??? */
206 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_FALSE
);
209 /* If we have a (packed) stencil buffer attached but no depth buffer,
210 * we still need to set up the shared depth/stencil state so we can use it.
212 if (depthRegion
== NULL
&& irbStencil
&& irbStencil
->region
)
213 depthRegion
= irbStencil
->region
;
216 * Update depth and stencil test state
218 if (ctx
->Driver
.Enable
) {
219 ctx
->Driver
.Enable(ctx
, GL_DEPTH_TEST
,
220 (ctx
->Depth
.Test
&& fb
->Visual
.depthBits
> 0));
221 ctx
->Driver
.Enable(ctx
, GL_STENCIL_TEST
,
222 (ctx
->Stencil
.Enabled
&& fb
->Visual
.stencilBits
> 0));
225 /* Mesa's Stencil._Enabled field is updated when
226 * _NEW_BUFFERS | _NEW_STENCIL, but i965 code assumes that the value
227 * only changes with _NEW_STENCIL (which seems sensible). So flag it
228 * here since this is the _NEW_BUFFERS path.
230 ctx
->NewState
|= (_NEW_DEPTH
| _NEW_STENCIL
);
233 intel
->vtbl
.set_draw_region(intel
, colorRegions
, depthRegion
,
234 fb
->_NumColorDrawBuffers
);
236 /* update viewport since it depends on window size */
238 intelCalcViewport(ctx
);
240 ctx
->NewState
|= _NEW_VIEWPORT
;
242 /* Set state we know depends on drawable parameters:
244 if (ctx
->Driver
.Scissor
)
245 ctx
->Driver
.Scissor(ctx
, ctx
->Scissor
.X
, ctx
->Scissor
.Y
,
246 ctx
->Scissor
.Width
, ctx
->Scissor
.Height
);
247 intel
->NewGLState
|= _NEW_SCISSOR
;
249 if (ctx
->Driver
.DepthRange
)
250 ctx
->Driver
.DepthRange(ctx
,
254 /* Update culling direction which changes depending on the
255 * orientation of the buffer:
257 if (ctx
->Driver
.FrontFace
)
258 ctx
->Driver
.FrontFace(ctx
, ctx
->Polygon
.FrontFace
);
260 ctx
->NewState
|= _NEW_POLYGON
;
265 intelDrawBuffer(GLcontext
* ctx
, GLenum mode
)
267 if ((ctx
->DrawBuffer
!= NULL
) && (ctx
->DrawBuffer
->Name
== 0)) {
268 struct intel_context
*const intel
= intel_context(ctx
);
269 const GLboolean was_front_buffer_rendering
=
270 intel
->is_front_buffer_rendering
;
272 intel
->is_front_buffer_rendering
= (mode
== GL_FRONT_LEFT
)
273 || (mode
== GL_FRONT
);
275 /* If we weren't front-buffer rendering before but we are now, make sure
276 * that the front-buffer has actually been allocated.
278 if (!was_front_buffer_rendering
&& intel
->is_front_buffer_rendering
) {
279 intel_update_renderbuffers(intel
->driContext
,
280 intel
->driContext
->driDrawablePriv
);
284 intel_draw_buffer(ctx
, ctx
->DrawBuffer
);
289 intelReadBuffer(GLcontext
* ctx
, GLenum mode
)
291 if ((ctx
->DrawBuffer
!= NULL
) && (ctx
->DrawBuffer
->Name
== 0)) {
292 struct intel_context
*const intel
= intel_context(ctx
);
293 const GLboolean was_front_buffer_reading
=
294 intel
->is_front_buffer_reading
;
296 intel
->is_front_buffer_reading
= (mode
== GL_FRONT_LEFT
)
297 || (mode
== GL_FRONT
);
299 /* If we weren't front-buffer reading before but we are now, make sure
300 * that the front-buffer has actually been allocated.
302 if (!was_front_buffer_reading
&& intel
->is_front_buffer_reading
) {
303 intel_update_renderbuffers(intel
->driContext
,
304 intel
->driContext
->driDrawablePriv
);
308 if (ctx
->ReadBuffer
== ctx
->DrawBuffer
) {
309 /* This will update FBO completeness status.
310 * A framebuffer will be incomplete if the GL_READ_BUFFER setting
311 * refers to a missing renderbuffer. Calling glReadBuffer can set
312 * that straight and can make the drawing buffer complete.
314 intel_draw_buffer(ctx
, ctx
->DrawBuffer
);
316 /* Generally, functions which read pixels (glReadPixels, glCopyPixels, etc)
317 * reference ctx->ReadBuffer and do appropriate state checks.
323 intelInitBufferFuncs(struct dd_function_table
*functions
)
325 functions
->DrawBuffer
= intelDrawBuffer
;
326 functions
->ReadBuffer
= intelReadBuffer
;