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