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/readpix.h"
40 #include "main/shaderapi.h"
41 #include "main/texobj.h"
42 #include "main/texenv.h"
43 #include "main/teximage.h"
44 #include "main/texparam.h"
45 #include "main/varray.h"
46 #include "main/viewport.h"
47 #include "swrast/swrast.h"
48 #include "drivers/common/meta.h"
49 #include "../glsl/ralloc.h"
51 /** Return offset in bytes of the field within a vertex struct */
52 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
55 * One-time init for drawing depth pixels.
58 init_blit_depth_pixels(struct gl_context
*ctx
)
60 static const char *program
=
62 "TEX result.depth, fragment.texcoord[0], texture[0], %s; \n"
65 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
66 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
67 const char *texTarget
;
69 assert(blit
->DepthFP
== 0);
71 /* replace %s with "RECT" or "2D" */
72 assert(strlen(program
) + 4 < sizeof(program2
));
73 if (tex
->Target
== GL_TEXTURE_RECTANGLE
)
77 _mesa_snprintf(program2
, sizeof(program2
), program
, texTarget
);
79 _mesa_GenProgramsARB(1, &blit
->DepthFP
);
80 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
81 _mesa_ProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
82 strlen(program2
), (const GLubyte
*) program2
);
86 setup_glsl_msaa_blit_shader(struct gl_context
*ctx
,
87 struct blit_state
*blit
,
88 struct gl_renderbuffer
*src_rb
,
91 const char *vs_source
;
95 enum blit_msaa_shader shader_index
;
96 const char *samplers
[] = {
97 [BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
] = "sampler2DMS",
98 [BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_INT
] = "isampler2DMS",
99 [BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_UINT
] = "usampler2DMS",
100 [BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY
] = "sampler2DMS",
101 [BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY_INT
] = "isampler2DMS",
102 [BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY_UINT
] = "usampler2DMS",
104 bool dst_is_msaa
= false;
106 const char *vec4_prefix
;
109 src_datatype
= _mesa_get_format_datatype(src_rb
->Format
);
111 /* depth-or-color glCopyTexImage fallback path that passes a NULL rb and
112 * doesn't handle integer.
114 src_datatype
= GL_UNSIGNED_NORMALIZED
;
117 if (ctx
->DrawBuffer
->Visual
.samples
> 1) {
118 /* If you're calling meta_BlitFramebuffer with the destination
119 * multisampled, this is the only path that will work -- swrast and
120 * CopyTexImage won't work on it either.
122 assert(ctx
->Extensions
.ARB_sample_shading
);
126 /* We need shader invocation per sample, not per pixel */
127 _mesa_set_enable(ctx
, GL_MULTISAMPLE
, GL_TRUE
);
128 _mesa_set_enable(ctx
, GL_SAMPLE_SHADING
, GL_TRUE
);
129 _mesa_MinSampleShading(1.0);
133 case GL_TEXTURE_2D_MULTISAMPLE
:
134 if (src_rb
->_BaseFormat
== GL_DEPTH_COMPONENT
||
135 src_rb
->_BaseFormat
== GL_DEPTH_STENCIL
) {
137 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY
;
139 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE
;
142 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY
;
144 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
;
148 _mesa_problem(ctx
, "Unkown texture target %s\n",
149 _mesa_lookup_enum_by_nr(target
));
150 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
;
153 /* We rely on the enum being sorted this way. */
154 STATIC_ASSERT(BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_INT
==
155 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+ 1);
156 STATIC_ASSERT(BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_UINT
==
157 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+ 2);
158 if (src_datatype
== GL_INT
) {
161 } else if (src_datatype
== GL_UNSIGNED_INT
) {
168 if (blit
->msaa_shaders
[shader_index
]) {
169 _mesa_UseProgram(blit
->msaa_shaders
[shader_index
]);
173 mem_ctx
= ralloc_context(NULL
);
175 if (shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE
||
176 shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY
) {
178 const char *arb_sample_shading_extension_string
;
181 arb_sample_shading_extension_string
= "#extension GL_ARB_sample_shading : enable";
182 sample_index
= "gl_SampleID";
184 /* Don't need that extension, since we're drawing to a single-sampled
187 arb_sample_shading_extension_string
= "";
188 /* From the GL 4.3 spec:
190 * "If there is a multisample buffer (the value of SAMPLE_BUFFERS
191 * is one), then values are obtained from the depth samples in
192 * this buffer. It is recommended that the depth value of the
193 * centermost sample be used, though implementations may choose
194 * any function of the depth sample values at each pixel.
196 * We're slacking and instead of choosing centermost, we've got 0.
201 vs_source
= ralloc_asprintf(mem_ctx
,
203 "in vec2 position;\n"
204 "in vec2 textureCoords;\n"
205 "out vec2 texCoords;\n"
208 " texCoords = textureCoords;\n"
209 " gl_Position = vec4(position, 0.0, 1.0);\n"
211 fs_source
= ralloc_asprintf(mem_ctx
,
213 "#extension GL_ARB_texture_multisample : enable\n"
215 "uniform sampler2DMS texSampler;\n"
216 "in vec2 texCoords;\n"
217 "out vec4 out_color;\n"
221 " gl_FragDepth = texelFetch(texSampler, ivec2(texCoords), %s).r;\n"
223 arb_sample_shading_extension_string
,
226 /* You can create 2D_MULTISAMPLE textures with 0 sample count (meaning 1
227 * sample). Yes, this is ridiculous.
229 int samples
= MAX2(src_rb
->NumSamples
, 1);
230 char *sample_resolve
;
231 const char *arb_sample_shading_extension_string
;
232 const char *merge_function
;
235 arb_sample_shading_extension_string
= "#extension GL_ARB_sample_shading : enable";
236 sample_resolve
= ralloc_asprintf(mem_ctx
, " out_color = texelFetch(texSampler, ivec2(texCoords), gl_SampleID);");
239 if (src_datatype
== GL_INT
) {
241 "ivec4 merge(ivec4 a, ivec4 b) { return (a >> ivec4(1)) + (b >> ivec4(1)) + (a & b & ivec4(1)); }\n";
242 } else if (src_datatype
== GL_UNSIGNED_INT
) {
244 "uvec4 merge(uvec4 a, uvec4 b) { return (a >> uvec4(1)) + (b >> uvec4(1)) + (a & b & uvec4(1)); }\n";
246 /* The divide will happen at the end for floats. */
248 "vec4 merge(vec4 a, vec4 b) { return (a + b); }\n";
251 arb_sample_shading_extension_string
= "";
253 /* We're assuming power of two samples for this resolution procedure.
255 * To avoid losing any floating point precision if the samples all
256 * happen to have the same value, we merge pairs of values at a time
257 * (so the floating point exponent just gets increased), rather than
258 * doing a naive sum and dividing.
260 assert((samples
& (samples
- 1)) == 0);
261 /* Fetch each individual sample. */
262 sample_resolve
= rzalloc_size(mem_ctx
, 1);
263 for (int i
= 0; i
< samples
; i
++) {
264 ralloc_asprintf_append(&sample_resolve
,
265 " %svec4 sample_1_%d = texelFetch(texSampler, ivec2(texCoords), %d);\n",
268 /* Now, merge each pair of samples, then merge each pair of those,
271 for (int step
= 2; step
<= samples
; step
*= 2) {
272 for (int i
= 0; i
< samples
; i
+= step
) {
273 ralloc_asprintf_append(&sample_resolve
,
274 " %svec4 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 "uniform %s texSampler;\n"
309 "in vec2 texCoords;\n"
310 "out %svec4 out_color;\n"
312 "%s" /* merge_function */
315 "%s\n" /* sample_resolve */
317 arb_sample_shading_extension_string
,
318 samplers
[shader_index
],
324 vs
= _mesa_meta_compile_shader_with_debug(ctx
, GL_VERTEX_SHADER
, vs_source
);
325 fs
= _mesa_meta_compile_shader_with_debug(ctx
, GL_FRAGMENT_SHADER
, fs_source
);
327 blit
->msaa_shaders
[shader_index
] = _mesa_CreateProgramObjectARB();
328 _mesa_AttachShader(blit
->msaa_shaders
[shader_index
], fs
);
329 _mesa_DeleteObjectARB(fs
);
330 _mesa_AttachShader(blit
->msaa_shaders
[shader_index
], vs
);
331 _mesa_DeleteObjectARB(vs
);
332 _mesa_BindAttribLocation(blit
->msaa_shaders
[shader_index
], 0, "position");
333 _mesa_BindAttribLocation(blit
->msaa_shaders
[shader_index
], 1, "texcoords");
334 _mesa_meta_link_program_with_debug(ctx
, blit
->msaa_shaders
[shader_index
]);
335 ralloc_free(mem_ctx
);
337 _mesa_UseProgram(blit
->msaa_shaders
[shader_index
]);
341 setup_glsl_blit_framebuffer(struct gl_context
*ctx
,
342 struct blit_state
*blit
,
343 struct gl_renderbuffer
*src_rb
,
346 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
347 assert(_mesa_is_desktop_gl(ctx
) || target
== GL_TEXTURE_2D
);
349 _mesa_meta_setup_vertex_objects(&blit
->VAO
, &blit
->VBO
, true, 2, 2, 0);
351 if (target
== GL_TEXTURE_2D_MULTISAMPLE
) {
352 setup_glsl_msaa_blit_shader(ctx
, blit
, src_rb
, target
);
354 _mesa_meta_setup_blit_shader(ctx
, target
, &blit
->shaders
);
359 * Try to do a color or depth glBlitFramebuffer using texturing.
361 * We can do this when the src renderbuffer is actually a texture, or when the
362 * driver exposes BindRenderbufferTexImage().
365 blitframebuffer_texture(struct gl_context
*ctx
,
366 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
367 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
368 GLenum filter
, GLint flipX
, GLint flipY
,
369 GLboolean glsl_version
, GLboolean do_depth
)
371 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
372 int att_index
= do_depth
? BUFFER_DEPTH
: readFb
->_ColorReadBufferIndex
;
373 const struct gl_renderbuffer_attachment
*readAtt
=
374 &readFb
->Attachment
[att_index
];
375 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
376 const GLint dstX
= MIN2(dstX0
, dstX1
);
377 const GLint dstY
= MIN2(dstY0
, dstY1
);
378 const GLint dstW
= abs(dstX1
- dstX0
);
379 const GLint dstH
= abs(dstY1
- dstY0
);
380 struct gl_texture_object
*texObj
;
385 GLuint sampler
, samplerSave
=
386 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
387 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
389 struct gl_renderbuffer
*rb
= readAtt
->Renderbuffer
;
391 if (rb
->NumSamples
&& !ctx
->Extensions
.ARB_texture_multisample
)
394 if (filter
== GL_SCALED_RESOLVE_FASTEST_EXT
||
395 filter
== GL_SCALED_RESOLVE_NICEST_EXT
) {
399 if (readAtt
->Texture
) {
400 /* If there's a texture attached of a type we can handle, then just use
403 srcLevel
= readAtt
->TextureLevel
;
404 texObj
= readAtt
->Texture
;
405 target
= texObj
->Target
;
409 case GL_TEXTURE_RECTANGLE
:
410 case GL_TEXTURE_2D_MULTISAMPLE
:
415 } else if (ctx
->Driver
.BindRenderbufferTexImage
) {
416 /* Otherwise, we need the driver to be able to bind a renderbuffer as
419 struct gl_texture_image
*texImage
;
421 if (rb
->NumSamples
> 1)
422 target
= GL_TEXTURE_2D_MULTISAMPLE
;
424 target
= GL_TEXTURE_2D
;
426 _mesa_GenTextures(1, &tempTex
);
427 _mesa_BindTexture(target
, tempTex
);
429 texObj
= _mesa_lookup_texture(ctx
, tempTex
);
430 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, srcLevel
);
432 if (!ctx
->Driver
.BindRenderbufferTexImage(ctx
, rb
, texImage
)) {
433 _mesa_DeleteTextures(1, &tempTex
);
436 if (ctx
->Driver
.FinishRenderTexture
&&
437 !rb
->NeedsFinishRenderTexture
) {
438 rb
->NeedsFinishRenderTexture
= true;
439 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
442 if (_mesa_is_winsys_fbo(readFb
)) {
444 srcY0
= rb
->Height
- srcY1
;
445 srcY1
= rb
->Height
- temp
;
453 baseLevelSave
= texObj
->BaseLevel
;
454 maxLevelSave
= texObj
->MaxLevel
;
457 setup_glsl_blit_framebuffer(ctx
, blit
, rb
, target
);
460 _mesa_meta_setup_ff_tnl_for_blit(&ctx
->Meta
->Blit
.VAO
,
461 &ctx
->Meta
->Blit
.VBO
,
465 _mesa_GenSamplers(1, &sampler
);
466 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, sampler
);
469 printf("Blit from texture!\n");
470 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
471 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
474 /* Prepare src texture state */
475 _mesa_BindTexture(target
, texObj
->Name
);
476 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MIN_FILTER
, filter
);
477 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MAG_FILTER
, filter
);
478 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
479 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
480 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
482 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
483 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
485 /* Always do our blits with no net sRGB decode or encode.
487 * However, if both the src and dst can be srgb decode/encoded, enable them
488 * so that we do any blending (from scaling or from MSAA resolves) in the
491 * Our choice of not doing any net encode/decode is from the GL 3.0
494 * "Blit operations bypass the fragment pipeline. The only fragment
495 * operations which affect a blit are the pixel ownership test and the
498 * The GL 4.4 specification disagrees and says that the sRGB part of the
499 * fragment pipeline applies, but this was found to break applications.
501 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
502 if (_mesa_get_format_color_encoding(rb
->Format
) == GL_SRGB
&&
503 ctx
->DrawBuffer
->Visual
.sRGBCapable
) {
504 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
506 _mesa_set_framebuffer_srgb(ctx
, GL_TRUE
);
508 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_SRGB_DECODE_EXT
,
510 /* set_framebuffer_srgb was set by _mesa_meta_begin(). */
515 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
516 _mesa_set_enable(ctx
, target
, GL_TRUE
);
519 /* Prepare vertex data (the VBO was previously created and bound) */
521 struct vertex verts
[4];
522 GLfloat s0
, t0
, s1
, t1
;
524 if (target
== GL_TEXTURE_2D
) {
525 const struct gl_texture_image
*texImage
526 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
527 s0
= srcX0
/ (float) texImage
->Width
;
528 s1
= srcX1
/ (float) texImage
->Width
;
529 t0
= srcY0
/ (float) texImage
->Height
;
530 t1
= srcY1
/ (float) texImage
->Height
;
533 assert(target
== GL_TEXTURE_RECTANGLE_ARB
||
534 target
== GL_TEXTURE_2D_MULTISAMPLE
);
541 /* Silence valgrind warnings about reading uninitialized stack. */
542 memset(verts
, 0, sizeof(verts
));
544 /* setup vertex positions */
545 verts
[0].x
= -1.0F
* flipX
;
546 verts
[0].y
= -1.0F
* flipY
;
547 verts
[1].x
= 1.0F
* flipX
;
548 verts
[1].y
= -1.0F
* flipY
;
549 verts
[2].x
= 1.0F
* flipX
;
550 verts
[2].y
= 1.0F
* flipY
;
551 verts
[3].x
= -1.0F
* flipX
;
552 verts
[3].y
= 1.0F
* flipY
;
554 verts
[0].tex
[0] = s0
;
555 verts
[0].tex
[1] = t0
;
556 verts
[1].tex
[0] = s1
;
557 verts
[1].tex
[1] = t0
;
558 verts
[2].tex
[0] = s1
;
559 verts
[2].tex
[1] = t1
;
560 verts
[3].tex
[0] = s0
;
561 verts
[3].tex
[1] = t1
;
563 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
567 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
568 _mesa_ColorMask(!do_depth
, !do_depth
, !do_depth
, !do_depth
);
569 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, do_depth
);
570 _mesa_DepthMask(do_depth
);
571 _mesa_DepthFunc(GL_ALWAYS
);
573 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
575 /* Restore texture object state, the texture binding will
576 * be restored by _mesa_meta_end().
578 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
579 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, baseLevelSave
);
580 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, maxLevelSave
);
583 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, samplerSave
);
584 _mesa_DeleteSamplers(1, &sampler
);
586 _mesa_DeleteTextures(1, &tempTex
);
592 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
593 * of texture mapping and polygon rendering.
596 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
597 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
598 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
599 GLbitfield mask
, GLenum filter
)
601 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
602 struct temp_texture
*tex
= _mesa_meta_get_temp_texture(ctx
);
603 struct temp_texture
*depthTex
= _mesa_meta_get_temp_depth_texture(ctx
);
604 const GLsizei maxTexSize
= tex
->MaxSize
;
605 const GLint srcX
= MIN2(srcX0
, srcX1
);
606 const GLint srcY
= MIN2(srcY0
, srcY1
);
607 const GLint srcW
= abs(srcX1
- srcX0
);
608 const GLint srcH
= abs(srcY1
- srcY0
);
609 const GLint dstX
= MIN2(dstX0
, dstX1
);
610 const GLint dstY
= MIN2(dstY0
, dstY1
);
611 const GLint dstW
= abs(dstX1
- dstX0
);
612 const GLint dstH
= abs(dstY1
- dstY0
);
613 const GLint srcFlipX
= (srcX1
- srcX0
) / srcW
;
614 const GLint srcFlipY
= (srcY1
- srcY0
) / srcH
;
615 const GLint dstFlipX
= (dstX1
- dstX0
) / dstW
;
616 const GLint dstFlipY
= (dstY1
- dstY0
) / dstH
;
617 const GLint flipX
= srcFlipX
* dstFlipX
;
618 const GLint flipY
= srcFlipY
* dstFlipY
;
620 struct vertex verts
[4];
622 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
623 ctx
->Extensions
.ARB_fragment_shader
&&
624 (ctx
->API
!= API_OPENGLES
);
626 /* In addition to falling back if the blit size is larger than the maximum
627 * texture size, fallback if the source is multisampled. This fallback can
628 * be removed once Mesa gets support ARB_texture_multisample.
630 if (srcW
> maxTexSize
|| srcH
> maxTexSize
) {
631 /* XXX avoid this fallback */
635 /* Multisample texture blit support requires texture multisample. */
636 if (ctx
->ReadBuffer
->Visual
.samples
> 0 &&
637 !ctx
->Extensions
.ARB_texture_multisample
) {
641 /* only scissor effects blit so save/clear all other relevant state */
642 _mesa_meta_begin(ctx
, ~MESA_META_SCISSOR
);
644 /* Try faster, direct texture approach first */
645 if (mask
& GL_COLOR_BUFFER_BIT
) {
646 if (blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
647 dstX0
, dstY0
, dstX1
, dstY1
,
648 filter
, dstFlipX
, dstFlipY
,
649 use_glsl_version
, false)) {
650 mask
&= ~GL_COLOR_BUFFER_BIT
;
658 if (mask
& GL_DEPTH_BUFFER_BIT
&& use_glsl_version
) {
659 if (blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
660 dstX0
, dstY0
, dstX1
, dstY1
,
661 filter
, dstFlipX
, dstFlipY
,
662 use_glsl_version
, true)) {
663 mask
&= ~GL_DEPTH_BUFFER_BIT
;
671 /* Choose between glsl version and fixed function version of
672 * BlitFramebuffer function.
674 if (use_glsl_version
) {
675 setup_glsl_blit_framebuffer(ctx
, blit
, NULL
, tex
->Target
);
678 _mesa_meta_setup_ff_tnl_for_blit(&blit
->VAO
, &blit
->VBO
, 2);
681 /* Silence valgrind warnings about reading uninitialized stack. */
682 memset(verts
, 0, sizeof(verts
));
684 /* Continue with "normal" approach which involves copying the src rect
685 * into a temporary texture and is "blitted" by drawing a textured quad.
688 /* setup vertex positions */
689 verts
[0].x
= -1.0F
* flipX
;
690 verts
[0].y
= -1.0F
* flipY
;
691 verts
[1].x
= 1.0F
* flipX
;
692 verts
[1].y
= -1.0F
* flipY
;
693 verts
[2].x
= 1.0F
* flipX
;
694 verts
[2].y
= 1.0F
* flipY
;
695 verts
[3].x
= -1.0F
* flipX
;
696 verts
[3].y
= 1.0F
* flipY
;
700 if (!use_glsl_version
)
701 _mesa_set_enable(ctx
, tex
->Target
, GL_TRUE
);
703 if (mask
& GL_COLOR_BUFFER_BIT
) {
704 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
705 const struct gl_renderbuffer
*colorReadRb
= readFb
->_ColorReadBuffer
;
706 const GLenum rb_base_format
=
707 _mesa_base_tex_format(ctx
, colorReadRb
->InternalFormat
);
709 /* Using the exact source rectangle to create the texture does incorrect
710 * linear filtering along the edges. So, allocate the texture extended along
711 * edges by one pixel in x, y directions.
713 _mesa_meta_setup_copypix_texture(ctx
, tex
,
714 srcX
- 1, srcY
- 1, srcW
+ 2, srcH
+ 2,
715 rb_base_format
, filter
);
716 /* texcoords (after texture allocation!) */
718 verts
[0].tex
[0] = 1.0F
;
719 verts
[0].tex
[1] = 1.0F
;
720 verts
[1].tex
[0] = tex
->Sright
- 1.0F
;
721 verts
[1].tex
[1] = 1.0F
;
722 verts
[2].tex
[0] = tex
->Sright
- 1.0F
;
723 verts
[2].tex
[1] = tex
->Ttop
- 1.0F
;
724 verts
[3].tex
[0] = 1.0F
;
725 verts
[3].tex
[1] = tex
->Ttop
- 1.0F
;
727 /* upload new vertex data */
728 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
731 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
732 _mesa_ColorMask(GL_TRUE
, GL_TRUE
, GL_TRUE
, GL_TRUE
);
733 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_FALSE
);
734 _mesa_DepthMask(GL_FALSE
);
735 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
736 mask
&= ~GL_COLOR_BUFFER_BIT
;
739 if ((mask
& GL_DEPTH_BUFFER_BIT
) &&
740 _mesa_is_desktop_gl(ctx
) &&
741 ctx
->Extensions
.ARB_depth_texture
&&
742 ctx
->Extensions
.ARB_fragment_program
) {
744 GLuint
*tmp
= malloc(srcW
* srcH
* sizeof(GLuint
));
748 newTex
= _mesa_meta_alloc_texture(depthTex
, srcW
, srcH
,
750 _mesa_ReadPixels(srcX
, srcY
, srcW
, srcH
, GL_DEPTH_COMPONENT
,
751 GL_UNSIGNED_INT
, tmp
);
752 _mesa_meta_setup_drawpix_texture(ctx
, depthTex
, newTex
,
753 srcW
, srcH
, GL_DEPTH_COMPONENT
,
754 GL_UNSIGNED_INT
, tmp
);
756 /* texcoords (after texture allocation!) */
758 verts
[0].tex
[0] = 0.0F
;
759 verts
[0].tex
[1] = 0.0F
;
760 verts
[1].tex
[0] = depthTex
->Sright
;
761 verts
[1].tex
[1] = 0.0F
;
762 verts
[2].tex
[0] = depthTex
->Sright
;
763 verts
[2].tex
[1] = depthTex
->Ttop
;
764 verts
[3].tex
[0] = 0.0F
;
765 verts
[3].tex
[1] = depthTex
->Ttop
;
767 /* upload new vertex data */
768 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
772 init_blit_depth_pixels(ctx
);
774 _mesa_BindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, blit
->DepthFP
);
775 _mesa_set_enable(ctx
, GL_FRAGMENT_PROGRAM_ARB
, GL_TRUE
);
776 _mesa_ColorMask(GL_FALSE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
777 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, GL_TRUE
);
778 _mesa_DepthFunc(GL_ALWAYS
);
779 _mesa_DepthMask(GL_TRUE
);
781 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
782 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
783 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
784 mask
&= ~GL_DEPTH_BUFFER_BIT
;
790 if (mask
& GL_STENCIL_BUFFER_BIT
) {
791 /* XXX can't easily do stencil */
794 if (!use_glsl_version
)
795 _mesa_set_enable(ctx
, tex
->Target
, GL_FALSE
);
801 _swrast_BlitFramebuffer(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
802 dstX0
, dstY0
, dstX1
, dstY1
, mask
, filter
);
807 _mesa_meta_glsl_blit_cleanup(struct blit_state
*blit
)
810 _mesa_DeleteVertexArrays(1, &blit
->VAO
);
812 _mesa_DeleteBuffers(1, &blit
->VBO
);
816 _mesa_DeleteProgramsARB(1, &blit
->DepthFP
);
820 _mesa_meta_blit_shader_table_cleanup(&blit
->shaders
);
822 _mesa_DeleteTextures(1, &blit
->depthTex
.TexObj
);
823 blit
->depthTex
.TexObj
= 0;