Skip to content

Commit

Permalink
Support for Tessellation (#607)
Browse files Browse the repository at this point in the history
* Fix typo OuptutTopologyAttribute -> OutputTopologyAttribute
First pass support for handing tesselation shaders - domain and hull.

* Added attribute PatchConstantFuncAttribute
* Added  visitHLSLPatchType(HLSLPatchType* type) such that the patch type template parameters are handled
* Added IRNotePatchConstantFunc - such that the patch constant function is referenced within IR
* Added support for outputing typical tesselation attributes (although minimal validation is performed)
* Added findFunctionDeclByName

* Small improvements to diagnostic.

* Improved diagnostics and checking for geometry shader attributes.

* Added diagnostic if patchconstantfunc is not found
Handle assert failure when outputing a domain shader alone and therefore attr->patchConstantFuncDecl is not set.

* Simple script tess.hlsl to test out domain/hull shaders.

* Added url for where hull shader attributes are defined.

* Fix unsigned/signed comparison warning.

* Restore removal of fix in "Improve generic argument inference for builtins (#598)"

* Update tessellation test case to compare against fxc

The test was previously comparing against fixed expected DXBC output, but this caused problems when the test runner tried to execute the test on Linux (where there is no fxc to invoke...), and would also be a potential source of problems down the road if different users run using different builds of fxc.

The simple solution here is to convert the test to compare against fxc output generated on the fly. That test type is already filtered out on non-Windows builds, so it eliminates the portability issue (in a crude way).

I also changed the test to compile both entry points in one compiler invocation, just to streamline things into fewer distinct tests.

* Eliminate unnecessary call to `lowerFuncDecl`

In a very obscure case this could cause a bug, if the patch-constant function had somehow already been lowered (because it was called somewhere else in the code).

The call should not be needed because `ensureDecl` will lower a declaration on-demand if required, so eliminating it causes no problems for code that wouldn't be in that extreme corner case.
  • Loading branch information
jsmall-zzz authored and Tim Foley committed Jun 27, 2018
1 parent 4bbd0e7 commit 22033f0
Show file tree
Hide file tree
Showing 11 changed files with 496 additions and 18 deletions.
176 changes: 168 additions & 8 deletions source/slang/check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1845,6 +1845,38 @@ namespace Slang
return Stage::Unknown;
}

bool hasIntArgs(Attribute* attr, int numArgs)
{
if (int(attr->args.Count()) != numArgs)
{
return false;
}
for (int i = 0; i < numArgs; ++i)
{
if (!attr->args[i]->As<IntegerLiteralExpr>())
{
return false;
}
}
return true;
}

bool hasStringArgs(Attribute* attr, int numArgs)
{
if (int(attr->args.Count()) != numArgs)
{
return false;
}
for (int i = 0; i < numArgs; ++i)
{
if (!attr->args[i]->As<StringLiteralExpr>())
{
return false;
}
}
return true;
}

bool validateAttribute(RefPtr<Attribute> attr)
{
if(auto numThreadsAttr = attr.As<NumThreadsAttribute>())
Expand Down Expand Up @@ -1898,7 +1930,27 @@ namespace Slang

entryPointAttr->stage = stage;
}
else
else if ((attr.As<DomainAttribute>()) ||
(attr.As<MaxTessFactorAttribute>()) ||
(attr.As<OutputTopologyAttribute>()) ||
(attr.As<PartitioningAttribute>()) ||
(attr.As<PatchConstantFuncAttribute>()))
{
// Let it go thru iff single string attribute
if (!hasStringArgs(attr, 1))
{
getSink()->diagnose(attr, Diagnostics::expectedSingleStringArg, attr->name);
}
}
else if (attr.As<OutputControlPointsAttribute>())
{
// Let it go thru iff single integral attribute
if (!hasIntArgs(attr, 1))
{
getSink()->diagnose(attr, Diagnostics::expectedSingleIntArg, attr->name);
}
}
else
{
if(attr->args.Count() == 0)
{
Expand Down Expand Up @@ -8231,18 +8283,89 @@ namespace Slang
return type;
}


FuncDecl* findFunctionDeclByName(EntryPointRequest* entryPoint, Name* name)
{
auto translationUnit = entryPoint->getTranslationUnit();
auto sink = &entryPoint->compileRequest->mSink;
auto translationUnitSyntax = translationUnit->SyntaxNode;

// Make sure we've got a query-able member dictionary
buildMemberDictionary(translationUnitSyntax);

// We will look up any global-scope declarations in the translation
// unit that match the name of our entry point.
Decl* firstDeclWithName = nullptr;
if (!translationUnitSyntax->memberDictionary.TryGetValue(name, firstDeclWithName))
{
// If there doesn't appear to be any such declaration, then we are done.

sink->diagnose(translationUnitSyntax, Diagnostics::entryPointFunctionNotFound, name);

return nullptr;
}

// We found at least one global-scope declaration with the right name,
// but (1) it might not be a function, and (2) there might be
// more than one function.
//
// We'll walk the linked list of declarations with the same name,
// to see what we find. Along the way we'll keep track of the
// first function declaration we find, if any:
FuncDecl* entryPointFuncDecl = nullptr;
for (auto ee = firstDeclWithName; ee; ee = ee->nextInContainerWithSameName)
{
// Is this declaration a function?
if (auto funcDecl = dynamic_cast<FuncDecl*>(ee))
{
// Skip non-primary declarations, so that
// we don't give an error when an entry
// point is forward-declared.
if (!isPrimaryDecl(funcDecl))
continue;

// is this the first one we've seen?
if (!entryPointFuncDecl)
{
// If so, this is a candidate to be
// the entry point function.
entryPointFuncDecl = funcDecl;
}
else
{
// Uh-oh! We've already seen a function declaration with this
// name before, so the whole thing is ambiguous. We need
// to diagnose and bail out.

sink->diagnose(translationUnitSyntax, Diagnostics::ambiguousEntryPoint, name);

// List all of the declarations that the user *might* mean
for (auto ff = firstDeclWithName; ff; ff = ff->nextInContainerWithSameName)
{
if (auto candidate = dynamic_cast<FuncDecl*>(ff))
{
sink->diagnose(candidate, Diagnostics::entryPointCandidate, candidate->getName());
}
}

// Bail out.
return nullptr;
}
}
}

return entryPointFuncDecl;
}

// Validate that an entry point function conforms to any additional
// constraints based on the stage (and profile?) it specifies.
void validateEntryPoint(
EntryPointRequest* /*entryPoint*/)
EntryPointRequest* entryPoint)
{
// TODO: We currently don't do any checking here, but this is the
// TODO: We currently do minimal checking here, but this is the
// right place to perform the following validation checks:
//
// * Does the entry point specify all of the attributes required
// by the chosen stage (e.g., a `[domain(...)]` attribute for]
// a hull shader.
//

// * Are the function input/output parameters and result type
// all valid for the chosen stage? (e.g., there shouldn't be
// an `OutputStream<X>` type in a vertex shader signature)
Expand All @@ -8263,6 +8386,43 @@ namespace Slang
// `Texture2D.Sample`, then that should produce an error because
// that function is specific to the fragment profile/stage.
//

if (entryPoint->getStage() == Stage::Hull)
{
auto translationUnit = entryPoint->getTranslationUnit();
auto sink = &entryPoint->compileRequest->mSink;
auto translationUnitSyntax = translationUnit->SyntaxNode;

auto attr = entryPoint->decl->FindModifier<PatchConstantFuncAttribute>();

if (attr)
{
if (attr->args.Count() != 1)
{
sink->diagnose(translationUnitSyntax, Diagnostics::badlyDefinedPatchConstantFunc, entryPoint->name);
return;
}

Expr* expr = attr->args[0];
StringLiteralExpr* stringLit = expr->As<StringLiteralExpr>();

if (!stringLit)
{
sink->diagnose(translationUnitSyntax, Diagnostics::badlyDefinedPatchConstantFunc, entryPoint->name);
return;
}

Name* name = entryPoint->compileRequest->getNamePool()->getName(stringLit->value);
FuncDecl* funcDecl = findFunctionDeclByName(entryPoint, name);
if (!funcDecl)
{
sink->diagnose(translationUnitSyntax, Diagnostics::attributeFunctionNotFound, name, "patchconstantfunc");
return;
}

attr->patchConstantFuncDecl = funcDecl;
}
}
}

// Given an `EntryPointRequest` specified via API or command line options,
Expand Down Expand Up @@ -8445,7 +8605,7 @@ namespace Slang
// set to `Batman` to know whether the setting for `B` is valid. In this limit
// the constraints can be mutually recursive (so `A : IMentor<B>`).
//
// The only way to check things corectly is to validate each conformance under
// The only way to check things correctly is to validate each conformance under
// a set of assumptions (substitutions) that includes all the type substitutions,
// and possibly also all the other constraints *except* the one to be validated.
//
Expand Down
2 changes: 1 addition & 1 deletion source/slang/core.meta.slang
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ __attributeTarget(FuncDecl)
attribute_syntax [outputcontrolpoints(count: int)] : OutputControlPointsAttribute;

__attributeTarget(FuncDecl)
attribute_syntax [outputtopology(topology)] : OuptutTopologyAttribute;
attribute_syntax [outputtopology(topology)] : OutputTopologyAttribute;

__attributeTarget(FuncDecl)
attribute_syntax [partitioning(mode)] : PartitioningAttribute;
Expand Down
2 changes: 1 addition & 1 deletion source/slang/core.meta.slang.h
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,7 @@ SLANG_RAW("__attributeTarget(FuncDecl)\n")
SLANG_RAW("attribute_syntax [outputcontrolpoints(count: int)] : OutputControlPointsAttribute;\n")
SLANG_RAW("\n")
SLANG_RAW("__attributeTarget(FuncDecl)\n")
SLANG_RAW("attribute_syntax [outputtopology(topology)] : OuptutTopologyAttribute;\n")
SLANG_RAW("attribute_syntax [outputtopology(topology)] : OutputTopologyAttribute;\n")
SLANG_RAW("\n")
SLANG_RAW("__attributeTarget(FuncDecl)\n")
SLANG_RAW("attribute_syntax [partitioning(mode)] : PartitioningAttribute;\n")
Expand Down
13 changes: 11 additions & 2 deletions source/slang/diagnostic-defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,20 @@ DIAGNOSTIC(33071, Error, expectedAStringLiteral, "expected a string literal")
// Attributes
DIAGNOSTIC(31000, Error, unknownAttributeName, "unknown attribute '$0'")
DIAGNOSTIC(31001, Error, attributeArgumentCountMismatch, "attribute '$0' expects $1 arguments ($2 provided)")
DIAGNOSTIC(31001, Error, attributeNotApplicable, "attribute '$0' is not valid here")
DIAGNOSTIC(31002, Error, attributeNotApplicable, "attribute '$0' is not valid here")

DIAGNOSTIC(31003, Error, badlyDefinedPatchConstantFunc, "hull shader '$0' has has badly defined 'patchconstantfunc' attribute.");

DIAGNOSTIC(31004, Error, expectedSingleIntArg, "attribute '$0' expects a single int argument")
DIAGNOSTIC(31005, Error, expectedSingleStringArg, "attribute '$0' expects a single string argument")

DIAGNOSTIC(31006, Error, attributeFunctionNotFound, "Could not find function '$0' for attribute'$1'")


DIAGNOSTIC(31100, Error, unknownStageName, "unknown stage name '$0'")



// Enums

DIAGNOSTIC(32000, Error, invalidEnumTagType, "invalid tag type for 'enum': '$0'")
Expand Down Expand Up @@ -265,7 +275,6 @@ DIAGNOSTIC(39999, Error, invalidFloatingPOintLiteralSuffix, "invalid suffix '$0'




DIAGNOSTIC(38000, Error, entryPointFunctionNotFound, "no function found matching entry point name '$0'")
DIAGNOSTIC(38001, Error, ambiguousEntryPoint, "more than one function matches entry point name '$0'")
DIAGNOSTIC(38002, Note, entryPointCandidate, "see candidate declaration for entry point '$0'")
Expand Down
Loading

0 comments on commit 22033f0

Please sign in to comment.