st/mesa: move declaration before code
[mesa.git] / src / mesa / state_tracker / st_cb_blit.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Brian Paul
31 */
32
33 #include "main/imports.h"
34 #include "main/image.h"
35 #include "main/macros.h"
36 #include "main/mfeatures.h"
37
38 #include "st_context.h"
39 #include "st_texture.h"
40 #include "st_cb_blit.h"
41 #include "st_cb_fbo.h"
42 #include "st_atom.h"
43
44 #include "util/u_blit.h"
45
46
47 void
48 st_init_blit(struct st_context *st)
49 {
50 st->blit = util_create_blit(st->pipe, st->cso_context);
51 }
52
53
54 void
55 st_destroy_blit(struct st_context *st)
56 {
57 util_destroy_blit(st->blit);
58 st->blit = NULL;
59 }
60
61
62 #if FEATURE_EXT_framebuffer_blit
63
64 static void
65 st_BlitFramebuffer_resolve(struct gl_context *ctx,
66 GLbitfield mask,
67 struct pipe_resolve_info *info)
68 {
69 const GLbitfield depthStencil = GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT;
70
71 struct st_context *st = st_context(ctx);
72
73 struct st_renderbuffer *srcRb, *dstRb;
74
75 if (mask & GL_COLOR_BUFFER_BIT) {
76 srcRb = st_renderbuffer(ctx->ReadBuffer->_ColorReadBuffer);
77 dstRb = st_renderbuffer(ctx->DrawBuffer->_ColorDrawBuffers[0]);
78
79 info->mask = PIPE_MASK_RGBA;
80
81 info->src.res = srcRb->texture;
82 info->src.layer = srcRb->surface->u.tex.first_layer;
83 info->dst.res = dstRb->texture;
84 info->dst.level = dstRb->surface->u.tex.level;
85 info->dst.layer = dstRb->surface->u.tex.first_layer;
86
87 st->pipe->resource_resolve(st->pipe, info);
88 }
89
90 if (mask & depthStencil) {
91 struct gl_renderbuffer_attachment *srcDepth, *srcStencil;
92 struct gl_renderbuffer_attachment *dstDepth, *dstStencil;
93 boolean combined;
94
95 srcDepth = &ctx->ReadBuffer->Attachment[BUFFER_DEPTH];
96 dstDepth = &ctx->DrawBuffer->Attachment[BUFFER_DEPTH];
97 srcStencil = &ctx->ReadBuffer->Attachment[BUFFER_STENCIL];
98 dstStencil = &ctx->DrawBuffer->Attachment[BUFFER_STENCIL];
99
100 combined =
101 st_is_depth_stencil_combined(srcDepth, srcStencil) &&
102 st_is_depth_stencil_combined(dstDepth, dstStencil);
103
104 if ((mask & GL_DEPTH_BUFFER_BIT) || combined) {
105 /* resolve depth and, if combined and requested, stencil as well */
106 srcRb = st_renderbuffer(srcDepth->Renderbuffer);
107 dstRb = st_renderbuffer(dstDepth->Renderbuffer);
108
109 info->mask = (mask & GL_DEPTH_BUFFER_BIT) ? PIPE_MASK_Z : 0;
110 if (combined && (mask & GL_STENCIL_BUFFER_BIT))
111 info->mask |= PIPE_MASK_S;
112
113 info->src.res = srcRb->texture;
114 info->src.layer = srcRb->surface->u.tex.first_layer;
115 info->dst.res = dstRb->texture;
116 info->dst.level = dstRb->surface->u.tex.level;
117 info->dst.layer = dstRb->surface->u.tex.first_layer;
118
119 st->pipe->resource_resolve(st->pipe, info);
120 }
121
122 if (mask & GL_STENCIL_BUFFER_BIT) {
123 /* resolve separate stencil buffer */
124 srcRb = st_renderbuffer(srcStencil->Renderbuffer);
125 dstRb = st_renderbuffer(dstStencil->Renderbuffer);
126
127 info->mask = PIPE_MASK_S;
128
129 info->src.res = srcRb->texture;
130 info->src.layer = srcRb->surface->u.tex.first_layer;
131 info->dst.res = dstRb->texture;
132 info->dst.level = dstRb->surface->u.tex.level;
133 info->dst.layer = dstRb->surface->u.tex.first_layer;
134
135 st->pipe->resource_resolve(st->pipe, info);
136 }
137 }
138 }
139
140 static void
141 st_BlitFramebuffer(struct gl_context *ctx,
142 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
143 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
144 GLbitfield mask, GLenum filter)
145 {
146 const GLbitfield depthStencil = (GL_DEPTH_BUFFER_BIT |
147 GL_STENCIL_BUFFER_BIT);
148 struct st_context *st = st_context(ctx);
149 const uint pFilter = ((filter == GL_NEAREST)
150 ? PIPE_TEX_MIPFILTER_NEAREST
151 : PIPE_TEX_MIPFILTER_LINEAR);
152 struct gl_framebuffer *readFB = ctx->ReadBuffer;
153 struct gl_framebuffer *drawFB = ctx->DrawBuffer;
154
155 st_validate_state(st);
156
157 if (!_mesa_clip_blit(ctx, &srcX0, &srcY0, &srcX1, &srcY1,
158 &dstX0, &dstY0, &dstX1, &dstY1)) {
159 return; /* nothing to draw/blit */
160 }
161
162 if (st_fb_orientation(drawFB) == Y_0_TOP) {
163 /* invert Y for dest */
164 dstY0 = drawFB->Height - dstY0;
165 dstY1 = drawFB->Height - dstY1;
166 }
167
168 if (st_fb_orientation(readFB) == Y_0_TOP) {
169 /* invert Y for src */
170 srcY0 = readFB->Height - srcY0;
171 srcY1 = readFB->Height - srcY1;
172 }
173
174 /* Disable conditional rendering. */
175 if (st->render_condition) {
176 st->pipe->render_condition(st->pipe, NULL, 0);
177 }
178
179 if (readFB->Visual.sampleBuffers > drawFB->Visual.sampleBuffers) {
180 struct pipe_resolve_info info;
181
182 if (dstX0 < dstX1) {
183 info.dst.x0 = dstX0;
184 info.dst.x1 = dstX1;
185 info.src.x0 = srcX0;
186 info.src.x1 = srcX1;
187 } else {
188 info.dst.x0 = dstX1;
189 info.dst.x1 = dstX0;
190 info.src.x0 = srcX1;
191 info.src.x1 = srcX0;
192 }
193 if (dstY0 < dstY1) {
194 info.dst.y0 = dstY0;
195 info.dst.y1 = dstY1;
196 info.src.y0 = srcY0;
197 info.src.y1 = srcY1;
198 } else {
199 info.dst.y0 = dstY1;
200 info.dst.y1 = dstY0;
201 info.src.y0 = srcY1;
202 info.src.y1 = srcY0;
203 }
204
205 st_BlitFramebuffer_resolve(ctx, mask, &info); /* filter doesn't apply */
206
207 goto done;
208 }
209
210 if (srcY0 > srcY1 && dstY0 > dstY1) {
211 /* Both src and dst are upside down. Swap Y to make it
212 * right-side up to increase odds of using a fast path.
213 * Recall that all Gallium raster coords have Y=0=top.
214 */
215 GLint tmp;
216 tmp = srcY0;
217 srcY0 = srcY1;
218 srcY1 = tmp;
219 tmp = dstY0;
220 dstY0 = dstY1;
221 dstY1 = tmp;
222 }
223
224 if (mask & GL_COLOR_BUFFER_BIT) {
225 struct gl_renderbuffer_attachment *srcAtt =
226 &readFB->Attachment[readFB->_ColorReadBufferIndex];
227
228 if(srcAtt->Type == GL_TEXTURE) {
229 struct st_texture_object *srcObj =
230 st_texture_object(srcAtt->Texture);
231 struct st_renderbuffer *dstRb =
232 st_renderbuffer(drawFB->_ColorDrawBuffers[0]);
233 struct pipe_surface *dstSurf = dstRb->surface;
234
235 if (!srcObj->pt)
236 goto done;
237
238 util_blit_pixels(st->blit, srcObj->pt, srcAtt->TextureLevel,
239 srcX0, srcY0, srcX1, srcY1,
240 srcAtt->Zoffset + srcAtt->CubeMapFace,
241 dstSurf, dstX0, dstY0, dstX1, dstY1,
242 0.0, pFilter);
243 }
244 else {
245 struct st_renderbuffer *srcRb =
246 st_renderbuffer(readFB->_ColorReadBuffer);
247 struct st_renderbuffer *dstRb =
248 st_renderbuffer(drawFB->_ColorDrawBuffers[0]);
249 struct pipe_surface *srcSurf = srcRb->surface;
250 struct pipe_surface *dstSurf = dstRb->surface;
251
252 util_blit_pixels(st->blit,
253 srcRb->texture, srcSurf->u.tex.level,
254 srcX0, srcY0, srcX1, srcY1,
255 srcSurf->u.tex.first_layer,
256 dstSurf, dstX0, dstY0, dstX1, dstY1,
257 0.0, pFilter);
258 }
259 }
260
261 if (mask & depthStencil) {
262 /* depth and/or stencil blit */
263
264 /* get src/dst depth surfaces */
265 struct gl_renderbuffer_attachment *srcDepth =
266 &readFB->Attachment[BUFFER_DEPTH];
267 struct gl_renderbuffer_attachment *dstDepth =
268 &drawFB->Attachment[BUFFER_DEPTH];
269 struct gl_renderbuffer_attachment *srcStencil =
270 &readFB->Attachment[BUFFER_STENCIL];
271 struct gl_renderbuffer_attachment *dstStencil =
272 &drawFB->Attachment[BUFFER_STENCIL];
273
274 struct st_renderbuffer *srcDepthRb =
275 st_renderbuffer(readFB->Attachment[BUFFER_DEPTH].Renderbuffer);
276 struct st_renderbuffer *dstDepthRb =
277 st_renderbuffer(drawFB->Attachment[BUFFER_DEPTH].Renderbuffer);
278 struct pipe_surface *dstDepthSurf =
279 dstDepthRb ? dstDepthRb->surface : NULL;
280
281 if ((mask & depthStencil) == depthStencil &&
282 st_is_depth_stencil_combined(srcDepth, srcStencil) &&
283 st_is_depth_stencil_combined(dstDepth, dstStencil)) {
284
285 /* Blitting depth and stencil values between combined
286 * depth/stencil buffers. This is the ideal case for such buffers.
287 */
288 util_blit_pixels(st->blit,
289 srcDepthRb->texture,
290 srcDepthRb->surface->u.tex.level,
291 srcX0, srcY0, srcX1, srcY1,
292 srcDepthRb->surface->u.tex.first_layer,
293 dstDepthSurf, dstX0, dstY0, dstX1, dstY1,
294 0.0, pFilter);
295 }
296 else {
297 /* blitting depth and stencil separately */
298
299 if (mask & GL_DEPTH_BUFFER_BIT) {
300 util_blit_pixels(st->blit, srcDepthRb->texture,
301 srcDepthRb->surface->u.tex.level,
302 srcX0, srcY0, srcX1, srcY1,
303 srcDepthRb->surface->u.tex.first_layer,
304 dstDepthSurf, dstX0, dstY0, dstX1, dstY1,
305 0.0, pFilter);
306 }
307
308 if (mask & GL_STENCIL_BUFFER_BIT) {
309 /* blit stencil only */
310 _mesa_problem(ctx, "st_BlitFramebuffer(STENCIL) not completed");
311 }
312 }
313 }
314
315 done:
316 /* Restore conditional rendering state. */
317 if (st->render_condition) {
318 st->pipe->render_condition(st->pipe, st->render_condition,
319 st->condition_mode);
320 }
321 }
322
323
324 void
325 st_init_blit_functions(struct dd_function_table *functions)
326 {
327 functions->BlitFramebuffer = st_BlitFramebuffer;
328 }
329
330 #endif /* FEATURE_EXT_framebuffer_blit */