2016-02-14 11:46:17 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-05-09 17:42:03 +00:00
|
|
|
#include <d3d9.h>
|
2016-02-14 11:46:17 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "common/common.h"
|
|
|
|
#include "osdep/windows_utils.h"
|
|
|
|
#include "hwdec.h"
|
|
|
|
#include "video/hwdec.h"
|
|
|
|
|
|
|
|
// for WGL_ACCESS_READ_ONLY_NV
|
|
|
|
#include <GL/wglext.h>
|
|
|
|
|
|
|
|
#define SHARED_SURFACE_D3DFMT D3DFMT_X8R8G8B8
|
|
|
|
#define SHARED_SURFACE_MPFMT IMGFMT_RGB0
|
|
|
|
struct priv {
|
2016-05-09 17:42:03 +00:00
|
|
|
struct mp_hwdec_ctx hwctx;
|
2016-02-14 11:46:17 +00:00
|
|
|
IDirect3DDevice9Ex *device;
|
|
|
|
HANDLE device_h;
|
|
|
|
|
2016-02-18 09:00:53 +00:00
|
|
|
IDirect3DSurface9 *rtarget;
|
|
|
|
HANDLE rtarget_h;
|
2016-02-14 11:46:17 +00:00
|
|
|
GLuint texture;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void destroy_objects(struct gl_hwdec *hw)
|
|
|
|
{
|
|
|
|
struct priv *p = hw->priv;
|
|
|
|
GL *gl = hw->gl;
|
|
|
|
|
2016-02-18 09:00:53 +00:00
|
|
|
if (p->rtarget_h && p->device_h) {
|
|
|
|
if (!gl->DXUnlockObjectsNV(p->device_h, 1, &p->rtarget_h)) {
|
2016-02-14 11:46:17 +00:00
|
|
|
MP_ERR(hw, "Failed unlocking texture for access by OpenGL: %s\n",
|
|
|
|
mp_LastError_to_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-18 09:00:53 +00:00
|
|
|
if (p->rtarget_h) {
|
|
|
|
if (!gl->DXUnregisterObjectNV(p->device_h, p->rtarget_h)) {
|
2016-02-14 11:46:17 +00:00
|
|
|
MP_ERR(hw, "Failed to unregister Direct3D surface with OpenGL: %s\n",
|
|
|
|
mp_LastError_to_str());
|
|
|
|
} else {
|
2016-02-18 09:00:53 +00:00
|
|
|
p->rtarget_h = 0;
|
2016-02-14 11:46:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gl->DeleteTextures(1, &p->texture);
|
|
|
|
p->texture = 0;
|
|
|
|
|
2016-02-18 09:00:53 +00:00
|
|
|
if (p->rtarget) {
|
|
|
|
IDirect3DSurface9_Release(p->rtarget);
|
|
|
|
p->rtarget = NULL;
|
2016-02-14 11:46:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy(struct gl_hwdec *hw)
|
|
|
|
{
|
|
|
|
struct priv *p = hw->priv;
|
|
|
|
destroy_objects(hw);
|
|
|
|
|
2016-05-09 17:42:03 +00:00
|
|
|
hwdec_devices_remove(hw->devs, &p->hwctx);
|
|
|
|
|
2016-02-14 11:46:17 +00:00
|
|
|
if (p->device)
|
|
|
|
IDirect3DDevice9Ex_Release(p->device);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int create(struct gl_hwdec *hw)
|
|
|
|
{
|
|
|
|
GL *gl = hw->gl;
|
2016-05-09 17:42:03 +00:00
|
|
|
if (!gl->MPGetNativeDisplay || !(gl->mpgl_caps & MPGL_CAP_DXINTEROP))
|
2016-02-14 11:46:17 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
struct priv *p = talloc_zero(hw, struct priv);
|
|
|
|
hw->priv = p;
|
|
|
|
|
2016-03-14 00:35:42 +00:00
|
|
|
// AMD drivers won't open multiple dxinterop HANDLES on the same D3D device,
|
|
|
|
// so we request the one already in use by context_dxinterop
|
|
|
|
p->device_h = gl->MPGetNativeDisplay("dxinterop_device_HANDLE");
|
|
|
|
if (!p->device_h)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// But we also still need the actual D3D device
|
2016-02-14 11:46:17 +00:00
|
|
|
p->device = gl->MPGetNativeDisplay("IDirect3DDevice9Ex");
|
|
|
|
if (!p->device)
|
|
|
|
return -1;
|
|
|
|
IDirect3DDevice9Ex_AddRef(p->device);
|
|
|
|
|
2016-05-09 17:42:03 +00:00
|
|
|
p->hwctx = (struct mp_hwdec_ctx){
|
|
|
|
.type = HWDEC_DXVA2,
|
|
|
|
.driver_name = hw->driver->name,
|
|
|
|
.ctx = (IDirect3DDevice9 *)p->device,
|
|
|
|
};
|
|
|
|
hwdec_devices_add(hw->devs, &p->hwctx);
|
2016-02-14 11:46:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int reinit(struct gl_hwdec *hw, struct mp_image_params *params)
|
|
|
|
{
|
|
|
|
struct priv *p = hw->priv;
|
|
|
|
GL *gl = hw->gl;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
destroy_objects(hw);
|
|
|
|
|
2016-02-18 09:00:53 +00:00
|
|
|
HANDLE share_handle = NULL;
|
|
|
|
hr = IDirect3DDevice9Ex_CreateRenderTarget(
|
2016-02-14 11:46:17 +00:00
|
|
|
p->device,
|
|
|
|
params->w, params->h,
|
2016-02-18 09:00:53 +00:00
|
|
|
SHARED_SURFACE_D3DFMT, D3DMULTISAMPLE_NONE, 0, FALSE,
|
|
|
|
&p->rtarget, &share_handle);
|
2016-02-14 11:46:17 +00:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
MP_ERR(hw, "Failed creating offscreen Direct3D surface: %s\n",
|
|
|
|
mp_HRESULT_to_str(hr));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2016-02-18 09:00:53 +00:00
|
|
|
if (share_handle &&
|
|
|
|
!gl->DXSetResourceShareHandleNV(p->rtarget, share_handle)) {
|
2016-02-14 11:46:17 +00:00
|
|
|
MP_ERR(hw, "Failed setting Direct3D/OpenGL share handle for surface: %s\n",
|
|
|
|
mp_LastError_to_str());
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
gl->GenTextures(1, &p->texture);
|
|
|
|
gl->BindTexture(GL_TEXTURE_2D, p->texture);
|
|
|
|
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
gl->BindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
2016-02-18 09:00:53 +00:00
|
|
|
p->rtarget_h = gl->DXRegisterObjectNV(p->device_h, p->rtarget, p->texture,
|
2016-02-14 11:46:17 +00:00
|
|
|
GL_TEXTURE_2D,
|
|
|
|
WGL_ACCESS_READ_ONLY_NV);
|
2016-02-18 09:00:53 +00:00
|
|
|
if (!p->rtarget_h) {
|
2016-02-14 11:46:17 +00:00
|
|
|
MP_ERR(hw, "Failed to register Direct3D surface with OpenGL: %s\n",
|
|
|
|
mp_LastError_to_str());
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2016-02-18 09:00:53 +00:00
|
|
|
if (!gl->DXLockObjectsNV(p->device_h, 1, &p->rtarget_h)) {
|
2016-02-14 11:46:17 +00:00
|
|
|
MP_ERR(hw, "Failed locking texture for access by OpenGL %s\n",
|
|
|
|
mp_LastError_to_str());
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
vo_opengl: refactor how hwdec interop exports textures
Rename gl_hwdec_driver.map_image to map_frame, and let it fill out a
struct gl_hwdec_frame describing the exact texture layout. This gives
more flexibility to what the hwdec interop can export. In particular, it
can export strange component orders/permutations and textures with
padded size. (The latter originating from cropped video.)
The way gl_hwdec_frame works is in the spirit of the rest of the
vo_opengl video processing code, which tends to put as much information
in immediate state (as part of the dataflow), instead of declaring it
globally. To some degree this duplicates the texplane and img_tex
structs, but until we somehow unify those, it's better to give the hwdec
state its own struct. The fact that changing the hwdec struct would
require changes and testing on at least 4 platform/GPU combinations
makes duplicating it almost a requirement to avoid pain later.
Make gl_hwdec_driver.reinit set the new image format and remove the
gl_hwdec.converted_imgfmt field.
Likewise, gl_hwdec.gl_texture_target is replaced with
gl_hwdec_plane.gl_target.
Split out a init_image_desc function from init_format. The latter is not
called in the hwdec case at all anymore. Setting up most of struct
texplane is also completely separate in the hwdec and normal cases.
video.c does not check whether the hwdec "mapped" image format is
supported. This should not really happen anyway, and if it does, the
hwdec interop backend must fail at creation time, so this is not an
issue.
2016-05-10 16:29:10 +00:00
|
|
|
params->imgfmt = SHARED_SURFACE_MPFMT;
|
2016-07-15 10:01:30 +00:00
|
|
|
params->hw_subfmt = 0;
|
vo_opengl: refactor how hwdec interop exports textures
Rename gl_hwdec_driver.map_image to map_frame, and let it fill out a
struct gl_hwdec_frame describing the exact texture layout. This gives
more flexibility to what the hwdec interop can export. In particular, it
can export strange component orders/permutations and textures with
padded size. (The latter originating from cropped video.)
The way gl_hwdec_frame works is in the spirit of the rest of the
vo_opengl video processing code, which tends to put as much information
in immediate state (as part of the dataflow), instead of declaring it
globally. To some degree this duplicates the texplane and img_tex
structs, but until we somehow unify those, it's better to give the hwdec
state its own struct. The fact that changing the hwdec struct would
require changes and testing on at least 4 platform/GPU combinations
makes duplicating it almost a requirement to avoid pain later.
Make gl_hwdec_driver.reinit set the new image format and remove the
gl_hwdec.converted_imgfmt field.
Likewise, gl_hwdec.gl_texture_target is replaced with
gl_hwdec_plane.gl_target.
Split out a init_image_desc function from init_format. The latter is not
called in the hwdec case at all anymore. Setting up most of struct
texplane is also completely separate in the hwdec and normal cases.
video.c does not check whether the hwdec "mapped" image format is
supported. This should not really happen anyway, and if it does, the
hwdec interop backend must fail at creation time, so this is not an
issue.
2016-05-10 16:29:10 +00:00
|
|
|
|
2016-02-14 11:46:17 +00:00
|
|
|
return 0;
|
|
|
|
fail:
|
|
|
|
destroy_objects(hw);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
vo_opengl: refactor how hwdec interop exports textures
Rename gl_hwdec_driver.map_image to map_frame, and let it fill out a
struct gl_hwdec_frame describing the exact texture layout. This gives
more flexibility to what the hwdec interop can export. In particular, it
can export strange component orders/permutations and textures with
padded size. (The latter originating from cropped video.)
The way gl_hwdec_frame works is in the spirit of the rest of the
vo_opengl video processing code, which tends to put as much information
in immediate state (as part of the dataflow), instead of declaring it
globally. To some degree this duplicates the texplane and img_tex
structs, but until we somehow unify those, it's better to give the hwdec
state its own struct. The fact that changing the hwdec struct would
require changes and testing on at least 4 platform/GPU combinations
makes duplicating it almost a requirement to avoid pain later.
Make gl_hwdec_driver.reinit set the new image format and remove the
gl_hwdec.converted_imgfmt field.
Likewise, gl_hwdec.gl_texture_target is replaced with
gl_hwdec_plane.gl_target.
Split out a init_image_desc function from init_format. The latter is not
called in the hwdec case at all anymore. Setting up most of struct
texplane is also completely separate in the hwdec and normal cases.
video.c does not check whether the hwdec "mapped" image format is
supported. This should not really happen anyway, and if it does, the
hwdec interop backend must fail at creation time, so this is not an
issue.
2016-05-10 16:29:10 +00:00
|
|
|
static int map_frame(struct gl_hwdec *hw, struct mp_image *hw_image,
|
|
|
|
struct gl_hwdec_frame *out_frame)
|
2016-02-14 11:46:17 +00:00
|
|
|
{
|
|
|
|
assert(hw_image && hw_image->imgfmt == hw->driver->imgfmt);
|
|
|
|
GL *gl = hw->gl;
|
|
|
|
struct priv *p = hw->priv;
|
|
|
|
HRESULT hr;
|
|
|
|
|
2016-02-18 09:00:53 +00:00
|
|
|
if (!gl->DXUnlockObjectsNV(p->device_h, 1, &p->rtarget_h)) {
|
2016-02-14 11:46:17 +00:00
|
|
|
MP_ERR(hw, "Failed unlocking texture for access by OpenGL: %s\n",
|
|
|
|
mp_LastError_to_str());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-17 08:59:11 +00:00
|
|
|
IDirect3DSurface9* hw_surface = (IDirect3DSurface9 *)hw_image->planes[3];
|
2016-02-14 11:46:17 +00:00
|
|
|
RECT rc = {0, 0, hw_image->w, hw_image->h};
|
|
|
|
hr = IDirect3DDevice9Ex_StretchRect(p->device,
|
|
|
|
hw_surface, &rc,
|
2016-02-18 09:00:53 +00:00
|
|
|
p->rtarget, &rc,
|
2016-02-14 11:46:17 +00:00
|
|
|
D3DTEXF_NONE);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
MP_ERR(hw, "Direct3D RGB conversion failed: %s", mp_HRESULT_to_str(hr));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-02-18 09:00:53 +00:00
|
|
|
if (!gl->DXLockObjectsNV(p->device_h, 1, &p->rtarget_h)) {
|
2016-02-14 11:46:17 +00:00
|
|
|
MP_ERR(hw, "Failed locking texture for access by OpenGL: %s\n",
|
|
|
|
mp_LastError_to_str());
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
vo_opengl: refactor how hwdec interop exports textures
Rename gl_hwdec_driver.map_image to map_frame, and let it fill out a
struct gl_hwdec_frame describing the exact texture layout. This gives
more flexibility to what the hwdec interop can export. In particular, it
can export strange component orders/permutations and textures with
padded size. (The latter originating from cropped video.)
The way gl_hwdec_frame works is in the spirit of the rest of the
vo_opengl video processing code, which tends to put as much information
in immediate state (as part of the dataflow), instead of declaring it
globally. To some degree this duplicates the texplane and img_tex
structs, but until we somehow unify those, it's better to give the hwdec
state its own struct. The fact that changing the hwdec struct would
require changes and testing on at least 4 platform/GPU combinations
makes duplicating it almost a requirement to avoid pain later.
Make gl_hwdec_driver.reinit set the new image format and remove the
gl_hwdec.converted_imgfmt field.
Likewise, gl_hwdec.gl_texture_target is replaced with
gl_hwdec_plane.gl_target.
Split out a init_image_desc function from init_format. The latter is not
called in the hwdec case at all anymore. Setting up most of struct
texplane is also completely separate in the hwdec and normal cases.
video.c does not check whether the hwdec "mapped" image format is
supported. This should not really happen anyway, and if it does, the
hwdec interop backend must fail at creation time, so this is not an
issue.
2016-05-10 16:29:10 +00:00
|
|
|
*out_frame = (struct gl_hwdec_frame){
|
|
|
|
.planes = {
|
|
|
|
{
|
|
|
|
.gl_texture = p->texture,
|
|
|
|
.gl_target = GL_TEXTURE_2D,
|
|
|
|
.tex_w = hw_image->w,
|
|
|
|
.tex_h = hw_image->h,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2016-02-14 11:46:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct gl_hwdec_driver gl_hwdec_dxva2gldx = {
|
|
|
|
.name = "dxva2-dxinterop",
|
|
|
|
.api = HWDEC_DXVA2,
|
|
|
|
.imgfmt = IMGFMT_DXVA2,
|
|
|
|
.create = create,
|
|
|
|
.reinit = reinit,
|
vo_opengl: refactor how hwdec interop exports textures
Rename gl_hwdec_driver.map_image to map_frame, and let it fill out a
struct gl_hwdec_frame describing the exact texture layout. This gives
more flexibility to what the hwdec interop can export. In particular, it
can export strange component orders/permutations and textures with
padded size. (The latter originating from cropped video.)
The way gl_hwdec_frame works is in the spirit of the rest of the
vo_opengl video processing code, which tends to put as much information
in immediate state (as part of the dataflow), instead of declaring it
globally. To some degree this duplicates the texplane and img_tex
structs, but until we somehow unify those, it's better to give the hwdec
state its own struct. The fact that changing the hwdec struct would
require changes and testing on at least 4 platform/GPU combinations
makes duplicating it almost a requirement to avoid pain later.
Make gl_hwdec_driver.reinit set the new image format and remove the
gl_hwdec.converted_imgfmt field.
Likewise, gl_hwdec.gl_texture_target is replaced with
gl_hwdec_plane.gl_target.
Split out a init_image_desc function from init_format. The latter is not
called in the hwdec case at all anymore. Setting up most of struct
texplane is also completely separate in the hwdec and normal cases.
video.c does not check whether the hwdec "mapped" image format is
supported. This should not really happen anyway, and if it does, the
hwdec interop backend must fail at creation time, so this is not an
issue.
2016-05-10 16:29:10 +00:00
|
|
|
.map_frame = map_frame,
|
2016-02-14 11:46:17 +00:00
|
|
|
.destroy = destroy,
|
|
|
|
};
|