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"
48 st_BlitFramebuffer(struct gl_context
*ctx
,
49 struct gl_framebuffer
*readFB
,
50 struct gl_framebuffer
*drawFB
,
51 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
52 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
53 GLbitfield mask
, GLenum filter
)
55 const GLbitfield depthStencil
= (GL_DEPTH_BUFFER_BIT
|
56 GL_STENCIL_BUFFER_BIT
);
57 struct st_context
*st
= st_context(ctx
);
58 const uint pFilter
= ((filter
== GL_NEAREST
)
59 ? PIPE_TEX_FILTER_NEAREST
60 : PIPE_TEX_FILTER_LINEAR
);
62 GLint srcX0
, srcY0
, srcX1
, srcY1
;
63 GLint dstX0
, dstY0
, dstX1
, dstY1
;
65 struct pipe_blit_info blit
;
67 st_manager_validate_framebuffers(st
);
69 /* Make sure bitmap rendering has landed in the framebuffers */
70 st_flush_bitmap_cache(st
);
71 st_invalidate_readpix_cache(st
);
82 /* NOTE: If the src and dst dimensions don't match, we cannot simply adjust
83 * the integer coordinates to account for clipping (or scissors) because that
84 * would make us cut off fractional parts, affecting the result of the blit.
86 * XXX: This should depend on mask !
88 if (!_mesa_clip_blit(ctx
, readFB
, drawFB
,
89 &clip
.srcX0
, &clip
.srcY0
, &clip
.srcX1
, &clip
.srcY1
,
90 &clip
.dstX0
, &clip
.dstY0
, &clip
.dstX1
, &clip
.dstY1
)) {
91 return; /* nothing to draw/blit */
93 memset(&blit
, 0, sizeof(struct pipe_blit_info
));
95 (dstX0
!= clip
.dstX0
) ||
96 (dstY0
!= clip
.dstY0
) ||
97 (dstX1
!= clip
.dstX1
) ||
98 (dstY1
!= clip
.dstY1
);
100 if (st_fb_orientation(drawFB
) == Y_0_TOP
) {
101 /* invert Y for dest */
102 dstY0
= drawFB
->Height
- dstY0
;
103 dstY1
= drawFB
->Height
- dstY1
;
104 /* invert Y for clip */
105 clip
.dstY0
= drawFB
->Height
- clip
.dstY0
;
106 clip
.dstY1
= drawFB
->Height
- clip
.dstY1
;
108 if (blit
.scissor_enable
) {
109 blit
.scissor
.minx
= MIN2(clip
.dstX0
, clip
.dstX1
);
110 blit
.scissor
.miny
= MIN2(clip
.dstY0
, clip
.dstY1
);
111 blit
.scissor
.maxx
= MAX2(clip
.dstX0
, clip
.dstX1
);
112 blit
.scissor
.maxy
= MAX2(clip
.dstY0
, clip
.dstY1
);
114 debug_printf("scissor = (%i,%i)-(%i,%i)\n",
115 blit
.scissor
.minx
,blit
.scissor
.miny
,
116 blit
.scissor
.maxx
,blit
.scissor
.maxy
);
120 if (st_fb_orientation(readFB
) == Y_0_TOP
) {
121 /* invert Y for src */
122 srcY0
= readFB
->Height
- srcY0
;
123 srcY1
= readFB
->Height
- srcY1
;
126 if (srcY0
> srcY1
&& dstY0
> dstY1
) {
127 /* Both src and dst are upside down. Swap Y to make it
128 * right-side up to increase odds of using a fast path.
129 * Recall that all Gallium raster coords have Y=0=top.
140 blit
.src
.box
.depth
= 1;
141 blit
.dst
.box
.depth
= 1;
143 /* Destination dimensions have to be positive: */
145 blit
.dst
.box
.x
= dstX0
;
146 blit
.src
.box
.x
= srcX0
;
147 blit
.dst
.box
.width
= dstX1
- dstX0
;
148 blit
.src
.box
.width
= srcX1
- srcX0
;
150 blit
.dst
.box
.x
= dstX1
;
151 blit
.src
.box
.x
= srcX1
;
152 blit
.dst
.box
.width
= dstX0
- dstX1
;
153 blit
.src
.box
.width
= srcX0
- srcX1
;
156 blit
.dst
.box
.y
= dstY0
;
157 blit
.src
.box
.y
= srcY0
;
158 blit
.dst
.box
.height
= dstY1
- dstY0
;
159 blit
.src
.box
.height
= srcY1
- srcY0
;
161 blit
.dst
.box
.y
= dstY1
;
162 blit
.src
.box
.y
= srcY1
;
163 blit
.dst
.box
.height
= dstY0
- dstY1
;
164 blit
.src
.box
.height
= srcY0
- srcY1
;
167 if (drawFB
!= ctx
->WinSysDrawBuffer
)
168 st_window_rectangles_to_blit(ctx
, &blit
);
170 blit
.filter
= pFilter
;
171 blit
.render_condition_enable
= TRUE
;
172 blit
.alpha_blend
= FALSE
;
174 if (mask
& GL_COLOR_BUFFER_BIT
) {
175 struct gl_renderbuffer_attachment
*srcAtt
=
176 &readFB
->Attachment
[readFB
->_ColorReadBufferIndex
];
179 blit
.mask
= PIPE_MASK_RGBA
;
181 if (srcAtt
->Type
== GL_TEXTURE
) {
182 struct st_texture_object
*srcObj
= st_texture_object(srcAtt
->Texture
);
184 if (!srcObj
|| !srcObj
->pt
) {
188 blit
.src
.resource
= srcObj
->pt
;
189 blit
.src
.level
= srcAtt
->TextureLevel
;
190 blit
.src
.box
.z
= srcAtt
->Zoffset
+ srcAtt
->CubeMapFace
;
191 blit
.src
.format
= srcObj
->pt
->format
;
193 if (!ctx
->Color
.sRGBEnabled
)
194 blit
.src
.format
= util_format_linear(blit
.src
.format
);
197 struct st_renderbuffer
*srcRb
=
198 st_renderbuffer(readFB
->_ColorReadBuffer
);
199 struct pipe_surface
*srcSurf
;
204 st_update_renderbuffer_surface(st
, srcRb
);
209 srcSurf
= srcRb
->surface
;
211 blit
.src
.resource
= srcSurf
->texture
;
212 blit
.src
.level
= srcSurf
->u
.tex
.level
;
213 blit
.src
.box
.z
= srcSurf
->u
.tex
.first_layer
;
214 blit
.src
.format
= srcSurf
->format
;
217 for (i
= 0; i
< drawFB
->_NumColorDrawBuffers
; i
++) {
218 struct st_renderbuffer
*dstRb
=
219 st_renderbuffer(drawFB
->_ColorDrawBuffers
[i
]);
222 struct pipe_surface
*dstSurf
;
224 st_update_renderbuffer_surface(st
, dstRb
);
226 dstSurf
= dstRb
->surface
;
229 blit
.dst
.resource
= dstSurf
->texture
;
230 blit
.dst
.level
= dstSurf
->u
.tex
.level
;
231 blit
.dst
.box
.z
= dstSurf
->u
.tex
.first_layer
;
232 blit
.dst
.format
= dstSurf
->format
;
234 st
->pipe
->blit(st
->pipe
, &blit
);
235 dstRb
->defined
= true; /* front buffer tracking */
241 if (mask
& depthStencil
) {
242 /* depth and/or stencil blit */
244 /* get src/dst depth surfaces */
245 struct st_renderbuffer
*srcDepthRb
=
246 st_renderbuffer(readFB
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
247 struct st_renderbuffer
*dstDepthRb
=
248 st_renderbuffer(drawFB
->Attachment
[BUFFER_DEPTH
].Renderbuffer
);
249 struct pipe_surface
*dstDepthSurf
=
250 dstDepthRb
? dstDepthRb
->surface
: NULL
;
252 struct st_renderbuffer
*srcStencilRb
=
253 st_renderbuffer(readFB
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
254 struct st_renderbuffer
*dstStencilRb
=
255 st_renderbuffer(drawFB
->Attachment
[BUFFER_STENCIL
].Renderbuffer
);
256 struct pipe_surface
*dstStencilSurf
=
257 dstStencilRb
? dstStencilRb
->surface
: NULL
;
259 if (_mesa_has_depthstencil_combined(readFB
) &&
260 _mesa_has_depthstencil_combined(drawFB
)) {
262 if (mask
& GL_DEPTH_BUFFER_BIT
)
263 blit
.mask
|= PIPE_MASK_Z
;
264 if (mask
& GL_STENCIL_BUFFER_BIT
)
265 blit
.mask
|= PIPE_MASK_S
;
267 blit
.dst
.resource
= dstDepthSurf
->texture
;
268 blit
.dst
.level
= dstDepthSurf
->u
.tex
.level
;
269 blit
.dst
.box
.z
= dstDepthSurf
->u
.tex
.first_layer
;
270 blit
.dst
.format
= dstDepthSurf
->format
;
272 blit
.src
.resource
= srcDepthRb
->texture
;
273 blit
.src
.level
= srcDepthRb
->surface
->u
.tex
.level
;
274 blit
.src
.box
.z
= srcDepthRb
->surface
->u
.tex
.first_layer
;
275 blit
.src
.format
= srcDepthRb
->surface
->format
;
277 st
->pipe
->blit(st
->pipe
, &blit
);
280 /* blitting depth and stencil separately */
282 if (mask
& GL_DEPTH_BUFFER_BIT
) {
283 blit
.mask
= PIPE_MASK_Z
;
285 blit
.dst
.resource
= dstDepthSurf
->texture
;
286 blit
.dst
.level
= dstDepthSurf
->u
.tex
.level
;
287 blit
.dst
.box
.z
= dstDepthSurf
->u
.tex
.first_layer
;
288 blit
.dst
.format
= dstDepthSurf
->format
;
290 blit
.src
.resource
= srcDepthRb
->texture
;
291 blit
.src
.level
= srcDepthRb
->surface
->u
.tex
.level
;
292 blit
.src
.box
.z
= srcDepthRb
->surface
->u
.tex
.first_layer
;
293 blit
.src
.format
= srcDepthRb
->surface
->format
;
295 st
->pipe
->blit(st
->pipe
, &blit
);
298 if (mask
& GL_STENCIL_BUFFER_BIT
) {
299 blit
.mask
= PIPE_MASK_S
;
301 blit
.dst
.resource
= dstStencilSurf
->texture
;
302 blit
.dst
.level
= dstStencilSurf
->u
.tex
.level
;
303 blit
.dst
.box
.z
= dstStencilSurf
->u
.tex
.first_layer
;
304 blit
.dst
.format
= dstStencilSurf
->format
;
306 blit
.src
.resource
= srcStencilRb
->texture
;
307 blit
.src
.level
= srcStencilRb
->surface
->u
.tex
.level
;
308 blit
.src
.box
.z
= srcStencilRb
->surface
->u
.tex
.first_layer
;
309 blit
.src
.format
= srcStencilRb
->surface
->format
;
311 st
->pipe
->blit(st
->pipe
, &blit
);
319 st_init_blit_functions(struct dd_function_table
*functions
)
321 functions
->BlitFramebuffer
= st_BlitFramebuffer
;