yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
43d0c2100
master
1#pragma once 2 3#include "core/slang-basic.h" 4#include "slang-rhi.h" 5#include "unit-test/slang-unit-test.h" 6 7using namespace rhi ; 8using Slang ::RefObject ; 9using Slang ::RefPtr ; 10 11namespace gfx_test 12{ 13struct Strides 14{ 15Size x ; 16Size y ; 17Size z ; 18}; 19 20struct ValidationTextureFormatBase :RefObject 21{ 22virtual void validateBlocksEqual (const void * actual ,const void * expected )= 0 ; 23 24virtual void initializeTexel ( 25void * texel , 26uint32_t x , 27uint32_t y , 28uint32_t z , 29uint32_t mip , 30uint32_t layer )= 0 ; 31}; 32 33template < typename T > 34struct ValidationTextureFormat :ValidationTextureFormatBase 35{ 36uint32_t componentCount ; 37 38ValidationTextureFormat (uint32_t componentCount ) 39 :componentCount (componentCount ){}; 40 41virtual void validateBlocksEqual (const void * actual ,const void * expected )override 42 { 43 autoa = (const T * )actual ; 44 autoe = (const T * )expected ; 45 46for (uint32_t i = 0 ;i < componentCount ;++ i ) 47 { 48SLANG_CHECK (a [i ]== e [i ]); 49 } 50 } 51 52virtual void initializeTexel ( 53void * texel , 54uint32_t x , 55uint32_t y , 56uint32_t z , 57uint32_t mip , 58uint32_t layer )override 59 { 60 autotemp = (T * )texel ; 61 62switch (componentCount ) 63 { 64case 1 : 65temp [0 ]= T (x + y + z + mip + layer ); 66break ; 67case 2 : 68temp [0 ]= T (x + z + layer ); 69temp [1 ]= T (y + mip ); 70break ; 71case 3 : 72temp [0 ]= T (x + mip ); 73temp [1 ]= T (y + layer ); 74temp [2 ]= T (z ); 75break ; 76case 4 : 77temp [0 ]= T (x + layer ); 78temp [1 ]= (T )y ; 79temp [2 ]= (T )z ; 80temp [3 ]= (T )mip ; 81break ; 82default : 83assert ("component count should be no greater than 4" ); 84SLANG_CHECK_ABORT (false); 85 } 86 } 87}; 88 89template < typename T > 90struct PackedValidationTextureFormat :ValidationTextureFormatBase 91{ 92int rBits ; 93int gBits ; 94int bBits ; 95int aBits ; 96 97PackedValidationTextureFormat (int rBits ,int gBits ,int bBits ,int aBits ) 98 :rBits (rBits ),gBits (gBits ),bBits (bBits ),aBits (aBits ){}; 99 100virtual void validateBlocksEqual (const void * actual ,const void * expected )override 101 { 102T a [4 ]; 103T e [4 ]; 104unpackTexel (* (const T * )actual ,a ); 105unpackTexel (* (const T * )expected ,e ); 106 107for (uint32_t i = 0 ;i < 4 ;++ i ) 108 { 109SLANG_CHECK (a [i ]== e [i ]); 110 } 111 } 112 113virtual void initializeTexel ( 114void * texel , 115uint32_t x , 116uint32_t y , 117uint32_t z , 118uint32_t mip , 119uint32_t layer )override 120 { 121T temp = 0 ; 122 123// The only formats which currently use this have either 3 or 4 channels. TODO: BC formats? 124if (aBits == 0 ) 125 { 126temp |=z ; 127temp <<=gBits ; 128temp |= (y + layer ); 129temp <<=rBits ; 130temp |= (x + mip ); 131 } 132else 133 { 134temp |=mip ; 135temp <<=bBits ; 136temp |=z ; 137temp <<=gBits ; 138temp |=y ; 139temp <<=rBits ; 140temp |= (x + layer ); 141 } 142 143* (T * )texel = temp ; 144 } 145 146void unpackTexel (T texel ,T * outComponents ) 147 { 148outComponents [0 ]= texel & ((1 <<rBits )- 1 ); 149texel >>=rBits ; 150 151outComponents [1 ]= texel & ((1 <<gBits )- 1 ); 152texel >>=gBits ; 153 154outComponents [2 ]= texel & ((1 <<bBits )- 1 ); 155texel >>=bBits ; 156 157outComponents [3 ]= texel & ((1 <<aBits )- 1 ); 158texel >>=aBits ; 159 } 160}; 161 162// Struct containing texture data and information for a specific subresource. 163struct ValidationTextureData :RefObject 164{ 165const void * textureData ; 166Extent3D extent ; 167Strides pitches ; 168 169void * getBlockAt (uint32_t x ,uint32_t y ,uint32_t z ) 170 { 171assert (x < extent .width ); 172assert (y < extent .height ); 173assert (z < extent .depth ); 174 175char * layerData = (char * )textureData + z * pitches .z ; 176char * rowData = layerData + y * pitches .y ; 177return rowData + x * pitches .x ; 178 } 179}; 180 181// Struct containing relevant information for a texture, including a list of its subresources 182// and all relevant information for each subresource. 183struct TextureInfo :RefObject 184{ 185Format format ; 186TextureType textureType ; 187 188Extent3D extent ; 189uint32_t mipCount ; 190uint32_t arrayLength ; 191 192std ::vector < RefPtr < ValidationTextureData >>subresourceObjects ; 193std ::vector < SubresourceData > subresourceDatas ; 194 195 ~TextureInfo (); 196}; 197 198inline TextureType toArrayType (TextureType type ) 199{ 200switch (type ) 201 { 202case TextureType ::Texture1D : 203return TextureType ::Texture1DArray ; 204case TextureType ::Texture2D : 205return TextureType ::Texture2DArray ; 206case TextureType ::Texture2DMS : 207return TextureType ::Texture2DMSArray ; 208case TextureType ::TextureCube : 209return TextureType ::TextureCubeArray ; 210default : 211return type ; 212 } 213} 214 215Size getTexelSize (Format format ); 216RefPtr < ValidationTextureFormatBase > getValidationTextureFormat (Format format ); 217void generateTextureData ( 218RefPtr < TextureInfo > texture , 219ValidationTextureFormatBase * validationFormat ); 220 221std ::vector < uint8_t > removePadding ( 222ISlangBlob * pixels , 223uint32_t width , 224uint32_t height , 225Size rowPitch , 226Size pixelSize ); 227Result writeImage (const char * filename ,ISlangBlob * pixels ,uint32_t width ,uint32_t height ); 228Result writeImage ( 229const char * filename , 230ISlangBlob * pixels , 231uint32_t width , 232uint32_t height , 233uint32_t rowPitch , 234uint32_t pixelSize ); 235 236}// namespace gfx_test