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"
32 #include "main/framebuffer.h"
33 #include "main/renderbuffer.h"
36 * Return pointer to current color drawing region, or NULL.
39 intel_drawbuf_region(struct intel_context *intel)
41 struct intel_renderbuffer *irbColor =
42 intel_renderbuffer(intel->ctx.DrawBuffer->_ColorDrawBuffers[0]);
44 return irbColor->region;
50 * Return pointer to current color reading region, or NULL.
53 intel_readbuf_region(struct intel_context *intel)
55 struct intel_renderbuffer *irb
56 = intel_renderbuffer(intel->ctx.ReadBuffer->_ColorReadBuffer);
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.
68 intel_check_front_buffer_rendering(struct intel_context *intel)
70 const struct gl_framebuffer *fb = intel->ctx.DrawBuffer;
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;
83 * Update the hardware state for drawing into a window or framebuffer object.
85 * Called by glDrawBuffer, glBindFramebufferEXT, MakeCurrent, and other
86 * places within the driver.
88 * Basically, this needs to be called any time the current framebuffer
89 * changes, the renderbuffers change, or we need to draw into different
93 intel_draw_buffer(struct gl_context * ctx, struct gl_framebuffer *fb)
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);
101 /* this can happen during the initial context initialization */
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.
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;
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;
127 /* Do this here, not core Mesa, since this function is called from
128 * many places within the driver.
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);
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.
141 /*_mesa_debug(ctx, "DrawBuffer: incomplete user FBO\n");*/
145 /* How many color buffers are we drawing into?
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.
151 if ((fb->Width > ctx->Const.MaxRenderbufferSize)
152 || (fb->Height > ctx->Const.MaxRenderbufferSize)
153 || (fb->_NumColorDrawBuffers == 0)) {
155 colorRegions[0] = NULL;
157 else if (fb->_NumColorDrawBuffers > 1) {
159 struct intel_renderbuffer *irb;
161 for (i = 0; i < fb->_NumColorDrawBuffers; i++) {
162 irb = intel_renderbuffer(fb->_ColorDrawBuffers[i]);
163 colorRegions[i] = irb ? irb->region : NULL;
167 /* Get the intel_renderbuffer for the single colorbuffer we're drawing
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);
175 colorRegions[0] = intel_get_rb_region(fb, BUFFER_BACK_LEFT);
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;
185 if (!colorRegions[0]) {
186 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_TRUE);
189 FALLBACK(intel, INTEL_FALLBACK_DRAW_BUFFER, GL_FALSE);
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);
200 } else { /* !irbDepth */
201 /* No fallback is needed because there is no depth buffer. */
202 FALLBACK(intel, INTEL_FALLBACK_DEPTH_BUFFER, GL_FALSE);
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);
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.
225 if (depthRegion == NULL && irbStencil && irbStencil->region
226 && irbStencil->Base.Format == MESA_FORMAT_S8_Z24) {
227 depthRegion = irbStencil->region;
231 * Update depth and stencil test state
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));
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.
245 intel->NewGLState |= (_NEW_DEPTH | _NEW_STENCIL);
248 intel->vtbl.set_draw_region(intel, colorRegions, depthRegion,
249 fb->_NumColorDrawBuffers);
250 intel->NewGLState |= _NEW_BUFFERS;
252 /* update viewport since it depends on window size */
254 intelCalcViewport(ctx);
256 intel->NewGLState |= _NEW_VIEWPORT;
258 /* Set state we know depends on drawable parameters:
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;
265 if (ctx->Driver.DepthRange)
266 ctx->Driver.DepthRange(ctx,
270 /* Update culling direction which changes depending on the
271 * orientation of the buffer:
273 if (ctx->Driver.FrontFace)
274 ctx->Driver.FrontFace(ctx, ctx->Polygon.FrontFace);
276 intel->NewGLState |= _NEW_POLYGON;
281 intelDrawBuffer(struct gl_context * ctx, GLenum mode)
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;
288 intel->is_front_buffer_rendering = (mode == GL_FRONT_LEFT)
289 || (mode == GL_FRONT);
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.
295 if (!was_front_buffer_rendering && intel->is_front_buffer_rendering)
296 dri2InvalidateDrawable(intel->driContext->driDrawablePriv);
299 intel_draw_buffer(ctx, ctx->DrawBuffer);
304 intelReadBuffer(struct gl_context * ctx, GLenum mode)
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;
311 intel->is_front_buffer_reading = (mode == GL_FRONT_LEFT)
312 || (mode == GL_FRONT);
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.
318 if (!was_front_buffer_reading && intel->is_front_buffer_reading)
319 dri2InvalidateDrawable(intel->driContext->driReadablePriv);
325 intelInitBufferFuncs(struct dd_function_table *functions)
327 functions->DrawBuffer = intelDrawBuffer;
328 functions->ReadBuffer = intelReadBuffer;