Add namespace to all classes

This commit is contained in:
Thraix
2023-02-05 10:52:27 +01:00
parent 708b81c571
commit 88979a5ab9
32 changed files with 2611 additions and 2535 deletions
-1
View File
@@ -192,7 +192,6 @@
<ClInclude Include="src\VertexBuffer.h" /> <ClInclude Include="src\VertexBuffer.h" />
<ClInclude Include="src\VertexDescriptor.h" /> <ClInclude Include="src\VertexDescriptor.h" />
<ClInclude Include="src\VulkanException.h" /> <ClInclude Include="src\VulkanException.h" />
<ClInclude Include="src\Window.h" />
<ClInclude Include="src\VertexPassthrough.h" /> <ClInclude Include="src\VertexPassthrough.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
-3
View File
@@ -50,9 +50,6 @@
<ClInclude Include="src\Instance.h"> <ClInclude Include="src\Instance.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\Window.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\Timer.h"> <ClInclude Include="src\Timer.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
+239
View File
@@ -0,0 +1,239 @@
#pragma once
#include "Buffer.h"
#include "DescriptorPool.h"
#include "DescriptorSet.h"
#include "Framebuffer.h"
#include "IndexBuffer.h"
#include "Instance.h"
#include "Pipeline.h"
#include "Texture2D.h"
#include "Timer.h"
#include "UniformBuffer.h"
#include "Vertex.h"
#include "VertexBuffer.h"
#include "VertexPassthrough.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
#include <optional>
#include <set>
#include <vector>
namespace Copium
{
const std::vector<Vertex> vertices = {
Vertex{{-0.5f, 0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}},
Vertex{{ 0.5f, 0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f}},
Vertex{{ 0.5f, 0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}},
Vertex{{-0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
Vertex{{-0.5f, 0.0f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}},
Vertex{{ 0.5f, 0.0f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f}},
Vertex{{ 0.5f, 0.0f, 0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}},
Vertex{{-0.5f, 0.0f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
};
const std::vector<uint16_t> indices = {
0, 1, 2, 2, 3, 0,
4, 5, 6, 6, 7, 4
};
const std::vector<VertexPassthrough> verticesPassthrough = {
VertexPassthrough{{-1.0f, -1.0f}},
VertexPassthrough{{ 1.0f, -1.0f}},
VertexPassthrough{{ 1.0f, 1.0f}},
VertexPassthrough{{-1.0f, 1.0f}},
};
const std::vector<uint16_t> indicesPassthrough = {
0, 1, 2, 2, 3, 0,
};
struct alignas(64) ShaderUniform
{
alignas(16) glm::mat4 projection;
alignas(16) glm::mat4 view;
alignas(16) glm::mat4 model;
alignas(16) glm::vec3 lightPos;
};
class Application final
{
private:
std::unique_ptr<Instance> instance;
std::unique_ptr<Pipeline> graphicsPipeline;
std::unique_ptr<Texture2D> texture2D;
std::unique_ptr<UniformBuffer> shaderUniformBuffer;
std::unique_ptr<DescriptorPool> descriptorPool;
std::unique_ptr<DescriptorSet> descriptorSet;
std::unique_ptr<VertexBuffer> vertexBuffer;
std::unique_ptr<IndexBuffer> indexBuffer;
std::unique_ptr<CommandBuffer> commandBuffer;
std::unique_ptr<Framebuffer> framebuffer;
std::unique_ptr<Pipeline> graphicsPipelinePassthrough;
std::unique_ptr<VertexBuffer> vertexBufferPassthrough;
std::unique_ptr<IndexBuffer> indexBufferPassthrough;
std::unique_ptr<DescriptorSet> descriptorSetPassthrough;
public:
Application()
{
InitializeInstance();
InitializeFrameBuffer();
InitializeGraphicsPipeline();
InitializeTextureSampler();
InitializeUniformBuffer();
InitializeDescriptorSets();
InitializeVertexBuffer();
InitializeIndexBuffer();
InitializeCommandBuffer();
}
~Application()
{
vkDeviceWaitIdle(instance->GetDevice());
}
Application(Application&&) = delete;
Application(const Application&) = delete;
Application& operator=(Application&&) = delete;
Application& operator=(const Application&) = delete;
bool Update()
{
if (!instance->BeginPresent())
return true;
RecordCommandBuffer();
commandBuffer->SubmitAsGraphicsQueue();
return instance->EndPresent();
}
private:
void InitializeInstance()
{
instance = std::make_unique<Instance>("Copium Engine");
}
void InitializeFrameBuffer()
{
framebuffer = std::make_unique<Framebuffer>(*instance, instance->GetSwapChain().GetExtent().width, instance->GetSwapChain().GetExtent().height);
}
void InitializeTextureSampler()
{
texture2D = std::make_unique<Texture2D>(*instance, "res/textures/texture.png");
}
void InitializeUniformBuffer()
{
shaderUniformBuffer = std::make_unique<UniformBuffer>(*instance, sizeof(ShaderUniform));
}
void InitializeDescriptorSets()
{
descriptorPool = std::make_unique<DescriptorPool>(*instance);
descriptorSet = std::make_unique<DescriptorSet>(*instance, *descriptorPool, graphicsPipeline->GetDescriptorSetLayout(0));
descriptorSet->AddUniform(*shaderUniformBuffer, 0);
descriptorSet->AddTexture2D(*texture2D, 1);
descriptorSetPassthrough = std::make_unique<DescriptorSet>(*instance, *descriptorPool, graphicsPipelinePassthrough->GetDescriptorSetLayout(0));
descriptorSetPassthrough->AddTexture2D(framebuffer->GetTexture2D(), 0);
}
void InitializeGraphicsPipeline()
{
PipelineCreator creator{framebuffer->GetRenderPass(), "res/shaders/shader.vert", "res/shaders/shader.frag"};
creator.AddDescriptorSetLayoutBinding(0, 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT);
creator.AddDescriptorSetLayoutBinding(0, 1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT);
creator.SetVertexDescriptor(Vertex::GetDescriptor());
creator.SetCullMode(VK_CULL_MODE_NONE);
graphicsPipeline = std::make_unique<Pipeline>(*instance, creator);
PipelineCreator creatorPassthrough{instance->GetSwapChain().GetRenderPass(), "res/shaders/passthrough.vert", "res/shaders/passthrough.frag"};
creatorPassthrough.AddDescriptorSetLayoutBinding(0, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT);
creatorPassthrough.SetVertexDescriptor(VertexPassthrough::GetDescriptor());
creatorPassthrough.SetCullMode(VK_CULL_MODE_NONE);
graphicsPipelinePassthrough = std::make_unique<Pipeline>(*instance, creatorPassthrough);
}
void InitializeVertexBuffer()
{
vertexBuffer = std::make_unique<VertexBuffer>(*instance, Vertex::GetDescriptor(), vertices.size());
vertexBuffer->Update(0, (void*)vertices.data());
vertexBufferPassthrough = std::make_unique<VertexBuffer>(*instance, VertexPassthrough::GetDescriptor(), verticesPassthrough.size());
vertexBufferPassthrough->Update(0, (void*)verticesPassthrough.data());
}
void InitializeIndexBuffer()
{
indexBuffer = std::make_unique<IndexBuffer>(*instance, indices.size());
indexBuffer->UpdateStaging((void*)indices.data());
indexBufferPassthrough = std::make_unique<IndexBuffer>(*instance, indicesPassthrough.size());
indexBufferPassthrough->UpdateStaging((void*)indicesPassthrough.data());
}
void InitializeCommandBuffer()
{
commandBuffer = std::make_unique<CommandBuffer>(*instance, CommandBuffer::Type::Dynamic);
}
void RecordCommandBuffer()
{
commandBuffer->Begin();
std::vector<VkClearValue> clearValues{2};
clearValues[0].color = {{0.0f, 0.0f, 0.0f, 1.0f}};
clearValues[1].depthStencil = {1.0f, 0};
framebuffer->Bind(*commandBuffer);
graphicsPipeline->Bind(*commandBuffer);
UpdateUniformBuffer();
vertexBuffer->Bind(*commandBuffer);
indexBuffer->Bind(*commandBuffer);
graphicsPipeline->SetDescriptorSet(0, *descriptorSet);
graphicsPipeline->BindDescriptorSets(commandBuffer->GetHandle());
indexBuffer->Draw(*commandBuffer);
framebuffer->Unbind(*commandBuffer);
instance->GetSwapChain().BeginFrameBuffer(*commandBuffer);
graphicsPipelinePassthrough->Bind(*commandBuffer);
graphicsPipelinePassthrough->SetDescriptorSet(0, *descriptorSetPassthrough);
graphicsPipelinePassthrough->BindDescriptorSets(commandBuffer->GetHandle());
vertexBufferPassthrough->Bind(*commandBuffer);
indexBufferPassthrough->Bind(*commandBuffer);
indexBufferPassthrough->Draw(*commandBuffer);
instance->GetSwapChain().EndFrameBuffer(*commandBuffer);
commandBuffer->End();
}
void UpdateUniformBuffer()
{
static Timer startTimer;
float time = startTimer.Elapsed();
ShaderUniform shaderUniform;
shaderUniform.view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
shaderUniform.projection = glm::perspective(glm::radians(45.0f), instance->GetSwapChain().GetExtent().width / (float)instance->GetSwapChain().GetExtent().height, 0.1f, 10.0f);
shaderUniform.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 1.0f, 0.0f));
shaderUniform.projection[1][1] *= -1;
shaderUniform.lightPos = glm::rotate(glm::mat4{1.0f}, time * glm::radians(45.0f), glm::vec3(0, 1, 0)) * glm::vec4{0.3, 0.1, 0, 1};
shaderUniformBuffer->Update(shaderUniform);
}
};
}
+3
View File
@@ -5,6 +5,8 @@
#include <optional> #include <optional>
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
namespace Copium
{
class Buffer class Buffer
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(Buffer); CP_DELETE_COPY_AND_MOVE_CTOR(Buffer);
@@ -168,3 +170,4 @@ public:
vkFreeCommandBuffers(instance.GetDevice(), instance.GetCommandPool(), 1, &commandBuffer); vkFreeCommandBuffers(instance.GetDevice(), instance.GetCommandPool(), 1, &commandBuffer);
} }
}; };
}
+12 -9
View File
@@ -4,26 +4,28 @@
#include "Instance.h" #include "Instance.h"
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
enum class CommandBufferType namespace Copium
{ {
SingleUse, Dynamic
};
class CommandBuffer class CommandBuffer
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(CommandBuffer); CP_DELETE_COPY_AND_MOVE_CTOR(CommandBuffer);
public:
enum class Type
{
SingleUse, Dynamic
};
private: private:
Instance& instance; Instance& instance;
std::vector<VkCommandBuffer> commandBuffers; std::vector<VkCommandBuffer> commandBuffers;
const CommandBufferType type; const Type type;
VkCommandBuffer currentCommandBuffer{VK_NULL_HANDLE}; VkCommandBuffer currentCommandBuffer{VK_NULL_HANDLE};
public: public:
CommandBuffer(Instance& instance, CommandBufferType type) CommandBuffer(Instance& instance, Type type)
: instance{instance}, type{type} : instance{instance}, type{type}
{ {
if (type == CommandBufferType::Dynamic) if (type == Type::Dynamic)
commandBuffers.resize(instance.GetMaxFramesInFlight()); commandBuffers.resize(instance.GetMaxFramesInFlight());
else else
commandBuffers.resize(1); commandBuffers.resize(1);
@@ -55,11 +57,11 @@ public:
beginInfo.pInheritanceInfo = nullptr; beginInfo.pInheritanceInfo = nullptr;
switch (type) switch (type)
{ {
case CommandBufferType::SingleUse: case Type::SingleUse:
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
currentCommandBuffer = commandBuffers.front(); currentCommandBuffer = commandBuffers.front();
break; break;
case CommandBufferType::Dynamic: case Type::Dynamic:
currentCommandBuffer = commandBuffers[instance.GetFlightIndex()]; currentCommandBuffer = commandBuffers[instance.GetFlightIndex()];
break; break;
default: default:
@@ -97,3 +99,4 @@ public:
return currentCommandBuffer; return currentCommandBuffer;
} }
}; };
}
+4 -1
View File
@@ -3,12 +3,14 @@
#include "Common.h" #include "Common.h"
#include "CommandBuffer.h" #include "CommandBuffer.h"
namespace Copium
{
class CommandBufferScoped : public CommandBuffer class CommandBufferScoped : public CommandBuffer
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(CommandBufferScoped); CP_DELETE_COPY_AND_MOVE_CTOR(CommandBufferScoped);
public: public:
CommandBufferScoped(Instance& instance) CommandBufferScoped(Instance& instance)
: CommandBuffer{instance, CommandBufferType::SingleUse} : CommandBuffer{instance, Type::SingleUse}
{ {
CommandBuffer::Begin(); CommandBuffer::Begin();
} }
@@ -19,3 +21,4 @@ public:
CommandBuffer::Submit(); CommandBuffer::Submit();
} }
}; };
}
+19 -17
View File
@@ -3,29 +3,29 @@
#include "VulkanException.h" #include "VulkanException.h"
#include <iostream> #include <iostream>
#define TERM_RED "\x1B[31m" #define CP_TERM_RED "\x1B[31m"
#define TERM_GREEN "\x1B[32m" #define CP_TERM_GREEN "\x1B[32m"
#define TERM_YELLOW "\x1B[33m" #define CP_TERM_YELLOW "\x1B[33m"
#define TERM_GRAY "\x1B[90m" #define CP_TERM_GRAY "\x1B[90m"
#define TERM_CLEAR "\033[0m" #define CP_TERM_CLEAR "\033[0m"
#define CP_DEBUG(format, ...) std::cout << TERM_GRAY << "[DBG] " << StringFormat(format, __VA_ARGS__) << TERM_CLEAR << std::endl #define CP_DEBUG(format, ...) std::cout << CP_TERM_GRAY << "[DBG] " << Copium::StringFormat(format, __VA_ARGS__) << CP_TERM_CLEAR << std::endl
#define CP_INFO(format, ...) std::cout << "[INF] " << StringFormat(format, __VA_ARGS__) << std::endl #define CP_INFO(format, ...) std::cout << "[INF] " << Copium::StringFormat(format, __VA_ARGS__) << std::endl
#define CP_WARN(format, ...) std::cout << TERM_YELLOW << "[WRN] " << StringFormat(format, __VA_ARGS__) << TERM_CLEAR << std::endl #define CP_WARN(format, ...) std::cout << CP_TERM_YELLOW << "[WRN] " << Copium::StringFormat(format, __VA_ARGS__) << CP_TERM_CLEAR << std::endl
#define CP_ERR(format, ...) std::cout << TERM_RED << "[ERR] " << StringFormat(format, __VA_ARGS__) << TERM_CLEAR << std::endl #define CP_ERR(format, ...) std::cout << CP_TERM_RED << "[ERR] " << Copium::StringFormat(format, __VA_ARGS__) << CP_TERM_CLEAR << std::endl
// Continue traces, will not print the [XXX] tag before the log // Continue traces, will not print the [XXX] tag before the log
#define CP_DEBUG_CONT(format, ...) std::cout << TERM_GRAY << " " << StringFormat(format, __VA_ARGS__) << TERM_CLEAR << std::endl #define CP_DEBUG_CONT(format, ...) std::cout << CP_TERM_GRAY << " " << Copium::StringFormat(format, __VA_ARGS__) << CP_TERM_CLEAR << std::endl
#define CP_INFO_CONT(format, ...) std::cout << " " << StringFormat(format, __VA_ARGS__) << std::endl #define CP_INFO_CONT(format, ...) std::cout << " " << Copium::StringFormat(format, __VA_ARGS__) << std::endl
#define CP_WARN_CONT(format, ...) std::cout << TERM_YELLOW << " " << StringFormat(format, __VA_ARGS__) << TERM_CLEAR << std::endl #define CP_WARN_CONT(format, ...) std::cout << CP_TERM_YELLOW << " " << Copium::StringFormat(format, __VA_ARGS__) << CP_TERM_CLEAR << std::endl
#define CP_ERR_CONT(format, ...) std::cout << TERM_RED << " " << StringFormat(format, __VA_ARGS__) << TERM_CLEAR << std::endl #define CP_ERR_CONT(format, ...) std::cout << CP_TERM_RED << " " << Copium::StringFormat(format, __VA_ARGS__) << CP_TERM_CLEAR << std::endl
#define CP_UNIMPLEMENTED() CP_WARN("%s is unimplemented", __FUNCTION__) #define CP_UNIMPLEMENTED() CP_WARN("%s is unimplemented", __FUNCTION__)
#define CP_ABORT(format, ...) \ #define CP_ABORT(format, ...) \
do \ do \
{ \ { \
CP_ERR(format, __VA_ARGS__); \ CP_ERR(format, __VA_ARGS__); \
throw std::runtime_error(StringFormat(format, __VA_ARGS__)); \ throw std::runtime_error(Copium::StringFormat(format, __VA_ARGS__)); \
} while(false) } while(false)
#define CP_ASSERT(Function, format, ...) \ #define CP_ASSERT(Function, format, ...) \
do \ do \
@@ -33,7 +33,7 @@
if(!(Function)) \ if(!(Function)) \
{ \ { \
CP_ERR(format, __VA_ARGS__); \ CP_ERR(format, __VA_ARGS__); \
throw std::runtime_error(StringFormat(format, __VA_ARGS__)); \ throw std::runtime_error(Copium::StringFormat(format, __VA_ARGS__)); \
} \ } \
} while(false) } while(false)
#define CP_VK_ASSERT(Function, format, ...) \ #define CP_VK_ASSERT(Function, format, ...) \
@@ -42,7 +42,7 @@
if(Function != VK_SUCCESS) \ if(Function != VK_SUCCESS) \
{ \ { \
CP_ERR(format, __VA_ARGS__); \ CP_ERR(format, __VA_ARGS__); \
throw VulkanException(StringFormat(format, __VA_ARGS__)); \ throw VulkanException(Copium::StringFormat(format, __VA_ARGS__)); \
} \ } \
} while(false) } while(false)
#define CP_DELETE_COPY_AND_MOVE_CTOR(ClassName) \ #define CP_DELETE_COPY_AND_MOVE_CTOR(ClassName) \
@@ -51,6 +51,8 @@
ClassName& operator=(ClassName&&) = delete; \ ClassName& operator=(ClassName&&) = delete; \
ClassName& operator=(const ClassName&) = delete ClassName& operator=(const ClassName&) = delete
namespace Copium
{
template<typename ... Args> template<typename ... Args>
std::string StringFormat(const std::string& format, Args... args) std::string StringFormat(const std::string& format, Args... args)
{ {
@@ -60,4 +62,4 @@ std::string StringFormat(const std::string& format, Args... args)
std::snprintf(buf.get(), size, format.c_str(), args...); std::snprintf(buf.get(), size, format.c_str(), args...);
return std::string(buf.get(), buf.get() + size - 1); return std::string(buf.get(), buf.get() + size - 1);
} }
}
+3
View File
@@ -1,6 +1,8 @@
#pragma once #pragma once
#include "Common.h" #include "Common.h"
namespace Copium
{
class DebugMessenger class DebugMessenger
{ {
@@ -86,3 +88,4 @@ private:
} }
} }
}; };
}
+3
View File
@@ -4,6 +4,8 @@
#include "Instance.h" #include "Instance.h"
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
namespace Copium
{
class DescriptorPool final class DescriptorPool final
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(DescriptorPool); CP_DELETE_COPY_AND_MOVE_CTOR(DescriptorPool);
@@ -59,3 +61,4 @@ public:
vkFreeDescriptorSets(instance.GetDevice(), descriptorPool, descriptorSets.size(), descriptorSets.data()); vkFreeDescriptorSets(instance.GetDevice(), descriptorPool, descriptorSets.size(), descriptorSets.data());
} }
}; };
}
+3
View File
@@ -6,6 +6,8 @@
#include "UniformBuffer.h" #include "UniformBuffer.h"
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
namespace Copium
{
class DescriptorSet final class DescriptorSet final
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(DescriptorSet); CP_DELETE_COPY_AND_MOVE_CTOR(DescriptorSet);
@@ -70,3 +72,4 @@ public:
return descriptorSets[instance.GetFlightIndex()]; return descriptorSets[instance.GetFlightIndex()];
} }
}; };
}
+6 -1
View File
@@ -8,8 +8,12 @@
#include <fstream> #include <fstream>
namespace FileSystem namespace Copium
{ {
class FileSystem
{
FileSystem() = delete;
public:
static std::vector<char> ReadFile(const std::string& filename) static std::vector<char> ReadFile(const std::string& filename)
{ {
std::ifstream file(filename, std::ios::ate | std::ios::binary); std::ifstream file(filename, std::ios::ate | std::ios::binary);
@@ -69,4 +73,5 @@ namespace FileSystem
CP_ASSERT(stat(filename.c_str(), &result) == 0, "Cannot stat file %s", filename.c_str()); CP_ASSERT(stat(filename.c_str(), &result) == 0, "Cannot stat file %s", filename.c_str());
return (int64_t)result.st_mtime; return (int64_t)result.st_mtime;
} }
};
} }
+3
View File
@@ -7,6 +7,8 @@
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
namespace Copium
{
// TODO: Add resizing (recreate image, depthImage, framebuffers) // TODO: Add resizing (recreate image, depthImage, framebuffers)
class Framebuffer class Framebuffer
{ {
@@ -172,3 +174,4 @@ private:
} }
} }
}; };
}
+7 -2
View File
@@ -6,6 +6,8 @@
#include "CommandBufferScoped.h" #include "CommandBufferScoped.h"
#include "Instance.h" #include "Instance.h"
namespace Copium
{
class Image class Image
{ {
Image() = delete; Image() = delete;
@@ -91,7 +93,8 @@ public:
if (HasStencilComponent(format)) { if (HasStencilComponent(format)) {
barrier.subresourceRange.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT; barrier.subresourceRange.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
} }
} else { }
else {
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
} }
@@ -101,7 +104,8 @@ public:
srcStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; srcStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
dstStage = VK_PIPELINE_STAGE_TRANSFER_BIT; dstStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
} else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) { }
else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
@@ -180,3 +184,4 @@ private:
CP_ABORT("SelectSupportedFormat : Failed to select supported format"); CP_ABORT("SelectSupportedFormat : Failed to select supported format");
} }
}; };
}
+3
View File
@@ -2,6 +2,8 @@
#include "Buffer.h" #include "Buffer.h"
namespace Copium
{
class IndexBuffer : public Buffer class IndexBuffer : public Buffer
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(IndexBuffer); CP_DELETE_COPY_AND_MOVE_CTOR(IndexBuffer);
@@ -22,3 +24,4 @@ public:
vkCmdDrawIndexed(commandBuffer, indexCount, 1, 0, 0, 0); vkCmdDrawIndexed(commandBuffer, indexCount, 1, 0, 0, 0);
} }
}; };
}
+3
View File
@@ -9,6 +9,8 @@
#include "SwapChain.h" #include "SwapChain.h"
#include "Timer.h" #include "Timer.h"
namespace Copium
{
class Instance final class Instance final
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(Instance); CP_DELETE_COPY_AND_MOVE_CTOR(Instance);
@@ -445,3 +447,4 @@ private:
instance->framebufferResized = true; instance->framebufferResized = true;
} }
}; };
}
+4 -2
View File
@@ -11,7 +11,8 @@
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
#include <map> #include <map>
namespace Copium
{
class Pipeline class Pipeline
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(Pipeline); CP_DELETE_COPY_AND_MOVE_CTOR(Pipeline);
@@ -106,7 +107,7 @@ private:
void InitializePipeline(const PipelineCreator& creator) void InitializePipeline(const PipelineCreator& creator)
{ {
Shader shader{instance, ShaderType::GlslFile, creator.vertexShader, creator.fragmentShader}; Shader shader{instance, Shader::Type::GlslFile, creator.vertexShader, creator.fragmentShader};
VkPipelineVertexInputStateCreateInfo vertexInputCreateInfo{}; VkPipelineVertexInputStateCreateInfo vertexInputCreateInfo{};
vertexInputCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; vertexInputCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
@@ -254,3 +255,4 @@ private:
} }
}; };
}
+3
View File
@@ -6,6 +6,8 @@
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
#include <map> #include <map>
namespace Copium
{
class PipelineCreator class PipelineCreator
{ {
struct DescriptorSetBinding struct DescriptorSetBinding
@@ -58,3 +60,4 @@ public:
frontFace = cullFrontFace; frontFace = cullFrontFace;
} }
}; };
}
+3
View File
@@ -4,6 +4,8 @@
#include <optional> #include <optional>
#include <vector> #include <vector>
namespace Copium
{
struct QueueFamiliesQuery struct QueueFamiliesQuery
{ {
std::optional<uint32_t> graphicsFamily; std::optional<uint32_t> graphicsFamily;
@@ -39,3 +41,4 @@ struct QueueFamiliesQuery
return graphicsFamily.has_value() && presentFamily.has_value(); return graphicsFamily.has_value() && presentFamily.has_value();
} }
}; };
}
+19 -15
View File
@@ -6,14 +6,17 @@
#include "Common.h" #include "Common.h"
#include "Instance.h" #include "Instance.h"
enum class ShaderType namespace Copium
{
class Shader
{
CP_DELETE_COPY_AND_MOVE_CTOR(Shader);
public:
enum class Type
{ {
GlslFile, GlslCode, SpvFile, SpvCode GlslFile, GlslCode, SpvFile, SpvCode
}; };
class Shader
{
CP_DELETE_COPY_AND_MOVE_CTOR(Shader);
private: private:
Instance& instance; Instance& instance;
@@ -21,29 +24,29 @@ private:
VkShaderModule fragShaderModule; VkShaderModule fragShaderModule;
std::vector<VkPipelineShaderStageCreateInfo> shaderStages; std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
public: public:
Shader(Instance& instance, ShaderType shaderType, const std::string& vertexInput, const std::string& fragmentInput) Shader(Instance& instance, Type type, const std::string& vertexInput, const std::string& fragmentInput)
: instance{instance} : instance{instance}
{ {
switch (shaderType) switch (type)
{ {
case ShaderType::GlslCode: case Type::GlslCode:
vertShaderModule = InitializeShaderModuleFromGlslCode(vertexInput, shaderc_vertex_shader); vertShaderModule = InitializeShaderModuleFromGlslCode(vertexInput, shaderc_vertex_shader);
fragShaderModule = InitializeShaderModuleFromGlslCode(fragmentInput, shaderc_fragment_shader); fragShaderModule = InitializeShaderModuleFromGlslCode(fragmentInput, shaderc_fragment_shader);
break; break;
case ShaderType::GlslFile: case Type::GlslFile:
vertShaderModule = InitializeShaderModuleFromGlslFile(vertexInput, shaderc_vertex_shader); vertShaderModule = InitializeShaderModuleFromGlslFile(vertexInput, shaderc_vertex_shader);
fragShaderModule = InitializeShaderModuleFromGlslFile(fragmentInput, shaderc_fragment_shader); fragShaderModule = InitializeShaderModuleFromGlslFile(fragmentInput, shaderc_fragment_shader);
break; break;
case ShaderType::SpvCode: case Type::SpvCode:
vertShaderModule = InitializeShaderModule(vertexInput); vertShaderModule = InitializeShaderModule(vertexInput);
fragShaderModule = InitializeShaderModule(fragmentInput); fragShaderModule = InitializeShaderModule(fragmentInput);
break; break;
case ShaderType::SpvFile: case Type::SpvFile:
vertShaderModule = InitializeShaderModule(FileSystem::ReadFile(vertexInput)); vertShaderModule = InitializeShaderModule(FileSystem::ReadFile(vertexInput));
fragShaderModule = InitializeShaderModule(FileSystem::ReadFile(fragmentInput)); fragShaderModule = InitializeShaderModule(FileSystem::ReadFile(fragmentInput));
break; break;
default: default:
CP_ASSERT(false, "Unreachable switch case %d", (int)shaderType); CP_ASSERT(false, "Unreachable switch case %d", (int)type);
} }
shaderStages.resize(2); shaderStages.resize(2);
@@ -87,7 +90,7 @@ private:
return InitializeShaderModule(reinterpret_cast<const uint32_t*>(codeSpv.data()), codeSpv.size()); return InitializeShaderModule(reinterpret_cast<const uint32_t*>(codeSpv.data()), codeSpv.size());
} }
VkShaderModule InitializeShaderModuleFromGlslFile(const std::string& filename, shaderc_shader_kind shaderType) VkShaderModule InitializeShaderModuleFromGlslFile(const std::string& filename, shaderc_shader_kind type)
{ {
std::string spvFilename = ".cache/" + filename + ".spv"; std::string spvFilename = ".cache/" + filename + ".spv";
try try
@@ -114,7 +117,7 @@ private:
options.SetOptimizationLevel(shaderc_optimization_level_size); options.SetOptimizationLevel(shaderc_optimization_level_size);
std::vector<char> glslCode = FileSystem::ReadFile(filename); std::vector<char> glslCode = FileSystem::ReadFile(filename);
shaderc::SpvCompilationResult result = compiler.CompileGlslToSpv(glslCode.data(), glslCode.size(), shaderType, filename.c_str(), options); shaderc::SpvCompilationResult result = compiler.CompileGlslToSpv(glslCode.data(), glslCode.size(), type, filename.c_str(), options);
CP_ASSERT(result.GetCompilationStatus() == shaderc_compilation_status_success, "Failed to compile shader: %s\n%s", filename.c_str(), result.GetErrorMessage().c_str()); CP_ASSERT(result.GetCompilationStatus() == shaderc_compilation_status_success, "Failed to compile shader: %s\n%s", filename.c_str(), result.GetErrorMessage().c_str());
std::vector<uint32_t> data{result.cbegin(), result.cend()}; std::vector<uint32_t> data{result.cbegin(), result.cend()};
@@ -122,14 +125,14 @@ private:
return InitializeShaderModule(data.data(), data.size() * sizeof(uint32_t)); return InitializeShaderModule(data.data(), data.size() * sizeof(uint32_t));
} }
VkShaderModule InitializeShaderModuleFromGlslCode(const std::string& code, shaderc_shader_kind shaderType) VkShaderModule InitializeShaderModuleFromGlslCode(const std::string& code, shaderc_shader_kind type)
{ {
shaderc::Compiler compiler; shaderc::Compiler compiler;
shaderc::CompileOptions options; shaderc::CompileOptions options;
options.SetOptimizationLevel(shaderc_optimization_level_size); options.SetOptimizationLevel(shaderc_optimization_level_size);
shaderc::SpvCompilationResult result = compiler.CompileGlslToSpv(code.data(), shaderType, "inline_shader_code", options); shaderc::SpvCompilationResult result = compiler.CompileGlslToSpv(code.data(), type, "inline_shader_code", options);
CP_ASSERT(result.GetCompilationStatus() == shaderc_compilation_status_success, "Failed to compile inline shader code: %s", result.GetErrorMessage()); CP_ASSERT(result.GetCompilationStatus() == shaderc_compilation_status_success, "Failed to compile inline shader code: %s", result.GetErrorMessage());
std::vector<uint32_t> data{result.cbegin(), result.cend()}; std::vector<uint32_t> data{result.cbegin(), result.cend()};
@@ -149,3 +152,4 @@ private:
return shaderModule; return shaderModule;
} }
}; };
}
+3
View File
@@ -10,6 +10,8 @@
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
#include <vector> #include <vector>
namespace Copium
{
SwapChainSupportDetails::SwapChainSupportDetails(VkSurfaceKHR surface, VkPhysicalDevice physicalDevice) SwapChainSupportDetails::SwapChainSupportDetails(VkSurfaceKHR surface, VkPhysicalDevice physicalDevice)
{ {
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &capabilities); vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, surface, &capabilities);
@@ -340,3 +342,4 @@ VkExtent2D SwapChain::SelectSwapExtent(GLFWwindow* window, const VkSurfaceCapabi
extent.height = std::clamp(extent.height, capabilities.minImageExtent.height, capabilities.maxImageExtent.height); extent.height = std::clamp(extent.height, capabilities.minImageExtent.height, capabilities.maxImageExtent.height);
return extent; return extent;
} }
}
+3
View File
@@ -6,6 +6,8 @@
#include <vector> #include <vector>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
namespace Copium
{
class Instance; class Instance;
class CommandBuffer; class CommandBuffer;
class Texture2D; class Texture2D;
@@ -63,3 +65,4 @@ private:
VkPresentModeKHR SelectSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes); VkPresentModeKHR SelectSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes);
VkExtent2D SelectSwapExtent(GLFWwindow* window, const VkSurfaceCapabilitiesKHR& capabilities); VkExtent2D SelectSwapExtent(GLFWwindow* window, const VkSurfaceCapabilitiesKHR& capabilities);
}; };
}
+3
View File
@@ -3,6 +3,8 @@
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h> #include <stb/stb_image.h>
namespace Copium
{
Texture2D::Texture2D(Instance& instance, const std::string& filename) Texture2D::Texture2D(Instance& instance, const std::string& filename)
: instance{instance}, type{Type::Static}, format{Format::Image} : instance{instance}, type{Type::Static}, format{Format::Image}
{ {
@@ -148,3 +150,4 @@ void Texture2D::InitializeSampler()
CP_VK_ASSERT(vkCreateSampler(instance.GetDevice(), &createInfo, nullptr, &sampler), "InitializeSampler : Failed to initialize texture sampler"); CP_VK_ASSERT(vkCreateSampler(instance.GetDevice(), &createInfo, nullptr, &sampler), "InitializeSampler : Failed to initialize texture sampler");
} }
}
+3 -1
View File
@@ -6,6 +6,8 @@
#include "Image.h" #include "Image.h"
#include "Instance.h" #include "Instance.h"
namespace Copium
{
// TODO: Separate Texture2D and Framebuffer Attachments // TODO: Separate Texture2D and Framebuffer Attachments
class Texture2D class Texture2D
{ {
@@ -42,4 +44,4 @@ private:
void InitializeTexture(int width, int height); void InitializeTexture(int width, int height);
void InitializeSampler(); void InitializeSampler();
}; };
}
+3
View File
@@ -2,6 +2,8 @@
#include <chrono> #include <chrono>
namespace Copium
{
class Timer class Timer
{ {
private: private:
@@ -21,3 +23,4 @@ public:
return std::chrono::duration<double>(std::chrono::high_resolution_clock::now() - startTime).count(); return std::chrono::duration<double>(std::chrono::high_resolution_clock::now() - startTime).count();
} }
}; };
}
+3
View File
@@ -4,6 +4,8 @@
#include "Buffer.h" #include "Buffer.h"
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
namespace Copium
{
class UniformBuffer : public Buffer class UniformBuffer : public Buffer
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(UniformBuffer); CP_DELETE_COPY_AND_MOVE_CTOR(UniformBuffer);
@@ -29,3 +31,4 @@ public:
return bufferInfo; return bufferInfo;
} }
}; };
}
+3
View File
@@ -4,6 +4,8 @@
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
#include "VertexDescriptor.h" #include "VertexDescriptor.h"
namespace Copium
{
struct Vertex { struct Vertex {
glm::vec3 pos; glm::vec3 pos;
glm::vec3 color; glm::vec3 color;
@@ -18,4 +20,5 @@ struct Vertex {
return descriptor; return descriptor;
} }
}; };
}
+3
View File
@@ -3,6 +3,8 @@
#include "Buffer.h" #include "Buffer.h"
#include "VertexDescriptor.h" #include "VertexDescriptor.h"
namespace Copium
{
class VertexBuffer : public Buffer class VertexBuffer : public Buffer
{ {
CP_DELETE_COPY_AND_MOVE_CTOR(VertexBuffer); CP_DELETE_COPY_AND_MOVE_CTOR(VertexBuffer);
@@ -33,3 +35,4 @@ public:
UpdateStaging(data, bindingOffsets[binding], bindingSizes[binding]); UpdateStaging(data, bindingOffsets[binding], bindingSizes[binding]);
} }
}; };
}
+3 -1
View File
@@ -2,7 +2,8 @@
#include <map> #include <map>
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
namespace Copium
{
class VertexDescriptor class VertexDescriptor
{ {
private: private:
@@ -58,3 +59,4 @@ private:
return description.binding; return description.binding;
} }
}; };
}
+3
View File
@@ -4,6 +4,8 @@
#include <vulkan/vulkan.hpp> #include <vulkan/vulkan.hpp>
#include "VertexDescriptor.h" #include "VertexDescriptor.h"
namespace Copium
{
struct VertexPassthrough { struct VertexPassthrough {
glm::vec2 texCoord; glm::vec2 texCoord;
@@ -14,3 +16,4 @@ struct VertexPassthrough {
return descriptor; return descriptor;
} }
}; };
}
+3
View File
@@ -2,6 +2,8 @@
#include <stdexcept> #include <stdexcept>
namespace Copium
{
class VulkanException : public std::runtime_error class VulkanException : public std::runtime_error
{ {
public: public:
@@ -9,3 +11,4 @@ public:
: runtime_error{str.c_str()} : runtime_error{str.c_str()}
{} {}
}; };
}
-6
View File
@@ -1,6 +0,0 @@
#pragma once
class Window
{
};
+4 -239
View File
@@ -1,250 +1,15 @@
#include "Buffer.h" #include "Application.h"
#include "DescriptorPool.h" #include "Common.h"
#include "DescriptorSet.h"
#include "Framebuffer.h"
#include "IndexBuffer.h"
#include "Instance.h"
#include "Pipeline.h"
#include "Texture2D.h"
#include "Timer.h" #include "Timer.h"
#include "UniformBuffer.h"
#include "Vertex.h"
#include "VertexBuffer.h"
#include "VertexPassthrough.h"
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <chrono>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
#include <optional>
#include <set>
#include <vector>
const std::vector<Vertex> vertices = {
Vertex{{-0.5f, 0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}},
Vertex{{ 0.5f, 0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f}},
Vertex{{ 0.5f, 0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}},
Vertex{{-0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
Vertex{{-0.5f, 0.0f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}},
Vertex{{ 0.5f, 0.0f, -0.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 0.0f}},
Vertex{{ 0.5f, 0.0f, 0.5f}, {0.0f, 0.0f, 1.0f}, {1.0f, 1.0f}},
Vertex{{-0.5f, 0.0f, 0.5f}, {1.0f, 1.0f, 1.0f}, {0.0f, 1.0f}},
};
const std::vector<uint16_t> indices = {
0, 1, 2, 2, 3, 0,
4, 5, 6, 6, 7, 4
};
const std::vector<VertexPassthrough> verticesPassthrough = {
VertexPassthrough{{-1.0f, -1.0f}},
VertexPassthrough{{ 1.0f, -1.0f}},
VertexPassthrough{{ 1.0f, 1.0f}},
VertexPassthrough{{-1.0f, 1.0f}},
};
const std::vector<uint16_t> indicesPassthrough = {
0, 1, 2, 2, 3, 0,
};
struct alignas(64) ShaderUniform
{
alignas(16) glm::mat4 projection;
alignas(16) glm::mat4 view;
alignas(16) glm::mat4 model;
alignas(16) glm::vec3 lightPos;
};
class Application final
{
private:
std::unique_ptr<Instance> instance;
std::unique_ptr<Pipeline> graphicsPipeline;
std::unique_ptr<Texture2D> texture2D;
std::unique_ptr<UniformBuffer> shaderUniformBuffer;
std::unique_ptr<DescriptorPool> descriptorPool;
std::unique_ptr<DescriptorSet> descriptorSet;
std::unique_ptr<VertexBuffer> vertexBuffer;
std::unique_ptr<IndexBuffer> indexBuffer;
std::unique_ptr<CommandBuffer> commandBuffer;
std::unique_ptr<Framebuffer> framebuffer;
std::unique_ptr<Pipeline> graphicsPipelinePassthrough;
std::unique_ptr<VertexBuffer> vertexBufferPassthrough;
std::unique_ptr<IndexBuffer> indexBufferPassthrough;
std::unique_ptr<DescriptorSet> descriptorSetPassthrough;
public:
Application()
{
InitializeInstance();
InitializeFrameBuffer();
InitializeGraphicsPipeline();
InitializeTextureSampler();
InitializeUniformBuffer();
InitializeDescriptorSets();
InitializeVertexBuffer();
InitializeIndexBuffer();
InitializeCommandBuffer();
}
~Application()
{
vkDeviceWaitIdle(instance->GetDevice());
}
Application(Application&&) = delete;
Application(const Application&) = delete;
Application& operator=(Application&&) = delete;
Application& operator=(const Application&) = delete;
bool Update()
{
if (!instance->BeginPresent())
return true;
RecordCommandBuffer();
commandBuffer->SubmitAsGraphicsQueue();
return instance->EndPresent();
}
private:
void InitializeInstance()
{
instance = std::make_unique<Instance>("Copium Engine");
}
void InitializeFrameBuffer()
{
framebuffer = std::make_unique<Framebuffer>(*instance, instance->GetSwapChain().GetExtent().width, instance->GetSwapChain().GetExtent().height);
}
void InitializeTextureSampler()
{
texture2D = std::make_unique<Texture2D>(*instance, "res/textures/texture.png");
}
void InitializeUniformBuffer()
{
shaderUniformBuffer = std::make_unique<UniformBuffer>(*instance, sizeof(ShaderUniform));
}
void InitializeDescriptorSets()
{
descriptorPool = std::make_unique<DescriptorPool>(*instance);
descriptorSet = std::make_unique<DescriptorSet>(*instance, *descriptorPool, graphicsPipeline->GetDescriptorSetLayout(0));
descriptorSet->AddUniform(*shaderUniformBuffer, 0);
descriptorSet->AddTexture2D(*texture2D, 1);
descriptorSetPassthrough = std::make_unique<DescriptorSet>(*instance, *descriptorPool, graphicsPipelinePassthrough->GetDescriptorSetLayout(0));
descriptorSetPassthrough->AddTexture2D(framebuffer->GetTexture2D(), 0);
}
void InitializeGraphicsPipeline()
{
PipelineCreator creator{framebuffer->GetRenderPass(), "res/shaders/shader.vert", "res/shaders/shader.frag"};
creator.AddDescriptorSetLayoutBinding(0, 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT);
creator.AddDescriptorSetLayoutBinding(0, 1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT);
creator.SetVertexDescriptor(Vertex::GetDescriptor());
creator.SetCullMode(VK_CULL_MODE_NONE);
graphicsPipeline = std::make_unique<Pipeline>(*instance, creator);
PipelineCreator creatorPassthrough{instance->GetSwapChain().GetRenderPass(), "res/shaders/passthrough.vert", "res/shaders/passthrough.frag"};
creatorPassthrough.AddDescriptorSetLayoutBinding(0, 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT);
creatorPassthrough.SetVertexDescriptor(VertexPassthrough::GetDescriptor());
creatorPassthrough.SetCullMode(VK_CULL_MODE_NONE);
graphicsPipelinePassthrough = std::make_unique<Pipeline>(*instance, creatorPassthrough);
}
void InitializeVertexBuffer()
{
vertexBuffer = std::make_unique<VertexBuffer>(*instance, Vertex::GetDescriptor(), vertices.size());
vertexBuffer->Update(0, (void*)vertices.data());
vertexBufferPassthrough = std::make_unique<VertexBuffer>(*instance, VertexPassthrough::GetDescriptor(), verticesPassthrough.size());
vertexBufferPassthrough->Update(0, (void*)verticesPassthrough.data());
}
void InitializeIndexBuffer()
{
indexBuffer = std::make_unique<IndexBuffer>(*instance, indices.size());
indexBuffer->UpdateStaging((void*)indices.data());
indexBufferPassthrough = std::make_unique<IndexBuffer>(*instance, indicesPassthrough.size());
indexBufferPassthrough->UpdateStaging((void*)indicesPassthrough.data());
}
void InitializeCommandBuffer()
{
commandBuffer = std::make_unique<CommandBuffer>(*instance, CommandBufferType::Dynamic);
}
void RecordCommandBuffer()
{
commandBuffer->Begin();
std::vector<VkClearValue> clearValues{2};
clearValues[0].color = {{0.0f, 0.0f, 0.0f, 1.0f}};
clearValues[1].depthStencil = {1.0f, 0};
framebuffer->Bind(*commandBuffer);
graphicsPipeline->Bind(*commandBuffer);
UpdateUniformBuffer();
vertexBuffer->Bind(*commandBuffer);
indexBuffer->Bind(*commandBuffer);
graphicsPipeline->SetDescriptorSet(0, *descriptorSet);
graphicsPipeline->BindDescriptorSets(commandBuffer->GetHandle());
indexBuffer->Draw(*commandBuffer);
framebuffer->Unbind(*commandBuffer);
instance->GetSwapChain().BeginFrameBuffer(*commandBuffer);
graphicsPipelinePassthrough->Bind(*commandBuffer);
graphicsPipelinePassthrough->SetDescriptorSet(0, *descriptorSetPassthrough);
graphicsPipelinePassthrough->BindDescriptorSets(commandBuffer->GetHandle());
vertexBufferPassthrough->Bind(*commandBuffer);
indexBufferPassthrough->Bind(*commandBuffer);
indexBufferPassthrough->Draw(*commandBuffer);
instance->GetSwapChain().EndFrameBuffer(*commandBuffer);
commandBuffer->End();
}
void UpdateUniformBuffer()
{
static Timer startTimer;
float time = startTimer.Elapsed();
ShaderUniform shaderUniform;
shaderUniform.view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
shaderUniform.projection = glm::perspective(glm::radians(45.0f), instance->GetSwapChain().GetExtent().width / (float)instance->GetSwapChain().GetExtent().height, 0.1f, 10.0f);
shaderUniform.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 1.0f, 0.0f));
shaderUniform.projection[1][1] *= -1;
shaderUniform.lightPos = glm::rotate(glm::mat4{1.0f}, time * glm::radians(45.0f), glm::vec3(0, 1, 0)) * glm::vec4{0.3, 0.1, 0, 1};
shaderUniformBuffer->Update(shaderUniform);
}
};
void func(const int* ptr) {
*const_cast<int*>(ptr) = 20;
}
int main() int main()
{ {
CP_ASSERT(glfwInit() == GLFW_TRUE, "main : Failed to initialize the glfw context"); CP_ASSERT(glfwInit() == GLFW_TRUE, "main : Failed to initialize the glfw context");
{ {
Application application; Copium::Application application;
Timer timer; Copium::Timer timer;
int frames = 0; int frames = 0;
while (application.Update()) while (application.Update())
{ {