Merge branch 'mesa_7_7_branch'
[mesa.git] / src / mesa / drivers / dri / intel / intel_buffers.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
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"
33
34
35 /**
36 * XXX move this into a new dri/common/cliprects.c file.
37 */
38 GLboolean
39 intel_intersect_cliprects(drm_clip_rect_t * dst,
40 const drm_clip_rect_t * a,
41 const drm_clip_rect_t * b)
42 {
43 GLint bx = b->x1;
44 GLint by = b->y1;
45 GLint bw = b->x2 - bx;
46 GLint bh = b->y2 - by;
47
48 if (bx < a->x1)
49 bw -= a->x1 - bx, bx = a->x1;
50 if (by < a->y1)
51 bh -= a->y1 - by, by = a->y1;
52 if (bx + bw > a->x2)
53 bw = a->x2 - bx;
54 if (by + bh > a->y2)
55 bh = a->y2 - by;
56 if (bw <= 0)
57 return GL_FALSE;
58 if (bh <= 0)
59 return GL_FALSE;
60
61 dst->x1 = bx;
62 dst->y1 = by;
63 dst->x2 = bx + bw;
64 dst->y2 = by + bh;
65
66 return GL_TRUE;
67 }
68
69 /**
70 * Return pointer to current color drawing region, or NULL.
71 */
72 struct intel_region *
73 intel_drawbuf_region(struct intel_context *intel)
74 {
75 struct intel_renderbuffer *irbColor =
76 intel_renderbuffer(intel->ctx.DrawBuffer->_ColorDrawBuffers[0]);
77 if (irbColor)
78 return irbColor->region;
79 else
80 return NULL;
81 }
82
83 /**
84 * Return pointer to current color reading region, or NULL.
85 */
86 struct intel_region *
87 intel_readbuf_region(struct intel_context *intel)
88 {
89 struct intel_renderbuffer *irb
90 = intel_renderbuffer(intel->ctx.ReadBuffer->_ColorReadBuffer);
91 if (irb)
92 return irb->region;
93 else
94 return NULL;
95 }
96
97 void
98 intel_get_cliprects(struct intel_context *intel,
99 struct drm_clip_rect **cliprects,
100 unsigned int *num_cliprects,
101 int *x_off, int *y_off)
102 {
103 intel->fboRect.x1 = 0;
104 intel->fboRect.y1 = 0;
105 intel->fboRect.x2 = intel->ctx.DrawBuffer->Width;
106 intel->fboRect.y2 = intel->ctx.DrawBuffer->Height;
107
108 *cliprects = &intel->fboRect;
109 *num_cliprects = 1;
110 *x_off = 0;
111 *y_off = 0;
112 }
113
114
115 /**
116 * Check if we're about to draw into the front color buffer.
117 * If so, set the intel->front_buffer_dirty field to true.
118 */
119 void
120 intel_check_front_buffer_rendering(struct intel_context *intel)
121 {
122 const struct gl_framebuffer *fb = intel->ctx.DrawBuffer;
123 if (fb->Name == 0) {
124 /* drawing to window system buffer */
125 if (fb->_NumColorDrawBuffers > 0) {
126 if (fb->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT) {
127 intel->front_buffer_dirty = GL_TRUE;
128 }
129 }
130 }
131 }
132
133
134 /**
135 * Update the hardware state for drawing into a window or framebuffer object.
136 *
137 * Called by glDrawBuffer, glBindFramebufferEXT, MakeCurrent, and other
138 * places within the driver.
139 *
140 * Basically, this needs to be called any time the current framebuffer
141 * changes, the renderbuffers change, or we need to draw into different
142 * color buffers.
143 */
144 void
145 intel_draw_buffer(GLcontext * ctx, struct gl_framebuffer *fb)
146 {
147 struct intel_context *intel = intel_context(ctx);
148 struct intel_region *colorRegions[MAX_DRAW_BUFFERS], *depthRegion = NULL;
149 struct intel_renderbuffer *irbDepth = NULL, *irbStencil = NULL;
150
151 if (!fb) {
152 /* this can happen during the initial context initialization */
153 return;
154 }
155
156 /* Do this here, not core Mesa, since this function is called from
157 * many places within the driver.
158 */
159 if (ctx->NewState & _NEW_BUFFERS) {
160 /* this updates the DrawBuffer->_NumColorDrawBuffers fields, etc */
161 _mesa_update_framebuffer(ctx);
162 /* this updates the DrawBuffer's Width/Height if it's a FBO */
163 _mesa_update_draw_buffer_bounds(ctx);
164 }
165
166 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
167 /* this may occur when we're called by glBindFrameBuffer() during
168 * the process of someone setting up renderbuffers, etc.
169 */
170 /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/
171 return;
172 }
173
174 /* How many color buffers are we drawing into?
175 *
176 * If there are zero buffers or the buffer is too big, don't configure any
177 * regions for hardware drawing. We'll fallback to software below. Not
178 * having regions set makes some of the software fallback paths faster.
179 */
180 if ((fb->Width > ctx->Const.MaxRenderbufferSize)
181 || (fb->Height > ctx->Const.MaxRenderbufferSize)
182 || (fb->_NumColorDrawBuffers == 0)) {
183 /* writing to 0 */
184 colorRegions[0] = NULL;
185 }
186 else if (fb->_NumColorDrawBuffers > 1) {
187 int i;
188 struct intel_renderbuffer *irb;
189
190 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
191 irb = intel_renderbuffer(fb->_ColorDrawBuffers[i]);
192 colorRegions[i] = irb ? irb->region : NULL;
193 }
194 }
195 else {
196 /* Get the intel_renderbuffer for the single colorbuffer we're drawing
197 * into.
198 */
199 if (fb->Name == 0) {
200 /* drawing to window system buffer */
201 if (fb->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT)
202 colorRegions[0] = intel_get_rb_region(fb, BUFFER_FRONT_LEFT);
203 else
204 colorRegions[0] = intel_get_rb_region(fb, BUFFER_BACK_LEFT);
205 }
206 else {
207 /* drawing to user-created FBO */
208 struct intel_renderbuffer *irb;
209 irb = intel_renderbuffer(fb->_ColorDrawBuffers[0]);
210 colorRegions[0] = (irb && irb->region) ? irb->region : NULL;
211 }
212 }
213
214 if (!colorRegions[0]) {
215 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_TRUE);
216 }
217 else {
218 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_FALSE);
219 }
220
221 /***
222 *** Get depth buffer region and check if we need a software fallback.
223 *** Note that the depth buffer is usually a DEPTH_STENCIL buffer.
224 ***/
225 if (fb->_DepthBuffer && fb->_DepthBuffer->Wrapped) {
226 irbDepth = intel_renderbuffer(fb->_DepthBuffer->Wrapped);
227 if (irbDepth && irbDepth->region) {
228 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_FALSE);
229 depthRegion = irbDepth->region;
230 }
231 else {
232 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_TRUE);
233 depthRegion = NULL;
234 }
235 }
236 else {
237 /* not using depth buffer */
238 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_FALSE);
239 depthRegion = NULL;
240 }
241
242 /***
243 *** Stencil buffer
244 *** This can only be hardware accelerated if we're using a
245 *** combined DEPTH_STENCIL buffer.
246 ***/
247 if (fb->_StencilBuffer && fb->_StencilBuffer->Wrapped) {
248 irbStencil = intel_renderbuffer(fb->_StencilBuffer->Wrapped);
249 if (irbStencil && irbStencil->region) {
250 ASSERT(irbStencil->Base.Format == MESA_FORMAT_S8_Z24);
251 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_FALSE);
252 }
253 else {
254 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_TRUE);
255 }
256 }
257 else {
258 /* XXX FBO: instead of FALSE, pass ctx->Stencil._Enabled ??? */
259 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_FALSE);
260 }
261
262 /* If we have a (packed) stencil buffer attached but no depth buffer,
263 * we still need to set up the shared depth/stencil state so we can use it.
264 */
265 if (depthRegion == NULL && irbStencil && irbStencil->region)
266 depthRegion = irbStencil->region;
267
268 /*
269 * Update depth and stencil test state
270 */
271 if (ctx->Driver.Enable) {
272 ctx->Driver.Enable(ctx, GL_DEPTH_TEST,
273 (ctx->Depth.Test && fb->Visual.depthBits > 0));
274 ctx->Driver.Enable(ctx, GL_STENCIL_TEST,
275 (ctx->Stencil.Enabled && fb->Visual.stencilBits > 0));
276 }
277 else {
278 /* Mesa's Stencil._Enabled field is updated when
279 * _NEW_BUFFERS | _NEW_STENCIL, but i965 code assumes that the value
280 * only changes with _NEW_STENCIL (which seems sensible). So flag it
281 * here since this is the _NEW_BUFFERS path.
282 */
283 ctx->NewState |= (_NEW_DEPTH | _NEW_STENCIL);
284 }
285
286 intel->vtbl.set_draw_region(intel, colorRegions, depthRegion,
287 fb->_NumColorDrawBuffers);
288
289 /* update viewport since it depends on window size */
290 #ifdef I915
291 intelCalcViewport(ctx);
292 #else
293 ctx->NewState |= _NEW_VIEWPORT;
294 #endif
295 /* Set state we know depends on drawable parameters:
296 */
297 if (ctx->Driver.Scissor)
298 ctx->Driver.Scissor(ctx, ctx->Scissor.X, ctx->Scissor.Y,
299 ctx->Scissor.Width, ctx->Scissor.Height);
300 intel->NewGLState |= _NEW_SCISSOR;
301
302 if (ctx->Driver.DepthRange)
303 ctx->Driver.DepthRange(ctx,
304 ctx->Viewport.Near,
305 ctx->Viewport.Far);
306
307 /* Update culling direction which changes depending on the
308 * orientation of the buffer:
309 */
310 if (ctx->Driver.FrontFace)
311 ctx->Driver.FrontFace(ctx, ctx->Polygon.FrontFace);
312 else
313 ctx->NewState |= _NEW_POLYGON;
314 }
315
316
317 static void
318 intelDrawBuffer(GLcontext * ctx, GLenum mode)
319 {
320 if ((ctx->DrawBuffer != NULL) && (ctx->DrawBuffer->Name == 0)) {
321 struct intel_context *const intel = intel_context(ctx);
322 const GLboolean was_front_buffer_rendering =
323 intel->is_front_buffer_rendering;
324
325 intel->is_front_buffer_rendering = (mode == GL_FRONT_LEFT)
326 || (mode == GL_FRONT);
327
328 /* If we weren't front-buffer rendering before but we are now, make sure
329 * that the front-buffer has actually been allocated.
330 */
331 if (!was_front_buffer_rendering && intel->is_front_buffer_rendering) {
332 intel_update_renderbuffers(intel->driContext,
333 intel->driContext->driDrawablePriv);
334 }
335 }
336
337 intel_draw_buffer(ctx, ctx->DrawBuffer);
338 }
339
340
341 static void
342 intelReadBuffer(GLcontext * ctx, GLenum mode)
343 {
344 if ((ctx->DrawBuffer != NULL) && (ctx->DrawBuffer->Name == 0)) {
345 struct intel_context *const intel = intel_context(ctx);
346 const GLboolean was_front_buffer_reading =
347 intel->is_front_buffer_reading;
348
349 intel->is_front_buffer_reading = (mode == GL_FRONT_LEFT)
350 || (mode == GL_FRONT);
351
352 /* If we weren't front-buffer reading before but we are now, make sure
353 * that the front-buffer has actually been allocated.
354 */
355 if (!was_front_buffer_reading && intel->is_front_buffer_reading) {
356 intel_update_renderbuffers(intel->driContext,
357 intel->driContext->driDrawablePriv);
358 }
359 }
360
361 if (ctx->ReadBuffer == ctx->DrawBuffer) {
362 /* This will update FBO completeness status.
363 * A framebuffer will be incomplete if the GL_READ_BUFFER setting
364 * refers to a missing renderbuffer. Calling glReadBuffer can set
365 * that straight and can make the drawing buffer complete.
366 */
367 intel_draw_buffer(ctx, ctx->DrawBuffer);
368 }
369 /* Generally, functions which read pixels (glReadPixels, glCopyPixels, etc)
370 * reference ctx->ReadBuffer and do appropriate state checks.
371 */
372 }
373
374
375 void
376 intelInitBufferFuncs(struct dd_function_table *functions)
377 {
378 functions->DrawBuffer = intelDrawBuffer;
379 functions->ReadBuffer = intelReadBuffer;
380 }