summaryrefslogtreecommitdiffstats
path: root/source
diff options
context:
space:
mode:
authorYong He <yongh@outlook.com>2018-01-03 12:46:23 -0800
committerYong He <yongh@outlook.com>2018-01-03 12:46:23 -0800
commit9f89c3b2de80c49222c4a6b48e845894c4256a5d (patch)
treef35c1aa361e01dc300fc5eafcff0f866f93edc01 /source
parentf52b93e15d68054a06db84ff86d077d9ce0af941 (diff)
Add API for querying TypeLayout from a Type
Added two API functions: 1. `spReflection_FindTypeByName`, which returns a DeclRefType to the struct type with the given name. The function finds from all loaded modules in a `CompileRequest` for a decl with the given name, construct a `Type` object and cache it in `CompileRequest::types` dictionary. The subsequent calls to `spReflection_FindTypeByName` with the same name will simply returned the cached Type objects. 2. `spReflection_GetTypeLayout`, which returns a `TypeLayout` for a given `Type`. This function creates and caches the `TypeLayout` in the `TargetRequest` object that is used to create the `ProgramLayout`.
Diffstat (limited to 'source')
-rw-r--r--source/slang/compiler.h7
-rw-r--r--source/slang/parameter-binding.cpp3
-rw-r--r--source/slang/reflection.cpp41
-rw-r--r--source/slang/type-layout.h2
4 files changed, 53 insertions, 0 deletions
diff --git a/source/slang/compiler.h b/source/slang/compiler.h
index 0e85a1088..2302c77c0 100644
--- a/source/slang/compiler.h
+++ b/source/slang/compiler.h
@@ -16,6 +16,7 @@ namespace Slang
class CompileRequest;
class ProgramLayout;
class PtrType;
+ class TypeLayout;
enum class CompilerMode
{
@@ -197,6 +198,9 @@ namespace Slang
// in the parent compile request (indexing matches
// the order they are given in the compile request)
List<CompileResult> entryPointResults;
+
+ // TypeLayouts created on the fly by reflection API
+ Dictionary<Type*, RefPtr<TypeLayout>> typeLayouts;
};
// A directory to be searched when looking for files (e.g., `#include`)
@@ -255,6 +259,9 @@ namespace Slang
// assocaited with a translation unit).
List<RefPtr<EntryPointRequest> > entryPoints;
+ // Types constructed by reflection API
+ Dictionary<String, RefPtr<Type>> types;
+
// The code generation profile we've been asked to use.
Profile profile;
diff --git a/source/slang/parameter-binding.cpp b/source/slang/parameter-binding.cpp
index 6145015f1..6dac47631 100644
--- a/source/slang/parameter-binding.cpp
+++ b/source/slang/parameter-binding.cpp
@@ -1786,6 +1786,8 @@ void generateParameterBindings(
return;
RefPtr<ProgramLayout> programLayout = new ProgramLayout();
+ programLayout->targetRequest = targetReq;
+
targetReq->layout = programLayout;
// Create a context to hold shared state during the process
@@ -2061,6 +2063,7 @@ RefPtr<ProgramLayout> specializeProgramLayout(
{
RefPtr<ProgramLayout> newProgramLayout;
newProgramLayout = new ProgramLayout();
+ newProgramLayout->targetRequest = targetReq;
newProgramLayout->bindingForHackSampler = programLayout->bindingForHackSampler;
newProgramLayout->hackSamplerVar = programLayout->hackSamplerVar;
newProgramLayout->globalGenericParams = programLayout->globalGenericParams;
diff --git a/source/slang/reflection.cpp b/source/slang/reflection.cpp
index 5270df8b4..0558ff773 100644
--- a/source/slang/reflection.cpp
+++ b/source/slang/reflection.cpp
@@ -428,6 +428,47 @@ SLANG_API char const* spReflectionType_GetName(SlangReflectionType* inType)
return nullptr;
}
+SLANG_API SlangReflectionType * spReflection_FindTypeByName(SlangReflection * reflection, char const * name)
+{
+ auto context = convert(reflection);
+ auto compileRequest = context->targetRequest->compileRequest;
+
+ RefPtr<Type> result;
+ if (compileRequest->types.TryGetValue(name, result))
+ return (SlangReflectionType*)result.Ptr();
+
+ Decl* resultDecl = nullptr;
+ for (auto module : compileRequest->loadedModulesList)
+ {
+ auto nameObj = compileRequest->getNamePool()->getName(name);
+ if (module->moduleDecl->memberDictionary.TryGetValue(nameObj, resultDecl))
+ break;
+ }
+ if (resultDecl)
+ {
+ RefPtr<DeclRefType> declRefType = new DeclRefType();
+ declRefType->declRef.decl = resultDecl;
+ compileRequest->types[name] = declRefType;
+ return (SlangReflectionType*)declRefType.Ptr();
+ }
+ return nullptr;
+}
+
+SLANG_API SlangReflectionTypeLayout* spReflection_GetTypeLayout(
+ SlangReflection* reflection,
+ SlangReflectionType* inType,
+ SlangLayoutRules /*rules*/)
+{
+ auto context = convert(reflection);
+ auto type = convert(inType);
+ auto layoutContext = getInitialLayoutContextForTarget(context->targetRequest);
+ RefPtr<TypeLayout> result;
+ if (context->targetRequest->typeLayouts.TryGetValue(type, result))
+ return (SlangReflectionTypeLayout*)result.Ptr();
+ result = CreateTypeLayout(layoutContext, type);
+ context->targetRequest->typeLayouts[type] = result;
+ return (SlangReflectionTypeLayout*)result.Ptr();
+}
SLANG_API SlangReflectionType* spReflectionType_GetResourceResultType(SlangReflectionType* inType)
{
diff --git a/source/slang/type-layout.h b/source/slang/type-layout.h
index 904dacd91..38fabb009 100644
--- a/source/slang/type-layout.h
+++ b/source/slang/type-layout.h
@@ -456,6 +456,8 @@ public:
// a dummy sampler just to appease glslang
int bindingForHackSampler = 0;
RefPtr<VarDeclBase> hackSamplerVar;
+
+ TargetRequest* targetRequest = nullptr;
};
struct LayoutRulesFamilyImpl;