sdl_gpu_test/private/sdl_gpu_test/scene/scene_renderer.hpp

101 lines
2.6 KiB
C++

#pragma once
#if !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_SCENE_SCENE_RENDERER_HPP_INCLUDED)
#define SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_SCENE_SCENE_RENDERER_HPP_INCLUDED 1
#include <optional>
#include <memory>
#include <unordered_map>
#include <vector>
#include "./transform3d.hpp"
#include "../application.hpp"
#include "../sdlpp/gpu.hpp"
namespace sdl_gpu_test
{
struct SceneMesh
{
sdlpp::GPUBuffer vertexBuffer;
std::size_t numVertices;
};
struct SceneTexture
{
sdlpp::GPUTexture texture;
};
struct CameraOptions
{
glm::vec3 position = glm::vec3(0.f);
float roll = 0.f;
float pitch = 0.f;
float yaw = 0.f;
float fovh = glm::radians(90.f);
float zNear = 0.f;
float zFar = 100.f;
};
struct SceneRendererRenderArgs
{
CameraOptions camera;
const sdlpp::GPUCommandBuffer* cmdBuffer;
const sdlpp::GPUTexture* targetTexture;
const sdlpp::GPUTexture* depthTexture;
unsigned targetTextureWidth;
unsigned targetTextureHeight;
};
struct RenderListUpdate
{
std::optional<std::shared_ptr<SceneMesh>> mesh;
std::optional<std::shared_ptr<SceneTexture>> texture;
std::optional<Transform3D> transform;
};
class SceneRenderer
{
public:
using mesh_id_t = std::uint64_t;
static constexpr mesh_id_t UNSET_MESH_ID = 0;
private:
struct RenderListEntry
{
std::shared_ptr<SceneMesh> mesh;
std::shared_ptr<SceneTexture> texture;
Transform3D transform;
mesh_id_t meshId;
};
Application* mApplication = nullptr;
std::unordered_map<std::string, std::weak_ptr<SceneMesh>> mCachedMeshes;
std::unordered_map<std::string, std::weak_ptr<SceneTexture>> mCachedTextures;
std::vector<RenderListEntry> mRenderList;
mesh_id_t mNextMeshId = 1;
sdlpp::GPUGraphicsPipeline mPipeline;
sdlpp::GPUSampler mSampler;
public:
void init(Application& application);
[[nodiscard]]
std::shared_ptr<SceneMesh> getMesh(const std::string& resourcePath);
[[nodiscard]]
std::shared_ptr<SceneTexture> getTexture(const std::string& resourcePath);
void addMeshToRenderList(std::shared_ptr<SceneMesh> mesh, std::shared_ptr<SceneTexture> texture,
const Transform3D& transform, mesh_id_t& inOutMeshId);
bool removeMeshFromRenderList(mesh_id_t meshId);
bool updateRenderListEntry(mesh_id_t meshId, const RenderListUpdate& update);
void render(const SceneRendererRenderArgs& args);
private:
void createPipeline();
};
} // namespace sdl_gpu_test
#endif // !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_SCENE_SCENE_RENDERER_HPP_INCLUDED)