pad-to-attribute-validation.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 [[pad_to(N)]] attribute for struct, class and SRGs.
  15. '''
  16. def check_StructuredBuffer_Vs_ConstantBuffer_Padding(thefile, compilerPath, silent, expectedSize):
  17. # Compile the shader with --srg and check that the final size of the struct
  18. # is 256 for both the StructureBuffer<MyStruct> DemoSrg::m_mySB, and MyStruct DemoSrg::m_myStruct
  19. j, ok = testfuncs.buildAndGetJson(thefile, compilerPath, silent, ["--srg"])
  20. if ok:
  21. if not silent: print (fg.CYAN+ style.BRIGHT+ "checkPadding: Verifying struct sizes..."+ style.RESET_ALL)
  22. predicates = []
  23. predicates.append(lambda expectedSize=expectedSize: j["ShaderResourceGroups"][0]["inputsForBufferViews"][0]["stride"] == expectedSize)
  24. predicates.append(lambda: j["ShaderResourceGroups"][0]["inputsForBufferViews"][0]["type"] == "StructuredBuffer<MyStruct>")
  25. predicates.append(lambda expectedSize=expectedSize: j["ShaderResourceGroups"][0]["inputsForSRGConstants"][27]["constantByteSize"] == expectedSize)
  26. predicates.append(lambda: j["ShaderResourceGroups"][0]["inputsForSRGConstants"][27]["typeName"] == "/MyStruct")
  27. ok = testfuncs.verifyAllPredicates(predicates, j, silent)
  28. if ok and not silent:
  29. print (style.BRIGHT+ "OK! "+ str(len(predicates)) + " checkPadding: All sizes were the same." + style.RESET_ALL)
  30. return ok
  31. def check_SRG_Padding(thefile, compilerPath, silent, expectedSize):
  32. j, ok = testfuncs.buildAndGetJson(thefile, compilerPath, silent, ["--srg"])
  33. if ok:
  34. if not silent: print (fg.CYAN+ style.BRIGHT+ "check_SRG_Padding: Verifying SRG sizes..."+ style.RESET_ALL)
  35. #The offset + size of the last variable in each SRG must match the value of @expectedSize.
  36. srg1LastVariableOffset = j["ShaderResourceGroups"][0]["inputsForSRGConstants"][-1]["constantByteOffset"]
  37. srg1LastVariableSize= j["ShaderResourceGroups"][0]["inputsForSRGConstants"][-1]["constantByteSize"]
  38. srg1Size = srg1LastVariableOffset + srg1LastVariableSize
  39. srg2LastVariableOffset = j["ShaderResourceGroups"][1]["inputsForSRGConstants"][-1]["constantByteOffset"]
  40. srg2LastVariableSize= j["ShaderResourceGroups"][1]["inputsForSRGConstants"][-1]["constantByteSize"]
  41. srg2Size = srg2LastVariableOffset + srg2LastVariableSize
  42. ok = (srg1Size == srg2Size) and (srg1Size == expectedSize)
  43. if not ok and not silent:
  44. errorMsg = f"Was expecting both SRG sizes to be {expectedSize}, instead got SRG1 size={srg1Size} and SRG2 size={srg2Size}"
  45. print (fg.RED + "FAIL (" + errorMsg + "):" + style.RESET_ALL)
  46. if ok and not silent:
  47. print (style.BRIGHT+ "OK! check_SRG_Padding: All sizes were the same." + style.RESET_ALL)
  48. return ok
  49. result = 0 # to define for sub-tests
  50. resultFailed = 0
  51. def doTests(compiler, silent, azdxcpath):
  52. global result
  53. global resultFailed
  54. # Working directory should have been set to this script's directory by the calling parent
  55. # You can get it once doTests() is called, but not during initialization of the module,
  56. # because at that time it will still be set to the working directory of the calling script
  57. workDir = os.getcwd()
  58. if not silent: print ("testing [[pad_to(256)]] attribute...")
  59. if check_StructuredBuffer_Vs_ConstantBuffer_Padding(os.path.join(workDir, "struct-pad-to-256.azsl"), compiler, silent, 256): result += 1
  60. else: resultFailed += 1
  61. if not silent: print ("testing [[pad_to(252)]] attribute...")
  62. if check_StructuredBuffer_Vs_ConstantBuffer_Padding(os.path.join(workDir, "struct-pad-to-252.azsl"), compiler, silent, 252): result += 1
  63. else: resultFailed += 1
  64. if not silent: print ("testing [[pad_to(N)]] for SRGs...")
  65. if check_SRG_Padding(os.path.join(workDir, "srg-pad-to-256.azsl"), compiler, silent, 256): result += 1
  66. else: resultFailed += 1
  67. if __name__ == "__main__":
  68. print ("please call from testapp.py")