Skip to content

Commit

Permalink
- Allow multiple cameras to be created
Browse files Browse the repository at this point in the history
- Move render target to camera
  • Loading branch information
edunad committed Oct 11, 2024
1 parent a0950d0 commit c798629
Show file tree
Hide file tree
Showing 34 changed files with 282 additions and 240 deletions.
5 changes: 4 additions & 1 deletion rawrbox.render/include/rawrbox/render/bindless.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ namespace rawrbox {

// SIGNATURES ------
static void createSignatures();
static void bindSignatures();
// -----------------

static uint32_t internalRegister(Diligent::ITextureView* view, rawrbox::TEXTURE_TYPE type);
Expand All @@ -80,6 +79,10 @@ namespace rawrbox {

static void update();

// SIGNATURES
static void bindSignatures();
// ----------------

// TEXTURES -------
static void registerTexture(rawrbox::TextureBase& texture);
static void registerTexture(rawrbox::TextureRender& texture);
Expand Down
47 changes: 40 additions & 7 deletions rawrbox.render/include/rawrbox/render/cameras/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <rawrbox/math/vector2.hpp>
#include <rawrbox/math/vector3.hpp>
#include <rawrbox/math/vector4.hpp>
#include <rawrbox/render/render_config.hpp>
#include <rawrbox/render/textures/render.hpp>
#include <rawrbox/utils/logger.hpp>

#include <RefCntAutoPtr.hpp>
Expand All @@ -11,6 +13,9 @@

#include <memory>

#define CREATE_FLAGS(name, value) \
const uint32_t name = 1 << value;

namespace rawrbox {
struct CameraStaticUniforms { // Uniforms that won't be updated that frequently
rawrbox::Matrix4x4 gProjection = {};
Expand All @@ -26,6 +31,12 @@ namespace rawrbox {
bool operator!=(const CameraStaticUniforms& other) const { return !operator==(other); }
};

namespace CameraLayers {
// USER DEFINED
RB_RENDER_CAMERA_LAYERS(CREATE_FLAGS)
// ------------
}; // namespace CameraLayers

struct CameraUniforms {
rawrbox::Matrix4x4 gView = {};
rawrbox::Matrix4x4 gViewInv = {};
Expand All @@ -47,6 +58,8 @@ namespace rawrbox {

class CameraBase {
protected:
bool _enabled = true;

rawrbox::Vector3f _pos = {};
rawrbox::Vector4f _angle = {};

Expand All @@ -55,27 +68,34 @@ namespace rawrbox {

rawrbox::Matrix4x4 _world = {};

uint32_t _layers = 0; // None by default, it's up to the user to use this or not

// Renderer ----
std::unique_ptr<rawrbox::TextureRender> _renderTarget = nullptr;
// --------------------------

float _z_near = 0.01F;
float _z_far = 100.F;

Diligent::RefCntAutoPtr<Diligent::IBuffer> _staticUniforms;
Diligent::RefCntAutoPtr<Diligent::IBuffer> _uniforms;

// LOGGER ------
std::unique_ptr<rawrbox::Logger> _logger = std::make_unique<rawrbox::Logger>("RawrBox-Camera");
// -------------

virtual void updateMtx();
virtual rawrbox::CameraStaticUniforms getStaticData();

virtual void initializeBuffers();

public:
virtual ~CameraBase();
static Diligent::RefCntAutoPtr<Diligent::IBuffer> staticUniforms;
static Diligent::RefCntAutoPtr<Diligent::IBuffer> uniforms;

CameraBase() = default;
CameraBase(const rawrbox::Vector2u& renderSize, bool depth = true);
CameraBase(CameraBase&&) = default;
CameraBase& operator=(CameraBase&&) = default;
CameraBase(const CameraBase&) = delete;
CameraBase& operator=(const CameraBase&) = delete;
virtual ~CameraBase();

// UTILS -----
virtual void setPos(const rawrbox::Vector3f& pos);
Expand All @@ -100,10 +120,23 @@ namespace rawrbox {

[[nodiscard]] virtual rawrbox::Vector3f worldToScreen(const rawrbox::Vector3f& pos) const;
[[nodiscard]] virtual rawrbox::Vector3f screenToWorld(const rawrbox::Vector2f& screen_pos, const rawrbox::Vector3f& origin = {0, 0, 0}) const;

virtual bool isEnabled() const;
virtual void setEnabled(bool enabled);

virtual uint32_t getLayers() const;
virtual void setLayers(uint32_t layers);
virtual bool shouldRenderLayer(uint32_t layer) const;
// ----------------

[[nodiscard]] virtual Diligent::IBuffer* uniforms() const;
[[nodiscard]] virtual Diligent::IBuffer* staticUniforms() const;
// RENDER TARGET ----
virtual void begin();
virtual void end();

[[nodiscard]] virtual Diligent::ITextureView* getDepth() const;
[[nodiscard]] virtual Diligent::ITextureView* getColor(bool rt = false) const;
[[nodiscard]] virtual rawrbox::TextureRender* getRenderTarget() const;
// ------------

virtual void initialize();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
namespace rawrbox {
class CameraPerspective : public rawrbox::CameraBase {
protected:
rawrbox::Vector2u _winSize = {};
float _FOV = 60.F;

void updateMtx() override;

public:
Expand All @@ -20,7 +18,7 @@ namespace rawrbox {
CameraPerspective& operator=(CameraPerspective&&) = default;
~CameraPerspective() override = default;

explicit CameraPerspective(const rawrbox::Vector2u& winSize, float FOV = 60.F, float near = 0.01F, float far = 100.F);
explicit CameraPerspective(const rawrbox::Vector2u& renderSize, float FOV = 60.F, float near = 0.01F, float far = 100.F, bool depth = true);

[[nodiscard]] rawrbox::Vector3f worldToScreen(const rawrbox::Vector3f& pos) const override;
[[nodiscard]] rawrbox::Vector3f screenToWorld(const rawrbox::Vector2f& screenPos, const rawrbox::Vector3f& origin = {0, 0, 0}) const override;
Expand Down
5 changes: 3 additions & 2 deletions rawrbox.render/include/rawrbox/render/plugins/base.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <rawrbox/math/vector2.hpp>
#include <rawrbox/render/cameras/base.hpp>
#include <rawrbox/render/enums/draw.hpp>
#include <rawrbox/render/textures/render.hpp>

Expand Down Expand Up @@ -40,8 +41,8 @@ namespace rawrbox {
virtual void resize(const rawrbox::Vector2u& renderSize);
virtual void upload();

virtual void preRender();
virtual void postRender(rawrbox::TextureRender& renderTarget);
virtual void preRender(const rawrbox::CameraBase& camera);
virtual void postRender(const rawrbox::CameraBase& camera);

virtual void update();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace rawrbox {
void signatures(std::vector<Diligent::PipelineResourceDesc>& sig) override;
void bindStatic(Diligent::IPipelineResourceSignature& sig) override;

void preRender() override;
void preRender(const rawrbox::CameraBase& camera) override;

std::string getID() override;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace rawrbox {
void signatures(std::vector<Diligent::PipelineResourceDesc>& sig) override;
void bindStatic(Diligent::IPipelineResourceSignature& sig) override;

void preRender() override;
void preRender(const rawrbox::CameraBase& camera) override;

[[nodiscard]] Diligent::IPipelineResourceSignature* getSignature(bool dynamic = true) const;
[[nodiscard]] Diligent::IShaderResourceBinding* getBind(bool dynamic = true) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace rawrbox {

void signatures(std::vector<Diligent::PipelineResourceDesc>& sig) override;
void bindStatic(Diligent::IPipelineResourceSignature& sig) override;
void postRender(rawrbox::TextureRender& render) override;
void postRender(const rawrbox::CameraBase& camera) override;

// PLUGIN UTILS ----
template <class T = rawrbox::PostProcessBase, typename... CallbackArgs>
Expand Down
7 changes: 7 additions & 0 deletions rawrbox.render/include/rawrbox/render/render_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@

#define RB_RENDER_THREAD_GROUP_SIZE (RB_RENDER_CLUSTERS_X_THREADS * RB_RENDER_CLUSTERS_Y_THREADS * RB_RENDER_CLUSTERS_Z_THREADS)
// ----------------

// CAMERA -----
// Add more layers if needed, you can then check if the camera should draw your model by checking if the layer is enabled
#define RB_RENDER_CAMERA_LAYERS(flag) \
flag(MY_LAYER_1, 1) \
flag(MY_LAYER_2, 2)
// ----------------
32 changes: 15 additions & 17 deletions rawrbox.render/include/rawrbox/render/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ namespace rawrbox {
std::map<std::string, std::unique_ptr<rawrbox::RenderPlugin>> _renderPlugins = {};
// --------------

// Post process renderer ----
std::unique_ptr<rawrbox::TextureRender> _render = nullptr;
// --------------------------

#ifdef _DEBUG
// QUERIES ---
std::unordered_map<std::string, std::unique_ptr<Diligent::ScopedQueryHelper>> _query = {};
Expand All @@ -64,8 +60,8 @@ namespace rawrbox {
std::map<std::string, rawrbox::RawrboxIntro> _introList = {{"./assets/textures/rawrbox.webp", {1.4F, false}}}; // rawrbox intro, always the first
//----

std::function<void(const rawrbox::DrawPass& pass)> _drawCall = nullptr;
std::function<void(const rawrbox::DrawPass& pass)> _tempRender = nullptr;
std::function<void(const rawrbox::CameraBase&, const rawrbox::DrawPass&)> _drawCall = nullptr;
std::function<void(const rawrbox::CameraBase&, const rawrbox::DrawPass&)> _tempRender = nullptr;

rawrbox::Colorf _clearColor = rawrbox::Colors::Black();

Expand All @@ -88,11 +84,14 @@ namespace rawrbox {

// OTHER HANDLES
std::unique_ptr<rawrbox::Logger> _logger = std::make_unique<rawrbox::Logger>("RawrBox-Renderer");
std::unique_ptr<rawrbox::CameraBase> _camera = nullptr;
std::unique_ptr<rawrbox::Stencil> _stencil = nullptr;
std::unique_ptr<rawrbox::TextureBLIT> _GPUBlit = nullptr;
// -------------

// CAMERAS ---
std::vector<std::unique_ptr<rawrbox::CameraBase>> _cameras = {};
// ----------

// INTRO ------
virtual void playIntro();
virtual void introComplete();
Expand Down Expand Up @@ -151,7 +150,7 @@ namespace rawrbox {
[[nodiscard]] virtual const std::map<std::string, std::unique_ptr<rawrbox::RenderPlugin>>& getPlugins() const;
// -----------------------------------

virtual void setDrawCall(std::function<void(const rawrbox::DrawPass& pass)> call);
virtual void setDrawCall(std::function<void(const rawrbox::CameraBase&, const rawrbox::DrawPass&)> call);

virtual void update();
virtual void render();
Expand All @@ -163,15 +162,17 @@ namespace rawrbox {

// CAMERA ------
template <class T = rawrbox::CameraBase, typename... CallbackArgs>
T* setupCamera(CallbackArgs&&... args) {
this->_camera = std::make_unique<T>(std::forward<CallbackArgs>(args)...);
this->setMainCamera(this->_camera.get());
T* createCamera(CallbackArgs&&... args) {
if (this->_initialized) CRITICAL_RAWRBOX("'createCamera' must be called before initializing the renderer!");
auto camera = std::make_unique<T>(std::forward<CallbackArgs>(args)...);

return dynamic_cast<T*>(this->_camera.get());
auto cameraPtr = camera.get();
this->_cameras.push_back(std::move(camera));
return dynamic_cast<T*>(cameraPtr);
}

virtual void setMainCamera(rawrbox::CameraBase* camera) const;
[[nodiscard]] virtual rawrbox::CameraBase* camera() const;
virtual void setActiveCamera(rawrbox::CameraBase* camera) const;
[[nodiscard]] virtual rawrbox::CameraBase* getActiveCamera() const;
// ----------------

// Utils ----
Expand All @@ -184,9 +185,6 @@ namespace rawrbox {
// WARNING: NOT THREAD SAFE!!
[[nodiscard]] virtual Diligent::IRenderDevice* device() const;

[[nodiscard]] virtual Diligent::ITextureView* getDepth() const;
[[nodiscard]] virtual Diligent::ITextureView* getColor(bool rt = false) const;

[[nodiscard]] virtual std::filesystem::path getShadersDirectory() const;
[[nodiscard]] virtual const Diligent::RENDER_DEVICE_TYPE& getRenderType() const;

Expand Down
32 changes: 9 additions & 23 deletions rawrbox.render/src/bindless.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ namespace rawrbox {

// Create signatures ------
createSignatures();
// -------------

// Bind signatures ------
bindSignatures();
// -------------
// ------------
}

void BindlessManager::shutdown() {
Expand All @@ -111,8 +107,6 @@ namespace rawrbox {
// SIGNATURES ---------
void BindlessManager::createSignatures() {
auto* renderer = rawrbox::RENDERER;

auto* camera = renderer->camera();
auto* device = renderer->device();

Diligent::PipelineResourceSignatureDesc PRSDesc;
Expand All @@ -127,12 +121,9 @@ namespace rawrbox {

{Diligent::SHADER_TYPE_PIXEL, "Constants", 1, Diligent::SHADER_RESOURCE_TYPE_CONSTANT_BUFFER, Diligent::SHADER_RESOURCE_VARIABLE_TYPE_STATIC},
{Diligent::SHADER_TYPE_PIXEL, "g_Textures", RB_RENDER_MAX_TEXTURES, Diligent::SHADER_RESOURCE_TYPE_TEXTURE_SRV, Diligent::SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, Diligent::PIPELINE_RESOURCE_FLAG_RUNTIME_ARRAY},
};

if (camera != nullptr) {
resources.emplace_back(Diligent::SHADER_TYPE_VERTEX | Diligent::SHADER_TYPE_PIXEL | Diligent::SHADER_TYPE_GEOMETRY, "Camera", 1, Diligent::SHADER_RESOURCE_TYPE_CONSTANT_BUFFER, Diligent::SHADER_RESOURCE_VARIABLE_TYPE_STATIC, Diligent::PIPELINE_RESOURCE_FLAG_NO_DYNAMIC_BUFFERS);
resources.emplace_back(Diligent::SHADER_TYPE_VERTEX | Diligent::SHADER_TYPE_PIXEL | Diligent::SHADER_TYPE_GEOMETRY, "SCamera", 1, Diligent::SHADER_RESOURCE_TYPE_CONSTANT_BUFFER, Diligent::SHADER_RESOURCE_VARIABLE_TYPE_STATIC, Diligent::PIPELINE_RESOURCE_FLAG_NO_DYNAMIC_BUFFERS);
}
{Diligent::SHADER_TYPE_VERTEX | Diligent::SHADER_TYPE_PIXEL | Diligent::SHADER_TYPE_GEOMETRY, "Camera", 1, Diligent::SHADER_RESOURCE_TYPE_CONSTANT_BUFFER, Diligent::SHADER_RESOURCE_VARIABLE_TYPE_STATIC, Diligent::PIPELINE_RESOURCE_FLAG_NO_DYNAMIC_BUFFERS},
{Diligent::SHADER_TYPE_VERTEX | Diligent::SHADER_TYPE_PIXEL | Diligent::SHADER_TYPE_GEOMETRY, "SCamera", 1, Diligent::SHADER_RESOURCE_TYPE_CONSTANT_BUFFER, Diligent::SHADER_RESOURCE_VARIABLE_TYPE_STATIC, Diligent::PIPELINE_RESOURCE_FLAG_NO_DYNAMIC_BUFFERS}};

// Add extra signatures ----
for (const auto& plugin : renderer->getPlugins()) {
Expand Down Expand Up @@ -162,22 +153,17 @@ namespace rawrbox {
}

void BindlessManager::bindSignatures() {
auto* renderer = rawrbox::RENDERER;
auto* camera = renderer->camera();

// Setup graphic binds ---
signature->GetStaticVariableByName(Diligent::SHADER_TYPE_VERTEX, "Constants")->Set(signatureBufferVertex);
signature->GetStaticVariableByName(Diligent::SHADER_TYPE_VERTEX, "SkinnedConstants")->Set(signatureBufferVertexSkinned);

if (camera != nullptr) {
signature->GetStaticVariableByName(Diligent::SHADER_TYPE_VERTEX, "Camera")->Set(camera->uniforms());
signature->GetStaticVariableByName(Diligent::SHADER_TYPE_PIXEL, "Camera")->Set(camera->uniforms());
signature->GetStaticVariableByName(Diligent::SHADER_TYPE_GEOMETRY, "Camera")->Set(camera->uniforms());
signature->GetStaticVariableByName(Diligent::SHADER_TYPE_VERTEX, "Camera")->Set(rawrbox::CameraBase::uniforms);
signature->GetStaticVariableByName(Diligent::SHADER_TYPE_PIXEL, "Camera")->Set(rawrbox::CameraBase::uniforms);
signature->GetStaticVariableByName(Diligent::SHADER_TYPE_GEOMETRY, "Camera")->Set(rawrbox::CameraBase::uniforms);

signature->GetStaticVariableByName(Diligent::SHADER_TYPE_VERTEX, "SCamera")->Set(camera->staticUniforms());
signature->GetStaticVariableByName(Diligent::SHADER_TYPE_PIXEL, "SCamera")->Set(camera->staticUniforms());
signature->GetStaticVariableByName(Diligent::SHADER_TYPE_GEOMETRY, "SCamera")->Set(camera->staticUniforms());
}
signature->GetStaticVariableByName(Diligent::SHADER_TYPE_VERTEX, "SCamera")->Set(rawrbox::CameraBase::staticUniforms);
signature->GetStaticVariableByName(Diligent::SHADER_TYPE_PIXEL, "SCamera")->Set(rawrbox::CameraBase::staticUniforms);
signature->GetStaticVariableByName(Diligent::SHADER_TYPE_GEOMETRY, "SCamera")->Set(rawrbox::CameraBase::staticUniforms);

signature->GetStaticVariableByName(Diligent::SHADER_TYPE_PIXEL, "Constants")->Set(signatureBufferPixel);
// ------------
Expand Down
Loading

0 comments on commit c798629

Please sign in to comment.