From 86669cbb781840f8de180b1f793275f4511d3b65 Mon Sep 17 00:00:00 2001 From: Alexandre Bléron Date: Wed, 26 Feb 2025 22:43:57 +0100 Subject: expose value of constant integers in module reflection (#6367) * Expose value of constant integers in module reflection This commit adds `VariableReflection::getDefaultValueInt` to get the value of a variable if it is a compile-time constant integer. TODO: currently it works only if the initializer expression is an integer literal, references to other constant values are not handled. * Update VarDecl folded constant value during DeclBodyVisitor Constant folding for integer values is already done internally by _validateCircularVarDefinition, this just reuses the result. * Address review comments & formatting * Formatting --------- Co-authored-by: Yong He --- examples/reflection-api/compute-simple.slang | 5 ++++- examples/reflection-api/main.cpp | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) (limited to 'examples/reflection-api') diff --git a/examples/reflection-api/compute-simple.slang b/examples/reflection-api/compute-simple.slang index 3d8872180..00d45d857 100644 --- a/examples/reflection-api/compute-simple.slang +++ b/examples/reflection-api/compute-simple.slang @@ -1,5 +1,8 @@ // compute-simple.slang +static const uint THREADGROUP_SIZE_X = 8; +static const uint THREADGROUP_SIZE_Y = THREADGROUP_SIZE_X; + struct ImageProcessingOptions { float3 tintColor; @@ -10,7 +13,7 @@ struct ImageProcessingOptions } [shader("compute")] -[numthreads(8, 8)] +[numthreads(THREADGROUP_SIZE_X, THREADGROUP_SIZE_Y)] void processImage( uint3 threadID : SV_DispatchThreadID, uniform Texture2D inputImage, diff --git a/examples/reflection-api/main.cpp b/examples/reflection-api/main.cpp index c072c641b..2ff4475bf 100644 --- a/examples/reflection-api/main.cpp +++ b/examples/reflection-api/main.cpp @@ -114,6 +114,21 @@ struct ReflectingPrinting List> componentsToLink; + // ### Variable decls + // + key("global constants"); + WITH_ARRAY() + for (auto decl : module->getModuleReflection()->getChildren()) + { + if (auto varDecl = decl->asVariable(); varDecl && + varDecl->findModifier(slang::Modifier::Const) && + varDecl->findModifier(slang::Modifier::Static)) + { + element(); + printVariable(varDecl); + } + } + // ### Finding Entry Points // @@ -213,6 +228,13 @@ struct ReflectingPrinting printQuotedString(name); key("type"); printType(type); + + int64_t value; + if (SLANG_SUCCEEDED(variable->getDefaultValueInt(&value))) + { + key("value"); + printf("%" PRId64, value); + } } // ### Types -- cgit v1.2.3