GenerateShaderVariantListForAllShaders.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. """
  2. Copyright (c) Contributors to the Open 3D Engine Project.
  3. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. """
  6. import json
  7. import os
  8. import re
  9. import shutil
  10. import sys
  11. import azlmbr.bus
  12. import azlmbr.paths
  13. import collections
  14. import GenerateShaderVariantListUtil
  15. from PySide2 import QtWidgets
  16. def main():
  17. msgBox = QtWidgets.QMessageBox()
  18. msgBox.setIcon(QtWidgets.QMessageBox.Information)
  19. msgBox.setWindowTitle("Generate shader variant lists for project")
  20. msgBox.setText("This process generates shader variant lists for your project and all active gems. Your project's ShaderVariants folder will be deleted and replaced with new shader variant lists. Make sure that the asset processor has finished processing all shader and material assets before proceeding because they will be used as part of this process. Continue?")
  21. msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel)
  22. if msgBox.exec() != QtWidgets.QMessageBox.Ok:
  23. return
  24. print("==== Begin generating shader variant lists for all shaders ==========")
  25. projectShaderVariantListFolder = os.path.join(azlmbr.paths.projectroot, "ShaderVariants")
  26. projectShaderVariantListFolderTmp = os.path.join(azlmbr.paths.projectroot, "$tmp_ShaderVariants")
  27. # Remove the temporary shade of variant list folder in case it wasn't cleared last run.
  28. if os.path.exists(projectShaderVariantListFolderTmp):
  29. shutil.rmtree(projectShaderVariantListFolderTmp)
  30. paths = azlmbr.atomtools.util.GetPathsInSourceFoldersMatchingExtension('shader')
  31. progressDialog = QtWidgets.QProgressDialog("Batch generating shader variant lists...", "Cancel", 0, len(paths))
  32. progressDialog.setMaximumWidth(400)
  33. progressDialog.setMaximumHeight(100)
  34. progressDialog.setModal(True)
  35. progressDialog.setWindowTitle("Batch generating shader variant lists")
  36. for i, path in enumerate(paths):
  37. shaderVariantList = GenerateShaderVariantListUtil.create_shadervariantlist_for_shader(path)
  38. if len(shaderVariantList.shaderVariants) == 0:
  39. continue
  40. relativePath = azlmbr.shadermanagementconsole.ShaderManagementConsoleRequestBus(azlmbr.bus.Broadcast, 'GenerateRelativeSourcePath', path)
  41. pre, ext = os.path.splitext(relativePath)
  42. savePath = os.path.join(projectShaderVariantListFolderTmp, f'{pre}.shadervariantlist')
  43. azlmbr.shader.SaveShaderVariantListSourceData(savePath, shaderVariantList)
  44. progressDialog.setValue(i)
  45. # processing events to update UI after progress bar changes
  46. QtWidgets.QApplication.processEvents()
  47. # Allowing the application to process idle events for one frame to update systems and garbage collect graphics resources
  48. azlmbr.atomtools.general.idle_wait_frames(1)
  49. if progressDialog.wasCanceled():
  50. return
  51. progressDialog.close()
  52. # Remove the final shader variant list folder and all of its contents
  53. if os.path.exists(projectShaderVariantListFolder):
  54. shutil.rmtree(projectShaderVariantListFolder)
  55. # Rename the temporary shader variant list folder to replace the final one
  56. if os.path.exists(projectShaderVariantListFolderTmp):
  57. os.rename(projectShaderVariantListFolderTmp, projectShaderVariantListFolder)
  58. print("==== Finish generating shader variant lists for all shaders ==========")
  59. if __name__ == "__main__":
  60. main()