intel_buffers.c revision 6062692cc6cd2a88d854b304d9a85bcf4bab0d11
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 32#include "main/framebuffer.h" 33#include "main/renderbuffer.h" 34 35/** 36 * Return pointer to current color drawing region, or NULL. 37 */ 38struct intel_region * 39intel_drawbuf_region(struct intel_context *intel) 40{ 41 struct intel_renderbuffer *irbColor = 42 intel_renderbuffer(intel->ctx.DrawBuffer->_ColorDrawBuffers[0]); 43 if (irbColor) 44 return irbColor->region; 45 else 46 return NULL; 47} 48 49/** 50 * Return pointer to current color reading region, or NULL. 51 */ 52struct intel_region * 53intel_readbuf_region(struct intel_context *intel) 54{ 55 struct intel_renderbuffer *irb 56 = intel_renderbuffer(intel->ctx.ReadBuffer->_ColorReadBuffer); 57 if (irb) 58 return irb->region; 59 else 60 return NULL; 61} 62 63/** 64 * Check if we're about to draw into the front color buffer. 65 * If so, set the intel->front_buffer_dirty field to true. 66 */ 67void 68intel_check_front_buffer_rendering(struct intel_context *intel) 69{ 70 const struct gl_framebuffer *fb = intel->ctx.DrawBuffer; 71 if (fb->Name == 0) { 72 /* drawing to window system buffer */ 73 if (fb->_NumColorDrawBuffers > 0) { 74 if (fb->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT) { 75 intel->front_buffer_dirty = GL_TRUE; 76 } 77 } 78 } 79} 80 81 82/** 83 * Update the hardware state for drawing into a window or framebuffer object. 84 * 85 * Called by glDrawBuffer, glBindFramebufferEXT, MakeCurrent, and other 86 * places within the driver. 87 * 88 * Basically, this needs to be called any time the current framebuffer 89 * changes, the renderbuffers change, or we need to draw into different 90 * color buffers. 91 */ 92void 93intel_draw_buffer(struct gl_context * ctx, struct gl_framebuffer *fb) 94{ 95 struct intel_context *intel = intel_context(ctx); 96 struct intel_region *colorRegions[MAX_DRAW_BUFFERS], *depthRegion = NULL; 97 struct intel_renderbuffer *irbDepth = NULL, *irbStencil = NULL; 98 bool fb_has_hiz = intel_framebuffer_has_hiz(fb); 99 100 if (!fb) { 101 /* this can happen during the initial context initialization */ 102 return; 103 } 104 105 /* 106 * If intel_context is using separate stencil, but the depth attachment 107 * (gl_framebuffer.Attachment[BUFFER_DEPTH]) has a packed depth/stencil 108 * format, then we must install the real depth buffer at fb->_DepthBuffer 109 * and set fb->_DepthBuffer->Wrapped before calling _mesa_update_framebuffer. 110 * Otherwise, _mesa_update_framebuffer will create and install a swras 111 * depth wrapper instead. 112 * 113 * Ditto for stencil. 114 */ 115 irbDepth = intel_get_renderbuffer(fb, BUFFER_DEPTH); 116 if (irbDepth && irbDepth->Base.Format == MESA_FORMAT_X8_Z24) { 117 _mesa_reference_renderbuffer(&fb->_DepthBuffer, &irbDepth->Base); 118 irbDepth->Base.Wrapped = fb->Attachment[BUFFER_DEPTH].Renderbuffer; 119 } 120 121 irbStencil = intel_get_renderbuffer(fb, BUFFER_STENCIL); 122 if (irbStencil && irbStencil->Base.Format == MESA_FORMAT_S8) { 123 _mesa_reference_renderbuffer(&fb->_StencilBuffer, &irbStencil->Base); 124 irbStencil->Base.Wrapped = fb->Attachment[BUFFER_STENCIL].Renderbuffer; 125 } 126 127 /* Do this here, not core Mesa, since this function is called from 128 * many places within the driver. 129 */ 130 if (ctx->NewState & _NEW_BUFFERS) { 131 /* this updates the DrawBuffer->_NumColorDrawBuffers fields, etc */ 132 _mesa_update_framebuffer(ctx); 133 /* this updates the DrawBuffer's Width/Height if it's a FBO */ 134 _mesa_update_draw_buffer_bounds(ctx); 135 } 136 137 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) { 138 /* this may occur when we're called by glBindFrameBuffer() during 139 * the process of someone setting up renderbuffers, etc. 140 */ 141 /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/ 142 return; 143 } 144 145 /* How many color buffers are we drawing into? 146 * 147 * If there are zero buffers or the buffer is too big, don't configure any 148 * regions for hardware drawing. We'll fallback to software below. Not 149 * having regions set makes some of the software fallback paths faster. 150 */ 151 if ((fb->Width > ctx->Const.MaxRenderbufferSize) 152 || (fb->Height > ctx->Const.MaxRenderbufferSize) 153 || (fb->_NumColorDrawBuffers == 0)) { 154 /* writing to 0 */ 155 colorRegions[0] = NULL; 156 } 157 else if (fb->_NumColorDrawBuffers > 1) { 158 int i; 159 struct intel_renderbuffer *irb; 160 161 for (i = 0; i < fb->_NumColorDrawBuffers; i++) { 162 irb = intel_renderbuffer(fb->_ColorDrawBuffers[i]); 163 colorRegions[i] = irb ? irb->region : NULL; 164 } 165 } 166 else { 167 /* Get the intel_renderbuffer for the single colorbuffer we're drawing 168 * into. 169 */ 170 if (fb->Name == 0) { 171 /* drawing to window system buffer */ 172 if (fb->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT) 173 colorRegions[0] = intel_get_rb_region(fb, BUFFER_FRONT_LEFT); 174 else 175 colorRegions[0] = intel_get_rb_region(fb, BUFFER_BACK_LEFT); 176 } 177 else { 178 /* drawing to user-created FBO */ 179 struct intel_renderbuffer *irb; 180 irb = intel_renderbuffer(fb->_ColorDrawBuffers[0]); 181 colorRegions[0] = (irb && irb->region) ? irb->region : NULL; 182 } 183 } 184 185 if (!colorRegions[0]) { 186 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_TRUE); 187 } 188 else { 189 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_FALSE); 190 } 191 192 /* Check for depth fallback. */ 193 if (irbDepth && irbDepth->region) { 194 assert(!fb_has_hiz || irbDepth->Base.Format != MESA_FORMAT_S8_Z24); 195 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_FALSE); 196 depthRegion = irbDepth->region; 197 } else if (irbDepth && !irbDepth->region) { 198 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_TRUE); 199 depthRegion = NULL; 200 } else { /* !irbDepth */ 201 /* No fallback is needed because there is no depth buffer. */ 202 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_FALSE); 203 depthRegion = NULL; 204 } 205 206 /* Check for stencil fallback. */ 207 if (irbStencil && irbStencil->region) { 208 if (!intel->has_separate_stencil) 209 assert(irbStencil->Base.Format == MESA_FORMAT_S8_Z24); 210 if (fb_has_hiz || intel->must_use_separate_stencil) 211 assert(irbStencil->Base.Format == MESA_FORMAT_S8); 212 if (irbStencil->Base.Format == MESA_FORMAT_S8) 213 assert(intel->has_separate_stencil); 214 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_FALSE); 215 } else if (irbStencil && !irbStencil->region) { 216 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_TRUE); 217 } else { /* !irbStencil */ 218 /* No fallback is needed because there is no stencil buffer. */ 219 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_FALSE); 220 } 221 222 /* If we have a (packed) stencil buffer attached but no depth buffer, 223 * we still need to set up the shared depth/stencil state so we can use it. 224 */ 225 if (depthRegion == NULL && irbStencil && irbStencil->region 226 && irbStencil->Base.Format == MESA_FORMAT_S8_Z24) { 227 depthRegion = irbStencil->region; 228 } 229 230 /* 231 * Update depth and stencil test state 232 */ 233 if (ctx->Driver.Enable) { 234 ctx->Driver.Enable(ctx, GL_DEPTH_TEST, 235 (ctx->Depth.Test && fb->Visual.depthBits > 0)); 236 ctx->Driver.Enable(ctx, GL_STENCIL_TEST, 237 (ctx->Stencil.Enabled && fb->Visual.stencilBits > 0)); 238 } 239 else { 240 /* Mesa's Stencil._Enabled field is updated when 241 * _NEW_BUFFERS | _NEW_STENCIL, but i965 code assumes that the value 242 * only changes with _NEW_STENCIL (which seems sensible). So flag it 243 * here since this is the _NEW_BUFFERS path. 244 */ 245 intel->NewGLState |= (_NEW_DEPTH | _NEW_STENCIL); 246 } 247 248 intel->vtbl.set_draw_region(intel, colorRegions, depthRegion, 249 fb->_NumColorDrawBuffers); 250 intel->NewGLState |= _NEW_BUFFERS; 251 252 /* update viewport since it depends on window size */ 253#ifdef I915 254 intelCalcViewport(ctx); 255#else 256 intel->NewGLState |= _NEW_VIEWPORT; 257#endif 258 /* Set state we know depends on drawable parameters: 259 */ 260 if (ctx->Driver.Scissor) 261 ctx->Driver.Scissor(ctx, ctx->Scissor.X, ctx->Scissor.Y, 262 ctx->Scissor.Width, ctx->Scissor.Height); 263 intel->NewGLState |= _NEW_SCISSOR; 264 265 if (ctx->Driver.DepthRange) 266 ctx->Driver.DepthRange(ctx, 267 ctx->Viewport.Near, 268 ctx->Viewport.Far); 269 270 /* Update culling direction which changes depending on the 271 * orientation of the buffer: 272 */ 273 if (ctx->Driver.FrontFace) 274 ctx->Driver.FrontFace(ctx, ctx->Polygon.FrontFace); 275 else 276 intel->NewGLState |= _NEW_POLYGON; 277} 278 279 280static void 281intelDrawBuffer(struct gl_context * ctx, GLenum mode) 282{ 283 if ((ctx->DrawBuffer != NULL) && (ctx->DrawBuffer->Name == 0)) { 284 struct intel_context *const intel = intel_context(ctx); 285 const GLboolean was_front_buffer_rendering = 286 intel->is_front_buffer_rendering; 287 288 intel->is_front_buffer_rendering = (mode == GL_FRONT_LEFT) 289 || (mode == GL_FRONT); 290 291 /* If we weren't front-buffer rendering before but we are now, 292 * invalidate our DRI drawable so we'll ask for new buffers 293 * (including the fake front) before we start rendering again. 294 */ 295 if (!was_front_buffer_rendering && intel->is_front_buffer_rendering) 296 dri2InvalidateDrawable(intel->driContext->driDrawablePriv); 297 } 298 299 intel_draw_buffer(ctx, ctx->DrawBuffer); 300} 301 302 303static void 304intelReadBuffer(struct gl_context * ctx, GLenum mode) 305{ 306 if ((ctx->DrawBuffer != NULL) && (ctx->DrawBuffer->Name == 0)) { 307 struct intel_context *const intel = intel_context(ctx); 308 const GLboolean was_front_buffer_reading = 309 intel->is_front_buffer_reading; 310 311 intel->is_front_buffer_reading = (mode == GL_FRONT_LEFT) 312 || (mode == GL_FRONT); 313 314 /* If we weren't front-buffer reading before but we are now, 315 * invalidate our DRI drawable so we'll ask for new buffers 316 * (including the fake front) before we start reading again. 317 */ 318 if (!was_front_buffer_reading && intel->is_front_buffer_reading) 319 dri2InvalidateDrawable(intel->driContext->driReadablePriv); 320 } 321} 322 323 324void 325intelInitBufferFuncs(struct dd_function_table *functions) 326{ 327 functions->DrawBuffer = intelDrawBuffer; 328 functions->ReadBuffer = intelReadBuffer; 329} 330