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 nr_storage_samples
,
47 bool supported
= screen
->is_format_supported(screen
, format
, PIPE_TEXTURE_2D
,
48 nr_samples
, nr_storage_samples
,
51 /* for sampling, some formats can be emulated.. it doesn't matter that
52 * the surface will have a format that the driver can't cope with because
53 * we'll give it sampler view formats that it can deal with and generate
54 * a shader variant that converts.
56 if ((usage
== PIPE_BIND_SAMPLER_VIEW
) && !supported
) {
57 if (format
== PIPE_FORMAT_IYUV
) {
58 supported
= screen
->is_format_supported(screen
, PIPE_FORMAT_R8_UNORM
,
59 PIPE_TEXTURE_2D
, nr_samples
,
60 nr_storage_samples
, usage
);
61 } else if (format
== PIPE_FORMAT_NV12
) {
62 supported
= screen
->is_format_supported(screen
, PIPE_FORMAT_R8_UNORM
,
63 PIPE_TEXTURE_2D
, nr_samples
,
64 nr_storage_samples
, usage
) &&
65 screen
->is_format_supported(screen
, PIPE_FORMAT_R8G8_UNORM
,
66 PIPE_TEXTURE_2D
, nr_samples
,
67 nr_storage_samples
, usage
);
75 * Return the gallium texture of an EGLImage.
78 st_get_egl_image(struct gl_context
*ctx
, GLeglImageOES image_handle
,
79 unsigned usage
, const char *error
, struct st_egl_image
*out
)
81 struct st_context
*st
= st_context(ctx
);
82 struct pipe_screen
*screen
= st
->pipe
->screen
;
83 struct st_manager
*smapi
=
84 (struct st_manager
*) st
->iface
.st_context_private
;
86 if (!smapi
|| !smapi
->get_egl_image
)
89 memset(out
, 0, sizeof(*out
));
90 if (!smapi
->get_egl_image(smapi
, (void *) image_handle
, out
)) {
91 /* image_handle does not refer to a valid EGL image object */
92 _mesa_error(ctx
, GL_INVALID_VALUE
, "%s(image handle not found)", error
);
96 if (!is_format_supported(screen
, out
->format
, out
->texture
->nr_samples
,
97 out
->texture
->nr_storage_samples
, usage
)) {
98 /* unable to specify a texture object using the specified EGL image */
99 pipe_resource_reference(&out
->texture
, NULL
);
100 _mesa_error(ctx
, GL_INVALID_OPERATION
, "%s(format not supported)", error
);
108 * Return the base format just like _mesa_base_fbo_format does.
111 st_pipe_format_to_base_format(enum pipe_format format
)
115 if (util_format_is_depth_or_stencil(format
)) {
116 if (util_format_is_depth_and_stencil(format
)) {
117 base_format
= GL_DEPTH_STENCIL
;
120 if (format
== PIPE_FORMAT_S8_UINT
)
121 base_format
= GL_STENCIL_INDEX
;
123 base_format
= GL_DEPTH_COMPONENT
;
127 /* is this enough? */
128 if (util_format_has_alpha(format
))
129 base_format
= GL_RGBA
;
131 base_format
= GL_RGB
;
138 st_egl_image_target_renderbuffer_storage(struct gl_context
*ctx
,
139 struct gl_renderbuffer
*rb
,
140 GLeglImageOES image_handle
)
142 struct st_renderbuffer
*strb
= st_renderbuffer(rb
);
143 struct st_egl_image stimg
;
145 if (st_get_egl_image(ctx
, image_handle
, PIPE_BIND_RENDER_TARGET
,
146 "glEGLImageTargetRenderbufferStorage",
148 struct pipe_context
*pipe
= st_context(ctx
)->pipe
;
149 struct pipe_surface
*ps
, surf_tmpl
;
151 u_surface_default_template(&surf_tmpl
, stimg
.texture
);
152 surf_tmpl
.format
= stimg
.format
;
153 surf_tmpl
.u
.tex
.level
= stimg
.level
;
154 surf_tmpl
.u
.tex
.first_layer
= stimg
.layer
;
155 surf_tmpl
.u
.tex
.last_layer
= stimg
.layer
;
156 ps
= pipe
->create_surface(pipe
, stimg
.texture
, &surf_tmpl
);
157 pipe_resource_reference(&stimg
.texture
, NULL
);
162 strb
->Base
.Width
= ps
->width
;
163 strb
->Base
.Height
= ps
->height
;
164 strb
->Base
.Format
= st_pipe_format_to_mesa_format(ps
->format
);
165 strb
->Base
._BaseFormat
= st_pipe_format_to_base_format(ps
->format
);
166 strb
->Base
.InternalFormat
= strb
->Base
._BaseFormat
;
168 struct pipe_surface
**psurf
=
169 util_format_is_srgb(ps
->format
) ? &strb
->surface_srgb
:
170 &strb
->surface_linear
;
172 pipe_surface_reference(psurf
, ps
);
173 strb
->surface
= *psurf
;
174 pipe_resource_reference(&strb
->texture
, ps
->texture
);
176 pipe_surface_reference(&ps
, NULL
);
181 st_bind_egl_image(struct gl_context
*ctx
,
182 struct gl_texture_object
*texObj
,
183 struct gl_texture_image
*texImage
,
184 struct st_egl_image
*stimg
)
186 struct st_context
*st
= st_context(ctx
);
187 struct st_texture_object
*stObj
;
188 struct st_texture_image
*stImage
;
189 GLenum internalFormat
;
190 mesa_format texFormat
;
192 /* map pipe format to base format */
193 if (util_format_get_component_bits(stimg
->format
,
194 UTIL_FORMAT_COLORSPACE_RGB
, 3) > 0)
195 internalFormat
= GL_RGBA
;
197 internalFormat
= GL_RGB
;
199 stObj
= st_texture_object(texObj
);
200 stImage
= st_texture_image(texImage
);
202 /* switch to surface based */
203 if (!stObj
->surface_based
) {
204 _mesa_clear_texture_object(ctx
, texObj
, NULL
);
205 stObj
->surface_based
= GL_TRUE
;
208 texFormat
= st_pipe_format_to_mesa_format(stimg
->format
);
210 /* TODO RequiredTextureImageUnits should probably be reset back
211 * to 1 somewhere if different texture is bound??
213 if (texFormat
== MESA_FORMAT_NONE
) {
214 switch (stimg
->format
) {
215 case PIPE_FORMAT_NV12
:
216 texFormat
= MESA_FORMAT_R_UNORM8
;
217 texObj
->RequiredTextureImageUnits
= 2;
219 case PIPE_FORMAT_IYUV
:
220 texFormat
= MESA_FORMAT_R_UNORM8
;
221 texObj
->RequiredTextureImageUnits
= 3;
224 unreachable("bad YUV format!");
228 _mesa_init_teximage_fields(ctx
, texImage
,
229 stimg
->texture
->width0
, stimg
->texture
->height0
,
230 1, 0, internalFormat
, texFormat
);
232 pipe_resource_reference(&stObj
->pt
, stimg
->texture
);
233 st_texture_release_all_sampler_views(st
, stObj
);
234 pipe_resource_reference(&stImage
->pt
, stObj
->pt
);
235 if (st
->pipe
->screen
->resource_changed
)
236 st
->pipe
->screen
->resource_changed(st
->pipe
->screen
, stImage
->pt
);
238 stObj
->surface_format
= stimg
->format
;
239 stObj
->level_override
= stimg
->level
;
240 stObj
->layer_override
= stimg
->layer
;
242 _mesa_dirty_texobj(ctx
, texObj
);
246 st_egl_image_target_texture_2d(struct gl_context
*ctx
, GLenum target
,
247 struct gl_texture_object
*texObj
,
248 struct gl_texture_image
*texImage
,
249 GLeglImageOES image_handle
)
251 struct st_egl_image stimg
;
253 if (!st_get_egl_image(ctx
, image_handle
, PIPE_BIND_SAMPLER_VIEW
,
254 "glEGLImageTargetTexture2D", &stimg
))
257 st_bind_egl_image(ctx
, texObj
, texImage
, &stimg
);
258 pipe_resource_reference(&stimg
.texture
, NULL
);
262 st_init_eglimage_functions(struct dd_function_table
*functions
)
264 functions
->EGLImageTargetTexture2D
= st_egl_image_target_texture_2d
;
265 functions
->EGLImageTargetRenderbufferStorage
= st_egl_image_target_renderbuffer_storage
;