engine/include/renderer.h

115 lines
3.4 KiB
C
Raw Permalink Normal View History

#ifndef ENGINE_INCLUDE_RENDERER_H_
#define ENGINE_INCLUDE_RENDERER_H_
2023-08-29 17:06:04 +00:00
#include <memory>
#include <unordered_map>
#include <glm/mat4x4.hpp>
2023-08-29 21:10:05 +00:00
#include <glm/trigonometric.hpp>
2023-08-29 17:06:04 +00:00
#include "application_component.h"
2023-08-29 17:06:04 +00:00
#include "gfx_device.h"
2023-08-31 09:51:12 +00:00
#include "systems/mesh_render_system.h"
2023-08-29 17:06:04 +00:00
namespace engine {
2023-08-29 17:06:04 +00:00
// A uniform struct that holds data of type T
template <typename T>
struct UniformDescriptor {
2024-03-14 04:25:02 +00:00
const gfx::DescriptorSetLayout* layout;
const gfx::DescriptorSet* set;
struct UniformBufferData {
T data;
} uniform_buffer_data;
gfx::UniformBuffer* uniform_buffer;
};
struct Line {
glm::vec3 pos1;
glm::vec3 pos2;
glm::vec3 color;
2023-08-29 17:06:04 +00:00
};
class Renderer : private ApplicationComponent {
2024-03-14 04:25:02 +00:00
public:
Renderer(Application& app, gfx::GraphicsSettings settings);
2023-08-29 17:06:04 +00:00
2024-03-14 04:25:02 +00:00
~Renderer();
2023-08-29 17:06:04 +00:00
2024-03-14 04:25:02 +00:00
void PreRender(bool window_is_resized, glm::mat4 camera_transform);
2023-08-29 21:10:05 +00:00
2024-03-14 04:25:02 +00:00
// staticList can be nullptr to render nothing
void Render(const RenderList* static_list, const RenderList* dynamic_list, const std::vector<Line>& debug_lines);
2023-08-29 21:10:05 +00:00
2024-03-14 04:25:02 +00:00
// getters
2023-08-29 21:10:05 +00:00
2024-03-14 04:25:02 +00:00
GFXDevice* GetDevice() { return device_.get(); }
2023-08-29 17:06:04 +00:00
2024-03-14 04:25:02 +00:00
const gfx::DescriptorSetLayout* GetGlobalSetLayout() { return global_uniform.layout; }
2023-08-29 17:06:04 +00:00
2024-03-14 04:25:02 +00:00
const gfx::DescriptorSetLayout* GetFrameSetLayout() { return frame_uniform.layout; }
2023-08-29 17:06:04 +00:00
2024-03-14 04:25:02 +00:00
const gfx::DescriptorSetLayout* GetMaterialSetLayout() { return material_set_layout; }
2023-08-29 17:06:04 +00:00
2024-03-14 04:25:02 +00:00
std::unordered_map<gfx::SamplerInfo, const gfx::Sampler*> samplers;
2023-08-29 17:06:04 +00:00
2024-03-14 04:25:02 +00:00
private:
std::unique_ptr<GFXDevice> device_;
2023-08-29 17:06:04 +00:00
2024-03-14 04:25:02 +00:00
struct CameraSettings {
float vertical_fov_radians = glm::radians(70.0f);
2024-03-19 11:32:51 +00:00
float clip_near = 0.1f;
2024-03-14 04:25:02 +00:00
float clip_far = 1000.0f;
} camera_settings_;
2023-08-29 21:10:05 +00:00
2024-03-14 04:25:02 +00:00
// ALL vertex shaders must begin with:
/*
layout(set = 0, binding = 0) uniform GlobalSetUniformBuffer {
mat4 proj;
} globalSetUniformBuffer;
2023-08-29 17:06:04 +00:00
2024-03-14 04:25:02 +00:00
layout(set = 1, binding = 0) uniform FrameSetUniformBuffer {
mat4 view;
} frameSetUniformBuffer;
2023-08-29 17:06:04 +00:00
2024-03-14 04:25:02 +00:00
layout( push_constant ) uniform Constants {
mat4 model;
} constants;
*/
// ALL fragment shaders must begin with:
/*
layout(set = 2, binding = 0) uniform sampler2D materialSetAlbedoSampler;
layout(set = 2, binding = 1) uniform sampler2D materialSetNormalSampler;
layout(set = 2, binding = 2) uniform sampler2D materialSetOcclusionSampler;
layout(set = 2, binding = 3) uniform sampler2D materialSetMetallicRoughnessSampler;
*/
2023-08-29 17:06:04 +00:00
2024-03-14 04:25:02 +00:00
// in vertex shader
UniformDescriptor<glm::mat4> global_uniform; // rarely updates; set 0
UniformDescriptor<glm::mat4> frame_uniform; // updates once per frame; set 1
// in fragment shader
const gfx::DescriptorSetLayout* material_set_layout; // set 2; set bound per material
2023-08-29 21:10:05 +00:00
2024-03-14 04:25:02 +00:00
float viewport_aspect_ratio_ = 1.0f;
2023-08-31 13:18:42 +00:00
2024-03-14 04:25:02 +00:00
const gfx::Pipeline* last_bound_pipeline_ = nullptr;
2023-08-31 13:18:42 +00:00
2024-03-14 04:25:02 +00:00
struct DebugRenderingThings {
const gfx::Pipeline* pipeline = nullptr;
// have a simple vertex buffer with 2 points that draws a line
const gfx::Buffer* vertex_buffer = nullptr;
// shader will take 2 clip space xyzw coords as push constants to define the line
} debug_rendering_things_{};
2024-03-19 11:32:51 +00:00
gfx::Image* skybox_cubemap = nullptr;
const gfx::Sampler* skybox_sampler = nullptr;
const gfx::Pipeline* skybox_pipeline = nullptr;
const gfx::Buffer* skybox_buffer = nullptr;
2024-03-14 04:25:02 +00:00
void DrawRenderList(gfx::DrawBuffer* draw_buffer, const RenderList& render_list);
2023-08-29 17:06:04 +00:00
};
2024-03-14 04:25:02 +00:00
} // namespace engine
2023-09-02 18:14:12 +00:00
#endif