This commit is contained in:
bailwillharr 2022-11-08 13:42:07 +00:00
parent f4fd782930
commit b65058ab3e
15 changed files with 105 additions and 63 deletions

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.8) cmake_minimum_required(VERSION 3.23)
# options # options
option(ENGINE_BUILD_TEST "Compile the test program" ON) option(ENGINE_BUILD_TEST "Compile the test program" ON)
@ -77,10 +77,8 @@ set(INCLUDE_FILES
) )
add_library(${PROJECT_NAME} STATIC add_library(${PROJECT_NAME} STATIC
${SRC_FILES} ${SRC_FILES}
${INCLUDE_FILES} ${INCLUDE_FILES}
) )
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/src" PREFIX "Source" FILES ${SRC_FILES}) source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/src" PREFIX "Source" FILES ${SRC_FILES})
@ -131,30 +129,36 @@ if (MINGW)
target_link_libraries(${PROJECT_NAME} PUBLIC mingw32) target_link_libraries(${PROJECT_NAME} PUBLIC mingw32)
endif() endif()
# GLAD: if(ENGINE_BUILD_OPENGL)
set(GLAD_PROFILE "core" CACHE INTERNAL "" FORCE) # GLAD:
set(GLAD_API "gl=3.3" CACHE INTERNAL "" FORCE) set(GLAD_PROFILE "core" CACHE INTERNAL "" FORCE)
if (CMAKE_BUILD_TYPE STREQUAL "Debug") set(GLAD_API "gl=3.3" CACHE INTERNAL "" FORCE)
set(GLAD_GENERATOR "c-debug" CACHE INTERNAL "" FORCE) if (CMAKE_BUILD_TYPE STREQUAL "Debug")
else() set(GLAD_GENERATOR "c-debug" CACHE INTERNAL "" FORCE)
set(GLAD_GENERATOR "c" CACHE INTERNAL "" FORCE) else()
set(GLAD_GENERATOR "c" CACHE INTERNAL "" FORCE)
endif()
set(GLAD_SPEC "gl" CACHE INTERNAL "" FORCE)
set(BUILD_SHARED_LIBS OFF)
add_subdirectory(dependencies/glad)
target_link_libraries(${PROJECT_NAME} PUBLIC glad)
target_include_directories(${PROJECT_NAME} PUBLIC dependencies/glad/include)
endif() endif()
set(GLAD_SPEC "gl" CACHE INTERNAL "" FORCE)
set(BUILD_SHARED_LIBS OFF)
add_subdirectory(dependencies/glad)
target_link_libraries(${PROJECT_NAME} PUBLIC glad)
target_include_directories(${PROJECT_NAME} PUBLIC dependencies/glad/include)
# Volk if(ENGINE_BUILD_VULKAN)
set(VOLK_STATIC_DEFINES "")
set(VOLK_PULL_IN_VULKAN ON)
set(VOLK_INSTALL OFF)
set(VOLK_HEADERS_ONLY ON)
add_subdirectory(dependencies/volk)
target_link_libraries(${PROJECT_NAME} PRIVATE volk_headers)
# Vulkan Memory Allocator # Volk
target_include_directories(${PROJECT_NAME} PRIVATE dependencies/VulkanMemoryAllocator/include) set(VOLK_STATIC_DEFINES "")
set(VOLK_PULL_IN_VULKAN ON)
set(VOLK_INSTALL OFF)
set(VOLK_HEADERS_ONLY ON)
add_subdirectory(dependencies/volk)
target_link_libraries(${PROJECT_NAME} PRIVATE volk_headers)
# Vulkan Memory Allocator
target_include_directories(${PROJECT_NAME} PRIVATE dependencies/VulkanMemoryAllocator/include)
endif()
# SDL2: # SDL2:
find_package(SDL2) find_package(SDL2)

View File

@ -6,7 +6,6 @@
#include "resources/shader.hpp" #include "resources/shader.hpp"
#include "resources/mesh.hpp" #include "resources/mesh.hpp"
#include "resources/texture.hpp"
#include <vector> #include <vector>
#include <string> #include <string>
@ -27,7 +26,7 @@ public:
void setTexture(const std::string& name); void setTexture(const std::string& name);
std::shared_ptr<resources::Mesh> m_mesh = nullptr; std::shared_ptr<resources::Mesh> m_mesh = nullptr;
std::shared_ptr<resources::Texture> m_texture; // std::shared_ptr<resources::Texture> m_texture;
glm::vec3 m_color = { 1.0f, 1.0f, 1.0f }; glm::vec3 m_color = { 1.0f, 1.0f, 1.0f };
glm::vec3 m_emission = { 0.0f, 0.0f, 0.0f }; glm::vec3 m_emission = { 0.0f, 0.0f, 0.0f };

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#if 0
#include "engine_api.h" #include "engine_api.h"
#include "component.hpp" #include "component.hpp"
@ -39,3 +41,4 @@ private:
}; };
} }
#endif

View File

@ -1,11 +1,11 @@
#pragma once #pragma once
#if 0
#include "engine_api.h" #include "engine_api.h"
#include "resource.hpp" #include "resource.hpp"
#include <glad/glad.h>
#include <glm/vec2.hpp> #include <glm/vec2.hpp>
#include <map> #include <map>
@ -33,3 +33,4 @@ public:
}; };
} }
#endif

View File

@ -1,10 +1,11 @@
#pragma once #pragma once
#if 0
#include "engine_api.h" #include "engine_api.h"
#include "resource.hpp" #include "resource.hpp"
#include <glad/glad.h>
namespace engine::resources { namespace engine::resources {
@ -12,11 +13,6 @@ class ENGINE_API Texture : public Resource {
private: private:
static GLuint s_binded_texture;
GLuint m_texture;
public: public:
Texture(const std::filesystem::path& resPath); Texture(const std::filesystem::path& resPath);
~Texture() override; ~Texture() override;
@ -27,3 +23,5 @@ public:
}; };
} }
#endif

View File

@ -1,3 +1,5 @@
#if 0
#include "components/text_ui_renderer.hpp" #include "components/text_ui_renderer.hpp"
#include "object.hpp" #include "object.hpp"
@ -12,8 +14,8 @@ UI::UI(Object* parent) : Component(parent, TypeEnum::UI)
m_font = parent->res.get<resources::Font>(FONTFILE); m_font = parent->res.get<resources::Font>(FONTFILE);
m_shader = parent->res.get<resources::Shader>("shaders/font.glsl"); m_shader = parent->res.get<resources::Shader>("shaders/font.glsl");
glEnable(GL_BLEND); // glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
} }
UI::~UI() UI::~UI()
@ -70,7 +72,7 @@ void UI::render(glm::mat4 transform)
{{xpos + w, ypos + h, 0.0f}, {}, {1.0f, 0.0f}}, {{xpos + w, ypos + h, 0.0f}, {}, {1.0f, 0.0f}},
});*/ });*/
glBindTexture(GL_TEXTURE_2D, glyph.textureID); // glBindTexture(GL_TEXTURE_2D, glyph.textureID);
// mesh.drawMesh(*m_shader); // mesh.drawMesh(*m_shader);
@ -83,3 +85,5 @@ void UI::render(glm::mat4 transform)
} }
} }
#endif

View File

@ -18,6 +18,8 @@
#define VMA_IMPLEMENTATION #define VMA_IMPLEMENTATION
#include <vk_mem_alloc.h> #include <vk_mem_alloc.h>
#include <shaderc/shaderc.hpp>
#include <SDL_vulkan.h> #include <SDL_vulkan.h>
#include <assert.h> #include <assert.h>
@ -145,6 +147,48 @@ namespace engine {
// functions // functions
static VkShaderModule compileShader(VkDevice device, shaderc_shader_kind kind, const std::string& source, const char* filename)
{
shaderc::Compiler compiler;
shaderc::CompileOptions options;
options.SetOptimizationLevel(shaderc_optimization_level_performance);
// preprocess
shaderc::PreprocessedSourceCompilationResult preprocessed = compiler.PreprocessGlsl(source, kind, filename, options);
if (preprocessed.GetCompilationStatus() != shaderc_compilation_status_success)
{
throw std::runtime_error(preprocessed.GetErrorMessage());
}
std::string shaderStr = { preprocessed.cbegin(), preprocessed.cend() };
// compile
shaderc::SpvCompilationResult module = compiler.CompileGlslToSpv(shaderStr, kind, filename, options);
if (module.GetCompilationStatus() != shaderc_compilation_status_success)
{
throw std::runtime_error(module.GetErrorMessage());
}
std::vector<uint32_t> shaderBytecode = { module.cbegin(), module.cend() };// not sure why sample code copy vector like this
VkShaderModuleCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
createInfo.codeSize = shaderBytecode.size();
createInfo.pCode = shaderBytecode.data();
VkShaderModule shaderModule;
if (vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
throw std::runtime_error("failed to create shader module!");
}
return shaderModule;
}
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);
@ -630,21 +674,6 @@ namespace engine {
} }
static VkShaderModule createShaderModule(VkDevice device, const std::vector<char>& code)
{
VkShaderModuleCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
createInfo.codeSize = code.size();
createInfo.pCode = reinterpret_cast<const uint32_t*>(code.data());
VkShaderModule shaderModule;
if (vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
throw std::runtime_error("failed to create shader module!");
}
return shaderModule;
}
static void copyBuffer(VkDevice device, VkCommandPool commandPool, VkQueue queue, VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size) static void copyBuffer(VkDevice device, VkCommandPool commandPool, VkQueue queue, VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size)
{ {
VkResult res; VkResult res;
@ -1135,9 +1164,6 @@ namespace engine {
descriptorSetLayoutInfo.pBindings = &pimpl->uboLayoutBinding; descriptorSetLayoutInfo.pBindings = &pimpl->uboLayoutBinding;
res = vkCreateDescriptorSetLayout(pimpl->device, &descriptorSetLayoutInfo, nullptr, &pimpl->uboLayout); res = vkCreateDescriptorSetLayout(pimpl->device, &descriptorSetLayoutInfo, nullptr, &pimpl->uboLayout);
assert(res == VK_SUCCESS); assert(res == VK_SUCCESS);
} }
GFXDevice::~GFXDevice() GFXDevice::~GFXDevice()
@ -1426,8 +1452,8 @@ namespace engine {
auto fragShaderCode = readFile(fragShaderPath); auto fragShaderCode = readFile(fragShaderPath);
INFO("Opened shader: {}", std::filesystem::path(vertShaderPath).filename().string()); INFO("Opened shader: {}", std::filesystem::path(vertShaderPath).filename().string());
VkShaderModule vertShaderModule = createShaderModule(pimpl->device, vertShaderCode); VkShaderModule vertShaderModule = compileShader(pimpl->device, shaderc_vertex_shader, vertShaderCode.data(), vertShaderPath);
VkShaderModule fragShaderModule = createShaderModule(pimpl->device, fragShaderCode); VkShaderModule fragShaderModule = compileShader(pimpl->device, shaderc_fragment_shader, fragShaderCode.data(), fragShaderPath);
VkPipelineShaderStageCreateInfo vertShaderStageInfo{ VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO }; VkPipelineShaderStageCreateInfo vertShaderStageInfo{ VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO };
vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT; vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;

View File

@ -123,7 +123,8 @@ namespace engine {
compList.renderers.emplace_back(dynamic_cast<Renderer*>(comp), newTransform); compList.renderers.emplace_back(dynamic_cast<Renderer*>(comp), newTransform);
break; break;
case Component::TypeEnum::UI: case Component::TypeEnum::UI:
compList.uis.emplace_back(dynamic_cast<UI*>(comp), newTransform); // compList.uis.emplace_back(dynamic_cast<UI*>(comp), newTransform);
throw std::runtime_error("UI is currently not supported");
break; break;
case Component::TypeEnum::CUSTOM: case Component::TypeEnum::CUSTOM:
compList.customs.emplace_back(dynamic_cast<CustomComponent*>(comp), newTransform); compList.customs.emplace_back(dynamic_cast<CustomComponent*>(comp), newTransform);

View File

@ -1,3 +1,4 @@
#if 0
#include "resources/font.hpp" #include "resources/font.hpp"
#include <ft2build.h> #include <ft2build.h>
@ -94,3 +95,4 @@ Font::Character Font::getChar(char c)
} }
} }
#endif

View File

@ -35,8 +35,8 @@ Shader::Shader(const std::filesystem::path& resPath) : Resource(resPath, "shader
vertexFormat.attributeDescriptions.emplace_back(1, gfx::VertexAttribFormat::VEC3, sizeof(glm::vec3)); // norm vertexFormat.attributeDescriptions.emplace_back(1, gfx::VertexAttribFormat::VEC3, sizeof(glm::vec3)); // norm
vertexFormat.attributeDescriptions.emplace_back(2, gfx::VertexAttribFormat::VEC2, sizeof(glm::vec3) + sizeof(glm::vec3)); // uv vertexFormat.attributeDescriptions.emplace_back(2, gfx::VertexAttribFormat::VEC2, sizeof(glm::vec3) + sizeof(glm::vec3)); // uv
const std::string vertexShaderPath = (resPath.parent_path()/std::filesystem::path(resPath.stem().string() + ".vert.spv")).string(); const std::string vertexShaderPath = (resPath.parent_path()/std::filesystem::path(resPath.stem().string() + ".vert")).string();
const std::string fragmentShaderPath = (resPath.parent_path()/std::filesystem::path(resPath.stem().string() + ".frag.spv")).string(); const std::string fragmentShaderPath = (resPath.parent_path()/std::filesystem::path(resPath.stem().string() + ".frag")).string();
m_pipeline = gfxdev->createPipeline(vertexShaderPath.c_str(), fragmentShaderPath.c_str(), vertexFormat, sizeof(UniformBuffer)); m_pipeline = gfxdev->createPipeline(vertexShaderPath.c_str(), fragmentShaderPath.c_str(), vertexFormat, sizeof(UniformBuffer));

View File

@ -1,3 +1,5 @@
#if 0
#include "resources/texture.hpp" #include "resources/texture.hpp"
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
@ -129,3 +131,4 @@ void Texture::bindTexture() const
} }
} }
#endif

View File

@ -62,9 +62,10 @@ namespace engine {
} }
} }
for (const auto& [c, t] : compList.uis) { /* for (const auto& [c, t] : compList.uis) {
c->render(t); c->render(t);
} }
*/
} }

Binary file not shown.

Binary file not shown.