mirror of
https://github.com/bailwillharr/engine.git
synced 2024-09-21 04:51:18 +00:00
Add renderer methods
This commit is contained in:
parent
0145bf187d
commit
87f98ce4c6
@ -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} PUBLIC DEFINITIONS "VMA_DEBUG_LOG=printf")
|
||||
|
||||
if (WIN32)
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE DEFINITIONS "NOMINMAX") # stop windows.h conflicting with 'std::max'
|
||||
endif()
|
||||
|
@ -68,12 +68,14 @@ class Application {
|
||||
private:
|
||||
std::unique_ptr<Window> window_;
|
||||
std::unique_ptr<InputManager> input_manager_;
|
||||
std::unique_ptr<SceneManager> scene_manager_;
|
||||
std::unique_ptr<Renderer> renderer_;
|
||||
std::unordered_map<size_t, std::unique_ptr<IResourceManager>>
|
||||
resource_managers_{};
|
||||
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;
|
||||
|
||||
template <typename T>
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <unordered_map>
|
||||
|
||||
#include <glm/mat4x4.hpp>
|
||||
#include <glm/trigonometric.hpp>
|
||||
|
||||
#include "gfx_device.h"
|
||||
|
||||
@ -28,6 +29,12 @@ class Renderer {
|
||||
|
||||
~Renderer();
|
||||
|
||||
void PreRender(bool window_is_resized, glm::mat4 camera_transform);
|
||||
|
||||
void Render();
|
||||
|
||||
// getters
|
||||
|
||||
GFXDevice* GetDevice() { return device_.get(); }
|
||||
|
||||
const gfx::DescriptorSetLayout* GetGlobalSetLayout() {
|
||||
@ -47,6 +54,12 @@ class Renderer {
|
||||
private:
|
||||
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:
|
||||
/*
|
||||
layout(set = 0, binding = 0) uniform GlobalSetUniformBuffer {
|
||||
@ -71,6 +84,8 @@ class Renderer {
|
||||
UniformDescriptor<glm::mat4> frame_uniform; // updates once per frame; set 1
|
||||
// in fragment shader
|
||||
const gfx::DescriptorSetLayout* material_set_layout; // set 2
|
||||
|
||||
float viewport_aspect_ratio_ = 1.0f;
|
||||
};
|
||||
|
||||
} // namespace engine
|
||||
|
@ -6,17 +6,26 @@
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <typeinfo>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#include "resources/mesh.h"
|
||||
|
||||
namespace engine {
|
||||
|
||||
class IResourceManager {
|
||||
public:
|
||||
virtual ~IResourceManager() = default;
|
||||
virtual ~IResourceManager(){};
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class ResourceManager : public IResourceManager {
|
||||
public:
|
||||
~ResourceManager() override {
|
||||
LOG_DEBUG("Destroying resource manager... '{}'", typeid(T).name());
|
||||
}
|
||||
|
||||
std::shared_ptr<T> Add(const std::string& name,
|
||||
std::unique_ptr<T>&& resource) {
|
||||
if (resources_.contains(name) == false) {
|
||||
|
@ -70,11 +70,11 @@ Application::Application(const char* appName, const char* appVersion,
|
||||
resources_path_ = getResourcesPath();
|
||||
|
||||
// register resource managers
|
||||
RegisterResourceManager<resources::Font>();
|
||||
RegisterResourceManager<resources::Mesh>();
|
||||
RegisterResourceManager<resources::Material>();
|
||||
RegisterResourceManager<resources::Texture>();
|
||||
RegisterResourceManager<resources::Shader>();
|
||||
RegisterResourceManager<resources::Material>();
|
||||
RegisterResourceManager<resources::Mesh>();
|
||||
RegisterResourceManager<resources::Font>();
|
||||
|
||||
renderer_ = std::make_unique<Renderer>(
|
||||
appName, appVersion, window_->GetHandle(), graphicsSettings);
|
||||
@ -148,10 +148,11 @@ Application::Application(const char* appName, const char* appVersion,
|
||||
}
|
||||
}
|
||||
|
||||
Application::~Application() {}
|
||||
Application::~Application() {
|
||||
}
|
||||
|
||||
void Application::GameLoop() {
|
||||
LOG_TRACE("Begin game loop...");
|
||||
LOG_DEBUG("Begin game loop...");
|
||||
|
||||
constexpr int FPS_LIMIT = 240;
|
||||
constexpr auto FRAMETIME_LIMIT =
|
||||
@ -170,11 +171,13 @@ void Application::GameLoop() {
|
||||
if (now - lastTick >= 1000000000LL * 5LL) [[unlikely]] {
|
||||
lastTick = now;
|
||||
LOG_INFO("fps: {}", window_->GetAvgFPS());
|
||||
renderer()->GetDevice()->LogPerformanceInfo();
|
||||
//renderer()->GetDevice()->LogPerformanceInfo();
|
||||
window_->ResetAvgFPS();
|
||||
}
|
||||
|
||||
/* render */
|
||||
renderer_->PreRender(window()->GetWindowResized(), glm::mat4{1.0f});
|
||||
renderer_->Render();
|
||||
|
||||
/* poll events */
|
||||
window_->GetInputAndEvents();
|
||||
|
@ -1,5 +1,9 @@
|
||||
#include "renderer.h"
|
||||
|
||||
#include <glm/mat4x4.hpp>
|
||||
#include <glm/trigonometric.hpp>
|
||||
#include <glm/ext/matrix_clip_space.hpp>
|
||||
|
||||
namespace engine {
|
||||
|
||||
Renderer::Renderer(const char* app_name, const char* app_version,
|
||||
@ -62,4 +66,35 @@ Renderer::~Renderer() {
|
||||
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
|
@ -18,10 +18,10 @@ Font::Font(const std::string& path) {
|
||||
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(
|
||||
const std::string& text, float height_px, int& width_out, int& height_out) {
|
||||
|
@ -5,43 +5,35 @@
|
||||
|
||||
namespace engine::resources {
|
||||
|
||||
Mesh::Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices)
|
||||
: gfx_(gfx)
|
||||
{
|
||||
Mesh::Mesh(GFXDevice* gfx, const std::vector<Vertex>& vertices) : gfx_(gfx) {
|
||||
std::vector<uint32_t> indices(vertices.size());
|
||||
for (uint32_t i = 0; i < indices.size(); i++) {
|
||||
indices[i] = i;
|
||||
}
|
||||
InitMesh(vertices, indices);
|
||||
}
|
||||
}
|
||||
|
||||
Mesh::~Mesh()
|
||||
{
|
||||
Mesh::~Mesh() {
|
||||
LOG_DEBUG("Destroying mesh...");
|
||||
gfx_->DestroyBuffer(ib_);
|
||||
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
|
||||
|
@ -56,11 +56,14 @@ Shader::Shader(Renderer* renderer, const char* vertPath, const char* fragPath,
|
||||
|
||||
pipeline_ = gfx_->CreatePipeline(info);
|
||||
|
||||
LOG_INFO("Loaded shader: {}, vertex attribs: {}", vertPath,
|
||||
vertFormat.attribute_descriptions.size());
|
||||
LOG_DEBUG("Created shader: {}, pipeline: {}", vertPath,
|
||||
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_; }
|
||||
|
||||
|
@ -52,7 +52,7 @@ Texture::Texture(Renderer* renderer, const std::string& path,
|
||||
gfx_->UpdateDescriptorCombinedImageSampler(
|
||||
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,
|
||||
@ -95,11 +95,11 @@ Texture::Texture(Renderer* renderer, const uint8_t* bitmap, int width,
|
||||
gfx_->UpdateDescriptorCombinedImageSampler(
|
||||
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() {
|
||||
LOG_INFO("Destroying texture...");
|
||||
LOG_DEBUG("Destroying texture...");
|
||||
gfx_->FreeDescriptorSet(descriptor_set_);
|
||||
gfx_->DestroyImage(image_);
|
||||
}
|
||||
|
@ -79,14 +79,14 @@ void PlayGame(GameSettings settings) {
|
||||
}
|
||||
|
||||
/* 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"),
|
||||
engine::resources::Texture::Filtering::kAnisotropic);
|
||||
|
||||
auto space_texture = std::make_shared<engine::resources::Texture>(
|
||||
app.renderer(), app.GetResourcePath("textures/space2.png"),
|
||||
engine::resources::Texture::Filtering::kAnisotropic);
|
||||
|
||||
*/
|
||||
/* skybox */
|
||||
{
|
||||
uint32_t skybox = my_scene->CreateEntity("skybox");
|
||||
@ -96,7 +96,8 @@ void PlayGame(GameSettings settings) {
|
||||
skybox_renderable->material =
|
||||
std::make_unique<engine::resources::Material>(
|
||||
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(),
|
||||
10.0f, 10.0f, 10.0f, 1.0f, true);
|
||||
|
||||
@ -120,7 +121,8 @@ void PlayGame(GameSettings settings) {
|
||||
floor_renderable->material =
|
||||
std::make_shared<engine::resources::Material>(
|
||||
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(),
|
||||
100.0f, 0.1f, 100.0f, 100.0f);
|
||||
|
||||
@ -155,7 +157,7 @@ void PlayGame(GameSettings settings) {
|
||||
auto textboxComponent =
|
||||
my_scene->AddComponent<engine::CustomComponent>(textbox);
|
||||
textboxComponent->onInit = [](void) {
|
||||
LOG_INFO("Textbox custom component initialised!");
|
||||
LOG_DEBUG("Textbox custom component initialised!");
|
||||
};
|
||||
|
||||
textboxComponent->onUpdate = [](float ts) {
|
||||
@ -163,7 +165,7 @@ void PlayGame(GameSettings settings) {
|
||||
time_elapsed += ts;
|
||||
if (time_elapsed >= 1.0f) {
|
||||
time_elapsed = 0.0f;
|
||||
LOG_INFO("COMPONENT UPDATE");
|
||||
//LOG_INFO("COMPONENT UPDATE");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user