From 9d8be4770646ee924dd725eb9c6bad5be5bec237 Mon Sep 17 00:00:00 2001 From: assiduous Date: Sun, 1 Nov 2020 15:30:06 -0800 Subject: Fixed engine factories in C API --- Graphics/GraphicsEngineD3D11/interface/EngineFactoryD3D11.h | 2 +- Graphics/GraphicsEngineD3D12/interface/EngineFactoryD3D12.h | 2 +- Graphics/GraphicsEngineD3D12/src/EngineFactoryD3D12.cpp | 8 ++++++++ Graphics/GraphicsEngineOpenGL/interface/EngineFactoryOpenGL.h | 2 +- Graphics/GraphicsEngineOpenGL/src/EngineFactoryOpenGL.cpp | 9 +++++++++ Graphics/GraphicsEngineVulkan/interface/EngineFactoryVk.h | 2 +- Graphics/GraphicsEngineVulkan/src/EngineFactoryVk.cpp | 9 +++++++++ Tests/IncludeTest/GraphicsEngineD3D11/EngineFactoryD3D11H_test.c | 2 +- Tests/IncludeTest/GraphicsEngineD3D12/EngineFactoryD3D12H_test.c | 2 +- .../IncludeTest/GraphicsEngineOpenGL/EngineFactoryOpenGLH_test.c | 2 +- Tests/IncludeTest/GraphicsEngineVk/EngineFactoryVkH_test.c | 2 +- 11 files changed, 34 insertions(+), 8 deletions(-) diff --git a/Graphics/GraphicsEngineD3D11/interface/EngineFactoryD3D11.h b/Graphics/GraphicsEngineD3D11/interface/EngineFactoryD3D11.h index f58dbd27..f5ccb59f 100644 --- a/Graphics/GraphicsEngineD3D11/interface/EngineFactoryD3D11.h +++ b/Graphics/GraphicsEngineD3D11/interface/EngineFactoryD3D11.h @@ -176,7 +176,7 @@ DILIGENT_END_INTERFACE typedef struct IEngineFactoryD3D11* (*GetEngineFactoryD3D11Type)(); -inline GetEngineFactoryD3D11Type LoadGraphicsEngineD3D11() +inline GetEngineFactoryD3D11Type DILIGENT_GLOBAL_FUNCTION(LoadGraphicsEngineD3D11)() { return (GetEngineFactoryD3D11Type)LoadEngineDll("GraphicsEngineD3D11", "GetEngineFactoryD3D11"); } diff --git a/Graphics/GraphicsEngineD3D12/interface/EngineFactoryD3D12.h b/Graphics/GraphicsEngineD3D12/interface/EngineFactoryD3D12.h index b90f3aa0..cc2ca2ee 100644 --- a/Graphics/GraphicsEngineD3D12/interface/EngineFactoryD3D12.h +++ b/Graphics/GraphicsEngineD3D12/interface/EngineFactoryD3D12.h @@ -198,7 +198,7 @@ DILIGENT_END_INTERFACE typedef struct IEngineFactoryD3D12* (*GetEngineFactoryD3D12Type)(); -inline GetEngineFactoryD3D12Type LoadGraphicsEngineD3D12() +inline GetEngineFactoryD3D12Type DILIGENT_GLOBAL_FUNCTION(LoadGraphicsEngineD3D12)() { return (GetEngineFactoryD3D12Type)LoadEngineDll("GraphicsEngineD3D12", "GetEngineFactoryD3D12"); } diff --git a/Graphics/GraphicsEngineD3D12/src/EngineFactoryD3D12.cpp b/Graphics/GraphicsEngineD3D12/src/EngineFactoryD3D12.cpp index d00d72d5..940249cd 100644 --- a/Graphics/GraphicsEngineD3D12/src/EngineFactoryD3D12.cpp +++ b/Graphics/GraphicsEngineD3D12/src/EngineFactoryD3D12.cpp @@ -547,3 +547,11 @@ IEngineFactoryD3D12* GetEngineFactoryD3D12() } } // namespace Diligent + +extern "C" +{ + Diligent::IEngineFactoryD3D12* Diligent_GetEngineFactoryD3D12() + { + return Diligent::GetEngineFactoryD3D12(); + } +} diff --git a/Graphics/GraphicsEngineOpenGL/interface/EngineFactoryOpenGL.h b/Graphics/GraphicsEngineOpenGL/interface/EngineFactoryOpenGL.h index cff83e54..6f005bfa 100644 --- a/Graphics/GraphicsEngineOpenGL/interface/EngineFactoryOpenGL.h +++ b/Graphics/GraphicsEngineOpenGL/interface/EngineFactoryOpenGL.h @@ -105,7 +105,7 @@ DILIGENT_END_INTERFACE typedef struct IEngineFactoryOpenGL* (*GetEngineFactoryOpenGLType)(); -inline GetEngineFactoryOpenGLType LoadGraphicsEngineOpenGL() +inline GetEngineFactoryOpenGLType DILIGENT_GLOBAL_FUNCTION(LoadGraphicsEngineOpenGL)() { return (GetEngineFactoryOpenGLType)LoadEngineDll("GraphicsEngineOpenGL", "GetEngineFactoryOpenGL"); } diff --git a/Graphics/GraphicsEngineOpenGL/src/EngineFactoryOpenGL.cpp b/Graphics/GraphicsEngineOpenGL/src/EngineFactoryOpenGL.cpp index dacd1fe3..118a4bcf 100644 --- a/Graphics/GraphicsEngineOpenGL/src/EngineFactoryOpenGL.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/EngineFactoryOpenGL.cpp @@ -302,3 +302,12 @@ Diligent::IEngineFactoryOpenGL* GetEngineFactoryOpenGL() } } // namespace Diligent + +extern "C" +{ + API_QUALIFIER + Diligent::IEngineFactoryOpenGL* Diligent_GetEngineFactoryOpenGL() + { + return Diligent::GetEngineFactoryOpenGL(); + } +} diff --git a/Graphics/GraphicsEngineVulkan/interface/EngineFactoryVk.h b/Graphics/GraphicsEngineVulkan/interface/EngineFactoryVk.h index be79c99f..0c5e347a 100644 --- a/Graphics/GraphicsEngineVulkan/interface/EngineFactoryVk.h +++ b/Graphics/GraphicsEngineVulkan/interface/EngineFactoryVk.h @@ -117,7 +117,7 @@ DILIGENT_END_INTERFACE typedef struct IEngineFactoryVk* (*GetEngineFactoryVkType)(); -inline GetEngineFactoryVkType LoadGraphicsEngineVk() +inline GetEngineFactoryVkType DILIGENT_GLOBAL_FUNCTION(LoadGraphicsEngineVk)() { return (GetEngineFactoryVkType)LoadEngineDll("GraphicsEngineVk", "GetEngineFactoryVk"); } diff --git a/Graphics/GraphicsEngineVulkan/src/EngineFactoryVk.cpp b/Graphics/GraphicsEngineVulkan/src/EngineFactoryVk.cpp index cbe1779f..fbbd9492 100644 --- a/Graphics/GraphicsEngineVulkan/src/EngineFactoryVk.cpp +++ b/Graphics/GraphicsEngineVulkan/src/EngineFactoryVk.cpp @@ -580,3 +580,12 @@ IEngineFactoryVk* GetEngineFactoryVk() } } // namespace Diligent + +extern "C" +{ + API_QUALIFIER + Diligent::IEngineFactoryVk* Diligent_GetEngineFactoryVk() + { + return Diligent::GetEngineFactoryVk(); + } +} diff --git a/Tests/IncludeTest/GraphicsEngineD3D11/EngineFactoryD3D11H_test.c b/Tests/IncludeTest/GraphicsEngineD3D11/EngineFactoryD3D11H_test.c index 12c58064..5cbab2e5 100644 --- a/Tests/IncludeTest/GraphicsEngineD3D11/EngineFactoryD3D11H_test.c +++ b/Tests/IncludeTest/GraphicsEngineD3D11/EngineFactoryD3D11H_test.c @@ -39,7 +39,7 @@ void TestEngineFactoryD3D11CInterface() { - GetEngineFactoryD3D11Type GetEngineFactoryD3D11 = LoadGraphicsEngineD3D11(); + GetEngineFactoryD3D11Type GetEngineFactoryD3D11 = Diligent_LoadGraphicsEngineD3D11(); struct IEngineFactoryD3D11* pFactory = GetEngineFactoryD3D11(); struct EngineD3D11CreateInfo EngineCI = {0}; IRenderDevice* pDevice = NULL; diff --git a/Tests/IncludeTest/GraphicsEngineD3D12/EngineFactoryD3D12H_test.c b/Tests/IncludeTest/GraphicsEngineD3D12/EngineFactoryD3D12H_test.c index 6d986fec..01bea75d 100644 --- a/Tests/IncludeTest/GraphicsEngineD3D12/EngineFactoryD3D12H_test.c +++ b/Tests/IncludeTest/GraphicsEngineD3D12/EngineFactoryD3D12H_test.c @@ -39,7 +39,7 @@ void TestEngineFactoryD3D12CInterface() { - GetEngineFactoryD3D12Type GetEngineFactoryD3D12 = LoadGraphicsEngineD3D12(); + GetEngineFactoryD3D12Type GetEngineFactoryD3D12 = Diligent_LoadGraphicsEngineD3D12(); struct IEngineFactoryD3D12* pFactory = GetEngineFactoryD3D12(); struct EngineD3D12CreateInfo EngineCI = {0}; IRenderDevice* pDevice = NULL; diff --git a/Tests/IncludeTest/GraphicsEngineOpenGL/EngineFactoryOpenGLH_test.c b/Tests/IncludeTest/GraphicsEngineOpenGL/EngineFactoryOpenGLH_test.c index 06758d24..89c5d847 100644 --- a/Tests/IncludeTest/GraphicsEngineOpenGL/EngineFactoryOpenGLH_test.c +++ b/Tests/IncludeTest/GraphicsEngineOpenGL/EngineFactoryOpenGLH_test.c @@ -33,7 +33,7 @@ void TestEngineFactoryGL_CInterface() { #if EXPLICITLY_LOAD_ENGINE_GL_DLL - GetEngineFactoryOpenGLType GetEngineFactoryGL = LoadGraphicsEngineOpenGL(); + GetEngineFactoryOpenGLType GetEngineFactoryGL = Diligent_LoadGraphicsEngineOpenGL(); IEngineFactoryOpenGL* pFactory = GetEngineFactoryGL(); #else IEngineFactoryOpenGL* pFactory = Diligent_GetEngineFactoryOpenGL(); diff --git a/Tests/IncludeTest/GraphicsEngineVk/EngineFactoryVkH_test.c b/Tests/IncludeTest/GraphicsEngineVk/EngineFactoryVkH_test.c index fa888524..72ec2186 100644 --- a/Tests/IncludeTest/GraphicsEngineVk/EngineFactoryVkH_test.c +++ b/Tests/IncludeTest/GraphicsEngineVk/EngineFactoryVkH_test.c @@ -36,7 +36,7 @@ void TestEngineFactoryVk_CInterface() { #if EXPLICITLY_LOAD_ENGINE_VK_DLL - GetEngineFactoryVkType GetEngineFactoryVk = LoadGraphicsEngineVk(); + GetEngineFactoryVkType GetEngineFactoryVk = Diligent_LoadGraphicsEngineVk(); IEngineFactoryVk* pFactory = GetEngineFactoryVk(); #else IEngineFactoryVk* pFactory = Diligent_GetEngineFactoryVk(); -- cgit v1.2.3 From 203fe80f8807d8e90b3ff17b8e88e23f1a882dee Mon Sep 17 00:00:00 2001 From: assiduous Date: Mon, 2 Nov 2020 11:31:52 -0800 Subject: Added CreateRenderPass and CreateFramebuffer C functions --- Graphics/GraphicsEngine/interface/RenderDevice.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Graphics/GraphicsEngine/interface/RenderDevice.h b/Graphics/GraphicsEngine/interface/RenderDevice.h index 252daf1a..47c759db 100644 --- a/Graphics/GraphicsEngine/interface/RenderDevice.h +++ b/Graphics/GraphicsEngine/interface/RenderDevice.h @@ -292,6 +292,8 @@ DILIGENT_END_INTERFACE # define IRenderDevice_CreateComputePipelineState(This, ...) CALL_IFACE_METHOD(RenderDevice, CreateComputePipelineState, This, __VA_ARGS__) # define IRenderDevice_CreateFence(This, ...) CALL_IFACE_METHOD(RenderDevice, CreateFence, This, __VA_ARGS__) # define IRenderDevice_CreateQuery(This, ...) CALL_IFACE_METHOD(RenderDevice, CreateQuery, This, __VA_ARGS__) +# define IRenderDevice_CreateRenderPass(This, ...) CALL_IFACE_METHOD(RenderDevice, CreateRenderPass, This, __VA_ARGS__) +# define IRenderDevice_CreateFramebuffer(This, ...) CALL_IFACE_METHOD(RenderDevice, CreateFramebuffer, This, __VA_ARGS__) # define IRenderDevice_GetDeviceCaps(This) CALL_IFACE_METHOD(RenderDevice, GetDeviceCaps, This) # define IRenderDevice_GetTextureFormatInfo(This, ...) CALL_IFACE_METHOD(RenderDevice, GetTextureFormatInfo, This, __VA_ARGS__) # define IRenderDevice_GetTextureFormatInfoExt(This, ...) CALL_IFACE_METHOD(RenderDevice, GetTextureFormatInfoExt, This, __VA_ARGS__) -- cgit v1.2.3 From 6902c978ce01d2e34941782501a76c50c1ec0ca2 Mon Sep 17 00:00:00 2001 From: assiduous Date: Sun, 8 Nov 2020 10:35:02 -0800 Subject: Broken shader test: added broken GLSL --- Tests/DiligentCoreAPITest/src/BrokenShaderTest.cpp | 39 ++++++++++++++++++---- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/Tests/DiligentCoreAPITest/src/BrokenShaderTest.cpp b/Tests/DiligentCoreAPITest/src/BrokenShaderTest.cpp index 542ee119..2320f1c3 100644 --- a/Tests/DiligentCoreAPITest/src/BrokenShaderTest.cpp +++ b/Tests/DiligentCoreAPITest/src/BrokenShaderTest.cpp @@ -35,14 +35,21 @@ using namespace Diligent::Testing; namespace { -static const char g_BrokenShaderSource[] = R"( +static const char g_BrokenHLSL[] = R"( void VSMain(out float4 pos : SV_POSITION) { pos = float3(0.0, 0.0, 0.0, 0.0); } )"; -TEST(Shader, CompilationFailure) +static const char g_BrokenGLSL[] = R"( +void VSMain() +{ + gl_Position = vec3(0.0, 0.0, 0.0); +} +)"; + +void TestBrokenShader(const char* Source, const char* Name, SHADER_SOURCE_LANGUAGE SourceLanguage, int ErrorAllowance) { auto* pEnv = TestingEnvironment::GetInstance(); auto* pDevice = pEnv->GetDevice(); @@ -50,18 +57,18 @@ TEST(Shader, CompilationFailure) TestingEnvironment::ScopedReset EnvironmentAutoReset; ShaderCreateInfo Attrs; - Attrs.Source = g_BrokenShaderSource; + Attrs.Source = Source; Attrs.EntryPoint = "VSMain"; Attrs.Desc.ShaderType = SHADER_TYPE_VERTEX; - Attrs.Desc.Name = "Broken shader test"; - Attrs.SourceLanguage = SHADER_SOURCE_LANGUAGE_HLSL; + Attrs.Desc.Name = Name; + Attrs.SourceLanguage = SourceLanguage; Attrs.ShaderCompiler = pEnv->GetDefaultCompiler(Attrs.SourceLanguage); Attrs.UseCombinedTextureSamplers = true; IDataBlob* pErrors = nullptr; Attrs.ppCompilerOutput = &pErrors; - pEnv->SetErrorAllowance(pDevice->GetDeviceCaps().IsVulkanDevice() ? 3 : 2, "\n\nNo worries, testing broken shader...\n\n"); + pEnv->SetErrorAllowance(ErrorAllowance, "\n\nNo worries, testing broken shader...\n\n"); RefCntAutoPtr pBrokenShader; pDevice->CreateShader(Attrs, &pBrokenShader); EXPECT_FALSE(pBrokenShader); @@ -71,4 +78,24 @@ TEST(Shader, CompilationFailure) pErrors->Release(); } +TEST(Shader, BrokenHLSL) +{ + const auto& deviceCaps = TestingEnvironment::GetInstance()->GetDevice()->GetDeviceCaps(); + // HLSL is supported in all backends + TestBrokenShader(g_BrokenHLSL, "Broken HLSL test", SHADER_SOURCE_LANGUAGE_HLSL, + deviceCaps.IsGLDevice() || deviceCaps.IsD3DDevice() ? 2 : 3); +} + +TEST(Shader, BrokenGLSL) +{ + const auto& deviceCaps = TestingEnvironment::GetInstance()->GetDevice()->GetDeviceCaps(); + if (deviceCaps.IsD3DDevice()) + { + GTEST_SKIP() << "GLSL is not supported in Direct3D"; + } + + TestBrokenShader(g_BrokenGLSL, "Broken GLSL test", SHADER_SOURCE_LANGUAGE_GLSL, + deviceCaps.IsGLDevice() ? 2 : 3); +} + } // namespace -- cgit v1.2.3 From 37a3d0771d015bfc64bd9bd6ef87e294cf5a3a59 Mon Sep 17 00:00:00 2001 From: assiduous Date: Sun, 8 Nov 2020 12:19:20 -0800 Subject: Broken shader test: added broken MSL; added SHADER_SOURCE_LANGUAGE_MSL --- Graphics/GraphicsEngine/interface/Shader.h | 3 +++ Tests/DiligentCoreAPITest/src/BrokenShaderTest.cpp | 30 ++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/Graphics/GraphicsEngine/interface/Shader.h b/Graphics/GraphicsEngine/interface/Shader.h index dce77a82..bb4acc84 100644 --- a/Graphics/GraphicsEngine/interface/Shader.h +++ b/Graphics/GraphicsEngine/interface/Shader.h @@ -71,6 +71,9 @@ DILIGENT_TYPED_ENUM(SHADER_SOURCE_LANGUAGE, Uint32) /// The source language is GLSL SHADER_SOURCE_LANGUAGE_GLSL, + /// The source language is Metal shading language (MSL) + SHADER_SOURCE_LANGUAGE_MSL, + /// The source language is GLSL that should be compiled verbatim /// By default the engine prepends GLSL shader source code with platform-specific diff --git a/Tests/DiligentCoreAPITest/src/BrokenShaderTest.cpp b/Tests/DiligentCoreAPITest/src/BrokenShaderTest.cpp index 2320f1c3..d69eb9c2 100644 --- a/Tests/DiligentCoreAPITest/src/BrokenShaderTest.cpp +++ b/Tests/DiligentCoreAPITest/src/BrokenShaderTest.cpp @@ -49,6 +49,25 @@ void VSMain() } )"; +static const char g_BrokenMSL[] = R"( +#include +#include + +using namespace metal; + +struct VSOut +{ + float4 pos [[position]]; +}; + +vertex VSOut vs_main() +{ + VSOut out = {}; + out.pos = float3(0.0, 0.0, 0.0); + return out; +} +)"; + void TestBrokenShader(const char* Source, const char* Name, SHADER_SOURCE_LANGUAGE SourceLanguage, int ErrorAllowance) { auto* pEnv = TestingEnvironment::GetInstance(); @@ -98,4 +117,15 @@ TEST(Shader, BrokenGLSL) deviceCaps.IsGLDevice() ? 2 : 3); } +TEST(Shader, BrokenMSL) +{ + const auto& deviceCaps = TestingEnvironment::GetInstance()->GetDevice()->GetDeviceCaps(); + if (!deviceCaps.IsMetalDevice()) + { + GTEST_SKIP() << "MSL is only supported in Metal"; + } + + TestBrokenShader(g_BrokenMSL, "Broken MSL test", SHADER_SOURCE_LANGUAGE_MSL, 2); +} + } // namespace -- cgit v1.2.3 From 91ed990b08858003abb76718502f78b71061cfb0 Mon Sep 17 00:00:00 2001 From: assiduous Date: Sun, 8 Nov 2020 19:49:14 -0800 Subject: Clear render target test: added Metal reference implementation --- .../src/ClearRenderTargetTest.cpp | 7 ++++++- .../src/Metal/ClearRenderTargetReferenceMtl.mm | 24 ++++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Tests/DiligentCoreAPITest/src/ClearRenderTargetTest.cpp b/Tests/DiligentCoreAPITest/src/ClearRenderTargetTest.cpp index 51f50492..676b4140 100644 --- a/Tests/DiligentCoreAPITest/src/ClearRenderTargetTest.cpp +++ b/Tests/DiligentCoreAPITest/src/ClearRenderTargetTest.cpp @@ -58,7 +58,7 @@ void ClearRenderTargetReferenceVk(ISwapChain* pSwapChain, const float ClearColor #endif #if METAL_SUPPORTED - +void ClearRenderTargetReferenceMtl(ISwapChain* pSwapChain, const float ClearColor[]); #endif } // namespace Testing @@ -102,7 +102,12 @@ void ClearRenderTargetReference(IRenderDevice* pDevice, case RENDER_DEVICE_TYPE_VULKAN: ClearRenderTargetReferenceVk(pSwapChain, ClearColor); break; +#endif +#if METAL_SUPPORTED + case RENDER_DEVICE_TYPE_METAL: + ClearRenderTargetReferenceMtl(pSwapChain, ClearColor); + break; #endif default: diff --git a/Tests/DiligentCoreAPITest/src/Metal/ClearRenderTargetReferenceMtl.mm b/Tests/DiligentCoreAPITest/src/Metal/ClearRenderTargetReferenceMtl.mm index 5c140e00..977b039d 100644 --- a/Tests/DiligentCoreAPITest/src/Metal/ClearRenderTargetReferenceMtl.mm +++ b/Tests/DiligentCoreAPITest/src/Metal/ClearRenderTargetReferenceMtl.mm @@ -29,6 +29,7 @@ #include "Metal/TestingSwapChainMtl.hpp" #include "DeviceContextMtl.h" +#include "TextureViewMtl.h" namespace Diligent { @@ -38,8 +39,27 @@ namespace Testing void ClearRenderTargetReferenceMtl(ISwapChain* pSwapChain, const float ClearColor[]) { - //auto* pEnv = TestingEnvironmentMtl::GetInstance(); - //auto* pContext = pEnv->GetDeviceContext(); + auto* const pEnv = TestingEnvironmentMtl::GetInstance(); + auto const mtlCommandQueue = pEnv->GetMtlCommandQueue(); + + auto* pTestingSwapChainMtl = ValidatedCast(pSwapChain); + + auto* pRTV = pTestingSwapChainMtl->GetCurrentBackBufferRTV(); + auto* mtlTexture = ValidatedCast(pRTV)->GetMtlTexture(); + + id mtlCommandBuffer = [mtlCommandQueue commandBuffer]; + + MTLRenderPassDescriptor* renderPassDesc = + [MTLRenderPassDescriptor renderPassDescriptor]; + renderPassDesc.colorAttachments[0].texture = mtlTexture; + renderPassDesc.colorAttachments[0].loadAction = MTLLoadActionClear; + renderPassDesc.colorAttachments[0].clearColor = + MTLClearColorMake(ClearColor[0], ClearColor[1], ClearColor[2], ClearColor[3]); + renderPassDesc.colorAttachments[0].storeAction = MTLStoreActionStore; + id renderEncoder = + [mtlCommandBuffer renderCommandEncoderWithDescriptor:renderPassDesc]; + [renderEncoder endEncoding]; + [mtlCommandBuffer commit]; } } // namespace Testing -- cgit v1.2.3 From fdc1dffa075b302bb43c2ed72bf13b6036de4f04 Mon Sep 17 00:00:00 2001 From: assiduous Date: Sun, 8 Nov 2020 22:04:13 -0800 Subject: Implemented compute shader test Metal reference --- .../include/InlineShaders/ComputeShaderTestMSL.h | 61 ++++++++++++++++++++++ .../src/Metal/ComputeShaderReferenceMtl.mm | 37 +++++++++++-- 2 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 Tests/DiligentCoreAPITest/include/InlineShaders/ComputeShaderTestMSL.h diff --git a/Tests/DiligentCoreAPITest/include/InlineShaders/ComputeShaderTestMSL.h b/Tests/DiligentCoreAPITest/include/InlineShaders/ComputeShaderTestMSL.h new file mode 100644 index 00000000..f2751400 --- /dev/null +++ b/Tests/DiligentCoreAPITest/include/InlineShaders/ComputeShaderTestMSL.h @@ -0,0 +1,61 @@ +/* + * Copyright 2019-2020 Diligent Graphics LLC + * Copyright 2015-2019 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#include + +namespace +{ + +namespace MSL +{ + +// clang-format off +const std::string FillTextureCS{ +R"( +#include +#include + +using namespace metal; + +kernel void CSMain(texture2d g_tex2DUAV [[texture(0)]], + uint3 gl_GlobalInvocationID [[thread_position_in_grid]]) +{ + if (gl_GlobalInvocationID.x < g_tex2DUAV.get_width() && + gl_GlobalInvocationID.y < g_tex2DUAV.get_height()) + { + g_tex2DUAV.write(float4(float2(gl_GlobalInvocationID.xy % uint2(256u)) / 256.0, 0.0, 1.0), + uint2(gl_GlobalInvocationID.xy)); + + } +} +)" +}; +// clang-format on + +} // namespace MSL + +} // namespace diff --git a/Tests/DiligentCoreAPITest/src/Metal/ComputeShaderReferenceMtl.mm b/Tests/DiligentCoreAPITest/src/Metal/ComputeShaderReferenceMtl.mm index 6341ecdb..489f36f2 100644 --- a/Tests/DiligentCoreAPITest/src/Metal/ComputeShaderReferenceMtl.mm +++ b/Tests/DiligentCoreAPITest/src/Metal/ComputeShaderReferenceMtl.mm @@ -29,8 +29,9 @@ #include "Metal/TestingSwapChainMtl.hpp" #include "DeviceContextMtl.h" +#include "TextureViewMtl.h" -//#include "InlineShaders/ComputeShaderTestGLSL.h" +#include "InlineShaders/ComputeShaderTestMSL.h" namespace Diligent { @@ -40,9 +41,37 @@ namespace Testing void ComputeShaderReferenceMtl(ISwapChain* pSwapChain) { - //auto* pEnv = TestingEnvironmentMtl::GetInstance(); - //auto vkDevice = pEnv->GetVkDevice(); - //auto* pContext = pEnv->GetDeviceContext(); + auto* const pEnv = TestingEnvironmentMtl::GetInstance(); + auto const mtlDevice = pEnv->GetMtlDevice(); + + auto* progSrc = [NSString stringWithUTF8String:MSL::FillTextureCS.c_str()]; + NSError *errors = nil; + id library = [mtlDevice newLibraryWithSource:progSrc + options:nil + error:&errors]; + ASSERT_TRUE(library != nil); + id computeFunc = [library newFunctionWithName:@"CSMain"]; + ASSERT_TRUE(computeFunc != nil); + auto* computePipeline = [mtlDevice newComputePipelineStateWithFunction:computeFunc error:&errors]; + ASSERT_TRUE(computePipeline != nil); + + auto* pTestingSwapChainMtl = ValidatedCast(pSwapChain); + auto* pUAV = pTestingSwapChainMtl->GetCurrentBackBufferUAV(); + auto* mtlTexture = ValidatedCast(pUAV)->GetMtlTexture(); + const auto& SCDesc = pTestingSwapChainMtl->GetDesc(); + + auto* mtlCommandQueue = pEnv->GetMtlCommandQueue(); + id mtlCommandBuffer = [mtlCommandQueue commandBuffer]; + auto* cmdEncoder = [mtlCommandBuffer computeCommandEncoder]; + ASSERT_TRUE(cmdEncoder != nil); + + [cmdEncoder setComputePipelineState:computePipeline]; + [cmdEncoder setTexture:mtlTexture atIndex:0]; + [cmdEncoder dispatchThreadgroups:MTLSizeMake((SCDesc.Width + 15) / 16, (SCDesc.Height + 15) / 16, 1) + threadsPerThreadgroup:MTLSizeMake(16, 16, 1)]; + + [cmdEncoder endEncoding]; + [mtlCommandBuffer commit]; } } // namespace Testing -- cgit v1.2.3 From b12f2af56bce8be66e686cdc1e9180b2fd107823 Mon Sep 17 00:00:00 2001 From: assiduous Date: Mon, 9 Nov 2020 09:30:48 -0800 Subject: Added draw command test Metal reference --- .../include/InlineShaders/DrawCommandTestMSL.h | 97 +++++++++++++++++ .../include/Metal/TestingSwapChainMtl.hpp | 3 + Tests/DiligentCoreAPITest/src/DrawCommandTest.cpp | 12 ++- .../src/Metal/DrawCommandReferenceMtl.mm | 118 +++++++++++---------- .../src/Metal/TestingSwapChainMtl.mm | 32 ++++++ 5 files changed, 204 insertions(+), 58 deletions(-) create mode 100644 Tests/DiligentCoreAPITest/include/InlineShaders/DrawCommandTestMSL.h diff --git a/Tests/DiligentCoreAPITest/include/InlineShaders/DrawCommandTestMSL.h b/Tests/DiligentCoreAPITest/include/InlineShaders/DrawCommandTestMSL.h new file mode 100644 index 00000000..581bb961 --- /dev/null +++ b/Tests/DiligentCoreAPITest/include/InlineShaders/DrawCommandTestMSL.h @@ -0,0 +1,97 @@ +/* + * Copyright 2019-2020 Diligent Graphics LLC + * Copyright 2015-2019 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#include + +namespace +{ + +namespace MSL +{ + +// clang-format off + +const std::string DrawTestFunctions{ +R"( + +#include +#include + +using namespace metal; + +struct VSOut +{ + float3 Color [[user(locn0)]]; + float4 Position [[position]]; +}; + +vertex VSOut QuadVS(uint VertexId [[vertex_id]]) +{ + float4 Pos[6] = + { + float4(-1.0, -0.5, 0.0, 1.0), + float4(-0.5, 0.5, 0.0, 1.0), + float4(0.0, -0.5, 0.0, 1.0), + float4(0.0, -0.5, 0.0, 1.0), + float4(0.5, 0.5, 0.0, 1.0), + float4(1.0, -0.5, 0.0, 1.0) + }; + float3 Col[6] = + { + float3(1.0, 0.0, 0.0), + float3(0.0, 1.0, 0.0), + float3(0.0, 0.0, 1.0), + float3(1.0, 0.0, 0.0), + float3(0.0, 1.0, 0.0), + float3(0.0, 0.0, 1.0) + }; + + VSOut out = {}; + out.Position = Pos[VertexId]; + out.Color = Col[VertexId]; + return out; +} + +struct PSOut +{ + float4 Color [[color(0)]]; +}; + +fragment PSOut QuadPS(VSOut in [[stage_in]]) +{ + PSOut out = {float4(in.Color.rgb, 1.0)}; + return out; +} + +)" +}; + +// clang-format on + +} // namespace MSL + +} // namespace diff --git a/Tests/DiligentCoreAPITest/include/Metal/TestingSwapChainMtl.hpp b/Tests/DiligentCoreAPITest/include/Metal/TestingSwapChainMtl.hpp index 6b957789..aac8083e 100644 --- a/Tests/DiligentCoreAPITest/include/Metal/TestingSwapChainMtl.hpp +++ b/Tests/DiligentCoreAPITest/include/Metal/TestingSwapChainMtl.hpp @@ -25,6 +25,8 @@ * of the possibility of such damages. */ +#include + #include "TestingSwapChainBase.hpp" namespace Diligent @@ -47,6 +49,7 @@ public: virtual void TakeSnapshot() override final; private: + id m_MtlStagingBuffer; }; } // namespace Testing diff --git a/Tests/DiligentCoreAPITest/src/DrawCommandTest.cpp b/Tests/DiligentCoreAPITest/src/DrawCommandTest.cpp index c42f6e81..d229c606 100644 --- a/Tests/DiligentCoreAPITest/src/DrawCommandTest.cpp +++ b/Tests/DiligentCoreAPITest/src/DrawCommandTest.cpp @@ -287,13 +287,15 @@ protected: ASSERT_NE(pPS, nullptr); } - PSODesc.Name = "Draw command test"; + PSODesc.Name = "Draw command test - procedural tris"; PSOCreateInfo.pVS = pProceduralVS; PSOCreateInfo.pPS = pPS; pDevice->CreateGraphicsPipelineState(PSOCreateInfo, &sm_pDrawProceduralPSO); ASSERT_NE(sm_pDrawProceduralPSO, nullptr); + PSODesc.Name = "Draw command test"; + InputLayoutDesc LayoutDesc; // clang-format off LayoutElement Elems[] = @@ -302,6 +304,7 @@ protected: LayoutElement{ 1, 0, 3, VT_FLOAT32} }; // clang-format on + GraphicsPipeline.InputLayout.LayoutElements = Elems; GraphicsPipeline.InputLayout.NumElements = _countof(Elems); @@ -309,13 +312,14 @@ protected: PSOCreateInfo.pPS = pPS; GraphicsPipeline.PrimitiveTopology = PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; pDevice->CreateGraphicsPipelineState(PSOCreateInfo, &sm_pDrawPSO); + ASSERT_NE(sm_pDrawPSO, nullptr); - PSODesc.Name = "Draw command test - 2x stride"; + PSODesc.Name = "Draw command test - 2x VB stride"; Elems[0].Stride = sizeof(Vertex) * 2; pDevice->CreateGraphicsPipelineState(PSOCreateInfo, &sm_pDraw_2xStride_PSO); - + ASSERT_NE(sm_pDraw_2xStride_PSO, nullptr); PSODesc.Name = "Instanced draw command test"; // clang-format off @@ -326,11 +330,13 @@ protected: LayoutElement{ 2, 1, 4, VT_FLOAT32, false, INPUT_ELEMENT_FREQUENCY_PER_INSTANCE} }; // clang-format on + GraphicsPipeline.InputLayout.LayoutElements = InstancedElems; GraphicsPipeline.InputLayout.NumElements = _countof(InstancedElems); PSOCreateInfo.pVS = pInstancedVS; pDevice->CreateGraphicsPipelineState(PSOCreateInfo, &sm_pDrawInstancedPSO); + ASSERT_NE(sm_pDrawInstancedPSO, nullptr); } static void TearDownTestSuite() diff --git a/Tests/DiligentCoreAPITest/src/Metal/DrawCommandReferenceMtl.mm b/Tests/DiligentCoreAPITest/src/Metal/DrawCommandReferenceMtl.mm index 763e90f0..cf989c7b 100644 --- a/Tests/DiligentCoreAPITest/src/Metal/DrawCommandReferenceMtl.mm +++ b/Tests/DiligentCoreAPITest/src/Metal/DrawCommandReferenceMtl.mm @@ -25,10 +25,14 @@ * of the possibility of such damages. */ +#include + #include "Metal/TestingEnvironmentMtl.hpp" #include "Metal/TestingSwapChainMtl.hpp" -//#include "InlineShaders/DrawCommandTestMSL.h" +#include "TextureViewMtl.h" + +#include "InlineShaders/DrawCommandTestMSL.h" namespace Diligent { @@ -43,84 +47,88 @@ class TriangleRenderer { public: - TriangleRenderer(const std::string& FSSource) + TriangleRenderer(const std::string& ProgramSource) { - auto* pEnv = TestingEnvironmentMtl::GetInstance(); + auto* const pEnv = TestingEnvironmentMtl::GetInstance(); + auto const mtlDevice = pEnv->GetMtlDevice(); + + auto* progSrc = [NSString stringWithUTF8String:MSL::DrawTestFunctions.c_str()]; + NSError *errors = nil; + id library = [mtlDevice newLibraryWithSource:progSrc + options:nil + error:&errors]; + id vertFunc = [library newFunctionWithName:@"QuadVS"]; + id fragFunc = [library newFunctionWithName:@"QuadPS"]; + MTLRenderPipelineDescriptor* renderPipelineDesc = + [[MTLRenderPipelineDescriptor alloc] init]; + renderPipelineDesc.vertexFunction = vertFunc; + renderPipelineDesc.fragmentFunction = fragFunc; + const auto& SCDesc = pEnv->GetSwapChain()->GetDesc(); + MTLPixelFormat pixelFormat = MTLPixelFormatInvalid; + switch (SCDesc.ColorBufferFormat) + { + case TEX_FORMAT_RGBA8_UNORM: + pixelFormat = MTLPixelFormatRGBA8Unorm; + break; + + default: + UNSUPPORTED("Unexpected swap chain color format"); + } + renderPipelineDesc.colorAttachments[0].pixelFormat = pixelFormat; + m_MtlPipeline = [mtlDevice + newRenderPipelineStateWithDescriptor:renderPipelineDesc error:&errors]; } ~TriangleRenderer() { } - void Draw(Uint32 Width, Uint32 Height, const float* pClearColor) + void Draw(const float* pClearColor) { - //auto* pEnv = TestingEnvironmentGL::GetInstance(); - /* - id device = MTLCreateSystemDefaultDevice(); - - id commandQueue = [device newCommandQueue]; - id commandBuffer = [commandQueue commandBuffer]; + auto* const pEnv = TestingEnvironmentMtl::GetInstance(); + auto const mtlCommandQueue = pEnv->GetMtlCommandQueue(); + + auto* const pSwapChain = pEnv->GetSwapChain(); + auto* pTestingSwapChainMtl = ValidatedCast(pSwapChain); + const auto& SCDesc = pTestingSwapChainMtl->GetDesc(); + + auto* pRTV = pTestingSwapChainMtl->GetCurrentBackBufferRTV(); + auto* mtlTexture = ValidatedCast(pRTV)->GetMtlTexture(); + + id mtlCommandBuffer = [mtlCommandQueue commandBuffer]; - MTLRenderPassDescriptor *renderPassDesc - = [MTLRenderPassDescriptor renderPassDescriptor]; - renderPassDesc.colorAttachments[0].texture = currentTexture; - renderPassDesc.colorAttachments[0].loadAction = MTLLoadActionClear; - renderPassDesc.colorAttachments[0].clearColor = MTLClearColorMake(0.0,1.0,1.0,1.0); + MTLRenderPassDescriptor* renderPassDesc = + [MTLRenderPassDescriptor renderPassDescriptor]; + renderPassDesc.colorAttachments[0].texture = mtlTexture; + renderPassDesc.colorAttachments[0].loadAction = MTLLoadActionClear; + renderPassDesc.colorAttachments[0].clearColor = MTLClearColorMake(0.0,0.0,0.0,0.0); + renderPassDesc.colorAttachments[0].storeAction = MTLStoreActionStore; id renderEncoder = - [commandBuffer renderCommandEncoderWithDescriptor:renderPassDesc]; - - static const float posData[] = { - 0.0f, 0.33f, 0.0f, 1.f, - -0.33f, -0.33f, 0.0f, 1.f, - 0.33f, -0.33f, 0.0f, 1.f, - }; - static const float colData[] = { - 1.f, 0.f, 0.f, 1.f, - 0.f, 1.f, 0.f, 1.f, - 0.f, 0.f, 1.f, 1.f, - }; - id posBuf = [device newBufferWithBytes:posData - length:sizeof(posData) options:nil]; - id colBuf = [device newBufferWithBytes:colorData - length:sizeof(colData) options:nil]; - [renderEncoder setVertexBuffer:posBuf offset:0 atIndex:0]; - [renderEncoder setVertexBuffer:colBuf offset:0 atIndex:1]; - - NSError *errors; - id library = [device newLibraryWithSource:progSrc options:nil - error:&errors]; - id vertFunc = [library newFunctionWithName:@"hello_vertex"]; - id fragFunc = [library newFunctionWithName:@"hello_fragment"]; - MTLRenderPipelineDescriptor *renderPipelineDesc - = [[MTLRenderPipelineDescriptor alloc] init]; - renderPipelineDesc.vertexFunction = vertFunc; - renderPipelineDesc.fragmentFunction = fragFunc; - renderPipelineDesc.colorAttachments[0].pixelFormat = currentTexture.pixelFormat; - id pipeline = [device - newRenderPipelineStateWithDescriptor:renderPipelineDesc error:&errors]; - [renderEncoder setRenderPipelineState:pipeline]; + [mtlCommandBuffer renderCommandEncoderWithDescriptor:renderPassDesc]; + [renderEncoder setViewport:MTLViewport{0, 0, (double) SCDesc.Width, (double) SCDesc.Height, 0, 1}]; + [renderEncoder setRenderPipelineState:m_MtlPipeline]; [renderEncoder drawPrimitives:MTLPrimitiveTypeTriangle - vertexStart:0 vertexCount:3]; + vertexStart:0 vertexCount:6]; [renderEncoder endEncoding]; - [commandBuffer commit]; - */ + [mtlCommandBuffer commit]; } private: + id m_MtlPipeline; }; } // namespace void RenderDrawCommandReferenceMtl(ISwapChain* pSwapChain, const float* pClearColor) { - //auto* pEnv = TestingEnvironmentMtl::GetInstance(); - //auto* pContext = pEnv->GetDeviceContext(); - //auto* pTestingSwapChainMtl = ValidatedCast(pSwapChain); + auto* pEnv = TestingEnvironmentMtl::GetInstance(); + auto* pContext = pEnv->GetDeviceContext(); - //TriRenderer.Draw(SCDesc.Width, SCDesc.Height, pClearColor); + TriangleRenderer TriRenderer{MSL::DrawTestFunctions}; + TriRenderer.Draw(pClearColor); // Make sure Diligent Engine will reset all GL states - //pContext->InvalidateState(); + pContext->InvalidateState(); } void RenderPassMSResolveReferenceMtl(ISwapChain* pSwapChain, const float* pClearColor) diff --git a/Tests/DiligentCoreAPITest/src/Metal/TestingSwapChainMtl.mm b/Tests/DiligentCoreAPITest/src/Metal/TestingSwapChainMtl.mm index ccf5592e..c6f93759 100644 --- a/Tests/DiligentCoreAPITest/src/Metal/TestingSwapChainMtl.mm +++ b/Tests/DiligentCoreAPITest/src/Metal/TestingSwapChainMtl.mm @@ -25,11 +25,14 @@ * of the possibility of such damages. */ +#include + #include "Metal/TestingSwapChainMtl.hpp" #include "Metal/TestingEnvironmentMtl.hpp" #include "RenderDeviceMtl.h" #include "DeviceContextMtl.h" +#include "TextureViewMtl.h" namespace Diligent { @@ -48,6 +51,10 @@ TestingSwapChainMtl::TestingSwapChainMtl(IReferenceCounters* pRefCounters, SCDesc // } { + auto mtlDevice = pEnv->GetMtlDevice(); + m_MtlStagingBuffer = + [mtlDevice newBufferWithLength:SCDesc.Width * SCDesc.Height * 4 + options:MTLResourceStorageModeManaged]; } TestingSwapChainMtl::~TestingSwapChainMtl() @@ -56,6 +63,31 @@ TestingSwapChainMtl::~TestingSwapChainMtl() void TestingSwapChainMtl::TakeSnapshot() { + auto* pEnv = TestingEnvironmentMtl::GetInstance(); + auto mtlCommandQueue = pEnv->GetMtlCommandQueue(); + + auto* pRTV = ValidatedCast(GetCurrentBackBufferRTV()); + auto mtlTexture = pRTV->GetMtlTexture(); + + m_ReferenceDataPitch = m_SwapChainDesc.Height * 4; + m_ReferenceData.resize(m_SwapChainDesc.Width * m_ReferenceDataPitch); + + auto commandBuffer = [mtlCommandQueue commandBuffer]; + auto blitEncoder = [commandBuffer blitCommandEncoder]; + [blitEncoder copyFromTexture:mtlTexture + sourceSlice:0 + sourceLevel:0 + sourceOrigin:MTLOrigin{0,0,0} + sourceSize:MTLSize{m_SwapChainDesc.Width, m_SwapChainDesc.Height, 1} + toBuffer:m_MtlStagingBuffer + destinationOffset:0 + destinationBytesPerRow:m_ReferenceDataPitch + destinationBytesPerImage:0]; + [blitEncoder synchronizeResource:m_MtlStagingBuffer]; + [blitEncoder endEncoding]; + [commandBuffer commit]; + [commandBuffer waitUntilCompleted]; + memcpy(m_ReferenceData.data(), [m_MtlStagingBuffer contents], m_ReferenceData.size()); } void CreateTestingSwapChainMtl(TestingEnvironmentMtl* pEnv, -- cgit v1.2.3 From 7144588e9b0c3f9d78857c31d728d85f891cfc12 Mon Sep 17 00:00:00 2001 From: assiduous Date: Mon, 9 Nov 2020 15:12:41 -0800 Subject: Implemented render pass MS resolve test Metal reference --- .../src/Metal/DrawCommandReferenceMtl.mm | 121 ++++++++++++++------- Tests/DiligentCoreAPITest/src/RenderPassTest.cpp | 32 +++++- 2 files changed, 107 insertions(+), 46 deletions(-) diff --git a/Tests/DiligentCoreAPITest/src/Metal/DrawCommandReferenceMtl.mm b/Tests/DiligentCoreAPITest/src/Metal/DrawCommandReferenceMtl.mm index cf989c7b..49869a38 100644 --- a/Tests/DiligentCoreAPITest/src/Metal/DrawCommandReferenceMtl.mm +++ b/Tests/DiligentCoreAPITest/src/Metal/DrawCommandReferenceMtl.mm @@ -47,10 +47,10 @@ class TriangleRenderer { public: - TriangleRenderer(const std::string& ProgramSource) + TriangleRenderer(const std::string& ProgramSource, Uint32 SampleCount = 1) { auto* const pEnv = TestingEnvironmentMtl::GetInstance(); - auto const mtlDevice = pEnv->GetMtlDevice(); + auto* const mtlDevice = pEnv->GetMtlDevice(); auto* progSrc = [NSString stringWithUTF8String:MSL::DrawTestFunctions.c_str()]; NSError *errors = nil; @@ -74,6 +74,7 @@ public: default: UNSUPPORTED("Unexpected swap chain color format"); } + renderPipelineDesc.sampleCount = SampleCount; renderPipelineDesc.colorAttachments[0].pixelFormat = pixelFormat; m_MtlPipeline = [mtlDevice newRenderPipelineStateWithDescriptor:renderPipelineDesc error:&errors]; @@ -83,34 +84,12 @@ public: { } - void Draw(const float* pClearColor) + void Draw(id renderEncoder) { - auto* const pEnv = TestingEnvironmentMtl::GetInstance(); - auto const mtlCommandQueue = pEnv->GetMtlCommandQueue(); - - auto* const pSwapChain = pEnv->GetSwapChain(); - auto* pTestingSwapChainMtl = ValidatedCast(pSwapChain); - const auto& SCDesc = pTestingSwapChainMtl->GetDesc(); - - auto* pRTV = pTestingSwapChainMtl->GetCurrentBackBufferRTV(); - auto* mtlTexture = ValidatedCast(pRTV)->GetMtlTexture(); - - id mtlCommandBuffer = [mtlCommandQueue commandBuffer]; - - MTLRenderPassDescriptor* renderPassDesc = - [MTLRenderPassDescriptor renderPassDescriptor]; - renderPassDesc.colorAttachments[0].texture = mtlTexture; - renderPassDesc.colorAttachments[0].loadAction = MTLLoadActionClear; - renderPassDesc.colorAttachments[0].clearColor = MTLClearColorMake(0.0,0.0,0.0,0.0); - renderPassDesc.colorAttachments[0].storeAction = MTLStoreActionStore; - id renderEncoder = - [mtlCommandBuffer renderCommandEncoderWithDescriptor:renderPassDesc]; - [renderEncoder setViewport:MTLViewport{0, 0, (double) SCDesc.Width, (double) SCDesc.Height, 0, 1}]; [renderEncoder setRenderPipelineState:m_MtlPipeline]; [renderEncoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:6]; [renderEncoder endEncoding]; - [mtlCommandBuffer commit]; } private: @@ -121,28 +100,88 @@ private: void RenderDrawCommandReferenceMtl(ISwapChain* pSwapChain, const float* pClearColor) { - auto* pEnv = TestingEnvironmentMtl::GetInstance(); - auto* pContext = pEnv->GetDeviceContext(); + auto* const pEnv = TestingEnvironmentMtl::GetInstance(); + auto* const mtlCommandQueue = pEnv->GetMtlCommandQueue(); + + auto* pTestingSwapChainMtl = ValidatedCast(pSwapChain); + const auto& SCDesc = pTestingSwapChainMtl->GetDesc(); + + auto* pRTV = pTestingSwapChainMtl->GetCurrentBackBufferRTV(); + auto* mtlBackBuffer = ValidatedCast(pRTV)->GetMtlTexture(); + + id mtlCommandBuffer = [mtlCommandQueue commandBuffer]; + + constexpr float Zero[4] = {}; + if (pClearColor == nullptr) + pClearColor = Zero; + + MTLRenderPassDescriptor* renderPassDesc = + [MTLRenderPassDescriptor renderPassDescriptor]; + renderPassDesc.colorAttachments[0].texture = mtlBackBuffer; + renderPassDesc.colorAttachments[0].loadAction = MTLLoadActionClear; + renderPassDesc.colorAttachments[0].clearColor = MTLClearColorMake(pClearColor[0], pClearColor[1], pClearColor[2], pClearColor[3]); + renderPassDesc.colorAttachments[0].storeAction = MTLStoreActionStore; + id renderEncoder = + [mtlCommandBuffer renderCommandEncoderWithDescriptor:renderPassDesc]; + [renderEncoder setViewport:MTLViewport{0, 0, (double) SCDesc.Width, (double) SCDesc.Height, 0, 1}]; TriangleRenderer TriRenderer{MSL::DrawTestFunctions}; - TriRenderer.Draw(pClearColor); + TriRenderer.Draw(renderEncoder); + + [mtlCommandBuffer commit]; - // Make sure Diligent Engine will reset all GL states - pContext->InvalidateState(); + // Make sure Diligent Engine will reset all states + pEnv->GetDeviceContext()->InvalidateState(); } void RenderPassMSResolveReferenceMtl(ISwapChain* pSwapChain, const float* pClearColor) { - //auto* pEnv = TestingEnvironmentMtl:GetInstance(); - //auto* pContext = pEnv->GetDeviceContext(); - //auto* pTestingSwapChainGL = ValidatedCast(pSwapChain); - - //const auto& SCDesc = pTestingSwapChainGL->GetDesc(); - - //TriangleRenderer TriRenderer{GLSL::DrawTest_FS}; - - // Make sure Diligent Engine will reset all GL states - //pContext->InvalidateState(); + auto* const pEnv = TestingEnvironmentMtl::GetInstance(); + auto* const mtlCommandQueue = pEnv->GetMtlCommandQueue(); + auto* const mtlDevice = pEnv->GetMtlDevice(); + + auto* pTestingSwapChainMtl = ValidatedCast(pSwapChain); + const auto& SCDesc = pTestingSwapChainMtl->GetDesc(); + + auto* pRTV = pTestingSwapChainMtl->GetCurrentBackBufferRTV(); + auto* mtlBackBuffer = ValidatedCast(pRTV)->GetMtlTexture(); + + constexpr Uint32 SampleCount = 4; + + MTLTextureDescriptor* msTextureDescriptor = [[MTLTextureDescriptor alloc] init]; + msTextureDescriptor.textureType = MTLTextureType2DMultisample; + msTextureDescriptor.width = SCDesc.Width; + msTextureDescriptor.height = SCDesc.Height; + msTextureDescriptor.sampleCount = SampleCount; + msTextureDescriptor.pixelFormat = mtlBackBuffer.pixelFormat; + msTextureDescriptor.arrayLength = 1; + msTextureDescriptor.mipmapLevelCount = 1; + msTextureDescriptor.storageMode = MTLStorageModePrivate; + msTextureDescriptor.allowGPUOptimizedContents = true; + msTextureDescriptor.usage = MTLTextureUsageRenderTarget; + auto mtlMSTexture = [mtlDevice newTextureWithDescriptor:msTextureDescriptor]; + ASSERT_TRUE(mtlMSTexture != nil); + + id mtlCommandBuffer = [mtlCommandQueue commandBuffer]; + + MTLRenderPassDescriptor* renderPassDesc = + [MTLRenderPassDescriptor renderPassDescriptor]; + renderPassDesc.colorAttachments[0].texture = mtlMSTexture; + renderPassDesc.colorAttachments[0].loadAction = MTLLoadActionClear; + renderPassDesc.colorAttachments[0].clearColor = MTLClearColorMake(pClearColor[0], pClearColor[1], pClearColor[2], pClearColor[3]); + renderPassDesc.colorAttachments[0].storeAction = MTLStoreActionMultisampleResolve; + renderPassDesc.colorAttachments[0].resolveTexture = mtlBackBuffer; + id renderEncoder = + [mtlCommandBuffer renderCommandEncoderWithDescriptor:renderPassDesc]; + [renderEncoder setViewport:MTLViewport{0, 0, (double) SCDesc.Width, (double) SCDesc.Height, 0, 1}]; + + TriangleRenderer TriRenderer{MSL::DrawTestFunctions, SampleCount}; + TriRenderer.Draw(renderEncoder); + + [mtlCommandBuffer commit]; + + // Make sure Diligent Engine will reset all states + pEnv->GetDeviceContext()->InvalidateState(); } void RenderPassInputAttachmentReferenceMtl(ISwapChain* pSwapChain, const float* pClearColor) @@ -153,7 +192,7 @@ void RenderPassInputAttachmentReferenceMtl(ISwapChain* pSwapChain, const float* //const auto& SCDesc = pTestingSwapChainMtl->GetDesc(); - // Make sure Diligent Engine will reset all GL states + // Make sure Diligent Engine will reset all states //pContext->InvalidateState(); } diff --git a/Tests/DiligentCoreAPITest/src/RenderPassTest.cpp b/Tests/DiligentCoreAPITest/src/RenderPassTest.cpp index 0298a500..24145cd9 100644 --- a/Tests/DiligentCoreAPITest/src/RenderPassTest.cpp +++ b/Tests/DiligentCoreAPITest/src/RenderPassTest.cpp @@ -65,7 +65,9 @@ void RenderPassInputAttachmentReferenceVk(ISwapChain* pSwapChain, const float* p #endif #if METAL_SUPPORTED - +void RenderDrawCommandReferenceMtl(ISwapChain* pSwapChain, const float* pClearColor); +void RenderPassMSResolveReferenceMtl(ISwapChain* pSwapChain, const float* pClearColor); +void RenderPassInputAttachmentReferenceMtl(ISwapChain* pSwapChain, const float* pClearColor); #endif } // namespace Testing @@ -472,17 +474,19 @@ TEST_F(RenderPassTest, CreateRenderPassAndFramebuffer) RPBeginInfo.StateTransitionMode = RESOURCE_STATE_TRANSITION_MODE_TRANSITION; pContext->BeginRenderPass(RPBeginInfo); - if (DeviceType != RENDER_DEVICE_TYPE_D3D12) + if (DeviceType != RENDER_DEVICE_TYPE_D3D12 && + DeviceType != RENDER_DEVICE_TYPE_METAL) { - // ClearDepthStencil is not allowed inside a render pass in Direct3D12 + // ClearDepthStencil is not allowed inside a render pass in Direct3D12 and Metal pContext->ClearDepthStencil(pTexViews[3], CLEAR_DEPTH_FLAG, 1.0, 0, RESOURCE_STATE_TRANSITION_MODE_VERIFY); } pContext->NextSubpass(); - if (DeviceType != RENDER_DEVICE_TYPE_D3D12) + if (DeviceType != RENDER_DEVICE_TYPE_D3D12 && + DeviceType != RENDER_DEVICE_TYPE_METAL) { - // ClearRenderTarget is not allowed inside a render pass in Direct3D12 + // ClearRenderTarget is not allowed inside a render pass in Direct3D12 and Metal float ClearColor[] = {0, 0, 0, 0}; pContext->ClearRenderTarget(pTexViews[4], ClearColor, RESOURCE_STATE_TRANSITION_MODE_VERIFY); } @@ -534,6 +538,12 @@ TEST_F(RenderPassTest, Draw) break; #endif +#if METAL_SUPPORTED + case RENDER_DEVICE_TYPE_METAL: + RenderDrawCommandReferenceMtl(pSwapChain, ClearColor); + break; +#endif + default: LOG_ERROR_AND_THROW("Unsupported device type"); } @@ -637,6 +647,12 @@ TEST_F(RenderPassTest, MSResolve) break; #endif +#if METAL_SUPPORTED + case RENDER_DEVICE_TYPE_METAL: + RenderPassMSResolveReferenceMtl(pSwapChain, ClearColor); + break; +#endif + default: LOG_ERROR_AND_THROW("Unsupported device type"); } @@ -774,6 +790,12 @@ TEST_F(RenderPassTest, InputAttachment) break; #endif +#if METAL_SUPPORTED + case RENDER_DEVICE_TYPE_METAL: + RenderPassInputAttachmentReferenceMtl(pSwapChain, ClearColor); + break; +#endif + default: LOG_ERROR_AND_THROW("Unsupported device type"); } -- cgit v1.2.3 From b6b7ab726dd6c0041f6ccf6b2bf8a09c3044b8e7 Mon Sep 17 00:00:00 2001 From: assiduous Date: Mon, 9 Nov 2020 17:03:23 -0800 Subject: Implemented render pass input attachment test Metal reference --- .../include/InlineShaders/DrawCommandTestMSL.h | 20 ++++-- .../src/Metal/DrawCommandReferenceMtl.mm | 76 +++++++++++++++++----- 2 files changed, 75 insertions(+), 21 deletions(-) diff --git a/Tests/DiligentCoreAPITest/include/InlineShaders/DrawCommandTestMSL.h b/Tests/DiligentCoreAPITest/include/InlineShaders/DrawCommandTestMSL.h index 581bb961..27a8e781 100644 --- a/Tests/DiligentCoreAPITest/include/InlineShaders/DrawCommandTestMSL.h +++ b/Tests/DiligentCoreAPITest/include/InlineShaders/DrawCommandTestMSL.h @@ -49,7 +49,7 @@ struct VSOut float4 Position [[position]]; }; -vertex VSOut QuadVS(uint VertexId [[vertex_id]]) +vertex VSOut TrisVS(uint VertexId [[vertex_id]]) { float4 Pos[6] = { @@ -76,14 +76,26 @@ vertex VSOut QuadVS(uint VertexId [[vertex_id]]) return out; } -struct PSOut +struct FSOut { float4 Color [[color(0)]]; }; -fragment PSOut QuadPS(VSOut in [[stage_in]]) +fragment FSOut TrisFS(VSOut in [[stage_in]]) { - PSOut out = {float4(in.Color.rgb, 1.0)}; + FSOut out = {float4(in.Color.rgb, 1.0)}; + return out; +} + +fragment FSOut InptAttFS(VSOut in [[stage_in]], + texture2d SubpassInput [[texture(0)]]) +{ + FSOut out; + + out.Color.rgb = in.Color.rgb * 0.125; + out.Color.rgb += (float3(1.0, 1.0, 1.0) - SubpassInput.read(uint2(in.Position.xy)).brg) * 0.875; + out.Color.a = 1.0; + return out; } diff --git a/Tests/DiligentCoreAPITest/src/Metal/DrawCommandReferenceMtl.mm b/Tests/DiligentCoreAPITest/src/Metal/DrawCommandReferenceMtl.mm index 49869a38..f5a3d4fb 100644 --- a/Tests/DiligentCoreAPITest/src/Metal/DrawCommandReferenceMtl.mm +++ b/Tests/DiligentCoreAPITest/src/Metal/DrawCommandReferenceMtl.mm @@ -47,7 +47,7 @@ class TriangleRenderer { public: - TriangleRenderer(const std::string& ProgramSource, Uint32 SampleCount = 1) + TriangleRenderer(NSString* fragEntry, Uint32 SampleCount = 1) { auto* const pEnv = TestingEnvironmentMtl::GetInstance(); auto* const mtlDevice = pEnv->GetMtlDevice(); @@ -57,8 +57,12 @@ public: id library = [mtlDevice newLibraryWithSource:progSrc options:nil error:&errors]; - id vertFunc = [library newFunctionWithName:@"QuadVS"]; - id fragFunc = [library newFunctionWithName:@"QuadPS"]; + if (library == nil) + { + LOG_ERROR_AND_THROW("Failed to create Metal library: ", [errors.localizedDescription cStringUsingEncoding:NSUTF8StringEncoding]); + } + id vertFunc = [library newFunctionWithName:@"TrisVS"]; + id fragFunc = [library newFunctionWithName:fragEntry]; MTLRenderPipelineDescriptor* renderPipelineDesc = [[MTLRenderPipelineDescriptor alloc] init]; renderPipelineDesc.vertexFunction = vertFunc; @@ -125,13 +129,10 @@ void RenderDrawCommandReferenceMtl(ISwapChain* pSwapChain, const float* pClearCo [mtlCommandBuffer renderCommandEncoderWithDescriptor:renderPassDesc]; [renderEncoder setViewport:MTLViewport{0, 0, (double) SCDesc.Width, (double) SCDesc.Height, 0, 1}]; - TriangleRenderer TriRenderer{MSL::DrawTestFunctions}; + TriangleRenderer TriRenderer{@"TrisFS"}; TriRenderer.Draw(renderEncoder); [mtlCommandBuffer commit]; - - // Make sure Diligent Engine will reset all states - pEnv->GetDeviceContext()->InvalidateState(); } void RenderPassMSResolveReferenceMtl(ISwapChain* pSwapChain, const float* pClearColor) @@ -175,25 +176,66 @@ void RenderPassMSResolveReferenceMtl(ISwapChain* pSwapChain, const float* pClear [mtlCommandBuffer renderCommandEncoderWithDescriptor:renderPassDesc]; [renderEncoder setViewport:MTLViewport{0, 0, (double) SCDesc.Width, (double) SCDesc.Height, 0, 1}]; - TriangleRenderer TriRenderer{MSL::DrawTestFunctions, SampleCount}; + TriangleRenderer TriRenderer{@"TrisFS", SampleCount}; TriRenderer.Draw(renderEncoder); [mtlCommandBuffer commit]; - - // Make sure Diligent Engine will reset all states - pEnv->GetDeviceContext()->InvalidateState(); } void RenderPassInputAttachmentReferenceMtl(ISwapChain* pSwapChain, const float* pClearColor) { - //auto* pEnv = TestingEnvironmentMtl::GetInstance(); - //auto* pContext = pEnv->GetDeviceContext(); - //auto* pTestingSwapChainMtl = ValidatedCast(pSwapChain); + auto* const pEnv = TestingEnvironmentMtl::GetInstance(); + auto* const mtlCommandQueue = pEnv->GetMtlCommandQueue(); + auto* const mtlDevice = pEnv->GetMtlDevice(); + + auto* pTestingSwapChainMtl = ValidatedCast(pSwapChain); + const auto& SCDesc = pTestingSwapChainMtl->GetDesc(); + + auto* pRTV = pTestingSwapChainMtl->GetCurrentBackBufferRTV(); + auto* mtlBackBuffer = ValidatedCast(pRTV)->GetMtlTexture(); - //const auto& SCDesc = pTestingSwapChainMtl->GetDesc(); + MTLTextureDescriptor* inptAttTextureDescriptor = [[MTLTextureDescriptor alloc] init]; + inptAttTextureDescriptor.textureType = MTLTextureType2D; + inptAttTextureDescriptor.width = SCDesc.Width; + inptAttTextureDescriptor.height = SCDesc.Height; + inptAttTextureDescriptor.pixelFormat = mtlBackBuffer.pixelFormat; + inptAttTextureDescriptor.arrayLength = 1; + inptAttTextureDescriptor.mipmapLevelCount = 1; + inptAttTextureDescriptor.storageMode = MTLStorageModePrivate; + inptAttTextureDescriptor.allowGPUOptimizedContents = true; + inptAttTextureDescriptor.usage = MTLTextureUsageRenderTarget | MTLTextureUsageShaderRead; + auto mtlInputAttachment = [mtlDevice newTextureWithDescriptor:inptAttTextureDescriptor]; + ASSERT_TRUE(mtlInputAttachment != nil); - // Make sure Diligent Engine will reset all states - //pContext->InvalidateState(); + id mtlCommandBuffer = [mtlCommandQueue commandBuffer]; + + MTLRenderPassDescriptor* subpass0Desc = + [MTLRenderPassDescriptor renderPassDescriptor]; + subpass0Desc.colorAttachments[0].texture = mtlInputAttachment; + subpass0Desc.colorAttachments[0].loadAction = MTLLoadActionClear; + subpass0Desc.colorAttachments[0].clearColor = MTLClearColorMake(0, 0, 0, 0); + subpass0Desc.colorAttachments[0].storeAction = MTLStoreActionStore; + id renderEncoder = + [mtlCommandBuffer renderCommandEncoderWithDescriptor:subpass0Desc]; + [renderEncoder setViewport:MTLViewport{0, 0, (double) SCDesc.Width, (double) SCDesc.Height, 0, 1}]; + + TriangleRenderer TriRenderer{@"TrisFS"}; + TriRenderer.Draw(renderEncoder); + + MTLRenderPassDescriptor* subpass1Desc = + [MTLRenderPassDescriptor renderPassDescriptor]; + subpass1Desc.colorAttachments[0].texture = mtlBackBuffer; + subpass1Desc.colorAttachments[0].loadAction = MTLLoadActionClear; + subpass1Desc.colorAttachments[0].clearColor = MTLClearColorMake(pClearColor[0], pClearColor[1], pClearColor[2], pClearColor[3]); + subpass1Desc.colorAttachments[0].storeAction = MTLStoreActionStore; + renderEncoder = [mtlCommandBuffer renderCommandEncoderWithDescriptor:subpass1Desc]; + [renderEncoder setViewport:MTLViewport{0, 0, (double) SCDesc.Width, (double) SCDesc.Height, 0, 1}]; + + TriangleRenderer TriRendererInptAtt{@"InptAttFS"}; + [renderEncoder setFragmentTexture:mtlInputAttachment atIndex:0]; + TriRendererInptAtt.Draw(renderEncoder); + + [mtlCommandBuffer commit]; } } // namespace Testing -- cgit v1.2.3