일단 완성

This commit is contained in:
2025-05-17 12:34:03 +09:00
parent 6ec8ce37d0
commit bba916f29e
22 changed files with 9564 additions and 95 deletions

8422
include/stb/stb_image.h Normal file

File diff suppressed because it is too large Load Diff

View 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

View File

@@ -1,15 +1,33 @@
#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 {
class Graphics final {
public:
Graphics(gsl::not_null<Window *> window);
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 EndFrame();
BufferHandle CreateVertexBuffer(gsl::span<Vertex> vertices);
BufferHandle CreateIndexBuffer(gsl::span<std::uint32_t> indices);
void DestroyBuffer(BufferHandle handle);
TextureHandle CreateTexture(gsl::czstring path);
void DestroyTexture(TextureHandle handle);
private:
struct QueueFamilyIndices {
std::optional<std::uint32_t> graphics_family = std::nullopt;
@@ -28,6 +46,8 @@ class Graphics final {
bool IsValid() const { return !formats.empty() && !present_modes.empty(); }
};
// 초기화
void InitializeVulkan();
void CreateInstance();
void SetupDebugMessenger();
@@ -38,6 +58,22 @@ class Graphics final {
void CreateImageViews();
void CreateRenderPass();
void CreateGraphicsPipeline();
void CreateFramebuffers();
void CreateCommandPool();
void CreateCommandBuffer();
void CreateSignals();
void CreateDescriptorSetLayouts();
void CreateDescriptorPools();
void CreateDescriptorSets();
void CreateTextureSampler();
void RecreateSwapChain();
void CleanupSwapChain();
// 랜더링
void BeginCommands();
void EndCommands();
std::vector<gsl::czstring> GetRequiredInstanceExtentions();
@@ -61,10 +97,29 @@ class Graphics final {
VkPresentModeKHR ChooseSwapPresentMode(
gsl::span<VkPresentModeKHR> present_modes);
VkExtent2D ChooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities);
std::uint32_t ChooseSwapImageCount(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, 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);
VkViewport GetViewport();
VkRect2D GetScissor();
std::array<gsl::czstring, 1> required_device_extentions_ = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME};
@@ -81,14 +136,36 @@ class Graphics final {
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;
gsl::not_null<Window *> window_;
VkCommandPool command_pool_ = VK_NULL_HANDLE;
VkCommandBuffer command_buffer_ = VK_NULL_HANDLE;
VkSemaphore image_available_signal_ = VK_NULL_HANDLE;
VkSemaphore render_finished_signal_ = VK_NULL_HANDLE;
VkFence still_rendering_fence_ = VK_NULL_HANDLE;
std::uint32_t current_image_index_ = 0;
VkDescriptorSetLayout uniform_set_layout_ = VK_NULL_HANDLE;
VkDescriptorPool uniform_pool_ = VK_NULL_HANDLE;
VkDescriptorSet uniform_set_ = VK_NULL_HANDLE;
BufferHandle uniform_buffer_;
void* uniform_buffer_location_;
VkDescriptorSetLayout texture_set_layout_ = VK_NULL_HANDLE;
VkDescriptorPool texture_pool_ = VK_NULL_HANDLE;
VkSampler texture_sampler_ = VK_NULL_HANDLE;
gsl::not_null<Window*> window_;
bool validation_enabled_ = false;
};

View 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

View File

@@ -0,0 +1,8 @@
#pragma once
namespace veng {
struct UniformTransformations {
glm::mat4 view;
glm::mat4 projection;
};
} // namespace veng

38
include/vulkan/vertex.h Normal file
View 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