Merge branch 'glsl2-head' into glsl2
[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(GLcontext *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_subresource srcSub;
118 struct pipe_surface *dstSurf = dstRb->surface;
119
120 if (!srcObj->pt)
121 return;
122
123 srcSub.face = srcAtt->CubeMapFace;
124 srcSub.level = srcAtt->TextureLevel;
125
126 util_blit_pixels(st->blit, srcObj->pt, srcSub,
127 srcX0, srcY0, srcX1, srcY1, srcAtt->Zoffset,
128 dstSurf, dstX0, dstY0, dstX1, dstY1,
129 0.0, pFilter);
130 }
131 else {
132 struct st_renderbuffer *srcRb =
133 st_renderbuffer(readFB->_ColorReadBuffer);
134 struct st_renderbuffer *dstRb =
135 st_renderbuffer(drawFB->_ColorDrawBuffers[0]);
136 struct pipe_surface *srcSurf = srcRb->surface;
137 struct pipe_surface *dstSurf = dstRb->surface;
138 struct pipe_subresource srcSub;
139
140 srcSub.face = srcSurf->face;
141 srcSub.level = srcSurf->level;
142
143 util_blit_pixels(st->blit,
144 srcRb->texture, srcSub, srcX0, srcY0, srcX1, srcY1,
145 srcSurf->zslice,
146 dstSurf, dstX0, dstY0, dstX1, dstY1,
147 0.0, pFilter);
148 }
149 }
150
151 if (mask & depthStencil) {
152 /* depth and/or stencil blit */
153
154 /* get src/dst depth surfaces */
155 struct st_renderbuffer *srcDepthRb =
156 st_renderbuffer(readFB->Attachment[BUFFER_DEPTH].Renderbuffer);
157 struct st_renderbuffer *dstDepthRb =
158 st_renderbuffer(drawFB->Attachment[BUFFER_DEPTH].Renderbuffer);
159 struct pipe_surface *srcDepthSurf =
160 srcDepthRb ? srcDepthRb->surface : NULL;
161 struct pipe_surface *dstDepthSurf =
162 dstDepthRb ? dstDepthRb->surface : NULL;
163
164 /* get src/dst stencil surfaces */
165 struct st_renderbuffer *srcStencilRb =
166 st_renderbuffer(readFB->Attachment[BUFFER_STENCIL].Renderbuffer);
167 struct st_renderbuffer *dstStencilRb =
168 st_renderbuffer(drawFB->Attachment[BUFFER_STENCIL].Renderbuffer);
169 struct pipe_surface *srcStencilSurf =
170 srcStencilRb ? srcStencilRb->surface : NULL;
171 struct pipe_surface *dstStencilSurf =
172 dstStencilRb ? dstStencilRb->surface : NULL;
173
174 if ((mask & depthStencil) == depthStencil &&
175 srcDepthSurf == srcStencilSurf &&
176 dstDepthSurf == dstStencilSurf) {
177 struct pipe_subresource srcSub;
178
179 srcSub.face = srcDepthRb->surface->face;
180 srcSub.level = srcDepthRb->surface->level;
181
182 /* Blitting depth and stencil values between combined
183 * depth/stencil buffers. This is the ideal case for such buffers.
184 */
185 util_blit_pixels(st->blit,
186 srcDepthRb->texture, srcSub, srcX0, srcY0, srcX1, srcY1,
187 srcDepthRb->surface->zslice,
188 dstDepthSurf, dstX0, dstY0, dstX1, dstY1,
189 0.0, pFilter);
190 }
191 else {
192 /* blitting depth and stencil separately */
193
194 if (mask & GL_DEPTH_BUFFER_BIT) {
195 /* blit Z only */
196 _mesa_problem(ctx, "st_BlitFramebuffer(DEPTH) not completed");
197 }
198
199 if (mask & GL_STENCIL_BUFFER_BIT) {
200 /* blit stencil only */
201 _mesa_problem(ctx, "st_BlitFramebuffer(STENCIL) not completed");
202 }
203 }
204 }
205 }
206
207
208 void
209 st_init_blit_functions(struct dd_function_table *functions)
210 {
211 functions->BlitFramebuffer = st_BlitFramebuffer;
212 }
213
214 #endif /* FEATURE_EXT_framebuffer_blit */