yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
04db5a956
master
1// d3d11-shader-object.h 2#pragma once 3#include "d3d11-base.h" 4#include "d3d11-buffer.h" 5#include "d3d11-helper-functions.h" 6#include "d3d11-resource-views.h" 7#include "d3d11-sampler.h" 8#include "d3d11-shader-object-layout.h" 9 10namespace gfx 11{ 12 13using namespace Slang ; 14 15namespace d3d11 16{ 17 18class ShaderObjectImpl 19 :public ShaderObjectBaseImpl < ShaderObjectImpl ,ShaderObjectLayoutImpl ,SimpleShaderObjectData > 20{ 21public : 22static Result create ( 23IDevice * device , 24ShaderObjectLayoutImpl * layout , 25ShaderObjectImpl ** outShaderObject ); 26 27RendererBase * getDevice () {return m_layout -> getDevice (); } 28 29SLANG_NO_THROW GfxCount SLANG_MCALL getEntryPointCount ()SLANG_OVERRIDE {return 0 ; } 30 31SLANG_NO_THROW Result SLANG_MCALL getEntryPoint (GfxIndex index ,IShaderObject ** outEntryPoint ) 32SLANG_OVERRIDE 33 { 34* outEntryPoint = nullptr ; 35return SLANG_OK ; 36 } 37 38virtual SLANG_NO_THROW const void * SLANG_MCALL getRawData ()override 39 { 40return m_data .getBuffer (); 41 } 42 43virtual SLANG_NO_THROW size_t SLANG_MCALL getSize ()override 44 { 45return (size_t )m_data .getCount (); 46 } 47 48SLANG_NO_THROW Result SLANG_MCALL 49setData (ShaderOffset const & inOffset ,void const * data ,size_t inSize )SLANG_OVERRIDE ; 50 51SLANG_NO_THROW Result SLANG_MCALL 52setResource (ShaderOffset const & offset ,IResourceView * resourceView )SLANG_OVERRIDE ; 53 54SLANG_NO_THROW Result SLANG_MCALL setSampler (ShaderOffset const & offset ,ISamplerState * sampler ) 55SLANG_OVERRIDE ; 56 57SLANG_NO_THROW Result SLANG_MCALL setCombinedTextureSampler ( 58ShaderOffset const & offset , 59IResourceView * textureView , 60ISamplerState * sampler )SLANG_OVERRIDE 61 { 62return SLANG_E_NOT_IMPLEMENTED ; 63 } 64 65public : 66protected : 67friend class ProgramVars ; 68 69Result init (IDevice * device ,ShaderObjectLayoutImpl * layout ); 70 71/// Write the uniform/ordinary data of this object into the given `dest` buffer at the given 72/// `offset` 73Result _writeOrdinaryData ( 74void * dest , 75size_t destSize , 76ShaderObjectLayoutImpl * specializedLayout ); 77 78/// Ensure that the `m_ordinaryDataBuffer` has been created, if it is needed 79/// 80/// The `specializedLayout` type must represent a specialized layout for this 81/// type that includes any "pending" data. 82/// 83Result _ensureOrdinaryDataBufferCreatedIfNeeded ( 84DeviceImpl * device , 85ShaderObjectLayoutImpl * specializedLayout ); 86 87/// Bind the buffer for ordinary/uniform data, if needed 88/// 89/// The `ioOffset` parameter will be updated to reflect the constant buffer 90/// register consumed by the ordinary data buffer, if one was bound. 91/// 92Result _bindOrdinaryDataBufferIfNeeded ( 93BindingContext * context , 94BindingOffset & ioOffset , 95ShaderObjectLayoutImpl * specializedLayout ); 96 97public : 98/// Bind this object as if it was declared as a `ConstantBuffer<T>` in Slang 99Result bindAsConstantBuffer ( 100BindingContext * context , 101BindingOffset const & inOffset , 102ShaderObjectLayoutImpl * specializedLayout ); 103 104/// Bind this object as a value that appears in the body of another object. 105/// 106/// This case is directly used when binding an object for an interface-type 107/// sub-object range when static specialization is used. It is also used 108/// indirectly when binding sub-objects to constant buffer or parameter 109/// block ranges. 110/// 111Result bindAsValue ( 112BindingContext * context , 113BindingOffset const & offset , 114ShaderObjectLayoutImpl * specializedLayout ); 115 116// Because the binding ranges have already been reflected 117// and organized as part of each shader object layout, 118// the object itself can store its data in a small number 119// of simple arrays. 120/// The shader resource views (SRVs) that are part of the state of this object 121List < RefPtr < ShaderResourceViewImpl >>m_srvs ; 122 123/// The unordered access views (UAVs) that are part of the state of this object 124List < RefPtr < UnorderedAccessViewImpl >>m_uavs ; 125 126/// The samplers that are part of the state of this object 127List < RefPtr < SamplerStateImpl >>m_samplers ; 128 129/// A constant buffer used to stored ordinary data for this object 130/// and existential-type sub-objects. 131/// 132/// Created on demand with `_createOrdinaryDataBufferIfNeeded()` 133RefPtr < BufferResourceImpl > m_ordinaryDataBuffer ; 134 135bool m_isConstantBufferDirty = true; 136 137/// Get the layout of this shader object with specialization arguments considered 138/// 139/// This operation should only be called after the shader object has been 140/// fully filled in and finalized. 141/// 142Result _getSpecializedLayout (ShaderObjectLayoutImpl ** outLayout ); 143 144/// Create the layout for this shader object with specialization arguments considered 145/// 146/// This operation is virtual so that it can be customized by `ProgramVars`. 147/// 148virtual Result _createSpecializedLayout (ShaderObjectLayoutImpl ** outLayout ); 149 150RefPtr < ShaderObjectLayoutImpl > m_specializedLayout ; 151}; 152 153class MutableShaderObjectImpl 154 :public MutableShaderObject < MutableShaderObjectImpl ,ShaderObjectLayoutImpl > 155{ 156}; 157 158class RootShaderObjectImpl :public ShaderObjectImpl 159{ 160typedef ShaderObjectImpl Super ; 161 162public : 163virtual SLANG_NO_THROW uint32_t SLANG_MCALL addRef ()override {return 1 ; } 164virtual SLANG_NO_THROW uint32_t SLANG_MCALL release ()override {return 1 ; } 165 166static Result create ( 167IDevice * device , 168RootShaderObjectLayoutImpl * layout , 169RootShaderObjectImpl ** outShaderObject ); 170 171RootShaderObjectLayoutImpl * getLayout () 172 { 173return static_cast < RootShaderObjectLayoutImpl *> (m_layout .Ptr ()); 174 } 175 176SLANG_NO_THROW GfxCount SLANG_MCALL getEntryPointCount ()SLANG_OVERRIDE 177 { 178return (GfxCount )m_entryPoints .getCount (); 179 } 180SLANG_NO_THROW SlangResult SLANG_MCALL 181getEntryPoint (GfxIndex index ,IShaderObject ** outEntryPoint )SLANG_OVERRIDE 182 { 183returnComPtr (outEntryPoint ,m_entryPoints [index ]); 184return SLANG_OK ; 185 } 186 187virtual Result collectSpecializationArgs (ExtendedShaderObjectTypeList & args )override ; 188 189/// Bind this object as a root shader object 190Result bindAsRoot (BindingContext * context ,RootShaderObjectLayoutImpl * specializedLayout ); 191 192 193protected : 194Result init (IDevice * device ,RootShaderObjectLayoutImpl * layout ); 195 196Result _createSpecializedLayout (ShaderObjectLayoutImpl ** outLayout )SLANG_OVERRIDE ; 197 198List < RefPtr < ShaderObjectImpl >>m_entryPoints ; 199}; 200 201}// namespace d3d11 202}// namespace gfx