Commit c87ca0bc authored by chili's avatar chili
Browse files

better system for bindable codex

parent 939f5af6
...@@ -18,7 +18,8 @@ App::App() ...@@ -18,7 +18,8 @@ App::App()
{ {
wnd.Gfx().SetProjection( dx::XMMatrixPerspectiveLH( 1.0f,9.0f / 16.0f,0.5f,40.0f ) ); wnd.Gfx().SetProjection( dx::XMMatrixPerspectiveLH( 1.0f,9.0f / 16.0f,0.5f,40.0f ) );
auto a = Bind::VertexShader::Resolve( wnd.Gfx(),"PhongVS.cso" ); auto a = Bind::VertexShader::Resolve( wnd.Gfx(),"PhongVS.cso" );
auto b = Bind::VertexShader::Resolve( wnd.Gfx(),"PhongVS.cso" ); auto b = Bind::Sampler::Resolve( wnd.Gfx() );
auto c = Bind::Sampler::Resolve( wnd.Gfx() );
} }
void App::DoFrame() void App::DoFrame()
......
...@@ -10,31 +10,28 @@ namespace Bind ...@@ -10,31 +10,28 @@ namespace Bind
class Codex class Codex
{ {
public: public:
static std::shared_ptr<Bindable> Resolve( const std::string& key ) noxnd template<class T,typename...Params>
static std::shared_ptr<Bindable> Resolve( Graphics& gfx,Params&&...p ) noxnd
{ {
return Get().Resolve_( key ); return Get().Resolve_<T>( gfx,std::forward<Params>( p )... );
}
static void Store( std::shared_ptr<Bindable> bind )
{
Get().Store_( std::move( bind ) );
} }
private: private:
std::shared_ptr<Bindable> Resolve_( const std::string& key ) const noxnd template<class T,typename...Params>
std::shared_ptr<Bindable> Resolve_( Graphics& gfx,Params&&...p ) noxnd
{ {
auto i = binds.find( key ); const auto key = T::GenerateUID( std::forward<Params>( p )... );
const auto i = binds.find( key );
if( i == binds.end() ) if( i == binds.end() )
{ {
return {}; auto bind = std::make_shared<T>( gfx,std::forward<Params>( p )... );
binds[key] = bind;
return bind;
} }
else else
{ {
return i->second; return i->second;
} }
} }
void Store_( std::shared_ptr<Bindable> bind )
{
binds[bind->GetUID()] = std::move( bind );
}
static Codex& Get() static Codex& Get()
{ {
static Codex codex; static Codex codex;
......
#include "Sampler.h" #include "Sampler.h"
#include "GraphicsThrowMacros.h" #include "GraphicsThrowMacros.h"
#include "BindableCodex.h"
namespace Bind namespace Bind
{ {
...@@ -20,4 +21,16 @@ namespace Bind ...@@ -20,4 +21,16 @@ namespace Bind
{ {
GetContext( gfx )->PSSetSamplers( 0,1,pSampler.GetAddressOf() ); GetContext( gfx )->PSSetSamplers( 0,1,pSampler.GetAddressOf() );
} }
std::shared_ptr<Bindable> Sampler::Resolve( Graphics& gfx )
{
return Codex::Resolve<Sampler>( gfx );
}
std::string Sampler::GenerateUID()
{
return typeid(Sampler).name();
}
std::string Sampler::GetUID() const noexcept
{
return GenerateUID();
}
} }
\ No newline at end of file
...@@ -8,6 +8,9 @@ namespace Bind ...@@ -8,6 +8,9 @@ namespace Bind
public: public:
Sampler( Graphics& gfx ); Sampler( Graphics& gfx );
void Bind( Graphics& gfx ) noexcept override; void Bind( Graphics& gfx ) noexcept override;
static std::shared_ptr<Bindable> Resolve( Graphics& gfx );
static std::string GenerateUID();
std::string GetUID() const noexcept override;
protected: protected:
Microsoft::WRL::ComPtr<ID3D11SamplerState> pSampler; Microsoft::WRL::ComPtr<ID3D11SamplerState> pSampler;
}; };
......
...@@ -5,8 +5,6 @@ ...@@ -5,8 +5,6 @@
namespace Bind namespace Bind
{ {
using namespace std::string_literals;
VertexShader::VertexShader( Graphics& gfx,const std::string& path ) VertexShader::VertexShader( Graphics& gfx,const std::string& path )
: :
path( path ) path( path )
...@@ -33,16 +31,11 @@ namespace Bind ...@@ -33,16 +31,11 @@ namespace Bind
} }
std::shared_ptr<Bindable> VertexShader::Resolve( Graphics& gfx,const std::string& path ) std::shared_ptr<Bindable> VertexShader::Resolve( Graphics& gfx,const std::string& path )
{ {
auto bind = Codex::Resolve( GenerateUID( path ) ); return Codex::Resolve<VertexShader>( gfx,path );
if( !bind )
{
bind = std::make_shared<VertexShader>( gfx,path );
Codex::Store( bind );
}
return bind;
} }
std::string VertexShader::GenerateUID( const std::string& path ) std::string VertexShader::GenerateUID( const std::string& path )
{ {
using namespace std::string_literals;
return typeid(VertexShader).name() + "#"s + path; return typeid(VertexShader).name() + "#"s + path;
} }
std::string VertexShader::GetUID() const noexcept std::string VertexShader::GetUID() const noexcept
......
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