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/arbprogram.h"
28 #include "main/arrayobj.h"
29 #include "main/blend.h"
30 #include "main/condrender.h"
31 #include "main/depth.h"
32 #include "main/enable.h"
33 #include "main/enums.h"
34 #include "main/fbobject.h"
35 #include "main/image.h"
36 #include "main/macros.h"
37 #include "main/matrix.h"
38 #include "main/multisample.h"
39 #include "main/objectlabel.h"
40 #include "main/readpix.h"
41 #include "main/scissor.h"
42 #include "main/shaderapi.h"
43 #include "main/texobj.h"
44 #include "main/texenv.h"
45 #include "main/teximage.h"
46 #include "main/texparam.h"
47 #include "main/uniforms.h"
48 #include "main/varray.h"
49 #include "main/viewport.h"
50 #include "swrast/swrast.h"
51 #include "drivers/common/meta.h"
52 #include "util/ralloc.h"
54 static struct gl_texture_object
*
55 texture_object_from_renderbuffer(struct gl_context
*, struct gl_renderbuffer
*);
57 static struct gl_sampler_object
*
58 setup_sampler(struct gl_context
*, struct gl_texture_object
*, GLenum target
,
59 GLenum filter
, GLuint srcLevel
);
61 /** Return offset in bytes of the field within a vertex struct */
62 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
65 setup_glsl_msaa_blit_shader(struct gl_context
*ctx
,
66 struct blit_state
*blit
,
67 const struct gl_framebuffer
*drawFb
,
68 struct gl_renderbuffer
*src_rb
,
71 const char *vs_source
;
74 enum blit_msaa_shader shader_index
;
75 bool dst_is_msaa
= false;
77 const char *vec4_prefix
;
78 const char *sampler_array_suffix
= "";
80 const char *texcoord_type
= "vec2";
82 int shader_offset
= 0;
85 samples
= MAX2(src_rb
->NumSamples
, 1);
86 src_datatype
= _mesa_get_format_datatype(src_rb
->Format
);
88 /* depth-or-color glCopyTexImage fallback path that passes a NULL rb and
89 * doesn't handle integer.
92 src_datatype
= GL_UNSIGNED_NORMALIZED
;
95 /* We expect only power of 2 samples in source multisample buffer. */
96 assert(samples
> 0 && util_is_power_of_two_nonzero(samples
));
97 while (samples
>> (shader_offset
+ 1)) {
100 /* Update the assert if we plan to support more than 16X MSAA. */
101 assert(shader_offset
>= 0 && shader_offset
<= 4);
103 if (drawFb
->Visual
.samples
> 1) {
104 /* If you're calling meta_BlitFramebuffer with the destination
105 * multisampled, this is the only path that will work -- swrast and
106 * CopyTexImage won't work on it either.
108 assert(ctx
->Extensions
.ARB_sample_shading
);
112 /* We need shader invocation per sample, not per pixel */
113 _mesa_set_enable(ctx
, GL_MULTISAMPLE
, GL_TRUE
);
114 _mesa_set_enable(ctx
, GL_SAMPLE_SHADING
, GL_TRUE
);
115 _mesa_MinSampleShading(1.0);
119 case GL_TEXTURE_2D_MULTISAMPLE
:
120 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
121 if (src_rb
&& (src_rb
->_BaseFormat
== GL_DEPTH_COMPONENT
||
122 src_rb
->_BaseFormat
== GL_DEPTH_STENCIL
)) {
124 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY
;
126 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE
;
129 shader_index
= BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY
;
131 shader_index
= BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+
136 if (target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
) {
137 shader_index
+= (BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_RESOLVE
-
138 BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
);
139 sampler_array_suffix
= "Array";
140 texcoord_type
= "vec3";
144 _mesa_problem(ctx
, "Unknown texture target %s\n",
145 _mesa_enum_to_string(target
));
146 shader_index
= BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
;
149 /* We rely on the enum being sorted this way. */
150 STATIC_ASSERT(BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_INT
==
151 BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+ 5);
152 STATIC_ASSERT(BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_UINT
==
153 BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE
+ 10);
154 if (src_datatype
== GL_INT
) {
157 } else if (src_datatype
== GL_UNSIGNED_INT
) {
164 if (blit
->msaa_shaders
[shader_index
]) {
165 _mesa_meta_use_program(ctx
, blit
->msaa_shaders
[shader_index
]);
169 mem_ctx
= ralloc_context(NULL
);
171 if (shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE
||
172 shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_DEPTH_RESOLVE
||
173 shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_DEPTH_COPY
||
174 shader_index
== BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY
) {
176 const char *tex_coords
= "texCoords";
179 sample_index
= "gl_SampleID";
180 name
= "depth MSAA copy";
182 if (ctx
->Extensions
.ARB_gpu_shader5
&& samples
>= 16) {
183 /* See comment below for the color copy */
184 tex_coords
= "interpolateAtOffset(texCoords, vec2(0.0))";
187 /* From the GL 4.3 spec:
189 * "If there is a multisample buffer (the value of SAMPLE_BUFFERS
190 * is one), then values are obtained from the depth samples in
191 * this buffer. It is recommended that the depth value of the
192 * centermost sample be used, though implementations may choose
193 * any function of the depth sample values at each pixel.
195 * We're slacking and instead of choosing centermost, we've got 0.
198 name
= "depth MSAA resolve";
201 vs_source
= ralloc_asprintf(mem_ctx
,
203 "#extension GL_ARB_explicit_attrib_location: enable\n"
204 "layout(location = 0) in vec2 position;\n"
205 "layout(location = 1) in %s textureCoords;\n"
206 "out %s texCoords;\n"
209 " texCoords = textureCoords;\n"
210 " gl_Position = vec4(position, 0.0, 1.0);\n"
214 fs_source
= ralloc_asprintf(mem_ctx
,
216 "#extension GL_ARB_texture_multisample : enable\n"
217 "#extension GL_ARB_sample_shading : enable\n"
218 "#extension GL_ARB_gpu_shader5 : enable\n"
219 "uniform sampler2DMS%s texSampler;\n"
221 "out vec4 out_color;\n"
225 " gl_FragDepth = texelFetch(texSampler, i%s(%s), %s).r;\n"
227 sampler_array_suffix
,
233 /* You can create 2D_MULTISAMPLE textures with 0 sample count (meaning 1
234 * sample). Yes, this is ridiculous.
236 char *sample_resolve
;
237 const char *merge_function
;
238 name
= ralloc_asprintf(mem_ctx
, "%svec4 MSAA %s",
240 dst_is_msaa
? "copy" : "resolve");
243 const char *tex_coords
;
245 if (ctx
->Extensions
.ARB_gpu_shader5
&& samples
>= 16) {
246 /* If interpolateAtOffset is available then it will be used to
247 * force the interpolation to the center. This is required at
248 * least on Intel hardware because it is possible to have a sample
249 * position on the 0 x or y axis which means it will lie exactly
250 * on the pixel boundary. If we let the hardware interpolate the
251 * coordinates at one of these positions then it is possible for
252 * it to jump to a neighboring texel when converting to ints due
253 * to rounding errors. This is only done for >= 16x MSAA because
254 * it probably has some overhead. It is more likely that some
255 * hardware will use one of these problematic positions at 16x
256 * MSAA because in that case in D3D they are defined to be at
259 tex_coords
= "interpolateAtOffset(texCoords, vec2(0.0))";
261 tex_coords
= "texCoords";
265 ralloc_asprintf(mem_ctx
,
266 " out_color = texelFetch(texSampler, "
267 "i%s(%s), gl_SampleID);",
268 texcoord_type
, tex_coords
);
275 if (src_datatype
== GL_INT
|| src_datatype
== GL_UNSIGNED_INT
) {
276 /* From the OpenGL ES 3.2 spec section 16.2.1:
278 * "If the source formats are integer types or stencil values,
279 * a single sample's value is selected for each pixel."
281 * The OpenGL 4.4 spec contains exactly the same language.
283 * We can accomplish this by making the merge function return just
284 * one of the two samples. The compiler should do the rest.
286 merge_function
= "gvec4 merge(gvec4 a, gvec4 b) { return a; }\n";
288 /* The divide will happen at the end for floats. */
290 "vec4 merge(vec4 a, vec4 b) { return (a + b); }\n";
293 /* We're assuming power of two samples for this resolution procedure.
295 * To avoid losing any floating point precision if the samples all
296 * happen to have the same value, we merge pairs of values at a time
297 * (so the floating point exponent just gets increased), rather than
298 * doing a naive sum and dividing.
300 assert(util_is_power_of_two_or_zero(samples
));
301 /* Fetch each individual sample. */
302 sample_resolve
= rzalloc_size(mem_ctx
, 1);
303 for (i
= 0; i
< samples
; i
++) {
304 ralloc_asprintf_append(&sample_resolve
,
305 " gvec4 sample_1_%d = texelFetch(texSampler, i%s(texCoords), %d);\n",
306 i
, texcoord_type
, i
);
308 /* Now, merge each pair of samples, then merge each pair of those,
311 for (step
= 2; step
<= samples
; step
*= 2) {
312 for (i
= 0; i
< samples
; i
+= step
) {
313 ralloc_asprintf_append(&sample_resolve
,
314 " gvec4 sample_%d_%d = merge(sample_%d_%d, sample_%d_%d);\n",
317 step
/ 2, i
+ step
/ 2);
321 /* Scale the final result. */
322 if (src_datatype
== GL_UNSIGNED_INT
|| src_datatype
== GL_INT
) {
323 ralloc_asprintf_append(&sample_resolve
,
324 " out_color = sample_%d_0;\n",
327 ralloc_asprintf_append(&sample_resolve
,
328 " gl_FragColor = sample_%d_0 / %f;\n",
329 samples
, (float)samples
);
333 vs_source
= ralloc_asprintf(mem_ctx
,
335 "#extension GL_ARB_explicit_attrib_location: enable\n"
336 "layout(location = 0) in vec2 position;\n"
337 "layout(location = 1) in %s textureCoords;\n"
338 "out %s texCoords;\n"
341 " texCoords = textureCoords;\n"
342 " gl_Position = vec4(position, 0.0, 1.0);\n"
346 fs_source
= ralloc_asprintf(mem_ctx
,
348 "#extension GL_ARB_texture_multisample : enable\n"
349 "#extension GL_ARB_sample_shading : enable\n"
350 "#extension GL_ARB_gpu_shader5 : enable\n"
351 "#define gvec4 %svec4\n"
352 "uniform %ssampler2DMS%s texSampler;\n"
354 "out gvec4 out_color;\n"
356 "%s" /* merge_function */
359 "%s\n" /* sample_resolve */
363 sampler_array_suffix
,
369 _mesa_meta_compile_and_link_program(ctx
, vs_source
, fs_source
, name
,
370 &blit
->msaa_shaders
[shader_index
]);
372 ralloc_free(mem_ctx
);
376 setup_glsl_blit_framebuffer(struct gl_context
*ctx
,
377 struct blit_state
*blit
,
378 const struct gl_framebuffer
*drawFb
,
379 struct gl_renderbuffer
*src_rb
,
380 GLenum target
, GLenum filter
,
384 unsigned texcoord_size
;
385 bool is_target_multisample
= target
== GL_TEXTURE_2D_MULTISAMPLE
||
386 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
;
388 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
389 assert(_mesa_is_desktop_gl(ctx
) || target
== GL_TEXTURE_2D
);
391 texcoord_size
= 2 + (src_rb
->Depth
> 1 ? 1 : 0);
393 _mesa_meta_setup_vertex_objects(ctx
, &blit
->VAO
, &blit
->buf_obj
, true,
394 2, texcoord_size
, 0);
396 if (is_target_multisample
) {
397 setup_glsl_msaa_blit_shader(ctx
, blit
, drawFb
, src_rb
, target
);
399 _mesa_meta_setup_blit_shader(ctx
, target
, do_depth
,
400 do_depth
? &blit
->shaders_with_depth
401 : &blit
->shaders_without_depth
);
406 * Try to do a color or depth glBlitFramebuffer using texturing.
408 * We can do this when the src renderbuffer is actually a texture, or when the
409 * driver exposes BindRenderbufferTexImage().
412 blitframebuffer_texture(struct gl_context
*ctx
,
413 const struct gl_framebuffer
*readFb
,
414 const struct gl_framebuffer
*drawFb
,
415 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
416 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
417 GLenum filter
, GLint flipX
, GLint flipY
,
418 GLboolean glsl_version
, GLboolean do_depth
)
420 int att_index
= do_depth
? BUFFER_DEPTH
: readFb
->_ColorReadBufferIndex
;
421 const struct gl_renderbuffer_attachment
*readAtt
=
422 &readFb
->Attachment
[att_index
];
423 struct blit_state
*blit
= &ctx
->Meta
->Blit
;
424 struct fb_tex_blit_state fb_tex_blit
;
425 const GLint dstX
= MIN2(dstX0
, dstX1
);
426 const GLint dstY
= MIN2(dstY0
, dstY1
);
427 const GLint dstW
= abs(dstX1
- dstX0
);
428 const GLint dstH
= abs(dstY1
- dstY0
);
429 const int srcW
= abs(srcX1
- srcX0
);
430 const int srcH
= abs(srcY1
- srcY0
);
431 bool scaled_blit
= false;
432 struct gl_texture_object
*texObj
;
435 struct gl_renderbuffer
*rb
= readAtt
->Renderbuffer
;
436 struct temp_texture
*meta_temp_texture
;
438 if (rb
->NumSamples
&& !ctx
->Extensions
.ARB_texture_multisample
)
441 _mesa_meta_fb_tex_blit_begin(ctx
, &fb_tex_blit
);
443 if (readAtt
->Texture
&&
444 (readAtt
->Texture
->Target
== GL_TEXTURE_2D
||
445 readAtt
->Texture
->Target
== GL_TEXTURE_RECTANGLE
||
446 readAtt
->Texture
->Target
== GL_TEXTURE_2D_MULTISAMPLE
||
447 readAtt
->Texture
->Target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
)) {
448 /* If there's a texture attached of a type we can handle, then just use
451 srcLevel
= readAtt
->TextureLevel
;
452 texObj
= readAtt
->Texture
;
453 } else if (!readAtt
->Texture
&& ctx
->Driver
.BindRenderbufferTexImage
) {
454 texObj
= texture_object_from_renderbuffer(ctx
, rb
);
458 fb_tex_blit
.temp_tex_obj
= texObj
;
461 if (_mesa_is_winsys_fbo(readFb
)) {
463 srcY0
= rb
->Height
- srcY1
;
464 srcY1
= rb
->Height
- temp
;
468 GLenum tex_base_format
;
469 /* Fall back to doing a CopyTexSubImage to get the destination
470 * renderbuffer into a texture.
472 if (ctx
->Meta
->Blit
.no_ctsi_fallback
)
475 if (rb
->NumSamples
> 1)
479 meta_temp_texture
= _mesa_meta_get_temp_depth_texture(ctx
);
480 tex_base_format
= GL_DEPTH_COMPONENT
;
482 meta_temp_texture
= _mesa_meta_get_temp_texture(ctx
);
484 _mesa_base_tex_format(ctx
, rb
->InternalFormat
);
488 texObj
= meta_temp_texture
->tex_obj
;
489 if (texObj
== NULL
) {
493 _mesa_meta_setup_copypix_texture(ctx
, meta_temp_texture
,
499 assert(texObj
->Target
== meta_temp_texture
->Target
);
507 target
= texObj
->Target
;
508 fb_tex_blit
.tex_obj
= texObj
;
509 fb_tex_blit
.baseLevelSave
= texObj
->BaseLevel
;
510 fb_tex_blit
.maxLevelSave
= texObj
->MaxLevel
;
511 fb_tex_blit
.stencilSamplingSave
= texObj
->StencilSampling
;
513 scaled_blit
= dstW
!= srcW
|| dstH
!= srcH
;
516 setup_glsl_blit_framebuffer(ctx
, blit
, drawFb
, rb
, target
, filter
, scaled_blit
,
520 _mesa_meta_setup_ff_tnl_for_blit(ctx
,
521 &ctx
->Meta
->Blit
.VAO
,
522 &ctx
->Meta
->Blit
.buf_obj
,
527 printf("Blit from texture!\n");
528 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
529 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
532 fb_tex_blit
.samp_obj
= setup_sampler(ctx
, texObj
, target
, filter
, srcLevel
);
534 if (ctx
->Extensions
.EXT_texture_sRGB_decode
) {
535 /* The GL 4.4 spec, section 18.3.1 ("Blitting Pixel Rectangles") says:
537 * "When values are taken from the read buffer, if FRAMEBUFFER_SRGB
538 * is enabled and the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
539 * for the framebuffer attachment corresponding to the read buffer
540 * is SRGB (see section 9.2.3), the red, green, and blue components
541 * are converted from the non-linear sRGB color space according to
544 * When values are written to the draw buffers, blit operations
545 * bypass most of the fragment pipeline. The only fragment
546 * operations which affect a blit are the pixel ownership test,
547 * the scissor test, and sRGB conversion (see section 17.3.9)."
549 * ES 3.0 contains nearly the exact same text, but omits the part
550 * about GL_FRAMEBUFFER_SRGB as that doesn't exist in ES. Mesa
551 * defaults it to on for ES contexts, so we can safely check it.
554 ctx
->Color
.sRGBEnabled
&& _mesa_is_format_srgb(rb
->Format
);
556 _mesa_set_sampler_srgb_decode(ctx
, fb_tex_blit
.samp_obj
,
557 decode
? GL_DECODE_EXT
558 : GL_SKIP_DECODE_EXT
);
562 _mesa_TexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
563 _mesa_set_enable(ctx
, target
, GL_TRUE
);
566 /* Prepare vertex data (the VBO was previously created and bound) */
568 struct vertex verts
[4];
569 GLfloat s0
, t0
, s1
, t1
;
571 if (target
== GL_TEXTURE_2D
) {
572 const struct gl_texture_image
*texImage
573 = _mesa_select_tex_image(texObj
, target
, srcLevel
);
574 s0
= srcX0
/ (float) texImage
->Width
;
575 s1
= srcX1
/ (float) texImage
->Width
;
576 t0
= srcY0
/ (float) texImage
->Height
;
577 t1
= srcY1
/ (float) texImage
->Height
;
580 assert(target
== GL_TEXTURE_RECTANGLE_ARB
||
581 target
== GL_TEXTURE_2D_MULTISAMPLE
||
582 target
== GL_TEXTURE_2D_MULTISAMPLE_ARRAY
);
589 /* Silence valgrind warnings about reading uninitialized stack. */
590 memset(verts
, 0, sizeof(verts
));
592 /* setup vertex positions */
593 verts
[0].x
= -1.0F
* flipX
;
594 verts
[0].y
= -1.0F
* flipY
;
595 verts
[1].x
= 1.0F
* flipX
;
596 verts
[1].y
= -1.0F
* flipY
;
597 verts
[2].x
= 1.0F
* flipX
;
598 verts
[2].y
= 1.0F
* flipY
;
599 verts
[3].x
= -1.0F
* flipX
;
600 verts
[3].y
= 1.0F
* flipY
;
602 verts
[0].tex
[0] = s0
;
603 verts
[0].tex
[1] = t0
;
604 verts
[0].tex
[2] = readAtt
->Zoffset
;
605 verts
[1].tex
[0] = s1
;
606 verts
[1].tex
[1] = t0
;
607 verts
[1].tex
[2] = readAtt
->Zoffset
;
608 verts
[2].tex
[0] = s1
;
609 verts
[2].tex
[1] = t1
;
610 verts
[2].tex
[2] = readAtt
->Zoffset
;
611 verts
[3].tex
[0] = s0
;
612 verts
[3].tex
[1] = t1
;
613 verts
[3].tex
[2] = readAtt
->Zoffset
;
615 _mesa_buffer_sub_data(ctx
, blit
->buf_obj
, 0, sizeof(verts
), verts
);
619 _mesa_set_viewport(ctx
, 0, dstX
, dstY
, dstW
, dstH
);
620 _mesa_ColorMask(!do_depth
, !do_depth
, !do_depth
, !do_depth
);
621 _mesa_set_enable(ctx
, GL_DEPTH_TEST
, do_depth
);
622 _mesa_DepthMask(do_depth
);
623 _mesa_DepthFunc(GL_ALWAYS
);
625 _mesa_DrawArrays(GL_TRIANGLE_FAN
, 0, 4);
626 _mesa_meta_fb_tex_blit_end(ctx
, target
, &fb_tex_blit
);
632 _mesa_meta_fb_tex_blit_begin(struct gl_context
*ctx
,
633 struct fb_tex_blit_state
*blit
)
635 /* None of the existing callers preinitialize fb_tex_blit_state to zeros,
636 * and both use stack variables. If samp_obj_save is not NULL,
637 * _mesa_reference_sampler_object will try to dereference it. Leaving
638 * random garbage in samp_obj_save can only lead to crashes.
640 * Since the state isn't persistent across calls, we won't catch ref
643 blit
->samp_obj_save
= NULL
;
644 _mesa_reference_sampler_object(ctx
, &blit
->samp_obj_save
,
645 ctx
->Texture
.Unit
[ctx
->Texture
.CurrentUnit
].Sampler
);
646 blit
->temp_tex_obj
= NULL
;
650 _mesa_meta_fb_tex_blit_end(struct gl_context
*ctx
, GLenum target
,
651 struct fb_tex_blit_state
*blit
)
653 struct gl_texture_object
*const texObj
=
654 _mesa_get_current_tex_object(ctx
, target
);
656 /* Either there is no temporary texture or the temporary texture is bound. */
657 assert(blit
->temp_tex_obj
== NULL
|| blit
->temp_tex_obj
== texObj
);
659 /* Restore texture object state, the texture binding will be restored by
660 * _mesa_meta_end(). If the texture is the temporary texture that is about
661 * to be destroyed, don't bother restoring its state.
663 if (blit
->temp_tex_obj
== NULL
) {
664 /* If the target restricts values for base level or max level, we assume
665 * that the original values were valid.
667 if (blit
->baseLevelSave
!= texObj
->BaseLevel
)
668 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_BASE_LEVEL
,
669 &blit
->baseLevelSave
, false);
671 if (blit
->maxLevelSave
!= texObj
->MaxLevel
)
672 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_MAX_LEVEL
,
673 &blit
->maxLevelSave
, false);
675 /* If ARB_stencil_texturing is not supported, the mode won't have changed. */
676 if (texObj
->StencilSampling
!= blit
->stencilSamplingSave
) {
677 /* GLint so the compiler won't complain about type signedness mismatch
678 * in the call to _mesa_texture_parameteriv below.
680 const GLint param
= blit
->stencilSamplingSave
?
681 GL_STENCIL_INDEX
: GL_DEPTH_COMPONENT
;
683 _mesa_texture_parameteriv(ctx
, texObj
, GL_DEPTH_STENCIL_TEXTURE_MODE
,
688 _mesa_bind_sampler(ctx
, ctx
->Texture
.CurrentUnit
, blit
->samp_obj_save
);
689 _mesa_reference_sampler_object(ctx
, &blit
->samp_obj_save
, NULL
);
690 _mesa_reference_sampler_object(ctx
, &blit
->samp_obj
, NULL
);
691 _mesa_delete_nameless_texture(ctx
, blit
->temp_tex_obj
);
694 static struct gl_texture_object
*
695 texture_object_from_renderbuffer(struct gl_context
*ctx
,
696 struct gl_renderbuffer
*rb
)
698 struct gl_texture_image
*texImage
;
699 struct gl_texture_object
*texObj
;
700 const GLenum target
= rb
->NumSamples
> 1
701 ? GL_TEXTURE_2D_MULTISAMPLE
: GL_TEXTURE_2D
;
703 texObj
= ctx
->Driver
.NewTextureObject(ctx
, 0xDEADBEEF, target
);
704 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, 0);
706 if (!ctx
->Driver
.BindRenderbufferTexImage(ctx
, rb
, texImage
)) {
707 _mesa_delete_nameless_texture(ctx
, texObj
);
711 if (ctx
->Driver
.FinishRenderTexture
&& !rb
->NeedsFinishRenderTexture
) {
712 rb
->NeedsFinishRenderTexture
= true;
713 ctx
->Driver
.FinishRenderTexture(ctx
, rb
);
719 static struct gl_sampler_object
*
720 setup_sampler(struct gl_context
*ctx
, struct gl_texture_object
*texObj
,
721 GLenum target
, GLenum filter
, GLuint srcLevel
)
723 struct gl_sampler_object
*samp_obj
;
724 GLenum tex_filter
= (filter
== GL_SCALED_RESOLVE_FASTEST_EXT
||
725 filter
== GL_SCALED_RESOLVE_NICEST_EXT
) ?
728 samp_obj
= ctx
->Driver
.NewSamplerObject(ctx
, 0xDEADBEEF);
729 if (samp_obj
== NULL
)
732 _mesa_bind_sampler(ctx
, ctx
->Texture
.CurrentUnit
, samp_obj
);
733 _mesa_set_sampler_filters(ctx
, samp_obj
, tex_filter
, tex_filter
);
734 _mesa_set_sampler_wrap(ctx
, samp_obj
, GL_CLAMP_TO_EDGE
, GL_CLAMP_TO_EDGE
,
737 /* Prepare src texture state */
738 _mesa_bind_texture(ctx
, target
, texObj
);
739 if (target
!= GL_TEXTURE_RECTANGLE_ARB
) {
740 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_BASE_LEVEL
,
741 (GLint
*) &srcLevel
, false);
742 _mesa_texture_parameteriv(ctx
, texObj
, GL_TEXTURE_MAX_LEVEL
,
743 (GLint
*) &srcLevel
, false);
750 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
751 * of texture mapping and polygon rendering.
754 _mesa_meta_BlitFramebuffer(struct gl_context
*ctx
,
755 const struct gl_framebuffer
*readFb
,
756 const struct gl_framebuffer
*drawFb
,
757 GLint srcX0
, GLint srcY0
, GLint srcX1
, GLint srcY1
,
758 GLint dstX0
, GLint dstY0
, GLint dstX1
, GLint dstY1
,
759 GLbitfield mask
, GLenum filter
)
761 const GLint dstW
= abs(dstX1
- dstX0
);
762 const GLint dstH
= abs(dstY1
- dstY0
);
763 const GLint dstFlipX
= (dstX1
- dstX0
) / dstW
;
764 const GLint dstFlipY
= (dstY1
- dstY0
) / dstH
;
767 GLint srcX0
, srcY0
, srcX1
, srcY1
;
768 GLint dstX0
, dstY0
, dstX1
, dstY1
;
770 srcX0
, srcY0
, srcX1
, srcY1
,
771 dstX0
, dstY0
, dstX1
, dstY1
774 const GLboolean use_glsl_version
= ctx
->Extensions
.ARB_vertex_shader
&&
775 ctx
->Extensions
.ARB_fragment_shader
;
777 /* Multisample texture blit support requires texture multisample. */
778 if (readFb
->Visual
.samples
> 0 &&
779 !ctx
->Extensions
.ARB_texture_multisample
) {
783 /* Clip a copy of the blit coordinates. If these differ from the input
784 * coordinates, then we'll set the scissor.
786 if (!_mesa_clip_blit(ctx
, readFb
, drawFb
,
787 &clip
.srcX0
, &clip
.srcY0
, &clip
.srcX1
, &clip
.srcY1
,
788 &clip
.dstX0
, &clip
.dstY0
, &clip
.dstX1
, &clip
.dstY1
)) {
789 /* clipped/scissored everything away */
793 /* Only scissor and FRAMEBUFFER_SRGB affect blit. Leave sRGB alone, but
794 * save restore scissor as we'll set a custom scissor if necessary.
796 _mesa_meta_begin(ctx
, MESA_META_ALL
&
797 ~(MESA_META_DRAW_BUFFERS
|
798 MESA_META_FRAMEBUFFER_SRGB
));
800 /* Dithering shouldn't be performed for glBlitFramebuffer */
801 _mesa_set_enable(ctx
, GL_DITHER
, GL_FALSE
);
803 /* If the clipping earlier changed the destination rect at all, then
804 * enable the scissor to clip to it.
806 if (clip
.dstX0
!= dstX0
|| clip
.dstY0
!= dstY0
||
807 clip
.dstX1
!= dstX1
|| clip
.dstY1
!= dstY1
) {
808 _mesa_set_enable(ctx
, GL_SCISSOR_TEST
, GL_TRUE
);
809 _mesa_Scissor(MIN2(clip
.dstX0
, clip
.dstX1
),
810 MIN2(clip
.dstY0
, clip
.dstY1
),
811 abs(clip
.dstX0
- clip
.dstX1
),
812 abs(clip
.dstY0
- clip
.dstY1
));
815 /* Try faster, direct texture approach first */
816 if (mask
& GL_COLOR_BUFFER_BIT
) {
817 if (blitframebuffer_texture(ctx
, readFb
, drawFb
,
818 srcX0
, srcY0
, srcX1
, srcY1
,
819 dstX0
, dstY0
, dstX1
, dstY1
,
820 filter
, dstFlipX
, dstFlipY
,
821 use_glsl_version
, false)) {
822 mask
&= ~GL_COLOR_BUFFER_BIT
;
826 if (mask
& GL_DEPTH_BUFFER_BIT
&& use_glsl_version
) {
827 if (blitframebuffer_texture(ctx
, readFb
, drawFb
,
828 srcX0
, srcY0
, srcX1
, srcY1
,
829 dstX0
, dstY0
, dstX1
, dstY1
,
830 filter
, dstFlipX
, dstFlipY
,
831 use_glsl_version
, true)) {
832 mask
&= ~GL_DEPTH_BUFFER_BIT
;
836 if (mask
& GL_STENCIL_BUFFER_BIT
) {
837 /* XXX can't easily do stencil */
846 _mesa_meta_glsl_blit_cleanup(struct gl_context
*ctx
, struct blit_state
*blit
)
849 _mesa_DeleteVertexArrays(1, &blit
->VAO
);
851 _mesa_reference_buffer_object(ctx
, &blit
->buf_obj
, NULL
);
854 _mesa_meta_blit_shader_table_cleanup(ctx
, &blit
->shaders_with_depth
);
855 _mesa_meta_blit_shader_table_cleanup(ctx
, &blit
->shaders_without_depth
);
857 if (blit
->depthTex
.tex_obj
!= NULL
) {
858 _mesa_delete_nameless_texture(ctx
, blit
->depthTex
.tex_obj
);
859 blit
->depthTex
.tex_obj
= NULL
;
864 _mesa_meta_and_swrast_BlitFramebuffer(struct gl_context
*ctx
,
865 struct gl_framebuffer
*readFb
,
866 struct gl_framebuffer
*drawFb
,
867 GLint srcX0
, GLint srcY0
,
868 GLint srcX1
, GLint srcY1
,
869 GLint dstX0
, GLint dstY0
,
870 GLint dstX1
, GLint dstY1
,
871 GLbitfield mask
, GLenum filter
)
873 mask
= _mesa_meta_BlitFramebuffer(ctx
, readFb
, drawFb
,
874 srcX0
, srcY0
, srcX1
, srcY1
,
875 dstX0
, dstY0
, dstX1
, dstY1
,
880 _swrast_BlitFramebuffer(ctx
, readFb
, drawFb
,
881 srcX0
, srcY0
, srcX1
, srcY1
,
882 dstX0
, dstY0
, dstX1
, dstY1
,