diff options
| author | Yong He <yonghe@outlook.com> | 2018-01-03 18:09:35 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-01-03 18:09:35 -0800 |
| commit | e90dfcfd6a0a6d92688012b1216c46c24965cfc0 (patch) | |
| tree | 89b1e0e3f0e95ea0bf586bd67fa36066f0021ce7 | |
| parent | 5da16a6360e40b9fd4d2275a5ef5b1af740c4abb (diff) | |
| parent | 550405d2de2ca617046e73fe5ec7e5e1765a5c97 (diff) | |
Merge pull request #349 from csyonghe/master
Add API for querying TypeLayout from a Type
| -rw-r--r-- | slang.h | 9 | ||||
| -rw-r--r-- | source/slang/check.cpp | 5 | ||||
| -rw-r--r-- | source/slang/compiler.h | 9 | ||||
| -rw-r--r-- | source/slang/parameter-binding.cpp | 3 | ||||
| -rw-r--r-- | source/slang/reflection.cpp | 36 | ||||
| -rw-r--r-- | source/slang/slang.cpp | 16 | ||||
| -rw-r--r-- | source/slang/type-layout.h | 2 |
7 files changed, 79 insertions, 1 deletions
@@ -633,6 +633,12 @@ extern "C" SLANG_STAGE_PIXEL = SLANG_STAGE_FRAGMENT, }; + typedef SlangUInt32 SlangLayoutRules; + enum + { + SLANG_LAYOUT_RULES_DEFAULT, + }; + // Type Reflection SLANG_API SlangTypeKind spReflectionType_GetKind(SlangReflectionType* type); @@ -739,6 +745,9 @@ extern "C" SLANG_API SlangReflectionTypeParameter* spReflection_GetTypeParameterByIndex(SlangReflection* reflection, unsigned int index); SLANG_API SlangReflectionTypeParameter* spReflection_FindTypeParameter(SlangReflection* reflection, char const* name); + SLANG_API SlangReflectionType* spReflection_FindTypeByName(SlangReflection* reflection, char const* name); + SLANG_API SlangReflectionTypeLayout* spReflection_GetTypeLayout(SlangReflection* reflection, SlangReflectionType* reflectionType, SlangLayoutRules rules); + SLANG_API SlangUInt spReflection_getEntryPointCount(SlangReflection* reflection); SLANG_API SlangReflectionEntryPoint* spReflection_getEntryPointByIndex(SlangReflection* reflection, SlangUInt index); diff --git a/source/slang/check.cpp b/source/slang/check.cpp index bc5d144b0..46ed9da15 100644 --- a/source/slang/check.cpp +++ b/source/slang/check.cpp @@ -5348,6 +5348,8 @@ namespace Slang DeclRef<GenericDecl> genericDeclRef, OverloadResolveContext& context) { + EnsureDecl(genericDeclRef.getDecl()); + ConstraintSystem constraints; constraints.genericDecl = genericDeclRef.getDecl(); @@ -6775,7 +6777,8 @@ namespace Slang // Lookup generic parameter types in global scope for (auto name : entryPoint->genericParameterTypeNames) { - if (!translationUnitSyntax->memberDictionary.TryGetValue(name, firstDeclWithName)) + firstDeclWithName = entryPoint->compileRequest->lookupGlobalDecl(name); + if (!firstDeclWithName) { // If there doesn't appear to be any such declaration, then // we need to diagnose it as an error, and then bail out. diff --git a/source/slang/compiler.h b/source/slang/compiler.h index 0e85a1088..1fca4751c 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; @@ -366,6 +373,8 @@ namespace Slang Name* name, SourceLoc const& loc); + Decl* lookupGlobalDecl(Name* name); + SourceManager* getSourceManager() { return sourceManager; 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..c9de75d6e 100644 --- a/source/slang/reflection.cpp +++ b/source/slang/reflection.cpp @@ -428,6 +428,42 @@ 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(); + + auto nameObj = compileRequest->getNamePool()->getName(name); + Decl* resultDecl = compileRequest->lookupGlobalDecl(nameObj); + 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/slang.cpp b/source/slang/slang.cpp index 3156e5008..0b3e0ceb7 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -590,6 +590,22 @@ RefPtr<ModuleDecl> CompileRequest::findOrImportModule( loc); } +Decl * CompileRequest::lookupGlobalDecl(Name * name) +{ + Decl* resultDecl = nullptr; + for (auto module : loadedModulesList) + { + if (module->moduleDecl->memberDictionary.TryGetValue(name, resultDecl)) + break; + } + for (auto transUnit : translationUnits) + { + if (transUnit->SyntaxNode->memberDictionary.TryGetValue(name, resultDecl)) + break; + } + return resultDecl; +} + RefPtr<ModuleDecl> findOrImportModule( CompileRequest* request, Name* name, diff --git a/source/slang/type-layout.h b/source/slang/type-layout.h index ed4c3eda5..66f6025b4 100644 --- a/source/slang/type-layout.h +++ b/source/slang/type-layout.h @@ -464,6 +464,8 @@ public: // a dummy sampler just to appease glslang int bindingForHackSampler = 0; RefPtr<VarDeclBase> hackSamplerVar; + + TargetRequest* targetRequest = nullptr; }; struct LayoutRulesFamilyImpl; |
