(D3D) Merge big included files into d3d.cpp

This commit is contained in:
twinaphex 2014-09-13 08:06:01 +02:00
parent 4b9f85fe37
commit 25b60fc0ca
3 changed files with 967 additions and 970 deletions

View File

@ -45,7 +45,714 @@
#include "../../gfx/shader_hlsl.h"
#endif
#include "d3d_shared.h"
#include "d3d_defines.h"
#if defined(HAVE_CG) || defined(HAVE_GLSL) || defined(HAVE_HLSL)
#ifdef HAVE_HLSL
#include "../shader_hlsl.h"
#endif
#endif
/* forward declarations */
static void d3d_calculate_rect(d3d_video_t *d3d,
unsigned width, unsigned height,
bool keep, float desired_aspect);
static bool d3d_init_luts(d3d_video_t *d3d);
static void d3d_set_font_rect(d3d_video_t *d3d,
const struct font_params *params);
static bool d3d_process_shader(d3d_video_t *d3d);
static bool d3d_init_multipass(d3d_video_t *d3d);
static void d3d_deinit_chain(d3d_video_t *d3d);
static bool d3d_init_chain(d3d_video_t *d3d,
const video_info_t *video_info);
#ifdef HAVE_OVERLAY
static void d3d_free_overlays(void *data);
static void d3d_free_overlay(void *data, overlay_t *overlay);
#endif
#ifdef _XBOX
static void d3d_reinit_renderchain(void *data,
const video_info_t *video);
#endif
static void renderchain_free(void *data);
void d3d_make_d3dpp(void *data, const video_info_t *info,
D3DPRESENT_PARAMETERS *d3dpp);
#ifdef HAVE_WINDOW
#define IDI_ICON 1
#define MAX_MONITORS 9
extern LRESULT CALLBACK WindowProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
static RECT d3d_monitor_rect(d3d_video_t *d3d);
#endif
#ifdef HAVE_MONITOR
namespace Monitor
{
static HMONITOR last_hm;
static HMONITOR all_hms[MAX_MONITORS];
static unsigned num_mons;
}
#endif
static void d3d_deinit_shader(void *data)
{
d3d_video_t *d3d = (d3d_video_t*)data;
(void)d3d;
(void)data;
#ifdef HAVE_CG
if (!d3d->cgCtx)
return;
cgD3D9UnloadAllPrograms();
cgD3D9SetDevice(NULL);
cgDestroyContext(d3d->cgCtx);
d3d->cgCtx = NULL;
#endif
}
static bool d3d_init_shader(void *data)
{
d3d_video_t *d3d = (d3d_video_t*)data;
(void)d3d;
(void)data;
#if defined(HAVE_HLSL)
RARCH_LOG("D3D]: Using HLSL shader backend.\n");
const gl_shader_backend_t *backend = &hlsl_backend;
const char *shader_path = g_settings.video.shader_path;
d3d->shader = backend;
if (!d3d->shader)
return false;
return d3d->shader->init(d3d, shader_path);
#elif defined(HAVE_CG)
d3d->cgCtx = cgCreateContext();
if (!d3d->cgCtx)
return false;
RARCH_LOG("[D3D]: Created shader context.\n");
HRESULT ret = cgD3D9SetDevice(d3d->dev);
if (FAILED(ret))
return false;
return true;
#elif defined(_XBOX1)
return false;
#endif
}
static void d3d_deinit_chain(d3d_video_t *d3d)
{
#ifdef _XBOX
renderchain_free(d3d);
#else
if (d3d->chain)
delete (renderchain_t *)d3d->chain;
d3d->chain = NULL;
#endif
}
static void d3d_deinitialize(void *data)
{
d3d_video_t *d3d = (d3d_video_t*)data;
if (d3d->font_ctx && d3d->font_ctx->deinit)
d3d->font_ctx->deinit(d3d);
d3d->font_ctx = NULL;
d3d_deinit_chain(d3d);
#ifdef HAVE_SHADERS
d3d_deinit_shader(d3d);
#endif
#ifndef _XBOX
d3d->needs_restore = false;
#endif
}
static bool d3d_init_base(void *data, const video_info_t *info)
{
d3d_video_t *d3d = (d3d_video_t*)data;
D3DPRESENT_PARAMETERS d3dpp;
d3d_make_d3dpp(d3d, info, &d3dpp);
d3d->g_pD3D = D3DCREATE_CTX(D3D_SDK_VERSION);
if (!d3d->g_pD3D)
{
RARCH_ERR("Failed to create D3D interface.\n");
return false;
}
if (FAILED(d3d->d3d_err = d3d->g_pD3D->CreateDevice(
d3d->cur_mon_id,
D3DDEVTYPE_HAL,
d3d->hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp,
&d3d->dev)))
{
RARCH_WARN("[D3D]: Failed to init device with hardware vertex processing (code: 0x%x). Trying to fall back to software vertex processing.\n",
(unsigned)d3d->d3d_err);
if (FAILED(d3d->d3d_err = d3d->g_pD3D->CreateDevice(
d3d->cur_mon_id,
D3DDEVTYPE_HAL,
d3d->hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3d->dev)))
{
RARCH_ERR("Failed to initialize device.\n");
return false;
}
}
return true;
}
static bool d3d_initialize(void *data, const video_info_t *info)
{
d3d_video_t *d3d = (d3d_video_t*)data;
bool ret = true;
if (!d3d->g_pD3D)
ret = d3d_init_base(d3d, info);
else if (d3d->needs_restore)
{
D3DPRESENT_PARAMETERS d3dpp;
d3d_make_d3dpp(d3d, info, &d3dpp);
if (d3d->dev->Reset(&d3dpp) != D3D_OK)
{
// Try to recreate the device completely ..
#ifndef _XBOX
HRESULT res = d3d->dev->TestCooperativeLevel();
const char *err;
switch (res)
{
case D3DERR_DEVICELOST:
err = "DEVICELOST";
break;
case D3DERR_DEVICENOTRESET:
err = "DEVICENOTRESET";
break;
case D3DERR_DRIVERINTERNALERROR:
err = "DRIVERINTERNALERROR";
break;
default:
err = "Unknown";
}
RARCH_WARN(
"[D3D]: Attempting to recover from dead state (%s).\n", err);
#else
RARCH_WARN("[D3D]: Attempting to recover from dead state.\n");
#endif
d3d_deinitialize(d3d);
d3d->g_pD3D->Release();
d3d->g_pD3D = NULL;
ret = d3d_init_base(d3d, info);
if (ret)
RARCH_LOG("[D3D]: Recovered from dead state.\n");
else
return ret;
}
}
if (!ret)
return ret;
d3d_calculate_rect(d3d, d3d->screen_width, d3d->screen_height,
info->force_aspect, g_extern.system.aspect_ratio);
#ifdef HAVE_SHADERS
if (!d3d_init_shader(d3d))
{
RARCH_ERR("Failed to initialize shader subsystem.\n");
return false;
}
#endif
if (!d3d_init_chain(d3d, info))
{
RARCH_ERR("Failed to initialize render chain.\n");
return false;
}
#if defined(_XBOX360)
strlcpy(g_settings.video.font_path, "game:\\media\\Arial_12.xpr",
sizeof(g_settings.video.font_path));
#endif
d3d->font_ctx = d3d_font_init_first(d3d, g_settings.video.font_path, 0);
if (!d3d->font_ctx)
{
RARCH_ERR("Failed to initialize font.\n");
return false;
}
return true;
}
static void d3d_set_viewport(d3d_video_t *d3d, int x, int y,
unsigned width, unsigned height)
{
D3DVIEWPORT viewport;
/* D3D doesn't support negative X/Y viewports ... */
if (x < 0)
x = 0;
if (y < 0)
y = 0;
viewport.X = x;
viewport.Y = y;
viewport.Width = width;
viewport.Height = height;
viewport.MinZ = 0.0f;
viewport.MaxZ = 1.0f;
d3d->final_viewport = viewport;
#ifndef _XBOX
d3d_set_font_rect(d3d, NULL);
#endif
}
bool d3d_restore(d3d_video_t *d3d)
{
d3d_deinitialize(d3d);
d3d->needs_restore = !d3d_initialize(d3d, &d3d->video_info);
if (d3d->needs_restore)
RARCH_ERR("[D3D]: Restore error.\n");
return !d3d->needs_restore;
}
static void d3d_calculate_rect(d3d_video_t *d3d,
unsigned width, unsigned height,
bool keep, float desired_aspect)
{
if (g_settings.video.scale_integer)
{
struct rarch_viewport vp = {0};
gfx_scale_integer(&vp, width, height, desired_aspect, keep);
d3d_set_viewport(d3d, vp.x, vp.y, vp.width, vp.height);
}
else if (!keep)
d3d_set_viewport(d3d, 0, 0, width, height);
else
{
if (g_settings.video.aspect_ratio_idx == ASPECT_RATIO_CUSTOM)
{
const rarch_viewport_t &custom =
g_extern.console.screen.viewports.custom_vp;
d3d_set_viewport(d3d, custom.x, custom.y,
custom.width, custom.height);
}
else
{
float device_aspect = static_cast<float>(width) /
static_cast<float>(height);
if (fabsf(device_aspect - desired_aspect) < 0.0001f)
d3d_set_viewport(d3d, 0, 0, width, height);
else if (device_aspect > desired_aspect)
{
float delta = (desired_aspect / device_aspect - 1.0f) / 2.0f + 0.5f;
d3d_set_viewport(d3d, int(roundf(width * (0.5f - delta))),
0, unsigned(roundf(2.0f * width * delta)), height);
}
else
{
float delta = (device_aspect / desired_aspect - 1.0f) / 2.0f + 0.5f;
d3d_set_viewport(d3d, 0, int(roundf(height * (0.5f - delta))),
width, unsigned(roundf(2.0f * height * delta)));
}
}
}
}
static void d3d_set_nonblock_state(void *data, bool state)
{
d3d_video_t *d3d = (d3d_video_t*)data;
d3d->video_info.vsync = !state;
if (d3d->ctx_driver && d3d->ctx_driver->swap_interval)
d3d->ctx_driver->swap_interval(d3d, state ? 0 : 1);
}
static bool d3d_alive(void *data)
{
d3d_video_t *d3d = (d3d_video_t*)data;
bool quit, resize;
quit = false;
resize = false;
if (d3d->ctx_driver && d3d->ctx_driver->check_window)
d3d->ctx_driver->check_window(d3d, &quit, &resize,
&d3d->screen_width, &d3d->screen_height, g_extern.frame_count);
if (quit)
d3d->quitting = quit;
else if (resize)
d3d->should_resize = true;
return !quit;
}
static bool d3d_focus(void *data)
{
d3d_video_t *d3d = (d3d_video_t*)data;
if (d3d && d3d->ctx_driver && d3d->ctx_driver->has_focus)
return d3d->ctx_driver->has_focus(d3d);
return false;
}
static void d3d_set_aspect_ratio(void *data, unsigned aspect_ratio_idx)
{
d3d_video_t *d3d = (d3d_video_t*)data;
switch (aspect_ratio_idx)
{
case ASPECT_RATIO_SQUARE:
gfx_set_square_pixel_viewport(
g_extern.system.av_info.geometry.base_width,
g_extern.system.av_info.geometry.base_height);
break;
case ASPECT_RATIO_CORE:
gfx_set_core_viewport();
break;
case ASPECT_RATIO_CONFIG:
gfx_set_config_viewport();
break;
default:
break;
}
g_extern.system.aspect_ratio = aspectratio_lut[aspect_ratio_idx].value;
d3d->video_info.force_aspect = true;
d3d->should_resize = true;
}
static void d3d_apply_state_changes(void *data)
{
d3d_video_t *d3d = (d3d_video_t*)data;
d3d->should_resize = true;
}
static void d3d_set_osd_msg(void *data, const char *msg,
const struct font_params *params)
{
d3d_video_t *d3d = (d3d_video_t*)data;
#ifndef _XBOX
if (params)
d3d_set_font_rect(d3d, params);
#endif
if (d3d && d3d->font_ctx && d3d->font_ctx->render_msg)
d3d->font_ctx->render_msg(d3d, msg, params);
}
/* Delay constructor due to lack of exceptions. */
static bool d3d_construct(d3d_video_t *d3d,
const video_info_t *info, const input_driver_t **input,
void **input_data)
{
d3d->should_resize = false;
#ifndef _XBOX
gfx_set_dwm();
#endif
#if defined(HAVE_MENU) && defined(HAVE_OVERLAY)
if (d3d->menu)
free(d3d->menu);
d3d->menu = (overlay_t*)calloc(1, sizeof(overlay_t));
if (!d3d->menu)
return false;
d3d->menu->tex_coords.x = 0;
d3d->menu->tex_coords.y = 0;
d3d->menu->tex_coords.w = 1;
d3d->menu->tex_coords.h = 1;
d3d->menu->vert_coords.x = 0;
d3d->menu->vert_coords.y = 1;
d3d->menu->vert_coords.w = 1;
d3d->menu->vert_coords.h = -1;
#endif
#if defined(HAVE_WINDOW) && !defined(_XBOX)
memset(&d3d->windowClass, 0, sizeof(d3d->windowClass));
d3d->windowClass.cbSize = sizeof(d3d->windowClass);
d3d->windowClass.style = CS_HREDRAW | CS_VREDRAW;
d3d->windowClass.lpfnWndProc = WindowProc;
d3d->windowClass.hInstance = NULL;
d3d->windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
d3d->windowClass.lpszClassName = "RetroArch";
d3d->windowClass.hIcon = LoadIcon(GetModuleHandle(NULL),
MAKEINTRESOURCE(IDI_ICON));
d3d->windowClass.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL),
MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 16, 16, 0);
if (!info->fullscreen)
d3d->windowClass.hbrBackground = (HBRUSH)COLOR_WINDOW;
RegisterClassEx(&d3d->windowClass);
#endif
unsigned full_x, full_y;
#ifdef HAVE_MONITOR
RECT mon_rect = d3d_monitor_rect(d3d);
bool windowed_full = g_settings.video.windowed_fullscreen;
full_x = (windowed_full || info->width == 0) ?
(mon_rect.right - mon_rect.left) : info->width;
full_y = (windowed_full || info->height == 0) ?
(mon_rect.bottom - mon_rect.top) : info->height;
RARCH_LOG("[D3D]: Monitor size: %dx%d.\n",
(int)(mon_rect.right - mon_rect.left),
(int)(mon_rect.bottom - mon_rect.top));
#else
if (d3d->ctx_driver && d3d->ctx_driver->get_video_size)
d3d->ctx_driver->get_video_size(d3d, &full_x, &full_y);
#endif
d3d->screen_width = info->fullscreen ? full_x : info->width;
d3d->screen_height = info->fullscreen ? full_y : info->height;
#if defined(HAVE_WINDOW) && !defined(_XBOX)
unsigned win_width = d3d->screen_width;
unsigned win_height = d3d->screen_height;
if (!info->fullscreen)
{
RECT rect = {0};
rect.right = d3d->screen_width;
rect.bottom = d3d->screen_height;
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
win_width = rect.right - rect.left;
win_height = rect.bottom - rect.top;
}
char buffer[128];
gfx_get_fps(buffer, sizeof(buffer), NULL, 0);
std::string title = buffer;
title += " || Direct3D";
d3d->hWnd = CreateWindowEx(0, "RetroArch", title.c_str(),
info->fullscreen ?
(WS_EX_TOPMOST | WS_POPUP) : WS_OVERLAPPEDWINDOW,
info->fullscreen ? mon_rect.left : CW_USEDEFAULT,
info->fullscreen ? mon_rect.top : CW_USEDEFAULT,
win_width, win_height,
NULL, NULL, NULL, d3d);
driver.display_type = RARCH_DISPLAY_WIN32;
driver.video_display = 0;
driver.video_window = (uintptr_t)d3d->hWnd;
#endif
if (d3d && d3d->ctx_driver && d3d->ctx_driver->show_mouse)
d3d->ctx_driver->show_mouse(d3d, !info->fullscreen
#ifdef HAVE_OVERLAY
|| d3d->overlays_enabled
#endif
);
#if defined(HAVE_WINDOW) && !defined(_XBOX)
ShowWindow(d3d->hWnd, SW_RESTORE);
UpdateWindow(d3d->hWnd);
SetForegroundWindow(d3d->hWnd);
SetFocus(d3d->hWnd);
#endif
#ifndef _XBOX
#ifdef HAVE_SHADERS
/* This should only be done once here
* to avoid set_shader() to be overridden
* later. */
enum rarch_shader_type type =
gfx_shader_parse_type(g_settings.video.shader_path, RARCH_SHADER_NONE);
if (g_settings.video.shader_enable && type == RARCH_SHADER_CG)
d3d->cg_shader = g_settings.video.shader_path;
if (!d3d_process_shader(d3d))
return false;
#endif
#endif
d3d->video_info = *info;
if (!d3d_initialize(d3d, &d3d->video_info))
return false;
if (input && input_data &&
d3d->ctx_driver && d3d->ctx_driver->input_driver)
d3d->ctx_driver->input_driver(d3d, input, input_data);
RARCH_LOG("[D3D]: Init complete.\n");
return true;
}
static void d3d_viewport_info(void *data, struct rarch_viewport *vp)
{
d3d_video_t *d3d = (d3d_video_t*)data;
vp->x = d3d->final_viewport.X;
vp->y = d3d->final_viewport.Y;
vp->width = d3d->final_viewport.Width;
vp->height = d3d->final_viewport.Height;
vp->full_width = d3d->screen_width;
vp->full_height = d3d->screen_height;
}
static void d3d_set_rotation(void *data, unsigned rot)
{
d3d_video_t *d3d = (d3d_video_t*)data;
d3d->dev_rotation = rot;
}
static void d3d_show_mouse(void *data, bool state)
{
d3d_video_t *d3d = (d3d_video_t*)data;
if (d3d && d3d->ctx_driver && d3d->ctx_driver->show_mouse)
d3d->ctx_driver->show_mouse(d3d, state);
}
static const gfx_ctx_driver_t *d3d_get_context(void *data)
{
/* Default to Direct3D9 for now.
TODO: GL core contexts through ANGLE? */
enum gfx_ctx_api api = GFX_CTX_DIRECT3D9_API;
unsigned major = 9, minor = 0;
(void)major;
(void)minor;
#if defined(_XBOX1)
api = GFX_CTX_DIRECT3D8_API;
major = 8;
#endif
return gfx_ctx_init_first(driver.video_data, api,
major, minor, false);
}
static void *d3d_init(const video_info_t *info,
const input_driver_t **input, void **input_data)
{
#ifdef _XBOX
if (driver.video_data)
{
d3d_video_t *vid = (d3d_video_t*)driver.video_data;
/* Reinitialize renderchain as we
* might have changed pixel formats.*/
d3d_reinit_renderchain(vid, info);
if (input && input_data)
{
*input = driver.input;
*input_data = driver.input_data;
}
driver.video_data_own = true;
driver.input_data_own = true;
return driver.video_data;
}
#endif
d3d_video_t *vid = new d3d_video_t();
if (!vid)
return NULL;
vid->ctx_driver = d3d_get_context(vid);
if (!vid->ctx_driver)
{
delete vid;
return NULL;
}
/* Default values */
vid->g_pD3D = NULL;
vid->dev = NULL;
#ifndef _XBOX
vid->font = NULL;
#endif
vid->dev_rotation = 0;
vid->needs_restore = false;
#ifdef HAVE_CG
vid->cgCtx = NULL;
#endif
#ifdef HAVE_OVERLAY
vid->overlays_enabled = false;
#endif
#ifdef _XBOX
vid->should_resize = false;
vid->vsync = info->vsync;
#else
vid->menu = NULL;
#endif
if (!d3d_construct(vid, info, input, input_data))
{
RARCH_ERR("[D3D]: Failed to init D3D.\n");
delete vid;
return NULL;
}
#ifdef _XBOX
driver.video_data_own = true;
driver.input_data_own = true;
#endif
return vid;
}
static void d3d_free(void *data)
{
d3d_video_t *d3d = (d3d_video_t*)data;
d3d_deinitialize(d3d);
#ifdef HAVE_OVERLAY
d3d_free_overlays(d3d);
#endif
#if defined(HAVE_MENU) && !defined(_XBOX)
d3d_free_overlay(d3d, d3d->menu);
#endif
#ifdef _XBOX
if (d3d->ctx_driver && d3d->ctx_driver->destroy)
d3d->ctx_driver->destroy(d3d);
d3d->ctx_driver = NULL;
#endif
if (d3d->dev)
d3d->dev->Release();
if (d3d->g_pD3D)
d3d->g_pD3D->Release();
#ifdef HAVE_MONITOR
Monitor::last_hm = MonitorFromWindow(d3d->hWnd,
MONITOR_DEFAULTTONEAREST);
DestroyWindow(d3d->hWnd);
#endif
if (d3d)
delete d3d;
#ifndef _XBOX
UnregisterClass("RetroArch", GetModuleHandle(NULL));
#endif
}
#ifdef _XBOX
#include "../../xdk/xdk_resources.h"
@ -514,7 +1221,265 @@ static bool d3d_init_luts(d3d_video_t *d3d)
#endif
#ifdef HAVE_OVERLAY
#include "d3d_overlays.cpp"
static void d3d_overlay_render(void *data, overlay_t *overlay)
{
void *verts;
unsigned i;
d3d_video_t *d3d = (d3d_video_t*)data;
if (!overlay || !overlay->tex)
return;
struct overlay_vertex
{
float x, y, z;
float u, v;
float r, g, b, a;
} vert[4];
if (!overlay->vert_buf)
{
D3DDevice_CreateVertexBuffers(d3d->dev, sizeof(vert),
d3d->dev->GetSoftwareVertexProcessing() ?
D3DUSAGE_SOFTWAREPROCESSING : 0, 0, D3DPOOL_MANAGED,
&overlay->vert_buf, NULL);
}
for (i = 0; i < 4; i++)
{
vert[i].z = 0.5f;
vert[i].r = vert[i].g = vert[i].b = 1.0f;
vert[i].a = overlay->alpha_mod;
}
float overlay_width = d3d->final_viewport.Width;
float overlay_height = d3d->final_viewport.Height;
vert[0].x = overlay->vert_coords.x * overlay_width;
vert[1].x = (overlay->vert_coords.x + overlay->vert_coords.w)
* overlay_width;
vert[2].x = overlay->vert_coords.x * overlay_width;
vert[3].x = (overlay->vert_coords.x + overlay->vert_coords.w)
* overlay_width;
vert[0].y = overlay->vert_coords.y * overlay_height;
vert[1].y = overlay->vert_coords.y * overlay_height;
vert[2].y = (overlay->vert_coords.y + overlay->vert_coords.h)
* overlay_height;
vert[3].y = (overlay->vert_coords.y + overlay->vert_coords.h)
* overlay_height;
vert[0].u = overlay->tex_coords.x;
vert[1].u = overlay->tex_coords.x + overlay->tex_coords.w;
vert[2].u = overlay->tex_coords.x;
vert[3].u = overlay->tex_coords.x + overlay->tex_coords.w;
vert[0].v = overlay->tex_coords.y;
vert[1].v = overlay->tex_coords.y;
vert[2].v = overlay->tex_coords.y + overlay->tex_coords.h;
vert[3].v = overlay->tex_coords.y + overlay->tex_coords.h;
/* Align texels and vertices. */
for (i = 0; i < 4; i++)
{
vert[i].x -= 0.5f;
vert[i].y += 0.5f;
}
overlay->vert_buf->Lock(0, sizeof(vert), &verts, 0);
memcpy(verts, vert, sizeof(vert));
overlay->vert_buf->Unlock();
// enable alpha
d3d->dev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
d3d->dev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
d3d->dev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
#ifndef _XBOX1
// set vertex decl for overlay
D3DVERTEXELEMENT vElems[4] = {
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT,
D3DDECLUSAGE_POSITION, 0},
{0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT,
D3DDECLUSAGE_TEXCOORD, 0},
{0, 20, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT,
D3DDECLUSAGE_COLOR, 0},
D3DDECL_END()
};
LPDIRECT3DVERTEXDECLARATION vertex_decl;
d3d->dev->CreateVertexDeclaration(vElems, &vertex_decl);
d3d->dev->SetVertexDeclaration(vertex_decl);
vertex_decl->Release();
#endif
d3d_set_stream_source(d3d->dev, 0, overlay->vert_buf,
0, sizeof(overlay_vertex));
if (overlay->fullscreen)
{
/* Set viewport to full window. */
D3DVIEWPORT vp_full;
vp_full.X = 0;
vp_full.Y = 0;
vp_full.Width = d3d->screen_width;
vp_full.Height = d3d->screen_height;
vp_full.MinZ = 0.0f;
vp_full.MaxZ = 1.0f;
d3d_set_viewport(d3d->dev, &vp_full);
}
/* Render overlay. */
d3d_set_texture(d3d->dev, 0, overlay->tex);
d3d_set_sampler_address_u(d3d->dev, 0, D3DTADDRESS_BORDER);
d3d_set_sampler_address_v(d3d->dev, 0, D3DTADDRESS_BORDER);
d3d_set_sampler_minfilter(d3d->dev, 0, D3DTEXF_LINEAR);
d3d_set_sampler_magfilter(d3d->dev, 0, D3DTEXF_LINEAR);
d3d_draw_primitive(d3d->dev, D3DPT_TRIANGLESTRIP, 0, 2);
/* Restore previous state. */
d3d->dev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
d3d_set_viewport(d3d->dev, &d3d->final_viewport);
}
static void d3d_free_overlay(void *data, overlay_t *overlay)
{
d3d_video_t *d3d = (d3d_video_t*)data;
if (overlay->tex)
overlay->tex->Release();
if (overlay->vert_buf)
overlay->vert_buf->Release();
}
static void d3d_free_overlays(void *data)
{
unsigned i;
d3d_video_t *d3d = (d3d_video_t*)data;
for (i = 0; i < d3d->overlays.size(); i++)
d3d_free_overlay(d3d, &d3d->overlays[i]);
d3d->overlays.clear();
}
static void d3d_overlay_tex_geom(void *data,
unsigned index,
float x, float y,
float w, float h)
{
d3d_video_t *d3d = (d3d_video_t*)data;
d3d->overlays[index].tex_coords.x = x;
d3d->overlays[index].tex_coords.y = y;
d3d->overlays[index].tex_coords.w = w;
d3d->overlays[index].tex_coords.h = h;
}
static void d3d_overlay_vertex_geom(void *data,
unsigned index,
float x, float y,
float w, float h)
{
d3d_video_t *d3d = (d3d_video_t*)data;
y = 1.0f - y;
h = -h;
d3d->overlays[index].vert_coords.x = x;
d3d->overlays[index].vert_coords.y = y;
d3d->overlays[index].vert_coords.w = w;
d3d->overlays[index].vert_coords.h = h;
}
static bool d3d_overlay_load(void *data,
const texture_image *images, unsigned num_images)
{
unsigned i, y;
d3d_video_t *d3d = (d3d_video_t*)data;
d3d_free_overlays(data);
d3d->overlays.resize(num_images);
for (i = 0; i < num_images; i++)
{
unsigned width = images[i].width;
unsigned height = images[i].height;
overlay_t &overlay = d3d->overlays[i];
if (FAILED(d3d->dev->CreateTexture(
width,
height,
1,
0,
D3DFMT_A8R8G8B8,
D3DPOOL_MANAGED,
&overlay.tex, NULL)))
{
RARCH_ERR("[D3D]: Failed to create overlay texture\n");
return false;
}
D3DLOCKED_RECT d3dlr;
if (SUCCEEDED(overlay.tex->LockRect(0, &d3dlr,
NULL, D3DLOCK_NOSYSLOCK)))
{
uint32_t *dst = static_cast<uint32_t*>(d3dlr.pBits);
const uint32_t *src = images[i].pixels;
unsigned pitch = d3dlr.Pitch >> 2;
for (y = 0; y < height; y++, dst += pitch, src += width)
memcpy(dst, src, width << 2);
overlay.tex->UnlockRect(0);
}
overlay.tex_w = width;
overlay.tex_h = height;
/* Default. Stretch to whole screen. */
d3d_overlay_tex_geom(d3d, i, 0, 0, 1, 1);
d3d_overlay_vertex_geom(d3d, i, 0, 0, 1, 1);
}
return true;
}
static void d3d_overlay_enable(void *data, bool state)
{
unsigned i;
d3d_video_t *d3d = (d3d_video_t*)data;
for (i = 0; i < d3d->overlays.size(); i++)
d3d->overlays_enabled = state;
if (d3d && d3d->ctx_driver && d3d->ctx_driver->show_mouse)
d3d->ctx_driver->show_mouse(d3d, state);
}
static void d3d_overlay_full_screen(void *data, bool enable)
{
unsigned i;
d3d_video_t *d3d = (d3d_video_t*)data;
for (i = 0; i < d3d->overlays.size(); i++)
d3d->overlays[i].fullscreen = enable;
}
static void d3d_overlay_set_alpha(void *data, unsigned index, float mod)
{
d3d_video_t *d3d = (d3d_video_t*)data;
d3d->overlays[index].alpha_mod = mod;
}
static const video_overlay_interface_t d3d_overlay_interface = {
d3d_overlay_enable,
d3d_overlay_load,
d3d_overlay_tex_geom,
d3d_overlay_vertex_geom,
d3d_overlay_full_screen,
d3d_overlay_set_alpha,
};
static void d3d_get_overlay_interface(void *data,
const video_overlay_interface_t **iface)
{
(void)data;
*iface = &d3d_overlay_interface;
}
#endif
static bool d3d_frame(void *data, const void *frame,

View File

@ -1,259 +0,0 @@
static void d3d_overlay_render(void *data, overlay_t *overlay)
{
void *verts;
unsigned i;
d3d_video_t *d3d = (d3d_video_t*)data;
if (!overlay || !overlay->tex)
return;
struct overlay_vertex
{
float x, y, z;
float u, v;
float r, g, b, a;
} vert[4];
if (!overlay->vert_buf)
{
D3DDevice_CreateVertexBuffers(d3d->dev, sizeof(vert),
d3d->dev->GetSoftwareVertexProcessing() ?
D3DUSAGE_SOFTWAREPROCESSING : 0, 0, D3DPOOL_MANAGED,
&overlay->vert_buf, NULL);
}
for (i = 0; i < 4; i++)
{
vert[i].z = 0.5f;
vert[i].r = vert[i].g = vert[i].b = 1.0f;
vert[i].a = overlay->alpha_mod;
}
float overlay_width = d3d->final_viewport.Width;
float overlay_height = d3d->final_viewport.Height;
vert[0].x = overlay->vert_coords.x * overlay_width;
vert[1].x = (overlay->vert_coords.x + overlay->vert_coords.w)
* overlay_width;
vert[2].x = overlay->vert_coords.x * overlay_width;
vert[3].x = (overlay->vert_coords.x + overlay->vert_coords.w)
* overlay_width;
vert[0].y = overlay->vert_coords.y * overlay_height;
vert[1].y = overlay->vert_coords.y * overlay_height;
vert[2].y = (overlay->vert_coords.y + overlay->vert_coords.h)
* overlay_height;
vert[3].y = (overlay->vert_coords.y + overlay->vert_coords.h)
* overlay_height;
vert[0].u = overlay->tex_coords.x;
vert[1].u = overlay->tex_coords.x + overlay->tex_coords.w;
vert[2].u = overlay->tex_coords.x;
vert[3].u = overlay->tex_coords.x + overlay->tex_coords.w;
vert[0].v = overlay->tex_coords.y;
vert[1].v = overlay->tex_coords.y;
vert[2].v = overlay->tex_coords.y + overlay->tex_coords.h;
vert[3].v = overlay->tex_coords.y + overlay->tex_coords.h;
/* Align texels and vertices. */
for (i = 0; i < 4; i++)
{
vert[i].x -= 0.5f;
vert[i].y += 0.5f;
}
overlay->vert_buf->Lock(0, sizeof(vert), &verts, 0);
memcpy(verts, vert, sizeof(vert));
overlay->vert_buf->Unlock();
// enable alpha
d3d->dev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
d3d->dev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
d3d->dev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
#ifndef _XBOX1
// set vertex decl for overlay
D3DVERTEXELEMENT vElems[4] = {
{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT,
D3DDECLUSAGE_POSITION, 0},
{0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT,
D3DDECLUSAGE_TEXCOORD, 0},
{0, 20, D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT,
D3DDECLUSAGE_COLOR, 0},
D3DDECL_END()
};
LPDIRECT3DVERTEXDECLARATION vertex_decl;
d3d->dev->CreateVertexDeclaration(vElems, &vertex_decl);
d3d->dev->SetVertexDeclaration(vertex_decl);
vertex_decl->Release();
#endif
d3d_set_stream_source(d3d->dev, 0, overlay->vert_buf,
0, sizeof(overlay_vertex));
if (overlay->fullscreen)
{
/* Set viewport to full window. */
D3DVIEWPORT vp_full;
vp_full.X = 0;
vp_full.Y = 0;
vp_full.Width = d3d->screen_width;
vp_full.Height = d3d->screen_height;
vp_full.MinZ = 0.0f;
vp_full.MaxZ = 1.0f;
d3d_set_viewport(d3d->dev, &vp_full);
}
/* Render overlay. */
d3d_set_texture(d3d->dev, 0, overlay->tex);
d3d_set_sampler_address_u(d3d->dev, 0, D3DTADDRESS_BORDER);
d3d_set_sampler_address_v(d3d->dev, 0, D3DTADDRESS_BORDER);
d3d_set_sampler_minfilter(d3d->dev, 0, D3DTEXF_LINEAR);
d3d_set_sampler_magfilter(d3d->dev, 0, D3DTEXF_LINEAR);
d3d_draw_primitive(d3d->dev, D3DPT_TRIANGLESTRIP, 0, 2);
/* Restore previous state. */
d3d->dev->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
d3d_set_viewport(d3d->dev, &d3d->final_viewport);
}
static void d3d_free_overlay(void *data, overlay_t *overlay)
{
d3d_video_t *d3d = (d3d_video_t*)data;
if (overlay->tex)
overlay->tex->Release();
if (overlay->vert_buf)
overlay->vert_buf->Release();
}
static void d3d_free_overlays(void *data)
{
unsigned i;
d3d_video_t *d3d = (d3d_video_t*)data;
for (i = 0; i < d3d->overlays.size(); i++)
d3d_free_overlay(d3d, &d3d->overlays[i]);
d3d->overlays.clear();
}
static void d3d_overlay_tex_geom(void *data,
unsigned index,
float x, float y,
float w, float h)
{
d3d_video_t *d3d = (d3d_video_t*)data;
d3d->overlays[index].tex_coords.x = x;
d3d->overlays[index].tex_coords.y = y;
d3d->overlays[index].tex_coords.w = w;
d3d->overlays[index].tex_coords.h = h;
}
static void d3d_overlay_vertex_geom(void *data,
unsigned index,
float x, float y,
float w, float h)
{
d3d_video_t *d3d = (d3d_video_t*)data;
y = 1.0f - y;
h = -h;
d3d->overlays[index].vert_coords.x = x;
d3d->overlays[index].vert_coords.y = y;
d3d->overlays[index].vert_coords.w = w;
d3d->overlays[index].vert_coords.h = h;
}
static bool d3d_overlay_load(void *data,
const texture_image *images, unsigned num_images)
{
unsigned i, y;
d3d_video_t *d3d = (d3d_video_t*)data;
d3d_free_overlays(data);
d3d->overlays.resize(num_images);
for (i = 0; i < num_images; i++)
{
unsigned width = images[i].width;
unsigned height = images[i].height;
overlay_t &overlay = d3d->overlays[i];
if (FAILED(d3d->dev->CreateTexture(
width,
height,
1,
0,
D3DFMT_A8R8G8B8,
D3DPOOL_MANAGED,
&overlay.tex, NULL)))
{
RARCH_ERR("[D3D]: Failed to create overlay texture\n");
return false;
}
D3DLOCKED_RECT d3dlr;
if (SUCCEEDED(overlay.tex->LockRect(0, &d3dlr,
NULL, D3DLOCK_NOSYSLOCK)))
{
uint32_t *dst = static_cast<uint32_t*>(d3dlr.pBits);
const uint32_t *src = images[i].pixels;
unsigned pitch = d3dlr.Pitch >> 2;
for (y = 0; y < height; y++, dst += pitch, src += width)
memcpy(dst, src, width << 2);
overlay.tex->UnlockRect(0);
}
overlay.tex_w = width;
overlay.tex_h = height;
/* Default. Stretch to whole screen. */
d3d_overlay_tex_geom(d3d, i, 0, 0, 1, 1);
d3d_overlay_vertex_geom(d3d, i, 0, 0, 1, 1);
}
return true;
}
static void d3d_overlay_enable(void *data, bool state)
{
unsigned i;
d3d_video_t *d3d = (d3d_video_t*)data;
for (i = 0; i < d3d->overlays.size(); i++)
d3d->overlays_enabled = state;
if (d3d && d3d->ctx_driver && d3d->ctx_driver->show_mouse)
d3d->ctx_driver->show_mouse(d3d, state);
}
static void d3d_overlay_full_screen(void *data, bool enable)
{
unsigned i;
d3d_video_t *d3d = (d3d_video_t*)data;
for (i = 0; i < d3d->overlays.size(); i++)
d3d->overlays[i].fullscreen = enable;
}
static void d3d_overlay_set_alpha(void *data, unsigned index, float mod)
{
d3d_video_t *d3d = (d3d_video_t*)data;
d3d->overlays[index].alpha_mod = mod;
}
static const video_overlay_interface_t d3d_overlay_interface = {
d3d_overlay_enable,
d3d_overlay_load,
d3d_overlay_tex_geom,
d3d_overlay_vertex_geom,
d3d_overlay_full_screen,
d3d_overlay_set_alpha,
};
static void d3d_get_overlay_interface(void *data,
const video_overlay_interface_t **iface)
{
(void)data;
*iface = &d3d_overlay_interface;
}

View File

@ -1,709 +0,0 @@
#include "d3d_defines.h"
#include "../gfx_common.h"
#if defined(HAVE_CG) || defined(HAVE_GLSL) || defined(HAVE_HLSL)
#ifdef HAVE_HLSL
#include "../shader_hlsl.h"
#endif
#endif
/* forward declarations */
static void d3d_calculate_rect(d3d_video_t *d3d,
unsigned width, unsigned height,
bool keep, float desired_aspect);
static bool d3d_init_luts(d3d_video_t *d3d);
static void d3d_set_font_rect(d3d_video_t *d3d,
const struct font_params *params);
static bool d3d_process_shader(d3d_video_t *d3d);
static bool d3d_init_multipass(d3d_video_t *d3d);
static void d3d_deinit_chain(d3d_video_t *d3d);
static bool d3d_init_chain(d3d_video_t *d3d,
const video_info_t *video_info);
#ifdef HAVE_OVERLAY
static void d3d_free_overlays(void *data);
static void d3d_free_overlay(void *data, overlay_t *overlay);
#endif
#ifdef _XBOX
static void d3d_reinit_renderchain(void *data,
const video_info_t *video);
#endif
static void renderchain_free(void *data);
void d3d_make_d3dpp(void *data, const video_info_t *info,
D3DPRESENT_PARAMETERS *d3dpp);
#ifdef HAVE_WINDOW
#define IDI_ICON 1
#define MAX_MONITORS 9
extern LRESULT CALLBACK WindowProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
static RECT d3d_monitor_rect(d3d_video_t *d3d);
#endif
#ifdef HAVE_MONITOR
namespace Monitor
{
static HMONITOR last_hm;
static HMONITOR all_hms[MAX_MONITORS];
static unsigned num_mons;
}
#endif
static void d3d_deinit_shader(void *data)
{
d3d_video_t *d3d = (d3d_video_t*)data;
(void)d3d;
(void)data;
#ifdef HAVE_CG
if (!d3d->cgCtx)
return;
cgD3D9UnloadAllPrograms();
cgD3D9SetDevice(NULL);
cgDestroyContext(d3d->cgCtx);
d3d->cgCtx = NULL;
#endif
}
static bool d3d_init_shader(void *data)
{
d3d_video_t *d3d = (d3d_video_t*)data;
(void)d3d;
(void)data;
#if defined(HAVE_HLSL)
RARCH_LOG("D3D]: Using HLSL shader backend.\n");
const gl_shader_backend_t *backend = &hlsl_backend;
const char *shader_path = g_settings.video.shader_path;
d3d->shader = backend;
if (!d3d->shader)
return false;
return d3d->shader->init(d3d, shader_path);
#elif defined(HAVE_CG)
d3d->cgCtx = cgCreateContext();
if (!d3d->cgCtx)
return false;
RARCH_LOG("[D3D]: Created shader context.\n");
HRESULT ret = cgD3D9SetDevice(d3d->dev);
if (FAILED(ret))
return false;
return true;
#elif defined(_XBOX1)
return false;
#endif
}
static void d3d_deinit_chain(d3d_video_t *d3d)
{
#ifdef _XBOX
renderchain_free(d3d);
#else
if (d3d->chain)
delete (renderchain_t *)d3d->chain;
d3d->chain = NULL;
#endif
}
static void d3d_deinitialize(void *data)
{
d3d_video_t *d3d = (d3d_video_t*)data;
if (d3d->font_ctx && d3d->font_ctx->deinit)
d3d->font_ctx->deinit(d3d);
d3d->font_ctx = NULL;
d3d_deinit_chain(d3d);
#ifdef HAVE_SHADERS
d3d_deinit_shader(d3d);
#endif
#ifndef _XBOX
d3d->needs_restore = false;
#endif
}
static bool d3d_init_base(void *data, const video_info_t *info)
{
d3d_video_t *d3d = (d3d_video_t*)data;
D3DPRESENT_PARAMETERS d3dpp;
d3d_make_d3dpp(d3d, info, &d3dpp);
d3d->g_pD3D = D3DCREATE_CTX(D3D_SDK_VERSION);
if (!d3d->g_pD3D)
{
RARCH_ERR("Failed to create D3D interface.\n");
return false;
}
if (FAILED(d3d->d3d_err = d3d->g_pD3D->CreateDevice(
d3d->cur_mon_id,
D3DDEVTYPE_HAL,
d3d->hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp,
&d3d->dev)))
{
RARCH_WARN("[D3D]: Failed to init device with hardware vertex processing (code: 0x%x). Trying to fall back to software vertex processing.\n",
(unsigned)d3d->d3d_err);
if (FAILED(d3d->d3d_err = d3d->g_pD3D->CreateDevice(
d3d->cur_mon_id,
D3DDEVTYPE_HAL,
d3d->hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3d->dev)))
{
RARCH_ERR("Failed to initialize device.\n");
return false;
}
}
return true;
}
static bool d3d_initialize(void *data, const video_info_t *info)
{
d3d_video_t *d3d = (d3d_video_t*)data;
bool ret = true;
if (!d3d->g_pD3D)
ret = d3d_init_base(d3d, info);
else if (d3d->needs_restore)
{
D3DPRESENT_PARAMETERS d3dpp;
d3d_make_d3dpp(d3d, info, &d3dpp);
if (d3d->dev->Reset(&d3dpp) != D3D_OK)
{
// Try to recreate the device completely ..
#ifndef _XBOX
HRESULT res = d3d->dev->TestCooperativeLevel();
const char *err;
switch (res)
{
case D3DERR_DEVICELOST:
err = "DEVICELOST";
break;
case D3DERR_DEVICENOTRESET:
err = "DEVICENOTRESET";
break;
case D3DERR_DRIVERINTERNALERROR:
err = "DRIVERINTERNALERROR";
break;
default:
err = "Unknown";
}
RARCH_WARN(
"[D3D]: Attempting to recover from dead state (%s).\n", err);
#else
RARCH_WARN("[D3D]: Attempting to recover from dead state.\n");
#endif
d3d_deinitialize(d3d);
d3d->g_pD3D->Release();
d3d->g_pD3D = NULL;
ret = d3d_init_base(d3d, info);
if (ret)
RARCH_LOG("[D3D]: Recovered from dead state.\n");
else
return ret;
}
}
if (!ret)
return ret;
d3d_calculate_rect(d3d, d3d->screen_width, d3d->screen_height,
info->force_aspect, g_extern.system.aspect_ratio);
#ifdef HAVE_SHADERS
if (!d3d_init_shader(d3d))
{
RARCH_ERR("Failed to initialize shader subsystem.\n");
return false;
}
#endif
if (!d3d_init_chain(d3d, info))
{
RARCH_ERR("Failed to initialize render chain.\n");
return false;
}
#if defined(_XBOX360)
strlcpy(g_settings.video.font_path, "game:\\media\\Arial_12.xpr",
sizeof(g_settings.video.font_path));
#endif
d3d->font_ctx = d3d_font_init_first(d3d, g_settings.video.font_path, 0);
if (!d3d->font_ctx)
{
RARCH_ERR("Failed to initialize font.\n");
return false;
}
return true;
}
static void d3d_set_viewport(d3d_video_t *d3d, int x, int y,
unsigned width, unsigned height)
{
D3DVIEWPORT viewport;
/* D3D doesn't support negative X/Y viewports ... */
if (x < 0)
x = 0;
if (y < 0)
y = 0;
viewport.X = x;
viewport.Y = y;
viewport.Width = width;
viewport.Height = height;
viewport.MinZ = 0.0f;
viewport.MaxZ = 1.0f;
d3d->final_viewport = viewport;
#ifndef _XBOX
d3d_set_font_rect(d3d, NULL);
#endif
}
bool d3d_restore(d3d_video_t *d3d)
{
d3d_deinitialize(d3d);
d3d->needs_restore = !d3d_initialize(d3d, &d3d->video_info);
if (d3d->needs_restore)
RARCH_ERR("[D3D]: Restore error.\n");
return !d3d->needs_restore;
}
static void d3d_calculate_rect(d3d_video_t *d3d,
unsigned width, unsigned height,
bool keep, float desired_aspect)
{
if (g_settings.video.scale_integer)
{
struct rarch_viewport vp = {0};
gfx_scale_integer(&vp, width, height, desired_aspect, keep);
d3d_set_viewport(d3d, vp.x, vp.y, vp.width, vp.height);
}
else if (!keep)
d3d_set_viewport(d3d, 0, 0, width, height);
else
{
if (g_settings.video.aspect_ratio_idx == ASPECT_RATIO_CUSTOM)
{
const rarch_viewport_t &custom =
g_extern.console.screen.viewports.custom_vp;
d3d_set_viewport(d3d, custom.x, custom.y,
custom.width, custom.height);
}
else
{
float device_aspect = static_cast<float>(width) /
static_cast<float>(height);
if (fabsf(device_aspect - desired_aspect) < 0.0001f)
d3d_set_viewport(d3d, 0, 0, width, height);
else if (device_aspect > desired_aspect)
{
float delta = (desired_aspect / device_aspect - 1.0f) / 2.0f + 0.5f;
d3d_set_viewport(d3d, int(roundf(width * (0.5f - delta))),
0, unsigned(roundf(2.0f * width * delta)), height);
}
else
{
float delta = (device_aspect / desired_aspect - 1.0f) / 2.0f + 0.5f;
d3d_set_viewport(d3d, 0, int(roundf(height * (0.5f - delta))),
width, unsigned(roundf(2.0f * height * delta)));
}
}
}
}
static void d3d_set_nonblock_state(void *data, bool state)
{
d3d_video_t *d3d = (d3d_video_t*)data;
d3d->video_info.vsync = !state;
if (d3d->ctx_driver && d3d->ctx_driver->swap_interval)
d3d->ctx_driver->swap_interval(d3d, state ? 0 : 1);
}
static bool d3d_alive(void *data)
{
d3d_video_t *d3d = (d3d_video_t*)data;
bool quit, resize;
quit = false;
resize = false;
if (d3d->ctx_driver && d3d->ctx_driver->check_window)
d3d->ctx_driver->check_window(d3d, &quit, &resize,
&d3d->screen_width, &d3d->screen_height, g_extern.frame_count);
if (quit)
d3d->quitting = quit;
else if (resize)
d3d->should_resize = true;
return !quit;
}
static bool d3d_focus(void *data)
{
d3d_video_t *d3d = (d3d_video_t*)data;
if (d3d && d3d->ctx_driver && d3d->ctx_driver->has_focus)
return d3d->ctx_driver->has_focus(d3d);
return false;
}
static void d3d_set_aspect_ratio(void *data, unsigned aspect_ratio_idx)
{
d3d_video_t *d3d = (d3d_video_t*)data;
switch (aspect_ratio_idx)
{
case ASPECT_RATIO_SQUARE:
gfx_set_square_pixel_viewport(
g_extern.system.av_info.geometry.base_width,
g_extern.system.av_info.geometry.base_height);
break;
case ASPECT_RATIO_CORE:
gfx_set_core_viewport();
break;
case ASPECT_RATIO_CONFIG:
gfx_set_config_viewport();
break;
default:
break;
}
g_extern.system.aspect_ratio = aspectratio_lut[aspect_ratio_idx].value;
d3d->video_info.force_aspect = true;
d3d->should_resize = true;
}
static void d3d_apply_state_changes(void *data)
{
d3d_video_t *d3d = (d3d_video_t*)data;
d3d->should_resize = true;
}
static void d3d_set_osd_msg(void *data, const char *msg,
const struct font_params *params)
{
d3d_video_t *d3d = (d3d_video_t*)data;
#ifndef _XBOX
if (params)
d3d_set_font_rect(d3d, params);
#endif
if (d3d && d3d->font_ctx && d3d->font_ctx->render_msg)
d3d->font_ctx->render_msg(d3d, msg, params);
}
/* Delay constructor due to lack of exceptions. */
static bool d3d_construct(d3d_video_t *d3d,
const video_info_t *info, const input_driver_t **input,
void **input_data)
{
d3d->should_resize = false;
#ifndef _XBOX
gfx_set_dwm();
#endif
#if defined(HAVE_MENU) && defined(HAVE_OVERLAY)
if (d3d->menu)
free(d3d->menu);
d3d->menu = (overlay_t*)calloc(1, sizeof(overlay_t));
if (!d3d->menu)
return false;
d3d->menu->tex_coords.x = 0;
d3d->menu->tex_coords.y = 0;
d3d->menu->tex_coords.w = 1;
d3d->menu->tex_coords.h = 1;
d3d->menu->vert_coords.x = 0;
d3d->menu->vert_coords.y = 1;
d3d->menu->vert_coords.w = 1;
d3d->menu->vert_coords.h = -1;
#endif
#if defined(HAVE_WINDOW) && !defined(_XBOX)
memset(&d3d->windowClass, 0, sizeof(d3d->windowClass));
d3d->windowClass.cbSize = sizeof(d3d->windowClass);
d3d->windowClass.style = CS_HREDRAW | CS_VREDRAW;
d3d->windowClass.lpfnWndProc = WindowProc;
d3d->windowClass.hInstance = NULL;
d3d->windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
d3d->windowClass.lpszClassName = "RetroArch";
d3d->windowClass.hIcon = LoadIcon(GetModuleHandle(NULL),
MAKEINTRESOURCE(IDI_ICON));
d3d->windowClass.hIconSm = (HICON)LoadImage(GetModuleHandle(NULL),
MAKEINTRESOURCE(IDI_ICON), IMAGE_ICON, 16, 16, 0);
if (!info->fullscreen)
d3d->windowClass.hbrBackground = (HBRUSH)COLOR_WINDOW;
RegisterClassEx(&d3d->windowClass);
#endif
unsigned full_x, full_y;
#ifdef HAVE_MONITOR
RECT mon_rect = d3d_monitor_rect(d3d);
bool windowed_full = g_settings.video.windowed_fullscreen;
full_x = (windowed_full || info->width == 0) ?
(mon_rect.right - mon_rect.left) : info->width;
full_y = (windowed_full || info->height == 0) ?
(mon_rect.bottom - mon_rect.top) : info->height;
RARCH_LOG("[D3D]: Monitor size: %dx%d.\n",
(int)(mon_rect.right - mon_rect.left),
(int)(mon_rect.bottom - mon_rect.top));
#else
if (d3d->ctx_driver && d3d->ctx_driver->get_video_size)
d3d->ctx_driver->get_video_size(d3d, &full_x, &full_y);
#endif
d3d->screen_width = info->fullscreen ? full_x : info->width;
d3d->screen_height = info->fullscreen ? full_y : info->height;
#if defined(HAVE_WINDOW) && !defined(_XBOX)
unsigned win_width = d3d->screen_width;
unsigned win_height = d3d->screen_height;
if (!info->fullscreen)
{
RECT rect = {0};
rect.right = d3d->screen_width;
rect.bottom = d3d->screen_height;
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
win_width = rect.right - rect.left;
win_height = rect.bottom - rect.top;
}
char buffer[128];
gfx_get_fps(buffer, sizeof(buffer), NULL, 0);
std::string title = buffer;
title += " || Direct3D";
d3d->hWnd = CreateWindowEx(0, "RetroArch", title.c_str(),
info->fullscreen ?
(WS_EX_TOPMOST | WS_POPUP) : WS_OVERLAPPEDWINDOW,
info->fullscreen ? mon_rect.left : CW_USEDEFAULT,
info->fullscreen ? mon_rect.top : CW_USEDEFAULT,
win_width, win_height,
NULL, NULL, NULL, d3d);
driver.display_type = RARCH_DISPLAY_WIN32;
driver.video_display = 0;
driver.video_window = (uintptr_t)d3d->hWnd;
#endif
if (d3d && d3d->ctx_driver && d3d->ctx_driver->show_mouse)
d3d->ctx_driver->show_mouse(d3d, !info->fullscreen
#ifdef HAVE_OVERLAY
|| d3d->overlays_enabled
#endif
);
#if defined(HAVE_WINDOW) && !defined(_XBOX)
ShowWindow(d3d->hWnd, SW_RESTORE);
UpdateWindow(d3d->hWnd);
SetForegroundWindow(d3d->hWnd);
SetFocus(d3d->hWnd);
#endif
#ifndef _XBOX
#ifdef HAVE_SHADERS
/* This should only be done once here
* to avoid set_shader() to be overridden
* later. */
enum rarch_shader_type type =
gfx_shader_parse_type(g_settings.video.shader_path, RARCH_SHADER_NONE);
if (g_settings.video.shader_enable && type == RARCH_SHADER_CG)
d3d->cg_shader = g_settings.video.shader_path;
if (!d3d_process_shader(d3d))
return false;
#endif
#endif
d3d->video_info = *info;
if (!d3d_initialize(d3d, &d3d->video_info))
return false;
if (input && input_data &&
d3d->ctx_driver && d3d->ctx_driver->input_driver)
d3d->ctx_driver->input_driver(d3d, input, input_data);
RARCH_LOG("[D3D]: Init complete.\n");
return true;
}
static void d3d_viewport_info(void *data, struct rarch_viewport *vp)
{
d3d_video_t *d3d = (d3d_video_t*)data;
vp->x = d3d->final_viewport.X;
vp->y = d3d->final_viewport.Y;
vp->width = d3d->final_viewport.Width;
vp->height = d3d->final_viewport.Height;
vp->full_width = d3d->screen_width;
vp->full_height = d3d->screen_height;
}
static void d3d_set_rotation(void *data, unsigned rot)
{
d3d_video_t *d3d = (d3d_video_t*)data;
d3d->dev_rotation = rot;
}
static void d3d_show_mouse(void *data, bool state)
{
d3d_video_t *d3d = (d3d_video_t*)data;
if (d3d && d3d->ctx_driver && d3d->ctx_driver->show_mouse)
d3d->ctx_driver->show_mouse(d3d, state);
}
static const gfx_ctx_driver_t *d3d_get_context(void *data)
{
/* Default to Direct3D9 for now.
TODO: GL core contexts through ANGLE? */
enum gfx_ctx_api api = GFX_CTX_DIRECT3D9_API;
unsigned major = 9, minor = 0;
(void)major;
(void)minor;
#if defined(_XBOX1)
api = GFX_CTX_DIRECT3D8_API;
major = 8;
#endif
return gfx_ctx_init_first(driver.video_data, api,
major, minor, false);
}
static void *d3d_init(const video_info_t *info,
const input_driver_t **input, void **input_data)
{
#ifdef _XBOX
if (driver.video_data)
{
d3d_video_t *vid = (d3d_video_t*)driver.video_data;
/* Reinitialize renderchain as we
* might have changed pixel formats.*/
d3d_reinit_renderchain(vid, info);
if (input && input_data)
{
*input = driver.input;
*input_data = driver.input_data;
}
driver.video_data_own = true;
driver.input_data_own = true;
return driver.video_data;
}
#endif
d3d_video_t *vid = new d3d_video_t();
if (!vid)
return NULL;
vid->ctx_driver = d3d_get_context(vid);
if (!vid->ctx_driver)
{
delete vid;
return NULL;
}
/* Default values */
vid->g_pD3D = NULL;
vid->dev = NULL;
#ifndef _XBOX
vid->font = NULL;
#endif
vid->dev_rotation = 0;
vid->needs_restore = false;
#ifdef HAVE_CG
vid->cgCtx = NULL;
#endif
#ifdef HAVE_OVERLAY
vid->overlays_enabled = false;
#endif
#ifdef _XBOX
vid->should_resize = false;
vid->vsync = info->vsync;
#else
vid->menu = NULL;
#endif
if (!d3d_construct(vid, info, input, input_data))
{
RARCH_ERR("[D3D]: Failed to init D3D.\n");
delete vid;
return NULL;
}
#ifdef _XBOX
driver.video_data_own = true;
driver.input_data_own = true;
#endif
return vid;
}
static void d3d_free(void *data)
{
d3d_video_t *d3d = (d3d_video_t*)data;
d3d_deinitialize(d3d);
#ifdef HAVE_OVERLAY
d3d_free_overlays(d3d);
#endif
#if defined(HAVE_MENU) && !defined(_XBOX)
d3d_free_overlay(d3d, d3d->menu);
#endif
#ifdef _XBOX
if (d3d->ctx_driver && d3d->ctx_driver->destroy)
d3d->ctx_driver->destroy(d3d);
d3d->ctx_driver = NULL;
#endif
if (d3d->dev)
d3d->dev->Release();
if (d3d->g_pD3D)
d3d->g_pD3D->Release();
#ifdef HAVE_MONITOR
Monitor::last_hm = MonitorFromWindow(d3d->hWnd,
MONITOR_DEFAULTTONEAREST);
DestroyWindow(d3d->hWnd);
#endif
if (d3d)
delete d3d;
#ifndef _XBOX
UnregisterClass("RetroArch", GetModuleHandle(NULL));
#endif
}