2 * Mesa 3-D graphics library
4 * Copyright (C) 2010 LunarG Inc.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * 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 OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Chia-I Wu <olv@lunarg.com>
28 #include "main/errors.h"
29 #include "main/texobj.h"
30 #include "main/teximage.h"
31 #include "util/u_inlines.h"
32 #include "util/format/u_format.h"
33 #include "st_cb_eglimage.h"
34 #include "st_cb_fbo.h"
35 #include "st_context.h"
36 #include "st_texture.h"
37 #include "st_format.h"
38 #include "st_manager.h"
39 #include "st_sampler_view.h"
40 #include "util/u_surface.h"
43 is_format_supported(struct pipe_screen
*screen
, enum pipe_format format
,
44 unsigned nr_samples
, unsigned nr_storage_samples
,
45 unsigned usage
, bool *native_supported
)
47 bool supported
= screen
->is_format_supported(screen
, format
, PIPE_TEXTURE_2D
,
48 nr_samples
, nr_storage_samples
,
50 *native_supported
= supported
;
52 /* for sampling, some formats can be emulated.. it doesn't matter that
53 * the surface will have a format that the driver can't cope with because
54 * we'll give it sampler view formats that it can deal with and generate
55 * a shader variant that converts.
57 if ((usage
== PIPE_BIND_SAMPLER_VIEW
) && !supported
) {
59 case PIPE_FORMAT_IYUV
:
60 supported
= screen
->is_format_supported(screen
, PIPE_FORMAT_R8_UNORM
,
61 PIPE_TEXTURE_2D
, nr_samples
,
62 nr_storage_samples
, usage
);
64 case PIPE_FORMAT_NV12
:
65 supported
= screen
->is_format_supported(screen
, PIPE_FORMAT_R8_UNORM
,
66 PIPE_TEXTURE_2D
, nr_samples
,
67 nr_storage_samples
, usage
) &&
68 screen
->is_format_supported(screen
, PIPE_FORMAT_R8G8_UNORM
,
69 PIPE_TEXTURE_2D
, nr_samples
,
70 nr_storage_samples
, usage
);
72 case PIPE_FORMAT_P016
:
73 supported
= screen
->is_format_supported(screen
, PIPE_FORMAT_R16_UNORM
,
74 PIPE_TEXTURE_2D
, nr_samples
,
75 nr_storage_samples
, usage
) &&
76 screen
->is_format_supported(screen
, PIPE_FORMAT_R16G16_UNORM
,
77 PIPE_TEXTURE_2D
, nr_samples
,
78 nr_storage_samples
, usage
);
80 case PIPE_FORMAT_YUYV
:
81 supported
= screen
->is_format_supported(screen
, PIPE_FORMAT_RG88_UNORM
,
82 PIPE_TEXTURE_2D
, nr_samples
,
83 nr_storage_samples
, usage
) &&
84 screen
->is_format_supported(screen
, PIPE_FORMAT_BGRA8888_UNORM
,
85 PIPE_TEXTURE_2D
, nr_samples
,
86 nr_storage_samples
, usage
);
88 case PIPE_FORMAT_UYVY
:
89 supported
= screen
->is_format_supported(screen
, PIPE_FORMAT_RG88_UNORM
,
90 PIPE_TEXTURE_2D
, nr_samples
,
91 nr_storage_samples
, usage
) &&
92 screen
->is_format_supported(screen
, PIPE_FORMAT_RGBA8888_UNORM
,
93 PIPE_TEXTURE_2D
, nr_samples
,
94 nr_storage_samples
, usage
);
96 case PIPE_FORMAT_AYUV
:
97 supported
= screen
->is_format_supported(screen
, PIPE_FORMAT_RGBA8888_UNORM
,
98 PIPE_TEXTURE_2D
, nr_samples
,
99 nr_storage_samples
, usage
);
101 case PIPE_FORMAT_XYUV
:
102 supported
= screen
->is_format_supported(screen
, PIPE_FORMAT_RGBX8888_UNORM
,
103 PIPE_TEXTURE_2D
, nr_samples
,
104 nr_storage_samples
, usage
);
115 * Return the gallium texture of an EGLImage.
118 st_get_egl_image(struct gl_context
*ctx
, GLeglImageOES image_handle
,
119 unsigned usage
, const char *error
, struct st_egl_image
*out
,
120 bool *native_supported
)
122 struct st_context
*st
= st_context(ctx
);
123 struct pipe_screen
*screen
= st
->pipe
->screen
;
124 struct st_manager
*smapi
=
125 (struct st_manager
*) st
->iface
.st_context_private
;
127 if (!smapi
|| !smapi
->get_egl_image
)
130 memset(out
, 0, sizeof(*out
));
131 if (!smapi
->get_egl_image(smapi
, (void *) image_handle
, out
)) {
132 /* image_handle does not refer to a valid EGL image object */
133 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(image handle not found)", error
);
137 if (!is_format_supported(screen
, out
->format
, out
->texture
->nr_samples
,
138 out
->texture
->nr_storage_samples
, usage
,
140 /* unable to specify a texture object using the specified EGL image */
141 pipe_resource_reference(&out
->texture
, NULL
);
142 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(format not supported)", error
);
150 * Return the base format just like _mesa_base_fbo_format does.
153 st_pipe_format_to_base_format(enum pipe_format format
)
157 if (util_format_is_depth_or_stencil(format
)) {
158 if (util_format_is_depth_and_stencil(format
)) {
159 base_format
= GL_DEPTH_STENCIL
;
162 if (format
== PIPE_FORMAT_S8_UINT
)
163 base_format
= GL_STENCIL_INDEX
;
165 base_format
= GL_DEPTH_COMPONENT
;
169 /* is this enough? */
170 if (util_format_has_alpha(format
))
171 base_format
= GL_RGBA
;
173 base_format
= GL_RGB
;
180 st_egl_image_target_renderbuffer_storage(struct gl_context
*ctx
,
181 struct gl_renderbuffer
*rb
,
182 GLeglImageOES image_handle
)
184 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
185 struct st_egl_image stimg
;
186 bool native_supported
;
188 if (st_get_egl_image(ctx
, image_handle
, PIPE_BIND_RENDER_TARGET
,
189 "glEGLImageTargetRenderbufferStorage",
190 &stimg
, &native_supported
)) {
191 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
192 struct pipe_surface
*ps
, surf_tmpl
;
194 u_surface_default_template(&surf_tmpl
, stimg
.texture
);
195 surf_tmpl
.format
= stimg
.format
;
196 surf_tmpl
.u
.tex
.level
= stimg
.level
;
197 surf_tmpl
.u
.tex
.first_layer
= stimg
.layer
;
198 surf_tmpl
.u
.tex
.last_layer
= stimg
.layer
;
199 ps
= pipe
->create_surface(pipe
, stimg
.texture
, &surf_tmpl
);
200 pipe_resource_reference(&stimg
.texture
, NULL
);
205 strb
->Base
.Format
= st_pipe_format_to_mesa_format(ps
->format
);
206 strb
->Base
._BaseFormat
= st_pipe_format_to_base_format(ps
->format
);
207 strb
->Base
.InternalFormat
= strb
->Base
._BaseFormat
;
209 st_set_ws_renderbuffer_surface(strb
, ps
);
210 pipe_surface_reference(&ps
, NULL
);
215 st_bind_egl_image(struct gl_context
*ctx
,
216 struct gl_texture_object
*texObj
,
217 struct gl_texture_image
*texImage
,
218 struct st_egl_image
*stimg
,
220 bool native_supported
)
222 struct st_context
*st
= st_context(ctx
);
223 struct st_texture_object
*stObj
;
224 struct st_texture_image
*stImage
;
225 GLenum internalFormat
;
226 mesa_format texFormat
;
228 /* map pipe format to base format */
229 if (util_format_get_component_bits(stimg
->format
,
230 UTIL_FORMAT_COLORSPACE_RGB
, 3) > 0)
231 internalFormat
= GL_RGBA
;
233 internalFormat
= GL_RGB
;
235 stObj
= st_texture_object(texObj
);
236 stImage
= st_texture_image(texImage
);
238 /* switch to surface based */
239 if (!stObj
->surface_based
) {
240 _mesa_clear_texture_object(ctx
, texObj
, NULL
);
241 stObj
->surface_based
= GL_TRUE
;
244 /* TODO RequiredTextureImageUnits should probably be reset back
245 * to 1 somewhere if different texture is bound??
247 if (!native_supported
) {
248 switch (stimg
->format
) {
249 case PIPE_FORMAT_NV12
:
250 texFormat
= MESA_FORMAT_R_UNORM8
;
251 texObj
->RequiredTextureImageUnits
= 2;
253 case PIPE_FORMAT_P016
:
254 texFormat
= MESA_FORMAT_R_UNORM16
;
255 texObj
->RequiredTextureImageUnits
= 2;
257 case PIPE_FORMAT_IYUV
:
258 texFormat
= MESA_FORMAT_R_UNORM8
;
259 texObj
->RequiredTextureImageUnits
= 3;
261 case PIPE_FORMAT_YUYV
:
262 case PIPE_FORMAT_UYVY
:
263 texFormat
= MESA_FORMAT_RG_UNORM8
;
264 texObj
->RequiredTextureImageUnits
= 2;
266 case PIPE_FORMAT_AYUV
:
267 texFormat
= MESA_FORMAT_R8G8B8A8_UNORM
;
268 internalFormat
= GL_RGBA
;
269 texObj
->RequiredTextureImageUnits
= 1;
271 case PIPE_FORMAT_XYUV
:
272 texFormat
= MESA_FORMAT_R8G8B8X8_UNORM
;
273 texObj
->RequiredTextureImageUnits
= 1;
276 unreachable("unexpected emulated format");
280 texFormat
= st_pipe_format_to_mesa_format(stimg
->format
);
281 /* Use previously derived internalformat as specified by
282 * EXT_EGL_image_storage.
284 if (tex_storage
&& texObj
->Target
== GL_TEXTURE_2D
285 && stimg
->internalformat
) {
286 internalFormat
= stimg
->internalformat
;
287 if (internalFormat
== GL_NONE
) {
288 _mesa_error(ctx
, GL_INVALID_OPERATION
, __func__
);
293 assert(texFormat
!= MESA_FORMAT_NONE
);
296 /* Minify texture size based on level set on the EGLImage. */
297 uint32_t width
= u_minify(stimg
->texture
->width0
, stimg
->level
);
298 uint32_t height
= u_minify(stimg
->texture
->height0
, stimg
->level
);
300 _mesa_init_teximage_fields(ctx
, texImage
, width
, height
,
301 1, 0, internalFormat
, texFormat
);
303 pipe_resource_reference(&stObj
->pt
, stimg
->texture
);
304 st_texture_release_all_sampler_views(st
, stObj
);
305 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
306 if (st
->pipe
->screen
->resource_changed
)
307 st
->pipe
->screen
->resource_changed(st
->pipe
->screen
, stImage
->pt
);
309 stObj
->surface_format
= stimg
->format
;
310 stObj
->level_override
= stimg
->level
;
311 stObj
->layer_override
= stimg
->layer
;
313 _mesa_dirty_texobj(ctx
, texObj
);
317 st_egl_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
318 struct gl_texture_object
*texObj
,
319 struct gl_texture_image
*texImage
,
320 GLeglImageOES image_handle
)
322 struct st_egl_image stimg
;
323 bool native_supported
;
325 if (!st_get_egl_image(ctx
, image_handle
, PIPE_BIND_SAMPLER_VIEW
,
326 "glEGLImageTargetTexture2D", &stimg
,
330 st_bind_egl_image(ctx
, texObj
, texImage
, &stimg
, false, native_supported
);
331 pipe_resource_reference(&stimg
.texture
, NULL
);
335 st_egl_image_target_tex_storage(struct gl_context
*ctx
, GLenum target
,
336 struct gl_texture_object
*texObj
,
337 struct gl_texture_image
*texImage
,
338 GLeglImageOES image_handle
)
340 struct st_egl_image stimg
;
341 bool native_supported
;
343 if (!st_get_egl_image(ctx
, image_handle
, PIPE_BIND_SAMPLER_VIEW
,
344 "glEGLImageTargetTexture2D", &stimg
,
348 st_bind_egl_image(ctx
, texObj
, texImage
, &stimg
, true, native_supported
);
349 pipe_resource_reference(&stimg
.texture
, NULL
);
353 st_init_eglimage_functions(struct dd_function_table
*functions
)
355 functions
->EGLImageTargetTexture2D
= st_egl_image_target_texture_2d
;
356 functions
->EGLImageTargetTexStorage
= st_egl_image_target_tex_storage
;
357 functions
->EGLImageTargetRenderbufferStorage
= st_egl_image_target_renderbuffer_storage
;