-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Specialization constants for shader options (#88)
* Add support for specialization constants * Add support for specialization constants on Metal * Add tests * Update lint files Signed-off-by: Akio Gaule <[email protected]>
- Loading branch information
Showing
24 changed files
with
360 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (c) Contributors to the Open 3D Engine Project. | ||
* For complete copyright and license terms please see the LICENSE at the root of this distribution. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 OR MIT | ||
* | ||
*/ | ||
|
||
#include <AzslcEmitter.h> | ||
#include "CommonVulkanPlatformEmitter.h" | ||
|
||
namespace AZ::ShaderCompiler | ||
{ | ||
string CommonVulkanPlatformEmitter::GetSpecializationConstant(const CodeEmitter& codeEmitter, const IdentifierUID& symbolUid, const Options& options) const | ||
{ | ||
std::stringstream stream; | ||
auto* ir = codeEmitter.GetIR(); | ||
auto* varInfo = ir->GetSymbolSubAs<VarInfo>(symbolUid.GetName()); | ||
auto retInfo = varInfo->GetTypeRefInfo(); | ||
string typeAsStr = codeEmitter.GetTranslatedName(retInfo, UsageContext::ReferenceSite); | ||
string defaultValue = codeEmitter.GetInitializerClause(varInfo); | ||
Modifiers forbidden = StorageFlag::Static; | ||
assert(varInfo->m_specializationId >= 0); | ||
stream << "[[vk::constant_id(" << varInfo->m_specializationId << ")]]\n"; | ||
if (retInfo.m_typeClass == TypeClass::Enum) | ||
{ | ||
// Enums are not a valid type for specialization constant, so we use the underlaying scalar type. | ||
// The we add a global static variable with the enum type that cast from the specialization constant. | ||
auto* enumClassInfo = ir->GetSymbolSubAs<ClassInfo>(retInfo.m_typeId.GetName()); | ||
auto& enumerators = enumClassInfo->GetOrderedMembers(); | ||
auto scalarType = enumClassInfo->Get<EnumerationInfo>()->m_underlyingType.m_arithmeticInfo.UnderlyingScalarToStr(); | ||
string scName = JoinAllNestedNamesWithUnderscore(symbolUid.m_name) + "_SC_OPTION"; | ||
stream << "const " << scalarType << " " << scName; | ||
// TODO: if using a default value, emit it as the underlying scalar type, since enums are not valid default values for | ||
// specialization constants (casting is also not allowed). We set default values for shader options at runtime so it's not | ||
// a problem. | ||
stream << " = (" << scalarType << ")" << "0;\n"; | ||
// Set the default value for the global static variable as the value of the specialization constant. | ||
defaultValue = std::move(scName); | ||
forbidden = Modifiers{}; | ||
} | ||
stream << codeEmitter.GetTranslatedName(varInfo->m_typeInfoExt, UsageContext::ReferenceSite, options, forbidden) + " "; | ||
stream << codeEmitter.GetTranslatedName(symbolUid.m_name, UsageContext::DeclarationSite) + " = "; | ||
stream << "(" << typeAsStr << ")" << (defaultValue.empty() ? "0" : defaultValue) << "; \n"; | ||
return stream.str(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (c) Contributors to the Open 3D Engine Project. | ||
* For complete copyright and license terms please see the LICENSE at the root of this distribution. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 OR MIT | ||
* | ||
*/ | ||
#pragma once | ||
|
||
#include <AzslcPlatformEmitter.h> | ||
|
||
namespace AZ::ShaderCompiler | ||
{ | ||
// PlatformEmitter is not a Backend by design. It's a supplement to CodeEmitter, not a replacement | ||
struct CommonVulkanPlatformEmitter : PlatformEmitter | ||
{ | ||
public: | ||
[[nodiscard]] | ||
string GetSpecializationConstant(const CodeEmitter& codeEmitter, const IdentifierUID& symbol, const Options& options) const override; | ||
|
||
protected: | ||
CommonVulkanPlatformEmitter() : PlatformEmitter {} {}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,5 +79,4 @@ namespace AZ::ShaderCompiler | |
} | ||
return { stream.str(), registerString }; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.