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 unsigned texcoord_size
;
330 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
331 assert(_mesa_is_desktop_gl(ctx
) || target
== GL_TEXTURE_2D
);
333 texcoord_size
= 2 + (src_rb
->Depth
> 1 ? 1 : 0);
335 _mesa_meta_setup_vertex_objects(&blit
->VAO
, &blit
->VBO
, true,
336 2, texcoord_size
, 0);
338 if (target
== GL_TEXTURE_2D_MULTISAMPLE
||
339 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
) {
340 setup_glsl_msaa_blit_shader(ctx
, blit
, src_rb
, target
);
342 _mesa_meta_setup_blit_shader(ctx
, target
, &blit
->shaders
);
347 * Try to do a color or depth glBlitFramebuffer using texturing.
349 * We can do this when the src renderbuffer is actually a texture, or when the
350 * driver exposes BindRenderbufferTexImage().
353 blitframebuffer_texture(struct gl_context
*ctx
,
354 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
355 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
356 GLenum filter
, GLint flipX
, GLint flipY
,
357 GLboolean glsl_version
, GLboolean do_depth
)
359 const struct gl_framebuffer
*readFb
= ctx
->ReadBuffer
;
360 int att_index
= do_depth
? BUFFER_DEPTH
: readFb
->_ColorReadBufferIndex
;
361 const struct gl_renderbuffer_attachment
*readAtt
=
362 &readFb
->Attachment
[att_index
];
363 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
364 struct fb_tex_blit_state fb_tex_blit
;
365 const GLint dstX
= MIN2(dstX0
, dstX1
);
366 const GLint dstY
= MIN2(dstY0
, dstY1
);
367 const GLint dstW
= abs(dstX1
- dstX0
);
368 const GLint dstH
= abs(dstY1
- dstY0
);
369 struct gl_texture_object
*texObj
;
372 struct gl_renderbuffer
*rb
= readAtt
->Renderbuffer
;
373 struct temp_texture
*meta_temp_texture
;
375 if (rb
->NumSamples
&& !ctx
->Extensions
.ARB_texture_multisample
)
378 if (filter
== GL_SCALED_RESOLVE_FASTEST_EXT
||
379 filter
== GL_SCALED_RESOLVE_NICEST_EXT
) {
383 _mesa_meta_fb_tex_blit_begin(ctx
, &fb_tex_blit
);
385 if (readAtt
->Texture
&&
386 (readAtt
->Texture
->Target
== GL_TEXTURE_2D
||
387 readAtt
->Texture
->Target
== GL_TEXTURE_RECTANGLE
||
388 readAtt
->Texture
->Target
== GL_TEXTURE_2D_MULTISAMPLE
||
389 readAtt
->Texture
->Target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
)) {
390 /* If there's a texture attached of a type we can handle, then just use
393 srcLevel
= readAtt
->TextureLevel
;
394 texObj
= readAtt
->Texture
;
395 target
= texObj
->Target
;
396 } else if (!readAtt
->Texture
&& ctx
->Driver
.BindRenderbufferTexImage
) {
397 if (!_mesa_meta_bind_rb_as_tex_image(ctx
, rb
, &fb_tex_blit
.tempTex
,
402 if (_mesa_is_winsys_fbo(readFb
)) {
404 srcY0
= rb
->Height
- srcY1
;
405 srcY1
= rb
->Height
- temp
;
409 GLenum tex_base_format
;
410 int srcW
= abs(srcY1
- srcY0
);
411 int srcH
= abs(srcY1
- srcY0
);
412 /* Fall back to doing a CopyTexSubImage to get the destination
413 * renderbuffer into a texture.
415 if (ctx
->Meta
->Blit
.no_ctsi_fallback
)
418 if (rb
->NumSamples
> 1)
422 meta_temp_texture
= _mesa_meta_get_temp_depth_texture(ctx
);
423 tex_base_format
= GL_DEPTH_COMPONENT
;
425 meta_temp_texture
= _mesa_meta_get_temp_texture(ctx
);
427 _mesa_base_tex_format(ctx
, rb
->InternalFormat
);
431 target
= meta_temp_texture
->Target
;
432 texObj
= _mesa_lookup_texture(ctx
, meta_temp_texture
->TexObj
);
434 _mesa_meta_setup_copypix_texture(ctx
, meta_temp_texture
,
447 fb_tex_blit
.baseLevelSave
= texObj
->BaseLevel
;
448 fb_tex_blit
.maxLevelSave
= texObj
->MaxLevel
;
451 setup_glsl_blit_framebuffer(ctx
, blit
, rb
, target
);
454 _mesa_meta_setup_ff_tnl_for_blit(&ctx
->Meta
->Blit
.VAO
,
455 &ctx
->Meta
->Blit
.VBO
,
460 printf("Blit from texture!\n");
461 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
462 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
465 fb_tex_blit
.sampler
= _mesa_meta_setup_sampler(ctx
, texObj
, target
, filter
,
468 /* Always do our blits with no net sRGB decode or encode.
470 * However, if both the src and dst can be srgb decode/encoded, enable them
471 * so that we do any blending (from scaling or from MSAA resolves) in the
474 * Our choice of not doing any net encode/decode is from the GL 3.0
477 * "Blit operations bypass the fragment pipeline. The only fragment
478 * operations which affect a blit are the pixel ownership test and the
481 * The GL 4.4 specification disagrees and says that the sRGB part of the
482 * fragment pipeline applies, but this was found to break applications.
484 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
485 if (_mesa_get_format_color_encoding(rb
->Format
) == GL_SRGB
&&
486 ctx
->DrawBuffer
->Visual
.sRGBCapable
) {
487 _mesa_SamplerParameteri(fb_tex_blit
.sampler
,
488 GL_TEXTURE_SRGB_DECODE_EXT
, GL_DECODE_EXT
);
489 _mesa_set_framebuffer_srgb(ctx
, GL_TRUE
);
491 _mesa_SamplerParameteri(fb_tex_blit
.sampler
,
492 GL_TEXTURE_SRGB_DECODE_EXT
,
494 /* set_framebuffer_srgb was set by _mesa_meta_begin(). */
499 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
500 _mesa_set_enable(ctx
, target
, GL_TRUE
);
503 /* Prepare vertex data (the VBO was previously created and bound) */
505 struct vertex verts
[4];
506 GLfloat s0
, t0
, s1
, t1
;
508 if (target
== GL_TEXTURE_2D
) {
509 const struct gl_texture_image
*texImage
510 = _mesa_select_tex_image(ctx
, texObj
, target
, srcLevel
);
511 s0
= srcX0
/ (float) texImage
->Width
;
512 s1
= srcX1
/ (float) texImage
->Width
;
513 t0
= srcY0
/ (float) texImage
->Height
;
514 t1
= srcY1
/ (float) texImage
->Height
;
517 assert(target
== GL_TEXTURE_RECTANGLE_ARB
||
518 target
== GL_TEXTURE_2D_MULTISAMPLE
||
519 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
);
526 /* Silence valgrind warnings about reading uninitialized stack. */
527 memset(verts
, 0, sizeof(verts
));
529 /* setup vertex positions */
530 verts
[0].x
= -1.0F
* flipX
;
531 verts
[0].y
= -1.0F
* flipY
;
532 verts
[1].x
= 1.0F
* flipX
;
533 verts
[1].y
= -1.0F
* flipY
;
534 verts
[2].x
= 1.0F
* flipX
;
535 verts
[2].y
= 1.0F
* flipY
;
536 verts
[3].x
= -1.0F
* flipX
;
537 verts
[3].y
= 1.0F
* flipY
;
539 verts
[0].tex
[0] = s0
;
540 verts
[0].tex
[1] = t0
;
541 verts
[0].tex
[2] = readAtt
->Zoffset
;
542 verts
[1].tex
[0] = s1
;
543 verts
[1].tex
[1] = t0
;
544 verts
[1].tex
[2] = readAtt
->Zoffset
;
545 verts
[2].tex
[0] = s1
;
546 verts
[2].tex
[1] = t1
;
547 verts
[2].tex
[2] = readAtt
->Zoffset
;
548 verts
[3].tex
[0] = s0
;
549 verts
[3].tex
[1] = t1
;
550 verts
[3].tex
[2] = readAtt
->Zoffset
;
552 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
556 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
557 _mesa_ColorMask(!do_depth
, !do_depth
, !do_depth
, !do_depth
);
558 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, do_depth
);
559 _mesa_DepthMask(do_depth
);
560 _mesa_DepthFunc(GL_ALWAYS
);
562 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
563 _mesa_meta_fb_tex_blit_end(ctx
, target
, &fb_tex_blit
);
569 _mesa_meta_fb_tex_blit_begin(const struct gl_context
*ctx
,
570 struct fb_tex_blit_state
*blit
)
573 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
574 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
579 _mesa_meta_fb_tex_blit_end(const struct gl_context
*ctx
, GLenum target
,
580 struct fb_tex_blit_state
*blit
)
582 /* Restore texture object state, the texture binding will
583 * be restored by _mesa_meta_end().
585 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
586 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, blit
->baseLevelSave
);
587 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, blit
->maxLevelSave
);
590 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, blit
->samplerSave
);
591 _mesa_DeleteSamplers(1, &blit
->sampler
);
593 _mesa_DeleteTextures(1, &blit
->tempTex
);
597 _mesa_meta_bind_rb_as_tex_image(struct gl_context
*ctx
,
598 struct gl_renderbuffer
*rb
,
600 struct gl_texture_object
**texObj
,
603 struct gl_texture_image
*texImage
;
605 if (rb
->NumSamples
> 1)
606 *target
= GL_TEXTURE_2D_MULTISAMPLE
;
608 *target
= GL_TEXTURE_2D
;
610 _mesa_GenTextures(1, tex
);
611 _mesa_BindTexture(*target
, *tex
);
612 *texObj
= _mesa_lookup_texture(ctx
, *tex
);
613 texImage
= _mesa_get_tex_image(ctx
, *texObj
, *target
, 0);
615 if (!ctx
->Driver
.BindRenderbufferTexImage(ctx
, rb
, texImage
)) {
616 _mesa_DeleteTextures(1, tex
);
620 if (ctx
->Driver
.FinishRenderTexture
&& !rb
->NeedsFinishRenderTexture
) {
621 rb
->NeedsFinishRenderTexture
= true;
622 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
629 _mesa_meta_setup_sampler(struct gl_context
*ctx
,
630 const struct gl_texture_object
*texObj
,
631 GLenum target
, GLenum filter
, GLuint srcLevel
)
635 _mesa_GenSamplers(1, &sampler
);
636 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, sampler
);
638 /* Prepare src texture state */
639 _mesa_BindTexture(target
, texObj
->Name
);
640 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MIN_FILTER
, filter
);
641 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MAG_FILTER
, filter
);
642 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
643 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
644 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
646 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
647 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
653 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
654 * of texture mapping and polygon rendering.
657 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
658 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
659 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
660 GLbitfield mask
, GLenum filter
)
662 const GLint dstW
= abs(dstX1
- dstX0
);
663 const GLint dstH
= abs(dstY1
- dstY0
);
664 const GLint dstFlipX
= (dstX1
- dstX0
) / dstW
;
665 const GLint dstFlipY
= (dstY1
- dstY0
) / dstH
;
668 GLint srcX0
, srcY0
, srcX1
, srcY1
;
669 GLint dstX0
, dstY0
, dstX1
, dstY1
;
671 srcX0
, srcY0
, srcX1
, srcY1
,
672 dstX0
, dstY0
, dstX1
, dstY1
675 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
676 ctx
->Extensions
.ARB_fragment_shader
;
678 /* Multisample texture blit support requires texture multisample. */
679 if (ctx
->ReadBuffer
->Visual
.samples
> 0 &&
680 !ctx
->Extensions
.ARB_texture_multisample
) {
684 /* Clip a copy of the blit coordinates. If these differ from the input
685 * coordinates, then we'll set the scissor.
687 if (!_mesa_clip_blit(ctx
, &clip
.srcX0
, &clip
.srcY0
, &clip
.srcX1
, &clip
.srcY1
,
688 &clip
.dstX0
, &clip
.dstY0
, &clip
.dstX1
, &clip
.dstY1
)) {
689 /* clipped/scissored everything away */
693 /* Only scissor affects blit, but we're doing to set a custom scissor if
694 * necessary anyway, so save/clear state.
696 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_DRAW_BUFFERS
);
698 /* If the clipping earlier changed the destination rect at all, then
699 * enable the scissor to clip to it.
701 if (clip
.dstX0
!= dstX0
|| clip
.dstY0
!= dstY0
||
702 clip
.dstX1
!= dstX1
|| clip
.dstY1
!= dstY1
) {
703 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_TRUE
);
704 _mesa_Scissor(MIN2(clip
.dstX0
, clip
.dstX1
),
705 MIN2(clip
.dstY0
, clip
.dstY1
),
706 abs(clip
.dstX0
- clip
.dstX1
),
707 abs(clip
.dstY0
- clip
.dstY1
));
710 /* Try faster, direct texture approach first */
711 if (mask
& GL_COLOR_BUFFER_BIT
) {
712 if (blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
713 dstX0
, dstY0
, dstX1
, dstY1
,
714 filter
, dstFlipX
, dstFlipY
,
715 use_glsl_version
, false)) {
716 mask
&= ~GL_COLOR_BUFFER_BIT
;
720 if (mask
& GL_DEPTH_BUFFER_BIT
&& use_glsl_version
) {
721 if (blitframebuffer_texture(ctx
, srcX0
, srcY0
, srcX1
, srcY1
,
722 dstX0
, dstY0
, dstX1
, dstY1
,
723 filter
, dstFlipX
, dstFlipY
,
724 use_glsl_version
, true)) {
725 mask
&= ~GL_DEPTH_BUFFER_BIT
;
729 if (mask
& GL_STENCIL_BUFFER_BIT
) {
730 /* XXX can't easily do stencil */
739 _mesa_meta_glsl_blit_cleanup(struct blit_state
*blit
)
742 _mesa_DeleteVertexArrays(1, &blit
->VAO
);
744 _mesa_DeleteBuffers(1, &blit
->VBO
);
748 _mesa_meta_blit_shader_table_cleanup(&blit
->shaders
);
750 _mesa_DeleteTextures(1, &blit
->depthTex
.TexObj
);
751 blit
->depthTex
.TexObj
= 0;
755 _mesa_meta_and_swrast_BlitFramebuffer(struct gl_context
*ctx
,
756 GLint srcX0
, GLint srcY0
,
757 GLint srcX1
, GLint srcY1
,
758 GLint dstX0
, GLint dstY0
,
759 GLint dstX1
, GLint dstY1
,
760 GLbitfield mask
, GLenum filter
)
762 mask
= _mesa_meta_BlitFramebuffer(ctx
,
763 srcX0
, srcY0
, srcX1
, srcY1
,
764 dstX0
, dstY0
, dstX1
, dstY1
,
769 _swrast_BlitFramebuffer(ctx
,
770 srcX0
, srcY0
, srcX1
, srcY1
,
771 dstX0
, dstY0
, dstX1
, dstY1
,