RetroArch/menu/drivers_display/menu_display_vulkan.c

344 lines
10 KiB
C
Raw Normal View History

2016-02-16 20:24:00 +01:00
/* RetroArch - A frontend for libretro.
2017-01-22 13:40:32 +01:00
* Copyright (C) 2016-2017 - Hans-Kristian Arntzen
* Copyright (C) 2011-2017 - Daniel De Matteis
2016-02-16 20:24:00 +01: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/>.
*/
#include <retro_miscellaneous.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-02-16 20:24:00 +01:00
#include "../../gfx/font_driver.h"
#include "../../gfx/video_driver.h"
2016-02-16 20:24:00 +01:00
#include "../../gfx/common/vulkan_common.h"
/* Will do Y-flip later, but try to make it similar to GL. */
2016-02-16 20:24:00 +01:00
static const float vk_vertexes[] = {
0, 0,
1, 0,
0, 1,
2016-02-16 20:24:00 +01:00
1, 1
};
static const float vk_tex_coords[] = {
0, 1,
1, 1,
0, 0,
1, 0
2016-02-16 20:24:00 +01:00
};
static const float vk_colors[] = {
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f,
};
2016-02-16 20:24:00 +01:00
static void *menu_display_vk_get_default_mvp(void)
{
2016-04-15 04:17:31 +02:00
vk_t *vk = (vk_t*)video_driver_get_ptr(false);
2016-02-16 20:24:00 +01:00
if (!vk)
return NULL;
return &vk->mvp_no_rot;
}
2016-04-15 04:43:48 +02:00
static const float *menu_display_vk_get_default_vertices(void)
{
return &vk_vertexes[0];
}
static const float *menu_display_vk_get_default_color(void)
{
return &vk_colors[0];
}
2016-04-15 04:43:48 +02:00
static const float *menu_display_vk_get_default_tex_coords(void)
{
return &vk_tex_coords[0];
}
2016-02-21 09:57:24 +01:00
static unsigned to_display_pipeline(
2016-02-25 19:52:40 +01:00
enum menu_display_prim_type type, bool blend)
2016-02-16 20:24:00 +01:00
{
2016-02-25 19:52:40 +01:00
return ((type == MENU_DISPLAY_PRIM_TRIANGLESTRIP) << 1) | (blend << 0);
2016-02-16 20:24:00 +01:00
}
#ifdef HAVE_SHADERPIPELINE
2016-05-08 11:11:28 +02:00
static unsigned to_menu_pipeline(
enum menu_display_prim_type type, unsigned pipeline)
{
switch (pipeline)
{
case VIDEO_SHADER_MENU:
return 4 + (type == MENU_DISPLAY_PRIM_TRIANGLESTRIP);
case VIDEO_SHADER_MENU_2:
2016-05-08 11:11:28 +02:00
return 6 + (type == MENU_DISPLAY_PRIM_TRIANGLESTRIP);
case VIDEO_SHADER_MENU_3:
return 8 + (type == MENU_DISPLAY_PRIM_TRIANGLESTRIP);
2017-02-20 21:51:58 +01:00
case VIDEO_SHADER_MENU_4:
return 10 + (type == MENU_DISPLAY_PRIM_TRIANGLESTRIP);
case VIDEO_SHADER_MENU_5:
return 12 + (type == MENU_DISPLAY_PRIM_TRIANGLESTRIP);
2016-05-08 11:11:28 +02:00
default:
return 0;
}
}
#endif
2016-05-08 11:11:28 +02:00
static void menu_display_vk_viewport(void *data)
{
menu_display_ctx_draw_t *draw = (menu_display_ctx_draw_t*)data;
vk_t *vk = (vk_t*)video_driver_get_ptr(false);
if (!vk || !draw)
return;
vk->vk_vp.x = draw->x;
vk->vk_vp.y = vk->context->swapchain_height - draw->y - draw->height;
vk->vk_vp.width = draw->width;
vk->vk_vp.height = draw->height;
vk->vk_vp.minDepth = 0.0f;
vk->vk_vp.maxDepth = 1.0f;
}
static void menu_display_vk_draw_pipeline(void *data)
{
2016-06-07 00:00:28 +02:00
#ifdef HAVE_SHADERPIPELINE
2017-03-05 19:28:34 +01:00
float output_size[2];
menu_display_ctx_draw_t *draw = (menu_display_ctx_draw_t*)data;
vk_t *vk = (vk_t*)video_driver_get_ptr(false);
video_coord_array_t *ca = NULL;
static uint8_t ubo_scratch_data[768];
2016-05-08 11:11:28 +02:00
static float t = 0.0f;
static struct video_coords blank_coords;
if (!vk || !draw)
return;
draw->x = 0;
draw->y = 0;
draw->matrix_data = NULL;
2017-03-05 19:28:34 +01:00
output_size[0] = (float)vk->context->swapchain_width;
output_size[1] = (float)vk->context->swapchain_height;
switch (draw->pipeline.id)
{
/* Ribbon */
default:
case VIDEO_SHADER_MENU:
case VIDEO_SHADER_MENU_2:
ca = menu_display_get_coords_array();
draw->coords = (struct video_coords*)&ca->coords;
draw->pipeline.backend_data = ubo_scratch_data;
draw->pipeline.backend_data_size = sizeof(float);
/* Match UBO layout in shader. */
memcpy(ubo_scratch_data, &t, sizeof(t));
break;
/* Snow simple */
case VIDEO_SHADER_MENU_3:
2017-02-20 21:51:58 +01:00
case VIDEO_SHADER_MENU_4:
case VIDEO_SHADER_MENU_5:
draw->pipeline.backend_data = ubo_scratch_data;
draw->pipeline.backend_data_size = sizeof(math_matrix_4x4) + 3 * sizeof(float);
/* Match UBO layout in shader. */
memcpy(ubo_scratch_data, menu_display_vk_get_default_mvp(), sizeof(math_matrix_4x4));
memcpy(ubo_scratch_data + sizeof(math_matrix_4x4), output_size, sizeof(output_size));
memcpy(ubo_scratch_data + sizeof(math_matrix_4x4) + 2 * sizeof(float), &t, sizeof(t));
draw->coords = &blank_coords;
blank_coords.vertices = 4;
draw->prim_type = MENU_DISPLAY_PRIM_TRIANGLESTRIP;
break;
}
2016-05-08 11:11:28 +02:00
t += 0.01;
2016-06-07 00:00:28 +02:00
#endif
}
2016-02-16 20:24:00 +01:00
static void menu_display_vk_draw(void *data)
{
unsigned i;
2016-02-21 09:57:24 +01:00
struct vk_buffer_range range;
struct vk_texture *texture = NULL;
const float *vertex = NULL;
const float *tex_coord = NULL;
const float *color = NULL;
struct vk_vertex *pv = NULL;
menu_display_ctx_draw_t *draw = (menu_display_ctx_draw_t*)data;
2016-04-15 04:17:31 +02:00
vk_t *vk = (vk_t*)video_driver_get_ptr(false);
2016-04-15 04:48:07 +02:00
if (!vk || !draw)
2016-02-16 20:24:00 +01:00
return;
2016-02-29 19:26:57 +01:00
texture = (struct vk_texture*)draw->texture;
vertex = draw->coords->vertex;
tex_coord = draw->coords->tex_coord;
color = draw->coords->color;
2016-02-16 20:24:00 +01:00
if (!vertex)
2016-04-15 04:43:48 +02:00
vertex = menu_display_vk_get_default_vertices();
2016-02-16 20:24:00 +01:00
if (!tex_coord)
2016-04-15 04:43:48 +02:00
tex_coord = menu_display_vk_get_default_tex_coords();
if (!draw->coords->lut_tex_coord)
draw->coords->lut_tex_coord = menu_display_vk_get_default_tex_coords();
2016-02-16 20:24:00 +01:00
if (!texture)
2016-02-29 19:26:57 +01:00
texture = &vk->display.blank_texture;
if (!color)
color = menu_display_vk_get_default_color();
2016-02-16 20:24:00 +01:00
menu_display_vk_viewport(draw);
2016-02-16 20:24:00 +01:00
vk->tracker.dirty |= VULKAN_DIRTY_DYNAMIC_BIT;
/* Bake interleaved VBO. Kinda ugly, we should probably try to move to
* an interleaved model to begin with ... */
if (!vulkan_buffer_chain_alloc(vk->context, &vk->chain->vbo,
draw->coords->vertices * sizeof(struct vk_vertex), &range))
return;
pv = (struct vk_vertex*)range.data;
for (i = 0; i < draw->coords->vertices; i++, pv++)
{
2016-02-29 19:26:57 +01:00
pv->x = *vertex++;
pv->y = 1.0f - (*vertex++); /* Y-flip. Vulkan is top-left clip space */
2016-02-29 19:26:57 +01:00
pv->tex_x = *tex_coord++;
pv->tex_y = *tex_coord++;
2016-02-16 20:24:00 +01:00
pv->color.r = *color++;
pv->color.g = *color++;
pv->color.b = *color++;
pv->color.a = *color++;
}
2016-05-08 11:11:28 +02:00
switch (draw->pipeline.id)
2016-02-16 20:24:00 +01:00
{
2016-06-07 00:00:28 +02:00
#ifdef HAVE_SHADERPIPELINE
2016-05-08 11:11:28 +02:00
case VIDEO_SHADER_MENU:
case VIDEO_SHADER_MENU_2:
2016-12-12 15:02:43 +01:00
case VIDEO_SHADER_MENU_3:
2017-02-20 21:51:58 +01:00
case VIDEO_SHADER_MENU_4:
case VIDEO_SHADER_MENU_5:
2016-05-08 11:11:28 +02:00
{
const struct vk_draw_triangles call = {
vk->display.pipelines[
to_menu_pipeline(draw->prim_type, draw->pipeline.id)],
NULL,
VK_NULL_HANDLE,
draw->pipeline.backend_data,
draw->pipeline.backend_data_size,
2016-05-08 11:11:28 +02:00
&range,
draw->coords->vertices,
};
vulkan_draw_triangles(vk, &call);
break;
}
break;
2016-06-07 00:00:28 +02:00
#endif
2016-05-08 11:11:28 +02:00
default:
{
2017-08-12 18:06:48 +02:00
struct vk_draw_triangles call;
call.pipeline = vk->display.pipelines[
to_display_pipeline(draw->prim_type, vk->display.blend)];
call.texture = texture;
call.sampler = texture->mipmap ?
vk->samplers.mipmap_linear :
(texture->default_smooth ? vk->samplers.linear : vk->samplers.nearest);
call.uniform = draw->matrix_data
? draw->matrix_data : menu_display_vk_get_default_mvp();
call.uniform_size = sizeof(math_matrix_4x4);
call.vbo = &range;
call.vertices = draw->coords->vertices;
2016-05-08 11:11:28 +02:00
vulkan_draw_triangles(vk, &call);
break;
}
2016-02-16 20:24:00 +01:00
}
}
static void menu_display_vk_restore_clear_color(void)
{
}
2016-04-15 04:20:57 +02:00
static void menu_display_vk_clear_color(menu_display_ctx_clearcolor_t *clearcolor)
2016-02-16 20:24:00 +01:00
{
VkClearRect rect;
2016-02-29 20:07:11 +01:00
VkClearAttachment attachment;
2016-04-15 04:17:31 +02:00
vk_t *vk = (vk_t*)video_driver_get_ptr(false);
if (!vk || !clearcolor)
return;
memset(&attachment, 0, sizeof(attachment));
2016-02-29 20:07:11 +01:00
attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
attachment.clearValue.color.float32[0] = clearcolor->r;
attachment.clearValue.color.float32[1] = clearcolor->g;
attachment.clearValue.color.float32[2] = clearcolor->b;
attachment.clearValue.color.float32[3] = clearcolor->a;
memset(&rect, 0, sizeof(rect));
2016-02-29 19:26:57 +01:00
rect.rect.extent.width = vk->context->swapchain_width;
rect.rect.extent.height = vk->context->swapchain_height;
2016-02-29 19:26:57 +01:00
rect.layerCount = 1;
vkCmdClearAttachments(vk->cmd, 1, &attachment, 1, &rect);
2016-02-16 20:24:00 +01:00
}
static void menu_display_vk_blend_begin(void)
{
2016-04-15 04:17:31 +02:00
vk_t *vk = (vk_t*)video_driver_get_ptr(false);
2016-02-16 20:24:00 +01:00
vk->display.blend = true;
}
static void menu_display_vk_blend_end(void)
{
2016-04-15 04:17:31 +02:00
vk_t *vk = (vk_t*)video_driver_get_ptr(false);
2016-02-16 20:24:00 +01:00
vk->display.blend = false;
}
static bool menu_display_vk_font_init_first(
void **font_handle, void *video_data, const char *font_path,
float font_size, bool is_threaded)
2016-02-16 20:24:00 +01:00
{
font_data_t **handle = (font_data_t**)font_handle;
*handle = font_driver_init_first(video_data,
font_path, font_size, true,
is_threaded,
FONT_DRIVER_RENDER_VULKAN_API);
return *handle;
2016-02-16 20:24:00 +01:00
}
menu_display_ctx_driver_t menu_display_ctx_vulkan = {
menu_display_vk_draw,
menu_display_vk_draw_pipeline,
menu_display_vk_viewport,
2016-02-16 20:24:00 +01:00
menu_display_vk_blend_begin,
menu_display_vk_blend_end,
menu_display_vk_restore_clear_color,
menu_display_vk_clear_color,
menu_display_vk_get_default_mvp,
2016-04-15 03:31:59 +02:00
menu_display_vk_get_default_vertices,
menu_display_vk_get_default_tex_coords,
2016-02-16 20:24:00 +01:00
menu_display_vk_font_init_first,
MENU_VIDEO_DRIVER_VULKAN,
"menu_display_vulkan",
};