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/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 usage
)
46 bool supported
= screen
->is_format_supported(screen
, format
, PIPE_TEXTURE_2D
,
49 /* for sampling, some formats can be emulated.. it doesn't matter that
50 * the surface will have a format that the driver can't cope with because
51 * we'll give it sampler view formats that it can deal with and generate
52 * a shader variant that converts.
54 if ((usage
== PIPE_BIND_SAMPLER_VIEW
) && !supported
) {
55 if (format
== PIPE_FORMAT_IYUV
) {
56 supported
= screen
->is_format_supported(screen
, PIPE_FORMAT_R8_UNORM
,
57 PIPE_TEXTURE_2D
, nr_samples
,
59 } else if (format
== PIPE_FORMAT_NV12
) {
60 supported
= screen
->is_format_supported(screen
, PIPE_FORMAT_R8_UNORM
,
61 PIPE_TEXTURE_2D
, nr_samples
,
63 screen
->is_format_supported(screen
, PIPE_FORMAT_R8G8_UNORM
,
64 PIPE_TEXTURE_2D
, nr_samples
,
73 * Return the gallium texture of an EGLImage.
76 st_get_egl_image(struct gl_context
*ctx
, GLeglImageOES image_handle
,
77 unsigned usage
, const char *error
, struct st_egl_image
*out
)
79 struct st_context
*st
= st_context(ctx
);
80 struct pipe_screen
*screen
= st
->pipe
->screen
;
81 struct st_manager
*smapi
=
82 (struct st_manager
*) st
->iface
.st_context_private
;
84 if (!smapi
|| !smapi
->get_egl_image
)
87 memset(out
, 0, sizeof(*out
));
88 if (!smapi
->get_egl_image(smapi
, (void *) image_handle
, out
)) {
89 /* image_handle does not refer to a valid EGL image object */
90 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(image handle not found)", error
);
94 if (!is_format_supported(screen
, out
->format
, out
->texture
->nr_samples
, usage
)) {
95 /* unable to specify a texture object using the specified EGL image */
96 pipe_resource_reference(&out
->texture
, NULL
);
97 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(format not supported)", error
);
105 * Return the base format just like _mesa_base_fbo_format does.
108 st_pipe_format_to_base_format(enum pipe_format format
)
112 if (util_format_is_depth_or_stencil(format
)) {
113 if (util_format_is_depth_and_stencil(format
)) {
114 base_format
= GL_DEPTH_STENCIL
;
117 if (format
== PIPE_FORMAT_S8_UINT
)
118 base_format
= GL_STENCIL_INDEX
;
120 base_format
= GL_DEPTH_COMPONENT
;
124 /* is this enough? */
125 if (util_format_has_alpha(format
))
126 base_format
= GL_RGBA
;
128 base_format
= GL_RGB
;
135 st_egl_image_target_renderbuffer_storage(struct gl_context
*ctx
,
136 struct gl_renderbuffer
*rb
,
137 GLeglImageOES image_handle
)
139 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
140 struct st_egl_image stimg
;
142 if (st_get_egl_image(ctx
, image_handle
, PIPE_BIND_RENDER_TARGET
,
143 "glEGLImageTargetRenderbufferStorage",
145 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
146 struct pipe_surface
*ps
, surf_tmpl
;
148 u_surface_default_template(&surf_tmpl
, stimg
.texture
);
149 surf_tmpl
.format
= stimg
.format
;
150 surf_tmpl
.u
.tex
.level
= stimg
.level
;
151 surf_tmpl
.u
.tex
.first_layer
= stimg
.layer
;
152 surf_tmpl
.u
.tex
.last_layer
= stimg
.layer
;
153 ps
= pipe
->create_surface(pipe
, stimg
.texture
, &surf_tmpl
);
154 pipe_resource_reference(&stimg
.texture
, NULL
);
159 strb
->Base
.Width
= ps
->width
;
160 strb
->Base
.Height
= ps
->height
;
161 strb
->Base
.Format
= st_pipe_format_to_mesa_format(ps
->format
);
162 strb
->Base
._BaseFormat
= st_pipe_format_to_base_format(ps
->format
);
163 strb
->Base
.InternalFormat
= strb
->Base
._BaseFormat
;
165 struct pipe_surface
**psurf
=
166 util_format_is_srgb(ps
->format
) ? &strb
->surface_srgb
:
167 &strb
->surface_linear
;
169 pipe_surface_reference(psurf
, ps
);
170 strb
->surface
= *psurf
;
171 pipe_resource_reference(&strb
->texture
, ps
->texture
);
173 pipe_surface_reference(&ps
, NULL
);
178 st_bind_egl_image(struct gl_context
*ctx
,
179 struct gl_texture_object
*texObj
,
180 struct gl_texture_image
*texImage
,
181 struct st_egl_image
*stimg
)
183 struct st_context
*st
= st_context(ctx
);
184 struct st_texture_object
*stObj
;
185 struct st_texture_image
*stImage
;
186 GLenum internalFormat
;
187 mesa_format texFormat
;
189 /* map pipe format to base format */
190 if (util_format_get_component_bits(stimg
->format
,
191 UTIL_FORMAT_COLORSPACE_RGB
, 3) > 0)
192 internalFormat
= GL_RGBA
;
194 internalFormat
= GL_RGB
;
196 stObj
= st_texture_object(texObj
);
197 stImage
= st_texture_image(texImage
);
199 /* switch to surface based */
200 if (!stObj
->surface_based
) {
201 _mesa_clear_texture_object(ctx
, texObj
, NULL
);
202 stObj
->surface_based
= GL_TRUE
;
205 texFormat
= st_pipe_format_to_mesa_format(stimg
->format
);
207 /* TODO RequiredTextureImageUnits should probably be reset back
208 * to 1 somewhere if different texture is bound??
210 if (texFormat
== MESA_FORMAT_NONE
) {
211 switch (stimg
->format
) {
212 case PIPE_FORMAT_NV12
:
213 texFormat
= MESA_FORMAT_R_UNORM8
;
214 texObj
->RequiredTextureImageUnits
= 2;
216 case PIPE_FORMAT_IYUV
:
217 texFormat
= MESA_FORMAT_R_UNORM8
;
218 texObj
->RequiredTextureImageUnits
= 3;
221 unreachable("bad YUV format!");
225 _mesa_init_teximage_fields(ctx
, texImage
,
226 stimg
->texture
->width0
, stimg
->texture
->height0
,
227 1, 0, internalFormat
, texFormat
);
229 pipe_resource_reference(&stObj
->pt
, stimg
->texture
);
230 st_texture_release_all_sampler_views(st
, stObj
);
231 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
233 stObj
->surface_format
= stimg
->format
;
234 stObj
->level_override
= stimg
->level
;
235 stObj
->layer_override
= stimg
->layer
;
237 _mesa_dirty_texobj(ctx
, texObj
);
241 st_egl_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
242 struct gl_texture_object
*texObj
,
243 struct gl_texture_image
*texImage
,
244 GLeglImageOES image_handle
)
246 struct st_egl_image stimg
;
248 if (!st_get_egl_image(ctx
, image_handle
, PIPE_BIND_SAMPLER_VIEW
,
249 "glEGLImageTargetTexture2D", &stimg
))
252 st_bind_egl_image(ctx
, texObj
, texImage
, &stimg
);
253 pipe_resource_reference(&stimg
.texture
, NULL
);
257 st_init_eglimage_functions(struct dd_function_table
*functions
)
259 functions
->EGLImageTargetTexture2D
= st_egl_image_target_texture_2d
;
260 functions
->EGLImageTargetRenderbufferStorage
= st_egl_image_target_renderbuffer_storage
;