Skip to content

Commit

Permalink
Remove unecessary template parameters for constant buffer. Rename Mvp…
Browse files Browse the repository at this point in the history
…CBuffer
  • Loading branch information
saffronjam committed Aug 14, 2021
1 parent c5d8228 commit 08ce499
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 208 deletions.
2 changes: 1 addition & 1 deletion Engine/Source/Saffron/Graphics/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Scene
EditorCamera _camera2;
std::shared_ptr<Mesh> _cameraMesh;
EditorCamera* _activeCamera = &_camera1;
bool _drawCameraFrustums;
bool _drawCameraFrustums = false;


SceneRenderer _sceneRenderer;
Expand Down
2 changes: 1 addition & 1 deletion Engine/Source/Saffron/Rendering/Bindables.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "Saffron/Rendering/Bindables/Framebuffer.h"
#include "Saffron/Rendering/Bindables/IndexBuffer.h"
#include "Saffron/Rendering/Bindables/InputLayout.h"
#include "Saffron/Rendering/Bindables/MvpCBuffer.h"
#include "Saffron/Rendering/Bindables/TransformCBuffer.h"
#include "Saffron/Rendering/Bindables/Sampler.h"
#include "Saffron/Rendering/Bindables/Shader.h"
#include "Saffron/Rendering/Bindables/Texture.h"
Expand Down
241 changes: 93 additions & 148 deletions Engine/Source/Saffron/Rendering/Bindables/ConstantBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ enum ConstantBufferBindFlags_
ConstantBufferBindFlags_PS = 1 << 1
};

template <class T, bool Deferred = true>
template <class T>
class ConstantBuffer : public Bindable
{
public:
explicit ConstantBuffer(uint slot);
explicit ConstantBuffer(const T& initialData, uint slot);

void Bind() const override;
Expand All @@ -33,6 +34,7 @@ class ConstantBuffer : public Bindable
auto Data() -> T&;
auto Data() const -> const T&;

static auto Create(uint slot = 0) -> std::shared_ptr<ConstantBuffer>;
static auto Create(const T& initialData, uint slot = 0) -> std::shared_ptr<ConstantBuffer>;

protected:
Expand All @@ -46,190 +48,133 @@ class ConstantBuffer : public Bindable
ConstantBufferBindFlags _bindFlags = ConstantBufferBindFlags_VS;
};

template <class T, bool Deferred>
ConstantBuffer<T, Deferred>::ConstantBuffer(const T& initialData, uint slot) :
_slot(slot),
_data(initialData)
template <class T>
ConstantBuffer<T>::ConstantBuffer(uint slot) :
_slot(slot)
{
}

template <class T>
ConstantBuffer<T>::ConstantBuffer(const T& initialData, uint slot) :
_data(initialData),
_slot(slot)
{
SetInitializer(
[this]
{
if constexpr (Deferred)
{
const auto inst = ShareThisAs<ConstantBuffer>();
Renderer::Submit(
[inst](const RendererPackage& package)
{
D3D11_BUFFER_DESC bd;
bd.Usage = D3D11_USAGE_DYNAMIC;
bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
bd.MiscFlags = 0;
bd.ByteWidth = sizeof T;
bd.StructureByteStride = 0;

D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = &inst->_data;

const auto hr = Renderer::Device().CreateBuffer(&bd, &sd, &inst->_nativeBuffer);
ThrowIfBad(hr);
}
);
}
else
{
D3D11_BUFFER_DESC bd;
bd.Usage = D3D11_USAGE_DYNAMIC;
bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
bd.MiscFlags = 0;
bd.ByteWidth = sizeof T;
bd.StructureByteStride = 0;

D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = &_data;

const auto hr = Renderer::Device().CreateBuffer(&bd, &sd, &_nativeBuffer);
ThrowIfBad(hr);
}
const auto inst = ShareThisAs<ConstantBuffer>();
Renderer::Submit(
[inst](const RendererPackage& package)
{
D3D11_BUFFER_DESC bd;
bd.Usage = D3D11_USAGE_DYNAMIC;
bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
bd.MiscFlags = 0;
bd.ByteWidth = sizeof T;
bd.StructureByteStride = 0;

D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = &inst->_data;

const auto hr = package.Device.CreateBuffer(&bd, &sd, &inst->_nativeBuffer);
ThrowIfBad(hr);
}
);
}
);
}

template <class T, bool Deferred>
void ConstantBuffer<T, Deferred>::Bind() const
template <class T>
void ConstantBuffer<T>::Bind() const
{
if constexpr (Deferred)
{
const auto inst = ShareThisAs<const ConstantBuffer<T, Deferred>>();
const auto inst = ShareThisAs<const ConstantBuffer<T>>();

if (inst->_dirty)
Renderer::Submit(
[inst](const RendererPackage& package)
{
const_cast<ConstantBuffer<T, Deferred>&>(*inst).UploadData();
}

Renderer::Submit(
[inst](const RendererPackage& package)
if (inst->_dirty)
{
if (inst->_bindFlags & ConstantBufferBindFlags_VS)
{
package.Context.VSSetConstantBuffers(inst->_slot, 1, inst->_nativeBuffer.GetAddressOf());
}
if (inst->_bindFlags & ConstantBufferBindFlags_PS)
{
package.Context.PSSetConstantBuffers(inst->_slot, 1, inst->_nativeBuffer.GetAddressOf());
}
const_cast<ConstantBuffer<T>&>(*inst).UploadData();
}
);
}
else
{
if (_dirty)
{
const_cast<ConstantBuffer<T, Deferred>&>(*this).UploadData();
}

auto& context = Renderer::Context();
if (_bindFlags & ConstantBufferBindFlags_VS)
{
context.VSSetConstantBuffers(_slot, 1, _nativeBuffer.GetAddressOf());
}
if (_bindFlags & ConstantBufferBindFlags_PS)
{
context.PSSetConstantBuffers(_slot, 1, _nativeBuffer.GetAddressOf());
if (inst->_bindFlags & ConstantBufferBindFlags_VS)
{
package.Context.VSSetConstantBuffers(inst->_slot, 1, inst->_nativeBuffer.GetAddressOf());
}
if (inst->_bindFlags & ConstantBufferBindFlags_PS)
{
package.Context.PSSetConstantBuffers(inst->_slot, 1, inst->_nativeBuffer.GetAddressOf());
}
}
}
);
}

template <class T, bool Deferred>
void ConstantBuffer<T, Deferred>::Update(const T& data)
template <class T>
void ConstantBuffer<T>::Update(const T& data)
{
if constexpr (Deferred)
{
const auto inst = ShareThisAs<ConstantBuffer<T, Deferred>>();
Renderer::Submit(
[=](const RendererPackage& package)
{
inst->_data = data;
inst->_dirty = true;
}
);
}
else
{
_data = data;
_dirty = true;
}
const auto inst = ShareThisAs<ConstantBuffer<T>>();
Renderer::Submit(
[=](const RendererPackage& package)
{
inst->_data = data;
inst->_dirty = true;
}
);
}

template <class T, bool Deferred>
void ConstantBuffer<T, Deferred>::UploadData()
template <class T>
void ConstantBuffer<T>::UploadData()
{
if constexpr (Deferred)
{
const auto inst = ShareThisAs<ConstantBuffer<T, Deferred>>();
Renderer::Submit(
[inst](const RendererPackage& package)
{
D3D11_MAPPED_SUBRESOURCE mr = {};
const auto hr = package.Context.Map(inst->_nativeBuffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mr);
ThrowIfBad(hr);

std::memcpy(mr.pData, &inst->_data, sizeof T);

package.Context.Unmap(inst->_nativeBuffer.Get(), 0);

inst->_dirty = false;
}
);
}
else
{
D3D11_MAPPED_SUBRESOURCE mr = {};
const auto hr = Renderer::Context().Map(_nativeBuffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mr);
ThrowIfBad(hr);
const auto inst = ShareThisAs<ConstantBuffer<T>>();
Renderer::Submit(
[inst](const RendererPackage& package)
{
D3D11_MAPPED_SUBRESOURCE mr = {};
const auto hr = package.Context.Map(inst->_nativeBuffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mr);
ThrowIfBad(hr);

std::memcpy(mr.pData, &_data, sizeof T);
std::memcpy(mr.pData, &inst->_data, sizeof T);

Renderer::Context().Unmap(_nativeBuffer.Get(), 0);
package.Context.Unmap(inst->_nativeBuffer.Get(), 0);

_dirty = false;
}
inst->_dirty = false;
}
);
}

template <class T, bool Deferred>
void ConstantBuffer<T, Deferred>::SetBindFlags(ConstantBufferBindFlags bindFlags)
template <class T>
void ConstantBuffer<T>::SetBindFlags(ConstantBufferBindFlags bindFlags)
{
if constexpr (Deferred)
{
const auto inst = ShareThisAs<ConstantBuffer>();
Renderer::Submit(
[=](const RendererPackage& package)
{
inst->_bindFlags = bindFlags;
}
);
}
else
{
_bindFlags = bindFlags;
}
const auto inst = ShareThisAs<ConstantBuffer>();
Renderer::Submit(
[=](const RendererPackage& package)
{
inst->_bindFlags = bindFlags;
}
);
}

template <class T, bool Deferred>
auto ConstantBuffer<T, Deferred>::Data() -> T&
template <class T>
auto ConstantBuffer<T>::Data() -> T&
{
return _data;
}

template <class T, bool Deferred>
auto ConstantBuffer<T, Deferred>::Data() const -> const T&
template <class T>
auto ConstantBuffer<T>::Data() const -> const T&
{
return const_cast<ConstantBuffer<T>&>(*this).Data();
}

template <class T, bool Deferred>
auto ConstantBuffer<T, Deferred>::Create(const T& initialData, uint slot) -> std::shared_ptr<ConstantBuffer>
template <class T>
auto ConstantBuffer<T>::Create(uint slot) -> std::shared_ptr<ConstantBuffer>
{
return BindableStore::Add<ConstantBuffer>(slot);
}

template <class T>
auto ConstantBuffer<T>::Create(const T& initialData, uint slot) -> std::shared_ptr<ConstantBuffer>
{
return BindableStore::Add<ConstantBuffer>(initialData, slot);
}
Expand Down
40 changes: 0 additions & 40 deletions Engine/Source/Saffron/Rendering/Bindables/MvpCBuffer.cpp

This file was deleted.

Loading

0 comments on commit 08ce499

Please sign in to comment.