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/uniforms.h"
49 #include "main/varray.h"
50 #include "main/viewport.h"
51 #include "swrast/swrast.h"
52 #include "drivers/common/meta.h"
53 #include "util/ralloc.h"
55 /** Return offset in bytes of the field within a vertex struct */
56 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
59 setup_glsl_msaa_blit_scaled_shader(struct gl_context
*ctx
,
60 struct blit_state
*blit
,
61 struct gl_renderbuffer
*src_rb
,
62 GLenum target
, GLenum filter
)
64 GLint loc_src_width
, loc_src_height
;
66 int shader_offset
= 0;
67 void *mem_ctx
= ralloc_context(NULL
);
69 char *name
, *sample_number
;
70 const uint8_t *sample_map
;
71 char *sample_map_str
= rzalloc_size(mem_ctx
, 1);
72 char *sample_map_expr
= rzalloc_size(mem_ctx
, 1);
73 char *texel_fetch_macro
= rzalloc_size(mem_ctx
, 1);
74 const char *sampler_array_suffix
= "";
76 enum blit_msaa_shader shader_index
;
79 samples
= MAX2(src_rb
->NumSamples
, 1);
80 y_scale
= samples
* 0.5;
82 /* We expect only power of 2 samples in source multisample buffer. */
83 assert(samples
> 0 && _mesa_is_pow_two(samples
));
84 while (samples
>> (shader_offset
+ 1)) {
87 /* Update the assert if we plan to support more than 8X MSAA. */
88 assert(shader_offset
> 0 && shader_offset
< 4);
90 assert(target
== GL_TEXTURE_2D_MULTISAMPLE
||
91 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
);
93 shader_index
= BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_SCALED_RESOLVE
+
96 if (target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
) {
97 shader_index
+= BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_SCALED_RESOLVE
-
98 BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_SCALED_RESOLVE
;
99 sampler_array_suffix
= "Array";
102 if (blit
->msaa_shaders
[shader_index
]) {
103 _mesa_UseProgram(blit
->msaa_shaders
[shader_index
]);
104 /* Update the uniform values. */
106 _mesa_GetUniformLocation(blit
->msaa_shaders
[shader_index
], "src_width");
108 _mesa_GetUniformLocation(blit
->msaa_shaders
[shader_index
], "src_height");
109 _mesa_Uniform1f(loc_src_width
, src_rb
->Width
);
110 _mesa_Uniform1f(loc_src_height
, src_rb
->Height
);
114 name
= ralloc_asprintf(mem_ctx
, "vec4 MSAA scaled resolve");
116 /* Below switch is used to setup the shader expression, which computes
117 * sample index and map it to to a sample number on hardware.
121 sample_number
= "sample_map[int(2 * fract(coord.x))]";
122 sample_map
= ctx
->Const
.SampleMap2x
;
125 sample_number
= "sample_map[int(2 * fract(coord.x) + 4 * fract(coord.y))]";
126 sample_map
= ctx
->Const
.SampleMap4x
;
129 sample_number
= "sample_map[int(2 * fract(coord.x) + 8 * fract(coord.y))]";
130 sample_map
= ctx
->Const
.SampleMap8x
;
133 sample_number
= NULL
;
135 _mesa_problem(ctx
, "Unsupported sample count %d\n", samples
);
136 unreachable("Unsupported sample count");
139 /* Create sample map string. */
140 for (i
= 0 ; i
< samples
- 1; i
++) {
141 ralloc_asprintf_append(&sample_map_str
, "%d, ", sample_map
[i
]);
143 ralloc_asprintf_append(&sample_map_str
, "%d", sample_map
[samples
- 1]);
145 /* Create sample map expression using above string. */
146 ralloc_asprintf_append(&sample_map_expr
,
147 " const int sample_map[%d] = int[%d](%s);\n",
148 samples
, samples
, sample_map_str
);
150 if (target
== GL_TEXTURE_2D_MULTISAMPLE
) {
151 ralloc_asprintf_append(&texel_fetch_macro
,
152 "#define TEXEL_FETCH(coord) texelFetch(texSampler, ivec2(coord), %s);\n",
155 ralloc_asprintf_append(&texel_fetch_macro
,
156 "#define TEXEL_FETCH(coord) texelFetch(texSampler, ivec3(coord, layer), %s);\n",
160 static const char vs_source
[] =
162 "in vec2 position;\n"
163 "in vec3 textureCoords;\n"
164 "out vec2 texCoords;\n"
165 "flat out int layer;\n"
168 " texCoords = textureCoords.xy;\n"
169 " layer = int(textureCoords.z);\n"
170 " gl_Position = vec4(position, 0.0, 1.0);\n"
174 fs_source
= ralloc_asprintf(mem_ctx
,
176 "#extension GL_ARB_texture_multisample : enable\n"
177 "uniform sampler2DMS%s texSampler;\n"
178 "uniform float src_width, src_height;\n"
179 "in vec2 texCoords;\n"
180 "flat in int layer;\n"
181 "out vec4 out_color;\n"
187 " const vec2 scale = vec2(2.0f, %ff);\n"
188 " const vec2 scale_inv = vec2(0.5f, %ff);\n"
189 " const vec2 s_0_offset = vec2(0.25f, %ff);\n"
190 " vec2 s_0_coord, s_1_coord, s_2_coord, s_3_coord;\n"
191 " vec4 s_0_color, s_1_color, s_2_color, s_3_color;\n"
192 " vec4 x_0_color, x_1_color;\n"
193 " vec2 tex_coord = texCoords - s_0_offset;\n"
195 " tex_coord *= scale;\n"
196 " tex_coord.x = clamp(tex_coord.x, 0.0f, scale.x * src_width - 1.0f);\n"
197 " tex_coord.y = clamp(tex_coord.y, 0.0f, scale.y * src_height - 1.0f);\n"
198 " interp = fract(tex_coord);\n"
199 " tex_coord = ivec2(tex_coord) * scale_inv;\n"
201 " /* Compute the sample coordinates used for filtering. */\n"
202 " s_0_coord = tex_coord;\n"
203 " s_1_coord = tex_coord + vec2(scale_inv.x, 0.0f);\n"
204 " s_2_coord = tex_coord + vec2(0.0f, scale_inv.y);\n"
205 " s_3_coord = tex_coord + vec2(scale_inv.x, scale_inv.y);\n"
207 " /* Fetch sample color values. */\n"
209 " s_0_color = TEXEL_FETCH(s_0_coord)\n"
210 " s_1_color = TEXEL_FETCH(s_1_coord)\n"
211 " s_2_color = TEXEL_FETCH(s_2_coord)\n"
212 " s_3_color = TEXEL_FETCH(s_3_coord)\n"
213 "#undef TEXEL_FETCH\n"
215 " /* Do bilinear filtering on sample colors. */\n"
216 " x_0_color = mix(s_0_color, s_1_color, interp.x);\n"
217 " x_1_color = mix(s_2_color, s_3_color, interp.x);\n"
218 " out_color = mix(x_0_color, x_1_color, interp.y);\n"
220 sampler_array_suffix
,
227 _mesa_meta_compile_and_link_program(ctx
, vs_source
, fs_source
, name
,
228 &blit
->msaa_shaders
[shader_index
]);
230 _mesa_GetUniformLocation(blit
->msaa_shaders
[shader_index
], "src_width");
232 _mesa_GetUniformLocation(blit
->msaa_shaders
[shader_index
], "src_height");
233 _mesa_Uniform1f(loc_src_width
, src_rb
->Width
);
234 _mesa_Uniform1f(loc_src_height
, src_rb
->Height
);
236 ralloc_free(mem_ctx
);
240 setup_glsl_msaa_blit_shader(struct gl_context
*ctx
,
241 struct blit_state
*blit
,
242 const struct gl_framebuffer
*drawFb
,
243 struct gl_renderbuffer
*src_rb
,
246 const char *vs_source
;
249 enum blit_msaa_shader shader_index
;
250 bool dst_is_msaa
= false;
252 const char *vec4_prefix
;
253 const char *sampler_array_suffix
= "";
255 const char *texcoord_type
= "vec2";
257 int shader_offset
= 0;
260 samples
= MAX2(src_rb
->NumSamples
, 1);
261 src_datatype
= _mesa_get_format_datatype(src_rb
->Format
);
263 /* depth-or-color glCopyTexImage fallback path that passes a NULL rb and
264 * doesn't handle integer.
267 src_datatype
= GL_UNSIGNED_NORMALIZED
;
270 /* We expect only power of 2 samples in source multisample buffer. */
271 assert(samples
> 0 && _mesa_is_pow_two(samples
));
272 while (samples
>> (shader_offset
+ 1)) {
275 /* Update the assert if we plan to support more than 16X MSAA. */
276 assert(shader_offset
>= 0 && shader_offset
<= 4);
278 if (drawFb
->Visual
.samples
> 1) {
279 /* If you're calling meta_BlitFramebuffer with the destination
280 * multisampled, this is the only path that will work -- swrast and
281 * CopyTexImage won't work on it either.
283 assert(ctx
->Extensions
.ARB_sample_shading
);
287 /* We need shader invocation per sample, not per pixel */
288 _mesa_set_enable(ctx
, GL_MULTISAMPLE
, GL_TRUE
);
289 _mesa_set_enable(ctx
, GL_SAMPLE_SHADING
, GL_TRUE
);
290 _mesa_MinSampleShading(1.0);
294 case GL_TEXTURE_2D_MULTISAMPLE
:
295 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
296 if (src_rb
&& (src_rb
->_BaseFormat
== GL_DEPTH_COMPONENT
||
297 src_rb
->_BaseFormat
== GL_DEPTH_STENCIL
)) {
299 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY
;
301 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE
;
304 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY
;
306 shader_index
= BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+
311 if (target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
) {
312 shader_index
+= (BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_RESOLVE
-
313 BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
);
314 sampler_array_suffix
= "Array";
315 texcoord_type
= "vec3";
319 _mesa_problem(ctx
, "Unkown texture target %s\n",
320 _mesa_enum_to_string(target
));
321 shader_index
= BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
;
324 /* We rely on the enum being sorted this way. */
325 STATIC_ASSERT(BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_INT
==
326 BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+ 5);
327 STATIC_ASSERT(BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_UINT
==
328 BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+ 10);
329 if (src_datatype
== GL_INT
) {
332 } else if (src_datatype
== GL_UNSIGNED_INT
) {
339 if (blit
->msaa_shaders
[shader_index
]) {
340 _mesa_UseProgram(blit
->msaa_shaders
[shader_index
]);
344 mem_ctx
= ralloc_context(NULL
);
346 if (shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE
||
347 shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_DEPTH_RESOLVE
||
348 shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_DEPTH_COPY
||
349 shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY
) {
351 const char *arb_sample_shading_extension_string
;
354 arb_sample_shading_extension_string
= "#extension GL_ARB_sample_shading : enable";
355 sample_index
= "gl_SampleID";
356 name
= "depth MSAA copy";
358 /* Don't need that extension, since we're drawing to a single-sampled
361 arb_sample_shading_extension_string
= "";
362 /* From the GL 4.3 spec:
364 * "If there is a multisample buffer (the value of SAMPLE_BUFFERS
365 * is one), then values are obtained from the depth samples in
366 * this buffer. It is recommended that the depth value of the
367 * centermost sample be used, though implementations may choose
368 * any function of the depth sample values at each pixel.
370 * We're slacking and instead of choosing centermost, we've got 0.
373 name
= "depth MSAA resolve";
376 vs_source
= ralloc_asprintf(mem_ctx
,
378 "in vec2 position;\n"
379 "in %s textureCoords;\n"
380 "out %s texCoords;\n"
383 " texCoords = textureCoords;\n"
384 " gl_Position = vec4(position, 0.0, 1.0);\n"
388 fs_source
= ralloc_asprintf(mem_ctx
,
390 "#extension GL_ARB_texture_multisample : enable\n"
392 "uniform sampler2DMS%s texSampler;\n"
394 "out vec4 out_color;\n"
398 " gl_FragDepth = texelFetch(texSampler, i%s(texCoords), %s).r;\n"
400 arb_sample_shading_extension_string
,
401 sampler_array_suffix
,
406 /* You can create 2D_MULTISAMPLE textures with 0 sample count (meaning 1
407 * sample). Yes, this is ridiculous.
409 char *sample_resolve
;
410 const char *arb_sample_shading_extension_string
;
411 const char *merge_function
;
412 name
= ralloc_asprintf(mem_ctx
, "%svec4 MSAA %s",
414 dst_is_msaa
? "copy" : "resolve");
417 arb_sample_shading_extension_string
= "#extension GL_ARB_sample_shading : enable";
418 sample_resolve
= ralloc_asprintf(mem_ctx
, " out_color = texelFetch(texSampler, i%s(texCoords), gl_SampleID);", texcoord_type
);
424 if (src_datatype
== GL_INT
|| src_datatype
== GL_UNSIGNED_INT
) {
426 "gvec4 merge(gvec4 a, gvec4 b) { return (a >> gvec4(1)) + (b >> gvec4(1)) + (a & b & gvec4(1)); }\n";
428 /* The divide will happen at the end for floats. */
430 "vec4 merge(vec4 a, vec4 b) { return (a + b); }\n";
433 arb_sample_shading_extension_string
= "";
435 /* We're assuming power of two samples for this resolution procedure.
437 * To avoid losing any floating point precision if the samples all
438 * happen to have the same value, we merge pairs of values at a time
439 * (so the floating point exponent just gets increased), rather than
440 * doing a naive sum and dividing.
442 assert(_mesa_is_pow_two(samples
));
443 /* Fetch each individual sample. */
444 sample_resolve
= rzalloc_size(mem_ctx
, 1);
445 for (i
= 0; i
< samples
; i
++) {
446 ralloc_asprintf_append(&sample_resolve
,
447 " gvec4 sample_1_%d = texelFetch(texSampler, i%s(texCoords), %d);\n",
448 i
, texcoord_type
, i
);
450 /* Now, merge each pair of samples, then merge each pair of those,
453 for (step
= 2; step
<= samples
; step
*= 2) {
454 for (i
= 0; i
< samples
; i
+= step
) {
455 ralloc_asprintf_append(&sample_resolve
,
456 " gvec4 sample_%d_%d = merge(sample_%d_%d, sample_%d_%d);\n",
459 step
/ 2, i
+ step
/ 2);
463 /* Scale the final result. */
464 if (src_datatype
== GL_UNSIGNED_INT
|| src_datatype
== GL_INT
) {
465 ralloc_asprintf_append(&sample_resolve
,
466 " out_color = sample_%d_0;\n",
469 ralloc_asprintf_append(&sample_resolve
,
470 " gl_FragColor = sample_%d_0 / %f;\n",
471 samples
, (float)samples
);
475 vs_source
= ralloc_asprintf(mem_ctx
,
477 "in vec2 position;\n"
478 "in %s textureCoords;\n"
479 "out %s texCoords;\n"
482 " texCoords = textureCoords;\n"
483 " gl_Position = vec4(position, 0.0, 1.0);\n"
487 fs_source
= ralloc_asprintf(mem_ctx
,
489 "#extension GL_ARB_texture_multisample : enable\n"
491 "#define gvec4 %svec4\n"
492 "uniform %ssampler2DMS%s texSampler;\n"
494 "out gvec4 out_color;\n"
496 "%s" /* merge_function */
499 "%s\n" /* sample_resolve */
501 arb_sample_shading_extension_string
,
504 sampler_array_suffix
,
510 _mesa_meta_compile_and_link_program(ctx
, vs_source
, fs_source
, name
,
511 &blit
->msaa_shaders
[shader_index
]);
513 ralloc_free(mem_ctx
);
517 setup_glsl_blit_framebuffer(struct gl_context
*ctx
,
518 struct blit_state
*blit
,
519 const struct gl_framebuffer
*drawFb
,
520 struct gl_renderbuffer
*src_rb
,
521 GLenum target
, GLenum filter
,
525 unsigned texcoord_size
;
526 bool is_target_multisample
= target
== GL_TEXTURE_2D_MULTISAMPLE
||
527 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
;
528 bool is_filter_scaled_resolve
= filter
== GL_SCALED_RESOLVE_FASTEST_EXT
||
529 filter
== GL_SCALED_RESOLVE_NICEST_EXT
;
531 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
532 assert(_mesa_is_desktop_gl(ctx
) || target
== GL_TEXTURE_2D
);
534 texcoord_size
= 2 + (src_rb
->Depth
> 1 ? 1 : 0);
536 _mesa_meta_setup_vertex_objects(&blit
->VAO
, &blit
->VBO
, true,
537 2, texcoord_size
, 0);
539 if (is_target_multisample
&& is_filter_scaled_resolve
&& is_scaled_blit
) {
540 setup_glsl_msaa_blit_scaled_shader(ctx
, blit
, src_rb
, target
, filter
);
541 } else if (is_target_multisample
) {
542 setup_glsl_msaa_blit_shader(ctx
, blit
, drawFb
, src_rb
, target
);
544 _mesa_meta_setup_blit_shader(ctx
, target
, do_depth
,
545 do_depth
? &blit
->shaders_with_depth
546 : &blit
->shaders_without_depth
);
551 * Try to do a color or depth glBlitFramebuffer using texturing.
553 * We can do this when the src renderbuffer is actually a texture, or when the
554 * driver exposes BindRenderbufferTexImage().
557 blitframebuffer_texture(struct gl_context
*ctx
,
558 const struct gl_framebuffer
*readFb
,
559 const struct gl_framebuffer
*drawFb
,
560 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
561 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
562 GLenum filter
, GLint flipX
, GLint flipY
,
563 GLboolean glsl_version
, GLboolean do_depth
)
565 int att_index
= do_depth
? BUFFER_DEPTH
: readFb
->_ColorReadBufferIndex
;
566 const struct gl_renderbuffer_attachment
*readAtt
=
567 &readFb
->Attachment
[att_index
];
568 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
569 struct fb_tex_blit_state fb_tex_blit
;
570 const GLint dstX
= MIN2(dstX0
, dstX1
);
571 const GLint dstY
= MIN2(dstY0
, dstY1
);
572 const GLint dstW
= abs(dstX1
- dstX0
);
573 const GLint dstH
= abs(dstY1
- dstY0
);
574 const int srcW
= abs(srcX1
- srcX0
);
575 const int srcH
= abs(srcY1
- srcY0
);
576 bool scaled_blit
= false;
577 struct gl_texture_object
*texObj
;
580 struct gl_renderbuffer
*rb
= readAtt
->Renderbuffer
;
581 struct temp_texture
*meta_temp_texture
;
583 if (rb
->NumSamples
&& !ctx
->Extensions
.ARB_texture_multisample
)
586 _mesa_meta_fb_tex_blit_begin(ctx
, &fb_tex_blit
);
588 if (readAtt
->Texture
&&
589 (readAtt
->Texture
->Target
== GL_TEXTURE_2D
||
590 readAtt
->Texture
->Target
== GL_TEXTURE_RECTANGLE
||
591 readAtt
->Texture
->Target
== GL_TEXTURE_2D_MULTISAMPLE
||
592 readAtt
->Texture
->Target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
)) {
593 /* If there's a texture attached of a type we can handle, then just use
596 srcLevel
= readAtt
->TextureLevel
;
597 texObj
= readAtt
->Texture
;
598 target
= texObj
->Target
;
599 } else if (!readAtt
->Texture
&& ctx
->Driver
.BindRenderbufferTexImage
) {
600 if (!_mesa_meta_bind_rb_as_tex_image(ctx
, rb
, &fb_tex_blit
.tempTex
,
605 if (_mesa_is_winsys_fbo(readFb
)) {
607 srcY0
= rb
->Height
- srcY1
;
608 srcY1
= rb
->Height
- temp
;
612 GLenum tex_base_format
;
613 /* Fall back to doing a CopyTexSubImage to get the destination
614 * renderbuffer into a texture.
616 if (ctx
->Meta
->Blit
.no_ctsi_fallback
)
619 if (rb
->NumSamples
> 1)
623 meta_temp_texture
= _mesa_meta_get_temp_depth_texture(ctx
);
624 tex_base_format
= GL_DEPTH_COMPONENT
;
626 meta_temp_texture
= _mesa_meta_get_temp_texture(ctx
);
628 _mesa_base_tex_format(ctx
, rb
->InternalFormat
);
632 target
= meta_temp_texture
->Target
;
633 texObj
= _mesa_lookup_texture(ctx
, meta_temp_texture
->TexObj
);
634 if (texObj
== NULL
) {
638 _mesa_meta_setup_copypix_texture(ctx
, meta_temp_texture
,
651 fb_tex_blit
.baseLevelSave
= texObj
->BaseLevel
;
652 fb_tex_blit
.maxLevelSave
= texObj
->MaxLevel
;
653 fb_tex_blit
.stencilSamplingSave
= texObj
->StencilSampling
;
655 scaled_blit
= dstW
!= srcW
|| dstH
!= srcH
;
658 setup_glsl_blit_framebuffer(ctx
, blit
, drawFb
, rb
, target
, filter
, scaled_blit
,
662 _mesa_meta_setup_ff_tnl_for_blit(&ctx
->Meta
->Blit
.VAO
,
663 &ctx
->Meta
->Blit
.VBO
,
668 printf("Blit from texture!\n");
669 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
670 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
673 fb_tex_blit
.sampler
= _mesa_meta_setup_sampler(ctx
, texObj
, target
, filter
,
676 /* Always do our blits with no net sRGB decode or encode.
678 * However, if both the src and dst can be srgb decode/encoded, enable them
679 * so that we do any blending (from scaling or from MSAA resolves) in the
682 * Our choice of not doing any net encode/decode is from the GL 3.0
685 * "Blit operations bypass the fragment pipeline. The only fragment
686 * operations which affect a blit are the pixel ownership test and the
689 * The GL 4.4 specification disagrees and says that the sRGB part of the
690 * fragment pipeline applies, but this was found to break applications.
692 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
693 if (_mesa_get_format_color_encoding(rb
->Format
) == GL_SRGB
&&
694 drawFb
->Visual
.sRGBCapable
) {
695 _mesa_SamplerParameteri(fb_tex_blit
.sampler
,
696 GL_TEXTURE_SRGB_DECODE_EXT
, GL_DECODE_EXT
);
697 _mesa_set_framebuffer_srgb(ctx
, GL_TRUE
);
699 _mesa_SamplerParameteri(fb_tex_blit
.sampler
,
700 GL_TEXTURE_SRGB_DECODE_EXT
,
702 /* set_framebuffer_srgb was set by _mesa_meta_begin(). */
707 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
708 _mesa_set_enable(ctx
, target
, GL_TRUE
);
711 /* Prepare vertex data (the VBO was previously created and bound) */
713 struct vertex verts
[4];
714 GLfloat s0
, t0
, s1
, t1
;
716 if (target
== GL_TEXTURE_2D
) {
717 const struct gl_texture_image
*texImage
718 = _mesa_select_tex_image(texObj
, target
, srcLevel
);
719 s0
= srcX0
/ (float) texImage
->Width
;
720 s1
= srcX1
/ (float) texImage
->Width
;
721 t0
= srcY0
/ (float) texImage
->Height
;
722 t1
= srcY1
/ (float) texImage
->Height
;
725 assert(target
== GL_TEXTURE_RECTANGLE_ARB
||
726 target
== GL_TEXTURE_2D_MULTISAMPLE
||
727 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
);
734 /* Silence valgrind warnings about reading uninitialized stack. */
735 memset(verts
, 0, sizeof(verts
));
737 /* setup vertex positions */
738 verts
[0].x
= -1.0F
* flipX
;
739 verts
[0].y
= -1.0F
* flipY
;
740 verts
[1].x
= 1.0F
* flipX
;
741 verts
[1].y
= -1.0F
* flipY
;
742 verts
[2].x
= 1.0F
* flipX
;
743 verts
[2].y
= 1.0F
* flipY
;
744 verts
[3].x
= -1.0F
* flipX
;
745 verts
[3].y
= 1.0F
* flipY
;
747 verts
[0].tex
[0] = s0
;
748 verts
[0].tex
[1] = t0
;
749 verts
[0].tex
[2] = readAtt
->Zoffset
;
750 verts
[1].tex
[0] = s1
;
751 verts
[1].tex
[1] = t0
;
752 verts
[1].tex
[2] = readAtt
->Zoffset
;
753 verts
[2].tex
[0] = s1
;
754 verts
[2].tex
[1] = t1
;
755 verts
[2].tex
[2] = readAtt
->Zoffset
;
756 verts
[3].tex
[0] = s0
;
757 verts
[3].tex
[1] = t1
;
758 verts
[3].tex
[2] = readAtt
->Zoffset
;
760 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
764 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
765 _mesa_ColorMask(!do_depth
, !do_depth
, !do_depth
, !do_depth
);
766 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, do_depth
);
767 _mesa_DepthMask(do_depth
);
768 _mesa_DepthFunc(GL_ALWAYS
);
770 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
771 _mesa_meta_fb_tex_blit_end(ctx
, target
, &fb_tex_blit
);
777 _mesa_meta_fb_tex_blit_begin(const struct gl_context
*ctx
,
778 struct fb_tex_blit_state
*blit
)
781 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
782 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
787 _mesa_meta_fb_tex_blit_end(struct gl_context
*ctx
, GLenum target
,
788 struct fb_tex_blit_state
*blit
)
790 /* Restore texture object state, the texture binding will
791 * be restored by _mesa_meta_end().
793 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
794 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, blit
->baseLevelSave
);
795 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, blit
->maxLevelSave
);
797 if (ctx
->Extensions
.ARB_stencil_texturing
) {
798 const struct gl_texture_object
*texObj
=
799 _mesa_get_current_tex_object(ctx
, target
);
801 if (texObj
->StencilSampling
!= blit
->stencilSamplingSave
)
802 _mesa_TexParameteri(target
, GL_DEPTH_STENCIL_TEXTURE_MODE
,
803 blit
->stencilSamplingSave
?
804 GL_STENCIL_INDEX
: GL_DEPTH_COMPONENT
);
808 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, blit
->samplerSave
);
809 _mesa_DeleteSamplers(1, &blit
->sampler
);
811 _mesa_DeleteTextures(1, &blit
->tempTex
);
815 _mesa_meta_bind_rb_as_tex_image(struct gl_context
*ctx
,
816 struct gl_renderbuffer
*rb
,
818 struct gl_texture_object
**texObj
,
821 struct gl_texture_image
*texImage
;
824 if (rb
->NumSamples
> 1)
825 *target
= GL_TEXTURE_2D_MULTISAMPLE
;
827 *target
= GL_TEXTURE_2D
;
830 _mesa_GenTextures(1, &tempTex
);
836 _mesa_BindTexture(*target
, *tex
);
837 *texObj
= _mesa_lookup_texture(ctx
, *tex
);
838 texImage
= _mesa_get_tex_image(ctx
, *texObj
, *target
, 0);
840 if (!ctx
->Driver
.BindRenderbufferTexImage(ctx
, rb
, texImage
)) {
841 _mesa_DeleteTextures(1, tex
);
845 if (ctx
->Driver
.FinishRenderTexture
&& !rb
->NeedsFinishRenderTexture
) {
846 rb
->NeedsFinishRenderTexture
= true;
847 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
854 _mesa_meta_setup_sampler(struct gl_context
*ctx
,
855 const struct gl_texture_object
*texObj
,
856 GLenum target
, GLenum filter
, GLuint srcLevel
)
859 GLenum tex_filter
= (filter
== GL_SCALED_RESOLVE_FASTEST_EXT
||
860 filter
== GL_SCALED_RESOLVE_NICEST_EXT
) ?
863 _mesa_GenSamplers(1, &sampler
);
864 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, sampler
);
866 /* Prepare src texture state */
867 _mesa_BindTexture(target
, texObj
->Name
);
868 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MIN_FILTER
, tex_filter
);
869 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MAG_FILTER
, tex_filter
);
870 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
871 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
872 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
874 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
875 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
881 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
882 * of texture mapping and polygon rendering.
885 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
886 const struct gl_framebuffer
*readFb
,
887 const struct gl_framebuffer
*drawFb
,
888 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
889 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
890 GLbitfield mask
, GLenum filter
)
892 const GLint dstW
= abs(dstX1
- dstX0
);
893 const GLint dstH
= abs(dstY1
- dstY0
);
894 const GLint dstFlipX
= (dstX1
- dstX0
) / dstW
;
895 const GLint dstFlipY
= (dstY1
- dstY0
) / dstH
;
898 GLint srcX0
, srcY0
, srcX1
, srcY1
;
899 GLint dstX0
, dstY0
, dstX1
, dstY1
;
901 srcX0
, srcY0
, srcX1
, srcY1
,
902 dstX0
, dstY0
, dstX1
, dstY1
905 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
906 ctx
->Extensions
.ARB_fragment_shader
;
908 /* Multisample texture blit support requires texture multisample. */
909 if (readFb
->Visual
.samples
> 0 &&
910 !ctx
->Extensions
.ARB_texture_multisample
) {
914 /* Clip a copy of the blit coordinates. If these differ from the input
915 * coordinates, then we'll set the scissor.
917 if (!_mesa_clip_blit(ctx
, readFb
, drawFb
,
918 &clip
.srcX0
, &clip
.srcY0
, &clip
.srcX1
, &clip
.srcY1
,
919 &clip
.dstX0
, &clip
.dstY0
, &clip
.dstX1
, &clip
.dstY1
)) {
920 /* clipped/scissored everything away */
924 /* Only scissor affects blit, but we're doing to set a custom scissor if
925 * necessary anyway, so save/clear state.
927 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_DRAW_BUFFERS
);
929 /* Dithering shouldn't be performed for glBlitFramebuffer */
930 _mesa_set_enable(ctx
, GL_DITHER
, GL_FALSE
);
932 /* If the clipping earlier changed the destination rect at all, then
933 * enable the scissor to clip to it.
935 if (clip
.dstX0
!= dstX0
|| clip
.dstY0
!= dstY0
||
936 clip
.dstX1
!= dstX1
|| clip
.dstY1
!= dstY1
) {
937 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_TRUE
);
938 _mesa_Scissor(MIN2(clip
.dstX0
, clip
.dstX1
),
939 MIN2(clip
.dstY0
, clip
.dstY1
),
940 abs(clip
.dstX0
- clip
.dstX1
),
941 abs(clip
.dstY0
- clip
.dstY1
));
944 /* Try faster, direct texture approach first */
945 if (mask
& GL_COLOR_BUFFER_BIT
) {
946 if (blitframebuffer_texture(ctx
, readFb
, drawFb
,
947 srcX0
, srcY0
, srcX1
, srcY1
,
948 dstX0
, dstY0
, dstX1
, dstY1
,
949 filter
, dstFlipX
, dstFlipY
,
950 use_glsl_version
, false)) {
951 mask
&= ~GL_COLOR_BUFFER_BIT
;
955 if (mask
& GL_DEPTH_BUFFER_BIT
&& use_glsl_version
) {
956 if (blitframebuffer_texture(ctx
, readFb
, drawFb
,
957 srcX0
, srcY0
, srcX1
, srcY1
,
958 dstX0
, dstY0
, dstX1
, dstY1
,
959 filter
, dstFlipX
, dstFlipY
,
960 use_glsl_version
, true)) {
961 mask
&= ~GL_DEPTH_BUFFER_BIT
;
965 if (mask
& GL_STENCIL_BUFFER_BIT
) {
966 /* XXX can't easily do stencil */
975 _mesa_meta_glsl_blit_cleanup(struct blit_state
*blit
)
978 _mesa_DeleteVertexArrays(1, &blit
->VAO
);
980 _mesa_DeleteBuffers(1, &blit
->VBO
);
984 _mesa_meta_blit_shader_table_cleanup(&blit
->shaders_with_depth
);
985 _mesa_meta_blit_shader_table_cleanup(&blit
->shaders_without_depth
);
987 _mesa_DeleteTextures(1, &blit
->depthTex
.TexObj
);
988 blit
->depthTex
.TexObj
= 0;
992 _mesa_meta_and_swrast_BlitFramebuffer(struct gl_context
*ctx
,
993 struct gl_framebuffer
*readFb
,
994 struct gl_framebuffer
*drawFb
,
995 GLint srcX0
, GLint srcY0
,
996 GLint srcX1
, GLint srcY1
,
997 GLint dstX0
, GLint dstY0
,
998 GLint dstX1
, GLint dstY1
,
999 GLbitfield mask
, GLenum filter
)
1001 mask
= _mesa_meta_BlitFramebuffer(ctx
, readFb
, drawFb
,
1002 srcX0
, srcY0
, srcX1
, srcY1
,
1003 dstX0
, dstY0
, dstX1
, dstY1
,
1008 _swrast_BlitFramebuffer(ctx
, readFb
, drawFb
,
1009 srcX0
, srcY0
, srcX1
, srcY1
,
1010 dstX0
, dstY0
, dstX1
, dstY1
,