1 /**************************************************************************
3 * Copyright 2013 Advanced Micro Devices, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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 THE COPYRIGHT HOLDER(S) OR AUTHOR(S) 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.
26 **************************************************************************/
30 * Christian König <christian.koenig@amd.com>
34 #include "main/texobj.h"
35 #include "main/teximage.h"
36 #include "main/errors.h"
37 #include "program/prog_instruction.h"
39 #include "pipe/p_state.h"
40 #include "pipe/p_video_codec.h"
42 #include "state_tracker/vdpau_interop.h"
44 #include "util/u_inlines.h"
47 #include "st_context.h"
48 #include "st_texture.h"
49 #include "st_format.h"
50 #include "st_cb_flush.h"
55 st_vdpau_map_surface(struct gl_context
*ctx
, GLenum target
, GLenum access
,
56 GLboolean output
, struct gl_texture_object
*texObj
,
57 struct gl_texture_image
*texImage
,
58 const GLvoid
*vdpSurface
, GLuint index
)
60 int (*getProcAddr
)(uint32_t device
, uint32_t id
, void **ptr
);
61 uint32_t device
= (uintptr_t)ctx
->vdpDevice
;
63 struct st_context
*st
= st_context(ctx
);
64 struct st_texture_object
*stObj
= st_texture_object(texObj
);
65 struct st_texture_image
*stImage
= st_texture_image(texImage
);
67 struct pipe_resource
*res
;
68 struct pipe_sampler_view templ
, **sampler_view
;
69 mesa_format texFormat
;
71 getProcAddr
= (void *)ctx
->vdpGetProcAddress
;
73 VdpOutputSurfaceGallium
*f
;
75 if (getProcAddr(device
, VDP_FUNC_ID_OUTPUT_SURFACE_GALLIUM
, (void**)&f
)) {
76 _mesa_error(ctx
, GL_INVALID_OPERATION
, "VDPAUMapSurfacesNV");
80 res
= f((uintptr_t)vdpSurface
);
83 _mesa_error(ctx
, GL_INVALID_OPERATION
, "VDPAUMapSurfacesNV");
88 struct pipe_sampler_view
*sv
;
89 VdpVideoSurfaceGallium
*f
;
91 struct pipe_video_buffer
*buffer
;
92 struct pipe_sampler_view
**samplers
;
94 if (getProcAddr(device
, VDP_FUNC_ID_VIDEO_SURFACE_GALLIUM
, (void**)&f
)) {
95 _mesa_error(ctx
, GL_INVALID_OPERATION
, "VDPAUMapSurfacesNV");
99 buffer
= f((uintptr_t)vdpSurface
);
101 _mesa_error(ctx
, GL_INVALID_OPERATION
, "VDPAUMapSurfacesNV");
105 samplers
= buffer
->get_sampler_view_planes(buffer
);
107 _mesa_error(ctx
, GL_INVALID_OPERATION
, "VDPAUMapSurfacesNV");
111 sv
= samplers
[index
>> 1];
113 _mesa_error(ctx
, GL_INVALID_OPERATION
, "VDPAUMapSurfacesNV");
121 _mesa_error(ctx
, GL_INVALID_OPERATION
, "VDPAUMapSurfacesNV");
125 /* do we have different screen objects ? */
126 if (res
->screen
!= st
->pipe
->screen
) {
127 _mesa_error(ctx
, GL_INVALID_OPERATION
, "VDPAUMapSurfacesNV");
131 /* switch to surface based */
132 if (!stObj
->surface_based
) {
133 _mesa_clear_texture_object(ctx
, texObj
);
134 stObj
->surface_based
= GL_TRUE
;
137 texFormat
= st_pipe_format_to_mesa_format(res
->format
);
139 _mesa_init_teximage_fields(ctx
, texImage
,
140 res
->width0
, res
->height0
, 1, 0, GL_RGBA
,
143 pipe_resource_reference(&stObj
->pt
, res
);
144 st_texture_release_all_sampler_views(st
, stObj
);
145 pipe_resource_reference(&stImage
->pt
, res
);
147 u_sampler_view_default_template(&templ
, res
, res
->format
);
148 templ
.u
.tex
.first_layer
= index
& 1;
149 templ
.u
.tex
.last_layer
= index
& 1;
150 templ
.swizzle_r
= GET_SWZ(stObj
->base
._Swizzle
, 0);
151 templ
.swizzle_g
= GET_SWZ(stObj
->base
._Swizzle
, 1);
152 templ
.swizzle_b
= GET_SWZ(stObj
->base
._Swizzle
, 2);
153 templ
.swizzle_a
= GET_SWZ(stObj
->base
._Swizzle
, 3);
155 sampler_view
= st_texture_get_sampler_view(st
, stObj
);
156 *sampler_view
= st
->pipe
->create_sampler_view(st
->pipe
, res
, &templ
);
158 stObj
->width0
= res
->width0
;
159 stObj
->height0
= res
->height0
;
161 stObj
->surface_format
= res
->format
;
163 _mesa_dirty_texobj(ctx
, texObj
);
167 st_vdpau_unmap_surface(struct gl_context
*ctx
, GLenum target
, GLenum access
,
168 GLboolean output
, struct gl_texture_object
*texObj
,
169 struct gl_texture_image
*texImage
,
170 const GLvoid
*vdpSurface
, GLuint index
)
172 struct st_context
*st
= st_context(ctx
);
173 struct st_texture_object
*stObj
= st_texture_object(texObj
);
174 struct st_texture_image
*stImage
= st_texture_image(texImage
);
176 pipe_resource_reference(&stObj
->pt
, NULL
);
177 st_texture_release_all_sampler_views(st
, stObj
);
178 pipe_resource_reference(&stImage
->pt
, NULL
);
180 _mesa_dirty_texobj(ctx
, texObj
);
182 st_flush(st
, NULL
, 0);
188 st_init_vdpau_functions(struct dd_function_table
*functions
)
191 functions
->VDPAUMapSurface
= st_vdpau_map_surface
;
192 functions
->VDPAUUnmapSurface
= st_vdpau_unmap_surface
;