remove freetype, begin font code

This commit is contained in:
Bailey Harrison 2023-05-14 14:40:16 +01:00
parent 99215c9919
commit 83e0935b15
11 changed files with 189 additions and 120 deletions

3
.gitmodules vendored
View File

@ -10,9 +10,6 @@
[submodule "dependencies/spdlog"] [submodule "dependencies/spdlog"]
path = dependencies/spdlog path = dependencies/spdlog
url = https://github.com/gabime/spdlog url = https://github.com/gabime/spdlog
[submodule "dependencies/freetype"]
path = dependencies/freetype
url = https://gitlab.freedesktop.org/freetype/freetype.git
[submodule "dependencies/volk"] [submodule "dependencies/volk"]
path = dependencies/volk path = dependencies/volk
url = https://github.com/zeux/volk url = https://github.com/zeux/volk

View File

@ -16,6 +16,8 @@ set(SRC_FILES
"src/gfx_device_vulkan.cpp" "src/gfx_device_vulkan.cpp"
"src/input_manager.cpp" "src/input_manager.cpp"
"src/libs/stb_image.cpp" "src/libs/stb_image.cpp"
"src/libs/stb_truetype.cpp"
"src/resources/font.cpp"
"src/resources/material.cpp" "src/resources/material.cpp"
"src/resources/mesh.cpp" "src/resources/mesh.cpp"
"src/resources/shader.cpp" "src/resources/shader.cpp"
@ -43,6 +45,7 @@ set(SRC_FILES
set(INCLUDE_FILES set(INCLUDE_FILES
"include/application.h" "include/application.h"
"include/components/collider.h" "include/components/collider.h"
"include/components/renderable_text.h"
"include/components/renderable.h" "include/components/renderable.h"
"include/components/transform.h" "include/components/transform.h"
"include/components/ui_element.h" "include/components/ui_element.h"
@ -57,6 +60,7 @@ set(INCLUDE_FILES
"include/log.h" "include/log.h"
"include/logger.h" "include/logger.h"
"include/resource_manager.h" "include/resource_manager.h"
"include/resources/font.h"
"include/resources/material.h" "include/resources/material.h"
"include/resources/mesh.h" "include/resources/mesh.h"
"include/resources/shader.h" "include/resources/shader.h"
@ -143,17 +147,6 @@ else()
target_link_libraries(${PROJECT_NAME} PRIVATE shaderc_shared) target_link_libraries(${PROJECT_NAME} PRIVATE shaderc_shared)
endif() endif()
# freetype
set(FT_DISABLE_ZLIB TRUE CACHE INTERNAL "" FORCE)
set(FT_DISABLE_BZIP2 TRUE CACHE INTERNAL "" FORCE)
set(FT_DISABLE_PNG TRUE CACHE INTERNAL "" FORCE)
set(FT_DISABLE_HARFBUZZ TRUE CACHE INTERNAL "" FORCE)
set(FT_DISABLE_BROTLI TRUE CACHE INTERNAL "" FORCE)
set(BUILD_SHARED_LIBS OFF CACHE INTERNAL "" FORCE)
add_subdirectory(dependencies/freetype)
target_link_libraries(${PROJECT_NAME} PRIVATE freetype)
target_include_directories(${PROJECT_NAME} PRIVATE dependencies/freetype/include)
# stb # stb
target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE dependencies/stb) target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE dependencies/stb)

@ -1 +0,0 @@
Subproject commit dd91f6e7f5a051818070c49715125fb72074023e

View File

@ -0,0 +1,26 @@
#ifndef ENGINE_INCLUDE_COMPONENTS_RENDERABLE_TEXT_H_
#define ENGINE_INCLUDE_COMPONENTS_RENDERABLE_TEXT_H_
#include <memory>
#include <string>
#include "resources/texture.h"
namespace engine {
struct RenderableTextComponent {
void SetText(const std::string& text) {
text_ = text;
invalidated_ = true;
}
private:
std::string text_{"hello world"};
bool invalidated_ =
true; // text has been changed, texture must be regenerated
std::unique_ptr<resources::Texture> texture_{};
};
} // namespace engine
#endif

23
include/resources/font.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef ENGINE_INCLUDE_RESOURCES_FONT_H_
#define ENGINE_INCLUDE_RESOURCES_FONT_H_
#include <string>
namespace engine {
namespace resources {
class Font {
public:
Font(const std::string& path);
~Font();
Font(const Font&) = delete;
Font& operator=(const Font&) = delete;
private:
};
} // namespace resources
} // namespace engine
#endif

View File

@ -12,6 +12,7 @@
#include "gfx_device.h" #include "gfx_device.h"
#include "input_manager.h" #include "input_manager.h"
#include "log.h" #include "log.h"
#include "resources/font.h"
#include "resources/material.h" #include "resources/material.h"
#include "resources/mesh.h" #include "resources/mesh.h"
#include "resources/shader.h" #include "resources/shader.h"
@ -69,6 +70,7 @@ namespace engine {
resources_path_ = getResourcesPath(); resources_path_ = getResourcesPath();
// register resource managers // register resource managers
RegisterResourceManager<resources::Font>();
RegisterResourceManager<resources::Texture>(); RegisterResourceManager<resources::Texture>();
RegisterResourceManager<resources::Shader>(); RegisterResourceManager<resources::Shader>();
RegisterResourceManager<resources::Material>(); RegisterResourceManager<resources::Material>();
@ -114,6 +116,12 @@ namespace engine {
render_data_.material_set_layout = gfxdev()->CreateDescriptorSetLayout(materialSetBindings); render_data_.material_set_layout = gfxdev()->CreateDescriptorSetLayout(materialSetBindings);
// default resources // default resources
{
auto monoFont = std::make_unique<resources::Font>(
GetResourcePath("engine/fonts/mono.ttf")
);
GetResourceManager<resources::Font>()->AddPersistent("builtin.mono", std::move(monoFont));
}
{ {
resources::Shader::VertexParams vertParams{}; resources::Shader::VertexParams vertParams{};
vertParams.has_normal = true; vertParams.has_normal = true;

View File

@ -0,0 +1,2 @@
#define STB_TRUETYPE_IMPLEMENTATION
#include <stb_truetype.h>

26
src/resources/font.cpp Normal file
View File

@ -0,0 +1,26 @@
#include "resources/font.h"
#include <string>
#include <stb_truetype.h>
#include "log.h"
#include "util/files.h"
namespace engine::resources {
Font::Font(const std::string& path) {
// auto font_buffer = util::ReadBinaryFile(path);
// constexpr int FIRST_CHAR = 32;
// constexpr int NUM_CHARS = 96;
// constexpr float SCALE = 128.0f;
// TODO finish this
LOG_INFO("Loaded font: {}", path);
}
Font::~Font() {}
} // namespace engine::resources

View File

@ -15,46 +15,48 @@
namespace engine { namespace engine {
RenderSystem::RenderSystem(Scene* scene) RenderSystem::RenderSystem(Scene* scene)
: System(scene, { typeid(TransformComponent).hash_code(), typeid(RenderableComponent).hash_code() }), : System(scene, {typeid(TransformComponent).hash_code(),
gfx_(scene_->app()->gfxdev()) typeid(RenderableComponent).hash_code()}),
{ gfx_(scene_->app()->gfxdev()) {}
}
RenderSystem::~RenderSystem() RenderSystem::~RenderSystem() {}
{
}
void RenderSystem::OnUpdate(float ts) void RenderSystem::OnUpdate(float ts) {
{
(void)ts; (void)ts;
RenderData& renderData = scene_->app()->render_data_; RenderData& render_data = scene_->app()->render_data_;
/* camera stuff */ /* camera stuff */
const auto cameraTransform = scene_->GetComponent<TransformComponent>(camera_.cam_entity); const auto camera_transform =
scene_->GetComponent<TransformComponent>(camera_.cam_entity);
// do not render if camera is not set // do not render if camera is not set
if (cameraTransform == nullptr) return; if (camera_transform == nullptr) return;
glm::mat4 viewMatrix = glm::inverse(cameraTransform->world_matrix);
if (scene_->app()->window()->GetWindowResized()) { if (scene_->app()->window()->GetWindowResized()) {
uint32_t w, h; uint32_t w, h;
gfx_->GetViewportSize(&w, &h); gfx_->GetViewportSize(&w, &h);
viewport_aspect_ratio_ = (float)w / (float)h; viewport_aspect_ratio_ = (float)w / (float)h;
const float verticalFovRadians = glm::radians(camera_.vertical_fov_degrees); const float vertical_fov_radians =
const glm::mat4 projMatrix = glm::perspectiveZO(verticalFovRadians, viewport_aspect_ratio_, camera_.clip_near, camera_.clip_far); glm::radians(camera_.vertical_fov_degrees);
/* update SET 0 */ const glm::mat4 proj_matrix =
RenderData::GlobalSetUniformBuffer globalSetUniformBuffer{ glm::perspectiveZO(vertical_fov_radians, viewport_aspect_ratio_,
.proj = projMatrix camera_.clip_near, camera_.clip_far);
}; /* update SET 0 (rarely changing uniforms)*/
gfx_->WriteUniformBuffer(renderData.global_set_uniform_buffer, 0, sizeof(RenderData::GlobalSetUniformBuffer), &globalSetUniformBuffer); RenderData::GlobalSetUniformBuffer global_set_uniform_buffer{};
global_set_uniform_buffer.proj = proj_matrix;
gfx_->WriteUniformBuffer(render_data.global_set_uniform_buffer, 0,
sizeof(RenderData::GlobalSetUniformBuffer),
&global_set_uniform_buffer);
} }
RenderData::FrameSetUniformBuffer frameSetUniformBuffer{ glm::mat4 view_matrix = glm::inverse(camera_transform->world_matrix);
.view = viewMatrix /* update SET 1 (per frame uniforms) */
}; RenderData::FrameSetUniformBuffer frame_set_uniform_buffer{};
gfx_->WriteUniformBuffer(renderData.frame_set_uniform_buffer, 0, sizeof(RenderData::FrameSetUniformBuffer), &frameSetUniformBuffer); frame_set_uniform_buffer.view = view_matrix;
gfx_->WriteUniformBuffer(render_data.frame_set_uniform_buffer, 0,
sizeof(RenderData::FrameSetUniformBuffer),
&frame_set_uniform_buffer);
/* render all renderable entities */ /* render all renderable entities */
@ -65,14 +67,14 @@ namespace engine {
struct DrawCallData { struct DrawCallData {
const gfx::Buffer* vb; const gfx::Buffer* vb;
const gfx::Buffer* ib; const gfx::Buffer* ib;
const gfx::DescriptorSet* materialSet; const gfx::DescriptorSet* material_set;
uint32_t indexCount; uint32_t index_count;
PushConstants pushConsts; PushConstants push_constants;
}; };
std::unordered_map <const gfx::Pipeline*, std::vector<DrawCallData>> pipelineDrawCalls{}; std::unordered_map<const gfx::Pipeline*, std::vector<DrawCallData>>
pipeline_draw_calls{};
for (uint32_t entity : entities_) { for (uint32_t entity : entities_) {
auto r = scene_->GetComponent<RenderableComponent>(entity); auto r = scene_->GetComponent<RenderableComponent>(entity);
assert(r != nullptr); assert(r != nullptr);
assert(r->material != nullptr); assert(r->material != nullptr);
@ -87,42 +89,43 @@ namespace engine {
DrawCallData data{}; DrawCallData data{};
data.vb = r->mesh->GetVB(); data.vb = r->mesh->GetVB();
data.ib = r->mesh->GetIB(); data.ib = r->mesh->GetIB();
data.materialSet = r->material->texture_->GetDescriptorSet(); data.material_set = r->material->texture_->GetDescriptorSet();
data.indexCount = r->mesh->GetCount(); data.index_count = r->mesh->GetCount();
data.pushConsts.model = t->world_matrix; data.push_constants.model = t->world_matrix;
pipelineDrawCalls[pipeline].push_back(data);
pipeline_draw_calls[pipeline].push_back(data);
} }
/* begin rendering */ /* begin rendering */
renderData.draw_buffer = gfx_->BeginRender(); render_data.draw_buffer = gfx_->BeginRender();
/* these descriptor set bindings should persist across pipeline changes */ /* these descriptor set bindings should persist across pipeline changes */
const gfx::Pipeline* firstPipeline = pipelineDrawCalls.begin()->first; const gfx::Pipeline* first_pipeline = pipeline_draw_calls.begin()->first;
gfx_->CmdBindDescriptorSet(renderData.draw_buffer, firstPipeline, renderData.global_set, 0); gfx_->CmdBindDescriptorSet(render_data.draw_buffer, first_pipeline,
gfx_->CmdBindDescriptorSet(renderData.draw_buffer, firstPipeline, renderData.frame_set, 1); render_data.global_set, 0);
gfx_->CmdBindDescriptorSet(render_data.draw_buffer, first_pipeline,
render_data.frame_set, 1);
for (const auto& [pipeline, drawCalls] : pipelineDrawCalls) { for (const auto& [pipeline, draw_calls] : pipeline_draw_calls) {
gfx_->CmdBindPipeline(renderData.draw_buffer, pipeline); gfx_->CmdBindPipeline(render_data.draw_buffer, pipeline);
for (const auto& drawCall : drawCalls) { for (const auto& draw_call : draw_calls) {
gfx_->CmdBindDescriptorSet(renderData.draw_buffer, pipeline, drawCall.materialSet, 2); gfx_->CmdBindDescriptorSet(render_data.draw_buffer, pipeline,
gfx_->CmdPushConstants(renderData.draw_buffer, pipeline, 0, sizeof(PushConstants), &drawCall.pushConsts); draw_call.material_set, 2);
gfx_->CmdBindVertexBuffer(renderData.draw_buffer, 0, drawCall.vb); gfx_->CmdPushConstants(render_data.draw_buffer, pipeline, 0,
gfx_->CmdBindIndexBuffer(renderData.draw_buffer, drawCall.ib); sizeof(PushConstants), &draw_call.push_constants);
gfx_->CmdDrawIndexed(renderData.draw_buffer, drawCall.indexCount, 1, 0, 0, 0); gfx_->CmdBindVertexBuffer(render_data.draw_buffer, 0, draw_call.vb);
gfx_->CmdBindIndexBuffer(render_data.draw_buffer, draw_call.ib);
gfx_->CmdDrawIndexed(render_data.draw_buffer, draw_call.index_count, 1, 0,
0, 0);
} }
} }
/* draw */ /* draw */
gfx_->FinishRender(renderData.draw_buffer); gfx_->FinishRender(render_data.draw_buffer);
} }
void RenderSystem::SetCameraEntity(uint32_t entity) void RenderSystem::SetCameraEntity(uint32_t entity) {
{
camera_.cam_entity = entity; camera_.cam_entity = entity;
} }
} } // namespace engine

View File

@ -7,18 +7,10 @@
namespace engine { namespace engine {
Render2DSystem::Render2DSystem(Scene* scene) Render2DSystem::Render2DSystem(Scene* scene)
: System(scene, { typeid(TransformComponent).hash_code() }) : System(scene, {typeid(TransformComponent).hash_code()}) {}
{
}
Render2DSystem::~Render2DSystem() Render2DSystem::~Render2DSystem() {}
{
}
void Render2DSystem::OnUpdate(float ts) void Render2DSystem::OnUpdate(float ts) { (void)ts; }
{
(void)ts;
}
}
} // namespace engine