RetroArch/gfx/drivers_display/gfx_display_d3d9hlsl.c

339 lines
9.6 KiB
C
Raw Normal View History

/* RetroArch - A frontend for libretro.
2017-01-22 13:40:32 +01:00
* 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/>.
*/
2018-04-22 14:27:39 +02:00
#define CINTERFACE
#include <retro_miscellaneous.h>
2005-04-23 20:22:55 +02:00
#include <gfx/math/matrix_4x4.h>
2016-09-08 06:02:41 +02:00
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
#include "../gfx_display.h"
2016-09-08 06:02:41 +02:00
#include "../../retroarch.h"
#include "../common/d3d_common.h"
#include "../common/d3d9_common.h"
2016-12-02 01:46:16 +01:00
static const float d3d9_hlsl_vertexes[] = {
0, 0,
1, 0,
0, 1,
1, 1
};
static const float d3d9_hlsl_tex_coords[] = {
0, 1,
1, 1,
0, 0,
1, 0
};
static const float *gfx_display_d3d9_hlsl_get_default_vertices(void)
2016-04-15 03:24:21 +02:00
{
return &d3d9_hlsl_vertexes[0];
2016-04-15 03:24:21 +02:00
}
static const float *gfx_display_d3d9_hlsl_get_default_tex_coords(void)
2016-04-15 03:24:21 +02:00
{
return &d3d9_hlsl_tex_coords[0];
2016-04-15 03:24:21 +02:00
}
static void *gfx_display_d3d9_hlsl_get_default_mvp(void *data)
2015-11-20 17:45:10 +01:00
{
2022-04-16 22:33:18 +02:00
static float id[] = { 1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
return &id;
}
2022-04-22 14:41:50 +02:00
static INT32 gfx_display_prim_to_d3d9_hlsl_enum(
2020-02-16 15:10:07 +01:00
enum gfx_display_prim_type prim_type)
{
switch (prim_type)
{
2020-02-16 15:10:07 +01:00
case GFX_DISPLAY_PRIM_TRIANGLES:
case GFX_DISPLAY_PRIM_TRIANGLESTRIP:
return D3DPT_COMM_TRIANGLESTRIP;
2020-02-16 15:10:07 +01:00
case GFX_DISPLAY_PRIM_NONE:
default:
break;
}
2022-04-16 22:33:18 +02:00
/* TODO/FIXME - hack */
return 0;
}
static void gfx_display_d3d9_hlsl_blend_begin(void *data)
{
d3d9_video_t *d3d = (d3d9_video_t*)data;
if (!d3d)
return;
IDirect3DDevice9_SetRenderState(d3d->dev, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
IDirect3DDevice9_SetRenderState(d3d->dev, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
IDirect3DDevice9_SetRenderState(d3d->dev, D3DRS_ALPHABLENDENABLE, true);
}
static void gfx_display_d3d9_hlsl_blend_end(void *data)
{
d3d9_video_t *d3d = (d3d9_video_t*)data;
if (!d3d)
return;
IDirect3DDevice9_SetRenderState(d3d->dev, D3DRS_ALPHABLENDENABLE, false);
}
2020-02-16 15:10:07 +01:00
static void gfx_display_d3d9_bind_texture(gfx_display_ctx_draw_t *draw,
d3d9_video_t *d3d)
{
LPDIRECT3DDEVICE9 dev = d3d->dev;
2018-05-03 15:20:06 +02:00
IDirect3DDevice9_SetTexture(dev, 0,
(IDirect3DBaseTexture9*)draw->texture);
IDirect3DDevice9_SetSamplerState(dev,
0, D3DSAMP_ADDRESSU, D3DTADDRESS_COMM_CLAMP);
IDirect3DDevice9_SetSamplerState(dev,
0, D3DSAMP_ADDRESSV, D3DTADDRESS_COMM_CLAMP);
IDirect3DDevice9_SetSamplerState(dev,
0, D3DSAMP_MINFILTER, D3DTEXF_COMM_LINEAR);
IDirect3DDevice9_SetSamplerState(dev,
0, D3DSAMP_MAGFILTER, D3DTEXF_COMM_LINEAR);
IDirect3DDevice9_SetSamplerState(dev, 0,
D3DSAMP_MIPFILTER, D3DTEXF_COMM_LINEAR);
}
static void gfx_display_d3d9_hlsl_draw(gfx_display_ctx_draw_t *draw,
void *data, unsigned video_width, unsigned video_height)
{
unsigned i;
math_matrix_4x4 mop, m1, m2;
2018-05-03 15:20:06 +02:00
LPDIRECT3DDEVICE9 dev;
2022-04-22 14:41:50 +02:00
D3DPRIMITIVETYPE type;
unsigned start = 0;
unsigned count = 0;
d3d9_video_t *d3d = (d3d9_video_t*)data;
Vertex * pv = NULL;
const float *vertex = NULL;
const float *tex_coord = NULL;
const float *color = NULL;
2020-08-14 15:58:43 +02:00
if (!d3d || !draw || draw->pipeline_id)
return;
2018-05-03 15:20:06 +02:00
2018-05-03 22:08:14 +02:00
dev = d3d->dev;
2018-05-03 15:20:06 +02:00
2020-02-10 13:18:27 +01:00
if ((d3d->menu_display.offset + draw->coords->vertices )
2018-01-21 07:57:25 +01:00
> (unsigned)d3d->menu_display.size)
return;
2022-04-25 17:06:11 +02:00
IDirect3DVertexBuffer9_Lock((LPDIRECT3DVERTEXBUFFER9)
d3d->menu_display.buffer, 0, 0, (void**)&pv, 0);
2018-01-19 03:40:34 +01:00
if (!pv)
return;
2018-01-19 03:40:34 +01:00
pv += d3d->menu_display.offset;
vertex = draw->coords->vertex;
tex_coord = draw->coords->tex_coord;
color = draw->coords->color;
2017-11-15 16:51:29 +01:00
if (!vertex)
vertex = &d3d9_hlsl_vertexes[0];
if (!tex_coord)
tex_coord = &d3d9_hlsl_tex_coords[0];
for (i = 0; i < draw->coords->vertices; i++)
{
int colors[4];
2018-01-19 03:40:34 +01:00
colors[0] = *color++ * 0xFF;
colors[1] = *color++ * 0xFF;
colors[2] = *color++ * 0xFF;
colors[3] = *color++ * 0xFF;
pv[i].x = *vertex++;
pv[i].y = *vertex++;
pv[i].z = 0.5f;
pv[i].u = *tex_coord++;
pv[i].v = *tex_coord++;
pv[i].color =
D3DCOLOR_ARGB(
colors[3], /* A */
colors[0], /* R */
colors[1], /* G */
colors[2] /* B */
);
}
2022-04-25 17:06:11 +02:00
IDirect3DVertexBuffer9_Unlock((LPDIRECT3DVERTEXBUFFER9)
2019-04-08 15:22:21 +02:00
d3d->menu_display.buffer);
2017-11-15 16:51:29 +01:00
2020-02-10 13:18:27 +01:00
if (!draw->matrix_data)
draw->matrix_data = gfx_display_d3d9_hlsl_get_default_mvp(d3d);
/* ugh */
2018-01-19 03:40:34 +01:00
matrix_4x4_scale(m1, 2.0, 2.0, 0);
matrix_4x4_translate(mop, -1.0, -1.0, 0);
matrix_4x4_multiply(m2, mop, m1);
2018-01-19 03:40:34 +01:00
matrix_4x4_multiply(m1,
*((math_matrix_4x4*)draw->matrix_data), m2);
matrix_4x4_scale(mop,
(draw->width / 2.0) / video_width,
(draw->height / 2.0) / video_height, 0);
matrix_4x4_multiply(m2, mop, m1);
2018-01-19 03:40:34 +01:00
matrix_4x4_translate(mop,
(draw->x + (draw->width / 2.0)) / video_width,
(draw->y + (draw->height / 2.0)) / video_height,
2018-01-19 03:40:34 +01:00
0);
matrix_4x4_multiply(m1, mop, m2);
matrix_4x4_multiply(m2, d3d->mvp_transposed, m1);
IDirect3DDevice9_SetVertexShaderConstantF(d3d->dev,
0, (const float*)&m2, 4);
if (draw && draw->texture)
2020-02-16 15:10:07 +01:00
gfx_display_d3d9_bind_texture(draw, d3d);
2022-04-22 14:41:50 +02:00
type = (D3DPRIMITIVETYPE)gfx_display_prim_to_d3d9_hlsl_enum(draw->prim_type);
2022-04-22 14:37:21 +02:00
start = d3d->menu_display.offset;
count = draw->coords->vertices -
2020-02-16 15:10:07 +01:00
((draw->prim_type == GFX_DISPLAY_PRIM_TRIANGLESTRIP)
2022-04-22 14:37:21 +02:00
? 2 : 0);
IDirect3DDevice9_BeginScene(dev);
IDirect3DDevice9_DrawPrimitive(dev, type, start, count);
IDirect3DDevice9_EndScene(dev);
d3d->menu_display.offset += draw->coords->vertices;
}
static void gfx_display_d3d9_hlsl_draw_pipeline(
gfx_display_ctx_draw_t *draw,
2021-04-08 00:38:24 +02:00
gfx_display_t *p_disp,
void *data, unsigned video_width, unsigned video_height)
{
static float t = 0;
2018-01-19 03:40:34 +01:00
video_coord_array_t *ca = NULL;
2019-02-03 15:49:35 -08:00
2018-01-19 03:40:34 +01:00
if (!draw)
return;
2020-09-25 10:31:07 +02:00
ca = &p_disp->dispca;
2018-01-19 03:40:34 +01:00
draw->x = 0;
draw->y = 0;
draw->coords = NULL;
draw->matrix_data = NULL;
2018-01-19 03:40:34 +01:00
if (ca)
draw->coords = (struct video_coords*)&ca->coords;
2020-08-14 15:58:43 +02:00
switch (draw->pipeline_id)
{
case VIDEO_SHADER_MENU:
2016-12-06 17:44:38 +01:00
case VIDEO_SHADER_MENU_2:
case VIDEO_SHADER_MENU_3:
/* TODO/FIXME - implement */
#if 0
2018-01-19 03:40:34 +01:00
{
struct uniform_info uniform_param = {0};
t += 0.01;
uniform_param.enabled = true;
uniform_param.lookup.enable = true;
uniform_param.lookup.add_prefix = true;
2020-08-14 15:58:43 +02:00
uniform_param.lookup.idx = draw->pipeline_id;
2018-01-19 03:40:34 +01:00
uniform_param.lookup.type = SHADER_PROGRAM_VERTEX;
uniform_param.type = UNIFORM_1F;
uniform_param.lookup.ident = "time";
uniform_param.result.f.v0 = t;
}
#endif
break;
}
}
2022-04-22 07:15:54 +08:00
static bool gfx_display_d3d9_hlsl_font_init_first(
2016-02-09 16:11:37 +01:00
void **font_handle, void *video_data,
const char *font_path, float menu_font_size,
bool is_threaded)
{
font_data_t **handle = (font_data_t**)font_handle;
2017-10-03 00:56:18 +02:00
if (!(*handle = font_driver_init_first(video_data,
font_path, menu_font_size, true,
is_threaded,
FONT_DRIVER_RENDER_D3D9_API)))
2017-10-03 00:56:18 +02:00
return false;
return true;
}
void gfx_display_d3d9_hlsl_scissor_begin(
void *data,
unsigned video_width, unsigned video_height,
2019-04-08 15:22:21 +02:00
int x, int y, unsigned width, unsigned height)
2019-01-26 20:12:49 -05:00
{
2019-04-08 15:22:21 +02:00
RECT rect;
d3d9_video_t *d3d9 = (d3d9_video_t*)data;
2019-01-26 20:12:49 -05:00
if (!d3d9 || !width || !height)
return;
2019-04-08 15:22:21 +02:00
rect.left = x;
rect.top = y;
rect.right = width + x;
rect.bottom = height + y;
IDirect3DDevice9_SetScissorRect(d3d9->dev, &rect);
2019-01-26 20:12:49 -05:00
}
void gfx_display_d3d9_hlsl_scissor_end(void *data,
unsigned video_width, unsigned video_height)
2019-01-26 20:12:49 -05:00
{
2019-04-08 15:22:21 +02:00
RECT rect;
d3d9_video_t *d3d9 = (d3d9_video_t*)data;
2019-01-26 20:12:49 -05:00
if (!d3d9)
return;
2019-04-10 00:22:36 -04:00
rect.left = 0;
rect.top = 0;
rect.right = video_width;
rect.bottom = video_height;
2019-01-26 20:12:49 -05:00
IDirect3DDevice9_SetScissorRect(d3d9->dev, &rect);
2019-01-26 20:12:49 -05:00
}
gfx_display_ctx_driver_t gfx_display_ctx_d3d9_hlsl = {
gfx_display_d3d9_hlsl_draw,
gfx_display_d3d9_hlsl_draw_pipeline,
gfx_display_d3d9_hlsl_blend_begin,
gfx_display_d3d9_hlsl_blend_end,
gfx_display_d3d9_hlsl_get_default_mvp,
gfx_display_d3d9_hlsl_get_default_vertices,
gfx_display_d3d9_hlsl_get_default_tex_coords,
2022-04-22 07:15:54 +08:00
gfx_display_d3d9_hlsl_font_init_first,
GFX_VIDEO_DRIVER_DIRECT3D9_HLSL,
"d3d9_hlsl",
false,
gfx_display_d3d9_hlsl_scissor_begin,
gfx_display_d3d9_hlsl_scissor_end
};