yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
3822f9243
master
1#ifndef SLANG_TEST_SHADER_INPUT_LAYOUT_H 2#define SLANG_TEST_SHADER_INPUT_LAYOUT_H 3 4#include "core/slang-basic.h" 5#include "core/slang-random-generator.h" 6#include "core/slang-writer.h" 7 8#include <slang-rhi.h> 9 10namespace renderer_test 11{ 12 13using namespace rhi ; 14 15enum class ShaderInputType 16{ 17Buffer , 18Texture , 19Sampler , 20CombinedTextureSampler , 21Array , 22UniformData , 23Object , 24Aggregate , 25Specialize , 26AccelerationStructure , 27}; 28 29enum class InputTextureContent 30{ 31Zero , 32One , 33ChessBoard , 34Gradient 35}; 36 37enum InputTextureSampleCount 38{ 39One = 1 , 40Two = 2 , 41Four = 4 , 42Eight = 8 , 43Sixteen = 16 , 44ThirtyTwo = 32 , 45SixtyFour = 64 , 46}; 47struct InputTextureDesc 48{ 49int dimension = 2 ; 50int arrayLength = 0 ; 51bool isCube = false; 52bool isDepthTexture = false; 53bool isRWTexture = false; 54int size = 4 ; 55int mipMapCount = 0 ;///< 0 means the maximum number of mips will be bound 56 57InputTextureSampleCount sampleCount = InputTextureSampleCount ::One ; 58Format format = Format ::RGBA8Unorm ; 59 60InputTextureContent content = InputTextureContent ::One ; 61}; 62 63enum class InputBufferType 64{ 65// ConstantBuffer, 66StorageBuffer , 67// RootConstantBuffer, 68}; 69 70struct InputBufferDesc 71{ 72InputBufferType type = InputBufferType ::StorageBuffer ; 73int stride = 0 ;// stride == 0 indicates an unstructured buffer. 74int elementCount = 1 ; 75Format format = Format ::Undefined ; 76// For RWStructuredBuffer, AppendStructuredBuffer, ConsumeStructuredBuffer 77// the default value of 0xffffffff indicates that a counter buffer should 78// not be assigned 79uint32_t counter = ~0u ; 80}; 81 82struct InputSamplerDesc 83{ 84bool isCompareSampler = false; 85TextureFilteringMode filteringMode = TextureFilteringMode ::Linear ; 86}; 87 88struct TextureData 89{ 90struct Slice 91 { 92static Slice make (void * values ,size_t size ) 93 { 94Slice slice ; 95slice .values = values ; 96slice .valuesCount = size ; 97return slice ; 98 } 99 100void * values = nullptr;///< Values of the type format 101size_t valuesCount = 0 ; 102 }; 103 104void addSlice (const void * data ,size_t elemCount ) 105 { 106const size_t totalSize = m_formatSize * elemCount ; 107void * dst = ::malloc (totalSize ); 108 ::memcpy (dst ,data ,totalSize ); 109m_slices .add (Slice ::make (dst ,elemCount )); 110 } 111void * addSlice (size_t elemCount ) 112 { 113const size_t totalSize = m_formatSize * elemCount ; 114void * dst = ::malloc (totalSize ); 115m_slices .add (Slice ::make (dst ,elemCount )); 116return dst ; 117 } 118 119/// Set the size of the slice in count of format sized elements 120void * setSliceCount (Slang ::Index sliceIndex ,size_t count ) 121 { 122 auto& slice = m_slices [sliceIndex ]; 123if (count != slice .valuesCount ) 124 { 125slice .values = ::realloc (slice .values ,count * m_formatSize ); 126slice .valuesCount = count ; 127 } 128return slice .values ; 129 } 130 131void init (Format format ) 132 { 133clearSlices (); 134 135const FormatInfo & formatInfo = getFormatInfo (format ); 136m_formatSize = uint8_t (formatInfo .blockSizeInBytes /formatInfo .pixelsPerBlock ); 137m_format = format ; 138 } 139 140 ~TextureData () {clearSlices (); } 141 142void clearSlices () 143 { 144for (auto& slice :m_slices ) 145 { 146if (slice .values ) 147 { 148 ::free (slice .values ); 149 } 150 } 151m_slices .clear (); 152 } 153 154rhi ::Format m_format = rhi ::Format ::Undefined ; 155uint8_t m_formatSize = 0 ; 156 157Slang ::List < Slice > m_slices ; 158int m_textureSize = 0 ; 159int m_mipLevels = 1 ; 160int m_arraySize = 1 ; 161}; 162 163class ShaderInputLayout 164{ 165public : 166class Val :public Slang ::RefObject 167 { 168public : 169Val (ShaderInputType kind ) 170 :kind (kind ) 171 { 172 } 173 174ShaderInputType kind ; 175bool isOutput = false; 176 }; 177typedef Slang ::RefPtr < Val > ValPtr ; 178 179class TextureVal :public Val 180 { 181public : 182TextureVal () 183 :Val (ShaderInputType ::Texture ) 184 { 185 } 186 187InputTextureDesc textureDesc ; 188 }; 189 190class DataValBase :public Val 191 { 192public : 193DataValBase (ShaderInputType kind ) 194 :Val (kind ) 195 { 196 } 197 198Slang ::List < unsigned int > bufferData ; 199 }; 200 201class BufferVal :public DataValBase 202 { 203public : 204BufferVal () 205 :DataValBase (ShaderInputType ::Buffer ) 206 { 207 } 208 209InputBufferDesc bufferDesc ; 210 }; 211 212class DataVal :public DataValBase 213 { 214public : 215DataVal () 216 :DataValBase (ShaderInputType ::UniformData ) 217 { 218 } 219 }; 220 221class SamplerVal :public Val 222 { 223public : 224SamplerVal () 225 :Val (ShaderInputType ::Sampler ) 226 { 227 } 228 229InputSamplerDesc samplerDesc ; 230 }; 231 232class CombinedTextureSamplerVal :public Val 233 { 234public : 235CombinedTextureSamplerVal () 236 :Val (ShaderInputType ::CombinedTextureSampler ) 237 { 238 } 239 240Slang ::RefPtr < TextureVal > textureVal ; 241Slang ::RefPtr < SamplerVal > samplerVal ; 242 }; 243 244class AccelerationStructureVal :public Val 245 { 246public : 247AccelerationStructureVal () 248 :Val (ShaderInputType ::AccelerationStructure ) 249 { 250 } 251 }; 252 253struct Field 254 { 255Slang ::String name ; 256ValPtr val ; 257 }; 258typedef Field Entry ; 259 260class ParentVal :public Val 261 { 262public : 263ParentVal (ShaderInputType kind ) 264 :Val (kind ) 265 { 266 } 267 268virtual void addField (Field const & field )= 0 ; 269 }; 270 271class AggVal :public ParentVal 272 { 273public : 274AggVal (ShaderInputType kind = ShaderInputType ::Aggregate ) 275 :ParentVal (kind ) 276 { 277 } 278 279Slang ::List < Field > fields ; 280 281virtual void addField (Field const & field )override ; 282 }; 283 284class ObjectVal :public Val 285 { 286public : 287ObjectVal () 288 :Val (ShaderInputType ::Object ) 289 { 290 } 291 292Slang ::String typeName ; 293ValPtr contentVal ; 294 }; 295 296class SpecializeVal :public Val 297 { 298public : 299ValPtr contentVal ; 300Slang ::List < Slang ::String > typeArgs ; 301SpecializeVal () 302 :Val (ShaderInputType ::Specialize ) 303 { 304 } 305 }; 306 307class ArrayVal :public ParentVal 308 { 309public : 310ArrayVal () 311 :ParentVal (ShaderInputType ::Array ) 312 { 313 } 314 315Slang ::List < ValPtr > vals ; 316 317virtual void addField (Field const & field )override ; 318 }; 319 320Slang ::RefPtr < AggVal > rootVal ; 321Slang ::List < Slang ::String > globalSpecializationArgs ; 322Slang ::List < Slang ::String > entryPointSpecializationArgs ; 323 324class TypeConformanceVal 325 { 326public : 327Slang ::String derivedTypeName ; 328Slang ::String baseTypeName ; 329Slang ::Int idOverride = -1 ; 330 }; 331Slang ::List < TypeConformanceVal > typeConformances ; 332 333int numRenderTargets = 1 ; 334 335Slang ::Index findEntryIndexByName (const Slang ::String & name )const ; 336 337void parse (Slang ::RandomGenerator * rand ,const char * source ); 338 339/// Writes a binding, if bindRoot is set, will try to honor the underlying type when outputting. 340/// If not will dump as uint32_t hex. 341static SlangResult writeBinding ( 342slang ::TypeLayoutReflection * typeLayout , 343const void * data , 344size_t sizeInBytes , 345Slang ::WriterHelper writer ); 346}; 347 348void generateTextureDataRGB8 (TextureData & output ,const InputTextureDesc & desc ); 349void generateTextureData (TextureData & output ,const InputTextureDesc & desc ); 350 351 352}// namespace renderer_test 353 354#endif