gallium: Add blending to pipe blit
[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
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_manager_validate_framebuffers(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 blit.alpha_blend = FALSE;
196
197 if (mask & GL_COLOR_BUFFER_BIT) {
198 struct gl_renderbuffer_attachment *srcAtt =
199 &readFB->Attachment[readFB->_ColorReadBufferIndex];
200
201 blit.mask = PIPE_MASK_RGBA;
202
203 if (srcAtt->Type == GL_TEXTURE) {
204 struct st_texture_object *srcObj = st_texture_object(srcAtt->Texture);
205 GLuint i;
206
207 if (!srcObj || !srcObj->pt) {
208 return;
209 }
210
211 for (i = 0; i < drawFB->_NumColorDrawBuffers; i++) {
212 struct st_renderbuffer *dstRb =
213 st_renderbuffer(drawFB->_ColorDrawBuffers[i]);
214
215 if (dstRb) {
216 struct pipe_surface *dstSurf = dstRb->surface;
217
218 if (dstSurf) {
219 blit.dst.resource = dstSurf->texture;
220 blit.dst.level = dstSurf->u.tex.level;
221 blit.dst.box.z = dstSurf->u.tex.first_layer;
222 blit.dst.format = util_format_linear(dstSurf->format);
223
224 blit.src.resource = srcObj->pt;
225 blit.src.level = srcAtt->TextureLevel;
226 blit.src.box.z = srcAtt->Zoffset + srcAtt->CubeMapFace;
227 blit.src.format = util_format_linear(srcObj->pt->format);
228
229 st_adjust_blit_for_msaa_resolve(&blit);
230
231 st->pipe->blit(st->pipe, &blit);
232 }
233 }
234 }
235 }
236 else {
237 struct st_renderbuffer *srcRb =
238 st_renderbuffer(readFB->_ColorReadBuffer);
239 struct pipe_surface *srcSurf;
240 GLuint i;
241
242 if (!srcRb || !srcRb->surface) {
243 return;
244 }
245
246 srcSurf = srcRb->surface;
247
248 for (i = 0; i < drawFB->_NumColorDrawBuffers; i++) {
249 struct st_renderbuffer *dstRb =
250 st_renderbuffer(drawFB->_ColorDrawBuffers[i]);
251
252 if (dstRb) {
253 struct pipe_surface *dstSurf = dstRb->surface;
254
255 if (dstSurf) {
256 blit.dst.resource = dstSurf->texture;
257 blit.dst.level = dstSurf->u.tex.level;
258 blit.dst.box.z = dstSurf->u.tex.first_layer;
259 blit.dst.format = util_format_linear(dstSurf->format);
260
261 blit.src.resource = srcSurf->texture;
262 blit.src.level = srcSurf->u.tex.level;
263 blit.src.box.z = srcSurf->u.tex.first_layer;
264 blit.src.format = util_format_linear(srcSurf->format);
265
266 st_adjust_blit_for_msaa_resolve(&blit);
267
268 st->pipe->blit(st->pipe, &blit);
269 }
270 }
271 }
272 }
273 }
274
275 if (mask & depthStencil) {
276 /* depth and/or stencil blit */
277
278 /* get src/dst depth surfaces */
279 struct st_renderbuffer *srcDepthRb =
280 st_renderbuffer(readFB->Attachment[BUFFER_DEPTH].Renderbuffer);
281 struct st_renderbuffer *dstDepthRb =
282 st_renderbuffer(drawFB->Attachment[BUFFER_DEPTH].Renderbuffer);
283 struct pipe_surface *dstDepthSurf =
284 dstDepthRb ? dstDepthRb->surface : NULL;
285
286 struct st_renderbuffer *srcStencilRb =
287 st_renderbuffer(readFB->Attachment[BUFFER_STENCIL].Renderbuffer);
288 struct st_renderbuffer *dstStencilRb =
289 st_renderbuffer(drawFB->Attachment[BUFFER_STENCIL].Renderbuffer);
290 struct pipe_surface *dstStencilSurf =
291 dstStencilRb ? dstStencilRb->surface : NULL;
292
293 if (_mesa_has_depthstencil_combined(readFB) &&
294 _mesa_has_depthstencil_combined(drawFB)) {
295 blit.mask = 0;
296 if (mask & GL_DEPTH_BUFFER_BIT)
297 blit.mask |= PIPE_MASK_Z;
298 if (mask & GL_STENCIL_BUFFER_BIT)
299 blit.mask |= PIPE_MASK_S;
300
301 blit.dst.resource = dstDepthSurf->texture;
302 blit.dst.level = dstDepthSurf->u.tex.level;
303 blit.dst.box.z = dstDepthSurf->u.tex.first_layer;
304 blit.dst.format = dstDepthSurf->format;
305
306 blit.src.resource = srcDepthRb->texture;
307 blit.src.level = srcDepthRb->surface->u.tex.level;
308 blit.src.box.z = srcDepthRb->surface->u.tex.first_layer;
309 blit.src.format = srcDepthRb->surface->format;
310
311 st->pipe->blit(st->pipe, &blit);
312 }
313 else {
314 /* blitting depth and stencil separately */
315
316 if (mask & GL_DEPTH_BUFFER_BIT) {
317 blit.mask = PIPE_MASK_Z;
318
319 blit.dst.resource = dstDepthSurf->texture;
320 blit.dst.level = dstDepthSurf->u.tex.level;
321 blit.dst.box.z = dstDepthSurf->u.tex.first_layer;
322 blit.dst.format = dstDepthSurf->format;
323
324 blit.src.resource = srcDepthRb->texture;
325 blit.src.level = srcDepthRb->surface->u.tex.level;
326 blit.src.box.z = srcDepthRb->surface->u.tex.first_layer;
327 blit.src.format = srcDepthRb->surface->format;
328
329 st->pipe->blit(st->pipe, &blit);
330 }
331
332 if (mask & GL_STENCIL_BUFFER_BIT) {
333 blit.mask = PIPE_MASK_S;
334
335 blit.dst.resource = dstStencilSurf->texture;
336 blit.dst.level = dstStencilSurf->u.tex.level;
337 blit.dst.box.z = dstStencilSurf->u.tex.first_layer;
338 blit.dst.format = dstStencilSurf->format;
339
340 blit.src.resource = srcStencilRb->texture;
341 blit.src.level = srcStencilRb->surface->u.tex.level;
342 blit.src.box.z = srcStencilRb->surface->u.tex.first_layer;
343 blit.src.format = srcStencilRb->surface->format;
344
345 st->pipe->blit(st->pipe, &blit);
346 }
347 }
348 }
349 }
350
351
352 void
353 st_init_blit_functions(struct dd_function_table *functions)
354 {
355 functions->BlitFramebuffer = st_BlitFramebuffer;
356 }