1768 lines
46 KiB
C
Raw Normal View History

2012-10-26 21:09:30 +02:00
/* RetroArch - A frontend for libretro.
2014-01-01 01:50:59 +01:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2017-01-22 13:40:32 +01:00
* Copyright (C) 2011-2017 - Daniel De Matteis
2014-01-01 01:50:59 +01:00
* Copyright (C) 2012-2014 - OV2
2015-05-20 17:28:12 -05:00
*
2012-10-26 21:09:30 +02:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef _XBOX
#include <xtl.h>
#include <xgraphics.h>
#endif
2015-07-11 19:47:20 +02:00
#include <formats/image.h>
2015-09-05 14:10:16 +02:00
#include <compat/strl.h>
#include <compat/posix_string.h>
#include <file/file_path.h>
#include <string/stdstring.h>
2017-06-28 04:41:38 +02:00
#include <retro_math.h>
2015-07-11 19:47:20 +02:00
2018-01-25 15:30:48 +01:00
#include <d3d8.h>
2015-01-07 22:23:45 +01:00
#include "d3d.h"
#include "../../defines/d3d_defines.h"
#include "../common/d3d_common.h"
2016-03-05 07:27:48 +01:00
#include "../video_coord_array.h"
2016-09-05 18:43:06 +02:00
#include "../../configuration.h"
2015-04-11 21:57:03 +02:00
#include "../../dynamic.h"
#include "../video_driver.h"
2015-12-20 21:59:57 +01:00
#ifdef HAVE_THREADS
#include "../video_thread_wrapper.h"
#endif
2015-04-09 05:16:02 +02:00
#include "../common/win32_common.h"
2014-09-11 20:59:52 +02:00
2015-09-05 14:10:16 +02:00
#ifndef _XBOX
#define HAVE_MONITOR
#define HAVE_WINDOW
2014-09-11 20:59:52 +02:00
#endif
2014-01-09 18:44:25 +01:00
2015-12-05 13:30:37 +01:00
#ifdef HAVE_MENU
#include "../../menu/menu_driver.h"
#endif
2017-01-19 17:20:42 +01:00
#include "../font_driver.h"
2016-05-08 05:29:10 +02:00
#include "../../core.h"
2015-11-23 12:14:53 +01:00
#include "../../verbosity.h"
2015-04-06 01:10:39 +02:00
2018-01-25 04:12:16 +01:00
static LPDIRECT3D8 g_pD3D8;
2016-01-07 01:23:52 +01:00
2018-01-23 07:18:46 +01:00
typedef struct d3d8_renderchain
{
unsigned pixel_size;
2018-01-25 01:45:03 +01:00
LPDIRECT3DDEVICE8 dev;
2018-01-23 07:18:46 +01:00
const video_info_t *video_info;
2018-01-25 04:28:50 +01:00
LPDIRECT3DTEXTURE8 tex;
2018-01-25 14:38:24 +01:00
LPDIRECT3DVERTEXBUFFER8 vertex_buf;
2018-01-23 07:18:46 +01:00
unsigned last_width;
unsigned last_height;
void *vertex_decl;
unsigned tex_w;
unsigned tex_h;
uint64_t frame_count;
} d3d8_renderchain_t;
static void d3d8_renderchain_set_mvp(
void *data,
void *chain_data,
void *shader_data,
const void *mat_data)
2018-01-23 07:18:46 +01:00
{
D3DMATRIX matrix;
d3d_video_t *d3d = (d3d_video_t*)data;
d3d_matrix_identity(&matrix);
d3d_set_transform(d3d->dev, D3DTS_PROJECTION, &matrix);
d3d_set_transform(d3d->dev, D3DTS_VIEW, &matrix);
if (mat_data)
d3d_matrix_transpose(&matrix, mat_data);
d3d_set_transform(d3d->dev, D3DTS_WORLD, &matrix);
}
static bool d3d8_renderchain_create_first_pass(void *data,
const video_info_t *info)
{
d3d_video_t *d3d = (d3d_video_t*)data;
2018-01-25 01:45:03 +01:00
LPDIRECT3DDEVICE8 d3dr = (LPDIRECT3DDEVICE8)d3d->dev;
2018-01-23 07:18:46 +01:00
d3d8_renderchain_t *chain = (d3d8_renderchain_t*)d3d->renderchain_data;
chain->vertex_buf = d3d_vertex_buffer_new(d3dr, 4 * sizeof(Vertex),
2018-01-25 14:34:20 +01:00
D3DUSAGE_WRITEONLY,
D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_DIFFUSE,
D3DPOOL_MANAGED,
2018-01-23 07:18:46 +01:00
NULL);
if (!chain->vertex_buf)
return false;
chain->tex = d3d_texture_new(d3dr, NULL,
chain->tex_w, chain->tex_h, 1, 0,
info->rgb32
?
d3d_get_xrgb8888_format() : d3d_get_rgb565_format(),
D3DPOOL_MANAGED, 0, 0, 0, NULL, NULL,
false);
if (!chain->tex)
return false;
d3d_set_sampler_address_u(d3dr, 0, D3DTADDRESS_BORDER);
d3d_set_sampler_address_v(d3dr, 0, D3DTADDRESS_BORDER);
d3d_set_render_state(d3dr, D3DRS_LIGHTING, 0);
d3d_set_render_state(d3dr, D3DRS_CULLMODE, D3DCULL_NONE);
d3d_set_render_state(d3dr, D3DRS_ZENABLE, FALSE);
return true;
}
static void d3d8_renderchain_set_vertices(void *data, unsigned pass,
unsigned vert_width, unsigned vert_height, uint64_t frame_count)
{
unsigned width, height;
d3d_video_t *d3d = (d3d_video_t*)data;
d3d8_renderchain_t *chain = d3d ? (d3d8_renderchain_t*)d3d->renderchain_data : NULL;
video_driver_get_size(&width, &height);
if (!chain)
return;
if (chain->last_width != vert_width || chain->last_height != vert_height)
{
Vertex vert[4];
float tex_w, tex_h;
void *verts = NULL;
chain->last_width = vert_width;
chain->last_height = vert_height;
tex_w = vert_width;
tex_h = vert_height;
vert[0].x = 0.0f;
2018-01-23 07:18:46 +01:00
vert[0].y = 1.0f;
vert[0].z = 1.0f;
vert[1].x = 1.0f;
vert[1].y = 1.0f;
vert[1].z = 1.0f;
vert[2].x = 0.0f;
vert[2].y = 0.0f;
2018-01-23 07:18:46 +01:00
vert[2].z = 1.0f;
vert[3].x = 1.0f;
vert[3].y = 0.0f;
2018-01-23 07:18:46 +01:00
vert[3].z = 1.0f;
vert[0].u = 0.0f;
vert[0].v = 0.0f;
vert[1].v = 0.0f;
vert[2].u = 0.0f;
vert[1].u = tex_w;
vert[2].v = tex_h;
vert[3].u = tex_w;
vert[3].v = tex_h;
#ifndef _XBOX
vert[1].u /= chain->tex_w;
vert[2].v /= chain->tex_h;
vert[3].u /= chain->tex_w;
vert[3].v /= chain->tex_h;
#endif
vert[0].color = 0xFFFFFFFF;
vert[1].color = 0xFFFFFFFF;
vert[2].color = 0xFFFFFFFF;
vert[3].color = 0xFFFFFFFF;
verts = d3d_vertex_buffer_lock(chain->vertex_buf);
memcpy(verts, vert, sizeof(vert));
d3d_vertex_buffer_unlock(chain->vertex_buf);
}
}
static void d3d8_renderchain_blit_to_texture(void *data, const void *frame,
unsigned width, unsigned height, unsigned pitch)
{
D3DLOCKED_RECT d3dlr;
d3d8_renderchain_t *chain = (d3d8_renderchain_t*)data;
2018-01-25 01:45:03 +01:00
LPDIRECT3DDEVICE8 d3dr = (LPDIRECT3DDEVICE8)chain->dev;
2018-01-23 07:18:46 +01:00
d3d_frame_postprocess(chain);
if (chain->last_width != width || chain->last_height != height)
{
d3d_lock_rectangle(chain->tex,
0, &d3dlr, NULL, chain->tex_h, D3DLOCK_NOSYSLOCK);
d3d_lock_rectangle_clear(chain->tex,
0, &d3dlr, NULL, chain->tex_h, D3DLOCK_NOSYSLOCK);
}
/* Set the texture to NULL so D3D doesn't complain about it being in use... */
d3d_set_texture(d3dr, 0, NULL);
if (d3d_lock_rectangle(chain->tex, 0, &d3dlr, NULL, 0, 0))
{
d3d_texture_blit(chain->pixel_size, chain->tex,
&d3dlr, frame, width, height, pitch);
d3d_unlock_rectangle(chain->tex);
}
}
static void d3d8_renderchain_free(void *data)
{
d3d8_renderchain_t *chain = (d3d8_renderchain_t*)data;
if (!chain)
return;
if (chain->tex)
d3d_texture_free(chain->tex);
d3d_vertex_buffer_free(chain->vertex_buf, chain->vertex_decl);
free(chain);
}
static void d3d8_renderchain_viewport_info(void *data,
struct video_viewport *vp)
{
unsigned width, height;
d3d_video_t *d3d = (d3d_video_t*)data;
if (!d3d || !vp)
return;
video_driver_get_size(&width, &height);
2018-01-25 10:03:34 +01:00
vp->x = d3d->final_viewport.x;
vp->y = d3d->final_viewport.y;
vp->width = d3d->final_viewport.width;
vp->height = d3d->final_viewport.height;
2018-01-23 07:18:46 +01:00
vp->full_width = width;
vp->full_height = height;
}
static void d3d8_renderchain_render_pass(
2018-01-25 01:45:03 +01:00
d3d_video_t *d3d, LPDIRECT3DDEVICE8 d3dr,
2018-01-23 07:18:46 +01:00
d3d8_renderchain_t *chain,
unsigned pass_index,
unsigned rotation)
{
settings_t *settings = config_get_ptr();
2018-01-25 11:51:24 +01:00
bool video_smooth = settings->bools.video_smooth;
2018-01-23 07:18:46 +01:00
d3d_set_texture(d3dr, 0, chain->tex);
2018-01-25 10:12:49 +01:00
d3d_set_sampler_magfilter(d3dr, pass_index, video_smooth ?
2018-01-23 07:18:46 +01:00
D3DTEXF_LINEAR : D3DTEXF_POINT);
2018-01-25 10:12:49 +01:00
d3d_set_sampler_minfilter(d3dr, pass_index, video_smooth ?
2018-01-23 07:18:46 +01:00
D3DTEXF_LINEAR : D3DTEXF_POINT);
2018-01-25 10:03:34 +01:00
d3d_set_viewports(chain->dev, (D3DVIEWPORT8*)&d3d->final_viewport);
2018-01-25 14:34:20 +01:00
d3d_set_vertex_shader(d3dr,
D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_DIFFUSE,
NULL);
2018-01-23 07:18:46 +01:00
d3d_set_stream_source(d3dr, 0, chain->vertex_buf, 0, sizeof(Vertex));
2018-01-24 22:50:44 +01:00
d3d8_renderchain_set_mvp(d3d, chain, NULL, &d3d->mvp_rotate);
2018-01-23 07:18:46 +01:00
d3d_draw_primitive(d3dr, D3DPT_TRIANGLESTRIP, 0, 2);
}
static bool d3d8_renderchain_render(void *data, const void *frame,
unsigned frame_width, unsigned frame_height,
unsigned pitch, unsigned rotation)
{
d3d_video_t *d3d = (d3d_video_t*)data;
2018-01-25 01:45:03 +01:00
LPDIRECT3DDEVICE8 d3dr = (LPDIRECT3DDEVICE8)d3d->dev;
2018-01-23 07:18:46 +01:00
d3d8_renderchain_t *chain = (d3d8_renderchain_t*)d3d->renderchain_data;
d3d8_renderchain_blit_to_texture(chain, frame, frame_width, frame_height, pitch);
d3d8_renderchain_set_vertices(d3d, 1, frame_width, frame_height, chain->frame_count);
d3d8_renderchain_render_pass(d3d, d3dr, chain, 0, rotation);
chain->frame_count++;
return true;
}
static bool d3d8_renderchain_init(void *data,
const void *_video_info,
void *dev_data,
const void *info_data,
bool rgb32
)
{
unsigned width, height;
d3d_video_t *d3d = (d3d_video_t*)data;
2018-01-25 01:45:03 +01:00
LPDIRECT3DDEVICE8 d3dr = (LPDIRECT3DDEVICE8)d3d->dev;
2018-01-23 07:18:46 +01:00
const video_info_t *video_info = (const video_info_t*)_video_info;
const struct LinkInfo *link_info = (const struct LinkInfo*)info_data;
d3d8_renderchain_t *chain = (d3d8_renderchain_t*)d3d->renderchain_data;
unsigned fmt = (rgb32) ? RETRO_PIXEL_FORMAT_XRGB8888 : RETRO_PIXEL_FORMAT_RGB565;
struct video_viewport *custom_vp = video_viewport_get_custom();
video_driver_get_size(&width, &height);
2018-01-25 01:45:03 +01:00
chain->dev = (LPDIRECT3DDEVICE8)dev_data;
2018-01-23 07:18:46 +01:00
chain->pixel_size = (fmt == RETRO_PIXEL_FORMAT_RGB565) ? 2 : 4;
chain->tex_w = link_info->tex_w;
chain->tex_h = link_info->tex_h;
if (!d3d8_renderchain_create_first_pass(d3d, video_info))
return false;
/* FIXME */
if (custom_vp->width == 0)
custom_vp->width = width;
if (custom_vp->height == 0)
custom_vp->height = height;
return true;
}
static void *d3d8_renderchain_new(void)
{
d3d8_renderchain_t *renderchain = (d3d8_renderchain_t*)calloc(1, sizeof(*renderchain));
if (!renderchain)
return NULL;
return renderchain;
}
static bool d3d8_init_chain(d3d_video_t *d3d, const video_info_t *video_info)
2016-01-09 04:12:33 +01:00
{
2018-01-19 03:52:32 +01:00
struct LinkInfo link_info;
2016-01-09 04:12:33 +01:00
unsigned current_width, current_height, out_width, out_height;
2017-10-02 07:48:57 +02:00
unsigned i = 0;
2016-01-09 04:12:33 +01:00
(void)i;
(void)current_width;
(void)current_height;
(void)out_width;
(void)out_height;
/* Setup information for first pass. */
2018-01-19 03:52:32 +01:00
link_info.pass = NULL;
link_info.tex_w = video_info->input_scale * RARCH_SCALE_BASE;
link_info.tex_h = video_info->input_scale * RARCH_SCALE_BASE;
2016-01-09 04:12:33 +01:00
link_info.pass = &d3d->shader.pass[0];
2018-01-23 07:18:46 +01:00
d3d->renderchain_data = d3d8_renderchain_new();
2016-01-09 04:12:33 +01:00
if (
2018-01-23 07:18:46 +01:00
!d3d8_renderchain_init(
2016-01-09 04:12:33 +01:00
d3d,
&d3d->video_info,
2018-01-25 10:03:34 +01:00
d3d->dev, &link_info,
2016-01-09 04:12:33 +01:00
d3d->video_info.rgb32)
)
{
RARCH_ERR("[D3D]: Failed to init render chain.\n");
return false;
}
2018-01-23 07:18:46 +01:00
RARCH_LOG("[D3D]: Renderchain driver: %s\n", "d3d8");
2016-01-09 04:12:33 +01:00
return true;
}
static bool d3d8_init_singlepass(d3d_video_t *d3d)
2016-01-09 04:10:36 +01:00
{
2017-10-02 07:48:57 +02:00
struct video_shader_pass *pass = NULL;
2016-01-09 04:10:36 +01:00
if (!d3d)
return false;
memset(&d3d->shader, 0, sizeof(d3d->shader));
d3d->shader.passes = 1;
2017-10-02 07:48:57 +02:00
pass = (struct video_shader_pass*)
2016-08-02 01:42:36 +02:00
&d3d->shader.pass[0];
2016-01-09 04:10:36 +01:00
pass->fbo.valid = true;
pass->fbo.scale_y = 1.0;
pass->fbo.type_y = RARCH_SCALE_VIEWPORT;
pass->fbo.scale_x = pass->fbo.scale_y;
pass->fbo.type_x = pass->fbo.type_y;
if (!string_is_empty(d3d->shader_path))
strlcpy(pass->source.path, d3d->shader_path,
sizeof(pass->source.path));
2016-01-09 04:10:36 +01:00
return true;
}
static void d3d8_viewport_info(void *data, struct video_viewport *vp)
2016-01-09 03:10:08 +01:00
{
d3d_video_t *d3d = (d3d_video_t*)data;
2018-01-23 07:18:46 +01:00
if (d3d)
d3d8_renderchain_viewport_info(d3d, vp);
2016-01-09 03:10:08 +01:00
}
static void d3d8_set_mvp(void *data,
void *shader_data,
const void *mat_data)
{
d3d_video_t *d3d = (d3d_video_t*)data;
2018-01-23 07:18:46 +01:00
if (d3d)
d3d8_renderchain_set_mvp(d3d, d3d->renderchain_data, shader_data, mat_data);
}
static void d3d8_overlay_render(d3d_video_t *d3d,
2017-12-31 17:53:11 +01:00
video_frame_info_t *video_info,
overlay_t *overlay)
2016-01-09 03:10:08 +01:00
{
2018-01-25 10:03:34 +01:00
D3DVIEWPORT8 vp_full;
2016-01-09 03:10:08 +01:00
struct video_viewport vp;
void *verts;
unsigned i;
Vertex vert[4];
unsigned width = video_info->width;
unsigned height = video_info->height;
2016-01-09 03:10:08 +01:00
2016-10-20 04:51:37 +02:00
if (!d3d || !overlay || !overlay->tex)
2016-01-09 03:10:08 +01:00
return;
if (!overlay->vert_buf)
{
overlay->vert_buf = d3d_vertex_buffer_new(
2018-01-25 14:34:20 +01:00
d3d->dev, sizeof(vert), D3DUSAGE_WRITEONLY,
D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_DIFFUSE,
D3DPOOL_MANAGED, NULL);
if (!overlay->vert_buf)
return;
}
2016-01-09 03:10:08 +01:00
for (i = 0; i < 4; i++)
{
vert[i].z = 0.5f;
vert[i].color = (((uint32_t)(overlay->alpha_mod * 0xFF)) << 24) | 0xFFFFFF;
2016-01-09 03:10:08 +01:00
}
d3d8_viewport_info(d3d, &vp);
2016-01-09 03:10:08 +01:00
vert[0].x = overlay->vert_coords[0];
vert[1].x = overlay->vert_coords[0] + overlay->vert_coords[2];
vert[2].x = overlay->vert_coords[0];
vert[3].x = overlay->vert_coords[0] + overlay->vert_coords[2];
vert[0].y = overlay->vert_coords[1];
vert[1].y = overlay->vert_coords[1];
vert[2].y = overlay->vert_coords[1] + overlay->vert_coords[3];
vert[3].y = overlay->vert_coords[1] + overlay->vert_coords[3];
vert[0].u = overlay->tex_coords[0];
vert[1].u = overlay->tex_coords[0] + overlay->tex_coords[2];
vert[2].u = overlay->tex_coords[0];
vert[3].u = overlay->tex_coords[0] + overlay->tex_coords[2];
vert[0].v = overlay->tex_coords[1];
vert[1].v = overlay->tex_coords[1];
vert[2].v = overlay->tex_coords[1] + overlay->tex_coords[3];
vert[3].v = overlay->tex_coords[1] + overlay->tex_coords[3];
2016-01-09 03:10:08 +01:00
2016-10-20 05:00:20 +02:00
verts = d3d_vertex_buffer_lock(overlay->vert_buf);
2016-01-09 03:10:08 +01:00
memcpy(verts, vert, sizeof(vert));
d3d_vertex_buffer_unlock(overlay->vert_buf);
d3d_enable_blend_func(d3d->dev);
2018-01-25 14:34:20 +01:00
d3d_set_vertex_shader(d3d->dev,
D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_DIFFUSE,
NULL);
2016-01-09 03:10:08 +01:00
d3d_set_stream_source(d3d->dev, 0, overlay->vert_buf,
0, sizeof(*vert));
if (overlay->fullscreen)
{
vp_full.X = 0;
vp_full.Y = 0;
vp_full.Width = width;
vp_full.Height = height;
vp_full.MinZ = 0.0f;
vp_full.MaxZ = 1.0f;
2018-01-23 06:30:46 +01:00
d3d_set_viewports(d3d->dev, &vp_full);
2016-01-09 03:10:08 +01:00
}
/* 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_disable_blend_func(d3d->dev);
2018-01-25 10:03:34 +01:00
2018-01-23 06:31:33 +01:00
d3d_set_viewports(d3d->dev, &d3d->final_viewport);
2016-01-09 03:10:08 +01:00
}
static void d3d8_free_overlay(d3d_video_t *d3d, overlay_t *overlay)
2016-01-09 03:10:08 +01:00
{
if (!d3d)
return;
d3d_texture_free(overlay->tex);
d3d_vertex_buffer_free(overlay->vert_buf, NULL);
}
static void d3d8_deinit_chain(d3d_video_t *d3d)
{
2018-01-23 07:21:53 +01:00
d3d8_renderchain_free(d3d->renderchain_data);
2016-01-07 00:29:12 +01:00
d3d->renderchain_data = NULL;
}
static void d3d8_deinitialize(d3d_video_t *d3d)
{
2015-03-15 17:51:27 +01:00
if (!d3d)
return;
font_driver_free_osd();
d3d8_deinit_chain(d3d);
d3d_vertex_buffer_free(d3d->menu_display.buffer, d3d->menu_display.decl);
d3d->menu_display.buffer = NULL;
d3d->menu_display.decl = NULL;
}
2017-09-05 00:57:58 +02:00
#define FS_PRESENTINTERVAL(pp) ((pp)->FullScreen_PresentationInterval)
static D3DFORMAT d3d8_get_color_format_backbuffer(bool rgb32, bool windowed)
2018-01-06 15:17:46 +01:00
{
D3DFORMAT fmt = D3DFMT_X8R8G8B8;
2018-01-06 15:17:46 +01:00
#ifdef _XBOX
if (!rgb32)
2018-01-20 16:45:45 +01:00
fmt = d3d_get_rgb565_format();
2018-01-06 15:17:46 +01:00
#else
2018-01-06 16:10:05 +01:00
if (windowed)
{
D3DDISPLAYMODE display_mode;
if (d3d_get_adapter_display_mode(g_pD3D8, 0, &display_mode))
2018-01-06 16:10:05 +01:00
fmt = display_mode.Format;
}
2018-01-06 15:17:46 +01:00
#endif
return fmt;
}
static bool d3d8_is_windowed_enable(bool info_fullscreen)
2018-01-06 15:32:02 +01:00
{
#ifndef _XBOX
settings_t *settings = config_get_ptr();
2018-01-07 11:06:27 +01:00
if (!info_fullscreen)
return true;
2018-01-06 15:32:02 +01:00
if (settings)
2018-01-07 11:06:27 +01:00
return settings->bools.video_windowed_fullscreen;
2018-01-06 15:32:02 +01:00
#endif
return false;
}
void d3d8_make_d3dpp(void *data,
const video_info_t *info, void *_d3dpp)
2015-04-11 21:54:52 +02:00
{
2018-01-07 11:06:27 +01:00
d3d_video_t *d3d = (d3d_video_t*)data;
D3DPRESENT_PARAMETERS *d3dpp = (D3DPRESENT_PARAMETERS*)_d3dpp;
bool windowed_enable = d3d8_is_windowed_enable(info->fullscreen);
2015-04-11 21:54:52 +02:00
memset(d3dpp, 0, sizeof(*d3dpp));
2018-01-07 11:06:27 +01:00
d3dpp->Windowed = windowed_enable;
FS_PRESENTINTERVAL(d3dpp) = D3DPRESENT_INTERVAL_IMMEDIATE;
if (info->vsync)
{
2018-01-07 11:06:27 +01:00
settings_t *settings = config_get_ptr();
2017-04-28 21:03:04 +02:00
switch (settings->uints.video_swap_interval)
{
default:
case 1:
2017-09-05 00:57:58 +02:00
FS_PRESENTINTERVAL(d3dpp) = D3DPRESENT_INTERVAL_ONE;
break;
case 2:
2017-09-05 00:57:58 +02:00
FS_PRESENTINTERVAL(d3dpp) = D3DPRESENT_INTERVAL_TWO;
break;
case 3:
2017-09-05 00:57:58 +02:00
FS_PRESENTINTERVAL(d3dpp) = D3DPRESENT_INTERVAL_THREE;
break;
case 4:
2017-09-05 00:57:58 +02:00
FS_PRESENTINTERVAL(d3dpp) = D3DPRESENT_INTERVAL_FOUR;
break;
}
}
2018-01-07 10:46:30 +01:00
/* PresentationInterval must be zero for windowed mode on DX8. */
if (d3dpp->Windowed)
FS_PRESENTINTERVAL(d3dpp) = D3DPRESENT_INTERVAL_DEFAULT;
2018-01-07 11:06:27 +01:00
d3dpp->SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp->BackBufferCount = 2;
d3dpp->BackBufferFormat = d3d8_get_color_format_backbuffer(
2018-01-07 11:06:27 +01:00
info->rgb32, windowed_enable);
2018-01-17 08:54:06 +01:00
#ifndef _XBOX
2018-01-07 11:06:27 +01:00
d3dpp->hDeviceWindow = win32_get_window();
2018-01-17 08:54:06 +01:00
#endif
2018-01-06 15:17:46 +01:00
2018-01-06 15:32:02 +01:00
if (!windowed_enable)
{
#ifdef _XBOX
2016-02-14 02:26:20 +01:00
gfx_ctx_mode_t mode;
2018-01-07 11:06:27 +01:00
unsigned width = 0;
unsigned height = 0;
2016-05-08 20:32:46 +02:00
video_context_driver_get_video_size(&mode);
2016-02-14 02:26:20 +01:00
2018-01-07 11:06:27 +01:00
width = mode.width;
height = mode.height;
mode.width = 0;
mode.height = 0;
2015-11-23 12:54:15 +01:00
video_driver_set_size(&width, &height);
#endif
2016-08-02 01:42:36 +02:00
video_driver_get_size(&d3dpp->BackBufferWidth,
&d3dpp->BackBufferHeight);
}
#ifdef _XBOX
d3dpp->MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp->EnableAutoDepthStencil = FALSE;
2018-01-03 14:37:35 +01:00
{
2018-01-03 14:42:09 +01:00
/* Get the "video mode" */
2018-01-07 11:06:27 +01:00
DWORD video_mode = XGetVideoFlags();
2018-01-03 14:42:09 +01:00
/* Check if we are able to use progressive mode. */
if (video_mode & XC_VIDEO_FLAGS_HDTV_480p)
d3dpp->Flags = D3DPRESENTFLAG_PROGRESSIVE;
2018-01-03 14:42:09 +01:00
else
d3dpp->Flags = D3DPRESENTFLAG_INTERLACED;
2018-01-03 14:42:09 +01:00
/* Only valid in PAL mode, not valid for HDTV modes. */
if (XGetVideoStandard() == XC_VIDEO_STANDARD_PAL_I)
{
if (video_mode & XC_VIDEO_FLAGS_PAL_60Hz)
d3dpp->FullScreen_RefreshRateInHz = 60;
else
d3dpp->FullScreen_RefreshRateInHz = 50;
}
if (XGetAVPack() == XC_AV_PACK_HDTV)
{
if (video_mode & XC_VIDEO_FLAGS_HDTV_480p)
d3dpp->Flags = D3DPRESENTFLAG_PROGRESSIVE;
else if (video_mode & XC_VIDEO_FLAGS_HDTV_720p)
d3dpp->Flags = D3DPRESENTFLAG_PROGRESSIVE;
else if (video_mode & XC_VIDEO_FLAGS_HDTV_1080i)
d3dpp->Flags = D3DPRESENTFLAG_INTERLACED;
}
#if 0
if (widescreen_mode)
d3dpp->Flags |= D3DPRESENTFLAG_WIDESCREEN;
#endif
2018-01-03 14:37:35 +01:00
}
#endif
2015-04-11 21:54:52 +02:00
}
static bool d3d8_init_base(void *data, const video_info_t *info)
{
D3DPRESENT_PARAMETERS d3dpp;
2018-01-20 02:09:51 +01:00
HWND focus_window = NULL;
2017-10-02 03:15:49 +02:00
d3d_video_t *d3d = (d3d_video_t*)data;
2015-03-16 18:33:47 +01:00
2018-01-17 08:54:06 +01:00
#ifndef _XBOX
focus_window = win32_get_window();
#endif
2018-01-07 10:46:30 +01:00
memset(&d3dpp, 0, sizeof(d3dpp));
2018-01-25 04:12:16 +01:00
g_pD3D8 = (LPDIRECT3D8)d3d_create();
2018-01-03 18:09:31 +01:00
2018-01-07 10:46:30 +01:00
/* this needs g_pD3D created first */
d3d8_make_d3dpp(d3d, info, &d3dpp);
2018-01-07 10:46:30 +01:00
if (!g_pD3D8)
{
2017-03-24 01:33:22 +01:00
RARCH_ERR("[D3D]: Failed to create D3D interface.\n");
return false;
}
2017-10-02 03:15:49 +02:00
if (!d3d_create_device(&d3d->dev, &d3dpp,
g_pD3D8,
2017-10-02 03:15:49 +02:00
focus_window,
d3d->cur_mon_id)
)
{
RARCH_ERR("[D3D]: Failed to initialize device.\n");
return false;
}
return true;
}
static void d3d8_calculate_rect(void *data,
2016-01-08 03:07:58 +01:00
unsigned *width, unsigned *height,
int *x, int *y,
bool force_full,
2015-11-10 22:18:08 +01:00
bool allow_rotate)
{
2016-02-13 22:07:56 +01:00
gfx_ctx_aspect_t aspect_data;
2016-01-08 03:09:41 +01:00
float device_aspect = (float)*width / *height;
d3d_video_t *d3d = (d3d_video_t*)data;
settings_t *settings = config_get_ptr();
2016-01-08 03:07:58 +01:00
video_driver_get_size(width, height);
2016-02-13 22:07:56 +01:00
aspect_data.aspect = &device_aspect;
aspect_data.width = *width;
aspect_data.height = *height;
2016-05-08 20:32:46 +02:00
video_context_driver_translate_aspect(&aspect_data);
2016-01-08 03:14:49 +01:00
*x = 0;
*y = 0;
2017-04-28 00:53:06 +02:00
if (settings->bools.video_scale_integer && !force_full)
{
struct video_viewport vp;
vp.x = 0;
vp.y = 0;
vp.width = 0;
vp.height = 0;
vp.full_width = 0;
vp.full_height = 0;
2016-01-08 03:14:49 +01:00
video_viewport_get_scaled_integer(&vp,
*width,
*height,
video_driver_get_aspect_ratio(),
d3d->keep_aspect);
*x = vp.x;
*y = vp.y;
*width = vp.width;
*height = vp.height;
}
else if (d3d->keep_aspect && !force_full)
{
2016-01-08 03:14:49 +01:00
float desired_aspect = video_driver_get_aspect_ratio();
#if defined(HAVE_MENU)
2017-04-28 21:03:04 +02:00
if (settings->uints.video_aspect_ratio_idx == ASPECT_RATIO_CUSTOM)
{
video_viewport_t *custom = video_viewport_get_custom();
2015-11-10 22:18:08 +01:00
*x = custom->x;
*y = custom->y;
*width = custom->width;
*height = custom->height;
}
else
2016-01-08 03:14:49 +01:00
#endif
{
float delta;
2016-01-08 03:14:49 +01:00
if (fabsf(device_aspect - desired_aspect) < 0.0001f)
{
/* If the aspect ratios of screen and desired aspect
* ratio are sufficiently equal (floating point stuff),
2016-01-08 03:14:49 +01:00
* assume they are actually equal.
*/
}
else if (device_aspect > desired_aspect)
{
2017-10-02 07:48:57 +02:00
delta = (desired_aspect / device_aspect - 1.0f) / 2.0f + 0.5f;
*x = (int)(roundf(*width * (0.5f - delta)));
*width = (unsigned)(roundf(2.0f * (*width) * delta));
}
else
{
2017-10-02 07:48:57 +02:00
delta = (device_aspect / desired_aspect - 1.0f) / 2.0f + 0.5f;
*y = (int)(roundf(*height * (0.5f - delta)));
*height = (unsigned)(roundf(2.0f * (*height) * delta));
}
}
}
2016-01-08 03:07:58 +01:00
}
static void d3d8_set_viewport(void *data,
2016-01-08 03:07:58 +01:00
unsigned width, unsigned height,
bool force_full,
bool allow_rotate)
{
D3DMATRIX proj, ortho, rot, matrix;
2016-01-08 03:07:58 +01:00
int x = 0;
int y = 0;
d3d_video_t *d3d = (d3d_video_t*)data;
d3d8_calculate_rect(data, &width, &height, &x, &y,
2016-01-08 03:07:58 +01:00
force_full, allow_rotate);
/* D3D doesn't support negative X/Y viewports ... */
if (x < 0)
x = 0;
if (y < 0)
y = 0;
2018-01-25 10:03:34 +01:00
d3d->final_viewport.x = x;
d3d->final_viewport.y = y;
d3d->final_viewport.width = width;
d3d->final_viewport.height= height;
d3d->final_viewport.min_z = 0.0f;
d3d->final_viewport.max_z = 0.0f;
d3d_matrix_ortho_off_center_lh(&ortho, 0, 1, 0, 1, 0.0f, 1.0f);
d3d_matrix_identity(&rot);
d3d_matrix_rotation_z(&rot, d3d->dev_rotation * (M_PI / 2.0));
d3d_matrix_multiply(&proj, &ortho, &rot);
2018-01-24 22:50:44 +01:00
d3d_matrix_transpose(&d3d->mvp, &ortho);
d3d_matrix_transpose(&d3d->mvp_rotate, &matrix);
2015-11-10 22:18:08 +01:00
}
static bool d3d8_initialize(d3d_video_t *d3d, const video_info_t *info)
{
unsigned width, height;
2015-04-05 16:34:31 +02:00
bool ret = true;
2015-03-20 22:08:36 +01:00
settings_t *settings = config_get_ptr();
2015-01-17 06:30:39 +01:00
2015-03-15 17:51:27 +01:00
if (!d3d)
return false;
if (!g_pD3D8)
ret = d3d8_init_base(d3d, info);
else if (d3d->needs_restore)
{
D3DPRESENT_PARAMETERS d3dpp;
2015-03-16 18:33:47 +01:00
d3d8_make_d3dpp(d3d, info, &d3dpp);
2015-03-16 18:33:47 +01:00
/* the D3DX font driver uses POOL_DEFAULT resources
* and will prevent a clean reset here
* another approach would be to keep track of all created D3D
* font objects and free/realloc them around the d3d_reset call */
menu_driver_ctl(RARCH_MENU_CTL_DEINIT, NULL);
2015-11-14 20:02:55 +01:00
if (!d3d_reset(d3d->dev, &d3dpp))
{
d3d8_deinitialize(d3d);
d3d_device_free(NULL, g_pD3D8);
g_pD3D8 = NULL;
2015-11-14 20:02:55 +01:00
ret = d3d8_init_base(d3d, info);
if (ret)
RARCH_LOG("[D3D]: Recovered from dead state.\n");
}
menu_driver_init(info->is_threaded);
}
if (!ret)
return ret;
if (!d3d8_init_chain(d3d, info))
{
2017-03-24 01:33:22 +01:00
RARCH_ERR("[D3D]: Failed to initialize render chain.\n");
return false;
}
video_driver_get_size(&width, &height);
d3d8_set_viewport(d3d,
2016-12-02 23:36:23 -05:00
width, height, false, true);
font_driver_init_osd(d3d, false,
2017-04-29 18:45:58 +02:00
info->is_threaded,
FONT_DRIVER_RENDER_DIRECT3D_API);
d3d->menu_display.offset = 0;
2018-01-15 20:58:13 +01:00
d3d->menu_display.size = 1024;
d3d->menu_display.buffer = d3d_vertex_buffer_new(
d3d->dev, d3d->menu_display.size * sizeof(Vertex),
2018-01-25 14:34:20 +01:00
D3DUSAGE_WRITEONLY,
D3DFVF_XYZ | D3DFVF_TEX1 | D3DFVF_DIFFUSE,
D3DPOOL_DEFAULT,
NULL);
if (!d3d->menu_display.buffer)
return false;
d3d_matrix_ortho_off_center_lh(&d3d->mvp_transposed, 0, 1, 0, 1, 0, 1);
d3d_matrix_transpose(&d3d->mvp, &d3d->mvp_transposed);
d3d_set_render_state(d3d->dev, D3DRS_CULLMODE, D3DCULL_NONE);
return true;
}
2016-01-08 09:48:46 +01:00
static bool d3d_restore(void *data)
2016-01-07 01:00:11 +01:00
{
d3d_video_t *d3d = (d3d_video_t*)data;
2016-01-07 01:00:11 +01:00
if (!d3d)
return false;
d3d8_deinitialize(d3d);
if (!d3d8_initialize(d3d, &d3d->video_info))
2016-01-07 02:55:27 +01:00
{
RARCH_ERR("[D3D]: Restore error.\n");
2016-01-07 02:55:27 +01:00
return false;
}
2016-01-07 02:55:27 +01:00
d3d->needs_restore = false;
return true;
}
static void d3d8_set_nonblock_state(void *data, bool state)
{
2016-02-13 20:45:45 +01:00
unsigned interval = state ? 0 : 1;
2015-04-09 18:38:24 +02:00
d3d_video_t *d3d = (d3d_video_t*)data;
2015-03-16 18:33:47 +01:00
if (!d3d)
return;
d3d->video_info.vsync = !state;
2016-05-08 20:32:46 +02:00
video_context_driver_swap_interval(&interval);
2016-01-07 00:38:42 +01:00
#ifndef _XBOX
2016-01-08 08:16:26 +01:00
d3d->needs_restore = true;
2016-01-07 00:45:09 +01:00
d3d_restore(d3d);
2016-01-07 00:38:42 +01:00
#endif
}
static bool d3d8_alive(void *data)
{
2016-02-13 19:53:14 +01:00
gfx_ctx_size_t size_data;
unsigned temp_width = 0;
unsigned temp_height = 0;
bool ret = false;
d3d_video_t *d3d = (d3d_video_t*)data;
bool quit = false;
bool resize = false;
2017-04-24 09:44:05 +02:00
/* Needed because some context drivers don't track their sizes */
video_driver_get_size(&temp_width, &temp_height);
2016-02-13 19:53:14 +01:00
size_data.quit = &quit;
size_data.resize = &resize;
size_data.width = &temp_width;
size_data.height = &temp_height;
2015-04-09 18:38:24 +02:00
2016-05-08 20:32:46 +02:00
if (video_context_driver_check_window(&size_data))
{
if (quit)
d3d->quitting = quit;
2016-01-08 05:06:12 +01:00
if (resize)
{
d3d->should_resize = true;
2017-05-20 17:47:56 +02:00
video_driver_set_resize(temp_width, temp_height);
2016-01-08 05:44:36 +01:00
d3d_restore(d3d);
2016-01-08 05:06:12 +01:00
}
ret = !quit;
}
if (temp_width != 0 && temp_height != 0)
2015-11-23 12:54:15 +01:00
video_driver_set_size(&temp_width, &temp_height);
return ret;
}
static bool d3d8_suppress_screensaver(void *data, bool enable)
2015-01-18 22:43:07 +01:00
{
2016-02-13 23:39:12 +01:00
bool enabled = enable;
2016-05-08 20:32:46 +02:00
return video_context_driver_suppress_screensaver(&enabled);
2015-01-18 22:43:07 +01:00
}
static void d3d8_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:
video_driver_set_viewport_square_pixel();
break;
case ASPECT_RATIO_CORE:
video_driver_set_viewport_core();
break;
case ASPECT_RATIO_CONFIG:
video_driver_set_viewport_config();
break;
default:
break;
}
2016-08-02 01:42:36 +02:00
video_driver_set_aspect_ratio_value(
aspectratio_lut[aspect_ratio_idx].value);
2015-03-16 18:33:47 +01:00
2015-03-21 05:42:49 +01:00
if (!d3d)
return;
2015-11-10 05:30:25 +01:00
d3d->keep_aspect = true;
2015-03-21 05:42:49 +01:00
d3d->should_resize = true;
}
static void d3d8_apply_state_changes(void *data)
{
d3d_video_t *d3d = (d3d_video_t*)data;
2015-01-17 06:30:39 +01:00
if (d3d)
d3d->should_resize = true;
}
static void d3d8_set_osd_msg(void *data,
video_frame_info_t *video_info,
const char *msg,
const void *params, void *font)
{
2015-04-05 16:34:31 +02:00
d3d_video_t *d3d = (d3d_video_t*)data;
font_driver_render_msg(video_info, font, msg, params);
}
static bool d3d8_init_internal(d3d_video_t *d3d,
const video_info_t *info, const input_driver_t **input,
void **input_data)
{
2016-02-13 23:26:33 +01:00
gfx_ctx_input_t inp;
2017-10-02 22:06:32 +02:00
#ifdef HAVE_MONITOR
bool windowed_full;
RECT mon_rect;
MONITORINFOEX current_mon;
HMONITOR hm_to_use;
#endif
#ifdef HAVE_WINDOW
DWORD style;
unsigned win_width = 0;
unsigned win_height = 0;
RECT rect = {0};
#endif
unsigned full_x = 0;
unsigned full_y = 0;
settings_t *settings = config_get_ptr();
2018-01-06 19:51:29 +01:00
overlay_t *menu = (overlay_t*)calloc(1, sizeof(*menu));
2015-01-17 06:30:39 +01:00
2018-01-06 19:51:29 +01:00
if (!menu)
return false;
2018-01-06 19:51:29 +01:00
d3d->menu = menu;
2018-01-06 15:42:47 +01:00
d3d->cur_mon_id = 0;
2015-11-11 03:48:05 +01:00
d3d->menu->tex_coords[0] = 0;
d3d->menu->tex_coords[1] = 0;
d3d->menu->tex_coords[2] = 1;
d3d->menu->tex_coords[3] = 1;
d3d->menu->vert_coords[0] = 0;
d3d->menu->vert_coords[1] = 1;
d3d->menu->vert_coords[2] = 1;
d3d->menu->vert_coords[3] = -1;
memset(&d3d->windowClass, 0, sizeof(d3d->windowClass));
2017-09-05 00:57:58 +02:00
#ifdef HAVE_WINDOW
2015-11-28 21:50:28 +01:00
d3d->windowClass.lpfnWndProc = WndProcD3D;
2015-11-19 08:30:37 +01:00
win32_window_init(&d3d->windowClass, true, NULL);
2015-11-19 08:37:25 +01:00
#endif
#ifdef HAVE_MONITOR
2015-11-11 17:57:15 +01:00
win32_monitor_info(&current_mon, &hm_to_use, &d3d->cur_mon_id);
2018-01-06 19:51:29 +01:00
mon_rect = current_mon.rcMonitor;
g_resize_width = info->width;
g_resize_height = info->height;
2015-11-11 05:32:00 +01:00
2018-01-06 19:51:29 +01:00
windowed_full = settings->bools.video_windowed_fullscreen;
2018-01-06 19:51:29 +01:00
full_x = (windowed_full || info->width == 0) ?
(mon_rect.right - mon_rect.left) : info->width;
2018-01-06 19:51:29 +01:00
full_y = (windowed_full || info->height == 0) ?
(mon_rect.bottom - mon_rect.top) : info->height;
2018-01-06 19:51:29 +01:00
2015-05-20 17:28:12 -05:00
RARCH_LOG("[D3D]: Monitor size: %dx%d.\n",
(int)(mon_rect.right - mon_rect.left),
(int)(mon_rect.bottom - mon_rect.top));
#else
2016-02-14 02:26:20 +01:00
{
gfx_ctx_mode_t mode;
2016-05-08 20:32:46 +02:00
video_context_driver_get_video_size(&mode);
2016-02-14 02:26:20 +01:00
full_x = mode.width;
full_y = mode.height;
}
#endif
{
unsigned new_width = info->fullscreen ? full_x : info->width;
unsigned new_height = info->fullscreen ? full_y : info->height;
video_driver_set_size(&new_width, &new_height);
}
2015-04-06 01:43:47 +02:00
#ifdef HAVE_WINDOW
2015-11-12 05:39:55 +01:00
video_driver_get_size(&win_width, &win_height);
2015-11-12 02:35:05 +01:00
2016-08-02 01:42:36 +02:00
win32_set_style(&current_mon, &hm_to_use, &win_width, &win_height,
info->fullscreen, windowed_full, &rect, &mon_rect, &style);
2015-11-12 02:51:40 +01:00
win32_window_create(d3d, style, &mon_rect, win_width,
win_height, info->fullscreen);
2016-01-09 05:04:47 +01:00
win32_set_window(&win_width, &win_height, info->fullscreen,
2016-12-02 23:36:23 -05:00
windowed_full, &rect);
#endif
if (!d3d8_init_singlepass(d3d))
return false;
d3d->video_info = *info;
if (!d3d8_initialize(d3d, &d3d->video_info))
return false;
2016-02-13 23:26:33 +01:00
inp.input = input;
inp.input_data = input_data;
2016-05-08 20:32:46 +02:00
video_context_driver_input_driver(&inp);
RARCH_LOG("[D3D]: Init complete.\n");
return true;
}
static void d3d8_set_rotation(void *data, unsigned rot)
{
d3d_video_t *d3d = (d3d_video_t*)data;
2015-11-10 00:13:17 +01:00
if (!d3d)
return;
d3d->dev_rotation = rot;
d3d->should_resize = true;
}
static void d3d8_show_mouse(void *data, bool state)
{
2016-05-08 20:32:46 +02:00
video_context_driver_show_mouse(&state);
}
static const gfx_ctx_driver_t *d3d8_get_context(void *data)
{
2015-04-05 16:34:31 +02:00
unsigned minor = 0;
2015-09-29 18:08:33 +02:00
unsigned major = 8;
enum gfx_ctx_api api = GFX_CTX_DIRECT3D8_API;
2018-01-06 19:51:29 +01:00
settings_t *settings = config_get_ptr();
2016-05-08 20:32:46 +02:00
return video_context_driver_init_first(data,
2017-04-28 22:59:13 +02:00
settings->arrays.video_context_driver,
2014-10-24 02:15:30 +02:00
api, major, minor, false);
}
static void *d3d8_init(const video_info_t *info,
const input_driver_t **input, void **input_data)
{
2016-01-07 07:10:02 +01:00
d3d_video_t *d3d = NULL;
const gfx_ctx_driver_t *ctx_driver = NULL;
2015-01-17 06:30:39 +01:00
2018-01-23 04:25:37 +01:00
if (!d3d_initialize_symbols(GFX_CTX_DIRECT3D8_API))
2018-01-03 18:38:13 +01:00
return NULL;
2017-10-02 07:48:57 +02:00
d3d = (d3d_video_t*)calloc(1, sizeof(*d3d));
2016-01-07 06:50:32 +01:00
if (!d3d)
2015-04-06 01:12:42 +02:00
goto error;
ctx_driver = d3d8_get_context(d3d);
2016-01-07 07:10:02 +01:00
if (!ctx_driver)
2015-04-06 01:12:42 +02:00
goto error;
/* Default values */
2016-01-07 06:50:32 +01:00
d3d->dev = NULL;
d3d->dev_rotation = 0;
d3d->needs_restore = false;
#ifdef HAVE_OVERLAY
2016-01-07 06:50:32 +01:00
d3d->overlays_enabled = false;
#endif
2016-01-07 06:50:32 +01:00
d3d->should_resize = false;
2017-12-31 17:53:11 +01:00
d3d->menu = NULL;
2016-05-08 20:40:42 +02:00
video_context_driver_set((const gfx_ctx_driver_t*)ctx_driver);
2015-04-09 18:38:24 +02:00
if (!d3d8_init_internal(d3d, info, input, input_data))
{
RARCH_ERR("[D3D]: Failed to init D3D.\n");
2015-04-06 01:12:42 +02:00
goto error;
}
2016-01-07 06:50:32 +01:00
d3d->keep_aspect = info->force_aspect;
2016-01-07 06:50:32 +01:00
return d3d;
2015-04-06 01:12:42 +02:00
error:
2016-05-08 20:32:46 +02:00
video_context_driver_destroy();
2016-01-07 06:50:32 +01:00
if (d3d)
2017-10-02 07:48:57 +02:00
free(d3d);
2015-04-06 01:12:42 +02:00
return NULL;
}
2016-01-09 03:10:37 +01:00
#ifdef HAVE_OVERLAY
static void d3d8_free_overlays(d3d_video_t *d3d)
2016-01-09 03:10:37 +01:00
{
unsigned i;
if (!d3d)
return;
2017-10-02 06:12:33 +02:00
for (i = 0; i < d3d->overlays_size; i++)
d3d8_free_overlay(d3d, &d3d->overlays[i]);
2017-10-02 06:12:33 +02:00
free(d3d->overlays);
d3d->overlays = NULL;
d3d->overlays_size = 0;
2016-01-09 03:10:37 +01:00
}
#endif
static void d3d8_free(void *data)
{
2015-11-17 09:41:18 +01:00
d3d_video_t *d3d = (d3d_video_t*)data;
if (!d3d)
return;
#ifdef HAVE_OVERLAY
d3d8_free_overlays(d3d);
2017-10-02 06:12:33 +02:00
if (d3d->overlays)
free(d3d->overlays);
d3d->overlays = NULL;
d3d->overlays_size = 0;
#endif
2015-04-06 01:43:47 +02:00
d3d8_free_overlay(d3d, d3d->menu);
2016-01-07 04:59:39 +01:00
if (d3d->menu)
free(d3d->menu);
2017-10-02 06:12:33 +02:00
d3d->menu = NULL;
2016-01-07 00:03:05 +01:00
d3d8_deinitialize(d3d);
2017-10-02 06:12:33 +02:00
video_context_driver_free();
if (!string_is_empty(d3d->shader_path))
free(d3d->shader_path);
d3d->shader_path = NULL;
d3d_device_free(d3d->dev, g_pD3D8);
d3d->dev = NULL;
g_pD3D8 = NULL;
2018-01-17 08:51:56 +01:00
#ifndef _XBOX
2016-06-04 05:54:33 +02:00
win32_monitor_from_window();
2018-01-17 08:51:56 +01:00
#endif
if (d3d)
2017-10-02 07:48:57 +02:00
free(d3d);
2018-01-03 18:38:13 +01:00
d3d_deinitialize_symbols();
2017-09-14 22:24:04 +02:00
#ifndef _XBOX
2015-11-17 09:44:26 +01:00
win32_destroy_window();
2017-09-14 22:24:04 +02:00
#endif
}
#ifdef HAVE_OVERLAY
static void d3d8_overlay_tex_geom(
2015-11-10 05:11:49 +01:00
void *data,
unsigned index,
float x, float y,
float w, float h)
{
2015-11-10 05:11:49 +01:00
d3d_video_t *d3d = (d3d_video_t*)data;
2015-03-16 18:33:47 +01:00
if (!d3d)
return;
2015-11-11 03:48:05 +01:00
d3d->overlays[index].tex_coords[0] = x;
d3d->overlays[index].tex_coords[1] = y;
d3d->overlays[index].tex_coords[2] = w;
d3d->overlays[index].tex_coords[3] = h;
2018-01-23 02:55:33 +01:00
#ifdef _XBOX
d3d->overlays[index].tex_coords[0] *= d3d->overlays[index].tex_w;
d3d->overlays[index].tex_coords[1] *= d3d->overlays[index].tex_h;
d3d->overlays[index].tex_coords[2] *= d3d->overlays[index].tex_w;
d3d->overlays[index].tex_coords[3] *= d3d->overlays[index].tex_h;
#endif
}
static void d3d8_overlay_vertex_geom(
2015-11-10 05:11:49 +01:00
void *data,
unsigned index,
float x, float y,
float w, float h)
{
2015-11-10 05:11:49 +01:00
d3d_video_t *d3d = (d3d_video_t*)data;
2015-03-16 18:33:47 +01:00
if (!d3d)
return;
2015-11-11 03:48:05 +01:00
y = 1.0f - y;
h = -h;
d3d->overlays[index].vert_coords[0] = x;
d3d->overlays[index].vert_coords[1] = y;
d3d->overlays[index].vert_coords[2] = w;
d3d->overlays[index].vert_coords[3] = h;
}
static bool d3d8_overlay_load(void *data,
const void *image_data, unsigned num_images)
{
unsigned i, y;
2017-10-02 06:12:33 +02:00
overlay_t *new_overlays = NULL;
d3d_video_t *d3d = (d3d_video_t*)data;
2015-07-11 19:47:20 +02:00
const struct texture_image *images = (const struct texture_image*)
image_data;
2015-01-17 06:30:39 +01:00
2015-03-15 17:54:53 +01:00
if (!d3d)
2016-12-02 23:36:23 -05:00
return false;
2015-03-15 17:54:53 +01:00
d3d8_free_overlays(d3d);
2017-10-02 06:12:33 +02:00
d3d->overlays = (overlay_t*)calloc(num_images, sizeof(*d3d->overlays));
d3d->overlays_size = num_images;
for (i = 0; i < num_images; i++)
{
2015-01-17 06:30:39 +01:00
D3DLOCKED_RECT d3dlr;
2015-04-05 16:34:31 +02:00
unsigned width = images[i].width;
unsigned height = images[i].height;
2014-09-13 22:25:30 +02:00
overlay_t *overlay = (overlay_t*)&d3d->overlays[i];
2015-11-11 04:16:18 +01:00
overlay->tex = d3d_texture_new(d3d->dev, NULL,
2014-09-13 18:42:13 +02:00
width, height, 1,
0,
d3d_get_argb8888_format(),
2015-05-20 17:28:12 -05:00
D3DPOOL_MANAGED, 0, 0, 0,
NULL, NULL, false);
2014-09-13 18:42:13 +02:00
2014-09-13 22:25:30 +02:00
if (!overlay->tex)
{
RARCH_ERR("[D3D]: Failed to create overlay texture\n");
return false;
}
2015-11-09 15:43:46 +01:00
if (d3d_lock_rectangle(overlay->tex, 0, &d3dlr,
2015-11-09 15:39:47 +01:00
NULL, 0, D3DLOCK_NOSYSLOCK))
{
2015-01-17 06:30:39 +01:00
uint32_t *dst = (uint32_t*)(d3dlr.pBits);
const uint32_t *src = images[i].pixels;
2015-01-17 06:30:39 +01:00
unsigned pitch = d3dlr.Pitch >> 2;
for (y = 0; y < height; y++, dst += pitch, src += width)
memcpy(dst, src, width << 2);
2015-11-09 15:39:47 +01:00
d3d_unlock_rectangle(overlay->tex);
}
2015-04-05 16:34:31 +02:00
overlay->tex_w = width;
overlay->tex_h = height;
/* Default. Stretch to whole screen. */
d3d8_overlay_tex_geom(d3d, i, 0, 0, 1, 1);
d3d8_overlay_vertex_geom(d3d, i, 0, 0, 1, 1);
}
return true;
}
static void d3d8_overlay_enable(void *data, bool state)
{
unsigned i;
2015-04-09 18:38:24 +02:00
d3d_video_t *d3d = (d3d_video_t*)data;
2014-11-14 23:06:34 -05:00
if (!d3d)
return;
2017-10-02 06:12:33 +02:00
for (i = 0; i < d3d->overlays_size; i++)
d3d->overlays_enabled = state;
2016-05-08 20:32:46 +02:00
video_context_driver_show_mouse(&state);
}
static void d3d8_overlay_full_screen(void *data, bool enable)
{
unsigned i;
d3d_video_t *d3d = (d3d_video_t*)data;
2017-10-02 06:12:33 +02:00
for (i = 0; i < d3d->overlays_size; i++)
d3d->overlays[i].fullscreen = enable;
}
static void d3d8_overlay_set_alpha(void *data, unsigned index, float mod)
{
d3d_video_t *d3d = (d3d_video_t*)data;
2015-01-17 06:30:39 +01:00
if (d3d)
d3d->overlays[index].alpha_mod = mod;
}
static const video_overlay_interface_t d3d8_overlay_interface = {
d3d8_overlay_enable,
d3d8_overlay_load,
d3d8_overlay_tex_geom,
d3d8_overlay_vertex_geom,
d3d8_overlay_full_screen,
d3d8_overlay_set_alpha,
};
static void d3d8_get_overlay_interface(void *data,
const video_overlay_interface_t **iface)
{
(void)data;
*iface = &d3d8_overlay_interface;
}
2014-01-09 18:44:25 +01:00
#endif
static bool d3d8_frame(void *data, const void *frame,
unsigned frame_width, unsigned frame_height,
2015-08-03 23:01:07 +02:00
uint64_t frame_count, unsigned pitch,
2017-01-18 17:41:27 +01:00
const char *msg, video_frame_info_t *video_info)
2012-10-26 21:09:30 +02:00
{
2018-01-25 10:03:34 +01:00
D3DVIEWPORT8 screen_vp;
2015-09-20 10:02:47 +02:00
unsigned i = 0;
d3d_video_t *d3d = (d3d_video_t*)data;
unsigned width = video_info->width;
unsigned height = video_info->height;
2015-01-17 06:47:00 +01:00
(void)i;
if (!frame)
2014-01-09 18:44:25 +01:00
return true;
2014-09-08 19:28:10 +02:00
/* We cannot recover in fullscreen. */
2016-01-07 00:26:38 +01:00
if (d3d->needs_restore)
2014-01-09 18:44:25 +01:00
{
2018-01-17 08:51:56 +01:00
#ifndef _XBOX
HWND window = win32_get_window();
2016-01-07 00:26:38 +01:00
if (IsIconic(window))
return true;
2018-01-17 08:51:56 +01:00
#endif
2016-01-07 00:26:38 +01:00
if (!d3d_restore(d3d))
{
RARCH_ERR("[D3D]: Failed to restore.\n");
return false;
}
2014-01-09 18:44:25 +01:00
}
if (d3d->should_resize)
{
d3d8_set_viewport(d3d, width, height, false, true);
d3d->should_resize = false;
2014-01-09 18:44:25 +01:00
}
2015-05-20 17:28:12 -05:00
/* render_chain() only clears out viewport,
* clear out everything. */
2017-01-18 17:41:27 +01:00
screen_vp.X = 0;
screen_vp.Y = 0;
screen_vp.MinZ = 0;
screen_vp.MaxZ = 1;
screen_vp.Width = width;
screen_vp.Height = height;
2018-01-23 06:31:33 +01:00
d3d_set_viewports(d3d->dev, &screen_vp);
2016-01-07 06:33:50 +01:00
d3d_clear(d3d->dev, 0, 0, D3DCLEAR_TARGET, 0, 1, 0);
2014-01-09 18:44:25 +01:00
2015-05-20 17:28:12 -05:00
/* Insert black frame first, so we
* can screenshot, etc. */
2017-01-18 17:41:27 +01:00
if (video_info->black_frame_insertion)
2014-01-09 18:44:25 +01:00
{
2016-01-07 06:33:50 +01:00
if (!d3d_swap(d3d, d3d->dev) || d3d->needs_restore)
2014-01-09 18:44:25 +01:00
return true;
2016-01-07 06:33:50 +01:00
d3d_clear(d3d->dev, 0, 0, D3DCLEAR_TARGET, 0, 1, 0);
2014-01-09 18:44:25 +01:00
}
2018-01-23 07:18:46 +01:00
if (!d3d8_renderchain_render(
2015-04-06 06:46:35 +02:00
d3d,
frame, frame_width, frame_height,
2015-04-06 06:46:35 +02:00
pitch, d3d->dev_rotation))
2014-01-09 18:44:25 +01:00
{
RARCH_ERR("[D3D]: Failed to render scene.\n");
return false;
}
#ifdef HAVE_MENU
2014-06-10 02:21:49 +02:00
if (d3d->menu && d3d->menu->enabled)
2016-01-07 06:49:10 +01:00
{
d3d8_set_mvp(d3d, NULL, &d3d->mvp);
d3d8_overlay_render(d3d, video_info, d3d->menu);
d3d->menu_display.offset = 0;
d3d_set_vertex_declaration(d3d->dev, d3d->menu_display.decl);
d3d_set_stream_source(d3d->dev, 0, d3d->menu_display.buffer, 0, sizeof(Vertex));
2018-01-23 06:31:33 +01:00
d3d_set_viewports(d3d->dev, &screen_vp);
2017-01-18 21:23:18 +01:00
menu_driver_frame(video_info);
2016-01-07 06:49:10 +01:00
}
2014-01-09 18:44:25 +01:00
#endif
#ifdef HAVE_OVERLAY
2014-07-25 19:58:45 -05:00
if (d3d->overlays_enabled)
2014-01-09 18:44:25 +01:00
{
d3d8_set_mvp(d3d, NULL, &d3d->mvp);
2017-10-02 06:12:33 +02:00
for (i = 0; i < d3d->overlays_size; i++)
d3d8_overlay_render(d3d, video_info, &d3d->overlays[i]);
2014-01-09 18:44:25 +01:00
}
#endif
if (msg && *msg)
{
2018-01-23 06:31:33 +01:00
d3d_set_viewports(d3d->dev, &screen_vp);
font_driver_render_msg(video_info, NULL, msg, NULL);
}
video_info->cb_update_window_title(
video_info->context_data, video_info);
2017-05-18 04:03:49 +02:00
video_info->cb_swap_buffers(
video_info->context_data, video_info);
2014-01-09 18:44:25 +01:00
return true;
2012-10-26 21:09:30 +02:00
}
static bool d3d8_read_viewport(void *data, uint8_t *buffer, bool is_idle)
2012-10-26 21:09:30 +02:00
{
2018-01-23 07:18:46 +01:00
return false;
2012-10-26 21:09:30 +02:00
}
static bool d3d8_set_shader(void *data,
2014-09-08 19:28:10 +02:00
enum rarch_shader_type type, const char *path)
2012-10-31 16:28:09 +01:00
{
return false;
2012-10-31 16:28:09 +01:00
}
static void d3d8_set_menu_texture_frame(void *data,
const void *frame, bool rgb32, unsigned width, unsigned height,
float alpha)
{
2015-03-16 18:33:47 +01:00
D3DLOCKED_RECT d3dlr;
d3d_video_t *d3d = (d3d_video_t*)data;
2014-01-09 18:44:25 +01:00
2015-04-06 01:51:58 +02:00
(void)d3dlr;
(void)frame;
(void)rgb32;
(void)width;
(void)height;
(void)alpha;
2017-12-31 17:53:11 +01:00
if ( !d3d->menu->tex ||
d3d->menu->tex_w != width ||
d3d->menu->tex_h != height)
2014-01-09 18:44:25 +01:00
{
if (d3d->menu)
d3d_texture_free(d3d->menu->tex);
2015-11-11 04:16:18 +01:00
d3d->menu->tex = d3d_texture_new(d3d->dev, NULL,
2014-09-13 18:42:13 +02:00
width, height, 1,
0, d3d_get_argb8888_format(),
D3DPOOL_MANAGED, 0, 0, 0, NULL, NULL, false);
2014-09-13 18:42:13 +02:00
2014-09-13 18:44:04 +02:00
if (!d3d->menu->tex)
2014-01-09 18:44:25 +01:00
{
2014-06-10 02:21:49 +02:00
RARCH_ERR("[D3D]: Failed to create menu texture.\n");
2014-01-09 18:44:25 +01:00
return;
}
2014-09-13 18:42:13 +02:00
d3d->menu->tex_w = width;
d3d->menu->tex_h = height;
2018-01-23 02:55:33 +01:00
#ifdef _XBOX
d3d->menu->tex_coords [2] = width;
d3d->menu->tex_coords[3] = height;
#endif
2014-01-09 18:44:25 +01:00
}
2014-06-10 02:21:49 +02:00
d3d->menu->alpha_mod = alpha;
2014-01-09 18:44:25 +01:00
2015-11-09 15:39:47 +01:00
if (d3d_lock_rectangle(d3d->menu->tex, 0, &d3dlr,
NULL, 0, D3DLOCK_NOSYSLOCK))
2014-01-09 18:44:25 +01:00
{
2015-01-17 06:30:39 +01:00
unsigned h, w;
2014-01-09 18:44:25 +01:00
if (rgb32)
{
2015-01-17 06:30:39 +01:00
uint8_t *dst = (uint8_t*)d3dlr.pBits;
2014-01-09 18:44:25 +01:00
const uint32_t *src = (const uint32_t*)frame;
2015-01-17 06:30:39 +01:00
for (h = 0; h < height; h++, dst += d3dlr.Pitch, src += width)
2014-01-09 18:44:25 +01:00
{
memcpy(dst, src, width * sizeof(uint32_t));
2014-09-08 19:28:10 +02:00
memset(dst + width * sizeof(uint32_t), 0,
d3dlr.Pitch - width * sizeof(uint32_t));
2014-01-09 18:44:25 +01:00
}
}
else
{
2015-01-17 06:30:39 +01:00
uint32_t *dst = (uint32_t*)d3dlr.pBits;
2014-01-09 18:44:25 +01:00
const uint16_t *src = (const uint16_t*)frame;
2015-01-17 06:30:39 +01:00
for (h = 0; h < height; h++, dst += d3dlr.Pitch >> 2, src += width)
2014-01-09 18:44:25 +01:00
{
2015-01-17 06:30:39 +01:00
for (w = 0; w < width; w++)
2014-01-09 18:44:25 +01:00
{
uint16_t c = src[w];
uint32_t r = (c >> 12) & 0xf;
uint32_t g = (c >> 8) & 0xf;
uint32_t b = (c >> 4) & 0xf;
uint32_t a = (c >> 0) & 0xf;
2015-04-05 16:34:31 +02:00
r = ((r << 4) | r) << 16;
g = ((g << 4) | g) << 8;
b = ((b << 4) | b) << 0;
a = ((a << 4) | a) << 24;
dst[w] = r | g | b | a;
2014-01-09 18:44:25 +01:00
}
}
}
2015-11-09 15:39:47 +01:00
2014-06-10 02:21:49 +02:00
if (d3d->menu)
2015-11-09 15:39:47 +01:00
d3d_unlock_rectangle(d3d->menu->tex);
2014-01-09 18:44:25 +01:00
}
}
static void d3d8_set_menu_texture_enable(void *data,
2014-09-08 19:28:10 +02:00
bool state, bool full_screen)
{
d3d_video_t *d3d = (d3d_video_t*)data;
if (!d3d || !d3d->menu)
return;
2015-04-05 16:34:31 +02:00
d3d->menu->enabled = state;
d3d->menu->fullscreen = full_screen;
}
static void d3d8_video_texture_load_d3d(d3d_video_t *d3d,
2016-01-08 17:51:03 +01:00
struct texture_image *ti,
enum texture_filter_type filter_type,
uintptr_t *id)
{
D3DLOCKED_RECT d3dlr;
2018-01-25 04:28:50 +01:00
unsigned usage = 0;
LPDIRECT3DTEXTURE8 tex = d3d_texture_new(d3d->dev, NULL,
ti->width, ti->height, 0,
usage, d3d_get_argb8888_format(),
D3DPOOL_MANAGED, 0, 0, 0,
NULL, NULL, false);
if (!tex)
{
RARCH_ERR("[D3D]: Failed to create texture\n");
return;
}
if (d3d_lock_rectangle(tex, 0, &d3dlr,
NULL, 0, D3DLOCK_NOSYSLOCK))
{
unsigned i;
uint32_t *dst = (uint32_t*)(d3dlr.pBits);
const uint32_t *src = ti->pixels;
unsigned pitch = d3dlr.Pitch >> 2;
for (i = 0; i < ti->height; i++, dst += pitch, src += ti->width)
memcpy(dst, src, ti->width << 2);
d3d_unlock_rectangle(tex);
}
*id = (uintptr_t)tex;
}
static int d3d8_video_texture_load_wrap_d3d(void *data)
{
2015-12-20 21:08:03 +01:00
uintptr_t id = 0;
d3d8_video_texture_load_d3d((d3d_video_t*)video_driver_get_ptr(true),
2016-08-02 01:42:36 +02:00
(struct texture_image*)data, TEXTURE_FILTER_LINEAR, &id);
2015-12-20 21:08:03 +01:00
return id;
}
static uintptr_t d3d8_load_texture(void *video_data, void *data,
bool threaded, enum texture_filter_type filter_type)
{
2015-12-20 21:08:03 +01:00
uintptr_t id = 0;
if (threaded)
{
custom_command_method_t func = d3d8_video_texture_load_wrap_d3d;
2016-05-10 03:14:23 +02:00
return video_thread_texture_load(data, func);
}
d3d8_video_texture_load_d3d((d3d_video_t*)video_driver_get_ptr(false),
2016-08-02 01:42:36 +02:00
(struct texture_image*)data, filter_type, &id);
2015-12-20 21:08:03 +01:00
return id;
}
static void d3d8_unload_texture(void *data, uintptr_t id)
{
2018-01-25 04:28:50 +01:00
LPDIRECT3DTEXTURE8 texid;
2016-01-08 17:51:03 +01:00
if (!id)
2016-12-02 23:36:23 -05:00
return;
2016-01-08 17:51:03 +01:00
2018-01-25 04:28:50 +01:00
texid = (LPDIRECT3DTEXTURE8)id;
2016-01-08 20:51:30 +01:00
d3d_texture_free(texid);
}
2014-01-01 01:09:31 +01:00
static const video_poke_interface_t d3d_poke_interface = {
2017-11-14 21:34:03 +01:00
NULL, /* set_coords */
d3d8_set_mvp,
d3d8_load_texture,
d3d8_unload_texture,
NULL,
NULL,
NULL, /* get_video_output_size */
NULL, /* get_video_output_prev */
NULL, /* get_video_output_next */
2015-04-25 02:35:06 +02:00
NULL, /* get_current_framebuffer */
NULL, /* get_proc_address */
d3d8_set_aspect_ratio,
d3d8_apply_state_changes,
d3d8_set_menu_texture_frame,
d3d8_set_menu_texture_enable,
d3d8_set_osd_msg,
d3d8_show_mouse,
};
static void d3d8_get_poke_interface(void *data,
2014-09-08 19:28:10 +02:00
const video_poke_interface_t **iface)
{
(void)data;
2014-01-01 01:09:31 +01:00
*iface = &d3d_poke_interface;
}
2018-01-23 02:55:33 +01:00
video_driver_t video_d3d8 = {
d3d8_init,
d3d8_frame,
d3d8_set_nonblock_state,
d3d8_alive,
NULL, /* focus */
d3d8_suppress_screensaver,
NULL, /* has_windowed */
d3d8_set_shader,
d3d8_free,
2018-01-23 02:55:33 +01:00
"d3d8",
d3d8_set_viewport,
d3d8_set_rotation,
d3d8_viewport_info,
d3d8_read_viewport,
NULL, /* read_frame_raw */
2013-03-27 15:55:59 +01:00
#ifdef HAVE_OVERLAY
d3d8_get_overlay_interface,
2013-03-27 15:55:59 +01:00
#endif
d3d8_get_poke_interface
2014-01-22 16:38:42 +01:00
};