Skip to content

Commit

Permalink
Fix shader generated by Layout. Input signature matches.
Browse files Browse the repository at this point in the history
  • Loading branch information
nieznanysprawiciel committed Oct 12, 2019
1 parent 9f1da40 commit 1b6f00f
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 64 deletions.
2 changes: 1 addition & 1 deletion DX11API/DX11Initializer/DX11APIObjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class DX11APIObjects
protected:

void SetDebugName ( ID3D11DeviceChild* child, const std::string& name );
bool IsDebugLayerEnabled ();
static bool IsDebugLayerEnabled ();
};


Expand Down
113 changes: 61 additions & 52 deletions DX11API/DX11Resources/DX11InputLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include "DX11InputLayout.h"
#include "DX11Compiler.h"

#include "swGraphicAPI/DX11API/DX11Initializer/DX11Utils.h"

#include <string.h>



RTTR_REGISTRATION
Expand All @@ -23,9 +27,10 @@ namespace sw

// ================================ //
//
DX11InputLayout::DX11InputLayout ( const AssetPath& fileName, ID3D11InputLayout* layout )
DX11InputLayout::DX11InputLayout ( const AssetPath& fileName, ID3D11InputLayout* layout, InputLayoutDescriptor desc )
: ShaderInputLayout( fileName )
, m_vertexLayout( layout )
, m_descriptor( std::move( desc ) )
{}

// ================================ //
Expand Down Expand Up @@ -74,8 +79,9 @@ class DX11LayoutTranslator
std::string GenerateMain () const;
std::string GenerateInputStruct ( const LayoutVec& layout ) const;
std::string GenerateAttribute ( const D3D11_INPUT_ELEMENT_DESC& attribute ) const;
std::string GenerateSemanticName ( const D3D11_INPUT_ELEMENT_DESC& attribute ) const;
std::string InputStructName () const { return "InputVS"; }
std::string MapSemanticToType ( DXGI_FORMAT format ) const;
std::string MapSemanticToType ( const char* semantic ) const;
};


Expand Down Expand Up @@ -118,26 +124,27 @@ D3D11_INPUT_ELEMENT_DESC DX11LayoutTranslator::Translate ( const sw::LayoutEnt

// ================================ //
//
const char* DX11LayoutTranslator::Translate ( sw::AttributeSemantic semantic ) const
static std::tuple< sw::AttributeSemantic, const char*, const char* > SemanticsMap[] =
{
std::make_tuple( sw::AttributeSemantic::Position, "POSITION", "float4" ),
std::make_tuple( sw::AttributeSemantic::Normal, "NORMAL", "float3" ),
std::make_tuple( sw::AttributeSemantic::Tangent, "TANGENT", "float3" ),
std::make_tuple( sw::AttributeSemantic::Binormal, "BINORMAL", "float3" ),
std::make_tuple( sw::AttributeSemantic::BlendIndicies, "BLENDINDICES", "uint" ),
std::make_tuple( sw::AttributeSemantic::BlendWeights, "BLENDWEIGHT", "float" ),
std::make_tuple( sw::AttributeSemantic::Color, "COLOR", "float4" ),
std::make_tuple( sw::AttributeSemantic::Texcoord, "TEXCOORD", "float2" ),
std::make_tuple( sw::AttributeSemantic::PositionTransformed, "POSITIONT", "float4" ),
std::make_tuple( sw::AttributeSemantic::PointSize, "PSIZE", "float" )
};

static std::pair< sw::AttributeSemantic, const char* > SemanticsMap[] =
{
std::make_pair( sw::AttributeSemantic::Position, "POSITION" ),
std::make_pair( sw::AttributeSemantic::Normal, "NORMAL" ),
std::make_pair( sw::AttributeSemantic::Tangent, "TANGENT" ),
std::make_pair( sw::AttributeSemantic::Binormal, "BINORMAL" ),
std::make_pair( sw::AttributeSemantic::BlendIndicies, "BLENDINDICES" ),
std::make_pair( sw::AttributeSemantic::BlendWeights, "BLENDWEIGHT" ),
std::make_pair( sw::AttributeSemantic::Color, "COLOR" ),
std::make_pair( sw::AttributeSemantic::Texcoord, "TEXCOORD" ),
std::make_pair( sw::AttributeSemantic::PositionTransformed, "POSITIONT" ),
std::make_pair( sw::AttributeSemantic::PointSize, "PSIZE" )
};

for( auto& pair : SemanticsMap )
if( pair.first == semantic )
return pair.second;
// ================================ //
//
const char* DX11LayoutTranslator::Translate ( sw::AttributeSemantic semantic ) const
{
for( auto& tuple : SemanticsMap )
if( std::get< 0 >( tuple ) == semantic )
return std::get< 1 >( tuple );

return "";
}
Expand Down Expand Up @@ -176,8 +183,8 @@ Size DX11LayoutTranslator::CountSemantic ( const char* semanticName ) cons
std::string DX11LayoutTranslator::GenerateMain () const
{
std::string main;
main = "struct OutputVS {};\n";
main += "OutputVS main()\n";
main = "struct OutputVS {};\n\n";
main += "OutputVS main( " + InputStructName() + " input )\n";
main += "{\n";
main += "return (OutputVS)0;\n";
main += "}\n";
Expand All @@ -198,7 +205,7 @@ std::string DX11LayoutTranslator::GenerateInputStruct ( const LayoutVec& la
layoutStruct += "\n";
}

layoutStruct += "\n};\n";
layoutStruct += "\n};\n\n";

return layoutStruct;
}
Expand All @@ -207,35 +214,30 @@ std::string DX11LayoutTranslator::GenerateInputStruct ( const LayoutVec& la
//
std::string DX11LayoutTranslator::GenerateAttribute ( const D3D11_INPUT_ELEMENT_DESC& attribute ) const
{
std::string fullSemanticStr = attribute.SemanticName + Convert::ToString( attribute.SemanticIndex );

std::string attributeEntry;
std::string fullSemanticStr = GenerateSemanticName( attribute );
std::string attributeEntry = "\t";

attributeEntry = MapSemanticToType( attribute.Format );
attributeEntry = MapSemanticToType( attribute.SemanticName );
attributeEntry += " m" + fullSemanticStr + " : " + fullSemanticStr + ";";

return attributeEntry;
}

// ================================ //
//
std::string DX11LayoutTranslator::MapSemanticToType ( DXGI_FORMAT format ) const
std::string DX11LayoutTranslator::GenerateSemanticName ( const D3D11_INPUT_ELEMENT_DESC& attribute ) const
{
switch( format )
{
case DXGI_FORMAT::DXGI_FORMAT_R8G8B8A8_UINT:
return "uint4";
case DXGI_FORMAT::DXGI_FORMAT_R8G8_UINT:
return "uint2";
case DXGI_FORMAT::DXGI_FORMAT_R32G32B32A32_FLOAT:
return "float4";
case DXGI_FORMAT::DXGI_FORMAT_R32G32B32_FLOAT:
return "float3";
case DXGI_FORMAT::DXGI_FORMAT_R32G32_FLOAT:
return "float2";
case DXGI_FORMAT::DXGI_FORMAT_R32_FLOAT:
return "float";
}
return attribute.SemanticName + Convert::ToString( attribute.SemanticIndex );
}

// ================================ //
//
std::string DX11LayoutTranslator::MapSemanticToType ( const char* semantic ) const
{
for( auto& tuple : SemanticsMap )
if( strcmp( std::get< 1 >( tuple ), semantic ) == 0 )
return std::get< 2 >( tuple );

return "float4";
}

Expand All @@ -249,31 +251,38 @@ sw::Nullable< DX11InputLayout* > DX11InputLayout::CreateLayout ( const AssetP
std::string shaderCode = layoutTranslator.GenerateShader();
sw::CompilationConfig config( ShaderType::VertexShader );

#ifdef _DEBUG
config.Debug = true;
#endif
if( IsDebugLayerEnabled() )
{
config.Debug = true;
}

auto compiledBlob = sw::DX11Compiler::CompileShader( shaderCode, "main", config );

if( !compiledBlob.IsValid() )
{
#ifdef _DEBUG
OutputDebugStringA( compiledBlob.GetErrorReason().c_str() );
#endif
if( IsDebugLayerEnabled() )
OutputDebugStringA( compiledBlob.GetErrorReason().c_str() );

return compiledBlob.GetError();
}

auto& compiledShader = compiledBlob.Get();
auto DX11layout = layoutTranslator.GetDX11LayoutDesc();

ID3D11InputLayout* DX11layoutInterface = nullptr;

HRESULT result = device->CreateInputLayout( DX11layout.data(), (UINT)DX11layout.size(), compiledShader->GetBufferPointer(), compiledShader->GetBufferSize(), &DX11layoutInterface );

if( FAILED( result ) )
return "[DX11InputLayout] Can't create layout.";
return fmt::format( "[DX11InputLayout] Can't create layout. {}", DX11Utils::ErrorString( result ) );

return new DX11InputLayout( fileName, DX11layoutInterface, layoutDesc );
}

return new DX11InputLayout( fileName, DX11layoutInterface );
// ================================ //
//
const InputLayoutDescriptor& DX11InputLayout::GetDescriptor () const
{
return m_descriptor;
}


Expand Down
9 changes: 6 additions & 3 deletions DX11API/DX11Resources/DX11InputLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,23 @@ class DX11InputLayout : public ShaderInputLayout, protected DX11APIObjects
private:

ComPtr< ID3D11InputLayout > m_vertexLayout;
InputLayoutDescriptor m_descriptor;

protected:

virtual ~DX11InputLayout ();

public:

explicit DX11InputLayout ( const AssetPath& fileName, ID3D11InputLayout* layout );

static sw::Nullable< DX11InputLayout* > CreateLayout ( const AssetPath& fileName, const InputLayoutDescriptor& layoutDesc );
explicit DX11InputLayout ( const AssetPath& fileName, ID3D11InputLayout* layout, InputLayoutDescriptor desc );
static sw::Nullable< DX11InputLayout* > CreateLayout ( const AssetPath& fileName, const InputLayoutDescriptor& layoutDesc );

public:

inline ID3D11InputLayout* Get () { return m_vertexLayout.Get(); }

virtual const InputLayoutDescriptor& GetDescriptor () const override;
};

} // sw
10 changes: 8 additions & 2 deletions MockAPI/MockResources/MockInputLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,21 @@ namespace sw

// ================================ //
//
MockInputLayout::MockInputLayout ( const AssetPath& fileName )
MockInputLayout::MockInputLayout ( const AssetPath& fileName, InputLayoutDescriptor layoutDesc )
: ShaderInputLayout( fileName )
, m_desc( std::move( layoutDesc ) )
{}

// ================================ //
//
sw::Nullable< MockInputLayout* > MockInputLayout::CreateLayout ( const AssetPath& fileName, const InputLayoutDescriptor& layoutDesc )
{
return new MockInputLayout( fileName );
return new MockInputLayout( fileName, layoutDesc );
}

const InputLayoutDescriptor& MockInputLayout::GetDescriptor () const
{
return m_desc;
}

// ================================ //
Expand Down
7 changes: 6 additions & 1 deletion MockAPI/MockResources/MockInputLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ class MockInputLayout : public ShaderInputLayout
{
RTTR_ENABLE( ShaderInputLayout );
private:

InputLayoutDescriptor m_desc;

protected:

virtual ~MockInputLayout ();

public:
explicit MockInputLayout ( const AssetPath& fileName );
explicit MockInputLayout ( const AssetPath& fileName, InputLayoutDescriptor desc );


static sw::Nullable< MockInputLayout* > CreateLayout ( const AssetPath& fileName, const InputLayoutDescriptor& layoutDesc );

virtual const InputLayoutDescriptor& GetDescriptor () const override;
};

} // sw
6 changes: 5 additions & 1 deletion Resources/Shaders/InputLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


#include "swGraphicAPI/Resources/Shaders/IShaderInputLayout.h"

#include "swGraphicAPI/Resources/Shaders/LayoutInitData.h"



Expand All @@ -30,6 +30,10 @@ class ShaderInputLayout : public IShaderInputLayout

public:
explicit ShaderInputLayout ( const AssetPath& assetPath ) : IShaderInputLayout( assetPath ) {}

public:

virtual const InputLayoutDescriptor& GetDescriptor () const = 0;
};

DEFINE_RESOURCE_PTR_TYPE( ShaderInputLayout );
Expand Down
5 changes: 3 additions & 2 deletions Resources/Shaders/Layouts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ RTTR_REGISTRATION
.property( "IsPerInstance", &sw::LayoutEntry::PerInstance )
.property( "InstanceDataStep", &sw::LayoutEntry::InstanceDataStep );

rttr::registration::class_< sw::InputLayoutDescriptor >( "sw::InputLayoutDescriptor" )
.property( "Entries", &sw::InputLayoutDescriptor::m_entries );
rttr::registration::class_< sw::InputLayoutDescriptor >( "sw::InputLayoutDescriptor" )
.property( "Entries", &sw::InputLayoutDescriptor::m_entries );


}

Expand Down
4 changes: 2 additions & 2 deletions Resources/Shaders/Shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ using namespace sw;

RTTR_REGISTRATION
{
rttr::registration::class_< ShaderInputLayout >( "sw::ShaderInputLayout" )
.property_readonly( "Descriptor", &sw::ShaderInputLayout::GetDescriptor );

// Shader
rttr::registration::class_< ShaderInputLayout >( "sw::ShaderInputLayout" );
rttr::registration::class_< IShader >( "sw::IShader" );

rttr::registration::class_< PixelShader >( "sw::PixelShader" );
Expand Down

0 comments on commit 1b6f00f

Please sign in to comment.