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