Merge remote-tracking branch 'jekstrand/wip/i965-uniforms' into vulkan
[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, "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_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 *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 "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 "#extension GL_ARB_sample_shading : enable\n"
401 "#extension GL_ARB_gpu_shader5 : enable\n"
402 "uniform sampler2DMS%s texSampler;\n"
403 "in %s texCoords;\n"
404 "out vec4 out_color;\n"
405 "\n"
406 "void main()\n"
407 "{\n"
408 " gl_FragDepth = texelFetch(texSampler, i%s(%s), %s).r;\n"
409 "}\n",
410 sampler_array_suffix,
411 texcoord_type,
412 texcoord_type,
413 tex_coords,
414 sample_index);
415 } else {
416 /* You can create 2D_MULTISAMPLE textures with 0 sample count (meaning 1
417 * sample). Yes, this is ridiculous.
418 */
419 char *sample_resolve;
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 const char *tex_coords;
427
428 if (ctx->Extensions.ARB_gpu_shader5 && samples >= 16) {
429 /* If interpolateAtOffset is available then it will be used to
430 * force the interpolation to the center. This is required at
431 * least on Intel hardware because it is possible to have a sample
432 * position on the 0 x or y axis which means it will lie exactly
433 * on the pixel boundary. If we let the hardware interpolate the
434 * coordinates at one of these positions then it is possible for
435 * it to jump to a neighboring texel when converting to ints due
436 * to rounding errors. This is only done for >= 16x MSAA because
437 * it probably has some overhead. It is more likely that some
438 * hardware will use one of these problematic positions at 16x
439 * MSAA because in that case in D3D they are defined to be at
440 * these positions.
441 */
442 tex_coords = "interpolateAtOffset(texCoords, vec2(0.0))";
443 } else {
444 tex_coords = "texCoords";
445 }
446
447 sample_resolve =
448 ralloc_asprintf(mem_ctx,
449 " out_color = texelFetch(texSampler, "
450 "i%s(%s), gl_SampleID);",
451 texcoord_type, tex_coords);
452
453 merge_function = "";
454 } else {
455 int i;
456 int step;
457
458 if (src_datatype == GL_INT || src_datatype == GL_UNSIGNED_INT) {
459 merge_function =
460 "gvec4 merge(gvec4 a, gvec4 b) { return (a >> gvec4(1)) + (b >> gvec4(1)) + (a & b & gvec4(1)); }\n";
461 } else {
462 /* The divide will happen at the end for floats. */
463 merge_function =
464 "vec4 merge(vec4 a, vec4 b) { return (a + b); }\n";
465 }
466
467 /* We're assuming power of two samples for this resolution procedure.
468 *
469 * To avoid losing any floating point precision if the samples all
470 * happen to have the same value, we merge pairs of values at a time
471 * (so the floating point exponent just gets increased), rather than
472 * doing a naive sum and dividing.
473 */
474 assert(_mesa_is_pow_two(samples));
475 /* Fetch each individual sample. */
476 sample_resolve = rzalloc_size(mem_ctx, 1);
477 for (i = 0; i < samples; i++) {
478 ralloc_asprintf_append(&sample_resolve,
479 " gvec4 sample_1_%d = texelFetch(texSampler, i%s(texCoords), %d);\n",
480 i, texcoord_type, i);
481 }
482 /* Now, merge each pair of samples, then merge each pair of those,
483 * etc.
484 */
485 for (step = 2; step <= samples; step *= 2) {
486 for (i = 0; i < samples; i += step) {
487 ralloc_asprintf_append(&sample_resolve,
488 " gvec4 sample_%d_%d = merge(sample_%d_%d, sample_%d_%d);\n",
489 step, i,
490 step / 2, i,
491 step / 2, i + step / 2);
492 }
493 }
494
495 /* Scale the final result. */
496 if (src_datatype == GL_UNSIGNED_INT || src_datatype == GL_INT) {
497 ralloc_asprintf_append(&sample_resolve,
498 " out_color = sample_%d_0;\n",
499 samples);
500 } else {
501 ralloc_asprintf_append(&sample_resolve,
502 " gl_FragColor = sample_%d_0 / %f;\n",
503 samples, (float)samples);
504 }
505 }
506
507 vs_source = ralloc_asprintf(mem_ctx,
508 "#version 130\n"
509 "in vec2 position;\n"
510 "in %s textureCoords;\n"
511 "out %s texCoords;\n"
512 "void main()\n"
513 "{\n"
514 " texCoords = textureCoords;\n"
515 " gl_Position = vec4(position, 0.0, 1.0);\n"
516 "}\n",
517 texcoord_type,
518 texcoord_type);
519 fs_source = ralloc_asprintf(mem_ctx,
520 "#version 130\n"
521 "#extension GL_ARB_texture_multisample : enable\n"
522 "#extension GL_ARB_sample_shading : enable\n"
523 "#extension GL_ARB_gpu_shader5 : enable\n"
524 "#define gvec4 %svec4\n"
525 "uniform %ssampler2DMS%s texSampler;\n"
526 "in %s texCoords;\n"
527 "out gvec4 out_color;\n"
528 "\n"
529 "%s" /* merge_function */
530 "void main()\n"
531 "{\n"
532 "%s\n" /* sample_resolve */
533 "}\n",
534 vec4_prefix,
535 vec4_prefix,
536 sampler_array_suffix,
537 texcoord_type,
538 merge_function,
539 sample_resolve);
540 }
541
542 _mesa_meta_compile_and_link_program(ctx, vs_source, fs_source, name,
543 &blit->msaa_shaders[shader_index]);
544
545 ralloc_free(mem_ctx);
546 }
547
548 static void
549 setup_glsl_blit_framebuffer(struct gl_context *ctx,
550 struct blit_state *blit,
551 const struct gl_framebuffer *drawFb,
552 struct gl_renderbuffer *src_rb,
553 GLenum target, GLenum filter,
554 bool is_scaled_blit,
555 bool do_depth)
556 {
557 unsigned texcoord_size;
558 bool is_target_multisample = target == GL_TEXTURE_2D_MULTISAMPLE ||
559 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
560 bool is_filter_scaled_resolve = filter == GL_SCALED_RESOLVE_FASTEST_EXT ||
561 filter == GL_SCALED_RESOLVE_NICEST_EXT;
562
563 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
564 assert(_mesa_is_desktop_gl(ctx) || target == GL_TEXTURE_2D);
565
566 texcoord_size = 2 + (src_rb->Depth > 1 ? 1 : 0);
567
568 _mesa_meta_setup_vertex_objects(ctx, &blit->VAO, &blit->buf_obj, true,
569 2, texcoord_size, 0);
570
571 if (is_target_multisample && is_filter_scaled_resolve && is_scaled_blit) {
572 setup_glsl_msaa_blit_scaled_shader(ctx, blit, src_rb, target, filter);
573 } else if (is_target_multisample) {
574 setup_glsl_msaa_blit_shader(ctx, blit, drawFb, src_rb, target);
575 } else {
576 _mesa_meta_setup_blit_shader(ctx, target, do_depth,
577 do_depth ? &blit->shaders_with_depth
578 : &blit->shaders_without_depth);
579 }
580 }
581
582 /**
583 * Try to do a color or depth glBlitFramebuffer using texturing.
584 *
585 * We can do this when the src renderbuffer is actually a texture, or when the
586 * driver exposes BindRenderbufferTexImage().
587 */
588 static bool
589 blitframebuffer_texture(struct gl_context *ctx,
590 const struct gl_framebuffer *readFb,
591 const struct gl_framebuffer *drawFb,
592 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
593 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
594 GLenum filter, GLint flipX, GLint flipY,
595 GLboolean glsl_version, GLboolean do_depth)
596 {
597 int att_index = do_depth ? BUFFER_DEPTH : readFb->_ColorReadBufferIndex;
598 const struct gl_renderbuffer_attachment *readAtt =
599 &readFb->Attachment[att_index];
600 struct blit_state *blit = &ctx->Meta->Blit;
601 struct fb_tex_blit_state fb_tex_blit;
602 const GLint dstX = MIN2(dstX0, dstX1);
603 const GLint dstY = MIN2(dstY0, dstY1);
604 const GLint dstW = abs(dstX1 - dstX0);
605 const GLint dstH = abs(dstY1 - dstY0);
606 const int srcW = abs(srcX1 - srcX0);
607 const int srcH = abs(srcY1 - srcY0);
608 bool scaled_blit = false;
609 struct gl_texture_object *texObj;
610 GLuint srcLevel;
611 GLenum target;
612 struct gl_renderbuffer *rb = readAtt->Renderbuffer;
613 struct temp_texture *meta_temp_texture;
614
615 if (rb->NumSamples && !ctx->Extensions.ARB_texture_multisample)
616 return false;
617
618 _mesa_meta_fb_tex_blit_begin(ctx, &fb_tex_blit);
619
620 if (readAtt->Texture &&
621 (readAtt->Texture->Target == GL_TEXTURE_2D ||
622 readAtt->Texture->Target == GL_TEXTURE_RECTANGLE ||
623 readAtt->Texture->Target == GL_TEXTURE_2D_MULTISAMPLE ||
624 readAtt->Texture->Target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)) {
625 /* If there's a texture attached of a type we can handle, then just use
626 * it directly.
627 */
628 srcLevel = readAtt->TextureLevel;
629 texObj = readAtt->Texture;
630 target = texObj->Target;
631 } else if (!readAtt->Texture && ctx->Driver.BindRenderbufferTexImage) {
632 if (!_mesa_meta_bind_rb_as_tex_image(ctx, rb, &fb_tex_blit.tempTex,
633 &texObj, &target))
634 return false;
635
636 srcLevel = 0;
637 if (_mesa_is_winsys_fbo(readFb)) {
638 GLint temp = srcY0;
639 srcY0 = rb->Height - srcY1;
640 srcY1 = rb->Height - temp;
641 flipY = -flipY;
642 }
643 } else {
644 GLenum tex_base_format;
645 /* Fall back to doing a CopyTexSubImage to get the destination
646 * renderbuffer into a texture.
647 */
648 if (ctx->Meta->Blit.no_ctsi_fallback)
649 return false;
650
651 if (rb->NumSamples > 1)
652 return false;
653
654 if (do_depth) {
655 meta_temp_texture = _mesa_meta_get_temp_depth_texture(ctx);
656 tex_base_format = GL_DEPTH_COMPONENT;
657 } else {
658 meta_temp_texture = _mesa_meta_get_temp_texture(ctx);
659 tex_base_format =
660 _mesa_base_tex_format(ctx, rb->InternalFormat);
661 }
662
663 srcLevel = 0;
664 target = meta_temp_texture->Target;
665 texObj = _mesa_lookup_texture(ctx, meta_temp_texture->TexObj);
666 if (texObj == NULL) {
667 return false;
668 }
669
670 _mesa_meta_setup_copypix_texture(ctx, meta_temp_texture,
671 srcX0, srcY0,
672 srcW, srcH,
673 tex_base_format,
674 filter);
675
676
677 srcX0 = 0;
678 srcY0 = 0;
679 srcX1 = srcW;
680 srcY1 = srcH;
681 }
682
683 fb_tex_blit.baseLevelSave = texObj->BaseLevel;
684 fb_tex_blit.maxLevelSave = texObj->MaxLevel;
685 fb_tex_blit.stencilSamplingSave = texObj->StencilSampling;
686
687 scaled_blit = dstW != srcW || dstH != srcH;
688
689 if (glsl_version) {
690 setup_glsl_blit_framebuffer(ctx, blit, drawFb, rb, target, filter, scaled_blit,
691 do_depth);
692 }
693 else {
694 _mesa_meta_setup_ff_tnl_for_blit(ctx,
695 &ctx->Meta->Blit.VAO,
696 &ctx->Meta->Blit.buf_obj,
697 2);
698 }
699
700 /*
701 printf("Blit from texture!\n");
702 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
703 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
704 */
705
706 fb_tex_blit.samp_obj = _mesa_meta_setup_sampler(ctx, texObj, target, filter,
707 srcLevel);
708
709 /* Always do our blits with no net sRGB decode or encode.
710 *
711 * However, if both the src and dst can be srgb decode/encoded, enable them
712 * so that we do any blending (from scaling or from MSAA resolves) in the
713 * right colorspace.
714 *
715 * Our choice of not doing any net encode/decode is from the GL 3.0
716 * specification:
717 *
718 * "Blit operations bypass the fragment pipeline. The only fragment
719 * operations which affect a blit are the pixel ownership test and the
720 * scissor test."
721 *
722 * The GL 4.4 specification disagrees and says that the sRGB part of the
723 * fragment pipeline applies, but this was found to break applications.
724 */
725 if (ctx->Extensions.EXT_texture_sRGB_decode) {
726 if (_mesa_get_format_color_encoding(rb->Format) == GL_SRGB &&
727 drawFb->Visual.sRGBCapable) {
728 _mesa_set_sampler_srgb_decode(ctx, fb_tex_blit.samp_obj,
729 GL_DECODE_EXT);
730 _mesa_set_framebuffer_srgb(ctx, GL_TRUE);
731 } else {
732 _mesa_set_sampler_srgb_decode(ctx, fb_tex_blit.samp_obj,
733 GL_SKIP_DECODE_EXT);
734 /* set_framebuffer_srgb was set by _mesa_meta_begin(). */
735 }
736 }
737
738 if (!glsl_version) {
739 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
740 _mesa_set_enable(ctx, target, GL_TRUE);
741 }
742
743 /* Prepare vertex data (the VBO was previously created and bound) */
744 {
745 struct vertex verts[4];
746 GLfloat s0, t0, s1, t1;
747
748 if (target == GL_TEXTURE_2D) {
749 const struct gl_texture_image *texImage
750 = _mesa_select_tex_image(texObj, target, srcLevel);
751 s0 = srcX0 / (float) texImage->Width;
752 s1 = srcX1 / (float) texImage->Width;
753 t0 = srcY0 / (float) texImage->Height;
754 t1 = srcY1 / (float) texImage->Height;
755 }
756 else {
757 assert(target == GL_TEXTURE_RECTANGLE_ARB ||
758 target == GL_TEXTURE_2D_MULTISAMPLE ||
759 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY);
760 s0 = (float) srcX0;
761 s1 = (float) srcX1;
762 t0 = (float) srcY0;
763 t1 = (float) srcY1;
764 }
765
766 /* Silence valgrind warnings about reading uninitialized stack. */
767 memset(verts, 0, sizeof(verts));
768
769 /* setup vertex positions */
770 verts[0].x = -1.0F * flipX;
771 verts[0].y = -1.0F * flipY;
772 verts[1].x = 1.0F * flipX;
773 verts[1].y = -1.0F * flipY;
774 verts[2].x = 1.0F * flipX;
775 verts[2].y = 1.0F * flipY;
776 verts[3].x = -1.0F * flipX;
777 verts[3].y = 1.0F * flipY;
778
779 verts[0].tex[0] = s0;
780 verts[0].tex[1] = t0;
781 verts[0].tex[2] = readAtt->Zoffset;
782 verts[1].tex[0] = s1;
783 verts[1].tex[1] = t0;
784 verts[1].tex[2] = readAtt->Zoffset;
785 verts[2].tex[0] = s1;
786 verts[2].tex[1] = t1;
787 verts[2].tex[2] = readAtt->Zoffset;
788 verts[3].tex[0] = s0;
789 verts[3].tex[1] = t1;
790 verts[3].tex[2] = readAtt->Zoffset;
791
792 _mesa_buffer_sub_data(ctx, blit->buf_obj, 0, sizeof(verts), verts,
793 __func__);
794 }
795
796 /* setup viewport */
797 _mesa_set_viewport(ctx, 0, dstX, dstY, dstW, dstH);
798 _mesa_ColorMask(!do_depth, !do_depth, !do_depth, !do_depth);
799 _mesa_set_enable(ctx, GL_DEPTH_TEST, do_depth);
800 _mesa_DepthMask(do_depth);
801 _mesa_DepthFunc(GL_ALWAYS);
802
803 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
804 _mesa_meta_fb_tex_blit_end(ctx, target, &fb_tex_blit);
805
806 return true;
807 }
808
809 void
810 _mesa_meta_fb_tex_blit_begin(struct gl_context *ctx,
811 struct fb_tex_blit_state *blit)
812 {
813 /* None of the existing callers preinitialize fb_tex_blit_state to zeros,
814 * and both use stack variables. If samp_obj_save is not NULL,
815 * _mesa_reference_sampler_object will try to dereference it. Leaving
816 * random garbage in samp_obj_save can only lead to crashes.
817 *
818 * Since the state isn't persistent across calls, we won't catch ref
819 * counting problems.
820 */
821 blit->samp_obj_save = NULL;
822 _mesa_reference_sampler_object(ctx, &blit->samp_obj_save,
823 ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler);
824 blit->tempTex = 0;
825 }
826
827 void
828 _mesa_meta_fb_tex_blit_end(struct gl_context *ctx, GLenum target,
829 struct fb_tex_blit_state *blit)
830 {
831 /* Restore texture object state, the texture binding will
832 * be restored by _mesa_meta_end().
833 */
834 if (target != GL_TEXTURE_RECTANGLE_ARB) {
835 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, blit->baseLevelSave);
836 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, blit->maxLevelSave);
837
838 if (ctx->Extensions.ARB_stencil_texturing) {
839 const struct gl_texture_object *texObj =
840 _mesa_get_current_tex_object(ctx, target);
841
842 if (texObj->StencilSampling != blit->stencilSamplingSave)
843 _mesa_TexParameteri(target, GL_DEPTH_STENCIL_TEXTURE_MODE,
844 blit->stencilSamplingSave ?
845 GL_STENCIL_INDEX : GL_DEPTH_COMPONENT);
846 }
847 }
848
849 _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, blit->samp_obj_save);
850 _mesa_reference_sampler_object(ctx, &blit->samp_obj_save, NULL);
851 _mesa_reference_sampler_object(ctx, &blit->samp_obj, NULL);
852
853 if (blit->tempTex)
854 _mesa_DeleteTextures(1, &blit->tempTex);
855 }
856
857 GLboolean
858 _mesa_meta_bind_rb_as_tex_image(struct gl_context *ctx,
859 struct gl_renderbuffer *rb,
860 GLuint *tex,
861 struct gl_texture_object **texObj,
862 GLenum *target)
863 {
864 struct gl_texture_image *texImage;
865 GLuint tempTex;
866
867 if (rb->NumSamples > 1)
868 *target = GL_TEXTURE_2D_MULTISAMPLE;
869 else
870 *target = GL_TEXTURE_2D;
871
872 tempTex = 0;
873 _mesa_GenTextures(1, &tempTex);
874 if (tempTex == 0)
875 return false;
876
877 *tex = tempTex;
878
879 _mesa_BindTexture(*target, *tex);
880 *texObj = _mesa_lookup_texture(ctx, *tex);
881 texImage = _mesa_get_tex_image(ctx, *texObj, *target, 0);
882
883 if (!ctx->Driver.BindRenderbufferTexImage(ctx, rb, texImage)) {
884 _mesa_DeleteTextures(1, tex);
885 return false;
886 }
887
888 if (ctx->Driver.FinishRenderTexture && !rb->NeedsFinishRenderTexture) {
889 rb->NeedsFinishRenderTexture = true;
890 ctx->Driver.FinishRenderTexture(ctx, rb);
891 }
892
893 return true;
894 }
895
896 struct gl_sampler_object *
897 _mesa_meta_setup_sampler(struct gl_context *ctx,
898 const struct gl_texture_object *texObj,
899 GLenum target, GLenum filter, GLuint srcLevel)
900 {
901 struct gl_sampler_object *samp_obj;
902 GLenum tex_filter = (filter == GL_SCALED_RESOLVE_FASTEST_EXT ||
903 filter == GL_SCALED_RESOLVE_NICEST_EXT) ?
904 GL_NEAREST : filter;
905
906 samp_obj = ctx->Driver.NewSamplerObject(ctx, 0xDEADBEEF);
907 if (samp_obj == NULL)
908 return NULL;
909
910 _mesa_bind_sampler(ctx, ctx->Texture.CurrentUnit, samp_obj);
911 _mesa_set_sampler_filters(ctx, samp_obj, tex_filter, tex_filter);
912 _mesa_set_sampler_wrap(ctx, samp_obj, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE,
913 samp_obj->WrapR);
914
915 /* Prepare src texture state */
916 _mesa_BindTexture(target, texObj->Name);
917 if (target != GL_TEXTURE_RECTANGLE_ARB) {
918 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, srcLevel);
919 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel);
920 }
921
922 return samp_obj;
923 }
924
925 /**
926 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
927 * of texture mapping and polygon rendering.
928 */
929 GLbitfield
930 _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
931 const struct gl_framebuffer *readFb,
932 const struct gl_framebuffer *drawFb,
933 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
934 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
935 GLbitfield mask, GLenum filter)
936 {
937 const GLint dstW = abs(dstX1 - dstX0);
938 const GLint dstH = abs(dstY1 - dstY0);
939 const GLint dstFlipX = (dstX1 - dstX0) / dstW;
940 const GLint dstFlipY = (dstY1 - dstY0) / dstH;
941
942 struct {
943 GLint srcX0, srcY0, srcX1, srcY1;
944 GLint dstX0, dstY0, dstX1, dstY1;
945 } clip = {
946 srcX0, srcY0, srcX1, srcY1,
947 dstX0, dstY0, dstX1, dstY1
948 };
949
950 const GLboolean use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
951 ctx->Extensions.ARB_fragment_shader;
952
953 /* Multisample texture blit support requires texture multisample. */
954 if (readFb->Visual.samples > 0 &&
955 !ctx->Extensions.ARB_texture_multisample) {
956 return mask;
957 }
958
959 /* Clip a copy of the blit coordinates. If these differ from the input
960 * coordinates, then we'll set the scissor.
961 */
962 if (!_mesa_clip_blit(ctx, readFb, drawFb,
963 &clip.srcX0, &clip.srcY0, &clip.srcX1, &clip.srcY1,
964 &clip.dstX0, &clip.dstY0, &clip.dstX1, &clip.dstY1)) {
965 /* clipped/scissored everything away */
966 return 0;
967 }
968
969 /* Only scissor affects blit, but we're doing to set a custom scissor if
970 * necessary anyway, so save/clear state.
971 */
972 _mesa_meta_begin(ctx, MESA_META_ALL & ~MESA_META_DRAW_BUFFERS);
973
974 /* Dithering shouldn't be performed for glBlitFramebuffer */
975 _mesa_set_enable(ctx, GL_DITHER, GL_FALSE);
976
977 /* If the clipping earlier changed the destination rect at all, then
978 * enable the scissor to clip to it.
979 */
980 if (clip.dstX0 != dstX0 || clip.dstY0 != dstY0 ||
981 clip.dstX1 != dstX1 || clip.dstY1 != dstY1) {
982 _mesa_set_enable(ctx, GL_SCISSOR_TEST, GL_TRUE);
983 _mesa_Scissor(MIN2(clip.dstX0, clip.dstX1),
984 MIN2(clip.dstY0, clip.dstY1),
985 abs(clip.dstX0 - clip.dstX1),
986 abs(clip.dstY0 - clip.dstY1));
987 }
988
989 /* Try faster, direct texture approach first */
990 if (mask & GL_COLOR_BUFFER_BIT) {
991 if (blitframebuffer_texture(ctx, readFb, drawFb,
992 srcX0, srcY0, srcX1, srcY1,
993 dstX0, dstY0, dstX1, dstY1,
994 filter, dstFlipX, dstFlipY,
995 use_glsl_version, false)) {
996 mask &= ~GL_COLOR_BUFFER_BIT;
997 }
998 }
999
1000 if (mask & GL_DEPTH_BUFFER_BIT && use_glsl_version) {
1001 if (blitframebuffer_texture(ctx, readFb, drawFb,
1002 srcX0, srcY0, srcX1, srcY1,
1003 dstX0, dstY0, dstX1, dstY1,
1004 filter, dstFlipX, dstFlipY,
1005 use_glsl_version, true)) {
1006 mask &= ~GL_DEPTH_BUFFER_BIT;
1007 }
1008 }
1009
1010 if (mask & GL_STENCIL_BUFFER_BIT) {
1011 /* XXX can't easily do stencil */
1012 }
1013
1014 _mesa_meta_end(ctx);
1015
1016 return mask;
1017 }
1018
1019 void
1020 _mesa_meta_glsl_blit_cleanup(struct gl_context *ctx, struct blit_state *blit)
1021 {
1022 if (blit->VAO) {
1023 _mesa_DeleteVertexArrays(1, &blit->VAO);
1024 blit->VAO = 0;
1025 _mesa_reference_buffer_object(ctx, &blit->buf_obj, NULL);
1026 }
1027
1028 _mesa_meta_blit_shader_table_cleanup(&blit->shaders_with_depth);
1029 _mesa_meta_blit_shader_table_cleanup(&blit->shaders_without_depth);
1030
1031 _mesa_DeleteTextures(1, &blit->depthTex.TexObj);
1032 blit->depthTex.TexObj = 0;
1033 }
1034
1035 void
1036 _mesa_meta_and_swrast_BlitFramebuffer(struct gl_context *ctx,
1037 struct gl_framebuffer *readFb,
1038 struct gl_framebuffer *drawFb,
1039 GLint srcX0, GLint srcY0,
1040 GLint srcX1, GLint srcY1,
1041 GLint dstX0, GLint dstY0,
1042 GLint dstX1, GLint dstY1,
1043 GLbitfield mask, GLenum filter)
1044 {
1045 mask = _mesa_meta_BlitFramebuffer(ctx, readFb, drawFb,
1046 srcX0, srcY0, srcX1, srcY1,
1047 dstX0, dstY0, dstX1, dstY1,
1048 mask, filter);
1049 if (mask == 0x0)
1050 return;
1051
1052 _swrast_BlitFramebuffer(ctx, readFb, drawFb,
1053 srcX0, srcY0, srcX1, srcY1,
1054 dstX0, dstY0, dstX1, dstY1,
1055 mask, filter);
1056 }