yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
5.5 KiB165 linesraw
1// d3d-util.h
2#pragma once
3
4#include "../flag-combiner.h"
5#include "core/slang-basic.h"
6#include "core/slang-platform.h"
7#include "slang-com-helper.h"
8#include "slang-com-ptr.h"
9#include "slang-gfx.h"
10
11#include <d3d12.h>
12#include <d3dcommon.h>
13#include <dxgi.h>
14#include <dxgiformat.h>
15#include <stdint.h>
16
17#if defined(__ID3D12Device5_FWD_DEFINED__) && defined(__ID3D12GraphicsCommandList4_FWD_DEFINED__)
18#define SLANG_GFX_HAS_DXR_SUPPORT 1
19#else
20#define SLANG_GFX_HAS_DXR_SUPPORT 0
21typedef ISlangUnknown ID3D12Device5;
22typedef ISlangUnknown ID3D12GraphicsCommandList4;
23
24#endif
25
26namespace gfx
27{
28
29class D3DUtil
30{
31public:
32    enum UsageType
33    {
34        USAGE_UNKNOWN,       ///< Generally used to mark an error
35        USAGE_TARGET,        ///< Format should be used when written as target
36        USAGE_DEPTH_STENCIL, ///< Format should be used when written as depth stencil
37        USAGE_SRV,           ///< Format if being read as srv
38        USAGE_COUNT_OF,
39    };
40    enum UsageFlag
41    {
42        USAGE_FLAG_MULTI_SAMPLE = 0x1, ///< If set will be used form multi sampling (such as MSAA)
43        USAGE_FLAG_SRV = 0x2, ///< If set means will be used as a shader resource view (SRV)
44    };
45
46    /// Get primitive topology as D3D primitive topology
47    static D3D_PRIMITIVE_TOPOLOGY getPrimitiveTopology(PrimitiveTopology prim);
48
49    static D3D12_PRIMITIVE_TOPOLOGY_TYPE getPrimitiveType(PrimitiveType type);
50
51    static D3D12_PRIMITIVE_TOPOLOGY_TYPE getPrimitiveType(PrimitiveTopology topology);
52
53    static D3D12_COMPARISON_FUNC getComparisonFunc(ComparisonFunc func);
54
55    static D3D12_DEPTH_STENCILOP_DESC translateStencilOpDesc(DepthStencilOpDesc desc);
56
57    /// Calculate size taking into account alignment. Alignment must be a power of 2
58    static UInt calcAligned(UInt size, UInt alignment)
59    {
60        return (size + alignment - 1) & ~(alignment - 1);
61    }
62
63    /// Compile HLSL code to DXBC
64    static Slang::Result compileHLSLShader(
65        char const* sourcePath,
66        char const* source,
67        char const* entryPointName,
68        char const* dxProfileName,
69        Slang::ComPtr<ID3DBlob>& shaderBlobOut);
70
71    /// Given a slang pixel format returns the equivalent DXGI_ pixel format. If the format is not
72    /// known, will return DXGI_FORMAT_UNKNOWN
73    static DXGI_FORMAT getMapFormat(Format format);
74
75    /// Given the usage, flags, and format will return the most suitable format. Will return
76    /// DXGI_UNKNOWN if combination is not possible
77    static DXGI_FORMAT calcFormat(UsageType usage, DXGI_FORMAT format);
78    /// Calculate appropriate format for creating a buffer for usage and flags
79    static DXGI_FORMAT calcResourceFormat(UsageType usage, Int usageFlags, DXGI_FORMAT format);
80    /// True if the type is 'typeless'
81    static bool isTypeless(DXGI_FORMAT format);
82
83    /// Returns number of bits used for color channel for format (for channels with multiple sizes,
84    /// returns smallest ie RGB565 -> 5)
85    static Int getNumColorChannelBits(DXGI_FORMAT fmt);
86
87    static SlangResult createFactory(
88        DeviceCheckFlags flags,
89        Slang::ComPtr<IDXGIFactory>& outFactory);
90
91    /// Get the dxgiModule
92    static Slang::SharedLibrary::Handle getDxgiModule();
93
94    /// Find adapters
95    static SlangResult findAdapters(
96        DeviceCheckFlags flags,
97        const AdapterLUID* adapterLUID,
98        IDXGIFactory* dxgiFactory,
99        Slang::List<Slang::ComPtr<IDXGIAdapter>>& dxgiAdapters);
100    /// Find adapters
101    static SlangResult findAdapters(
102        DeviceCheckFlags flags,
103        const AdapterLUID* adapterLUID,
104        Slang::List<Slang::ComPtr<IDXGIAdapter>>& dxgiAdapters);
105
106    static AdapterLUID getAdapterLUID(IDXGIAdapter* dxgiAdapter);
107
108    /// True if the adapter is warp
109    static bool isWarp(IDXGIFactory* dxgiFactory, IDXGIAdapter* adapter);
110
111    static bool isUAVBinding(slang::BindingType bindingType);
112
113    static int getShaderModelFromProfileName(const char* profile);
114
115    static uint32_t getPlaneSlice(DXGI_FORMAT format, TextureAspect aspect);
116
117    static uint32_t getPlaneSliceCount(DXGI_FORMAT format);
118
119    static D3D12_INPUT_CLASSIFICATION getInputSlotClass(InputSlotClass slotClass);
120
121    static D3D12_FILL_MODE getFillMode(FillMode mode);
122
123    static D3D12_CULL_MODE getCullMode(CullMode mode);
124
125    static D3D12_BLEND_OP getBlendOp(BlendOp op);
126
127    static D3D12_BLEND getBlendFactor(BlendFactor factor);
128
129    static uint32_t getSubresourceIndex(
130        uint32_t mipIndex,
131        uint32_t arrayIndex,
132        uint32_t planeIndex,
133        uint32_t mipLevelCount,
134        uint32_t arraySize);
135
136    static uint32_t getSubresourceMipLevel(uint32_t subresourceIndex, uint32_t mipLevelCount);
137
138    static D3D12_RESOURCE_STATES getResourceState(ResourceState state);
139
140    static SlangResult reportLiveObjects();
141
142    /// Call after a DXGI_ERROR_DEVICE_REMOVED/DXGI_ERROR_DEVICE_RESET on present, to wait for
143    /// dumping to complete. Will return SLANG_OK if wait happened successfully
144    static SlangResult waitForCrashDumpCompletion(HRESULT res);
145};
146
147#if SLANG_GFX_HAS_DXR_SUPPORT
148struct D3DAccelerationStructureInputsBuilder
149{
150    D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS desc = {};
151    D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO prebuildInfo = {};
152    Slang::List<D3D12_RAYTRACING_GEOMETRY_DESC> geomDescs;
153    Slang::Result build(
154        const IAccelerationStructure::BuildInputs& buildInputs,
155        IDebugCallback* callback);
156
157private:
158    D3D12_RAYTRACING_GEOMETRY_FLAGS
159    translateGeometryFlags(IAccelerationStructure::GeometryFlags::Enum flags)
160    {
161        return (D3D12_RAYTRACING_GEOMETRY_FLAGS)flags;
162    }
163};
164#endif
165} // namespace gfx