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