mesa: fixup inconsistent naming of RG16 formats
[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/mfeatures.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 #include "st_atom.h"
43
44 #include "util/u_blit.h"
45 #include "util/u_format.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 static void
64 st_BlitFramebuffer(struct gl_context *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 = st_context(ctx);
72 const uint pFilter = ((filter == GL_NEAREST)
73 ? PIPE_TEX_FILTER_NEAREST
74 : PIPE_TEX_FILTER_LINEAR);
75 struct gl_framebuffer *readFB = ctx->ReadBuffer;
76 struct gl_framebuffer *drawFB = ctx->DrawBuffer;
77 struct {
78 GLint srcX0, srcY0, srcX1, srcY1;
79 GLint dstX0, dstY0, dstX1, dstY1;
80 } clip;
81 struct pipe_blit_info blit;
82
83 st_validate_state(st);
84
85 clip.srcX0 = srcX0;
86 clip.srcY0 = srcY0;
87 clip.srcX1 = srcX1;
88 clip.srcY1 = srcY1;
89 clip.dstX0 = dstX0;
90 clip.dstY0 = dstY0;
91 clip.dstX1 = dstX1;
92 clip.dstY1 = dstY1;
93
94 /* NOTE: If the src and dst dimensions don't match, we cannot simply adjust
95 * the integer coordinates to account for clipping (or scissors) because that
96 * would make us cut off fractional parts, affecting the result of the blit.
97 *
98 * XXX: This should depend on mask !
99 */
100 if (!_mesa_clip_blit(ctx,
101 &clip.srcX0, &clip.srcY0, &clip.srcX1, &clip.srcY1,
102 &clip.dstX0, &clip.dstY0, &clip.dstX1, &clip.dstY1)) {
103 return; /* nothing to draw/blit */
104 }
105 blit.scissor_enable =
106 (dstX0 != clip.dstX0) ||
107 (dstY0 != clip.dstY0) ||
108 (dstX1 != clip.dstX1) ||
109 (dstY1 != clip.dstY1);
110
111 if (st_fb_orientation(drawFB) == Y_0_TOP) {
112 /* invert Y for dest */
113 dstY0 = drawFB->Height - dstY0;
114 dstY1 = drawFB->Height - dstY1;
115 /* invert Y for clip */
116 clip.dstY0 = drawFB->Height - clip.dstY0;
117 clip.dstY1 = drawFB->Height - clip.dstY1;
118 }
119 if (blit.scissor_enable) {
120 blit.scissor.minx = MIN2(clip.dstX0, clip.dstX1);
121 blit.scissor.miny = MIN2(clip.dstY0, clip.dstY1);
122 blit.scissor.maxx = MAX2(clip.dstX0, clip.dstX1);
123 blit.scissor.maxy = MAX2(clip.dstY0, clip.dstY1);
124 #if 0
125 debug_printf("scissor = (%i,%i)-(%i,%i)\n",
126 blit.scissor.minx,blit.scissor.miny,
127 blit.scissor.maxx,blit.scissor.maxy);
128 #endif
129 }
130
131 if (st_fb_orientation(readFB) == Y_0_TOP) {
132 /* invert Y for src */
133 srcY0 = readFB->Height - srcY0;
134 srcY1 = readFB->Height - srcY1;
135 }
136
137 if (srcY0 > srcY1 && dstY0 > dstY1) {
138 /* Both src and dst are upside down. Swap Y to make it
139 * right-side up to increase odds of using a fast path.
140 * Recall that all Gallium raster coords have Y=0=top.
141 */
142 GLint tmp;
143 tmp = srcY0;
144 srcY0 = srcY1;
145 srcY1 = tmp;
146 tmp = dstY0;
147 dstY0 = dstY1;
148 dstY1 = tmp;
149 }
150
151 blit.src.box.depth = 1;
152 blit.dst.box.depth = 1;
153
154 /* Destination dimensions have to be positive: */
155 if (dstX0 < dstX1) {
156 blit.dst.box.x = dstX0;
157 blit.src.box.x = srcX0;
158 blit.dst.box.width = dstX1 - dstX0;
159 blit.src.box.width = srcX1 - srcX0;
160 } else {
161 blit.dst.box.x = dstX1;
162 blit.src.box.x = srcX1;
163 blit.dst.box.width = dstX0 - dstX1;
164 blit.src.box.width = srcX0 - srcX1;
165 }
166 if (dstY0 < dstY1) {
167 blit.dst.box.y = dstY0;
168 blit.src.box.y = srcY0;
169 blit.dst.box.height = dstY1 - dstY0;
170 blit.src.box.height = srcY1 - srcY0;
171 } else {
172 blit.dst.box.y = dstY1;
173 blit.src.box.y = srcY1;
174 blit.dst.box.height = dstY0 - dstY1;
175 blit.src.box.height = srcY0 - srcY1;
176 }
177
178 blit.filter = pFilter;
179
180 if (mask & GL_COLOR_BUFFER_BIT) {
181 struct gl_renderbuffer_attachment *srcAtt =
182 &readFB->Attachment[readFB->_ColorReadBufferIndex];
183
184 blit.mask = PIPE_MASK_RGBA;
185
186 if (srcAtt->Type == GL_TEXTURE) {
187 struct st_texture_object *srcObj = st_texture_object(srcAtt->Texture);
188 GLuint i;
189
190 if (!srcObj || !srcObj->pt) {
191 return;
192 }
193
194 for (i = 0; i < drawFB->_NumColorDrawBuffers; i++) {
195 struct st_renderbuffer *dstRb =
196 st_renderbuffer(drawFB->_ColorDrawBuffers[i]);
197
198 if (dstRb) {
199 struct pipe_surface *dstSurf = dstRb->surface;
200
201 if (dstSurf) {
202 blit.dst.resource = dstSurf->texture;
203 blit.dst.level = dstSurf->u.tex.level;
204 blit.dst.box.z = dstSurf->u.tex.first_layer;
205 blit.dst.format = util_format_linear(dstSurf->format);
206
207 blit.src.resource = srcObj->pt;
208 blit.src.level = srcAtt->TextureLevel;
209 blit.src.box.z = srcAtt->Zoffset + srcAtt->CubeMapFace;
210 blit.src.format = util_format_linear(srcObj->pt->format);
211
212 st->pipe->blit(st->pipe, &blit);
213 }
214 }
215 }
216 }
217 else {
218 struct st_renderbuffer *srcRb =
219 st_renderbuffer(readFB->_ColorReadBuffer);
220 struct pipe_surface *srcSurf;
221 GLuint i;
222
223 if (!srcRb || !srcRb->surface) {
224 return;
225 }
226
227 srcSurf = srcRb->surface;
228
229 for (i = 0; i < drawFB->_NumColorDrawBuffers; i++) {
230 struct st_renderbuffer *dstRb =
231 st_renderbuffer(drawFB->_ColorDrawBuffers[i]);
232
233 if (dstRb) {
234 struct pipe_surface *dstSurf = dstRb->surface;
235
236 if (dstSurf) {
237 blit.dst.resource = dstSurf->texture;
238 blit.dst.level = dstSurf->u.tex.level;
239 blit.dst.box.z = dstSurf->u.tex.first_layer;
240 blit.dst.format = util_format_linear(dstSurf->format);
241
242 blit.src.resource = srcSurf->texture;
243 blit.src.level = srcSurf->u.tex.level;
244 blit.src.box.z = srcSurf->u.tex.first_layer;
245 blit.src.format = util_format_linear(srcSurf->format);
246
247 st->pipe->blit(st->pipe, &blit);
248 }
249 }
250 }
251 }
252 }
253
254 if (mask & depthStencil) {
255 /* depth and/or stencil blit */
256
257 /* get src/dst depth surfaces */
258 struct gl_renderbuffer_attachment *srcDepth =
259 &readFB->Attachment[BUFFER_DEPTH];
260 struct gl_renderbuffer_attachment *dstDepth =
261 &drawFB->Attachment[BUFFER_DEPTH];
262 struct gl_renderbuffer_attachment *srcStencil =
263 &readFB->Attachment[BUFFER_STENCIL];
264 struct gl_renderbuffer_attachment *dstStencil =
265 &drawFB->Attachment[BUFFER_STENCIL];
266
267 struct st_renderbuffer *srcDepthRb =
268 st_renderbuffer(srcDepth->Renderbuffer);
269 struct st_renderbuffer *dstDepthRb =
270 st_renderbuffer(dstDepth->Renderbuffer);
271 struct pipe_surface *dstDepthSurf =
272 dstDepthRb ? dstDepthRb->surface : NULL;
273
274 struct st_renderbuffer *srcStencilRb =
275 st_renderbuffer(srcStencil->Renderbuffer);
276 struct st_renderbuffer *dstStencilRb =
277 st_renderbuffer(dstStencil->Renderbuffer);
278 struct pipe_surface *dstStencilSurf =
279 dstStencilRb ? dstStencilRb->surface : NULL;
280
281 if (st_is_depth_stencil_combined(srcDepth, srcStencil) &&
282 st_is_depth_stencil_combined(dstDepth, dstStencil)) {
283 blit.mask = 0;
284 if (mask & GL_DEPTH_BUFFER_BIT)
285 blit.mask |= PIPE_MASK_Z;
286 if (mask & GL_STENCIL_BUFFER_BIT)
287 blit.mask |= PIPE_MASK_S;
288
289 blit.dst.resource = dstDepthSurf->texture;
290 blit.dst.level = dstDepthSurf->u.tex.level;
291 blit.dst.box.z = dstDepthSurf->u.tex.first_layer;
292 blit.dst.format = dstDepthSurf->format;
293
294 blit.src.resource = srcDepthRb->texture;
295 blit.src.level = srcDepthRb->surface->u.tex.level;
296 blit.src.box.z = srcDepthRb->surface->u.tex.first_layer;
297 blit.src.format = srcDepthRb->surface->format;
298
299 st->pipe->blit(st->pipe, &blit);
300 }
301 else {
302 /* blitting depth and stencil separately */
303
304 if (mask & GL_DEPTH_BUFFER_BIT) {
305 blit.mask = PIPE_MASK_Z;
306
307 blit.dst.resource = dstDepthSurf->texture;
308 blit.dst.level = dstDepthSurf->u.tex.level;
309 blit.dst.box.z = dstDepthSurf->u.tex.first_layer;
310 blit.dst.format = dstDepthSurf->format;
311
312 blit.src.resource = srcDepthRb->texture;
313 blit.src.level = srcDepthRb->surface->u.tex.level;
314 blit.src.box.z = srcDepthRb->surface->u.tex.first_layer;
315 blit.src.format = srcDepthRb->surface->format;
316
317 st->pipe->blit(st->pipe, &blit);
318 }
319
320 if (mask & GL_STENCIL_BUFFER_BIT) {
321 blit.mask = PIPE_MASK_S;
322
323 blit.dst.resource = dstStencilSurf->texture;
324 blit.dst.level = dstStencilSurf->u.tex.level;
325 blit.dst.box.z = dstStencilSurf->u.tex.first_layer;
326 blit.dst.format = dstStencilSurf->format;
327
328 blit.src.resource = srcStencilRb->texture;
329 blit.src.level = srcStencilRb->surface->u.tex.level;
330 blit.src.box.z = srcStencilRb->surface->u.tex.first_layer;
331 blit.src.format = srcStencilRb->surface->format;
332
333 st->pipe->blit(st->pipe, &blit);
334 }
335 }
336 }
337 }
338
339
340 void
341 st_init_blit_functions(struct dd_function_table *functions)
342 {
343 functions->BlitFramebuffer = st_BlitFramebuffer;
344 }