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