b92c2e2f22bc874ded85bbdfba8fce5d8c176afe
[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/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"
54
55 /** Return offset in bytes of the field within a vertex struct */
56 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
57
58 static void
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)
63 {
64 GLint loc_src_width, loc_src_height;
65 int i, samples;
66 int shader_offset = 0;
67 void *mem_ctx = ralloc_context(NULL);
68 char *fs_source;
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;
77
78 assert(src_rb);
79 samples = MAX2(src_rb->NumSamples, 1);
80
81 if (samples == 16)
82 x_scale = 4.0;
83 else
84 x_scale = 2.0;
85 y_scale = samples / x_scale;
86
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)) {
90 shader_offset++;
91 }
92 /* Update the assert if we plan to support more than 16X MSAA. */
93 assert(shader_offset > 0 && shader_offset <= 4);
94
95 assert(target == GL_TEXTURE_2D_MULTISAMPLE ||
96 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY);
97
98 shader_index = BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_SCALED_RESOLVE +
99 shader_offset - 1;
100
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";
105 }
106
107 if (blit->msaa_shaders[shader_index]) {
108 _mesa_UseProgram(blit->msaa_shaders[shader_index]);
109 /* Update the uniform values. */
110 loc_src_width =
111 _mesa_GetUniformLocation(blit->msaa_shaders[shader_index], "src_width");
112 loc_src_height =
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);
116 return;
117 }
118
119 name = ralloc_asprintf(mem_ctx, "vec4 MSAA scaled resolve");
120
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.
123 */
124 switch(samples) {
125 case 2:
126 sample_number = "sample_map[int(2 * fract(coord.x))]";
127 sample_map = ctx->Const.SampleMap2x;
128 break;
129 case 4:
130 sample_number = "sample_map[int(2 * fract(coord.x) + 4 * fract(coord.y))]";
131 sample_map = ctx->Const.SampleMap4x;
132 break;
133 case 8:
134 sample_number = "sample_map[int(2 * fract(coord.x) + 8 * fract(coord.y))]";
135 sample_map = ctx->Const.SampleMap8x;
136 break;
137 case 16:
138 sample_number = "sample_map[int(4 * fract(coord.x) + 16 * fract(coord.y))]";
139 sample_map = ctx->Const.SampleMap16x;
140 break;
141 default:
142 sample_number = NULL;
143 sample_map = NULL;
144 _mesa_problem(ctx, "Unsupported sample count %d\n", samples);
145 unreachable("Unsupported sample count");
146 }
147
148 /* Create sample map string. */
149 for (i = 0 ; i < samples - 1; i++) {
150 ralloc_asprintf_append(&sample_map_str, "%d, ", sample_map[i]);
151 }
152 ralloc_asprintf_append(&sample_map_str, "%d", sample_map[samples - 1]);
153
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);
158
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",
162 sample_number);
163 } else {
164 ralloc_asprintf_append(&texel_fetch_macro,
165 "#define TEXEL_FETCH(coord) texelFetch(texSampler, ivec3(coord, layer), %s);\n",
166 sample_number);
167 }
168
169 static const char vs_source[] =
170 "#version 130\n"
171 "in vec2 position;\n"
172 "in vec3 textureCoords;\n"
173 "out vec2 texCoords;\n"
174 "flat out int layer;\n"
175 "void main()\n"
176 "{\n"
177 " texCoords = textureCoords.xy;\n"
178 " layer = int(textureCoords.z);\n"
179 " gl_Position = vec4(position, 0.0, 1.0);\n"
180 "}\n"
181 ;
182
183 fs_source = ralloc_asprintf(mem_ctx,
184 "#version 130\n"
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"
191 "\n"
192 "void main()\n"
193 "{\n"
194 "%s"
195 " vec2 interp;\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"
203 "\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"
209 "\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"
215 "\n"
216 " /* Fetch sample color values. */\n"
217 "%s"
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"
223 "\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"
228 "}\n",
229 sampler_array_suffix,
230 sample_map_expr,
231 x_scale, y_scale,
232 1.0f / x_scale, 1.0f / y_scale,
233 0.5f / x_scale, 0.5f / y_scale,
234 texel_fetch_macro);
235
236 _mesa_meta_compile_and_link_program(ctx, vs_source, fs_source, name,
237 &blit->msaa_shaders[shader_index]);
238 loc_src_width =
239 _mesa_GetUniformLocation(blit->msaa_shaders[shader_index], "src_width");
240 loc_src_height =
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);
244
245 ralloc_free(mem_ctx);
246 }
247
248 static void
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,
253 GLenum target)
254 {
255 const char *vs_source;
256 char *fs_source;
257 void *mem_ctx;
258 enum blit_msaa_shader shader_index;
259 bool dst_is_msaa = false;
260 GLenum src_datatype;
261 const char *vec4_prefix;
262 const char *sampler_array_suffix = "";
263 char *name;
264 const char *texcoord_type = "vec2";
265 int samples;
266 int shader_offset = 0;
267
268 if (src_rb) {
269 samples = MAX2(src_rb->NumSamples, 1);
270 src_datatype = _mesa_get_format_datatype(src_rb->Format);
271 } else {
272 /* depth-or-color glCopyTexImage fallback path that passes a NULL rb and
273 * doesn't handle integer.
274 */
275 samples = 1;
276 src_datatype = GL_UNSIGNED_NORMALIZED;
277 }
278
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)) {
282 shader_offset++;
283 }
284 /* Update the assert if we plan to support more than 16X MSAA. */
285 assert(shader_offset >= 0 && shader_offset <= 4);
286
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.
291 */
292 assert(ctx->Extensions.ARB_sample_shading);
293
294 dst_is_msaa = true;
295
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);
300 }
301
302 switch (target) {
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)) {
307 if (dst_is_msaa)
308 shader_index = BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY;
309 else
310 shader_index = BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE;
311 } else {
312 if (dst_is_msaa)
313 shader_index = BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY;
314 else {
315 shader_index = BLIT_1X_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE +
316 shader_offset;
317 }
318 }
319
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";
325 }
326 break;
327 default:
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;
331 }
332
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) {
339 shader_index += 5;
340 vec4_prefix = "i";
341 } else if (src_datatype == GL_UNSIGNED_INT) {
342 shader_index += 10;
343 vec4_prefix = "u";
344 } else {
345 vec4_prefix = "";
346 }
347
348 if (blit->msaa_shaders[shader_index]) {
349 _mesa_UseProgram(blit->msaa_shaders[shader_index]);
350 return;
351 }
352
353 mem_ctx = ralloc_context(NULL);
354
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) {
359 char *sample_index;
360 const char *arb_sample_shading_extension_string;
361
362 if (dst_is_msaa) {
363 arb_sample_shading_extension_string = "#extension GL_ARB_sample_shading : enable";
364 sample_index = "gl_SampleID";
365 name = "depth MSAA copy";
366 } else {
367 /* Don't need that extension, since we're drawing to a single-sampled
368 * destination.
369 */
370 arb_sample_shading_extension_string = "";
371 /* From the GL 4.3 spec:
372 *
373 * "If there is a multisample buffer (the value of SAMPLE_BUFFERS
374 * is one), then values are obtained from the depth samples in
375 * this buffer. It is recommended that the depth value of the
376 * centermost sample be used, though implementations may choose
377 * any function of the depth sample values at each pixel.
378 *
379 * We're slacking and instead of choosing centermost, we've got 0.
380 */
381 sample_index = "0";
382 name = "depth MSAA resolve";
383 }
384
385 vs_source = ralloc_asprintf(mem_ctx,
386 "#version 130\n"
387 "in vec2 position;\n"
388 "in %s textureCoords;\n"
389 "out %s texCoords;\n"
390 "void main()\n"
391 "{\n"
392 " texCoords = textureCoords;\n"
393 " gl_Position = vec4(position, 0.0, 1.0);\n"
394 "}\n",
395 texcoord_type,
396 texcoord_type);
397 fs_source = ralloc_asprintf(mem_ctx,
398 "#version 130\n"
399 "#extension GL_ARB_texture_multisample : enable\n"
400 "%s\n"
401 "uniform sampler2DMS%s texSampler;\n"
402 "in %s texCoords;\n"
403 "out vec4 out_color;\n"
404 "\n"
405 "void main()\n"
406 "{\n"
407 " gl_FragDepth = texelFetch(texSampler, i%s(texCoords), %s).r;\n"
408 "}\n",
409 arb_sample_shading_extension_string,
410 sampler_array_suffix,
411 texcoord_type,
412 texcoord_type,
413 sample_index);
414 } else {
415 /* You can create 2D_MULTISAMPLE textures with 0 sample count (meaning 1
416 * sample). Yes, this is ridiculous.
417 */
418 char *sample_resolve;
419 const char *arb_sample_shading_extension_string;
420 const char *merge_function;
421 name = ralloc_asprintf(mem_ctx, "%svec4 MSAA %s",
422 vec4_prefix,
423 dst_is_msaa ? "copy" : "resolve");
424
425 if (dst_is_msaa) {
426 arb_sample_shading_extension_string = "#extension GL_ARB_sample_shading : enable";
427 sample_resolve = ralloc_asprintf(mem_ctx, " out_color = texelFetch(texSampler, i%s(texCoords), gl_SampleID);", texcoord_type);
428 merge_function = "";
429 } else {
430 int i;
431 int step;
432
433 if (src_datatype == GL_INT || src_datatype == GL_UNSIGNED_INT) {
434 merge_function =
435 "gvec4 merge(gvec4 a, gvec4 b) { return (a >> gvec4(1)) + (b >> gvec4(1)) + (a & b & gvec4(1)); }\n";
436 } else {
437 /* The divide will happen at the end for floats. */
438 merge_function =
439 "vec4 merge(vec4 a, vec4 b) { return (a + b); }\n";
440 }
441
442 arb_sample_shading_extension_string = "";
443
444 /* We're assuming power of two samples for this resolution procedure.
445 *
446 * To avoid losing any floating point precision if the samples all
447 * happen to have the same value, we merge pairs of values at a time
448 * (so the floating point exponent just gets increased), rather than
449 * doing a naive sum and dividing.
450 */
451 assert(_mesa_is_pow_two(samples));
452 /* Fetch each individual sample. */
453 sample_resolve = rzalloc_size(mem_ctx, 1);
454 for (i = 0; i < samples; i++) {
455 ralloc_asprintf_append(&sample_resolve,
456 " gvec4 sample_1_%d = texelFetch(texSampler, i%s(texCoords), %d);\n",
457 i, texcoord_type, i);
458 }
459 /* Now, merge each pair of samples, then merge each pair of those,
460 * etc.
461 */
462 for (step = 2; step <= samples; step *= 2) {
463 for (i = 0; i < samples; i += step) {
464 ralloc_asprintf_append(&sample_resolve,
465 " gvec4 sample_%d_%d = merge(sample_%d_%d, sample_%d_%d);\n",
466 step, i,
467 step / 2, i,
468 step / 2, i + step / 2);
469 }
470 }
471
472 /* Scale the final result. */
473 if (src_datatype == GL_UNSIGNED_INT || src_datatype == GL_INT) {
474 ralloc_asprintf_append(&sample_resolve,
475 " out_color = sample_%d_0;\n",
476 samples);
477 } else {
478 ralloc_asprintf_append(&sample_resolve,
479 " gl_FragColor = sample_%d_0 / %f;\n",
480 samples, (float)samples);
481 }
482 }
483
484 vs_source = ralloc_asprintf(mem_ctx,
485 "#version 130\n"
486 "in vec2 position;\n"
487 "in %s textureCoords;\n"
488 "out %s texCoords;\n"
489 "void main()\n"
490 "{\n"
491 " texCoords = textureCoords;\n"
492 " gl_Position = vec4(position, 0.0, 1.0);\n"
493 "}\n",
494 texcoord_type,
495 texcoord_type);
496 fs_source = ralloc_asprintf(mem_ctx,
497 "#version 130\n"
498 "#extension GL_ARB_texture_multisample : enable\n"
499 "%s\n"
500 "#define gvec4 %svec4\n"
501 "uniform %ssampler2DMS%s texSampler;\n"
502 "in %s texCoords;\n"
503 "out gvec4 out_color;\n"
504 "\n"
505 "%s" /* merge_function */
506 "void main()\n"
507 "{\n"
508 "%s\n" /* sample_resolve */
509 "}\n",
510 arb_sample_shading_extension_string,
511 vec4_prefix,
512 vec4_prefix,
513 sampler_array_suffix,
514 texcoord_type,
515 merge_function,
516 sample_resolve);
517 }
518
519 _mesa_meta_compile_and_link_program(ctx, vs_source, fs_source, name,
520 &blit->msaa_shaders[shader_index]);
521
522 ralloc_free(mem_ctx);
523 }
524
525 static void
526 setup_glsl_blit_framebuffer(struct gl_context *ctx,
527 struct blit_state *blit,
528 const struct gl_framebuffer *drawFb,
529 struct gl_renderbuffer *src_rb,
530 GLenum target, GLenum filter,
531 bool is_scaled_blit,
532 bool do_depth)
533 {
534 unsigned texcoord_size;
535 bool is_target_multisample = target == GL_TEXTURE_2D_MULTISAMPLE ||
536 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
537 bool is_filter_scaled_resolve = filter == GL_SCALED_RESOLVE_FASTEST_EXT ||
538 filter == GL_SCALED_RESOLVE_NICEST_EXT;
539
540 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
541 assert(_mesa_is_desktop_gl(ctx) || target == GL_TEXTURE_2D);
542
543 texcoord_size = 2 + (src_rb->Depth > 1 ? 1 : 0);
544
545 _mesa_meta_setup_vertex_objects(&blit->VAO, &blit->VBO, true,
546 2, texcoord_size, 0);
547
548 if (is_target_multisample && is_filter_scaled_resolve && is_scaled_blit) {
549 setup_glsl_msaa_blit_scaled_shader(ctx, blit, src_rb, target, filter);
550 } else if (is_target_multisample) {
551 setup_glsl_msaa_blit_shader(ctx, blit, drawFb, src_rb, target);
552 } else {
553 _mesa_meta_setup_blit_shader(ctx, target, do_depth,
554 do_depth ? &blit->shaders_with_depth
555 : &blit->shaders_without_depth);
556 }
557 }
558
559 /**
560 * Try to do a color or depth glBlitFramebuffer using texturing.
561 *
562 * We can do this when the src renderbuffer is actually a texture, or when the
563 * driver exposes BindRenderbufferTexImage().
564 */
565 static bool
566 blitframebuffer_texture(struct gl_context *ctx,
567 const struct gl_framebuffer *readFb,
568 const struct gl_framebuffer *drawFb,
569 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
570 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
571 GLenum filter, GLint flipX, GLint flipY,
572 GLboolean glsl_version, GLboolean do_depth)
573 {
574 int att_index = do_depth ? BUFFER_DEPTH : readFb->_ColorReadBufferIndex;
575 const struct gl_renderbuffer_attachment *readAtt =
576 &readFb->Attachment[att_index];
577 struct blit_state *blit = &ctx->Meta->Blit;
578 struct fb_tex_blit_state fb_tex_blit;
579 const GLint dstX = MIN2(dstX0, dstX1);
580 const GLint dstY = MIN2(dstY0, dstY1);
581 const GLint dstW = abs(dstX1 - dstX0);
582 const GLint dstH = abs(dstY1 - dstY0);
583 const int srcW = abs(srcX1 - srcX0);
584 const int srcH = abs(srcY1 - srcY0);
585 bool scaled_blit = false;
586 struct gl_texture_object *texObj;
587 GLuint srcLevel;
588 GLenum target;
589 struct gl_renderbuffer *rb = readAtt->Renderbuffer;
590 struct temp_texture *meta_temp_texture;
591
592 if (rb->NumSamples && !ctx->Extensions.ARB_texture_multisample)
593 return false;
594
595 _mesa_meta_fb_tex_blit_begin(ctx, &fb_tex_blit);
596
597 if (readAtt->Texture &&
598 (readAtt->Texture->Target == GL_TEXTURE_2D ||
599 readAtt->Texture->Target == GL_TEXTURE_RECTANGLE ||
600 readAtt->Texture->Target == GL_TEXTURE_2D_MULTISAMPLE ||
601 readAtt->Texture->Target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)) {
602 /* If there's a texture attached of a type we can handle, then just use
603 * it directly.
604 */
605 srcLevel = readAtt->TextureLevel;
606 texObj = readAtt->Texture;
607 target = texObj->Target;
608 } else if (!readAtt->Texture && ctx->Driver.BindRenderbufferTexImage) {
609 if (!_mesa_meta_bind_rb_as_tex_image(ctx, rb, &fb_tex_blit.tempTex,
610 &texObj, &target))
611 return false;
612
613 srcLevel = 0;
614 if (_mesa_is_winsys_fbo(readFb)) {
615 GLint temp = srcY0;
616 srcY0 = rb->Height - srcY1;
617 srcY1 = rb->Height - temp;
618 flipY = -flipY;
619 }
620 } else {
621 GLenum tex_base_format;
622 /* Fall back to doing a CopyTexSubImage to get the destination
623 * renderbuffer into a texture.
624 */
625 if (ctx->Meta->Blit.no_ctsi_fallback)
626 return false;
627
628 if (rb->NumSamples > 1)
629 return false;
630
631 if (do_depth) {
632 meta_temp_texture = _mesa_meta_get_temp_depth_texture(ctx);
633 tex_base_format = GL_DEPTH_COMPONENT;
634 } else {
635 meta_temp_texture = _mesa_meta_get_temp_texture(ctx);
636 tex_base_format =
637 _mesa_base_tex_format(ctx, rb->InternalFormat);
638 }
639
640 srcLevel = 0;
641 target = meta_temp_texture->Target;
642 texObj = _mesa_lookup_texture(ctx, meta_temp_texture->TexObj);
643 if (texObj == NULL) {
644 return false;
645 }
646
647 _mesa_meta_setup_copypix_texture(ctx, meta_temp_texture,
648 srcX0, srcY0,
649 srcW, srcH,
650 tex_base_format,
651 filter);
652
653
654 srcX0 = 0;
655 srcY0 = 0;
656 srcX1 = srcW;
657 srcY1 = srcH;
658 }
659
660 fb_tex_blit.baseLevelSave = texObj->BaseLevel;
661 fb_tex_blit.maxLevelSave = texObj->MaxLevel;
662 fb_tex_blit.stencilSamplingSave = texObj->StencilSampling;
663
664 scaled_blit = dstW != srcW || dstH != srcH;
665
666 if (glsl_version) {
667 setup_glsl_blit_framebuffer(ctx, blit, drawFb, rb, target, filter, scaled_blit,
668 do_depth);
669 }
670 else {
671 _mesa_meta_setup_ff_tnl_for_blit(&ctx->Meta->Blit.VAO,
672 &ctx->Meta->Blit.VBO,
673 2);
674 }
675
676 /*
677 printf("Blit from texture!\n");
678 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
679 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
680 */
681
682 fb_tex_blit.sampler = _mesa_meta_setup_sampler(ctx, texObj, target, filter,
683 srcLevel);
684
685 /* Always do our blits with no net sRGB decode or encode.
686 *
687 * However, if both the src and dst can be srgb decode/encoded, enable them
688 * so that we do any blending (from scaling or from MSAA resolves) in the
689 * right colorspace.
690 *
691 * Our choice of not doing any net encode/decode is from the GL 3.0
692 * specification:
693 *
694 * "Blit operations bypass the fragment pipeline. The only fragment
695 * operations which affect a blit are the pixel ownership test and the
696 * scissor test."
697 *
698 * The GL 4.4 specification disagrees and says that the sRGB part of the
699 * fragment pipeline applies, but this was found to break applications.
700 */
701 if (ctx->Extensions.EXT_texture_sRGB_decode) {
702 if (_mesa_get_format_color_encoding(rb->Format) == GL_SRGB &&
703 drawFb->Visual.sRGBCapable) {
704 _mesa_SamplerParameteri(fb_tex_blit.sampler,
705 GL_TEXTURE_SRGB_DECODE_EXT, GL_DECODE_EXT);
706 _mesa_set_framebuffer_srgb(ctx, GL_TRUE);
707 } else {
708 _mesa_SamplerParameteri(fb_tex_blit.sampler,
709 GL_TEXTURE_SRGB_DECODE_EXT,
710 GL_SKIP_DECODE_EXT);
711 /* set_framebuffer_srgb was set by _mesa_meta_begin(). */
712 }
713 }
714
715 if (!glsl_version) {
716 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
717 _mesa_set_enable(ctx, target, GL_TRUE);
718 }
719
720 /* Prepare vertex data (the VBO was previously created and bound) */
721 {
722 struct vertex verts[4];
723 GLfloat s0, t0, s1, t1;
724
725 if (target == GL_TEXTURE_2D) {
726 const struct gl_texture_image *texImage
727 = _mesa_select_tex_image(texObj, target, srcLevel);
728 s0 = srcX0 / (float) texImage->Width;
729 s1 = srcX1 / (float) texImage->Width;
730 t0 = srcY0 / (float) texImage->Height;
731 t1 = srcY1 / (float) texImage->Height;
732 }
733 else {
734 assert(target == GL_TEXTURE_RECTANGLE_ARB ||
735 target == GL_TEXTURE_2D_MULTISAMPLE ||
736 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY);
737 s0 = (float) srcX0;
738 s1 = (float) srcX1;
739 t0 = (float) srcY0;
740 t1 = (float) srcY1;
741 }
742
743 /* Silence valgrind warnings about reading uninitialized stack. */
744 memset(verts, 0, sizeof(verts));
745
746 /* setup vertex positions */
747 verts[0].x = -1.0F * flipX;
748 verts[0].y = -1.0F * flipY;
749 verts[1].x = 1.0F * flipX;
750 verts[1].y = -1.0F * flipY;
751 verts[2].x = 1.0F * flipX;
752 verts[2].y = 1.0F * flipY;
753 verts[3].x = -1.0F * flipX;
754 verts[3].y = 1.0F * flipY;
755
756 verts[0].tex[0] = s0;
757 verts[0].tex[1] = t0;
758 verts[0].tex[2] = readAtt->Zoffset;
759 verts[1].tex[0] = s1;
760 verts[1].tex[1] = t0;
761 verts[1].tex[2] = readAtt->Zoffset;
762 verts[2].tex[0] = s1;
763 verts[2].tex[1] = t1;
764 verts[2].tex[2] = readAtt->Zoffset;
765 verts[3].tex[0] = s0;
766 verts[3].tex[1] = t1;
767 verts[3].tex[2] = readAtt->Zoffset;
768
769 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
770 }
771
772 /* setup viewport */
773 _mesa_set_viewport(ctx, 0, dstX, dstY, dstW, dstH);
774 _mesa_ColorMask(!do_depth, !do_depth, !do_depth, !do_depth);
775 _mesa_set_enable(ctx, GL_DEPTH_TEST, do_depth);
776 _mesa_DepthMask(do_depth);
777 _mesa_DepthFunc(GL_ALWAYS);
778
779 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
780 _mesa_meta_fb_tex_blit_end(ctx, target, &fb_tex_blit);
781
782 return true;
783 }
784
785 void
786 _mesa_meta_fb_tex_blit_begin(const struct gl_context *ctx,
787 struct fb_tex_blit_state *blit)
788 {
789 blit->samplerSave =
790 ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler ?
791 ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler->Name : 0;
792 blit->tempTex = 0;
793 }
794
795 void
796 _mesa_meta_fb_tex_blit_end(struct gl_context *ctx, GLenum target,
797 struct fb_tex_blit_state *blit)
798 {
799 /* Restore texture object state, the texture binding will
800 * be restored by _mesa_meta_end().
801 */
802 if (target != GL_TEXTURE_RECTANGLE_ARB) {
803 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, blit->baseLevelSave);
804 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, blit->maxLevelSave);
805
806 if (ctx->Extensions.ARB_stencil_texturing) {
807 const struct gl_texture_object *texObj =
808 _mesa_get_current_tex_object(ctx, target);
809
810 if (texObj->StencilSampling != blit->stencilSamplingSave)
811 _mesa_TexParameteri(target, GL_DEPTH_STENCIL_TEXTURE_MODE,
812 blit->stencilSamplingSave ?
813 GL_STENCIL_INDEX : GL_DEPTH_COMPONENT);
814 }
815 }
816
817 _mesa_BindSampler(ctx->Texture.CurrentUnit, blit->samplerSave);
818 _mesa_DeleteSamplers(1, &blit->sampler);
819 if (blit->tempTex)
820 _mesa_DeleteTextures(1, &blit->tempTex);
821 }
822
823 GLboolean
824 _mesa_meta_bind_rb_as_tex_image(struct gl_context *ctx,
825 struct gl_renderbuffer *rb,
826 GLuint *tex,
827 struct gl_texture_object **texObj,
828 GLenum *target)
829 {
830 struct gl_texture_image *texImage;
831 GLuint tempTex;
832
833 if (rb->NumSamples > 1)
834 *target = GL_TEXTURE_2D_MULTISAMPLE;
835 else
836 *target = GL_TEXTURE_2D;
837
838 tempTex = 0;
839 _mesa_GenTextures(1, &tempTex);
840 if (tempTex == 0)
841 return false;
842
843 *tex = tempTex;
844
845 _mesa_BindTexture(*target, *tex);
846 *texObj = _mesa_lookup_texture(ctx, *tex);
847 texImage = _mesa_get_tex_image(ctx, *texObj, *target, 0);
848
849 if (!ctx->Driver.BindRenderbufferTexImage(ctx, rb, texImage)) {
850 _mesa_DeleteTextures(1, tex);
851 return false;
852 }
853
854 if (ctx->Driver.FinishRenderTexture && !rb->NeedsFinishRenderTexture) {
855 rb->NeedsFinishRenderTexture = true;
856 ctx->Driver.FinishRenderTexture(ctx, rb);
857 }
858
859 return true;
860 }
861
862 GLuint
863 _mesa_meta_setup_sampler(struct gl_context *ctx,
864 const struct gl_texture_object *texObj,
865 GLenum target, GLenum filter, GLuint srcLevel)
866 {
867 GLuint sampler;
868 GLenum tex_filter = (filter == GL_SCALED_RESOLVE_FASTEST_EXT ||
869 filter == GL_SCALED_RESOLVE_NICEST_EXT) ?
870 GL_NEAREST : filter;
871
872 _mesa_GenSamplers(1, &sampler);
873 _mesa_BindSampler(ctx->Texture.CurrentUnit, sampler);
874
875 /* Prepare src texture state */
876 _mesa_BindTexture(target, texObj->Name);
877 _mesa_SamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, tex_filter);
878 _mesa_SamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, tex_filter);
879 if (target != GL_TEXTURE_RECTANGLE_ARB) {
880 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, srcLevel);
881 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel);
882 }
883 _mesa_SamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
884 _mesa_SamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
885
886 return sampler;
887 }
888
889 /**
890 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
891 * of texture mapping and polygon rendering.
892 */
893 GLbitfield
894 _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
895 const struct gl_framebuffer *readFb,
896 const struct gl_framebuffer *drawFb,
897 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
898 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
899 GLbitfield mask, GLenum filter)
900 {
901 const GLint dstW = abs(dstX1 - dstX0);
902 const GLint dstH = abs(dstY1 - dstY0);
903 const GLint dstFlipX = (dstX1 - dstX0) / dstW;
904 const GLint dstFlipY = (dstY1 - dstY0) / dstH;
905
906 struct {
907 GLint srcX0, srcY0, srcX1, srcY1;
908 GLint dstX0, dstY0, dstX1, dstY1;
909 } clip = {
910 srcX0, srcY0, srcX1, srcY1,
911 dstX0, dstY0, dstX1, dstY1
912 };
913
914 const GLboolean use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
915 ctx->Extensions.ARB_fragment_shader;
916
917 /* Multisample texture blit support requires texture multisample. */
918 if (readFb->Visual.samples > 0 &&
919 !ctx->Extensions.ARB_texture_multisample) {
920 return mask;
921 }
922
923 /* Clip a copy of the blit coordinates. If these differ from the input
924 * coordinates, then we'll set the scissor.
925 */
926 if (!_mesa_clip_blit(ctx, readFb, drawFb,
927 &clip.srcX0, &clip.srcY0, &clip.srcX1, &clip.srcY1,
928 &clip.dstX0, &clip.dstY0, &clip.dstX1, &clip.dstY1)) {
929 /* clipped/scissored everything away */
930 return 0;
931 }
932
933 /* Only scissor affects blit, but we're doing to set a custom scissor if
934 * necessary anyway, so save/clear state.
935 */
936 _mesa_meta_begin(ctx, MESA_META_ALL & ~MESA_META_DRAW_BUFFERS);
937
938 /* Dithering shouldn't be performed for glBlitFramebuffer */
939 _mesa_set_enable(ctx, GL_DITHER, GL_FALSE);
940
941 /* If the clipping earlier changed the destination rect at all, then
942 * enable the scissor to clip to it.
943 */
944 if (clip.dstX0 != dstX0 || clip.dstY0 != dstY0 ||
945 clip.dstX1 != dstX1 || clip.dstY1 != dstY1) {
946 _mesa_set_enable(ctx, GL_SCISSOR_TEST, GL_TRUE);
947 _mesa_Scissor(MIN2(clip.dstX0, clip.dstX1),
948 MIN2(clip.dstY0, clip.dstY1),
949 abs(clip.dstX0 - clip.dstX1),
950 abs(clip.dstY0 - clip.dstY1));
951 }
952
953 /* Try faster, direct texture approach first */
954 if (mask & GL_COLOR_BUFFER_BIT) {
955 if (blitframebuffer_texture(ctx, readFb, drawFb,
956 srcX0, srcY0, srcX1, srcY1,
957 dstX0, dstY0, dstX1, dstY1,
958 filter, dstFlipX, dstFlipY,
959 use_glsl_version, false)) {
960 mask &= ~GL_COLOR_BUFFER_BIT;
961 }
962 }
963
964 if (mask & GL_DEPTH_BUFFER_BIT && use_glsl_version) {
965 if (blitframebuffer_texture(ctx, readFb, drawFb,
966 srcX0, srcY0, srcX1, srcY1,
967 dstX0, dstY0, dstX1, dstY1,
968 filter, dstFlipX, dstFlipY,
969 use_glsl_version, true)) {
970 mask &= ~GL_DEPTH_BUFFER_BIT;
971 }
972 }
973
974 if (mask & GL_STENCIL_BUFFER_BIT) {
975 /* XXX can't easily do stencil */
976 }
977
978 _mesa_meta_end(ctx);
979
980 return mask;
981 }
982
983 void
984 _mesa_meta_glsl_blit_cleanup(struct blit_state *blit)
985 {
986 if (blit->VAO) {
987 _mesa_DeleteVertexArrays(1, &blit->VAO);
988 blit->VAO = 0;
989 _mesa_DeleteBuffers(1, &blit->VBO);
990 blit->VBO = 0;
991 }
992
993 _mesa_meta_blit_shader_table_cleanup(&blit->shaders_with_depth);
994 _mesa_meta_blit_shader_table_cleanup(&blit->shaders_without_depth);
995
996 _mesa_DeleteTextures(1, &blit->depthTex.TexObj);
997 blit->depthTex.TexObj = 0;
998 }
999
1000 void
1001 _mesa_meta_and_swrast_BlitFramebuffer(struct gl_context *ctx,
1002 struct gl_framebuffer *readFb,
1003 struct gl_framebuffer *drawFb,
1004 GLint srcX0, GLint srcY0,
1005 GLint srcX1, GLint srcY1,
1006 GLint dstX0, GLint dstY0,
1007 GLint dstX1, GLint dstY1,
1008 GLbitfield mask, GLenum filter)
1009 {
1010 mask = _mesa_meta_BlitFramebuffer(ctx, readFb, drawFb,
1011 srcX0, srcY0, srcX1, srcY1,
1012 dstX0, dstY0, dstX1, dstY1,
1013 mask, filter);
1014 if (mask == 0x0)
1015 return;
1016
1017 _swrast_BlitFramebuffer(ctx, readFb, drawFb,
1018 srcX0, srcY0, srcX1, srcY1,
1019 dstX0, dstY0, dstX1, dstY1,
1020 mask, filter);
1021 }