yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
e4611e2e3
master
1#ifndef SLANG_PRELUDE_CPP_TYPES_H 2#define SLANG_PRELUDE_CPP_TYPES_H 3 4#ifdef SLANG_PRELUDE_NAMESPACE 5namespace SLANG_PRELUDE_NAMESPACE 6{ 7#endif 8 9#ifndef SLANG_FORCE_INLINE 10#define SLANG_FORCE_INLINE inline 11#endif 12 13#include "slang-cpp-types-core.h" 14 15typedef Vector < float ,2 > float2 ; 16typedef Vector < float ,3 > float3 ; 17typedef Vector < float ,4 > float4 ; 18 19typedef Vector < int32_t ,2 > int2 ; 20typedef Vector < int32_t ,3 > int3 ; 21typedef Vector < int32_t ,4 > int4 ; 22 23typedef Vector < uint32_t ,2 > uint2 ; 24typedef Vector < uint32_t ,3 > uint3 ; 25typedef Vector < uint32_t ,4 > uint4 ; 26 27// We can just map `NonUniformResourceIndex` type directly to the index type on CPU, as CPU does not 28// require any special handling around such accesses. 29typedef size_t NonUniformResourceIndex ; 30 31// ----------------------------- ResourceType ----------------------------------------- 32 33// https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-structuredbuffer-getdimensions 34// Missing Load(_In_ int Location, _Out_ uint Status); 35 36template < typename T > 37struct RWStructuredBuffer 38{ 39SLANG_FORCE_INLINE T & operator [](size_t index )const 40 { 41SLANG_BOUND_CHECK (index ,count ); 42return data [index ]; 43 } 44const T & Load (size_t index )const 45 { 46SLANG_BOUND_CHECK (index ,count ); 47return data [index ]; 48 } 49void GetDimensions (uint32_t * outNumStructs ,uint32_t * outStride ) 50 { 51* outNumStructs = uint32_t (count ); 52* outStride = uint32_t (sizeof (T )); 53 } 54 55T * data ; 56size_t count ; 57}; 58 59template < typename T > 60struct StructuredBuffer 61{ 62SLANG_FORCE_INLINE T & operator [](size_t index )const 63 { 64SLANG_BOUND_CHECK (index ,count ); 65return data [index ]; 66 } 67T & Load (size_t index )const 68 { 69SLANG_BOUND_CHECK (index ,count ); 70return data [index ]; 71 } 72void GetDimensions (uint32_t * outNumStructs ,uint32_t * outStride ) 73 { 74* outNumStructs = uint32_t (count ); 75* outStride = uint32_t (sizeof (T )); 76 } 77 78T * data ; 79size_t count ; 80}; 81 82 83template < typename T > 84struct RWBuffer 85{ 86SLANG_FORCE_INLINE T & operator [](size_t index )const 87 { 88SLANG_BOUND_CHECK (index ,count ); 89return data [index ]; 90 } 91const T & Load (size_t index )const 92 { 93SLANG_BOUND_CHECK (index ,count ); 94return data [index ]; 95 } 96void GetDimensions (uint32_t * outCount ) {* outCount = uint32_t (count ); } 97 98T * data ; 99size_t count ; 100}; 101 102template < typename T > 103struct Buffer 104{ 105SLANG_FORCE_INLINE const T & operator [](size_t index )const 106 { 107SLANG_BOUND_CHECK (index ,count ); 108return data [index ]; 109 } 110const T & Load (size_t index )const 111 { 112SLANG_BOUND_CHECK (index ,count ); 113return data [index ]; 114 } 115void GetDimensions (uint32_t * outCount ) {* outCount = uint32_t (count ); } 116 117T * data ; 118size_t count ; 119}; 120 121// Missing Load(_In_ int Location, _Out_ uint Status); 122struct ByteAddressBuffer 123{ 124void GetDimensions (uint32_t * outDim )const {* outDim = uint32_t (sizeInBytes ); } 125uint32_t Load (size_t index )const 126 { 127SLANG_BOUND_CHECK_BYTE_ADDRESS (index ,4 ,sizeInBytes ); 128return data [index >>2 ]; 129 } 130uint2 Load2 (size_t index )const 131 { 132SLANG_BOUND_CHECK_BYTE_ADDRESS (index ,8 ,sizeInBytes ); 133const size_t dataIdx = index >>2 ; 134return uint2 {data [dataIdx ],data [dataIdx + 1 ]}; 135 } 136uint3 Load3 (size_t index )const 137 { 138SLANG_BOUND_CHECK_BYTE_ADDRESS (index ,12 ,sizeInBytes ); 139const size_t dataIdx = index >>2 ; 140return uint3 {data [dataIdx ],data [dataIdx + 1 ],data [dataIdx + 2 ]}; 141 } 142uint4 Load4 (size_t index )const 143 { 144SLANG_BOUND_CHECK_BYTE_ADDRESS (index ,16 ,sizeInBytes ); 145const size_t dataIdx = index >>2 ; 146return uint4 {data [dataIdx ],data [dataIdx + 1 ],data [dataIdx + 2 ],data [dataIdx + 3 ]}; 147 } 148template < typename T > 149T Load (size_t index )const 150 { 151SLANG_BOUND_CHECK_BYTE_ADDRESS (index ,sizeof (T ),sizeInBytes ); 152return * (const T * )(((const char * )data )+ index ); 153 } 154 155const uint32_t * data ; 156size_t sizeInBytes ;//< Must be multiple of 4 157}; 158 159// https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-rwbyteaddressbuffer 160// Missing support for Atomic operations 161// Missing support for Load with status 162struct RWByteAddressBuffer 163{ 164void GetDimensions (uint32_t * outDim )const {* outDim = uint32_t (sizeInBytes ); } 165 166uint32_t Load (size_t index )const 167 { 168SLANG_BOUND_CHECK_BYTE_ADDRESS (index ,4 ,sizeInBytes ); 169return data [index >>2 ]; 170 } 171uint2 Load2 (size_t index )const 172 { 173SLANG_BOUND_CHECK_BYTE_ADDRESS (index ,8 ,sizeInBytes ); 174const size_t dataIdx = index >>2 ; 175return uint2 {data [dataIdx ],data [dataIdx + 1 ]}; 176 } 177uint3 Load3 (size_t index )const 178 { 179SLANG_BOUND_CHECK_BYTE_ADDRESS (index ,12 ,sizeInBytes ); 180const size_t dataIdx = index >>2 ; 181return uint3 {data [dataIdx ],data [dataIdx + 1 ],data [dataIdx + 2 ]}; 182 } 183uint4 Load4 (size_t index )const 184 { 185SLANG_BOUND_CHECK_BYTE_ADDRESS (index ,16 ,sizeInBytes ); 186const size_t dataIdx = index >>2 ; 187return uint4 {data [dataIdx ],data [dataIdx + 1 ],data [dataIdx + 2 ],data [dataIdx + 3 ]}; 188 } 189template < typename T > 190T Load (size_t index )const 191 { 192SLANG_BOUND_CHECK_BYTE_ADDRESS (index ,sizeof (T ),sizeInBytes ); 193return * (const T * )(((const char * )data )+ index ); 194 } 195 196void Store (size_t index ,uint32_t v )const 197 { 198SLANG_BOUND_CHECK_BYTE_ADDRESS (index ,4 ,sizeInBytes ); 199data [index >>2 ]= v ; 200 } 201void Store2 (size_t index ,uint2 v )const 202 { 203SLANG_BOUND_CHECK_BYTE_ADDRESS (index ,8 ,sizeInBytes ); 204const size_t dataIdx = index >>2 ; 205data [dataIdx + 0 ]= v .x ; 206data [dataIdx + 1 ]= v .y ; 207 } 208void Store3 (size_t index ,uint3 v )const 209 { 210SLANG_BOUND_CHECK_BYTE_ADDRESS (index ,12 ,sizeInBytes ); 211const size_t dataIdx = index >>2 ; 212data [dataIdx + 0 ]= v .x ; 213data [dataIdx + 1 ]= v .y ; 214data [dataIdx + 2 ]= v .z ; 215 } 216void Store4 (size_t index ,uint4 v )const 217 { 218SLANG_BOUND_CHECK_BYTE_ADDRESS (index ,16 ,sizeInBytes ); 219const size_t dataIdx = index >>2 ; 220data [dataIdx + 0 ]= v .x ; 221data [dataIdx + 1 ]= v .y ; 222data [dataIdx + 2 ]= v .z ; 223data [dataIdx + 3 ]= v .w ; 224 } 225template < typename T > 226void Store (size_t index ,T const & value )const 227 { 228SLANG_BOUND_CHECK_BYTE_ADDRESS (index ,sizeof (T ),sizeInBytes ); 229* (T * )(((char * )data )+ index )= value ; 230 } 231 232uint32_t * data ; 233size_t sizeInBytes ;//< Must be multiple of 4 234}; 235 236struct ISamplerState ; 237struct ISamplerComparisonState ; 238 239struct SamplerState 240{ 241ISamplerState * state ; 242}; 243 244struct SamplerComparisonState 245{ 246ISamplerComparisonState * state ; 247}; 248 249#ifndef SLANG_RESOURCE_SHAPE 250#define SLANG_RESOURCE_SHAPE 251typedef unsigned int SlangResourceShape ; 252enum 253{ 254SLANG_RESOURCE_BASE_SHAPE_MASK = 0x0F , 255 256SLANG_RESOURCE_NONE = 0x00 , 257 258SLANG_TEXTURE_1D = 0x01 , 259SLANG_TEXTURE_2D = 0x02 , 260SLANG_TEXTURE_3D = 0x03 , 261SLANG_TEXTURE_CUBE = 0x04 , 262SLANG_TEXTURE_BUFFER = 0x05 , 263 264SLANG_STRUCTURED_BUFFER = 0x06 , 265SLANG_BYTE_ADDRESS_BUFFER = 0x07 , 266SLANG_RESOURCE_UNKNOWN = 0x08 , 267SLANG_ACCELERATION_STRUCTURE = 0x09 , 268SLANG_TEXTURE_SUBPASS = 0x0A , 269 270SLANG_RESOURCE_EXT_SHAPE_MASK = 0xF0 , 271 272SLANG_TEXTURE_FEEDBACK_FLAG = 0x10 , 273SLANG_TEXTURE_ARRAY_FLAG = 0x40 , 274SLANG_TEXTURE_MULTISAMPLE_FLAG = 0x80 , 275 276SLANG_TEXTURE_1D_ARRAY = SLANG_TEXTURE_1D |SLANG_TEXTURE_ARRAY_FLAG , 277SLANG_TEXTURE_2D_ARRAY = SLANG_TEXTURE_2D |SLANG_TEXTURE_ARRAY_FLAG , 278SLANG_TEXTURE_CUBE_ARRAY = SLANG_TEXTURE_CUBE |SLANG_TEXTURE_ARRAY_FLAG , 279 280SLANG_TEXTURE_2D_MULTISAMPLE = SLANG_TEXTURE_2D |SLANG_TEXTURE_MULTISAMPLE_FLAG , 281SLANG_TEXTURE_2D_MULTISAMPLE_ARRAY = 282SLANG_TEXTURE_2D |SLANG_TEXTURE_MULTISAMPLE_FLAG |SLANG_TEXTURE_ARRAY_FLAG , 283SLANG_TEXTURE_SUBPASS_MULTISAMPLE = SLANG_TEXTURE_SUBPASS |SLANG_TEXTURE_MULTISAMPLE_FLAG , 284}; 285#endif 286 287// 288struct TextureDimensions 289{ 290void reset () 291 { 292shape = 0 ; 293width = height = depth = 0 ; 294numberOfLevels = 0 ; 295arrayElementCount = 0 ; 296 } 297int getDimSizes (uint32_t outDims [4 ])const 298 { 299const autobaseShape = (shape & SLANG_RESOURCE_BASE_SHAPE_MASK ); 300int count = 0 ; 301switch (baseShape) 302{ 303case SLANG_TEXTURE_1D : 304{ 305outDims[count ++ ] = width; 306break ; 307} 308case SLANG_TEXTURE_2D : 309{ 310outDims[count ++ ] = width; 311outDims[count ++ ] = height; 312break ; 313} 314case SLANG_TEXTURE_3D : 315{ 316outDims[count ++ ] = width; 317outDims[count ++ ] = height; 318outDims[count ++ ] = depth; 319break ; 320} 321case SLANG_TEXTURE_CUBE : 322{ 323outDims[count ++ ] = width; 324outDims[count ++ ] = height; 325outDims[count ++ ] = 6 ; 326break ; 327} 328} 329 330if (shape & SLANG_TEXTURE_ARRAY_FLAG ) 331{ 332outDims[count ++ ] = arrayElementCount; 333} 334return count; 335} 336int getMIPDims ( int outDims[ 3 ]) const 337{ 338const auto baseShape = (shape & SLANG_RESOURCE_BASE_SHAPE_MASK ); 339int count = 0 ; 340switch (baseShape) 341{ 342case SLANG_TEXTURE_1D : 343{ 344outDims[count ++ ] = width; 345break ; 346} 347case SLANG_TEXTURE_CUBE : 348case SLANG_TEXTURE_2D : 349{ 350outDims[count ++ ] = width; 351outDims[count ++ ] = height; 352break ; 353} 354case SLANG_TEXTURE_3D : 355{ 356outDims[count ++ ] = width; 357outDims[count ++ ] = height; 358outDims[count ++ ] = depth; 359break ; 360} 361} 362return count; 363} 364int calcMaxMIPLevels () const 365{ 366int dims[ 3 ]; 367const int dimCount = getMIPDims (dims); 368for ( int count = 1 ; true ; count ++ ) 369{ 370bool allOne = true; 371for ( int i = 0 ; i < dimCount; ++ i) 372{ 373if (dims[i] > 1 ) 374{ 375allOne = false; 376dims[i] >>= 1 ; 377} 378} 379if (allOne) 380{ 381return count; 382} 383} 384} 385 386uint32_t shape; 387uint32_t width, height, depth; 388uint32_t numberOfLevels; 389uint32_t arrayElementCount; ///< For array types, 0 otherwise 390}; 391 392 393// Texture 394 395struct ITexture 396{ 397virtual TextureDimensions GetDimensions ( int mipLevel = -1 ) = 0 ; 398virtual void Load ( const int32_t * v, void * outData, size_t dataSize) = 0 ; 399virtual void Sample ( 400SamplerState samplerState, 401const float * loc, 402void * outData, 403size_t dataSize) = 0 ; 404virtual void SampleLevel ( 405SamplerState samplerState, 406const float * loc, 407float level, 408void * outData, 409size_t dataSize) = 0 ; 410}; 411 412template < typename T > 413struct Texture1D 414{ 415void GetDimensions ( uint32_t * outWidth) { * outWidth = texture -> GetDimensions (). width ; } 416void GetDimensions ( uint32_t mipLevel, uint32_t * outWidth, uint32_t * outNumberOfLevels) 417{ 418auto dims = texture -> GetDimensions (mipLevel); 419* outWidth = dims. width ; 420* outNumberOfLevels = dims. numberOfLevels ; 421} 422 423void GetDimensions ( float * outWidth) { * outWidth = texture -> GetDimensions (). width ; } 424void GetDimensions ( uint32_t mipLevel, float * outWidth, float * outNumberOfLevels) 425{ 426auto dims = texture -> GetDimensions (mipLevel); 427* outWidth = dims. width ; 428* outNumberOfLevels = dims. numberOfLevels ; 429} 430 431T Load ( const int2 & loc) const 432{ 433T out; 434texture -> Load ( & loc. x , & out, sizeof (out)); 435return out; 436} 437T Sample ( SamplerState samplerState, float loc) const 438{ 439T out; 440texture -> Sample (samplerState, & loc, & out, sizeof (out)); 441return out; 442} 443T SampleLevel ( SamplerState samplerState, float loc, float level) const 444{ 445T out; 446texture -> SampleLevel (samplerState, & loc, level, & out, sizeof (out)); 447return out; 448} 449 450ITexture * texture; 451}; 452 453template < typename T > 454struct Texture2D 455{ 456void GetDimensions ( uint32_t * outWidth, uint32_t * outHeight) 457{ 458const auto dims = texture -> GetDimensions (); 459* outWidth = dims. width ; 460* outHeight = dims. height ; 461} 462void GetDimensions ( 463uint32_t mipLevel, 464uint32_t * outWidth, 465uint32_t * outHeight, 466uint32_t * outNumberOfLevels) 467{ 468const auto dims = texture -> GetDimensions (mipLevel); 469* outWidth = dims. width ; 470* outHeight = dims. height ; 471* outNumberOfLevels = dims. numberOfLevels ; 472} 473void GetDimensions ( float * outWidth, float * outHeight) 474{ 475const auto dims = texture -> GetDimensions (); 476* outWidth = dims. width ; 477* outHeight = dims. height ; 478} 479void GetDimensions ( 480uint32_t mipLevel, 481float * outWidth, 482float * outHeight, 483float * outNumberOfLevels) 484{ 485const auto dims = texture -> GetDimensions (mipLevel); 486* outWidth = dims. width ; 487* outHeight = dims. height ; 488* outNumberOfLevels = dims. numberOfLevels ; 489} 490 491T Load ( const int3 & loc) const 492{ 493T out; 494texture -> Load ( & loc. x , & out, sizeof (out)); 495return out; 496} 497T Sample ( SamplerState samplerState, const float2 & loc) const 498{ 499T out; 500texture -> Sample (samplerState, & loc. x , & out, sizeof (out)); 501return out; 502} 503T SampleLevel ( SamplerState samplerState, const float2 & loc, float level) const 504{ 505T out; 506texture -> SampleLevel (samplerState, & loc. x , level, & out, sizeof (out)); 507return out; 508} 509 510ITexture * texture; 511}; 512 513template < typename T > 514struct Texture3D 515{ 516void GetDimensions ( uint32_t * outWidth, uint32_t * outHeight, uint32_t * outDepth) 517{ 518const auto dims = texture -> GetDimensions (); 519* outWidth = dims. width ; 520* outHeight = dims. height ; 521* outDepth = dims. depth ; 522} 523void GetDimensions ( 524uint32_t mipLevel, 525uint32_t * outWidth, 526uint32_t * outHeight, 527uint32_t * outDepth, 528uint32_t * outNumberOfLevels) 529{ 530const auto dims = texture -> GetDimensions (mipLevel); 531* outWidth = dims. width ; 532* outHeight = dims. height ; 533* outDepth = dims. depth ; 534* outNumberOfLevels = dims. numberOfLevels ; 535} 536void GetDimensions ( float * outWidth, float * outHeight, float * outDepth) 537{ 538const auto dims = texture -> GetDimensions (); 539* outWidth = dims. width ; 540* outHeight = dims. height ; 541* outDepth = dims. depth ; 542} 543void GetDimensions ( 544uint32_t mipLevel, 545float * outWidth, 546float * outHeight, 547float * outDepth, 548float * outNumberOfLevels) 549{ 550const auto dims = texture -> GetDimensions (mipLevel); 551* outWidth = dims. width ; 552* outHeight = dims. height ; 553* outDepth = dims. depth ; 554* outNumberOfLevels = dims. numberOfLevels ; 555} 556 557T Load ( const int4 & loc) const 558{ 559T out; 560texture -> Load ( & loc. x , & out, sizeof (out)); 561return out; 562} 563T Sample ( SamplerState samplerState, const float3 & loc) const 564{ 565T out; 566texture -> Sample (samplerState, & loc. x , & out, sizeof (out)); 567return out; 568} 569T SampleLevel ( SamplerState samplerState, const float3 & loc, float level) const 570{ 571T out; 572texture -> SampleLevel (samplerState, & loc. x , level, & out, sizeof (out)); 573return out; 574} 575 576ITexture * texture; 577}; 578 579template < typename T > 580struct TextureCube 581{ 582void GetDimensions ( uint32_t * outWidth, uint32_t * outHeight) 583{ 584const auto dims = texture -> GetDimensions (); 585* outWidth = dims. width ; 586* outHeight = dims. height ; 587} 588void GetDimensions ( 589uint32_t mipLevel, 590uint32_t * outWidth, 591uint32_t * outHeight, 592uint32_t * outNumberOfLevels) 593{ 594const auto dims = texture -> GetDimensions (mipLevel); 595* outWidth = dims. width ; 596* outHeight = dims. height ; 597* outNumberOfLevels = dims. numberOfLevels ; 598} 599void GetDimensions ( float * outWidth, float * outHeight) 600{ 601const auto dims = texture -> GetDimensions (); 602* outWidth = dims. width ; 603* outHeight = dims. height ; 604} 605void GetDimensions ( 606uint32_t mipLevel, 607float * outWidth, 608float * outHeight, 609float * outNumberOfLevels) 610{ 611const auto dims = texture -> GetDimensions (mipLevel); 612* outWidth = dims. width ; 613* outHeight = dims. height ; 614* outNumberOfLevels = dims. numberOfLevels ; 615} 616 617T Sample ( SamplerState samplerState, const float3 & loc) const 618{ 619T out; 620texture -> Sample (samplerState, & loc. x , & out, sizeof (out)); 621return out; 622} 623T SampleLevel ( SamplerState samplerState, const float3 & loc, float level) const 624{ 625T out; 626texture -> SampleLevel (samplerState, & loc. x , level, & out, sizeof (out)); 627return out; 628} 629 630ITexture * texture; 631}; 632 633template < typename T > 634struct Texture1DArray 635{ 636void GetDimensions ( uint32_t * outWidth, uint32_t * outElements) 637{ 638auto dims = texture -> GetDimensions (); 639* outWidth = dims. width ; 640* outElements = dims. arrayElementCount ; 641} 642void GetDimensions ( 643uint32_t mipLevel, 644uint32_t * outWidth, 645uint32_t * outElements, 646uint32_t * outNumberOfLevels) 647{ 648auto dims = texture -> GetDimensions (mipLevel); 649* outWidth = dims. width ; 650* outNumberOfLevels = dims. numberOfLevels ; 651* outElements = dims. arrayElementCount ; 652} 653void GetDimensions ( float * outWidth, float * outElements) 654{ 655auto dims = texture -> GetDimensions (); 656* outWidth = dims. width ; 657* outElements = dims. arrayElementCount ; 658} 659void GetDimensions ( 660uint32_t mipLevel, 661float * outWidth, 662float * outElements, 663float * outNumberOfLevels) 664{ 665auto dims = texture -> GetDimensions (mipLevel); 666* outWidth = dims. width ; 667* outNumberOfLevels = dims. numberOfLevels ; 668* outElements = dims. arrayElementCount ; 669} 670 671T Load ( const int3 & loc) const 672{ 673T out; 674texture -> Load ( & loc. x , & out, sizeof (out)); 675return out; 676} 677T Sample ( SamplerState samplerState, const float2 & loc) const 678{ 679T out; 680texture -> Sample (samplerState, & loc. x , & out, sizeof (out)); 681return out; 682} 683T SampleLevel ( SamplerState samplerState, const float2 & loc, float level) const 684{ 685T out; 686texture -> SampleLevel (samplerState, & loc. x , level, & out, sizeof (out)); 687return out; 688} 689 690ITexture * texture; 691}; 692 693template < typename T > 694struct Texture2DArray 695{ 696void GetDimensions ( uint32_t * outWidth, uint32_t * outHeight, uint32_t * outElements) 697{ 698auto dims = texture -> GetDimensions (); 699* outWidth = dims. width ; 700* outHeight = dims. height ; 701* outElements = dims. arrayElementCount ; 702} 703void GetDimensions ( 704uint32_t mipLevel, 705uint32_t * outWidth, 706uint32_t * outHeight, 707uint32_t * outElements, 708uint32_t * outNumberOfLevels) 709{ 710auto dims = texture -> GetDimensions (mipLevel); 711* outWidth = dims. width ; 712* outHeight = dims. height ; 713* outElements = dims. arrayElementCount ; 714* outNumberOfLevels = dims. numberOfLevels ; 715} 716 717void GetDimensions ( uint32_t * outWidth, float * outHeight, float * outElements) 718{ 719auto dims = texture -> GetDimensions (); 720* outWidth = dims. width ; 721* outHeight = dims. height ; 722* outElements = dims. arrayElementCount ; 723} 724void GetDimensions ( 725uint32_t mipLevel, 726float * outWidth, 727float * outHeight, 728float * outElements, 729float * outNumberOfLevels) 730{ 731auto dims = texture -> GetDimensions (mipLevel); 732* outWidth = dims. width ; 733* outHeight = dims. height ; 734* outElements = dims. arrayElementCount ; 735* outNumberOfLevels = dims. numberOfLevels ; 736} 737 738T Load ( const int4 & loc) const 739{ 740T out; 741texture -> Load ( & loc. x , & out, sizeof (out)); 742return out; 743} 744T Sample ( SamplerState samplerState, const float3 & loc) const 745{ 746T out; 747texture -> Sample (samplerState, & loc. x , & out, sizeof (out)); 748return out; 749} 750T SampleLevel ( SamplerState samplerState, const float3 & loc, float level) const 751{ 752T out; 753texture -> SampleLevel (samplerState, & loc. x , level, & out, sizeof (out)); 754return out; 755} 756 757ITexture * texture; 758}; 759 760template < typename T > 761struct TextureCubeArray 762{ 763void GetDimensions ( uint32_t * outWidth, uint32_t * outHeight, uint32_t * outElements) 764{ 765auto dims = texture -> GetDimensions (); 766* outWidth = dims. width ; 767* outHeight = dims. height ; 768* outElements = dims. arrayElementCount ; 769} 770void GetDimensions ( 771uint32_t mipLevel, 772uint32_t * outWidth, 773uint32_t * outHeight, 774uint32_t * outElements, 775uint32_t * outNumberOfLevels) 776{ 777auto dims = texture -> GetDimensions (mipLevel); 778* outWidth = dims. width ; 779* outHeight = dims. height ; 780* outElements = dims. arrayElementCount ; 781* outNumberOfLevels = dims. numberOfLevels ; 782} 783 784void GetDimensions ( uint32_t * outWidth, float * outHeight, float * outElements) 785{ 786auto dims = texture -> GetDimensions (); 787* outWidth = dims. width ; 788* outHeight = dims. height ; 789* outElements = dims. arrayElementCount ; 790} 791void GetDimensions ( 792uint32_t mipLevel, 793float * outWidth, 794float * outHeight, 795float * outElements, 796float * outNumberOfLevels) 797{ 798auto dims = texture -> GetDimensions (mipLevel); 799* outWidth = dims. width ; 800* outHeight = dims. height ; 801* outElements = dims. arrayElementCount ; 802* outNumberOfLevels = dims. numberOfLevels ; 803} 804 805T Sample ( SamplerState samplerState, const float4 & loc) const 806{ 807T out; 808texture -> Sample (samplerState, & loc. x , & out, sizeof (out)); 809return out; 810} 811T SampleLevel ( SamplerState samplerState, const float4 & loc, float level) const 812{ 813T out; 814texture -> SampleLevel (samplerState, & loc. x , level, & out, sizeof (out)); 815return out; 816} 817 818ITexture * texture; 819}; 820 821/* !!!!!!!!!!!!!!!!!!!!!!!!!!! RWTexture !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ 822 823struct IRWTexture : ITexture 824{ 825/// Get the reference to the element at loc. 826virtual void * refAt ( const uint32_t * loc) = 0 ; 827}; 828 829template < typename T > 830struct RWTexture1D 831{ 832void GetDimensions ( uint32_t * outWidth) { * outWidth = texture -> GetDimensions (). width ; } 833void GetDimensions ( uint32_t mipLevel, uint32_t * outWidth, uint32_t * outNumberOfLevels) 834{ 835auto dims = texture -> GetDimensions (mipLevel); 836* outWidth = dims. width ; 837* outNumberOfLevels = dims. numberOfLevels ; 838} 839 840void GetDimensions ( float * outWidth) { * outWidth = texture -> GetDimensions (). width ; } 841void GetDimensions ( uint32_t mipLevel, float * outWidth, float * outNumberOfLevels) 842{ 843auto dims = texture -> GetDimensions (mipLevel); 844* outWidth = dims. width ; 845* outNumberOfLevels = dims. numberOfLevels ; 846} 847 848T Load ( int32_t loc) const 849{ 850T out; 851texture -> Load ( & loc, & out, sizeof (out)); 852return out; 853} 854T & operator[](uint32_t loc) { return * ( T * )texture -> refAt ( & loc); } 855IRWTexture * texture; 856}; 857 858template < typename T > 859struct RWTexture2D 860{ 861void GetDimensions ( uint32_t * outWidth, uint32_t * outHeight) 862{ 863const auto dims = texture -> GetDimensions (); 864* outWidth = dims. width ; 865* outHeight = dims. height ; 866} 867void GetDimensions ( 868uint32_t mipLevel, 869uint32_t * outWidth, 870uint32_t * outHeight, 871uint32_t * outNumberOfLevels) 872{ 873const auto dims = texture -> GetDimensions (mipLevel); 874* outWidth = dims. width ; 875* outHeight = dims. height ; 876* outNumberOfLevels = dims. numberOfLevels ; 877} 878void GetDimensions ( float * outWidth, float * outHeight) 879{ 880const auto dims = texture -> GetDimensions (); 881* outWidth = dims. width ; 882* outHeight = dims. height ; 883} 884void GetDimensions ( 885uint32_t mipLevel, 886float * outWidth, 887float * outHeight, 888float * outNumberOfLevels) 889{ 890const auto dims = texture -> GetDimensions (mipLevel); 891* outWidth = dims. width ; 892* outHeight = dims. height ; 893* outNumberOfLevels = dims. numberOfLevels ; 894} 895 896T Load ( const int2 & loc) const 897{ 898T out; 899texture -> Load ( & loc. x , & out, sizeof (out)); 900return out; 901} 902T & operator[](const uint2 & loc) { return * ( T * )texture -> refAt ( & loc. x ); } 903IRWTexture * texture; 904}; 905 906template < typename T > 907struct RWTexture3D 908{ 909void GetDimensions ( uint32_t * outWidth, uint32_t * outHeight, uint32_t * outDepth) 910{ 911const auto dims = texture -> GetDimensions (); 912* outWidth = dims. width ; 913* outHeight = dims. height ; 914* outDepth = dims. depth ; 915} 916void GetDimensions ( 917uint32_t mipLevel, 918uint32_t * outWidth, 919uint32_t * outHeight, 920uint32_t * outDepth, 921uint32_t * outNumberOfLevels) 922{ 923const auto dims = texture -> GetDimensions (mipLevel); 924* outWidth = dims. width ; 925* outHeight = dims. height ; 926* outDepth = dims. depth ; 927* outNumberOfLevels = dims. numberOfLevels ; 928} 929void GetDimensions ( float * outWidth, float * outHeight, float * outDepth) 930{ 931const auto dims = texture -> GetDimensions (); 932* outWidth = dims. width ; 933* outHeight = dims. height ; 934* outDepth = dims. depth ; 935} 936void GetDimensions ( 937uint32_t mipLevel, 938float * outWidth, 939float * outHeight, 940float * outDepth, 941float * outNumberOfLevels) 942{ 943const auto dims = texture -> GetDimensions (mipLevel); 944* outWidth = dims. width ; 945* outHeight = dims. height ; 946* outDepth = dims. depth ; 947* outNumberOfLevels = dims. numberOfLevels ; 948} 949 950T Load ( const int3 & loc) const 951{ 952T out; 953texture -> Load ( & loc. x , & out, sizeof (out)); 954return out; 955} 956T & operator[](const uint3 & loc) { return * ( T * )texture -> refAt ( & loc. x ); } 957IRWTexture * texture; 958}; 959 960 961template < typename T > 962struct RWTexture1DArray 963{ 964void GetDimensions ( uint32_t * outWidth, uint32_t * outElements) 965{ 966auto dims = texture -> GetDimensions (); 967* outWidth = dims. width ; 968* outElements = dims. arrayElementCount ; 969} 970void GetDimensions ( 971uint32_t mipLevel, 972uint32_t * outWidth, 973uint32_t * outElements, 974uint32_t * outNumberOfLevels) 975{ 976const auto dims = texture -> GetDimensions (mipLevel); 977* outWidth = dims. width ; 978* outElements = dims. arrayElementCount ; 979* outNumberOfLevels = dims. numberOfLevels ; 980} 981void GetDimensions ( float * outWidth, float * outElements) 982{ 983auto dims = texture -> GetDimensions (); 984* outWidth = dims. width ; 985* outElements = dims. arrayElementCount ; 986} 987void GetDimensions ( 988uint32_t mipLevel, 989float * outWidth, 990float * outElements, 991float * outNumberOfLevels) 992{ 993const auto dims = texture -> GetDimensions (mipLevel); 994* outWidth = dims. width ; 995* outElements = dims. arrayElementCount ; 996* outNumberOfLevels = dims. numberOfLevels ; 997} 998 999T Load ( int2 loc) const 1000{ 1001T out; 1002texture -> Load ( & loc. x , & out, sizeof (out)); 1003return out; 1004} 1005T & operator[](uint2 loc) { return * ( T * )texture -> refAt ( & loc. x ); } 1006 1007IRWTexture * texture; 1008}; 1009 1010template < typename T > 1011struct RWTexture2DArray 1012{ 1013void GetDimensions ( uint32_t * outWidth, uint32_t * outHeight, uint32_t * outElements) 1014{ 1015auto dims = texture -> GetDimensions (); 1016* outWidth = dims. width ; 1017* outHeight = dims. height ; 1018* outElements = dims. arrayElementCount ; 1019} 1020void GetDimensions ( 1021uint32_t mipLevel, 1022uint32_t * outWidth, 1023uint32_t * outHeight, 1024uint32_t * outElements, 1025uint32_t * outNumberOfLevels) 1026{ 1027const auto dims = texture -> GetDimensions (mipLevel); 1028* outWidth = dims. width ; 1029* outHeight = dims. height ; 1030* outElements = dims. arrayElementCount ; 1031* outNumberOfLevels = dims. numberOfLevels ; 1032} 1033void GetDimensions ( float * outWidth, float * outHeight, float * outElements) 1034{ 1035auto dims = texture -> GetDimensions (); 1036* outWidth = dims. width ; 1037* outHeight = dims. height ; 1038* outElements = dims. arrayElementCount ; 1039} 1040void GetDimensions ( 1041uint32_t mipLevel, 1042float * outWidth, 1043float * outHeight, 1044float * outElements, 1045float * outNumberOfLevels) 1046{ 1047const auto dims = texture -> GetDimensions (mipLevel); 1048* outWidth = dims. width ; 1049* outHeight = dims. height ; 1050* outElements = dims. arrayElementCount ; 1051* outNumberOfLevels = dims. numberOfLevels ; 1052} 1053 1054T Load ( const int3 & loc) const 1055{ 1056T out; 1057texture -> Load ( & loc. x , & out, sizeof (out)); 1058return out; 1059} 1060T & operator[](const uint3 & loc) { return * ( T * )texture -> refAt ( & loc. x ); } 1061 1062IRWTexture * texture; 1063}; 1064 1065// FeedbackTexture 1066 1067struct FeedbackType 1068{ 1069}; 1070struct SAMPLER_FEEDBACK_MIN_MIP : FeedbackType 1071{ 1072}; 1073struct SAMPLER_FEEDBACK_MIP_REGION_USED : FeedbackType 1074{ 1075}; 1076 1077struct IFeedbackTexture 1078{ 1079virtual TextureDimensions GetDimensions ( int mipLevel = -1 ) = 0 ; 1080 1081// Note here we pass the optional clamp parameter as a pointer. Passing nullptr means no clamp. 1082// This was preferred over having two function definitions, and having to differentiate their 1083// names 1084virtual void WriteSamplerFeedback ( 1085ITexture * tex, 1086SamplerState samp, 1087const float * location, 1088const float * clamp = nullptr) = 0 ; 1089virtual void WriteSamplerFeedbackBias ( 1090ITexture * tex, 1091SamplerState samp, 1092const float * location, 1093float bias, 1094const float * clamp = nullptr) = 0 ; 1095virtual void WriteSamplerFeedbackGrad ( 1096ITexture * tex, 1097SamplerState samp, 1098const float * location, 1099const float * ddx, 1100const float * ddy, 1101const float * clamp = nullptr) = 0 ; 1102 1103virtual void WriteSamplerFeedbackLevel ( 1104ITexture * tex, 1105SamplerState samp, 1106const float * location, 1107float lod) = 0 ; 1108}; 1109 1110template < typename T > 1111struct FeedbackTexture2D 1112{ 1113void GetDimensions ( uint32_t * outWidth, uint32_t * outHeight) 1114{ 1115const auto dims = texture -> GetDimensions (); 1116* outWidth = dims. width ; 1117* outHeight = dims. height ; 1118} 1119void GetDimensions ( 1120uint32_t mipLevel, 1121uint32_t * outWidth, 1122uint32_t * outHeight, 1123uint32_t * outNumberOfLevels) 1124{ 1125const auto dims = texture -> GetDimensions (mipLevel); 1126* outWidth = dims. width ; 1127* outHeight = dims. height ; 1128* outNumberOfLevels = dims. numberOfLevels ; 1129} 1130void GetDimensions ( float * outWidth, float * outHeight) 1131{ 1132const auto dims = texture -> GetDimensions (); 1133* outWidth = dims. width ; 1134* outHeight = dims. height ; 1135} 1136void GetDimensions ( 1137uint32_t mipLevel, 1138float * outWidth, 1139float * outHeight, 1140float * outNumberOfLevels) 1141{ 1142const auto dims = texture -> GetDimensions (mipLevel); 1143* outWidth = dims. width ; 1144* outHeight = dims. height ; 1145* outNumberOfLevels = dims. numberOfLevels ; 1146} 1147 1148template < typename S > 1149void WriteSamplerFeedback (Texture2D < S > tex, SamplerState samp, float2 location, float clamp) 1150{ 1151texture -> WriteSamplerFeedback (tex. texture , samp, & location. x , & clamp); 1152} 1153 1154template < typename S > 1155void WriteSamplerFeedbackBias ( 1156Texture2D < S > tex, 1157SamplerState samp, 1158float2 location, 1159float bias, 1160float clamp) 1161{ 1162texture -> WriteSamplerFeedbackBias (tex. texture , samp, & location. x , bias, & clamp); 1163} 1164 1165template < typename S > 1166void WriteSamplerFeedbackGrad ( 1167Texture2D < S > tex, 1168SamplerState samp, 1169float2 location, 1170float2 ddx, 1171float2 ddy, 1172float clamp) 1173{ 1174texture -> WriteSamplerFeedbackGrad (tex. texture , samp, & location. x , & ddx. x , & ddy. x , & clamp); 1175} 1176 1177// Level 1178 1179template < typename S > 1180void WriteSamplerFeedbackLevel (Texture2D < S > tex, SamplerState samp, float2 location, float lod) 1181{ 1182texture -> WriteSamplerFeedbackLevel (tex. texture , samp, & location. x , lod); 1183} 1184 1185// Without Clamp 1186template < typename S > 1187void WriteSamplerFeedback (Texture2D < S > tex, SamplerState samp, float2 location) 1188{ 1189texture -> WriteSamplerFeedback (tex. texture , samp, & location. x ); 1190} 1191 1192template < typename S > 1193void WriteSamplerFeedbackBias (Texture2D < S > tex, SamplerState samp, float2 location, float bias) 1194{ 1195texture -> WriteSamplerFeedbackBias (tex. texture , samp, & location. x , bias); 1196} 1197 1198template < typename S > 1199void WriteSamplerFeedbackGrad ( 1200Texture2D < S > tex, 1201SamplerState samp, 1202float2 location, 1203float2 ddx, 1204float2 ddy) 1205{ 1206texture -> WriteSamplerFeedbackGrad (tex. texture , samp, & location. x , & ddx. x , & ddy. x ); 1207} 1208 1209IFeedbackTexture * texture; 1210}; 1211 1212template < typename T > 1213struct FeedbackTexture2DArray 1214{ 1215void GetDimensions ( uint32_t * outWidth, uint32_t * outHeight, uint32_t * outElements) 1216{ 1217auto dims = texture -> GetDimensions (); 1218* outWidth = dims. width ; 1219* outHeight = dims. height ; 1220* outElements = dims. arrayElementCount ; 1221} 1222void GetDimensions ( 1223uint32_t mipLevel, 1224uint32_t * outWidth, 1225uint32_t * outHeight, 1226uint32_t * outElements, 1227uint32_t * outNumberOfLevels) 1228{ 1229const auto dims = texture -> GetDimensions (mipLevel); 1230* outWidth = dims. width ; 1231* outHeight = dims. height ; 1232* outElements = dims. arrayElementCount ; 1233* outNumberOfLevels = dims. numberOfLevels ; 1234} 1235void GetDimensions ( float * outWidth, float * outHeight, float * outElements) 1236{ 1237auto dims = texture -> GetDimensions (); 1238* outWidth = dims. width ; 1239* outHeight = dims. height ; 1240* outElements = dims. arrayElementCount ; 1241} 1242void GetDimensions ( 1243uint32_t mipLevel, 1244float * outWidth, 1245float * outHeight, 1246float * outElements, 1247float * outNumberOfLevels) 1248{ 1249const auto dims = texture -> GetDimensions (mipLevel); 1250* outWidth = dims. width ; 1251* outHeight = dims. height ; 1252* outElements = dims. arrayElementCount ; 1253* outNumberOfLevels = dims. numberOfLevels ; 1254} 1255 1256template < typename S > 1257void WriteSamplerFeedback ( 1258Texture2DArray < S > texArray, 1259SamplerState samp, 1260float3 location, 1261float clamp) 1262{ 1263texture -> WriteSamplerFeedback (texArray. texture , samp, & location. x , & clamp); 1264} 1265 1266template < typename S > 1267void WriteSamplerFeedbackBias ( 1268Texture2DArray < S > texArray, 1269SamplerState samp, 1270float3 location, 1271float bias, 1272float clamp) 1273{ 1274texture -> WriteSamplerFeedbackBias (texArray. texture , samp, & location. x , bias, & clamp); 1275} 1276 1277template < typename S > 1278void WriteSamplerFeedbackGrad ( 1279Texture2DArray < S > texArray, 1280SamplerState samp, 1281float3 location, 1282float3 ddx, 1283float3 ddy, 1284float clamp) 1285{ 1286texture 1287-> WriteSamplerFeedbackGrad (texArray. texture , samp, & location. x , & ddx. x , & ddy. x , & clamp); 1288} 1289 1290// Level 1291template < typename S > 1292void WriteSamplerFeedbackLevel ( 1293Texture2DArray < S > texArray, 1294SamplerState samp, 1295float3 location, 1296float lod) 1297{ 1298texture -> WriteSamplerFeedbackLevel (texArray. texture , samp, & location. x , lod); 1299} 1300 1301// Without Clamp 1302 1303template < typename S > 1304void WriteSamplerFeedback (Texture2DArray < S > texArray, SamplerState samp, float3 location) 1305{ 1306texture -> WriteSamplerFeedback (texArray. texture , samp, & location. x ); 1307} 1308 1309template < typename S > 1310void WriteSamplerFeedbackBias ( 1311Texture2DArray < S > texArray, 1312SamplerState samp, 1313float3 location, 1314float bias) 1315{ 1316texture -> WriteSamplerFeedbackBias (texArray. texture , samp, & location. x , bias); 1317} 1318 1319template < typename S > 1320void WriteSamplerFeedbackGrad ( 1321Texture2DArray < S > texArray, 1322SamplerState samp, 1323float3 location, 1324float3 ddx, 1325float3 ddy) 1326{ 1327texture -> WriteSamplerFeedbackGrad (texArray. texture , samp, & location. x , & ddx. x , & ddy. x ); 1328} 1329 1330IFeedbackTexture * texture; 1331}; 1332 1333/* Varying input for Compute */ 1334 1335/* Used when running a single thread */ 1336struct ComputeThreadVaryingInput 1337{ 1338uint3 groupID ; 1339uint3 groupThreadID ; 1340}; 1341 1342struct ComputeVaryingInput 1343{ 1344uint3 startGroupID ; ///< start groupID 1345uint3 endGroupID ; ///< Non inclusive end groupID 1346}; 1347 1348// The uniformEntryPointParams and uniformState must be set to structures that match layout that the 1349// kernel expects. This can be determined via reflection for example. 1350 1351typedef void ( * ComputeThreadFunc )( 1352ComputeThreadVaryingInput * varyingInput, 1353void * uniformEntryPointParams, 1354void * uniformState); 1355typedef void ( * ComputeFunc )( 1356ComputeVaryingInput * varyingInput, 1357void * uniformEntryPointParams, 1358void * uniformState); 1359 1360#ifdef SLANG_PRELUDE_NAMESPACE 1361} 1362#endif 1363 1364#endif