Commit c60c4cb1 authored by chili's avatar chili
Browse files

unified bind container with shared_ptr

parent dd684b5e
...@@ -11,22 +11,16 @@ void Drawable::Draw( Graphics& gfx ) const noxnd ...@@ -11,22 +11,16 @@ void Drawable::Draw( Graphics& gfx ) const noxnd
{ {
b->Bind( gfx ); b->Bind( gfx );
} }
for( auto& b : GetStaticBinds() )
{
b->Bind( gfx );
}
gfx.DrawIndexed( pIndexBuffer->GetCount() ); gfx.DrawIndexed( pIndexBuffer->GetCount() );
} }
void Drawable::AddBind( std::unique_ptr<Bindable> bind ) noxnd void Drawable::AddBind( std::shared_ptr<Bindable> bind ) noxnd
{ {
assert( "*Must* use AddIndexBuffer to bind index buffer" && typeid(*bind) != typeid(IndexBuffer) ); // special case for index buffer
if( typeid(*bind) == typeid(IndexBuffer) )
{
assert( "Binding multiple index buffers not allowed" && pIndexBuffer == nullptr );
pIndexBuffer = &static_cast<IndexBuffer&>(*bind);
}
binds.push_back( std::move( bind ) ); binds.push_back( std::move( bind ) );
} }
\ No newline at end of file
void Drawable::AddIndexBuffer( std::unique_ptr<IndexBuffer> ibuf ) noxnd
{
assert( "Attempting to add index buffer a second time" && pIndexBuffer == nullptr );
pIndexBuffer = ibuf.get();
binds.push_back( std::move( ibuf ) );
}
\ No newline at end of file
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "Graphics.h" #include "Graphics.h"
#include <DirectXMath.h> #include <DirectXMath.h>
#include "ConditionalNoexcept.h" #include "ConditionalNoexcept.h"
#include <memory>
namespace Bind namespace Bind
{ {
...@@ -11,15 +12,11 @@ namespace Bind ...@@ -11,15 +12,11 @@ namespace Bind
class Drawable class Drawable
{ {
template<class T>
friend class DrawableBase;
public: public:
Drawable() = default; Drawable() = default;
Drawable( const Drawable& ) = delete; Drawable( const Drawable& ) = delete;
virtual DirectX::XMMATRIX GetTransformXM() const noexcept = 0; virtual DirectX::XMMATRIX GetTransformXM() const noexcept = 0;
void Draw( Graphics& gfx ) const noxnd; void Draw( Graphics& gfx ) const noxnd;
virtual void Update( float dt ) noexcept
{}
virtual ~Drawable() = default; virtual ~Drawable() = default;
protected: protected:
template<class T> template<class T>
...@@ -34,11 +31,8 @@ protected: ...@@ -34,11 +31,8 @@ protected:
} }
return nullptr; return nullptr;
} }
void AddBind( std::unique_ptr<Bind::Bindable> bind ) noxnd; void AddBind( std::shared_ptr<Bind::Bindable> bind ) noxnd;
void AddIndexBuffer( std::unique_ptr<Bind::IndexBuffer> ibuf ) noxnd;
private:
virtual const std::vector<std::unique_ptr<Bind::Bindable>>& GetStaticBinds() const noexcept = 0;
private: private:
const Bind::IndexBuffer* pIndexBuffer = nullptr; const Bind::IndexBuffer* pIndexBuffer = nullptr;
std::vector<std::unique_ptr<Bind::Bindable>> binds; std::vector<std::shared_ptr<Bind::Bindable>> binds;
}; };
\ No newline at end of file
#pragma once
#include "Drawable.h"
#include "IndexBuffer.h"
#include "ConditionalNoexcept.h"
template<class T>
class DrawableBase : public Drawable
{
protected:
static bool IsStaticInitialized() noexcept
{
return !staticBinds.empty();
}
static void AddStaticBind( std::unique_ptr<Bind::Bindable> bind ) noxnd
{
assert( "*Must* use AddStaticIndexBuffer to bind index buffer" && typeid(*bind) != typeid(Bind::IndexBuffer) );
staticBinds.push_back( std::move( bind ) );
}
void AddStaticIndexBuffer( std::unique_ptr<Bind::IndexBuffer> ibuf ) noxnd
{
assert( "Attempting to add index buffer a second time" && pIndexBuffer == nullptr );
pIndexBuffer = ibuf.get();
staticBinds.push_back( std::move( ibuf ) );
}
void SetIndexFromStatic() noxnd
{
assert( "Attempting to add index buffer a second time" && pIndexBuffer == nullptr );
for( const auto& b : staticBinds )
{
if( const auto p = dynamic_cast<Bind::IndexBuffer*>(b.get()) )
{
pIndexBuffer = p;
return;
}
}
assert( "Failed to find index buffer in static binds" && pIndexBuffer != nullptr );
}
private:
const std::vector<std::unique_ptr<Bind::Bindable>>& GetStaticBinds() const noexcept override
{
return staticBinds;
}
private:
static std::vector<std::unique_ptr<Bind::Bindable>> staticBinds;
};
template<class T>
std::vector<std::unique_ptr<Bind::Bindable>> DrawableBase<T>::staticBinds;
\ No newline at end of file
...@@ -32,27 +32,16 @@ const std::string& ModelException::GetNote() const noexcept ...@@ -32,27 +32,16 @@ const std::string& ModelException::GetNote() const noexcept
} }
// Mesh // Mesh
Mesh::Mesh( Graphics& gfx,std::vector<std::unique_ptr<Bind::Bindable>> bindPtrs ) Mesh::Mesh( Graphics& gfx,std::vector<std::shared_ptr<Bind::Bindable>> bindPtrs )
{ {
if( !IsStaticInitialized() ) AddBind( std::make_shared<Bind::Topology>( gfx,D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) );
{
AddStaticBind( std::make_unique<Bind::Topology>( gfx,D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) );
}
for( auto& pb : bindPtrs ) for( auto& pb : bindPtrs )
{
if( auto pi = dynamic_cast<Bind::IndexBuffer*>(pb.get()) )
{
AddIndexBuffer( std::unique_ptr<Bind::IndexBuffer>{ pi } );
pb.release();
}
else
{ {
AddBind( std::move( pb ) ); AddBind( std::move( pb ) );
} }
}
AddBind( std::make_unique<Bind::TransformCbuf>( gfx,*this ) ); AddBind( std::make_shared<Bind::TransformCbuf>( gfx,*this ) );
} }
void Mesh::Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const noxnd void Mesh::Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const noxnd
{ {
...@@ -268,7 +257,7 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh,const a ...@@ -268,7 +257,7 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh,const a
indices.push_back( face.mIndices[2] ); indices.push_back( face.mIndices[2] );
} }
std::vector<std::unique_ptr<Bind::Bindable>> bindablePtrs; std::vector<std::shared_ptr<Bind::Bindable>> bindablePtrs;
bool hasSpecularMap = false; bool hasSpecularMap = false;
float shininess = 35.0f; float shininess = 35.0f;
...@@ -281,11 +270,11 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh,const a ...@@ -281,11 +270,11 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh,const a
aiString texFileName; aiString texFileName;
material.GetTexture( aiTextureType_DIFFUSE,0,&texFileName ); material.GetTexture( aiTextureType_DIFFUSE,0,&texFileName );
bindablePtrs.push_back( std::make_unique<Bind::Texture>( gfx,Surface::FromFile( base + texFileName.C_Str() ) ) ); bindablePtrs.push_back( std::make_shared<Bind::Texture>( gfx,Surface::FromFile( base + texFileName.C_Str() ) ) );
if( material.GetTexture( aiTextureType_SPECULAR,0,&texFileName ) == aiReturn_SUCCESS ) if( material.GetTexture( aiTextureType_SPECULAR,0,&texFileName ) == aiReturn_SUCCESS )
{ {
bindablePtrs.push_back( std::make_unique<Bind::Texture>( gfx,Surface::FromFile( base + texFileName.C_Str() ),1 ) ); bindablePtrs.push_back( std::make_shared<Bind::Texture>( gfx,Surface::FromFile( base + texFileName.C_Str() ),1 ) );
hasSpecularMap = true; hasSpecularMap = true;
} }
else else
...@@ -293,26 +282,26 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh,const a ...@@ -293,26 +282,26 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh,const a
material.Get( AI_MATKEY_SHININESS,shininess ); material.Get( AI_MATKEY_SHININESS,shininess );
} }
bindablePtrs.push_back( std::make_unique<Bind::Sampler>( gfx ) ); bindablePtrs.push_back( std::make_shared<Bind::Sampler>( gfx ) );
} }
bindablePtrs.push_back( std::make_unique<Bind::VertexBuffer>( gfx,vbuf ) ); bindablePtrs.push_back( std::make_shared<Bind::VertexBuffer>( gfx,vbuf ) );
bindablePtrs.push_back( std::make_unique<Bind::IndexBuffer>( gfx,indices ) ); bindablePtrs.push_back( std::make_shared<Bind::IndexBuffer>( gfx,indices ) );
auto pvs = std::make_unique<Bind::VertexShader>( gfx,L"PhongVS.cso" ); auto pvs = std::make_shared<Bind::VertexShader>( gfx,L"PhongVS.cso" );
auto pvsbc = pvs->GetBytecode(); auto pvsbc = pvs->GetBytecode();
bindablePtrs.push_back( std::move( pvs ) ); bindablePtrs.push_back( std::move( pvs ) );
bindablePtrs.push_back( std::make_unique<Bind::InputLayout>( gfx,vbuf.GetLayout().GetD3DLayout(),pvsbc ) ); bindablePtrs.push_back( std::make_shared<Bind::InputLayout>( gfx,vbuf.GetLayout().GetD3DLayout(),pvsbc ) );
if( hasSpecularMap ) if( hasSpecularMap )
{ {
bindablePtrs.push_back( std::make_unique<Bind::PixelShader>( gfx,L"PhongPSSpecMap.cso" ) ); bindablePtrs.push_back( std::make_shared<Bind::PixelShader>( gfx,L"PhongPSSpecMap.cso" ) );
} }
else else
{ {
bindablePtrs.push_back( std::make_unique<Bind::PixelShader>( gfx,L"PhongPS.cso" ) ); bindablePtrs.push_back( std::make_shared<Bind::PixelShader>( gfx,L"PhongPS.cso" ) );
struct PSMaterialConstant struct PSMaterialConstant
{ {
...@@ -321,7 +310,7 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh,const a ...@@ -321,7 +310,7 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh,const a
float padding[2]; float padding[2];
} pmc; } pmc;
pmc.specularPower = shininess; pmc.specularPower = shininess;
bindablePtrs.push_back( std::make_unique<Bind::PixelConstantBuffer<PSMaterialConstant>>( gfx,pmc,1u ) ); bindablePtrs.push_back( std::make_shared<Bind::PixelConstantBuffer<PSMaterialConstant>>( gfx,pmc,1u ) );
} }
return std::make_unique<Mesh>( gfx,std::move( bindablePtrs ) ); return std::make_unique<Mesh>( gfx,std::move( bindablePtrs ) );
......
#pragma once #pragma once
#include "DrawableBase.h" #include "Drawable.h"
#include "BindableCommon.h" #include "BindableCommon.h"
#include "Vertex.h" #include "Vertex.h"
#include <optional> #include <optional>
...@@ -20,10 +20,10 @@ private: ...@@ -20,10 +20,10 @@ private:
std::string note; std::string note;
}; };
class Mesh : public DrawableBase<Mesh> class Mesh : public Drawable
{ {
public: public:
Mesh( Graphics& gfx,std::vector<std::unique_ptr<Bind::Bindable>> bindPtrs ); Mesh( Graphics& gfx,std::vector<std::shared_ptr<Bind::Bindable>> bindPtrs );
void Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const noxnd; void Draw( Graphics& gfx,DirectX::FXMMATRIX accumulatedTransform ) const noxnd;
DirectX::XMMATRIX GetTransformXM() const noexcept override; DirectX::XMMATRIX GetTransformXM() const noexcept override;
private: private:
......
...@@ -9,48 +9,39 @@ SolidSphere::SolidSphere( Graphics& gfx,float radius ) ...@@ -9,48 +9,39 @@ SolidSphere::SolidSphere( Graphics& gfx,float radius )
using namespace Bind; using namespace Bind;
namespace dx = DirectX; namespace dx = DirectX;
if( !IsStaticInitialized() )
{
struct Vertex struct Vertex
{ {
dx::XMFLOAT3 pos; dx::XMFLOAT3 pos;
}; };
auto model = Sphere::Make<Vertex>(); auto model = Sphere::Make<Vertex>();
model.Transform( dx::XMMatrixScaling( radius,radius,radius ) ); model.Transform( dx::XMMatrixScaling( radius,radius,radius ) );
AddBind( std::make_unique<VertexBuffer>( gfx,model.vertices ) ); AddBind( std::make_shared<VertexBuffer>( gfx,model.vertices ) );
AddIndexBuffer( std::make_unique<IndexBuffer>( gfx,model.indices ) ); AddBind( std::make_shared<IndexBuffer>( gfx,model.indices ) );
auto pvs = std::make_unique<VertexShader>( gfx,L"SolidVS.cso" ); auto pvs = std::make_shared<VertexShader>( gfx,L"SolidVS.cso" );
auto pvsbc = pvs->GetBytecode(); auto pvsbc = pvs->GetBytecode();
AddStaticBind( std::move( pvs ) ); AddBind( std::move( pvs ) );
AddStaticBind( std::make_unique<PixelShader>( gfx,L"SolidPS.cso" ) ); AddBind( std::make_shared<PixelShader>( gfx,L"SolidPS.cso" ) );
struct PSColorConstant struct PSColorConstant
{ {
dx::XMFLOAT3 color = { 1.0f,1.0f,1.0f }; dx::XMFLOAT3 color = { 1.0f,1.0f,1.0f };
float padding; float padding;
} colorConst; } colorConst;
AddStaticBind( std::make_unique<PixelConstantBuffer<PSColorConstant>>( gfx,colorConst ) ); AddBind( std::make_shared<PixelConstantBuffer<PSColorConstant>>( gfx,colorConst ) );
const std::vector<D3D11_INPUT_ELEMENT_DESC> ied = const std::vector<D3D11_INPUT_ELEMENT_DESC> ied =
{ {
{ "Position",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 }, { "Position",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
}; };
AddStaticBind( std::make_unique<InputLayout>( gfx,ied,pvsbc ) ); AddBind( std::make_shared<InputLayout>( gfx,ied,pvsbc ) );
AddStaticBind( std::make_unique<Topology>( gfx,D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) ); AddBind( std::make_shared<Topology>( gfx,D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) );
}
else
{
SetIndexFromStatic();
}
AddBind( std::make_unique<TransformCbuf>( gfx,*this ) ); AddBind( std::make_shared<TransformCbuf>( gfx,*this ) );
} }
void SolidSphere::Update( float dt ) noexcept {}
void SolidSphere::SetPos( DirectX::XMFLOAT3 pos ) noexcept void SolidSphere::SetPos( DirectX::XMFLOAT3 pos ) noexcept
{ {
this->pos = pos; this->pos = pos;
......
#pragma once #pragma once
#include "DrawableBase.h" #include "Drawable.h"
class SolidSphere : public DrawableBase<SolidSphere> class SolidSphere : public Drawable
{ {
public: public:
SolidSphere( Graphics& gfx,float radius ); SolidSphere( Graphics& gfx,float radius );
void Update( float dt ) noexcept override;
void SetPos( DirectX::XMFLOAT3 pos ) noexcept; void SetPos( DirectX::XMFLOAT3 pos ) noexcept;
DirectX::XMMATRIX GetTransformXM() const noexcept override; DirectX::XMMATRIX GetTransformXM() const noexcept override;
private: private:
......
...@@ -141,7 +141,6 @@ ...@@ -141,7 +141,6 @@
<ClInclude Include="ConditionalNoexcept.h" /> <ClInclude Include="ConditionalNoexcept.h" />
<ClInclude Include="ConstantBuffers.h" /> <ClInclude Include="ConstantBuffers.h" />
<ClInclude Include="Drawable.h" /> <ClInclude Include="Drawable.h" />
<ClInclude Include="DrawableBase.h" />
<ClInclude Include="dxerr.h" /> <ClInclude Include="dxerr.h" />
<ClInclude Include="DxgiInfoManager.h" /> <ClInclude Include="DxgiInfoManager.h" />
<ClInclude Include="GDIPlusManager.h" /> <ClInclude Include="GDIPlusManager.h" />
......
...@@ -221,9 +221,6 @@ ...@@ -221,9 +221,6 @@
<ClInclude Include="Drawable.h"> <ClInclude Include="Drawable.h">
<Filter>Header Files\Drawable</Filter> <Filter>Header Files\Drawable</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="DrawableBase.h">
<Filter>Header Files\Drawable</Filter>
</ClInclude>
<ClInclude Include="WindowsThrowMacros.h"> <ClInclude Include="WindowsThrowMacros.h">
<Filter>Header Files\Macros</Filter> <Filter>Header Files\Macros</Filter>
</ClInclude> </ClInclude>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment