Skip to content

Commit deeb864

Browse files
chuckgenomeTim Foley
authored and
Tim Foley
committed
Add ability to obfuscate name when generating GLSL/HLSL source (shader-slang#1075)
1 parent 9a09e2e commit deeb864

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

slang.h

+3
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,9 @@ extern "C"
560560
/* Skip code generation step, just check the code and generate layout */
561561
SLANG_COMPILE_FLAG_NO_CODEGEN = 1 << 4,
562562

563+
/* Obfuscate shader names on release products */
564+
SLANG_COMPILE_FLAG_OBFUSCATE = 1 << 5,
565+
563566
/* Deprecated flags: kept around to allow existing applications to
564567
compile. Note that the relevant features will still be left in
565568
their default state. */

source/slang/slang-compiler.h

+3
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,9 @@ namespace Slang
16361636
//
16371637
bool useUnknownImageFormatAsDefault = false;
16381638

1639+
// Remove name hints to help obfuscate code
1640+
//
1641+
bool obfuscateCode = false;
16391642
private:
16401643
RefPtr<ComponentType> m_program;
16411644
};

source/slang/slang-emit-c-like.cpp

+20-7
Original file line numberDiff line numberDiff line change
@@ -637,23 +637,36 @@ String CLikeSourceEmitter::generateName(IRInst* inst)
637637
//
638638
if(auto nameHintDecoration = inst->findDecoration<IRNameHintDecoration>())
639639
{
640-
// The name we output will basically be:
640+
// The (non-obfuscated) name we output will basically be:
641641
//
642642
// <nameHint>_<uniqueID>
643643
//
644644
// Except that we will "scrub" the name hint first,
645645
// and we will omit the underscore if the (scrubbed)
646646
// name hint already ends with one.
647647
//
648-
649-
String nameHint = nameHintDecoration->getName();
650-
nameHint = scrubName(nameHint);
648+
// The obfuscated name we output will simply be:
649+
//
650+
// _<uniqueID>
651+
//
651652

652653
StringBuilder sb;
653-
sb.append(nameHint);
654654

655-
// Avoid introducing a double underscore
656-
if(!nameHint.endsWith("_"))
655+
if (!m_compileRequest->obfuscateCode)
656+
{
657+
658+
String nameHint = nameHintDecoration->getName();
659+
nameHint = scrubName(nameHint);
660+
661+
sb.append(nameHint);
662+
663+
// Avoid introducing a double underscore
664+
if (!nameHint.endsWith("_"))
665+
{
666+
sb.append("_");
667+
}
668+
}
669+
else
657670
{
658671
sb.append("_");
659672
}

source/slang/slang-options.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,10 @@ struct OptionsParser
775775
{
776776
requestImpl->getBackEndReq()->useUnknownImageFormatAsDefault = true;
777777
}
778+
else if (argStr == "-obfuscate")
779+
{
780+
requestImpl->getBackEndReq()->obfuscateCode = true;
781+
}
778782
else if (argStr == "-file-system")
779783
{
780784
String name;

0 commit comments

Comments
 (0)