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"
36 * XXX move this into a new dri/common/cliprects.c file.
39 intel_intersect_cliprects(drm_clip_rect_t
* dst
,
40 const drm_clip_rect_t
* a
,
41 const drm_clip_rect_t
* b
)
45 GLint bw
= b
->x2
- bx
;
46 GLint bh
= b
->y2
- by
;
49 bw
-= a
->x1
- bx
, bx
= a
->x1
;
51 bh
-= a
->y1
- by
, by
= a
->y1
;
70 * Return pointer to current color drawing region, or NULL.
73 intel_drawbuf_region(struct intel_context
*intel
)
75 struct intel_renderbuffer
*irbColor
=
76 intel_renderbuffer(intel
->ctx
.DrawBuffer
->_ColorDrawBuffers
[0]);
78 return irbColor
->region
;
84 * Return pointer to current color reading region, or NULL.
87 intel_readbuf_region(struct intel_context
*intel
)
89 struct intel_renderbuffer
*irb
90 = intel_renderbuffer(intel
->ctx
.ReadBuffer
->_ColorReadBuffer
);
98 * Check if we're about to draw into the front color buffer.
99 * If so, set the intel->front_buffer_dirty field to true.
102 intel_check_front_buffer_rendering(struct intel_context
*intel
)
104 const struct gl_framebuffer
*fb
= intel
->ctx
.DrawBuffer
;
106 /* drawing to window system buffer */
107 if (fb
->_NumColorDrawBuffers
> 0) {
108 if (fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
) {
109 intel
->front_buffer_dirty
= GL_TRUE
;
117 * Update the hardware state for drawing into a window or framebuffer object.
119 * Called by glDrawBuffer, glBindFramebufferEXT, MakeCurrent, and other
120 * places within the driver.
122 * Basically, this needs to be called any time the current framebuffer
123 * changes, the renderbuffers change, or we need to draw into different
127 intel_draw_buffer(GLcontext
* ctx
, struct gl_framebuffer
*fb
)
129 struct intel_context
*intel
= intel_context(ctx
);
130 struct intel_region
*colorRegions
[MAX_DRAW_BUFFERS
], *depthRegion
= NULL
;
131 struct intel_renderbuffer
*irbDepth
= NULL
, *irbStencil
= NULL
;
134 /* this can happen during the initial context initialization */
138 /* Do this here, not core Mesa, since this function is called from
139 * many places within the driver.
141 if (ctx
->NewState
& _NEW_BUFFERS
) {
142 /* this updates the DrawBuffer->_NumColorDrawBuffers fields, etc */
143 _mesa_update_framebuffer(ctx
);
144 /* this updates the DrawBuffer's Width/Height if it's a FBO */
145 _mesa_update_draw_buffer_bounds(ctx
);
148 if (fb
->_Status
!= GL_FRAMEBUFFER_COMPLETE_EXT
) {
149 /* this may occur when we're called by glBindFrameBuffer() during
150 * the process of someone setting up renderbuffers, etc.
152 /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/
156 /* How many color buffers are we drawing into?
158 * If there are zero buffers or the buffer is too big, don't configure any
159 * regions for hardware drawing. We'll fallback to software below. Not
160 * having regions set makes some of the software fallback paths faster.
162 if ((fb
->Width
> ctx
->Const
.MaxRenderbufferSize
)
163 || (fb
->Height
> ctx
->Const
.MaxRenderbufferSize
)
164 || (fb
->_NumColorDrawBuffers
== 0)) {
166 colorRegions
[0] = NULL
;
168 else if (fb
->_NumColorDrawBuffers
> 1) {
170 struct intel_renderbuffer
*irb
;
172 for (i
= 0; i
< fb
->_NumColorDrawBuffers
; i
++) {
173 irb
= intel_renderbuffer(fb
->_ColorDrawBuffers
[i
]);
174 colorRegions
[i
] = irb
? irb
->region
: NULL
;
178 /* Get the intel_renderbuffer for the single colorbuffer we're drawing
182 /* drawing to window system buffer */
183 if (fb
->_ColorDrawBufferIndexes
[0] == BUFFER_FRONT_LEFT
)
184 colorRegions
[0] = intel_get_rb_region(fb
, BUFFER_FRONT_LEFT
);
186 colorRegions
[0] = intel_get_rb_region(fb
, BUFFER_BACK_LEFT
);
189 /* drawing to user-created FBO */
190 struct intel_renderbuffer
*irb
;
191 irb
= intel_renderbuffer(fb
->_ColorDrawBuffers
[0]);
192 colorRegions
[0] = (irb
&& irb
->region
) ? irb
->region
: NULL
;
196 if (!colorRegions
[0]) {
197 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_TRUE
);
200 FALLBACK(intel
, INTEL_FALLBACK_DRAW_BUFFER
, GL_FALSE
);
204 *** Get depth buffer region and check if we need a software fallback.
205 *** Note that the depth buffer is usually a DEPTH_STENCIL buffer.
207 if (fb
->_DepthBuffer
&& fb
->_DepthBuffer
->Wrapped
) {
208 irbDepth
= intel_renderbuffer(fb
->_DepthBuffer
->Wrapped
);
209 if (irbDepth
&& irbDepth
->region
) {
210 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_FALSE
);
211 depthRegion
= irbDepth
->region
;
214 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_TRUE
);
219 /* not using depth buffer */
220 FALLBACK(intel
, INTEL_FALLBACK_DEPTH_BUFFER
, GL_FALSE
);
226 *** This can only be hardware accelerated if we're using a
227 *** combined DEPTH_STENCIL buffer.
229 if (fb
->_StencilBuffer
&& fb
->_StencilBuffer
->Wrapped
) {
230 irbStencil
= intel_renderbuffer(fb
->_StencilBuffer
->Wrapped
);
231 if (irbStencil
&& irbStencil
->region
) {
232 ASSERT(irbStencil
->Base
.Format
== MESA_FORMAT_S8_Z24
);
233 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_FALSE
);
236 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_TRUE
);
240 /* XXX FBO: instead of FALSE, pass ctx->Stencil._Enabled ??? */
241 FALLBACK(intel
, INTEL_FALLBACK_STENCIL_BUFFER
, GL_FALSE
);
244 /* If we have a (packed) stencil buffer attached but no depth buffer,
245 * we still need to set up the shared depth/stencil state so we can use it.
247 if (depthRegion
== NULL
&& irbStencil
&& irbStencil
->region
)
248 depthRegion
= irbStencil
->region
;
251 * Update depth and stencil test state
253 if (ctx
->Driver
.Enable
) {
254 ctx
->Driver
.Enable(ctx
, GL_DEPTH_TEST
,
255 (ctx
->Depth
.Test
&& fb
->Visual
.depthBits
> 0));
256 ctx
->Driver
.Enable(ctx
, GL_STENCIL_TEST
,
257 (ctx
->Stencil
.Enabled
&& fb
->Visual
.stencilBits
> 0));
260 /* Mesa's Stencil._Enabled field is updated when
261 * _NEW_BUFFERS | _NEW_STENCIL, but i965 code assumes that the value
262 * only changes with _NEW_STENCIL (which seems sensible). So flag it
263 * here since this is the _NEW_BUFFERS path.
265 ctx
->NewState
|= (_NEW_DEPTH
| _NEW_STENCIL
);
268 intel
->vtbl
.set_draw_region(intel
, colorRegions
, depthRegion
,
269 fb
->_NumColorDrawBuffers
);
271 /* update viewport since it depends on window size */
273 intelCalcViewport(ctx
);
275 ctx
->NewState
|= _NEW_VIEWPORT
;
277 /* Set state we know depends on drawable parameters:
279 if (ctx
->Driver
.Scissor
)
280 ctx
->Driver
.Scissor(ctx
, ctx
->Scissor
.X
, ctx
->Scissor
.Y
,
281 ctx
->Scissor
.Width
, ctx
->Scissor
.Height
);
282 intel
->NewGLState
|= _NEW_SCISSOR
;
284 if (ctx
->Driver
.DepthRange
)
285 ctx
->Driver
.DepthRange(ctx
,
289 /* Update culling direction which changes depending on the
290 * orientation of the buffer:
292 if (ctx
->Driver
.FrontFace
)
293 ctx
->Driver
.FrontFace(ctx
, ctx
->Polygon
.FrontFace
);
295 ctx
->NewState
|= _NEW_POLYGON
;
300 intelDrawBuffer(GLcontext
* ctx
, GLenum mode
)
302 if ((ctx
->DrawBuffer
!= NULL
) && (ctx
->DrawBuffer
->Name
== 0)) {
303 struct intel_context
*const intel
= intel_context(ctx
);
304 const GLboolean was_front_buffer_rendering
=
305 intel
->is_front_buffer_rendering
;
307 intel
->is_front_buffer_rendering
= (mode
== GL_FRONT_LEFT
)
308 || (mode
== GL_FRONT
);
310 /* If we weren't front-buffer rendering before but we are now, make sure
311 * that the front-buffer has actually been allocated.
313 if (!was_front_buffer_rendering
&& intel
->is_front_buffer_rendering
) {
314 intel_update_renderbuffers(intel
->driContext
,
315 intel
->driContext
->driDrawablePriv
);
319 intel_draw_buffer(ctx
, ctx
->DrawBuffer
);
324 intelReadBuffer(GLcontext
* ctx
, GLenum mode
)
326 if ((ctx
->DrawBuffer
!= NULL
) && (ctx
->DrawBuffer
->Name
== 0)) {
327 struct intel_context
*const intel
= intel_context(ctx
);
328 const GLboolean was_front_buffer_reading
=
329 intel
->is_front_buffer_reading
;
331 intel
->is_front_buffer_reading
= (mode
== GL_FRONT_LEFT
)
332 || (mode
== GL_FRONT
);
334 /* If we weren't front-buffer reading before but we are now, make sure
335 * that the front-buffer has actually been allocated.
337 if (!was_front_buffer_reading
&& intel
->is_front_buffer_reading
) {
338 intel_update_renderbuffers(intel
->driContext
,
339 intel
->driContext
->driDrawablePriv
);
343 if (ctx
->ReadBuffer
== ctx
->DrawBuffer
) {
344 /* This will update FBO completeness status.
345 * A framebuffer will be incomplete if the GL_READ_BUFFER setting
346 * refers to a missing renderbuffer. Calling glReadBuffer can set
347 * that straight and can make the drawing buffer complete.
349 intel_draw_buffer(ctx
, ctx
->DrawBuffer
);
351 /* Generally, functions which read pixels (glReadPixels, glCopyPixels, etc)
352 * reference ctx->ReadBuffer and do appropriate state checks.
358 intelInitBufferFuncs(struct dd_function_table
*functions
)
360 functions
->DrawBuffer
= intelDrawBuffer
;
361 functions
->ReadBuffer
= intelReadBuffer
;