a01e33f9a660a81b8289becc8e6598a2c141c93e
[mesa.git] / src / mesa / drivers / common / meta_blit.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
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.
23 */
24
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"
53
54 static struct gl_texture_object *
55 texture_object_from_renderbuffer(struct gl_context *, struct gl_renderbuffer *);
56
57 static struct gl_sampler_object *
58 setup_sampler(struct gl_context *, struct gl_texture_object *, GLenum target,
59 GLenum filter, GLuint srcLevel);
60
61 /** Return offset in bytes of the field within a vertex struct */
62 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
63
64 static void
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,
69 GLenum target)
70 {
71 const char *vs_source;
72 char *fs_source;
73 void *mem_ctx;
74 enum blit_msaa_shader shader_index;
75 bool dst_is_msaa = false;
76 GLenum src_datatype;
77 const char *vec4_prefix;
78 const char *sampler_array_suffix = "";
79 char *name;
80 const char *texcoord_type = "vec2";
81 int samples;
82 int shader_offset = 0;
83
84 if (src_rb) {
85 samples = MAX2(src_rb->NumSamples, 1);
86 src_datatype = _mesa_get_format_datatype(src_rb->Format);
87 } else {
88 /* depth-or-color glCopyTexImage fallback path that passes a NULL rb and
89 * doesn't handle integer.
90 */
91 samples = 1;
92 src_datatype = GL_UNSIGNED_NORMALIZED;
93 }
94
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)) {
98 shader_offset++;
99 }
100 /* Update the assert if we plan to support more than 16X MSAA. */
101 assert(shader_offset >= 0 && shader_offset <= 4);
102
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.
107 */
108 assert(ctx->Extensions.ARB_sample_shading);
109
110 dst_is_msaa = true;
111
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);
116 }
117
118 switch (target) {
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)) {
123 if (dst_is_msaa)
124 shader_index = BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY;
125 else
126 shader_index = BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE;
127 } else {
128 if (dst_is_msaa)
129 shader_index = BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY;
130 else {
131 shader_index = BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE +
132 shader_offset;
133 }
134 }
135
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";
141 }
142 break;
143 default:
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;
147 }
148
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) {
155 shader_index += 5;
156 vec4_prefix = "i";
157 } else if (src_datatype == GL_UNSIGNED_INT) {
158 shader_index += 10;
159 vec4_prefix = "u";
160 } else {
161 vec4_prefix = "";
162 }
163
164 if (blit->msaa_shaders[shader_index]) {
165 _mesa_meta_use_program(ctx, blit->msaa_shaders[shader_index]);
166 return;
167 }
168
169 mem_ctx = ralloc_context(NULL);
170
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) {
175 char *sample_index;
176 const char *tex_coords = "texCoords";
177
178 if (dst_is_msaa) {
179 sample_index = "gl_SampleID";
180 name = "depth MSAA copy";
181
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))";
185 }
186 } else {
187 /* From the GL 4.3 spec:
188 *
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.
194 *
195 * We're slacking and instead of choosing centermost, we've got 0.
196 */
197 sample_index = "0";
198 name = "depth MSAA resolve";
199 }
200
201 vs_source = ralloc_asprintf(mem_ctx,
202 "#version 130\n"
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"
207 "void main()\n"
208 "{\n"
209 " texCoords = textureCoords;\n"
210 " gl_Position = vec4(position, 0.0, 1.0);\n"
211 "}\n",
212 texcoord_type,
213 texcoord_type);
214 fs_source = ralloc_asprintf(mem_ctx,
215 "#version 130\n"
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"
220 "in %s texCoords;\n"
221 "out vec4 out_color;\n"
222 "\n"
223 "void main()\n"
224 "{\n"
225 " gl_FragDepth = texelFetch(texSampler, i%s(%s), %s).r;\n"
226 "}\n",
227 sampler_array_suffix,
228 texcoord_type,
229 texcoord_type,
230 tex_coords,
231 sample_index);
232 } else {
233 /* You can create 2D_MULTISAMPLE textures with 0 sample count (meaning 1
234 * sample). Yes, this is ridiculous.
235 */
236 char *sample_resolve;
237 const char *merge_function;
238 name = ralloc_asprintf(mem_ctx, "%svec4 MSAA %s",
239 vec4_prefix,
240 dst_is_msaa ? "copy" : "resolve");
241
242 if (dst_is_msaa) {
243 const char *tex_coords;
244
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
257 * these positions.
258 */
259 tex_coords = "interpolateAtOffset(texCoords, vec2(0.0))";
260 } else {
261 tex_coords = "texCoords";
262 }
263
264 sample_resolve =
265 ralloc_asprintf(mem_ctx,
266 " out_color = texelFetch(texSampler, "
267 "i%s(%s), gl_SampleID);",
268 texcoord_type, tex_coords);
269
270 merge_function = "";
271 } else {
272 int i;
273 int step;
274
275 if (src_datatype == GL_INT || src_datatype == GL_UNSIGNED_INT) {
276 /* From the OpenGL ES 3.2 spec section 16.2.1:
277 *
278 * "If the source formats are integer types or stencil values,
279 * a single sample's value is selected for each pixel."
280 *
281 * The OpenGL 4.4 spec contains exactly the same language.
282 *
283 * We can accomplish this by making the merge function return just
284 * one of the two samples. The compiler should do the rest.
285 */
286 merge_function = "gvec4 merge(gvec4 a, gvec4 b) { return a; }\n";
287 } else {
288 /* The divide will happen at the end for floats. */
289 merge_function =
290 "vec4 merge(vec4 a, vec4 b) { return (a + b); }\n";
291 }
292
293 /* We're assuming power of two samples for this resolution procedure.
294 *
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.
299 */
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);
307 }
308 /* Now, merge each pair of samples, then merge each pair of those,
309 * etc.
310 */
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",
315 step, i,
316 step / 2, i,
317 step / 2, i + step / 2);
318 }
319 }
320
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",
325 samples);
326 } else {
327 ralloc_asprintf_append(&sample_resolve,
328 " gl_FragColor = sample_%d_0 / %f;\n",
329 samples, (float)samples);
330 }
331 }
332
333 vs_source = ralloc_asprintf(mem_ctx,
334 "#version 130\n"
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"
339 "void main()\n"
340 "{\n"
341 " texCoords = textureCoords;\n"
342 " gl_Position = vec4(position, 0.0, 1.0);\n"
343 "}\n",
344 texcoord_type,
345 texcoord_type);
346 fs_source = ralloc_asprintf(mem_ctx,
347 "#version 130\n"
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"
353 "in %s texCoords;\n"
354 "out gvec4 out_color;\n"
355 "\n"
356 "%s" /* merge_function */
357 "void main()\n"
358 "{\n"
359 "%s\n" /* sample_resolve */
360 "}\n",
361 vec4_prefix,
362 vec4_prefix,
363 sampler_array_suffix,
364 texcoord_type,
365 merge_function,
366 sample_resolve);
367 }
368
369 _mesa_meta_compile_and_link_program(ctx, vs_source, fs_source, name,
370 &blit->msaa_shaders[shader_index]);
371
372 ralloc_free(mem_ctx);
373 }
374
375 static void
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,
381 bool is_scaled_blit,
382 bool do_depth)
383 {
384 unsigned texcoord_size;
385 bool is_target_multisample = target == GL_TEXTURE_2D_MULTISAMPLE ||
386 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
387
388 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
389 assert(_mesa_is_desktop_gl(ctx) || target == GL_TEXTURE_2D);
390
391 texcoord_size = 2 + (src_rb->Depth > 1 ? 1 : 0);
392
393 _mesa_meta_setup_vertex_objects(ctx, &blit->VAO, &blit->buf_obj, true,
394 2, texcoord_size, 0);
395
396 if (is_target_multisample) {
397 setup_glsl_msaa_blit_shader(ctx, blit, drawFb, src_rb, target);
398 } else {
399 _mesa_meta_setup_blit_shader(ctx, target, do_depth,
400 do_depth ? &blit->shaders_with_depth
401 : &blit->shaders_without_depth);
402 }
403 }
404
405 /**
406 * Try to do a color or depth glBlitFramebuffer using texturing.
407 *
408 * We can do this when the src renderbuffer is actually a texture, or when the
409 * driver exposes BindRenderbufferTexImage().
410 */
411 static bool
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)
419 {
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;
433 GLuint srcLevel;
434 GLenum target;
435 struct gl_renderbuffer *rb = readAtt->Renderbuffer;
436 struct temp_texture *meta_temp_texture;
437
438 if (rb->NumSamples && !ctx->Extensions.ARB_texture_multisample)
439 return false;
440
441 _mesa_meta_fb_tex_blit_begin(ctx, &fb_tex_blit);
442
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
449 * it directly.
450 */
451 srcLevel = readAtt->TextureLevel;
452 texObj = readAtt->Texture;
453 } else if (!readAtt->Texture && ctx->Driver.BindRenderbufferTexImage) {
454 texObj = texture_object_from_renderbuffer(ctx, rb);
455 if (texObj == NULL)
456 return false;
457
458 fb_tex_blit.temp_tex_obj = texObj;
459
460 srcLevel = 0;
461 if (_mesa_is_winsys_fbo(readFb)) {
462 GLint temp = srcY0;
463 srcY0 = rb->Height - srcY1;
464 srcY1 = rb->Height - temp;
465 flipY = -flipY;
466 }
467 } else {
468 GLenum tex_base_format;
469 /* Fall back to doing a CopyTexSubImage to get the destination
470 * renderbuffer into a texture.
471 */
472 if (ctx->Meta->Blit.no_ctsi_fallback)
473 return false;
474
475 if (rb->NumSamples > 1)
476 return false;
477
478 if (do_depth) {
479 meta_temp_texture = _mesa_meta_get_temp_depth_texture(ctx);
480 tex_base_format = GL_DEPTH_COMPONENT;
481 } else {
482 meta_temp_texture = _mesa_meta_get_temp_texture(ctx);
483 tex_base_format =
484 _mesa_base_tex_format(ctx, rb->InternalFormat);
485 }
486
487 srcLevel = 0;
488 texObj = meta_temp_texture->tex_obj;
489 if (texObj == NULL) {
490 return false;
491 }
492
493 _mesa_meta_setup_copypix_texture(ctx, meta_temp_texture,
494 srcX0, srcY0,
495 srcW, srcH,
496 tex_base_format,
497 filter);
498
499 assert(texObj->Target == meta_temp_texture->Target);
500
501 srcX0 = 0;
502 srcY0 = 0;
503 srcX1 = srcW;
504 srcY1 = srcH;
505 }
506
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;
512
513 scaled_blit = dstW != srcW || dstH != srcH;
514
515 if (glsl_version) {
516 setup_glsl_blit_framebuffer(ctx, blit, drawFb, rb, target, filter, scaled_blit,
517 do_depth);
518 }
519 else {
520 _mesa_meta_setup_ff_tnl_for_blit(ctx,
521 &ctx->Meta->Blit.VAO,
522 &ctx->Meta->Blit.buf_obj,
523 2);
524 }
525
526 /*
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);
530 */
531
532 fb_tex_blit.samp_obj = setup_sampler(ctx, texObj, target, filter, srcLevel);
533
534 if (ctx->Extensions.EXT_texture_sRGB_decode) {
535 /* The GL 4.4 spec, section 18.3.1 ("Blitting Pixel Rectangles") says:
536 *
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
542 * equation 3.24.
543 *
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)."
548 *
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.
552 */
553 const bool decode =
554 ctx->Color.sRGBEnabled && _mesa_is_format_srgb(rb->Format);
555
556 _mesa_set_sampler_srgb_decode(ctx, fb_tex_blit.samp_obj,
557 decode ? GL_DECODE_EXT
558 : GL_SKIP_DECODE_EXT);
559 }
560
561 if (!glsl_version) {
562 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
563 _mesa_set_enable(ctx, target, GL_TRUE);
564 }
565
566 /* Prepare vertex data (the VBO was previously created and bound) */
567 {
568 struct vertex verts[4];
569 GLfloat s0, t0, s1, t1;
570
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;
578 }
579 else {
580 assert(target == GL_TEXTURE_RECTANGLE_ARB ||
581 target == GL_TEXTURE_2D_MULTISAMPLE ||
582 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY);
583 s0 = (float) srcX0;
584 s1 = (float) srcX1;
585 t0 = (float) srcY0;
586 t1 = (float) srcY1;
587 }
588
589 /* Silence valgrind warnings about reading uninitialized stack. */
590 memset(verts, 0, sizeof(verts));
591
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;
601
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;
614
615 _mesa_buffer_sub_data(ctx, blit->buf_obj, 0, sizeof(verts), verts);
616 }
617
618 /* setup viewport */
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);
624
625 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
626 _mesa_meta_fb_tex_blit_end(ctx, target, &fb_tex_blit);
627
628 return true;
629 }
630
631 void
632 _mesa_meta_fb_tex_blit_begin(struct gl_context *ctx,
633 struct fb_tex_blit_state *blit)
634 {
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.
639 *
640 * Since the state isn't persistent across calls, we won't catch ref
641 * counting problems.
642 */
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;
647 }
648
649 void
650 _mesa_meta_fb_tex_blit_end(struct gl_context *ctx, GLenum target,
651 struct fb_tex_blit_state *blit)
652 {
653 struct gl_texture_object *const texObj =
654 _mesa_get_current_tex_object(ctx, target);
655
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);
658
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.
662 */
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.
666 */
667 if (blit->baseLevelSave != texObj->BaseLevel)
668 _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_BASE_LEVEL,
669 &blit->baseLevelSave, false);
670
671 if (blit->maxLevelSave != texObj->MaxLevel)
672 _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_MAX_LEVEL,
673 &blit->maxLevelSave, false);
674
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.
679 */
680 const GLint param = blit->stencilSamplingSave ?
681 GL_STENCIL_INDEX : GL_DEPTH_COMPONENT;
682
683 _mesa_texture_parameteriv(ctx, texObj, GL_DEPTH_STENCIL_TEXTURE_MODE,
684 &param, false);
685 }
686 }
687
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);
692 }
693
694 static struct gl_texture_object *
695 texture_object_from_renderbuffer(struct gl_context *ctx,
696 struct gl_renderbuffer *rb)
697 {
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;
702
703 texObj = ctx->Driver.NewTextureObject(ctx, 0xDEADBEEF, target);
704 texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
705
706 if (!ctx->Driver.BindRenderbufferTexImage(ctx, rb, texImage)) {
707 _mesa_delete_nameless_texture(ctx, texObj);
708 return NULL;
709 }
710
711 if (ctx->Driver.FinishRenderTexture && !rb->NeedsFinishRenderTexture) {
712 rb->NeedsFinishRenderTexture = true;
713 ctx->Driver.FinishRenderTexture(ctx, rb);
714 }
715
716 return texObj;
717 }
718
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)
722 {
723 struct gl_sampler_object *samp_obj;
724 GLenum tex_filter = (filter == GL_SCALED_RESOLVE_FASTEST_EXT ||
725 filter == GL_SCALED_RESOLVE_NICEST_EXT) ?
726 GL_NEAREST : filter;
727
728 samp_obj = ctx->Driver.NewSamplerObject(ctx, 0xDEADBEEF);
729 if (samp_obj == NULL)
730 return NULL;
731
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,
735 samp_obj->WrapR);
736
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);
744 }
745
746 return samp_obj;
747 }
748
749 /**
750 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
751 * of texture mapping and polygon rendering.
752 */
753 GLbitfield
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)
760 {
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;
765
766 struct {
767 GLint srcX0, srcY0, srcX1, srcY1;
768 GLint dstX0, dstY0, dstX1, dstY1;
769 } clip = {
770 srcX0, srcY0, srcX1, srcY1,
771 dstX0, dstY0, dstX1, dstY1
772 };
773
774 const GLboolean use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
775 ctx->Extensions.ARB_fragment_shader;
776
777 /* Multisample texture blit support requires texture multisample. */
778 if (readFb->Visual.samples > 0 &&
779 !ctx->Extensions.ARB_texture_multisample) {
780 return mask;
781 }
782
783 /* Clip a copy of the blit coordinates. If these differ from the input
784 * coordinates, then we'll set the scissor.
785 */
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 */
790 return 0;
791 }
792
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.
795 */
796 _mesa_meta_begin(ctx, MESA_META_ALL &
797 ~(MESA_META_DRAW_BUFFERS |
798 MESA_META_FRAMEBUFFER_SRGB));
799
800 /* Dithering shouldn't be performed for glBlitFramebuffer */
801 _mesa_set_enable(ctx, GL_DITHER, GL_FALSE);
802
803 /* If the clipping earlier changed the destination rect at all, then
804 * enable the scissor to clip to it.
805 */
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));
813 }
814
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;
823 }
824 }
825
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;
833 }
834 }
835
836 if (mask & GL_STENCIL_BUFFER_BIT) {
837 /* XXX can't easily do stencil */
838 }
839
840 _mesa_meta_end(ctx);
841
842 return mask;
843 }
844
845 void
846 _mesa_meta_glsl_blit_cleanup(struct gl_context *ctx, struct blit_state *blit)
847 {
848 if (blit->VAO) {
849 _mesa_DeleteVertexArrays(1, &blit->VAO);
850 blit->VAO = 0;
851 _mesa_reference_buffer_object(ctx, &blit->buf_obj, NULL);
852 }
853
854 _mesa_meta_blit_shader_table_cleanup(ctx, &blit->shaders_with_depth);
855 _mesa_meta_blit_shader_table_cleanup(ctx, &blit->shaders_without_depth);
856
857 if (blit->depthTex.tex_obj != NULL) {
858 _mesa_delete_nameless_texture(ctx, blit->depthTex.tex_obj);
859 blit->depthTex.tex_obj = NULL;
860 }
861 }
862
863 void
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)
872 {
873 mask = _mesa_meta_BlitFramebuffer(ctx, readFb, drawFb,
874 srcX0, srcY0, srcX1, srcY1,
875 dstX0, dstY0, dstX1, dstY1,
876 mask, filter);
877 if (mask == 0x0)
878 return;
879
880 _swrast_BlitFramebuffer(ctx, readFb, drawFb,
881 srcX0, srcY0, srcX1, srcY1,
882 dstX0, dstY0, dstX1, dstY1,
883 mask, filter);
884 }