Merge branch 'gallium-nopointsizeminmax'
[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 static void
64 st_BlitFramebuffer(GLcontext *ctx,
65 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
66 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
67 GLbitfield mask, GLenum filter)
68 {
69 const GLbitfield depthStencil = (GL_DEPTH_BUFFER_BIT |
70 GL_STENCIL_BUFFER_BIT);
71 struct st_context *st = ctx->st;
72 const uint pFilter = ((filter == GL_NEAREST)
73 ? PIPE_TEX_MIPFILTER_NEAREST
74 : PIPE_TEX_MIPFILTER_LINEAR);
75 struct gl_framebuffer *readFB = ctx->ReadBuffer;
76 struct gl_framebuffer *drawFB = ctx->DrawBuffer;
77
78 if (!_mesa_clip_blit(ctx, &srcX0, &srcY0, &srcX1, &srcY1,
79 &dstX0, &dstY0, &dstX1, &dstY1)) {
80 return; /* nothing to draw/blit */
81 }
82
83 if (st_fb_orientation(drawFB) == Y_0_TOP) {
84 /* invert Y for dest */
85 dstY0 = drawFB->Height - dstY0;
86 dstY1 = drawFB->Height - dstY1;
87 }
88
89 if (st_fb_orientation(readFB) == Y_0_TOP) {
90 /* invert Y for src */
91 srcY0 = readFB->Height - srcY0;
92 srcY1 = readFB->Height - srcY1;
93 }
94
95 if (srcY0 > srcY1 && dstY0 > dstY1) {
96 /* Both src and dst are upside down. Swap Y to make it
97 * right-side up to increase odds of using a fast path.
98 * Recall that all Gallium raster coords have Y=0=top.
99 */
100 GLint tmp;
101 tmp = srcY0;
102 srcY0 = srcY1;
103 srcY1 = tmp;
104 tmp = dstY0;
105 dstY0 = dstY1;
106 dstY1 = tmp;
107 }
108
109 if (mask & GL_COLOR_BUFFER_BIT) {
110 struct gl_renderbuffer_attachment *srcAtt =
111 &readFB->Attachment[readFB->_ColorReadBufferIndex];
112
113 if(srcAtt->Type == GL_TEXTURE) {
114 struct pipe_screen *screen = ctx->st->pipe->screen;
115 const struct st_texture_object *srcObj =
116 st_texture_object(srcAtt->Texture);
117 struct st_renderbuffer *dstRb =
118 st_renderbuffer(drawFB->_ColorDrawBuffers[0]);
119 struct pipe_surface *srcSurf;
120 struct pipe_surface *dstSurf = dstRb->surface;
121
122 if (!srcObj->pt)
123 return;
124
125 srcSurf = screen->get_tex_surface(screen,
126 srcObj->pt,
127 srcAtt->CubeMapFace,
128 srcAtt->TextureLevel,
129 srcAtt->Zoffset,
130 PIPE_BUFFER_USAGE_GPU_READ);
131 if(!srcSurf)
132 return;
133
134 util_blit_pixels(st->blit,
135 srcSurf, srcX0, srcY0, srcX1, srcY1,
136 dstSurf, dstX0, dstY0, dstX1, dstY1,
137 0.0, pFilter);
138
139 pipe_surface_reference(&srcSurf, NULL);
140 }
141 else {
142 struct st_renderbuffer *srcRb =
143 st_renderbuffer(readFB->_ColorReadBuffer);
144 struct st_renderbuffer *dstRb =
145 st_renderbuffer(drawFB->_ColorDrawBuffers[0]);
146 struct pipe_surface *srcSurf = srcRb->surface;
147 struct pipe_surface *dstSurf = dstRb->surface;
148
149 util_blit_pixels(st->blit,
150 srcSurf, srcX0, srcY0, srcX1, srcY1,
151 dstSurf, dstX0, dstY0, dstX1, dstY1,
152 0.0, pFilter);
153 }
154 }
155
156 if (mask & depthStencil) {
157 /* depth and/or stencil blit */
158
159 /* get src/dst depth surfaces */
160 struct st_renderbuffer *srcDepthRb =
161 st_renderbuffer(readFB->Attachment[BUFFER_DEPTH].Renderbuffer);
162 struct st_renderbuffer *dstDepthRb =
163 st_renderbuffer(drawFB->Attachment[BUFFER_DEPTH].Renderbuffer);
164 struct pipe_surface *srcDepthSurf =
165 srcDepthRb ? srcDepthRb->surface : NULL;
166 struct pipe_surface *dstDepthSurf =
167 dstDepthRb ? dstDepthRb->surface : NULL;
168
169 /* get src/dst stencil surfaces */
170 struct st_renderbuffer *srcStencilRb =
171 st_renderbuffer(readFB->Attachment[BUFFER_STENCIL].Renderbuffer);
172 struct st_renderbuffer *dstStencilRb =
173 st_renderbuffer(drawFB->Attachment[BUFFER_STENCIL].Renderbuffer);
174 struct pipe_surface *srcStencilSurf =
175 srcStencilRb ? srcStencilRb->surface : NULL;
176 struct pipe_surface *dstStencilSurf =
177 dstStencilRb ? dstStencilRb->surface : NULL;
178
179 if ((mask & depthStencil) == depthStencil &&
180 srcDepthSurf == srcStencilSurf &&
181 dstDepthSurf == dstStencilSurf) {
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 srcDepthSurf, srcX0, srcY0, srcX1, srcY1,
187 dstDepthSurf, dstX0, dstY0, dstX1, dstY1,
188 0.0, pFilter);
189 }
190 else {
191 /* blitting depth and stencil separately */
192
193 if (mask & GL_DEPTH_BUFFER_BIT) {
194 /* blit Z only */
195 _mesa_problem(ctx, "st_BlitFramebuffer(DEPTH) not completed");
196 }
197
198 if (mask & GL_STENCIL_BUFFER_BIT) {
199 /* blit stencil only */
200 _mesa_problem(ctx, "st_BlitFramebuffer(STENCIL) not completed");
201 }
202 }
203 }
204 }
205 #endif /* FEATURE_EXT_framebuffer_blit */
206
207
208
209 void
210 st_init_blit_functions(struct dd_function_table *functions)
211 {
212 #if FEATURE_EXT_framebuffer_blit
213 functions->BlitFramebuffer = st_BlitFramebuffer;
214 #endif
215 }