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_blit.h"
45 #include "util/u_format.h"
49 st_init_blit(struct st_context
*st
)
51 st
->blit
= util_create_blit(st
->pipe
, st
->cso_context
);
56 st_destroy_blit(struct st_context
*st
)
58 util_destroy_blit(st
->blit
);
64 st_BlitFramebuffer(struct gl_context
*ctx
,
65 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
66 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
67 GLbitfield mask
, GLenum filter
)
69 const GLbitfield depthStencil
= (GL_DEPTH_BUFFER_BIT
|
70 GL_STENCIL_BUFFER_BIT
);
71 struct st_context
*st
= st_context(ctx
);
72 const uint pFilter
= ((filter
== GL_NEAREST
)
73 ? PIPE_TEX_FILTER_NEAREST
74 : PIPE_TEX_FILTER_LINEAR
);
75 struct gl_framebuffer
*readFB
= ctx
->ReadBuffer
;
76 struct gl_framebuffer
*drawFB
= ctx
->DrawBuffer
;
78 GLint srcX0
, srcY0
, srcX1
, srcY1
;
79 GLint dstX0
, dstY0
, dstX1
, dstY1
;
81 struct pipe_blit_info blit
;
83 st_validate_state(st
);
94 /* NOTE: If the src and dst dimensions don't match, we cannot simply adjust
95 * the integer coordinates to account for clipping (or scissors) because that
96 * would make us cut off fractional parts, affecting the result of the blit.
98 * XXX: This should depend on mask !
100 if (!_mesa_clip_blit(ctx
,
101 &clip
.srcX0
, &clip
.srcY0
, &clip
.srcX1
, &clip
.srcY1
,
102 &clip
.dstX0
, &clip
.dstY0
, &clip
.dstX1
, &clip
.dstY1
)) {
103 return; /* nothing to draw/blit */
105 blit
.scissor_enable
=
106 (dstX0
!= clip
.dstX0
) ||
107 (dstY0
!= clip
.dstY0
) ||
108 (dstX1
!= clip
.dstX1
) ||
109 (dstY1
!= clip
.dstY1
);
111 if (st_fb_orientation(drawFB
) == Y_0_TOP
) {
112 /* invert Y for dest */
113 dstY0
= drawFB
->Height
- dstY0
;
114 dstY1
= drawFB
->Height
- dstY1
;
115 /* invert Y for clip */
116 clip
.dstY0
= drawFB
->Height
- clip
.dstY0
;
117 clip
.dstY1
= drawFB
->Height
- clip
.dstY1
;
119 if (blit
.scissor_enable
) {
120 blit
.scissor
.minx
= MIN2(clip
.dstX0
, clip
.dstX1
);
121 blit
.scissor
.miny
= MIN2(clip
.dstY0
, clip
.dstY1
);
122 blit
.scissor
.maxx
= MAX2(clip
.dstX0
, clip
.dstX1
);
123 blit
.scissor
.maxy
= MAX2(clip
.dstY0
, clip
.dstY1
);
125 debug_printf("scissor = (%i,%i)-(%i,%i)\n",
126 blit
.scissor
.minx
,blit
.scissor
.miny
,
127 blit
.scissor
.maxx
,blit
.scissor
.maxy
);
131 if (st_fb_orientation(readFB
) == Y_0_TOP
) {
132 /* invert Y for src */
133 srcY0
= readFB
->Height
- srcY0
;
134 srcY1
= readFB
->Height
- srcY1
;
137 if (srcY0
> srcY1
&& dstY0
> dstY1
) {
138 /* Both src and dst are upside down. Swap Y to make it
139 * right-side up to increase odds of using a fast path.
140 * Recall that all Gallium raster coords have Y=0=top.
151 blit
.src
.box
.depth
= 1;
152 blit
.dst
.box
.depth
= 1;
154 /* Destination dimensions have to be positive: */
156 blit
.dst
.box
.x
= dstX0
;
157 blit
.src
.box
.x
= srcX0
;
158 blit
.dst
.box
.width
= dstX1
- dstX0
;
159 blit
.src
.box
.width
= srcX1
- srcX0
;
161 blit
.dst
.box
.x
= dstX1
;
162 blit
.src
.box
.x
= srcX1
;
163 blit
.dst
.box
.width
= dstX0
- dstX1
;
164 blit
.src
.box
.width
= srcX0
- srcX1
;
167 blit
.dst
.box
.y
= dstY0
;
168 blit
.src
.box
.y
= srcY0
;
169 blit
.dst
.box
.height
= dstY1
- dstY0
;
170 blit
.src
.box
.height
= srcY1
- srcY0
;
172 blit
.dst
.box
.y
= dstY1
;
173 blit
.src
.box
.y
= srcY1
;
174 blit
.dst
.box
.height
= dstY0
- dstY1
;
175 blit
.src
.box
.height
= srcY0
- srcY1
;
178 blit
.filter
= pFilter
;
180 if (mask
& GL_COLOR_BUFFER_BIT
) {
181 struct gl_renderbuffer_attachment
*srcAtt
=
182 &readFB
->Attachment
[readFB
->_ColorReadBufferIndex
];
184 blit
.mask
= PIPE_MASK_RGBA
;
186 if (srcAtt
->Type
== GL_TEXTURE
) {
187 struct st_texture_object
*srcObj
= st_texture_object(srcAtt
->Texture
);
190 if (!srcObj
|| !srcObj
->pt
) {
194 for (i
= 0; i
< drawFB
->_NumColorDrawBuffers
; i
++) {
195 struct st_renderbuffer
*dstRb
=
196 st_renderbuffer(drawFB
->_ColorDrawBuffers
[i
]);
199 struct pipe_surface
*dstSurf
= dstRb
->surface
;
202 blit
.dst
.resource
= dstSurf
->texture
;
203 blit
.dst
.level
= dstSurf
->u
.tex
.level
;
204 blit
.dst
.box
.z
= dstSurf
->u
.tex
.first_layer
;
205 blit
.dst
.format
= util_format_linear(dstSurf
->format
);
207 blit
.src
.resource
= srcObj
->pt
;
208 blit
.src
.level
= srcAtt
->TextureLevel
;
209 blit
.src
.box
.z
= srcAtt
->Zoffset
+ srcAtt
->CubeMapFace
;
210 blit
.src
.format
= util_format_linear(srcObj
->pt
->format
);
212 st
->pipe
->blit(st
->pipe
, &blit
);
218 struct st_renderbuffer
*srcRb
=
219 st_renderbuffer(readFB
->_ColorReadBuffer
);
220 struct pipe_surface
*srcSurf
;
223 if (!srcRb
|| !srcRb
->surface
) {
227 srcSurf
= srcRb
->surface
;
229 for (i
= 0; i
< drawFB
->_NumColorDrawBuffers
; i
++) {
230 struct st_renderbuffer
*dstRb
=
231 st_renderbuffer(drawFB
->_ColorDrawBuffers
[i
]);
234 struct pipe_surface
*dstSurf
= dstRb
->surface
;
237 blit
.dst
.resource
= dstSurf
->texture
;
238 blit
.dst
.level
= dstSurf
->u
.tex
.level
;
239 blit
.dst
.box
.z
= dstSurf
->u
.tex
.first_layer
;
240 blit
.dst
.format
= util_format_linear(dstSurf
->format
);
242 blit
.src
.resource
= srcSurf
->texture
;
243 blit
.src
.level
= srcSurf
->u
.tex
.level
;
244 blit
.src
.box
.z
= srcSurf
->u
.tex
.first_layer
;
245 blit
.src
.format
= util_format_linear(srcSurf
->format
);
247 st
->pipe
->blit(st
->pipe
, &blit
);
254 if (mask
& depthStencil
) {
255 /* depth and/or stencil blit */
257 /* get src/dst depth surfaces */
258 struct gl_renderbuffer_attachment
*srcDepth
=
259 &readFB
->Attachment
[BUFFER_DEPTH
];
260 struct gl_renderbuffer_attachment
*dstDepth
=
261 &drawFB
->Attachment
[BUFFER_DEPTH
];
262 struct gl_renderbuffer_attachment
*srcStencil
=
263 &readFB
->Attachment
[BUFFER_STENCIL
];
264 struct gl_renderbuffer_attachment
*dstStencil
=
265 &drawFB
->Attachment
[BUFFER_STENCIL
];
267 struct st_renderbuffer
*srcDepthRb
=
268 st_renderbuffer(srcDepth
->Renderbuffer
);
269 struct st_renderbuffer
*dstDepthRb
=
270 st_renderbuffer(dstDepth
->Renderbuffer
);
271 struct pipe_surface
*dstDepthSurf
=
272 dstDepthRb
? dstDepthRb
->surface
: NULL
;
274 struct st_renderbuffer
*srcStencilRb
=
275 st_renderbuffer(srcStencil
->Renderbuffer
);
276 struct st_renderbuffer
*dstStencilRb
=
277 st_renderbuffer(dstStencil
->Renderbuffer
);
278 struct pipe_surface
*dstStencilSurf
=
279 dstStencilRb
? dstStencilRb
->surface
: NULL
;
281 if (st_is_depth_stencil_combined(srcDepth
, srcStencil
) &&
282 st_is_depth_stencil_combined(dstDepth
, dstStencil
)) {
284 if (mask
& GL_DEPTH_BUFFER_BIT
)
285 blit
.mask
|= PIPE_MASK_Z
;
286 if (mask
& GL_STENCIL_BUFFER_BIT
)
287 blit
.mask
|= PIPE_MASK_S
;
289 blit
.dst
.resource
= dstDepthSurf
->texture
;
290 blit
.dst
.level
= dstDepthSurf
->u
.tex
.level
;
291 blit
.dst
.box
.z
= dstDepthSurf
->u
.tex
.first_layer
;
292 blit
.dst
.format
= dstDepthSurf
->format
;
294 blit
.src
.resource
= srcDepthRb
->texture
;
295 blit
.src
.level
= srcDepthRb
->surface
->u
.tex
.level
;
296 blit
.src
.box
.z
= srcDepthRb
->surface
->u
.tex
.first_layer
;
297 blit
.src
.format
= srcDepthRb
->surface
->format
;
299 st
->pipe
->blit(st
->pipe
, &blit
);
302 /* blitting depth and stencil separately */
304 if (mask
& GL_DEPTH_BUFFER_BIT
) {
305 blit
.mask
= PIPE_MASK_Z
;
307 blit
.dst
.resource
= dstDepthSurf
->texture
;
308 blit
.dst
.level
= dstDepthSurf
->u
.tex
.level
;
309 blit
.dst
.box
.z
= dstDepthSurf
->u
.tex
.first_layer
;
310 blit
.dst
.format
= dstDepthSurf
->format
;
312 blit
.src
.resource
= srcDepthRb
->texture
;
313 blit
.src
.level
= srcDepthRb
->surface
->u
.tex
.level
;
314 blit
.src
.box
.z
= srcDepthRb
->surface
->u
.tex
.first_layer
;
315 blit
.src
.format
= srcDepthRb
->surface
->format
;
317 st
->pipe
->blit(st
->pipe
, &blit
);
320 if (mask
& GL_STENCIL_BUFFER_BIT
) {
321 blit
.mask
= PIPE_MASK_S
;
323 blit
.dst
.resource
= dstStencilSurf
->texture
;
324 blit
.dst
.level
= dstStencilSurf
->u
.tex
.level
;
325 blit
.dst
.box
.z
= dstStencilSurf
->u
.tex
.first_layer
;
326 blit
.dst
.format
= dstStencilSurf
->format
;
328 blit
.src
.resource
= srcStencilRb
->texture
;
329 blit
.src
.level
= srcStencilRb
->surface
->u
.tex
.level
;
330 blit
.src
.box
.z
= srcStencilRb
->surface
->u
.tex
.first_layer
;
331 blit
.src
.format
= srcStencilRb
->surface
->format
;
333 st
->pipe
->blit(st
->pipe
, &blit
);
341 st_init_blit_functions(struct dd_function_table
*functions
)
343 functions
->BlitFramebuffer
= st_BlitFramebuffer
;