build_webgpu_cmd_buffer.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/usr/bin/env python3
  2. # Copyright 2018 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """code generator for webgpu command buffers."""
  6. import filecmp
  7. import os
  8. import os.path
  9. import sys
  10. from optparse import OptionParser
  11. import build_cmd_buffer_lib
  12. # Named type info object represents a named type that is used in API call
  13. # arguments. The named types are used in 'webgpu_cmd_buffer_functions.txt'.
  14. #
  15. # Options are documented in build_gles2_cmd_buffer.py/build_raster_cmd_buffer.py
  16. _NAMED_TYPE_INFO = {
  17. 'PowerPreference': {
  18. 'type': 'PowerPreference',
  19. 'valid': [
  20. 'PowerPreference::kDefault',
  21. 'PowerPreference::kHighPerformance',
  22. 'PowerPreference::kLowPower',
  23. ],
  24. 'invalid': [
  25. 'PowerPreference::kNumPowerPreferences',
  26. ],
  27. },
  28. 'MailboxFlags': {
  29. 'type': 'MailboxFlags',
  30. 'valid': [
  31. 'WEBGPU_MAILBOX_NONE',
  32. 'WEBGPU_MAILBOX_DISCARD',
  33. ],
  34. },
  35. }
  36. # A function info object specifies the type and other special data for the
  37. # command that will be generated. A base function info object is generated by
  38. # parsing the "webgpu_cmd_buffer_functions.txt", one for each function in the
  39. # file. These function info objects can be augmented and their values can be
  40. # overridden by adding an object to the table below.
  41. #
  42. # Must match function names specified in "webgpu_cmd_buffer_functions.txt".
  43. #
  44. # Options are documented in build_gles2_cmd_buffer.py/build_raster_cmd_buffer.py
  45. # (Note: some options (like decoder_func and unit_test) currently have no
  46. # effect, because WriteServiceImplementation and WriteServiceUnitTests are not
  47. # used below.)
  48. _FUNCTION_INFO = {
  49. 'DawnCommands': {
  50. 'impl_func': False,
  51. 'internal': True,
  52. 'data_transfer_methods': ['shm'],
  53. 'cmd_args': 'uint32_t commands_shm_id, '
  54. 'uint32_t commands_shm_offset, uint32_t size',
  55. 'size_args': {
  56. 'commands': 'size * sizeof(char)',
  57. },
  58. },
  59. 'AssociateMailbox': {
  60. 'impl_func': False,
  61. 'client_test': False,
  62. 'type': 'PUT',
  63. 'count': 16, # GL_MAILBOX_SIZE_CHROMIUM
  64. },
  65. 'DissociateMailbox': {
  66. 'impl_func': False,
  67. 'client_test': False,
  68. },
  69. 'DissociateMailboxForPresent': {
  70. 'impl_func': False,
  71. 'client_test': False,
  72. },
  73. 'DestroyServer': {
  74. 'impl_func': False,
  75. 'internal': True,
  76. },
  77. }
  78. def main(argv):
  79. """This is the main function."""
  80. parser = OptionParser()
  81. parser.add_option(
  82. "--output-dir",
  83. help="Output directory for generated files. Defaults to chromium root "
  84. "directory.")
  85. parser.add_option(
  86. "-v", "--verbose", action="store_true", help="Verbose logging output.")
  87. parser.add_option(
  88. "-c", "--check", action="store_true",
  89. help="Check if output files match generated files in chromium root "
  90. "directory. Use this in PRESUBMIT scripts with --output-dir.")
  91. (options, _) = parser.parse_args(args=argv)
  92. # This script lives under src/gpu/command_buffer.
  93. script_dir = os.path.dirname(os.path.abspath(__file__))
  94. assert script_dir.endswith(os.path.normpath("src/gpu/command_buffer"))
  95. # os.path.join doesn't do the right thing with relative paths.
  96. chromium_root_dir = os.path.abspath(script_dir + "/../..")
  97. # Support generating files under gen/ and for PRESUBMIT.
  98. if options.output_dir:
  99. output_dir = options.output_dir
  100. else:
  101. output_dir = chromium_root_dir
  102. os.chdir(output_dir)
  103. # This script lives under gpu/command_buffer, cd to base directory.
  104. build_cmd_buffer_lib.InitializePrefix("WebGPU")
  105. gen = build_cmd_buffer_lib.GLGenerator(
  106. options.verbose, "2018", _FUNCTION_INFO, _NAMED_TYPE_INFO,
  107. chromium_root_dir)
  108. gen.ParseGLH("gpu/command_buffer/webgpu_cmd_buffer_functions.txt")
  109. gen.WriteCommandIds("gpu/command_buffer/common/webgpu_cmd_ids_autogen.h")
  110. gen.WriteFormat("gpu/command_buffer/common/webgpu_cmd_format_autogen.h")
  111. gen.WriteFormatTest(
  112. "gpu/command_buffer/common/webgpu_cmd_format_test_autogen.h")
  113. gen.WriteGLES2InterfaceHeader(
  114. "gpu/command_buffer/client/webgpu_interface_autogen.h")
  115. gen.WriteGLES2ImplementationHeader(
  116. "gpu/command_buffer/client/webgpu_implementation_autogen.h")
  117. gen.WriteGLES2InterfaceStub(
  118. "gpu/command_buffer/client/webgpu_interface_stub_autogen.h")
  119. gen.WriteGLES2InterfaceStubImpl(
  120. "gpu/command_buffer/client/webgpu_interface_stub_impl_autogen.h")
  121. gen.WriteGLES2Implementation(
  122. "gpu/command_buffer/client/webgpu_implementation_impl_autogen.h")
  123. gen.WriteGLES2ImplementationUnitTests(
  124. "gpu/command_buffer/client/webgpu_implementation_unittest_autogen.h")
  125. gen.WriteCmdHelperHeader(
  126. "gpu/command_buffer/client/webgpu_cmd_helper_autogen.h")
  127. # Note: No gen.WriteServiceImplementation
  128. # Note: No gen.WriteServiceUnitTests
  129. gen.WriteServiceUtilsHeader(
  130. "gpu/command_buffer/service/webgpu_cmd_validation_autogen.h")
  131. gen.WriteServiceUtilsImplementation(
  132. "gpu/command_buffer/service/"
  133. "webgpu_cmd_validation_implementation_autogen.h")
  134. build_cmd_buffer_lib.Format(gen.generated_cpp_filenames, output_dir,
  135. chromium_root_dir)
  136. if gen.errors > 0:
  137. print("build_webgpu_cmd_buffer.py: Failed with %d errors" % gen.errors)
  138. return 1
  139. check_failed_filenames = []
  140. if options.check:
  141. for filename in gen.generated_cpp_filenames:
  142. if not filecmp.cmp(os.path.join(output_dir, filename),
  143. os.path.join(chromium_root_dir, filename)):
  144. check_failed_filenames.append(filename)
  145. if len(check_failed_filenames) > 0:
  146. print('Please run gpu/command_buffer/build_webgpu_cmd_buffer.py')
  147. print('Failed check on autogenerated command buffer files:')
  148. for filename in check_failed_filenames:
  149. print(filename)
  150. return 1
  151. return 0
  152. if __name__ == '__main__':
  153. sys.exit(main(sys.argv[1:]))