yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
1// cuda-shader-object-layout.cpp 2#include "cuda-shader-object-layout.h" 3 4namespace gfx 5{ 6#ifdef GFX_ENABLE_CUDA 7using namespace Slang ; 8 9namespace cuda 10{ 11 12ShaderObjectLayoutImpl ::ShaderObjectLayoutImpl ( 13RendererBase * renderer , 14 slang::ISession * session , 15 slang::TypeLayoutReflection * layout ) 16{ 17m_elementTypeLayout = _unwrapParameterGroups (layout ,m_containerType ); 18 19initBase (renderer ,session ,m_elementTypeLayout ); 20 21// Compute the binding ranges that are used to store 22// the logical contents of the object in memory. These will relate 23// to the descriptor ranges in the various sets, but not always 24// in a one-to-one fashion. 25 26SlangInt bindingRangeCount = m_elementTypeLayout -> getBindingRangeCount (); 27for (SlangInt r = 0 ;r < bindingRangeCount ;++ r ) 28 { 29 slang::BindingType slangBindingType = m_elementTypeLayout -> getBindingRangeType (r ); 30SlangInt count = m_elementTypeLayout -> getBindingRangeBindingCount (r ); 31 slang::TypeLayoutReflection * slangLeafTypeLayout = 32m_elementTypeLayout -> getBindingRangeLeafTypeLayout (r ); 33 34SlangInt descriptorSetIndex = m_elementTypeLayout -> getBindingRangeDescriptorSetIndex (r ); 35SlangInt rangeIndexInDescriptorSet = 36m_elementTypeLayout -> getBindingRangeFirstDescriptorRangeIndex (r ); 37 38// TODO: This logic assumes that for any binding range that might consume 39// multiple kinds of resources, the descriptor range for its uniform 40// usage will be the first one in the range. 41// 42// We need to decide whether that assumption is one we intend to support 43// applications making, or whether they should be forced to perform a 44// linear search over the descriptor ranges for a specific binding range. 45// 46auto uniformOffset = m_elementTypeLayout -> getDescriptorSetDescriptorRangeIndexOffset ( 47descriptorSetIndex , 48rangeIndexInDescriptorSet ); 49 50Index baseIndex = 0 ; 51Index subObjectIndex = 0 ; 52switch (slangBindingType ) 53 { 54case slang::BindingType ::ConstantBuffer : 55case slang::BindingType ::ParameterBlock : 56case slang::BindingType ::ExistentialValue : 57baseIndex = m_subObjectCount ; 58subObjectIndex = baseIndex ; 59m_subObjectCount += count ; 60break ; 61case slang::BindingType ::RawBuffer : 62case slang::BindingType ::MutableRawBuffer : 63if (slangLeafTypeLayout -> getType ()-> getElementType ()!= nullptr ) 64 { 65// A structured buffer occupies both a resource slot and 66// a sub-object slot. 67subObjectIndex = m_subObjectCount ; 68m_subObjectCount += count ; 69 } 70baseIndex = m_resourceCount ; 71m_resourceCount += count ; 72break ; 73default : 74baseIndex = m_resourceCount ; 75m_resourceCount += count ; 76break ; 77 } 78 79BindingRangeInfo bindingRangeInfo ; 80bindingRangeInfo .bindingType = slangBindingType ; 81bindingRangeInfo .count = count ; 82bindingRangeInfo .baseIndex = baseIndex ; 83bindingRangeInfo .uniformOffset = uniformOffset ; 84bindingRangeInfo .subObjectIndex = subObjectIndex ; 85bindingRangeInfo .isSpecializable = m_elementTypeLayout -> isBindingRangeSpecializable (r ); 86m_bindingRanges .add (bindingRangeInfo ); 87 } 88 89SlangInt subObjectRangeCount = m_elementTypeLayout -> getSubObjectRangeCount (); 90for (SlangInt r = 0 ;r < subObjectRangeCount ;++ r ) 91 { 92SlangInt bindingRangeIndex = m_elementTypeLayout -> getSubObjectRangeBindingRangeIndex (r ); 93auto slangBindingType = m_elementTypeLayout -> getBindingRangeType (bindingRangeIndex ); 94 slang::TypeLayoutReflection * slangLeafTypeLayout = 95m_elementTypeLayout -> getBindingRangeLeafTypeLayout (bindingRangeIndex ); 96 97// A sub-object range can either represent a sub-object of a known 98// type, like a `ConstantBuffer<Foo>` or `ParameterBlock<Foo>` 99// (in which case we can pre-compute a layout to use, based on 100// the type `Foo`) *or* it can represent a sub-object of some 101// existential type (e.g., `IBar`) in which case we cannot 102// know the appropriate type/layout of sub-object to allocate. 103// 104RefPtr < ShaderObjectLayoutImpl > subObjectLayout ; 105if (slangBindingType != slang::BindingType ::ExistentialValue ) 106 { 107subObjectLayout = new ShaderObjectLayoutImpl ( 108renderer , 109session , 110slangLeafTypeLayout -> getElementTypeLayout ()); 111 } 112 113SubObjectRangeInfo subObjectRange ; 114subObjectRange .bindingRangeIndex = bindingRangeIndex ; 115subObjectRange .layout = subObjectLayout ; 116subObjectRanges .add (subObjectRange ); 117 } 118} 119 120Index ShaderObjectLayoutImpl ::getResourceCount ()const 121{ 122return m_resourceCount ; 123} 124Index ShaderObjectLayoutImpl ::getSubObjectCount ()const 125{ 126return m_subObjectCount ; 127} 128List < SubObjectRangeInfo >& ShaderObjectLayoutImpl ::getSubObjectRanges () 129{ 130return subObjectRanges ; 131} 132BindingRangeInfo ShaderObjectLayoutImpl ::getBindingRange (Index index ) 133{ 134return m_bindingRanges [index ]; 135} 136Index ShaderObjectLayoutImpl ::getBindingRangeCount ()const 137{ 138return m_bindingRanges .getCount (); 139} 140 141RootShaderObjectLayoutImpl ::RootShaderObjectLayoutImpl ( 142RendererBase * renderer , 143 slang::ProgramLayout * inProgramLayout ) 144 :ShaderObjectLayoutImpl ( 145renderer , 146inProgramLayout -> getSession (), 147inProgramLayout -> getGlobalParamsTypeLayout ()) 148 ,programLayout (inProgramLayout ) 149{ 150for (UInt i = 0 ;i < programLayout -> getEntryPointCount ();i ++ ) 151 { 152entryPointLayouts .add (new ShaderObjectLayoutImpl ( 153renderer , 154programLayout -> getSession (), 155programLayout -> getEntryPointByIndex (i )-> getTypeLayout ())); 156 } 157} 158 159int RootShaderObjectLayoutImpl ::getKernelIndex (UnownedStringSlice kernelName ) 160{ 161for (int i = 0 ;i < (int )programLayout -> getEntryPointCount ();i ++ ) 162 { 163auto entryPoint = programLayout -> getEntryPointByIndex (i ); 164if (kernelName == entryPoint -> getName ()) 165 { 166return i ; 167 } 168 } 169return -1 ; 170} 171 172void RootShaderObjectLayoutImpl ::getKernelThreadGroupSize (int kernelIndex ,UInt * threadGroupSizes ) 173{ 174auto entryPoint = programLayout -> getEntryPointByIndex (kernelIndex ); 175entryPoint -> getComputeThreadGroupSize (3 ,threadGroupSizes ); 176} 177 178}// namespace cuda 179#endif 180}// namespace gfx