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_blit.h"
40 #include "st_cb_fbo.h"
43 #include "util/u_format.h"
47 st_adjust_blit_for_msaa_resolve(struct pipe_blit_info
*blit
)
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.
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.
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.
63 if (blit
->src
.resource
->nr_samples
> 1 &&
64 blit
->dst
.resource
->nr_samples
<= 1) {
65 blit
->dst
.format
= blit
->dst
.resource
->format
;
67 if (util_format_is_srgb(blit
->dst
.resource
->format
))
68 blit
->src
.format
= util_format_srgb(blit
->src
.resource
->format
);
70 blit
->src
.format
= util_format_linear(blit
->src
.resource
->format
);
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
)
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
;
89 GLint srcX0
, srcY0
, srcX1
, srcY1
;
90 GLint dstX0
, dstY0
, dstX1
, dstY1
;
92 struct pipe_blit_info blit
;
94 st_validate_state(st
);
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.
109 * XXX: This should depend on mask !
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 */
116 blit
.scissor_enable
=
117 (dstX0
!= clip
.dstX0
) ||
118 (dstY0
!= clip
.dstY0
) ||
119 (dstX1
!= clip
.dstX1
) ||
120 (dstY1
!= clip
.dstY1
);
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
;
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
);
136 debug_printf("scissor = (%i,%i)-(%i,%i)\n",
137 blit
.scissor
.minx
,blit
.scissor
.miny
,
138 blit
.scissor
.maxx
,blit
.scissor
.maxy
);
142 if (st_fb_orientation(readFB
) == Y_0_TOP
) {
143 /* invert Y for src */
144 srcY0
= readFB
->Height
- srcY0
;
145 srcY1
= readFB
->Height
- srcY1
;
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.
162 blit
.src
.box
.depth
= 1;
163 blit
.dst
.box
.depth
= 1;
165 /* Destination dimensions have to be positive: */
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
;
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
;
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
;
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
;
189 blit
.filter
= pFilter
;
191 if (mask
& GL_COLOR_BUFFER_BIT
) {
192 struct gl_renderbuffer_attachment
*srcAtt
=
193 &readFB
->Attachment
[readFB
->_ColorReadBufferIndex
];
195 blit
.mask
= PIPE_MASK_RGBA
;
197 if (srcAtt
->Type
== GL_TEXTURE
) {
198 struct st_texture_object
*srcObj
= st_texture_object(srcAtt
->Texture
);
201 if (!srcObj
|| !srcObj
->pt
) {
205 for (i
= 0; i
< drawFB
->_NumColorDrawBuffers
; i
++) {
206 struct st_renderbuffer
*dstRb
=
207 st_renderbuffer(drawFB
->_ColorDrawBuffers
[i
]);
210 struct pipe_surface
*dstSurf
= dstRb
->surface
;
213 blit
.dst
.resource
= dstSurf
->texture
;
214 blit
.dst
.level
= dstSurf
->u
.tex
.level
;
215 blit
.dst
.box
.z
= dstSurf
->u
.tex
.first_layer
;
216 blit
.dst
.format
= util_format_linear(dstSurf
->format
);
218 blit
.src
.resource
= srcObj
->pt
;
219 blit
.src
.level
= srcAtt
->TextureLevel
;
220 blit
.src
.box
.z
= srcAtt
->Zoffset
+ srcAtt
->CubeMapFace
;
221 blit
.src
.format
= util_format_linear(srcObj
->pt
->format
);
223 st_adjust_blit_for_msaa_resolve(&blit
);
225 st
->pipe
->blit(st
->pipe
, &blit
);
231 struct st_renderbuffer
*srcRb
=
232 st_renderbuffer(readFB
->_ColorReadBuffer
);
233 struct pipe_surface
*srcSurf
;
236 if (!srcRb
|| !srcRb
->surface
) {
240 srcSurf
= srcRb
->surface
;
242 for (i
= 0; i
< drawFB
->_NumColorDrawBuffers
; i
++) {
243 struct st_renderbuffer
*dstRb
=
244 st_renderbuffer(drawFB
->_ColorDrawBuffers
[i
]);
247 struct pipe_surface
*dstSurf
= dstRb
->surface
;
250 blit
.dst
.resource
= dstSurf
->texture
;
251 blit
.dst
.level
= dstSurf
->u
.tex
.level
;
252 blit
.dst
.box
.z
= dstSurf
->u
.tex
.first_layer
;
253 blit
.dst
.format
= util_format_linear(dstSurf
->format
);
255 blit
.src
.resource
= srcSurf
->texture
;
256 blit
.src
.level
= srcSurf
->u
.tex
.level
;
257 blit
.src
.box
.z
= srcSurf
->u
.tex
.first_layer
;
258 blit
.src
.format
= util_format_linear(srcSurf
->format
);
260 st_adjust_blit_for_msaa_resolve(&blit
);
262 st
->pipe
->blit(st
->pipe
, &blit
);
269 if (mask
& depthStencil
) {
270 /* depth and/or stencil blit */
272 /* get src/dst depth surfaces */
273 struct st_renderbuffer
*srcDepthRb
=
274 st_renderbuffer(readFB
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
275 struct st_renderbuffer
*dstDepthRb
=
276 st_renderbuffer(drawFB
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
277 struct pipe_surface
*dstDepthSurf
=
278 dstDepthRb
? dstDepthRb
->surface
: NULL
;
280 struct st_renderbuffer
*srcStencilRb
=
281 st_renderbuffer(readFB
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
282 struct st_renderbuffer
*dstStencilRb
=
283 st_renderbuffer(drawFB
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
284 struct pipe_surface
*dstStencilSurf
=
285 dstStencilRb
? dstStencilRb
->surface
: NULL
;
287 if (_mesa_has_depthstencil_combined(readFB
) &&
288 _mesa_has_depthstencil_combined(drawFB
)) {
290 if (mask
& GL_DEPTH_BUFFER_BIT
)
291 blit
.mask
|= PIPE_MASK_Z
;
292 if (mask
& GL_STENCIL_BUFFER_BIT
)
293 blit
.mask
|= PIPE_MASK_S
;
295 blit
.dst
.resource
= dstDepthSurf
->texture
;
296 blit
.dst
.level
= dstDepthSurf
->u
.tex
.level
;
297 blit
.dst
.box
.z
= dstDepthSurf
->u
.tex
.first_layer
;
298 blit
.dst
.format
= dstDepthSurf
->format
;
300 blit
.src
.resource
= srcDepthRb
->texture
;
301 blit
.src
.level
= srcDepthRb
->surface
->u
.tex
.level
;
302 blit
.src
.box
.z
= srcDepthRb
->surface
->u
.tex
.first_layer
;
303 blit
.src
.format
= srcDepthRb
->surface
->format
;
305 st
->pipe
->blit(st
->pipe
, &blit
);
308 /* blitting depth and stencil separately */
310 if (mask
& GL_DEPTH_BUFFER_BIT
) {
311 blit
.mask
= PIPE_MASK_Z
;
313 blit
.dst
.resource
= dstDepthSurf
->texture
;
314 blit
.dst
.level
= dstDepthSurf
->u
.tex
.level
;
315 blit
.dst
.box
.z
= dstDepthSurf
->u
.tex
.first_layer
;
316 blit
.dst
.format
= dstDepthSurf
->format
;
318 blit
.src
.resource
= srcDepthRb
->texture
;
319 blit
.src
.level
= srcDepthRb
->surface
->u
.tex
.level
;
320 blit
.src
.box
.z
= srcDepthRb
->surface
->u
.tex
.first_layer
;
321 blit
.src
.format
= srcDepthRb
->surface
->format
;
323 st
->pipe
->blit(st
->pipe
, &blit
);
326 if (mask
& GL_STENCIL_BUFFER_BIT
) {
327 blit
.mask
= PIPE_MASK_S
;
329 blit
.dst
.resource
= dstStencilSurf
->texture
;
330 blit
.dst
.level
= dstStencilSurf
->u
.tex
.level
;
331 blit
.dst
.box
.z
= dstStencilSurf
->u
.tex
.first_layer
;
332 blit
.dst
.format
= dstStencilSurf
->format
;
334 blit
.src
.resource
= srcStencilRb
->texture
;
335 blit
.src
.level
= srcStencilRb
->surface
->u
.tex
.level
;
336 blit
.src
.box
.z
= srcStencilRb
->surface
->u
.tex
.first_layer
;
337 blit
.src
.format
= srcStencilRb
->surface
->format
;
339 st
->pipe
->blit(st
->pipe
, &blit
);
347 st_init_blit_functions(struct dd_function_table
*functions
)
349 functions
->BlitFramebuffer
= st_BlitFramebuffer
;