1
Fork 0
sm64/src/pc/gfx/gfx_vulkan.cpp

467 lines
18 KiB
C++
Raw Normal View History

2022-10-03 22:49:19 -04:00
#include <time.h>
#include <errno.h>
2022-10-04 09:34:57 -04:00
#include <vulkan/vulkan.h>
#include <vector>
#include <cstring>
#include <cstdio>
#include <array>
2022-10-03 22:49:19 -04:00
#include "gfx_rendering_api.h"
#include "gfx_pc.h"
#include "gfx_window_manager_api.h"
static VkInstance instance = VK_NULL_HANDLE;
static VkPhysicalDevice physical_device = VK_NULL_HANDLE;
static VkDevice device = VK_NULL_HANDLE;
static VkQueue graphics_queue = VK_NULL_HANDLE;
static VkQueue present_queue = VK_NULL_HANDLE;
static VkCommandPool command_pool = VK_NULL_HANDLE;
static VkSwapchainKHR swapchain = VK_NULL_HANDLE;
static VkFormat surface_format;
static std::vector<VkImage> swapchain_images;
static std::vector<VkImageView> swapchain_views;
static VkRenderPass render_pass = VK_NULL_HANDLE;
2022-10-04 09:34:57 -04:00
VKAPI_ATTR VkBool32 VKAPI_CALL
gfx_vulkan_debug_callback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
VkDebugUtilsMessageTypeFlagsEXT messageType,
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
void* pUserData) {
printf("%s\n", pCallbackData->pMessage);
return VK_FALSE;
}
2022-10-03 22:49:19 -04:00
static bool gfx_vulkan_renderer_z_is_from_0_to_1(void) {
return false;
}
static void gfx_vulkan_renderer_unload_shader(struct ShaderProgram *old_prg) {
}
static void gfx_vulkan_renderer_load_shader(struct ShaderProgram *new_prg) {
}
static struct ShaderProgram *gfx_vulkan_renderer_create_and_load_new_shader(uint32_t shader_id) {
return NULL;
}
static struct ShaderProgram *gfx_vulkan_renderer_lookup_shader(uint32_t shader_id) {
return NULL;
}
static void gfx_vulkan_renderer_shader_get_info(struct ShaderProgram *prg, uint8_t *num_inputs, bool used_textures[2]) {
*num_inputs = 0;
used_textures[0] = false;
used_textures[1] = false;
}
static uint32_t gfx_vulkan_renderer_new_texture(void) {
return 0;
}
static void gfx_vulkan_renderer_select_texture(int tile, uint32_t texture_id) {
}
static void gfx_vulkan_renderer_upload_texture(const uint8_t *rgba32_buf, int width, int height) {
}
static void gfx_vulkan_renderer_set_sampler_parameters(int tile, bool linear_filter, uint32_t cms, uint32_t cmt) {
}
static void gfx_vulkan_renderer_set_depth_test(bool depth_test) {
}
static void gfx_vulkan_renderer_set_depth_mask(bool z_upd) {
}
static void gfx_vulkan_renderer_set_zmode_decal(bool zmode_decal) {
}
static void gfx_vulkan_renderer_set_viewport(int x, int y, int width, int height) {
}
static void gfx_vulkan_renderer_set_scissor(int x, int y, int width, int height) {
}
static void gfx_vulkan_renderer_set_use_alpha(bool use_alpha) {
}
static void gfx_vulkan_renderer_draw_triangles(float buf_vbo[], size_t buf_vbo_len, size_t buf_vbo_num_tris) {
}
2022-10-04 09:34:57 -04:00
static void gfx_vulkan_create_instance() {
std::vector<const char*> instance_extensions = {"VK_EXT_debug_utils"};
uint32_t extension_count = 0;
vkEnumerateInstanceExtensionProperties(nullptr, &extension_count, nullptr);
std::vector<VkExtensionProperties> extensions(extension_count);
vkEnumerateInstanceExtensionProperties(nullptr, &extension_count, extensions.data());
for(auto& extension : extensions) {
if (strstr(extension.extensionName, "surface") != nullptr) {
instance_extensions.push_back(extension.extensionName);
}
if (strstr(extension.extensionName, "VK_KHR_get_physical_device_properties2") != nullptr) {
instance_extensions.push_back(extension.extensionName);
}
}
VkDebugUtilsMessengerCreateInfoEXT debug_create_info = {};
debug_create_info.sType =
VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
debug_create_info.messageSeverity =
VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
debug_create_info.messageType =
VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT;
debug_create_info.pfnUserCallback = gfx_vulkan_debug_callback;
VkInstanceCreateInfo create_info = {};
create_info.pNext = &debug_create_info;
create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
create_info.ppEnabledExtensionNames = instance_extensions.data();
create_info.enabledExtensionCount = instance_extensions.size();
vkCreateInstance(&create_info, nullptr, &instance);
printf("Created vulkan instance!\n");
}
static void gfx_vulkan_create_command_pool(const int graphics_family_index) {
VkCommandPoolCreateInfo pool_create_info = {};
pool_create_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
pool_create_info.queueFamilyIndex = graphics_family_index;
pool_create_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
vkCreateCommandPool(device, &pool_create_info, nullptr, &command_pool);
}
static void gfx_vulkan_create_device() {
// pick physical device
uint32_t device_count = 0;
vkEnumeratePhysicalDevices(instance, &device_count, nullptr);
std::vector<VkPhysicalDevice> devices(device_count);
vkEnumeratePhysicalDevices(instance, &device_count, devices.data());
for (auto device : devices) {
VkPhysicalDeviceProperties device_properties;
vkGetPhysicalDeviceProperties(device, &device_properties);
printf("GPU Found: %s\n", device_properties.deviceName);
}
physical_device = devices[0];
uint32_t extension_count = 0;
vkEnumerateDeviceExtensionProperties(physical_device, nullptr,
&extension_count, nullptr);
std::vector<VkExtensionProperties> extension_properties(extension_count);
vkEnumerateDeviceExtensionProperties(
physical_device, nullptr, &extension_count,
extension_properties.data());
uint32_t graphics_family_index = 0, present_family_index = 0;
// create logical device
uint32_t queue_family_count = 0;
vkGetPhysicalDeviceQueueFamilyProperties(physical_device, &queue_family_count,
nullptr);
std::vector<VkQueueFamilyProperties> queueFamilies(queue_family_count);
vkGetPhysicalDeviceQueueFamilyProperties(physical_device, &queue_family_count,
queueFamilies.data());
int i = 0;
for (const auto&queue_family : queueFamilies) {
if (queue_family.queueCount > 0 && queue_family.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
graphics_family_index = i;
}
i++;
}
std::vector<VkDeviceQueueCreateInfo> queue_create_infos;
if (graphics_family_index == present_family_index) {
VkDeviceQueueCreateInfo queue_create_info = {};
queue_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queue_create_info.queueFamilyIndex = graphics_family_index;
queue_create_info.queueCount = 1;
float queuePriority = 1.0f;
queue_create_info.pQueuePriorities = &queuePriority;
queue_create_infos.push_back(queue_create_info);
} else {
// graphics
{
VkDeviceQueueCreateInfo queue_create_info = {};
queue_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queue_create_info.queueFamilyIndex = graphics_family_index;
queue_create_info.queueCount = 1;
float queuePriority = 1.0f;
queue_create_info.pQueuePriorities = &queuePriority;
queue_create_infos.push_back(queue_create_info);
}
// present
{
VkDeviceQueueCreateInfo queue_create_info = {};
queue_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queue_create_info.queueFamilyIndex = present_family_index;
queue_create_info.queueCount = 1;
float queuePriority = 1.0f;
queue_create_info.pQueuePriorities = &queuePriority;
queue_create_infos.push_back(queue_create_info);
}
}
std::array<const char*, 1> device_extensions = {"VK_KHR_swapchain"};
2022-10-04 09:34:57 -04:00
VkDeviceCreateInfo device_create_info = {};
device_create_info.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
device_create_info.pQueueCreateInfos = queue_create_infos.data();
device_create_info.queueCreateInfoCount =
static_cast<uint32_t>(queue_create_infos.size());
device_create_info.ppEnabledExtensionNames = device_extensions.data();
device_create_info.enabledExtensionCount = device_extensions.size();
2022-10-04 09:34:57 -04:00
vkCreateDevice(physical_device, &device_create_info, nullptr, &device);
printf("Created vulkan device!\n");
vkGetDeviceQueue(device, graphics_family_index, 0, &graphics_queue);
vkGetDeviceQueue(device, present_family_index, 0, &present_queue);
gfx_vulkan_create_command_pool(graphics_family_index);
}
static void gfx_vulkan_create_swapchain() {
vkQueueWaitIdle(present_queue);
// create surface from wapi
VkSurfaceKHR surface =
static_cast<VkSurfaceKHR>(gfx_get_current_windowing_api()->create_surface(instance));
VkBool32 supported;
vkGetPhysicalDeviceSurfaceSupportKHR(physical_device, 0,
surface, &supported);
// query swapchain support
VkSurfaceCapabilitiesKHR capabilities;
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(
physical_device, surface, &capabilities);
std::vector<VkSurfaceFormatKHR> formats;
uint32_t format_count;
vkGetPhysicalDeviceSurfaceFormatsKHR(
physical_device, surface, &format_count, nullptr);
formats.resize(format_count);
vkGetPhysicalDeviceSurfaceFormatsKHR(
physical_device, surface, &format_count, formats.data());
std::vector<VkPresentModeKHR> present_modes;
uint32_t present_mode_count;
vkGetPhysicalDeviceSurfacePresentModesKHR(
physical_device, surface, &present_mode_count, nullptr);
present_modes.resize(present_mode_count);
vkGetPhysicalDeviceSurfacePresentModesKHR(
physical_device, surface, &present_mode_count,
present_modes.data());
// choosing swapchain features
surface_format = formats[0].format;
for (const auto& available_format : formats) {
if (available_format.format == VK_FORMAT_B8G8R8A8_UNORM && available_format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
surface_format = available_format.format;
}
}
VkPresentModeKHR present_mode = VK_PRESENT_MODE_FIFO_KHR;
for (const auto& available_mode : present_modes) {
if (available_mode == VK_PRESENT_MODE_MAILBOX_KHR) {
present_mode = available_mode;
}
}
uint32_t image_count = capabilities.minImageCount + 1;
if (capabilities.maxImageCount > 0 && image_count > capabilities.maxImageCount) {
image_count = capabilities.maxImageCount;
}
// create swapchain
VkSwapchainCreateInfoKHR swapchain_create_info = {};
swapchain_create_info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
swapchain_create_info.surface = surface;
swapchain_create_info.minImageCount = image_count;
swapchain_create_info.imageFormat = surface_format;
swapchain_create_info.imageColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR; // FIXME: hardcoded
swapchain_create_info.imageExtent.width = 640; // FIXME: hardcoded
swapchain_create_info.imageExtent.height = 480;
swapchain_create_info.imageArrayLayers = 1;
swapchain_create_info.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
swapchain_create_info.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchain_create_info.preTransform = capabilities.currentTransform;
swapchain_create_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
swapchain_create_info.presentMode = present_mode;
swapchain_create_info.clipped = VK_TRUE;
VkSwapchainKHR old_swapchain = swapchain;
swapchain_create_info.oldSwapchain = old_swapchain;
vkCreateSwapchainKHR(device, &swapchain_create_info, nullptr, &swapchain);
if(old_swapchain != VK_NULL_HANDLE) {
vkDestroySwapchainKHR(device, old_swapchain, nullptr);
}
vkGetSwapchainImagesKHR(device, swapchain, &image_count,
nullptr);
swapchain_images.resize(image_count);
vkGetSwapchainImagesKHR(device, swapchain, &image_count, swapchain_images.data());
swapchain_views.resize(swapchain_images.size());
for (size_t i = 0; i < swapchain_images.size(); i++) {
VkImageViewCreateInfo view_create_info = {};
view_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
view_create_info.image = swapchain_images[i];
view_create_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
view_create_info.format = surface_format;
view_create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
view_create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
view_create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
view_create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
view_create_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
view_create_info.subresourceRange.baseMipLevel = 0;
view_create_info.subresourceRange.levelCount = 1;
view_create_info.subresourceRange.baseArrayLayer = 0;
view_create_info.subresourceRange.layerCount = 1;
vkCreateImageView(device, &view_create_info, nullptr,&swapchain_views[i]);
}
printf("Initialized swap chain!\n");
}
static void gfx_vulkan_create_render_pass() {
VkAttachmentDescription color_attachment = {};
color_attachment.format = surface_format;
color_attachment.samples = VK_SAMPLE_COUNT_1_BIT;
color_attachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
color_attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
color_attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
color_attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
color_attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
color_attachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
VkAttachmentReference color_attachment_ref = {};
color_attachment_ref.attachment = 0;
color_attachment_ref.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkAttachmentDescription depth_attachment = {};
depth_attachment.format = VK_FORMAT_D32_SFLOAT;
depth_attachment.samples = VK_SAMPLE_COUNT_1_BIT;
depth_attachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
depth_attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
depth_attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
depth_attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
depth_attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
depth_attachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
VkAttachmentReference depth_attachment_ref = {};
depth_attachment_ref.attachment = 1;
depth_attachment_ref.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
VkSubpassDependency dependency = {};
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.dstSubpass = 0;
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
dependency.srcAccessMask = 0;
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
dependency.dependencyFlags = 0;
VkSubpassDescription subpass = {};
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachmentCount = 1;
subpass.pColorAttachments = &color_attachment_ref;
subpass.pDepthStencilAttachment = &depth_attachment_ref;
std::array<VkAttachmentDescription, 2> attachments = { color_attachment, depth_attachment };
VkRenderPassCreateInfo render_pass_create_info = {};
render_pass_create_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
render_pass_create_info.attachmentCount = attachments.size();
render_pass_create_info.pAttachments = attachments.data();
render_pass_create_info.subpassCount = 1;
render_pass_create_info.pSubpasses = &subpass;
render_pass_create_info.dependencyCount = 1;
render_pass_create_info.pDependencies = &dependency;
vkCreateRenderPass(device, &render_pass_create_info, nullptr, &render_pass);
}
2022-10-03 22:49:19 -04:00
static void gfx_vulkan_renderer_init(void) {
2022-10-04 09:34:57 -04:00
gfx_vulkan_create_instance();
gfx_vulkan_create_device();
gfx_vulkan_create_swapchain();
gfx_vulkan_create_render_pass();
2022-10-03 22:49:19 -04:00
}
static void gfx_vulkan_renderer_on_resize(void) {
2022-10-04 09:34:57 -04:00
// doesn't seem to be called?
2022-10-03 22:49:19 -04:00
}
static void gfx_vulkan_renderer_start_frame(void) {
}
static void gfx_vulkan_renderer_end_frame(void) {
}
static void gfx_vulkan_renderer_finish_render(void) {
}
static const char* gfx_vulkan_get_name() {
return "Vulkan";
}
struct GfxRenderingAPI gfx_vulkan_api = {
gfx_vulkan_renderer_z_is_from_0_to_1,
gfx_vulkan_renderer_unload_shader,
gfx_vulkan_renderer_load_shader,
gfx_vulkan_renderer_create_and_load_new_shader,
gfx_vulkan_renderer_lookup_shader,
gfx_vulkan_renderer_shader_get_info,
gfx_vulkan_renderer_new_texture,
gfx_vulkan_renderer_select_texture,
gfx_vulkan_renderer_upload_texture,
gfx_vulkan_renderer_set_sampler_parameters,
gfx_vulkan_renderer_set_depth_test,
gfx_vulkan_renderer_set_depth_mask,
gfx_vulkan_renderer_set_zmode_decal,
gfx_vulkan_renderer_set_viewport,
gfx_vulkan_renderer_set_scissor,
gfx_vulkan_renderer_set_use_alpha,
gfx_vulkan_renderer_draw_triangles,
gfx_vulkan_renderer_init,
gfx_vulkan_renderer_on_resize,
gfx_vulkan_renderer_start_frame,
gfx_vulkan_renderer_end_frame,
gfx_vulkan_renderer_finish_render,
gfx_vulkan_get_name,
};