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