Commit 551cc463 authored by chili's avatar chili
Browse files

refactoring tentative

parent ffdeae8c
#pragma once
#include "TestObject.h"
class SkinnedBox : public TestObject<SkinnedBox>
{
public:
SkinnedBox( Graphics& gfx,std::mt19937& rng,
std::uniform_real_distribution<float>& adist,
std::uniform_real_distribution<float>& ddist,
std::uniform_real_distribution<float>& odist,
std::uniform_real_distribution<float>& rdist );
};
\ No newline at end of file
#include "SolidSphere.h" #include "SolidSphere.h"
#include "BindableBase.h" #include "BindableCommon.h"
#include "GraphicsThrowMacros.h" #include "GraphicsThrowMacros.h"
#include "Sphere.h" #include "Sphere.h"
SolidSphere::SolidSphere( Graphics& gfx,float radius ) SolidSphere::SolidSphere( Graphics& gfx,float radius )
{ {
using namespace Bind;
namespace dx = DirectX; namespace dx = DirectX;
if( !IsStaticInitialized() ) if( !IsStaticInitialized() )
......
...@@ -62,7 +62,7 @@ void Surface::Clear( Color fillValue ) noexcept ...@@ -62,7 +62,7 @@ void Surface::Clear( Color fillValue ) noexcept
memset( pBuffer.get(),fillValue.dword,width * height * sizeof( Color ) ); memset( pBuffer.get(),fillValue.dword,width * height * sizeof( Color ) );
} }
void Surface::PutPixel( unsigned int x,unsigned int y,Color c ) noexcept(!IS_DEBUG) void Surface::PutPixel( unsigned int x,unsigned int y,Color c ) noxnd
{ {
assert( x >= 0 ); assert( x >= 0 );
assert( y >= 0 ); assert( y >= 0 );
...@@ -71,7 +71,7 @@ void Surface::PutPixel( unsigned int x,unsigned int y,Color c ) noexcept(!IS_DEB ...@@ -71,7 +71,7 @@ void Surface::PutPixel( unsigned int x,unsigned int y,Color c ) noexcept(!IS_DEB
pBuffer[y * width + x] = c; pBuffer[y * width + x] = c;
} }
Surface::Color Surface::GetPixel( unsigned int x,unsigned int y ) const noexcept(!IS_DEBUG) Surface::Color Surface::GetPixel( unsigned int x,unsigned int y ) const noxnd
{ {
assert( x >= 0 ); assert( x >= 0 );
assert( y >= 0 ); assert( y >= 0 );
...@@ -203,7 +203,7 @@ void Surface::Save( const std::string& filename ) const ...@@ -203,7 +203,7 @@ void Surface::Save( const std::string& filename ) const
} }
} }
void Surface::Copy( const Surface& src ) noexcept(!IS_DEBUG) void Surface::Copy( const Surface& src ) noxnd
{ {
assert( width == src.width ); assert( width == src.width );
assert( height == src.height ); assert( height == src.height );
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <string> #include <string>
#include <assert.h> #include <assert.h>
#include <memory> #include <memory>
#include "ConditionalNoexcept.h"
class Surface class Surface
...@@ -121,8 +122,8 @@ public: ...@@ -121,8 +122,8 @@ public:
Surface& operator=( const Surface& ) = delete; Surface& operator=( const Surface& ) = delete;
~Surface(); ~Surface();
void Clear( Color fillValue ) noexcept; void Clear( Color fillValue ) noexcept;
void PutPixel( unsigned int x,unsigned int y,Color c ) noexcept(!IS_DEBUG); void PutPixel( unsigned int x,unsigned int y,Color c ) noxnd;
Color GetPixel( unsigned int x,unsigned int y ) const noexcept(!IS_DEBUG); Color GetPixel( unsigned int x,unsigned int y ) const noxnd;
unsigned int GetWidth() const noexcept; unsigned int GetWidth() const noexcept;
unsigned int GetHeight() const noexcept; unsigned int GetHeight() const noexcept;
Color* GetBufferPtr() noexcept; Color* GetBufferPtr() noexcept;
...@@ -130,7 +131,7 @@ public: ...@@ -130,7 +131,7 @@ public:
const Color* GetBufferPtrConst() const noexcept; const Color* GetBufferPtrConst() const noexcept;
static Surface FromFile( const std::string& name ); static Surface FromFile( const std::string& name );
void Save( const std::string& filename ) const; void Save( const std::string& filename ) const;
void Copy( const Surface& src ) noexcept(!IS_DEBUG); void Copy( const Surface& src ) noxnd;
private: private:
Surface( unsigned int width,unsigned int height,std::unique_ptr<Color[]> pBufferParam ) noexcept; Surface( unsigned int width,unsigned int height,std::unique_ptr<Color[]> pBufferParam ) noexcept;
private: private:
......
...@@ -2,45 +2,48 @@ ...@@ -2,45 +2,48 @@
#include "Surface.h" #include "Surface.h"
#include "GraphicsThrowMacros.h" #include "GraphicsThrowMacros.h"
namespace wrl = Microsoft::WRL; namespace Bind
Texture::Texture( Graphics& gfx,const Surface& s )
{ {
INFOMAN( gfx ); namespace wrl = Microsoft::WRL;
// create texture resource Texture::Texture( Graphics& gfx,const Surface& s )
D3D11_TEXTURE2D_DESC textureDesc = {}; {
textureDesc.Width = s.GetWidth(); INFOMAN( gfx );
textureDesc.Height = s.GetHeight();
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
textureDesc.SampleDesc.Count = 1;
textureDesc.SampleDesc.Quality = 0;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = s.GetBufferPtr();
sd.SysMemPitch = s.GetWidth() * sizeof( Surface::Color );
wrl::ComPtr<ID3D11Texture2D> pTexture;
GFX_THROW_INFO( GetDevice( gfx )->CreateTexture2D(
&textureDesc,&sd,&pTexture
) );
// create the resource view on the texture // create texture resource
D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = {}; D3D11_TEXTURE2D_DESC textureDesc = {};
srvDesc.Format = textureDesc.Format; textureDesc.Width = s.GetWidth();
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; textureDesc.Height = s.GetHeight();
srvDesc.Texture2D.MostDetailedMip = 0; textureDesc.MipLevels = 1;
srvDesc.Texture2D.MipLevels = 1; textureDesc.ArraySize = 1;
GFX_THROW_INFO( GetDevice( gfx )->CreateShaderResourceView( textureDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
pTexture.Get(),&srvDesc,&pTextureView textureDesc.SampleDesc.Count = 1;
) ); textureDesc.SampleDesc.Quality = 0;
} textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = s.GetBufferPtr();
sd.SysMemPitch = s.GetWidth() * sizeof( Surface::Color );
wrl::ComPtr<ID3D11Texture2D> pTexture;
GFX_THROW_INFO( GetDevice( gfx )->CreateTexture2D(
&textureDesc,&sd,&pTexture
) );
void Texture::Bind( Graphics& gfx ) noexcept // create the resource view on the texture
{ D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
GetContext( gfx )->PSSetShaderResources( 0u,1u,pTextureView.GetAddressOf() ); srvDesc.Format = textureDesc.Format;
srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
srvDesc.Texture2D.MostDetailedMip = 0;
srvDesc.Texture2D.MipLevels = 1;
GFX_THROW_INFO( GetDevice( gfx )->CreateShaderResourceView(
pTexture.Get(),&srvDesc,&pTextureView
) );
}
void Texture::Bind( Graphics& gfx ) noexcept
{
GetContext( gfx )->PSSetShaderResources( 0u,1u,pTextureView.GetAddressOf() );
}
} }
#pragma once #pragma once
#include "Bindable.h" #include "Bindable.h"
class Texture : public Bindable class Surface;
namespace Bind
{ {
public: class Texture : public Bindable
Texture( Graphics& gfx,const class Surface& s ); {
void Bind( Graphics& gfx ) noexcept override; public:
protected: Texture( Graphics& gfx,const Surface& s );
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> pTextureView; void Bind( Graphics& gfx ) noexcept override;
}; protected:
Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> pTextureView;
};
}
Texture2D tex;
SamplerState splr;
float4 main( float2 tc : TexCoord ) : SV_Target
{
return tex.Sample( splr,tc );
}
\ No newline at end of file
cbuffer CBuf
{
matrix transform;
};
struct VSOut
{
float2 tex : TexCoord;
float4 pos : SV_Position;
};
VSOut main( float3 pos : Position,float2 tex : TexCoord )
{
VSOut vso;
vso.pos = mul( float4(pos,1.0f),transform );
vso.tex = tex;
return vso;
}
\ No newline at end of file
cbuffer LightCBuf
{
float3 lightPos;
float3 ambient;
float3 diffuseColor;
float diffuseIntensity;
float attConst;
float attLin;
float attQuad;
};
cbuffer ObjectCBuf
{
float specularIntensity;
float specularPower;
float padding[2];
};
Texture2D tex;
SamplerState splr;
float4 main( float3 worldPos : Position,float3 n : Normal,float2 tc : Texcoord ) : SV_Target
{
// fragment to light vector data
const float3 vToL = lightPos - worldPos;
const float distToL = length( vToL );
const float3 dirToL = vToL / distToL;
// attenuation
const float att = 1.0f / (attConst + attLin * distToL + attQuad * (distToL * distToL));
// diffuse intensity
const float3 diffuse = diffuseColor * diffuseIntensity * att * max( 0.0f,dot( dirToL,n ) );
// reflected light vector
const float3 w = n * dot( vToL,n );
const float3 r = w * 2.0f - vToL;
// calculate specular intensity based on angle between viewing vector and reflection vector, narrow with power function
const float3 specular = att * (diffuseColor * diffuseIntensity) * specularIntensity * pow( max( 0.0f,dot( normalize( -r ),normalize( worldPos ) ) ),specularPower );
// final color
return float4(saturate(diffuse + ambient + specular), 1.0f) * tex.Sample(splr, tc);
}
\ No newline at end of file
cbuffer CBuf
{
matrix modelView;
matrix modelViewProj;
};
struct VSOut
{
float3 worldPos : Position;
float3 normal : Normal;
float2 tc : Texcoord;
float4 pos : SV_Position;
};
VSOut main( float3 pos : Position,float3 n : Normal,float2 tc : Texcoord )
{
VSOut vso;
vso.worldPos = (float3)mul( float4(pos,1.0f),modelView );
vso.normal = mul( n,(float3x3)modelView );
vso.pos = mul( float4(pos,1.0f),modelViewProj );
vso.tc = tc;
return vso;
}
\ No newline at end of file
#include "Topology.h" #include "Topology.h"
Topology::Topology( Graphics& gfx,D3D11_PRIMITIVE_TOPOLOGY type ) namespace Bind
:
type( type )
{}
void Topology::Bind( Graphics& gfx ) noexcept
{ {
GetContext( gfx )->IASetPrimitiveTopology( type ); Topology::Topology( Graphics& gfx,D3D11_PRIMITIVE_TOPOLOGY type )
:
type( type )
{}
void Topology::Bind( Graphics& gfx ) noexcept
{
GetContext( gfx )->IASetPrimitiveTopology( type );
}
} }
#pragma once #pragma once
#include "Bindable.h" #include "Bindable.h"
class Topology : public Bindable namespace Bind
{ {
public: class Topology : public Bindable
Topology( Graphics& gfx,D3D11_PRIMITIVE_TOPOLOGY type ); {
void Bind( Graphics& gfx ) noexcept override; public:
protected: Topology( Graphics& gfx,D3D11_PRIMITIVE_TOPOLOGY type );
D3D11_PRIMITIVE_TOPOLOGY type; void Bind( Graphics& gfx ) noexcept override;
}; protected:
\ No newline at end of file D3D11_PRIMITIVE_TOPOLOGY type;
};
}
\ No newline at end of file
#include "TransformCbuf.h" #include "TransformCbuf.h"
TransformCbuf::TransformCbuf( Graphics& gfx,const Drawable& parent,UINT slot ) namespace Bind
:
parent( parent )
{ {
if( !pVcbuf ) TransformCbuf::TransformCbuf( Graphics& gfx,const Drawable& parent,UINT slot )
:
parent( parent )
{ {
pVcbuf = std::make_unique<VertexConstantBuffer<Transforms>>( gfx,slot ); if( !pVcbuf )
{
pVcbuf = std::make_unique<VertexConstantBuffer<Transforms>>( gfx,slot );
}
} }
}
void TransformCbuf::Bind( Graphics& gfx ) noexcept void TransformCbuf::Bind( Graphics& gfx ) noexcept
{
const auto modelView = parent.GetTransformXM() * gfx.GetCamera();
const Transforms tf =
{ {
DirectX::XMMatrixTranspose( modelView ), const auto modelView = parent.GetTransformXM() * gfx.GetCamera();
DirectX::XMMatrixTranspose( const Transforms tf =
modelView * {
gfx.GetProjection() DirectX::XMMatrixTranspose( modelView ),
) DirectX::XMMatrixTranspose(
}; modelView *
pVcbuf->Update( gfx,tf ); gfx.GetProjection()
pVcbuf->Bind( gfx ); )
} };
pVcbuf->Update( gfx,tf );
pVcbuf->Bind( gfx );
}
std::unique_ptr<VertexConstantBuffer<TransformCbuf::Transforms>> TransformCbuf::pVcbuf; std::unique_ptr<VertexConstantBuffer<TransformCbuf::Transforms>> TransformCbuf::pVcbuf;
\ No newline at end of file }
\ No newline at end of file
...@@ -3,18 +3,21 @@ ...@@ -3,18 +3,21 @@
#include "Drawable.h" #include "Drawable.h"
#include <DirectXMath.h> #include <DirectXMath.h>
class TransformCbuf : public Bindable namespace Bind
{ {
private: class TransformCbuf : public Bindable
struct Transforms
{ {
DirectX::XMMATRIX modelViewProj; private:
DirectX::XMMATRIX model; struct Transforms
{
DirectX::XMMATRIX modelViewProj;
DirectX::XMMATRIX model;
};
public:
TransformCbuf( Graphics& gfx,const Drawable& parent,UINT slot = 0u );
void Bind( Graphics& gfx ) noexcept override;
private:
static std::unique_ptr<VertexConstantBuffer<Transforms>> pVcbuf;
const Drawable& parent;
}; };
public: }
TransformCbuf( Graphics& gfx,const Drawable& parent,UINT slot = 0u ); \ No newline at end of file
void Bind( Graphics& gfx ) noexcept override;
private:
static std::unique_ptr<VertexConstantBuffer<Transforms>> pVcbuf;
const Drawable& parent;
};
\ No newline at end of file
#include "Vertex.h"
namespace Dvtx
{
// VertexLayout
const VertexLayout::Element& VertexLayout::ResolveByIndex( size_t i ) const noxnd
{
return elements[i];
}
VertexLayout& VertexLayout::Append( ElementType type ) noxnd
{
elements.emplace_back( type,Size() );
return *this;
}
size_t VertexLayout::Size() const noxnd
{
return elements.empty() ? 0u : elements.back().GetOffsetAfter();
}
size_t VertexLayout::GetElementCount() const noexcept
{
return elements.size();
}
std::vector<D3D11_INPUT_ELEMENT_DESC> VertexLayout::GetD3DLayout() const noxnd
{
std::vector<D3D11_INPUT_ELEMENT_DESC> desc;
desc.reserve( GetElementCount() );
for( const auto& e : elements )
{
desc.push_back( e.GetDesc() );
}
return desc;
}
// VertexLayout::Element
VertexLayout::Element::Element( ElementType type,size_t offset )
:
type( type ),
offset( offset )
{}
size_t VertexLayout::Element::GetOffsetAfter() const noxnd
{
return offset + Size();
}
size_t VertexLayout::Element::GetOffset() const
{
return offset;
}
size_t VertexLayout::Element::Size() const noxnd
{
return SizeOf( type );
}
constexpr size_t VertexLayout::Element::SizeOf( ElementType type ) noxnd
{
switch( type )
{
case Position2D:
return sizeof( Map<Position2D>::SysType );
case Position3D:
return sizeof( Map<Position3D>::SysType );
case Texture2D:
return sizeof( Map<Texture2D>::SysType );
case Normal:
return sizeof( Map<Normal>::SysType );
case Float3Color:
return sizeof( Map<Float3Color>::SysType );
case Float4Color:
return sizeof( Map<Float4Color>::SysType );
case BGRAColor:
return sizeof( Map<BGRAColor>::SysType );
}
assert( "Invalid element type" && false );
return 0u;
}
VertexLayout::ElementType VertexLayout::Element::GetType() const noexcept
{
return type;
}
D3D11_INPUT_ELEMENT_DESC VertexLayout::Element::GetDesc() const noxnd
{
switch( type )
{
case Position2D:
return GenerateDesc<Position2D>( GetOffset() );
case Position3D:
return GenerateDesc<Position3D>( GetOffset() );
case Texture2D:
return GenerateDesc<Texture2D>( GetOffset() );
case Normal:
return GenerateDesc<Normal>( GetOffset() );
case Float3Color:
return GenerateDesc<Float3Color>( GetOffset() );
case Float4Color:
return GenerateDesc<Float4Color>( GetOffset() );
case BGRAColor:
return GenerateDesc<BGRAColor>( GetOffset() );
}
assert( "Invalid element type" && false );
return { "INVALID",0,DXGI_FORMAT_UNKNOWN,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 };
}
// Vertex
Vertex::Vertex( char* pData,const VertexLayout& layout ) noxnd
:
pData( pData ),
layout( layout )
{
assert( pData != nullptr );
}
ConstVertex::ConstVertex( const Vertex& v ) noxnd
:
vertex( v )
{}
// VertexBuffer
VertexBuffer::VertexBuffer( VertexLayout layout ) noxnd
:
layout( std::move( layout ) )
{}
const char* VertexBuffer::GetData() const noxnd
{
return buffer.data();
}
const VertexLayout& VertexBuffer::GetLayout() const noexcept
{
return layout;
}
size_t VertexBuffer::Size() const noxnd
{
return buffer.size() / layout.Size();
}
size_t VertexBuffer::SizeBytes() const noxnd
{
return buffer.size();
}
Vertex VertexBuffer::Back() noxnd
{
assert( buffer.size() != 0u );
return Vertex{ buffer.data() + buffer.size() - layout.Size(),layout };
}
Vertex VertexBuffer::Front() noxnd
{
assert( buffer.size() != 0u );
return Vertex{ buffer.data(),layout };
}
Vertex VertexBuffer::operator[]( size_t i ) noxnd
{
assert( i < Size() );
return Vertex{ buffer.data() + layout.Size() * i,layout };
}
ConstVertex VertexBuffer::Back() const noxnd
{
return const_cast<VertexBuffer*>(this)->Back();
}
ConstVertex VertexBuffer::Front() const noxnd
{
return const_cast<VertexBuffer*>(this)->Front();
}
ConstVertex VertexBuffer::operator[]( size_t i ) const noxnd
{
return const_cast<VertexBuffer&>(*this)[i];
}
}
\ No newline at end of file
...@@ -2,17 +2,11 @@ ...@@ -2,17 +2,11 @@
#include <vector> #include <vector>
#include <type_traits> #include <type_traits>
#include "Graphics.h" #include "Graphics.h"
#include "Color.h"
#include "ConditionalNoexcept.h"
namespace hw3dexp namespace Dvtx
{ {
struct BGRAColor
{
unsigned char a;
unsigned char r;
unsigned char g;
unsigned char b;
};
class VertexLayout class VertexLayout
{ {
public: public:
...@@ -66,7 +60,7 @@ namespace hw3dexp ...@@ -66,7 +60,7 @@ namespace hw3dexp
}; };
template<> struct Map<BGRAColor> template<> struct Map<BGRAColor>
{ {
using SysType = hw3dexp::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";
}; };
...@@ -74,74 +68,16 @@ namespace hw3dexp ...@@ -74,74 +68,16 @@ namespace hw3dexp
class Element class Element
{ {
public: public:
Element( ElementType type,size_t offset ) Element( ElementType type,size_t offset );
: size_t GetOffsetAfter() const noxnd;
type( type ), size_t GetOffset() const;
offset( offset ) size_t Size() const noxnd;
{} static constexpr size_t SizeOf( ElementType type ) noxnd;
size_t GetOffsetAfter() const noexcept(!IS_DEBUG) ElementType GetType() const noexcept;
{ D3D11_INPUT_ELEMENT_DESC GetDesc() const noxnd;
return offset + Size();
}
size_t GetOffset() const
{
return offset;
}
size_t Size() const noexcept(!IS_DEBUG)
{
return SizeOf( type );
}
static constexpr size_t SizeOf( ElementType type ) noexcept(!IS_DEBUG)
{
switch( type )
{
case Position2D:
return sizeof( Map<Position2D>::SysType );
case Position3D:
return sizeof( Map<Position3D>::SysType );
case Texture2D:
return sizeof( Map<Texture2D>::SysType );
case Normal:
return sizeof( Map<Normal>::SysType );
case Float3Color:
return sizeof( Map<Float3Color>::SysType );
case Float4Color:
return sizeof( Map<Float4Color>::SysType );
case BGRAColor:
return sizeof( Map<BGRAColor>::SysType );
}
assert( "Invalid element type" && false );
return 0u;
}
ElementType GetType() const noexcept
{
return type;
}
D3D11_INPUT_ELEMENT_DESC GetDesc() const noexcept(!IS_DEBUG)
{
switch( type )
{
case Position2D:
return GenerateDesc<Position2D>( GetOffset() );
case Position3D:
return GenerateDesc<Position3D>( GetOffset() );
case Texture2D:
return GenerateDesc<Texture2D>( GetOffset() );
case Normal:
return GenerateDesc<Normal>( GetOffset() );
case Float3Color:
return GenerateDesc<Float3Color>( GetOffset() );
case Float4Color:
return GenerateDesc<Float4Color>( GetOffset() );
case BGRAColor:
return GenerateDesc<BGRAColor>( GetOffset() );
}
assert( "Invalid element type" && false );
return { "INVALID",0,DXGI_FORMAT_UNKNOWN,0,0,D3D11_INPUT_PER_VERTEX_DATA,0 };
}
private: private:
template<ElementType type> template<ElementType type>
static constexpr D3D11_INPUT_ELEMENT_DESC GenerateDesc( size_t offset ) noexcept(!IS_DEBUG) static constexpr D3D11_INPUT_ELEMENT_DESC GenerateDesc( size_t offset ) noxnd
{ {
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 };
} }
...@@ -151,7 +87,7 @@ namespace hw3dexp ...@@ -151,7 +87,7 @@ namespace hw3dexp
}; };
public: public:
template<ElementType Type> template<ElementType Type>
const Element& Resolve() const noexcept(!IS_DEBUG) const Element& Resolve() const noxnd
{ {
for( auto& e : elements ) for( auto& e : elements )
{ {
...@@ -163,33 +99,11 @@ namespace hw3dexp ...@@ -163,33 +99,11 @@ namespace hw3dexp
assert( "Could not resolve element type" && false ); assert( "Could not resolve element type" && false );
return elements.front(); return elements.front();
} }
const Element& ResolveByIndex( size_t i ) const noexcept(!IS_DEBUG) const Element& ResolveByIndex( size_t i ) const noxnd;
{ VertexLayout& Append( ElementType type ) noxnd;
return elements[i]; size_t Size() const noxnd;
} size_t GetElementCount() const noexcept;
VertexLayout& Append( ElementType type ) noexcept(!IS_DEBUG) std::vector<D3D11_INPUT_ELEMENT_DESC> GetD3DLayout() const noxnd;
{
elements.emplace_back( type,Size() );
return *this;
}
size_t Size() const noexcept(!IS_DEBUG)
{
return elements.empty() ? 0u : elements.back().GetOffsetAfter();
}
size_t GetElementCount() const noexcept
{
return elements.size();
}
std::vector<D3D11_INPUT_ELEMENT_DESC> GetD3DLayout() const noexcept(!IS_DEBUG)
{
std::vector<D3D11_INPUT_ELEMENT_DESC> desc;
desc.reserve( GetElementCount() );
for( const auto& e : elements )
{
desc.push_back( e.GetDesc() );
}
return desc;
}
private: private:
std::vector<Element> elements; std::vector<Element> elements;
}; };
...@@ -199,13 +113,13 @@ namespace hw3dexp ...@@ -199,13 +113,13 @@ namespace hw3dexp
friend class VertexBuffer; friend class VertexBuffer;
public: public:
template<VertexLayout::ElementType Type> template<VertexLayout::ElementType Type>
auto& Attr() noexcept(!IS_DEBUG) auto& Attr() noxnd
{ {
auto pAttribute = pData + layout.Resolve<Type>().GetOffset(); auto pAttribute = pData + layout.Resolve<Type>().GetOffset();
return *reinterpret_cast<typename VertexLayout::Map<Type>::SysType*>(pAttribute); return *reinterpret_cast<typename VertexLayout::Map<Type>::SysType*>(pAttribute);
} }
template<typename T> template<typename T>
void SetAttributeByIndex( size_t i,T&& val ) noexcept(!IS_DEBUG) void SetAttributeByIndex( size_t i,T&& val ) noxnd
{ {
const auto& element = layout.ResolveByIndex( i ); const auto& element = layout.ResolveByIndex( i );
auto pAttribute = pData + element.GetOffset(); auto pAttribute = pData + element.GetOffset();
...@@ -237,24 +151,18 @@ namespace hw3dexp ...@@ -237,24 +151,18 @@ namespace hw3dexp
} }
} }
protected: protected:
Vertex( char* pData,const VertexLayout& layout ) noexcept(!IS_DEBUG) Vertex( char* pData,const VertexLayout& layout ) noxnd;
:
pData( pData ),
layout( layout )
{
assert( pData != nullptr );
}
private: private:
template<typename First,typename ...Rest>
// enables parameter pack setting of multiple parameters by element index // enables parameter pack setting of multiple parameters by element index
void SetAttributeByIndex( size_t i,First&& first,Rest&&... rest ) noexcept(!IS_DEBUG) template<typename First,typename ...Rest>
void SetAttributeByIndex( size_t i,First&& first,Rest&&... rest ) noxnd
{ {
SetAttributeByIndex( i,std::forward<First>( first ) ); SetAttributeByIndex( i,std::forward<First>( first ) );
SetAttributeByIndex( i + 1,std::forward<Rest>( rest )... ); SetAttributeByIndex( i + 1,std::forward<Rest>( rest )... );
} }
// helper to reduce code duplication in SetAttributeByIndex // helper to reduce code duplication in SetAttributeByIndex
template<VertexLayout::ElementType DestLayoutType,typename SrcType> template<VertexLayout::ElementType DestLayoutType,typename SrcType>
void SetAttribute( char* pAttribute,SrcType&& val ) noexcept(!IS_DEBUG) void SetAttribute( char* pAttribute,SrcType&& val ) noxnd
{ {
using Dest = typename VertexLayout::Map<DestLayoutType>::SysType; using Dest = typename VertexLayout::Map<DestLayoutType>::SysType;
if constexpr( std::is_assignable<Dest,SrcType>::value ) if constexpr( std::is_assignable<Dest,SrcType>::value )
...@@ -274,12 +182,9 @@ namespace hw3dexp ...@@ -274,12 +182,9 @@ namespace hw3dexp
class ConstVertex class ConstVertex
{ {
public: public:
ConstVertex( const Vertex& v ) noexcept(!IS_DEBUG) ConstVertex( const Vertex& v ) noxnd;
:
vertex( v )
{}
template<VertexLayout::ElementType Type> template<VertexLayout::ElementType Type>
const auto& Attr() const noexcept(!IS_DEBUG) const auto& Attr() const noxnd
{ {
return const_cast<Vertex&>(vertex).Attr<Type>(); return const_cast<Vertex&>(vertex).Attr<Type>();
} }
...@@ -290,60 +195,24 @@ namespace hw3dexp ...@@ -290,60 +195,24 @@ namespace hw3dexp
class VertexBuffer class VertexBuffer
{ {
public: public:
VertexBuffer( VertexLayout layout ) noexcept(!IS_DEBUG) VertexBuffer( VertexLayout layout ) noxnd;
: const char* GetData() const noxnd;
layout( std::move( layout ) ) const VertexLayout& GetLayout() const noexcept;
{} size_t Size() const noxnd;
const char* GetData() const noexcept(!IS_DEBUG) size_t SizeBytes() const noxnd;
{
return buffer.data();
}
const VertexLayout& GetLayout() const noexcept
{
return layout;
}
size_t Size() const noexcept(!IS_DEBUG)
{
return buffer.size() / layout.Size();
}
size_t SizeBytes() const noexcept(!IS_DEBUG)
{
return buffer.size();
}
template<typename ...Params> template<typename ...Params>
void EmplaceBack( Params&&... params ) noexcept(!IS_DEBUG) void EmplaceBack( Params&&... params ) noxnd
{ {
assert( sizeof...(params) == layout.GetElementCount() && "Param count doesn't match number of vertex elements" ); assert( sizeof...(params) == layout.GetElementCount() && "Param count doesn't match number of vertex elements" );
buffer.resize( buffer.size() + layout.Size() ); buffer.resize( buffer.size() + layout.Size() );
Back().SetAttributeByIndex( 0u,std::forward<Params>( params )... ); Back().SetAttributeByIndex( 0u,std::forward<Params>( params )... );
} }
Vertex Back() noexcept(!IS_DEBUG) Vertex Back() noxnd;
{ Vertex Front() noxnd;
assert( buffer.size() != 0u ); Vertex operator[]( size_t i ) noxnd;
return Vertex{ buffer.data() + buffer.size() - layout.Size(),layout }; ConstVertex Back() const noxnd;
} ConstVertex Front() const noxnd;
Vertex Front() noexcept(!IS_DEBUG) ConstVertex operator[]( size_t i ) const noxnd;
{
assert( buffer.size() != 0u );
return Vertex{ buffer.data(),layout };
}
Vertex operator[]( size_t i ) noexcept(!IS_DEBUG)
{
assert( i < Size() );
return Vertex{ buffer.data() + layout.Size() * i,layout };
}
ConstVertex Back() const noexcept(!IS_DEBUG)
{
return const_cast<VertexBuffer*>(this)->Back();
}
ConstVertex Front() const noexcept(!IS_DEBUG)
{
return const_cast<VertexBuffer*>(this)->Front();
}
ConstVertex operator[]( size_t i ) const noexcept(!IS_DEBUG)
{
return const_cast<VertexBuffer&>(*this)[i];
}
private: private:
std::vector<char> buffer; std::vector<char> buffer;
VertexLayout layout; VertexLayout layout;
......
#include "VertexBuffer.h" #include "VertexBuffer.h"
void VertexBuffer::Bind( Graphics& gfx ) noexcept namespace Bind
{ {
const UINT offset = 0u; VertexBuffer::VertexBuffer( Graphics& gfx,const Dvtx::VertexBuffer& vbuf )
GetContext( gfx )->IASetVertexBuffers( 0u,1u,pVertexBuffer.GetAddressOf(),&stride,&offset ); :
stride( (UINT)vbuf.GetLayout().Size() )
{
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( vbuf.SizeBytes() );
bd.StructureByteStride = stride;
D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = vbuf.GetData();
GFX_THROW_INFO( GetDevice( gfx )->CreateBuffer( &bd,&sd,&pVertexBuffer ) );
}
void VertexBuffer::Bind( Graphics& gfx ) noexcept
{
const UINT offset = 0u;
GetContext( gfx )->IASetVertexBuffers( 0u,1u,pVertexBuffer.GetAddressOf(),&stride,&offset );
}
} }
...@@ -3,46 +3,33 @@ ...@@ -3,46 +3,33 @@
#include "GraphicsThrowMacros.h" #include "GraphicsThrowMacros.h"
#include "Vertex.h" #include "Vertex.h"
class VertexBuffer : public Bindable namespace Bind
{ {
public: class VertexBuffer : public Bindable
template<class V>
VertexBuffer( Graphics& gfx,const std::vector<V>& vertices )
:
stride( sizeof( V ) )
{ {
INFOMAN( gfx ); public:
template<class V>
VertexBuffer( Graphics& gfx,const std::vector<V>& vertices )
:
stride( sizeof( V ) )
{
INFOMAN( gfx );
D3D11_BUFFER_DESC bd = {}; D3D11_BUFFER_DESC bd = {};
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER; bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.Usage = D3D11_USAGE_DEFAULT; bd.Usage = D3D11_USAGE_DEFAULT;
bd.CPUAccessFlags = 0u; bd.CPUAccessFlags = 0u;
bd.MiscFlags = 0u; bd.MiscFlags = 0u;
bd.ByteWidth = UINT( sizeof( V ) * vertices.size() ); bd.ByteWidth = UINT( sizeof( V ) * vertices.size() );
bd.StructureByteStride = sizeof( V ); bd.StructureByteStride = sizeof( V );
D3D11_SUBRESOURCE_DATA sd = {}; D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = vertices.data(); sd.pSysMem = vertices.data();
GFX_THROW_INFO( GetDevice( gfx )->CreateBuffer( &bd,&sd,&pVertexBuffer ) ); GFX_THROW_INFO( GetDevice( gfx )->CreateBuffer( &bd,&sd,&pVertexBuffer ) );
} }
VertexBuffer( Graphics& gfx,const hw3dexp::VertexBuffer& vbuf ) VertexBuffer( Graphics& gfx,const Dvtx::VertexBuffer& vbuf );
: void Bind( Graphics& gfx ) noexcept override;
stride( (UINT)vbuf.GetLayout().Size() ) protected:
{ UINT stride;
INFOMAN( gfx ); Microsoft::WRL::ComPtr<ID3D11Buffer> pVertexBuffer;
};
D3D11_BUFFER_DESC bd = {}; }
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.Usage = D3D11_USAGE_DEFAULT;
bd.CPUAccessFlags = 0u;
bd.MiscFlags = 0u;
bd.ByteWidth = UINT( vbuf.SizeBytes() );
bd.StructureByteStride = stride;
D3D11_SUBRESOURCE_DATA sd = {};
sd.pSysMem = vbuf.GetData();
GFX_THROW_INFO( GetDevice( gfx )->CreateBuffer( &bd,&sd,&pVertexBuffer ) );
}
void Bind( Graphics& gfx ) noexcept override;
protected:
UINT stride;
Microsoft::WRL::ComPtr<ID3D11Buffer> pVertexBuffer;
};
#include "VertexShader.h" #include "VertexShader.h"
#include "GraphicsThrowMacros.h" #include "GraphicsThrowMacros.h"
namespace Bind
VertexShader::VertexShader( Graphics& gfx,const std::wstring& path )
{ {
INFOMAN( gfx ); VertexShader::VertexShader( Graphics& gfx,const std::wstring& path )
{
INFOMAN( gfx );
GFX_THROW_INFO( D3DReadFileToBlob( path.c_str(),&pBytecodeBlob ) ); GFX_THROW_INFO( D3DReadFileToBlob( path.c_str(),&pBytecodeBlob ) );
GFX_THROW_INFO( GetDevice( gfx )->CreateVertexShader( GFX_THROW_INFO( GetDevice( gfx )->CreateVertexShader(
pBytecodeBlob->GetBufferPointer(), pBytecodeBlob->GetBufferPointer(),
pBytecodeBlob->GetBufferSize(), pBytecodeBlob->GetBufferSize(),
nullptr, nullptr,
&pVertexShader &pVertexShader
) ); ) );
} }
void VertexShader::Bind( Graphics& gfx ) noexcept void VertexShader::Bind( Graphics& gfx ) noexcept
{ {
GetContext( gfx )->VSSetShader( pVertexShader.Get(),nullptr,0u ); GetContext( gfx )->VSSetShader( pVertexShader.Get(),nullptr,0u );
} }
ID3DBlob* VertexShader::GetBytecode() const noexcept ID3DBlob* VertexShader::GetBytecode() const noexcept
{ {
return pBytecodeBlob.Get(); return pBytecodeBlob.Get();
}
} }
#pragma once #pragma once
#include "Bindable.h" #include "Bindable.h"
class VertexShader : public Bindable namespace Bind
{ {
public: class VertexShader : public Bindable
VertexShader( Graphics& gfx,const std::wstring& path ); {
void Bind( Graphics& gfx ) noexcept override; public:
ID3DBlob* GetBytecode() const noexcept; VertexShader( Graphics& gfx,const std::wstring& path );
protected: void Bind( Graphics& gfx ) noexcept override;
Microsoft::WRL::ComPtr<ID3DBlob> pBytecodeBlob; ID3DBlob* GetBytecode() const noexcept;
Microsoft::WRL::ComPtr<ID3D11VertexShader> pVertexShader; protected:
}; Microsoft::WRL::ComPtr<ID3DBlob> pBytecodeBlob;
\ No newline at end of file Microsoft::WRL::ComPtr<ID3D11VertexShader> pVertexShader;
};
}
\ No newline at end of file
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