code review changes and fixes

This commit is contained in:
ohyzha
2024-08-02 15:27:36 +03:00
parent e69a553b18
commit be549dccf6
18 changed files with 86 additions and 51 deletions

View File

@@ -1,11 +1,5 @@
include(FetchContent) include(FetchContent)
unset(ASSIMP_FOUND)
find_package(ASSIMP QUIET)
if (NOT ASSIMP_FOUND)
message(FATAL_ERROR "Assimp package is required to build msdfgen")
endif()
if(NOT DEFINED MSDFGEN_REPO) if(NOT DEFINED MSDFGEN_REPO)
set(MSDFGEN_REPO https://github.com/Chlumsky/msdfgen.git) set(MSDFGEN_REPO https://github.com/Chlumsky/msdfgen.git)
endif () endif ()

View File

@@ -8,6 +8,7 @@ include(cmake/SetupVulkan.cmake)
include(cmake/Filter.cmake) include(cmake/Filter.cmake)
include(cmake/AppleHelper.cmake) include(cmake/AppleHelper.cmake)
include(cmake/SetShaderDependency.cmake) include(cmake/SetShaderDependency.cmake)
include(cmake/CopyResources.cmake)
set(DEPENDENCY_MIRROR_FILE "DependencyMirrors.txt" CACHE STRING "Dependency mirror") set(DEPENDENCY_MIRROR_FILE "DependencyMirrors.txt" CACHE STRING "Dependency mirror")
VarsFromFile("${DEPENDENCY_MIRROR_FILE}") # Load mirror list (for CICD) VarsFromFile("${DEPENDENCY_MIRROR_FILE}") # Load mirror list (for CICD)
@@ -107,6 +108,7 @@ if (WIN32)
endif () endif ()
SetupVulkan(openVulkanoCpp) SetupVulkan(openVulkanoCpp)
CopyResources(openVulkanoCpp "${CMAKE_CURRENT_SOURCE_DIR}/fonts" ".ttf")
SetShaderDependency(openVulkanoCpp SetShaderDependency(openVulkanoCpp
${CMAKE_CURRENT_SOURCE_DIR}/openVulkanoCpp/Shader ${CMAKE_CURRENT_SOURCE_DIR}/openVulkanoCpp/Shader
${SHADER_OUTPUT_DEST}) ${SHADER_OUTPUT_DEST})

23
cmake/CopyResources.cmake Normal file
View File

@@ -0,0 +1,23 @@
function(CopyResources TARGET FROM EXTENSIONS)
file(GLOB RESOURCES "${FROM}/*")
set(RESOURCES_TO_COPY "")
if (${EXTENSIONS} STREQUAL "*")
set(RESOURCES_TO_COPY ${RESOURCES})
else()
foreach(RESOURCE ${RESOURCES})
get_filename_component(EXT "${RESOURCE}" EXT)
list(FIND EXTENSIONS ${EXT} EXT_FOUND)
if(NOT EXT_FOUND EQUAL -1)
list(APPEND RESOURCES_TO_COPY "${RESOURCE}")
endif()
endforeach()
endif()
foreach(RESOURCE ${RESOURCES_TO_COPY})
add_custom_command(TARGET ${TARGET} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${RESOURCE} $<TARGET_FILE_DIR:${TARGET}>
)
endforeach()
endfunction()

View File

@@ -18,11 +18,13 @@
#include "Input/InputManager.hpp" #include "Input/InputManager.hpp"
#include "Host/GraphicsAppManager.hpp" #include "Host/GraphicsAppManager.hpp"
#include "Host/GLFW/WindowGLFW.hpp" #include "Host/GLFW/WindowGLFW.hpp"
#include "Host/ResourceLoader.hpp"
#include "Math/Math.hpp" #include "Math/Math.hpp"
#include "Base/EngineConfiguration.hpp" #include "Base/EngineConfiguration.hpp"
#include "Controller/FreeCamCameraController.hpp" #include "Controller/FreeCamCameraController.hpp"
#include "Image/ImageLoaderPng.hpp" #include "Image/ImageLoaderPng.hpp"
#include "Scene/FontAtlasGenerator.hpp" #include "Scene/FontAtlasGenerator.hpp"
#include <filesystem>
#ifdef _WIN32 #ifdef _WIN32
#undef TRANSPARENT #undef TRANSPARENT
@@ -33,6 +35,7 @@ namespace OpenVulkano
using namespace Scene; using namespace Scene;
using namespace Input; using namespace Input;
using namespace Math; using namespace Math;
namespace fs = std::filesystem;
class TextExampleAppImpl final : public TextExampleApp class TextExampleAppImpl final : public TextExampleApp
{ {
@@ -49,14 +52,26 @@ namespace OpenVulkano
m_cam.Init(70, 16, 9, 0.1f, 100); m_cam.Init(70, 16, 9, 0.1f, 100);
m_scene.SetCamera(&m_cam); m_scene.SetCamera(&m_cam);
m_cfg.applyBorder = true; static std::vector<std::pair<std::string, TextConfig>> texts;
texts.push_back(std::make_pair("ABab?.^{}_cdFGETG123)(", TextConfig()));
texts.push_back(std::make_pair("Hello, World!", TextConfig()));
texts[0].second.applyBorder = true;
texts[1].second.backgroundColor.a = 1;
const std::array texts = { "ABab?.^{}_cdFGETG123)(", "Hello, World!" };
const int N = texts.size(); const int N = texts.size();
const std::string font = (OpenVulkano::Utils::GetFontsDirectory() / "Roboto-Regular.ttf").string(); std::string fontPath;
m_uniBuffer.Init(sizeof(TextConfig), &m_cfg, 3); auto& resourceLoader = ResourceLoader::GetInstance();
resourceLoader.GetResource("Roboto-Regular.ttf", fontPath);
const std::string atlasPath = (fs::path(fontPath).parent_path() / "roboto-regular-atlas.png").string();
m_nodesPool.resize(N); m_nodesPool.resize(N);
m_drawablesPool.resize(N); m_drawablesPool.resize(N);
m_uniBuffers.resize(N);
for (int i = 0; i < N; i++)
{
m_uniBuffers[i].Init(sizeof(TextConfig), &texts[i].second, 3);
}
m_shader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/text"); m_shader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/text");
m_shader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT, "Shader/text"); m_shader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT, "Shader/text");
@@ -66,16 +81,16 @@ namespace OpenVulkano
m_shader.alphaBlend = true; m_shader.alphaBlend = true;
m_shader.cullMode = CullMode::NONE; m_shader.cullMode = CullMode::NONE;
m_atlasGenerator.GenerateAtlas(font, (OpenVulkano::Utils::GetBuildDirectory() / "atlas.png").string()); m_atlasGenerator.GenerateAtlas(fontPath, atlasPath);
for (int i = 0; i < texts.size(); i++) for (int i = 0; i < texts.size(); i++)
{ {
Text* t = new Text(); Text* t = new Text();
t->SetFontAtlasGenerator(&m_atlasGenerator); t->SetFontAtlasGenerator(&m_atlasGenerator);
t->SetConfig(m_cfg); t->SetConfig(texts[i].second);
t->SetUniformBuffer(&m_uniBuffer); t->SetUniformBuffer(&m_uniBuffers[i]);
t->SetShader(&m_shader); t->SetShader(&m_shader);
t->GenerateText(texts[i]); t->GenerateText(texts[i].first);
m_drawablesPool[i] = t; m_drawablesPool[i] = t;
m_nodesPool[i].Init(); m_nodesPool[i].Init();
m_nodesPool[i].SetMatrix( m_nodesPool[i].SetMatrix(
@@ -111,10 +126,9 @@ namespace OpenVulkano
private: private:
OpenVulkano::Scene::Scene m_scene; OpenVulkano::Scene::Scene m_scene;
PerspectiveCamera m_cam; PerspectiveCamera m_cam;
UniformBuffer m_uniBuffer; std::vector<UniformBuffer> m_uniBuffers;
OpenVulkano::FreeCamCameraController m_camController; OpenVulkano::FreeCamCameraController m_camController;
Shader m_shader; Shader m_shader;
TextConfig m_cfg;
FontAtlasGenerator m_atlasGenerator; FontAtlasGenerator m_atlasGenerator;
std::vector<SimpleDrawable*> m_drawablesPool; std::vector<SimpleDrawable*> m_drawablesPool;
std::vector<Node> m_nodesPool; std::vector<Node> m_nodesPool;

View File

@@ -40,8 +40,9 @@ namespace OpenVulkano
} }
} }
Array<char> ResourceLoaderAppDirLinux::GetResource(const std::string& resourceName) Array<char> ResourceLoaderAppDirLinux::GetResource(const std::string& resourceName, std::string& resourcePath)
{ {
return Utils::ReadFile(GetAppDir() + resourceName, true); resourcePath = GetAppDir() + resourceName;
return Utils::ReadFile(resourcePath, true);
} }
} }

View File

@@ -13,6 +13,6 @@ namespace OpenVulkano
class ResourceLoaderAppDirLinux final : public ResourceLoader class ResourceLoaderAppDirLinux final : public ResourceLoader
{ {
public: public:
Array<char> GetResource(const std::string& resourceName) override; Array<char> GetResource(const std::string& resourceName, std::string& resourcePath) override;
}; };
} }

View File

@@ -15,13 +15,13 @@ namespace OpenVulkano
std::vector<std::unique_ptr<ResourceLoader>> m_loaders; std::vector<std::unique_ptr<ResourceLoader>> m_loaders;
public: public:
Array<char> GetResource(const std::string& resourceName) override Array<char> GetResource(const std::string& resourceName, std::string& resourcePath) override
{ {
for(auto& loader : m_loaders) for(auto& loader : m_loaders)
{ {
try try
{ {
auto res = loader->GetResource(resourceName); auto res = loader->GetResource(resourceName, resourcePath);
if (!res.Empty()) return res; if (!res.Empty()) return res;
} }
catch (const std::exception& e) catch (const std::exception& e)

View File

@@ -15,7 +15,7 @@ namespace OpenVulkano
public: public:
virtual ~ResourceLoader() = default; virtual ~ResourceLoader() = default;
virtual Array<char> GetResource(const std::string& resourceName) = 0; virtual Array<char> GetResource(const std::string& resourceName, std::string& resourcePath) = 0;
static ResourceLoader& GetInstance(); static ResourceLoader& GetInstance();

View File

@@ -90,11 +90,12 @@ namespace OpenVulkano
return Array<char>(buffer); return Array<char>(buffer);
} }
Array<char> WebResourceLoader::GetResource(const std::string& resourceName) Array<char> WebResourceLoader::GetResource(const std::string& resourceName, std::string& resourcePath)
{ {
if (IsUrl(resourceName)) if (IsUrl(resourceName))
{ {
std::filesystem::path cacheFilePath = GetCacheFilePath(resourceName); std::filesystem::path cacheFilePath = GetCacheFilePath(resourceName);
resourcePath = resourceName;
if (std::filesystem::exists(cacheFilePath)) { return Utils::ReadFile(cacheFilePath.string()); } if (std::filesystem::exists(cacheFilePath)) { return Utils::ReadFile(cacheFilePath.string()); }
else { return DownloadResource(resourceName); } else { return DownloadResource(resourceName); }
} }

View File

@@ -24,6 +24,6 @@ namespace OpenVulkano
WebResourceLoader(); WebResourceLoader();
~WebResourceLoader(); ~WebResourceLoader();
Array<char> GetResource(const std::string& resourceName) override; Array<char> GetResource(const std::string& resourceName, std::string& resourcePath) override;
}; };
} }

View File

@@ -38,8 +38,9 @@ namespace OpenVulkano
} }
} }
Array<char> ResourceLoaderAppDirWindows::GetResource(const std::string& resourceName) Array<char> ResourceLoaderAppDirWindows::GetResource(const std::string& resourceName, std::string& resourcePath)
{ {
return Utils::ReadFile(GetAppDir() + resourceName); resourcePath = GetAppDir() + resourceName;
return Utils::ReadFile(resourcePath);
} }
} }

View File

@@ -13,6 +13,6 @@ namespace OpenVulkano
class ResourceLoaderAppDirWindows final : public ResourceLoader class ResourceLoaderAppDirWindows final : public ResourceLoader
{ {
public: public:
Array<char> GetResource(const std::string& resourceName) override; Array<char> GetResource(const std::string& resourceName, std::string& resourcePath) override;
}; };
} }

View File

@@ -5,6 +5,7 @@
*/ */
#include "ImageLoaderJpeg.hpp" #include "ImageLoaderJpeg.hpp"
#include "Base/Utils.hpp"
#include <Data/Containers/Array.hpp> #include <Data/Containers/Array.hpp>
#include <fstream> #include <fstream>
#include <cstring> #include <cstring>
@@ -20,10 +21,8 @@ namespace OpenVulkano::Image
{ {
std::unique_ptr<Image> ImageLoaderJpeg::loadFromFile(const std::string& filePath) std::unique_ptr<Image> ImageLoaderJpeg::loadFromFile(const std::string& filePath)
{ {
std::ifstream file(filePath, std::ios::binary); Array<char> buffer = OpenVulkano::Utils::ReadFile(filePath);
if (!file) { throw std::runtime_error("Could not open file: " + filePath); } return loadJpeg(reinterpret_cast<uint8_t*>(buffer.Data()), buffer.Size());
std::vector<uint8_t> buffer((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
return loadJpeg(buffer.data(), buffer.size());
} }
std::unique_ptr<Image> ImageLoaderJpeg::loadFromMemory(const std::vector<uint8_t>& buffer) std::unique_ptr<Image> ImageLoaderJpeg::loadFromMemory(const std::vector<uint8_t>& buffer)
@@ -59,7 +58,7 @@ namespace OpenVulkano::Image
} }
#else #else
{ {
return loadData(data, static_cast<int>(size), 3); return loadData(data, static_cast<int>(size));
} }
#endif #endif
} }

View File

@@ -5,6 +5,7 @@
*/ */
#include "ImageLoaderPng.hpp" #include "ImageLoaderPng.hpp"
#include "Base/Utils.hpp"
#include <Data/Containers/Array.hpp> #include <Data/Containers/Array.hpp>
#include <fstream> #include <fstream>
#include <cstring> #include <cstring>
@@ -13,10 +14,8 @@ namespace OpenVulkano::Image
{ {
std::unique_ptr<Image> ImageLoaderPng::loadFromFile(const std::string& filePath) std::unique_ptr<Image> ImageLoaderPng::loadFromFile(const std::string& filePath)
{ {
std::ifstream file(filePath, std::ios::binary); Array<char> buffer = OpenVulkano::Utils::ReadFile(filePath);
if (!file) { throw std::runtime_error("Could not open file: " + filePath); } return loadData(reinterpret_cast<uint8_t*>(buffer.Data()), static_cast<int>(buffer.Size()));
std::vector<uint8_t> buffer((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
return loadData(buffer.data(), static_cast<int>(buffer.size()));
} }
std::unique_ptr<Image> ImageLoaderPng::loadFromMemory(const std::vector<uint8_t>& buffer) std::unique_ptr<Image> ImageLoaderPng::loadFromMemory(const std::vector<uint8_t>& buffer)

View File

@@ -32,7 +32,7 @@ namespace OpenVulkano::Scene
Scene* m_scene = nullptr; Scene* m_scene = nullptr;
Shader* m_shader = nullptr; Shader* m_shader = nullptr;
const DrawEncoder m_encoder; const DrawEncoder m_encoder;
DrawPhase m_drawPhase; const DrawPhase m_drawPhase;
public: public:
explicit Drawable(const DrawEncoder& encoder, explicit Drawable(const DrawEncoder& encoder,
@@ -45,8 +45,6 @@ namespace OpenVulkano::Scene
void SetShader(Shader* shader) { m_shader = shader; } void SetShader(Shader* shader) { m_shader = shader; }
void SetDrawPhase(DrawPhase phase) { m_drawPhase = phase; }
[[nodiscard]] Scene* GetScene() const { return m_scene; } [[nodiscard]] Scene* GetScene() const { return m_scene; }
[[nodiscard]] const auto& GetNodes() const { return m_nodes; } [[nodiscard]] const auto& GetNodes() const { return m_nodes; }

View File

@@ -17,10 +17,6 @@
#endif #endif
#include <stdexcept> #include <stdexcept>
#include "msdfgen.h"
#include "msdfgen-ext.h"
#include "msdf-atlas-gen/msdf-atlas-gen.h"
namespace OpenVulkano::Scene namespace OpenVulkano::Scene
{ {
Geometry::Geometry(const Geometry& other) Geometry::Geometry(const Geometry& other)

View File

@@ -24,12 +24,13 @@ namespace OpenVulkano::Scene
struct TextConfig struct TextConfig
{ {
Math::Vector4f textColor = { 1, 1, 1, 0 }; // vec4 to match paddding (multiple of 16) Math::Vector4f textColor = { 1, 1, 1, 1 };
Math::Vector3f borderColor = { 1, 0, 0 }; Math::Vector4f borderColor = { 1, 0, 0, 1 };
Math::Vector4f backgroundColor = { 0, 1, 0, 0 };
float threshold = 0.4f; float threshold = 0.4f;
float borderSize = 0.05f; float borderSize = 0.05f;
float smoothing = 1.f/32.f; float smoothing = 1.f/32.f;
bool applyBorder = false; uint32_t applyBorder = false;
//bool sdfMultiChannel = false; //bool sdfMultiChannel = false;
}; };

View File

@@ -8,8 +8,9 @@ layout(set = 2, binding = 0) uniform sampler2D texSampler;
layout(set = 3, binding = 0) uniform TextConfig layout(set = 3, binding = 0) uniform TextConfig
{ {
vec3 textColor; vec4 textColor;
vec3 borderColor; vec4 borderColor;
vec4 backgroundColor;
float threshold; float threshold;
float borderSize; float borderSize;
float smoothing; float smoothing;
@@ -24,10 +25,15 @@ void main()
{ {
float border = smoothstep(textConfig.threshold + textConfig.borderSize - textConfig.smoothing, float border = smoothstep(textConfig.threshold + textConfig.borderSize - textConfig.smoothing,
textConfig.threshold + textConfig.borderSize + textConfig.smoothing, distance); textConfig.threshold + textConfig.borderSize + textConfig.smoothing, distance);
outColor = vec4(mix(textConfig.borderColor, textConfig.textColor, border), 1) * alpha; outColor = mix(textConfig.borderColor, textConfig.textColor, border) * alpha;
} }
else else
{ {
outColor = vec4(textConfig.textColor, 1) * alpha; outColor = vec4(textConfig.textColor) * alpha;
}
if (textConfig.backgroundColor.a != 0)
{
outColor = mix(textConfig.backgroundColor, outColor, alpha);
} }
} }