1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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"
36 #include "main/mfeatures.h"
38 #include "st_context.h"
39 #include "st_texture.h"
40 #include "st_cb_blit.h"
41 #include "st_cb_fbo.h"
44 #include "util/u_format.h"
48 st_BlitFramebuffer(struct gl_context
*ctx
,
49 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
50 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
51 GLbitfield mask
, GLenum filter
)
53 const GLbitfield depthStencil
= (GL_DEPTH_BUFFER_BIT
|
54 GL_STENCIL_BUFFER_BIT
);
55 struct st_context
*st
= st_context(ctx
);
56 const uint pFilter
= ((filter
== GL_NEAREST
)
57 ? PIPE_TEX_FILTER_NEAREST
58 : PIPE_TEX_FILTER_LINEAR
);
59 struct gl_framebuffer
*readFB
= ctx
->ReadBuffer
;
60 struct gl_framebuffer
*drawFB
= ctx
->DrawBuffer
;
62 GLint srcX0
, srcY0
, srcX1
, srcY1
;
63 GLint dstX0
, dstY0
, dstX1
, dstY1
;
65 struct pipe_blit_info blit
;
67 st_validate_state(st
);
78 /* NOTE: If the src and dst dimensions don't match, we cannot simply adjust
79 * the integer coordinates to account for clipping (or scissors) because that
80 * would make us cut off fractional parts, affecting the result of the blit.
82 * XXX: This should depend on mask !
84 if (!_mesa_clip_blit(ctx
,
85 &clip
.srcX0
, &clip
.srcY0
, &clip
.srcX1
, &clip
.srcY1
,
86 &clip
.dstX0
, &clip
.dstY0
, &clip
.dstX1
, &clip
.dstY1
)) {
87 return; /* nothing to draw/blit */
90 (dstX0
!= clip
.dstX0
) ||
91 (dstY0
!= clip
.dstY0
) ||
92 (dstX1
!= clip
.dstX1
) ||
93 (dstY1
!= clip
.dstY1
);
95 if (st_fb_orientation(drawFB
) == Y_0_TOP
) {
96 /* invert Y for dest */
97 dstY0
= drawFB
->Height
- dstY0
;
98 dstY1
= drawFB
->Height
- dstY1
;
99 /* invert Y for clip */
100 clip
.dstY0
= drawFB
->Height
- clip
.dstY0
;
101 clip
.dstY1
= drawFB
->Height
- clip
.dstY1
;
103 if (blit
.scissor_enable
) {
104 blit
.scissor
.minx
= MIN2(clip
.dstX0
, clip
.dstX1
);
105 blit
.scissor
.miny
= MIN2(clip
.dstY0
, clip
.dstY1
);
106 blit
.scissor
.maxx
= MAX2(clip
.dstX0
, clip
.dstX1
);
107 blit
.scissor
.maxy
= MAX2(clip
.dstY0
, clip
.dstY1
);
109 debug_printf("scissor = (%i,%i)-(%i,%i)\n",
110 blit
.scissor
.minx
,blit
.scissor
.miny
,
111 blit
.scissor
.maxx
,blit
.scissor
.maxy
);
115 if (st_fb_orientation(readFB
) == Y_0_TOP
) {
116 /* invert Y for src */
117 srcY0
= readFB
->Height
- srcY0
;
118 srcY1
= readFB
->Height
- srcY1
;
121 if (srcY0
> srcY1
&& dstY0
> dstY1
) {
122 /* Both src and dst are upside down. Swap Y to make it
123 * right-side up to increase odds of using a fast path.
124 * Recall that all Gallium raster coords have Y=0=top.
135 blit
.src
.box
.depth
= 1;
136 blit
.dst
.box
.depth
= 1;
138 /* Destination dimensions have to be positive: */
140 blit
.dst
.box
.x
= dstX0
;
141 blit
.src
.box
.x
= srcX0
;
142 blit
.dst
.box
.width
= dstX1
- dstX0
;
143 blit
.src
.box
.width
= srcX1
- srcX0
;
145 blit
.dst
.box
.x
= dstX1
;
146 blit
.src
.box
.x
= srcX1
;
147 blit
.dst
.box
.width
= dstX0
- dstX1
;
148 blit
.src
.box
.width
= srcX0
- srcX1
;
151 blit
.dst
.box
.y
= dstY0
;
152 blit
.src
.box
.y
= srcY0
;
153 blit
.dst
.box
.height
= dstY1
- dstY0
;
154 blit
.src
.box
.height
= srcY1
- srcY0
;
156 blit
.dst
.box
.y
= dstY1
;
157 blit
.src
.box
.y
= srcY1
;
158 blit
.dst
.box
.height
= dstY0
- dstY1
;
159 blit
.src
.box
.height
= srcY0
- srcY1
;
162 blit
.filter
= pFilter
;
164 if (mask
& GL_COLOR_BUFFER_BIT
) {
165 struct gl_renderbuffer_attachment
*srcAtt
=
166 &readFB
->Attachment
[readFB
->_ColorReadBufferIndex
];
168 blit
.mask
= PIPE_MASK_RGBA
;
170 if (srcAtt
->Type
== GL_TEXTURE
) {
171 struct st_texture_object
*srcObj
= st_texture_object(srcAtt
->Texture
);
174 if (!srcObj
|| !srcObj
->pt
) {
178 for (i
= 0; i
< drawFB
->_NumColorDrawBuffers
; i
++) {
179 struct st_renderbuffer
*dstRb
=
180 st_renderbuffer(drawFB
->_ColorDrawBuffers
[i
]);
183 struct pipe_surface
*dstSurf
= dstRb
->surface
;
186 blit
.dst
.resource
= dstSurf
->texture
;
187 blit
.dst
.level
= dstSurf
->u
.tex
.level
;
188 blit
.dst
.box
.z
= dstSurf
->u
.tex
.first_layer
;
189 blit
.dst
.format
= util_format_linear(dstSurf
->format
);
191 blit
.src
.resource
= srcObj
->pt
;
192 blit
.src
.level
= srcAtt
->TextureLevel
;
193 blit
.src
.box
.z
= srcAtt
->Zoffset
+ srcAtt
->CubeMapFace
;
194 blit
.src
.format
= util_format_linear(srcObj
->pt
->format
);
196 st
->pipe
->blit(st
->pipe
, &blit
);
202 struct st_renderbuffer
*srcRb
=
203 st_renderbuffer(readFB
->_ColorReadBuffer
);
204 struct pipe_surface
*srcSurf
;
207 if (!srcRb
|| !srcRb
->surface
) {
211 srcSurf
= srcRb
->surface
;
213 for (i
= 0; i
< drawFB
->_NumColorDrawBuffers
; i
++) {
214 struct st_renderbuffer
*dstRb
=
215 st_renderbuffer(drawFB
->_ColorDrawBuffers
[i
]);
218 struct pipe_surface
*dstSurf
= dstRb
->surface
;
221 blit
.dst
.resource
= dstSurf
->texture
;
222 blit
.dst
.level
= dstSurf
->u
.tex
.level
;
223 blit
.dst
.box
.z
= dstSurf
->u
.tex
.first_layer
;
224 blit
.dst
.format
= util_format_linear(dstSurf
->format
);
226 blit
.src
.resource
= srcSurf
->texture
;
227 blit
.src
.level
= srcSurf
->u
.tex
.level
;
228 blit
.src
.box
.z
= srcSurf
->u
.tex
.first_layer
;
229 blit
.src
.format
= util_format_linear(srcSurf
->format
);
231 st
->pipe
->blit(st
->pipe
, &blit
);
238 if (mask
& depthStencil
) {
239 /* depth and/or stencil blit */
241 /* get src/dst depth surfaces */
242 struct gl_renderbuffer_attachment
*srcDepth
=
243 &readFB
->Attachment
[BUFFER_DEPTH
];
244 struct gl_renderbuffer_attachment
*dstDepth
=
245 &drawFB
->Attachment
[BUFFER_DEPTH
];
246 struct gl_renderbuffer_attachment
*srcStencil
=
247 &readFB
->Attachment
[BUFFER_STENCIL
];
248 struct gl_renderbuffer_attachment
*dstStencil
=
249 &drawFB
->Attachment
[BUFFER_STENCIL
];
251 struct st_renderbuffer
*srcDepthRb
=
252 st_renderbuffer(srcDepth
->Renderbuffer
);
253 struct st_renderbuffer
*dstDepthRb
=
254 st_renderbuffer(dstDepth
->Renderbuffer
);
255 struct pipe_surface
*dstDepthSurf
=
256 dstDepthRb
? dstDepthRb
->surface
: NULL
;
258 struct st_renderbuffer
*srcStencilRb
=
259 st_renderbuffer(srcStencil
->Renderbuffer
);
260 struct st_renderbuffer
*dstStencilRb
=
261 st_renderbuffer(dstStencil
->Renderbuffer
);
262 struct pipe_surface
*dstStencilSurf
=
263 dstStencilRb
? dstStencilRb
->surface
: NULL
;
265 if (st_is_depth_stencil_combined(srcDepth
, srcStencil
) &&
266 st_is_depth_stencil_combined(dstDepth
, dstStencil
)) {
268 if (mask
& GL_DEPTH_BUFFER_BIT
)
269 blit
.mask
|= PIPE_MASK_Z
;
270 if (mask
& GL_STENCIL_BUFFER_BIT
)
271 blit
.mask
|= PIPE_MASK_S
;
273 blit
.dst
.resource
= dstDepthSurf
->texture
;
274 blit
.dst
.level
= dstDepthSurf
->u
.tex
.level
;
275 blit
.dst
.box
.z
= dstDepthSurf
->u
.tex
.first_layer
;
276 blit
.dst
.format
= dstDepthSurf
->format
;
278 blit
.src
.resource
= srcDepthRb
->texture
;
279 blit
.src
.level
= srcDepthRb
->surface
->u
.tex
.level
;
280 blit
.src
.box
.z
= srcDepthRb
->surface
->u
.tex
.first_layer
;
281 blit
.src
.format
= srcDepthRb
->surface
->format
;
283 st
->pipe
->blit(st
->pipe
, &blit
);
286 /* blitting depth and stencil separately */
288 if (mask
& GL_DEPTH_BUFFER_BIT
) {
289 blit
.mask
= PIPE_MASK_Z
;
291 blit
.dst
.resource
= dstDepthSurf
->texture
;
292 blit
.dst
.level
= dstDepthSurf
->u
.tex
.level
;
293 blit
.dst
.box
.z
= dstDepthSurf
->u
.tex
.first_layer
;
294 blit
.dst
.format
= dstDepthSurf
->format
;
296 blit
.src
.resource
= srcDepthRb
->texture
;
297 blit
.src
.level
= srcDepthRb
->surface
->u
.tex
.level
;
298 blit
.src
.box
.z
= srcDepthRb
->surface
->u
.tex
.first_layer
;
299 blit
.src
.format
= srcDepthRb
->surface
->format
;
301 st
->pipe
->blit(st
->pipe
, &blit
);
304 if (mask
& GL_STENCIL_BUFFER_BIT
) {
305 blit
.mask
= PIPE_MASK_S
;
307 blit
.dst
.resource
= dstStencilSurf
->texture
;
308 blit
.dst
.level
= dstStencilSurf
->u
.tex
.level
;
309 blit
.dst
.box
.z
= dstStencilSurf
->u
.tex
.first_layer
;
310 blit
.dst
.format
= dstStencilSurf
->format
;
312 blit
.src
.resource
= srcStencilRb
->texture
;
313 blit
.src
.level
= srcStencilRb
->surface
->u
.tex
.level
;
314 blit
.src
.box
.z
= srcStencilRb
->surface
->u
.tex
.first_layer
;
315 blit
.src
.format
= srcStencilRb
->surface
->format
;
317 st
->pipe
->blit(st
->pipe
, &blit
);
325 st_init_blit_functions(struct dd_function_table
*functions
)
327 functions
->BlitFramebuffer
= st_BlitFramebuffer
;