cbuffer CBuf cbuffer CBuf
{ {
matrix modelView; matrix modelView;
matrix modelViewProj; matrix modelViewProj;
}; };
struct VSOut struct VSOut
{ {
float3 worldPos : Position; float3 worldPos : Position;
float3 normal : Normal; float3 normal : Normal;
float4 pos : SV_Position; float2 tc : Texcoord;
float4 pos : SV_Position;
}; };
VSOut main( float3 pos : Position,float3 n : Normal ) VSOut main(float3 pos : Position, float3 n : Normal, float2 tc : Texcoord)
{ {
VSOut vso; VSOut vso;
vso.worldPos = (float3)mul( float4(pos,1.0f),modelView ); vso.worldPos = (float3) mul(float4(pos, 1.0f), modelView);
vso.normal = mul( n,(float3x3)modelView ); vso.normal = mul(n, (float3x3) modelView);
vso.pos = mul( float4(pos,1.0f),modelViewProj ); vso.pos = mul(float4(pos, 1.0f), modelViewProj);
return vso; vso.tc = tc;
return vso;
} }
\ No newline at end of file
#include "PixelShader.h" #include "PixelShader.h"
#include "GraphicsThrowMacros.h" #include "GraphicsThrowMacros.h"
#include "BindableCodex.h"
namespace Bind namespace Bind
{ {
PixelShader::PixelShader( Graphics& gfx,const std::wstring& path ) PixelShader::PixelShader( Graphics& gfx,const std::string& path )
:
path( path )
{ {
INFOMAN( gfx ); INFOMAN( gfx );
Microsoft::WRL::ComPtr<ID3DBlob> pBlob; 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 ) ); GFX_THROW_INFO( GetDevice( gfx )->CreatePixelShader( pBlob->GetBufferPointer(),pBlob->GetBufferSize(),nullptr,&pPixelShader ) );
} }
...@@ -16,4 +19,17 @@ namespace Bind ...@@ -16,4 +19,17 @@ namespace Bind
{ {
GetContext( gfx )->PSSetShader( pPixelShader.Get(),nullptr,0u ); GetContext( gfx )->PSSetShader( pPixelShader.Get(),nullptr,0u );
} }
std::shared_ptr<PixelShader> 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 ...@@ -6,9 +6,13 @@ namespace Bind
class PixelShader : public Bindable class PixelShader : public Bindable
{ {
public: public:
PixelShader( Graphics& gfx,const std::wstring& path ); PixelShader( Graphics& gfx,const std::string& path );
void Bind( Graphics& gfx ) noexcept override; void Bind( Graphics& gfx ) noexcept override;
static std::shared_ptr<PixelShader> Resolve( Graphics& gfx,const std::string& path );
static std::string GenerateUID( const std::string& path );
std::string GetUID() const noexcept override;
protected: protected:
std::string path;
Microsoft::WRL::ComPtr<ID3D11PixelShader> pPixelShader; Microsoft::WRL::ComPtr<ID3D11PixelShader> pPixelShader;
}; };
} }
\ No newline at end of file
#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<Sampler> 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<Sampler> 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;
}; };
......
#include "SolidSphere.h" #include "SolidSphere.h"
#include "BindableCommon.h" #include "BindableCommon.h"
#include "GraphicsThrowMacros.h" #include "GraphicsThrowMacros.h"
#include "Vertex.h"
#include "Sphere.h" #include "Sphere.h"
...@@ -8,48 +9,32 @@ SolidSphere::SolidSphere( Graphics& gfx,float radius ) ...@@ -8,48 +9,32 @@ SolidSphere::SolidSphere( Graphics& gfx,float radius )
{ {
using namespace Bind; using namespace Bind;
namespace dx = DirectX; namespace dx = DirectX;
auto model = Sphere::Make();
model.Transform( dx::XMMatrixScaling( radius,radius,radius ) );
const auto geometryTag = "$sphere." + std::to_string( radius );
AddBind( VertexBuffer::Resolve( gfx,geometryTag,model.vertices ) );
AddBind( IndexBuffer::Resolve( gfx,geometryTag,model.indices ) );
if( !IsStaticInitialized() ) auto pvs = VertexShader::Resolve( gfx,"SolidVS.cso" );
{ auto pvsbc = pvs->GetBytecode();
struct Vertex AddBind( std::move( pvs ) );
{
dx::XMFLOAT3 pos; AddBind( PixelShader::Resolve( gfx,"SolidPS.cso" ) );
};
auto model = Sphere::Make<Vertex>(); struct PSColorConstant
model.Transform( dx::XMMatrixScaling( radius,radius,radius ) );
AddBind( std::make_unique<VertexBuffer>( gfx,model.vertices ) );
AddIndexBuffer( std::make_unique<IndexBuffer>( gfx,model.indices ) );
auto pvs = std::make_unique<VertexShader>( gfx,L"SolidVS.cso" );
auto pvsbc = pvs->GetBytecode();
AddStaticBind( std::move( pvs ) );
AddStaticBind( std::make_unique<PixelShader>( gfx,L"SolidPS.cso" ) );
struct PSColorConstant
{
dx::XMFLOAT3 color = { 1.0f,1.0f,1.0f };
float padding;
} colorConst;
AddStaticBind( std::make_unique<PixelConstantBuffer<PSColorConstant>>( gfx,colorConst ) );
const std::vector<D3D11_INPUT_ELEMENT_DESC> ied =
{
{ "Position",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 },
};
AddStaticBind( std::make_unique<InputLayout>( gfx,ied,pvsbc ) );
AddStaticBind( std::make_unique<Topology>( gfx,D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) );
}
else
{ {
SetIndexFromStatic(); dx::XMFLOAT3 color = { 1.0f,1.0f,1.0f };
} float padding;
} colorConst;
AddBind( PixelConstantBuffer<PSColorConstant>::Resolve( gfx,colorConst ) );
AddBind( std::make_unique<TransformCbuf>( gfx,*this ) ); AddBind( InputLayout::Resolve( gfx,model.vertices.GetLayout(),pvsbc ) );
}
AddBind( Topology::Resolve( gfx,D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST ) );
void SolidSphere::Update( float dt ) noexcept {} AddBind( std::make_shared<TransformCbuf>( gfx,*this ) );
}
void SolidSphere::SetPos( DirectX::XMFLOAT3 pos ) noexcept void SolidSphere::SetPos( DirectX::XMFLOAT3 pos ) noexcept
{ {
......
#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:
......
#pragma once #pragma once
#include <optional>
#include "Vertex.h"
#include "IndexedTriangleList.h" #include "IndexedTriangleList.h"
#include <DirectXMath.h> #include <DirectXMath.h>
#include "ChiliMath.h" #include "ChiliMath.h"
...@@ -6,8 +8,7 @@ ...@@ -6,8 +8,7 @@
class Sphere class Sphere
{ {
public: public:
template<class V> static IndexedTriangleList MakeTesselated( Dvtx::VertexLayout layout,int latDiv,int longDiv )
static IndexedTriangleList<V> MakeTesselated( int latDiv,int longDiv )
{ {
namespace dx = DirectX; namespace dx = DirectX;
assert( latDiv >= 3 ); assert( latDiv >= 3 );
...@@ -18,7 +19,7 @@ public: ...@@ -18,7 +19,7 @@ public:
const float lattitudeAngle = PI / latDiv; const float lattitudeAngle = PI / latDiv;
const float longitudeAngle = 2.0f * PI / longDiv; const float longitudeAngle = 2.0f * PI / longDiv;
std::vector<V> vertices; Dvtx::VertexBuffer vb{ std::move( layout ) };
for( int iLat = 1; iLat < latDiv; iLat++ ) for( int iLat = 1; iLat < latDiv; iLat++ )
{ {
const auto latBase = dx::XMVector3Transform( const auto latBase = dx::XMVector3Transform(
...@@ -27,22 +28,29 @@ public: ...@@ -27,22 +28,29 @@ public:
); );
for( int iLong = 0; iLong < longDiv; iLong++ ) for( int iLong = 0; iLong < longDiv; iLong++ )
{ {
vertices.emplace_back(); dx::XMFLOAT3 calculatedPos;
auto v = dx::XMVector3Transform( auto v = dx::XMVector3Transform(
latBase, latBase,
dx::XMMatrixRotationZ( longitudeAngle * iLong ) dx::XMMatrixRotationZ( longitudeAngle * iLong )
); );
dx::XMStoreFloat3( &vertices.back().pos,v ); dx::XMStoreFloat3( &calculatedPos,v );
vb.EmplaceBack( calculatedPos );
} }
} }
// add the cap vertices // add the cap vertices
const auto iNorthPole = (unsigned short)vertices.size(); const auto iNorthPole = (unsigned short)vb.Size();
vertices.emplace_back(); {
dx::XMStoreFloat3( &vertices.back().pos,base ); dx::XMFLOAT3 northPos;
const auto iSouthPole = (unsigned short)vertices.size(); dx::XMStoreFloat3( &northPos,base );
vertices.emplace_back(); vb.EmplaceBack( northPos );
dx::XMStoreFloat3( &vertices.back().pos,dx::XMVectorNegate( base ) ); }
const auto iSouthPole = (unsigned short)vb.Size();
{
dx::XMFLOAT3 southPos;
dx::XMStoreFloat3( &southPos,dx::XMVectorNegate( base ) );
vb.EmplaceBack( southPos );
}
const auto calcIdx = [latDiv,longDiv]( unsigned short iLat,unsigned short iLong ) const auto calcIdx = [latDiv,longDiv]( unsigned short iLat,unsigned short iLong )
{ return iLat * longDiv + iLong; }; { return iLat * longDiv + iLong; };
...@@ -89,11 +97,15 @@ public: ...@@ -89,11 +97,15 @@ public:
indices.push_back( calcIdx( latDiv - 2,longDiv - 1 ) ); indices.push_back( calcIdx( latDiv - 2,longDiv - 1 ) );
indices.push_back( iSouthPole ); indices.push_back( iSouthPole );
return { std::move( vertices ),std::move( indices ) }; return { std::move( vb ),std::move( indices ) };
} }
template<class V> static IndexedTriangleList Make( std::optional<Dvtx::VertexLayout> layout = std::nullopt )
static IndexedTriangleList<V> Make()
{ {
return MakeTesselated<V>( 12,24 ); using Element = Dvtx::VertexLayout::ElementType;
if( !layout )
{
layout = Dvtx::VertexLayout{}.Append( Element::Position3D );
}
return MakeTesselated( std::move( *layout ),12,24 );
} }
}; };
\ No newline at end of file
#include "Texture.h" #include "Texture.h"
#include "Surface.h" #include "Surface.h"
#include "GraphicsThrowMacros.h" #include "GraphicsThrowMacros.h"
#include "BindableCodex.h"
namespace Bind namespace Bind
{ {
namespace wrl = Microsoft::WRL; namespace wrl = Microsoft::WRL;
Texture::Texture( Graphics& gfx,const Surface& s ) Texture::Texture( Graphics& gfx,const std::string& path,UINT slot )
:
path( path ),
slot( slot )
{ {
INFOMAN( gfx ); INFOMAN( gfx );
// load surface
const auto s = Surface::FromFile( path );
// create texture resource // create texture resource
D3D11_TEXTURE2D_DESC textureDesc = {}; D3D11_TEXTURE2D_DESC textureDesc = {};
textureDesc.Width = s.GetWidth(); textureDesc.Width = s.GetWidth();
...@@ -44,6 +51,19 @@ namespace Bind ...@@ -44,6 +51,19 @@ namespace Bind
void Texture::Bind( Graphics& gfx ) noexcept void Texture::Bind( Graphics& gfx ) noexcept
{ {
GetContext( gfx )->PSSetShaderResources( 0u,1u,pTextureView.GetAddressOf() ); GetContext( gfx )->PSSetShaderResources( slot,1u,pTextureView.GetAddressOf() );
}
std::shared_ptr<Texture> 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,9 +8,15 @@ namespace Bind ...@@ -8,9 +8,15 @@ namespace Bind
class Texture : public Bindable class Texture : public Bindable
{ {
public: public:
Texture( Graphics& gfx,const Surface& s ); Texture( Graphics& gfx,const std::string& path,UINT slot = 0 );
void Bind( Graphics& gfx ) noexcept override; void Bind( Graphics& gfx ) noexcept override;
static std::shared_ptr<Texture> Resolve( Graphics& gfx,const std::string& path,UINT slot = 0 );
static std::string GenerateUID( const std::string& path,UINT slot = 0 );
std::string GetUID() const noexcept override;
private:
unsigned int slot;
protected: protected:
std::string path;
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> pTextureView; Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> pTextureView;
}; };
} }
#include "Topology.h" #include "Topology.h"
#include "BindableCodex.h"
namespace Bind namespace Bind
{ {
...@@ -11,4 +12,17 @@ namespace Bind ...@@ -11,4 +12,17 @@ namespace Bind
{ {
GetContext( gfx )->IASetPrimitiveTopology( type ); GetContext( gfx )->IASetPrimitiveTopology( type );
} }
std::shared_ptr<Topology> 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 ...@@ -8,6 +8,9 @@ namespace Bind
public: public:
Topology( Graphics& gfx,D3D11_PRIMITIVE_TOPOLOGY type ); Topology( Graphics& gfx,D3D11_PRIMITIVE_TOPOLOGY type );
void Bind( Graphics& gfx ) noexcept override; void Bind( Graphics& gfx ) noexcept override;
static std::shared_ptr<Topology> Resolve( Graphics& gfx,D3D11_PRIMITIVE_TOPOLOGY type );
static std::string GenerateUID( D3D11_PRIMITIVE_TOPOLOGY type );
std::string GetUID() const noexcept override;
protected: protected:
D3D11_PRIMITIVE_TOPOLOGY type; D3D11_PRIMITIVE_TOPOLOGY type;
}; };
......
...@@ -30,6 +30,15 @@ namespace Dvtx ...@@ -30,6 +30,15 @@ namespace Dvtx
} }
return desc; return desc;
} }
std::string VertexLayout::GetCode() const noxnd
{
std::string code;
for( const auto& e : elements )
{
code += e.GetCode();
}
return code;
}
// VertexLayout::Element // VertexLayout::Element
...@@ -76,6 +85,28 @@ namespace Dvtx ...@@ -76,6 +85,28 @@ namespace Dvtx
{ {
return type; 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 D3D11_INPUT_ELEMENT_DESC VertexLayout::Element::GetDesc() const noxnd
{ {
switch( type ) switch( type )
......
...@@ -27,42 +27,49 @@ namespace Dvtx ...@@ -27,42 +27,49 @@ namespace Dvtx
using SysType = DirectX::XMFLOAT2; using SysType = DirectX::XMFLOAT2;
static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R32G32_FLOAT; static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R32G32_FLOAT;
static constexpr const char* semantic = "Position"; static constexpr const char* semantic = "Position";
static constexpr const char* code = "P2";
}; };
template<> struct Map<Position3D> template<> struct Map<Position3D>
{ {
using SysType = DirectX::XMFLOAT3; using SysType = DirectX::XMFLOAT3;
static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R32G32B32_FLOAT; static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R32G32B32_FLOAT;
static constexpr const char* semantic = "Position"; static constexpr const char* semantic = "Position";
static constexpr const char* code = "P3";
}; };
template<> struct Map<Texture2D> template<> struct Map<Texture2D>
{ {
using SysType = DirectX::XMFLOAT2; using SysType = DirectX::XMFLOAT2;
static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R32G32_FLOAT; static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R32G32_FLOAT;
static constexpr const char* semantic = "Texcoord"; static constexpr const char* semantic = "Texcoord";
static constexpr const char* code = "T2";
}; };
template<> struct Map<Normal> template<> struct Map<Normal>
{ {
using SysType = DirectX::XMFLOAT3; using SysType = DirectX::XMFLOAT3;
static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R32G32B32_FLOAT; static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R32G32B32_FLOAT;
static constexpr const char* semantic = "Normal"; static constexpr const char* semantic = "Normal";
static constexpr const char* code = "N";
}; };
template<> struct Map<Float3Color> template<> struct Map<Float3Color>
{ {
using SysType = DirectX::XMFLOAT3; using SysType = DirectX::XMFLOAT3;
static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R32G32B32_FLOAT; static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R32G32B32_FLOAT;
static constexpr const char* semantic = "Color"; static constexpr const char* semantic = "Color";
static constexpr const char* code = "C3";
}; };
template<> struct Map<Float4Color> template<> struct Map<Float4Color>
{ {
using SysType = DirectX::XMFLOAT4; using SysType = DirectX::XMFLOAT4;
static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R32G32B32A32_FLOAT; static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R32G32B32A32_FLOAT;
static constexpr const char* semantic = "Color"; static constexpr const char* semantic = "Color";
static constexpr const char* code = "C4";
}; };
template<> struct Map<BGRAColor> template<> struct Map<BGRAColor>
{ {
using SysType = ::BGRAColor; using SysType = ::BGRAColor;
static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R8G8B8A8_UNORM; static constexpr DXGI_FORMAT dxgiFormat = DXGI_FORMAT_R8G8B8A8_UNORM;
static constexpr const char* semantic = "Color"; static constexpr const char* semantic = "Color";
static constexpr const char* code = "C8";
}; };
class Element class Element
...@@ -75,9 +82,10 @@ namespace Dvtx ...@@ -75,9 +82,10 @@ namespace Dvtx
static constexpr size_t SizeOf( ElementType type ) noxnd; static constexpr size_t SizeOf( ElementType type ) noxnd;
ElementType GetType() const noexcept; ElementType GetType() const noexcept;
D3D11_INPUT_ELEMENT_DESC GetDesc() const noxnd; D3D11_INPUT_ELEMENT_DESC GetDesc() const noxnd;
const char* GetCode() const noexcept;
private: private:
template<ElementType type> 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 }; return { Map<type>::semantic,0,Map<type>::dxgiFormat,0,(UINT)offset,D3D11_INPUT_PER_VERTEX_DATA,0 };
} }
...@@ -104,6 +112,7 @@ namespace Dvtx ...@@ -104,6 +112,7 @@ namespace Dvtx
size_t Size() const noxnd; size_t Size() const noxnd;
size_t GetElementCount() const noexcept; size_t GetElementCount() const noexcept;
std::vector<D3D11_INPUT_ELEMENT_DESC> GetD3DLayout() const noxnd; std::vector<D3D11_INPUT_ELEMENT_DESC> GetD3DLayout() const noxnd;
std::string GetCode() const noxnd;
private: private:
std::vector<Element> elements; std::vector<Element> elements;
}; };
......
#include "VertexBuffer.h" #include "VertexBuffer.h"
#include "BindableCodex.h"
namespace Bind namespace Bind
{ {
VertexBuffer::VertexBuffer( Graphics& gfx,const Dvtx::VertexBuffer& vbuf ) 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 ); INFOMAN( gfx );
...@@ -25,4 +31,19 @@ namespace Bind ...@@ -25,4 +31,19 @@ namespace Bind
const UINT offset = 0u; const UINT offset = 0u;
GetContext( gfx )->IASetVertexBuffers( 0u,1u,pVertexBuffer.GetAddressOf(),&stride,&offset ); GetContext( gfx )->IASetVertexBuffers( 0u,1u,pVertexBuffer.GetAddressOf(),&stride,&offset );
} }
std::shared_ptr<VertexBuffer> VertexBuffer::Resolve( Graphics& gfx,const std::string& tag,
const Dvtx::VertexBuffer& vbuf )
{
assert( tag != "?" );
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,27 +8,21 @@ namespace Bind ...@@ -8,27 +8,21 @@ namespace Bind
class VertexBuffer : public Bindable class VertexBuffer : public Bindable
{ {
public: public:
template<class V> VertexBuffer( Graphics& gfx,const std::string& tag,const Dvtx::VertexBuffer& vbuf );
VertexBuffer( Graphics& gfx,const std::vector<V>& vertices )
:
stride( sizeof( V ) )
{
INFOMAN( gfx );
D3D11_BUFFER_DESC bd = {};
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.Usage = D3D11_USAGE_DEFAULT;
bd.CPUAccessFlags = 0u;
bd.MiscFlags = 0u;
bd.ByteWidth = UINT( sizeof( V ) * vertices.size() );
bd.StructureByteStride = sizeof( V );
D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = vertices.data();
GFX_THROW_INFO( GetDevice( gfx )->CreateBuffer( &bd,&sd,&pVertexBuffer ) );
}
VertexBuffer( Graphics& gfx,const Dvtx::VertexBuffer& vbuf ); VertexBuffer( Graphics& gfx,const Dvtx::VertexBuffer& vbuf );
void Bind( Graphics& gfx ) noexcept override; void Bind( Graphics& gfx ) noexcept override;
static std::shared_ptr<VertexBuffer> 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: protected:
std::string tag;
UINT stride; UINT stride;
Microsoft::WRL::ComPtr<ID3D11Buffer> pVertexBuffer; Microsoft::WRL::ComPtr<ID3D11Buffer> pVertexBuffer;
}; };
......
#include "VertexShader.h" #include "VertexShader.h"
#include "GraphicsThrowMacros.h" #include "GraphicsThrowMacros.h"
#include "BindableCodex.h"
#include <typeinfo>
namespace Bind namespace Bind
{ {
VertexShader::VertexShader( Graphics& gfx,const std::wstring& path ) VertexShader::VertexShader( Graphics& gfx,const std::string& path )
:
path( path )
{ {
INFOMAN( gfx ); 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( GFX_THROW_INFO( GetDevice( gfx )->CreateVertexShader(
pBytecodeBlob->GetBufferPointer(), pBytecodeBlob->GetBufferPointer(),
pBytecodeBlob->GetBufferSize(), pBytecodeBlob->GetBufferSize(),
...@@ -25,4 +29,17 @@ namespace Bind ...@@ -25,4 +29,17 @@ namespace Bind
{ {
return pBytecodeBlob.Get(); return pBytecodeBlob.Get();
} }
std::shared_ptr<VertexShader> VertexShader::Resolve( Graphics& gfx,const std::string& path )
{
return Codex::Resolve<VertexShader>( gfx,path );
}
std::string VertexShader::GenerateUID( const std::string& path )
{
using namespace std::string_literals;
return typeid(VertexShader).name() + "#"s + path;
}
std::string VertexShader::GetUID() const noexcept
{
return GenerateUID( path );
}
} }
...@@ -6,10 +6,14 @@ namespace Bind ...@@ -6,10 +6,14 @@ namespace Bind
class VertexShader : public Bindable class VertexShader : public Bindable
{ {
public: public:
VertexShader( Graphics& gfx,const std::wstring& path ); VertexShader( Graphics& gfx,const std::string& path );
void Bind( Graphics& gfx ) noexcept override; void Bind( Graphics& gfx ) noexcept override;
ID3DBlob* GetBytecode() const noexcept; ID3DBlob* GetBytecode() const noexcept;
static std::shared_ptr<VertexShader> Resolve( Graphics& gfx,const std::string& path );
static std::string GenerateUID( const std::string& path );
std::string GetUID() const noexcept override;
protected: protected:
std::string path;
Microsoft::WRL::ComPtr<ID3DBlob> pBytecodeBlob; Microsoft::WRL::ComPtr<ID3DBlob> pBytecodeBlob;
Microsoft::WRL::ComPtr<ID3D11VertexShader> pVertexShader; Microsoft::WRL::ComPtr<ID3D11VertexShader> pVertexShader;
}; };
......
...@@ -131,6 +131,7 @@ ...@@ -131,6 +131,7 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="App.h" /> <ClInclude Include="App.h" />
<ClInclude Include="Bindable.h" /> <ClInclude Include="Bindable.h" />
<ClInclude Include="BindableCodex.h" />
<ClInclude Include="BindableCommon.h" /> <ClInclude Include="BindableCommon.h" />
<ClInclude Include="Camera.h" /> <ClInclude Include="Camera.h" />
<ClInclude Include="ChiliException.h" /> <ClInclude Include="ChiliException.h" />
...@@ -141,7 +142,6 @@ ...@@ -141,7 +142,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" />
...@@ -191,6 +191,14 @@ ...@@ -191,6 +191,14 @@
<None Include="DXTrace.inl" /> <None Include="DXTrace.inl" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<FxCompile Include="PhongPSSpecMap.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">4.0</ShaderModel>
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Pixel</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Release|x64'">4.0</ShaderModel>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
<ObjectFileOutput Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(ProjectDir)%(Filename).cso</ObjectFileOutput>
</FxCompile>
<FxCompile Include="SolidPS.hlsl"> <FxCompile Include="SolidPS.hlsl">
<ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType> <ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType>
<ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">4.0</ShaderModel> <ShaderModel Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">4.0</ShaderModel>
......
...@@ -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>
...@@ -299,6 +296,9 @@ ...@@ -299,6 +296,9 @@
<ClInclude Include="ConditionalNoexcept.h"> <ClInclude Include="ConditionalNoexcept.h">
<Filter>Header Files\Macros</Filter> <Filter>Header Files\Macros</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="BindableCodex.h">
<Filter>Header Files\Bindable</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="hw3d.rc"> <ResourceCompile Include="hw3d.rc">
...@@ -334,5 +334,8 @@ ...@@ -334,5 +334,8 @@
<FxCompile Include="SolidVS.hlsl"> <FxCompile Include="SolidVS.hlsl">
<Filter>Shader</Filter> <Filter>Shader</Filter>
</FxCompile> </FxCompile>
<FxCompile Include="PhongPSSpecMap.hlsl">
<Filter>Shader</Filter>
</FxCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>
\ No newline at end of file