srg-strip-unused.py 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """
  4. Copyright (c) Contributors to the Open 3D Engine Project.
  5. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  6. SPDX-License-Identifier: Apache-2.0 OR MIT
  7. """
  8. import sys
  9. import os
  10. sys.path.append("..")
  11. from clr import *
  12. import testfuncs
  13. '''
  14. Validates the functionality of the --strip-unused-srgs flag.
  15. '''
  16. def testUnusedSrgStripping(thefile, compilerPath, silent, aliveSrgs, deadSrgs):
  17. # First we compile as is, no srg stripping, and make sure all SRGs are present in the symbol output
  18. symbols, ok = testfuncs.buildAndGetSymbols(thefile, compilerPath, silent)
  19. if ok:
  20. if not silent: print (fg.CYAN+ style.BRIGHT+ "testUnusedSrgStripping: Verifying no srg was stripped..."+ style.RESET_ALL)
  21. predicates = []
  22. for srgName in aliveSrgs + deadSrgs:
  23. symbolExpr = "Symbol '{}'".format(srgName)
  24. predicates.append(lambda symbolExpr=symbolExpr: symbols[symbolExpr]['kind'] == 'ShaderResourceGroup')
  25. ok = testfuncs.verifyAllPredicates(predicates, symbols, silent)
  26. if ok and not silent:
  27. print (style.BRIGHT+ "OK! "+ str(len(predicates)) + " testUnusedSrgStripping: Verified no srg was stripped."+ style.RESET_ALL)
  28. else:
  29. return False
  30. # Now we force unused srg removal and make sure that only @aliveSrgs are present and all @deadSrgs got removed.
  31. symbols, ok = testfuncs.buildAndGetSymbols(thefile, compilerPath, silent, ["--strip-unused-srgs"])
  32. if ok:
  33. if not silent: print (fg.CYAN+ style.BRIGHT+ "testUnusedSrgStripping: Verifying srgs were stripped..."+ style.RESET_ALL)
  34. predicates = []
  35. for srgName in aliveSrgs:
  36. symbolExpr = "Symbol '{}'".format(srgName)
  37. predicates.append(lambda symbolExpr=symbolExpr: symbols[symbolExpr]['kind'] == 'ShaderResourceGroup')
  38. for srgName in deadSrgs:
  39. symbolExpr = "Symbol '{}'".format(srgName)
  40. predicates.append(lambda symbolExpr=symbolExpr: symbolExpr not in symbols)
  41. ok = testfuncs.verifyAllPredicates(predicates, symbols, silent)
  42. if ok and not silent:
  43. print (style.BRIGHT+ "OK! "+ str(len(predicates)) + " testUnusedSrgStripping: Verified srgs were stripped."+ style.RESET_ALL)
  44. return ok
  45. result = 0 # to define for sub-tests
  46. resultFailed = 0
  47. def doTests(compiler, silent, azdxcpath):
  48. global result
  49. global resultFailed
  50. # Working directory should have been set to this script's directory by the calling parent
  51. # You can get it once doTests() is called, but not during initialization of the module,
  52. # because at that time it will still be set to the working directory of the calling script
  53. workDir = os.getcwd()
  54. if not silent: print ("testing for removal of two SRGs...")
  55. if testUnusedSrgStripping(os.path.join(workDir, "srg-strip-unused-SRG2-SRG3.azsl"), compiler, silent,
  56. aliveSrgs=["/SRG1"], deadSrgs=["/SRG2", "/SRG3"] ): result += 1
  57. else: resultFailed += 1
  58. if not silent: print ("testing for removal of one SRG...")
  59. if testUnusedSrgStripping(os.path.join(workDir, "srg-strip-unused-SRG3.azsl"), compiler, silent,
  60. aliveSrgs=["/SRG1", "/SRG2"], deadSrgs=["/SRG3"] ): result += 1
  61. else: resultFailed += 1
  62. if not silent: print ("testing for survival of all SRGs...")
  63. if testUnusedSrgStripping(os.path.join(workDir, "srg-strip-unused-none.azsl"), compiler, silent,
  64. aliveSrgs=["/SRG1", "/SRG2", "/SRG3"], deadSrgs=[] ): result += 1
  65. else: resultFailed += 1
  66. if not silent: print ("testing for removal of unused partial SRG...")
  67. if testUnusedSrgStripping(os.path.join(workDir, "srg-strip-unused-partial.azsl"), compiler, silent,
  68. aliveSrgs=["/MainSRG"], deadSrgs=["/PartialSRG1", "/CompleteSRG2"] ): result += 1
  69. else: resultFailed += 1
  70. if not silent: print ("testing for survival of all SRGs because one of them has the fallback key...")
  71. if testUnusedSrgStripping(os.path.join(workDir, "srg-strip-unused-none-fallback.azsl"), compiler, silent,
  72. aliveSrgs=["/SRG1", "/SRG2", "/SRG3"], deadSrgs=[] ): result += 1
  73. else: resultFailed += 1
  74. if __name__ == "__main__":
  75. print ("please call from testapp.py")