st/mesa: only resolve if number of samples is > 1
[mesa.git] / src / mesa / state_tracker / st_cb_blit.c
1 /**************************************************************************
2 *
3 * Copyright 2007 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 /*
29 * Authors:
30 * Brian Paul
31 */
32
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/macros.h"
36 #include "main/mfeatures.h"
37
38 #include "st_context.h"
39 #include "st_texture.h"
40 #include "st_cb_blit.h"
41 #include "st_cb_fbo.h"
42 #include "st_atom.h"
43
44 #include "util/u_blit.h"
45
46
47 void
48 st_init_blit(struct st_context *st)
49 {
50 st->blit = util_create_blit(st->pipe, st->cso_context);
51 }
52
53
54 void
55 st_destroy_blit(struct st_context *st)
56 {
57 util_destroy_blit(st->blit);
58 st->blit = NULL;
59 }
60
61
62 #if FEATURE_EXT_framebuffer_blit
63
64 static void
65 st_BlitFramebuffer_resolve(struct gl_context *ctx,
66 GLbitfield mask,
67 struct pipe_resolve_info *info)
68 {
69 const GLbitfield depthStencil = GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT;
70
71 struct st_context *st = st_context(ctx);
72
73 struct st_renderbuffer *srcRb, *dstRb;
74
75 if (mask & GL_COLOR_BUFFER_BIT) {
76 srcRb = st_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
77 dstRb = st_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
78
79 info->mask = PIPE_MASK_RGBA;
80
81 info->src.res = srcRb->texture;
82 info->src.layer = srcRb->surface->u.tex.first_layer;
83 info->dst.res = dstRb->texture;
84 info->dst.level = dstRb->surface->u.tex.level;
85 info->dst.layer = dstRb->surface->u.tex.first_layer;
86
87 st->pipe->resource_resolve(st->pipe, info);
88 }
89
90 if (mask & depthStencil) {
91 struct gl_renderbuffer_attachment *srcDepth, *srcStencil;
92 struct gl_renderbuffer_attachment *dstDepth, *dstStencil;
93 boolean combined;
94
95 srcDepth = &ctx->ReadBuffer->Attachment[BUFFER_DEPTH];
96 dstDepth = &ctx->DrawBuffer->Attachment[BUFFER_DEPTH];
97 srcStencil = &ctx->ReadBuffer->Attachment[BUFFER_STENCIL];
98 dstStencil = &ctx->DrawBuffer->Attachment[BUFFER_STENCIL];
99
100 combined =
101 st_is_depth_stencil_combined(srcDepth, srcStencil) &&
102 st_is_depth_stencil_combined(dstDepth, dstStencil);
103
104 if ((mask & GL_DEPTH_BUFFER_BIT) || combined) {
105 /* resolve depth and, if combined and requested, stencil as well */
106 srcRb = st_renderbuffer(srcDepth->Renderbuffer);
107 dstRb = st_renderbuffer(dstDepth->Renderbuffer);
108
109 info->mask = (mask & GL_DEPTH_BUFFER_BIT) ? PIPE_MASK_Z : 0;
110 if (combined && (mask & GL_STENCIL_BUFFER_BIT)) {
111 mask &= ~GL_STENCIL_BUFFER_BIT;
112 info->mask |= PIPE_MASK_S;
113 }
114
115 info->src.res = srcRb->texture;
116 info->src.layer = srcRb->surface->u.tex.first_layer;
117 info->dst.res = dstRb->texture;
118 info->dst.level = dstRb->surface->u.tex.level;
119 info->dst.layer = dstRb->surface->u.tex.first_layer;
120
121 st->pipe->resource_resolve(st->pipe, info);
122 }
123
124 if (mask & GL_STENCIL_BUFFER_BIT) {
125 /* resolve separate stencil buffer */
126 srcRb = st_renderbuffer(srcStencil->Renderbuffer);
127 dstRb = st_renderbuffer(dstStencil->Renderbuffer);
128
129 info->mask = PIPE_MASK_S;
130
131 info->src.res = srcRb->texture;
132 info->src.layer = srcRb->surface->u.tex.first_layer;
133 info->dst.res = dstRb->texture;
134 info->dst.level = dstRb->surface->u.tex.level;
135 info->dst.layer = dstRb->surface->u.tex.first_layer;
136
137 st->pipe->resource_resolve(st->pipe, info);
138 }
139 }
140 }
141
142 static void
143 st_BlitFramebuffer(struct gl_context *ctx,
144 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
145 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
146 GLbitfield mask, GLenum filter)
147 {
148 const GLbitfield depthStencil = (GL_DEPTH_BUFFER_BIT |
149 GL_STENCIL_BUFFER_BIT);
150 struct st_context *st = st_context(ctx);
151 const uint pFilter = ((filter == GL_NEAREST)
152 ? PIPE_TEX_MIPFILTER_NEAREST
153 : PIPE_TEX_MIPFILTER_LINEAR);
154 struct gl_framebuffer *readFB = ctx->ReadBuffer;
155 struct gl_framebuffer *drawFB = ctx->DrawBuffer;
156
157 st_validate_state(st);
158
159 if (!_mesa_clip_blit(ctx, &srcX0, &srcY0, &srcX1, &srcY1,
160 &dstX0, &dstY0, &dstX1, &dstY1)) {
161 return; /* nothing to draw/blit */
162 }
163
164 if (st_fb_orientation(drawFB) == Y_0_TOP) {
165 /* invert Y for dest */
166 dstY0 = drawFB->Height - dstY0;
167 dstY1 = drawFB->Height - dstY1;
168 }
169
170 if (st_fb_orientation(readFB) == Y_0_TOP) {
171 /* invert Y for src */
172 srcY0 = readFB->Height - srcY0;
173 srcY1 = readFB->Height - srcY1;
174 }
175
176 /* Disable conditional rendering. */
177 if (st->render_condition) {
178 st->pipe->render_condition(st->pipe, NULL, 0);
179 }
180
181 if (readFB->Visual.sampleBuffers > drawFB->Visual.sampleBuffers &&
182 readFB->Visual.samples > 1) {
183 struct pipe_resolve_info info;
184
185 if (dstX0 < dstX1) {
186 info.dst.x0 = dstX0;
187 info.dst.x1 = dstX1;
188 info.src.x0 = srcX0;
189 info.src.x1 = srcX1;
190 } else {
191 info.dst.x0 = dstX1;
192 info.dst.x1 = dstX0;
193 info.src.x0 = srcX1;
194 info.src.x1 = srcX0;
195 }
196 if (dstY0 < dstY1) {
197 info.dst.y0 = dstY0;
198 info.dst.y1 = dstY1;
199 info.src.y0 = srcY0;
200 info.src.y1 = srcY1;
201 } else {
202 info.dst.y0 = dstY1;
203 info.dst.y1 = dstY0;
204 info.src.y0 = srcY1;
205 info.src.y1 = srcY0;
206 }
207
208 st_BlitFramebuffer_resolve(ctx, mask, &info); /* filter doesn't apply */
209
210 goto done;
211 }
212
213 if (srcY0 > srcY1 && dstY0 > dstY1) {
214 /* Both src and dst are upside down. Swap Y to make it
215 * right-side up to increase odds of using a fast path.
216 * Recall that all Gallium raster coords have Y=0=top.
217 */
218 GLint tmp;
219 tmp = srcY0;
220 srcY0 = srcY1;
221 srcY1 = tmp;
222 tmp = dstY0;
223 dstY0 = dstY1;
224 dstY1 = tmp;
225 }
226
227 if (mask & GL_COLOR_BUFFER_BIT) {
228 struct gl_renderbuffer_attachment *srcAtt =
229 &readFB->Attachment[readFB->_ColorReadBufferIndex];
230
231 if(srcAtt->Type == GL_TEXTURE) {
232 struct st_texture_object *srcObj =
233 st_texture_object(srcAtt->Texture);
234 struct st_renderbuffer *dstRb =
235 st_renderbuffer(drawFB->_ColorDrawBuffers[0]);
236 struct pipe_surface *dstSurf = dstRb->surface;
237
238 if (!srcObj->pt)
239 goto done;
240
241 util_blit_pixels(st->blit, srcObj->pt, srcAtt->TextureLevel,
242 srcX0, srcY0, srcX1, srcY1,
243 srcAtt->Zoffset + srcAtt->CubeMapFace,
244 dstSurf, dstX0, dstY0, dstX1, dstY1,
245 0.0, pFilter);
246 }
247 else {
248 struct st_renderbuffer *srcRb =
249 st_renderbuffer(readFB->_ColorReadBuffer);
250 struct st_renderbuffer *dstRb =
251 st_renderbuffer(drawFB->_ColorDrawBuffers[0]);
252 struct pipe_surface *srcSurf = srcRb->surface;
253 struct pipe_surface *dstSurf = dstRb->surface;
254
255 util_blit_pixels(st->blit,
256 srcRb->texture, srcSurf->u.tex.level,
257 srcX0, srcY0, srcX1, srcY1,
258 srcSurf->u.tex.first_layer,
259 dstSurf, dstX0, dstY0, dstX1, dstY1,
260 0.0, pFilter);
261 }
262 }
263
264 if (mask & depthStencil) {
265 /* depth and/or stencil blit */
266
267 /* get src/dst depth surfaces */
268 struct gl_renderbuffer_attachment *srcDepth =
269 &readFB->Attachment[BUFFER_DEPTH];
270 struct gl_renderbuffer_attachment *dstDepth =
271 &drawFB->Attachment[BUFFER_DEPTH];
272 struct gl_renderbuffer_attachment *srcStencil =
273 &readFB->Attachment[BUFFER_STENCIL];
274 struct gl_renderbuffer_attachment *dstStencil =
275 &drawFB->Attachment[BUFFER_STENCIL];
276
277 struct st_renderbuffer *srcDepthRb =
278 st_renderbuffer(readFB->Attachment[BUFFER_DEPTH].Renderbuffer);
279 struct st_renderbuffer *dstDepthRb =
280 st_renderbuffer(drawFB->Attachment[BUFFER_DEPTH].Renderbuffer);
281 struct pipe_surface *dstDepthSurf =
282 dstDepthRb ? dstDepthRb->surface : NULL;
283
284 if ((mask & depthStencil) == depthStencil &&
285 st_is_depth_stencil_combined(srcDepth, srcStencil) &&
286 st_is_depth_stencil_combined(dstDepth, dstStencil)) {
287
288 /* Blitting depth and stencil values between combined
289 * depth/stencil buffers. This is the ideal case for such buffers.
290 */
291 util_blit_pixels(st->blit,
292 srcDepthRb->texture,
293 srcDepthRb->surface->u.tex.level,
294 srcX0, srcY0, srcX1, srcY1,
295 srcDepthRb->surface->u.tex.first_layer,
296 dstDepthSurf, dstX0, dstY0, dstX1, dstY1,
297 0.0, pFilter);
298 }
299 else {
300 /* blitting depth and stencil separately */
301
302 if (mask & GL_DEPTH_BUFFER_BIT) {
303 util_blit_pixels(st->blit, srcDepthRb->texture,
304 srcDepthRb->surface->u.tex.level,
305 srcX0, srcY0, srcX1, srcY1,
306 srcDepthRb->surface->u.tex.first_layer,
307 dstDepthSurf, dstX0, dstY0, dstX1, dstY1,
308 0.0, pFilter);
309 }
310
311 if (mask & GL_STENCIL_BUFFER_BIT) {
312 /* blit stencil only */
313 _mesa_problem(ctx, "st_BlitFramebuffer(STENCIL) not completed");
314 }
315 }
316 }
317
318 done:
319 /* Restore conditional rendering state. */
320 if (st->render_condition) {
321 st->pipe->render_condition(st->pipe, st->render_condition,
322 st->condition_mode);
323 }
324 }
325
326
327 void
328 st_init_blit_functions(struct dd_function_table *functions)
329 {
330 functions->BlitFramebuffer = st_BlitFramebuffer;
331 }
332
333 #endif /* FEATURE_EXT_framebuffer_blit */