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