Commit 3826545c authored by chili's avatar chili
Browse files

bindables refactored to be compatible with codex

parent c87ca0bc
......@@ -2,6 +2,7 @@
#include "Bindable.h"
#include "BindableCodex.h"
#include <type_traits>
#include <memory>
#include <unordered_map>
......@@ -13,6 +14,7 @@ namespace Bind
template<class T,typename...Params>
static std::shared_ptr<Bindable> Resolve( Graphics& gfx,Params&&...p ) noxnd
{
static_assert( std::is_base_of<Bindable,T>::value,"Can only resolve classes derived from Bindable" );
return Get().Resolve_<T>( gfx,std::forward<Params>( p )... );
}
private:
......
#pragma once
#include "Bindable.h"
#include "GraphicsThrowMacros.h"
#include "BindableCodex.h"
namespace Bind
{
......@@ -71,6 +72,18 @@ namespace Bind
{
GetContext( gfx )->VSSetConstantBuffers( slot,1u,pConstantBuffer.GetAddressOf() );
}
static std::shared_ptr<Bindable> Resolve( Graphics& gfx )
{
return Codex::Resolve<VertexConstantBuffer>( gfx );
}
static std::string GenerateUID()
{
return typeid(VertexConstantBuffer).name();
}
std::string GetUID() const noexcept override
{
return GenerateUID();
}
};
template<typename C>
......@@ -85,5 +98,17 @@ namespace Bind
{
GetContext( gfx )->PSSetConstantBuffers( slot,1u,pConstantBuffer.GetAddressOf() );
}
std::shared_ptr<Bindable> Resolve( Graphics& gfx )
{
return Codex::Resolve<PixelConstantBuffer>( gfx );
}
static std::string GenerateUID()
{
return typeid(PixelConstantBuffer).name();
}
std::string GetUID() const noexcept override
{
return GenerateUID();
}
};
}
\ No newline at end of file
#include "IndexBuffer.h"
#include "GraphicsThrowMacros.h"
#include "BindableCodex.h"
namespace Bind
{
IndexBuffer::IndexBuffer( Graphics& gfx,const std::vector<unsigned short>& indices )
:
IndexBuffer( gfx,"?",indices )
{}
IndexBuffer::IndexBuffer( Graphics& gfx,std::string tag,const std::vector<unsigned short>& indices )
:
tag( tag ),
count( (UINT)indices.size() )
{
INFOMAN( gfx );
......@@ -30,4 +36,18 @@ namespace Bind
{
return count;
}
std::shared_ptr<Bindable> IndexBuffer::Resolve( Graphics& gfx,const std::string& tag,
const std::vector<unsigned short>& indices )
{
return Codex::Resolve<IndexBuffer>( gfx,tag,indices );
}
std::string IndexBuffer::GenerateUID_( const std::string& tag )
{
using namespace std::string_literals;
return typeid(IndexBuffer).name() + "#"s + tag;
}
std::string IndexBuffer::GetUID() const noexcept
{
return GenerateUID_( tag );
}
}
......@@ -7,9 +7,21 @@ namespace Bind
{
public:
IndexBuffer( Graphics& gfx,const std::vector<unsigned short>& indices );
IndexBuffer( Graphics& gfx,std::string tag,const std::vector<unsigned short>& indices );
void Bind( Graphics& gfx ) noexcept override;
UINT GetCount() const noexcept;
static std::shared_ptr<Bindable> Resolve( Graphics& gfx,const std::string& tag,
const std::vector<unsigned short>& indices );
template<typename...Ignore>
static std::string GenerateUID( const std::string& tag,Ignore&&...ignore )
{
return GenerateUID_( tag );
}
std::string GetUID() const noexcept override;
private:
static std::string GenerateUID_( const std::string& tag );
protected:
std::string tag;
UINT count;
Microsoft::WRL::ComPtr<ID3D11Buffer> pIndexBuffer;
};
......
#include "InputLayout.h"
#include "GraphicsThrowMacros.h"
#include "BindableCodex.h"
#include "Vertex.h"
namespace Bind
{
InputLayout::InputLayout( Graphics& gfx,
const std::vector<D3D11_INPUT_ELEMENT_DESC>& layout,
Dvtx::VertexLayout layout_in,
ID3DBlob* pVertexShaderBytecode )
:
layout( std::move( layout_in ) )
{
INFOMAN( gfx );
const auto d3dLayout = layout.GetD3DLayout();
GFX_THROW_INFO( GetDevice( gfx )->CreateInputLayout(
layout.data(),(UINT)layout.size(),
d3dLayout.data(),(UINT)d3dLayout.size(),
pVertexShaderBytecode->GetBufferPointer(),
pVertexShaderBytecode->GetBufferSize(),
&pInputLayout
......@@ -21,4 +27,18 @@ namespace Bind
{
GetContext( gfx )->IASetInputLayout( pInputLayout.Get() );
}
std::shared_ptr<Bindable> InputLayout::Resolve( Graphics& gfx,
const Dvtx::VertexLayout& layout,ID3DBlob* pVertexShaderBytecode )
{
return Codex::Resolve<InputLayout>( gfx,layout,pVertexShaderBytecode );
}
std::string InputLayout::GenerateUID( const Dvtx::VertexLayout& layout,ID3DBlob* pVertexShaderBytecode )
{
using namespace std::string_literals;
return typeid(InputLayout).name() + "#"s + layout.GetCode();
}
std::string InputLayout::GetUID() const noexcept
{
return GenerateUID( layout );
}
}
#pragma once
#include "Bindable.h"
#include "Vertex.h"
namespace Bind
{
......@@ -7,10 +8,15 @@ namespace Bind
{
public:
InputLayout( Graphics& gfx,
const std::vector<D3D11_INPUT_ELEMENT_DESC>& layout,
Dvtx::VertexLayout layout,
ID3DBlob* pVertexShaderBytecode );
void Bind( Graphics& gfx ) noexcept override;
static std::shared_ptr<Bindable> Resolve( Graphics& gfx,
const Dvtx::VertexLayout& layout,ID3DBlob* pVertexShaderBytecode );
static std::string GenerateUID( const Dvtx::VertexLayout& layout,ID3DBlob* pVertexShaderBytecode = nullptr );
std::string GetUID() const noexcept override;
protected:
Dvtx::VertexLayout layout;
Microsoft::WRL::ComPtr<ID3D11InputLayout> pInputLayout;
};
}
\ No newline at end of file
......@@ -270,11 +270,11 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh,const a
aiString texFileName;
material.GetTexture( aiTextureType_DIFFUSE,0,&texFileName );
bindablePtrs.push_back( std::make_shared<Bind::Texture>( gfx,Surface::FromFile( base + texFileName.C_Str() ) ) );
bindablePtrs.push_back( std::make_shared<Bind::Texture>( gfx,base + texFileName.C_Str() ) );
if( material.GetTexture( aiTextureType_SPECULAR,0,&texFileName ) == aiReturn_SUCCESS )
{
bindablePtrs.push_back( std::make_shared<Bind::Texture>( gfx,Surface::FromFile( base + texFileName.C_Str() ),1 ) );
bindablePtrs.push_back( std::make_shared<Bind::Texture>( gfx,base + texFileName.C_Str(),1 ) );
hasSpecularMap = true;
}
else
......@@ -293,15 +293,15 @@ std::unique_ptr<Mesh> Model::ParseMesh( Graphics& gfx,const aiMesh& mesh,const a
auto pvsbc = pvs->GetBytecode();
bindablePtrs.push_back( std::move( pvs ) );
bindablePtrs.push_back( std::make_shared<Bind::InputLayout>( gfx,vbuf.GetLayout().GetD3DLayout(),pvsbc ) );
bindablePtrs.push_back( std::make_shared<Bind::InputLayout>( gfx,vbuf.GetLayout(),pvsbc ) );
if( hasSpecularMap )
{
bindablePtrs.push_back( std::make_shared<Bind::PixelShader>( gfx,L"PhongPSSpecMap.cso" ) );
bindablePtrs.push_back( std::make_shared<Bind::PixelShader>( gfx,"PhongPSSpecMap.cso" ) );
}
else
{
bindablePtrs.push_back( std::make_shared<Bind::PixelShader>( gfx,L"PhongPS.cso" ) );
bindablePtrs.push_back( std::make_shared<Bind::PixelShader>( gfx,"PhongPS.cso" ) );
struct PSMaterialConstant
{
......
#include "PixelShader.h"
#include "GraphicsThrowMacros.h"
#include "BindableCodex.h"
namespace Bind
{
PixelShader::PixelShader( Graphics& gfx,const std::wstring& path )
PixelShader::PixelShader( Graphics& gfx,const std::string& path )
:
path( path )
{
INFOMAN( gfx );
Microsoft::WRL::ComPtr<ID3DBlob> pBlob;
GFX_THROW_INFO( D3DReadFileToBlob( path.c_str(),&pBlob ) );
GFX_THROW_INFO( D3DReadFileToBlob( std::wstring{path.begin(),path.end()}.c_str(),&pBlob ) );
GFX_THROW_INFO( GetDevice( gfx )->CreatePixelShader( pBlob->GetBufferPointer(),pBlob->GetBufferSize(),nullptr,&pPixelShader ) );
}
......@@ -16,4 +19,17 @@ namespace Bind
{
GetContext( gfx )->PSSetShader( pPixelShader.Get(),nullptr,0u );
}
std::shared_ptr<Bindable> PixelShader::Resolve( Graphics& gfx,const std::string& path )
{
return Codex::Resolve<PixelShader>( gfx,path );
}
std::string PixelShader::GenerateUID( const std::string& path )
{
using namespace std::string_literals;
return typeid(PixelShader).name() + "#"s + path;
}
std::string PixelShader::GetUID() const noexcept
{
return GenerateUID( path );
}
}
......@@ -6,9 +6,13 @@ namespace Bind
class PixelShader : public Bindable
{
public:
PixelShader( Graphics& gfx,const std::wstring& path );
PixelShader( Graphics& gfx,const std::string& path );
void Bind( Graphics& gfx ) noexcept override;
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<ID3D11PixelShader> pPixelShader;
};
}
\ No newline at end of file
......@@ -19,7 +19,7 @@ SolidSphere::SolidSphere( Graphics& gfx,float radius )
auto pvsbc = pvs->GetBytecode();
AddBind( std::move( pvs ) );
AddBind( std::make_shared<PixelShader>( gfx,L"SolidPS.cso" ) );
AddBind( std::make_shared<PixelShader>( gfx,"SolidPS.cso" ) );
struct PSColorConstant
{
......@@ -28,7 +28,7 @@ SolidSphere::SolidSphere( Graphics& gfx,float radius )
} colorConst;
AddBind( std::make_shared<PixelConstantBuffer<PSColorConstant>>( gfx,colorConst ) );
AddBind( std::make_shared<InputLayout>( gfx,model.vertices.GetLayout().GetD3DLayout(),pvsbc ) );
AddBind( std::make_shared<InputLayout>( gfx,model.vertices.GetLayout(),pvsbc ) );
AddBind( std::make_shared<Topology>( gfx,D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) );
......
#include "Texture.h"
#include "Surface.h"
#include "GraphicsThrowMacros.h"
#include "BindableCodex.h"
namespace Bind
{
namespace wrl = Microsoft::WRL;
Texture::Texture( Graphics& gfx,const Surface& s,unsigned int slot )
Texture::Texture( Graphics& gfx,const std::string& path,UINT slot )
:
path( path ),
slot( slot )
{
INFOMAN( gfx );
// load surface
const auto s = Surface::FromFile( path );
// create texture resource
D3D11_TEXTURE2D_DESC textureDesc = {};
textureDesc.Width = s.GetWidth();
......@@ -48,4 +53,17 @@ namespace Bind
{
GetContext( gfx )->PSSetShaderResources( slot,1u,pTextureView.GetAddressOf() );
}
std::shared_ptr<Bindable> Texture::Resolve( Graphics& gfx,const std::string& path,UINT slot )
{
return Codex::Resolve<Texture>( gfx,path,slot );
}
std::string Texture::GenerateUID( const std::string& path,UINT slot )
{
using namespace std::string_literals;
return typeid(Texture).name() + "#"s + path + "#" + std::to_string( slot );
}
std::string Texture::GetUID() const noexcept
{
return GenerateUID( path,slot );
}
}
......@@ -8,11 +8,15 @@ namespace Bind
class Texture : public Bindable
{
public:
Texture( Graphics& gfx,const Surface& s,unsigned int slot = 0 );
Texture( Graphics& gfx,const std::string& path,UINT slot = 0 );
void Bind( Graphics& gfx ) noexcept override;
static std::shared_ptr<Bindable> Resolve( Graphics& gfx,const std::string& path,UINT slot );
static std::string GenerateUID( const std::string& path,UINT slot );
std::string GetUID() const noexcept override;
private:
unsigned int slot;
protected:
std::string path;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> pTextureView;
};
}
#include "Topology.h"
#include "BindableCodex.h"
namespace Bind
{
......@@ -11,4 +12,17 @@ namespace Bind
{
GetContext( gfx )->IASetPrimitiveTopology( type );
}
std::shared_ptr<Bindable> Topology::Resolve( Graphics& gfx,D3D11_PRIMITIVE_TOPOLOGY type )
{
return Codex::Resolve<Topology>( gfx,type );
}
std::string Topology::GenerateUID( D3D11_PRIMITIVE_TOPOLOGY type )
{
using namespace std::string_literals;
return typeid(Topology).name() + "#"s + std::to_string( type );
}
std::string Topology::GetUID() const noexcept
{
return GenerateUID( type );
}
}
......@@ -8,6 +8,9 @@ namespace Bind
public:
Topology( Graphics& gfx,D3D11_PRIMITIVE_TOPOLOGY type );
void Bind( Graphics& gfx ) noexcept override;
static std::shared_ptr<Bindable> Resolve( Graphics& gfx,D3D11_PRIMITIVE_TOPOLOGY type );
static std::string GenerateUID( D3D11_PRIMITIVE_TOPOLOGY type );
std::string GetUID() const noexcept override;
protected:
D3D11_PRIMITIVE_TOPOLOGY type;
};
......
......@@ -30,6 +30,15 @@ namespace Dvtx
}
return desc;
}
std::string VertexLayout::GetCode() const noxnd
{
std::string code;
for( const auto& e : elements )
{
code += e.GetCode();
}
return code;
}
// VertexLayout::Element
......@@ -76,6 +85,28 @@ namespace Dvtx
{
return type;
}
const char* Dvtx::VertexLayout::Element::GetCode() const noexcept
{
switch( type )
{
case Position2D:
return Map<Position2D>::code;
case Position3D:
return Map<Position3D>::code;
case Texture2D:
return Map<Texture2D>::code;
case Normal:
return Map<Normal>::code;
case Float3Color:
return Map<Float3Color>::code;
case Float4Color:
return Map<Float4Color>::code;
case BGRAColor:
return Map<BGRAColor>::code;
}
assert( "Invalid element type" && false );
return "Invalid";
}
D3D11_INPUT_ELEMENT_DESC VertexLayout::Element::GetDesc() const noxnd
{
switch( type )
......
......@@ -27,42 +27,49 @@ namespace Dvtx
using SysType = DirectX::XMFLOAT2;
static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R32G32_FLOAT;
static constexpr const char* semantic = "Position";
static constexpr const char* code = "P2";
};
template<> struct Map<Position3D>
{
using SysType = DirectX::XMFLOAT3;
static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R32G32B32_FLOAT;
static constexpr const char* semantic = "Position";
static constexpr const char* code = "P3";
};
template<> struct Map<Texture2D>
{
using SysType = DirectX::XMFLOAT2;
static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R32G32_FLOAT;
static constexpr const char* semantic = "Texcoord";
static constexpr const char* code = "T2";
};
template<> struct Map<Normal>
{
using SysType = DirectX::XMFLOAT3;
static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R32G32B32_FLOAT;
static constexpr const char* semantic = "Normal";
static constexpr const char* code = "N";
};
template<> struct Map<Float3Color>
{
using SysType = DirectX::XMFLOAT3;
static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R32G32B32_FLOAT;
static constexpr const char* semantic = "Color";
static constexpr const char* code = "C3";
};
template<> struct Map<Float4Color>
{
using SysType = DirectX::XMFLOAT4;
static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R32G32B32A32_FLOAT;
static constexpr const char* semantic = "Color";
static constexpr const char* code = "C4";
};
template<> struct Map<BGRAColor>
{
using SysType = ::BGRAColor;
static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
static constexpr const char* semantic = "Color";
static constexpr const char* code = "C8";
};
class Element
......@@ -75,9 +82,10 @@ namespace Dvtx
static constexpr size_t SizeOf( ElementType type ) noxnd;
ElementType GetType() const noexcept;
D3D11_INPUT_ELEMENT_DESC GetDesc() const noxnd;
const char* GetCode() const noexcept;
private:
template<ElementType type>
static constexpr D3D11_INPUT_ELEMENT_DESC GenerateDesc( size_t offset ) noxnd
static constexpr D3D11_INPUT_ELEMENT_DESC GenerateDesc( size_t offset ) noexcept
{
return { Map<type>::semantic,0,Map<type>::dxgiFormat,0,(UINT)offset,D3D11_INPUT_PER_VERTEX_DATA,0 };
}
......@@ -104,6 +112,7 @@ namespace Dvtx
size_t Size() const noxnd;
size_t GetElementCount() const noexcept;
std::vector<D3D11_INPUT_ELEMENT_DESC> GetD3DLayout() const noxnd;
std::string GetCode() const noxnd;
private:
std::vector<Element> elements;
};
......
#include "VertexBuffer.h"
#include "BindableCodex.h"
namespace Bind
{
VertexBuffer::VertexBuffer( Graphics& gfx,const Dvtx::VertexBuffer& vbuf )
:
stride( (UINT)vbuf.GetLayout().Size() )
VertexBuffer( gfx,"?",vbuf )
{}
VertexBuffer::VertexBuffer( Graphics& gfx,const std::string& tag,const Dvtx::VertexBuffer& vbuf )
:
stride( (UINT)vbuf.GetLayout().Size() ),
tag( tag )
{
INFOMAN( gfx );
......@@ -25,4 +31,18 @@ namespace Bind
const UINT offset = 0u;
GetContext( gfx )->IASetVertexBuffers( 0u,1u,pVertexBuffer.GetAddressOf(),&stride,&offset );
}
std::shared_ptr<Bindable> VertexBuffer::Resolve( Graphics& gfx,const std::string& tag,
const Dvtx::VertexBuffer& vbuf )
{
return Codex::Resolve<VertexBuffer>( gfx,tag,vbuf );
}
std::string VertexBuffer::GenerateUID_( const std::string& tag )
{
using namespace std::string_literals;
return typeid(VertexBuffer).name() + "#"s + tag;
}
std::string VertexBuffer::GetUID() const noexcept
{
return GenerateUID( tag );
}
}
......@@ -8,9 +8,21 @@ namespace Bind
class VertexBuffer : public Bindable
{
public:
VertexBuffer( Graphics& gfx,const std::string& tag,const Dvtx::VertexBuffer& vbuf );
VertexBuffer( Graphics& gfx,const Dvtx::VertexBuffer& vbuf );
void Bind( Graphics& gfx ) noexcept override;
static std::shared_ptr<Bindable> Resolve( Graphics& gfx,const std::string& tag,
const Dvtx::VertexBuffer& vbuf );
template<typename...Ignore>
static std::string GenerateUID( const std::string& tag,Ignore&&...ignore )
{
return GenerateUID_( tag );
}
std::string GetUID() const noexcept override;
private:
static std::string GenerateUID_( const std::string& tag );
protected:
std::string tag;
UINT stride;
Microsoft::WRL::ComPtr<ID3D11Buffer> pVertexBuffer;
};
......
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