yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
5df3a74af
master
1// unit-test-parameter-usage-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 14// Test that the isParameterLocationUsed API works. 15 16SLANG_UNIT_TEST (isParameterLocationUsedReflection ) 17{ 18// Source for a module that contains an undecorated entrypoint. 19const char * userSourceBody = R"( 20Texture2D g_tex : register(t0); 21struct Params { 22Texture2D tex2; 23Texture2D tex3; 24}; 25struct Material 26{ 27float2 uvScale; 28float2 uvBias; 29} 30ParameterBlock<Params> gParams; 31ConstantBuffer<Material> gcMaterial; 32ParameterBlock<Material> gMaterial; 33[shader("fragment")] 34float4 fragMain(float4 pos:SV_Position, float unused:COLOR0, float4 used:COLOR1) : SV_Target 35{ 36return g_tex.Load(int3(0, 0, 0)) + gParams.tex3.Load(int3(0)) + used + gMaterial.uvScale.x + gcMaterial.uvBias.x; 37} 38)" ; 39 40auto moduleName = "moduleG" + String (Process ::getId ()); 41String userSource = "import " + moduleName + ";\n" + userSourceBody ; 42ComPtr < slang::IGlobalSession > globalSession ; 43SLANG_CHECK (slang_createGlobalSession (SLANG_API_VERSION ,globalSession .writeRef ())== SLANG_OK ); 44 slang::TargetDesc targetDesc = {}; 45targetDesc .format = SLANG_SPIRV ; 46targetDesc .profile = globalSession -> findProfile ("spirv_1_5" ); 47 slang::SessionDesc sessionDesc = {}; 48sessionDesc .targetCount = 1 ; 49sessionDesc .targets = & targetDesc ; 50ComPtr < slang::ISession > session ; 51SLANG_CHECK (globalSession -> createSession (sessionDesc ,session .writeRef ())== SLANG_OK ); 52 53ComPtr < slang::IBlob > diagnosticBlob ; 54auto module = session -> loadModuleFromSourceString ( 55"m" , 56"m.slang" , 57userSourceBody , 58diagnosticBlob .writeRef ()); 59SLANG_CHECK (module != nullptr ); 60 61ComPtr < slang::IEntryPoint > entryPoint ; 62module -> findAndCheckEntryPoint ( 63"fragMain" , 64SLANG_STAGE_FRAGMENT , 65entryPoint .writeRef (), 66diagnosticBlob .writeRef ()); 67SLANG_CHECK (entryPoint != nullptr ); 68 69ComPtr < slang::IComponentType > compositeProgram ; 70 slang::IComponentType * components []= {module ,entryPoint .get ()}; 71session -> createCompositeComponentType ( 72components , 732 , 74compositeProgram .writeRef (), 75diagnosticBlob .writeRef ()); 76SLANG_CHECK (compositeProgram != nullptr ); 77 78ComPtr < slang::IComponentType > linkedProgram ; 79compositeProgram -> link (linkedProgram .writeRef (),nullptr ); 80 81ComPtr < slang::IMetadata > metadata ; 82linkedProgram -> getTargetMetadata (0 ,metadata .writeRef (),nullptr ); 83 84bool isUsed = false; 85metadata -> isParameterLocationUsed (SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT ,0 ,0 ,isUsed ); 86SLANG_CHECK (isUsed ); 87 88metadata -> isParameterLocationUsed (SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT ,0 ,1 ,isUsed ); 89SLANG_CHECK (isUsed ); 90 91metadata -> isParameterLocationUsed (SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT ,0 ,2 ,isUsed ); 92SLANG_CHECK (!isUsed ); 93 94metadata -> isParameterLocationUsed (SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT ,1 ,0 ,isUsed ); 95SLANG_CHECK (!isUsed ); 96 97metadata -> isParameterLocationUsed (SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT ,1 ,1 ,isUsed ); 98SLANG_CHECK (isUsed ); 99 100metadata -> isParameterLocationUsed (SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT ,2 ,0 ,isUsed ); 101SLANG_CHECK (isUsed ); 102 103metadata -> isParameterLocationUsed (SLANG_PARAMETER_CATEGORY_VARYING_INPUT ,0 ,0 ,isUsed ); 104SLANG_CHECK (!isUsed ); 105 106metadata -> isParameterLocationUsed (SLANG_PARAMETER_CATEGORY_VARYING_INPUT ,0 ,1 ,isUsed ); 107SLANG_CHECK (isUsed ); 108}