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"
44 #include "util/u_format.h"
48 st_adjust_blit_for_msaa_resolve(struct pipe_blit_info
*blit
)
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.
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.
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.
64 if (blit
->src
.resource
->nr_samples
> 1 &&
65 blit
->dst
.resource
->nr_samples
<= 1) {
66 blit
->dst
.format
= blit
->dst
.resource
->format
;
68 if (util_format_is_srgb(blit
->dst
.resource
->format
))
69 blit
->src
.format
= util_format_srgb(blit
->src
.resource
->format
);
71 blit
->src
.format
= util_format_linear(blit
->src
.resource
->format
);
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
)
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
);
90 GLint srcX0
, srcY0
, srcX1
, srcY1
;
91 GLint dstX0
, dstY0
, dstX1
, dstY1
;
93 struct pipe_blit_info blit
;
95 st_validate_state(st
);
97 /* Make sure bitmap rendering has landed in the framebuffers */
98 st_flush_bitmap_cache(st
);
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.
113 * XXX: This should depend on mask !
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 */
120 blit
.scissor_enable
=
121 (dstX0
!= clip
.dstX0
) ||
122 (dstY0
!= clip
.dstY0
) ||
123 (dstX1
!= clip
.dstX1
) ||
124 (dstY1
!= clip
.dstY1
);
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
;
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
);
140 debug_printf("scissor = (%i,%i)-(%i,%i)\n",
141 blit
.scissor
.minx
,blit
.scissor
.miny
,
142 blit
.scissor
.maxx
,blit
.scissor
.maxy
);
146 if (st_fb_orientation(readFB
) == Y_0_TOP
) {
147 /* invert Y for src */
148 srcY0
= readFB
->Height
- srcY0
;
149 srcY1
= readFB
->Height
- srcY1
;
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.
166 blit
.src
.box
.depth
= 1;
167 blit
.dst
.box
.depth
= 1;
169 /* Destination dimensions have to be positive: */
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
;
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
;
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
;
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
;
193 blit
.filter
= pFilter
;
194 blit
.render_condition_enable
= TRUE
;
196 if (mask
& GL_COLOR_BUFFER_BIT
) {
197 struct gl_renderbuffer_attachment
*srcAtt
=
198 &readFB
->Attachment
[readFB
->_ColorReadBufferIndex
];
200 blit
.mask
= PIPE_MASK_RGBA
;
202 if (srcAtt
->Type
== GL_TEXTURE
) {
203 struct st_texture_object
*srcObj
= st_texture_object(srcAtt
->Texture
);
206 if (!srcObj
|| !srcObj
->pt
) {
210 for (i
= 0; i
< drawFB
->_NumColorDrawBuffers
; i
++) {
211 struct st_renderbuffer
*dstRb
=
212 st_renderbuffer(drawFB
->_ColorDrawBuffers
[i
]);
215 struct pipe_surface
*dstSurf
= dstRb
->surface
;
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
);
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
);
228 st_adjust_blit_for_msaa_resolve(&blit
);
230 st
->pipe
->blit(st
->pipe
, &blit
);
236 struct st_renderbuffer
*srcRb
=
237 st_renderbuffer(readFB
->_ColorReadBuffer
);
238 struct pipe_surface
*srcSurf
;
241 if (!srcRb
|| !srcRb
->surface
) {
245 srcSurf
= srcRb
->surface
;
247 for (i
= 0; i
< drawFB
->_NumColorDrawBuffers
; i
++) {
248 struct st_renderbuffer
*dstRb
=
249 st_renderbuffer(drawFB
->_ColorDrawBuffers
[i
]);
252 struct pipe_surface
*dstSurf
= dstRb
->surface
;
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
);
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
);
265 st_adjust_blit_for_msaa_resolve(&blit
);
267 st
->pipe
->blit(st
->pipe
, &blit
);
274 if (mask
& depthStencil
) {
275 /* depth and/or stencil blit */
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
;
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
;
292 if (_mesa_has_depthstencil_combined(readFB
) &&
293 _mesa_has_depthstencil_combined(drawFB
)) {
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
;
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
;
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
;
310 st
->pipe
->blit(st
->pipe
, &blit
);
313 /* blitting depth and stencil separately */
315 if (mask
& GL_DEPTH_BUFFER_BIT
) {
316 blit
.mask
= PIPE_MASK_Z
;
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
;
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
;
328 st
->pipe
->blit(st
->pipe
, &blit
);
331 if (mask
& GL_STENCIL_BUFFER_BIT
) {
332 blit
.mask
= PIPE_MASK_S
;
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
;
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
;
344 st
->pipe
->blit(st
->pipe
, &blit
);
352 st_init_blit_functions(struct dd_function_table
*functions
)
354 functions
->BlitFramebuffer
= st_BlitFramebuffer
;