Skip to content

Commit d3be7e2

Browse files
Tests: Add native MSL mesh shader tests
1 parent c2e1ac6 commit d3be7e2

1 file changed

Lines changed: 323 additions & 0 deletions

File tree

Tests/DiligentCoreAPITest/src/MeshShaderTest.cpp

Lines changed: 323 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "gtest/gtest.h"
3232

3333
#include "InlineShaders/MeshShaderTestHLSL.h"
34+
#include "InlineShaders/MeshShaderTestMSL.h"
3435

3536
namespace Diligent
3637
{
@@ -576,4 +577,326 @@ TEST(MeshShaderTest, DrawTrisWithAmplificationShader)
576577
pSwapChain->Present();
577578
}
578579

580+
581+
TEST(MeshShaderTest, DrawTriangle_MSL)
582+
{
583+
auto* pEnv = GPUTestingEnvironment::GetInstance();
584+
auto* pDevice = pEnv->GetDevice();
585+
const auto& deviceInfo = pDevice->GetDeviceInfo();
586+
if (!deviceInfo.IsMetalDevice())
587+
{
588+
GTEST_SKIP() << "MSL is only supported in Metal";
589+
}
590+
if (!deviceInfo.Features.MeshShaders)
591+
{
592+
GTEST_SKIP() << "Mesh shader is not supported by this device";
593+
}
594+
595+
GPUTestingEnvironment::ScopedReset EnvironmentAutoReset;
596+
597+
auto* pSwapChain = pEnv->GetSwapChain();
598+
auto* pContext = pEnv->GetDeviceContext();
599+
600+
RefCntAutoPtr<ITestingSwapChain> pTestingSwapChain(pSwapChain, IID_TestingSwapChain);
601+
if (pTestingSwapChain)
602+
{
603+
pContext->Flush();
604+
pContext->InvalidateState();
605+
606+
#if METAL_SUPPORTED
607+
MeshShaderDrawReferenceMtl(pSwapChain);
608+
#else
609+
LOG_ERROR_AND_THROW("Metal is not supported");
610+
#endif
611+
612+
pTestingSwapChain->TakeSnapshot();
613+
}
614+
615+
ITextureView* pRTVs[] = {pSwapChain->GetCurrentBackBufferRTV()};
616+
pContext->SetRenderTargets(1, pRTVs, nullptr, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
617+
618+
float ClearColor[] = {0.f, 0.f, 0.f, 0.f};
619+
pContext->ClearRenderTarget(pRTVs[0], ClearColor, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
620+
621+
GraphicsPipelineStateCreateInfo PSOCreateInfo;
622+
623+
auto& PSODesc = PSOCreateInfo.PSODesc;
624+
auto& GraphicsPipeline = PSOCreateInfo.GraphicsPipeline;
625+
626+
PSODesc.Name = "MSL mesh shader test";
627+
628+
PSODesc.PipelineType = PIPELINE_TYPE_MESH;
629+
GraphicsPipeline.NumRenderTargets = 1;
630+
GraphicsPipeline.RTVFormats[0] = pSwapChain->GetDesc().ColorBufferFormat;
631+
GraphicsPipeline.PrimitiveTopology = PRIMITIVE_TOPOLOGY_UNDEFINED;
632+
GraphicsPipeline.RasterizerDesc.CullMode = CULL_MODE_BACK;
633+
GraphicsPipeline.RasterizerDesc.FillMode = FILL_MODE_SOLID;
634+
GraphicsPipeline.RasterizerDesc.FrontCounterClockwise = False;
635+
636+
GraphicsPipeline.DepthStencilDesc.DepthEnable = False;
637+
638+
ShaderCreateInfo ShaderCI;
639+
ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_MSL;
640+
641+
RefCntAutoPtr<IShader> pMS;
642+
{
643+
ShaderCI.Desc = {"MSL mesh shader test - MS", SHADER_TYPE_MESH, true};
644+
ShaderCI.EntryPoint = "MSmain";
645+
ShaderCI.Source = MSL::MeshShaderTest.c_str();
646+
647+
pDevice->CreateShader(ShaderCI, &pMS);
648+
ASSERT_NE(pMS, nullptr);
649+
}
650+
651+
RefCntAutoPtr<IShader> pPS;
652+
{
653+
ShaderCI.Desc = {"MSL mesh shader test - PS", SHADER_TYPE_PIXEL, true};
654+
ShaderCI.EntryPoint = "PSmain";
655+
ShaderCI.Source = MSL::MeshShaderTest.c_str();
656+
657+
pDevice->CreateShader(ShaderCI, &pPS);
658+
ASSERT_NE(pPS, nullptr);
659+
}
660+
661+
PSOCreateInfo.pMS = pMS;
662+
PSOCreateInfo.pPS = pPS;
663+
RefCntAutoPtr<IPipelineState> pPSO;
664+
pDevice->CreateGraphicsPipelineState(PSOCreateInfo, &pPSO);
665+
ASSERT_NE(pPSO, nullptr);
666+
667+
pContext->SetPipelineState(pPSO);
668+
669+
DrawMeshAttribsMtl mtlDrawAttrs{4, 1, 1};
670+
DrawMeshAttribs drawAttrs(1, DRAW_FLAG_VERIFY_ALL, &mtlDrawAttrs);
671+
pContext->DrawMesh(drawAttrs);
672+
673+
pSwapChain->Present();
674+
}
675+
676+
677+
TEST(MeshShaderTest, DrawTriangleIndirect_MSL)
678+
{
679+
auto* pEnv = GPUTestingEnvironment::GetInstance();
680+
auto* pDevice = pEnv->GetDevice();
681+
const auto& deviceInfo = pDevice->GetDeviceInfo();
682+
if (!deviceInfo.IsMetalDevice())
683+
{
684+
GTEST_SKIP() << "MSL is only supported in Metal";
685+
}
686+
if (!deviceInfo.Features.MeshShaders)
687+
{
688+
GTEST_SKIP() << "Mesh shader is not supported by this device";
689+
}
690+
691+
GPUTestingEnvironment::ScopedReset EnvironmentAutoReset;
692+
693+
auto* pSwapChain = pEnv->GetSwapChain();
694+
auto* pContext = pEnv->GetDeviceContext();
695+
696+
RefCntAutoPtr<ITestingSwapChain> pTestingSwapChain(pSwapChain, IID_TestingSwapChain);
697+
if (pTestingSwapChain)
698+
{
699+
pContext->Flush();
700+
pContext->InvalidateState();
701+
702+
#if METAL_SUPPORTED
703+
MeshShaderIndirectDrawReferenceMtl(pSwapChain);
704+
#else
705+
LOG_ERROR_AND_THROW("Metal is not supported");
706+
#endif
707+
708+
pTestingSwapChain->TakeSnapshot();
709+
}
710+
711+
ITextureView* pRTVs[] = {pSwapChain->GetCurrentBackBufferRTV()};
712+
pContext->SetRenderTargets(1, pRTVs, nullptr, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
713+
714+
float ClearColor[] = {0.f, 0.f, 0.f, 0.f};
715+
pContext->ClearRenderTarget(pRTVs[0], ClearColor, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
716+
717+
GraphicsPipelineStateCreateInfo PSOCreateInfo;
718+
719+
auto& PSODesc = PSOCreateInfo.PSODesc;
720+
auto& GraphicsPipeline = PSOCreateInfo.GraphicsPipeline;
721+
722+
PSODesc.Name = "MSL mesh shader indirect test";
723+
724+
PSODesc.PipelineType = PIPELINE_TYPE_MESH;
725+
GraphicsPipeline.NumRenderTargets = 1;
726+
GraphicsPipeline.RTVFormats[0] = pSwapChain->GetDesc().ColorBufferFormat;
727+
GraphicsPipeline.PrimitiveTopology = PRIMITIVE_TOPOLOGY_UNDEFINED;
728+
GraphicsPipeline.RasterizerDesc.CullMode = CULL_MODE_BACK;
729+
GraphicsPipeline.RasterizerDesc.FillMode = FILL_MODE_SOLID;
730+
GraphicsPipeline.RasterizerDesc.FrontCounterClockwise = False;
731+
732+
GraphicsPipeline.DepthStencilDesc.DepthEnable = False;
733+
734+
ShaderCreateInfo ShaderCI;
735+
ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_MSL;
736+
737+
RefCntAutoPtr<IShader> pMS;
738+
{
739+
ShaderCI.Desc = {"MSL mesh shader indirect test - MS", SHADER_TYPE_MESH, true};
740+
ShaderCI.EntryPoint = "MSmain";
741+
ShaderCI.Source = MSL::MeshShaderTest.c_str();
742+
743+
pDevice->CreateShader(ShaderCI, &pMS);
744+
ASSERT_NE(pMS, nullptr);
745+
}
746+
747+
RefCntAutoPtr<IShader> pPS;
748+
{
749+
ShaderCI.Desc = {"MSL mesh shader indirect test - PS", SHADER_TYPE_PIXEL, true};
750+
ShaderCI.EntryPoint = "PSmain";
751+
ShaderCI.Source = MSL::MeshShaderTest.c_str();
752+
753+
pDevice->CreateShader(ShaderCI, &pPS);
754+
ASSERT_NE(pPS, nullptr);
755+
}
756+
757+
PSOCreateInfo.pMS = pMS;
758+
PSOCreateInfo.pPS = pPS;
759+
RefCntAutoPtr<IPipelineState> pPSO;
760+
pDevice->CreateGraphicsPipelineState(PSOCreateInfo, &pPSO);
761+
ASSERT_NE(pPSO, nullptr);
762+
763+
struct IndirectBuffData
764+
{
765+
Uint32 IndirectData[3] = {1, 1, 1};
766+
};
767+
IndirectBuffData Data;
768+
769+
BufferDesc IndirectBufferDesc;
770+
IndirectBufferDesc.Name = "MSL indirect mesh buffer";
771+
IndirectBufferDesc.Usage = USAGE_IMMUTABLE;
772+
IndirectBufferDesc.Size = sizeof(Data);
773+
IndirectBufferDesc.BindFlags = BIND_INDIRECT_DRAW_ARGS;
774+
775+
BufferData InitData{&Data, sizeof(Data)};
776+
777+
RefCntAutoPtr<IBuffer> pBuffer;
778+
pDevice->CreateBuffer(IndirectBufferDesc, &InitData, &pBuffer);
779+
ASSERT_NE(pBuffer, nullptr);
780+
781+
pContext->SetPipelineState(pPSO);
782+
783+
DrawMeshAttribsMtl mtlDrawAttrs{4, 1, 1};
784+
DrawMeshIndirectAttribs drawAttrs;
785+
drawAttrs.pAttribsBuffer = pBuffer;
786+
drawAttrs.Flags = DRAW_FLAG_VERIFY_ALL;
787+
drawAttrs.AttribsBufferStateTransitionMode = RESOURCE_STATE_TRANSITION_MODE_TRANSITION;
788+
drawAttrs.pMtlAttribs = &mtlDrawAttrs;
789+
790+
pContext->DrawMeshIndirect(drawAttrs);
791+
792+
pSwapChain->Present();
793+
}
794+
795+
796+
TEST(MeshShaderTest, DrawTrisWithAmplificationShader_MSL)
797+
{
798+
auto* pEnv = GPUTestingEnvironment::GetInstance();
799+
auto* pDevice = pEnv->GetDevice();
800+
const auto& deviceInfo = pDevice->GetDeviceInfo();
801+
if (!deviceInfo.IsMetalDevice())
802+
{
803+
GTEST_SKIP() << "MSL is only supported in Metal";
804+
}
805+
if (!deviceInfo.Features.MeshShaders)
806+
{
807+
GTEST_SKIP() << "Mesh shader is not supported by this device";
808+
}
809+
810+
GPUTestingEnvironment::ScopedReset EnvironmentAutoReset;
811+
812+
auto* pSwapChain = pEnv->GetSwapChain();
813+
auto* pContext = pEnv->GetDeviceContext();
814+
815+
RefCntAutoPtr<ITestingSwapChain> pTestingSwapChain(pSwapChain, IID_TestingSwapChain);
816+
if (pTestingSwapChain)
817+
{
818+
pContext->Flush();
819+
pContext->InvalidateState();
820+
821+
#if METAL_SUPPORTED
822+
AmplificationShaderDrawReferenceMtl(pSwapChain);
823+
#else
824+
LOG_ERROR_AND_THROW("Metal is not supported");
825+
#endif
826+
827+
pTestingSwapChain->TakeSnapshot();
828+
}
829+
830+
ITextureView* pRTVs[] = {pSwapChain->GetCurrentBackBufferRTV()};
831+
pContext->SetRenderTargets(1, pRTVs, nullptr, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
832+
833+
float ClearColor[] = {0.f, 0.f, 0.f, 0.f};
834+
pContext->ClearRenderTarget(pRTVs[0], ClearColor, RESOURCE_STATE_TRANSITION_MODE_TRANSITION);
835+
836+
GraphicsPipelineStateCreateInfo PSOCreateInfo;
837+
838+
auto& PSODesc = PSOCreateInfo.PSODesc;
839+
auto& GraphicsPipeline = PSOCreateInfo.GraphicsPipeline;
840+
841+
PSODesc.Name = "MSL amplification shader test";
842+
843+
PSODesc.PipelineType = PIPELINE_TYPE_MESH;
844+
GraphicsPipeline.NumRenderTargets = 1;
845+
GraphicsPipeline.RTVFormats[0] = pSwapChain->GetDesc().ColorBufferFormat;
846+
GraphicsPipeline.PrimitiveTopology = PRIMITIVE_TOPOLOGY_UNDEFINED;
847+
GraphicsPipeline.RasterizerDesc.CullMode = CULL_MODE_BACK;
848+
GraphicsPipeline.RasterizerDesc.FillMode = FILL_MODE_SOLID;
849+
GraphicsPipeline.RasterizerDesc.FrontCounterClockwise = False;
850+
851+
GraphicsPipeline.DepthStencilDesc.DepthEnable = False;
852+
853+
ShaderCreateInfo ShaderCI;
854+
ShaderCI.SourceLanguage = SHADER_SOURCE_LANGUAGE_MSL;
855+
856+
RefCntAutoPtr<IShader> pAS;
857+
{
858+
ShaderCI.Desc = {"MSL amplification shader test - AS", SHADER_TYPE_AMPLIFICATION, true};
859+
ShaderCI.EntryPoint = "OBJmain";
860+
ShaderCI.Source = MSL::AmplificationShaderTest.c_str();
861+
862+
pDevice->CreateShader(ShaderCI, &pAS);
863+
ASSERT_NE(pAS, nullptr);
864+
}
865+
866+
RefCntAutoPtr<IShader> pMS;
867+
{
868+
ShaderCI.Desc = {"MSL amplification shader test - MS", SHADER_TYPE_MESH, true};
869+
ShaderCI.EntryPoint = "AmpMSmain";
870+
ShaderCI.Source = MSL::AmplificationShaderTest.c_str();
871+
872+
pDevice->CreateShader(ShaderCI, &pMS);
873+
ASSERT_NE(pMS, nullptr);
874+
}
875+
876+
RefCntAutoPtr<IShader> pPS;
877+
{
878+
ShaderCI.Desc = {"MSL amplification shader test - PS", SHADER_TYPE_PIXEL, true};
879+
ShaderCI.EntryPoint = "AmpPSmain";
880+
ShaderCI.Source = MSL::AmplificationShaderTest.c_str();
881+
882+
pDevice->CreateShader(ShaderCI, &pPS);
883+
ASSERT_NE(pPS, nullptr);
884+
}
885+
886+
PSOCreateInfo.pAS = pAS;
887+
PSOCreateInfo.pMS = pMS;
888+
PSOCreateInfo.pPS = pPS;
889+
RefCntAutoPtr<IPipelineState> pPSO;
890+
pDevice->CreateGraphicsPipelineState(PSOCreateInfo, &pPSO);
891+
ASSERT_NE(pPSO, nullptr);
892+
893+
pContext->SetPipelineState(pPSO);
894+
895+
DrawMeshAttribsMtl mtlDrawAttrs{8, 1, 1, 1, 1, 1};
896+
DrawMeshAttribs drawAttrs(8, DRAW_FLAG_VERIFY_ALL, &mtlDrawAttrs);
897+
pContext->DrawMesh(drawAttrs);
898+
899+
pSwapChain->Present();
900+
}
901+
579902
} // namespace

0 commit comments

Comments
 (0)