mesa: Move declaration to top of block.
[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 unsigned texcoord_size;
329
330 /* target = GL_TEXTURE_RECTANGLE is not supported in GLES 3.0 */
331 assert(_mesa_is_desktop_gl(ctx) || target == GL_TEXTURE_2D);
332
333 texcoord_size = 2 + (src_rb->Depth > 1 ? 1 : 0);
334
335 _mesa_meta_setup_vertex_objects(&blit->VAO, &blit->VBO, true,
336 2, texcoord_size, 0);
337
338 if (target == GL_TEXTURE_2D_MULTISAMPLE ||
339 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY) {
340 setup_glsl_msaa_blit_shader(ctx, blit, src_rb, target);
341 } else {
342 _mesa_meta_setup_blit_shader(ctx, target, &blit->shaders);
343 }
344 }
345
346 /**
347 * Try to do a color or depth glBlitFramebuffer using texturing.
348 *
349 * We can do this when the src renderbuffer is actually a texture, or when the
350 * driver exposes BindRenderbufferTexImage().
351 */
352 static bool
353 blitframebuffer_texture(struct gl_context *ctx,
354 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
355 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
356 GLenum filter, GLint flipX, GLint flipY,
357 GLboolean glsl_version, GLboolean do_depth)
358 {
359 const struct gl_framebuffer *readFb = ctx->ReadBuffer;
360 int att_index = do_depth ? BUFFER_DEPTH : readFb->_ColorReadBufferIndex;
361 const struct gl_renderbuffer_attachment *readAtt =
362 &readFb->Attachment[att_index];
363 struct blit_state *blit = &ctx->Meta->Blit;
364 struct fb_tex_blit_state fb_tex_blit;
365 const GLint dstX = MIN2(dstX0, dstX1);
366 const GLint dstY = MIN2(dstY0, dstY1);
367 const GLint dstW = abs(dstX1 - dstX0);
368 const GLint dstH = abs(dstY1 - dstY0);
369 struct gl_texture_object *texObj;
370 GLuint srcLevel;
371 GLenum target;
372 struct gl_renderbuffer *rb = readAtt->Renderbuffer;
373 struct temp_texture *meta_temp_texture;
374
375 if (rb->NumSamples && !ctx->Extensions.ARB_texture_multisample)
376 return false;
377
378 if (filter == GL_SCALED_RESOLVE_FASTEST_EXT ||
379 filter == GL_SCALED_RESOLVE_NICEST_EXT) {
380 filter = GL_LINEAR;
381 }
382
383 _mesa_meta_fb_tex_blit_begin(ctx, &fb_tex_blit);
384
385 if (readAtt->Texture &&
386 (readAtt->Texture->Target == GL_TEXTURE_2D ||
387 readAtt->Texture->Target == GL_TEXTURE_RECTANGLE ||
388 readAtt->Texture->Target == GL_TEXTURE_2D_MULTISAMPLE ||
389 readAtt->Texture->Target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)) {
390 /* If there's a texture attached of a type we can handle, then just use
391 * it directly.
392 */
393 srcLevel = readAtt->TextureLevel;
394 texObj = readAtt->Texture;
395 target = texObj->Target;
396 } else if (!readAtt->Texture && ctx->Driver.BindRenderbufferTexImage) {
397 if (!_mesa_meta_bind_rb_as_tex_image(ctx, rb, &fb_tex_blit.tempTex,
398 &texObj, &target))
399 return false;
400
401 srcLevel = 0;
402 if (_mesa_is_winsys_fbo(readFb)) {
403 GLint temp = srcY0;
404 srcY0 = rb->Height - srcY1;
405 srcY1 = rb->Height - temp;
406 flipY = -flipY;
407 }
408 } else {
409 GLenum tex_base_format;
410 int srcW = abs(srcY1 - srcY0);
411 int srcH = abs(srcY1 - srcY0);
412 /* Fall back to doing a CopyTexSubImage to get the destination
413 * renderbuffer into a texture.
414 */
415 if (ctx->Meta->Blit.no_ctsi_fallback)
416 return false;
417
418 if (rb->NumSamples > 1)
419 return false;
420
421 if (do_depth) {
422 meta_temp_texture = _mesa_meta_get_temp_depth_texture(ctx);
423 tex_base_format = GL_DEPTH_COMPONENT;
424 } else {
425 meta_temp_texture = _mesa_meta_get_temp_texture(ctx);
426 tex_base_format =
427 _mesa_base_tex_format(ctx, rb->InternalFormat);
428 }
429
430 srcLevel = 0;
431 target = meta_temp_texture->Target;
432 texObj = _mesa_lookup_texture(ctx, meta_temp_texture->TexObj);
433
434 _mesa_meta_setup_copypix_texture(ctx, meta_temp_texture,
435 srcX0, srcY0,
436 srcW, srcH,
437 tex_base_format,
438 filter);
439
440
441 srcX0 = 0;
442 srcY0 = 0;
443 srcX1 = srcW;
444 srcY1 = srcH;
445 }
446
447 fb_tex_blit.baseLevelSave = texObj->BaseLevel;
448 fb_tex_blit.maxLevelSave = texObj->MaxLevel;
449
450 if (glsl_version) {
451 setup_glsl_blit_framebuffer(ctx, blit, rb, target);
452 }
453 else {
454 _mesa_meta_setup_ff_tnl_for_blit(&ctx->Meta->Blit.VAO,
455 &ctx->Meta->Blit.VBO,
456 2);
457 }
458
459 /*
460 printf("Blit from texture!\n");
461 printf(" srcAtt %p dstAtt %p\n", readAtt, drawAtt);
462 printf(" srcTex %p dstText %p\n", texObj, drawAtt->Texture);
463 */
464
465 fb_tex_blit.sampler = _mesa_meta_setup_sampler(ctx, texObj, target, filter,
466 srcLevel);
467
468 /* Always do our blits with no net sRGB decode or encode.
469 *
470 * However, if both the src and dst can be srgb decode/encoded, enable them
471 * so that we do any blending (from scaling or from MSAA resolves) in the
472 * right colorspace.
473 *
474 * Our choice of not doing any net encode/decode is from the GL 3.0
475 * specification:
476 *
477 * "Blit operations bypass the fragment pipeline. The only fragment
478 * operations which affect a blit are the pixel ownership test and the
479 * scissor test."
480 *
481 * The GL 4.4 specification disagrees and says that the sRGB part of the
482 * fragment pipeline applies, but this was found to break applications.
483 */
484 if (ctx->Extensions.EXT_texture_sRGB_decode) {
485 if (_mesa_get_format_color_encoding(rb->Format) == GL_SRGB &&
486 ctx->DrawBuffer->Visual.sRGBCapable) {
487 _mesa_SamplerParameteri(fb_tex_blit.sampler,
488 GL_TEXTURE_SRGB_DECODE_EXT, GL_DECODE_EXT);
489 _mesa_set_framebuffer_srgb(ctx, GL_TRUE);
490 } else {
491 _mesa_SamplerParameteri(fb_tex_blit.sampler,
492 GL_TEXTURE_SRGB_DECODE_EXT,
493 GL_SKIP_DECODE_EXT);
494 /* set_framebuffer_srgb was set by _mesa_meta_begin(). */
495 }
496 }
497
498 if (!glsl_version) {
499 _mesa_TexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
500 _mesa_set_enable(ctx, target, GL_TRUE);
501 }
502
503 /* Prepare vertex data (the VBO was previously created and bound) */
504 {
505 struct vertex verts[4];
506 GLfloat s0, t0, s1, t1;
507
508 if (target == GL_TEXTURE_2D) {
509 const struct gl_texture_image *texImage
510 = _mesa_select_tex_image(ctx, texObj, target, srcLevel);
511 s0 = srcX0 / (float) texImage->Width;
512 s1 = srcX1 / (float) texImage->Width;
513 t0 = srcY0 / (float) texImage->Height;
514 t1 = srcY1 / (float) texImage->Height;
515 }
516 else {
517 assert(target == GL_TEXTURE_RECTANGLE_ARB ||
518 target == GL_TEXTURE_2D_MULTISAMPLE ||
519 target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY);
520 s0 = (float) srcX0;
521 s1 = (float) srcX1;
522 t0 = (float) srcY0;
523 t1 = (float) srcY1;
524 }
525
526 /* Silence valgrind warnings about reading uninitialized stack. */
527 memset(verts, 0, sizeof(verts));
528
529 /* setup vertex positions */
530 verts[0].x = -1.0F * flipX;
531 verts[0].y = -1.0F * flipY;
532 verts[1].x = 1.0F * flipX;
533 verts[1].y = -1.0F * flipY;
534 verts[2].x = 1.0F * flipX;
535 verts[2].y = 1.0F * flipY;
536 verts[3].x = -1.0F * flipX;
537 verts[3].y = 1.0F * flipY;
538
539 verts[0].tex[0] = s0;
540 verts[0].tex[1] = t0;
541 verts[0].tex[2] = readAtt->Zoffset;
542 verts[1].tex[0] = s1;
543 verts[1].tex[1] = t0;
544 verts[1].tex[2] = readAtt->Zoffset;
545 verts[2].tex[0] = s1;
546 verts[2].tex[1] = t1;
547 verts[2].tex[2] = readAtt->Zoffset;
548 verts[3].tex[0] = s0;
549 verts[3].tex[1] = t1;
550 verts[3].tex[2] = readAtt->Zoffset;
551
552 _mesa_BufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(verts), verts);
553 }
554
555 /* setup viewport */
556 _mesa_set_viewport(ctx, 0, dstX, dstY, dstW, dstH);
557 _mesa_ColorMask(!do_depth, !do_depth, !do_depth, !do_depth);
558 _mesa_set_enable(ctx, GL_DEPTH_TEST, do_depth);
559 _mesa_DepthMask(do_depth);
560 _mesa_DepthFunc(GL_ALWAYS);
561
562 _mesa_DrawArrays(GL_TRIANGLE_FAN, 0, 4);
563 _mesa_meta_fb_tex_blit_end(ctx, target, &fb_tex_blit);
564
565 return true;
566 }
567
568 void
569 _mesa_meta_fb_tex_blit_begin(const struct gl_context *ctx,
570 struct fb_tex_blit_state *blit)
571 {
572 blit->samplerSave =
573 ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler ?
574 ctx->Texture.Unit[ctx->Texture.CurrentUnit].Sampler->Name : 0;
575 blit->tempTex = 0;
576 }
577
578 void
579 _mesa_meta_fb_tex_blit_end(const struct gl_context *ctx, GLenum target,
580 struct fb_tex_blit_state *blit)
581 {
582 /* Restore texture object state, the texture binding will
583 * be restored by _mesa_meta_end().
584 */
585 if (target != GL_TEXTURE_RECTANGLE_ARB) {
586 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, blit->baseLevelSave);
587 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, blit->maxLevelSave);
588 }
589
590 _mesa_BindSampler(ctx->Texture.CurrentUnit, blit->samplerSave);
591 _mesa_DeleteSamplers(1, &blit->sampler);
592 if (blit->tempTex)
593 _mesa_DeleteTextures(1, &blit->tempTex);
594 }
595
596 GLboolean
597 _mesa_meta_bind_rb_as_tex_image(struct gl_context *ctx,
598 struct gl_renderbuffer *rb,
599 GLuint *tex,
600 struct gl_texture_object **texObj,
601 GLenum *target)
602 {
603 struct gl_texture_image *texImage;
604
605 if (rb->NumSamples > 1)
606 *target = GL_TEXTURE_2D_MULTISAMPLE;
607 else
608 *target = GL_TEXTURE_2D;
609
610 _mesa_GenTextures(1, tex);
611 _mesa_BindTexture(*target, *tex);
612 *texObj = _mesa_lookup_texture(ctx, *tex);
613 texImage = _mesa_get_tex_image(ctx, *texObj, *target, 0);
614
615 if (!ctx->Driver.BindRenderbufferTexImage(ctx, rb, texImage)) {
616 _mesa_DeleteTextures(1, tex);
617 return false;
618 }
619
620 if (ctx->Driver.FinishRenderTexture && !rb->NeedsFinishRenderTexture) {
621 rb->NeedsFinishRenderTexture = true;
622 ctx->Driver.FinishRenderTexture(ctx, rb);
623 }
624
625 return true;
626 }
627
628 GLuint
629 _mesa_meta_setup_sampler(struct gl_context *ctx,
630 const struct gl_texture_object *texObj,
631 GLenum target, GLenum filter, GLuint srcLevel)
632 {
633 GLuint sampler;
634
635 _mesa_GenSamplers(1, &sampler);
636 _mesa_BindSampler(ctx->Texture.CurrentUnit, sampler);
637
638 /* Prepare src texture state */
639 _mesa_BindTexture(target, texObj->Name);
640 _mesa_SamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, filter);
641 _mesa_SamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, filter);
642 if (target != GL_TEXTURE_RECTANGLE_ARB) {
643 _mesa_TexParameteri(target, GL_TEXTURE_BASE_LEVEL, srcLevel);
644 _mesa_TexParameteri(target, GL_TEXTURE_MAX_LEVEL, srcLevel);
645 }
646 _mesa_SamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
647 _mesa_SamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
648
649 return sampler;
650 }
651
652 /**
653 * Meta implementation of ctx->Driver.BlitFramebuffer() in terms
654 * of texture mapping and polygon rendering.
655 */
656 GLbitfield
657 _mesa_meta_BlitFramebuffer(struct gl_context *ctx,
658 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
659 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
660 GLbitfield mask, GLenum filter)
661 {
662 const GLint dstW = abs(dstX1 - dstX0);
663 const GLint dstH = abs(dstY1 - dstY0);
664 const GLint dstFlipX = (dstX1 - dstX0) / dstW;
665 const GLint dstFlipY = (dstY1 - dstY0) / dstH;
666
667 struct {
668 GLint srcX0, srcY0, srcX1, srcY1;
669 GLint dstX0, dstY0, dstX1, dstY1;
670 } clip = {
671 srcX0, srcY0, srcX1, srcY1,
672 dstX0, dstY0, dstX1, dstY1
673 };
674
675 const GLboolean use_glsl_version = ctx->Extensions.ARB_vertex_shader &&
676 ctx->Extensions.ARB_fragment_shader;
677
678 /* Multisample texture blit support requires texture multisample. */
679 if (ctx->ReadBuffer->Visual.samples > 0 &&
680 !ctx->Extensions.ARB_texture_multisample) {
681 return mask;
682 }
683
684 /* Clip a copy of the blit coordinates. If these differ from the input
685 * coordinates, then we'll set the scissor.
686 */
687 if (!_mesa_clip_blit(ctx, &clip.srcX0, &clip.srcY0, &clip.srcX1, &clip.srcY1,
688 &clip.dstX0, &clip.dstY0, &clip.dstX1, &clip.dstY1)) {
689 /* clipped/scissored everything away */
690 return 0;
691 }
692
693 /* Only scissor affects blit, but we're doing to set a custom scissor if
694 * necessary anyway, so save/clear state.
695 */
696 _mesa_meta_begin(ctx, MESA_META_ALL & ~MESA_META_DRAW_BUFFERS);
697
698 /* If the clipping earlier changed the destination rect at all, then
699 * enable the scissor to clip to it.
700 */
701 if (clip.dstX0 != dstX0 || clip.dstY0 != dstY0 ||
702 clip.dstX1 != dstX1 || clip.dstY1 != dstY1) {
703 _mesa_set_enable(ctx, GL_SCISSOR_TEST, GL_TRUE);
704 _mesa_Scissor(MIN2(clip.dstX0, clip.dstX1),
705 MIN2(clip.dstY0, clip.dstY1),
706 abs(clip.dstX0 - clip.dstX1),
707 abs(clip.dstY0 - clip.dstY1));
708 }
709
710 /* Try faster, direct texture approach first */
711 if (mask & GL_COLOR_BUFFER_BIT) {
712 if (blitframebuffer_texture(ctx, srcX0, srcY0, srcX1, srcY1,
713 dstX0, dstY0, dstX1, dstY1,
714 filter, dstFlipX, dstFlipY,
715 use_glsl_version, false)) {
716 mask &= ~GL_COLOR_BUFFER_BIT;
717 }
718 }
719
720 if (mask & GL_DEPTH_BUFFER_BIT && use_glsl_version) {
721 if (blitframebuffer_texture(ctx, srcX0, srcY0, srcX1, srcY1,
722 dstX0, dstY0, dstX1, dstY1,
723 filter, dstFlipX, dstFlipY,
724 use_glsl_version, true)) {
725 mask &= ~GL_DEPTH_BUFFER_BIT;
726 }
727 }
728
729 if (mask & GL_STENCIL_BUFFER_BIT) {
730 /* XXX can't easily do stencil */
731 }
732
733 _mesa_meta_end(ctx);
734
735 return mask;
736 }
737
738 void
739 _mesa_meta_glsl_blit_cleanup(struct blit_state *blit)
740 {
741 if (blit->VAO) {
742 _mesa_DeleteVertexArrays(1, &blit->VAO);
743 blit->VAO = 0;
744 _mesa_DeleteBuffers(1, &blit->VBO);
745 blit->VBO = 0;
746 }
747
748 _mesa_meta_blit_shader_table_cleanup(&blit->shaders);
749
750 _mesa_DeleteTextures(1, &blit->depthTex.TexObj);
751 blit->depthTex.TexObj = 0;
752 }
753
754 void
755 _mesa_meta_and_swrast_BlitFramebuffer(struct gl_context *ctx,
756 GLint srcX0, GLint srcY0,
757 GLint srcX1, GLint srcY1,
758 GLint dstX0, GLint dstY0,
759 GLint dstX1, GLint dstY1,
760 GLbitfield mask, GLenum filter)
761 {
762 mask = _mesa_meta_BlitFramebuffer(ctx,
763 srcX0, srcY0, srcX1, srcY1,
764 dstX0, dstY0, dstX1, dstY1,
765 mask, filter);
766 if (mask == 0x0)
767 return;
768
769 _swrast_BlitFramebuffer(ctx,
770 srcX0, srcY0, srcX1, srcY1,
771 dstX0, dstY0, dstX1, dstY1,
772 mask, filter);
773 }