mesa/st: leave current query enabled during glBlitFramebuffer
[mesa.git] / src / mesa / state_tracker / st_cb_blit.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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
37 #include "st_context.h"
38 #include "st_texture.h"
39 #include "st_cb_blit.h"
40 #include "st_cb_fbo.h"
41 #include "st_atom.h"
42
43 #include "util/u_format.h"
44
45
46 static void
47 st_adjust_blit_for_msaa_resolve(struct pipe_blit_info *blit)
48 {
49 /* Even though we do multisample resolves at the time of the blit, OpenGL
50 * specification defines them as if they happen at the time of rendering,
51 * which means that the type of averaging we do during the resolve should
52 * only depend on the source format; the destination format should be
53 * ignored. But, specification doesn't seem to be strict about it.
54 *
55 * It has been observed that mulitisample resolves produce slightly better
56 * looking images when averaging is done using destination format. NVIDIA's
57 * proprietary OpenGL driver also follows this approach.
58 *
59 * When multisampling, if the source and destination formats are equal
60 * (aside from the color space), we choose to blit in sRGB space to get
61 * this higher quality image.
62 */
63 if (blit->src.resource->nr_samples > 1 &&
64 blit->dst.resource->nr_samples <= 1) {
65 blit->dst.format = blit->dst.resource->format;
66
67 if (util_format_is_srgb(blit->dst.resource->format))
68 blit->src.format = util_format_srgb(blit->src.resource->format);
69 else
70 blit->src.format = util_format_linear(blit->src.resource->format);
71 }
72 }
73
74 static void
75 st_BlitFramebuffer(struct gl_context *ctx,
76 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
77 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
78 GLbitfield mask, GLenum filter)
79 {
80 const GLbitfield depthStencil = (GL_DEPTH_BUFFER_BIT |
81 GL_STENCIL_BUFFER_BIT);
82 struct st_context *st = st_context(ctx);
83 const uint pFilter = ((filter == GL_NEAREST)
84 ? PIPE_TEX_FILTER_NEAREST
85 : PIPE_TEX_FILTER_LINEAR);
86 struct gl_framebuffer *readFB = ctx->ReadBuffer;
87 struct gl_framebuffer *drawFB = ctx->DrawBuffer;
88 struct {
89 GLint srcX0, srcY0, srcX1, srcY1;
90 GLint dstX0, dstY0, dstX1, dstY1;
91 } clip;
92 struct pipe_blit_info blit;
93
94 st_validate_state(st);
95
96 clip.srcX0 = srcX0;
97 clip.srcY0 = srcY0;
98 clip.srcX1 = srcX1;
99 clip.srcY1 = srcY1;
100 clip.dstX0 = dstX0;
101 clip.dstY0 = dstY0;
102 clip.dstX1 = dstX1;
103 clip.dstY1 = dstY1;
104
105 /* NOTE: If the src and dst dimensions don't match, we cannot simply adjust
106 * the integer coordinates to account for clipping (or scissors) because that
107 * would make us cut off fractional parts, affecting the result of the blit.
108 *
109 * XXX: This should depend on mask !
110 */
111 if (!_mesa_clip_blit(ctx,
112 &clip.srcX0, &clip.srcY0, &clip.srcX1, &clip.srcY1,
113 &clip.dstX0, &clip.dstY0, &clip.dstX1, &clip.dstY1)) {
114 return; /* nothing to draw/blit */
115 }
116 blit.scissor_enable =
117 (dstX0 != clip.dstX0) ||
118 (dstY0 != clip.dstY0) ||
119 (dstX1 != clip.dstX1) ||
120 (dstY1 != clip.dstY1);
121
122 if (st_fb_orientation(drawFB) == Y_0_TOP) {
123 /* invert Y for dest */
124 dstY0 = drawFB->Height - dstY0;
125 dstY1 = drawFB->Height - dstY1;
126 /* invert Y for clip */
127 clip.dstY0 = drawFB->Height - clip.dstY0;
128 clip.dstY1 = drawFB->Height - clip.dstY1;
129 }
130 if (blit.scissor_enable) {
131 blit.scissor.minx = MIN2(clip.dstX0, clip.dstX1);
132 blit.scissor.miny = MIN2(clip.dstY0, clip.dstY1);
133 blit.scissor.maxx = MAX2(clip.dstX0, clip.dstX1);
134 blit.scissor.maxy = MAX2(clip.dstY0, clip.dstY1);
135 #if 0
136 debug_printf("scissor = (%i,%i)-(%i,%i)\n",
137 blit.scissor.minx,blit.scissor.miny,
138 blit.scissor.maxx,blit.scissor.maxy);
139 #endif
140 }
141
142 if (st_fb_orientation(readFB) == Y_0_TOP) {
143 /* invert Y for src */
144 srcY0 = readFB->Height - srcY0;
145 srcY1 = readFB->Height - srcY1;
146 }
147
148 if (srcY0 > srcY1 && dstY0 > dstY1) {
149 /* Both src and dst are upside down. Swap Y to make it
150 * right-side up to increase odds of using a fast path.
151 * Recall that all Gallium raster coords have Y=0=top.
152 */
153 GLint tmp;
154 tmp = srcY0;
155 srcY0 = srcY1;
156 srcY1 = tmp;
157 tmp = dstY0;
158 dstY0 = dstY1;
159 dstY1 = tmp;
160 }
161
162 blit.src.box.depth = 1;
163 blit.dst.box.depth = 1;
164
165 /* Destination dimensions have to be positive: */
166 if (dstX0 < dstX1) {
167 blit.dst.box.x = dstX0;
168 blit.src.box.x = srcX0;
169 blit.dst.box.width = dstX1 - dstX0;
170 blit.src.box.width = srcX1 - srcX0;
171 } else {
172 blit.dst.box.x = dstX1;
173 blit.src.box.x = srcX1;
174 blit.dst.box.width = dstX0 - dstX1;
175 blit.src.box.width = srcX0 - srcX1;
176 }
177 if (dstY0 < dstY1) {
178 blit.dst.box.y = dstY0;
179 blit.src.box.y = srcY0;
180 blit.dst.box.height = dstY1 - dstY0;
181 blit.src.box.height = srcY1 - srcY0;
182 } else {
183 blit.dst.box.y = dstY1;
184 blit.src.box.y = srcY1;
185 blit.dst.box.height = dstY0 - dstY1;
186 blit.src.box.height = srcY0 - srcY1;
187 }
188
189 blit.filter = pFilter;
190 blit.render_condition_enable = TRUE;
191
192 if (mask & GL_COLOR_BUFFER_BIT) {
193 struct gl_renderbuffer_attachment *srcAtt =
194 &readFB->Attachment[readFB->_ColorReadBufferIndex];
195
196 blit.mask = PIPE_MASK_RGBA;
197
198 if (srcAtt->Type == GL_TEXTURE) {
199 struct st_texture_object *srcObj = st_texture_object(srcAtt->Texture);
200 GLuint i;
201
202 if (!srcObj || !srcObj->pt) {
203 return;
204 }
205
206 for (i = 0; i < drawFB->_NumColorDrawBuffers; i++) {
207 struct st_renderbuffer *dstRb =
208 st_renderbuffer(drawFB->_ColorDrawBuffers[i]);
209
210 if (dstRb) {
211 struct pipe_surface *dstSurf = dstRb->surface;
212
213 if (dstSurf) {
214 blit.dst.resource = dstSurf->texture;
215 blit.dst.level = dstSurf->u.tex.level;
216 blit.dst.box.z = dstSurf->u.tex.first_layer;
217 blit.dst.format = util_format_linear(dstSurf->format);
218
219 blit.src.resource = srcObj->pt;
220 blit.src.level = srcAtt->TextureLevel;
221 blit.src.box.z = srcAtt->Zoffset + srcAtt->CubeMapFace;
222 blit.src.format = util_format_linear(srcObj->pt->format);
223
224 st_adjust_blit_for_msaa_resolve(&blit);
225
226 st->pipe->blit(st->pipe, &blit);
227 }
228 }
229 }
230 }
231 else {
232 struct st_renderbuffer *srcRb =
233 st_renderbuffer(readFB->_ColorReadBuffer);
234 struct pipe_surface *srcSurf;
235 GLuint i;
236
237 if (!srcRb || !srcRb->surface) {
238 return;
239 }
240
241 srcSurf = srcRb->surface;
242
243 for (i = 0; i < drawFB->_NumColorDrawBuffers; i++) {
244 struct st_renderbuffer *dstRb =
245 st_renderbuffer(drawFB->_ColorDrawBuffers[i]);
246
247 if (dstRb) {
248 struct pipe_surface *dstSurf = dstRb->surface;
249
250 if (dstSurf) {
251 blit.dst.resource = dstSurf->texture;
252 blit.dst.level = dstSurf->u.tex.level;
253 blit.dst.box.z = dstSurf->u.tex.first_layer;
254 blit.dst.format = util_format_linear(dstSurf->format);
255
256 blit.src.resource = srcSurf->texture;
257 blit.src.level = srcSurf->u.tex.level;
258 blit.src.box.z = srcSurf->u.tex.first_layer;
259 blit.src.format = util_format_linear(srcSurf->format);
260
261 st_adjust_blit_for_msaa_resolve(&blit);
262
263 st->pipe->blit(st->pipe, &blit);
264 }
265 }
266 }
267 }
268 }
269
270 if (mask & depthStencil) {
271 /* depth and/or stencil blit */
272
273 /* get src/dst depth surfaces */
274 struct st_renderbuffer *srcDepthRb =
275 st_renderbuffer(readFB->Attachment[BUFFER_DEPTH].Renderbuffer);
276 struct st_renderbuffer *dstDepthRb =
277 st_renderbuffer(drawFB->Attachment[BUFFER_DEPTH].Renderbuffer);
278 struct pipe_surface *dstDepthSurf =
279 dstDepthRb ? dstDepthRb->surface : NULL;
280
281 struct st_renderbuffer *srcStencilRb =
282 st_renderbuffer(readFB->Attachment[BUFFER_STENCIL].Renderbuffer);
283 struct st_renderbuffer *dstStencilRb =
284 st_renderbuffer(drawFB->Attachment[BUFFER_STENCIL].Renderbuffer);
285 struct pipe_surface *dstStencilSurf =
286 dstStencilRb ? dstStencilRb->surface : NULL;
287
288 if (_mesa_has_depthstencil_combined(readFB) &&
289 _mesa_has_depthstencil_combined(drawFB)) {
290 blit.mask = 0;
291 if (mask & GL_DEPTH_BUFFER_BIT)
292 blit.mask |= PIPE_MASK_Z;
293 if (mask & GL_STENCIL_BUFFER_BIT)
294 blit.mask |= PIPE_MASK_S;
295
296 blit.dst.resource = dstDepthSurf->texture;
297 blit.dst.level = dstDepthSurf->u.tex.level;
298 blit.dst.box.z = dstDepthSurf->u.tex.first_layer;
299 blit.dst.format = dstDepthSurf->format;
300
301 blit.src.resource = srcDepthRb->texture;
302 blit.src.level = srcDepthRb->surface->u.tex.level;
303 blit.src.box.z = srcDepthRb->surface->u.tex.first_layer;
304 blit.src.format = srcDepthRb->surface->format;
305
306 st->pipe->blit(st->pipe, &blit);
307 }
308 else {
309 /* blitting depth and stencil separately */
310
311 if (mask & GL_DEPTH_BUFFER_BIT) {
312 blit.mask = PIPE_MASK_Z;
313
314 blit.dst.resource = dstDepthSurf->texture;
315 blit.dst.level = dstDepthSurf->u.tex.level;
316 blit.dst.box.z = dstDepthSurf->u.tex.first_layer;
317 blit.dst.format = dstDepthSurf->format;
318
319 blit.src.resource = srcDepthRb->texture;
320 blit.src.level = srcDepthRb->surface->u.tex.level;
321 blit.src.box.z = srcDepthRb->surface->u.tex.first_layer;
322 blit.src.format = srcDepthRb->surface->format;
323
324 st->pipe->blit(st->pipe, &blit);
325 }
326
327 if (mask & GL_STENCIL_BUFFER_BIT) {
328 blit.mask = PIPE_MASK_S;
329
330 blit.dst.resource = dstStencilSurf->texture;
331 blit.dst.level = dstStencilSurf->u.tex.level;
332 blit.dst.box.z = dstStencilSurf->u.tex.first_layer;
333 blit.dst.format = dstStencilSurf->format;
334
335 blit.src.resource = srcStencilRb->texture;
336 blit.src.level = srcStencilRb->surface->u.tex.level;
337 blit.src.box.z = srcStencilRb->surface->u.tex.first_layer;
338 blit.src.format = srcStencilRb->surface->format;
339
340 st->pipe->blit(st->pipe, &blit);
341 }
342 }
343 }
344 }
345
346
347 void
348 st_init_blit_functions(struct dd_function_table *functions)
349 {
350 functions->BlitFramebuffer = st_BlitFramebuffer;
351 }