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 *vs_source
;
75 const char *sampler_array_suffix
= "";
76 const char *texcoord_type
= "vec2";
78 enum blit_msaa_shader shader_index
;
81 samples
= MAX2(src_rb
->NumSamples
, 1);
82 y_scale
= samples
* 0.5;
84 /* We expect only power of 2 samples in source multisample buffer. */
85 assert(samples
> 0 && is_power_of_two(samples
));
86 while (samples
>> (shader_offset
+ 1)) {
89 /* Update the assert if we plan to support more than 8X MSAA. */
90 assert(shader_offset
> 0 && shader_offset
< 4);
92 assert(target
== GL_TEXTURE_2D_MULTISAMPLE
||
93 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
);
95 shader_index
= BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_SCALED_RESOLVE
+
98 if (target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
) {
99 shader_index
+= BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_SCALED_RESOLVE
-
100 BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_SCALED_RESOLVE
;
101 sampler_array_suffix
= "Array";
102 texcoord_type
= "vec3";
105 if (blit
->msaa_shaders
[shader_index
]) {
106 _mesa_UseProgram(blit
->msaa_shaders
[shader_index
]);
107 /* Update the uniform values. */
109 _mesa_GetUniformLocation(blit
->msaa_shaders
[shader_index
], "src_width");
111 _mesa_GetUniformLocation(blit
->msaa_shaders
[shader_index
], "src_height");
112 _mesa_Uniform1f(loc_src_width
, src_rb
->Width
);
113 _mesa_Uniform1f(loc_src_height
, src_rb
->Height
);
117 name
= ralloc_asprintf(mem_ctx
, "vec4 MSAA scaled resolve");
119 /* Below switch is used to setup the shader expression, which computes
120 * sample index and map it to to a sample number on hardware.
124 sample_number
= "sample_map[int(2 * fract(coord.x))]";
125 sample_map
= ctx
->Const
.SampleMap2x
;
128 sample_number
= "sample_map[int(2 * fract(coord.x) + 4 * fract(coord.y))]";
129 sample_map
= ctx
->Const
.SampleMap4x
;
132 sample_number
= "sample_map[int(2 * fract(coord.x) + 8 * fract(coord.y))]";
133 sample_map
= ctx
->Const
.SampleMap8x
;
136 sample_number
= NULL
;
138 _mesa_problem(ctx
, "Unsupported sample count %d\n", samples
);
139 unreachable("Unsupported sample count");
142 /* Create sample map string. */
143 for (i
= 0 ; i
< samples
- 1; i
++) {
144 ralloc_asprintf_append(&sample_map_str
, "%d, ", sample_map
[i
]);
146 ralloc_asprintf_append(&sample_map_str
, "%d", sample_map
[samples
- 1]);
148 /* Create sample map expression using above string. */
149 ralloc_asprintf_append(&sample_map_expr
,
150 " const int sample_map[%d] = int[%d](%s);\n",
151 samples
, samples
, sample_map_str
);
153 ralloc_asprintf_append(&texel_fetch_macro
,
154 "#define TEXEL_FETCH(coord) texelFetch(texSampler, i%s(coord), %s);\n",
155 texcoord_type
, sample_number
);
157 vs_source
= ralloc_asprintf(mem_ctx
,
159 "in vec2 position;\n"
160 "in %s textureCoords;\n"
161 "out %s texCoords;\n"
164 " texCoords = textureCoords;\n"
165 " gl_Position = vec4(position, 0.0, 1.0);\n"
169 fs_source
= ralloc_asprintf(mem_ctx
,
171 "#extension GL_ARB_texture_multisample : enable\n"
172 "uniform sampler2DMS%s texSampler;\n"
173 "uniform float src_width, src_height;\n"
175 "out vec4 out_color;\n"
181 " const vec2 scale = vec2(2.0f, %ff);\n"
182 " const vec2 scale_inv = vec2(0.5f, %ff);\n"
183 " const vec2 s_0_offset = vec2(0.25f, %ff);\n"
184 " vec2 s_0_coord, s_1_coord, s_2_coord, s_3_coord;\n"
185 " vec4 s_0_color, s_1_color, s_2_color, s_3_color;\n"
186 " vec4 x_0_color, x_1_color;\n"
187 " vec2 tex_coord = texCoords - s_0_offset;\n"
189 " tex_coord *= scale;\n"
190 " clamp(tex_coord.x, 0.0f, scale.x * src_width - 1.0f);\n"
191 " clamp(tex_coord.y, 0.0f, scale.y * src_height - 1.0f);\n"
192 " interp = fract(tex_coord);\n"
193 " tex_coord = ivec2(tex_coord) * scale_inv;\n"
195 " /* Compute the sample coordinates used for filtering. */\n"
196 " s_0_coord = tex_coord;\n"
197 " s_1_coord = tex_coord + vec2(scale_inv.x, 0.0f);\n"
198 " s_2_coord = tex_coord + vec2(0.0f, scale_inv.y);\n"
199 " s_3_coord = tex_coord + vec2(scale_inv.x, scale_inv.y);\n"
201 " /* Fetch sample color values. */\n"
203 " s_0_color = TEXEL_FETCH(s_0_coord)\n"
204 " s_1_color = TEXEL_FETCH(s_1_coord)\n"
205 " s_2_color = TEXEL_FETCH(s_2_coord)\n"
206 " s_3_color = TEXEL_FETCH(s_3_coord)\n"
207 "#undef TEXEL_FETCH\n"
209 " /* Do bilinear filtering on sample colors. */\n"
210 " x_0_color = mix(s_0_color, s_1_color, interp.x);\n"
211 " x_1_color = mix(s_2_color, s_3_color, interp.x);\n"
212 " out_color = mix(x_0_color, x_1_color, interp.y);\n"
214 sampler_array_suffix
,
222 _mesa_meta_compile_and_link_program(ctx
, vs_source
, fs_source
, name
,
223 &blit
->msaa_shaders
[shader_index
]);
225 _mesa_GetUniformLocation(blit
->msaa_shaders
[shader_index
], "src_width");
227 _mesa_GetUniformLocation(blit
->msaa_shaders
[shader_index
], "src_height");
228 _mesa_Uniform1f(loc_src_width
, src_rb
->Width
);
229 _mesa_Uniform1f(loc_src_height
, src_rb
->Height
);
231 ralloc_free(mem_ctx
);
235 setup_glsl_msaa_blit_shader(struct gl_context
*ctx
,
236 struct blit_state
*blit
,
237 const struct gl_framebuffer
*drawFb
,
238 struct gl_renderbuffer
*src_rb
,
241 const char *vs_source
;
244 enum blit_msaa_shader shader_index
;
245 bool dst_is_msaa
= false;
247 const char *vec4_prefix
;
248 const char *sampler_array_suffix
= "";
250 const char *texcoord_type
= "vec2";
252 int shader_offset
= 0;
255 samples
= MAX2(src_rb
->NumSamples
, 1);
256 src_datatype
= _mesa_get_format_datatype(src_rb
->Format
);
258 /* depth-or-color glCopyTexImage fallback path that passes a NULL rb and
259 * doesn't handle integer.
262 src_datatype
= GL_UNSIGNED_NORMALIZED
;
265 /* We expect only power of 2 samples in source multisample buffer. */
266 assert(samples
> 0 && is_power_of_two(samples
));
267 while (samples
>> (shader_offset
+ 1)) {
270 /* Update the assert if we plan to support more than 16X MSAA. */
271 assert(shader_offset
>= 0 && shader_offset
<= 4);
273 if (drawFb
->Visual
.samples
> 1) {
274 /* If you're calling meta_BlitFramebuffer with the destination
275 * multisampled, this is the only path that will work -- swrast and
276 * CopyTexImage won't work on it either.
278 assert(ctx
->Extensions
.ARB_sample_shading
);
282 /* We need shader invocation per sample, not per pixel */
283 _mesa_set_enable(ctx
, GL_MULTISAMPLE
, GL_TRUE
);
284 _mesa_set_enable(ctx
, GL_SAMPLE_SHADING
, GL_TRUE
);
285 _mesa_MinSampleShading(1.0);
289 case GL_TEXTURE_2D_MULTISAMPLE
:
290 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
291 if (src_rb
&& (src_rb
->_BaseFormat
== GL_DEPTH_COMPONENT
||
292 src_rb
->_BaseFormat
== GL_DEPTH_STENCIL
)) {
294 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY
;
296 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE
;
299 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY
;
301 shader_index
= BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+
306 if (target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
) {
307 shader_index
+= (BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_RESOLVE
-
308 BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
);
309 sampler_array_suffix
= "Array";
310 texcoord_type
= "vec3";
314 _mesa_problem(ctx
, "Unkown texture target %s\n",
315 _mesa_lookup_enum_by_nr(target
));
316 shader_index
= BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
;
319 /* We rely on the enum being sorted this way. */
320 STATIC_ASSERT(BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_INT
==
321 BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+ 5);
322 STATIC_ASSERT(BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_UINT
==
323 BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+ 10);
324 if (src_datatype
== GL_INT
) {
327 } else if (src_datatype
== GL_UNSIGNED_INT
) {
334 if (blit
->msaa_shaders
[shader_index
]) {
335 _mesa_UseProgram(blit
->msaa_shaders
[shader_index
]);
339 mem_ctx
= ralloc_context(NULL
);
341 if (shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE
||
342 shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_DEPTH_RESOLVE
||
343 shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_DEPTH_COPY
||
344 shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY
) {
346 const char *arb_sample_shading_extension_string
;
349 arb_sample_shading_extension_string
= "#extension GL_ARB_sample_shading : enable";
350 sample_index
= "gl_SampleID";
351 name
= "depth MSAA copy";
353 /* Don't need that extension, since we're drawing to a single-sampled
356 arb_sample_shading_extension_string
= "";
357 /* From the GL 4.3 spec:
359 * "If there is a multisample buffer (the value of SAMPLE_BUFFERS
360 * is one), then values are obtained from the depth samples in
361 * this buffer. It is recommended that the depth value of the
362 * centermost sample be used, though implementations may choose
363 * any function of the depth sample values at each pixel.
365 * We're slacking and instead of choosing centermost, we've got 0.
368 name
= "depth MSAA resolve";
371 vs_source
= ralloc_asprintf(mem_ctx
,
373 "in vec2 position;\n"
374 "in %s textureCoords;\n"
375 "out %s texCoords;\n"
378 " texCoords = textureCoords;\n"
379 " gl_Position = vec4(position, 0.0, 1.0);\n"
383 fs_source
= ralloc_asprintf(mem_ctx
,
385 "#extension GL_ARB_texture_multisample : enable\n"
387 "uniform sampler2DMS%s texSampler;\n"
389 "out vec4 out_color;\n"
393 " gl_FragDepth = texelFetch(texSampler, i%s(texCoords), %s).r;\n"
395 arb_sample_shading_extension_string
,
396 sampler_array_suffix
,
401 /* You can create 2D_MULTISAMPLE textures with 0 sample count (meaning 1
402 * sample). Yes, this is ridiculous.
404 char *sample_resolve
;
405 const char *arb_sample_shading_extension_string
;
406 const char *merge_function
;
407 name
= ralloc_asprintf(mem_ctx
, "%svec4 MSAA %s",
409 dst_is_msaa
? "copy" : "resolve");
412 arb_sample_shading_extension_string
= "#extension GL_ARB_sample_shading : enable";
413 sample_resolve
= ralloc_asprintf(mem_ctx
, " out_color = texelFetch(texSampler, i%s(texCoords), gl_SampleID);", texcoord_type
);
419 if (src_datatype
== GL_INT
|| src_datatype
== GL_UNSIGNED_INT
) {
421 "gvec4 merge(gvec4 a, gvec4 b) { return (a >> gvec4(1)) + (b >> gvec4(1)) + (a & b & gvec4(1)); }\n";
423 /* The divide will happen at the end for floats. */
425 "vec4 merge(vec4 a, vec4 b) { return (a + b); }\n";
428 arb_sample_shading_extension_string
= "";
430 /* We're assuming power of two samples for this resolution procedure.
432 * To avoid losing any floating point precision if the samples all
433 * happen to have the same value, we merge pairs of values at a time
434 * (so the floating point exponent just gets increased), rather than
435 * doing a naive sum and dividing.
437 assert(is_power_of_two(samples
));
438 /* Fetch each individual sample. */
439 sample_resolve
= rzalloc_size(mem_ctx
, 1);
440 for (i
= 0; i
< samples
; i
++) {
441 ralloc_asprintf_append(&sample_resolve
,
442 " gvec4 sample_1_%d = texelFetch(texSampler, i%s(texCoords), %d);\n",
443 i
, texcoord_type
, i
);
445 /* Now, merge each pair of samples, then merge each pair of those,
448 for (step
= 2; step
<= samples
; step
*= 2) {
449 for (i
= 0; i
< samples
; i
+= step
) {
450 ralloc_asprintf_append(&sample_resolve
,
451 " gvec4 sample_%d_%d = merge(sample_%d_%d, sample_%d_%d);\n",
454 step
/ 2, i
+ step
/ 2);
458 /* Scale the final result. */
459 if (src_datatype
== GL_UNSIGNED_INT
|| src_datatype
== GL_INT
) {
460 ralloc_asprintf_append(&sample_resolve
,
461 " out_color = sample_%d_0;\n",
464 ralloc_asprintf_append(&sample_resolve
,
465 " gl_FragColor = sample_%d_0 / %f;\n",
466 samples
, (float)samples
);
470 vs_source
= ralloc_asprintf(mem_ctx
,
472 "in vec2 position;\n"
473 "in %s textureCoords;\n"
474 "out %s texCoords;\n"
477 " texCoords = textureCoords;\n"
478 " gl_Position = vec4(position, 0.0, 1.0);\n"
482 fs_source
= ralloc_asprintf(mem_ctx
,
484 "#extension GL_ARB_texture_multisample : enable\n"
486 "#define gvec4 %svec4\n"
487 "uniform %ssampler2DMS%s texSampler;\n"
489 "out gvec4 out_color;\n"
491 "%s" /* merge_function */
494 "%s\n" /* sample_resolve */
496 arb_sample_shading_extension_string
,
499 sampler_array_suffix
,
505 _mesa_meta_compile_and_link_program(ctx
, vs_source
, fs_source
, name
,
506 &blit
->msaa_shaders
[shader_index
]);
508 ralloc_free(mem_ctx
);
512 setup_glsl_blit_framebuffer(struct gl_context
*ctx
,
513 struct blit_state
*blit
,
514 const struct gl_framebuffer
*drawFb
,
515 struct gl_renderbuffer
*src_rb
,
516 GLenum target
, GLenum filter
,
520 unsigned texcoord_size
;
521 bool is_target_multisample
= target
== GL_TEXTURE_2D_MULTISAMPLE
||
522 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
;
523 bool is_filter_scaled_resolve
= filter
== GL_SCALED_RESOLVE_FASTEST_EXT
||
524 filter
== GL_SCALED_RESOLVE_NICEST_EXT
;
526 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
527 assert(_mesa_is_desktop_gl(ctx
) || target
== GL_TEXTURE_2D
);
529 texcoord_size
= 2 + (src_rb
->Depth
> 1 ? 1 : 0);
531 _mesa_meta_setup_vertex_objects(&blit
->VAO
, &blit
->VBO
, true,
532 2, texcoord_size
, 0);
534 if (is_target_multisample
&& is_filter_scaled_resolve
&& is_scaled_blit
) {
535 setup_glsl_msaa_blit_scaled_shader(ctx
, blit
, src_rb
, target
, filter
);
536 } else if (is_target_multisample
) {
537 setup_glsl_msaa_blit_shader(ctx
, blit
, drawFb
, src_rb
, target
);
539 _mesa_meta_setup_blit_shader(ctx
, target
, do_depth
,
540 do_depth
? &blit
->shaders_with_depth
541 : &blit
->shaders_without_depth
);
546 * Try to do a color or depth glBlitFramebuffer using texturing.
548 * We can do this when the src renderbuffer is actually a texture, or when the
549 * driver exposes BindRenderbufferTexImage().
552 blitframebuffer_texture(struct gl_context
*ctx
,
553 const struct gl_framebuffer
*readFb
,
554 const struct gl_framebuffer
*drawFb
,
555 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
556 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
557 GLenum filter
, GLint flipX
, GLint flipY
,
558 GLboolean glsl_version
, GLboolean do_depth
)
560 int att_index
= do_depth
? BUFFER_DEPTH
: readFb
->_ColorReadBufferIndex
;
561 const struct gl_renderbuffer_attachment
*readAtt
=
562 &readFb
->Attachment
[att_index
];
563 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
564 struct fb_tex_blit_state fb_tex_blit
;
565 const GLint dstX
= MIN2(dstX0
, dstX1
);
566 const GLint dstY
= MIN2(dstY0
, dstY1
);
567 const GLint dstW
= abs(dstX1
- dstX0
);
568 const GLint dstH
= abs(dstY1
- dstY0
);
569 const int srcW
= abs(srcX1
- srcX0
);
570 const int srcH
= abs(srcY1
- srcY0
);
571 bool scaled_blit
= false;
572 struct gl_texture_object
*texObj
;
575 struct gl_renderbuffer
*rb
= readAtt
->Renderbuffer
;
576 struct temp_texture
*meta_temp_texture
;
578 if (rb
->NumSamples
&& !ctx
->Extensions
.ARB_texture_multisample
)
581 _mesa_meta_fb_tex_blit_begin(ctx
, &fb_tex_blit
);
583 if (readAtt
->Texture
&&
584 (readAtt
->Texture
->Target
== GL_TEXTURE_2D
||
585 readAtt
->Texture
->Target
== GL_TEXTURE_RECTANGLE
||
586 readAtt
->Texture
->Target
== GL_TEXTURE_2D_MULTISAMPLE
||
587 readAtt
->Texture
->Target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
)) {
588 /* If there's a texture attached of a type we can handle, then just use
591 srcLevel
= readAtt
->TextureLevel
;
592 texObj
= readAtt
->Texture
;
593 target
= texObj
->Target
;
594 } else if (!readAtt
->Texture
&& ctx
->Driver
.BindRenderbufferTexImage
) {
595 if (!_mesa_meta_bind_rb_as_tex_image(ctx
, rb
, &fb_tex_blit
.tempTex
,
600 if (_mesa_is_winsys_fbo(readFb
)) {
602 srcY0
= rb
->Height
- srcY1
;
603 srcY1
= rb
->Height
- temp
;
607 GLenum tex_base_format
;
608 /* Fall back to doing a CopyTexSubImage to get the destination
609 * renderbuffer into a texture.
611 if (ctx
->Meta
->Blit
.no_ctsi_fallback
)
614 if (rb
->NumSamples
> 1)
618 meta_temp_texture
= _mesa_meta_get_temp_depth_texture(ctx
);
619 tex_base_format
= GL_DEPTH_COMPONENT
;
621 meta_temp_texture
= _mesa_meta_get_temp_texture(ctx
);
623 _mesa_base_tex_format(ctx
, rb
->InternalFormat
);
627 target
= meta_temp_texture
->Target
;
628 texObj
= _mesa_lookup_texture(ctx
, meta_temp_texture
->TexObj
);
629 if (texObj
== NULL
) {
633 _mesa_meta_setup_copypix_texture(ctx
, meta_temp_texture
,
646 fb_tex_blit
.baseLevelSave
= texObj
->BaseLevel
;
647 fb_tex_blit
.maxLevelSave
= texObj
->MaxLevel
;
648 fb_tex_blit
.stencilSamplingSave
= texObj
->StencilSampling
;
650 scaled_blit
= dstW
!= srcW
|| dstH
!= srcH
;
653 setup_glsl_blit_framebuffer(ctx
, blit
, drawFb
, rb
, target
, filter
, scaled_blit
,
657 _mesa_meta_setup_ff_tnl_for_blit(&ctx
->Meta
->Blit
.VAO
,
658 &ctx
->Meta
->Blit
.VBO
,
663 printf("Blit from texture!\n");
664 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
665 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
668 fb_tex_blit
.sampler
= _mesa_meta_setup_sampler(ctx
, texObj
, target
, filter
,
671 /* Always do our blits with no net sRGB decode or encode.
673 * However, if both the src and dst can be srgb decode/encoded, enable them
674 * so that we do any blending (from scaling or from MSAA resolves) in the
677 * Our choice of not doing any net encode/decode is from the GL 3.0
680 * "Blit operations bypass the fragment pipeline. The only fragment
681 * operations which affect a blit are the pixel ownership test and the
684 * The GL 4.4 specification disagrees and says that the sRGB part of the
685 * fragment pipeline applies, but this was found to break applications.
687 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
688 if (_mesa_get_format_color_encoding(rb
->Format
) == GL_SRGB
&&
689 drawFb
->Visual
.sRGBCapable
) {
690 _mesa_SamplerParameteri(fb_tex_blit
.sampler
,
691 GL_TEXTURE_SRGB_DECODE_EXT
, GL_DECODE_EXT
);
692 _mesa_set_framebuffer_srgb(ctx
, GL_TRUE
);
694 _mesa_SamplerParameteri(fb_tex_blit
.sampler
,
695 GL_TEXTURE_SRGB_DECODE_EXT
,
697 /* set_framebuffer_srgb was set by _mesa_meta_begin(). */
702 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
703 _mesa_set_enable(ctx
, target
, GL_TRUE
);
706 /* Prepare vertex data (the VBO was previously created and bound) */
708 struct vertex verts
[4];
709 GLfloat s0
, t0
, s1
, t1
;
711 if (target
== GL_TEXTURE_2D
) {
712 const struct gl_texture_image
*texImage
713 = _mesa_select_tex_image(texObj
, target
, srcLevel
);
714 s0
= srcX0
/ (float) texImage
->Width
;
715 s1
= srcX1
/ (float) texImage
->Width
;
716 t0
= srcY0
/ (float) texImage
->Height
;
717 t1
= srcY1
/ (float) texImage
->Height
;
720 assert(target
== GL_TEXTURE_RECTANGLE_ARB
||
721 target
== GL_TEXTURE_2D_MULTISAMPLE
||
722 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
);
729 /* Silence valgrind warnings about reading uninitialized stack. */
730 memset(verts
, 0, sizeof(verts
));
732 /* setup vertex positions */
733 verts
[0].x
= -1.0F
* flipX
;
734 verts
[0].y
= -1.0F
* flipY
;
735 verts
[1].x
= 1.0F
* flipX
;
736 verts
[1].y
= -1.0F
* flipY
;
737 verts
[2].x
= 1.0F
* flipX
;
738 verts
[2].y
= 1.0F
* flipY
;
739 verts
[3].x
= -1.0F
* flipX
;
740 verts
[3].y
= 1.0F
* flipY
;
742 verts
[0].tex
[0] = s0
;
743 verts
[0].tex
[1] = t0
;
744 verts
[0].tex
[2] = readAtt
->Zoffset
;
745 verts
[1].tex
[0] = s1
;
746 verts
[1].tex
[1] = t0
;
747 verts
[1].tex
[2] = readAtt
->Zoffset
;
748 verts
[2].tex
[0] = s1
;
749 verts
[2].tex
[1] = t1
;
750 verts
[2].tex
[2] = readAtt
->Zoffset
;
751 verts
[3].tex
[0] = s0
;
752 verts
[3].tex
[1] = t1
;
753 verts
[3].tex
[2] = readAtt
->Zoffset
;
755 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
759 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
760 _mesa_ColorMask(!do_depth
, !do_depth
, !do_depth
, !do_depth
);
761 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, do_depth
);
762 _mesa_DepthMask(do_depth
);
763 _mesa_DepthFunc(GL_ALWAYS
);
765 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
766 _mesa_meta_fb_tex_blit_end(ctx
, target
, &fb_tex_blit
);
772 _mesa_meta_fb_tex_blit_begin(const struct gl_context
*ctx
,
773 struct fb_tex_blit_state
*blit
)
776 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
777 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
782 _mesa_meta_fb_tex_blit_end(struct gl_context
*ctx
, GLenum target
,
783 struct fb_tex_blit_state
*blit
)
785 /* Restore texture object state, the texture binding will
786 * be restored by _mesa_meta_end().
788 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
789 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, blit
->baseLevelSave
);
790 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, blit
->maxLevelSave
);
792 if (ctx
->Extensions
.ARB_stencil_texturing
) {
793 const struct gl_texture_object
*texObj
=
794 _mesa_get_current_tex_object(ctx
, target
);
796 if (texObj
->StencilSampling
!= blit
->stencilSamplingSave
)
797 _mesa_TexParameteri(target
, GL_DEPTH_STENCIL_TEXTURE_MODE
,
798 blit
->stencilSamplingSave
?
799 GL_STENCIL_INDEX
: GL_DEPTH_COMPONENT
);
803 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, blit
->samplerSave
);
804 _mesa_DeleteSamplers(1, &blit
->sampler
);
806 _mesa_DeleteTextures(1, &blit
->tempTex
);
810 _mesa_meta_bind_rb_as_tex_image(struct gl_context
*ctx
,
811 struct gl_renderbuffer
*rb
,
813 struct gl_texture_object
**texObj
,
816 struct gl_texture_image
*texImage
;
819 if (rb
->NumSamples
> 1)
820 *target
= GL_TEXTURE_2D_MULTISAMPLE
;
822 *target
= GL_TEXTURE_2D
;
825 _mesa_GenTextures(1, &tempTex
);
831 _mesa_BindTexture(*target
, *tex
);
832 *texObj
= _mesa_lookup_texture(ctx
, *tex
);
833 texImage
= _mesa_get_tex_image(ctx
, *texObj
, *target
, 0);
835 if (!ctx
->Driver
.BindRenderbufferTexImage(ctx
, rb
, texImage
)) {
836 _mesa_DeleteTextures(1, tex
);
840 if (ctx
->Driver
.FinishRenderTexture
&& !rb
->NeedsFinishRenderTexture
) {
841 rb
->NeedsFinishRenderTexture
= true;
842 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
849 _mesa_meta_setup_sampler(struct gl_context
*ctx
,
850 const struct gl_texture_object
*texObj
,
851 GLenum target
, GLenum filter
, GLuint srcLevel
)
854 GLenum tex_filter
= (filter
== GL_SCALED_RESOLVE_FASTEST_EXT
||
855 filter
== GL_SCALED_RESOLVE_NICEST_EXT
) ?
858 _mesa_GenSamplers(1, &sampler
);
859 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, sampler
);
861 /* Prepare src texture state */
862 _mesa_BindTexture(target
, texObj
->Name
);
863 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MIN_FILTER
, tex_filter
);
864 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MAG_FILTER
, tex_filter
);
865 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
866 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
867 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
869 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
870 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
876 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
877 * of texture mapping and polygon rendering.
880 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
881 const struct gl_framebuffer
*readFb
,
882 const struct gl_framebuffer
*drawFb
,
883 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
884 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
885 GLbitfield mask
, GLenum filter
)
887 const GLint dstW
= abs(dstX1
- dstX0
);
888 const GLint dstH
= abs(dstY1
- dstY0
);
889 const GLint dstFlipX
= (dstX1
- dstX0
) / dstW
;
890 const GLint dstFlipY
= (dstY1
- dstY0
) / dstH
;
893 GLint srcX0
, srcY0
, srcX1
, srcY1
;
894 GLint dstX0
, dstY0
, dstX1
, dstY1
;
896 srcX0
, srcY0
, srcX1
, srcY1
,
897 dstX0
, dstY0
, dstX1
, dstY1
900 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
901 ctx
->Extensions
.ARB_fragment_shader
;
903 /* Multisample texture blit support requires texture multisample. */
904 if (readFb
->Visual
.samples
> 0 &&
905 !ctx
->Extensions
.ARB_texture_multisample
) {
909 /* Clip a copy of the blit coordinates. If these differ from the input
910 * coordinates, then we'll set the scissor.
912 if (!_mesa_clip_blit(ctx
, readFb
, drawFb
,
913 &clip
.srcX0
, &clip
.srcY0
, &clip
.srcX1
, &clip
.srcY1
,
914 &clip
.dstX0
, &clip
.dstY0
, &clip
.dstX1
, &clip
.dstY1
)) {
915 /* clipped/scissored everything away */
919 /* Only scissor affects blit, but we're doing to set a custom scissor if
920 * necessary anyway, so save/clear state.
922 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_DRAW_BUFFERS
);
924 /* Dithering shouldn't be performed for glBlitFramebuffer */
925 _mesa_set_enable(ctx
, GL_DITHER
, GL_FALSE
);
927 /* If the clipping earlier changed the destination rect at all, then
928 * enable the scissor to clip to it.
930 if (clip
.dstX0
!= dstX0
|| clip
.dstY0
!= dstY0
||
931 clip
.dstX1
!= dstX1
|| clip
.dstY1
!= dstY1
) {
932 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_TRUE
);
933 _mesa_Scissor(MIN2(clip
.dstX0
, clip
.dstX1
),
934 MIN2(clip
.dstY0
, clip
.dstY1
),
935 abs(clip
.dstX0
- clip
.dstX1
),
936 abs(clip
.dstY0
- clip
.dstY1
));
939 /* Try faster, direct texture approach first */
940 if (mask
& GL_COLOR_BUFFER_BIT
) {
941 if (blitframebuffer_texture(ctx
, readFb
, drawFb
,
942 srcX0
, srcY0
, srcX1
, srcY1
,
943 dstX0
, dstY0
, dstX1
, dstY1
,
944 filter
, dstFlipX
, dstFlipY
,
945 use_glsl_version
, false)) {
946 mask
&= ~GL_COLOR_BUFFER_BIT
;
950 if (mask
& GL_DEPTH_BUFFER_BIT
&& use_glsl_version
) {
951 if (blitframebuffer_texture(ctx
, readFb
, drawFb
,
952 srcX0
, srcY0
, srcX1
, srcY1
,
953 dstX0
, dstY0
, dstX1
, dstY1
,
954 filter
, dstFlipX
, dstFlipY
,
955 use_glsl_version
, true)) {
956 mask
&= ~GL_DEPTH_BUFFER_BIT
;
960 if (mask
& GL_STENCIL_BUFFER_BIT
) {
961 /* XXX can't easily do stencil */
970 _mesa_meta_glsl_blit_cleanup(struct blit_state
*blit
)
973 _mesa_DeleteVertexArrays(1, &blit
->VAO
);
975 _mesa_DeleteBuffers(1, &blit
->VBO
);
979 _mesa_meta_blit_shader_table_cleanup(&blit
->shaders_with_depth
);
980 _mesa_meta_blit_shader_table_cleanup(&blit
->shaders_without_depth
);
982 _mesa_DeleteTextures(1, &blit
->depthTex
.TexObj
);
983 blit
->depthTex
.TexObj
= 0;
987 _mesa_meta_and_swrast_BlitFramebuffer(struct gl_context
*ctx
,
988 struct gl_framebuffer
*readFb
,
989 struct gl_framebuffer
*drawFb
,
990 GLint srcX0
, GLint srcY0
,
991 GLint srcX1
, GLint srcY1
,
992 GLint dstX0
, GLint dstY0
,
993 GLint dstX1
, GLint dstY1
,
994 GLbitfield mask
, GLenum filter
)
996 mask
= _mesa_meta_BlitFramebuffer(ctx
, readFb
, drawFb
,
997 srcX0
, srcY0
, srcX1
, srcY1
,
998 dstX0
, dstY0
, dstX1
, dstY1
,
1003 _swrast_BlitFramebuffer(ctx
, readFb
, drawFb
,
1004 srcX0
, srcY0
, srcX1
, srcY1
,
1005 dstX0
, dstY0
, dstX1
, dstY1
,