1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/macros.h"
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"
45 #include "util/u_format.h"
49 st_adjust_blit_for_msaa_resolve(struct pipe_blit_info
*blit
)
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.
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.
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.
65 if (blit
->src
.resource
->nr_samples
> 1 &&
66 blit
->dst
.resource
->nr_samples
<= 1) {
67 blit
->dst
.format
= blit
->dst
.resource
->format
;
69 if (util_format_is_srgb(blit
->dst
.resource
->format
))
70 blit
->src
.format
= util_format_srgb(blit
->src
.resource
->format
);
72 blit
->src
.format
= util_format_linear(blit
->src
.resource
->format
);
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
)
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
);
91 GLint srcX0
, srcY0
, srcX1
, srcY1
;
92 GLint dstX0
, dstY0
, dstX1
, dstY1
;
94 struct pipe_blit_info blit
;
96 st_manager_validate_framebuffers(st
);
98 /* Make sure bitmap rendering has landed in the framebuffers */
99 st_flush_bitmap_cache(st
);
100 st_invalidate_readpix_cache(st
);
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.
115 * XXX: This should depend on mask !
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 */
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
);
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
;
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
);
143 debug_printf("scissor = (%i,%i)-(%i,%i)\n",
144 blit
.scissor
.minx
,blit
.scissor
.miny
,
145 blit
.scissor
.maxx
,blit
.scissor
.maxy
);
149 if (st_fb_orientation(readFB
) == Y_0_TOP
) {
150 /* invert Y for src */
151 srcY0
= readFB
->Height
- srcY0
;
152 srcY1
= readFB
->Height
- srcY1
;
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.
169 blit
.src
.box
.depth
= 1;
170 blit
.dst
.box
.depth
= 1;
172 /* Destination dimensions have to be positive: */
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
;
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
;
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
;
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
;
196 if (drawFB
!= ctx
->WinSysDrawBuffer
)
197 st_window_rectangles_to_blit(ctx
, &blit
);
199 blit
.filter
= pFilter
;
200 blit
.render_condition_enable
= TRUE
;
201 blit
.alpha_blend
= FALSE
;
203 if (mask
& GL_COLOR_BUFFER_BIT
) {
204 struct gl_renderbuffer_attachment
*srcAtt
=
205 &readFB
->Attachment
[readFB
->_ColorReadBufferIndex
];
207 blit
.mask
= PIPE_MASK_RGBA
;
209 if (srcAtt
->Type
== GL_TEXTURE
) {
210 struct st_texture_object
*srcObj
= st_texture_object(srcAtt
->Texture
);
213 if (!srcObj
|| !srcObj
->pt
) {
217 for (i
= 0; i
< drawFB
->_NumColorDrawBuffers
; i
++) {
218 struct st_renderbuffer
*dstRb
=
219 st_renderbuffer(drawFB
->_ColorDrawBuffers
[i
]);
222 struct pipe_surface
*dstSurf
= dstRb
->surface
;
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
);
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
);
235 st_adjust_blit_for_msaa_resolve(&blit
);
237 st
->pipe
->blit(st
->pipe
, &blit
);
238 dstRb
->defined
= true; /* front buffer tracking */
244 struct st_renderbuffer
*srcRb
=
245 st_renderbuffer(readFB
->_ColorReadBuffer
);
246 struct pipe_surface
*srcSurf
;
249 if (!srcRb
|| !srcRb
->surface
) {
253 srcSurf
= srcRb
->surface
;
255 for (i
= 0; i
< drawFB
->_NumColorDrawBuffers
; i
++) {
256 struct st_renderbuffer
*dstRb
=
257 st_renderbuffer(drawFB
->_ColorDrawBuffers
[i
]);
260 struct pipe_surface
*dstSurf
= dstRb
->surface
;
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
);
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
);
273 st_adjust_blit_for_msaa_resolve(&blit
);
275 st
->pipe
->blit(st
->pipe
, &blit
);
276 dstRb
->defined
= true; /* front buffer tracking */
283 if (mask
& depthStencil
) {
284 /* depth and/or stencil blit */
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
;
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
;
301 if (_mesa_has_depthstencil_combined(readFB
) &&
302 _mesa_has_depthstencil_combined(drawFB
)) {
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
;
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
;
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
;
319 st
->pipe
->blit(st
->pipe
, &blit
);
322 /* blitting depth and stencil separately */
324 if (mask
& GL_DEPTH_BUFFER_BIT
) {
325 blit
.mask
= PIPE_MASK_Z
;
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
;
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
;
337 st
->pipe
->blit(st
->pipe
, &blit
);
340 if (mask
& GL_STENCIL_BUFFER_BIT
) {
341 blit
.mask
= PIPE_MASK_S
;
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
;
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
;
353 st
->pipe
->blit(st
->pipe
, &blit
);
361 st_init_blit_functions(struct dd_function_table
*functions
)
363 functions
->BlitFramebuffer
= st_BlitFramebuffer
;