Merge remote branch 'vdpau/pipe-video' into pipe-video
[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
37 #include "st_context.h"
38 #include "st_texture.h"
39 #include "st_cb_blit.h"
40 #include "st_cb_fbo.h"
41
42 #include "util/u_blit.h"
43
44
45 void
46 st_init_blit(struct st_context *st)
47 {
48 st->blit = util_create_blit(st->pipe, st->cso_context);
49 }
50
51
52 void
53 st_destroy_blit(struct st_context *st)
54 {
55 util_destroy_blit(st->blit);
56 st->blit = NULL;
57 }
58
59
60 #if FEATURE_EXT_framebuffer_blit
61
62 static void
63 st_BlitFramebuffer(struct gl_context *ctx,
64 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
65 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
66 GLbitfield mask, GLenum filter)
67 {
68 const GLbitfield depthStencil = (GL_DEPTH_BUFFER_BIT |
69 GL_STENCIL_BUFFER_BIT);
70 struct st_context *st = st_context(ctx);
71 const uint pFilter = ((filter == GL_NEAREST)
72 ? PIPE_TEX_MIPFILTER_NEAREST
73 : PIPE_TEX_MIPFILTER_LINEAR);
74 struct gl_framebuffer *readFB = ctx->ReadBuffer;
75 struct gl_framebuffer *drawFB = ctx->DrawBuffer;
76
77 if (!_mesa_clip_blit(ctx, &srcX0, &srcY0, &srcX1, &srcY1,
78 &dstX0, &dstY0, &dstX1, &dstY1)) {
79 return; /* nothing to draw/blit */
80 }
81
82 if (st_fb_orientation(drawFB) == Y_0_TOP) {
83 /* invert Y for dest */
84 dstY0 = drawFB->Height - dstY0;
85 dstY1 = drawFB->Height - dstY1;
86 }
87
88 if (st_fb_orientation(readFB) == Y_0_TOP) {
89 /* invert Y for src */
90 srcY0 = readFB->Height - srcY0;
91 srcY1 = readFB->Height - srcY1;
92 }
93
94 if (srcY0 > srcY1 && dstY0 > dstY1) {
95 /* Both src and dst are upside down. Swap Y to make it
96 * right-side up to increase odds of using a fast path.
97 * Recall that all Gallium raster coords have Y=0=top.
98 */
99 GLint tmp;
100 tmp = srcY0;
101 srcY0 = srcY1;
102 srcY1 = tmp;
103 tmp = dstY0;
104 dstY0 = dstY1;
105 dstY1 = tmp;
106 }
107
108 if (mask & GL_COLOR_BUFFER_BIT) {
109 struct gl_renderbuffer_attachment *srcAtt =
110 &readFB->Attachment[readFB->_ColorReadBufferIndex];
111
112 if(srcAtt->Type == GL_TEXTURE) {
113 struct st_texture_object *srcObj =
114 st_texture_object(srcAtt->Texture);
115 struct st_renderbuffer *dstRb =
116 st_renderbuffer(drawFB->_ColorDrawBuffers[0]);
117 struct pipe_surface *dstSurf = dstRb->surface;
118
119 if (!srcObj->pt)
120 return;
121
122 util_blit_pixels(st->blit, srcObj->pt, srcAtt->TextureLevel,
123 srcX0, srcY0, srcX1, srcY1,
124 srcAtt->Zoffset + srcAtt->CubeMapFace,
125 dstSurf, dstX0, dstY0, dstX1, dstY1,
126 0.0, pFilter);
127 }
128 else {
129 struct st_renderbuffer *srcRb =
130 st_renderbuffer(readFB->_ColorReadBuffer);
131 struct st_renderbuffer *dstRb =
132 st_renderbuffer(drawFB->_ColorDrawBuffers[0]);
133 struct pipe_surface *srcSurf = srcRb->surface;
134 struct pipe_surface *dstSurf = dstRb->surface;
135
136 util_blit_pixels(st->blit,
137 srcRb->texture, srcSurf->u.tex.level,
138 srcX0, srcY0, srcX1, srcY1,
139 srcSurf->u.tex.first_layer,
140 dstSurf, dstX0, dstY0, dstX1, dstY1,
141 0.0, pFilter);
142 }
143 }
144
145 if (mask & depthStencil) {
146 /* depth and/or stencil blit */
147
148 /* get src/dst depth surfaces */
149 struct gl_renderbuffer_attachment *srcDepth =
150 &readFB->Attachment[BUFFER_DEPTH];
151 struct gl_renderbuffer_attachment *dstDepth =
152 &drawFB->Attachment[BUFFER_DEPTH];
153 struct gl_renderbuffer_attachment *srcStencil =
154 &readFB->Attachment[BUFFER_STENCIL];
155 struct gl_renderbuffer_attachment *dstStencil =
156 &drawFB->Attachment[BUFFER_STENCIL];
157
158 struct st_renderbuffer *srcDepthRb =
159 st_renderbuffer(readFB->Attachment[BUFFER_DEPTH].Renderbuffer);
160 struct st_renderbuffer *dstDepthRb =
161 st_renderbuffer(drawFB->Attachment[BUFFER_DEPTH].Renderbuffer);
162 struct pipe_surface *dstDepthSurf =
163 dstDepthRb ? dstDepthRb->surface : NULL;
164
165 if ((mask & depthStencil) == depthStencil &&
166 st_is_depth_stencil_combined(srcDepth, srcStencil) &&
167 st_is_depth_stencil_combined(dstDepth, dstStencil)) {
168
169 /* Blitting depth and stencil values between combined
170 * depth/stencil buffers. This is the ideal case for such buffers.
171 */
172 util_blit_pixels(st->blit,
173 srcDepthRb->texture,
174 srcDepthRb->surface->u.tex.level,
175 srcX0, srcY0, srcX1, srcY1,
176 srcDepthRb->surface->u.tex.first_layer,
177 dstDepthSurf, dstX0, dstY0, dstX1, dstY1,
178 0.0, pFilter);
179 }
180 else {
181 /* blitting depth and stencil separately */
182
183 if (mask & GL_DEPTH_BUFFER_BIT) {
184 util_blit_pixels(st->blit, srcDepthRb->texture,
185 srcDepthRb->surface->u.tex.level,
186 srcX0, srcY0, srcX1, srcY1,
187 srcDepthRb->surface->u.tex.first_layer,
188 dstDepthSurf, dstX0, dstY0, dstX1, dstY1,
189 0.0, pFilter);
190 }
191
192 if (mask & GL_STENCIL_BUFFER_BIT) {
193 /* blit stencil only */
194 _mesa_problem(ctx, "st_BlitFramebuffer(STENCIL) not completed");
195 }
196 }
197 }
198 }
199
200
201 void
202 st_init_blit_functions(struct dd_function_table *functions)
203 {
204 functions->BlitFramebuffer = st_BlitFramebuffer;
205 }
206
207 #endif /* FEATURE_EXT_framebuffer_blit */