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