RetroArch/menu/drivers_display/menu_display_d3d.c

303 lines
8.4 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/>.
*/
#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 "../menu_driver.h"
2016-09-08 06:02:41 +02:00
2016-01-19 23:46:52 +01:00
#include "../../retroarch.h"
#include "../../gfx/font_driver.h"
#include "../../gfx/video_driver.h"
2016-08-01 22:23:01 +02:00
#include "../../gfx/drivers/d3d.h"
2015-11-18 13:00:13 +01:00
#include "../../gfx/common/d3d_common.h"
2016-12-02 01:46:16 +01:00
#define BYTE_CLAMP(i) (int) ((((i) > 255) ? 255 : (((i) < 0) ? 0 : (i))))
static const float d3d_vertexes[] = {
0, 0,
1, 0,
0, 1,
1, 1
};
static const float d3d_tex_coords[] = {
0, 1,
1, 1,
0, 0,
1, 0
};
2016-04-15 03:24:21 +02:00
static const float *menu_display_d3d_get_default_vertices(void)
{
return &d3d_vertexes[0];
}
static const float *menu_display_d3d_get_default_tex_coords(void)
{
return &d3d_tex_coords[0];
}
2015-11-20 17:45:10 +01:00
static void *menu_display_d3d_get_default_mvp(void)
{
static math_matrix_4x4 id;
matrix_4x4_identity(id);
return &id;
}
2018-01-15 21:44:34 +01:00
static D3DPRIMITIVETYPE menu_display_prim_to_d3d_enum(
2016-02-09 16:11:37 +01:00
enum menu_display_prim_type prim_type)
{
switch (prim_type)
{
case MENU_DISPLAY_PRIM_TRIANGLES:
case MENU_DISPLAY_PRIM_TRIANGLESTRIP:
return D3DPT_TRIANGLESTRIP;
case MENU_DISPLAY_PRIM_NONE:
default:
break;
}
2018-01-15 21:44:34 +01:00
/* TOD/FIXME - hack */
return (D3DPRIMITIVETYPE)0;
}
static void menu_display_d3d_blend_begin(void)
{
2016-04-15 04:17:31 +02:00
d3d_video_t *d3d = (d3d_video_t*)video_driver_get_ptr(false);
if (!d3d)
return;
d3d_enable_blend_func(d3d->dev);
}
static void menu_display_d3d_blend_end(void)
{
2016-04-15 04:17:31 +02:00
d3d_video_t *d3d = (d3d_video_t*)video_driver_get_ptr(false);
if (!d3d)
return;
d3d_disable_blend_func(d3d->dev);
}
static void menu_display_d3d_viewport(void *data)
{
#if 0
2016-02-09 16:11:37 +01:00
D3DVIEWPORT vp = {0};
menu_display_ctx_draw_t *draw = (menu_display_ctx_draw_t*)data;
d3d_video_t *d3d = (d3d_video_t*)video_driver_get_ptr(false);
if (!d3d || !draw)
return;
vp.X = draw->x;
vp.Y = d3d->video_info.height - (draw->y + draw->height);
vp.Width = draw->width;
vp.Height = draw->height;
vp.MinZ = 0.0f;
vp.MaxZ = 1.0f;
d3d_set_viewports(d3d->dev, &vp);
#endif
}
static void menu_display_d3d_bind_texture(void *data)
{
d3d_video_t *d3d = (d3d_video_t*)video_driver_get_ptr(false);
menu_display_ctx_draw_t *draw = (menu_display_ctx_draw_t*)data;
if (!d3d || !draw || !draw->texture)
return;
d3d_set_texture(d3d->dev, 0, (void*)draw->texture);
d3d_set_sampler_address_u(d3d->dev, 0, D3DTADDRESS_CLAMP);
d3d_set_sampler_address_v(d3d->dev, 0, D3DTADDRESS_CLAMP);
d3d_set_sampler_minfilter(d3d->dev, 0, D3DTEXF_LINEAR);
d3d_set_sampler_magfilter(d3d->dev, 0, D3DTEXF_LINEAR);
d3d_set_sampler_mipfilter(d3d->dev, 0, D3DTEXF_LINEAR);
}
static void menu_display_d3d_draw(void *data)
{
unsigned i;
video_shader_ctx_mvp_t mvp;
math_matrix_4x4 mop, m1, m2;
unsigned width, height;
d3d_video_t *d3d = (d3d_video_t*)video_driver_get_ptr(false);
2016-02-09 16:11:37 +01:00
menu_display_ctx_draw_t *draw = (menu_display_ctx_draw_t*)data;
float* pv = NULL;
const float *vertex = NULL;
const float *tex_coord = NULL;
const float *color = NULL;
if (!d3d || !draw)
return;
if(draw->pipeline.id)
return;
if(d3d->menu_display.offset + draw->coords->vertices > d3d->menu_display.size)
return;
pv = (float*)d3d_vertex_buffer_lock(d3d->menu_display.buffer);
pv += d3d->menu_display.offset * 8;
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 = menu_display_d3d_get_default_vertices();
if (!tex_coord)
tex_coord = menu_display_d3d_get_default_tex_coords();
for (i = 0; i < draw->coords->vertices; i++)
{
*pv++ = *vertex++;
*pv++ = *vertex++;
*pv++ = *tex_coord++;
*pv++ = *tex_coord++;
*pv++ = *color++;
*pv++ = *color++;
*pv++ = *color++;
*pv++ = *color++;
}
d3d_vertex_buffer_unlock(d3d->menu_display.buffer);
2017-11-15 16:51:29 +01:00
if(!draw->matrix_data)
draw->matrix_data = menu_display_d3d_get_default_mvp();
/* ugh */
video_driver_get_size(&width, &height);
matrix_4x4_scale(m1, 2.0, 2.0, 0);
matrix_4x4_translate(mop, -1.0, -1.0, 0);
matrix_4x4_multiply(m2, mop, m1);
matrix_4x4_multiply(m1, *((math_matrix_4x4*)draw->matrix_data), m2);
matrix_4x4_scale(mop, (draw->width / 2.0) / width, (draw->height / 2.0) / height, 0);
matrix_4x4_multiply(m2, mop, m1);
matrix_4x4_translate(mop, (draw->x + (draw->width / 2.0)) / width, (draw->y + (draw->height / 2.0)) / height,0);
matrix_4x4_multiply(m1, mop, m2);
matrix_4x4_multiply(m2, d3d->mvp_transposed, m1);
d3d_matrix_transpose(&m1, &m2);
mvp.data = d3d;
mvp.matrix = &m1;
video_driver_set_mvp(&mvp);
menu_display_d3d_bind_texture(draw);
d3d_draw_primitive(d3d->dev, menu_display_prim_to_d3d_enum(draw->prim_type), d3d->menu_display.offset,
draw->coords->vertices - (draw->prim_type == MENU_DISPLAY_PRIM_TRIANGLESTRIP? 2 : 0));
d3d->menu_display.offset += draw->coords->vertices;
}
static void menu_display_d3d_draw_pipeline(void *data)
{
#if defined(HAVE_HLSL) || defined(HAVE_CG)
menu_display_ctx_draw_t *draw = (menu_display_ctx_draw_t*)data;
struct uniform_info uniform_param = {0};
static float t = 0;
2017-09-11 01:21:57 +02:00
video_coord_array_t *ca = menu_display_get_coords_array();
2017-09-11 01:21:57 +02:00
(void)uniform_param;
draw->x = 0;
draw->y = 0;
2016-05-10 02:32:49 +02:00
draw->coords = (struct video_coords*)(&ca->coords);
draw->matrix_data = NULL;
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:
t += 0.01;
uniform_param.enabled = true;
uniform_param.lookup.enable = true;
uniform_param.lookup.add_prefix = true;
2016-04-26 12:47:00 +02:00
uniform_param.lookup.idx = draw->pipeline.id;
uniform_param.lookup.type = SHADER_PROGRAM_VERTEX;
uniform_param.type = UNIFORM_1F;
uniform_param.lookup.ident = "time";
uniform_param.result.f.v0 = t;
break;
}
#endif
}
static void menu_display_d3d_restore_clear_color(void)
{
/* not needed */
}
2016-04-15 04:20:57 +02:00
static void menu_display_d3d_clear_color(menu_display_ctx_clearcolor_t *clearcolor)
{
2016-02-09 16:11:37 +01:00
DWORD clear_color = 0;
2016-04-15 04:17:31 +02:00
d3d_video_t *d3d = (d3d_video_t*)video_driver_get_ptr(false);
2016-02-09 16:11:37 +01:00
if (!d3d || !clearcolor)
return;
2016-02-09 16:11:37 +01:00
clear_color = D3DCOLOR_ARGB(
BYTE_CLAMP(clearcolor->a * 255.0f), /* A */
BYTE_CLAMP(clearcolor->r * 255.0f), /* R */
BYTE_CLAMP(clearcolor->g * 255.0f), /* G */
BYTE_CLAMP(clearcolor->b * 255.0f) /* B */
);
d3d_clear(d3d->dev, 0, NULL, D3DCLEAR_TARGET, clear_color, 0, 0);
}
2015-12-05 11:29:06 +01:00
static bool menu_display_d3d_font_init_first(
2016-02-09 16:11:37 +01:00
void **font_handle, void *video_data,
const char *font_path, float 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, font_size, true,
is_threaded,
2017-10-03 00:56:18 +02:00
FONT_DRIVER_RENDER_DIRECT3D_API)))
return false;
return true;
}
menu_display_ctx_driver_t menu_display_ctx_d3d = {
menu_display_d3d_draw,
menu_display_d3d_draw_pipeline,
menu_display_d3d_viewport,
menu_display_d3d_blend_begin,
menu_display_d3d_blend_end,
menu_display_d3d_restore_clear_color,
menu_display_d3d_clear_color,
menu_display_d3d_get_default_mvp,
2016-04-15 03:31:59 +02:00
menu_display_d3d_get_default_vertices,
menu_display_d3d_get_default_tex_coords,
menu_display_d3d_font_init_first,
MENU_VIDEO_DRIVER_DIRECT3D,
"menu_display_d3d",
};