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