st/mesa: Remove the now unused gl_vertex_array.
[mesa.git] / src / mesa / state_tracker / st_cb_blit.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 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.
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
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"
44
45 #include "util/u_format.h"
46
47 static void
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)
54 {
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);
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_manager_validate_framebuffers(st);
68
69 /* Make sure bitmap rendering has landed in the framebuffers */
70 st_flush_bitmap_cache(st);
71 st_invalidate_readpix_cache(st);
72
73 clip.srcX0 = srcX0;
74 clip.srcY0 = srcY0;
75 clip.srcX1 = srcX1;
76 clip.srcY1 = srcY1;
77 clip.dstX0 = dstX0;
78 clip.dstY0 = dstY0;
79 clip.dstX1 = dstX1;
80 clip.dstY1 = dstY1;
81
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.
85 *
86 * XXX: This should depend on mask !
87 */
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 */
92 }
93 memset(&blit, 0, sizeof(struct pipe_blit_info));
94 blit.scissor_enable =
95 (dstX0 != clip.dstX0) ||
96 (dstY0 != clip.dstY0) ||
97 (dstX1 != clip.dstX1) ||
98 (dstY1 != clip.dstY1);
99
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;
107 }
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);
113 #if 0
114 debug_printf("scissor = (%i,%i)-(%i,%i)\n",
115 blit.scissor.minx,blit.scissor.miny,
116 blit.scissor.maxx,blit.scissor.maxy);
117 #endif
118 }
119
120 if (st_fb_orientation(readFB) == Y_0_TOP) {
121 /* invert Y for src */
122 srcY0 = readFB->Height - srcY0;
123 srcY1 = readFB->Height - srcY1;
124 }
125
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.
130 */
131 GLint tmp;
132 tmp = srcY0;
133 srcY0 = srcY1;
134 srcY1 = tmp;
135 tmp = dstY0;
136 dstY0 = dstY1;
137 dstY1 = tmp;
138 }
139
140 blit.src.box.depth = 1;
141 blit.dst.box.depth = 1;
142
143 /* Destination dimensions have to be positive: */
144 if (dstX0 < dstX1) {
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;
149 } else {
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;
154 }
155 if (dstY0 < dstY1) {
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;
160 } else {
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;
165 }
166
167 if (drawFB != ctx->WinSysDrawBuffer)
168 st_window_rectangles_to_blit(ctx, &blit);
169
170 blit.filter = pFilter;
171 blit.render_condition_enable = TRUE;
172 blit.alpha_blend = FALSE;
173
174 if (mask & GL_COLOR_BUFFER_BIT) {
175 struct gl_renderbuffer_attachment *srcAtt =
176 &readFB->Attachment[readFB->_ColorReadBufferIndex];
177 GLuint i;
178
179 blit.mask = PIPE_MASK_RGBA;
180
181 if (srcAtt->Type == GL_TEXTURE) {
182 struct st_texture_object *srcObj = st_texture_object(srcAtt->Texture);
183
184 if (!srcObj || !srcObj->pt) {
185 return;
186 }
187
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;
192
193 if (!ctx->Color.sRGBEnabled)
194 blit.src.format = util_format_linear(blit.src.format);
195 }
196 else {
197 struct st_renderbuffer *srcRb =
198 st_renderbuffer(readFB->_ColorReadBuffer);
199 struct pipe_surface *srcSurf;
200
201 if (!srcRb)
202 return;
203
204 st_update_renderbuffer_surface(st, srcRb);
205
206 if (!srcRb->surface)
207 return;
208
209 srcSurf = srcRb->surface;
210
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;
215 }
216
217 for (i = 0; i < drawFB->_NumColorDrawBuffers; i++) {
218 struct st_renderbuffer *dstRb =
219 st_renderbuffer(drawFB->_ColorDrawBuffers[i]);
220
221 if (dstRb) {
222 struct pipe_surface *dstSurf;
223
224 st_update_renderbuffer_surface(st, dstRb);
225
226 dstSurf = dstRb->surface;
227
228 if (dstSurf) {
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;
233
234 st->pipe->blit(st->pipe, &blit);
235 dstRb->defined = true; /* front buffer tracking */
236 }
237 }
238 }
239 }
240
241 if (mask & depthStencil) {
242 /* depth and/or stencil blit */
243
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;
251
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;
258
259 if (_mesa_has_depthstencil_combined(readFB) &&
260 _mesa_has_depthstencil_combined(drawFB)) {
261 blit.mask = 0;
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;
266
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;
271
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;
276
277 st->pipe->blit(st->pipe, &blit);
278 }
279 else {
280 /* blitting depth and stencil separately */
281
282 if (mask & GL_DEPTH_BUFFER_BIT) {
283 blit.mask = PIPE_MASK_Z;
284
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;
289
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;
294
295 st->pipe->blit(st->pipe, &blit);
296 }
297
298 if (mask & GL_STENCIL_BUFFER_BIT) {
299 blit.mask = PIPE_MASK_S;
300
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;
305
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;
310
311 st->pipe->blit(st->pipe, &blit);
312 }
313 }
314 }
315 }
316
317
318 void
319 st_init_blit_functions(struct dd_function_table *functions)
320 {
321 functions->BlitFramebuffer = st_BlitFramebuffer;
322 }