Added docs for reflection and interop APIs

このコミットが含まれているのは:
Sergio Pedri committed 2021-03-12 17:25:11 +01:00
1 親 d9298a93e8
コミット c65a36c87d
1ファイル変更
+38
+38
ファイルの表示
@@ -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<MainKernel>(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<T, TPixel>` 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<ID3D12Device> d3D12Device = default;
using ComPtr<ID3D12Resource> d3D12Resource = default;
using ReadWriteBuffer<float> buffer = Gpu.Default.AllocateReadWriteBuffer<float>(128);
// Get the underlying ID3D12Device object
InteropServices.GetID3D12Device(Gpu.Default, __uuidof<ID3D12Device>(), (void**)d3D12Device.GetAddressOf());
// Get the underlying ID3D12Resource object
InteropServices.GetID3D12Resource(buffer, __uuidof<ID3D12Resource>(), (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: