2023-08-06 08:48:11 -04:00
|
|
|
// SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2022-04-11 23:11:33 -04:00
|
|
|
#pragma once
|
|
|
|
|
2023-09-26 21:21:04 -04:00
|
|
|
#include <QString>
|
2022-04-11 23:11:33 -04:00
|
|
|
#include <array>
|
2022-04-28 17:50:05 -04:00
|
|
|
#include <glm/ext/matrix_float4x4.hpp>
|
2022-08-11 17:53:56 -04:00
|
|
|
#include <map>
|
2023-08-06 08:48:11 -04:00
|
|
|
#include <vector>
|
|
|
|
#include <vulkan/vulkan.h>
|
2022-04-11 23:11:33 -04:00
|
|
|
|
2022-08-10 14:52:28 -04:00
|
|
|
#include <physis.hpp>
|
2022-04-12 00:54:11 -04:00
|
|
|
|
2022-04-12 08:55:38 -04:00
|
|
|
struct RenderPart {
|
2022-04-12 01:57:37 -04:00
|
|
|
size_t numIndices;
|
2022-04-12 00:54:11 -04:00
|
|
|
|
2022-04-12 01:57:37 -04:00
|
|
|
VkBuffer vertexBuffer, indexBuffer;
|
|
|
|
VkDeviceMemory vertexMemory, indexMemory;
|
2023-04-09 15:26:27 -04:00
|
|
|
|
|
|
|
int materialIndex = 0;
|
2022-04-12 00:54:11 -04:00
|
|
|
};
|
|
|
|
|
2022-08-11 17:53:56 -04:00
|
|
|
struct RenderTexture {
|
|
|
|
VkImage handle = VK_NULL_HANDLE;
|
|
|
|
VkDeviceMemory memory = VK_NULL_HANDLE;
|
|
|
|
VkImageView view = VK_NULL_HANDLE;
|
|
|
|
VkSampler sampler = VK_NULL_HANDLE;
|
|
|
|
};
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
enum class MaterialType { Object, Skin };
|
2023-04-09 15:26:27 -04:00
|
|
|
|
|
|
|
struct RenderMaterial {
|
|
|
|
MaterialType type = MaterialType::Object;
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
RenderTexture *diffuseTexture = nullptr;
|
|
|
|
RenderTexture *normalTexture = nullptr;
|
|
|
|
RenderTexture *specularTexture = nullptr;
|
|
|
|
RenderTexture *multiTexture = nullptr;
|
2023-04-09 15:26:27 -04:00
|
|
|
};
|
|
|
|
|
2022-04-12 08:55:38 -04:00
|
|
|
struct RenderModel {
|
2023-09-26 21:21:04 -04:00
|
|
|
QString name;
|
|
|
|
|
2022-08-10 14:52:28 -04:00
|
|
|
physis_MDL model;
|
2022-04-12 08:55:38 -04:00
|
|
|
std::vector<RenderPart> parts;
|
2022-04-28 17:50:05 -04:00
|
|
|
std::array<glm::mat4, 128> boneData;
|
2023-04-09 15:26:27 -04:00
|
|
|
std::vector<RenderMaterial> materials;
|
2022-08-11 17:53:56 -04:00
|
|
|
|
2023-10-13 14:59:28 -04:00
|
|
|
uint16_t from_body_id = 101;
|
|
|
|
uint16_t to_body_id = 101;
|
|
|
|
|
2023-04-09 15:26:27 -04:00
|
|
|
VkBuffer boneInfoBuffer = VK_NULL_HANDLE;
|
|
|
|
VkDeviceMemory boneInfoMemory = VK_NULL_HANDLE;
|
2022-04-12 08:55:38 -04:00
|
|
|
};
|
|
|
|
|
2023-09-26 17:09:12 -04:00
|
|
|
class ImGuiPass;
|
|
|
|
struct ImGuiContext;
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
class Renderer
|
|
|
|
{
|
2022-04-11 23:11:33 -04:00
|
|
|
public:
|
|
|
|
Renderer();
|
|
|
|
|
2022-04-12 00:54:11 -04:00
|
|
|
void initPipeline();
|
2022-04-28 17:50:05 -04:00
|
|
|
void initDescriptors();
|
2022-05-03 12:14:50 -04:00
|
|
|
void initDepth(int width, int height);
|
2022-04-12 12:19:46 -04:00
|
|
|
bool initSwapchain(VkSurfaceKHR surface, int width, int height);
|
2022-04-12 00:30:17 -04:00
|
|
|
void resize(VkSurfaceKHR surface, int width, int height);
|
2022-04-11 23:11:33 -04:00
|
|
|
|
2023-12-10 07:13:42 -05:00
|
|
|
void destroySwapchain();
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
RenderModel addModel(const physis_MDL &model, int lod);
|
2023-12-09 22:35:59 -05:00
|
|
|
void reloadModel(RenderModel &model, uint32_t lod);
|
2023-10-12 23:45:45 -04:00
|
|
|
RenderTexture addTexture(uint32_t width, uint32_t height, const uint8_t *data, uint32_t data_size);
|
2022-04-12 00:54:11 -04:00
|
|
|
|
2023-12-10 08:39:45 -05:00
|
|
|
void render(const std::vector<RenderModel> &models);
|
2022-04-11 23:11:33 -04:00
|
|
|
|
|
|
|
VkInstance instance = VK_NULL_HANDLE;
|
|
|
|
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
|
|
|
|
VkDevice device = VK_NULL_HANDLE;
|
|
|
|
VkQueue graphicsQueue = VK_NULL_HANDLE, presentQueue = VK_NULL_HANDLE;
|
|
|
|
VkCommandPool commandPool = VK_NULL_HANDLE;
|
|
|
|
VkSwapchainKHR swapchain = VK_NULL_HANDLE;
|
|
|
|
VkExtent2D swapchainExtent;
|
|
|
|
std::vector<VkImage> swapchainImages;
|
|
|
|
std::vector<VkImageView> swapchainViews;
|
|
|
|
std::vector<VkFramebuffer> swapchainFramebuffers;
|
|
|
|
VkRenderPass renderPass;
|
|
|
|
std::array<VkCommandBuffer, 3> commandBuffers;
|
|
|
|
std::array<VkFence, 3> inFlightFences;
|
|
|
|
std::array<VkSemaphore, 3> imageAvailableSemaphores, renderFinishedSemaphores;
|
|
|
|
uint32_t currentFrame = 0;
|
2022-04-28 17:50:05 -04:00
|
|
|
|
2022-05-03 12:14:50 -04:00
|
|
|
VkImage depthImage;
|
|
|
|
VkDeviceMemory depthMemory;
|
|
|
|
VkImageView depthView;
|
|
|
|
|
2023-07-09 11:53:12 -04:00
|
|
|
VkImage dummyImage;
|
|
|
|
VkDeviceMemory dummyMemory;
|
|
|
|
VkImageView dummyView;
|
|
|
|
VkSampler dummySampler;
|
|
|
|
|
2022-04-28 17:50:05 -04:00
|
|
|
VkDescriptorPool descriptorPool = VK_NULL_HANDLE;
|
|
|
|
|
|
|
|
VkDescriptorSetLayout setLayout = VK_NULL_HANDLE;
|
2022-08-11 17:53:56 -04:00
|
|
|
|
2023-09-23 14:07:47 -04:00
|
|
|
std::map<uint64_t, VkDescriptorSet> cachedDescriptors;
|
2022-04-28 17:50:05 -04:00
|
|
|
|
2022-04-12 01:57:37 -04:00
|
|
|
VkPipeline pipeline;
|
|
|
|
VkPipelineLayout pipelineLayout;
|
2022-04-12 00:54:11 -04:00
|
|
|
|
|
|
|
std::tuple<VkBuffer, VkDeviceMemory> createBuffer(size_t size, VkBufferUsageFlags usageFlags);
|
|
|
|
|
|
|
|
uint32_t findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties);
|
|
|
|
|
2023-12-10 08:39:45 -05:00
|
|
|
VkShaderModule createShaderModule(const uint32_t *code, int length);
|
2022-04-12 00:54:11 -04:00
|
|
|
|
2023-12-10 08:39:45 -05:00
|
|
|
VkShaderModule loadShaderFromDisk(std::string_view path);
|
2022-08-11 17:53:56 -04:00
|
|
|
|
|
|
|
VkCommandBuffer beginSingleTimeCommands();
|
|
|
|
|
|
|
|
void endSingleTimeCommands(VkCommandBuffer pT);
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
void inlineTransitionImageLayout(VkCommandBuffer commandBuffer,
|
|
|
|
VkImage image,
|
|
|
|
VkFormat format,
|
|
|
|
VkImageAspectFlags aspect,
|
|
|
|
VkImageSubresourceRange range,
|
|
|
|
VkImageLayout oldLayout,
|
|
|
|
VkImageLayout newLayout,
|
2022-08-11 17:53:56 -04:00
|
|
|
VkPipelineStageFlags src_stage_mask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
|
|
|
|
VkPipelineStageFlags dst_stage_mask = VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
|
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
VkDescriptorSet createDescriptorFor(const RenderModel &model, const RenderMaterial &material);
|
2023-04-09 15:26:27 -04:00
|
|
|
|
2023-10-12 23:45:45 -04:00
|
|
|
uint64_t hash(const RenderModel &model, const RenderMaterial &material);
|
2023-07-06 17:35:26 -04:00
|
|
|
|
|
|
|
glm::mat4 view;
|
2023-07-09 11:53:12 -04:00
|
|
|
|
2023-09-26 17:09:12 -04:00
|
|
|
ImGuiContext *ctx = nullptr;
|
|
|
|
|
2023-07-09 11:53:12 -04:00
|
|
|
private:
|
|
|
|
void createDummyTexture();
|
2023-09-26 17:09:12 -04:00
|
|
|
|
|
|
|
ImGuiPass *imGuiPass = nullptr;
|
2022-04-11 23:11:33 -04:00
|
|
|
};
|