s/Tungsten Graphics/VMware/
[mesa.git] / src / mesa / state_tracker / st_cb_blit.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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
37 #include "st_context.h"
38 #include "st_texture.h"
39 #include "st_cb_blit.h"
40 #include "st_cb_fbo.h"
41 #include "st_atom.h"
42
43 #include "util/u_format.h"
44
45
46 static void
47 st_adjust_blit_for_msaa_resolve(struct pipe_blit_info *blit)
48 {
49 /* Even though we do multisample resolves at the time of the blit, OpenGL
50 * specification defines them as if they happen at the time of rendering,
51 * which means that the type of averaging we do during the resolve should
52 * only depend on the source format; the destination format should be
53 * ignored. But, specification doesn't seem to be strict about it.
54 *
55 * It has been observed that mulitisample resolves produce slightly better
56 * looking images when averaging is done using destination format. NVIDIA's
57 * proprietary OpenGL driver also follows this approach.
58 *
59 * When multisampling, if the source and destination formats are equal
60 * (aside from the color space), we choose to blit in sRGB space to get
61 * this higher quality image.
62 */
63 if (blit->src.resource->nr_samples > 1 &&
64 blit->dst.resource->nr_samples <= 1) {
65 blit->dst.format = blit->dst.resource->format;
66
67 if (util_format_is_srgb(blit->dst.resource->format))
68 blit->src.format = util_format_srgb(blit->src.resource->format);
69 else
70 blit->src.format = util_format_linear(blit->src.resource->format);
71 }
72 }
73
74 static void
75 st_BlitFramebuffer(struct gl_context *ctx,
76 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
77 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
78 GLbitfield mask, GLenum filter)
79 {
80 const GLbitfield depthStencil = (GL_DEPTH_BUFFER_BIT |
81 GL_STENCIL_BUFFER_BIT);
82 struct st_context *st = st_context(ctx);
83 const uint pFilter = ((filter == GL_NEAREST)
84 ? PIPE_TEX_FILTER_NEAREST
85 : PIPE_TEX_FILTER_LINEAR);
86 struct gl_framebuffer *readFB = ctx->ReadBuffer;
87 struct gl_framebuffer *drawFB = ctx->DrawBuffer;
88 struct {
89 GLint srcX0, srcY0, srcX1, srcY1;
90 GLint dstX0, dstY0, dstX1, dstY1;
91 } clip;
92 struct pipe_blit_info blit;
93
94 st_validate_state(st);
95
96 clip.srcX0 = srcX0;
97 clip.srcY0 = srcY0;
98 clip.srcX1 = srcX1;
99 clip.srcY1 = srcY1;
100 clip.dstX0 = dstX0;
101 clip.dstY0 = dstY0;
102 clip.dstX1 = dstX1;
103 clip.dstY1 = dstY1;
104
105 /* NOTE: If the src and dst dimensions don't match, we cannot simply adjust
106 * the integer coordinates to account for clipping (or scissors) because that
107 * would make us cut off fractional parts, affecting the result of the blit.
108 *
109 * XXX: This should depend on mask !
110 */
111 if (!_mesa_clip_blit(ctx,
112 &clip.srcX0, &clip.srcY0, &clip.srcX1, &clip.srcY1,
113 &clip.dstX0, &clip.dstY0, &clip.dstX1, &clip.dstY1)) {
114 return; /* nothing to draw/blit */
115 }
116 blit.scissor_enable =
117 (dstX0 != clip.dstX0) ||
118 (dstY0 != clip.dstY0) ||
119 (dstX1 != clip.dstX1) ||
120 (dstY1 != clip.dstY1);
121
122 if (st_fb_orientation(drawFB) == Y_0_TOP) {
123 /* invert Y for dest */
124 dstY0 = drawFB->Height - dstY0;
125 dstY1 = drawFB->Height - dstY1;
126 /* invert Y for clip */
127 clip.dstY0 = drawFB->Height - clip.dstY0;
128 clip.dstY1 = drawFB->Height - clip.dstY1;
129 }
130 if (blit.scissor_enable) {
131 blit.scissor.minx = MIN2(clip.dstX0, clip.dstX1);
132 blit.scissor.miny = MIN2(clip.dstY0, clip.dstY1);
133 blit.scissor.maxx = MAX2(clip.dstX0, clip.dstX1);
134 blit.scissor.maxy = MAX2(clip.dstY0, clip.dstY1);
135 #if 0
136 debug_printf("scissor = (%i,%i)-(%i,%i)\n",
137 blit.scissor.minx,blit.scissor.miny,
138 blit.scissor.maxx,blit.scissor.maxy);
139 #endif
140 }
141
142 if (st_fb_orientation(readFB) == Y_0_TOP) {
143 /* invert Y for src */
144 srcY0 = readFB->Height - srcY0;
145 srcY1 = readFB->Height - srcY1;
146 }
147
148 if (srcY0 > srcY1 && dstY0 > dstY1) {
149 /* Both src and dst are upside down. Swap Y to make it
150 * right-side up to increase odds of using a fast path.
151 * Recall that all Gallium raster coords have Y=0=top.
152 */
153 GLint tmp;
154 tmp = srcY0;
155 srcY0 = srcY1;
156 srcY1 = tmp;
157 tmp = dstY0;
158 dstY0 = dstY1;
159 dstY1 = tmp;
160 }
161
162 blit.src.box.depth = 1;
163 blit.dst.box.depth = 1;
164
165 /* Destination dimensions have to be positive: */
166 if (dstX0 < dstX1) {
167 blit.dst.box.x = dstX0;
168 blit.src.box.x = srcX0;
169 blit.dst.box.width = dstX1 - dstX0;
170 blit.src.box.width = srcX1 - srcX0;
171 } else {
172 blit.dst.box.x = dstX1;
173 blit.src.box.x = srcX1;
174 blit.dst.box.width = dstX0 - dstX1;
175 blit.src.box.width = srcX0 - srcX1;
176 }
177 if (dstY0 < dstY1) {
178 blit.dst.box.y = dstY0;
179 blit.src.box.y = srcY0;
180 blit.dst.box.height = dstY1 - dstY0;
181 blit.src.box.height = srcY1 - srcY0;
182 } else {
183 blit.dst.box.y = dstY1;
184 blit.src.box.y = srcY1;
185 blit.dst.box.height = dstY0 - dstY1;
186 blit.src.box.height = srcY0 - srcY1;
187 }
188
189 blit.filter = pFilter;
190
191 if (mask & GL_COLOR_BUFFER_BIT) {
192 struct gl_renderbuffer_attachment *srcAtt =
193 &readFB->Attachment[readFB->_ColorReadBufferIndex];
194
195 blit.mask = PIPE_MASK_RGBA;
196
197 if (srcAtt->Type == GL_TEXTURE) {
198 struct st_texture_object *srcObj = st_texture_object(srcAtt->Texture);
199 GLuint i;
200
201 if (!srcObj || !srcObj->pt) {
202 return;
203 }
204
205 for (i = 0; i < drawFB->_NumColorDrawBuffers; i++) {
206 struct st_renderbuffer *dstRb =
207 st_renderbuffer(drawFB->_ColorDrawBuffers[i]);
208
209 if (dstRb) {
210 struct pipe_surface *dstSurf = dstRb->surface;
211
212 if (dstSurf) {
213 blit.dst.resource = dstSurf->texture;
214 blit.dst.level = dstSurf->u.tex.level;
215 blit.dst.box.z = dstSurf->u.tex.first_layer;
216 blit.dst.format = util_format_linear(dstSurf->format);
217
218 blit.src.resource = srcObj->pt;
219 blit.src.level = srcAtt->TextureLevel;
220 blit.src.box.z = srcAtt->Zoffset + srcAtt->CubeMapFace;
221 blit.src.format = util_format_linear(srcObj->pt->format);
222
223 st_adjust_blit_for_msaa_resolve(&blit);
224
225 st->pipe->blit(st->pipe, &blit);
226 }
227 }
228 }
229 }
230 else {
231 struct st_renderbuffer *srcRb =
232 st_renderbuffer(readFB->_ColorReadBuffer);
233 struct pipe_surface *srcSurf;
234 GLuint i;
235
236 if (!srcRb || !srcRb->surface) {
237 return;
238 }
239
240 srcSurf = srcRb->surface;
241
242 for (i = 0; i < drawFB->_NumColorDrawBuffers; i++) {
243 struct st_renderbuffer *dstRb =
244 st_renderbuffer(drawFB->_ColorDrawBuffers[i]);
245
246 if (dstRb) {
247 struct pipe_surface *dstSurf = dstRb->surface;
248
249 if (dstSurf) {
250 blit.dst.resource = dstSurf->texture;
251 blit.dst.level = dstSurf->u.tex.level;
252 blit.dst.box.z = dstSurf->u.tex.first_layer;
253 blit.dst.format = util_format_linear(dstSurf->format);
254
255 blit.src.resource = srcSurf->texture;
256 blit.src.level = srcSurf->u.tex.level;
257 blit.src.box.z = srcSurf->u.tex.first_layer;
258 blit.src.format = util_format_linear(srcSurf->format);
259
260 st_adjust_blit_for_msaa_resolve(&blit);
261
262 st->pipe->blit(st->pipe, &blit);
263 }
264 }
265 }
266 }
267 }
268
269 if (mask & depthStencil) {
270 /* depth and/or stencil blit */
271
272 /* get src/dst depth surfaces */
273 struct st_renderbuffer *srcDepthRb =
274 st_renderbuffer(readFB->Attachment[BUFFER_DEPTH].Renderbuffer);
275 struct st_renderbuffer *dstDepthRb =
276 st_renderbuffer(drawFB->Attachment[BUFFER_DEPTH].Renderbuffer);
277 struct pipe_surface *dstDepthSurf =
278 dstDepthRb ? dstDepthRb->surface : NULL;
279
280 struct st_renderbuffer *srcStencilRb =
281 st_renderbuffer(readFB->Attachment[BUFFER_STENCIL].Renderbuffer);
282 struct st_renderbuffer *dstStencilRb =
283 st_renderbuffer(drawFB->Attachment[BUFFER_STENCIL].Renderbuffer);
284 struct pipe_surface *dstStencilSurf =
285 dstStencilRb ? dstStencilRb->surface : NULL;
286
287 if (_mesa_has_depthstencil_combined(readFB) &&
288 _mesa_has_depthstencil_combined(drawFB)) {
289 blit.mask = 0;
290 if (mask & GL_DEPTH_BUFFER_BIT)
291 blit.mask |= PIPE_MASK_Z;
292 if (mask & GL_STENCIL_BUFFER_BIT)
293 blit.mask |= PIPE_MASK_S;
294
295 blit.dst.resource = dstDepthSurf->texture;
296 blit.dst.level = dstDepthSurf->u.tex.level;
297 blit.dst.box.z = dstDepthSurf->u.tex.first_layer;
298 blit.dst.format = dstDepthSurf->format;
299
300 blit.src.resource = srcDepthRb->texture;
301 blit.src.level = srcDepthRb->surface->u.tex.level;
302 blit.src.box.z = srcDepthRb->surface->u.tex.first_layer;
303 blit.src.format = srcDepthRb->surface->format;
304
305 st->pipe->blit(st->pipe, &blit);
306 }
307 else {
308 /* blitting depth and stencil separately */
309
310 if (mask & GL_DEPTH_BUFFER_BIT) {
311 blit.mask = PIPE_MASK_Z;
312
313 blit.dst.resource = dstDepthSurf->texture;
314 blit.dst.level = dstDepthSurf->u.tex.level;
315 blit.dst.box.z = dstDepthSurf->u.tex.first_layer;
316 blit.dst.format = dstDepthSurf->format;
317
318 blit.src.resource = srcDepthRb->texture;
319 blit.src.level = srcDepthRb->surface->u.tex.level;
320 blit.src.box.z = srcDepthRb->surface->u.tex.first_layer;
321 blit.src.format = srcDepthRb->surface->format;
322
323 st->pipe->blit(st->pipe, &blit);
324 }
325
326 if (mask & GL_STENCIL_BUFFER_BIT) {
327 blit.mask = PIPE_MASK_S;
328
329 blit.dst.resource = dstStencilSurf->texture;
330 blit.dst.level = dstStencilSurf->u.tex.level;
331 blit.dst.box.z = dstStencilSurf->u.tex.first_layer;
332 blit.dst.format = dstStencilSurf->format;
333
334 blit.src.resource = srcStencilRb->texture;
335 blit.src.level = srcStencilRb->surface->u.tex.level;
336 blit.src.box.z = srcStencilRb->surface->u.tex.first_layer;
337 blit.src.format = srcStencilRb->surface->format;
338
339 st->pipe->blit(st->pipe, &blit);
340 }
341 }
342 }
343 }
344
345
346 void
347 st_init_blit_functions(struct dd_function_table *functions)
348 {
349 functions->BlitFramebuffer = st_BlitFramebuffer;
350 }