From 92afd7387d8137dd06487a59c2e71938ed3b336a Mon Sep 17 00:00:00 2001 From: aliaspider Date: Wed, 7 Feb 2018 21:59:18 +0100 Subject: [PATCH] (D3D12) add a font driver. --- Makefile.common | 3 +- gfx/common/d3d12_common.h | 14 +- gfx/drivers/d3d12.c | 54 ++++- gfx/drivers_font/d3d12_font.c | 374 ++++++++++++++++++++++++++++++++++ gfx/font_driver.c | 36 ++++ gfx/font_driver.h | 2 + griffin/griffin.c | 4 + 7 files changed, 477 insertions(+), 10 deletions(-) create mode 100644 gfx/drivers_font/d3d12_font.c diff --git a/Makefile.common b/Makefile.common index 498be0f888..d8f78db89b 100644 --- a/Makefile.common +++ b/Makefile.common @@ -1268,7 +1268,8 @@ ifeq ($(HAVE_D3D11), 1) endif ifeq ($(HAVE_D3D12), 1) - OBJ += gfx/drivers/d3d12.o gfx/common/d3d12_common.o + OBJ += gfx/drivers/d3d12.o gfx/common/d3d12_common.o \ + gfx/drivers_font/d3d12_font.o DEFINES += -DHAVE_D3D12 endif diff --git a/gfx/common/d3d12_common.h b/gfx/common/d3d12_common.h index 77a6a159d1..eb320e3ba3 100644 --- a/gfx/common/d3d12_common.h +++ b/gfx/common/d3d12_common.h @@ -1376,12 +1376,13 @@ typedef struct struct { - D3D12PipelineState pipe; - D3D12PipelineState pipe_font; - D3D12Resource vbo; - int offset; - int capacity; - bool enabled; + D3D12PipelineState pipe; + D3D12PipelineState pipe_font; + D3D12Resource vbo; + D3D12_VERTEX_BUFFER_VIEW vbo_view; + int offset; + int capacity; + bool enabled; } sprites; D3D12PipelineState pipes[GFX_MAX_SHADERS]; @@ -1432,6 +1433,7 @@ D3D12_GPU_VIRTUAL_ADDRESS d3d12_create_buffer(D3D12Device device, UINT size_in_bytes, D3D12Resource* buffer); void d3d12_init_texture(D3D12Device device, d3d12_texture_t* tex); +void d3d12_release_texture(d3d12_texture_t* texture); void d3d12_update_texture( int width, diff --git a/gfx/drivers/d3d12.c b/gfx/drivers/d3d12.c index b21cfa61ef..87f866747c 100644 --- a/gfx/drivers/d3d12.c +++ b/gfx/drivers/d3d12.c @@ -16,14 +16,17 @@ #define CINTERFACE #include +#include #include #include "../video_driver.h" +#include "../font_driver.h" #include "../common/win32_common.h" #include "../common/dxgi_common.h" #include "../common/d3d12_common.h" #include "../common/d3dcompiler_common.h" +//#include "../../menu/menu_driver.h" #include "../../driver.h" #include "../../verbosity.h" #include "../../configuration.h" @@ -327,6 +330,10 @@ static void d3d12_gfx_free(void* data) unsigned i; d3d12_video_t* d3d12 = (d3d12_video_t*)data; + font_driver_free_osd(); + + Release(d3d12->sprites.vbo); + Release(d3d12->frame.ubo); Release(d3d12->frame.vbo); Release(d3d12->frame.texture.handle); @@ -416,6 +423,12 @@ d3d12_gfx_init(const video_info_t* video, const input_driver_t** input, void** i d3d12_create_fullscreen_quad_vbo(d3d12->device, &d3d12->frame.vbo_view, &d3d12->frame.vbo); d3d12_create_fullscreen_quad_vbo(d3d12->device, &d3d12->menu.vbo_view, &d3d12->menu.vbo); + d3d12->sprites.capacity = 4096; + d3d12->sprites.vbo_view.SizeInBytes = sizeof(d3d12_sprite_t) * d3d12->sprites.capacity; + d3d12->sprites.vbo_view.StrideInBytes = sizeof(d3d12_sprite_t); + d3d12->sprites.vbo_view.BufferLocation = d3d12_create_buffer( + d3d12->device, d3d12->sprites.vbo_view.SizeInBytes, &d3d12->sprites.vbo); + d3d12->keep_aspect = video->force_aspect; d3d12->chain.vsync = video->vsync; d3d12->format = video->rgb32 ? DXGI_FORMAT_B8G8R8X8_UNORM : DXGI_FORMAT_B5G6R5_UNORM; @@ -444,6 +457,8 @@ d3d12_gfx_init(const video_info_t* video, const input_driver_t** input, void** i d3d12->vp.full_height = video->height; d3d12->resize_viewport = true; + font_driver_init_osd(d3d12, false, video->is_threaded, FONT_DRIVER_RENDER_D3D12_API); + return d3d12; error: @@ -471,7 +486,7 @@ static bool d3d12_gfx_frame( for (i = 0; i < countof(d3d12->chain.renderTargets); i++) Release(d3d12->chain.renderTargets[i]); - DXGIResizeBuffers(d3d12->chain.handle, 0, 0, 0, (DXGI_FORMAT)0, 0); + DXGIResizeBuffers(d3d12->chain.handle, 0, 0, 0, DXGI_FORMAT_UNKNOWN, 0); for (i = 0; i < countof(d3d12->chain.renderTargets); i++) { @@ -555,11 +570,30 @@ static bool d3d12_gfx_frame( D3D12DrawInstanced(d3d12->queue.cmd, 4, 1, 0, 0); } + D3D12RSSetViewports(d3d12->queue.cmd, 1, &d3d12->chain.viewport); + D3D12RSSetScissorRects(d3d12->queue.cmd, 1, &d3d12->chain.scissorRect); + + D3D12SetPipelineState(d3d12->queue.cmd, d3d12->sprites.pipe); + D3D12IASetPrimitiveTopology(d3d12->queue.cmd, D3D_PRIMITIVE_TOPOLOGY_POINTLIST); + D3D12IASetVertexBuffers(d3d12->queue.cmd, 0, 1, &d3d12->sprites.vbo_view); + + d3d12->sprites.enabled = true; +#if 0 + if (d3d12->menu.enabled) + menu_driver_frame(video_info); +#endif + if (msg && *msg) + { + font_driver_render_msg(video_info, NULL, msg, NULL); + dxgi_update_title(video_info); + } + d3d12->sprites.enabled = false; + + d3d12_resource_transition( d3d12->queue.cmd, d3d12->chain.renderTargets[d3d12->chain.frame_index], D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT); D3D12CloseGraphicsCommandList(d3d12->queue.cmd); - D3D12ExecuteGraphicsCommandLists(d3d12->queue.handle, 1, &d3d12->queue.cmd); #if 1 @@ -711,6 +745,20 @@ static void d3d12_gfx_apply_state_changes(void* data) d3d12->resize_viewport = true; } +static void d3d12_gfx_set_osd_msg( + void* data, video_frame_info_t* video_info, const char* msg, const void* params, void* font) +{ + d3d12_video_t* d3d12 = (d3d12_video_t*)data; + + if (d3d12) + { + if (d3d12->sprites.enabled) + font_driver_render_msg(video_info, font, msg, params); + else + printf("OSD msg: %s\n", msg); + } +} + static const video_poke_interface_t d3d12_poke_interface = { NULL, /* set_coords */ NULL, /* set_mvp */ @@ -727,7 +775,7 @@ static const video_poke_interface_t d3d12_poke_interface = { d3d12_gfx_apply_state_changes, d3d12_set_menu_texture_frame, d3d12_set_menu_texture_enable, - NULL, /* set_osd_msg */ + d3d12_gfx_set_osd_msg, NULL, /* show_mouse */ NULL, /* grab_mouse_toggle */ NULL, /* get_current_shader */ diff --git a/gfx/drivers_font/d3d12_font.c b/gfx/drivers_font/d3d12_font.c new file mode 100644 index 0000000000..ec884bf63c --- /dev/null +++ b/gfx/drivers_font/d3d12_font.c @@ -0,0 +1,374 @@ +/* RetroArch - A frontend for libretro. + * Copyright (C) 2014-2018 - Ali Bouhlel + * + * 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 . + */ + +#define CINTERFACE + +#include +#include +#include +#include +#include + +#include "../font_driver.h" +#include "../video_driver.h" +#include "../common/d3d12_common.h" + +#include "../../verbosity.h" + +typedef struct +{ + d3d12_texture_t texture; + const font_renderer_driver_t* font_driver; + void* font_data; + struct font_atlas* atlas; +} d3d12_font_t; + +static void* +d3d12_font_init_font(void* data, const char* font_path, float font_size, bool is_threaded) +{ + d3d12_video_t* d3d12 = (d3d12_video_t*)data; + d3d12_font_t* font = (d3d12_font_t*)calloc(1, sizeof(*font)); + + if (!font) + return NULL; + + if (!font_renderer_create_default( + (const void**)&font->font_driver, &font->font_data, font_path, font_size)) + { + RARCH_WARN("Couldn't initialize font renderer.\n"); + free(font); + return NULL; + } + + font->atlas = font->font_driver->get_atlas(font->font_data); + font->texture.sampler = d3d12->samplers[RARCH_FILTER_LINEAR][RARCH_WRAP_BORDER]; + font->texture.desc.Width = font->atlas->width; + font->texture.desc.Height = font->atlas->height; + font->texture.desc.Format = DXGI_FORMAT_A8_UNORM; + font->texture.srv_heap = &d3d12->desc.srv_heap; + d3d12_init_texture(d3d12->device, &font->texture); + d3d12_update_texture( + font->atlas->width, font->atlas->height, font->atlas->width, DXGI_FORMAT_A8_UNORM, + font->atlas->buffer, &font->texture); + font->atlas->dirty = false; + + return font; +} + +static void d3d12_font_free_font(void* data, bool is_threaded) +{ + d3d12_font_t* font = (d3d12_font_t*)data; + + if (!font) + return; + + if (font->font_driver && font->font_data && font->font_driver->free) + font->font_driver->free(font->font_data); + + d3d12_release_texture(&font->texture); + + free(font); +} + +static int d3d12_font_get_message_width(void* data, const char* msg, unsigned msg_len, float scale) +{ + d3d12_font_t* font = (d3d12_font_t*)data; + + unsigned i; + int delta_x = 0; + + if (!font) + return 0; + + for (i = 0; i < msg_len; i++) + { + const struct font_glyph* glyph; + const char* msg_tmp = &msg[i]; + unsigned code = utf8_walk(&msg_tmp); + unsigned skip = msg_tmp - &msg[i]; + + if (skip > 1) + i += skip - 1; + + glyph = font->font_driver->get_glyph(font->font_data, code); + + if (!glyph) /* Do something smarter here ... */ + glyph = font->font_driver->get_glyph(font->font_data, '?'); + + if (!glyph) + continue; + + delta_x += glyph->advance_x; + } + + return delta_x * scale; +} + +static void d3d12_font_render_line( + video_frame_info_t* video_info, + d3d12_font_t* font, + const char* msg, + unsigned msg_len, + float scale, + const unsigned int color, + float pos_x, + float pos_y, + unsigned text_align) +{ + unsigned i, count; + void* mapped_vbo; + d3d12_sprite_t* v; + d3d12_sprite_t* vbo_start; + d3d12_video_t* d3d12 = (d3d12_video_t*)video_driver_get_ptr(false); + unsigned width = video_info->width; + unsigned height = video_info->height; + int x = roundf(pos_x * width); + int y = roundf((1.0 - pos_y) * height); + D3D12_RANGE range = { 0, 0 }; + + if (!d3d12->sprites.enabled || msg_len > (unsigned)d3d12->sprites.capacity) + return; + + if (d3d12->sprites.offset + msg_len > (unsigned)d3d12->sprites.capacity) + d3d12->sprites.offset = 0; + + switch (text_align) + { + case TEXT_ALIGN_RIGHT: + x -= d3d12_font_get_message_width(font, msg, msg_len, scale); + break; + + case TEXT_ALIGN_CENTER: + x -= d3d12_font_get_message_width(font, msg, msg_len, scale) / 2; + break; + } + + D3D12Map(d3d12->sprites.vbo, 0, &range, (void**)&vbo_start); + + v = vbo_start + d3d12->sprites.offset; + range.Begin = (uintptr_t)v - (uintptr_t)vbo_start; + + for (i = 0; i < msg_len; i++) + { + const struct font_glyph* glyph; + const char* msg_tmp = &msg[i]; + unsigned code = utf8_walk(&msg_tmp); + unsigned skip = msg_tmp - &msg[i]; + + if (skip > 1) + i += skip - 1; + + glyph = font->font_driver->get_glyph(font->font_data, code); + + if (!glyph) /* Do something smarter here ... */ + glyph = font->font_driver->get_glyph(font->font_data, '?'); + + if (!glyph) + continue; + + v->pos.x = (x + glyph->draw_offset_x) * scale / (float)d3d12->chain.viewport.Width; + v->pos.y = (y + glyph->draw_offset_y) * scale / (float)d3d12->chain.viewport.Height; + v->pos.w = glyph->width * scale / (float)d3d12->chain.viewport.Width; + v->pos.h = glyph->height * scale / (float)d3d12->chain.viewport.Height; + + v->coords.u = glyph->atlas_offset_x / (float)font->texture.desc.Width; + v->coords.v = glyph->atlas_offset_y / (float)font->texture.desc.Height; + v->coords.w = glyph->width / (float)font->texture.desc.Width; + v->coords.h = glyph->height / (float)font->texture.desc.Height; + + v->params.scaling = 1; + v->params.rotation = 0; + + v->colors[0] = color; + v->colors[1] = color; + v->colors[2] = color; + v->colors[3] = color; + + v++; + + x += glyph->advance_x * scale; + y += glyph->advance_y * scale; + } + + range.End = (uintptr_t)v - (uintptr_t)vbo_start; + D3D12Unmap(d3d12->sprites.vbo, 0, &range); + + count = v - vbo_start - d3d12->sprites.offset; + + if (!count) + return; + + if (font->atlas->dirty) + { + d3d12_update_texture( + font->atlas->width, font->atlas->height, font->atlas->width, DXGI_FORMAT_A8_UNORM, + font->atlas->buffer, &font->texture); + font->atlas->dirty = false; + } + + if(font->texture.dirty) + d3d12_upload_texture(d3d12->queue.cmd, &font->texture); + + D3D12SetPipelineState(d3d12->queue.cmd, d3d12->sprites.pipe_font); + d3d12_set_texture_and_sampler(d3d12->queue.cmd, &font->texture); + D3D12DrawInstanced(d3d12->queue.cmd, count, 1, d3d12->sprites.offset, 0); + + D3D12SetPipelineState(d3d12->queue.cmd, d3d12->sprites.pipe); + + d3d12->sprites.offset += count; +} + +static void d3d12_font_render_message( + video_frame_info_t* video_info, + d3d12_font_t* font, + const char* msg, + float scale, + const unsigned int color, + float pos_x, + float pos_y, + unsigned text_align) +{ + int lines = 0; + float line_height; + + if (!msg || !*msg) + return; + + /* If the font height is not supported just draw as usual */ + if (!font->font_driver->get_line_height) + { + d3d12_font_render_line( + video_info, font, msg, strlen(msg), scale, color, pos_x, pos_y, text_align); + return; + } + + line_height = font->font_driver->get_line_height(font->font_data) * scale / video_info->height; + + for (;;) + { + const char* delim = strchr(msg, '\n'); + + /* Draw the line */ + if (delim) + { + unsigned msg_len = delim - msg; + d3d12_font_render_line( + video_info, font, msg, msg_len, scale, color, pos_x, + pos_y - (float)lines * line_height, text_align); + msg += msg_len + 1; + lines++; + } + else + { + unsigned msg_len = strlen(msg); + d3d12_font_render_line( + video_info, font, msg, msg_len, scale, color, pos_x, + pos_y - (float)lines * line_height, text_align); + break; + } + } +} + +static void d3d12_font_render_msg( + video_frame_info_t* video_info, void* data, const char* msg, const void* userdata) +{ + float x, y, scale, drop_mod, drop_alpha; + int drop_x, drop_y; + enum text_alignment text_align; + unsigned color, color_dark, r, g, b, alpha, r_dark, g_dark, b_dark, alpha_dark; + d3d12_font_t* font = (d3d12_font_t*)data; + const struct font_params* params = (const struct font_params*)userdata; + unsigned width = video_info->width; + unsigned height = video_info->height; + + if (!font || !msg || !*msg) + return; + + if (params) + { + x = params->x; + y = params->y; + scale = params->scale; + text_align = params->text_align; + drop_x = params->drop_x; + drop_y = params->drop_y; + drop_mod = params->drop_mod; + drop_alpha = params->drop_alpha; + r = FONT_COLOR_GET_RED(params->color); + g = FONT_COLOR_GET_GREEN(params->color); + b = FONT_COLOR_GET_BLUE(params->color); + alpha = FONT_COLOR_GET_ALPHA(params->color); + color = DXGI_COLOR_RGBA(r, g, b, alpha); + } + else + { + x = video_info->font_msg_pos_x; + y = video_info->font_msg_pos_y; + scale = 1.0f; + text_align = TEXT_ALIGN_LEFT; + + r = (video_info->font_msg_color_r * 255); + g = (video_info->font_msg_color_g * 255); + b = (video_info->font_msg_color_b * 255); + alpha = 255; + color = DXGI_COLOR_RGBA(r, g, b, alpha); + + drop_x = -2; + drop_y = -2; + drop_mod = 0.3f; + drop_alpha = 1.0f; + } + + if (drop_x || drop_y) + { + r_dark = r * drop_mod; + g_dark = g * drop_mod; + b_dark = b * drop_mod; + alpha_dark = alpha * drop_alpha; + color_dark = DXGI_COLOR_RGBA(r_dark, g_dark, b_dark, alpha_dark); + + d3d12_font_render_message( + video_info, font, msg, scale, color_dark, x + scale * drop_x / width, + y + scale * drop_y / height, text_align); + } + + d3d12_font_render_message(video_info, font, msg, scale, color, x, y, text_align); +} + +static const struct font_glyph* d3d12_font_get_glyph(void* data, uint32_t code) +{ + d3d12_font_t* font = (d3d12_font_t*)data; + + if (!font || !font->font_driver) + return NULL; + + if (!font->font_driver->ident) + return NULL; + + return font->font_driver->get_glyph((void*)font->font_driver, code); +} + +static void d3d12_font_bind_block(void* data, void* userdata) { (void)data; } + +font_renderer_t d3d12_font = { + d3d12_font_init_font, + d3d12_font_free_font, + d3d12_font_render_msg, + "d3d12font", + d3d12_font_get_glyph, + d3d12_font_bind_block, + NULL, /* flush */ + d3d12_font_get_message_width, +}; diff --git a/gfx/font_driver.c b/gfx/font_driver.c index afb343abb9..9133ed2844 100644 --- a/gfx/font_driver.c +++ b/gfx/font_driver.c @@ -309,6 +309,37 @@ static bool d3d11_font_init_first( } #endif +#ifdef HAVE_D3D12 +static const font_renderer_t *d3d12_font_backends[] = { + &d3d12_font, + NULL, +}; + +static bool d3d12_font_init_first( + const void **font_driver, void **font_handle, + void *video_data, const char *font_path, + float font_size, bool is_threaded) +{ + unsigned i; + + for (i = 0; d3d12_font_backends[i]; i++) + { + void *data = d3d12_font_backends[i]->init(video_data, + font_path, font_size, + is_threaded); + + if (!data) + continue; + + *font_driver = d3d12_font_backends[i]; + *font_handle = data; + return true; + } + + return false; +} +#endif + #ifdef HAVE_VITA2D static const font_renderer_t *vita2d_font_backends[] = { &vita2d_vita_font @@ -430,6 +461,11 @@ static bool font_init_first( return d3d11_font_init_first(font_driver, font_handle, video_data, font_path, font_size, is_threaded); #endif +#ifdef HAVE_D3D12 + case FONT_DRIVER_RENDER_D3D12_API: + return d3d12_font_init_first(font_driver, font_handle, + video_data, font_path, font_size, is_threaded); +#endif #ifdef HAVE_VITA2D case FONT_DRIVER_RENDER_VITA2D: return vita2d_font_init_first(font_driver, font_handle, diff --git a/gfx/font_driver.h b/gfx/font_driver.h index 2cfa3fe7ae..2ae4030710 100644 --- a/gfx/font_driver.h +++ b/gfx/font_driver.h @@ -32,6 +32,7 @@ enum font_driver_render_api FONT_DRIVER_RENDER_OPENGL_API, FONT_DRIVER_RENDER_DIRECT3D_API, FONT_DRIVER_RENDER_D3D11_API, + FONT_DRIVER_RENDER_D3D12_API, FONT_DRIVER_RENDER_VITA2D, FONT_DRIVER_RENDER_CTR, FONT_DRIVER_RENDER_WIIU, @@ -186,6 +187,7 @@ extern font_renderer_t ctr_font; extern font_renderer_t wiiu_font; extern font_renderer_t vulkan_raster_font; extern font_renderer_t d3d11_font; +extern font_renderer_t d3d12_font; extern font_renderer_t caca_font; extern font_renderer_t gdi_font; extern font_renderer_t vga_font; diff --git a/griffin/griffin.c b/griffin/griffin.c index dad50dc0a9..30d21c4f35 100644 --- a/griffin/griffin.c +++ b/griffin/griffin.c @@ -517,6 +517,10 @@ FONTS #include "../gfx/drivers_font/d3d11_font.c" #endif +#if defined(HAVE_D3D12) +#include "../gfx/drivers_font/d3d12_font.c" +#endif + /*============================================================ INPUT ============================================================ */