+ struct gl_context *ctx = &intel->ctx;
+ struct gl_framebuffer *fb = ctx->DrawBuffer;
+ struct intel_region *colorRegions[MAX_DRAW_BUFFERS], *depthRegion = NULL;
+ struct intel_renderbuffer *irbDepth = NULL, *irbStencil = NULL;
+
+ if (!fb) {
+ /* this can happen during the initial context initialization */
+ return;
+ }
+
+ irbDepth = intel_get_renderbuffer(fb, BUFFER_DEPTH);
+ irbStencil = intel_get_renderbuffer(fb, BUFFER_STENCIL);
+
+ /* Do this here, not core Mesa, since this function is called from
+ * many places within the driver.
+ */
+ if (ctx->NewState & _NEW_BUFFERS) {
+ /* this updates the DrawBuffer->_NumColorDrawBuffers fields, etc */
+ _mesa_update_framebuffer(ctx, ctx->ReadBuffer, ctx->DrawBuffer);
+ /* this updates the DrawBuffer's Width/Height if it's a FBO */
+ _mesa_update_draw_buffer_bounds(ctx, ctx->DrawBuffer);
+ }
+
+ if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
+ /* this may occur when we're called by glBindFrameBuffer() during
+ * the process of someone setting up renderbuffers, etc.
+ */
+ /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/
+ return;
+ }
+
+ /* How many color buffers are we drawing into?
+ *
+ * If there are zero buffers or the buffer is too big, don't configure any
+ * regions for hardware drawing. We'll fallback to software below. Not
+ * having regions set makes some of the software fallback paths faster.
+ */
+ if ((fb->Width > ctx->Const.MaxRenderbufferSize)
+ || (fb->Height > ctx->Const.MaxRenderbufferSize)
+ || (fb->_NumColorDrawBuffers == 0)) {
+ /* writing to 0 */
+ colorRegions[0] = NULL;
+ }
+ else if (fb->_NumColorDrawBuffers > 1) {
+ int i;
+ struct intel_renderbuffer *irb;
+
+ for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
+ irb = intel_renderbuffer(fb->_ColorDrawBuffers[i]);
+ colorRegions[i] = (irb && irb->mt) ? irb->mt->region : NULL;
+ }
+ }
+ else {
+ /* Get the intel_renderbuffer for the single colorbuffer we're drawing
+ * into.
+ */
+ if (_mesa_is_winsys_fbo(fb)) {
+ /* drawing to window system buffer */
+ if (fb->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT)
+ colorRegions[0] = intel_get_rb_region(fb, BUFFER_FRONT_LEFT);
+ else
+ colorRegions[0] = intel_get_rb_region(fb, BUFFER_BACK_LEFT);
+ }
+ else {
+ /* drawing to user-created FBO */
+ struct intel_renderbuffer *irb;
+ irb = intel_renderbuffer(fb->_ColorDrawBuffers[0]);
+ colorRegions[0] = (irb && irb->mt->region) ? irb->mt->region : NULL;
+ }
+ }
+
+ if (!colorRegions[0]) {
+ FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, true);
+ }
+ else {
+ FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, false);
+ }
+
+ /* Check for depth fallback. */
+ if (irbDepth && irbDepth->mt) {
+ FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, false);
+ depthRegion = irbDepth->mt->region;
+ } else if (irbDepth && !irbDepth->mt) {
+ FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, true);
+ depthRegion = NULL;
+ } else { /* !irbDepth */
+ /* No fallback is needed because there is no depth buffer. */
+ FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, false);
+ depthRegion = NULL;
+ }
+
+ /* Check for stencil fallback. */
+ if (irbStencil && irbStencil->mt) {
+ assert(intel_rb_format(irbStencil) == MESA_FORMAT_Z24_UNORM_S8_UINT);
+ FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, false);
+ } else if (irbStencil && !irbStencil->mt) {
+ FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, true);
+ } else { /* !irbStencil */
+ /* No fallback is needed because there is no stencil buffer. */
+ FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, false);
+ }
+
+ /* If we have a (packed) stencil buffer attached but no depth buffer,
+ * we still need to set up the shared depth/stencil state so we can use it.
+ */
+ if (depthRegion == NULL && irbStencil && irbStencil->mt
+ && intel_rb_format(irbStencil) == MESA_FORMAT_Z24_UNORM_S8_UINT) {
+ depthRegion = irbStencil->mt->region;
+ }
+
+ /*
+ * Update depth and stencil test state
+ */
+ ctx->Driver.Enable(ctx, GL_DEPTH_TEST, ctx->Depth.Test);
+ ctx->Driver.Enable(ctx, GL_STENCIL_TEST,
+ (ctx->Stencil.Enabled && fb->Visual.stencilBits > 0));
+
+ intel->vtbl.set_draw_region(intel, colorRegions, depthRegion,
+ fb->_NumColorDrawBuffers);
+ intel->NewGLState |= _NEW_BUFFERS;
+
+ /* Set state we know depends on drawable parameters:
+ */
+ intelCalcViewport(ctx);
+ ctx->Driver.Scissor(ctx);
+
+ /* Update culling direction which changes depending on the
+ * orientation of the buffer:
+ */
+ ctx->Driver.FrontFace(ctx, ctx->Polygon.FrontFace);