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
,
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
= "";
75 float x_scale
, y_scale
;
76 enum blit_msaa_shader shader_index
;
79 samples
= MAX2(src_rb
->NumSamples
, 1);
85 y_scale
= samples
/ x_scale
;
87 /* We expect only power of 2 samples in source multisample buffer. */
88 assert(samples
> 0 && _mesa_is_pow_two(samples
));
89 while (samples
>> (shader_offset
+ 1)) {
92 /* Update the assert if we plan to support more than 16X MSAA. */
93 assert(shader_offset
> 0 && shader_offset
<= 4);
95 assert(target
== GL_TEXTURE_2D_MULTISAMPLE
||
96 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
);
98 shader_index
= BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_SCALED_RESOLVE
+
101 if (target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
) {
102 shader_index
+= BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_SCALED_RESOLVE
-
103 BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_SCALED_RESOLVE
;
104 sampler_array_suffix
= "Array";
107 if (blit
->msaa_shaders
[shader_index
]) {
108 _mesa_meta_use_program(ctx
, blit
->msaa_shaders
[shader_index
]);
109 /* Update the uniform values. */
111 _mesa_program_resource_location(blit
->msaa_shaders
[shader_index
], GL_UNIFORM
, "src_width");
113 _mesa_program_resource_location(blit
->msaa_shaders
[shader_index
], GL_UNIFORM
, "src_height");
114 _mesa_Uniform1f(loc_src_width
, src_rb
->Width
);
115 _mesa_Uniform1f(loc_src_height
, src_rb
->Height
);
119 name
= ralloc_asprintf(mem_ctx
, "vec4 MSAA scaled resolve");
121 /* Below switch is used to setup the shader expression, which computes
122 * sample index and map it to to a sample number on hardware.
126 sample_number
= "sample_map[int(2 * fract(coord.x))]";
127 sample_map
= ctx
->Const
.SampleMap2x
;
130 sample_number
= "sample_map[int(2 * fract(coord.x) + 4 * fract(coord.y))]";
131 sample_map
= ctx
->Const
.SampleMap4x
;
134 sample_number
= "sample_map[int(2 * fract(coord.x) + 8 * fract(coord.y))]";
135 sample_map
= ctx
->Const
.SampleMap8x
;
138 sample_number
= "sample_map[int(4 * fract(coord.x) + 16 * fract(coord.y))]";
139 sample_map
= ctx
->Const
.SampleMap16x
;
142 sample_number
= NULL
;
144 _mesa_problem(ctx
, "Unsupported sample count %d\n", samples
);
145 unreachable("Unsupported sample count");
148 /* Create sample map string. */
149 for (i
= 0 ; i
< samples
- 1; i
++) {
150 ralloc_asprintf_append(&sample_map_str
, "%d, ", sample_map
[i
]);
152 ralloc_asprintf_append(&sample_map_str
, "%d", sample_map
[samples
- 1]);
154 /* Create sample map expression using above string. */
155 ralloc_asprintf_append(&sample_map_expr
,
156 " const int sample_map[%d] = int[%d](%s);\n",
157 samples
, samples
, sample_map_str
);
159 if (target
== GL_TEXTURE_2D_MULTISAMPLE
) {
160 ralloc_asprintf_append(&texel_fetch_macro
,
161 "#define TEXEL_FETCH(coord) texelFetch(texSampler, ivec2(coord), %s);\n",
164 ralloc_asprintf_append(&texel_fetch_macro
,
165 "#define TEXEL_FETCH(coord) texelFetch(texSampler, ivec3(coord, layer), %s);\n",
169 static const char vs_source
[] =
171 "#extension GL_ARB_explicit_attrib_location: enable\n"
172 "layout(location = 0) in vec2 position;\n"
173 "layout(location = 1) in vec3 textureCoords;\n"
174 "out vec2 texCoords;\n"
175 "flat out int layer;\n"
178 " texCoords = textureCoords.xy;\n"
179 " layer = int(textureCoords.z);\n"
180 " gl_Position = vec4(position, 0.0, 1.0);\n"
184 fs_source
= ralloc_asprintf(mem_ctx
,
186 "#extension GL_ARB_texture_multisample : enable\n"
187 "uniform sampler2DMS%s texSampler;\n"
188 "uniform float src_width, src_height;\n"
189 "in vec2 texCoords;\n"
190 "flat in int layer;\n"
191 "out vec4 out_color;\n"
197 " const vec2 scale = vec2(%ff, %ff);\n"
198 " const vec2 scale_inv = vec2(%ff, %ff);\n"
199 " const vec2 s_0_offset = vec2(%ff, %ff);\n"
200 " vec2 s_0_coord, s_1_coord, s_2_coord, s_3_coord;\n"
201 " vec4 s_0_color, s_1_color, s_2_color, s_3_color;\n"
202 " vec4 x_0_color, x_1_color;\n"
203 " vec2 tex_coord = texCoords - s_0_offset;\n"
205 " tex_coord *= scale;\n"
206 " tex_coord.x = clamp(tex_coord.x, 0.0f, scale.x * src_width - 1.0f);\n"
207 " tex_coord.y = clamp(tex_coord.y, 0.0f, scale.y * src_height - 1.0f);\n"
208 " interp = fract(tex_coord);\n"
209 " tex_coord = ivec2(tex_coord) * scale_inv;\n"
211 " /* Compute the sample coordinates used for filtering. */\n"
212 " s_0_coord = tex_coord;\n"
213 " s_1_coord = tex_coord + vec2(scale_inv.x, 0.0f);\n"
214 " s_2_coord = tex_coord + vec2(0.0f, scale_inv.y);\n"
215 " s_3_coord = tex_coord + vec2(scale_inv.x, scale_inv.y);\n"
217 " /* Fetch sample color values. */\n"
219 " s_0_color = TEXEL_FETCH(s_0_coord)\n"
220 " s_1_color = TEXEL_FETCH(s_1_coord)\n"
221 " s_2_color = TEXEL_FETCH(s_2_coord)\n"
222 " s_3_color = TEXEL_FETCH(s_3_coord)\n"
223 "#undef TEXEL_FETCH\n"
225 " /* Do bilinear filtering on sample colors. */\n"
226 " x_0_color = mix(s_0_color, s_1_color, interp.x);\n"
227 " x_1_color = mix(s_2_color, s_3_color, interp.x);\n"
228 " out_color = mix(x_0_color, x_1_color, interp.y);\n"
230 sampler_array_suffix
,
233 1.0f
/ x_scale
, 1.0f
/ y_scale
,
234 0.5f
/ x_scale
, 0.5f
/ y_scale
,
237 _mesa_meta_compile_and_link_program(ctx
, vs_source
, fs_source
, name
,
238 &blit
->msaa_shaders
[shader_index
]);
240 _mesa_program_resource_location(blit
->msaa_shaders
[shader_index
], GL_UNIFORM
, "src_width");
242 _mesa_program_resource_location(blit
->msaa_shaders
[shader_index
], GL_UNIFORM
, "src_height");
243 _mesa_Uniform1f(loc_src_width
, src_rb
->Width
);
244 _mesa_Uniform1f(loc_src_height
, src_rb
->Height
);
246 ralloc_free(mem_ctx
);
250 setup_glsl_msaa_blit_shader(struct gl_context
*ctx
,
251 struct blit_state
*blit
,
252 const struct gl_framebuffer
*drawFb
,
253 struct gl_renderbuffer
*src_rb
,
256 const char *vs_source
;
259 enum blit_msaa_shader shader_index
;
260 bool dst_is_msaa
= false;
262 const char *vec4_prefix
;
263 const char *sampler_array_suffix
= "";
265 const char *texcoord_type
= "vec2";
267 int shader_offset
= 0;
270 samples
= MAX2(src_rb
->NumSamples
, 1);
271 src_datatype
= _mesa_get_format_datatype(src_rb
->Format
);
273 /* depth-or-color glCopyTexImage fallback path that passes a NULL rb and
274 * doesn't handle integer.
277 src_datatype
= GL_UNSIGNED_NORMALIZED
;
280 /* We expect only power of 2 samples in source multisample buffer. */
281 assert(samples
> 0 && _mesa_is_pow_two(samples
));
282 while (samples
>> (shader_offset
+ 1)) {
285 /* Update the assert if we plan to support more than 16X MSAA. */
286 assert(shader_offset
>= 0 && shader_offset
<= 4);
288 if (drawFb
->Visual
.samples
> 1) {
289 /* If you're calling meta_BlitFramebuffer with the destination
290 * multisampled, this is the only path that will work -- swrast and
291 * CopyTexImage won't work on it either.
293 assert(ctx
->Extensions
.ARB_sample_shading
);
297 /* We need shader invocation per sample, not per pixel */
298 _mesa_set_enable(ctx
, GL_MULTISAMPLE
, GL_TRUE
);
299 _mesa_set_enable(ctx
, GL_SAMPLE_SHADING
, GL_TRUE
);
300 _mesa_MinSampleShading(1.0);
304 case GL_TEXTURE_2D_MULTISAMPLE
:
305 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
306 if (src_rb
&& (src_rb
->_BaseFormat
== GL_DEPTH_COMPONENT
||
307 src_rb
->_BaseFormat
== GL_DEPTH_STENCIL
)) {
309 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY
;
311 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE
;
314 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY
;
316 shader_index
= BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+
321 if (target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
) {
322 shader_index
+= (BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_RESOLVE
-
323 BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
);
324 sampler_array_suffix
= "Array";
325 texcoord_type
= "vec3";
329 _mesa_problem(ctx
, "Unknown texture target %s\n",
330 _mesa_enum_to_string(target
));
331 shader_index
= BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
;
334 /* We rely on the enum being sorted this way. */
335 STATIC_ASSERT(BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_INT
==
336 BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+ 5);
337 STATIC_ASSERT(BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_UINT
==
338 BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+ 10);
339 if (src_datatype
== GL_INT
) {
342 } else if (src_datatype
== GL_UNSIGNED_INT
) {
349 if (blit
->msaa_shaders
[shader_index
]) {
350 _mesa_meta_use_program(ctx
, blit
->msaa_shaders
[shader_index
]);
354 mem_ctx
= ralloc_context(NULL
);
356 if (shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE
||
357 shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_DEPTH_RESOLVE
||
358 shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_DEPTH_COPY
||
359 shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY
) {
361 const char *tex_coords
= "texCoords";
364 sample_index
= "gl_SampleID";
365 name
= "depth MSAA copy";
367 if (ctx
->Extensions
.ARB_gpu_shader5
&& samples
>= 16) {
368 /* See comment below for the color copy */
369 tex_coords
= "interpolateAtOffset(texCoords, vec2(0.0))";
372 /* From the GL 4.3 spec:
374 * "If there is a multisample buffer (the value of SAMPLE_BUFFERS
375 * is one), then values are obtained from the depth samples in
376 * this buffer. It is recommended that the depth value of the
377 * centermost sample be used, though implementations may choose
378 * any function of the depth sample values at each pixel.
380 * We're slacking and instead of choosing centermost, we've got 0.
383 name
= "depth MSAA resolve";
386 vs_source
= ralloc_asprintf(mem_ctx
,
388 "#extension GL_ARB_explicit_attrib_location: enable\n"
389 "layout(location = 0) in vec2 position;\n"
390 "layout(location = 1) in %s textureCoords;\n"
391 "out %s texCoords;\n"
394 " texCoords = textureCoords;\n"
395 " gl_Position = vec4(position, 0.0, 1.0);\n"
399 fs_source
= ralloc_asprintf(mem_ctx
,
401 "#extension GL_ARB_texture_multisample : enable\n"
402 "#extension GL_ARB_sample_shading : enable\n"
403 "#extension GL_ARB_gpu_shader5 : enable\n"
404 "uniform sampler2DMS%s texSampler;\n"
406 "out vec4 out_color;\n"
410 " gl_FragDepth = texelFetch(texSampler, i%s(%s), %s).r;\n"
412 sampler_array_suffix
,
418 /* You can create 2D_MULTISAMPLE textures with 0 sample count (meaning 1
419 * sample). Yes, this is ridiculous.
421 char *sample_resolve
;
422 const char *merge_function
;
423 name
= ralloc_asprintf(mem_ctx
, "%svec4 MSAA %s",
425 dst_is_msaa
? "copy" : "resolve");
428 const char *tex_coords
;
430 if (ctx
->Extensions
.ARB_gpu_shader5
&& samples
>= 16) {
431 /* If interpolateAtOffset is available then it will be used to
432 * force the interpolation to the center. This is required at
433 * least on Intel hardware because it is possible to have a sample
434 * position on the 0 x or y axis which means it will lie exactly
435 * on the pixel boundary. If we let the hardware interpolate the
436 * coordinates at one of these positions then it is possible for
437 * it to jump to a neighboring texel when converting to ints due
438 * to rounding errors. This is only done for >= 16x MSAA because
439 * it probably has some overhead. It is more likely that some
440 * hardware will use one of these problematic positions at 16x
441 * MSAA because in that case in D3D they are defined to be at
444 tex_coords
= "interpolateAtOffset(texCoords, vec2(0.0))";
446 tex_coords
= "texCoords";
450 ralloc_asprintf(mem_ctx
,
451 " out_color = texelFetch(texSampler, "
452 "i%s(%s), gl_SampleID);",
453 texcoord_type
, tex_coords
);
460 if (src_datatype
== GL_INT
|| src_datatype
== GL_UNSIGNED_INT
) {
461 /* From the OpenGL ES 3.2 spec section 16.2.1:
463 * "If the source formats are integer types or stencil values,
464 * a single sample's value is selected for each pixel."
466 * The OpenGL 4.4 spec contains exactly the same language.
468 * We can accomplish this by making the merge function return just
469 * one of the two samples. The compiler should do the rest.
471 merge_function
= "gvec4 merge(gvec4 a, gvec4 b) { return a; }\n";
473 /* The divide will happen at the end for floats. */
475 "vec4 merge(vec4 a, vec4 b) { return (a + b); }\n";
478 /* We're assuming power of two samples for this resolution procedure.
480 * To avoid losing any floating point precision if the samples all
481 * happen to have the same value, we merge pairs of values at a time
482 * (so the floating point exponent just gets increased), rather than
483 * doing a naive sum and dividing.
485 assert(_mesa_is_pow_two(samples
));
486 /* Fetch each individual sample. */
487 sample_resolve
= rzalloc_size(mem_ctx
, 1);
488 for (i
= 0; i
< samples
; i
++) {
489 ralloc_asprintf_append(&sample_resolve
,
490 " gvec4 sample_1_%d = texelFetch(texSampler, i%s(texCoords), %d);\n",
491 i
, texcoord_type
, i
);
493 /* Now, merge each pair of samples, then merge each pair of those,
496 for (step
= 2; step
<= samples
; step
*= 2) {
497 for (i
= 0; i
< samples
; i
+= step
) {
498 ralloc_asprintf_append(&sample_resolve
,
499 " gvec4 sample_%d_%d = merge(sample_%d_%d, sample_%d_%d);\n",
502 step
/ 2, i
+ step
/ 2);
506 /* Scale the final result. */
507 if (src_datatype
== GL_UNSIGNED_INT
|| src_datatype
== GL_INT
) {
508 ralloc_asprintf_append(&sample_resolve
,
509 " out_color = sample_%d_0;\n",
512 ralloc_asprintf_append(&sample_resolve
,
513 " gl_FragColor = sample_%d_0 / %f;\n",
514 samples
, (float)samples
);
518 vs_source
= ralloc_asprintf(mem_ctx
,
520 "#extension GL_ARB_explicit_attrib_location: enable\n"
521 "layout(location = 0) in vec2 position;\n"
522 "layout(location = 1) in %s textureCoords;\n"
523 "out %s texCoords;\n"
526 " texCoords = textureCoords;\n"
527 " gl_Position = vec4(position, 0.0, 1.0);\n"
531 fs_source
= ralloc_asprintf(mem_ctx
,
533 "#extension GL_ARB_texture_multisample : enable\n"
534 "#extension GL_ARB_sample_shading : enable\n"
535 "#extension GL_ARB_gpu_shader5 : enable\n"
536 "#define gvec4 %svec4\n"
537 "uniform %ssampler2DMS%s texSampler;\n"
539 "out gvec4 out_color;\n"
541 "%s" /* merge_function */
544 "%s\n" /* sample_resolve */
548 sampler_array_suffix
,
554 _mesa_meta_compile_and_link_program(ctx
, vs_source
, fs_source
, name
,
555 &blit
->msaa_shaders
[shader_index
]);
557 ralloc_free(mem_ctx
);
561 setup_glsl_blit_framebuffer(struct gl_context
*ctx
,
562 struct blit_state
*blit
,
563 const struct gl_framebuffer
*drawFb
,
564 struct gl_renderbuffer
*src_rb
,
565 GLenum target
, GLenum filter
,
569 unsigned texcoord_size
;
570 bool is_target_multisample
= target
== GL_TEXTURE_2D_MULTISAMPLE
||
571 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
;
572 bool is_filter_scaled_resolve
= filter
== GL_SCALED_RESOLVE_FASTEST_EXT
||
573 filter
== GL_SCALED_RESOLVE_NICEST_EXT
;
575 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
576 assert(_mesa_is_desktop_gl(ctx
) || target
== GL_TEXTURE_2D
);
578 texcoord_size
= 2 + (src_rb
->Depth
> 1 ? 1 : 0);
580 _mesa_meta_setup_vertex_objects(ctx
, &blit
->VAO
, &blit
->buf_obj
, true,
581 2, texcoord_size
, 0);
583 if (is_target_multisample
&& is_filter_scaled_resolve
&& is_scaled_blit
) {
584 setup_glsl_msaa_blit_scaled_shader(ctx
, blit
, src_rb
, target
);
585 } else if (is_target_multisample
) {
586 setup_glsl_msaa_blit_shader(ctx
, blit
, drawFb
, src_rb
, target
);
588 _mesa_meta_setup_blit_shader(ctx
, target
, do_depth
,
589 do_depth
? &blit
->shaders_with_depth
590 : &blit
->shaders_without_depth
);
595 * Try to do a color or depth glBlitFramebuffer using texturing.
597 * We can do this when the src renderbuffer is actually a texture, or when the
598 * driver exposes BindRenderbufferTexImage().
601 blitframebuffer_texture(struct gl_context
*ctx
,
602 const struct gl_framebuffer
*readFb
,
603 const struct gl_framebuffer
*drawFb
,
604 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
605 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
606 GLenum filter
, GLint flipX
, GLint flipY
,
607 GLboolean glsl_version
, GLboolean do_depth
)
609 int att_index
= do_depth
? BUFFER_DEPTH
: readFb
->_ColorReadBufferIndex
;
610 const struct gl_renderbuffer_attachment
*readAtt
=
611 &readFb
->Attachment
[att_index
];
612 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
613 struct fb_tex_blit_state fb_tex_blit
;
614 const GLint dstX
= MIN2(dstX0
, dstX1
);
615 const GLint dstY
= MIN2(dstY0
, dstY1
);
616 const GLint dstW
= abs(dstX1
- dstX0
);
617 const GLint dstH
= abs(dstY1
- dstY0
);
618 const int srcW
= abs(srcX1
- srcX0
);
619 const int srcH
= abs(srcY1
- srcY0
);
620 bool scaled_blit
= false;
621 struct gl_texture_object
*texObj
;
624 struct gl_renderbuffer
*rb
= readAtt
->Renderbuffer
;
625 struct temp_texture
*meta_temp_texture
;
627 if (rb
->NumSamples
&& !ctx
->Extensions
.ARB_texture_multisample
)
630 _mesa_meta_fb_tex_blit_begin(ctx
, &fb_tex_blit
);
632 if (readAtt
->Texture
&&
633 (readAtt
->Texture
->Target
== GL_TEXTURE_2D
||
634 readAtt
->Texture
->Target
== GL_TEXTURE_RECTANGLE
||
635 readAtt
->Texture
->Target
== GL_TEXTURE_2D_MULTISAMPLE
||
636 readAtt
->Texture
->Target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
)) {
637 /* If there's a texture attached of a type we can handle, then just use
640 srcLevel
= readAtt
->TextureLevel
;
641 texObj
= readAtt
->Texture
;
642 } else if (!readAtt
->Texture
&& ctx
->Driver
.BindRenderbufferTexImage
) {
643 texObj
= _mesa_meta_texture_object_from_renderbuffer(ctx
, rb
);
647 fb_tex_blit
.temp_tex_obj
= texObj
;
650 if (_mesa_is_winsys_fbo(readFb
)) {
652 srcY0
= rb
->Height
- srcY1
;
653 srcY1
= rb
->Height
- temp
;
657 GLenum tex_base_format
;
658 /* Fall back to doing a CopyTexSubImage to get the destination
659 * renderbuffer into a texture.
661 if (ctx
->Meta
->Blit
.no_ctsi_fallback
)
664 if (rb
->NumSamples
> 1)
668 meta_temp_texture
= _mesa_meta_get_temp_depth_texture(ctx
);
669 tex_base_format
= GL_DEPTH_COMPONENT
;
671 meta_temp_texture
= _mesa_meta_get_temp_texture(ctx
);
673 _mesa_base_tex_format(ctx
, rb
->InternalFormat
);
677 texObj
= meta_temp_texture
->tex_obj
;
678 if (texObj
== NULL
) {
682 _mesa_meta_setup_copypix_texture(ctx
, meta_temp_texture
,
688 assert(texObj
->Target
== meta_temp_texture
->Target
);
696 target
= texObj
->Target
;
697 fb_tex_blit
.tex_obj
= texObj
;
698 fb_tex_blit
.baseLevelSave
= texObj
->BaseLevel
;
699 fb_tex_blit
.maxLevelSave
= texObj
->MaxLevel
;
700 fb_tex_blit
.stencilSamplingSave
= texObj
->StencilSampling
;
702 scaled_blit
= dstW
!= srcW
|| dstH
!= srcH
;
705 setup_glsl_blit_framebuffer(ctx
, blit
, drawFb
, rb
, target
, filter
, scaled_blit
,
709 _mesa_meta_setup_ff_tnl_for_blit(ctx
,
710 &ctx
->Meta
->Blit
.VAO
,
711 &ctx
->Meta
->Blit
.buf_obj
,
716 printf("Blit from texture!\n");
717 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
718 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
721 fb_tex_blit
.samp_obj
= _mesa_meta_setup_sampler(ctx
, texObj
, target
, filter
,
724 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
725 /* The GL 4.4 spec, section 18.3.1 ("Blitting Pixel Rectangles") says:
727 * "When values are taken from the read buffer, if FRAMEBUFFER_SRGB
728 * is enabled and the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
729 * for the framebuffer attachment corresponding to the read buffer
730 * is SRGB (see section 9.2.3), the red, green, and blue components
731 * are converted from the non-linear sRGB color space according to
734 * When values are written to the draw buffers, blit operations
735 * bypass most of the fragment pipeline. The only fragment
736 * operations which affect a blit are the pixel ownership test,
737 * the scissor test, and sRGB conversion (see section 17.3.9)."
739 * ES 3.0 contains nearly the exact same text, but omits the part
740 * about GL_FRAMEBUFFER_SRGB as that doesn't exist in ES. Mesa
741 * defaults it to on for ES contexts, so we can safely check it.
744 ctx
->Color
.sRGBEnabled
&& _mesa_is_format_srgb(rb
->Format
);
746 _mesa_set_sampler_srgb_decode(ctx
, fb_tex_blit
.samp_obj
,
747 decode
? GL_DECODE_EXT
748 : GL_SKIP_DECODE_EXT
);
752 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
753 _mesa_set_enable(ctx
, target
, GL_TRUE
);
756 /* Prepare vertex data (the VBO was previously created and bound) */
758 struct vertex verts
[4];
759 GLfloat s0
, t0
, s1
, t1
;
761 if (target
== GL_TEXTURE_2D
) {
762 const struct gl_texture_image
*texImage
763 = _mesa_select_tex_image(texObj
, target
, srcLevel
);
764 s0
= srcX0
/ (float) texImage
->Width
;
765 s1
= srcX1
/ (float) texImage
->Width
;
766 t0
= srcY0
/ (float) texImage
->Height
;
767 t1
= srcY1
/ (float) texImage
->Height
;
770 assert(target
== GL_TEXTURE_RECTANGLE_ARB
||
771 target
== GL_TEXTURE_2D_MULTISAMPLE
||
772 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
);
779 /* Silence valgrind warnings about reading uninitialized stack. */
780 memset(verts
, 0, sizeof(verts
));
782 /* setup vertex positions */
783 verts
[0].x
= -1.0F
* flipX
;
784 verts
[0].y
= -1.0F
* flipY
;
785 verts
[1].x
= 1.0F
* flipX
;
786 verts
[1].y
= -1.0F
* flipY
;
787 verts
[2].x
= 1.0F
* flipX
;
788 verts
[2].y
= 1.0F
* flipY
;
789 verts
[3].x
= -1.0F
* flipX
;
790 verts
[3].y
= 1.0F
* flipY
;
792 verts
[0].tex
[0] = s0
;
793 verts
[0].tex
[1] = t0
;
794 verts
[0].tex
[2] = readAtt
->Zoffset
;
795 verts
[1].tex
[0] = s1
;
796 verts
[1].tex
[1] = t0
;
797 verts
[1].tex
[2] = readAtt
->Zoffset
;
798 verts
[2].tex
[0] = s1
;
799 verts
[2].tex
[1] = t1
;
800 verts
[2].tex
[2] = readAtt
->Zoffset
;
801 verts
[3].tex
[0] = s0
;
802 verts
[3].tex
[1] = t1
;
803 verts
[3].tex
[2] = readAtt
->Zoffset
;
805 _mesa_buffer_sub_data(ctx
, blit
->buf_obj
, 0, sizeof(verts
), verts
);
809 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
810 _mesa_ColorMask(!do_depth
, !do_depth
, !do_depth
, !do_depth
);
811 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, do_depth
);
812 _mesa_DepthMask(do_depth
);
813 _mesa_DepthFunc(GL_ALWAYS
);
815 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
816 _mesa_meta_fb_tex_blit_end(ctx
, target
, &fb_tex_blit
);
822 _mesa_meta_fb_tex_blit_begin(struct gl_context
*ctx
,
823 struct fb_tex_blit_state
*blit
)
825 /* None of the existing callers preinitialize fb_tex_blit_state to zeros,
826 * and both use stack variables. If samp_obj_save is not NULL,
827 * _mesa_reference_sampler_object will try to dereference it. Leaving
828 * random garbage in samp_obj_save can only lead to crashes.
830 * Since the state isn't persistent across calls, we won't catch ref
833 blit
->samp_obj_save
= NULL
;
834 _mesa_reference_sampler_object(ctx
, &blit
->samp_obj_save
,
835 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
);
836 blit
->temp_tex_obj
= NULL
;
840 _mesa_meta_fb_tex_blit_end(struct gl_context
*ctx
, GLenum target
,
841 struct fb_tex_blit_state
*blit
)
843 struct gl_texture_object
*const texObj
=
844 _mesa_get_current_tex_object(ctx
, target
);
846 /* Either there is no temporary texture or the temporary texture is bound. */
847 assert(blit
->temp_tex_obj
== NULL
|| blit
->temp_tex_obj
== texObj
);
849 /* Restore texture object state, the texture binding will be restored by
850 * _mesa_meta_end(). If the texture is the temporary texture that is about
851 * to be destroyed, don't bother restoring its state.
853 if (blit
->temp_tex_obj
== NULL
) {
854 /* If the target restricts values for base level or max level, we assume
855 * that the original values were valid.
857 if (blit
->baseLevelSave
!= texObj
->BaseLevel
)
858 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_BASE_LEVEL
,
859 &blit
->baseLevelSave
, false);
861 if (blit
->maxLevelSave
!= texObj
->MaxLevel
)
862 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_MAX_LEVEL
,
863 &blit
->maxLevelSave
, false);
865 /* If ARB_stencil_texturing is not supported, the mode won't have changed. */
866 if (texObj
->StencilSampling
!= blit
->stencilSamplingSave
) {
867 /* GLint so the compiler won't complain about type signedness mismatch
868 * in the call to _mesa_texture_parameteriv below.
870 const GLint param
= blit
->stencilSamplingSave
?
871 GL_STENCIL_INDEX
: GL_DEPTH_COMPONENT
;
873 _mesa_texture_parameteriv(ctx
, texObj
, GL_DEPTH_STENCIL_TEXTURE_MODE
,
878 _mesa_bind_sampler(ctx
, ctx
->Texture
.CurrentUnit
, blit
->samp_obj_save
);
879 _mesa_reference_sampler_object(ctx
, &blit
->samp_obj_save
, NULL
);
880 _mesa_reference_sampler_object(ctx
, &blit
->samp_obj
, NULL
);
881 _mesa_delete_nameless_texture(ctx
, blit
->temp_tex_obj
);
884 struct gl_texture_object
*
885 _mesa_meta_texture_object_from_renderbuffer(struct gl_context
*ctx
,
886 struct gl_renderbuffer
*rb
)
888 struct gl_texture_image
*texImage
;
889 struct gl_texture_object
*texObj
;
890 const GLenum target
= rb
->NumSamples
> 1
891 ? GL_TEXTURE_2D_MULTISAMPLE
: GL_TEXTURE_2D
;
893 texObj
= ctx
->Driver
.NewTextureObject(ctx
, 0xDEADBEEF, target
);
894 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
896 if (!ctx
->Driver
.BindRenderbufferTexImage(ctx
, rb
, texImage
)) {
897 _mesa_delete_nameless_texture(ctx
, texObj
);
901 if (ctx
->Driver
.FinishRenderTexture
&& !rb
->NeedsFinishRenderTexture
) {
902 rb
->NeedsFinishRenderTexture
= true;
903 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
909 struct gl_sampler_object
*
910 _mesa_meta_setup_sampler(struct gl_context
*ctx
,
911 struct gl_texture_object
*texObj
,
912 GLenum target
, GLenum filter
, GLuint srcLevel
)
914 struct gl_sampler_object
*samp_obj
;
915 GLenum tex_filter
= (filter
== GL_SCALED_RESOLVE_FASTEST_EXT
||
916 filter
== GL_SCALED_RESOLVE_NICEST_EXT
) ?
919 samp_obj
= ctx
->Driver
.NewSamplerObject(ctx
, 0xDEADBEEF);
920 if (samp_obj
== NULL
)
923 _mesa_bind_sampler(ctx
, ctx
->Texture
.CurrentUnit
, samp_obj
);
924 _mesa_set_sampler_filters(ctx
, samp_obj
, tex_filter
, tex_filter
);
925 _mesa_set_sampler_wrap(ctx
, samp_obj
, GL_CLAMP_TO_EDGE
, GL_CLAMP_TO_EDGE
,
928 /* Prepare src texture state */
929 _mesa_bind_texture(ctx
, target
, texObj
);
930 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
931 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_BASE_LEVEL
,
932 (GLint
*) &srcLevel
, false);
933 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_MAX_LEVEL
,
934 (GLint
*) &srcLevel
, false);
941 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
942 * of texture mapping and polygon rendering.
945 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
946 const struct gl_framebuffer
*readFb
,
947 const struct gl_framebuffer
*drawFb
,
948 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
949 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
950 GLbitfield mask
, GLenum filter
)
952 const GLint dstW
= abs(dstX1
- dstX0
);
953 const GLint dstH
= abs(dstY1
- dstY0
);
954 const GLint dstFlipX
= (dstX1
- dstX0
) / dstW
;
955 const GLint dstFlipY
= (dstY1
- dstY0
) / dstH
;
958 GLint srcX0
, srcY0
, srcX1
, srcY1
;
959 GLint dstX0
, dstY0
, dstX1
, dstY1
;
961 srcX0
, srcY0
, srcX1
, srcY1
,
962 dstX0
, dstY0
, dstX1
, dstY1
965 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
966 ctx
->Extensions
.ARB_fragment_shader
;
968 /* Multisample texture blit support requires texture multisample. */
969 if (readFb
->Visual
.samples
> 0 &&
970 !ctx
->Extensions
.ARB_texture_multisample
) {
974 /* Clip a copy of the blit coordinates. If these differ from the input
975 * coordinates, then we'll set the scissor.
977 if (!_mesa_clip_blit(ctx
, readFb
, drawFb
,
978 &clip
.srcX0
, &clip
.srcY0
, &clip
.srcX1
, &clip
.srcY1
,
979 &clip
.dstX0
, &clip
.dstY0
, &clip
.dstX1
, &clip
.dstY1
)) {
980 /* clipped/scissored everything away */
984 /* Only scissor and FRAMEBUFFER_SRGB affect blit. Leave sRGB alone, but
985 * save restore scissor as we'll set a custom scissor if necessary.
987 _mesa_meta_begin(ctx
, MESA_META_ALL
&
988 ~(MESA_META_DRAW_BUFFERS
|
989 MESA_META_FRAMEBUFFER_SRGB
));
991 /* Dithering shouldn't be performed for glBlitFramebuffer */
992 _mesa_set_enable(ctx
, GL_DITHER
, GL_FALSE
);
994 /* If the clipping earlier changed the destination rect at all, then
995 * enable the scissor to clip to it.
997 if (clip
.dstX0
!= dstX0
|| clip
.dstY0
!= dstY0
||
998 clip
.dstX1
!= dstX1
|| clip
.dstY1
!= dstY1
) {
999 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_TRUE
);
1000 _mesa_Scissor(MIN2(clip
.dstX0
, clip
.dstX1
),
1001 MIN2(clip
.dstY0
, clip
.dstY1
),
1002 abs(clip
.dstX0
- clip
.dstX1
),
1003 abs(clip
.dstY0
- clip
.dstY1
));
1006 /* Try faster, direct texture approach first */
1007 if (mask
& GL_COLOR_BUFFER_BIT
) {
1008 if (blitframebuffer_texture(ctx
, readFb
, drawFb
,
1009 srcX0
, srcY0
, srcX1
, srcY1
,
1010 dstX0
, dstY0
, dstX1
, dstY1
,
1011 filter
, dstFlipX
, dstFlipY
,
1012 use_glsl_version
, false)) {
1013 mask
&= ~GL_COLOR_BUFFER_BIT
;
1017 if (mask
& GL_DEPTH_BUFFER_BIT
&& use_glsl_version
) {
1018 if (blitframebuffer_texture(ctx
, readFb
, drawFb
,
1019 srcX0
, srcY0
, srcX1
, srcY1
,
1020 dstX0
, dstY0
, dstX1
, dstY1
,
1021 filter
, dstFlipX
, dstFlipY
,
1022 use_glsl_version
, true)) {
1023 mask
&= ~GL_DEPTH_BUFFER_BIT
;
1027 if (mask
& GL_STENCIL_BUFFER_BIT
) {
1028 /* XXX can't easily do stencil */
1031 _mesa_meta_end(ctx
);
1037 _mesa_meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
)
1040 _mesa_DeleteVertexArrays(1, &blit
->VAO
);
1042 _mesa_reference_buffer_object(ctx
, &blit
->buf_obj
, NULL
);
1045 _mesa_meta_blit_shader_table_cleanup(ctx
, &blit
->shaders_with_depth
);
1046 _mesa_meta_blit_shader_table_cleanup(ctx
, &blit
->shaders_without_depth
);
1048 if (blit
->depthTex
.tex_obj
!= NULL
) {
1049 _mesa_delete_nameless_texture(ctx
, blit
->depthTex
.tex_obj
);
1050 blit
->depthTex
.tex_obj
= NULL
;
1055 _mesa_meta_and_swrast_BlitFramebuffer(struct gl_context
*ctx
,
1056 struct gl_framebuffer
*readFb
,
1057 struct gl_framebuffer
*drawFb
,
1058 GLint srcX0
, GLint srcY0
,
1059 GLint srcX1
, GLint srcY1
,
1060 GLint dstX0
, GLint dstY0
,
1061 GLint dstX1
, GLint dstY1
,
1062 GLbitfield mask
, GLenum filter
)
1064 mask
= _mesa_meta_BlitFramebuffer(ctx
, readFb
, drawFb
,
1065 srcX0
, srcY0
, srcX1
, srcY1
,
1066 dstX0
, dstY0
, dstX1
, dstY1
,
1071 _swrast_BlitFramebuffer(ctx
, readFb
, drawFb
,
1072 srcX0
, srcY0
, srcX1
, srcY1
,
1073 dstX0
, dstY0
, dstX1
, dstY1
,