Merge remote-tracking branch 'origin/master' into pipe-video
[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 bool fb_has_hiz = intel_framebuffer_has_hiz(fb);
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 ***/
171 if (fb->_DepthBuffer && fb->_DepthBuffer->Wrapped) {
172 irbDepth = intel_renderbuffer(fb->_DepthBuffer->Wrapped);
173 if (irbDepth && irbDepth->region) {
174 assert(!fb_has_hiz || irbDepth->Base.Format != MESA_FORMAT_S8_Z24);
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 ***/
192 if (fb->_StencilBuffer && fb->_StencilBuffer->Wrapped) {
193 irbStencil = intel_renderbuffer(fb->_StencilBuffer->Wrapped);
194 if (irbStencil && irbStencil->region) {
195 if (!intel->has_separate_stencil)
196 assert(irbStencil->Base.Format == MESA_FORMAT_S8_Z24);
197 if (fb_has_hiz || intel->must_use_separate_stencil)
198 assert(irbStencil->Base.Format == MESA_FORMAT_S8);
199 if (irbStencil->Base.Format == MESA_FORMAT_S8)
200 assert(intel->has_separate_stencil);
201 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_FALSE);
202 }
203 else {
204 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_TRUE);
205 }
206 }
207 else {
208 /* XXX FBO: instead of FALSE, pass ctx->Stencil._Enabled ??? */
209 FALLBACK(intel, INTEL_FALLBACK_STENCIL_BUFFER, GL_FALSE);
210 }
211
212 /* If we have a (packed) stencil buffer attached but no depth buffer,
213 * we still need to set up the shared depth/stencil state so we can use it.
214 */
215 if (depthRegion == NULL && irbStencil && irbStencil->region
216 && irbStencil->Base.Format == MESA_FORMAT_S8_Z24) {
217 depthRegion = irbStencil->region;
218 }
219
220 /*
221 * Update depth and stencil test state
222 */
223 if (ctx->Driver.Enable) {
224 ctx->Driver.Enable(ctx, GL_DEPTH_TEST,
225 (ctx->Depth.Test && fb->Visual.depthBits > 0));
226 ctx->Driver.Enable(ctx, GL_STENCIL_TEST,
227 (ctx->Stencil.Enabled && fb->Visual.stencilBits > 0));
228 }
229 else {
230 /* Mesa's Stencil._Enabled field is updated when
231 * _NEW_BUFFERS | _NEW_STENCIL, but i965 code assumes that the value
232 * only changes with _NEW_STENCIL (which seems sensible). So flag it
233 * here since this is the _NEW_BUFFERS path.
234 */
235 intel->NewGLState |= (_NEW_DEPTH | _NEW_STENCIL);
236 }
237
238 intel->vtbl.set_draw_region(intel, colorRegions, depthRegion,
239 fb->_NumColorDrawBuffers);
240 intel->NewGLState |= _NEW_BUFFERS;
241
242 /* update viewport since it depends on window size */
243 #ifdef I915
244 intelCalcViewport(ctx);
245 #else
246 intel->NewGLState |= _NEW_VIEWPORT;
247 #endif
248 /* Set state we know depends on drawable parameters:
249 */
250 if (ctx->Driver.Scissor)
251 ctx->Driver.Scissor(ctx, ctx->Scissor.X, ctx->Scissor.Y,
252 ctx->Scissor.Width, ctx->Scissor.Height);
253 intel->NewGLState |= _NEW_SCISSOR;
254
255 if (ctx->Driver.DepthRange)
256 ctx->Driver.DepthRange(ctx,
257 ctx->Viewport.Near,
258 ctx->Viewport.Far);
259
260 /* Update culling direction which changes depending on the
261 * orientation of the buffer:
262 */
263 if (ctx->Driver.FrontFace)
264 ctx->Driver.FrontFace(ctx, ctx->Polygon.FrontFace);
265 else
266 intel->NewGLState |= _NEW_POLYGON;
267 }
268
269
270 static void
271 intelDrawBuffer(struct gl_context * ctx, GLenum mode)
272 {
273 if ((ctx->DrawBuffer != NULL) && (ctx->DrawBuffer->Name == 0)) {
274 struct intel_context *const intel = intel_context(ctx);
275 const GLboolean was_front_buffer_rendering =
276 intel->is_front_buffer_rendering;
277
278 intel->is_front_buffer_rendering = (mode == GL_FRONT_LEFT)
279 || (mode == GL_FRONT);
280
281 /* If we weren't front-buffer rendering before but we are now,
282 * invalidate our DRI drawable so we'll ask for new buffers
283 * (including the fake front) before we start rendering again.
284 */
285 if (!was_front_buffer_rendering && intel->is_front_buffer_rendering)
286 dri2InvalidateDrawable(intel->driContext->driDrawablePriv);
287 }
288
289 intel_draw_buffer(ctx, ctx->DrawBuffer);
290 }
291
292
293 static void
294 intelReadBuffer(struct gl_context * ctx, GLenum mode)
295 {
296 if ((ctx->DrawBuffer != NULL) && (ctx->DrawBuffer->Name == 0)) {
297 struct intel_context *const intel = intel_context(ctx);
298 const GLboolean was_front_buffer_reading =
299 intel->is_front_buffer_reading;
300
301 intel->is_front_buffer_reading = (mode == GL_FRONT_LEFT)
302 || (mode == GL_FRONT);
303
304 /* If we weren't front-buffer reading before but we are now,
305 * invalidate our DRI drawable so we'll ask for new buffers
306 * (including the fake front) before we start reading again.
307 */
308 if (!was_front_buffer_reading && intel->is_front_buffer_reading)
309 dri2InvalidateDrawable(intel->driContext->driReadablePriv);
310 }
311 }
312
313
314 void
315 intelInitBufferFuncs(struct dd_function_table *functions)
316 {
317 functions->DrawBuffer = intelDrawBuffer;
318 functions->ReadBuffer = intelReadBuffer;
319 }