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