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
= "";
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_UseProgram(blit
->msaa_shaders
[shader_index
]);
109 /* Update the uniform values. */
111 _mesa_GetUniformLocation(blit
->msaa_shaders
[shader_index
], "src_width");
113 _mesa_GetUniformLocation(blit
->msaa_shaders
[shader_index
], "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 "in vec2 position;\n"
172 "in vec3 textureCoords;\n"
173 "out vec2 texCoords;\n"
174 "flat out int layer;\n"
177 " texCoords = textureCoords.xy;\n"
178 " layer = int(textureCoords.z);\n"
179 " gl_Position = vec4(position, 0.0, 1.0);\n"
183 fs_source
= ralloc_asprintf(mem_ctx
,
185 "#extension GL_ARB_texture_multisample : enable\n"
186 "uniform sampler2DMS%s texSampler;\n"
187 "uniform float src_width, src_height;\n"
188 "in vec2 texCoords;\n"
189 "flat in int layer;\n"
190 "out vec4 out_color;\n"
196 " const vec2 scale = vec2(%ff, %ff);\n"
197 " const vec2 scale_inv = vec2(%ff, %ff);\n"
198 " const vec2 s_0_offset = vec2(%ff, %ff);\n"
199 " vec2 s_0_coord, s_1_coord, s_2_coord, s_3_coord;\n"
200 " vec4 s_0_color, s_1_color, s_2_color, s_3_color;\n"
201 " vec4 x_0_color, x_1_color;\n"
202 " vec2 tex_coord = texCoords - s_0_offset;\n"
204 " tex_coord *= scale;\n"
205 " tex_coord.x = clamp(tex_coord.x, 0.0f, scale.x * src_width - 1.0f);\n"
206 " tex_coord.y = clamp(tex_coord.y, 0.0f, scale.y * src_height - 1.0f);\n"
207 " interp = fract(tex_coord);\n"
208 " tex_coord = ivec2(tex_coord) * scale_inv;\n"
210 " /* Compute the sample coordinates used for filtering. */\n"
211 " s_0_coord = tex_coord;\n"
212 " s_1_coord = tex_coord + vec2(scale_inv.x, 0.0f);\n"
213 " s_2_coord = tex_coord + vec2(0.0f, scale_inv.y);\n"
214 " s_3_coord = tex_coord + vec2(scale_inv.x, scale_inv.y);\n"
216 " /* Fetch sample color values. */\n"
218 " s_0_color = TEXEL_FETCH(s_0_coord)\n"
219 " s_1_color = TEXEL_FETCH(s_1_coord)\n"
220 " s_2_color = TEXEL_FETCH(s_2_coord)\n"
221 " s_3_color = TEXEL_FETCH(s_3_coord)\n"
222 "#undef TEXEL_FETCH\n"
224 " /* Do bilinear filtering on sample colors. */\n"
225 " x_0_color = mix(s_0_color, s_1_color, interp.x);\n"
226 " x_1_color = mix(s_2_color, s_3_color, interp.x);\n"
227 " out_color = mix(x_0_color, x_1_color, interp.y);\n"
229 sampler_array_suffix
,
232 1.0f
/ x_scale
, 1.0f
/ y_scale
,
233 0.5f
/ x_scale
, 0.5f
/ y_scale
,
236 _mesa_meta_compile_and_link_program(ctx
, vs_source
, fs_source
, name
,
237 &blit
->msaa_shaders
[shader_index
]);
239 _mesa_GetUniformLocation(blit
->msaa_shaders
[shader_index
], "src_width");
241 _mesa_GetUniformLocation(blit
->msaa_shaders
[shader_index
], "src_height");
242 _mesa_Uniform1f(loc_src_width
, src_rb
->Width
);
243 _mesa_Uniform1f(loc_src_height
, src_rb
->Height
);
245 ralloc_free(mem_ctx
);
249 setup_glsl_msaa_blit_shader(struct gl_context
*ctx
,
250 struct blit_state
*blit
,
251 const struct gl_framebuffer
*drawFb
,
252 struct gl_renderbuffer
*src_rb
,
255 const char *vs_source
;
258 enum blit_msaa_shader shader_index
;
259 bool dst_is_msaa
= false;
261 const char *vec4_prefix
;
262 const char *sampler_array_suffix
= "";
264 const char *texcoord_type
= "vec2";
266 int shader_offset
= 0;
269 samples
= MAX2(src_rb
->NumSamples
, 1);
270 src_datatype
= _mesa_get_format_datatype(src_rb
->Format
);
272 /* depth-or-color glCopyTexImage fallback path that passes a NULL rb and
273 * doesn't handle integer.
276 src_datatype
= GL_UNSIGNED_NORMALIZED
;
279 /* We expect only power of 2 samples in source multisample buffer. */
280 assert(samples
> 0 && _mesa_is_pow_two(samples
));
281 while (samples
>> (shader_offset
+ 1)) {
284 /* Update the assert if we plan to support more than 16X MSAA. */
285 assert(shader_offset
>= 0 && shader_offset
<= 4);
287 if (drawFb
->Visual
.samples
> 1) {
288 /* If you're calling meta_BlitFramebuffer with the destination
289 * multisampled, this is the only path that will work -- swrast and
290 * CopyTexImage won't work on it either.
292 assert(ctx
->Extensions
.ARB_sample_shading
);
296 /* We need shader invocation per sample, not per pixel */
297 _mesa_set_enable(ctx
, GL_MULTISAMPLE
, GL_TRUE
);
298 _mesa_set_enable(ctx
, GL_SAMPLE_SHADING
, GL_TRUE
);
299 _mesa_MinSampleShading(1.0);
303 case GL_TEXTURE_2D_MULTISAMPLE
:
304 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
305 if (src_rb
&& (src_rb
->_BaseFormat
== GL_DEPTH_COMPONENT
||
306 src_rb
->_BaseFormat
== GL_DEPTH_STENCIL
)) {
308 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY
;
310 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE
;
313 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY
;
315 shader_index
= BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+
320 if (target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
) {
321 shader_index
+= (BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_RESOLVE
-
322 BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
);
323 sampler_array_suffix
= "Array";
324 texcoord_type
= "vec3";
328 _mesa_problem(ctx
, "Unkown texture target %s\n",
329 _mesa_enum_to_string(target
));
330 shader_index
= BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
;
333 /* We rely on the enum being sorted this way. */
334 STATIC_ASSERT(BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_INT
==
335 BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+ 5);
336 STATIC_ASSERT(BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_UINT
==
337 BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+ 10);
338 if (src_datatype
== GL_INT
) {
341 } else if (src_datatype
== GL_UNSIGNED_INT
) {
348 if (blit
->msaa_shaders
[shader_index
]) {
349 _mesa_UseProgram(blit
->msaa_shaders
[shader_index
]);
353 mem_ctx
= ralloc_context(NULL
);
355 if (shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE
||
356 shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_DEPTH_RESOLVE
||
357 shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_DEPTH_COPY
||
358 shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY
) {
362 sample_index
= "gl_SampleID";
363 name
= "depth MSAA copy";
365 /* From the GL 4.3 spec:
367 * "If there is a multisample buffer (the value of SAMPLE_BUFFERS
368 * is one), then values are obtained from the depth samples in
369 * this buffer. It is recommended that the depth value of the
370 * centermost sample be used, though implementations may choose
371 * any function of the depth sample values at each pixel.
373 * We're slacking and instead of choosing centermost, we've got 0.
376 name
= "depth MSAA resolve";
379 vs_source
= ralloc_asprintf(mem_ctx
,
381 "in vec2 position;\n"
382 "in %s textureCoords;\n"
383 "out %s texCoords;\n"
386 " texCoords = textureCoords;\n"
387 " gl_Position = vec4(position, 0.0, 1.0);\n"
391 fs_source
= ralloc_asprintf(mem_ctx
,
393 "#extension GL_ARB_texture_multisample : enable\n"
394 "#extension GL_ARB_sample_shading : enable\n"
395 "uniform sampler2DMS%s texSampler;\n"
397 "out vec4 out_color;\n"
401 " gl_FragDepth = texelFetch(texSampler, i%s(texCoords), %s).r;\n"
403 sampler_array_suffix
,
408 /* You can create 2D_MULTISAMPLE textures with 0 sample count (meaning 1
409 * sample). Yes, this is ridiculous.
411 char *sample_resolve
;
412 const char *merge_function
;
413 name
= ralloc_asprintf(mem_ctx
, "%svec4 MSAA %s",
415 dst_is_msaa
? "copy" : "resolve");
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 /* We're assuming power of two samples for this resolution procedure.
435 * To avoid losing any floating point precision if the samples all
436 * happen to have the same value, we merge pairs of values at a time
437 * (so the floating point exponent just gets increased), rather than
438 * doing a naive sum and dividing.
440 assert(_mesa_is_pow_two(samples
));
441 /* Fetch each individual sample. */
442 sample_resolve
= rzalloc_size(mem_ctx
, 1);
443 for (i
= 0; i
< samples
; i
++) {
444 ralloc_asprintf_append(&sample_resolve
,
445 " gvec4 sample_1_%d = texelFetch(texSampler, i%s(texCoords), %d);\n",
446 i
, texcoord_type
, i
);
448 /* Now, merge each pair of samples, then merge each pair of those,
451 for (step
= 2; step
<= samples
; step
*= 2) {
452 for (i
= 0; i
< samples
; i
+= step
) {
453 ralloc_asprintf_append(&sample_resolve
,
454 " gvec4 sample_%d_%d = merge(sample_%d_%d, sample_%d_%d);\n",
457 step
/ 2, i
+ step
/ 2);
461 /* Scale the final result. */
462 if (src_datatype
== GL_UNSIGNED_INT
|| src_datatype
== GL_INT
) {
463 ralloc_asprintf_append(&sample_resolve
,
464 " out_color = sample_%d_0;\n",
467 ralloc_asprintf_append(&sample_resolve
,
468 " gl_FragColor = sample_%d_0 / %f;\n",
469 samples
, (float)samples
);
473 vs_source
= ralloc_asprintf(mem_ctx
,
475 "in vec2 position;\n"
476 "in %s textureCoords;\n"
477 "out %s texCoords;\n"
480 " texCoords = textureCoords;\n"
481 " gl_Position = vec4(position, 0.0, 1.0);\n"
485 fs_source
= ralloc_asprintf(mem_ctx
,
487 "#extension GL_ARB_texture_multisample : enable\n"
488 "#extension GL_ARB_sample_shading : enable\n"
489 "#define gvec4 %svec4\n"
490 "uniform %ssampler2DMS%s texSampler;\n"
492 "out gvec4 out_color;\n"
494 "%s" /* merge_function */
497 "%s\n" /* sample_resolve */
501 sampler_array_suffix
,
507 _mesa_meta_compile_and_link_program(ctx
, vs_source
, fs_source
, name
,
508 &blit
->msaa_shaders
[shader_index
]);
510 ralloc_free(mem_ctx
);
514 setup_glsl_blit_framebuffer(struct gl_context
*ctx
,
515 struct blit_state
*blit
,
516 const struct gl_framebuffer
*drawFb
,
517 struct gl_renderbuffer
*src_rb
,
518 GLenum target
, GLenum filter
,
522 unsigned texcoord_size
;
523 bool is_target_multisample
= target
== GL_TEXTURE_2D_MULTISAMPLE
||
524 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
;
525 bool is_filter_scaled_resolve
= filter
== GL_SCALED_RESOLVE_FASTEST_EXT
||
526 filter
== GL_SCALED_RESOLVE_NICEST_EXT
;
528 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
529 assert(_mesa_is_desktop_gl(ctx
) || target
== GL_TEXTURE_2D
);
531 texcoord_size
= 2 + (src_rb
->Depth
> 1 ? 1 : 0);
533 _mesa_meta_setup_vertex_objects(&blit
->VAO
, &blit
->VBO
, true,
534 2, texcoord_size
, 0);
536 if (is_target_multisample
&& is_filter_scaled_resolve
&& is_scaled_blit
) {
537 setup_glsl_msaa_blit_scaled_shader(ctx
, blit
, src_rb
, target
, filter
);
538 } else if (is_target_multisample
) {
539 setup_glsl_msaa_blit_shader(ctx
, blit
, drawFb
, src_rb
, target
);
541 _mesa_meta_setup_blit_shader(ctx
, target
, do_depth
,
542 do_depth
? &blit
->shaders_with_depth
543 : &blit
->shaders_without_depth
);
548 * Try to do a color or depth glBlitFramebuffer using texturing.
550 * We can do this when the src renderbuffer is actually a texture, or when the
551 * driver exposes BindRenderbufferTexImage().
554 blitframebuffer_texture(struct gl_context
*ctx
,
555 const struct gl_framebuffer
*readFb
,
556 const struct gl_framebuffer
*drawFb
,
557 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
558 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
559 GLenum filter
, GLint flipX
, GLint flipY
,
560 GLboolean glsl_version
, GLboolean do_depth
)
562 int att_index
= do_depth
? BUFFER_DEPTH
: readFb
->_ColorReadBufferIndex
;
563 const struct gl_renderbuffer_attachment
*readAtt
=
564 &readFb
->Attachment
[att_index
];
565 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
566 struct fb_tex_blit_state fb_tex_blit
;
567 const GLint dstX
= MIN2(dstX0
, dstX1
);
568 const GLint dstY
= MIN2(dstY0
, dstY1
);
569 const GLint dstW
= abs(dstX1
- dstX0
);
570 const GLint dstH
= abs(dstY1
- dstY0
);
571 const int srcW
= abs(srcX1
- srcX0
);
572 const int srcH
= abs(srcY1
- srcY0
);
573 bool scaled_blit
= false;
574 struct gl_texture_object
*texObj
;
577 struct gl_renderbuffer
*rb
= readAtt
->Renderbuffer
;
578 struct temp_texture
*meta_temp_texture
;
580 if (rb
->NumSamples
&& !ctx
->Extensions
.ARB_texture_multisample
)
583 _mesa_meta_fb_tex_blit_begin(ctx
, &fb_tex_blit
);
585 if (readAtt
->Texture
&&
586 (readAtt
->Texture
->Target
== GL_TEXTURE_2D
||
587 readAtt
->Texture
->Target
== GL_TEXTURE_RECTANGLE
||
588 readAtt
->Texture
->Target
== GL_TEXTURE_2D_MULTISAMPLE
||
589 readAtt
->Texture
->Target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
)) {
590 /* If there's a texture attached of a type we can handle, then just use
593 srcLevel
= readAtt
->TextureLevel
;
594 texObj
= readAtt
->Texture
;
595 target
= texObj
->Target
;
596 } else if (!readAtt
->Texture
&& ctx
->Driver
.BindRenderbufferTexImage
) {
597 if (!_mesa_meta_bind_rb_as_tex_image(ctx
, rb
, &fb_tex_blit
.tempTex
,
602 if (_mesa_is_winsys_fbo(readFb
)) {
604 srcY0
= rb
->Height
- srcY1
;
605 srcY1
= rb
->Height
- temp
;
609 GLenum tex_base_format
;
610 /* Fall back to doing a CopyTexSubImage to get the destination
611 * renderbuffer into a texture.
613 if (ctx
->Meta
->Blit
.no_ctsi_fallback
)
616 if (rb
->NumSamples
> 1)
620 meta_temp_texture
= _mesa_meta_get_temp_depth_texture(ctx
);
621 tex_base_format
= GL_DEPTH_COMPONENT
;
623 meta_temp_texture
= _mesa_meta_get_temp_texture(ctx
);
625 _mesa_base_tex_format(ctx
, rb
->InternalFormat
);
629 target
= meta_temp_texture
->Target
;
630 texObj
= _mesa_lookup_texture(ctx
, meta_temp_texture
->TexObj
);
631 if (texObj
== NULL
) {
635 _mesa_meta_setup_copypix_texture(ctx
, meta_temp_texture
,
648 fb_tex_blit
.baseLevelSave
= texObj
->BaseLevel
;
649 fb_tex_blit
.maxLevelSave
= texObj
->MaxLevel
;
650 fb_tex_blit
.stencilSamplingSave
= texObj
->StencilSampling
;
652 scaled_blit
= dstW
!= srcW
|| dstH
!= srcH
;
655 setup_glsl_blit_framebuffer(ctx
, blit
, drawFb
, rb
, target
, filter
, scaled_blit
,
659 _mesa_meta_setup_ff_tnl_for_blit(&ctx
->Meta
->Blit
.VAO
,
660 &ctx
->Meta
->Blit
.VBO
,
665 printf("Blit from texture!\n");
666 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
667 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
670 fb_tex_blit
.sampler
= _mesa_meta_setup_sampler(ctx
, texObj
, target
, filter
,
673 /* Always do our blits with no net sRGB decode or encode.
675 * However, if both the src and dst can be srgb decode/encoded, enable them
676 * so that we do any blending (from scaling or from MSAA resolves) in the
679 * Our choice of not doing any net encode/decode is from the GL 3.0
682 * "Blit operations bypass the fragment pipeline. The only fragment
683 * operations which affect a blit are the pixel ownership test and the
686 * The GL 4.4 specification disagrees and says that the sRGB part of the
687 * fragment pipeline applies, but this was found to break applications.
689 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
690 if (_mesa_get_format_color_encoding(rb
->Format
) == GL_SRGB
&&
691 drawFb
->Visual
.sRGBCapable
) {
692 _mesa_SamplerParameteri(fb_tex_blit
.sampler
,
693 GL_TEXTURE_SRGB_DECODE_EXT
, GL_DECODE_EXT
);
694 _mesa_set_framebuffer_srgb(ctx
, GL_TRUE
);
696 _mesa_SamplerParameteri(fb_tex_blit
.sampler
,
697 GL_TEXTURE_SRGB_DECODE_EXT
,
699 /* set_framebuffer_srgb was set by _mesa_meta_begin(). */
704 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
705 _mesa_set_enable(ctx
, target
, GL_TRUE
);
708 /* Prepare vertex data (the VBO was previously created and bound) */
710 struct vertex verts
[4];
711 GLfloat s0
, t0
, s1
, t1
;
713 if (target
== GL_TEXTURE_2D
) {
714 const struct gl_texture_image
*texImage
715 = _mesa_select_tex_image(texObj
, target
, srcLevel
);
716 s0
= srcX0
/ (float) texImage
->Width
;
717 s1
= srcX1
/ (float) texImage
->Width
;
718 t0
= srcY0
/ (float) texImage
->Height
;
719 t1
= srcY1
/ (float) texImage
->Height
;
722 assert(target
== GL_TEXTURE_RECTANGLE_ARB
||
723 target
== GL_TEXTURE_2D_MULTISAMPLE
||
724 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
);
731 /* Silence valgrind warnings about reading uninitialized stack. */
732 memset(verts
, 0, sizeof(verts
));
734 /* setup vertex positions */
735 verts
[0].x
= -1.0F
* flipX
;
736 verts
[0].y
= -1.0F
* flipY
;
737 verts
[1].x
= 1.0F
* flipX
;
738 verts
[1].y
= -1.0F
* flipY
;
739 verts
[2].x
= 1.0F
* flipX
;
740 verts
[2].y
= 1.0F
* flipY
;
741 verts
[3].x
= -1.0F
* flipX
;
742 verts
[3].y
= 1.0F
* flipY
;
744 verts
[0].tex
[0] = s0
;
745 verts
[0].tex
[1] = t0
;
746 verts
[0].tex
[2] = readAtt
->Zoffset
;
747 verts
[1].tex
[0] = s1
;
748 verts
[1].tex
[1] = t0
;
749 verts
[1].tex
[2] = readAtt
->Zoffset
;
750 verts
[2].tex
[0] = s1
;
751 verts
[2].tex
[1] = t1
;
752 verts
[2].tex
[2] = readAtt
->Zoffset
;
753 verts
[3].tex
[0] = s0
;
754 verts
[3].tex
[1] = t1
;
755 verts
[3].tex
[2] = readAtt
->Zoffset
;
757 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB
, 0, sizeof(verts
), verts
);
761 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
762 _mesa_ColorMask(!do_depth
, !do_depth
, !do_depth
, !do_depth
);
763 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, do_depth
);
764 _mesa_DepthMask(do_depth
);
765 _mesa_DepthFunc(GL_ALWAYS
);
767 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
768 _mesa_meta_fb_tex_blit_end(ctx
, target
, &fb_tex_blit
);
774 _mesa_meta_fb_tex_blit_begin(const struct gl_context
*ctx
,
775 struct fb_tex_blit_state
*blit
)
778 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
?
779 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
->Name
: 0;
784 _mesa_meta_fb_tex_blit_end(struct gl_context
*ctx
, GLenum target
,
785 struct fb_tex_blit_state
*blit
)
787 /* Restore texture object state, the texture binding will
788 * be restored by _mesa_meta_end().
790 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
791 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, blit
->baseLevelSave
);
792 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, blit
->maxLevelSave
);
794 if (ctx
->Extensions
.ARB_stencil_texturing
) {
795 const struct gl_texture_object
*texObj
=
796 _mesa_get_current_tex_object(ctx
, target
);
798 if (texObj
->StencilSampling
!= blit
->stencilSamplingSave
)
799 _mesa_TexParameteri(target
, GL_DEPTH_STENCIL_TEXTURE_MODE
,
800 blit
->stencilSamplingSave
?
801 GL_STENCIL_INDEX
: GL_DEPTH_COMPONENT
);
805 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, blit
->samplerSave
);
806 _mesa_DeleteSamplers(1, &blit
->sampler
);
808 _mesa_DeleteTextures(1, &blit
->tempTex
);
812 _mesa_meta_bind_rb_as_tex_image(struct gl_context
*ctx
,
813 struct gl_renderbuffer
*rb
,
815 struct gl_texture_object
**texObj
,
818 struct gl_texture_image
*texImage
;
821 if (rb
->NumSamples
> 1)
822 *target
= GL_TEXTURE_2D_MULTISAMPLE
;
824 *target
= GL_TEXTURE_2D
;
827 _mesa_GenTextures(1, &tempTex
);
833 _mesa_BindTexture(*target
, *tex
);
834 *texObj
= _mesa_lookup_texture(ctx
, *tex
);
835 texImage
= _mesa_get_tex_image(ctx
, *texObj
, *target
, 0);
837 if (!ctx
->Driver
.BindRenderbufferTexImage(ctx
, rb
, texImage
)) {
838 _mesa_DeleteTextures(1, tex
);
842 if (ctx
->Driver
.FinishRenderTexture
&& !rb
->NeedsFinishRenderTexture
) {
843 rb
->NeedsFinishRenderTexture
= true;
844 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
851 _mesa_meta_setup_sampler(struct gl_context
*ctx
,
852 const struct gl_texture_object
*texObj
,
853 GLenum target
, GLenum filter
, GLuint srcLevel
)
856 GLenum tex_filter
= (filter
== GL_SCALED_RESOLVE_FASTEST_EXT
||
857 filter
== GL_SCALED_RESOLVE_NICEST_EXT
) ?
860 _mesa_GenSamplers(1, &sampler
);
861 _mesa_BindSampler(ctx
->Texture
.CurrentUnit
, sampler
);
863 /* Prepare src texture state */
864 _mesa_BindTexture(target
, texObj
->Name
);
865 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MIN_FILTER
, tex_filter
);
866 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_MAG_FILTER
, tex_filter
);
867 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
868 _mesa_TexParameteri(target
, GL_TEXTURE_BASE_LEVEL
, srcLevel
);
869 _mesa_TexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, srcLevel
);
871 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
872 _mesa_SamplerParameteri(sampler
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
878 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
879 * of texture mapping and polygon rendering.
882 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
883 const struct gl_framebuffer
*readFb
,
884 const struct gl_framebuffer
*drawFb
,
885 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
886 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
887 GLbitfield mask
, GLenum filter
)
889 const GLint dstW
= abs(dstX1
- dstX0
);
890 const GLint dstH
= abs(dstY1
- dstY0
);
891 const GLint dstFlipX
= (dstX1
- dstX0
) / dstW
;
892 const GLint dstFlipY
= (dstY1
- dstY0
) / dstH
;
895 GLint srcX0
, srcY0
, srcX1
, srcY1
;
896 GLint dstX0
, dstY0
, dstX1
, dstY1
;
898 srcX0
, srcY0
, srcX1
, srcY1
,
899 dstX0
, dstY0
, dstX1
, dstY1
902 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
903 ctx
->Extensions
.ARB_fragment_shader
;
905 /* Multisample texture blit support requires texture multisample. */
906 if (readFb
->Visual
.samples
> 0 &&
907 !ctx
->Extensions
.ARB_texture_multisample
) {
911 /* Clip a copy of the blit coordinates. If these differ from the input
912 * coordinates, then we'll set the scissor.
914 if (!_mesa_clip_blit(ctx
, readFb
, drawFb
,
915 &clip
.srcX0
, &clip
.srcY0
, &clip
.srcX1
, &clip
.srcY1
,
916 &clip
.dstX0
, &clip
.dstY0
, &clip
.dstX1
, &clip
.dstY1
)) {
917 /* clipped/scissored everything away */
921 /* Only scissor affects blit, but we're doing to set a custom scissor if
922 * necessary anyway, so save/clear state.
924 _mesa_meta_begin(ctx
, MESA_META_ALL
& ~MESA_META_DRAW_BUFFERS
);
926 /* Dithering shouldn't be performed for glBlitFramebuffer */
927 _mesa_set_enable(ctx
, GL_DITHER
, GL_FALSE
);
929 /* If the clipping earlier changed the destination rect at all, then
930 * enable the scissor to clip to it.
932 if (clip
.dstX0
!= dstX0
|| clip
.dstY0
!= dstY0
||
933 clip
.dstX1
!= dstX1
|| clip
.dstY1
!= dstY1
) {
934 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_TRUE
);
935 _mesa_Scissor(MIN2(clip
.dstX0
, clip
.dstX1
),
936 MIN2(clip
.dstY0
, clip
.dstY1
),
937 abs(clip
.dstX0
- clip
.dstX1
),
938 abs(clip
.dstY0
- clip
.dstY1
));
941 /* Try faster, direct texture approach first */
942 if (mask
& GL_COLOR_BUFFER_BIT
) {
943 if (blitframebuffer_texture(ctx
, readFb
, drawFb
,
944 srcX0
, srcY0
, srcX1
, srcY1
,
945 dstX0
, dstY0
, dstX1
, dstY1
,
946 filter
, dstFlipX
, dstFlipY
,
947 use_glsl_version
, false)) {
948 mask
&= ~GL_COLOR_BUFFER_BIT
;
952 if (mask
& GL_DEPTH_BUFFER_BIT
&& use_glsl_version
) {
953 if (blitframebuffer_texture(ctx
, readFb
, drawFb
,
954 srcX0
, srcY0
, srcX1
, srcY1
,
955 dstX0
, dstY0
, dstX1
, dstY1
,
956 filter
, dstFlipX
, dstFlipY
,
957 use_glsl_version
, true)) {
958 mask
&= ~GL_DEPTH_BUFFER_BIT
;
962 if (mask
& GL_STENCIL_BUFFER_BIT
) {
963 /* XXX can't easily do stencil */
972 _mesa_meta_glsl_blit_cleanup(struct blit_state
*blit
)
975 _mesa_DeleteVertexArrays(1, &blit
->VAO
);
977 _mesa_DeleteBuffers(1, &blit
->VBO
);
981 _mesa_meta_blit_shader_table_cleanup(&blit
->shaders_with_depth
);
982 _mesa_meta_blit_shader_table_cleanup(&blit
->shaders_without_depth
);
984 _mesa_DeleteTextures(1, &blit
->depthTex
.TexObj
);
985 blit
->depthTex
.TexObj
= 0;
989 _mesa_meta_and_swrast_BlitFramebuffer(struct gl_context
*ctx
,
990 struct gl_framebuffer
*readFb
,
991 struct gl_framebuffer
*drawFb
,
992 GLint srcX0
, GLint srcY0
,
993 GLint srcX1
, GLint srcY1
,
994 GLint dstX0
, GLint dstY0
,
995 GLint dstX1
, GLint dstY1
,
996 GLbitfield mask
, GLenum filter
)
998 mask
= _mesa_meta_BlitFramebuffer(ctx
, readFb
, drawFb
,
999 srcX0
, srcY0
, srcX1
, srcY1
,
1000 dstX0
, dstY0
, dstX1
, dstY1
,
1005 _swrast_BlitFramebuffer(ctx
, readFb
, drawFb
,
1006 srcX0
, srcY0
, srcX1
, srcY1
,
1007 dstX0
, dstY0
, dstX1
, dstY1
,