yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
78dc7c3fd
master
1// unit-test-atomic-reflection.cpp 2 3#include "../../source/core/slang-io.h" 4#include "../../source/core/slang-process.h" 5#include "slang-com-ptr.h" 6#include "slang.h" 7#include "unit-test/slang-unit-test.h" 8 9#include <stdio.h> 10#include <stdlib.h> 11 12using namespace Slang ; 13 14SLANG_UNIT_TEST (atomicReflection ) 15{ 16const char * userSourceBody = R"( 17RWStructuredBuffer<uint> bufA; // for reference 18RWStructuredBuffer<Atomic<uint>> bufB; 19struct TestStruct { 20Atomic<uint> fieldA; 21Atomic<uint> fieldB[2]; 22vector<Atomic<uint>, 2> fieldC; 23}; 24RWStructuredBuffer<TestStruct> bufC; 25 26[shader("compute")] 27[numthreads(64, 1, 1)] 28void main(uint2 dispatchThreadId : SV_DispatchThreadID) 29{ 30} 31)" ; 32String userSource = userSourceBody ; 33ComPtr < slang::IGlobalSession > globalSession ; 34SLANG_CHECK (slang_createGlobalSession (SLANG_API_VERSION ,globalSession .writeRef ())== SLANG_OK ); 35 slang::TargetDesc targetDesc = {}; 36targetDesc .format = SLANG_HLSL ; 37targetDesc .profile = globalSession -> findProfile ("sm_5_0" ); 38 slang::SessionDesc sessionDesc = {}; 39sessionDesc .targetCount = 1 ; 40sessionDesc .targets = & targetDesc ; 41ComPtr < slang::ISession > session ; 42SLANG_CHECK (globalSession -> createSession (sessionDesc ,session .writeRef ())== SLANG_OK ); 43 44ComPtr < slang::IBlob > diagnosticBlob ; 45auto module = session -> loadModuleFromSourceString ( 46"m" , 47"m.slang" , 48userSourceBody , 49diagnosticBlob .writeRef ()); 50SLANG_CHECK (module != nullptr ); 51 52auto reflection = module -> getLayout (); 53 54auto bufAVariableLayout = reflection -> getParameterByIndex (0 ); 55auto bufBVariableLayout = reflection -> getParameterByIndex (1 ); 56auto bufCVariableLayout = reflection -> getParameterByIndex (2 ); 57 58SLANG_CHECK (bufAVariableLayout != nullptr ); 59SLANG_CHECK (bufBVariableLayout != nullptr ); 60SLANG_CHECK (bufCVariableLayout != nullptr ); 61 62// This test walks down the parameters in a different way from the JSON 63// option. The slangc option "-reflection-json" walks: 64// param->getTypeLayout()->getElementTypeLayout()->getType()->getKind() 65// This test uses an alternate walk: 66// param->getVariable()->getType()->getElementType()->getKind() 67 68auto bufAVariable = bufAVariableLayout -> getVariable (); 69auto bufBVariable = bufBVariableLayout -> getVariable (); 70auto bufCVariable = bufCVariableLayout -> getVariable (); 71 72SLANG_CHECK (bufAVariable != nullptr ); 73SLANG_CHECK (bufBVariable != nullptr ); 74SLANG_CHECK (bufCVariable != nullptr ); 75 76auto bufAType = bufAVariable -> getType (); 77auto bufBType = bufBVariable -> getType (); 78auto bufCType = bufCVariable -> getType (); 79 80SLANG_CHECK (bufAType -> getKind ()== slang::TypeReflection ::Kind ::Resource ); 81SLANG_CHECK (bufBType -> getKind ()== slang::TypeReflection ::Kind ::Resource ); 82SLANG_CHECK (bufCType -> getKind ()== slang::TypeReflection ::Kind ::Resource ); 83 84auto bufAElementType = bufAType -> getElementType (); 85auto bufBElementType = bufBType -> getElementType (); 86auto bufCElementType = bufCType -> getElementType (); 87 88SLANG_CHECK (bufAElementType -> getKind ()== slang::TypeReflection ::Kind ::Scalar ); 89SLANG_CHECK (bufBElementType -> getKind ()== slang::TypeReflection ::Kind ::Scalar ); 90SLANG_CHECK (bufAElementType -> getScalarType ()== slang::TypeReflection ::ScalarType ::UInt32 ); 91SLANG_CHECK (bufBElementType -> getScalarType ()== slang::TypeReflection ::ScalarType ::UInt32 ); 92 93auto bufAResResultType = bufAType -> getResourceResultType (); 94auto bufBResResultType = bufBType -> getResourceResultType (); 95 96SLANG_CHECK (bufAResResultType -> getKind ()== slang::TypeReflection ::Kind ::Scalar ); 97SLANG_CHECK (bufBResResultType -> getKind ()== slang::TypeReflection ::Kind ::Scalar ); 98SLANG_CHECK (bufAResResultType -> getScalarType ()== slang::TypeReflection ::ScalarType ::UInt32 ); 99SLANG_CHECK (bufBResResultType -> getScalarType ()== slang::TypeReflection ::ScalarType ::UInt32 ); 100 101// Atomics embedded in structs require traversing the fields 102SLANG_CHECK (bufCElementType -> getKind ()== slang::TypeReflection ::Kind ::Struct ); 103 104auto bufCFieldCount = bufCElementType -> getFieldCount (); 105SLANG_CHECK (bufCFieldCount == 3 ); 106 107auto fieldA = bufCElementType -> getFieldByIndex (0 ); 108auto fieldB = bufCElementType -> getFieldByIndex (1 ); 109auto fieldC = bufCElementType -> getFieldByIndex (2 ); 110 111SLANG_CHECK (fieldA != nullptr ); 112SLANG_CHECK (fieldB != nullptr ); 113SLANG_CHECK (fieldC != nullptr ); 114 115auto fieldAType = fieldA -> getType (); 116auto fieldBType = fieldB -> getType (); 117auto fieldCType = fieldC -> getType (); 118 119SLANG_CHECK (fieldAType -> getKind ()== slang::TypeReflection ::Kind ::Scalar ); 120SLANG_CHECK (fieldAType -> getScalarType ()== slang::TypeReflection ::ScalarType ::UInt32 ); 121 122SLANG_CHECK (fieldBType -> getKind ()== slang::TypeReflection ::Kind ::Array ); 123SLANG_CHECK (fieldBType -> getElementCount ()== 2 ); 124auto fieldBElementType = fieldBType -> getElementType (); 125SLANG_CHECK (fieldBElementType != nullptr ); 126SLANG_CHECK (fieldBElementType -> getKind ()== slang::TypeReflection ::Kind ::Scalar ); 127SLANG_CHECK (fieldBElementType -> getScalarType ()== slang::TypeReflection ::ScalarType ::UInt32 ); 128 129SLANG_CHECK (fieldCType -> getKind ()== slang::TypeReflection ::Kind ::Vector ); 130SLANG_CHECK (fieldCType -> getElementCount ()== 2 ); 131auto fieldCElementType = fieldCType -> getElementType (); 132SLANG_CHECK (fieldCElementType != nullptr ); 133SLANG_CHECK (fieldCElementType -> getKind ()== slang::TypeReflection ::Kind ::Scalar ); 134SLANG_CHECK (fieldCElementType -> getScalarType ()== slang::TypeReflection ::ScalarType ::UInt32 ); 135}