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/image.h"
37 #include "main/macros.h"
38 #include "main/matrix.h"
39 #include "main/multisample.h"
40 #include "main/objectlabel.h"
41 #include "main/readpix.h"
42 #include "main/scissor.h"
43 #include "main/shaderapi.h"
44 #include "main/texobj.h"
45 #include "main/texenv.h"
46 #include "main/teximage.h"
47 #include "main/texparam.h"
48 #include "main/varray.h"
49 #include "main/viewport.h"
50 #include "swrast/swrast.h"
51 #include "drivers/common/meta.h"
52 #include "../glsl/ralloc.h"
54 /** Return offset in bytes of the field within a vertex struct */
55 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
58 setup_glsl_msaa_blit_shader(struct gl_context
*ctx
,
59 struct blit_state
*blit
,
60 struct gl_renderbuffer
*src_rb
,
63 const char *vs_source
;
66 enum blit_msaa_shader shader_index
;
67 bool dst_is_msaa
= false;
69 const char *vec4_prefix
;
70 const char *sampler_array_suffix
= "";
72 const char *texcoord_type
= "vec2";
75 src_datatype
= _mesa_get_format_datatype(src_rb
->Format
);
77 /* depth-or-color glCopyTexImage fallback path that passes a NULL rb and
78 * doesn't handle integer.
80 src_datatype
= GL_UNSIGNED_NORMALIZED
;
83 if (ctx
->DrawBuffer
->Visual
.samples
> 1) {
84 /* If you're calling meta_BlitFramebuffer with the destination
85 * multisampled, this is the only path that will work -- swrast and
86 * CopyTexImage won't work on it either.
88 assert(ctx
->Extensions
.ARB_sample_shading
);
92 /* We need shader invocation per sample, not per pixel */
93 _mesa_set_enable(ctx
, GL_MULTISAMPLE
, GL_TRUE
);
94 _mesa_set_enable(ctx
, GL_SAMPLE_SHADING
, GL_TRUE
);
95 _mesa_MinSampleShading(1.0);
99 case GL_TEXTURE_2D_MULTISAMPLE
:
100 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
101 if (src_rb
->_BaseFormat
== GL_DEPTH_COMPONENT
||
102 src_rb
->_BaseFormat
== GL_DEPTH_STENCIL
) {
104 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY
;
106 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE
;
109 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY
;
111 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
;
114 if (target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
) {
115 shader_index
+= (BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_RESOLVE
-
116 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
);
117 sampler_array_suffix
= "Array";
118 texcoord_type
= "vec3";
122 _mesa_problem(ctx
, "Unkown texture target %s\n",
123 _mesa_lookup_enum_by_nr(target
));
124 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
;
127 /* We rely on the enum being sorted this way. */
128 STATIC_ASSERT(BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_INT
==
129 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+ 1);
130 STATIC_ASSERT(BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_UINT
==
131 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+ 2);
132 if (src_datatype
== GL_INT
) {
135 } else if (src_datatype
== GL_UNSIGNED_INT
) {
142 if (blit
->msaa_shaders
[shader_index
]) {
143 _mesa_UseProgram(blit
->msaa_shaders
[shader_index
]);
147 mem_ctx
= ralloc_context(NULL
);
149 if (shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE
||
150 shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_DEPTH_RESOLVE
||
151 shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_DEPTH_COPY
||
152 shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY
) {
154 const char *arb_sample_shading_extension_string
;
157 arb_sample_shading_extension_string
= "#extension GL_ARB_sample_shading : enable";
158 sample_index
= "gl_SampleID";
159 name
= "depth MSAA copy";
161 /* Don't need that extension, since we're drawing to a single-sampled
164 arb_sample_shading_extension_string
= "";
165 /* From the GL 4.3 spec:
167 * "If there is a multisample buffer (the value of SAMPLE_BUFFERS
168 * is one), then values are obtained from the depth samples in
169 * this buffer. It is recommended that the depth value of the
170 * centermost sample be used, though implementations may choose
171 * any function of the depth sample values at each pixel.
173 * We're slacking and instead of choosing centermost, we've got 0.
176 name
= "depth MSAA resolve";
179 vs_source
= ralloc_asprintf(mem_ctx
,
181 "in vec2 position;\n"
182 "in %s textureCoords;\n"
183 "out %s texCoords;\n"
186 " texCoords = textureCoords;\n"
187 " gl_Position = vec4(position, 0.0, 1.0);\n"
191 fs_source
= ralloc_asprintf(mem_ctx
,
193 "#extension GL_ARB_texture_multisample : enable\n"
195 "uniform sampler2DMS%s texSampler;\n"
197 "out vec4 out_color;\n"
201 " gl_FragDepth = texelFetch(texSampler, i%s(texCoords), %s).r;\n"
203 arb_sample_shading_extension_string
,
204 sampler_array_suffix
,
209 /* You can create 2D_MULTISAMPLE textures with 0 sample count (meaning 1
210 * sample). Yes, this is ridiculous.
213 char *sample_resolve
;
214 const char *arb_sample_shading_extension_string
;
215 const char *merge_function
;
216 name
= ralloc_asprintf(mem_ctx
, "%svec4 MSAA %s",
218 dst_is_msaa
? "copy" : "resolve");
220 samples
= MAX2(src_rb
->NumSamples
, 1);
223 arb_sample_shading_extension_string
= "#extension GL_ARB_sample_shading : enable";
224 sample_resolve
= ralloc_asprintf(mem_ctx
, " out_color = texelFetch(texSampler, i%s(texCoords), gl_SampleID);", texcoord_type
);
230 if (src_datatype
== GL_INT
|| src_datatype
== GL_UNSIGNED_INT
) {
232 "gvec4 merge(gvec4 a, gvec4 b) { return (a >> gvec4(1)) + (b >> gvec4(1)) + (a & b & gvec4(1)); }\n";
234 /* The divide will happen at the end for floats. */
236 "vec4 merge(vec4 a, vec4 b) { return (a + b); }\n";
239 arb_sample_shading_extension_string
= "";
241 /* We're assuming power of two samples for this resolution procedure.
243 * To avoid losing any floating point precision if the samples all
244 * happen to have the same value, we merge pairs of values at a time
245 * (so the floating point exponent just gets increased), rather than
246 * doing a naive sum and dividing.
248 assert((samples
& (samples
- 1)) == 0);
249 /* Fetch each individual sample. */
250 sample_resolve
= rzalloc_size(mem_ctx
, 1);
251 for (i
= 0; i
< samples
; i
++) {
252 ralloc_asprintf_append(&sample_resolve
,
253 " gvec4 sample_1_%d = texelFetch(texSampler, i%s(texCoords), %d);\n",
254 i
, texcoord_type
, i
);
256 /* Now, merge each pair of samples, then merge each pair of those,
259 for (step
= 2; step
<= samples
; step
*= 2) {
260 for (i
= 0; i
< samples
; i
+= step
) {
261 ralloc_asprintf_append(&sample_resolve
,
262 " gvec4 sample_%d_%d = merge(sample_%d_%d, sample_%d_%d);\n",
265 step
/ 2, i
+ step
/ 2);
269 /* Scale the final result. */
270 if (src_datatype
== GL_UNSIGNED_INT
|| src_datatype
== GL_INT
) {
271 ralloc_asprintf_append(&sample_resolve
,
272 " out_color = sample_%d_0;\n",
275 ralloc_asprintf_append(&sample_resolve
,
276 " out_color = sample_%d_0 / %f;\n",
277 samples
, (float)samples
);
281 vs_source
= ralloc_asprintf(mem_ctx
,
283 "in vec2 position;\n"
284 "in %s textureCoords;\n"
285 "out %s texCoords;\n"
288 " texCoords = textureCoords;\n"
289 " gl_Position = vec4(position, 0.0, 1.0);\n"
293 fs_source
= ralloc_asprintf(mem_ctx
,
295 "#extension GL_ARB_texture_multisample : enable\n"
297 "#define gvec4 %svec4\n"
298 "uniform %ssampler2DMS%s texSampler;\n"
300 "out gvec4 out_color;\n"
302 "%s" /* merge_function */
305 "%s\n" /* sample_resolve */
307 arb_sample_shading_extension_string
,
310 sampler_array_suffix
,
316 _mesa_meta_compile_and_link_program(ctx
, vs_source
, fs_source
, name
,
317 &blit
->msaa_shaders
[shader_index
]);
319 ralloc_free(mem_ctx
);
323 setup_glsl_blit_framebuffer(struct gl_context
*ctx
,
324 struct blit_state
*blit
,
325 struct gl_renderbuffer
*src_rb
,
328 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
329 assert(_mesa_is_desktop_gl(ctx
) || target
== GL_TEXTURE_2D
);
331 unsigned texcoord_size
= 2 + (src_rb
->Depth
> 1 ? 1 : 0);
333 _mesa_meta_setup_vertex_objects(&blit
->VAO
, &blit
->VBO
, true,
334 2, texcoord_size
, 0);
336 if (target
== GL_TEXTURE_2D_MULTISAMPLE
||
337 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
) {
338 setup_glsl_msaa_blit_shader(ctx
, blit
, src_rb
, target
);
340 _mesa_meta_setup_blit_shader(ctx
, target
, &blit
->shaders
);
345 * Try to do a color or depth glBlitFramebuffer using texturing.
347 * We can do this when the src renderbuffer is actually a texture, or when the
348 * driver exposes BindRenderbufferTexImage().
351 blitframebuffer_texture(struct gl_context
*ctx
,
352 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
353 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
354 GLenum filter
, GLint flipX
, GLint flipY
,
355 GLboolean glsl_version
, GLboolean do_depth
)
357 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
358 int att_index
= do_depth
? BUFFER_DEPTH
: readFb
->_ColorReadBufferIndex
;
359 const struct gl_renderbuffer_attachment
*readAtt
=
360 &readFb
->Attachment
[att_index
];
361 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
362 struct fb_tex_blit_state fb_tex_blit
;
363 const GLint dstX
= MIN2(dstX0
, dstX1
);
364 const GLint dstY
= MIN2(dstY0
, dstY1
);
365 const GLint dstW
= abs(dstX1
- dstX0
);
366 const GLint dstH
= abs(dstY1
- dstY0
);
367 struct gl_texture_object
*texObj
;
370 struct gl_renderbuffer
*rb
= readAtt
->Renderbuffer
;
371 struct temp_texture
*meta_temp_texture
;
373 if (rb
->NumSamples
&& !ctx
->Extensions
.ARB_texture_multisample
)
376 if (filter
== GL_SCALED_RESOLVE_FASTEST_EXT
||
377 filter
== GL_SCALED_RESOLVE_NICEST_EXT
) {
381 _mesa_meta_fb_tex_blit_begin(ctx
, &fb_tex_blit
);
383 if (readAtt
->Texture
&&
384 (readAtt
->Texture
->Target
== GL_TEXTURE_2D
||
385 readAtt
->Texture
->Target
== GL_TEXTURE_RECTANGLE
||
386 readAtt
->Texture
->Target
== GL_TEXTURE_2D_MULTISAMPLE
||
387 readAtt
->Texture
->Target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
)) {
388 /* If there's a texture attached of a type we can handle, then just use
391 srcLevel
= readAtt
->TextureLevel
;
392 texObj
= readAtt
->Texture
;
393 target
= texObj
->Target
;
394 } else if (!readAtt
->Texture
&& ctx
->Driver
.BindRenderbufferTexImage
) {
395 if (!_mesa_meta_bind_rb_as_tex_image(ctx
, rb
, &fb_tex_blit
.tempTex
,
400 if (_mesa_is_winsys_fbo(readFb
)) {
402 srcY0
= rb
->Height
- srcY1
;
403 srcY1
= rb
->Height
- temp
;
407 GLenum tex_base_format
;
408 int srcW
= abs(srcY1
- srcY0
);
409 int srcH
= abs(srcY1
- srcY0
);
410 /* Fall back to doing a CopyTexSubImage to get the destination
411 * renderbuffer into a texture.
413 if (ctx
->Meta
->Blit
.no_ctsi_fallback
)
416 if (rb
->NumSamples
> 1)
420 meta_temp_texture
= _mesa_meta_get_temp_depth_texture(ctx
);
421 tex_base_format
= GL_DEPTH_COMPONENT
;
423 meta_temp_texture
= _mesa_meta_get_temp_texture(ctx
);
425 _mesa_base_tex_format(ctx
, rb
->InternalFormat
);
429 target
= meta_temp_texture
->Target
;
430 texObj
= _mesa_lookup_texture(ctx
, meta_temp_texture
->TexObj
);
432 _mesa_meta_setup_copypix_texture(ctx
, meta_temp_texture
,
445 fb_tex_blit
.baseLevelSave
= texObj
->BaseLevel
;
446 fb_tex_blit
.maxLevelSave
= texObj
->MaxLevel
;
449 setup_glsl_blit_framebuffer(ctx
, blit
, rb
, target
);
452 _mesa_meta_setup_ff_tnl_for_blit(&ctx
->Meta
->Blit
.VAO
,
453 &ctx
->Meta
->Blit
.VBO
,
458 printf("Blit from texture!\n");
459 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
460 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
463 fb_tex_blit
.sampler
= _mesa_meta_setup_sampler(ctx
, texObj
, target
, filter
,
466 /* Always do our blits with no net sRGB decode or encode.
468 * However, if both the src and dst can be srgb decode/encoded, enable them
469 * so that we do any blending (from scaling or from MSAA resolves) in the
472 * Our choice of not doing any net encode/decode is from the GL 3.0
475 * "Blit operations bypass the fragment pipeline. The only fragment
476 * operations which affect a blit are the pixel ownership test and the
479 * The GL 4.4 specification disagrees and says that the sRGB part of the
480 * fragment pipeline applies, but this was found to break applications.
482 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
483 if (_mesa_get_format_color_encoding(rb
->Format
) == GL_SRGB
&&
484 ctx
->DrawBuffer
->Visual
.sRGBCapable
) {
485 _mesa_SamplerParameteri(fb_tex_blit
.sampler
,
486 GL_TEXTURE_SRGB_DECODE_EXT
, GL_DECODE_EXT
);
487 _mesa_set_framebuffer_srgb(ctx
, GL_TRUE
);
489 _mesa_SamplerParameteri(fb_tex_blit
.sampler
,
490 GL_TEXTURE_SRGB_DECODE_EXT
,
492 /* set_framebuffer_srgb was set by _mesa_meta_begin(). */
497 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
498 _mesa_set_enable(ctx
, target
, GL_TRUE
);
501 /* Prepare vertex data (the VBO was previously created and bound) */
503 struct vertex verts
[4];
504 GLfloat s0
, t0
, s1
, t1
;
506 if (target
== GL_TEXTURE_2D
) {
507 const struct gl_texture_image
*texImage
508 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
509 s0
= srcX0
/ (float) texImage
->Width
;
510 s1
= srcX1
/ (float) texImage
->Width
;
511 t0
= srcY0
/ (float) texImage
->Height
;
512 t1
= srcY1
/ (float) texImage
->Height
;
515 assert(target
== GL_TEXTURE_RECTANGLE_ARB
||
516 target
== GL_TEXTURE_2D_MULTISAMPLE
||
517 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
);
524 /* Silence valgrind warnings about reading uninitialized stack. */
525 memset(verts
, 0, sizeof(verts
));
527 /* setup vertex positions */
528 verts
[0].x
= -1.0F
* flipX
;
529 verts
[0].y
= -1.0F
* flipY
;
530 verts
[1].x
= 1.0F
* flipX
;
531 verts
[1].y
= -1.0F
* flipY
;
532 verts
[2].x
= 1.0F
* flipX
;
533 verts
[2].y
= 1.0F
* flipY
;
534 verts
[3].x
= -1.0F
* flipX
;
535 verts
[3].y
= 1.0F
* flipY
;
537 verts
[0].tex
[0] = s0
;
538 verts
[0].tex
[1] = t0
;
539 verts
[0].tex
[2] = readAtt
->Zoffset
;
540 verts
[1].tex
[0] = s1
;
541 verts
[1].tex
[1] = t0
;
542 verts
[1].tex
[2] = readAtt
->Zoffset
;
543 verts
[2].tex
[0] = s1
;
544 verts
[2].tex
[1] = t1
;
545 verts
[2].tex
[2] = readAtt
->Zoffset
;
546 verts
[3].tex
[0] = s0
;
547 verts
[3].tex
[1] = t1
;
548 verts
[3].tex
[2] = readAtt
->Zoffset
;
550 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
554 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
555 _mesa_ColorMask(!do_depth
, !do_depth
, !do_depth
, !do_depth
);
556 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, do_depth
);
557 _mesa_DepthMask(do_depth
);
558 _mesa_DepthFunc(GL_ALWAYS
);
560 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
561 _mesa_meta_fb_tex_blit_end(ctx
, target
, &fb_tex_blit
);
567 _mesa_meta_fb_tex_blit_begin(const struct gl_context
*ctx
,
568 struct fb_tex_blit_state
*blit
)
571 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
572 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
577 _mesa_meta_fb_tex_blit_end(const struct gl_context
*ctx
, GLenum target
,
578 struct fb_tex_blit_state
*blit
)
580 /* Restore texture object state, the texture binding will
581 * be restored by _mesa_meta_end().
583 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
584 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, blit
->baseLevelSave
);
585 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, blit
->maxLevelSave
);
588 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, blit
->samplerSave
);
589 _mesa_DeleteSamplers(1, &blit
->sampler
);
591 _mesa_DeleteTextures(1, &blit
->tempTex
);
595 _mesa_meta_bind_rb_as_tex_image(struct gl_context
*ctx
,
596 struct gl_renderbuffer
*rb
,
598 struct gl_texture_object
**texObj
,
601 struct gl_texture_image
*texImage
;
603 if (rb
->NumSamples
> 1)
604 *target
= GL_TEXTURE_2D_MULTISAMPLE
;
606 *target
= GL_TEXTURE_2D
;
608 _mesa_GenTextures(1, tex
);
609 _mesa_BindTexture(*target
, *tex
);
610 *texObj
= _mesa_lookup_texture(ctx
, *tex
);
611 texImage
= _mesa_get_tex_image(ctx
, *texObj
, *target
, 0);
613 if (!ctx
->Driver
.BindRenderbufferTexImage(ctx
, rb
, texImage
)) {
614 _mesa_DeleteTextures(1, tex
);
618 if (ctx
->Driver
.FinishRenderTexture
&& !rb
->NeedsFinishRenderTexture
) {
619 rb
->NeedsFinishRenderTexture
= true;
620 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
627 _mesa_meta_setup_sampler(struct gl_context
*ctx
,
628 const struct gl_texture_object
*texObj
,
629 GLenum target
, GLenum filter
, GLuint srcLevel
)
633 _mesa_GenSamplers(1, &sampler
);
634 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, sampler
);
636 /* Prepare src texture state */
637 _mesa_BindTexture(target
, texObj
->Name
);
638 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MIN_FILTER
, filter
);
639 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MAG_FILTER
, filter
);
640 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
641 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
642 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
644 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
645 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
651 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
652 * of texture mapping and polygon rendering.
655 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
656 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
657 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
658 GLbitfield mask
, GLenum filter
)
660 const GLint dstW
= abs(dstX1
- dstX0
);
661 const GLint dstH
= abs(dstY1
- dstY0
);
662 const GLint dstFlipX
= (dstX1
- dstX0
) / dstW
;
663 const GLint dstFlipY
= (dstY1
- dstY0
) / dstH
;
666 GLint srcX0
, srcY0
, srcX1
, srcY1
;
667 GLint dstX0
, dstY0
, dstX1
, dstY1
;
669 srcX0
, srcY0
, srcX1
, srcY1
,
670 dstX0
, dstY0
, dstX1
, dstY1
673 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
674 ctx
->Extensions
.ARB_fragment_shader
;
676 /* Multisample texture blit support requires texture multisample. */
677 if (ctx
->ReadBuffer
->Visual
.samples
> 0 &&
678 !ctx
->Extensions
.ARB_texture_multisample
) {
682 /* Clip a copy of the blit coordinates. If these differ from the input
683 * coordinates, then we'll set the scissor.
685 if (!_mesa_clip_blit(ctx
, &clip
.srcX0
, &clip
.srcY0
, &clip
.srcX1
, &clip
.srcY1
,
686 &clip
.dstX0
, &clip
.dstY0
, &clip
.dstX1
, &clip
.dstY1
)) {
687 /* clipped/scissored everything away */
691 /* Only scissor affects blit, but we're doing to set a custom scissor if
692 * necessary anyway, so save/clear state.
694 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_DRAW_BUFFERS
);
696 /* If the clipping earlier changed the destination rect at all, then
697 * enable the scissor to clip to it.
699 if (clip
.dstX0
!= dstX0
|| clip
.dstY0
!= dstY0
||
700 clip
.dstX1
!= dstX1
|| clip
.dstY1
!= dstY1
) {
701 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_TRUE
);
702 _mesa_Scissor(MIN2(clip
.dstX0
, clip
.dstX1
),
703 MIN2(clip
.dstY0
, clip
.dstY1
),
704 abs(clip
.dstX0
- clip
.dstX1
),
705 abs(clip
.dstY0
- clip
.dstY1
));
708 /* Try faster, direct texture approach first */
709 if (mask
& GL_COLOR_BUFFER_BIT
) {
710 if (blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
711 dstX0
, dstY0
, dstX1
, dstY1
,
712 filter
, dstFlipX
, dstFlipY
,
713 use_glsl_version
, false)) {
714 mask
&= ~GL_COLOR_BUFFER_BIT
;
718 if (mask
& GL_DEPTH_BUFFER_BIT
&& use_glsl_version
) {
719 if (blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
720 dstX0
, dstY0
, dstX1
, dstY1
,
721 filter
, dstFlipX
, dstFlipY
,
722 use_glsl_version
, true)) {
723 mask
&= ~GL_DEPTH_BUFFER_BIT
;
727 if (mask
& GL_STENCIL_BUFFER_BIT
) {
728 /* XXX can't easily do stencil */
737 _mesa_meta_glsl_blit_cleanup(struct blit_state
*blit
)
740 _mesa_DeleteVertexArrays(1, &blit
->VAO
);
742 _mesa_DeleteBuffers(1, &blit
->VBO
);
746 _mesa_meta_blit_shader_table_cleanup(&blit
->shaders
);
748 _mesa_DeleteTextures(1, &blit
->depthTex
.TexObj
);
749 blit
->depthTex
.TexObj
= 0;
753 _mesa_meta_and_swrast_BlitFramebuffer(struct gl_context
*ctx
,
754 GLint srcX0
, GLint srcY0
,
755 GLint srcX1
, GLint srcY1
,
756 GLint dstX0
, GLint dstY0
,
757 GLint dstX1
, GLint dstY1
,
758 GLbitfield mask
, GLenum filter
)
760 mask
= _mesa_meta_BlitFramebuffer(ctx
,
761 srcX0
, srcY0
, srcX1
, srcY1
,
762 dstX0
, dstY0
, dstX1
, dstY1
,
767 _swrast_BlitFramebuffer(ctx
,
768 srcX0
, srcY0
, srcX1
, srcY1
,
769 dstX0
, dstY0
, dstX1
, dstY1
,