Commit 939f5af6 authored by chili's avatar chili
Browse files

bindable codex prototype

parent 9cd80da7
......@@ -5,6 +5,7 @@
#include "Surface.h"
#include "GDIPlusManager.h"
#include "imgui/imgui.h"
#include "VertexBuffer.h"
namespace dx = DirectX;
......@@ -16,6 +17,8 @@ App::App()
light( wnd.Gfx() )
{
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 b = Bind::VertexShader::Resolve( wnd.Gfx(),"PhongVS.cso" );
}
void App::DoFrame()
......
......@@ -8,6 +8,11 @@ namespace Bind
{
public:
virtual void Bind( Graphics& gfx ) noexcept = 0;
virtual std::string GetUID() const noexcept
{
assert( false );
return "";
}
virtual ~Bindable() = default;
protected:
static ID3D11DeviceContext* GetContext( Graphics& gfx ) noexcept;
......
#pragma once
#include "Bindable.h"
#include "BindableCodex.h"
#include <memory>
#include <unordered_map>
namespace Bind
{
class Codex
{
public:
static std::shared_ptr<Bindable> Resolve( const std::string& key ) noxnd
{
return Get().Resolve_( key );
}
static void Store( std::shared_ptr<Bindable> bind )
{
Get().Store_( std::move( bind ) );
}
private:
std::shared_ptr<Bindable> Resolve_( const std::string& key ) const noxnd
{
auto i = binds.find( key );
if( i == binds.end() )
{
return {};
}
else
{
return i->second;
}
}
void Store_( std::shared_ptr<Bindable> bind )
{
binds[bind->GetUID()] = std::move( bind );
}
static Codex& Get()
{
static Codex codex;
return codex;
}
private:
std::unordered_map<std::string,std::shared_ptr<Bindable>> binds;
};
}
\ No newline at end of file
......@@ -289,7 +289,7 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh,const a
bindablePtrs.push_back( std::make_shared<Bind::IndexBuffer>( gfx,indices ) );
auto pvs = std::make_shared<Bind::VertexShader>( gfx,L"PhongVS.cso" );
auto pvs = std::make_shared<Bind::VertexShader>( gfx,"PhongVS.cso" );
auto pvsbc = pvs->GetBytecode();
bindablePtrs.push_back( std::move( pvs ) );
......
......@@ -15,7 +15,7 @@ SolidSphere::SolidSphere( Graphics& gfx,float radius )
AddBind( std::make_shared<VertexBuffer>( gfx,model.vertices ) );
AddBind( std::make_shared<IndexBuffer>( gfx,model.indices ) );
auto pvs = std::make_shared<VertexShader>( gfx,L"SolidVS.cso" );
auto pvs = std::make_shared<VertexShader>( gfx,"SolidVS.cso" );
auto pvsbc = pvs->GetBytecode();
AddBind( std::move( pvs ) );
......
#include "VertexShader.h"
#include "GraphicsThrowMacros.h"
#include "BindableCodex.h"
#include <typeinfo>
namespace Bind
{
VertexShader::VertexShader( Graphics& gfx,const std::wstring& path )
using namespace std::string_literals;
VertexShader::VertexShader( Graphics& gfx,const std::string& path )
:
path( path )
{
INFOMAN( gfx );
GFX_THROW_INFO( D3DReadFileToBlob( path.c_str(),&pBytecodeBlob ) );
GFX_THROW_INFO( D3DReadFileToBlob( std::wstring{path.begin(),path.end()}.c_str(),&pBytecodeBlob ) );
GFX_THROW_INFO( GetDevice( gfx )->CreateVertexShader(
pBytecodeBlob->GetBufferPointer(),
pBytecodeBlob->GetBufferSize(),
......@@ -25,4 +31,22 @@ namespace Bind
{
return pBytecodeBlob.Get();
}
std::shared_ptr<Bindable> VertexShader::Resolve( Graphics& gfx,const std::string& path )
{
auto bind = Codex::Resolve( GenerateUID( path ) );
if( !bind )
{
bind = std::make_shared<VertexShader>( gfx,path );
Codex::Store( bind );
}
return bind;
}
std::string VertexShader::GenerateUID( const std::string& path )
{
return typeid(VertexShader).name() + "#"s + path;
}
std::string VertexShader::GetUID() const noexcept
{
return GenerateUID( path );
}
}
......@@ -6,10 +6,14 @@ namespace Bind
class VertexShader : public Bindable
{
public:
VertexShader( Graphics& gfx,const std::wstring& path );
VertexShader( Graphics& gfx,const std::string& path );
void Bind( Graphics& gfx ) noexcept override;
ID3DBlob* GetBytecode() const noexcept;
static std::shared_ptr<Bindable> Resolve( Graphics& gfx,const std::string& path );
static std::string GenerateUID( const std::string& path );
std::string GetUID() const noexcept override;
protected:
std::string path;
Microsoft::WRL::ComPtr<ID3DBlob> pBytecodeBlob;
Microsoft::WRL::ComPtr<ID3D11VertexShader> pVertexShader;
};
......
......@@ -131,6 +131,7 @@
<ItemGroup>
<ClInclude Include="App.h" />
<ClInclude Include="Bindable.h" />
<ClInclude Include="BindableCodex.h" />
<ClInclude Include="BindableCommon.h" />
<ClInclude Include="Camera.h" />
<ClInclude Include="ChiliException.h" />
......
......@@ -296,6 +296,9 @@
<ClInclude Include="ConditionalNoexcept.h">
<Filter>Header Files\Macros</Filter>
</ClInclude>
<ClInclude Include="BindableCodex.h">
<Filter>Header Files\Bindable</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="hw3d.rc">
......
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