meta blit: Set Z texcoord during meta blit to sample the correct layer
[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/varray.h"
49 #include "main/viewport.h"
50 #include "swrast/swrast.h"
51 #include "drivers/common/meta.h"
52 #include "../glsl/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_shader(struct gl_context *ctx,
59 struct blit_state *blit,
60 struct gl_renderbuffer *src_rb,
61 GLenum target)
62 {
63 const char *vs_source;
64 char *fs_source;
65 void *mem_ctx;
66 enum blit_msaa_shader shader_index;
67 bool dst_is_msaa = false;
68 GLenum src_datatype;
69 const char *vec4_prefix;
70 const char *sampler_array_suffix = "";
71 char *name;
72 const char *texcoord_type = "vec2";
73
74 if (src_rb) {
75 src_datatype = _mesa_get_format_datatype(src_rb->Format);
76 } else {
77 /* depth-or-color glCopyTexImage fallback path that passes a NULL rb and
78 * doesn't handle integer.
79 */
80 src_datatype = GL_UNSIGNED_NORMALIZED;
81 }
82
83 if (ctx->DrawBuffer->Visual.samples > 1) {
84 /* If you're calling meta_BlitFramebuffer with the destination
85 * multisampled, this is the only path that will work -- swrast and
86 * CopyTexImage won't work on it either.
87 */
88 assert(ctx->Extensions.ARB_sample_shading);
89
90 dst_is_msaa = true;
91
92 /* We need shader invocation per sample, not per pixel */
93 _mesa_set_enable(ctx, GL_MULTISAMPLE, GL_TRUE);
94 _mesa_set_enable(ctx, GL_SAMPLE_SHADING, GL_TRUE);
95 _mesa_MinSampleShading(1.0);
96 }
97
98 switch (target) {
99 case GL_TEXTURE_2D_MULTISAMPLE:
100 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
101 if (src_rb->_BaseFormat == GL_DEPTH_COMPONENT ||
102 src_rb->_BaseFormat == GL_DEPTH_STENCIL) {
103 if (dst_is_msaa)
104 shader_index = BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY;
105 else
106 shader_index = BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE;
107 } else {
108 if (dst_is_msaa)
109 shader_index = BLIT_MSAA_SHADER_2D_MULTISAMPLE_COPY;
110 else
111 shader_index = BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE;
112 }
113
114 if (target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) {
115 shader_index += (BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_RESOLVE -
116 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE);
117 sampler_array_suffix = "Array";
118 texcoord_type = "vec3";
119 }
120 break;
121 default:
122 _mesa_problem(ctx, "Unkown texture target %s\n",
123 _mesa_lookup_enum_by_nr(target));
124 shader_index = BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE;
125 }
126
127 /* We rely on the enum being sorted this way. */
128 STATIC_ASSERT(BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_INT ==
129 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE + 1);
130 STATIC_ASSERT(BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE_UINT ==
131 BLIT_MSAA_SHADER_2D_MULTISAMPLE_RESOLVE + 2);
132 if (src_datatype == GL_INT) {
133 shader_index++;
134 vec4_prefix = "i";
135 } else if (src_datatype == GL_UNSIGNED_INT) {
136 shader_index += 2;
137 vec4_prefix = "u";
138 } else {
139 vec4_prefix = "";
140 }
141
142 if (blit->msaa_shaders[shader_index]) {
143 _mesa_UseProgram(blit->msaa_shaders[shader_index]);
144 return;
145 }
146
147 mem_ctx = ralloc_context(NULL);
148
149 if (shader_index == BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_RESOLVE ||
150 shader_index == BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_DEPTH_RESOLVE ||
151 shader_index == BLIT_MSAA_SHADER_2D_MULTISAMPLE_ARRAY_DEPTH_COPY ||
152 shader_index == BLIT_MSAA_SHADER_2D_MULTISAMPLE_DEPTH_COPY) {
153 char *sample_index;
154 const char *arb_sample_shading_extension_string;
155
156 if (dst_is_msaa) {
157 arb_sample_shading_extension_string = "#extension GL_ARB_sample_shading : enable";
158 sample_index = "gl_SampleID";
159 name = "depth MSAA copy";
160 } else {
161 /* Don't need that extension, since we're drawing to a single-sampled
162 * destination.
163 */
164 arb_sample_shading_extension_string = "";
165 /* From the GL 4.3 spec:
166 *
167 * "If there is a multisample buffer (the value of SAMPLE_BUFFERS
168 * is one), then values are obtained from the depth samples in
169 * this buffer. It is recommended that the depth value of the
170 * centermost sample be used, though implementations may choose
171 * any function of the depth sample values at each pixel.
172 *
173 * We're slacking and instead of choosing centermost, we've got 0.
174 */
175 sample_index = "0";
176 name = "depth MSAA resolve";
177 }
178
179 vs_source = ralloc_asprintf(mem_ctx,
180 "#version 130\n"
181 "in vec2 position;\n"
182 "in %s textureCoords;\n"
183 "out %s texCoords;\n"
184 "void main()\n"
185 "{\n"
186 " texCoords = textureCoords;\n"
187 " gl_Position = vec4(position, 0.0, 1.0);\n"
188 "}\n",
189 texcoord_type,
190 texcoord_type);
191 fs_source = ralloc_asprintf(mem_ctx,
192 "#version 130\n"
193 "#extension GL_ARB_texture_multisample : enable\n"
194 "%s\n"
195 "uniform sampler2DMS%s texSampler;\n"
196 "in %s texCoords;\n"
197 "out vec4 out_color;\n"
198 "\n"
199 "void main()\n"
200 "{\n"
201 " gl_FragDepth = texelFetch(texSampler, i%s(texCoords), %s).r;\n"
202 "}\n",
203 arb_sample_shading_extension_string,
204 sampler_array_suffix,
205 texcoord_type,
206 texcoord_type,
207 sample_index);
208 } else {
209 /* You can create 2D_MULTISAMPLE textures with 0 sample count (meaning 1
210 * sample). Yes, this is ridiculous.
211 */
212 int samples;
213 char *sample_resolve;
214 const char *arb_sample_shading_extension_string;
215 const char *merge_function;
216 name = ralloc_asprintf(mem_ctx, "%svec4 MSAA %s",
217 vec4_prefix,
218 dst_is_msaa ? "copy" : "resolve");
219
220 samples = MAX2(src_rb->NumSamples, 1);
221
222 if (dst_is_msaa) {
223 arb_sample_shading_extension_string = "#extension GL_ARB_sample_shading : enable";
224 sample_resolve = ralloc_asprintf(mem_ctx, " out_color = texelFetch(texSampler, i%s(texCoords), gl_SampleID);", texcoord_type);
225 merge_function = "";
226 } else {
227 int i;
228 int step;
229
230 if (src_datatype == GL_INT || src_datatype == GL_UNSIGNED_INT) {
231 merge_function =
232 "gvec4 merge(gvec4 a, gvec4 b) { return (a >> gvec4(1)) + (b >> gvec4(1)) + (a & b & gvec4(1)); }\n";
233 } else {
234 /* The divide will happen at the end for floats. */
235 merge_function =
236 "vec4 merge(vec4 a, vec4 b) { return (a + b); }\n";
237 }
238
239 arb_sample_shading_extension_string = "";
240
241 /* We're assuming power of two samples for this resolution procedure.
242 *
243 * To avoid losing any floating point precision if the samples all
244 * happen to have the same value, we merge pairs of values at a time
245 * (so the floating point exponent just gets increased), rather than
246 * doing a naive sum and dividing.
247 */
248 assert((samples & (samples - 1)) == 0);
249 /* Fetch each individual sample. */
250 sample_resolve = rzalloc_size(mem_ctx, 1);
251 for (i = 0; i < samples; i++) {
252 ralloc_asprintf_append(&sample_resolve,
253 " gvec4 sample_1_%d = texelFetch(texSampler, i%s(texCoords), %d);\n",
254 i, texcoord_type, i);
255 }
256 /* Now, merge each pair of samples, then merge each pair of those,
257 * etc.
258 */
259 for (step = 2; step <= samples; step *= 2) {
260 for (i = 0; i < samples; i += step) {
261 ralloc_asprintf_append(&sample_resolve,
262 " gvec4 sample_%d_%d = merge(sample_%d_%d, sample_%d_%d);\n",
263 step, i,
264 step / 2, i,
265 step / 2, i + step / 2);
266 }
267 }
268
269 /* Scale the final result. */
270 if (src_datatype == GL_UNSIGNED_INT || src_datatype == GL_INT) {
271 ralloc_asprintf_append(&sample_resolve,
272 " out_color = sample_%d_0;\n",
273 samples);
274 } else {
275 ralloc_asprintf_append(&sample_resolve,
276 " out_color = sample_%d_0 / %f;\n",
277 samples, (float)samples);
278 }
279 }
280
281 vs_source = ralloc_asprintf(mem_ctx,
282 "#version 130\n"
283 "in vec2 position;\n"
284 "in %s textureCoords;\n"
285 "out %s texCoords;\n"
286 "void main()\n"
287 "{\n"
288 " texCoords = textureCoords;\n"
289 " gl_Position = vec4(position, 0.0, 1.0);\n"
290 "}\n",
291 texcoord_type,
292 texcoord_type);
293 fs_source = ralloc_asprintf(mem_ctx,
294 "#version 130\n"
295 "#extension GL_ARB_texture_multisample : enable\n"
296 "%s\n"
297 "#define gvec4 %svec4\n"
298 "uniform %ssampler2DMS%s texSampler;\n"
299 "in %s texCoords;\n"
300 "out gvec4 out_color;\n"
301 "\n"
302 "%s" /* merge_function */
303 "void main()\n"
304 "{\n"
305 "%s\n" /* sample_resolve */
306 "}\n",
307 arb_sample_shading_extension_string,
308 vec4_prefix,
309 vec4_prefix,
310 sampler_array_suffix,
311 texcoord_type,
312 merge_function,
313 sample_resolve);
314 }
315
316 _mesa_meta_compile_and_link_program(ctx, vs_source, fs_source, name,
317 &blit->msaa_shaders[shader_index]);
318
319 ralloc_free(mem_ctx);
320 }
321
322 static void
323 setup_glsl_blit_framebuffer(struct gl_context *ctx,
324 struct blit_state *blit,
325 struct gl_renderbuffer *src_rb,
326 GLenum target)
327 {
328 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
329 assert(_mesa_is_desktop_gl(ctx) || target == GL_TEXTURE_2D);
330
331 unsigned texcoord_size = 2 + (src_rb->Depth > 1 ? 1 : 0);
332
333 _mesa_meta_setup_vertex_objects(&blit->VAO, &blit->VBO, true,
334 2, texcoord_size, 0);
335
336 if (target == GL_TEXTURE_2D_MULTISAMPLE ||
337 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) {
338 setup_glsl_msaa_blit_shader(ctx, blit, src_rb, target);
339 } else {
340 _mesa_meta_setup_blit_shader(ctx, target, &blit->shaders);
341 }
342 }
343
344 /**
345 * Try to do a color or depth glBlitFramebuffer using texturing.
346 *
347 * We can do this when the src renderbuffer is actually a texture, or when the
348 * driver exposes BindRenderbufferTexImage().
349 */
350 static bool
351 blitframebuffer_texture(struct gl_context *ctx,
352 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
353 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
354 GLenum filter, GLint flipX, GLint flipY,
355 GLboolean glsl_version, GLboolean do_depth)
356 {
357 const struct gl_framebuffer *readFb = ctx->ReadBuffer;
358 int att_index = do_depth ? BUFFER_DEPTH : readFb->_ColorReadBufferIndex;
359 const struct gl_renderbuffer_attachment *readAtt =
360 &readFb->Attachment[att_index];
361 struct blit_state *blit = &ctx->Meta->Blit;
362 struct fb_tex_blit_state fb_tex_blit;
363 const GLint dstX = MIN2(dstX0, dstX1);
364 const GLint dstY = MIN2(dstY0, dstY1);
365 const GLint dstW = abs(dstX1 - dstX0);
366 const GLint dstH = abs(dstY1 - dstY0);
367 struct gl_texture_object *texObj;
368 GLuint srcLevel;
369 GLenum target;
370 struct gl_renderbuffer *rb = readAtt->Renderbuffer;
371 struct temp_texture *meta_temp_texture;
372
373 if (rb->NumSamples && !ctx->Extensions.ARB_texture_multisample)
374 return false;
375
376 if (filter == GL_SCALED_RESOLVE_FASTEST_EXT ||
377 filter == GL_SCALED_RESOLVE_NICEST_EXT) {
378 filter = GL_LINEAR;
379 }
380
381 _mesa_meta_fb_tex_blit_begin(ctx, &fb_tex_blit);
382
383 if (readAtt->Texture &&
384 (readAtt->Texture->Target == GL_TEXTURE_2D ||
385 readAtt->Texture->Target == GL_TEXTURE_RECTANGLE ||
386 readAtt->Texture->Target == GL_TEXTURE_2D_MULTISAMPLE ||
387 readAtt->Texture->Target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)) {
388 /* If there's a texture attached of a type we can handle, then just use
389 * it directly.
390 */
391 srcLevel = readAtt->TextureLevel;
392 texObj = readAtt->Texture;
393 target = texObj->Target;
394 } else if (!readAtt->Texture && ctx->Driver.BindRenderbufferTexImage) {
395 if (!_mesa_meta_bind_rb_as_tex_image(ctx, rb, &fb_tex_blit.tempTex,
396 &texObj, &target))
397 return false;
398
399 srcLevel = 0;
400 if (_mesa_is_winsys_fbo(readFb)) {
401 GLint temp = srcY0;
402 srcY0 = rb->Height - srcY1;
403 srcY1 = rb->Height - temp;
404 flipY = -flipY;
405 }
406 } else {
407 GLenum tex_base_format;
408 int srcW = abs(srcY1 - srcY0);
409 int srcH = abs(srcY1 - srcY0);
410 /* Fall back to doing a CopyTexSubImage to get the destination
411 * renderbuffer into a texture.
412 */
413 if (ctx->Meta->Blit.no_ctsi_fallback)
414 return false;
415
416 if (rb->NumSamples > 1)
417 return false;
418
419 if (do_depth) {
420 meta_temp_texture = _mesa_meta_get_temp_depth_texture(ctx);
421 tex_base_format = GL_DEPTH_COMPONENT;
422 } else {
423 meta_temp_texture = _mesa_meta_get_temp_texture(ctx);
424 tex_base_format =
425 _mesa_base_tex_format(ctx, rb->InternalFormat);
426 }
427
428 srcLevel = 0;
429 target = meta_temp_texture->Target;
430 texObj = _mesa_lookup_texture(ctx, meta_temp_texture->TexObj);
431
432 _mesa_meta_setup_copypix_texture(ctx, meta_temp_texture,
433 srcX0, srcY0,
434 srcW, srcH,
435 tex_base_format,
436 filter);
437
438
439 srcX0 = 0;
440 srcY0 = 0;
441 srcX1 = srcW;
442 srcY1 = srcH;
443 }
444
445 fb_tex_blit.baseLevelSave = texObj->BaseLevel;
446 fb_tex_blit.maxLevelSave = texObj->MaxLevel;
447
448 if (glsl_version) {
449 setup_glsl_blit_framebuffer(ctx, blit, rb, target);
450 }
451 else {
452 _mesa_meta_setup_ff_tnl_for_blit(&ctx->Meta->Blit.VAO,
453 &ctx->Meta->Blit.VBO,
454 2);
455 }
456
457 /*
458 printf("Blit from texture!\n");
459 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
460 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
461 */
462
463 fb_tex_blit.sampler = _mesa_meta_setup_sampler(ctx, texObj, target, filter,
464 srcLevel);
465
466 /* Always do our blits with no net sRGB decode or encode.
467 *
468 * However, if both the src and dst can be srgb decode/encoded, enable them
469 * so that we do any blending (from scaling or from MSAA resolves) in the
470 * right colorspace.
471 *
472 * Our choice of not doing any net encode/decode is from the GL 3.0
473 * specification:
474 *
475 * "Blit operations bypass the fragment pipeline. The only fragment
476 * operations which affect a blit are the pixel ownership test and the
477 * scissor test."
478 *
479 * The GL 4.4 specification disagrees and says that the sRGB part of the
480 * fragment pipeline applies, but this was found to break applications.
481 */
482 if (ctx->Extensions.EXT_texture_sRGB_decode) {
483 if (_mesa_get_format_color_encoding(rb->Format) == GL_SRGB &&
484 ctx->DrawBuffer->Visual.sRGBCapable) {
485 _mesa_SamplerParameteri(fb_tex_blit.sampler,
486 GL_TEXTURE_SRGB_DECODE_EXT, GL_DECODE_EXT);
487 _mesa_set_framebuffer_srgb(ctx, GL_TRUE);
488 } else {
489 _mesa_SamplerParameteri(fb_tex_blit.sampler,
490 GL_TEXTURE_SRGB_DECODE_EXT,
491 GL_SKIP_DECODE_EXT);
492 /* set_framebuffer_srgb was set by _mesa_meta_begin(). */
493 }
494 }
495
496 if (!glsl_version) {
497 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
498 _mesa_set_enable(ctx, target, GL_TRUE);
499 }
500
501 /* Prepare vertex data (the VBO was previously created and bound) */
502 {
503 struct vertex verts[4];
504 GLfloat s0, t0, s1, t1;
505
506 if (target == GL_TEXTURE_2D) {
507 const struct gl_texture_image *texImage
508 = _mesa_select_tex_image(ctx, texObj, target, srcLevel);
509 s0 = srcX0 / (float) texImage->Width;
510 s1 = srcX1 / (float) texImage->Width;
511 t0 = srcY0 / (float) texImage->Height;
512 t1 = srcY1 / (float) texImage->Height;
513 }
514 else {
515 assert(target == GL_TEXTURE_RECTANGLE_ARB ||
516 target == GL_TEXTURE_2D_MULTISAMPLE ||
517 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY);
518 s0 = (float) srcX0;
519 s1 = (float) srcX1;
520 t0 = (float) srcY0;
521 t1 = (float) srcY1;
522 }
523
524 /* Silence valgrind warnings about reading uninitialized stack. */
525 memset(verts, 0, sizeof(verts));
526
527 /* setup vertex positions */
528 verts[0].x = -1.0F * flipX;
529 verts[0].y = -1.0F * flipY;
530 verts[1].x = 1.0F * flipX;
531 verts[1].y = -1.0F * flipY;
532 verts[2].x = 1.0F * flipX;
533 verts[2].y = 1.0F * flipY;
534 verts[3].x = -1.0F * flipX;
535 verts[3].y = 1.0F * flipY;
536
537 verts[0].tex[0] = s0;
538 verts[0].tex[1] = t0;
539 verts[0].tex[2] = readAtt->Zoffset;
540 verts[1].tex[0] = s1;
541 verts[1].tex[1] = t0;
542 verts[1].tex[2] = readAtt->Zoffset;
543 verts[2].tex[0] = s1;
544 verts[2].tex[1] = t1;
545 verts[2].tex[2] = readAtt->Zoffset;
546 verts[3].tex[0] = s0;
547 verts[3].tex[1] = t1;
548 verts[3].tex[2] = readAtt->Zoffset;
549
550 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
551 }
552
553 /* setup viewport */
554 _mesa_set_viewport(ctx, 0, dstX, dstY, dstW, dstH);
555 _mesa_ColorMask(!do_depth, !do_depth, !do_depth, !do_depth);
556 _mesa_set_enable(ctx, GL_DEPTH_TEST, do_depth);
557 _mesa_DepthMask(do_depth);
558 _mesa_DepthFunc(GL_ALWAYS);
559
560 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
561 _mesa_meta_fb_tex_blit_end(ctx, target, &fb_tex_blit);
562
563 return true;
564 }
565
566 void
567 _mesa_meta_fb_tex_blit_begin(const struct gl_context *ctx,
568 struct fb_tex_blit_state *blit)
569 {
570 blit->samplerSave =
571 ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler ?
572 ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler->Name : 0;
573 blit->tempTex = 0;
574 }
575
576 void
577 _mesa_meta_fb_tex_blit_end(const struct gl_context *ctx, GLenum target,
578 struct fb_tex_blit_state *blit)
579 {
580 /* Restore texture object state, the texture binding will
581 * be restored by _mesa_meta_end().
582 */
583 if (target != GL_TEXTURE_RECTANGLE_ARB) {
584 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, blit->baseLevelSave);
585 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, blit->maxLevelSave);
586 }
587
588 _mesa_BindSampler(ctx->Texture.CurrentUnit, blit->samplerSave);
589 _mesa_DeleteSamplers(1, &blit->sampler);
590 if (blit->tempTex)
591 _mesa_DeleteTextures(1, &blit->tempTex);
592 }
593
594 GLboolean
595 _mesa_meta_bind_rb_as_tex_image(struct gl_context *ctx,
596 struct gl_renderbuffer *rb,
597 GLuint *tex,
598 struct gl_texture_object **texObj,
599 GLenum *target)
600 {
601 struct gl_texture_image *texImage;
602
603 if (rb->NumSamples > 1)
604 *target = GL_TEXTURE_2D_MULTISAMPLE;
605 else
606 *target = GL_TEXTURE_2D;
607
608 _mesa_GenTextures(1, tex);
609 _mesa_BindTexture(*target, *tex);
610 *texObj = _mesa_lookup_texture(ctx, *tex);
611 texImage = _mesa_get_tex_image(ctx, *texObj, *target, 0);
612
613 if (!ctx->Driver.BindRenderbufferTexImage(ctx, rb, texImage)) {
614 _mesa_DeleteTextures(1, tex);
615 return false;
616 }
617
618 if (ctx->Driver.FinishRenderTexture && !rb->NeedsFinishRenderTexture) {
619 rb->NeedsFinishRenderTexture = true;
620 ctx->Driver.FinishRenderTexture(ctx, rb);
621 }
622
623 return true;
624 }
625
626 GLuint
627 _mesa_meta_setup_sampler(struct gl_context *ctx,
628 const struct gl_texture_object *texObj,
629 GLenum target, GLenum filter, GLuint srcLevel)
630 {
631 GLuint sampler;
632
633 _mesa_GenSamplers(1, &sampler);
634 _mesa_BindSampler(ctx->Texture.CurrentUnit, sampler);
635
636 /* Prepare src texture state */
637 _mesa_BindTexture(target, texObj->Name);
638 _mesa_SamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, filter);
639 _mesa_SamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, filter);
640 if (target != GL_TEXTURE_RECTANGLE_ARB) {
641 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, srcLevel);
642 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel);
643 }
644 _mesa_SamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
645 _mesa_SamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
646
647 return sampler;
648 }
649
650 /**
651 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
652 * of texture mapping and polygon rendering.
653 */
654 GLbitfield
655 _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
656 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
657 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
658 GLbitfield mask, GLenum filter)
659 {
660 const GLint dstW = abs(dstX1 - dstX0);
661 const GLint dstH = abs(dstY1 - dstY0);
662 const GLint dstFlipX = (dstX1 - dstX0) / dstW;
663 const GLint dstFlipY = (dstY1 - dstY0) / dstH;
664
665 struct {
666 GLint srcX0, srcY0, srcX1, srcY1;
667 GLint dstX0, dstY0, dstX1, dstY1;
668 } clip = {
669 srcX0, srcY0, srcX1, srcY1,
670 dstX0, dstY0, dstX1, dstY1
671 };
672
673 const GLboolean use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
674 ctx->Extensions.ARB_fragment_shader;
675
676 /* Multisample texture blit support requires texture multisample. */
677 if (ctx->ReadBuffer->Visual.samples > 0 &&
678 !ctx->Extensions.ARB_texture_multisample) {
679 return mask;
680 }
681
682 /* Clip a copy of the blit coordinates. If these differ from the input
683 * coordinates, then we'll set the scissor.
684 */
685 if (!_mesa_clip_blit(ctx, &clip.srcX0, &clip.srcY0, &clip.srcX1, &clip.srcY1,
686 &clip.dstX0, &clip.dstY0, &clip.dstX1, &clip.dstY1)) {
687 /* clipped/scissored everything away */
688 return 0;
689 }
690
691 /* Only scissor affects blit, but we're doing to set a custom scissor if
692 * necessary anyway, so save/clear state.
693 */
694 _mesa_meta_begin(ctx, MESA_META_ALL & ~MESA_META_DRAW_BUFFERS);
695
696 /* If the clipping earlier changed the destination rect at all, then
697 * enable the scissor to clip to it.
698 */
699 if (clip.dstX0 != dstX0 || clip.dstY0 != dstY0 ||
700 clip.dstX1 != dstX1 || clip.dstY1 != dstY1) {
701 _mesa_set_enable(ctx, GL_SCISSOR_TEST, GL_TRUE);
702 _mesa_Scissor(MIN2(clip.dstX0, clip.dstX1),
703 MIN2(clip.dstY0, clip.dstY1),
704 abs(clip.dstX0 - clip.dstX1),
705 abs(clip.dstY0 - clip.dstY1));
706 }
707
708 /* Try faster, direct texture approach first */
709 if (mask & GL_COLOR_BUFFER_BIT) {
710 if (blitframebuffer_texture(ctx, srcX0, srcY0, srcX1, srcY1,
711 dstX0, dstY0, dstX1, dstY1,
712 filter, dstFlipX, dstFlipY,
713 use_glsl_version, false)) {
714 mask &= ~GL_COLOR_BUFFER_BIT;
715 }
716 }
717
718 if (mask & GL_DEPTH_BUFFER_BIT && use_glsl_version) {
719 if (blitframebuffer_texture(ctx, srcX0, srcY0, srcX1, srcY1,
720 dstX0, dstY0, dstX1, dstY1,
721 filter, dstFlipX, dstFlipY,
722 use_glsl_version, true)) {
723 mask &= ~GL_DEPTH_BUFFER_BIT;
724 }
725 }
726
727 if (mask & GL_STENCIL_BUFFER_BIT) {
728 /* XXX can't easily do stencil */
729 }
730
731 _mesa_meta_end(ctx);
732
733 return mask;
734 }
735
736 void
737 _mesa_meta_glsl_blit_cleanup(struct blit_state *blit)
738 {
739 if (blit->VAO) {
740 _mesa_DeleteVertexArrays(1, &blit->VAO);
741 blit->VAO = 0;
742 _mesa_DeleteBuffers(1, &blit->VBO);
743 blit->VBO = 0;
744 }
745
746 _mesa_meta_blit_shader_table_cleanup(&blit->shaders);
747
748 _mesa_DeleteTextures(1, &blit->depthTex.TexObj);
749 blit->depthTex.TexObj = 0;
750 }
751
752 void
753 _mesa_meta_and_swrast_BlitFramebuffer(struct gl_context *ctx,
754 GLint srcX0, GLint srcY0,
755 GLint srcX1, GLint srcY1,
756 GLint dstX0, GLint dstY0,
757 GLint dstX1, GLint dstY1,
758 GLbitfield mask, GLenum filter)
759 {
760 mask = _mesa_meta_BlitFramebuffer(ctx,
761 srcX0, srcY0, srcX1, srcY1,
762 dstX0, dstY0, dstX1, dstY1,
763 mask, filter);
764 if (mask == 0x0)
765 return;
766
767 _swrast_BlitFramebuffer(ctx,
768 srcX0, srcY0, srcX1, srcY1,
769 dstX0, dstY0, dstX1, dstY1,
770 mask, filter);
771 }