intel: Enable blit glCopyTexSubImage/glBlitFramebuffer with sRGB.
[mesa.git] / src / mesa / drivers / dri / intel / intel_tex_copy.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * 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
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "main/mtypes.h"
29 #include "main/enums.h"
30 #include "main/image.h"
31 #include "main/teximage.h"
32 #include "main/texstate.h"
33 #include "main/fbobject.h"
34
35 #include "drivers/common/meta.h"
36
37 #include "intel_screen.h"
38 #include "intel_context.h"
39 #include "intel_mipmap_tree.h"
40 #include "intel_regions.h"
41 #include "intel_fbo.h"
42 #include "intel_tex.h"
43 #include "intel_blit.h"
44 #ifndef I915
45 #include "brw_context.h"
46 #endif
47
48 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
49
50
51 bool
52 intel_copy_texsubimage(struct intel_context *intel,
53 struct intel_texture_image *intelImage,
54 GLint dstx, GLint dsty,
55 struct intel_renderbuffer *irb,
56 GLint x, GLint y, GLsizei width, GLsizei height)
57 {
58 struct gl_context *ctx = &intel->ctx;
59 const GLenum internalFormat = intelImage->base.Base.InternalFormat;
60 bool copy_supported = false;
61 bool copy_supported_with_alpha_override = false;
62
63 intel_prepare_render(intel);
64
65 /* glCopyTexSubImage() can't be called on multisampled renderbuffers or
66 * textures.
67 */
68 assert(!irb->Base.Base.NumSamples);
69 assert(!intelImage->base.Base.NumSamples);
70
71 if (!intelImage->mt || !irb || !irb->mt) {
72 if (unlikely(INTEL_DEBUG & DEBUG_PERF))
73 fprintf(stderr, "%s fail %p %p (0x%08x)\n",
74 __FUNCTION__, intelImage->mt, irb, internalFormat);
75 return false;
76 }
77
78 if (intelImage->base.Base.TexObject->Target == GL_TEXTURE_1D_ARRAY ||
79 intelImage->base.Base.TexObject->Target == GL_TEXTURE_2D_ARRAY) {
80 perf_debug("no support for array textures\n");
81 }
82
83 /* glCopyTexImage (and the glBlitFramebuffer() path that reuses this)
84 * doesn't do any sRGB conversions.
85 */
86 gl_format src_format = _mesa_get_srgb_format_linear(intel_rb_format(irb));
87 gl_format dst_format = _mesa_get_srgb_format_linear(intelImage->base.Base.TexFormat);
88
89 copy_supported = src_format == dst_format;
90
91 /* Converting ARGB8888 to XRGB8888 is trivial: ignore the alpha bits */
92 if (src_format == MESA_FORMAT_ARGB8888 &&
93 dst_format == MESA_FORMAT_XRGB8888) {
94 copy_supported = true;
95 }
96
97 /* Converting XRGB8888 to ARGB8888 requires setting the alpha bits to 1.0 */
98 if (src_format == MESA_FORMAT_XRGB8888 &&
99 dst_format == MESA_FORMAT_ARGB8888) {
100 copy_supported_with_alpha_override = true;
101 }
102
103 if (!copy_supported && !copy_supported_with_alpha_override) {
104 perf_debug("%s mismatched formats %s, %s\n",
105 __FUNCTION__,
106 _mesa_get_format_name(intelImage->base.Base.TexFormat),
107 _mesa_get_format_name(intel_rb_format(irb)));
108 return false;
109 }
110
111 /* blit from src buffer to texture */
112 if (!intel_miptree_blit(intel,
113 irb->mt, irb->mt_level, irb->mt_layer,
114 x, y, irb->Base.Base.Name == 0,
115 intelImage->mt, intelImage->base.Base.Level,
116 intelImage->base.Base.Face,
117 dstx, dsty, false,
118 width, height, GL_COPY)) {
119 return false;
120 }
121
122 if (copy_supported_with_alpha_override)
123 intel_set_teximage_alpha_to_one(ctx, intelImage);
124
125 return true;
126 }
127
128
129 static void
130 intelCopyTexSubImage(struct gl_context *ctx, GLuint dims,
131 struct gl_texture_image *texImage,
132 GLint xoffset, GLint yoffset, GLint zoffset,
133 struct gl_renderbuffer *rb,
134 GLint x, GLint y,
135 GLsizei width, GLsizei height)
136 {
137 struct intel_context *intel = intel_context(ctx);
138 if (dims != 3) {
139 #ifndef I915
140 /* Try BLORP first. It can handle almost everything. */
141 if (brw_blorp_copytexsubimage(intel, rb, texImage, x, y,
142 xoffset, yoffset, width, height))
143 return;
144 #endif
145
146 /* Next, try the BLT engine. */
147 if (intel_copy_texsubimage(intel,
148 intel_texture_image(texImage),
149 xoffset, yoffset,
150 intel_renderbuffer(rb), x, y, width, height))
151 return;
152 }
153
154 /* Finally, fall back to meta. This will likely be slow. */
155 perf_debug("%s - fallback to swrast\n", __FUNCTION__);
156 _mesa_meta_CopyTexSubImage(ctx, dims, texImage,
157 xoffset, yoffset, zoffset,
158 rb, x, y, width, height);
159 }
160
161
162 void
163 intelInitTextureCopyImageFuncs(struct dd_function_table *functions)
164 {
165 functions->CopyTexSubImage = intelCopyTexSubImage;
166 }