remove final imports.h and imports.c bits
[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 /** Return offset in bytes of the field within a vertex struct */
55 #define OFFSET(FIELD) ((void *) offsetof(struct vertex, FIELD))
56
57 static void
58 setup_glsl_msaa_blit_scaled_shader(struct gl_context *ctx,
59 struct blit_state *blit,
60 struct gl_renderbuffer *src_rb,
61 GLenum target)
62 {
63 GLint loc_src_width, loc_src_height;
64 int i, samples;
65 int shader_offset = 0;
66 void *mem_ctx = ralloc_context(NULL);
67 char *fs_source;
68 char *name, *sample_number;
69 const uint8_t *sample_map;
70 char *sample_map_str = rzalloc_size(mem_ctx, 1);
71 char *sample_map_expr = rzalloc_size(mem_ctx, 1);
72 char *texel_fetch_macro = rzalloc_size(mem_ctx, 1);
73 const char *sampler_array_suffix = "";
74 float x_scale, y_scale;
75 enum blit_msaa_shader shader_index;
76
77 assert(src_rb);
78 samples = MAX2(src_rb->NumSamples, 1);
79
80 if (samples == 16)
81 x_scale = 4.0;
82 else
83 x_scale = 2.0;
84 y_scale = samples / x_scale;
85
86 /* We expect only power of 2 samples in source multisample buffer. */
87 assert(samples > 0 && util_is_power_of_two_nonzero(samples));
88 while (samples >> (shader_offset + 1)) {
89 shader_offset++;
90 }
91 /* Update the assert if we plan to support more than 16X MSAA. */
92 assert(shader_offset > 0 && shader_offset <= 4);
93
94 assert(target == GL_TEXTURE_2D_MULTISAMPLE ||
95 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY);
96
97 shader_index = BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_SCALED_RESOLVE +
98 shader_offset - 1;
99
100 if (target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) {
101 shader_index += BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_SCALED_RESOLVE -
102 BLIT_2X_MSAA_SHADER_2D_MULTISAMPLE_SCALED_RESOLVE;
103 sampler_array_suffix = "Array";
104 }
105
106 if (blit->msaa_shaders[shader_index]) {
107 _mesa_meta_use_program(ctx, blit->msaa_shaders[shader_index]);
108 /* Update the uniform values. */
109 loc_src_width =
110 _mesa_program_resource_location(blit->msaa_shaders[shader_index], GL_UNIFORM, "src_width");
111 loc_src_height =
112 _mesa_program_resource_location(blit->msaa_shaders[shader_index], GL_UNIFORM, "src_height");
113 _mesa_Uniform1f(loc_src_width, src_rb->Width);
114 _mesa_Uniform1f(loc_src_height, src_rb->Height);
115 return;
116 }
117
118 name = ralloc_asprintf(mem_ctx, "vec4 MSAA scaled resolve");
119
120 /* Below switch is used to setup the shader expression, which computes
121 * sample index and map it to to a sample number on hardware.
122 */
123 switch(samples) {
124 case 2:
125 sample_number = "sample_map[int(2 * fract(coord.x))]";
126 sample_map = ctx->Const.SampleMap2x;
127 break;
128 case 4:
129 sample_number = "sample_map[int(2 * fract(coord.x) + 4 * fract(coord.y))]";
130 sample_map = ctx->Const.SampleMap4x;
131 break;
132 case 8:
133 sample_number = "sample_map[int(2 * fract(coord.x) + 8 * fract(coord.y))]";
134 sample_map = ctx->Const.SampleMap8x;
135 break;
136 case 16:
137 sample_number = "sample_map[int(4 * fract(coord.x) + 16 * fract(coord.y))]";
138 sample_map = ctx->Const.SampleMap16x;
139 break;
140 default:
141 sample_number = NULL;
142 sample_map = NULL;
143 _mesa_problem(ctx, "Unsupported sample count %d\n", samples);
144 unreachable("Unsupported sample count");
145 }
146
147 /* Create sample map string. */
148 for (i = 0 ; i < samples - 1; i++) {
149 ralloc_asprintf_append(&sample_map_str, "%d, ", sample_map[i]);
150 }
151 ralloc_asprintf_append(&sample_map_str, "%d", sample_map[samples - 1]);
152
153 /* Create sample map expression using above string. */
154 ralloc_asprintf_append(&sample_map_expr,
155 " const int sample_map[%d] = int[%d](%s);\n",
156 samples, samples, sample_map_str);
157
158 if (target == GL_TEXTURE_2D_MULTISAMPLE) {
159 ralloc_asprintf_append(&texel_fetch_macro,
160 "#define TEXEL_FETCH(coord) texelFetch(texSampler, ivec2(coord), %s);\n",
161 sample_number);
162 } else {
163 ralloc_asprintf_append(&texel_fetch_macro,
164 "#define TEXEL_FETCH(coord) texelFetch(texSampler, ivec3(coord, layer), %s);\n",
165 sample_number);
166 }
167
168 static const char vs_source[] =
169 "#version 130\n"
170 "#extension GL_ARB_explicit_attrib_location: enable\n"
171 "layout(location = 0) in vec2 position;\n"
172 "layout(location = 1) 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_program_resource_location(blit->msaa_shaders[shader_index], GL_UNIFORM, "src_width");
240 loc_src_height =
241 _mesa_program_resource_location(blit->msaa_shaders[shader_index], GL_UNIFORM, "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 && util_is_power_of_two_nonzero(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, "Unknown 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_meta_use_program(ctx, 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 *tex_coords = "texCoords";
361
362 if (dst_is_msaa) {
363 sample_index = "gl_SampleID";
364 name = "depth MSAA copy";
365
366 if (ctx->Extensions.ARB_gpu_shader5 && samples >= 16) {
367 /* See comment below for the color copy */
368 tex_coords = "interpolateAtOffset(texCoords, vec2(0.0))";
369 }
370 } else {
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 "#extension GL_ARB_explicit_attrib_location: enable\n"
388 "layout(location = 0) in vec2 position;\n"
389 "layout(location = 1) in %s textureCoords;\n"
390 "out %s texCoords;\n"
391 "void main()\n"
392 "{\n"
393 " texCoords = textureCoords;\n"
394 " gl_Position = vec4(position, 0.0, 1.0);\n"
395 "}\n",
396 texcoord_type,
397 texcoord_type);
398 fs_source = ralloc_asprintf(mem_ctx,
399 "#version 130\n"
400 "#extension GL_ARB_texture_multisample : enable\n"
401 "#extension GL_ARB_sample_shading : enable\n"
402 "#extension GL_ARB_gpu_shader5 : enable\n"
403 "uniform sampler2DMS%s texSampler;\n"
404 "in %s texCoords;\n"
405 "out vec4 out_color;\n"
406 "\n"
407 "void main()\n"
408 "{\n"
409 " gl_FragDepth = texelFetch(texSampler, i%s(%s), %s).r;\n"
410 "}\n",
411 sampler_array_suffix,
412 texcoord_type,
413 texcoord_type,
414 tex_coords,
415 sample_index);
416 } else {
417 /* You can create 2D_MULTISAMPLE textures with 0 sample count (meaning 1
418 * sample). Yes, this is ridiculous.
419 */
420 char *sample_resolve;
421 const char *merge_function;
422 name = ralloc_asprintf(mem_ctx, "%svec4 MSAA %s",
423 vec4_prefix,
424 dst_is_msaa ? "copy" : "resolve");
425
426 if (dst_is_msaa) {
427 const char *tex_coords;
428
429 if (ctx->Extensions.ARB_gpu_shader5 && samples >= 16) {
430 /* If interpolateAtOffset is available then it will be used to
431 * force the interpolation to the center. This is required at
432 * least on Intel hardware because it is possible to have a sample
433 * position on the 0 x or y axis which means it will lie exactly
434 * on the pixel boundary. If we let the hardware interpolate the
435 * coordinates at one of these positions then it is possible for
436 * it to jump to a neighboring texel when converting to ints due
437 * to rounding errors. This is only done for >= 16x MSAA because
438 * it probably has some overhead. It is more likely that some
439 * hardware will use one of these problematic positions at 16x
440 * MSAA because in that case in D3D they are defined to be at
441 * these positions.
442 */
443 tex_coords = "interpolateAtOffset(texCoords, vec2(0.0))";
444 } else {
445 tex_coords = "texCoords";
446 }
447
448 sample_resolve =
449 ralloc_asprintf(mem_ctx,
450 " out_color = texelFetch(texSampler, "
451 "i%s(%s), gl_SampleID);",
452 texcoord_type, tex_coords);
453
454 merge_function = "";
455 } else {
456 int i;
457 int step;
458
459 if (src_datatype == GL_INT || src_datatype == GL_UNSIGNED_INT) {
460 /* From the OpenGL ES 3.2 spec section 16.2.1:
461 *
462 * "If the source formats are integer types or stencil values,
463 * a single sample's value is selected for each pixel."
464 *
465 * The OpenGL 4.4 spec contains exactly the same language.
466 *
467 * We can accomplish this by making the merge function return just
468 * one of the two samples. The compiler should do the rest.
469 */
470 merge_function = "gvec4 merge(gvec4 a, gvec4 b) { return a; }\n";
471 } else {
472 /* The divide will happen at the end for floats. */
473 merge_function =
474 "vec4 merge(vec4 a, vec4 b) { return (a + b); }\n";
475 }
476
477 /* We're assuming power of two samples for this resolution procedure.
478 *
479 * To avoid losing any floating point precision if the samples all
480 * happen to have the same value, we merge pairs of values at a time
481 * (so the floating point exponent just gets increased), rather than
482 * doing a naive sum and dividing.
483 */
484 assert(util_is_power_of_two_or_zero(samples));
485 /* Fetch each individual sample. */
486 sample_resolve = rzalloc_size(mem_ctx, 1);
487 for (i = 0; i < samples; i++) {
488 ralloc_asprintf_append(&sample_resolve,
489 " gvec4 sample_1_%d = texelFetch(texSampler, i%s(texCoords), %d);\n",
490 i, texcoord_type, i);
491 }
492 /* Now, merge each pair of samples, then merge each pair of those,
493 * etc.
494 */
495 for (step = 2; step <= samples; step *= 2) {
496 for (i = 0; i < samples; i += step) {
497 ralloc_asprintf_append(&sample_resolve,
498 " gvec4 sample_%d_%d = merge(sample_%d_%d, sample_%d_%d);\n",
499 step, i,
500 step / 2, i,
501 step / 2, i + step / 2);
502 }
503 }
504
505 /* Scale the final result. */
506 if (src_datatype == GL_UNSIGNED_INT || src_datatype == GL_INT) {
507 ralloc_asprintf_append(&sample_resolve,
508 " out_color = sample_%d_0;\n",
509 samples);
510 } else {
511 ralloc_asprintf_append(&sample_resolve,
512 " gl_FragColor = sample_%d_0 / %f;\n",
513 samples, (float)samples);
514 }
515 }
516
517 vs_source = ralloc_asprintf(mem_ctx,
518 "#version 130\n"
519 "#extension GL_ARB_explicit_attrib_location: enable\n"
520 "layout(location = 0) in vec2 position;\n"
521 "layout(location = 1) in %s textureCoords;\n"
522 "out %s texCoords;\n"
523 "void main()\n"
524 "{\n"
525 " texCoords = textureCoords;\n"
526 " gl_Position = vec4(position, 0.0, 1.0);\n"
527 "}\n",
528 texcoord_type,
529 texcoord_type);
530 fs_source = ralloc_asprintf(mem_ctx,
531 "#version 130\n"
532 "#extension GL_ARB_texture_multisample : enable\n"
533 "#extension GL_ARB_sample_shading : enable\n"
534 "#extension GL_ARB_gpu_shader5 : enable\n"
535 "#define gvec4 %svec4\n"
536 "uniform %ssampler2DMS%s texSampler;\n"
537 "in %s texCoords;\n"
538 "out gvec4 out_color;\n"
539 "\n"
540 "%s" /* merge_function */
541 "void main()\n"
542 "{\n"
543 "%s\n" /* sample_resolve */
544 "}\n",
545 vec4_prefix,
546 vec4_prefix,
547 sampler_array_suffix,
548 texcoord_type,
549 merge_function,
550 sample_resolve);
551 }
552
553 _mesa_meta_compile_and_link_program(ctx, vs_source, fs_source, name,
554 &blit->msaa_shaders[shader_index]);
555
556 ralloc_free(mem_ctx);
557 }
558
559 static void
560 setup_glsl_blit_framebuffer(struct gl_context *ctx,
561 struct blit_state *blit,
562 const struct gl_framebuffer *drawFb,
563 struct gl_renderbuffer *src_rb,
564 GLenum target, GLenum filter,
565 bool is_scaled_blit,
566 bool do_depth)
567 {
568 unsigned texcoord_size;
569 bool is_target_multisample = target == GL_TEXTURE_2D_MULTISAMPLE ||
570 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
571 bool is_filter_scaled_resolve = filter == GL_SCALED_RESOLVE_FASTEST_EXT ||
572 filter == GL_SCALED_RESOLVE_NICEST_EXT;
573
574 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
575 assert(_mesa_is_desktop_gl(ctx) || target == GL_TEXTURE_2D);
576
577 texcoord_size = 2 + (src_rb->Depth > 1 ? 1 : 0);
578
579 _mesa_meta_setup_vertex_objects(ctx, &blit->VAO, &blit->buf_obj, true,
580 2, texcoord_size, 0);
581
582 if (is_target_multisample && is_filter_scaled_resolve && is_scaled_blit) {
583 setup_glsl_msaa_blit_scaled_shader(ctx, blit, src_rb, target);
584 } else if (is_target_multisample) {
585 setup_glsl_msaa_blit_shader(ctx, blit, drawFb, src_rb, target);
586 } else {
587 _mesa_meta_setup_blit_shader(ctx, target, do_depth,
588 do_depth ? &blit->shaders_with_depth
589 : &blit->shaders_without_depth);
590 }
591 }
592
593 /**
594 * Try to do a color or depth glBlitFramebuffer using texturing.
595 *
596 * We can do this when the src renderbuffer is actually a texture, or when the
597 * driver exposes BindRenderbufferTexImage().
598 */
599 static bool
600 blitframebuffer_texture(struct gl_context *ctx,
601 const struct gl_framebuffer *readFb,
602 const struct gl_framebuffer *drawFb,
603 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
604 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
605 GLenum filter, GLint flipX, GLint flipY,
606 GLboolean glsl_version, GLboolean do_depth)
607 {
608 int att_index = do_depth ? BUFFER_DEPTH : readFb->_ColorReadBufferIndex;
609 const struct gl_renderbuffer_attachment *readAtt =
610 &readFb->Attachment[att_index];
611 struct blit_state *blit = &ctx->Meta->Blit;
612 struct fb_tex_blit_state fb_tex_blit;
613 const GLint dstX = MIN2(dstX0, dstX1);
614 const GLint dstY = MIN2(dstY0, dstY1);
615 const GLint dstW = abs(dstX1 - dstX0);
616 const GLint dstH = abs(dstY1 - dstY0);
617 const int srcW = abs(srcX1 - srcX0);
618 const int srcH = abs(srcY1 - srcY0);
619 bool scaled_blit = false;
620 struct gl_texture_object *texObj;
621 GLuint srcLevel;
622 GLenum target;
623 struct gl_renderbuffer *rb = readAtt->Renderbuffer;
624 struct temp_texture *meta_temp_texture;
625
626 if (rb->NumSamples && !ctx->Extensions.ARB_texture_multisample)
627 return false;
628
629 _mesa_meta_fb_tex_blit_begin(ctx, &fb_tex_blit);
630
631 if (readAtt->Texture &&
632 (readAtt->Texture->Target == GL_TEXTURE_2D ||
633 readAtt->Texture->Target == GL_TEXTURE_RECTANGLE ||
634 readAtt->Texture->Target == GL_TEXTURE_2D_MULTISAMPLE ||
635 readAtt->Texture->Target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)) {
636 /* If there's a texture attached of a type we can handle, then just use
637 * it directly.
638 */
639 srcLevel = readAtt->TextureLevel;
640 texObj = readAtt->Texture;
641 } else if (!readAtt->Texture && ctx->Driver.BindRenderbufferTexImage) {
642 texObj = _mesa_meta_texture_object_from_renderbuffer(ctx, rb);
643 if (texObj == NULL)
644 return false;
645
646 fb_tex_blit.temp_tex_obj = texObj;
647
648 srcLevel = 0;
649 if (_mesa_is_winsys_fbo(readFb)) {
650 GLint temp = srcY0;
651 srcY0 = rb->Height - srcY1;
652 srcY1 = rb->Height - temp;
653 flipY = -flipY;
654 }
655 } else {
656 GLenum tex_base_format;
657 /* Fall back to doing a CopyTexSubImage to get the destination
658 * renderbuffer into a texture.
659 */
660 if (ctx->Meta->Blit.no_ctsi_fallback)
661 return false;
662
663 if (rb->NumSamples > 1)
664 return false;
665
666 if (do_depth) {
667 meta_temp_texture = _mesa_meta_get_temp_depth_texture(ctx);
668 tex_base_format = GL_DEPTH_COMPONENT;
669 } else {
670 meta_temp_texture = _mesa_meta_get_temp_texture(ctx);
671 tex_base_format =
672 _mesa_base_tex_format(ctx, rb->InternalFormat);
673 }
674
675 srcLevel = 0;
676 texObj = meta_temp_texture->tex_obj;
677 if (texObj == NULL) {
678 return false;
679 }
680
681 _mesa_meta_setup_copypix_texture(ctx, meta_temp_texture,
682 srcX0, srcY0,
683 srcW, srcH,
684 tex_base_format,
685 filter);
686
687 assert(texObj->Target == meta_temp_texture->Target);
688
689 srcX0 = 0;
690 srcY0 = 0;
691 srcX1 = srcW;
692 srcY1 = srcH;
693 }
694
695 target = texObj->Target;
696 fb_tex_blit.tex_obj = texObj;
697 fb_tex_blit.baseLevelSave = texObj->BaseLevel;
698 fb_tex_blit.maxLevelSave = texObj->MaxLevel;
699 fb_tex_blit.stencilSamplingSave = texObj->StencilSampling;
700
701 scaled_blit = dstW != srcW || dstH != srcH;
702
703 if (glsl_version) {
704 setup_glsl_blit_framebuffer(ctx, blit, drawFb, rb, target, filter, scaled_blit,
705 do_depth);
706 }
707 else {
708 _mesa_meta_setup_ff_tnl_for_blit(ctx,
709 &ctx->Meta->Blit.VAO,
710 &ctx->Meta->Blit.buf_obj,
711 2);
712 }
713
714 /*
715 printf("Blit from texture!\n");
716 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
717 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
718 */
719
720 fb_tex_blit.samp_obj = _mesa_meta_setup_sampler(ctx, texObj, target, filter,
721 srcLevel);
722
723 if (ctx->Extensions.EXT_texture_sRGB_decode) {
724 /* The GL 4.4 spec, section 18.3.1 ("Blitting Pixel Rectangles") says:
725 *
726 * "When values are taken from the read buffer, if FRAMEBUFFER_SRGB
727 * is enabled and the value of FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING
728 * for the framebuffer attachment corresponding to the read buffer
729 * is SRGB (see section 9.2.3), the red, green, and blue components
730 * are converted from the non-linear sRGB color space according to
731 * equation 3.24.
732 *
733 * When values are written to the draw buffers, blit operations
734 * bypass most of the fragment pipeline. The only fragment
735 * operations which affect a blit are the pixel ownership test,
736 * the scissor test, and sRGB conversion (see section 17.3.9)."
737 *
738 * ES 3.0 contains nearly the exact same text, but omits the part
739 * about GL_FRAMEBUFFER_SRGB as that doesn't exist in ES. Mesa
740 * defaults it to on for ES contexts, so we can safely check it.
741 */
742 const bool decode =
743 ctx->Color.sRGBEnabled && _mesa_is_format_srgb(rb->Format);
744
745 _mesa_set_sampler_srgb_decode(ctx, fb_tex_blit.samp_obj,
746 decode ? GL_DECODE_EXT
747 : GL_SKIP_DECODE_EXT);
748 }
749
750 if (!glsl_version) {
751 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
752 _mesa_set_enable(ctx, target, GL_TRUE);
753 }
754
755 /* Prepare vertex data (the VBO was previously created and bound) */
756 {
757 struct vertex verts[4];
758 GLfloat s0, t0, s1, t1;
759
760 if (target == GL_TEXTURE_2D) {
761 const struct gl_texture_image *texImage
762 = _mesa_select_tex_image(texObj, target, srcLevel);
763 s0 = srcX0 / (float) texImage->Width;
764 s1 = srcX1 / (float) texImage->Width;
765 t0 = srcY0 / (float) texImage->Height;
766 t1 = srcY1 / (float) texImage->Height;
767 }
768 else {
769 assert(target == GL_TEXTURE_RECTANGLE_ARB ||
770 target == GL_TEXTURE_2D_MULTISAMPLE ||
771 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY);
772 s0 = (float) srcX0;
773 s1 = (float) srcX1;
774 t0 = (float) srcY0;
775 t1 = (float) srcY1;
776 }
777
778 /* Silence valgrind warnings about reading uninitialized stack. */
779 memset(verts, 0, sizeof(verts));
780
781 /* setup vertex positions */
782 verts[0].x = -1.0F * flipX;
783 verts[0].y = -1.0F * flipY;
784 verts[1].x = 1.0F * flipX;
785 verts[1].y = -1.0F * flipY;
786 verts[2].x = 1.0F * flipX;
787 verts[2].y = 1.0F * flipY;
788 verts[3].x = -1.0F * flipX;
789 verts[3].y = 1.0F * flipY;
790
791 verts[0].tex[0] = s0;
792 verts[0].tex[1] = t0;
793 verts[0].tex[2] = readAtt->Zoffset;
794 verts[1].tex[0] = s1;
795 verts[1].tex[1] = t0;
796 verts[1].tex[2] = readAtt->Zoffset;
797 verts[2].tex[0] = s1;
798 verts[2].tex[1] = t1;
799 verts[2].tex[2] = readAtt->Zoffset;
800 verts[3].tex[0] = s0;
801 verts[3].tex[1] = t1;
802 verts[3].tex[2] = readAtt->Zoffset;
803
804 _mesa_buffer_sub_data(ctx, blit->buf_obj, 0, sizeof(verts), verts);
805 }
806
807 /* setup viewport */
808 _mesa_set_viewport(ctx, 0, dstX, dstY, dstW, dstH);
809 _mesa_ColorMask(!do_depth, !do_depth, !do_depth, !do_depth);
810 _mesa_set_enable(ctx, GL_DEPTH_TEST, do_depth);
811 _mesa_DepthMask(do_depth);
812 _mesa_DepthFunc(GL_ALWAYS);
813
814 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
815 _mesa_meta_fb_tex_blit_end(ctx, target, &fb_tex_blit);
816
817 return true;
818 }
819
820 void
821 _mesa_meta_fb_tex_blit_begin(struct gl_context *ctx,
822 struct fb_tex_blit_state *blit)
823 {
824 /* None of the existing callers preinitialize fb_tex_blit_state to zeros,
825 * and both use stack variables. If samp_obj_save is not NULL,
826 * _mesa_reference_sampler_object will try to dereference it. Leaving
827 * random garbage in samp_obj_save can only lead to crashes.
828 *
829 * Since the state isn't persistent across calls, we won't catch ref
830 * counting problems.
831 */
832 blit->samp_obj_save = NULL;
833 _mesa_reference_sampler_object(ctx, &blit->samp_obj_save,
834 ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler);
835 blit->temp_tex_obj = NULL;
836 }
837
838 void
839 _mesa_meta_fb_tex_blit_end(struct gl_context *ctx, GLenum target,
840 struct fb_tex_blit_state *blit)
841 {
842 struct gl_texture_object *const texObj =
843 _mesa_get_current_tex_object(ctx, target);
844
845 /* Either there is no temporary texture or the temporary texture is bound. */
846 assert(blit->temp_tex_obj == NULL || blit->temp_tex_obj == texObj);
847
848 /* Restore texture object state, the texture binding will be restored by
849 * _mesa_meta_end(). If the texture is the temporary texture that is about
850 * to be destroyed, don't bother restoring its state.
851 */
852 if (blit->temp_tex_obj == NULL) {
853 /* If the target restricts values for base level or max level, we assume
854 * that the original values were valid.
855 */
856 if (blit->baseLevelSave != texObj->BaseLevel)
857 _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_BASE_LEVEL,
858 &blit->baseLevelSave, false);
859
860 if (blit->maxLevelSave != texObj->MaxLevel)
861 _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_MAX_LEVEL,
862 &blit->maxLevelSave, false);
863
864 /* If ARB_stencil_texturing is not supported, the mode won't have changed. */
865 if (texObj->StencilSampling != blit->stencilSamplingSave) {
866 /* GLint so the compiler won't complain about type signedness mismatch
867 * in the call to _mesa_texture_parameteriv below.
868 */
869 const GLint param = blit->stencilSamplingSave ?
870 GL_STENCIL_INDEX : GL_DEPTH_COMPONENT;
871
872 _mesa_texture_parameteriv(ctx, texObj, GL_DEPTH_STENCIL_TEXTURE_MODE,
873 &param, false);
874 }
875 }
876
877 _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, blit->samp_obj_save);
878 _mesa_reference_sampler_object(ctx, &blit->samp_obj_save, NULL);
879 _mesa_reference_sampler_object(ctx, &blit->samp_obj, NULL);
880 _mesa_delete_nameless_texture(ctx, blit->temp_tex_obj);
881 }
882
883 struct gl_texture_object *
884 _mesa_meta_texture_object_from_renderbuffer(struct gl_context *ctx,
885 struct gl_renderbuffer *rb)
886 {
887 struct gl_texture_image *texImage;
888 struct gl_texture_object *texObj;
889 const GLenum target = rb->NumSamples > 1
890 ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
891
892 texObj = ctx->Driver.NewTextureObject(ctx, 0xDEADBEEF, target);
893 texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
894
895 if (!ctx->Driver.BindRenderbufferTexImage(ctx, rb, texImage)) {
896 _mesa_delete_nameless_texture(ctx, texObj);
897 return NULL;
898 }
899
900 if (ctx->Driver.FinishRenderTexture && !rb->NeedsFinishRenderTexture) {
901 rb->NeedsFinishRenderTexture = true;
902 ctx->Driver.FinishRenderTexture(ctx, rb);
903 }
904
905 return texObj;
906 }
907
908 struct gl_sampler_object *
909 _mesa_meta_setup_sampler(struct gl_context *ctx,
910 struct gl_texture_object *texObj,
911 GLenum target, GLenum filter, GLuint srcLevel)
912 {
913 struct gl_sampler_object *samp_obj;
914 GLenum tex_filter = (filter == GL_SCALED_RESOLVE_FASTEST_EXT ||
915 filter == GL_SCALED_RESOLVE_NICEST_EXT) ?
916 GL_NEAREST : filter;
917
918 samp_obj = ctx->Driver.NewSamplerObject(ctx, 0xDEADBEEF);
919 if (samp_obj == NULL)
920 return NULL;
921
922 _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, samp_obj);
923 _mesa_set_sampler_filters(ctx, samp_obj, tex_filter, tex_filter);
924 _mesa_set_sampler_wrap(ctx, samp_obj, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE,
925 samp_obj->WrapR);
926
927 /* Prepare src texture state */
928 _mesa_bind_texture(ctx, target, texObj);
929 if (target != GL_TEXTURE_RECTANGLE_ARB) {
930 _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_BASE_LEVEL,
931 (GLint *) &srcLevel, false);
932 _mesa_texture_parameteriv(ctx, texObj, GL_TEXTURE_MAX_LEVEL,
933 (GLint *) &srcLevel, false);
934 }
935
936 return samp_obj;
937 }
938
939 /**
940 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
941 * of texture mapping and polygon rendering.
942 */
943 GLbitfield
944 _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
945 const struct gl_framebuffer *readFb,
946 const struct gl_framebuffer *drawFb,
947 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
948 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
949 GLbitfield mask, GLenum filter)
950 {
951 const GLint dstW = abs(dstX1 - dstX0);
952 const GLint dstH = abs(dstY1 - dstY0);
953 const GLint dstFlipX = (dstX1 - dstX0) / dstW;
954 const GLint dstFlipY = (dstY1 - dstY0) / dstH;
955
956 struct {
957 GLint srcX0, srcY0, srcX1, srcY1;
958 GLint dstX0, dstY0, dstX1, dstY1;
959 } clip = {
960 srcX0, srcY0, srcX1, srcY1,
961 dstX0, dstY0, dstX1, dstY1
962 };
963
964 const GLboolean use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
965 ctx->Extensions.ARB_fragment_shader;
966
967 /* Multisample texture blit support requires texture multisample. */
968 if (readFb->Visual.samples > 0 &&
969 !ctx->Extensions.ARB_texture_multisample) {
970 return mask;
971 }
972
973 /* Clip a copy of the blit coordinates. If these differ from the input
974 * coordinates, then we'll set the scissor.
975 */
976 if (!_mesa_clip_blit(ctx, readFb, drawFb,
977 &clip.srcX0, &clip.srcY0, &clip.srcX1, &clip.srcY1,
978 &clip.dstX0, &clip.dstY0, &clip.dstX1, &clip.dstY1)) {
979 /* clipped/scissored everything away */
980 return 0;
981 }
982
983 /* Only scissor and FRAMEBUFFER_SRGB affect blit. Leave sRGB alone, but
984 * save restore scissor as we'll set a custom scissor if necessary.
985 */
986 _mesa_meta_begin(ctx, MESA_META_ALL &
987 ~(MESA_META_DRAW_BUFFERS |
988 MESA_META_FRAMEBUFFER_SRGB));
989
990 /* Dithering shouldn't be performed for glBlitFramebuffer */
991 _mesa_set_enable(ctx, GL_DITHER, GL_FALSE);
992
993 /* If the clipping earlier changed the destination rect at all, then
994 * enable the scissor to clip to it.
995 */
996 if (clip.dstX0 != dstX0 || clip.dstY0 != dstY0 ||
997 clip.dstX1 != dstX1 || clip.dstY1 != dstY1) {
998 _mesa_set_enable(ctx, GL_SCISSOR_TEST, GL_TRUE);
999 _mesa_Scissor(MIN2(clip.dstX0, clip.dstX1),
1000 MIN2(clip.dstY0, clip.dstY1),
1001 abs(clip.dstX0 - clip.dstX1),
1002 abs(clip.dstY0 - clip.dstY1));
1003 }
1004
1005 /* Try faster, direct texture approach first */
1006 if (mask & GL_COLOR_BUFFER_BIT) {
1007 if (blitframebuffer_texture(ctx, readFb, drawFb,
1008 srcX0, srcY0, srcX1, srcY1,
1009 dstX0, dstY0, dstX1, dstY1,
1010 filter, dstFlipX, dstFlipY,
1011 use_glsl_version, false)) {
1012 mask &= ~GL_COLOR_BUFFER_BIT;
1013 }
1014 }
1015
1016 if (mask & GL_DEPTH_BUFFER_BIT && use_glsl_version) {
1017 if (blitframebuffer_texture(ctx, readFb, drawFb,
1018 srcX0, srcY0, srcX1, srcY1,
1019 dstX0, dstY0, dstX1, dstY1,
1020 filter, dstFlipX, dstFlipY,
1021 use_glsl_version, true)) {
1022 mask &= ~GL_DEPTH_BUFFER_BIT;
1023 }
1024 }
1025
1026 if (mask & GL_STENCIL_BUFFER_BIT) {
1027 /* XXX can't easily do stencil */
1028 }
1029
1030 _mesa_meta_end(ctx);
1031
1032 return mask;
1033 }
1034
1035 void
1036 _mesa_meta_glsl_blit_cleanup(struct gl_context *ctx, struct blit_state *blit)
1037 {
1038 if (blit->VAO) {
1039 _mesa_DeleteVertexArrays(1, &blit->VAO);
1040 blit->VAO = 0;
1041 _mesa_reference_buffer_object(ctx, &blit->buf_obj, NULL);
1042 }
1043
1044 _mesa_meta_blit_shader_table_cleanup(ctx, &blit->shaders_with_depth);
1045 _mesa_meta_blit_shader_table_cleanup(ctx, &blit->shaders_without_depth);
1046
1047 if (blit->depthTex.tex_obj != NULL) {
1048 _mesa_delete_nameless_texture(ctx, blit->depthTex.tex_obj);
1049 blit->depthTex.tex_obj = NULL;
1050 }
1051 }
1052
1053 void
1054 _mesa_meta_and_swrast_BlitFramebuffer(struct gl_context *ctx,
1055 struct gl_framebuffer *readFb,
1056 struct gl_framebuffer *drawFb,
1057 GLint srcX0, GLint srcY0,
1058 GLint srcX1, GLint srcY1,
1059 GLint dstX0, GLint dstY0,
1060 GLint dstX1, GLint dstY1,
1061 GLbitfield mask, GLenum filter)
1062 {
1063 mask = _mesa_meta_BlitFramebuffer(ctx, readFb, drawFb,
1064 srcX0, srcY0, srcX1, srcY1,
1065 dstX0, dstY0, dstX1, dstY1,
1066 mask, filter);
1067 if (mask == 0x0)
1068 return;
1069
1070 _swrast_BlitFramebuffer(ctx, readFb, drawFb,
1071 srcX0, srcY0, srcX1, srcY1,
1072 dstX0, dstY0, dstX1, dstY1,
1073 mask, filter);
1074 }