st/mesa: remove what is left from u_blit
[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_format.h"
45
46
47 static void
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)
52 {
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;
61 struct {
62 GLint srcX0, srcY0, srcX1, srcY1;
63 GLint dstX0, dstY0, dstX1, dstY1;
64 } clip;
65 struct pipe_blit_info blit;
66
67 st_validate_state(st);
68
69 clip.srcX0 = srcX0;
70 clip.srcY0 = srcY0;
71 clip.srcX1 = srcX1;
72 clip.srcY1 = srcY1;
73 clip.dstX0 = dstX0;
74 clip.dstY0 = dstY0;
75 clip.dstX1 = dstX1;
76 clip.dstY1 = dstY1;
77
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.
81 *
82 * XXX: This should depend on mask !
83 */
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 */
88 }
89 blit.scissor_enable =
90 (dstX0 != clip.dstX0) ||
91 (dstY0 != clip.dstY0) ||
92 (dstX1 != clip.dstX1) ||
93 (dstY1 != clip.dstY1);
94
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;
102 }
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);
108 #if 0
109 debug_printf("scissor = (%i,%i)-(%i,%i)\n",
110 blit.scissor.minx,blit.scissor.miny,
111 blit.scissor.maxx,blit.scissor.maxy);
112 #endif
113 }
114
115 if (st_fb_orientation(readFB) == Y_0_TOP) {
116 /* invert Y for src */
117 srcY0 = readFB->Height - srcY0;
118 srcY1 = readFB->Height - srcY1;
119 }
120
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.
125 */
126 GLint tmp;
127 tmp = srcY0;
128 srcY0 = srcY1;
129 srcY1 = tmp;
130 tmp = dstY0;
131 dstY0 = dstY1;
132 dstY1 = tmp;
133 }
134
135 blit.src.box.depth = 1;
136 blit.dst.box.depth = 1;
137
138 /* Destination dimensions have to be positive: */
139 if (dstX0 < dstX1) {
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;
144 } else {
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;
149 }
150 if (dstY0 < dstY1) {
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;
155 } else {
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;
160 }
161
162 blit.filter = pFilter;
163
164 if (mask & GL_COLOR_BUFFER_BIT) {
165 struct gl_renderbuffer_attachment *srcAtt =
166 &readFB->Attachment[readFB->_ColorReadBufferIndex];
167
168 blit.mask = PIPE_MASK_RGBA;
169
170 if (srcAtt->Type == GL_TEXTURE) {
171 struct st_texture_object *srcObj = st_texture_object(srcAtt->Texture);
172 GLuint i;
173
174 if (!srcObj || !srcObj->pt) {
175 return;
176 }
177
178 for (i = 0; i < drawFB->_NumColorDrawBuffers; i++) {
179 struct st_renderbuffer *dstRb =
180 st_renderbuffer(drawFB->_ColorDrawBuffers[i]);
181
182 if (dstRb) {
183 struct pipe_surface *dstSurf = dstRb->surface;
184
185 if (dstSurf) {
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);
190
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);
195
196 st->pipe->blit(st->pipe, &blit);
197 }
198 }
199 }
200 }
201 else {
202 struct st_renderbuffer *srcRb =
203 st_renderbuffer(readFB->_ColorReadBuffer);
204 struct pipe_surface *srcSurf;
205 GLuint i;
206
207 if (!srcRb || !srcRb->surface) {
208 return;
209 }
210
211 srcSurf = srcRb->surface;
212
213 for (i = 0; i < drawFB->_NumColorDrawBuffers; i++) {
214 struct st_renderbuffer *dstRb =
215 st_renderbuffer(drawFB->_ColorDrawBuffers[i]);
216
217 if (dstRb) {
218 struct pipe_surface *dstSurf = dstRb->surface;
219
220 if (dstSurf) {
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);
225
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);
230
231 st->pipe->blit(st->pipe, &blit);
232 }
233 }
234 }
235 }
236 }
237
238 if (mask & depthStencil) {
239 /* depth and/or stencil blit */
240
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];
250
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;
257
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;
264
265 if (st_is_depth_stencil_combined(srcDepth, srcStencil) &&
266 st_is_depth_stencil_combined(dstDepth, dstStencil)) {
267 blit.mask = 0;
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;
272
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;
277
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;
282
283 st->pipe->blit(st->pipe, &blit);
284 }
285 else {
286 /* blitting depth and stencil separately */
287
288 if (mask & GL_DEPTH_BUFFER_BIT) {
289 blit.mask = PIPE_MASK_Z;
290
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;
295
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;
300
301 st->pipe->blit(st->pipe, &blit);
302 }
303
304 if (mask & GL_STENCIL_BUFFER_BIT) {
305 blit.mask = PIPE_MASK_S;
306
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;
311
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;
316
317 st->pipe->blit(st->pipe, &blit);
318 }
319 }
320 }
321 }
322
323
324 void
325 st_init_blit_functions(struct dd_function_table *functions)
326 {
327 functions->BlitFramebuffer = st_BlitFramebuffer;
328 }