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