yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
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 : 32enum UsageType 33 { 34USAGE_UNKNOWN ,///< Generally used to mark an error 35USAGE_TARGET ,///< Format should be used when written as target 36USAGE_DEPTH_STENCIL ,///< Format should be used when written as depth stencil 37USAGE_SRV ,///< Format if being read as srv 38USAGE_COUNT_OF , 39 }; 40enum UsageFlag 41{ 42USAGE_FLAG_MULTI_SAMPLE = 0x1 , ///< If set will be used form multi sampling (such as MSAA) 43USAGE_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 47static D3D_PRIMITIVE_TOPOLOGY getPrimitiveTopology ( PrimitiveTopology prim); 48 49static D3D12_PRIMITIVE_TOPOLOGY_TYPE getPrimitiveType ( PrimitiveType type); 50 51static D3D12_PRIMITIVE_TOPOLOGY_TYPE getPrimitiveType ( PrimitiveTopology topology); 52 53static D3D12_COMPARISON_FUNC getComparisonFunc ( ComparisonFunc func); 54 55static D3D12_DEPTH_STENCILOP_DESC translateStencilOpDesc ( DepthStencilOpDesc desc); 56 57/// Calculate size taking into account alignment. Alignment must be a power of 2 58static UInt calcAligned ( UInt size, UInt alignment) 59{ 60return (size + alignment - 1 ) & ~(alignment - 1 ); 61} 62 63/// Compile HLSL code to DXBC 64static Slang ::Result compileHLSLShader ( 65char const * sourcePath, 66char const * source, 67char const * entryPointName, 68char const * dxProfileName, 69Slang ::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 73static 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 77static DXGI_FORMAT calcFormat ( UsageType usage, DXGI_FORMAT format); 78/// Calculate appropriate format for creating a buffer for usage and flags 79static DXGI_FORMAT calcResourceFormat ( UsageType usage, Int usageFlags, DXGI_FORMAT format); 80/// True if the type is 'typeless' 81static 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) 85static Int getNumColorChannelBits ( DXGI_FORMAT fmt); 86 87static SlangResult createFactory ( 88DeviceCheckFlags flags, 89Slang ::ComPtr < IDXGIFactory >& outFactory); 90 91/// Get the dxgiModule 92static Slang ::SharedLibrary::Handle getDxgiModule (); 93 94/// Find adapters 95static SlangResult findAdapters ( 96DeviceCheckFlags flags, 97const AdapterLUID * adapterLUID, 98IDXGIFactory * dxgiFactory, 99Slang ::List < Slang::ComPtr < IDXGIAdapter>> & dxgiAdapters); 100/// Find adapters 101static SlangResult findAdapters ( 102DeviceCheckFlags flags, 103const AdapterLUID * adapterLUID, 104Slang ::List < Slang::ComPtr < IDXGIAdapter>> & dxgiAdapters); 105 106static AdapterLUID getAdapterLUID ( IDXGIAdapter * dxgiAdapter); 107 108/// True if the adapter is warp 109static bool isWarp ( IDXGIFactory * dxgiFactory, IDXGIAdapter * adapter); 110 111static bool isUAVBinding ( slang ::BindingType bindingType); 112 113static int getShaderModelFromProfileName ( const char * profile); 114 115static uint32_t getPlaneSlice ( DXGI_FORMAT format, TextureAspect aspect); 116 117static uint32_t getPlaneSliceCount ( DXGI_FORMAT format); 118 119static D3D12_INPUT_CLASSIFICATION getInputSlotClass ( InputSlotClass slotClass); 120 121static D3D12_FILL_MODE getFillMode ( FillMode mode); 122 123static D3D12_CULL_MODE getCullMode ( CullMode mode); 124 125static D3D12_BLEND_OP getBlendOp ( BlendOp op); 126 127static D3D12_BLEND getBlendFactor ( BlendFactor factor); 128 129static uint32_t getSubresourceIndex ( 130uint32_t mipIndex, 131uint32_t arrayIndex, 132uint32_t planeIndex, 133uint32_t mipLevelCount, 134uint32_t arraySize); 135 136static uint32_t getSubresourceMipLevel ( uint32_t subresourceIndex, uint32_t mipLevelCount); 137 138static D3D12_RESOURCE_STATES getResourceState ( ResourceState state); 139 140static 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 144static SlangResult waitForCrashDumpCompletion ( HRESULT res); 145}; 146 147#if SLANG_GFX_HAS_DXR_SUPPORT 148struct D3DAccelerationStructureInputsBuilder 149{ 150D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_INPUTS desc = {}; 151D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO prebuildInfo = {}; 152Slang ::List < D3D12_RAYTRACING_GEOMETRY_DESC > geomDescs; 153Slang :: Result build( 154const IAccelerationStructure ::BuildInputs & buildInputs, 155IDebugCallback * callback); 156 157private : 158D3D12_RAYTRACING_GEOMETRY_FLAGS 159translateGeometryFlags ( IAccelerationStructure ::GeometryFlags::Enum flags) 160{ 161return ( D3D12_RAYTRACING_GEOMETRY_FLAGS )flags; 162} 163}; 164#endif 165} // namespace gfx