From 22033f06573f900dc030c487b2c30feddf3d8f16 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Wed, 27 Jun 2018 16:53:48 -0400 Subject: Support for Tessellation (#607) * 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. --- source/slang/emit.cpp | 153 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 149 insertions(+), 4 deletions(-) (limited to 'source/slang/emit.cpp') diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index 1b8de806a..1a56c1d4d 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -3654,6 +3654,12 @@ struct EmitVisitor } break; + case kIROp_NotePatchConstantFunc: + { + // No-op + break; + } + case kIROp_Var: { auto ptrType = cast(inst->getDataType()); @@ -4129,7 +4135,98 @@ struct EmitVisitor } } + + + IRInst* findFirstInst(IRFunc* irFunc, IROp op) + { + for (auto block : irFunc->getBlocks()) + { + for (auto inst : block->getChildren()) + { + if (inst->op == op) + { + return inst; + } + } + } + return nullptr; + } + + void emitAttributeSingleString(const char* name, FuncDecl* entryPoint, Attribute* attrib) + { + assert(attrib); + + attrib->args.Count(); + if (attrib->args.Count() != 1) + { + SLANG_DIAGNOSE_UNEXPECTED(getSink(), entryPoint->loc, "Attribute expects single parameter"); + return; + } + + Expr* expr = attrib->args[0]; + + auto stringLitExpr = expr->As(); + if (!stringLitExpr) + { + SLANG_DIAGNOSE_UNEXPECTED(getSink(), entryPoint->loc, "Attribute parameter expecting to be a string "); + return; + } + + emit("["); + emit(name); + emit("(\""); + emit(stringLitExpr->value); + emit("\")]\n"); + } + + void emitAttributeSingleInt(const char* name, FuncDecl* entryPoint, Attribute* attrib) + { + assert(attrib); + + attrib->args.Count(); + if (attrib->args.Count() != 1) + { + SLANG_DIAGNOSE_UNEXPECTED(getSink(), entryPoint->loc, "Attribute expects single parameter"); + return; + } + + Expr* expr = attrib->args[0]; + + auto intLitExpr = expr->As(); + if (!intLitExpr) + { + SLANG_DIAGNOSE_UNEXPECTED(getSink(), entryPoint->loc, "Attribute expects an int"); + return; + } + + emit("["); + emit(name); + emit("("); + emit(intLitExpr->value); + emit(")]\n"); + } + + void emitFuncDeclPatchConstantFuncAttribute(IRFunc* irFunc, FuncDecl* entryPoint, PatchConstantFuncAttribute* attrib) + { + SLANG_UNUSED(attrib); + + auto irPatchFunc = static_cast(findFirstInst(irFunc, kIROp_NotePatchConstantFunc)); + assert(irPatchFunc); + if (!irPatchFunc) + { + SLANG_DIAGNOSE_UNEXPECTED(getSink(), entryPoint->loc, "Unable to find NotePatchConstantFunc instruction"); + return; + } + + const String irName = getIRName(irPatchFunc->getFunc()); + + emit("[patchconstantfunc(\""); + emit(irName); + emit("\")]\n"); + } + void emitIREntryPointAttributes_HLSL( + IRFunc* irFunc, EmitContext* ctx, EntryPointLayout* entryPointLayout) { @@ -4199,8 +4296,54 @@ struct EmitVisitor Emit(attrib->value); emit(")]\n"); } + break; + } + case Stage::Domain: + { + FuncDecl* entryPoint = entryPointLayout->entryPoint; + /* [domain("isoline")] */ + if (auto attrib = entryPoint->FindModifier()) + { + emitAttributeSingleString("domain", entryPoint, attrib); + } + + break; + } + case Stage::Hull: + { + // Lists these are only attributes for hull shader + // https://docs.microsoft.com/en-us/windows/desktop/direct3d11/direct3d-11-advanced-stages-hull-shader-design + + FuncDecl* entryPoint = entryPointLayout->entryPoint; + + /* [domain("isoline")] */ + if (auto attrib = entryPoint->FindModifier()) + { + emitAttributeSingleString("domain", entryPoint, attrib); + } + /* [domain("partitioning")] */ + if (auto attrib = entryPoint->FindModifier()) + { + emitAttributeSingleString("partitioning", entryPoint, attrib); + } + /* [outputtopology("line")] */ + if (auto attrib = entryPoint->FindModifier()) + { + emitAttributeSingleString("outputtopology", entryPoint, attrib); + } + /* [outputcontrolpoints(4)] */ + if (auto attrib = entryPoint->FindModifier()) + { + emitAttributeSingleInt("outputcontrolpoints", entryPoint, attrib); + } + /* [patchconstantfunc("HSConst")] */ + if (auto attrib = entryPoint->FindModifier()) + { + emitFuncDeclPatchConstantFuncAttribute(irFunc, entryPoint, attrib); + } + + break; } - break; // TODO: There are other stages that will need this kind of handling. default: break; @@ -4208,6 +4351,7 @@ struct EmitVisitor } void emitIREntryPointAttributes_GLSL( + IRFunc* /*irFunc*/, EmitContext* /*ctx*/, EntryPointLayout* entryPointLayout) { @@ -4310,17 +4454,18 @@ struct EmitVisitor } void emitIREntryPointAttributes( + IRFunc* irFunc, EmitContext* ctx, EntryPointLayout* entryPointLayout) { switch(getTarget(ctx)) { case CodeGenTarget::HLSL: - emitIREntryPointAttributes_HLSL(ctx, entryPointLayout); + emitIREntryPointAttributes_HLSL(irFunc, ctx, entryPointLayout); break; case CodeGenTarget::GLSL: - emitIREntryPointAttributes_GLSL(ctx, entryPointLayout); + emitIREntryPointAttributes_GLSL(irFunc, ctx, entryPointLayout); break; } } @@ -4390,7 +4535,7 @@ struct EmitVisitor auto entryPointLayout = asEntryPoint(func); if (entryPointLayout) { - emitIREntryPointAttributes(ctx, entryPointLayout); + emitIREntryPointAttributes(func, ctx, entryPointLayout); } auto name = getIRFuncName(func); -- cgit v1.2.3