diff --git a/libvo/d3d_shader_yuv.h b/libvo/d3d_shader_yuv.h new file mode 100644 index 0000000000..49ef753b4c --- /dev/null +++ b/libvo/d3d_shader_yuv.h @@ -0,0 +1,142 @@ +#if 0 +// +// Generated by Microsoft (R) HLSL Shader Compiler 9.27.952.3022 +// +// fxc /Tps_2_0 /Fhz:\tmp\mplayer\libvo\d3d_shader_yuv.h +// z:\tmp\mplayer\libvo\d3d_shader_yuv.hlsl /Vnd3d_shader_yuv +// +// +// Parameters: +// +// float4x4 colormatrix; +// sampler2D tex0; +// sampler2D tex1; +// sampler2D tex2; +// +// +// Registers: +// +// Name Reg Size +// ------------ ----- ---- +// colormatrix c0 4 +// tex0 s0 1 +// tex1 s1 1 +// tex2 s2 1 +// + + ps_2_0 + def c4, 1, 0, 0, 0 + dcl t0.xy + dcl t1.xy + dcl t2.xy + dcl_2d s0 + dcl_2d s1 + dcl_2d s2 + texld r0, t0, s0 + texld r1, t1, s1 + texld r2, t2, s2 + mov r0.y, r1.x + mov r0.z, r2.x + mov r0.w, c4.x + dp4 r1.x, r0, c0 + dp4 r1.y, r0, c1 + dp4 r1.z, r0, c2 + dp4 r1.w, r0, c3 + mov oC0, r1 + +// approximately 11 instruction slots used (3 texture, 8 arithmetic) +#endif + +const BYTE d3d_shader_yuv[] = +{ + 0, 2, 255, 255, 254, 255, + 67, 0, 67, 84, 65, 66, + 28, 0, 0, 0, 215, 0, + 0, 0, 0, 2, 255, 255, + 4, 0, 0, 0, 28, 0, + 0, 0, 0, 1, 0, 0, + 208, 0, 0, 0, 108, 0, + 0, 0, 2, 0, 0, 0, + 4, 0, 2, 0, 120, 0, + 0, 0, 0, 0, 0, 0, + 136, 0, 0, 0, 3, 0, + 0, 0, 1, 0, 2, 0, + 144, 0, 0, 0, 0, 0, + 0, 0, 160, 0, 0, 0, + 3, 0, 1, 0, 1, 0, + 6, 0, 168, 0, 0, 0, + 0, 0, 0, 0, 184, 0, + 0, 0, 3, 0, 2, 0, + 1, 0, 10, 0, 192, 0, + 0, 0, 0, 0, 0, 0, + 99, 111, 108, 111, 114, 109, + 97, 116, 114, 105, 120, 0, + 3, 0, 3, 0, 4, 0, + 4, 0, 1, 0, 0, 0, + 0, 0, 0, 0, 116, 101, + 120, 48, 0, 171, 171, 171, + 4, 0, 12, 0, 1, 0, + 1, 0, 1, 0, 0, 0, + 0, 0, 0, 0, 116, 101, + 120, 49, 0, 171, 171, 171, + 4, 0, 12, 0, 1, 0, + 1, 0, 1, 0, 0, 0, + 0, 0, 0, 0, 116, 101, + 120, 50, 0, 171, 171, 171, + 4, 0, 12, 0, 1, 0, + 1, 0, 1, 0, 0, 0, + 0, 0, 0, 0, 112, 115, + 95, 50, 95, 48, 0, 77, + 105, 99, 114, 111, 115, 111, + 102, 116, 32, 40, 82, 41, + 32, 72, 76, 83, 76, 32, + 83, 104, 97, 100, 101, 114, + 32, 67, 111, 109, 112, 105, + 108, 101, 114, 32, 57, 46, + 50, 55, 46, 57, 53, 50, + 46, 51, 48, 50, 50, 0, + 81, 0, 0, 5, 4, 0, + 15, 160, 0, 0, 128, 63, + 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + 31, 0, 0, 2, 0, 0, + 0, 128, 0, 0, 3, 176, + 31, 0, 0, 2, 0, 0, + 0, 128, 1, 0, 3, 176, + 31, 0, 0, 2, 0, 0, + 0, 128, 2, 0, 3, 176, + 31, 0, 0, 2, 0, 0, + 0, 144, 0, 8, 15, 160, + 31, 0, 0, 2, 0, 0, + 0, 144, 1, 8, 15, 160, + 31, 0, 0, 2, 0, 0, + 0, 144, 2, 8, 15, 160, + 66, 0, 0, 3, 0, 0, + 15, 128, 0, 0, 228, 176, + 0, 8, 228, 160, 66, 0, + 0, 3, 1, 0, 15, 128, + 1, 0, 228, 176, 1, 8, + 228, 160, 66, 0, 0, 3, + 2, 0, 15, 128, 2, 0, + 228, 176, 2, 8, 228, 160, + 1, 0, 0, 2, 0, 0, + 2, 128, 1, 0, 0, 128, + 1, 0, 0, 2, 0, 0, + 4, 128, 2, 0, 0, 128, + 1, 0, 0, 2, 0, 0, + 8, 128, 4, 0, 0, 160, + 9, 0, 0, 3, 1, 0, + 1, 128, 0, 0, 228, 128, + 0, 0, 228, 160, 9, 0, + 0, 3, 1, 0, 2, 128, + 0, 0, 228, 128, 1, 0, + 228, 160, 9, 0, 0, 3, + 1, 0, 4, 128, 0, 0, + 228, 128, 2, 0, 228, 160, + 9, 0, 0, 3, 1, 0, + 8, 128, 0, 0, 228, 128, + 3, 0, 228, 160, 1, 0, + 0, 2, 0, 8, 15, 128, + 1, 0, 228, 128, 255, 255, + 0, 0 +}; diff --git a/libvo/d3d_shader_yuv.hlsl b/libvo/d3d_shader_yuv.hlsl new file mode 100644 index 0000000000..9d46e536fc --- /dev/null +++ b/libvo/d3d_shader_yuv.hlsl @@ -0,0 +1,20 @@ +// Compile with: +// fxc.exe /Tps_2_0 /Fhd3d_shader_yuv.h d3d_shader_yuv.hlsl /Vnd3d_shader_yuv + +sampler2D tex0 : register(s0); +sampler2D tex1 : register(s1); +sampler2D tex2 : register(s2); + +uniform float4x4 colormatrix : register(c0); + +float4 main(float2 t0 : TEXCOORD0, + float2 t1 : TEXCOORD1, + float2 t2 : TEXCOORD2) + : COLOR +{ + float4 c = float4(tex2D(tex0, t0).x, + tex2D(tex1, t1).x, + tex2D(tex2, t2).x, + 1); + return mul(c, colormatrix); +} diff --git a/libvo/vo_direct3d.c b/libvo/vo_direct3d.c index a1124f5412..79c91cbea3 100644 --- a/libvo/vo_direct3d.c +++ b/libvo/vo_direct3d.c @@ -27,9 +27,13 @@ #include #include #include "config.h" +#include "options.h" +#include "subopt-helper.h" #include "talloc.h" #include "video_out.h" #include "video_out_internal.h" +#include "csputils.h" +#include "libmpcodecs/img_format.h" #include "fastmemcpy.h" #include "mp_msg.h" #include "aspect.h" @@ -39,18 +43,26 @@ #include "sub/sub.h" #include "eosd_packer.h" +// shaders generated by fxc.exe from d3d_shader_yuv.hlsl +#include "d3d_shader_yuv.h" + static const vo_info_t info = { "Direct3D 9 Renderer", "direct3d", - "Georgi Petrov (gogothebee) ", + "Georgi Petrov (gogothebee) and others", "" }; -// texture format for EOSD -// 0: use D3DFMT_A8L8 -// 1: use D3DFMT_A8 (doesn't work with wine) -#define USE_A8 0 +// TODO: beg someone to add this (there is already IMGFMT_Y8) +// equals MAKEFOURCC('Y', '1', '6', ' ') +#define IMGFMT_Y16 0x20363159 + +#define IMGFMT_IS_Y(x) ((x) == IMGFMT_Y8 || (x) == IMGFMT_Y16) +#define IMGFMT_Y_DEPTH(x) ((x) == IMGFMT_Y8 ? 8 : 16) + +#define DEVTYPE D3DDEVTYPE_HAL +//#define DEVTYPE D3DDEVTYPE_REF /* * Link essential libvo functions: preinit, config, control, draw_frame, @@ -74,6 +86,14 @@ typedef struct { float tu, tv; } vertex_eosd; +#define D3DFVF_VIDEO_VERTEX (D3DFVF_XYZ | D3DFVF_TEX3) + +typedef struct { + float x, y, z; + // pairs of texture coordinates for up to 3 planes + float t[3][2]; +} vertex_video; + struct d3dtex { // user-requested size int w, h; @@ -91,9 +111,32 @@ struct d3dtex { IDirect3DTexture9 *device; }; +struct texplane { + int bytes_per_pixel; + int bits_per_pixel; + // chroma shifts + // e.g. get the plane's width in pixels with (priv->src_width >> shift_x) + int shift_x, shift_y; + D3DFORMAT d3d_format; + struct d3dtex texture; + // temporary locking during uploading the frame (e.g. for draw_slice) + D3DLOCKED_RECT locked_rect; + // value used to clear the image with memset (YUV chroma planes do not use + // the value 0 for this) + uint8_t clearval; +}; + /* Global variables "priv" structure. I try to keep their count low. */ static struct global_priv { + int opt_prefer_stretchrect; + int opt_disable_textures; + int opt_disable_stretchrect; + int opt_disable_shaders; + int opt_only_8bit; + int opt_disable_eosd; + int opt_disable_texture_align; + int is_paused; /**< 1 = Movie is paused, 0 = Movie is not paused */ int is_clear_needed; /**< 1 = Clear the backbuffer before StretchRect @@ -107,6 +150,17 @@ static struct global_priv { int src_height; /**< Source (movie) heigth */ int border_x; /**< horizontal border value for OSD */ int border_y; /**< vertical border value for OSD */ + int image_format; /**< mplayer image format */ + bool use_textures; /**< use 3D texture rendering, instead of + StretchRect */ + bool use_shaders; /**< use shader for YUV color conversion + (or possibly for RGB video equalizers) */ + + int plane_count; + struct texplane planes[3]; + + IDirect3DPixelShader9 *pixel_shader; + const BYTE *pixel_shader_data; D3DFORMAT movie_src_fmt; /**< Movie colorspace format (depends on the movie's codec) */ @@ -118,13 +172,14 @@ static struct global_priv { LPDIRECT3D9 d3d_handle; /**< Direct3D Handle */ LPDIRECT3DDEVICE9 d3d_device; /**< The Direct3D Adapter */ + bool d3d_in_scene; /**< BeginScene was called, EndScene not */ IDirect3DSurface9 *d3d_surface; /**< Offscreen Direct3D Surface. MPlayer renders inside it. Uses colorspace priv->movie_src_fmt */ struct d3dtex texture_osd; /**< RGBA */ IDirect3DSurface9 *d3d_backbuf; /**< Video card's back buffer (used to display next frame) */ - struct d3dtex texture_eosd; /**< A8 or A8L8 (see USE_A8) */ + struct d3dtex texture_eosd; /**< A8 */ int cur_backbuf_width; /**< Current backbuffer width */ int cur_backbuf_height; /**< Current backbuffer height */ int is_osd_populated; /**< 1 = OSD texture has something to display, @@ -138,34 +193,44 @@ static struct global_priv { int max_texture_width; /**< from the device capabilities */ int max_texture_height; /**< from the device capabilities */ + D3DMATRIX d3d_colormatrix; + struct mp_csp_details colorspace; + struct mp_csp_equalizer video_eq; + struct eosd_packer *eosd; /**< EOSD packer (image positions etc.) */ vertex_eosd *eosd_vb; /**< temporary memory for D3D when rendering EOSD */ } *priv; -typedef struct { +struct fmt_entry { const unsigned int mplayer_fmt; /**< Given by MPlayer */ const D3DFORMAT fourcc; /**< Required by D3D's test function */ -} struct_fmt_table; +}; /* Map table from reported MPlayer format to the required fourcc. This is needed to perform the format query. */ -static const struct_fmt_table fmt_table[] = { +static const struct fmt_entry fmt_table[] = { + // planar YUV {IMGFMT_YV12, MAKEFOURCC('Y','V','1','2')}, {IMGFMT_I420, MAKEFOURCC('I','4','2','0')}, {IMGFMT_IYUV, MAKEFOURCC('I','Y','U','V')}, {IMGFMT_YVU9, MAKEFOURCC('Y','V','U','9')}, + // packed YUV {IMGFMT_YUY2, D3DFMT_YUY2}, {IMGFMT_UYVY, D3DFMT_UYVY}, + // packed RGB {IMGFMT_BGR32, D3DFMT_X8R8G8B8}, {IMGFMT_RGB32, D3DFMT_X8B8G8R8}, {IMGFMT_BGR24, D3DFMT_R8G8B8}, //untested {IMGFMT_BGR16, D3DFMT_R5G6B5}, {IMGFMT_BGR15, D3DFMT_X1R5G5B5}, {IMGFMT_BGR8 , D3DFMT_R3G3B2}, //untested + // grayscale (can be considered both packed and planar) + {IMGFMT_Y8, D3DFMT_L8}, + {IMGFMT_Y16, D3DFMT_L16}, + {0}, }; -#define DISPLAY_FORMAT_TABLE_ENTRIES (sizeof(fmt_table) / sizeof(fmt_table[0])) typedef enum back_buffer_action { BACKBUFFER_CREATE, @@ -174,7 +239,28 @@ typedef enum back_buffer_action { static void generate_eosd(mp_eosd_images_t *); static void draw_eosd(void); +static void update_colorspace(void); +static void d3d_clear_video_textures(void); +static int resize_d3d(void); +static void d3d_matrix_identity(D3DMATRIX *m) +{ + memset(m, 0, sizeof(D3DMATRIX)); + m->_11 = m->_22 = m->_33 = m->_44 = 1.0f; +} + +static void d3d_matrix_ortho(D3DMATRIX *m, float left, float right, + float bottom, float top) +{ + d3d_matrix_identity(m); + m->_11 = 2.0f / (right - left); + m->_22 = 2.0f / (top - bottom); + m->_33 = 1.0f; + m->_41 = -(right + left) / (right - left); + m->_42 = -(top + bottom) / (top - bottom); + m->_43 = 0; + m->_44 = 1.0f; +} /**************************************************************************** * * @@ -186,6 +272,18 @@ static void draw_eosd(void); * * ****************************************************************************/ +static bool d3d_begin_scene(void) +{ + if (!priv->d3d_in_scene) { + if (FAILED(IDirect3DDevice9_BeginScene(priv->d3d_device))) { + mp_msg(MSGT_VO, MSGL_ERR, "BeginScene failed.\n"); + return false; + } + priv->d3d_in_scene = true; + } + return true; +} + /** @brief Calculate scaled fullscreen movie rectangle with * preserved aspect ratio. */ @@ -241,9 +339,10 @@ static void d3d_fix_texture_size(int *width, int *height) /* device only supports square textures */ tex_width = tex_height = tex_width > tex_height ? tex_width : tex_height; // better round up to a multiple of 16 - // (xxx: why???) - tex_width = (tex_width + 15) & ~15; - tex_height = (tex_height + 15) & ~15; + if (!priv->opt_disable_texture_align) { + tex_width = (tex_width + 15) & ~15; + tex_height = (tex_height + 15) & ~15; + } *width = tex_width; *height = tex_height; @@ -304,20 +403,60 @@ error_exit: static IDirect3DBaseTexture9 *d3dtex_get_render_texture(struct d3dtex *tex) { - return (IDirect3DBaseTexture9 *) - (priv->device_texture_sys ? tex->system : tex->device); + return (IDirect3DBaseTexture9 *)(tex->device ? tex->device : tex->system); } // Copy system texture contents to device texture. static bool d3dtex_update(struct d3dtex *tex) { - if (priv->device_texture_sys) + if (!tex->device) return true; return !FAILED(IDirect3DDevice9_UpdateTexture(priv->d3d_device, (IDirect3DBaseTexture9 *)tex->system, (IDirect3DBaseTexture9 *)tex->device)); } +static void d3d_unlock_video_objects(void) +{ + bool any_failed = false; + + if (priv->locked_rect.pBits) { + if (FAILED(IDirect3DSurface9_UnlockRect(priv->d3d_surface))) + any_failed = true; + } + priv->locked_rect.pBits = NULL; + + for (int n = 0; n < priv->plane_count; n++) { + struct texplane *plane = &priv->planes[n]; + if (plane->locked_rect.pBits) { + if (FAILED(IDirect3DTexture9_UnlockRect(plane->texture.system, 0))) + any_failed = true; + } + plane->locked_rect.pBits = NULL; + } + + if (any_failed) + mp_msg(MSGT_VO, MSGL_V, "Unlocking video objects failed.\n"); +} + +// Free video surface/textures, shaders, etc. +static void d3d_destroy_video_objects(void) +{ + d3d_unlock_video_objects(); + + if (priv->d3d_surface) + IDirect3DSurface9_Release(priv->d3d_surface); + priv->d3d_surface = NULL; + + for (int n = 0; n < priv->plane_count; n++) { + d3dtex_release(&priv->planes[n].texture); + } + + if (priv->pixel_shader) + IDirect3DPixelShader9_Release(priv->pixel_shader); + priv->pixel_shader = NULL; +} + /** @brief Destroy D3D Offscreen and Backbuffer surfaces. */ static void destroy_d3d_surfaces(void) @@ -325,13 +464,7 @@ static void destroy_d3d_surfaces(void) mp_msg(MSGT_VO, MSGL_V, "destroy_d3d_surfaces called.\n"); /* Let's destroy the old (if any) D3D Surfaces */ - if (priv->locked_rect.pBits) - IDirect3DSurface9_UnlockRect(priv->d3d_surface); - priv->locked_rect.pBits = NULL; - - if (priv->d3d_surface) - IDirect3DSurface9_Release(priv->d3d_surface); - priv->d3d_surface = NULL; + d3d_destroy_video_objects(); d3dtex_release(&priv->texture_osd); @@ -345,6 +478,102 @@ static void destroy_d3d_surfaces(void) eosd_packer_reinit(priv->eosd, 0, 0); } +// Allocate video surface or textures, and create shaders if needed. +static bool d3d_configure_video_objects(void) +{ + int n; + bool need_clear = false; + + assert(priv->image_format != 0); + + if (priv->use_textures) { + for (n = 0; n < priv->plane_count; n++) { + struct texplane *plane = &priv->planes[n]; + + if (!plane->texture.system) { + if (!d3dtex_allocate(&plane->texture, + plane->d3d_format, + priv->src_width >> plane->shift_x, + priv->src_height >> plane->shift_y)) + { + mp_msg(MSGT_VO, MSGL_ERR, "Allocating plane %d" + " failed.\n", n); + return false; + } + + mp_msg(MSGT_VO, MSGL_V, "Allocated plane %d:" + " %d bit, shift=%d/%d size=%d/%d (%d/%d).\n", n, + plane->bits_per_pixel, + plane->shift_x, plane->shift_y, + plane->texture.w, plane->texture.h, + plane->texture.tex_w, plane->texture.tex_h); + + need_clear = true; + } + } + + if (need_clear) + d3d_clear_video_textures(); + + if (priv->pixel_shader_data) { + if (!priv->pixel_shader && + FAILED(IDirect3DDevice9_CreatePixelShader(priv->d3d_device, + (DWORD *)priv->pixel_shader_data, &priv->pixel_shader))) + { + mp_msg(MSGT_VO, MSGL_ERR, "Failed to create " + "YUV conversion pixel shader.\n"); + return false; + } + } + + } else { + + if (!priv->d3d_surface && + FAILED(IDirect3DDevice9_CreateOffscreenPlainSurface( + priv->d3d_device, priv->src_width, priv->src_height, + priv->movie_src_fmt, D3DPOOL_DEFAULT, &priv->d3d_surface, NULL))) { + mp_msg(MSGT_VO, MSGL_ERR, + "Allocating offscreen surface failed.\n"); + return false; + } + } + + return true; +} + +static bool d3d_lock_video_textures(void) +{ + for (int n = 0; n < priv->plane_count; n++) { + struct texplane *plane = &priv->planes[n]; + + if (!plane->locked_rect.pBits) { + if (FAILED(IDirect3DTexture9_LockRect(plane->texture.system, 0, + &plane->locked_rect, NULL, 0))) + { + mp_msg(MSGT_VO, MSGL_V, "Texture lock failure.\n"); + d3d_unlock_video_objects(); + return false; + } + } + } + + return true; +} + +static void d3d_clear_video_textures(void) +{ + if (!d3d_lock_video_textures()) + return; + + for (int n = 0; n < priv->plane_count; n++) { + struct texplane *plane = &priv->planes[n]; + memset(plane->locked_rect.pBits, plane->clearval, + plane->locked_rect.Pitch * plane->texture.tex_h); + } + + d3d_unlock_video_objects(); +} + /** @brief Create D3D Offscreen and Backbuffer surfaces. Each * surface is created only if it's not already present. * @return 1 on success, 0 on failure @@ -355,15 +584,6 @@ static int create_d3d_surfaces(void) int tex_width = osd_width, tex_height = osd_height; mp_msg(MSGT_VO, MSGL_V, "create_d3d_surfaces called.\n"); - if (!priv->d3d_surface && - FAILED(IDirect3DDevice9_CreateOffscreenPlainSurface( - priv->d3d_device, priv->src_width, priv->src_height, - priv->movie_src_fmt, D3DPOOL_DEFAULT, &priv->d3d_surface, NULL))) { - mp_msg(MSGT_VO, MSGL_ERR, - "Allocating offscreen surface failed.\n"); - return 0; - } - if (!priv->d3d_backbuf && FAILED(IDirect3DDevice9_GetBackBuffer(priv->d3d_device, 0, 0, D3DBACKBUFFER_TYPE_MONO, @@ -372,6 +592,9 @@ static int create_d3d_surfaces(void) return 0; } + if (!d3d_configure_video_objects()) + return 0; + /* create OSD */ d3d_fix_texture_size(&tex_width, &tex_height); @@ -405,13 +628,23 @@ static int create_d3d_surfaces(void) priv->texture_osd.h = osd_height; /* setup default renderstate */ - IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_SRCBLEND, D3DBLEND_ONE); + IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHAFUNC, D3DCMP_GREATER); IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHAREF, (DWORD)0x0); IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_LIGHTING, FALSE); - IDirect3DDevice9_SetSamplerState(priv->d3d_device, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); - IDirect3DDevice9_SetSamplerState(priv->d3d_device, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); + + // we use up to 3 samplers for up to 3 YUV planes + for (int n = 0; n < 3; n++) { + IDirect3DDevice9_SetSamplerState(priv->d3d_device, n, D3DSAMP_MINFILTER, + D3DTEXF_LINEAR); + IDirect3DDevice9_SetSamplerState(priv->d3d_device, n, D3DSAMP_MAGFILTER, + D3DTEXF_LINEAR); + IDirect3DDevice9_SetSamplerState(priv->d3d_device, n, D3DSAMP_ADDRESSU, + D3DTADDRESS_CLAMP); + IDirect3DDevice9_SetSamplerState(priv->d3d_device, n, D3DSAMP_ADDRESSV, + D3DTADDRESS_CLAMP); + } if (priv->eosd && !priv->texture_eosd.system) eosd_packer_reinit(priv->eosd, priv->max_texture_width, @@ -466,7 +699,7 @@ static int change_d3d_backbuffer(back_buffer_action_e action) if (action == BACKBUFFER_CREATE && FAILED(IDirect3D9_CreateDevice(priv->d3d_handle, D3DADAPTER_DEFAULT, - D3DDEVTYPE_HAL, vo_w32_window, + DEVTYPE, vo_w32_window, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &present_params, &priv->d3d_device))) { mp_msg(MSGT_VO, MSGL_V, @@ -496,7 +729,6 @@ static int change_d3d_backbuffer(back_buffer_action_e action) static int configure_d3d(void) { D3DDISPLAYMODE disp_mode; - D3DVIEWPORT9 vp = {0, 0, vo_dwidth, vo_dheight, 0, 1}; mp_msg(MSGT_VO, MSGL_V, "configure_d3d called.\n"); @@ -521,13 +753,7 @@ static int configure_d3d(void) if (!create_d3d_surfaces()) return 0; - if (FAILED(IDirect3DDevice9_SetViewport(priv->d3d_device, - &vp))) { - mp_msg(MSGT_VO, MSGL_ERR, "Setting viewport failed.\n"); - return 0; - } - - calc_fs_rect(); + resize_d3d(); return 1; } @@ -597,6 +823,11 @@ static int resize_d3d(void) return 0; } + // so that screen coordinates map to D3D ones + D3DMATRIX view; + d3d_matrix_ortho(&view, 0.5f, vp.Width + 0.5f, vp.Height + 0.5f, 0.5f); + IDirect3DDevice9_SetTransform(priv->d3d_device, D3DTS_VIEW, &view); + calc_fs_rect(); #ifdef CONFIG_FREETYPE @@ -632,6 +863,20 @@ static void uninit_d3d(void) static uint32_t d3d_draw_frame(void); +static uint32_t d3d_upload_and_render_frame_texture(mp_image_t *mpi) +{ + if (!(mpi->flags & MP_IMGFLAG_DRAW_CALLBACK)) + draw_slice(mpi->planes, mpi->stride, mpi->w, mpi->h, 0, 0); + + d3d_unlock_video_objects(); + + for (int n = 0; n < priv->plane_count; n++) { + d3dtex_update(&priv->planes[n].texture); + } + + return d3d_draw_frame(); +} + /** @brief Render a frame on the screen. * @param mpi mpi structure with the decoded frame inside * @return VO_TRUE on success, VO_ERROR on failure @@ -647,6 +892,9 @@ static uint32_t d3d_upload_and_render_frame(mp_image_t *mpi) if (!priv->d3d_device) return VO_TRUE; + if (priv->use_textures) + return d3d_upload_and_render_frame_texture(mpi); + if (mpi->flags & MP_IMGFLAG_DRAW_CALLBACK) goto skip_upload; @@ -668,22 +916,17 @@ static uint32_t d3d_upload_and_render_frame(mp_image_t *mpi) mpi->height, priv->locked_rect.Pitch, mpi->stride[0]); skip_upload: - /* This unlock is used for both slice_draw path and DRAW_IMAGE path. */ - if (FAILED(IDirect3DSurface9_UnlockRect(priv->d3d_surface))) { - mp_msg(MSGT_VO, MSGL_V, "Surface unlock failed.\n"); - return VO_ERROR; - } - priv->locked_rect.pBits = NULL; + d3d_unlock_video_objects(); return d3d_draw_frame(); } static uint32_t d3d_draw_frame(void) { - if (FAILED(IDirect3DDevice9_BeginScene(priv->d3d_device))) { - mp_msg(MSGT_VO, MSGL_ERR, "BeginScene failed.\n"); + int n; + + if (!d3d_begin_scene()) return VO_ERROR; - } if (priv->is_clear_needed) { IDirect3DDevice9_Clear(priv->d3d_device, 0, NULL, @@ -691,52 +934,257 @@ static uint32_t d3d_draw_frame(void) priv->is_clear_needed = 0; } - if (FAILED(IDirect3DDevice9_StretchRect(priv->d3d_device, - priv->d3d_surface, - &priv->fs_panscan_rect, - priv->d3d_backbuf, - &priv->fs_movie_rect, - D3DTEXF_LINEAR))) { - mp_msg(MSGT_VO, MSGL_ERR, - "Copying frame to the backbuffer failed.\n"); - return VO_ERROR; - } + if (priv->use_textures) { - if (FAILED(IDirect3DDevice9_EndScene(priv->d3d_device))) { - mp_msg(MSGT_VO, MSGL_ERR, "EndScene failed.\n"); - return VO_ERROR; + for (n = 0; n < priv->plane_count; n++) { + IDirect3DDevice9_SetTexture(priv->d3d_device, n, + d3dtex_get_render_texture(&priv->planes[n].texture)); + } + + RECT rm = priv->fs_movie_rect; + RECT rs = priv->fs_panscan_rect; + + vertex_video vb[] = { + { rm.left, rm.top, 0.0f}, + { rm.right, rm.top, 0.0f}, + { rm.left, rm.bottom, 0.0f}, + { rm.right, rm.bottom, 0.0f} + }; + + float texc[4][2] = { + { rs.left, rs.top}, + { rs.right, rs.top}, + { rs.left, rs.bottom}, + { rs.right, rs.bottom} + }; + + for (n = 0; n < priv->plane_count; n++) { + float s_x = (1.0f / (1 << priv->planes[n].shift_x)) + / priv->planes[n].texture.tex_w; + float s_y = (1.0f / (1 << priv->planes[n].shift_y)) + / priv->planes[n].texture.tex_h; + for (int i = 0; i < 4; i++) { + vb[i].t[n][0] = texc[i][0] * s_x; + vb[i].t[n][1] = texc[i][1] * s_y; + } + } + + if (priv->pixel_shader) { + IDirect3DDevice9_SetPixelShader(priv->d3d_device, priv->pixel_shader); + IDirect3DDevice9_SetPixelShaderConstantF(priv->d3d_device, 0, + &priv->d3d_colormatrix._11, + 4); + } + + IDirect3DDevice9_SetFVF(priv->d3d_device, D3DFVF_VIDEO_VERTEX); + IDirect3DDevice9_DrawPrimitiveUP(priv->d3d_device, D3DPT_TRIANGLESTRIP, + 2, &vb[0], sizeof(vertex_video)); + + IDirect3DDevice9_SetPixelShader(priv->d3d_device, NULL); + + for (n = 0; n < priv->plane_count; n++) { + IDirect3DDevice9_SetTexture(priv->d3d_device, n, NULL); + } + + } else { + if (FAILED(IDirect3DDevice9_StretchRect(priv->d3d_device, + priv->d3d_surface, + &priv->fs_panscan_rect, + priv->d3d_backbuf, + &priv->fs_movie_rect, + D3DTEXF_LINEAR))) { + mp_msg(MSGT_VO, MSGL_ERR, + "Copying frame to the backbuffer failed.\n"); + return VO_ERROR; + } } return VO_TRUE; } -static const struct_fmt_table *check_format(uint32_t movie_fmt) +// Return the high byte of the value that represents white in chroma (U/V) +static int get_chroma_clear_val(int bit_depth) { - int i; - for (i = 0; i < DISPLAY_FORMAT_TABLE_ENTRIES; i++) { - if (fmt_table[i].mplayer_fmt == movie_fmt) { - /* Test conversion from Movie colorspace to - * display's target colorspace. */ - if (FAILED(IDirect3D9_CheckDeviceFormatConversion(priv->d3d_handle, - D3DADAPTER_DEFAULT, - D3DDEVTYPE_HAL, - fmt_table[i].fourcc, - priv->desktop_fmt))) { - mp_msg(MSGT_VO, MSGL_V, "Rejected image format: %s\n", - vo_format_name(fmt_table[i].mplayer_fmt)); - return NULL; + return 1 << (bit_depth - 1 & 7); +} + +// this macro is supposed to work on all formats supported by 3D rendering, and +// that produce "reasonable" output (i.e. no mixed up colors) +#define IMGFMT_IS_ANY_RND(x) (IMGFMT_IS_BGR(x) || IMGFMT_IS_RGB(x) || IMGFMT_IS_Y(x)) + +// pixel size in bit for any IMGFMT_IS_ANY_RND(x)==true +// we assume that the actual pixel strides are always aligned on bytes +static int imgfmt_any_rnd_depth(int fmt) +{ + if (IMGFMT_IS_BGR(fmt)) + return IMGFMT_BGR_DEPTH(fmt); + if (IMGFMT_IS_RGB(fmt)) + return IMGFMT_RGB_DEPTH(fmt); + if (IMGFMT_IS_Y(fmt)) + return IMGFMT_Y_DEPTH(fmt); + assert(false); + return 0; +} + +static D3DFORMAT check_format(uint32_t movie_fmt, bool as_texture) +{ + const char *type = as_texture ? "texture rendering" : "StretchRect"; + const struct fmt_entry *cur = &fmt_table[0]; + + // Don't try to handle weird packed texture formats (although I don't know + // if D3D9 would even accept any such format for 3D rendering; and we + // certainly don't try any tricks like matching it to RGB formats and + // applying a YUV conversion matrix) + if (as_texture && !IMGFMT_IS_ANY_RND(movie_fmt)) + return 0; + + while (cur->mplayer_fmt) { + if (cur->mplayer_fmt == movie_fmt) { + HRESULT res; + if (as_texture) { + res = IDirect3D9_CheckDeviceFormat(priv->d3d_handle, + D3DADAPTER_DEFAULT, + DEVTYPE, + priv->desktop_fmt, + D3DUSAGE_DYNAMIC | D3DUSAGE_QUERY_FILTER, + D3DRTYPE_TEXTURE, + cur->fourcc); + } else { + /* Test conversion from Movie colorspace to + * display's target colorspace. */ + res = IDirect3D9_CheckDeviceFormatConversion(priv->d3d_handle, + D3DADAPTER_DEFAULT, + DEVTYPE, + cur->fourcc, + priv->desktop_fmt); + } + if (FAILED(res)) { + mp_msg(MSGT_VO, MSGL_V, "Rejected image format " + "(%s): %s\n", type, vo_format_name(cur->mplayer_fmt)); + return 0; } - mp_msg(MSGT_VO, MSGL_V, "Accepted image format: %s\n", - vo_format_name(fmt_table[i].mplayer_fmt)); + mp_msg(MSGT_VO, MSGL_DBG2, "Accepted image format " + "(%s): %s\n", type, vo_format_name(cur->mplayer_fmt)); - return &fmt_table[i]; + return cur->fourcc; } + cur++; } return 0; } +// Check whether YUV conversion with shaders can be done. +// Returns the format the individual planes should use (or 0 on failure) +static D3DFORMAT check_shader_conversion(uint32_t fmt) +{ + if (priv->opt_disable_shaders) + return 0; + int component_bits; + if (!mp_get_chroma_shift(fmt, NULL, NULL, &component_bits)) + return 0; + if (component_bits < 8 || component_bits > 16) + return 0; + bool is_8bit = component_bits == 8; + if (!is_8bit && priv->opt_only_8bit) + return 0; + return check_format(is_8bit ? IMGFMT_Y8 : IMGFMT_Y16, true); +} + +// Return if the image format can be used. If it can, decide which rendering +// and conversion mode to use. +// If initialize is true, actually setup all variables to use the picked +// rendering mode. +static bool init_rendering_mode(uint32_t fmt, bool initialize) +{ + int n; + int blit_d3dfmt = check_format(fmt, false); + int texture_d3dfmt = check_format(fmt, true); + int shader_d3dfmt = check_shader_conversion(fmt); + + if (priv->opt_disable_textures) + texture_d3dfmt = 0; + if (priv->opt_disable_shaders) + shader_d3dfmt = 0; + if (priv->opt_disable_stretchrect) + blit_d3dfmt = 0; + + if (!(blit_d3dfmt || shader_d3dfmt || texture_d3dfmt)) + return false; + + mp_msg(MSGT_VO, MSGL_V, "Accepted rendering methods for " + "format='%s': StretchRect=%#x, Texture=%#x, Texture+Shader=%#x.\n", + vo_format_name(fmt), blit_d3dfmt, texture_d3dfmt, shader_d3dfmt); + + if (!initialize) + return true; + + // initialization doesn't fail beyond this point + + priv->use_shaders = false; + priv->use_textures = false; + priv->movie_src_fmt = 0; + priv->pixel_shader_data = NULL; + priv->plane_count = 0; + priv->image_format = fmt; + + if (blit_d3dfmt && priv->opt_prefer_stretchrect) + texture_d3dfmt = shader_d3dfmt = 0; + + if (texture_d3dfmt) { + priv->use_textures = true; + } else if (shader_d3dfmt) { + priv->use_textures = true; + priv->use_shaders = true; + } else { + assert(!!blit_d3dfmt); + } + + if (priv->use_textures) { + mp_msg(MSGT_VO, MSGL_V, "Using 3D rendering.\n"); + + struct texplane *planes = &priv->planes[0]; + planes[0].shift_x = planes[0].shift_y = 0; + planes[0].clearval = 0; + + if (!priv->use_shaders) { + assert(IMGFMT_IS_ANY_RND(priv->image_format)); + priv->plane_count = 1; + planes[0].bits_per_pixel = imgfmt_any_rnd_depth(priv->image_format); + planes[0].d3d_format = texture_d3dfmt; + } else { + mp_msg(MSGT_VO, MSGL_V, "Using YUV shaders.\n"); + + int sx, sy, component_bits; + mp_get_chroma_shift(priv->image_format, &sx, &sy, &component_bits); + priv->plane_count = 3; + for (n = 0; n < 3; n++) { + planes[n].d3d_format = shader_d3dfmt; + planes[n].bits_per_pixel = component_bits; + if (n > 0) { + planes[n].shift_x = sx; + planes[n].shift_y = sy; + planes[n].clearval = get_chroma_clear_val(component_bits); + } + } + priv->pixel_shader_data = d3d_shader_yuv; + } + + for (n = 0; n < priv->plane_count; n++) { + planes[n].bytes_per_pixel = (planes[n].bits_per_pixel + 7) / 8; + } + + } else { + mp_msg(MSGT_VO, MSGL_V, "Using StretchRect.\n"); + + priv->movie_src_fmt = blit_d3dfmt; + } + + update_colorspace(); + + return true; +} /** @brief Query if movie colorspace is supported by the HW. * @return 0 on failure, device capabilities (not probed @@ -744,7 +1192,7 @@ static const struct_fmt_table *check_format(uint32_t movie_fmt) */ static int query_format(uint32_t movie_fmt) { - if (!check_format(movie_fmt)) + if (!init_rendering_mode(movie_fmt, false)) return 0; int eosd_caps = VFCAP_CSP_SUPPORTED | VFCAP_CSP_SUPPORTED_BY_HW @@ -764,8 +1212,52 @@ static int query_format(uint32_t movie_fmt) * * ****************************************************************************/ +static void update_colorspace(void) +{ + float coeff[3][4]; + struct mp_csp_params csp = { .colorspace = priv->colorspace }; + mp_csp_copy_equalizer_values(&csp, &priv->video_eq); + if (priv->use_shaders) { + csp.input_bits = priv->planes[0].bits_per_pixel; + csp.texture_bits = (csp.input_bits + 7) & ~7; + } + mp_get_yuv2rgb_coeffs(&csp, coeff); + for (int row = 0; row < 3; row++) { + for (int col = 0; col < 4; col++) { + priv->d3d_colormatrix.m[row][col] = coeff[row][col]; + } + } +} + +const char *options_help_text = "-vo direct3d command line help:\n" +"Example: -vo direct3d:disable-eosd:disable-textures\n" +"Options:\n" +" prefer-stretchrect\n" +" Use IDirect3DDevice9::StretchRect over other methods if possible.\n" +" disable-stretchrect\n" +" Never render the video using IDirect3DDevice9::StretchRect.\n" +" disable-textures\n" +" Never render the video using D3D texture rendering. (Rendering with\n" +" textures + shader will still be allowed. Add disable-shaders to\n" +" completely disable video rendering with textures.)\n" +" disable-shaders\n" +" Never use shaders when rendering video.\n" +" only-8bit\n" +" Never render YUV video with more than 8 bits per component.\n" +" (Using this flag will force software conversion to 8 bit.)\n" +" disable-eosd\n" +" Disable EOSD rendering for subtitles.\n" +" (Using this flag might force the insertion of the 'ass' video filter,\n" +" which will render the subtitles in software.)\n" +" force-power-of-2\n" +" Always force textures to power of 2, even if the device reports\n" +" non-power-of-2 texture sizes as supported.\n" +" disable-texture-align\n" +" Normally texture sizes are always aligned to 16. With this option\n" +" enabled, the video texture will always have exactly the same size as\n" +" the video itself.\n"; /** @brief libvo Callback: Preinitialize the video card. * Preinit the hardware just enough to be queried about @@ -783,11 +1275,30 @@ static int preinit(const char *arg) /* Set to zero all global variables. */ priv = talloc_zero(NULL, struct global_priv); + *priv = (struct global_priv) { + .colorspace = MP_CSP_DETAILS_DEFAULTS, + .video_eq = { MP_CSP_EQ_CAPS_COLORMATRIX }, + }; - //xxx make it possible to disable eosd by command line - bool enable_eosd = true; + int opt_force_power_of_2 = false; - if (enable_eosd) + const opt_t subopts[] = { + {"prefer-stretchrect", OPT_ARG_BOOL, &priv->opt_prefer_stretchrect, NULL}, + {"disable-textures", OPT_ARG_BOOL, &priv->opt_disable_textures, NULL}, + {"disable-stretchrect", OPT_ARG_BOOL, &priv->opt_disable_stretchrect, NULL}, + {"disable-shaders", OPT_ARG_BOOL, &priv->opt_disable_shaders, NULL}, + {"only-8bit", OPT_ARG_BOOL, &priv->opt_only_8bit, NULL}, + {"disable-eosd", OPT_ARG_BOOL, &priv->opt_disable_eosd, NULL}, + {"force-power-of-2", OPT_ARG_BOOL, &opt_force_power_of_2, NULL}, + {"disable-texture-align", OPT_ARG_BOOL, &priv->opt_disable_texture_align, NULL}, + {NULL} + }; + if (subopt_parse(arg, subopts) != 0) { + mp_msg(MSGT_VO, MSGL_FATAL, options_help_text); + return -1; + } + + if (!priv->opt_disable_eosd) priv->eosd = eosd_packer_create(priv); priv->d3d9_dll = LoadLibraryA("d3d9.dll"); @@ -825,7 +1336,7 @@ static int preinit(const char *arg) if (FAILED(IDirect3D9_GetDeviceCaps(priv->d3d_handle, D3DADAPTER_DEFAULT, - D3DDEVTYPE_HAL, + DEVTYPE, &disp_caps))) { mp_msg(MSGT_VO, MSGL_ERR, "Reading display capabilities failed.\n"); goto err_out; @@ -841,6 +1352,9 @@ static int preinit(const char *arg) priv->max_texture_width = disp_caps.MaxTextureWidth; priv->max_texture_height = disp_caps.MaxTextureHeight; + if (opt_force_power_of_2) + priv->device_caps_power2_only = 1; + mp_msg(MSGT_VO, MSGL_V, "device_caps_power2_only %d, device_caps_square_only %d\n" "device_texture_sys %d\n" "max_texture_width %d, max_texture_height %d\n", @@ -903,10 +1417,31 @@ static int control(uint32_t request, void *data) priv->is_clear_needed = 1; d3d_draw_frame(); return VO_TRUE; - case VOCTRL_SET_EQUALIZER: - return VO_NOTIMPL; - case VOCTRL_GET_EQUALIZER: - return VO_NOTIMPL; + case VOCTRL_SET_YUV_COLORSPACE: + priv->colorspace = *(struct mp_csp_details *)data; + update_colorspace(); + return VO_TRUE; + case VOCTRL_GET_YUV_COLORSPACE: + if (!priv->use_shaders) + break; // no idea what the heck D3D YUV uses + *(struct mp_csp_details *)data = priv->colorspace; + return VO_TRUE; + case VOCTRL_SET_EQUALIZER: { + if (!priv->use_shaders) + break; + struct voctrl_set_equalizer_args *args = data; + if (mp_csp_equalizer_set(&priv->video_eq, args->name, args->value) < 0) + return VO_NOTIMPL; + update_colorspace(); + return VO_TRUE; + } + case VOCTRL_GET_EQUALIZER: { + if (!priv->use_shaders) + break; + struct voctrl_get_equalizer_args *args = data; + return mp_csp_equalizer_get(&priv->video_eq, args->name, args->valueptr) + >= 0 ? VO_TRUE : VO_NOTIMPL; + } case VOCTRL_ONTOP: vo_w32_ontop(); return VO_TRUE; @@ -918,7 +1453,7 @@ static int control(uint32_t request, void *data) w32_update_xinerama_info(); return VO_TRUE; case VOCTRL_SET_PANSCAN: - calc_fs_rect(); + resize_d3d(); return VO_TRUE; case VOCTRL_GET_PANSCAN: return VO_TRUE; @@ -939,7 +1474,7 @@ static int control(uint32_t request, void *data) return VO_TRUE; } } - return VO_FALSE; + return VO_NOTIMPL; } /** @brief libvo Callback: Configre the Direct3D adapter. @@ -957,10 +1492,6 @@ static int config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t options, char *title, uint32_t format) { - const struct_fmt_table *fmt_entry = check_format(format); - if (!fmt_entry) - return VO_ERROR; - /* w32_common framework call. Creates window on the screen with * the given coordinates. */ @@ -969,17 +1500,15 @@ static int config(uint32_t width, uint32_t height, uint32_t d_width, return VO_ERROR; } - if ((priv->movie_src_fmt != fmt_entry->fourcc) + if ((priv->image_format != format) || (priv->src_width != width) || (priv->src_height != height)) { - priv->movie_src_fmt = fmt_entry->fourcc; + d3d_destroy_video_objects(); + priv->src_width = width; priv->src_height = height; - - if (priv->d3d_surface) - IDirect3DSurface9_Release(priv->d3d_surface); - priv->d3d_surface = NULL; + init_rendering_mode(format, true); } if (!priv->d3d_device) { @@ -998,6 +1527,13 @@ static int config(uint32_t width, uint32_t height, uint32_t d_width, */ static void flip_page(void) { + if (priv->d3d_in_scene) { + if (FAILED(IDirect3DDevice9_EndScene(priv->d3d_device))) { + mp_msg(MSGT_VO, MSGL_ERR, "EndScene failed.\n"); + } + priv->d3d_in_scene = false; + } + RECT rect = {0, 0, vo_dwidth, vo_dheight}; if (!priv->d3d_device || FAILED(IDirect3DDevice9_Present(priv->d3d_device, &rect, 0, 0, 0))) { @@ -1045,6 +1581,26 @@ static void check_events(void) flip_page(); } +static int draw_slice_textures(uint8_t *src[], int stride[], int w,int h,int x,int y ) +{ + if (!d3d_lock_video_textures()) + return VO_FALSE; + + for (int n = 0; n < priv->plane_count; n++) { + struct texplane *plane = &priv->planes[n]; + + int dst_stride = plane->locked_rect.Pitch; + uint8_t *pdst = (uint8_t*)plane->locked_rect.pBits + + (y >> plane->shift_y) * dst_stride + + (x >> plane->shift_x) * plane->bytes_per_pixel; + + memcpy_pic(pdst, src[n], (w >> plane->shift_x) * plane->bytes_per_pixel, + h >> plane->shift_y, dst_stride, stride[n]); + } + + return 0; +} + /** @brief libvo Callback: Draw slice * @return 0 on success */ @@ -1059,6 +1615,9 @@ static int draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y ) if (!priv->d3d_device) return 0; + if (priv->use_textures) + return draw_slice_textures(src, stride, w, h, x, y); + /* Lock the offscreen surface if it's not already locked. */ if (!priv->locked_rect.pBits) { if (FAILED(IDirect3DSurface9_LockRect(priv->d3d_surface, @@ -1200,10 +1759,10 @@ static void draw_osd(void) if (priv->is_osd_populated) { vertex_osd osd_quad_vb[] = { - {-1.0f, 1.0f, 0.0f, 0, 0 }, - { 1.0f, 1.0f, 0.0f, 1, 0 }, - {-1.0f,-1.0f, 0.0f, 0, 1 }, - { 1.0f,-1.0f, 0.0f, 1, 1 } + { 0, 0, 0.0f, 0, 0 }, + { vo_dwidth, 0, 0.0f, 1, 0 }, + { 0, vo_dheight, 0.0f, 0, 1 }, + { vo_dwidth, vo_dheight, 0.0f, 1, 1 } }; /* calculate the texture coordinates */ @@ -1212,10 +1771,7 @@ static void draw_osd(void) osd_quad_vb[2].tv = osd_quad_vb[3].tv = (float)priv->texture_osd.h / priv->texture_osd.tex_h; - if (FAILED(IDirect3DDevice9_BeginScene(priv->d3d_device))) { - mp_msg(MSGT_VO,MSGL_ERR,"BeginScene failed.\n"); - return; - } + d3d_begin_scene(); /* turn on alpha test */ IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHABLENDENABLE, TRUE); @@ -1228,14 +1784,11 @@ static void draw_osd(void) IDirect3DDevice9_SetFVF(priv->d3d_device, D3DFVF_OSD_VERTEX); IDirect3DDevice9_DrawPrimitiveUP(priv->d3d_device, D3DPT_TRIANGLESTRIP, 2, osd_quad_vb, sizeof(vertex_osd)); + IDirect3DDevice9_SetTexture(priv->d3d_device, 0, NULL); + /* turn off alpha test */ IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHATESTENABLE, FALSE); IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHABLENDENABLE, FALSE); - - if (FAILED(IDirect3DDevice9_EndScene(priv->d3d_device))) { - mp_msg(MSGT_VO,MSGL_ERR,"EndScene failed.\n"); - return; - } } } @@ -1255,12 +1808,7 @@ static void d3d_realloc_eosd_texture(void) // we can always free it d3dtex_release(&priv->texture_eosd); - D3DFORMAT fmt = D3DFMT_A8; -#if !USE_A8 - fmt = D3DFMT_A8L8; -#endif - - d3dtex_allocate(&priv->texture_eosd, fmt, new_w, new_h); + d3dtex_allocate(&priv->texture_eosd, D3DFMT_A8, new_w, new_h); } static D3DCOLOR ass_to_d3d_color(uint32_t color) @@ -1315,22 +1863,10 @@ static void generate_eosd(mp_eosd_images_t *imgs) struct eosd_target *target = &priv->eosd->targets[i]; ASS_Image *img = target->ass_img; char *src = img->bitmap; -#if USE_A8 char *dst = (char*)locked_rect.pBits + target->source.x0 + locked_rect.Pitch * target->source.y0; -#else - char *dst = (char*)locked_rect.pBits + target->source.x0*2 - + locked_rect.Pitch * target->source.y0; -#endif for (int y = 0; y < img->h; y++) { -#if USE_A8 memcpy(dst, src, img->w); -#else - for (int x = 0; x < img->w; x++) { - dst[x*2+0] = 255; - dst[x*2+1] = src[x]; - } -#endif src += img->stride; dst += locked_rect.Pitch; } @@ -1363,27 +1899,6 @@ static void generate_eosd(mp_eosd_images_t *imgs) d3dtex_update(&priv->texture_eosd); } -// unfortunately we can't use the D3DX library - -static void d3d_matrix_identity(D3DMATRIX *m) -{ - memset(m, 0, sizeof(D3DMATRIX)); - m->_11 = m->_22 = m->_33 = m->_44 = 1.0f; -} - -static void d3d_matrix_ortho(D3DMATRIX *m, float left, float right, - float bottom, float top) -{ - d3d_matrix_identity(m); - m->_11 = 2.0f / (right - left); - m->_22 = 2.0f / (top - bottom); - m->_33 = 1.0f; - m->_41 = -(right + left) / (right - left); - m->_42 = -(top + bottom) / (top - bottom); - m->_43 = 0; - m->_44 = 1.0f; -} - static void draw_eosd(void) { // we can not render OSD if we lost the device e.g. because it was uncooperative @@ -1393,41 +1908,19 @@ static void draw_eosd(void) if (!priv->eosd->targets_count) return; - //xxx need to set up a transform for EOSD rendering when drawing it - if (FAILED(IDirect3DDevice9_BeginScene(priv->d3d_device))) { - mp_msg(MSGT_VO,MSGL_ERR,"BeginScene failed (EOSD).\n"); - return; - } + d3d_begin_scene(); IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHABLENDENABLE, TRUE); //IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHATESTENABLE, TRUE); - D3DMATRIX m; - // so that screen coordinates map to D3D ones - D3DVIEWPORT9 p; - IDirect3DDevice9_GetViewport(priv->d3d_device, &p); - d3d_matrix_ortho(&m, 0.5f, p.Width + 0.5f, p.Height + 0.5f, 0.5f); - IDirect3DDevice9_SetTransform(priv->d3d_device, D3DTS_VIEW, &m); - IDirect3DDevice9_SetTexture(priv->d3d_device, 0, d3dtex_get_render_texture(&priv->texture_eosd)); - IDirect3DDevice9_SetRenderState(priv->d3d_device, - D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); - IDirect3DDevice9_SetRenderState(priv->d3d_device, - D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); - -#if USE_A8 // do not use the color value from the A8 texture, because that is black - // we need either white, or no blending with the texture color at all - // use the value in D3DTSS_CONSTANT instead (0xffffffff=white by default) - // xxx wine doesn't like this (fails to compile the generated GL shader) - // and D3DTA_SPECULAR leaves the images black - IDirect3DDevice9_SetTextureStageState(priv->d3d_device, 0, - D3DTSS_ALPHAARG1, D3DTA_TEXTURE); + IDirect3DDevice9_SetRenderState(priv->d3d_device,D3DRS_TEXTUREFACTOR, + 0xFFFFFFFF); IDirect3DDevice9_SetTextureStageState(priv->d3d_device,0, - D3DTSS_COLORARG1, D3DTA_CONSTANT); -#endif + D3DTSS_COLORARG1, D3DTA_TFACTOR); IDirect3DDevice9_SetTextureStageState(priv->d3d_device, 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE); @@ -1437,18 +1930,13 @@ static void draw_eosd(void) priv->eosd->targets_count * 2, priv->eosd_vb, sizeof(vertex_eosd)); - d3d_matrix_identity(&m); - IDirect3DDevice9_SetTransform(priv->d3d_device, D3DTS_VIEW, &m); - + IDirect3DDevice9_SetTextureStageState(priv->d3d_device,0, + D3DTSS_COLORARG1, D3DTA_TEXTURE); IDirect3DDevice9_SetTextureStageState(priv->d3d_device, 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); + IDirect3DDevice9_SetTexture(priv->d3d_device, 0, NULL); + IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHABLENDENABLE, FALSE); //IDirect3DDevice9_SetRenderState(priv->d3d_device, D3DRS_ALPHATESTENABLE, FALSE); - - if (FAILED(IDirect3DDevice9_EndScene(priv->d3d_device))) { - mp_msg(MSGT_VO,MSGL_ERR,"EndScene failed (EOSD).\n"); - return; - } - }