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)
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)
set(MSDFGEN_REPO https://github.com/Chlumsky/msdfgen.git)
endif ()

View File

@@ -8,6 +8,7 @@ include(cmake/SetupVulkan.cmake)
include(cmake/Filter.cmake)
include(cmake/AppleHelper.cmake)
include(cmake/SetShaderDependency.cmake)
include(cmake/CopyResources.cmake)
set(DEPENDENCY_MIRROR_FILE "DependencyMirrors.txt" CACHE STRING "Dependency mirror")
VarsFromFile("${DEPENDENCY_MIRROR_FILE}") # Load mirror list (for CICD)
@@ -107,6 +108,7 @@ if (WIN32)
endif ()
SetupVulkan(openVulkanoCpp)
CopyResources(openVulkanoCpp "${CMAKE_CURRENT_SOURCE_DIR}/fonts" ".ttf")
SetShaderDependency(openVulkanoCpp
${CMAKE_CURRENT_SOURCE_DIR}/openVulkanoCpp/Shader
${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 "Host/GraphicsAppManager.hpp"
#include "Host/GLFW/WindowGLFW.hpp"
#include "Host/ResourceLoader.hpp"
#include "Math/Math.hpp"
#include "Base/EngineConfiguration.hpp"
#include "Controller/FreeCamCameraController.hpp"
#include "Image/ImageLoaderPng.hpp"
#include "Scene/FontAtlasGenerator.hpp"
#include <filesystem>
#ifdef _WIN32
#undef TRANSPARENT
@@ -33,6 +35,7 @@ namespace OpenVulkano
using namespace Scene;
using namespace Input;
using namespace Math;
namespace fs = std::filesystem;
class TextExampleAppImpl final : public TextExampleApp
{
@@ -49,14 +52,26 @@ namespace OpenVulkano
m_cam.Init(70, 16, 9, 0.1f, 100);
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 std::string font = (OpenVulkano::Utils::GetFontsDirectory() / "Roboto-Regular.ttf").string();
m_uniBuffer.Init(sizeof(TextConfig), &m_cfg, 3);
std::string fontPath;
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_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::FRAGMENT, "Shader/text");
@@ -66,16 +81,16 @@ namespace OpenVulkano
m_shader.alphaBlend = true;
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++)
{
Text* t = new Text();
t->SetFontAtlasGenerator(&m_atlasGenerator);
t->SetConfig(m_cfg);
t->SetUniformBuffer(&m_uniBuffer);
t->SetConfig(texts[i].second);
t->SetUniformBuffer(&m_uniBuffers[i]);
t->SetShader(&m_shader);
t->GenerateText(texts[i]);
t->GenerateText(texts[i].first);
m_drawablesPool[i] = t;
m_nodesPool[i].Init();
m_nodesPool[i].SetMatrix(
@@ -111,10 +126,9 @@ namespace OpenVulkano
private:
OpenVulkano::Scene::Scene m_scene;
PerspectiveCamera m_cam;
UniformBuffer m_uniBuffer;
std::vector<UniformBuffer> m_uniBuffers;
OpenVulkano::FreeCamCameraController m_camController;
Shader m_shader;
TextConfig m_cfg;
FontAtlasGenerator m_atlasGenerator;
std::vector<SimpleDrawable*> m_drawablesPool;
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
{
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;
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)
{
try
{
auto res = loader->GetResource(resourceName);
auto res = loader->GetResource(resourceName, resourcePath);
if (!res.Empty()) return res;
}
catch (const std::exception& e)

View File

@@ -15,7 +15,7 @@ namespace OpenVulkano
public:
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();

View File

@@ -90,11 +90,12 @@ namespace OpenVulkano
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))
{
std::filesystem::path cacheFilePath = GetCacheFilePath(resourceName);
resourcePath = resourceName;
if (std::filesystem::exists(cacheFilePath)) { return Utils::ReadFile(cacheFilePath.string()); }
else { return DownloadResource(resourceName); }
}

View File

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

View File

@@ -5,6 +5,7 @@
*/
#include "ImageLoaderPng.hpp"
#include "Base/Utils.hpp"
#include <Data/Containers/Array.hpp>
#include <fstream>
#include <cstring>
@@ -13,10 +14,8 @@ namespace OpenVulkano::Image
{
std::unique_ptr<Image> ImageLoaderPng::loadFromFile(const std::string& filePath)
{
std::ifstream file(filePath, std::ios::binary);
if (!file) { throw std::runtime_error("Could not open file: " + filePath); }
std::vector<uint8_t> buffer((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
return loadData(buffer.data(), static_cast<int>(buffer.size()));
Array<char> buffer = OpenVulkano::Utils::ReadFile(filePath);
return loadData(reinterpret_cast<uint8_t*>(buffer.Data()), static_cast<int>(buffer.Size()));
}
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;
Shader* m_shader = nullptr;
const DrawEncoder m_encoder;
DrawPhase m_drawPhase;
const DrawPhase m_drawPhase;
public:
explicit Drawable(const DrawEncoder& encoder,
@@ -45,8 +45,6 @@ namespace OpenVulkano::Scene
void SetShader(Shader* shader) { m_shader = shader; }
void SetDrawPhase(DrawPhase phase) { m_drawPhase = phase; }
[[nodiscard]] Scene* GetScene() const { return m_scene; }
[[nodiscard]] const auto& GetNodes() const { return m_nodes; }

View File

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

View File

@@ -24,12 +24,13 @@ namespace OpenVulkano::Scene
struct TextConfig
{
Math::Vector4f textColor = { 1, 1, 1, 0 }; // vec4 to match paddding (multiple of 16)
Math::Vector3f borderColor = { 1, 0, 0 };
Math::Vector4f textColor = { 1, 1, 1, 1 };
Math::Vector4f borderColor = { 1, 0, 0, 1 };
Math::Vector4f backgroundColor = { 0, 1, 0, 0 };
float threshold = 0.4f;
float borderSize = 0.05f;
float smoothing = 1.f/32.f;
bool applyBorder = false;
uint32_t applyBorder = 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
{
vec3 textColor;
vec3 borderColor;
vec4 textColor;
vec4 borderColor;
vec4 backgroundColor;
float threshold;
float borderSize;
float smoothing;
@@ -24,10 +25,15 @@ void main()
{
float border = smoothstep(textConfig.threshold + textConfig.borderSize - textConfig.smoothing,
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
{
outColor = vec4(textConfig.textColor, 1) * alpha;
outColor = vec4(textConfig.textColor) * alpha;
}
if (textConfig.backgroundColor.a != 0)
{
outColor = mix(textConfig.backgroundColor, outColor, alpha);
}
}