iocp 구현하기
This commit is contained in:
12
Client/include/vulkan_engine/vulkan/buffer_handle.h
Normal file
12
Client/include/vulkan_engine/vulkan/buffer_handle.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
namespace veng {
|
||||
|
||||
struct BufferHandle {
|
||||
VkBuffer buffer = VK_NULL_HANDLE;
|
||||
VkDeviceMemory memory = VK_NULL_HANDLE;
|
||||
};
|
||||
|
||||
} // namespace veng
|
||||
39
Client/include/vulkan_engine/vulkan/coordinate.h
Normal file
39
Client/include/vulkan_engine/vulkan/coordinate.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#include "vulkan_engine/asset/object/model.h"
|
||||
|
||||
namespace std {
|
||||
template <>
|
||||
struct hash<glm::i64vec3> {
|
||||
std::size_t operator()(const glm::i64vec3& v) const noexcept {
|
||||
std::size_t h1 = std::hash<int64_t>()(v.x);
|
||||
std::size_t h2 = std::hash<int64_t>()(v.y);
|
||||
std::size_t h3 = std::hash<int64_t>()(v.z);
|
||||
return h1 ^ (h2 << 1) ^ (h3 << 2);
|
||||
}
|
||||
};
|
||||
} // namespace std
|
||||
|
||||
namespace veng {
|
||||
|
||||
class Coord {
|
||||
public:
|
||||
Coord() : seg(glm::i64vec3(0)), pos(glm::vec3(0.f)) {}
|
||||
Coord(glm::vec3 _pos) : seg(glm::i64vec3(0)), pos(_pos) {}
|
||||
Coord(glm::i64vec3 _seg, glm::vec3 _pos) : seg(_seg), pos(_pos) {}
|
||||
|
||||
glm::i64vec3 seg;
|
||||
glm::vec3 pos;
|
||||
|
||||
Coord operator+(const Coord& other) const;
|
||||
Coord operator-(const Coord& other) const;
|
||||
|
||||
std::unordered_map<glm::i64vec3, glm::vec3> coord_system;
|
||||
|
||||
private:
|
||||
const std::float_t border = 1000.f;
|
||||
};
|
||||
|
||||
} // namespace veng
|
||||
54
Client/include/vulkan_engine/vulkan/engine.h
Normal file
54
Client/include/vulkan_engine/vulkan/engine.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
#include "vulkan_engine/asset/loader.h"
|
||||
#include "graphics.h"
|
||||
#include "physics.h"
|
||||
#include "utils/thread_pool.h"
|
||||
|
||||
namespace veng {
|
||||
|
||||
class Engine {
|
||||
public:
|
||||
Engine(gsl::not_null<Graphics*> vulkan_graphics)
|
||||
: vulkan_graphics(vulkan_graphics) {}
|
||||
|
||||
void init();
|
||||
|
||||
void LoadModelAsset(std::string path, std::string name);
|
||||
const Model* GetStaticModel(std::string name);
|
||||
|
||||
Model* SpawnModel(std::string asset_name, std::string name);
|
||||
Model* SpawnLifedModel(std::string asset_name, std::string name,
|
||||
std::float_t lifespan);
|
||||
Model* GetSpawnedObject(std::string name);
|
||||
|
||||
void Update();
|
||||
|
||||
std::function<void(Engine&)> BeginPlay = [](Engine& engine) {};
|
||||
std::function<void(Engine&, std::float_t delta_time)> Tick =
|
||||
[](Engine& engine, std::float_t delta_time) {};
|
||||
|
||||
glm::mat4 view =
|
||||
glm::lookAt(glm::vec3(0.f, 0.f, -5.f), glm::vec3(0.f, 0.f, 0.f),
|
||||
glm::vec3(0.f, -1.f, 0.f));
|
||||
glm::mat4 projection =
|
||||
glm::perspective(glm::radians(103.f), 800.f / 600.f, 0.1f, 1000.f);
|
||||
|
||||
gsl::not_null<Graphics*> vulkan_graphics;
|
||||
|
||||
private:
|
||||
Loader asset_loader_;
|
||||
Physics physics_controller_;
|
||||
utils::ThreadPool thread_pool_;
|
||||
|
||||
glm::ivec2 window_size_ = {0, 0};
|
||||
std::double_t last_frame_time_ = 0.0;
|
||||
|
||||
std::unordered_map<std::string, veng::Model> model_assets_;
|
||||
|
||||
std::unordered_map<std::string, veng::Model> dynamic_immortal_models_;
|
||||
std::unordered_map<std::string, std::pair<veng::Model, std::float_t>>
|
||||
dynamic_models_;
|
||||
};
|
||||
|
||||
} // namespace veng
|
||||
193
Client/include/vulkan_engine/vulkan/graphics.h
Normal file
193
Client/include/vulkan_engine/vulkan/graphics.h
Normal file
@@ -0,0 +1,193 @@
|
||||
#pragma once
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
#include "buffer_handle.h"
|
||||
#include "glfw/glfw_window.h"
|
||||
#include "precomp.h"
|
||||
#include "texture_handle.h"
|
||||
#include "vertex.h"
|
||||
|
||||
namespace veng {
|
||||
|
||||
struct Frame {
|
||||
VkSemaphore image_available_signal = VK_NULL_HANDLE;
|
||||
VkSemaphore render_finished_signal = VK_NULL_HANDLE;
|
||||
VkFence still_rendering_fence = VK_NULL_HANDLE;
|
||||
|
||||
VkCommandBuffer command_buffer = VK_NULL_HANDLE;
|
||||
|
||||
VkDescriptorSet uniform_set = VK_NULL_HANDLE;
|
||||
BufferHandle uniform_buffer;
|
||||
void* uniform_buffer_location;
|
||||
};
|
||||
|
||||
class Graphics final {
|
||||
public:
|
||||
Graphics(gsl::not_null<Window*> window);
|
||||
~Graphics();
|
||||
|
||||
bool BeginFrame();
|
||||
void SetModelMatrix(glm::mat4 model);
|
||||
void SetViewProjection(glm::mat4 view, glm::mat4 projection);
|
||||
void SetTexture(TextureHandle handle);
|
||||
void RenderBuffer(BufferHandle handle, std::uint32_t vertex_count);
|
||||
void RenderIndexedBuffer(BufferHandle vertex_buffer,
|
||||
BufferHandle index_buffer, std::uint32_t count);
|
||||
void RenderModel(struct Model* model);
|
||||
void EndFrame();
|
||||
|
||||
BufferHandle CreateVertexBuffer(gsl::span<Vertex> vertices);
|
||||
BufferHandle CreateIndexBuffer(gsl::span<std::uint32_t> indices);
|
||||
void DestroyBuffer(BufferHandle handle);
|
||||
TextureHandle CreateTexture(gsl::czstring path);
|
||||
TextureHandle CreateTexture(std::vector<std::uint8_t> image_file_data);
|
||||
TextureHandle CreateTexture(gsl::span<std::uint8_t> image_file_data);
|
||||
void DestroyTexture(TextureHandle handle);
|
||||
|
||||
gsl::not_null<Window*> window;
|
||||
private:
|
||||
struct QueueFamilyIndices {
|
||||
std::optional<std::uint32_t> graphics_family = std::nullopt;
|
||||
std::optional<std::uint32_t> presentation_family = std::nullopt;
|
||||
|
||||
bool IsValid() const {
|
||||
return graphics_family.has_value() && presentation_family.has_value();
|
||||
}
|
||||
};
|
||||
|
||||
struct SwapChainProperties {
|
||||
VkSurfaceCapabilitiesKHR capabilities = {};
|
||||
std::vector<VkSurfaceFormatKHR> formats;
|
||||
std::vector<VkPresentModeKHR> present_modes;
|
||||
|
||||
bool IsValid() const { return !formats.empty() && !present_modes.empty(); }
|
||||
};
|
||||
|
||||
// 초기화
|
||||
|
||||
void InitializeVulkan();
|
||||
void CreateInstance();
|
||||
void SetupDebugMessenger();
|
||||
void PickPhysicalDevice();
|
||||
void CreateLogicalDeviceAndQueues();
|
||||
void CreateSurface();
|
||||
void CreateSwapChain();
|
||||
void CreateImageViews();
|
||||
void CreateRenderPass();
|
||||
void CreateGraphicsPipeline();
|
||||
void CreateFramebuffers();
|
||||
void CreateCommandPool();
|
||||
void CreateCommandBuffer();
|
||||
void CreateSignals();
|
||||
void CreateDescriptorSetLayouts();
|
||||
void CreateDescriptorPools();
|
||||
void CreateDescriptorSets();
|
||||
void CreateTextureSampler();
|
||||
void CreateDepthResources();
|
||||
|
||||
void RecreateSwapChain();
|
||||
void CleanupSwapChain();
|
||||
|
||||
// 랜더링
|
||||
|
||||
void BeginCommands();
|
||||
void EndCommands();
|
||||
|
||||
std::vector<gsl::czstring> GetRequiredInstanceExtentions();
|
||||
|
||||
static gsl::span<gsl::czstring> GetSuggestedInstanceExtentions();
|
||||
static std::vector<VkExtensionProperties> GetSupprotedInstanceExtensions();
|
||||
static bool AreAllExtensionsSupported(gsl::span<gsl::czstring> extensions);
|
||||
|
||||
static std::vector<VkLayerProperties> GetSupprotedValidationLayers();
|
||||
static bool AreAllLayersSupported(gsl::span<gsl::czstring> extensions);
|
||||
|
||||
QueueFamilyIndices FindQueueFamilies(VkPhysicalDevice device);
|
||||
SwapChainProperties GetSwapChainProperties(VkPhysicalDevice device);
|
||||
bool IsDeviceSuitable(VkPhysicalDevice device);
|
||||
std::vector<VkPhysicalDevice> GetAvailableDevices();
|
||||
bool AreAllDeviceExtensionsSupported(VkPhysicalDevice device);
|
||||
std::vector<VkExtensionProperties> GetDeviceAvailableExtensions(
|
||||
VkPhysicalDevice device);
|
||||
|
||||
VkSurfaceFormatKHR ChooseSwapSurfaceFormat(
|
||||
gsl::span<VkSurfaceFormatKHR> formats);
|
||||
VkPresentModeKHR ChooseSwapPresentMode(
|
||||
gsl::span<VkPresentModeKHR> present_modes);
|
||||
VkExtent2D ChooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities);
|
||||
std::uint32_t ChooseSwapImageCount(
|
||||
const VkSurfaceCapabilitiesKHR& capabilities);
|
||||
|
||||
VkShaderModule CreateShaderModule(gsl::span<std::uint8_t> buffer);
|
||||
|
||||
std::uint32_t FindMemoryType(std::uint32_t type_bits_filter,
|
||||
VkMemoryPropertyFlags required_properties);
|
||||
|
||||
BufferHandle CreateBuffer(VkDeviceSize size, VkBufferUsageFlags usage,
|
||||
VkMemoryPropertyFlags properties);
|
||||
VkCommandBuffer BeginTransientCommandBuffer();
|
||||
void EndTransientCommandBuffer(VkCommandBuffer command_buffer);
|
||||
void CreateUniformBuffers();
|
||||
|
||||
TextureHandle CreateImage(glm::ivec2 size, VkFormat image_format,
|
||||
VkBufferUsageFlags usage,
|
||||
VkMemoryPropertyFlags properties);
|
||||
void TransitionImageLayout(VkImage image, VkImageLayout old_layout,
|
||||
VkImageLayout new_layout);
|
||||
void CopyBufferToImage(VkBuffer buffer, VkImage image, glm::ivec2 image_size);
|
||||
VkImageView CreateImageView(VkImage image, VkFormat format,
|
||||
VkImageAspectFlags aspect_flag);
|
||||
|
||||
VkViewport GetViewport();
|
||||
VkRect2D GetScissor();
|
||||
|
||||
std::array<gsl::czstring, 1> required_device_extentions_ = {
|
||||
VK_KHR_SWAPCHAIN_EXTENSION_NAME};
|
||||
|
||||
VkInstance instance_ = VK_NULL_HANDLE;
|
||||
VkDebugUtilsMessengerEXT debug_messenger_;
|
||||
|
||||
VkPhysicalDevice physical_device_ = VK_NULL_HANDLE;
|
||||
VkDevice logical_device_ = VK_NULL_HANDLE;
|
||||
VkQueue graphics_queue_ = VK_NULL_HANDLE;
|
||||
VkQueue present_queue_ = VK_NULL_HANDLE;
|
||||
|
||||
VkSurfaceKHR surface_ = VK_NULL_HANDLE;
|
||||
VkSwapchainKHR swap_chain_ = VK_NULL_HANDLE;
|
||||
VkSurfaceFormatKHR surface_format_;
|
||||
VkPresentModeKHR present_mode_;
|
||||
VkExtent2D extent_;
|
||||
|
||||
std::vector<VkImage> swap_chain_images_;
|
||||
std::vector<VkImageView> swap_chain_image_views_;
|
||||
std::vector<VkFramebuffer> swap_chain_framebuffers_;
|
||||
|
||||
VkPipelineLayout pipeline_layout_ = VK_NULL_HANDLE;
|
||||
VkRenderPass render_pass_ = VK_NULL_HANDLE;
|
||||
VkPipeline pipeline_ = VK_NULL_HANDLE;
|
||||
VkDescriptorSetLayout descriptor_set_layout_ = VK_NULL_HANDLE;
|
||||
|
||||
VkCommandPool command_pool_ = VK_NULL_HANDLE;
|
||||
|
||||
std::uint32_t current_image_index_ = 0;
|
||||
|
||||
VkDescriptorSetLayout uniform_set_layout_ = VK_NULL_HANDLE;
|
||||
VkDescriptorPool uniform_pool_ = VK_NULL_HANDLE;
|
||||
|
||||
|
||||
VkDescriptorSetLayout texture_set_layout_ = VK_NULL_HANDLE;
|
||||
VkDescriptorPool texture_pool_ = VK_NULL_HANDLE;
|
||||
VkSampler texture_sampler_ = VK_NULL_HANDLE;
|
||||
TextureHandle depth_texture_;
|
||||
|
||||
std::array<Frame, MAX_BUFFERED_FRAMES> frames_;
|
||||
std::int32_t current_frame_ = 0;
|
||||
|
||||
bool validation_enabled_ = false;
|
||||
};
|
||||
|
||||
VkDebugUtilsMessengerCreateInfoEXT GetCreateMessengerInfo();
|
||||
bool IsExtensionSupported(gsl::span<VkExtensionProperties> extensions,
|
||||
gsl::czstring name);
|
||||
|
||||
} // namespace veng
|
||||
21
Client/include/vulkan_engine/vulkan/physics.h
Normal file
21
Client/include/vulkan_engine/vulkan/physics.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "vulkan_engine/asset/object/model.h"
|
||||
#include "vulkan_engine/vulkan/vertex.h"
|
||||
#include "utils/thread_pool.h"
|
||||
|
||||
namespace veng {
|
||||
class Physics {
|
||||
public:
|
||||
void invokeOnColisionEvent(gsl::not_null<utils::ThreadPool *> thread_pool, gsl::span<Model*> models);
|
||||
|
||||
bool RayTrace(const glm::vec3& rayOrigin, const glm::vec3& rayDir,
|
||||
const glm::vec3& v0, const glm::vec3& v1, const glm::vec3& v2,
|
||||
std::float_t& outDistance);
|
||||
|
||||
private:
|
||||
bool IsPointInsideMesh_(const glm::vec3& point,
|
||||
const std::vector<veng::Vertex>& vertices,
|
||||
const std::vector<std::uint32_t>& indices);
|
||||
};
|
||||
} // namespace veng
|
||||
14
Client/include/vulkan_engine/vulkan/texture_handle.h
Normal file
14
Client/include/vulkan_engine/vulkan/texture_handle.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
namespace veng {
|
||||
|
||||
struct TextureHandle {
|
||||
VkImage image = VK_NULL_HANDLE;
|
||||
VkImageView image_view = VK_NULL_HANDLE;
|
||||
VkDeviceMemory memory = VK_NULL_HANDLE;
|
||||
VkDescriptorSet set = VK_NULL_HANDLE;
|
||||
};
|
||||
|
||||
} // namespace veng
|
||||
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
namespace veng {
|
||||
struct UniformTransformations {
|
||||
glm::mat4 view;
|
||||
glm::mat4 projection;
|
||||
};
|
||||
} // namespace veng
|
||||
38
Client/include/vulkan_engine/vulkan/vertex.h
Normal file
38
Client/include/vulkan_engine/vulkan/vertex.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
namespace veng {
|
||||
struct Vertex {
|
||||
Vertex() : position(glm::vec3(0.f)), uv(glm::vec2(0.f)) {}
|
||||
Vertex(glm::vec3 _position, glm::vec2 _uv) : position(_position), uv(_uv) {}
|
||||
|
||||
glm::vec3 position;
|
||||
glm::vec2 uv;
|
||||
|
||||
static VkVertexInputBindingDescription GetBindingDescription() {
|
||||
VkVertexInputBindingDescription description = {};
|
||||
description.binding = 0;
|
||||
description.stride = sizeof(Vertex);
|
||||
description.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
|
||||
|
||||
return description;
|
||||
}
|
||||
|
||||
static std::array<VkVertexInputAttributeDescription, 2> GetAttributeDescriptions() {
|
||||
std::array<VkVertexInputAttributeDescription, 2> descriptions = {};
|
||||
|
||||
descriptions[0].binding = 0;
|
||||
descriptions[0].location = 0;
|
||||
descriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
|
||||
descriptions[0].offset = offsetof(Vertex, position);
|
||||
|
||||
descriptions[1].binding = 0;
|
||||
descriptions[1].location = 1;
|
||||
descriptions[1].format = VK_FORMAT_R32G32_SFLOAT;
|
||||
descriptions[1].offset = offsetof(Vertex, uv);
|
||||
|
||||
return descriptions;
|
||||
}
|
||||
};
|
||||
} // namespace veng
|
||||
Reference in New Issue
Block a user