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