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"
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_manager_validate_framebuffers(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
;
195 blit
.alpha_blend
= FALSE
;
197 if (mask
& GL_COLOR_BUFFER_BIT
) {
198 struct gl_renderbuffer_attachment
*srcAtt
=
199 &readFB
->Attachment
[readFB
->_ColorReadBufferIndex
];
201 blit
.mask
= PIPE_MASK_RGBA
;
203 if (srcAtt
->Type
== GL_TEXTURE
) {
204 struct st_texture_object
*srcObj
= st_texture_object(srcAtt
->Texture
);
207 if (!srcObj
|| !srcObj
->pt
) {
211 for (i
= 0; i
< drawFB
->_NumColorDrawBuffers
; i
++) {
212 struct st_renderbuffer
*dstRb
=
213 st_renderbuffer(drawFB
->_ColorDrawBuffers
[i
]);
216 struct pipe_surface
*dstSurf
= dstRb
->surface
;
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
);
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
);
229 st_adjust_blit_for_msaa_resolve(&blit
);
231 st
->pipe
->blit(st
->pipe
, &blit
);
237 struct st_renderbuffer
*srcRb
=
238 st_renderbuffer(readFB
->_ColorReadBuffer
);
239 struct pipe_surface
*srcSurf
;
242 if (!srcRb
|| !srcRb
->surface
) {
246 srcSurf
= srcRb
->surface
;
248 for (i
= 0; i
< drawFB
->_NumColorDrawBuffers
; i
++) {
249 struct st_renderbuffer
*dstRb
=
250 st_renderbuffer(drawFB
->_ColorDrawBuffers
[i
]);
253 struct pipe_surface
*dstSurf
= dstRb
->surface
;
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
);
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
);
266 st_adjust_blit_for_msaa_resolve(&blit
);
268 st
->pipe
->blit(st
->pipe
, &blit
);
275 if (mask
& depthStencil
) {
276 /* depth and/or stencil blit */
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
;
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
;
293 if (_mesa_has_depthstencil_combined(readFB
) &&
294 _mesa_has_depthstencil_combined(drawFB
)) {
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
;
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
;
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
;
311 st
->pipe
->blit(st
->pipe
, &blit
);
314 /* blitting depth and stencil separately */
316 if (mask
& GL_DEPTH_BUFFER_BIT
) {
317 blit
.mask
= PIPE_MASK_Z
;
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
;
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
;
329 st
->pipe
->blit(st
->pipe
, &blit
);
332 if (mask
& GL_STENCIL_BUFFER_BIT
) {
333 blit
.mask
= PIPE_MASK_S
;
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
;
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
;
345 st
->pipe
->blit(st
->pipe
, &blit
);
353 st_init_blit_functions(struct dd_function_table
*functions
)
355 functions
->BlitFramebuffer
= st_BlitFramebuffer
;