From c65a36c87dbcc09c637f768d2a584818853924fb Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 12 Mar 2021 17:25:11 +0100 Subject: [PATCH] Added docs for reflection and interop APIs --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/README.md b/README.md index b4d6f124..3961bc21 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ - [Dispatch info](#dispatch-info) - [Working with images](#working-with-images) - [Shader metaprogramming](#shader-metaprogramming) + - [Inspecting shaders](#inspecting-shaders) + - [DirectX interop](#directx-interop) - [Requirements](#requirements) - [F# support](#f-support) - [Sponsors](#sponsors) @@ -297,6 +299,42 @@ Gpu.Default.For(width, height, new ApplyFunction(buffer, Square)); buffer.CopyTo(array); ``` +## Inspecting shaders + +For users that are familiar with the HLSL language and might want to access more info on a given shader generated by **ComputeSharp**, such as the compiled HLSL code or the statistics exposed by the [DirectX 12 reflection APIs](https://docs.microsoft.com/windows/win32/api/d3d12shader/nn-d3d12shader-id3d12shaderreflection), the library includes a `ReflectionServices` class that allows to easily gather all these details on a given shader type. + +Assuming we have the same shader defined in [Quick start](#quick-start), here is how this class can be used: + +```csharp +ReflectionServices.GetShaderInfo(out ShaderInfo shaderInfo); + +// Access info here, for instance... +string hlslSource = shaderInfo.HlslSource; +uint numberOfResources = shaderInfo.BoundResourceCount; +uint instructionCount = shaderInfo.InstructionCount; +``` + +## DirectX interop + +There are cases where it might be needed to manually interop with other DirectX APIs, to perform operations that are not included in the public API surface exposed by **ComputeSharp**. For instance, to copy the contents of a `ReadWriteTextre2D` to the backbuffer of a swap chain object, so that it can be rendered in a window. For these scenarios, the `InteropServices` class exposes APIs to easily retrieve the underlying COM pointers for any of the wrapping types in the library, and to perform a `QueryInterface` call on them to retrieve a COM pointer of a specified interface type. Here is how these APIs can be used: + +```csharp +using ComPtr d3D12Device = default; +using ComPtr d3D12Resource = default; + +using ReadWriteBuffer buffer = Gpu.Default.AllocateReadWriteBuffer(128); + +// Get the underlying ID3D12Device object +InteropServices.GetID3D12Device(Gpu.Default, __uuidof(), (void**)d3D12Device.GetAddressOf()); + +// Get the underlying ID3D12Resource object +InteropServices.GetID3D12Resource(buffer, __uuidof(), (void**)d3D12Resource.GetAddressOf()); + +// Now the COM objects can be used directly, eg. to display images with a swap chain +``` + +The `InteropServices` class is also used by the swap chain sample to access the underlying COM objects and use them the rendered frames in a Win32 window. The full source code is available [here](https://github.com/Sergio0694/ComputeSharp/blob/main/samples/ComputeSharp.SwapChain/Backend/SwapChainApplication%7BT%7D.cs), and it provides reference implementation of how these APIs can be used to interop with other DirectX objects. + # Requirements The **ComputeSharp** library has the following requirements: