yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
ac0dc491e
master
1// metal-helper-functions.h 2#pragma once 3#include "metal-base.h" 4 5namespace gfx 6{ 7using namespace Slang ; 8namespace metal 9{ 10 11/// A "simple" binding offset that records an offset in buffer/texture/sampler slots 12struct BindingOffset 13{ 14uint32_t buffer = 0 ; 15uint32_t texture = 0 ; 16uint32_t sampler = 0 ; 17 18/// Create a default (zero) offset 19BindingOffset ()= default ; 20 21/// Create an offset based on offset information in the given Slang `varLayout` 22BindingOffset (slang ::VariableLayoutReflection * varLayout ) 23 { 24if (varLayout ) 25 { 26buffer = (uint32_t )varLayout -> getOffset (SLANG_PARAMETER_CATEGORY_METAL_BUFFER ); 27texture = (uint32_t )varLayout -> getOffset (SLANG_PARAMETER_CATEGORY_METAL_TEXTURE ); 28sampler = (uint32_t )varLayout -> getOffset (SLANG_PARAMETER_CATEGORY_METAL_SAMPLER ); 29 } 30 } 31 32/// Create an offset based on size/stride information in the given Slang `typeLayout` 33BindingOffset (slang ::TypeLayoutReflection * typeLayout ) 34 { 35if (typeLayout ) 36 { 37buffer = (uint32_t )typeLayout -> getSize (SLANG_PARAMETER_CATEGORY_METAL_BUFFER ); 38texture = (uint32_t )typeLayout -> getSize (SLANG_PARAMETER_CATEGORY_METAL_TEXTURE ); 39sampler = (uint32_t )typeLayout -> getSize (SLANG_PARAMETER_CATEGORY_METAL_SAMPLER ); 40 } 41 } 42 43/// Add any values in the given `offset` 44void operator += (BindingOffset const & offset ) 45 { 46buffer += offset .buffer ; 47texture += offset .texture ; 48sampler += offset .sampler ; 49 } 50}; 51 52/// Contextual data and operations required when binding shader objects to the pipeline state 53struct BindingContext 54{ 55DeviceImpl * device = nullptr; 56virtual void setBuffer (MTL ::Buffer * buffer ,NS ::UInteger index )= 0 ; 57virtual void setTexture (MTL ::Texture * texture ,NS ::UInteger index )= 0 ; 58virtual void setSampler (MTL ::SamplerState * sampler ,NS ::UInteger index )= 0 ; 59virtual void useResources ( 60MTL ::Resource const ** resources , 61NS ::UInteger count , 62MTL ::ResourceUsage usage )= 0 ; 63}; 64 65struct ComputeBindingContext :public BindingContext 66{ 67MTL ::ComputeCommandEncoder * encoder ; 68 69Result init (DeviceImpl * device ,MTL ::ComputeCommandEncoder * encoder ) 70 { 71this -> device = device ; 72this -> encoder = encoder ; 73return SLANG_OK ; 74 } 75 76void setBuffer (MTL ::Buffer * buffer ,NS ::UInteger index )override 77 { 78encoder -> setBuffer (buffer ,0 ,index ); 79 } 80 81void setTexture (MTL ::Texture * texture ,NS ::UInteger index )override 82 { 83encoder -> setTexture (texture ,index ); 84 } 85 86void setSampler (MTL ::SamplerState * sampler ,NS ::UInteger index )override 87 { 88encoder -> setSamplerState (sampler ,index ); 89 } 90 91void useResources (MTL ::Resource const ** resources ,NS ::UInteger count ,MTL ::ResourceUsage usage ) 92override 93 { 94encoder -> useResources (resources ,count ,usage ); 95 } 96}; 97 98struct RenderBindingContext :public BindingContext 99{ 100MTL ::RenderCommandEncoder * encoder ; 101 102Result init (DeviceImpl * device ,MTL ::RenderCommandEncoder * encoder ) 103 { 104this -> device = device ; 105this -> encoder = encoder ; 106return SLANG_OK ; 107 } 108 109void setBuffer (MTL ::Buffer * buffer ,NS ::UInteger index )override 110 { 111encoder -> setVertexBuffer (buffer ,0 ,index ); 112encoder -> setFragmentBuffer (buffer ,0 ,index ); 113 } 114 115void setTexture (MTL ::Texture * texture ,NS ::UInteger index )override 116 { 117encoder -> setVertexTexture (texture ,index ); 118encoder -> setFragmentTexture (texture ,index ); 119 } 120 121void setSampler (MTL ::SamplerState * sampler ,NS ::UInteger index )override 122 { 123encoder -> setVertexSamplerState (sampler ,index ); 124encoder -> setFragmentSamplerState (sampler ,index ); 125 } 126 127void useResources (MTL ::Resource const ** resources ,NS ::UInteger count ,MTL ::ResourceUsage usage ) 128override 129 { 130encoder -> useResources (resources ,count ,usage ); 131 } 132}; 133 134}// namespace metal 135 136Result SLANG_MCALL getMetalAdapters (List < AdapterInfo >& outAdapters ); 137Result SLANG_MCALL createMetalDevice (const IDevice ::Desc * desc ,IDevice ** outRenderer ); 138 139}// namespace gfx