From 9493ee06fc73de3c8c8115db67078d9aa62069e1 Mon Sep 17 00:00:00 2001 From: Dan Liebault Date: Thu, 6 May 2021 23:40:48 +0200 Subject: [PATCH 01/11] dynamic texture binding --- Engine/Source.cpp | 21 +-------------- Engine/assets/shaders/frag.spv | Bin 692 -> 536 bytes Engine/assets/shaders/shader.frag | 7 ++--- Engine/assets/shaders/shader_compiler.bat | 4 +-- Engine/assets/shaders/vert.spv | Bin 1804 -> 1804 bytes Engine/renderer/Renderer.cpp | 30 ++++++++++++++++++---- Engine/renderer/Renderer.h | 1 + Engine/world/Material.cpp | 28 +++++++++++++++++--- Engine/world/Material.h | 10 +++++++- Engine/world/Mesh.cpp | 17 +++++++++--- Engine/world/Mesh.h | 1 + Engine/world/Texture.cpp | 6 ----- Engine/world/Texture.h | 3 --- 13 files changed, 82 insertions(+), 46 deletions(-) diff --git a/Engine/Source.cpp b/Engine/Source.cpp index 2db8ede..cd72e7f 100644 --- a/Engine/Source.cpp +++ b/Engine/Source.cpp @@ -8,18 +8,13 @@ int main() Logger::init(); std::shared_ptr room_texture = std::make_shared(); - room_texture->loadTexture("assets/textures/viking_room.png", engine.getRenderEngine()); + //room_texture->loadTexture("assets/textures/viking_room.png", engine.getRenderEngine()); std::shared_ptr room = std::make_shared(engine.getRenderEngine()); room->loadMesh("assets/models/viking_room.obj"); room->bindMatToMesh(0, room_texture); room->setPosition({ 3.0f, 0.0f, 0.0f }); - std::shared_ptr room2_texture = std::make_shared(); - room2_texture->loadTexture("assets/textures/viking_room.png", engine.getRenderEngine()); - - std::shared_ptr room2 = std::make_shared(engine.getRenderEngine()); - std::shared_ptr scene = std::make_shared(); scene->addGameObject(room); @@ -29,20 +24,6 @@ int main() // running loop do { - if (init == 30000) { - room2->loadMesh("assets/models/viking_room.obj"); - room2->bindMatToMesh(0, room2_texture); - room2->setPosition({ 13.0f, 0.0f, 0.0f }); - - scene->addGameObject(room2); - } - - init++; - - if (init == 60000) { - scene->removeGameObject(room2); - } - engine.update(); engine.draw(); } while (!engine.shouldClose()); diff --git a/Engine/assets/shaders/frag.spv b/Engine/assets/shaders/frag.spv index eac4c9dd34e7015a7260fff49584bf25b42a7c6b..0e7d70f131c8a07a5be16e4e79509063939ab125 100644 GIT binary patch delta 249 zcmdnOI)jCmnMs+Qfq{{Mi-Chdcp|TvYg zL5czySiuVTp<+M^E+zmK0~roeCkPc|0@48t>_EuCAOyr9{UG%qd5~pxK<9xx0FwI) Jbe;te0|4dQ5CZ@J literal 692 zcmYk2%}WAN6vc1qsAc()r9==~dvQ?_L_xWjjjIe=4pY9q&_-q^@bBxdY7=yRlS4kZ zbLX6Q-nk#e!b!z!!zxy_uKk^s6~vg;tej@ijJZwEq5xSzoMMYzVxspl|K${?rwv`Ey6fVU68znQx^x(OaTf%e`BUq9~r`T6%)d4g9PVvm*pLVEm3V z^pE?(%*#FA=yQx`{=TgVnX#WuYuSHC|7CGKMOW|H1(?}-`c{?MDE#FLGbh}ECf-s; zPrdb%<3lNHx?kcGFAQd}kE6~7S%Yj|M{iwo);up)}FC72? diff --git a/Engine/assets/shaders/shader.frag b/Engine/assets/shaders/shader.frag index ffaea9d..082910e 100644 --- a/Engine/assets/shaders/shader.frag +++ b/Engine/assets/shaders/shader.frag @@ -1,13 +1,14 @@ #version 450 #extension GL_ARB_separate_shader_objects : enable -layout(binding = 1) uniform sampler2D texSampler; +//layout(binding = 1) uniform sampler2D texSampler; layout(location = 0) in vec3 fragColor; -layout(location = 1) in vec2 fragTexCoord; +//layout(location = 1) in vec2 fragTexCoord; layout(location = 0) out vec4 outColor; void main() { - outColor = texture(texSampler, fragTexCoord); + outColor = vec4(fragColor, 1.0f); + //outColor = texture(texSampler, fragTexCoord); } \ No newline at end of file diff --git a/Engine/assets/shaders/shader_compiler.bat b/Engine/assets/shaders/shader_compiler.bat index 75b899c..6ed8887 100644 --- a/Engine/assets/shaders/shader_compiler.bat +++ b/Engine/assets/shaders/shader_compiler.bat @@ -1,3 +1,3 @@ -C:\VulkanSDK\1.1.82.1\Bin32\glslangValidator.exe -V shader.vert -C:\VulkanSDK\1.1.82.1\Bin32\glslangValidator.exe -V shader.frag +F:\Programs\VulkanSDK\1.2.170.0\Bin32\glslangValidator.exe -V shader.vert +F:\Programs\VulkanSDK\1.2.170.0\Bin32\glslangValidator.exe -V shader.frag pause \ No newline at end of file diff --git a/Engine/assets/shaders/vert.spv b/Engine/assets/shaders/vert.spv index 8b4708a0939a98c98c3cfeb07b537d38fa4e0b9a..5edc67928f992a671804fc4e38870d1976a52aa6 100644 GIT binary patch delta 18 ZcmeC->*3>MW>RKnU|?k6+Q`Yw4geYx0z3c! delta 18 ZcmeC->*3>MW>RKnU|?im-^j_#4geYi0yzKx diff --git a/Engine/renderer/Renderer.cpp b/Engine/renderer/Renderer.cpp index 6111d76..4db8bc5 100644 --- a/Engine/renderer/Renderer.cpp +++ b/Engine/renderer/Renderer.cpp @@ -492,20 +492,35 @@ void Renderer::allocateDescriptorSet(VkDescriptorSet& descriptor_set) } } -void Renderer::updateDescriptorSet(const VkBuffer& ubo, const VkDescriptorSet& descriptor_set, const VkImageView& image_view, const VkSampler& image_sampler) +void Renderer::updateDescriptorSet(const VkBuffer& ubo, const VkDescriptorSet& descriptor_set) { + std::array descriptorWrites = {}; + VkDescriptorBufferInfo bufferInfo = {}; bufferInfo.buffer = ubo; bufferInfo.offset = 0; bufferInfo.range = sizeof(UniformBufferObject); - VkDescriptorImageInfo imageInfo = {}; - imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; - imageInfo.imageView = image_view; - imageInfo.sampler = image_sampler; + descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; + descriptorWrites[0].dstSet = descriptor_set; + descriptorWrites[0].dstBinding = 0; + descriptorWrites[0].dstArrayElement = 0; + descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; + descriptorWrites[0].descriptorCount = 1; + descriptorWrites[0].pBufferInfo = &bufferInfo; + vkUpdateDescriptorSets(m_graphic.device, static_cast(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr); +} +void Renderer::updateDescriptorSet(const VkBuffer& ubo, const VkDescriptorSet& descriptor_set, const VkImageView& image_view, const VkSampler& image_sampler) +{ std::array descriptorWrites = {}; + + VkDescriptorBufferInfo bufferInfo = {}; + bufferInfo.buffer = ubo; + bufferInfo.offset = 0; + bufferInfo.range = sizeof(UniformBufferObject); + descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; descriptorWrites[0].dstSet = descriptor_set; descriptorWrites[0].dstBinding = 0; @@ -514,6 +529,11 @@ void Renderer::updateDescriptorSet(const VkBuffer& ubo, const VkDescriptorSet& d descriptorWrites[0].descriptorCount = 1; descriptorWrites[0].pBufferInfo = &bufferInfo; + VkDescriptorImageInfo imageInfo = {}; + imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; + imageInfo.imageView = image_view; + imageInfo.sampler = image_sampler; + descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; descriptorWrites[1].dstSet = descriptor_set; descriptorWrites[1].dstBinding = 1; diff --git a/Engine/renderer/Renderer.h b/Engine/renderer/Renderer.h index 6c89ebd..e894492 100644 --- a/Engine/renderer/Renderer.h +++ b/Engine/renderer/Renderer.h @@ -74,6 +74,7 @@ class Renderer void createDescriptorLayout(); void createDescriptorPool(); void allocateDescriptorSet(VkDescriptorSet& descriptor_set); + void updateDescriptorSet(const VkBuffer& ubo, const VkDescriptorSet& descriptor_set); void updateDescriptorSet(const VkBuffer& ubo, const VkDescriptorSet& descriptor_set, const VkImageView& image_view, const VkSampler& image_sampler); void createDepthResources(); diff --git a/Engine/world/Material.cpp b/Engine/world/Material.cpp index 1b727e0..0177da5 100644 --- a/Engine/world/Material.cpp +++ b/Engine/world/Material.cpp @@ -2,6 +2,7 @@ Material::Material() { + m_descriptor_set = VK_NULL_HANDLE; } Material::~Material() @@ -9,6 +10,17 @@ Material::~Material() m_texture.reset(); } +void Material::setColor(glm::vec3 color) +{ + m_color = std::make_shared(color); +} + +void Material::clearColor() +{ + m_color.reset(); + m_color = nullptr; +} + void Material::loadTexture(const std::string& texture_path, std::shared_ptr renderer) { m_texture = std::make_shared(texture_path, renderer); @@ -17,13 +29,23 @@ void Material::loadTexture(const std::string& texture_path, std::shared_ptrcreateTextureSampler(); } +void Material::destroyTexture() +{ + m_texture.reset(); + m_texture = nullptr; +} + +std::shared_ptr Material::getColor() +{ + return std::shared_ptr(); +} + std::shared_ptr Material::getTexture() { return m_texture; } -void Material::destroyTexture() +VkDescriptorSet& Material::getDescriptorSet() { - m_texture.reset(); - m_texture = nullptr; + return m_descriptor_set; } diff --git a/Engine/world/Material.h b/Engine/world/Material.h index 72e6c70..d7e5785 100644 --- a/Engine/world/Material.h +++ b/Engine/world/Material.h @@ -13,12 +13,20 @@ class Material Material(); ~Material(); + void setColor(glm::vec3 color); + void clearColor(); + void loadTexture(const std::string& texture_path, std::shared_ptr renderer); - std::shared_ptr getTexture(); void destroyTexture(); + std::shared_ptr getColor(); + std::shared_ptr getTexture(); + VkDescriptorSet& getDescriptorSet(); private: std::shared_ptr m_texture; + std::shared_ptr m_color; + + VkDescriptorSet m_descriptor_set; }; #endif // !_MATERIAL_H \ No newline at end of file diff --git a/Engine/world/Mesh.cpp b/Engine/world/Mesh.cpp index 4b7766e..8f58042 100644 --- a/Engine/world/Mesh.cpp +++ b/Engine/world/Mesh.cpp @@ -1,5 +1,8 @@ #include "Mesh.h" +Mesh::Mesh(std::shared_ptr p_renderer) : mp_renderer(p_renderer) +{ +} Mesh::Mesh(const std::string& obj_path, std::shared_ptr p_renderer) : m_obj_path(obj_path), mp_renderer(p_renderer) { @@ -18,7 +21,7 @@ void Mesh::draw(const VkCommandBuffer& command_buffer, const Pipeline& pipeline) vkCmdBindVertexBuffers(command_buffer, 0, 1, vertex_buffer, offsets); vkCmdBindIndexBuffer(command_buffer, m_buffer.index, 0, VK_INDEX_TYPE_UINT32); - vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.layout, 0, 1, &p_material->getTexture()->getDescriptorSet(), 0, nullptr); + vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.layout, 0, 1, &p_material->getDescriptorSet(), 0, nullptr); vkCmdDrawIndexed(command_buffer, static_cast(m_indices.size()), 1, 0, 0, 0); } @@ -67,8 +70,16 @@ void Mesh::loadModel() void Mesh::bindMaterial(std::shared_ptr mat, VkBuffer& ubo, std::shared_ptr renderer) { - renderer->allocateDescriptorSet(mat->getTexture()->getDescriptorSet()); - renderer->updateDescriptorSet(ubo, mat->getTexture()->getDescriptorSet(), mat->getTexture()->getImageView(), mat->getTexture()->getSampler()); + renderer->allocateDescriptorSet(mat->getDescriptorSet()); + + if (mat->getTexture() == nullptr) + { + renderer->updateDescriptorSet(ubo, mat->getDescriptorSet()); + } + else + { + renderer->updateDescriptorSet(ubo, mat->getDescriptorSet(), mat->getTexture()->getImageView(), mat->getTexture()->getSampler()); + } p_material = mat; } diff --git a/Engine/world/Mesh.h b/Engine/world/Mesh.h index c6f1f5b..c1a535a 100644 --- a/Engine/world/Mesh.h +++ b/Engine/world/Mesh.h @@ -17,6 +17,7 @@ class Mesh { public: + Mesh(std::shared_ptr p_renderer); Mesh(const std::string& obj_path, std::shared_ptr p_renderer); ~Mesh(); diff --git a/Engine/world/Texture.cpp b/Engine/world/Texture.cpp index 20793d0..6d29022 100644 --- a/Engine/world/Texture.cpp +++ b/Engine/world/Texture.cpp @@ -10,7 +10,6 @@ Texture::Texture(const std::string& texture_path, std::shared_ptr p_re m_texture_view = VK_NULL_HANDLE; m_texture_sampler = VK_NULL_HANDLE; m_texture_memory = VK_NULL_HANDLE; - m_descriptor_set = VK_NULL_HANDLE; } Texture::~Texture() @@ -100,8 +99,3 @@ const VkSampler& Texture::getSampler() { return m_texture_sampler; } - -VkDescriptorSet& Texture::getDescriptorSet() -{ - return m_descriptor_set; -} diff --git a/Engine/world/Texture.h b/Engine/world/Texture.h index a07a442..a1fbcd3 100644 --- a/Engine/world/Texture.h +++ b/Engine/world/Texture.h @@ -15,7 +15,6 @@ class Texture const VkImageView& getImageView(); const VkSampler& getSampler(); - VkDescriptorSet& getDescriptorSet(); private: const std::string m_texture_path; @@ -25,8 +24,6 @@ class Texture VkSampler m_texture_sampler; VkDeviceMemory m_texture_memory; - VkDescriptorSet m_descriptor_set; - std::shared_ptr mp_renderer; }; From 9dd104ba17af766ac55b45d97146ae6af7347b52 Mon Sep 17 00:00:00 2001 From: Dan Liebault Date: Fri, 7 May 2021 17:54:09 +0200 Subject: [PATCH 02/11] new shader for no texture material --- Engine/Source.cpp | 2 +- .../shaders/HLSL/no_texture_shader.frag | 10 ++++++++++ Engine/assets/shaders/{ => HLSL}/shader.vert | 0 .../assets/shaders/HLSL/shader_compiler.bat | 7 +++++++ .../{shader.frag => HLSL/texture_shader.frag} | 6 +++--- Engine/assets/shaders/no_texture_shader.spv | Bin 0 -> 608 bytes Engine/assets/shaders/shader_compiler.bat | 3 --- Engine/assets/shaders/texture_shader.spv | Bin 0 -> 896 bytes Engine/assets/shaders/vert.spv | Bin 1804 -> 1876 bytes Engine/renderer/VulkanPipeline.cpp | 2 +- 10 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 Engine/assets/shaders/HLSL/no_texture_shader.frag rename Engine/assets/shaders/{ => HLSL}/shader.vert (100%) create mode 100644 Engine/assets/shaders/HLSL/shader_compiler.bat rename Engine/assets/shaders/{shader.frag => HLSL/texture_shader.frag} (57%) create mode 100644 Engine/assets/shaders/no_texture_shader.spv delete mode 100644 Engine/assets/shaders/shader_compiler.bat create mode 100644 Engine/assets/shaders/texture_shader.spv diff --git a/Engine/Source.cpp b/Engine/Source.cpp index cd72e7f..243254e 100644 --- a/Engine/Source.cpp +++ b/Engine/Source.cpp @@ -8,7 +8,7 @@ int main() Logger::init(); std::shared_ptr room_texture = std::make_shared(); - //room_texture->loadTexture("assets/textures/viking_room.png", engine.getRenderEngine()); + room_texture->loadTexture("assets/textures/viking_room.png", engine.getRenderEngine()); std::shared_ptr room = std::make_shared(engine.getRenderEngine()); room->loadMesh("assets/models/viking_room.obj"); diff --git a/Engine/assets/shaders/HLSL/no_texture_shader.frag b/Engine/assets/shaders/HLSL/no_texture_shader.frag new file mode 100644 index 0000000..c33b655 --- /dev/null +++ b/Engine/assets/shaders/HLSL/no_texture_shader.frag @@ -0,0 +1,10 @@ +#version 450 +#extension GL_ARB_separate_shader_objects : enable + +layout(location = 0) in vec3 fragColor; + +layout(location = 0) out vec4 outColor; + +void main() { + outColor = vec4(fragColor, 1.0f); +} \ No newline at end of file diff --git a/Engine/assets/shaders/shader.vert b/Engine/assets/shaders/HLSL/shader.vert similarity index 100% rename from Engine/assets/shaders/shader.vert rename to Engine/assets/shaders/HLSL/shader.vert diff --git a/Engine/assets/shaders/HLSL/shader_compiler.bat b/Engine/assets/shaders/HLSL/shader_compiler.bat new file mode 100644 index 0000000..0e6f3f5 --- /dev/null +++ b/Engine/assets/shaders/HLSL/shader_compiler.bat @@ -0,0 +1,7 @@ +F:\Programs\VulkanSDK\1.2.170.0\Bin32\glslangValidator.exe -V shader.vert +F:\Programs\VulkanSDK\1.2.170.0\Bin32\glslangValidator.exe -V texture_shader.frag +F:\Programs\VulkanSDK\1.2.170.0\Bin32\glslangValidator.exe -V no_texture_shader.frag +pause +F:\Programs\VulkanSDK\1.2.170.0\Bin32\glslc.exe shader.vert -o vert.spv +F:\Programs\VulkanSDK\1.2.170.0\Bin32\glslc.exe texture_shader.frag -o texture_shader.spv +F:\Programs\VulkanSDK\1.2.170.0\Bin32\glslc.exe no_texture_shader.frag -o no_texture_shader.spv \ No newline at end of file diff --git a/Engine/assets/shaders/shader.frag b/Engine/assets/shaders/HLSL/texture_shader.frag similarity index 57% rename from Engine/assets/shaders/shader.frag rename to Engine/assets/shaders/HLSL/texture_shader.frag index 082910e..4b05b1c 100644 --- a/Engine/assets/shaders/shader.frag +++ b/Engine/assets/shaders/HLSL/texture_shader.frag @@ -1,14 +1,14 @@ #version 450 #extension GL_ARB_separate_shader_objects : enable -//layout(binding = 1) uniform sampler2D texSampler; +layout(binding = 1) uniform sampler2D texSampler; layout(location = 0) in vec3 fragColor; -//layout(location = 1) in vec2 fragTexCoord; +layout(location = 1) in vec2 fragTexCoord; layout(location = 0) out vec4 outColor; void main() { outColor = vec4(fragColor, 1.0f); - //outColor = texture(texSampler, fragTexCoord); + outColor = texture(texSampler, fragTexCoord); } \ No newline at end of file diff --git a/Engine/assets/shaders/no_texture_shader.spv b/Engine/assets/shaders/no_texture_shader.spv new file mode 100644 index 0000000000000000000000000000000000000000..cb13e606fc0041e24ff6a63c0ec7dcca466732aa GIT binary patch literal 608 zcmYk2-Acni5QWF4X=`f#Ea;t7ycCKTDuPr|k=z98&jTzmSv3-qkfefleKw!U8^Lqd zM6xiMowMKWoQc&s=!$HJBLkVr-i9y>RhEyZ#pOoG7cGcxpci- z6=azz%FkG|cT$BXyDr^Eah^9ANY~wFol{aae=CB0CHh!OT|D_gP z+~OR21h80hrn;VD8qTwCCKI#Y!M+1+hS&wqxmmOS4Bs9y{~Y!6s%8&rD+VX+_^KeZLGd|WVT`*+pt%5Z=1F%#%#@6RrG_4;GihugSYPvQFN_Q z3DG>Uwlx*;t0{_7sg30Rr1@RYu9wiY?@xjb%7^Ey+6;qs>kfQ z9A{~kt0ggB)$@-$`ne?G`pT8=<|4Y9r%p;wJ7F$#U){|(%kr4}zFuJN*8mxq9n>+k z_DIUegGX}q2&OK2@Rl~ku13|?tUC8~Zi(BK52R=?M;fM-e?3 z{hA_g!J`M$Gu*x~+OFK}@6k}xZRrF@zHUprqsq^eKu7)O3lSXk!OV=&&$c4U7BuD9T*xzuegIO?Bid+hJ2 socrj>vxm=mm3@-qP39WNnF&naJ92vdrXHyCQjP|Gc^~dpQ+<%%0l@@HU diff --git a/Engine/renderer/VulkanPipeline.cpp b/Engine/renderer/VulkanPipeline.cpp index c0cf8d1..249f7cd 100644 --- a/Engine/renderer/VulkanPipeline.cpp +++ b/Engine/renderer/VulkanPipeline.cpp @@ -17,7 +17,7 @@ void VulkanPipeline::createPipeline(Pipeline& pipeline) auto attributeDescriptions = Vertex::getAttributeDescriptions(); auto vertShaderCode = readFile("assets/shaders/vert.spv"); - auto fragShaderCode = readFile("assets/shaders/frag.spv"); + auto fragShaderCode = readFile("assets/shaders/texture_shader.spv"); VkShaderModule vertShaderModule = createShaderModule(vertShaderCode); VkShaderModule fragShaderModule = createShaderModule(fragShaderCode); From 15f1092e579b8fe4b53fb7522958daa8b774b4e8 Mon Sep 17 00:00:00 2001 From: Dan Liebault Date: Sat, 8 May 2021 15:52:58 +0200 Subject: [PATCH 03/11] pipelines are now abstract and multiples shaders can be used --- Engine/Engine.cpp | 10 +++--- Engine/Engine.h | 3 +- Engine/Engine.vcxproj | 6 ++-- Engine/Engine.vcxproj.filters | 10 ++++-- Engine/Source.cpp | 2 +- Engine/renderer/Renderer.cpp | 30 ++++++++++-------- Engine/renderer/Renderer.h | 11 ++++--- Engine/renderer/VulkanPipeline.cpp | 49 ++++++++++++++++++++++++++---- Engine/renderer/VulkanPipeline.h | 19 ++++++++++-- Engine/world/GameObject.cpp | 4 +-- Engine/world/GameObject.h | 2 +- Engine/world/Material.cpp | 9 ++++-- Engine/world/Material.h | 5 ++- Engine/world/Mesh.cpp | 6 ++-- Engine/world/Mesh.h | 2 +- Engine/world/Scene.cpp | 4 +-- Engine/world/Scene.h | 2 +- 17 files changed, 124 insertions(+), 50 deletions(-) diff --git a/Engine/Engine.cpp b/Engine/Engine.cpp index 8e7d7d1..843539b 100644 --- a/Engine/Engine.cpp +++ b/Engine/Engine.cpp @@ -12,8 +12,8 @@ Engine::Engine() mp_renderer = std::make_shared(mp_window->getHandle(), mp_config->width, mp_config->height); + // create pipelines - mp_renderer->createNewPipeline(base_pipeline); mp_renderer->createDepthResources(); mp_renderer->createFramebuffer(); @@ -25,7 +25,7 @@ Engine::~Engine() { vkDeviceWaitIdle(mp_renderer->getDevice()); - mp_renderer->cleanSwapchain(std::make_shared(base_pipeline)); + mp_renderer->cleanSwapchain(); for (size_t i = 0; i < mp_scene->getObjects().size(); i++) { @@ -83,9 +83,9 @@ void Engine::update() const int frame = mp_renderer->getFrameIndex(); if (mp_scene->isUpdate(frame)) { - mp_renderer->beginRecordCommandBuffers(mp_renderer->getCommandBuffer(frame), mp_renderer->getFrameBuffer(frame), base_pipeline); + mp_renderer->beginRecordCommandBuffers(mp_renderer->getCommandBuffer(frame), mp_renderer->getFrameBuffer(frame)); - mp_scene->render(base_pipeline, mp_renderer->getCommandBuffer(frame), frame); + mp_scene->render(mp_renderer->getCommandBuffer(frame), frame); mp_renderer->endRecordCommandBuffers(mp_renderer->getCommandBuffer(frame)); } @@ -98,5 +98,5 @@ void Engine::update() void Engine::draw() { - mp_renderer->draw(base_pipeline); + mp_renderer->draw(); } diff --git a/Engine/Engine.h b/Engine/Engine.h index ad3f4da..ecc69f3 100644 --- a/Engine/Engine.h +++ b/Engine/Engine.h @@ -32,8 +32,7 @@ class Engine std::shared_ptr mp_camera; std::shared_ptr mp_player; std::shared_ptr mp_config; - - Pipeline base_pipeline; + std::chrono::steady_clock::time_point m_last_time; }; diff --git a/Engine/Engine.vcxproj b/Engine/Engine.vcxproj index e383d5c..e00c1ad 100644 --- a/Engine/Engine.vcxproj +++ b/Engine/Engine.vcxproj @@ -93,8 +93,10 @@ - - + + + + 15.0 diff --git a/Engine/Engine.vcxproj.filters b/Engine/Engine.vcxproj.filters index 0353715..9c93542 100644 --- a/Engine/Engine.vcxproj.filters +++ b/Engine/Engine.vcxproj.filters @@ -175,10 +175,16 @@ - + Asset - + + Asset + + + Asset + + Asset diff --git a/Engine/Source.cpp b/Engine/Source.cpp index 243254e..9070e56 100644 --- a/Engine/Source.cpp +++ b/Engine/Source.cpp @@ -7,7 +7,7 @@ int main() Engine engine; Logger::init(); - std::shared_ptr room_texture = std::make_shared(); + std::shared_ptr room_texture = std::make_shared(SHADER::NONE, engine.getRenderEngine()); room_texture->loadTexture("assets/textures/viking_room.png", engine.getRenderEngine()); std::shared_ptr room = std::make_shared(engine.getRenderEngine()); diff --git a/Engine/renderer/Renderer.cpp b/Engine/renderer/Renderer.cpp index 4db8bc5..07c3354 100644 --- a/Engine/renderer/Renderer.cpp +++ b/Engine/renderer/Renderer.cpp @@ -17,7 +17,6 @@ Renderer::Renderer(GLFWwindow& window, uint32_t width, uint32_t height) WIDTH = std::make_unique(width); HEIGHT = std::make_unique(height); - m_pPipelineFactory = std::make_unique(m_graphic); m_pBufferFactory = std::make_unique(m_graphic); setupInstance(window); @@ -26,6 +25,8 @@ Renderer::Renderer(GLFWwindow& window, uint32_t width, uint32_t height) setupRenderPass(); setupDescriptorSetLayout(); setupCommandPool(); + + m_pPipelineFactory = std::make_unique(m_graphic); } @@ -51,7 +52,7 @@ Renderer::~Renderer() vkDestroyInstance(m_graphic.instance, nullptr); } -int32_t Renderer::draw(Pipeline& pipeline) +int32_t Renderer::draw() { vkWaitForFences(m_graphic.device, 1, &m_graphic.fences_in_flight[m_frame_index], VK_TRUE, std::numeric_limits::max()); vkResetFences(m_graphic.device, 1, &m_graphic.fences_in_flight[m_frame_index]); @@ -67,7 +68,7 @@ int32_t Renderer::draw(Pipeline& pipeline) if (result == VK_ERROR_OUT_OF_DATE_KHR) { - recreateSwapchain(pipeline); + recreateSwapchain(); return 1; } else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) @@ -118,7 +119,7 @@ int32_t Renderer::draw(Pipeline& pipeline) if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR) { - recreateSwapchain(pipeline); + recreateSwapchain(); return 1; } else if (result != VK_SUCCESS) @@ -165,7 +166,7 @@ void Renderer::setupCommandPool() m_commandPool = std::make_unique(m_graphic); } -void Renderer::createNewPipeline(Pipeline& pipeline) +void Renderer::createNewPipeline(std::shared_ptr pipeline) { m_pPipelineFactory->createPipeline(pipeline); } @@ -205,6 +206,11 @@ std::unique_ptr& Renderer::getBufferFactory() return m_pBufferFactory; } +std::unique_ptr& Renderer::GetPipelineFactory() +{ + return m_pPipelineFactory; +} + const int Renderer::getFrameIndex() { return m_frame_index; @@ -294,7 +300,7 @@ void Renderer::allocateCommandBuffers() } } -void Renderer::beginRecordCommandBuffers(VkCommandBuffer & commandBuffer, VkFramebuffer& frameBuffer, Pipeline & pipeline) +void Renderer::beginRecordCommandBuffers(VkCommandBuffer & commandBuffer, VkFramebuffer& frameBuffer) { std::array clear_values = {}; clear_values[0].color = { 0.0f, 0.0f, 0.0f, 1.0f }; @@ -322,7 +328,6 @@ void Renderer::beginRecordCommandBuffers(VkCommandBuffer & commandBuffer, VkFram renderPassInfo.pClearValues = clear_values.data(); m_pRenderpass->beginRenderPass(commandBuffer, renderPassInfo); - m_pPipelineFactory->bindPipeline(commandBuffer, pipeline); } void Renderer::endRecordCommandBuffers(VkCommandBuffer & commandBuffer) @@ -625,10 +630,10 @@ void Renderer::createImage(uint32_t width, uint32_t height, VkFormat format, VkI vkBindImageMemory(m_graphic.device, image, imageMemory, 0); } -void Renderer::recreateSwapchain(Pipeline& pipeline) +void Renderer::recreateSwapchain() { vkDeviceWaitIdle(m_graphic.device); - cleanSwapchain(std::make_shared(pipeline)); + cleanSwapchain(); m_swapchain->createSwapchain(); @@ -636,13 +641,13 @@ void Renderer::recreateSwapchain(Pipeline& pipeline) VkFormat format = findSupportedFormat({ VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT }, VK_IMAGE_TILING_OPTIMAL, VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT); m_pRenderpass->createRenderPass(format); - createNewPipeline(pipeline); + m_pPipelineFactory->CreatePipelines(); createDepthResources(); createFramebuffer(); allocateCommandBuffers(); } -void Renderer::cleanSwapchain(std::shared_ptr pPipeline) +void Renderer::cleanSwapchain() { vkDestroyImageView(m_graphic.device, m_graphic.depth_view, nullptr); vkDestroyImage(m_graphic.device, m_graphic.depth_image, nullptr); @@ -656,8 +661,7 @@ void Renderer::cleanSwapchain(std::shared_ptr pPipeline) vkFreeCommandBuffers(m_graphic.device, m_graphic.command_pool, static_cast(m_graphic.command_buffers.size()), m_graphic.command_buffers.data()); - vkDestroyPipeline(m_graphic.device, pPipeline->handle, nullptr); - vkDestroyPipelineLayout(m_graphic.device, pPipeline->layout, nullptr); + m_pPipelineFactory->DestroyPipelines(); vkDestroyRenderPass(m_graphic.device, m_graphic.render_pass, nullptr); diff --git a/Engine/renderer/Renderer.h b/Engine/renderer/Renderer.h index e894492..d2c68bf 100644 --- a/Engine/renderer/Renderer.h +++ b/Engine/renderer/Renderer.h @@ -35,7 +35,7 @@ class Renderer Renderer(GLFWwindow& window, uint32_t width, uint32_t height); ~Renderer(); - int32_t draw(Pipeline& pipeline); + int32_t draw(); void setupInstance(GLFWwindow& window); void setupDevice(); @@ -43,7 +43,7 @@ class Renderer void setupRenderPass(); void setupDescriptorSetLayout(); void setupCommandPool(); - void createNewPipeline(Pipeline& pipeline); + void createNewPipeline(std::shared_ptr pipeline); //getters VkDevice& getDevice(); @@ -53,6 +53,7 @@ class Renderer const size_t getNumberCommandBuffer(); VkFramebuffer& getFrameBuffer(const size_t& i); std::unique_ptr& getBufferFactory(); + std::unique_ptr& GetPipelineFactory(); const int getFrameIndex(); //rendering @@ -62,7 +63,7 @@ class Renderer void createUBO(VkBuffer& uniform_buffer, VkDeviceMemory& uniform_memory); void allocateCommandBuffers(); - void beginRecordCommandBuffers(VkCommandBuffer& commandBuffer, VkFramebuffer& frameBuffer, Pipeline& pipeline); + void beginRecordCommandBuffers(VkCommandBuffer& commandBuffer, VkFramebuffer& frameBuffer); void endRecordCommandBuffers(VkCommandBuffer& commandBuffer); //!rendering @@ -82,11 +83,11 @@ class Renderer //cleaning void destroyUniformBuffer(); void destroyBuffers(Buffer& buffers); - void cleanSwapchain(std::shared_ptr pPipeline); + void cleanSwapchain(); //init fonctions - void recreateSwapchain(Pipeline& pipeline); + void recreateSwapchain(); //helper fonctions diff --git a/Engine/renderer/VulkanPipeline.cpp b/Engine/renderer/VulkanPipeline.cpp index 249f7cd..5e5332b 100644 --- a/Engine/renderer/VulkanPipeline.cpp +++ b/Engine/renderer/VulkanPipeline.cpp @@ -4,6 +4,7 @@ VulkanPipeline::VulkanPipeline(Graphics & m_graphic) : m_graphic(m_graphic) { + CreatePipelines(); } @@ -11,7 +12,7 @@ VulkanPipeline::~VulkanPipeline() { } -void VulkanPipeline::createPipeline(Pipeline& pipeline) +void VulkanPipeline::createPipeline(std::shared_ptr p_pipeline) { auto bindingDescription = Vertex::getBindingDescription(); auto attributeDescriptions = Vertex::getAttributeDescriptions(); @@ -114,7 +115,7 @@ void VulkanPipeline::createPipeline(Pipeline& pipeline) pipelineLayoutInfo.setLayoutCount = 1; pipelineLayoutInfo.pSetLayouts = &m_graphic.descriptor_set_layout; - if (vkCreatePipelineLayout(m_graphic.device, &pipelineLayoutInfo, nullptr, &pipeline.layout) != VK_SUCCESS) + if (vkCreatePipelineLayout(m_graphic.device, &pipelineLayoutInfo, nullptr, &p_pipeline->layout) != VK_SUCCESS) { throw std::runtime_error("failed to create pipeline layout!"); } @@ -130,12 +131,12 @@ void VulkanPipeline::createPipeline(Pipeline& pipeline) pipelineInfo.pMultisampleState = &multisampling; pipelineInfo.pDepthStencilState = &depthStencil; pipelineInfo.pColorBlendState = &colorBlending; - pipelineInfo.layout = pipeline.layout; + pipelineInfo.layout = p_pipeline->layout; pipelineInfo.renderPass = m_graphic.render_pass; pipelineInfo.subpass = 0; pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; - if (vkCreateGraphicsPipelines(m_graphic.device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &pipeline.handle) != VK_SUCCESS) + if (vkCreateGraphicsPipelines(m_graphic.device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &p_pipeline->handle) != VK_SUCCESS) { throw std::runtime_error("failed to create graphics pipeline!"); } @@ -144,9 +145,45 @@ void VulkanPipeline::createPipeline(Pipeline& pipeline) vkDestroyShaderModule(m_graphic.device, vertShaderModule, nullptr); } -void VulkanPipeline::bindPipeline(VkCommandBuffer& commandBuffer, Pipeline& pipeline) +void VulkanPipeline::bindPipeline(VkCommandBuffer& commandBuffer, std::shared_ptr pipeline) { - vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.handle); + vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->handle); +} + +void VulkanPipeline::CreatePipelines() +{ + m_base_pipeline = std::make_shared(); + m_no_texture_pipeline = std::make_shared(); + m_texture_pipeline = std::make_shared(); + + createPipeline(m_base_pipeline); + createPipeline(m_no_texture_pipeline); + createPipeline(m_texture_pipeline); +} + +void VulkanPipeline::DestroyPipelines() +{ + vkDestroyPipeline(m_graphic.device, m_base_pipeline->handle, nullptr); + vkDestroyPipelineLayout(m_graphic.device, m_base_pipeline->layout, nullptr); + + vkDestroyPipeline(m_graphic.device, m_no_texture_pipeline->handle, nullptr); + vkDestroyPipelineLayout(m_graphic.device, m_no_texture_pipeline->layout, nullptr); + + vkDestroyPipeline(m_graphic.device, m_texture_pipeline->handle, nullptr); + vkDestroyPipelineLayout(m_graphic.device, m_texture_pipeline->layout, nullptr); +} + +std::shared_ptr VulkanPipeline::GetPipeline(const SHADER shader) +{ + switch (shader) + { + case SHADER::NO_TEXTURE: + return m_no_texture_pipeline; + case SHADER::TEXTURE: + return m_texture_pipeline; + default: + return m_base_pipeline; + } } std::vector VulkanPipeline::readFile(const std::string & filename) diff --git a/Engine/renderer/VulkanPipeline.h b/Engine/renderer/VulkanPipeline.h index 158cc9e..d83b377 100644 --- a/Engine/renderer/VulkanPipeline.h +++ b/Engine/renderer/VulkanPipeline.h @@ -6,20 +6,35 @@ #include "../graphics/Vertex.h" #include "../graphics/Graphics.h" +enum class SHADER +{ + NONE, + NO_TEXTURE, + TEXTURE +}; + class VulkanPipeline { public: VulkanPipeline(Graphics & m_graphic); ~VulkanPipeline(); - void createPipeline(Pipeline& pipeline); - void bindPipeline(VkCommandBuffer& commandBuffer, Pipeline& pipeline); + void createPipeline(std::shared_ptr pipeline); + void bindPipeline(VkCommandBuffer& commandBuffer, std::shared_ptr pipeline); + void CreatePipelines(); + void DestroyPipelines(); + + std::shared_ptr GetPipeline(const SHADER shader); private: std::vector readFile(const std::string& filename); VkShaderModule createShaderModule(const std::vector& code); + std::shared_ptr m_texture_pipeline; + std::shared_ptr m_no_texture_pipeline; + std::shared_ptr m_base_pipeline; + Graphics & m_graphic; }; diff --git a/Engine/world/GameObject.cpp b/Engine/world/GameObject.cpp index 6059165..f06a7e9 100644 --- a/Engine/world/GameObject.cpp +++ b/Engine/world/GameObject.cpp @@ -22,11 +22,11 @@ void GameObject::destroy() vkFreeMemory(mp_renderer->getDevice(), m_ubo_memory, nullptr); } -void GameObject::registerDrawCmd(VkCommandBuffer& command_buffer, Pipeline& pipeline) +void GameObject::registerDrawCmd(VkCommandBuffer& command_buffer) { for (size_t i = 0; i < m_meshes.size(); i++) { - m_meshes[i].draw(command_buffer, pipeline); + m_meshes[i].draw(command_buffer); } } diff --git a/Engine/world/GameObject.h b/Engine/world/GameObject.h index cdbec75..5b7fa36 100644 --- a/Engine/world/GameObject.h +++ b/Engine/world/GameObject.h @@ -14,7 +14,7 @@ class GameObject void destroy(); - void registerDrawCmd(VkCommandBuffer& command_buffer, Pipeline& pipeline); + void registerDrawCmd(VkCommandBuffer& command_buffer); void bindMatToMesh(const size_t& index, std::shared_ptr p_material); void loadMesh(const std::string& mesh_path); diff --git a/Engine/world/Material.cpp b/Engine/world/Material.cpp index 0177da5..36bd945 100644 --- a/Engine/world/Material.cpp +++ b/Engine/world/Material.cpp @@ -1,6 +1,6 @@ #include "Material.h" -Material::Material() +Material::Material(const SHADER shader, std::shared_ptr p_renderer) : m_shader(shader), mp_renderer(p_renderer) { m_descriptor_set = VK_NULL_HANDLE; } @@ -37,7 +37,7 @@ void Material::destroyTexture() std::shared_ptr Material::getColor() { - return std::shared_ptr(); + return m_color; } std::shared_ptr Material::getTexture() @@ -45,6 +45,11 @@ std::shared_ptr Material::getTexture() return m_texture; } +std::shared_ptr Material::GetPipeline() +{ + return mp_renderer->GetPipelineFactory()->GetPipeline(m_shader); +} + VkDescriptorSet& Material::getDescriptorSet() { return m_descriptor_set; diff --git a/Engine/world/Material.h b/Engine/world/Material.h index d7e5785..6ff3e77 100644 --- a/Engine/world/Material.h +++ b/Engine/world/Material.h @@ -10,7 +10,7 @@ class Material { public: - Material(); + Material(const SHADER shader, std::shared_ptr p_renderer); ~Material(); void setColor(glm::vec3 color); @@ -21,12 +21,15 @@ class Material std::shared_ptr getColor(); std::shared_ptr getTexture(); + std::shared_ptr GetPipeline(); VkDescriptorSet& getDescriptorSet(); private: std::shared_ptr m_texture; std::shared_ptr m_color; + const SHADER m_shader; VkDescriptorSet m_descriptor_set; + std::shared_ptr mp_renderer; }; #endif // !_MATERIAL_H \ No newline at end of file diff --git a/Engine/world/Mesh.cpp b/Engine/world/Mesh.cpp index 8f58042..597f888 100644 --- a/Engine/world/Mesh.cpp +++ b/Engine/world/Mesh.cpp @@ -13,15 +13,17 @@ Mesh::~Mesh() { } -void Mesh::draw(const VkCommandBuffer& command_buffer, const Pipeline& pipeline) +void Mesh::draw(const VkCommandBuffer& command_buffer) { + vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, p_material->GetPipeline()->handle); + VkBuffer vertex_buffer[] = { m_buffer.vertex }; VkDeviceSize offsets[] = { 0 }; vkCmdBindVertexBuffers(command_buffer, 0, 1, vertex_buffer, offsets); vkCmdBindIndexBuffer(command_buffer, m_buffer.index, 0, VK_INDEX_TYPE_UINT32); - vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline.layout, 0, 1, &p_material->getDescriptorSet(), 0, nullptr); + vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, p_material->GetPipeline()->layout, 0, 1, &p_material->getDescriptorSet(), 0, nullptr); vkCmdDrawIndexed(command_buffer, static_cast(m_indices.size()), 1, 0, 0, 0); } diff --git a/Engine/world/Mesh.h b/Engine/world/Mesh.h index c1a535a..c24de48 100644 --- a/Engine/world/Mesh.h +++ b/Engine/world/Mesh.h @@ -21,7 +21,7 @@ class Mesh Mesh(const std::string& obj_path, std::shared_ptr p_renderer); ~Mesh(); - void draw(const VkCommandBuffer& command_buffer, const Pipeline& pipeline); + void draw(const VkCommandBuffer& command_buffer); void loadModel(); void bindMaterial(std::shared_ptr mat, VkBuffer& ubo, std::shared_ptr renderer); void createBuffer(std::shared_ptr engine); diff --git a/Engine/world/Scene.cpp b/Engine/world/Scene.cpp index c0985f2..eca397c 100644 --- a/Engine/world/Scene.cpp +++ b/Engine/world/Scene.cpp @@ -44,11 +44,11 @@ R3DResult Scene::removeGameObject(std::shared_ptr gameobject) return R3DResult::R3D_OBJECT_NOT_FOUND; } -void Scene::render(Pipeline& pipeline, VkCommandBuffer& command_buffer, const int i) +void Scene::render(VkCommandBuffer& command_buffer, const int i) { for (size_t i = 0; i < vp_objects.size(); i++) { - vp_objects[i]->registerDrawCmd(command_buffer, pipeline); + vp_objects[i]->registerDrawCmd(command_buffer); } m_changed[i] = false; diff --git a/Engine/world/Scene.h b/Engine/world/Scene.h index 5f9e626..6c153b7 100644 --- a/Engine/world/Scene.h +++ b/Engine/world/Scene.h @@ -18,7 +18,7 @@ class Scene R3DResult addGameObject(std::shared_ptr gameobject); R3DResult removeGameObject(std::shared_ptr gameobject); - void render(Pipeline& pipeline, VkCommandBuffer& command_buffer, const int i); + void render(VkCommandBuffer& command_buffer, const int i); void updateUBO(std::shared_ptr p_camera, std::shared_ptr p_renderer); const bool& isUpdate(const int i); From c40a9c0b78a8632aedb0a6468c426c52f87e2913 Mon Sep 17 00:00:00 2001 From: Dan Liebault Date: Sat, 8 May 2021 16:12:38 +0200 Subject: [PATCH 04/11] pipeline are kept under an array --- Engine/Source.cpp | 2 +- Engine/renderer/Renderer.cpp | 5 ---- Engine/renderer/VulkanPipeline.cpp | 40 +++++++++++++----------------- Engine/renderer/VulkanPipeline.h | 10 +++----- Engine/world/Material.cpp | 2 +- Engine/world/Material.h | 2 +- Engine/world/Mesh.cpp | 4 +-- 7 files changed, 26 insertions(+), 39 deletions(-) diff --git a/Engine/Source.cpp b/Engine/Source.cpp index 9070e56..45b2319 100644 --- a/Engine/Source.cpp +++ b/Engine/Source.cpp @@ -7,7 +7,7 @@ int main() Engine engine; Logger::init(); - std::shared_ptr room_texture = std::make_shared(SHADER::NONE, engine.getRenderEngine()); + std::shared_ptr room_texture = std::make_shared(SHADER::DEFAULT, engine.getRenderEngine()); room_texture->loadTexture("assets/textures/viking_room.png", engine.getRenderEngine()); std::shared_ptr room = std::make_shared(engine.getRenderEngine()); diff --git a/Engine/renderer/Renderer.cpp b/Engine/renderer/Renderer.cpp index 07c3354..bc64861 100644 --- a/Engine/renderer/Renderer.cpp +++ b/Engine/renderer/Renderer.cpp @@ -166,11 +166,6 @@ void Renderer::setupCommandPool() m_commandPool = std::make_unique(m_graphic); } -void Renderer::createNewPipeline(std::shared_ptr pipeline) -{ - m_pPipelineFactory->createPipeline(pipeline); -} - VkDevice& Renderer::getDevice() { return m_graphic.device; diff --git a/Engine/renderer/VulkanPipeline.cpp b/Engine/renderer/VulkanPipeline.cpp index 5e5332b..1a0c718 100644 --- a/Engine/renderer/VulkanPipeline.cpp +++ b/Engine/renderer/VulkanPipeline.cpp @@ -12,7 +12,7 @@ VulkanPipeline::~VulkanPipeline() { } -void VulkanPipeline::createPipeline(std::shared_ptr p_pipeline) +void VulkanPipeline::createPipeline(Pipeline& pipeline) { auto bindingDescription = Vertex::getBindingDescription(); auto attributeDescriptions = Vertex::getAttributeDescriptions(); @@ -115,7 +115,7 @@ void VulkanPipeline::createPipeline(std::shared_ptr p_pipeline) pipelineLayoutInfo.setLayoutCount = 1; pipelineLayoutInfo.pSetLayouts = &m_graphic.descriptor_set_layout; - if (vkCreatePipelineLayout(m_graphic.device, &pipelineLayoutInfo, nullptr, &p_pipeline->layout) != VK_SUCCESS) + if (vkCreatePipelineLayout(m_graphic.device, &pipelineLayoutInfo, nullptr, &pipeline.layout) != VK_SUCCESS) { throw std::runtime_error("failed to create pipeline layout!"); } @@ -131,12 +131,12 @@ void VulkanPipeline::createPipeline(std::shared_ptr p_pipeline) pipelineInfo.pMultisampleState = &multisampling; pipelineInfo.pDepthStencilState = &depthStencil; pipelineInfo.pColorBlendState = &colorBlending; - pipelineInfo.layout = p_pipeline->layout; + pipelineInfo.layout = pipeline.layout; pipelineInfo.renderPass = m_graphic.render_pass; pipelineInfo.subpass = 0; pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; - if (vkCreateGraphicsPipelines(m_graphic.device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &p_pipeline->handle) != VK_SUCCESS) + if (vkCreateGraphicsPipelines(m_graphic.device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &pipeline.handle) != VK_SUCCESS) { throw std::runtime_error("failed to create graphics pipeline!"); } @@ -152,37 +152,31 @@ void VulkanPipeline::bindPipeline(VkCommandBuffer& commandBuffer, std::shared_pt void VulkanPipeline::CreatePipelines() { - m_base_pipeline = std::make_shared(); - m_no_texture_pipeline = std::make_shared(); - m_texture_pipeline = std::make_shared(); - - createPipeline(m_base_pipeline); - createPipeline(m_no_texture_pipeline); - createPipeline(m_texture_pipeline); + for (size_t i = 0; i < m_pipelines.size(); i++) + { + createPipeline(m_pipelines[i]); + } } void VulkanPipeline::DestroyPipelines() { - vkDestroyPipeline(m_graphic.device, m_base_pipeline->handle, nullptr); - vkDestroyPipelineLayout(m_graphic.device, m_base_pipeline->layout, nullptr); - - vkDestroyPipeline(m_graphic.device, m_no_texture_pipeline->handle, nullptr); - vkDestroyPipelineLayout(m_graphic.device, m_no_texture_pipeline->layout, nullptr); - - vkDestroyPipeline(m_graphic.device, m_texture_pipeline->handle, nullptr); - vkDestroyPipelineLayout(m_graphic.device, m_texture_pipeline->layout, nullptr); + for (size_t i = 0; i < m_pipelines.size(); i++) + { + vkDestroyPipeline(m_graphic.device, m_pipelines[i].handle, nullptr); + vkDestroyPipelineLayout(m_graphic.device, m_pipelines[i].layout, nullptr); + } } -std::shared_ptr VulkanPipeline::GetPipeline(const SHADER shader) +const Pipeline& VulkanPipeline::GetPipeline(const SHADER shader) { switch (shader) { case SHADER::NO_TEXTURE: - return m_no_texture_pipeline; + return m_pipelines[1]; case SHADER::TEXTURE: - return m_texture_pipeline; + return m_pipelines[2]; default: - return m_base_pipeline; + return m_pipelines[0]; } } diff --git a/Engine/renderer/VulkanPipeline.h b/Engine/renderer/VulkanPipeline.h index d83b377..c47d42d 100644 --- a/Engine/renderer/VulkanPipeline.h +++ b/Engine/renderer/VulkanPipeline.h @@ -8,7 +8,7 @@ enum class SHADER { - NONE, + DEFAULT, NO_TEXTURE, TEXTURE }; @@ -19,21 +19,19 @@ class VulkanPipeline VulkanPipeline(Graphics & m_graphic); ~VulkanPipeline(); - void createPipeline(std::shared_ptr pipeline); + void createPipeline(Pipeline& pipeline); void bindPipeline(VkCommandBuffer& commandBuffer, std::shared_ptr pipeline); void CreatePipelines(); void DestroyPipelines(); - std::shared_ptr GetPipeline(const SHADER shader); + const Pipeline& GetPipeline(const SHADER shader); private: std::vector readFile(const std::string& filename); VkShaderModule createShaderModule(const std::vector& code); - std::shared_ptr m_texture_pipeline; - std::shared_ptr m_no_texture_pipeline; - std::shared_ptr m_base_pipeline; + std::array m_pipelines; Graphics & m_graphic; }; diff --git a/Engine/world/Material.cpp b/Engine/world/Material.cpp index 36bd945..c450c85 100644 --- a/Engine/world/Material.cpp +++ b/Engine/world/Material.cpp @@ -45,7 +45,7 @@ std::shared_ptr Material::getTexture() return m_texture; } -std::shared_ptr Material::GetPipeline() +const Pipeline& Material::GetPipeline() { return mp_renderer->GetPipelineFactory()->GetPipeline(m_shader); } diff --git a/Engine/world/Material.h b/Engine/world/Material.h index 6ff3e77..1813bd8 100644 --- a/Engine/world/Material.h +++ b/Engine/world/Material.h @@ -21,7 +21,7 @@ class Material std::shared_ptr getColor(); std::shared_ptr getTexture(); - std::shared_ptr GetPipeline(); + const Pipeline& GetPipeline(); VkDescriptorSet& getDescriptorSet(); private: std::shared_ptr m_texture; diff --git a/Engine/world/Mesh.cpp b/Engine/world/Mesh.cpp index 597f888..0498ee9 100644 --- a/Engine/world/Mesh.cpp +++ b/Engine/world/Mesh.cpp @@ -15,7 +15,7 @@ Mesh::~Mesh() void Mesh::draw(const VkCommandBuffer& command_buffer) { - vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, p_material->GetPipeline()->handle); + vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, p_material->GetPipeline().handle); VkBuffer vertex_buffer[] = { m_buffer.vertex }; VkDeviceSize offsets[] = { 0 }; @@ -23,7 +23,7 @@ void Mesh::draw(const VkCommandBuffer& command_buffer) vkCmdBindVertexBuffers(command_buffer, 0, 1, vertex_buffer, offsets); vkCmdBindIndexBuffer(command_buffer, m_buffer.index, 0, VK_INDEX_TYPE_UINT32); - vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, p_material->GetPipeline()->layout, 0, 1, &p_material->getDescriptorSet(), 0, nullptr); + vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, p_material->GetPipeline().layout, 0, 1, &p_material->getDescriptorSet(), 0, nullptr); vkCmdDrawIndexed(command_buffer, static_cast(m_indices.size()), 1, 0, 0, 0); } From 93eed8319af10fdc4ab34d308b1d07ed2da2c6f7 Mon Sep 17 00:00:00 2001 From: Dan Liebault Date: Sat, 8 May 2021 16:23:58 +0200 Subject: [PATCH 05/11] abstract shader files --- Engine/Source.cpp | 2 +- Engine/renderer/VulkanPipeline.cpp | 87 ++++++++++++++---------------- Engine/renderer/VulkanPipeline.h | 11 ++-- 3 files changed, 47 insertions(+), 53 deletions(-) diff --git a/Engine/Source.cpp b/Engine/Source.cpp index 45b2319..4759102 100644 --- a/Engine/Source.cpp +++ b/Engine/Source.cpp @@ -7,7 +7,7 @@ int main() Engine engine; Logger::init(); - std::shared_ptr room_texture = std::make_shared(SHADER::DEFAULT, engine.getRenderEngine()); + std::shared_ptr room_texture = std::make_shared(SHADER::TEXTURE, engine.getRenderEngine()); room_texture->loadTexture("assets/textures/viking_room.png", engine.getRenderEngine()); std::shared_ptr room = std::make_shared(engine.getRenderEngine()); diff --git a/Engine/renderer/VulkanPipeline.cpp b/Engine/renderer/VulkanPipeline.cpp index 1a0c718..b7cc661 100644 --- a/Engine/renderer/VulkanPipeline.cpp +++ b/Engine/renderer/VulkanPipeline.cpp @@ -12,13 +12,43 @@ VulkanPipeline::~VulkanPipeline() { } -void VulkanPipeline::createPipeline(Pipeline& pipeline) +void VulkanPipeline::CreatePipelines() +{ + for (size_t i = 0; i < m_pipelines.size(); i++) + { + createPipeline(m_pipelines[i], m_shader_files[i]); + } +} + +void VulkanPipeline::DestroyPipelines() +{ + for (size_t i = 0; i < m_pipelines.size(); i++) + { + vkDestroyPipeline(m_graphic.device, m_pipelines[i].handle, nullptr); + vkDestroyPipelineLayout(m_graphic.device, m_pipelines[i].layout, nullptr); + } +} + +const Pipeline& VulkanPipeline::GetPipeline(const SHADER shader) +{ + switch (shader) + { + case SHADER::NO_TEXTURE: + return m_pipelines[1]; + case SHADER::TEXTURE: + return m_pipelines[2]; + default: + return m_pipelines[0]; + } +} + +void VulkanPipeline::createPipeline(Pipeline& pipeline, const std::string& shader_file) { auto bindingDescription = Vertex::getBindingDescription(); auto attributeDescriptions = Vertex::getAttributeDescriptions(); auto vertShaderCode = readFile("assets/shaders/vert.spv"); - auto fragShaderCode = readFile("assets/shaders/texture_shader.spv"); + auto fragShaderCode = readFile(shader_file); VkShaderModule vertShaderModule = createShaderModule(vertShaderCode); VkShaderModule fragShaderModule = createShaderModule(fragShaderCode); @@ -145,39 +175,20 @@ void VulkanPipeline::createPipeline(Pipeline& pipeline) vkDestroyShaderModule(m_graphic.device, vertShaderModule, nullptr); } -void VulkanPipeline::bindPipeline(VkCommandBuffer& commandBuffer, std::shared_ptr pipeline) -{ - vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->handle); -} - -void VulkanPipeline::CreatePipelines() +VkShaderModule VulkanPipeline::createShaderModule(const std::vector& code) { - for (size_t i = 0; i < m_pipelines.size(); i++) - { - createPipeline(m_pipelines[i]); - } -} + VkShaderModuleCreateInfo shader_module_info = {}; + shader_module_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; + shader_module_info.codeSize = code.size(); + shader_module_info.pCode = reinterpret_cast(code.data()); -void VulkanPipeline::DestroyPipelines() -{ - for (size_t i = 0; i < m_pipelines.size(); i++) + VkShaderModule shaderModule; + if (vkCreateShaderModule(m_graphic.device, &shader_module_info, nullptr, &shaderModule) != VK_SUCCESS) { - vkDestroyPipeline(m_graphic.device, m_pipelines[i].handle, nullptr); - vkDestroyPipelineLayout(m_graphic.device, m_pipelines[i].layout, nullptr); + throw std::runtime_error("failed to create shader module!"); } -} -const Pipeline& VulkanPipeline::GetPipeline(const SHADER shader) -{ - switch (shader) - { - case SHADER::NO_TEXTURE: - return m_pipelines[1]; - case SHADER::TEXTURE: - return m_pipelines[2]; - default: - return m_pipelines[0]; - } + return shaderModule; } std::vector VulkanPipeline::readFile(const std::string & filename) @@ -199,19 +210,3 @@ std::vector VulkanPipeline::readFile(const std::string & filename) return buffer; } - -VkShaderModule VulkanPipeline::createShaderModule(const std::vector& code) -{ - VkShaderModuleCreateInfo shader_module_info = {}; - shader_module_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; - shader_module_info.codeSize = code.size(); - shader_module_info.pCode = reinterpret_cast(code.data()); - - VkShaderModule shaderModule; - if (vkCreateShaderModule(m_graphic.device, &shader_module_info, nullptr, &shaderModule) != VK_SUCCESS) - { - throw std::runtime_error("failed to create shader module!"); - } - - return shaderModule; -} diff --git a/Engine/renderer/VulkanPipeline.h b/Engine/renderer/VulkanPipeline.h index c47d42d..ccd4aa5 100644 --- a/Engine/renderer/VulkanPipeline.h +++ b/Engine/renderer/VulkanPipeline.h @@ -19,21 +19,20 @@ class VulkanPipeline VulkanPipeline(Graphics & m_graphic); ~VulkanPipeline(); - void createPipeline(Pipeline& pipeline); - void bindPipeline(VkCommandBuffer& commandBuffer, std::shared_ptr pipeline); void CreatePipelines(); void DestroyPipelines(); const Pipeline& GetPipeline(const SHADER shader); private: - - std::vector readFile(const std::string& filename); + void createPipeline(Pipeline& pipeline, const std::string& shader_file); VkShaderModule createShaderModule(const std::vector& code); - std::array m_pipelines; + std::vector readFile(const std::string& filename);//should be in other file - Graphics & m_graphic; + std::array m_shader_files = {"assets/shaders/frag.spv", "assets/shaders/no_texture_shader.spv", "assets/shaders/texture_shader.spv"}; + std::array m_pipelines; + Graphics & m_graphic;//should be shared ptr }; #endif _VULKAN_PIPELINE_H \ No newline at end of file From 27ba33cdfbdc0ce0cea8bc2013e3898ff9df3eee Mon Sep 17 00:00:00 2001 From: Dan Liebault Date: Sat, 8 May 2021 16:42:00 +0200 Subject: [PATCH 06/11] move shader files arr in a struct for modding --- Engine/Engine.vcxproj | 1 + Engine/Engine.vcxproj.filters | 3 +++ Engine/Source.cpp | 2 +- Engine/graphics/Shaders.h | 15 +++++++++++++++ Engine/renderer/VulkanPipeline.cpp | 8 ++++---- Engine/renderer/VulkanPipeline.h | 7 ++++--- Engine/world/Material.cpp | 2 +- Engine/world/Material.h | 4 ++-- 8 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 Engine/graphics/Shaders.h diff --git a/Engine/Engine.vcxproj b/Engine/Engine.vcxproj index e00c1ad..930dbe4 100644 --- a/Engine/Engine.vcxproj +++ b/Engine/Engine.vcxproj @@ -73,6 +73,7 @@ + diff --git a/Engine/Engine.vcxproj.filters b/Engine/Engine.vcxproj.filters index 9c93542..9127757 100644 --- a/Engine/Engine.vcxproj.filters +++ b/Engine/Engine.vcxproj.filters @@ -173,6 +173,9 @@ Header + + Engine\Graphics + diff --git a/Engine/Source.cpp b/Engine/Source.cpp index 4759102..ee2e204 100644 --- a/Engine/Source.cpp +++ b/Engine/Source.cpp @@ -7,7 +7,7 @@ int main() Engine engine; Logger::init(); - std::shared_ptr room_texture = std::make_shared(SHADER::TEXTURE, engine.getRenderEngine()); + std::shared_ptr room_texture = std::make_shared(TSHADER::TEXTURE, engine.getRenderEngine()); room_texture->loadTexture("assets/textures/viking_room.png", engine.getRenderEngine()); std::shared_ptr room = std::make_shared(engine.getRenderEngine()); diff --git a/Engine/graphics/Shaders.h b/Engine/graphics/Shaders.h new file mode 100644 index 0000000..5d979e7 --- /dev/null +++ b/Engine/graphics/Shaders.h @@ -0,0 +1,15 @@ +#ifndef _SHADERS_H +#define _SHADERS_H + +#include +#include + +struct Shaders { + std::array fragment_shader_files = { + "assets/shaders/frag.spv", + "assets/shaders/no_texture_shader.spv", + "assets/shaders/texture_shader.spv" + }; +}; + +#endif // !_SHADERS_H \ No newline at end of file diff --git a/Engine/renderer/VulkanPipeline.cpp b/Engine/renderer/VulkanPipeline.cpp index b7cc661..e68a545 100644 --- a/Engine/renderer/VulkanPipeline.cpp +++ b/Engine/renderer/VulkanPipeline.cpp @@ -16,7 +16,7 @@ void VulkanPipeline::CreatePipelines() { for (size_t i = 0; i < m_pipelines.size(); i++) { - createPipeline(m_pipelines[i], m_shader_files[i]); + createPipeline(m_pipelines[i], m_shaders.fragment_shader_files[i]); } } @@ -29,13 +29,13 @@ void VulkanPipeline::DestroyPipelines() } } -const Pipeline& VulkanPipeline::GetPipeline(const SHADER shader) +const Pipeline& VulkanPipeline::GetPipeline(const TSHADER shader) { switch (shader) { - case SHADER::NO_TEXTURE: + case TSHADER::NO_TEXTURE: return m_pipelines[1]; - case SHADER::TEXTURE: + case TSHADER::TEXTURE: return m_pipelines[2]; default: return m_pipelines[0]; diff --git a/Engine/renderer/VulkanPipeline.h b/Engine/renderer/VulkanPipeline.h index ccd4aa5..290a244 100644 --- a/Engine/renderer/VulkanPipeline.h +++ b/Engine/renderer/VulkanPipeline.h @@ -5,8 +5,9 @@ #include "../graphics/Vertex.h" #include "../graphics/Graphics.h" +#include "../graphics/Shaders.h" -enum class SHADER +enum class TSHADER { DEFAULT, NO_TEXTURE, @@ -22,7 +23,7 @@ class VulkanPipeline void CreatePipelines(); void DestroyPipelines(); - const Pipeline& GetPipeline(const SHADER shader); + const Pipeline& GetPipeline(const TSHADER shader); private: void createPipeline(Pipeline& pipeline, const std::string& shader_file); @@ -30,9 +31,9 @@ class VulkanPipeline std::vector readFile(const std::string& filename);//should be in other file - std::array m_shader_files = {"assets/shaders/frag.spv", "assets/shaders/no_texture_shader.spv", "assets/shaders/texture_shader.spv"}; std::array m_pipelines; Graphics & m_graphic;//should be shared ptr + Shaders m_shaders; }; #endif _VULKAN_PIPELINE_H \ No newline at end of file diff --git a/Engine/world/Material.cpp b/Engine/world/Material.cpp index c450c85..bbd475c 100644 --- a/Engine/world/Material.cpp +++ b/Engine/world/Material.cpp @@ -1,6 +1,6 @@ #include "Material.h" -Material::Material(const SHADER shader, std::shared_ptr p_renderer) : m_shader(shader), mp_renderer(p_renderer) +Material::Material(const TSHADER shader, std::shared_ptr p_renderer) : m_shader(shader), mp_renderer(p_renderer) { m_descriptor_set = VK_NULL_HANDLE; } diff --git a/Engine/world/Material.h b/Engine/world/Material.h index 1813bd8..771978a 100644 --- a/Engine/world/Material.h +++ b/Engine/world/Material.h @@ -10,7 +10,7 @@ class Material { public: - Material(const SHADER shader, std::shared_ptr p_renderer); + Material(const TSHADER shader, std::shared_ptr p_renderer); ~Material(); void setColor(glm::vec3 color); @@ -27,7 +27,7 @@ class Material std::shared_ptr m_texture; std::shared_ptr m_color; - const SHADER m_shader; + const TSHADER m_shader; VkDescriptorSet m_descriptor_set; std::shared_ptr mp_renderer; }; From 5d35d73856ff4a556e66aad79e9effbdea4569f6 Mon Sep 17 00:00:00 2001 From: Dan Liebault Date: Sat, 8 May 2021 16:44:00 +0200 Subject: [PATCH 07/11] rename pipeline factory --- Engine/renderer/Renderer.cpp | 8 ++++---- Engine/renderer/Renderer.h | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Engine/renderer/Renderer.cpp b/Engine/renderer/Renderer.cpp index bc64861..704416e 100644 --- a/Engine/renderer/Renderer.cpp +++ b/Engine/renderer/Renderer.cpp @@ -26,7 +26,7 @@ Renderer::Renderer(GLFWwindow& window, uint32_t width, uint32_t height) setupDescriptorSetLayout(); setupCommandPool(); - m_pPipelineFactory = std::make_unique(m_graphic); + mp_pipelines_manager = std::make_unique(m_graphic); } @@ -203,7 +203,7 @@ std::unique_ptr& Renderer::getBufferFactory() std::unique_ptr& Renderer::GetPipelineFactory() { - return m_pPipelineFactory; + return mp_pipelines_manager; } const int Renderer::getFrameIndex() @@ -636,7 +636,7 @@ void Renderer::recreateSwapchain() VkFormat format = findSupportedFormat({ VK_FORMAT_D32_SFLOAT, VK_FORMAT_D32_SFLOAT_S8_UINT, VK_FORMAT_D24_UNORM_S8_UINT }, VK_IMAGE_TILING_OPTIMAL, VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT); m_pRenderpass->createRenderPass(format); - m_pPipelineFactory->CreatePipelines(); + mp_pipelines_manager->CreatePipelines(); createDepthResources(); createFramebuffer(); allocateCommandBuffers(); @@ -656,7 +656,7 @@ void Renderer::cleanSwapchain() vkFreeCommandBuffers(m_graphic.device, m_graphic.command_pool, static_cast(m_graphic.command_buffers.size()), m_graphic.command_buffers.data()); - m_pPipelineFactory->DestroyPipelines(); + mp_pipelines_manager->DestroyPipelines(); vkDestroyRenderPass(m_graphic.device, m_graphic.render_pass, nullptr); diff --git a/Engine/renderer/Renderer.h b/Engine/renderer/Renderer.h index d2c68bf..e08375c 100644 --- a/Engine/renderer/Renderer.h +++ b/Engine/renderer/Renderer.h @@ -43,7 +43,6 @@ class Renderer void setupRenderPass(); void setupDescriptorSetLayout(); void setupCommandPool(); - void createNewPipeline(std::shared_ptr pipeline); //getters VkDevice& getDevice(); @@ -138,7 +137,7 @@ class Renderer std::unique_ptr m_pRenderpass; std::unique_ptr m_descriptor; std::unique_ptr m_commandPool; - std::unique_ptr m_pPipelineFactory; + std::unique_ptr mp_pipelines_manager; std::unique_ptr m_pBufferFactory; size_t m_frame_index = 0; From 42b93e42c81b0da129c7d938dba1f6f5973bffc3 Mon Sep 17 00:00:00 2001 From: Dan Liebault Date: Sat, 8 May 2021 17:05:47 +0200 Subject: [PATCH 08/11] material has now a factory --- Engine/Engine.cpp | 14 ++++++++++++-- Engine/Engine.h | 4 ++++ Engine/Source.cpp | 5 +++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Engine/Engine.cpp b/Engine/Engine.cpp index 843539b..9b06be9 100644 --- a/Engine/Engine.cpp +++ b/Engine/Engine.cpp @@ -12,8 +12,6 @@ Engine::Engine() mp_renderer = std::make_shared(mp_window->getHandle(), mp_config->width, mp_config->height); - // create pipelines - mp_renderer->createDepthResources(); mp_renderer->createFramebuffer(); @@ -63,6 +61,18 @@ std::shared_ptr Engine::getRenderEngine() return mp_renderer; } +const Material Engine::CreateMaterial(const TSHADER shader) +{ + return Material(shader, mp_renderer); +} + +const Material Engine::CreateMaterial(const TSHADER shader, const std::string& texture_file) +{ + Material mat(shader, mp_renderer); + mat.loadTexture(texture_file, mp_renderer); + return mat; +} + const bool& Engine::shouldClose() { return glfwWindowShouldClose(&mp_window->getHandle()); diff --git a/Engine/Engine.h b/Engine/Engine.h index ecc69f3..c3a773d 100644 --- a/Engine/Engine.h +++ b/Engine/Engine.h @@ -21,6 +21,10 @@ class Engine std::shared_ptr getRenderEngine(); + // CREATE MATERIAL + const Material CreateMaterial(const TSHADER shader); + const Material CreateMaterial(const TSHADER shader, const std::string& texture_file); + const bool& shouldClose(); private: diff --git a/Engine/Source.cpp b/Engine/Source.cpp index ee2e204..b834c9a 100644 --- a/Engine/Source.cpp +++ b/Engine/Source.cpp @@ -7,8 +7,9 @@ int main() Engine engine; Logger::init(); - std::shared_ptr room_texture = std::make_shared(TSHADER::TEXTURE, engine.getRenderEngine()); - room_texture->loadTexture("assets/textures/viking_room.png", engine.getRenderEngine()); + std::shared_ptr room_texture = std::make_shared(engine.CreateMaterial(TSHADER::TEXTURE, "assets/textures/viking_room.png")); + //std::shared_ptr room_texture = std::make_shared(TSHADER::NO_TEXTURE, engine.getRenderEngine()); + //room_texture->loadTexture("assets/textures/viking_room.png", engine.getRenderEngine()); std::shared_ptr room = std::make_shared(engine.getRenderEngine()); room->loadMesh("assets/models/viking_room.obj"); From 8673c4dcdfb38f3ac2f2fd7439d29cb0825a0201 Mon Sep 17 00:00:00 2001 From: Dan Liebault Date: Sat, 8 May 2021 17:09:11 +0200 Subject: [PATCH 09/11] renderer is no longer needed to load texture --- Engine/Engine.cpp | 2 +- Engine/world/Material.cpp | 6 +++--- Engine/world/Material.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Engine/Engine.cpp b/Engine/Engine.cpp index 9b06be9..14a9f3d 100644 --- a/Engine/Engine.cpp +++ b/Engine/Engine.cpp @@ -69,7 +69,7 @@ const Material Engine::CreateMaterial(const TSHADER shader) const Material Engine::CreateMaterial(const TSHADER shader, const std::string& texture_file) { Material mat(shader, mp_renderer); - mat.loadTexture(texture_file, mp_renderer); + mat.LoadTexture(texture_file); return mat; } diff --git a/Engine/world/Material.cpp b/Engine/world/Material.cpp index bbd475c..3bd817a 100644 --- a/Engine/world/Material.cpp +++ b/Engine/world/Material.cpp @@ -21,15 +21,15 @@ void Material::clearColor() m_color = nullptr; } -void Material::loadTexture(const std::string& texture_path, std::shared_ptr renderer) +void Material::LoadTexture(const std::string& texture_path) { - m_texture = std::make_shared(texture_path, renderer); + m_texture = std::make_shared(texture_path, mp_renderer); m_texture->createTextureImage(); m_texture->createTextureImageView(); m_texture->createTextureSampler(); } -void Material::destroyTexture() +void Material::DestroyTexture() { m_texture.reset(); m_texture = nullptr; diff --git a/Engine/world/Material.h b/Engine/world/Material.h index 771978a..a0425f4 100644 --- a/Engine/world/Material.h +++ b/Engine/world/Material.h @@ -16,8 +16,8 @@ class Material void setColor(glm::vec3 color); void clearColor(); - void loadTexture(const std::string& texture_path, std::shared_ptr renderer); - void destroyTexture(); + void LoadTexture(const std::string& texture_path); + void DestroyTexture(); std::shared_ptr getColor(); std::shared_ptr getTexture(); From 1c6ac5a87e5e92517a1651c15b8634d0d4a2a66c Mon Sep 17 00:00:00 2001 From: Dan Liebault Date: Sat, 8 May 2021 17:14:12 +0200 Subject: [PATCH 10/11] access to renderer is removed --- Engine/Engine.cpp | 12 ++++++------ Engine/Engine.h | 5 +++-- Engine/Source.cpp | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Engine/Engine.cpp b/Engine/Engine.cpp index 14a9f3d..1be4d38 100644 --- a/Engine/Engine.cpp +++ b/Engine/Engine.cpp @@ -29,7 +29,7 @@ Engine::~Engine() { for (size_t t = 0; t < mp_scene->getObjects()[i]->getMeshesCount(); t++) { - mp_scene->getObjects()[i]->getMesh(t).getMaterial()->destroyTexture(); + mp_scene->getObjects()[i]->getMesh(t).getMaterial()->DestroyTexture(); mp_scene->getObjects()[i]->getMesh(t).destroyMesh(); } @@ -56,11 +56,6 @@ void Engine::registerGameObject(std::shared_ptr gameobject) mp_scene->addGameObject(gameobject); } -std::shared_ptr Engine::getRenderEngine() -{ - return mp_renderer; -} - const Material Engine::CreateMaterial(const TSHADER shader) { return Material(shader, mp_renderer); @@ -73,6 +68,11 @@ const Material Engine::CreateMaterial(const TSHADER shader, const std::string& t return mat; } +const GameObject Engine::CreateGameObject() +{ + return GameObject(mp_renderer); +} + const bool& Engine::shouldClose() { return glfwWindowShouldClose(&mp_window->getHandle()); diff --git a/Engine/Engine.h b/Engine/Engine.h index c3a773d..c177008 100644 --- a/Engine/Engine.h +++ b/Engine/Engine.h @@ -19,12 +19,13 @@ class Engine void setScene(std::shared_ptr p_scene); void registerGameObject(std::shared_ptr gameobject); - std::shared_ptr getRenderEngine(); - // CREATE MATERIAL const Material CreateMaterial(const TSHADER shader); const Material CreateMaterial(const TSHADER shader, const std::string& texture_file); + // CREATE GAMEOBJECT + const GameObject CreateGameObject(); + const bool& shouldClose(); private: diff --git a/Engine/Source.cpp b/Engine/Source.cpp index b834c9a..69b79a8 100644 --- a/Engine/Source.cpp +++ b/Engine/Source.cpp @@ -11,7 +11,7 @@ int main() //std::shared_ptr room_texture = std::make_shared(TSHADER::NO_TEXTURE, engine.getRenderEngine()); //room_texture->loadTexture("assets/textures/viking_room.png", engine.getRenderEngine()); - std::shared_ptr room = std::make_shared(engine.getRenderEngine()); + std::shared_ptr room = std::make_shared(engine.CreateGameObject()); room->loadMesh("assets/models/viking_room.obj"); room->bindMatToMesh(0, room_texture); room->setPosition({ 3.0f, 0.0f, 0.0f }); From 5ed311d074f583a00c7a93daeac9582733c64d04 Mon Sep 17 00:00:00 2001 From: Dan Liebault Date: Sat, 8 May 2021 17:45:22 +0200 Subject: [PATCH 11/11] abstract object creation to engine --- Engine/Engine.cpp | 21 ++++++++++++++------- Engine/Engine.h | 7 ++++--- Engine/Source.cpp | 7 +++---- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/Engine/Engine.cpp b/Engine/Engine.cpp index 1be4d38..d76750b 100644 --- a/Engine/Engine.cpp +++ b/Engine/Engine.cpp @@ -56,21 +56,28 @@ void Engine::registerGameObject(std::shared_ptr gameobject) mp_scene->addGameObject(gameobject); } -const Material Engine::CreateMaterial(const TSHADER shader) +const std::shared_ptr Engine::CreateMaterial(const TSHADER shader) { - return Material(shader, mp_renderer); + return std::make_shared(shader, mp_renderer); } -const Material Engine::CreateMaterial(const TSHADER shader, const std::string& texture_file) +const std::shared_ptr Engine::CreateMaterial(const TSHADER shader, const std::string& texture_file) { - Material mat(shader, mp_renderer); - mat.LoadTexture(texture_file); + std::shared_ptr mat = std::make_shared(shader, mp_renderer); + mat->LoadTexture(texture_file); return mat; } -const GameObject Engine::CreateGameObject() +const std::shared_ptr Engine::CreateGameObject() { - return GameObject(mp_renderer); + return std::make_shared(mp_renderer); +} + +const std::shared_ptr Engine::CreateGameObject(const std::string& object_file) +{ + std::shared_ptr go = std::make_shared(mp_renderer); + go->loadMesh(object_file); + return go; } const bool& Engine::shouldClose() diff --git a/Engine/Engine.h b/Engine/Engine.h index c177008..c971bae 100644 --- a/Engine/Engine.h +++ b/Engine/Engine.h @@ -20,11 +20,12 @@ class Engine void registerGameObject(std::shared_ptr gameobject); // CREATE MATERIAL - const Material CreateMaterial(const TSHADER shader); - const Material CreateMaterial(const TSHADER shader, const std::string& texture_file); + const std::shared_ptr CreateMaterial(const TSHADER shader); + const std::shared_ptr CreateMaterial(const TSHADER shader, const std::string& texture_file); // CREATE GAMEOBJECT - const GameObject CreateGameObject(); + const std::shared_ptr CreateGameObject(); + const std::shared_ptr CreateGameObject(const std::string& object_file); const bool& shouldClose(); diff --git a/Engine/Source.cpp b/Engine/Source.cpp index 69b79a8..f186c9b 100644 --- a/Engine/Source.cpp +++ b/Engine/Source.cpp @@ -7,11 +7,10 @@ int main() Engine engine; Logger::init(); - std::shared_ptr room_texture = std::make_shared(engine.CreateMaterial(TSHADER::TEXTURE, "assets/textures/viking_room.png")); - //std::shared_ptr room_texture = std::make_shared(TSHADER::NO_TEXTURE, engine.getRenderEngine()); - //room_texture->loadTexture("assets/textures/viking_room.png", engine.getRenderEngine()); + std::shared_ptr room_texture = engine.CreateMaterial(TSHADER::TEXTURE); + room_texture->LoadTexture("assets/textures/viking_room.png"); - std::shared_ptr room = std::make_shared(engine.CreateGameObject()); + std::shared_ptr room = engine.CreateGameObject(); room->loadMesh("assets/models/viking_room.obj"); room->bindMatToMesh(0, room_texture); room->setPosition({ 3.0f, 0.0f, 0.0f });