RetroArch/gfx/common/d3d9_common.h

526 lines
13 KiB
C
Raw Normal View History

/* RetroArch - A frontend for libretro.
* Copyright (C) 2011-2017 - Daniel De Matteis
*
* 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/>.
*/
#ifndef _D3D9_COMMON_H
#define _D3D9_COMMON_H
#include <boolean.h>
#include <retro_common_api.h>
2018-04-22 14:00:32 +02:00
#include <retro_inline.h>
2018-05-03 22:03:12 +02:00
#include <gfx/math/matrix_4x4.h>
2018-04-22 14:00:32 +02:00
#include <d3d9.h>
2018-05-14 03:57:27 +02:00
#include "d3d_common.h"
#include "../../retroarch.h"
2018-04-22 14:00:32 +02:00
#include "../../verbosity.h"
#define D3D9_DECL_FVF_TEXCOORD(stream, offset, index) \
{ (WORD)(stream), (WORD)(offset * sizeof(float)), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, \
D3DDECLUSAGE_TEXCOORD, (BYTE)(index) }
RETRO_BEGIN_DECLS
typedef struct d3d9_video d3d9_video_t;
2018-05-03 22:03:12 +02:00
typedef struct d3d9_video
{
bool keep_aspect;
bool should_resize;
bool quitting;
bool needs_restore;
bool overlays_enabled;
/* TODO - refactor this away properly. */
bool resolution_hd_enable;
/* Only used for Xbox */
bool widescreen_mode;
2018-05-03 22:03:12 +02:00
unsigned cur_mon_id;
unsigned dev_rotation;
overlay_t *menu;
void *renderchain_data;
RECT font_rect;
RECT font_rect_shifted;
math_matrix_4x4 mvp;
math_matrix_4x4 mvp_rotate;
math_matrix_4x4 mvp_transposed;
struct video_viewport vp;
struct video_shader shader;
video_info_t video_info;
2018-05-14 03:57:27 +02:00
#ifdef HAVE_WINDOW
2018-05-03 22:03:12 +02:00
WNDCLASSEX windowClass;
2018-05-14 03:57:27 +02:00
#endif
2018-05-03 22:03:12 +02:00
LPDIRECT3DDEVICE9 dev;
2018-05-13 18:24:19 +02:00
D3DVIEWPORT9 final_viewport;
2018-05-03 22:03:12 +02:00
char *shader_path;
struct
{
int size;
int offset;
void *buffer;
void *decl;
}menu_display;
size_t overlays_size;
overlay_t *overlays;
} d3d9_video_t;
void *d3d9_vertex_buffer_new(void *dev,
unsigned length, unsigned usage, unsigned fvf,
INT32 pool, void *handle);
2018-04-22 14:20:59 +02:00
static INLINE void *d3d9_vertex_buffer_lock(LPDIRECT3DVERTEXBUFFER9 vertbuf)
2018-04-22 14:00:32 +02:00
{
2018-04-22 14:20:59 +02:00
void *buf = NULL;
2018-04-22 14:00:32 +02:00
if (!vertbuf)
return NULL;
IDirect3DVertexBuffer9_Lock(vertbuf, 0, 0, &buf, 0);
return buf;
}
2018-04-22 14:20:59 +02:00
static INLINE void d3d9_vertex_buffer_unlock(LPDIRECT3DVERTEXBUFFER9 vertbuf)
2018-04-22 14:00:32 +02:00
{
2018-04-22 14:20:59 +02:00
if (vertbuf)
IDirect3DVertexBuffer9_Unlock(vertbuf);
2018-04-22 14:00:32 +02:00
}
void d3d9_vertex_buffer_free(void *vertex_data, void *vertex_declaration);
2018-04-22 14:20:59 +02:00
static INLINE bool d3d9_texture_get_surface_level(
LPDIRECT3DTEXTURE9 tex,
2018-04-22 14:00:32 +02:00
unsigned idx, void **_ppsurface_level)
{
2019-02-03 15:49:35 -08:00
if (tex &&
2018-04-22 14:00:32 +02:00
SUCCEEDED(IDirect3DTexture9_GetSurfaceLevel(
tex, idx, (IDirect3DSurface9**)_ppsurface_level)))
return true;
return false;
}
void *d3d9_texture_new(void *dev,
const char *path, unsigned width, unsigned height,
unsigned miplevels, unsigned usage, INT32 format,
INT32 pool, unsigned filter, unsigned mipfilter,
INT32 color_key, void *src_info,
PALETTEENTRY *palette, bool want_mipmap);
2018-04-22 14:00:32 +02:00
static INLINE void d3d9_set_stream_source(
2018-04-22 14:20:59 +02:00
LPDIRECT3DDEVICE9 dev,
unsigned stream_no,
LPDIRECT3DVERTEXBUFFER9 stream_vertbuf,
unsigned offset_bytes,
2018-04-22 14:00:32 +02:00
unsigned stride)
{
if (stream_vertbuf)
IDirect3DDevice9_SetStreamSource(dev, stream_no, stream_vertbuf,
offset_bytes,
stride);
}
2018-04-22 14:20:59 +02:00
static INLINE bool d3d9_lock_rectangle(
LPDIRECT3DTEXTURE9 tex,
unsigned level,
D3DLOCKED_RECT *lr,
const RECT *rect,
2018-04-22 14:00:32 +02:00
unsigned rectangle_height, unsigned flags)
{
if (!tex)
return false;
#ifdef _XBOX
2018-04-22 14:20:59 +02:00
IDirect3DTexture9_LockRect(tex, level, lr, rect, flags);
2018-04-22 14:00:32 +02:00
#else
2018-04-22 14:20:59 +02:00
if (IDirect3DTexture9_LockRect(tex, level, lr, rect, flags) != D3D_OK)
2018-04-22 14:00:32 +02:00
return false;
#endif
2018-04-22 14:00:32 +02:00
return true;
}
static INLINE void d3d9_lock_rectangle_clear(void *tex,
2018-05-14 05:04:45 +02:00
unsigned level, D3DLOCKED_RECT *lr, RECT *rect,
2018-04-22 14:00:32 +02:00
unsigned rectangle_height, unsigned flags)
{
#if defined(_XBOX)
level = 0;
#endif
memset(lr->pBits, level, rectangle_height * lr->Pitch);
IDirect3DTexture9_UnlockRect((LPDIRECT3DTEXTURE9)tex, 0);
2018-04-22 14:00:32 +02:00
}
static INLINE bool d3d9_create_vertex_shader(
2018-04-22 14:20:59 +02:00
LPDIRECT3DDEVICE9 dev, const DWORD *a, void **b)
2018-04-22 14:00:32 +02:00
{
if (dev && IDirect3DDevice9_CreateVertexShader(dev, a,
(LPDIRECT3DVERTEXSHADER9*)b) == D3D_OK)
return true;
return false;
}
static INLINE bool d3d9_create_pixel_shader(
2018-04-22 14:20:59 +02:00
LPDIRECT3DDEVICE9 dev, const DWORD *a, void **b)
2018-04-22 14:00:32 +02:00
{
if (dev &&
IDirect3DDevice9_CreatePixelShader(dev, a,
(LPDIRECT3DPIXELSHADER9*)b) == D3D_OK)
return true;
return false;
}
2018-04-22 14:20:59 +02:00
static INLINE void d3d9_texture_blit(
unsigned pixel_size,
void *tex,
2018-04-22 14:20:59 +02:00
D3DLOCKED_RECT *lr, const void *frame,
2018-04-22 14:00:32 +02:00
unsigned width, unsigned height, unsigned pitch)
{
unsigned y;
for (y = 0; y < height; y++)
{
const uint8_t *in = (const uint8_t*)frame + y * pitch;
uint8_t *out = (uint8_t*)lr->pBits + y * lr->Pitch;
2018-04-22 14:00:32 +02:00
memcpy(out, in, width * pixel_size);
}
}
2018-04-22 14:20:59 +02:00
static INLINE bool d3d9_vertex_declaration_new(
LPDIRECT3DDEVICE9 dev,
2018-04-22 14:00:32 +02:00
const void *vertex_data, void **decl_data)
{
const D3DVERTEXELEMENT9 *vertex_elements = (const D3DVERTEXELEMENT9*)vertex_data;
LPDIRECT3DVERTEXDECLARATION9 **vertex_decl = (LPDIRECT3DVERTEXDECLARATION9**)decl_data;
if (SUCCEEDED(IDirect3DDevice9_CreateVertexDeclaration(dev,
vertex_elements, (IDirect3DVertexDeclaration9**)vertex_decl)))
return true;
return false;
}
void d3d9_frame_postprocess(void *data);
2018-04-23 15:40:43 +02:00
static INLINE bool d3d9_device_get_render_target_data(
2018-05-14 05:04:45 +02:00
LPDIRECT3DDEVICE9 dev,
LPDIRECT3DSURFACE9 src, LPDIRECT3DSURFACE9 dst)
2018-04-23 15:40:43 +02:00
{
#ifndef _XBOX
if (dev &&
SUCCEEDED(IDirect3DDevice9_GetRenderTargetData(
dev, src, dst)))
return true;
#endif
return false;
}
2018-04-22 14:20:59 +02:00
static INLINE bool d3d9_device_get_render_target(
LPDIRECT3DDEVICE9 dev,
2018-04-22 14:00:32 +02:00
unsigned idx, void **data)
{
if (dev &&
SUCCEEDED(IDirect3DDevice9_GetRenderTarget(dev,
idx, (LPDIRECT3DSURFACE9*)data)))
return true;
return false;
}
2018-04-23 15:40:43 +02:00
static INLINE bool d3d9_device_create_offscreen_plain_surface(
2018-05-14 05:04:45 +02:00
LPDIRECT3DDEVICE9 dev,
unsigned width,
unsigned height,
unsigned format,
unsigned pool,
void **surf_data,
2018-04-23 15:40:43 +02:00
void *data)
{
#ifndef _XBOX
if (SUCCEEDED(IDirect3DDevice9_CreateOffscreenPlainSurface(dev,
width, height,
(D3DFORMAT)format, (D3DPOOL)pool,
(LPDIRECT3DSURFACE9*)surf_data,
(HANDLE*)data)))
return true;
#endif
return false;
}
2018-04-22 14:00:32 +02:00
static INLINE bool d3d9_get_adapter_display_mode(
2018-04-22 14:20:59 +02:00
LPDIRECT3D9 d3d,
unsigned idx,
2018-05-14 05:04:45 +02:00
D3DDISPLAYMODE *display_mode)
2018-04-22 14:00:32 +02:00
{
if (!d3d)
return false;
#ifndef _XBOX
2018-04-22 14:20:59 +02:00
if (FAILED(
IDirect3D9_GetAdapterDisplayMode(
2018-05-14 05:04:45 +02:00
d3d, idx, display_mode)))
2018-04-22 14:00:32 +02:00
return false;
#endif
return true;
}
bool d3d9_create_device(void *dev,
void *d3dpp,
void *d3d,
HWND focus_window,
unsigned cur_mon_id);
bool d3d9_reset(void *dev, void *d3dpp);
2018-05-14 05:04:45 +02:00
static INLINE void d3d9_device_free(LPDIRECT3DDEVICE9 dev, LPDIRECT3D9 pd3d)
2018-04-23 15:40:43 +02:00
{
if (dev)
IDirect3DDevice9_Release(dev);
if (pd3d)
IDirect3D9_Release(pd3d);
}
void *d3d9_create(void);
bool d3d9_initialize_symbols(enum gfx_ctx_api api);
void d3d9_deinitialize_symbols(void);
2018-04-22 14:20:59 +02:00
static INLINE bool d3d9_check_device_type(
LPDIRECT3D9 d3d,
unsigned idx,
INT32 disp_format,
INT32 backbuffer_format,
2018-04-22 14:00:32 +02:00
bool windowed_mode)
{
if (d3d &&
SUCCEEDED(IDirect3D9_CheckDeviceType(d3d,
0,
D3DDEVTYPE_HAL,
(D3DFORMAT)disp_format,
(D3DFORMAT)backbuffer_format,
windowed_mode)))
return true;
return false;
}
bool d3d9x_create_font_indirect(void *dev,
void *desc, void **font_data);
void d3d9x_font_draw_text(void *data, void *sprite_data, void *string_data,
unsigned count, void *rect_data, unsigned format, unsigned color);
void d3d9x_font_get_text_metrics(void *data, void *metrics);
2018-04-23 15:04:19 +02:00
void d3d9x_buffer_release(void *data);
void d3d9x_font_release(void *data);
bool d3d9x_compile_shader(
const char *src,
unsigned src_data_len,
const void *pdefines,
void *pinclude,
const char *pfunctionname,
const char *pprofile,
unsigned flags,
void *ppshader,
void *pperrormsgs,
void *ppconstanttable);
bool d3d9x_compile_shader_from_file(
const char *src,
const void *pdefines,
void *pinclude,
const char *pfunctionname,
const char *pprofile,
unsigned flags,
void *ppshader,
void *pperrormsgs,
void *ppconstanttable);
void d3d9x_constant_table_set_float_array(LPDIRECT3DDEVICE9 dev,
void *p, void *_handle, const void *_pf, unsigned count);
void d3d9x_constant_table_set_defaults(LPDIRECT3DDEVICE9 dev,
void *p);
2018-04-23 17:04:30 +02:00
void d3d9x_constant_table_set_matrix(LPDIRECT3DDEVICE9 dev,
void *p, void *data, const void *matrix);
const void *d3d9x_get_buffer_ptr(void *data);
const bool d3d9x_constant_table_set_float(void *p,
void *a, void *b, float val);
2018-04-23 15:26:35 +02:00
void *d3d9x_constant_table_get_constant_by_name(void *_tbl,
void *_handle, void *_name);
2018-04-22 14:00:32 +02:00
static INLINE INT32 d3d9_get_rgb565_format(void)
{
#ifdef _XBOX
return D3DFMT_LIN_R5G6B5;
#else
return D3DFMT_R5G6B5;
#endif
}
static INLINE INT32 d3d9_get_argb8888_format(void)
{
#ifdef _XBOX
return D3DFMT_LIN_A8R8G8B8;
#else
return D3DFMT_A8R8G8B8;
#endif
}
static INLINE INT32 d3d9_get_xrgb8888_format(void)
{
#ifdef _XBOX
return D3DFMT_LIN_X8R8G8B8;
#else
return D3DFMT_X8R8G8B8;
#endif
}
2018-05-14 21:32:14 +02:00
static INLINE void d3d9_convert_geometry(
const struct LinkInfo *info,
unsigned *out_width,
unsigned *out_height,
unsigned width,
unsigned height,
D3DVIEWPORT9 *final_viewport)
{
if (!info)
return;
switch (info->pass->fbo.type_x)
{
case RARCH_SCALE_VIEWPORT:
*out_width = info->pass->fbo.scale_x * final_viewport->Width;
break;
case RARCH_SCALE_ABSOLUTE:
*out_width = info->pass->fbo.abs_x;
break;
case RARCH_SCALE_INPUT:
*out_width = info->pass->fbo.scale_x * width;
break;
}
switch (info->pass->fbo.type_y)
{
case RARCH_SCALE_VIEWPORT:
*out_height = info->pass->fbo.scale_y * final_viewport->Height;
break;
case RARCH_SCALE_ABSOLUTE:
*out_height = info->pass->fbo.abs_y;
break;
case RARCH_SCALE_INPUT:
*out_height = info->pass->fbo.scale_y * height;
break;
}
}
2022-04-19 11:03:24 +02:00
void d3d9_make_d3dpp(d3d9_video_t *d3d,
const video_info_t *info, void *_d3dpp);
2022-04-19 11:03:24 +02:00
void d3d9_calculate_rect(d3d9_video_t *d3d,
unsigned *width, unsigned *height,
int *x, int *y,
bool force_full,
bool allow_rotate);
void d3d9_log_info(const struct LinkInfo *info);
#ifdef HAVE_OVERLAY
void d3d9_free_overlays(d3d9_video_t *d3d);
#endif
#if defined(HAVE_MENU) || defined(HAVE_OVERLAY)
void d3d9_free_overlay(d3d9_video_t *d3d, overlay_t *overlay);
void d3d9_overlay_render(d3d9_video_t *d3d,
unsigned width,
unsigned height,
overlay_t *overlay, bool force_linear);
#endif
#if defined(HAVE_OVERLAY)
void d3d9_get_overlay_interface(void *data,
const video_overlay_interface_t **iface);
#endif
void d3d9_set_rotation(void *data, unsigned rot);
void d3d9_viewport_info(void *data, struct video_viewport *vp);
bool d3d9_read_viewport(void *data, uint8_t *buffer, bool is_idle);
bool d3d9_has_windowed(void *data);
bool d3d9_suppress_screensaver(void *data, bool enable);
bool d3d9_process_shader(d3d9_video_t *d3d);
uintptr_t d3d9_load_texture(void *video_data, void *data,
bool threaded, enum texture_filter_type filter_type);
void d3d9_set_osd_msg(void *data,
const char *msg,
const void *params, void *font);
void d3d9_unload_texture(void *data,
bool threaded, uintptr_t id);
void d3d9_set_video_mode(void *data,
unsigned width, unsigned height,
bool fullscreen);
void d3d9_set_aspect_ratio(void *data, unsigned aspect_ratio_idx);
void d3d9_set_menu_texture_frame(void *data,
const void *frame, bool rgb32, unsigned width, unsigned height,
float alpha);
2022-04-19 11:03:24 +02:00
void d3d9_set_viewport(void *data,
unsigned width, unsigned height,
bool force_full,
bool allow_rotate);
void d3d9_set_menu_texture_enable(void *data,
bool state, bool full_screen);
void d3d9_blit_to_texture(
LPDIRECT3DTEXTURE9 tex,
const void *frame,
unsigned tex_width, unsigned tex_height,
unsigned width, unsigned height,
unsigned last_width, unsigned last_height,
unsigned pitch, unsigned pixel_size);
2022-04-19 11:03:24 +02:00
void d3d9_apply_state_changes(void *data);
extern LPDIRECT3D9 g_pD3D9;
RETRO_END_DECLS
#endif