Add renderer methods

This commit is contained in:
Bailey Harrison 2023-08-29 22:10:05 +01:00
parent 0145bf187d
commit 87f98ce4c6
11 changed files with 122 additions and 63 deletions

View File

@ -92,8 +92,6 @@ source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/include" PREFIX "Include" FILES $
target_compile_definitions(${PROJECT_NAME} PRIVATE DEFINITIONS "ENGINE_EXPORTS") target_compile_definitions(${PROJECT_NAME} PRIVATE DEFINITIONS "ENGINE_EXPORTS")
target_compile_definitions(${PROJECT_NAME} PUBLIC DEFINITIONS "VMA_DEBUG_LOG=printf")
if (WIN32) if (WIN32)
target_compile_definitions(${PROJECT_NAME} PRIVATE DEFINITIONS "NOMINMAX") # stop windows.h conflicting with 'std::max' target_compile_definitions(${PROJECT_NAME} PRIVATE DEFINITIONS "NOMINMAX") # stop windows.h conflicting with 'std::max'
endif() endif()

View File

@ -68,12 +68,14 @@ class Application {
private: private:
std::unique_ptr<Window> window_; std::unique_ptr<Window> window_;
std::unique_ptr<InputManager> input_manager_; std::unique_ptr<InputManager> input_manager_;
std::unique_ptr<SceneManager> scene_manager_;
std::unique_ptr<Renderer> renderer_; std::unique_ptr<Renderer> renderer_;
std::unordered_map<size_t, std::unique_ptr<IResourceManager>> std::unordered_map<size_t, std::unique_ptr<IResourceManager>>
resource_managers_{}; resource_managers_{};
std::filesystem::path resources_path_; std::filesystem::path resources_path_;
// Most resources and class instances in the game exist in this object
std::unique_ptr<SceneManager> scene_manager_;
bool enable_frame_limiter_ = true; bool enable_frame_limiter_ = true;
template <typename T> template <typename T>

View File

@ -5,6 +5,7 @@
#include <unordered_map> #include <unordered_map>
#include <glm/mat4x4.hpp> #include <glm/mat4x4.hpp>
#include <glm/trigonometric.hpp>
#include "gfx_device.h" #include "gfx_device.h"
@ -28,6 +29,12 @@ class Renderer {
~Renderer(); ~Renderer();
void PreRender(bool window_is_resized, glm::mat4 camera_transform);
void Render();
// getters
GFXDevice* GetDevice() { return device_.get(); } GFXDevice* GetDevice() { return device_.get(); }
const gfx::DescriptorSetLayout* GetGlobalSetLayout() { const gfx::DescriptorSetLayout* GetGlobalSetLayout() {
@ -47,6 +54,12 @@ class Renderer {
private: private:
std::unique_ptr<GFXDevice> device_; std::unique_ptr<GFXDevice> device_;
struct CameraSettings {
float vertical_fov_radians = glm::radians(70.0f);
float clip_near = 0.5f;
float clip_far = 1000.0f;
} camera_settings_;
// ALL vertex shaders must begin with: // ALL vertex shaders must begin with:
/* /*
layout(set = 0, binding = 0) uniform GlobalSetUniformBuffer { layout(set = 0, binding = 0) uniform GlobalSetUniformBuffer {
@ -71,6 +84,8 @@ class Renderer {
UniformDescriptor<glm::mat4> frame_uniform; // updates once per frame; set 1 UniformDescriptor<glm::mat4> frame_uniform; // updates once per frame; set 1
// in fragment shader // in fragment shader
const gfx::DescriptorSetLayout* material_set_layout; // set 2 const gfx::DescriptorSetLayout* material_set_layout; // set 2
float viewport_aspect_ratio_ = 1.0f;
}; };
} // namespace engine } // namespace engine

View File

@ -6,17 +6,26 @@
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include <typeinfo>
#include "log.h"
#include "resources/mesh.h"
namespace engine { namespace engine {
class IResourceManager { class IResourceManager {
public: public:
virtual ~IResourceManager() = default; virtual ~IResourceManager(){};
}; };
template <class T> template <class T>
class ResourceManager : public IResourceManager { class ResourceManager : public IResourceManager {
public: public:
~ResourceManager() override {
LOG_DEBUG("Destroying resource manager... '{}'", typeid(T).name());
}
std::shared_ptr<T> Add(const std::string& name, std::shared_ptr<T> Add(const std::string& name,
std::unique_ptr<T>&& resource) { std::unique_ptr<T>&& resource) {
if (resources_.contains(name) == false) { if (resources_.contains(name) == false) {

View File

@ -70,11 +70,11 @@ Application::Application(const char* appName, const char* appVersion,
resources_path_ = getResourcesPath(); resources_path_ = getResourcesPath();
// register resource managers // register resource managers
RegisterResourceManager<resources::Font>(); RegisterResourceManager<resources::Mesh>();
RegisterResourceManager<resources::Material>();
RegisterResourceManager<resources::Texture>(); RegisterResourceManager<resources::Texture>();
RegisterResourceManager<resources::Shader>(); RegisterResourceManager<resources::Shader>();
RegisterResourceManager<resources::Material>(); RegisterResourceManager<resources::Font>();
RegisterResourceManager<resources::Mesh>();
renderer_ = std::make_unique<Renderer>( renderer_ = std::make_unique<Renderer>(
appName, appVersion, window_->GetHandle(), graphicsSettings); appName, appVersion, window_->GetHandle(), graphicsSettings);
@ -148,10 +148,11 @@ Application::Application(const char* appName, const char* appVersion,
} }
} }
Application::~Application() {} Application::~Application() {
}
void Application::GameLoop() { void Application::GameLoop() {
LOG_TRACE("Begin game loop..."); LOG_DEBUG("Begin game loop...");
constexpr int FPS_LIMIT = 240; constexpr int FPS_LIMIT = 240;
constexpr auto FRAMETIME_LIMIT = constexpr auto FRAMETIME_LIMIT =
@ -170,11 +171,13 @@ void Application::GameLoop() {
if (now - lastTick >= 1000000000LL * 5LL) [[unlikely]] { if (now - lastTick >= 1000000000LL * 5LL) [[unlikely]] {
lastTick = now; lastTick = now;
LOG_INFO("fps: {}", window_->GetAvgFPS()); LOG_INFO("fps: {}", window_->GetAvgFPS());
renderer()->GetDevice()->LogPerformanceInfo(); //renderer()->GetDevice()->LogPerformanceInfo();
window_->ResetAvgFPS(); window_->ResetAvgFPS();
} }
/* render */ /* render */
renderer_->PreRender(window()->GetWindowResized(), glm::mat4{1.0f});
renderer_->Render();
/* poll events */ /* poll events */
window_->GetInputAndEvents(); window_->GetInputAndEvents();

View File

@ -1,5 +1,9 @@
#include "renderer.h" #include "renderer.h"
#include <glm/mat4x4.hpp>
#include <glm/trigonometric.hpp>
#include <glm/ext/matrix_clip_space.hpp>
namespace engine { namespace engine {
Renderer::Renderer(const char* app_name, const char* app_version, Renderer::Renderer(const char* app_name, const char* app_version,
@ -62,4 +66,35 @@ Renderer::~Renderer() {
device_->DestroyDescriptorSetLayout(global_uniform.layout); device_->DestroyDescriptorSetLayout(global_uniform.layout);
} }
void Renderer::PreRender(bool window_is_resized, glm::mat4 camera_transform) {
if (window_is_resized) {
uint32_t w, h;
device_->GetViewportSize(&w, &h);
viewport_aspect_ratio_ = (float)w / (float)h;
const glm::mat4 proj_matrix = glm::perspectiveZO(
camera_settings_.vertical_fov_radians, viewport_aspect_ratio_,
camera_settings_.clip_near, camera_settings_.clip_far);
/* update SET 0 (rarely changing uniforms)*/
global_uniform.uniform_buffer_data.data = proj_matrix;
device_->WriteUniformBuffer(global_uniform.uniform_buffer, 0,
sizeof(global_uniform.uniform_buffer_data),
&global_uniform.uniform_buffer_data);
}
// set camera view matrix uniform
/* update SET 1 (per frame uniforms) */
const glm::mat4 view_matrix = glm::inverse(camera_transform);
frame_uniform.uniform_buffer_data.data = view_matrix;
device_->WriteUniformBuffer(frame_uniform.uniform_buffer, 0,
sizeof(frame_uniform.uniform_buffer_data),
&frame_uniform.uniform_buffer_data);
}
void Renderer::Render()
{
gfx::DrawBuffer* draw_buffer = device_->BeginRender();
device_->FinishRender(draw_buffer);
}
} // namespace engine } // namespace engine

View File

@ -18,10 +18,10 @@ Font::Font(const std::string& path) {
throw std::runtime_error("Failed to initialise font!"); throw std::runtime_error("Failed to initialise font!");
} }
LOG_INFO("Loaded font: {}", path); LOG_DEBUG("Created font: {}", path);
} }
Font::~Font() {} Font::~Font() { LOG_DEBUG("Destroying font..."); }
std::unique_ptr<std::vector<uint8_t>> Font::GetTextBitmap( std::unique_ptr<std::vector<uint8_t>> Font::GetTextBitmap(
const std::string& text, float height_px, int& width_out, int& height_out) { const std::string& text, float height_px, int& width_out, int& height_out) {

View File

@ -5,43 +5,35 @@
namespace engine::resources { namespace engine::resources {
Mesh::Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices) Mesh::Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices) : gfx_(gfx) {
: gfx_(gfx)
{
std::vector<uint32_t> indices(vertices.size()); std::vector<uint32_t> indices(vertices.size());
for (uint32_t i = 0; i < indices.size(); i++) { for (uint32_t i = 0; i < indices.size(); i++) {
indices[i] = i; indices[i] = i;
} }
InitMesh(vertices, indices); InitMesh(vertices, indices);
} }
Mesh::~Mesh() Mesh::~Mesh() {
{ LOG_DEBUG("Destroying mesh...");
gfx_->DestroyBuffer(ib_); gfx_->DestroyBuffer(ib_);
gfx_->DestroyBuffer(vb_); gfx_->DestroyBuffer(vb_);
}
const gfx::Buffer* Mesh::GetVB()
{
return vb_;
}
const gfx::Buffer* Mesh::GetIB()
{
return ib_;
}
uint32_t Mesh::GetCount()
{
return count_;
}
void Mesh::InitMesh(const std::vector<Vertex>& vertices, const std::vector<uint32_t>& indices)
{
vb_ = gfx_->CreateBuffer(gfx::BufferType::kVertex, vertices.size() * sizeof(Vertex), vertices.data());
ib_ = gfx_->CreateBuffer(gfx::BufferType::kIndex, indices.size() * sizeof(uint32_t), indices.data());
count_ = (uint32_t)indices.size();
LOG_INFO("Loaded mesh, vertices: {}, indices: {}", vertices.size(), indices.size());
}
} }
const gfx::Buffer* Mesh::GetVB() { return vb_; }
const gfx::Buffer* Mesh::GetIB() { return ib_; }
uint32_t Mesh::GetCount() { return count_; }
void Mesh::InitMesh(const std::vector<Vertex>& vertices,
const std::vector<uint32_t>& indices) {
vb_ = gfx_->CreateBuffer(gfx::BufferType::kVertex,
vertices.size() * sizeof(Vertex), vertices.data());
ib_ = gfx_->CreateBuffer(gfx::BufferType::kIndex,
indices.size() * sizeof(uint32_t), indices.data());
count_ = (uint32_t)indices.size();
LOG_DEBUG("Created mesh, vertices: {}, indices: {}", vertices.size(),
indices.size());
}
} // namespace engine::resources

View File

@ -56,11 +56,14 @@ Shader::Shader(Renderer* renderer, const char* vertPath, const char* fragPath,
pipeline_ = gfx_->CreatePipeline(info); pipeline_ = gfx_->CreatePipeline(info);
LOG_INFO("Loaded shader: {}, vertex attribs: {}", vertPath, LOG_DEBUG("Created shader: {}, pipeline: {}", vertPath,
vertFormat.attribute_descriptions.size()); static_cast<const void*>(pipeline_));
} }
Shader::~Shader() { gfx_->DestroyPipeline(pipeline_); } Shader::~Shader() {
LOG_DEBUG("Destroying shader... pipeline: {}", static_cast<const void*>(pipeline_));
gfx_->DestroyPipeline(pipeline_);
}
const gfx::Pipeline* Shader::GetPipeline() { return pipeline_; } const gfx::Pipeline* Shader::GetPipeline() { return pipeline_; }

View File

@ -52,7 +52,7 @@ Texture::Texture(Renderer* renderer, const std::string& path,
gfx_->UpdateDescriptorCombinedImageSampler( gfx_->UpdateDescriptorCombinedImageSampler(
descriptor_set_, 0, image_, renderer->samplers.at(samplerInfo)); descriptor_set_, 0, image_, renderer->samplers.at(samplerInfo));
LOG_INFO("Loaded texture: {}, width: {} height: {}", path, width, height); LOG_DEBUG("Created texture: {}, width: {} height: {}", path, width, height);
} }
Texture::Texture(Renderer* renderer, const uint8_t* bitmap, int width, Texture::Texture(Renderer* renderer, const uint8_t* bitmap, int width,
@ -95,11 +95,11 @@ Texture::Texture(Renderer* renderer, const uint8_t* bitmap, int width,
gfx_->UpdateDescriptorCombinedImageSampler( gfx_->UpdateDescriptorCombinedImageSampler(
descriptor_set_, 0, image_, renderer->samplers.at(samplerInfo)); descriptor_set_, 0, image_, renderer->samplers.at(samplerInfo));
LOG_INFO("Loaded texture: BITMAP, width: {} height: {}", width, height); LOG_DEBUG("Created texture: BITMAP, width: {} height: {}", width, height);
} }
Texture::~Texture() { Texture::~Texture() {
LOG_INFO("Destroying texture..."); LOG_DEBUG("Destroying texture...");
gfx_->FreeDescriptorSet(descriptor_set_); gfx_->FreeDescriptorSet(descriptor_set_);
gfx_->DestroyImage(image_); gfx_->DestroyImage(image_);
} }

View File

@ -79,14 +79,14 @@ void PlayGame(GameSettings settings) {
} }
/* shared resources */ /* shared resources */
auto grass_texture = std::make_shared<engine::resources::Texture>( /* auto grass_texture = std::make_shared<engine::resources::Texture>(
app.renderer(), app.GetResourcePath("textures/grass.jpg"), app.renderer(), app.GetResourcePath("textures/grass.jpg"),
engine::resources::Texture::Filtering::kAnisotropic); engine::resources::Texture::Filtering::kAnisotropic);
auto space_texture = std::make_shared<engine::resources::Texture>( auto space_texture = std::make_shared<engine::resources::Texture>(
app.renderer(), app.GetResourcePath("textures/space2.png"), app.renderer(), app.GetResourcePath("textures/space2.png"),
engine::resources::Texture::Filtering::kAnisotropic); engine::resources::Texture::Filtering::kAnisotropic);
*/
/* skybox */ /* skybox */
{ {
uint32_t skybox = my_scene->CreateEntity("skybox"); uint32_t skybox = my_scene->CreateEntity("skybox");
@ -96,7 +96,8 @@ void PlayGame(GameSettings settings) {
skybox_renderable->material = skybox_renderable->material =
std::make_unique<engine::resources::Material>( std::make_unique<engine::resources::Material>(
app.GetResource<engine::resources::Shader>("builtin.skybox")); app.GetResource<engine::resources::Shader>("builtin.skybox"));
skybox_renderable->material->texture_ = space_texture; skybox_renderable->material->texture_ =
app.GetResource<engine::resources::Texture>("builtin.white");
skybox_renderable->mesh = GenCuboidMesh(app.renderer()->GetDevice(), skybox_renderable->mesh = GenCuboidMesh(app.renderer()->GetDevice(),
10.0f, 10.0f, 10.0f, 1.0f, true); 10.0f, 10.0f, 10.0f, 1.0f, true);
@ -120,7 +121,8 @@ void PlayGame(GameSettings settings) {
floor_renderable->material = floor_renderable->material =
std::make_shared<engine::resources::Material>( std::make_shared<engine::resources::Material>(
app.GetResource<engine::resources::Shader>("builtin.standard")); app.GetResource<engine::resources::Shader>("builtin.standard"));
floor_renderable->material->texture_ = grass_texture; floor_renderable->material->texture_ =
app.GetResource<engine::resources::Texture>("builtin.white");
floor_renderable->mesh = GenCuboidMesh(app.renderer()->GetDevice(), floor_renderable->mesh = GenCuboidMesh(app.renderer()->GetDevice(),
100.0f, 0.1f, 100.0f, 100.0f); 100.0f, 0.1f, 100.0f, 100.0f);
@ -155,7 +157,7 @@ void PlayGame(GameSettings settings) {
auto textboxComponent = auto textboxComponent =
my_scene->AddComponent<engine::CustomComponent>(textbox); my_scene->AddComponent<engine::CustomComponent>(textbox);
textboxComponent->onInit = [](void) { textboxComponent->onInit = [](void) {
LOG_INFO("Textbox custom component initialised!"); LOG_DEBUG("Textbox custom component initialised!");
}; };
textboxComponent->onUpdate = [](float ts) { textboxComponent->onUpdate = [](float ts) {
@ -163,7 +165,7 @@ void PlayGame(GameSettings settings) {
time_elapsed += ts; time_elapsed += ts;
if (time_elapsed >= 1.0f) { if (time_elapsed >= 1.0f) {
time_elapsed = 0.0f; time_elapsed = 0.0f;
LOG_INFO("COMPONENT UPDATE"); //LOG_INFO("COMPONENT UPDATE");
} }
}; };
} }