2 * Mesa 3-D graphics library
4 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
25 #include "main/glheader.h"
26 #include "main/mtypes.h"
27 #include "main/imports.h"
28 #include "main/arbprogram.h"
29 #include "main/arrayobj.h"
30 #include "main/blend.h"
31 #include "main/condrender.h"
32 #include "main/depth.h"
33 #include "main/enable.h"
34 #include "main/enums.h"
35 #include "main/fbobject.h"
36 #include "main/macros.h"
37 #include "main/matrix.h"
38 #include "main/multisample.h"
39 #include "main/objectlabel.h"
40 #include "main/readpix.h"
41 #include "main/shaderapi.h"
42 #include "main/texobj.h"
43 #include "main/texenv.h"
44 #include "main/teximage.h"
45 #include "main/texparam.h"
46 #include "main/varray.h"
47 #include "main/viewport.h"
48 #include "swrast/swrast.h"
49 #include "drivers/common/meta.h"
50 #include "../glsl/ralloc.h"
52 /** Return offset in bytes of the field within a vertex struct */
53 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
56 * One-time init for drawing depth pixels.
59 init_blit_depth_pixels(struct gl_context
*ctx
)
61 static const char *program
=
63 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
66 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
67 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
68 const char *texTarget
;
70 assert(blit
->DepthFP
== 0);
72 /* replace %s with "RECT" or "2D" */
73 assert(strlen(program
) + 4 < sizeof(program2
));
74 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
78 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
80 _mesa_GenProgramsARB(1, &blit
->DepthFP
);
81 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
82 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
83 strlen(program2
), (const GLubyte
*) program2
);
87 setup_glsl_msaa_blit_shader(struct gl_context
*ctx
,
88 struct blit_state
*blit
,
89 struct gl_renderbuffer
*src_rb
,
92 const char *vs_source
;
96 enum blit_msaa_shader shader_index
;
97 bool dst_is_msaa
= false;
99 const char *vec4_prefix
;
103 src_datatype
= _mesa_get_format_datatype(src_rb
->Format
);
105 /* depth-or-color glCopyTexImage fallback path that passes a NULL rb and
106 * doesn't handle integer.
108 src_datatype
= GL_UNSIGNED_NORMALIZED
;
111 if (ctx
->DrawBuffer
->Visual
.samples
> 1) {
112 /* If you're calling meta_BlitFramebuffer with the destination
113 * multisampled, this is the only path that will work -- swrast and
114 * CopyTexImage won't work on it either.
116 assert(ctx
->Extensions
.ARB_sample_shading
);
120 /* We need shader invocation per sample, not per pixel */
121 _mesa_set_enable(ctx
, GL_MULTISAMPLE
, GL_TRUE
);
122 _mesa_set_enable(ctx
, GL_SAMPLE_SHADING
, GL_TRUE
);
123 _mesa_MinSampleShading(1.0);
127 case GL_TEXTURE_2D_MULTISAMPLE
:
128 if (src_rb
->_BaseFormat
== GL_DEPTH_COMPONENT
||
129 src_rb
->_BaseFormat
== GL_DEPTH_STENCIL
) {
131 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY
;
133 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE
;
136 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY
;
138 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
;
142 _mesa_problem(ctx
, "Unkown texture target %s\n",
143 _mesa_lookup_enum_by_nr(target
));
144 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
;
147 /* We rely on the enum being sorted this way. */
148 STATIC_ASSERT(BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_INT
==
149 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+ 1);
150 STATIC_ASSERT(BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_UINT
==
151 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+ 2);
152 if (src_datatype
== GL_INT
) {
155 } else if (src_datatype
== GL_UNSIGNED_INT
) {
162 if (blit
->msaa_shaders
[shader_index
]) {
163 _mesa_UseProgram(blit
->msaa_shaders
[shader_index
]);
167 mem_ctx
= ralloc_context(NULL
);
169 if (shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE
||
170 shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY
) {
172 const char *arb_sample_shading_extension_string
;
175 arb_sample_shading_extension_string
= "#extension GL_ARB_sample_shading : enable";
176 sample_index
= "gl_SampleID";
177 name
= "depth MSAA copy";
179 /* Don't need that extension, since we're drawing to a single-sampled
182 arb_sample_shading_extension_string
= "";
183 /* From the GL 4.3 spec:
185 * "If there is a multisample buffer (the value of SAMPLE_BUFFERS
186 * is one), then values are obtained from the depth samples in
187 * this buffer. It is recommended that the depth value of the
188 * centermost sample be used, though implementations may choose
189 * any function of the depth sample values at each pixel.
191 * We're slacking and instead of choosing centermost, we've got 0.
194 name
= "depth MSAA resolve";
197 vs_source
= ralloc_asprintf(mem_ctx
,
199 "in vec2 position;\n"
200 "in vec2 textureCoords;\n"
201 "out vec2 texCoords;\n"
204 " texCoords = textureCoords;\n"
205 " gl_Position = vec4(position, 0.0, 1.0);\n"
207 fs_source
= ralloc_asprintf(mem_ctx
,
209 "#extension GL_ARB_texture_multisample : enable\n"
211 "uniform sampler2DMS texSampler;\n"
212 "in vec2 texCoords;\n"
213 "out vec4 out_color;\n"
217 " gl_FragDepth = texelFetch(texSampler, ivec2(texCoords), %s).r;\n"
219 arb_sample_shading_extension_string
,
222 /* You can create 2D_MULTISAMPLE textures with 0 sample count (meaning 1
223 * sample). Yes, this is ridiculous.
226 char *sample_resolve
;
227 const char *arb_sample_shading_extension_string
;
228 const char *merge_function
;
229 name
= ralloc_asprintf(mem_ctx
, "%svec4 MSAA %s",
231 dst_is_msaa
? "copy" : "resolve");
233 samples
= MAX2(src_rb
->NumSamples
, 1);
236 arb_sample_shading_extension_string
= "#extension GL_ARB_sample_shading : enable";
237 sample_resolve
= ralloc_asprintf(mem_ctx
, " out_color = texelFetch(texSampler, ivec2(texCoords), gl_SampleID);");
243 if (src_datatype
== GL_INT
|| src_datatype
== GL_UNSIGNED_INT
) {
245 "gvec4 merge(gvec4 a, gvec4 b) { return (a >> gvec4(1)) + (b >> gvec4(1)) + (a & b & gvec4(1)); }\n";
247 /* The divide will happen at the end for floats. */
249 "vec4 merge(vec4 a, vec4 b) { return (a + b); }\n";
252 arb_sample_shading_extension_string
= "";
254 /* We're assuming power of two samples for this resolution procedure.
256 * To avoid losing any floating point precision if the samples all
257 * happen to have the same value, we merge pairs of values at a time
258 * (so the floating point exponent just gets increased), rather than
259 * doing a naive sum and dividing.
261 assert((samples
& (samples
- 1)) == 0);
262 /* Fetch each individual sample. */
263 sample_resolve
= rzalloc_size(mem_ctx
, 1);
264 for (i
= 0; i
< samples
; i
++) {
265 ralloc_asprintf_append(&sample_resolve
,
266 " gvec4 sample_1_%d = texelFetch(texSampler, ivec2(texCoords), %d);\n",
269 /* Now, merge each pair of samples, then merge each pair of those,
272 for (step
= 2; step
<= samples
; step
*= 2) {
273 for (i
= 0; i
< samples
; i
+= step
) {
274 ralloc_asprintf_append(&sample_resolve
,
275 " gvec4 sample_%d_%d = merge(sample_%d_%d, sample_%d_%d);\n",
278 step
/ 2, i
+ step
/ 2);
282 /* Scale the final result. */
283 if (src_datatype
== GL_UNSIGNED_INT
|| src_datatype
== GL_INT
) {
284 ralloc_asprintf_append(&sample_resolve
,
285 " out_color = sample_%d_0;\n",
288 ralloc_asprintf_append(&sample_resolve
,
289 " out_color = sample_%d_0 / %f;\n",
290 samples
, (float)samples
);
294 vs_source
= ralloc_asprintf(mem_ctx
,
296 "in vec2 position;\n"
297 "in vec2 textureCoords;\n"
298 "out vec2 texCoords;\n"
301 " texCoords = textureCoords;\n"
302 " gl_Position = vec4(position, 0.0, 1.0);\n"
304 fs_source
= ralloc_asprintf(mem_ctx
,
306 "#extension GL_ARB_texture_multisample : enable\n"
308 "#define gvec4 %svec4\n"
309 "uniform %ssampler2DMS texSampler;\n"
310 "in vec2 texCoords;\n"
311 "out gvec4 out_color;\n"
313 "%s" /* merge_function */
316 "%s\n" /* sample_resolve */
318 arb_sample_shading_extension_string
,
325 vs
= _mesa_meta_compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
326 fs
= _mesa_meta_compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
328 blit
->msaa_shaders
[shader_index
] = _mesa_CreateProgramObjectARB();
329 _mesa_AttachShader(blit
->msaa_shaders
[shader_index
], fs
);
330 _mesa_DeleteObjectARB(fs
);
331 _mesa_AttachShader(blit
->msaa_shaders
[shader_index
], vs
);
332 _mesa_DeleteObjectARB(vs
);
333 _mesa_BindAttribLocation(blit
->msaa_shaders
[shader_index
], 0, "position");
334 _mesa_BindAttribLocation(blit
->msaa_shaders
[shader_index
], 1, "texcoords");
335 _mesa_meta_link_program_with_debug(ctx
, blit
->msaa_shaders
[shader_index
]);
336 _mesa_ObjectLabel(GL_PROGRAM
, blit
->msaa_shaders
[shader_index
], -1, name
);
337 ralloc_free(mem_ctx
);
339 _mesa_UseProgram(blit
->msaa_shaders
[shader_index
]);
343 setup_glsl_blit_framebuffer(struct gl_context
*ctx
,
344 struct blit_state
*blit
,
345 struct gl_renderbuffer
*src_rb
,
348 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
349 assert(_mesa_is_desktop_gl(ctx
) || target
== GL_TEXTURE_2D
);
351 _mesa_meta_setup_vertex_objects(&blit
->VAO
, &blit
->VBO
, true, 2, 2, 0);
353 if (target
== GL_TEXTURE_2D_MULTISAMPLE
) {
354 setup_glsl_msaa_blit_shader(ctx
, blit
, src_rb
, target
);
356 _mesa_meta_setup_blit_shader(ctx
, target
, &blit
->shaders
);
361 * Try to do a color or depth glBlitFramebuffer using texturing.
363 * We can do this when the src renderbuffer is actually a texture, or when the
364 * driver exposes BindRenderbufferTexImage().
367 blitframebuffer_texture(struct gl_context
*ctx
,
368 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
369 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
370 GLenum filter
, GLint flipX
, GLint flipY
,
371 GLboolean glsl_version
, GLboolean do_depth
)
373 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
374 int att_index
= do_depth
? BUFFER_DEPTH
: readFb
->_ColorReadBufferIndex
;
375 const struct gl_renderbuffer_attachment
*readAtt
=
376 &readFb
->Attachment
[att_index
];
377 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
378 const GLint dstX
= MIN2(dstX0
, dstX1
);
379 const GLint dstY
= MIN2(dstY0
, dstY1
);
380 const GLint dstW
= abs(dstX1
- dstX0
);
381 const GLint dstH
= abs(dstY1
- dstY0
);
382 struct gl_texture_object
*texObj
;
387 GLuint sampler
, samplerSave
=
388 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
389 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
391 struct gl_renderbuffer
*rb
= readAtt
->Renderbuffer
;
393 if (rb
->NumSamples
&& !ctx
->Extensions
.ARB_texture_multisample
)
396 if (filter
== GL_SCALED_RESOLVE_FASTEST_EXT
||
397 filter
== GL_SCALED_RESOLVE_NICEST_EXT
) {
401 if (readAtt
->Texture
) {
402 /* If there's a texture attached of a type we can handle, then just use
405 srcLevel
= readAtt
->TextureLevel
;
406 texObj
= readAtt
->Texture
;
407 target
= texObj
->Target
;
411 case GL_TEXTURE_RECTANGLE
:
412 case GL_TEXTURE_2D_MULTISAMPLE
:
417 } else if (ctx
->Driver
.BindRenderbufferTexImage
) {
418 /* Otherwise, we need the driver to be able to bind a renderbuffer as
421 struct gl_texture_image
*texImage
;
423 if (rb
->NumSamples
> 1)
424 target
= GL_TEXTURE_2D_MULTISAMPLE
;
426 target
= GL_TEXTURE_2D
;
428 _mesa_GenTextures(1, &tempTex
);
429 _mesa_BindTexture(target
, tempTex
);
431 texObj
= _mesa_lookup_texture(ctx
, tempTex
);
432 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, srcLevel
);
434 if (!ctx
->Driver
.BindRenderbufferTexImage(ctx
, rb
, texImage
)) {
435 _mesa_DeleteTextures(1, &tempTex
);
438 if (ctx
->Driver
.FinishRenderTexture
&&
439 !rb
->NeedsFinishRenderTexture
) {
440 rb
->NeedsFinishRenderTexture
= true;
441 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
444 if (_mesa_is_winsys_fbo(readFb
)) {
446 srcY0
= rb
->Height
- srcY1
;
447 srcY1
= rb
->Height
- temp
;
455 baseLevelSave
= texObj
->BaseLevel
;
456 maxLevelSave
= texObj
->MaxLevel
;
459 setup_glsl_blit_framebuffer(ctx
, blit
, rb
, target
);
462 _mesa_meta_setup_ff_tnl_for_blit(&ctx
->Meta
->Blit
.VAO
,
463 &ctx
->Meta
->Blit
.VBO
,
467 _mesa_GenSamplers(1, &sampler
);
468 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, sampler
);
471 printf("Blit from texture!\n");
472 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
473 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
476 /* Prepare src texture state */
477 _mesa_BindTexture(target
, texObj
->Name
);
478 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MIN_FILTER
, filter
);
479 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MAG_FILTER
, filter
);
480 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
481 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
482 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
484 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
485 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
487 /* Always do our blits with no net sRGB decode or encode.
489 * However, if both the src and dst can be srgb decode/encoded, enable them
490 * so that we do any blending (from scaling or from MSAA resolves) in the
493 * Our choice of not doing any net encode/decode is from the GL 3.0
496 * "Blit operations bypass the fragment pipeline. The only fragment
497 * operations which affect a blit are the pixel ownership test and the
500 * The GL 4.4 specification disagrees and says that the sRGB part of the
501 * fragment pipeline applies, but this was found to break applications.
503 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
504 if (_mesa_get_format_color_encoding(rb
->Format
) == GL_SRGB
&&
505 ctx
->DrawBuffer
->Visual
.sRGBCapable
) {
506 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
508 _mesa_set_framebuffer_srgb(ctx
, GL_TRUE
);
510 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
512 /* set_framebuffer_srgb was set by _mesa_meta_begin(). */
517 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
518 _mesa_set_enable(ctx
, target
, GL_TRUE
);
521 /* Prepare vertex data (the VBO was previously created and bound) */
523 struct vertex verts
[4];
524 GLfloat s0
, t0
, s1
, t1
;
526 if (target
== GL_TEXTURE_2D
) {
527 const struct gl_texture_image
*texImage
528 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
529 s0
= srcX0
/ (float) texImage
->Width
;
530 s1
= srcX1
/ (float) texImage
->Width
;
531 t0
= srcY0
/ (float) texImage
->Height
;
532 t1
= srcY1
/ (float) texImage
->Height
;
535 assert(target
== GL_TEXTURE_RECTANGLE_ARB
||
536 target
== GL_TEXTURE_2D_MULTISAMPLE
);
543 /* Silence valgrind warnings about reading uninitialized stack. */
544 memset(verts
, 0, sizeof(verts
));
546 /* setup vertex positions */
547 verts
[0].x
= -1.0F
* flipX
;
548 verts
[0].y
= -1.0F
* flipY
;
549 verts
[1].x
= 1.0F
* flipX
;
550 verts
[1].y
= -1.0F
* flipY
;
551 verts
[2].x
= 1.0F
* flipX
;
552 verts
[2].y
= 1.0F
* flipY
;
553 verts
[3].x
= -1.0F
* flipX
;
554 verts
[3].y
= 1.0F
* flipY
;
556 verts
[0].tex
[0] = s0
;
557 verts
[0].tex
[1] = t0
;
558 verts
[1].tex
[0] = s1
;
559 verts
[1].tex
[1] = t0
;
560 verts
[2].tex
[0] = s1
;
561 verts
[2].tex
[1] = t1
;
562 verts
[3].tex
[0] = s0
;
563 verts
[3].tex
[1] = t1
;
565 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
569 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
570 _mesa_ColorMask(!do_depth
, !do_depth
, !do_depth
, !do_depth
);
571 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, do_depth
);
572 _mesa_DepthMask(do_depth
);
573 _mesa_DepthFunc(GL_ALWAYS
);
575 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
577 /* Restore texture object state, the texture binding will
578 * be restored by _mesa_meta_end().
580 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
581 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
582 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
585 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
586 _mesa_DeleteSamplers(1, &sampler
);
588 _mesa_DeleteTextures(1, &tempTex
);
594 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
595 * of texture mapping and polygon rendering.
598 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
599 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
600 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
601 GLbitfield mask
, GLenum filter
)
603 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
604 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
605 struct temp_texture
*depthTex
= _mesa_meta_get_temp_depth_texture(ctx
);
606 const GLsizei maxTexSize
= tex
->MaxSize
;
607 const GLint srcX
= MIN2(srcX0
, srcX1
);
608 const GLint srcY
= MIN2(srcY0
, srcY1
);
609 const GLint srcW
= abs(srcX1
- srcX0
);
610 const GLint srcH
= abs(srcY1
- srcY0
);
611 const GLint dstX
= MIN2(dstX0
, dstX1
);
612 const GLint dstY
= MIN2(dstY0
, dstY1
);
613 const GLint dstW
= abs(dstX1
- dstX0
);
614 const GLint dstH
= abs(dstY1
- dstY0
);
615 const GLint srcFlipX
= (srcX1
- srcX0
) / srcW
;
616 const GLint srcFlipY
= (srcY1
- srcY0
) / srcH
;
617 const GLint dstFlipX
= (dstX1
- dstX0
) / dstW
;
618 const GLint dstFlipY
= (dstY1
- dstY0
) / dstH
;
619 const GLint flipX
= srcFlipX
* dstFlipX
;
620 const GLint flipY
= srcFlipY
* dstFlipY
;
622 struct vertex verts
[4];
624 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
625 ctx
->Extensions
.ARB_fragment_shader
&&
626 (ctx
->API
!= API_OPENGLES
);
628 /* In addition to falling back if the blit size is larger than the maximum
629 * texture size, fallback if the source is multisampled. This fallback can
630 * be removed once Mesa gets support ARB_texture_multisample.
632 if (srcW
> maxTexSize
|| srcH
> maxTexSize
) {
633 /* XXX avoid this fallback */
637 /* Multisample texture blit support requires texture multisample. */
638 if (ctx
->ReadBuffer
->Visual
.samples
> 0 &&
639 !ctx
->Extensions
.ARB_texture_multisample
) {
643 /* only scissor effects blit so save/clear all other relevant state */
644 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
646 /* Try faster, direct texture approach first */
647 if (mask
& GL_COLOR_BUFFER_BIT
) {
648 if (blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
649 dstX0
, dstY0
, dstX1
, dstY1
,
650 filter
, dstFlipX
, dstFlipY
,
651 use_glsl_version
, false)) {
652 mask
&= ~GL_COLOR_BUFFER_BIT
;
660 if (mask
& GL_DEPTH_BUFFER_BIT
&& use_glsl_version
) {
661 if (blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
662 dstX0
, dstY0
, dstX1
, dstY1
,
663 filter
, dstFlipX
, dstFlipY
,
664 use_glsl_version
, true)) {
665 mask
&= ~GL_DEPTH_BUFFER_BIT
;
673 /* Choose between glsl version and fixed function version of
674 * BlitFramebuffer function.
676 if (use_glsl_version
) {
677 setup_glsl_blit_framebuffer(ctx
, blit
, NULL
, tex
->Target
);
680 _mesa_meta_setup_ff_tnl_for_blit(&blit
->VAO
, &blit
->VBO
, 2);
683 /* Silence valgrind warnings about reading uninitialized stack. */
684 memset(verts
, 0, sizeof(verts
));
686 /* Continue with "normal" approach which involves copying the src rect
687 * into a temporary texture and is "blitted" by drawing a textured quad.
690 /* setup vertex positions */
691 verts
[0].x
= -1.0F
* flipX
;
692 verts
[0].y
= -1.0F
* flipY
;
693 verts
[1].x
= 1.0F
* flipX
;
694 verts
[1].y
= -1.0F
* flipY
;
695 verts
[2].x
= 1.0F
* flipX
;
696 verts
[2].y
= 1.0F
* flipY
;
697 verts
[3].x
= -1.0F
* flipX
;
698 verts
[3].y
= 1.0F
* flipY
;
702 if (!use_glsl_version
)
703 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
705 if (mask
& GL_COLOR_BUFFER_BIT
) {
706 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
707 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
708 const GLenum rb_base_format
=
709 _mesa_base_tex_format(ctx
, colorReadRb
->InternalFormat
);
711 /* Using the exact source rectangle to create the texture does incorrect
712 * linear filtering along the edges. So, allocate the texture extended along
713 * edges by one pixel in x, y directions.
715 _mesa_meta_setup_copypix_texture(ctx
, tex
,
716 srcX
- 1, srcY
- 1, srcW
+ 2, srcH
+ 2,
717 rb_base_format
, filter
);
718 /* texcoords (after texture allocation!) */
720 verts
[0].tex
[0] = 1.0F
;
721 verts
[0].tex
[1] = 1.0F
;
722 verts
[1].tex
[0] = tex
->Sright
- 1.0F
;
723 verts
[1].tex
[1] = 1.0F
;
724 verts
[2].tex
[0] = tex
->Sright
- 1.0F
;
725 verts
[2].tex
[1] = tex
->Ttop
- 1.0F
;
726 verts
[3].tex
[0] = 1.0F
;
727 verts
[3].tex
[1] = tex
->Ttop
- 1.0F
;
729 /* upload new vertex data */
730 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
733 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
734 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
735 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
736 _mesa_DepthMask(GL_FALSE
);
737 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
738 mask
&= ~GL_COLOR_BUFFER_BIT
;
741 if ((mask
& GL_DEPTH_BUFFER_BIT
) &&
742 _mesa_is_desktop_gl(ctx
) &&
743 ctx
->Extensions
.ARB_depth_texture
&&
744 ctx
->Extensions
.ARB_fragment_program
) {
746 GLuint
*tmp
= malloc(srcW
* srcH
* sizeof(GLuint
));
750 newTex
= _mesa_meta_alloc_texture(depthTex
, srcW
, srcH
,
752 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
, GL_DEPTH_COMPONENT
,
753 GL_UNSIGNED_INT
, tmp
);
754 _mesa_meta_setup_drawpix_texture(ctx
, depthTex
, newTex
,
755 srcW
, srcH
, GL_DEPTH_COMPONENT
,
756 GL_UNSIGNED_INT
, tmp
);
758 /* texcoords (after texture allocation!) */
760 verts
[0].tex
[0] = 0.0F
;
761 verts
[0].tex
[1] = 0.0F
;
762 verts
[1].tex
[0] = depthTex
->Sright
;
763 verts
[1].tex
[1] = 0.0F
;
764 verts
[2].tex
[0] = depthTex
->Sright
;
765 verts
[2].tex
[1] = depthTex
->Ttop
;
766 verts
[3].tex
[0] = 0.0F
;
767 verts
[3].tex
[1] = depthTex
->Ttop
;
769 /* upload new vertex data */
770 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
774 init_blit_depth_pixels(ctx
);
776 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
777 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
778 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
779 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
780 _mesa_DepthFunc(GL_ALWAYS
);
781 _mesa_DepthMask(GL_TRUE
);
783 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
784 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
785 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
786 mask
&= ~GL_DEPTH_BUFFER_BIT
;
792 if (mask
& GL_STENCIL_BUFFER_BIT
) {
793 /* XXX can't easily do stencil */
796 if (!use_glsl_version
)
797 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
803 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
804 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
809 _mesa_meta_glsl_blit_cleanup(struct blit_state
*blit
)
812 _mesa_DeleteVertexArrays(1, &blit
->VAO
);
814 _mesa_DeleteBuffers(1, &blit
->VBO
);
818 _mesa_DeleteProgramsARB(1, &blit
->DepthFP
);
822 _mesa_meta_blit_shader_table_cleanup(&blit
->shaders
);
824 _mesa_DeleteTextures(1, &blit
->depthTex
.TexObj
);
825 blit
->depthTex
.TexObj
= 0;