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