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