Debian Codegraph @

src/gfxstream/codegen/scripts/cerealgenerator.py

raw
1 # Copyright 2023 Google LLC
2 # SPDX-License-Identifier: MIT
3
4 import os, re, sys
5 from generator import *
6 from pathlib import Path, PurePosixPath
7
8 import cereal
9 from cereal.wrapperdefs import VULKAN_STREAM_TYPE
10 from cereal.wrapperdefs import VULKAN_STREAM_TYPE_GUEST
11
12 # CerealGenerator - generates set of driver sources
13 # while being agnostic to the stream implementation
14 from reg import GroupInfo, TypeInfo, EnumInfo
15
16 VK_CEREAL_FLAG_HOST = 1
17 VK_CEREAL_FLAG_GUEST = 2
18 VK_CEREAL_FLAG_ALL = VK_CEREAL_FLAG_GUEST | VK_CEREAL_FLAG_HOST
19
20 SUPPORTED_FEATURES = [
21 "VK_VERSION_1_0",
22 "VK_BASE_VERSION_1_0",
23 "VK_COMPUTE_VERSION_1_0",
24 "VK_GRAPHICS_VERSION_1_0",
25 "VK_VERSION_1_1",
26 "VK_BASE_VERSION_1_1",
27 "VK_COMPUTE_VERSION_1_1",
28 "VK_GRAPHICS_VERSION_1_1",
29 "VK_VERSION_1_2",
30 "VK_BASE_VERSION_1_2",
31 "VK_COMPUTE_VERSION_1_2",
32 "VK_GRAPHICS_VERSION_1_2",
33 "VK_VERSION_1_3",
34 "VK_BASE_VERSION_1_3",
35 "VK_COMPUTE_VERSION_1_3",
36 "VK_GRAPHICS_VERSION_1_3",
37 "VK_VERSION_1_4",
38 "VK_BASE_VERSION_1_4",
39 "VK_COMPUTE_VERSION_1_4",
40 "VK_GRAPHICS_VERSION_1_4",
41 # Instance extensions
42 "VK_KHR_get_physical_device_properties2",
43 "VK_KHR_external_semaphore_capabilities",
44 "VK_KHR_external_memory_capabilities",
45 "VK_KHR_external_fence_capabilities",
46 "VK_EXT_debug_utils",
47 "VK_EXT_debug_report",
48 "VK_EXT_validation_features",
49 # Device extensions
50 "VK_EXT_external_memory_host",
51 "VK_KHR_storage_buffer_storage_class",
52 "VK_KHR_vulkan_memory_model",
53 "VK_KHR_buffer_device_address",
54 "VK_KHR_maintenance1",
55 "VK_KHR_maintenance2",
56 "VK_KHR_maintenance3",
57 "VK_KHR_bind_memory2",
58 "VK_KHR_dedicated_allocation",
59 "VK_KHR_get_memory_requirements2",
60 "VK_KHR_sampler_ycbcr_conversion",
61 "VK_KHR_global_priority",
62 "VK_KHR_shader_float16_int8",
63 "VK_AMD_gpu_shader_half_float",
64 "VK_NV_shader_subgroup_partitioned",
65 "VK_KHR_shader_subgroup_extended_types",
66 "VK_EXT_provoking_vertex",
67 "VK_EXT_line_rasterization",
68 "VK_KHR_line_rasterization",
69 "VK_EXT_transform_feedback",
70 "VK_EXT_primitive_topology_list_restart",
71 "VK_EXT_index_type_uint8",
72 "VK_EXT_load_store_op_none",
73 "VK_EXT_swapchain_colorspace",
74 "VK_EXT_custom_border_color",
75 "VK_EXT_shader_stencil_export",
76 "VK_KHR_image_format_list",
77 "VK_KHR_incremental_present",
78 "VK_KHR_pipeline_executable_properties",
79 "VK_EXT_queue_family_foreign",
80 "VK_EXT_scalar_block_layout",
81 "VK_KHR_external_semaphore",
82 "VK_KHR_external_semaphore_fd",
83 "VK_KHR_external_memory",
84 "VK_KHR_external_fence",
85 "VK_KHR_external_fence_fd",
86 "VK_EXT_device_memory_report",
87 "VK_EXT_memory_budget",
88 "VK_KHR_create_renderpass2",
89 "VK_KHR_imageless_framebuffer",
90 "VK_KHR_descriptor_update_template",
91 "VK_EXT_depth_clip_enable",
92 "VK_EXT_robustness2",
93 "VK_KHR_multiview",
94 "VK_EXT_blend_operation_advanced",
95 "VK_EXT_frame_boundary",
96 # see aosp/2736079 + b/268351352
97 "VK_EXT_swapchain_maintenance1",
98 "VK_KHR_maintenance5",
99 "VK_EXT_host_image_copy",
100 "VK_EXT_image_compression_control",
101 "VK_EXT_image_compression_control_swapchain",
102 "VK_EXT_image_drm_format_modifier",
103 # VK1.3 extensions: see b/298704840
104 "VK_KHR_copy_commands2",
105 "VK_KHR_dynamic_rendering",
106 "VK_KHR_format_feature_flags2",
107 "VK_KHR_maintenance4",
108 "VK_KHR_shader_integer_dot_product",
109 "VK_KHR_shader_non_semantic_info",
110 "VK_KHR_shader_terminate_invocation",
111 "VK_KHR_synchronization2",
112 "VK_KHR_zero_initialize_workgroup_memory",
113 "VK_EXT_4444_formats",
114 "VK_EXT_extended_dynamic_state",
115 "VK_EXT_extended_dynamic_state2",
116 "VK_EXT_image_robustness",
117 "VK_EXT_inline_uniform_block",
118 "VK_EXT_pipeline_creation_cache_control",
119 "VK_EXT_pipeline_creation_feedback",
120 "VK_EXT_private_data",
121 "VK_EXT_shader_demote_to_helper_invocation",
122 "VK_EXT_subgroup_size_control",
123 "VK_EXT_texel_buffer_alignment",
124 "VK_EXT_texture_compression_astc_hdr",
125 "VK_EXT_tooling_info",
126 "VK_EXT_ycbcr_2plane_444_formats",
127 # Host dispatch
128 "VK_KHR_surface",
129 "VK_KHR_swapchain",
130 "VK_KHR_xcb_surface",
131 "VK_KHR_win32_surface",
132 "VK_EXT_metal_surface",
133 "VK_EXT_metal_objects",
134 "VK_EXT_external_memory_metal",
135 "VK_KHR_external_semaphore_win32",
136 "VK_KHR_external_memory_win32",
137 "VK_NV_device_diagnostic_checkpoints",
138 "VK_KHR_ray_tracing_pipeline",
139 "VK_KHR_pipeline_library",
140 "VK_MVK_macos_surface",
141 # Android
142 "VK_ANDROID_native_buffer",
143 "VK_ANDROID_external_memory_android_hardware_buffer",
144 "VK_KHR_android_surface",
145 # Linux
146 "VK_KHR_external_memory_fd",
147 # Custom
148 "VK_GOOGLE_gfxstream",
149 # Used in tests without proper support checks
150 "VK_EXT_graphics_pipeline_library",
151 # Used by guest ANGLE
152 "VK_EXT_vertex_attribute_divisor",
153 # QNX
154 "VK_QNX_screen_surface",
155 "VK_QNX_external_memory_screen_buffer",
156 # b/320855472 Chrome
157 "VK_EXT_fragment_density_map",
158 # b/349122558 Zink
159 "VK_EXT_color_write_enable",
160 "VK_EXT_primitives_generated_query",
161 ]
162
163 HOST_MODULES = ["goldfish_vk_extension_structs", "goldfish_vk_marshaling",
164 "goldfish_vk_reserved_marshaling", "goldfish_vk_deepcopy",
165 "goldfish_vk_dispatch", "goldfish_vk_transform", "vk_decoder",
166 "vk_decoder_snapshot", "vk_sub_decoder"]
167
168 # By default, the all wrappers are run all on all features. In certain cases,
169 # we wish run wrappers when the module requires it. For example, `VK_GOOGLE_gfxstream`
170 # shouldn't generate a function table entry since it's an internal interface.
171 SUPPORTED_MODULES = {
172 "VK_EXT_external_memory_host": HOST_MODULES,
173 "VK_EXT_debug_utils": HOST_MODULES,
174 "VK_EXT_debug_report": HOST_MODULES,
175 "VK_EXT_validation_features": HOST_MODULES,
176 "VK_KHR_surface": ["goldfish_vk_dispatch"],
177 "VK_KHR_xcb_surface": ["goldfish_vk_dispatch"],
178 "VK_KHR_win32_surface": ["goldfish_vk_dispatch"],
179 "VK_EXT_metal_surface": ["goldfish_vk_dispatch"],
180 "VK_EXT_metal_objects": ["goldfish_vk_dispatch"],
181 "VK_EXT_external_memory_metal": ["goldfish_vk_dispatch"],
182 "VK_KHR_external_semaphore_win32" : ["goldfish_vk_dispatch"],
183 "VK_KHR_external_memory_win32" : ["goldfish_vk_dispatch"],
184 "VK_MVK_macos_surface" : ["goldfish_vk_dispatch"],
185 # Host dispatch for Linux hosts + and entrypoint for guests
186 "VK_KHR_external_memory_fd": ["goldfish_vk_dispatch", "func_table"],
187 "VK_QNX_screen_surface": ["goldfish_vk_dispatch"],
188 "VK_QNX_external_memory_screen_buffer": ["goldfish_vk_dispatch"],
189 "VK_ANDROID_external_memory_android_hardware_buffer": ["goldfish_vk_dispatch", "func_table"],
190 "VK_KHR_android_surface": ["func_table"],
191 "VK_EXT_swapchain_maintenance1" : HOST_MODULES,
192 "VK_KHR_swapchain" : HOST_MODULES,
193 "VK_NV_device_diagnostic_checkpoints": ["goldfish_vk_dispatch"],
194 "VK_KHR_ray_tracing_pipeline": ["goldfish_vk_dispatch"],
195 "VK_KHR_pipeline_library": HOST_MODULES,
196 }
197
198 # These modules will be used when the feature is not supported.
199 # This is necessary to cover all extensions where needed.
200 UNSUPPORTED_FEATURE_MODULES = {
201 "goldfish_vk_extension_structs",
202 }
203
204
205 REQUIRED_TYPES = {
206 "int",
207 "uint16_t",
208 "int64_t",
209 "double",
210 "VkPresentScalingFlagsEXT",
211 "VkPresentGravityFlagsEXT",
212 "VkRenderingAreaInfo",
213 "VkRenderingAreaInfoKHR",
214 }
215
216 copyrightHeader = """// Copyright (C) 2018 The Android Open Source Project
217 // Copyright (C) 2018 Google Inc.
218 //
219 // Licensed under the Apache License, Version 2.0 (the "License");
220 // you may not use this file except in compliance with the License.
221 // You may obtain a copy of the License at
222 //
223 // http://www.apache.org/licenses/LICENSE-2.0
224 //
225 // Unless required by applicable law or agreed to in writing, software
226 // distributed under the License is distributed on an "AS IS" BASIS,
227 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
228 // See the License for the specific language governing permissions and
229 // limitations under the License.
230 """
231
232 coprightHeaderPy = """# Copyright (C) 2025 The Android Open Source Project
233 #
234 # Licensed under the Apache License, Version 2.0 (the "License");
235 # you may not use this file except in compliance with the License.
236 # You may obtain a copy of the License at
237 #
238 # http://www.apache.org/licenses/LICENSE-2.0
239 #
240 # Unless required by applicable law or agreed to in writing, software
241 # distributed under the License is distributed on an "AS IS" BASIS,
242 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
243 # See the License for the specific language governing permissions and
244 # limitations under the License.
245
246 """
247
248 # We put the long generated commands in a separate paragraph, so that the formatter won't mess up
249 # with other texts.
250 autogeneratedHeaderTemplate = """
251 // Autogenerated module %s
252 //
253 // %s
254 //
255 // Please do not modify directly;
256 // re-run mesa3d/src/gfxstream/codegen/generate-gfxstream-vulkan.sh,
257 // or directly from Python by defining:
258 // VULKAN_REGISTRY_XML_DIR : Directory containing vk.xml
259 // VULKAN_REGISTRY_SCRIPTS_DIR : Directory containing genvk.py
260 // CEREAL_OUTPUT_DIR: Where to put the generated sources.
261 //
262 // python3 $VULKAN_REGISTRY_SCRIPTS_DIR/genvk.py -registry $VULKAN_REGISTRY_XML_DIR/vk.xml cereal -o $CEREAL_OUTPUT_DIR
263 //
264 """
265 namespaceHostBegin ="""
266 namespace gfxstream {
267 namespace host {
268 namespace vk {\n
269 """
270
271 namespaceHostEnd = """
272 } // namespace vk
273 } // namespace host
274 } // namespace gfxstream
275 """
276
277 namespaceBegin ="""
278 namespace gfxstream {
279 namespace vk {\n
280 """
281
282 namespaceEnd = """
283 } // namespace vk
284 } // namespace gfxstream
285 """
286
287 def banner_command(argv):
288 """Return sanitized command-line description.
289 |argv| must be a list of command-line parameters, e.g. sys.argv.
290 Return a string corresponding to the command, with platform-specific
291 paths removed."""
292
293 return os.path.basename(argv[0])
294
295 def envGetOrDefault(key, default=None):
296 if key in os.environ:
297 return os.environ[key]
298 return default
299
300 # ---- methods overriding base class ----
301 # beginFile(genOpts)
302 # endFile()
303 # beginFeature(interface, emit)
304 # endFeature()
305 # genType(typeinfo,name)
306 # genStruct(typeinfo,name)
307 # genGroup(groupinfo,name)
308 # genEnum(enuminfo, name)
309 # genCmd(cmdinfo)
310 class CerealGenerator(OutputGenerator):
311
312 """Generate serialization code"""
313 def __init__(self, errFile = sys.stderr,
314 warnFile = sys.stderr,
315 diagFile = sys.stdout):
316 OutputGenerator.__init__(self, errFile, warnFile, diagFile)
317
318 self.typeInfo = cereal.VulkanTypeInfo(self)
319
320 self.modules = {}
321 self.protos = {}
322 self.moduleList = []
323 self.protoList = []
324
325 self.wrappers = []
326
327 self.codegen = cereal.CodeGen()
328 self.featureSupported = False
329 self.supportedModules = None
330
331 # The cereal variant should be an environmental variable of one of
332 # the following:
333 # - "guest"
334 # - "host"
335 # - "both"
336 cerealVariant = envGetOrDefault("CEREAL_VARIANT", "both")
337 if cerealVariant == "guest":
338 self.cerealFlags = VK_CEREAL_FLAG_GUEST
339 elif cerealVariant == "host":
340 self.cerealFlags = VK_CEREAL_FLAG_HOST
341 else:
342 self.cerealFlags = VK_CEREAL_FLAG_ALL
343
344 # THe host always needs all possible guest struct definitions, while the guest only needs
345 # platform sepcific headers.
346 self.hostCommonExtraVulkanHeaders = '#include "vk_android_native_buffer_gfxstream.h"'
347
348 encoderInclude = f"""
349 #include "goldfish_vk_private_defs.h"
350 #include <memory>
351
352 namespace gfxstream {{
353 namespace guest {{
354 class IOStream;
355 }} // namespace guest
356 }} // namespace gfxstream
357 """
358 encoderImplInclude = f"""
359 #include "Resources.h"
360 #include "ResourceTracker.h"
361 #include "Validation.h"
362 #include "%s.h"
363 #include "gfxstream/guest/IOStream.h"
364
365 #include "AlignedBuf.h"
366 #include "BumpPool.h"
367
368 #include "goldfish_vk_marshaling_guest.h"
369 #include "goldfish_vk_reserved_marshaling_guest.h"
370 #include "goldfish_vk_deepcopy_guest.h"
371 #include "goldfish_vk_counting_guest.h"
372 #include "goldfish_vk_private_defs.h"
373 #include "goldfish_vk_transform_guest.h"
374
375 #include <memory>
376 #include <optional>
377 #include <unordered_map>
378 #include <string>
379 #include <vector>
380
381 """ % VULKAN_STREAM_TYPE_GUEST
382
383 functableImplInclude = """
384 #include "VkEncoder.h"
385 #include "ResourceTracker.h"
386 #include "gfxstream_vk_entrypoints.h"
387 #include "gfxstream_vk_private.h"
388
389 #include "goldfish_vk_private_defs.h"
390
391 #include <cstring>
392
393 // Stuff we are not going to use but if included,
394 // will cause compile errors. These are Android Vulkan
395 // required extensions, but the approach will be to
396 // implement them completely on the guest side.
397 #undef VK_KHR_android_surface
398 #if defined(LINUX_GUEST_BUILD) || DETECT_OS_FUCHSIA || DETECT_OS_WINDOWS
399 #undef VK_ANDROID_native_buffer
400 #endif
401 """
402 marshalIncludeGuest = """
403 #include "goldfish_vk_marshaling_guest.h"
404 #include "goldfish_vk_private_defs.h"
405 #include "%s.h"
406
407 // Stuff we are not going to use but if included,
408 // will cause compile errors. These are Android Vulkan
409 // required extensions, but the approach will be to
410 // implement them completely on the guest side.
411 #undef VK_KHR_android_surface
412 #undef VK_ANDROID_external_memory_android_hardware_buffer
413 """ % VULKAN_STREAM_TYPE_GUEST
414
415 reservedmarshalIncludeGuest = """
416 #include "goldfish_vk_marshaling_guest.h"
417 #include "goldfish_vk_private_defs.h"
418 #include "%s.h"
419
420 // Stuff we are not going to use but if included,
421 // will cause compile errors. These are Android Vulkan
422 // required extensions, but the approach will be to
423 // implement them completely on the guest side.
424 #undef VK_KHR_android_surface
425 #undef VK_ANDROID_external_memory_android_hardware_buffer
426 """ % VULKAN_STREAM_TYPE_GUEST
427
428 reservedMarshalingHostIncludes = """
429 #include "vulkan_boxed_handles.h"
430
431 """
432
433 reservedmarshalImplIncludeGuest = """
434 #include "Resources.h"
435 """
436
437 vulkanStreamIncludeHost = f"""
438 {self.hostCommonExtraVulkanHeaders}
439 #include "goldfish_vk_private_defs.h"
440 #include "gfxstream/host/stream_utils.h"
441 #include "vulkan_stream.h"
442 """
443
444 poolInclude = f"""
445 {self.hostCommonExtraVulkanHeaders}
446 #include "goldfish_vk_private_defs.h"
447 #include "gfxstream/BumpPool.h"
448 using gfxstream::base::Allocator;
449 using gfxstream::base::BumpPool;
450 """
451 transformIncludeGuest = """
452 #include "goldfish_vk_private_defs.h"
453 """
454 transformInclude = f"""
455 {self.hostCommonExtraVulkanHeaders}
456 #include "goldfish_vk_private_defs.h"
457 #include "goldfish_vk_extension_structs.h"
458 """
459 transformImplIncludeGuest = """
460 #include "ResourceTracker.h"
461 """
462 transformImplInclude = """
463 #include "vk_decoder_global_state.h"
464 """
465 deepcopyInclude = """
466 #include "vk_util.h"
467 """
468
469 deepcopyHostInclude = """
470 #include "vk_utils.h"
471 """
472
473 poolIncludeGuest = f"""
474 #include "goldfish_vk_private_defs.h"
475 #include "BumpPool.h"
476 using gfxstream::aemu::Allocator;
477 using gfxstream::aemu::BumpPool;
478 // Stuff we are not going to use but if included,
479 // will cause compile errors. These are Android Vulkan
480 // required extensions, but the approach will be to
481 // implement them completely on the guest side.
482 #undef VK_KHR_android_surface
483 #undef VK_ANDROID_external_memory_android_hardware_buffer
484 """
485 dispatchHeaderDefs = f"""
486 {self.hostCommonExtraVulkanHeaders}
487 #include "goldfish_vk_private_defs.h"
488 namespace gfxstream {{
489 namespace host {{
490 namespace vk {{
491
492 struct VulkanDispatch;
493
494 }} // namespace vk
495 }} // namespace host
496 }} // namespace gfxstream
497 using DlOpenFunc = void* (void);
498 using DlSymFunc = void* (void*, const char*);
499 """
500
501 extensionStructsInclude = f"""
502 {self.hostCommonExtraVulkanHeaders}
503 #include "gfxstream/common/logging.h"
504 #include "goldfish_vk_private_defs.h"
505 #include "vulkan/vk_enum_string_helper.h"
506 """
507
508 extensionStructsIncludeGuest = """
509 #include "vk_platform_compat.h"
510 #include "goldfish_vk_private_defs.h"
511 // Stuff we are not going to use but if included,
512 // will cause compile errors. These are Android Vulkan
513 // required extensions, but the approach will be to
514 // implement them completely on the guest side.
515 #undef VK_KHR_android_surface
516 #undef VK_ANDROID_external_memory_android_hardware_buffer
517 """
518 commonCerealImplIncludes = """
519 #include "goldfish_vk_extension_structs.h"
520 #include "goldfish_vk_private_defs.h"
521 #include <string.h>
522 """
523 commonCerealIncludesGuest = """
524 #include "vk_platform_compat.h"
525 """
526 commonCerealImplIncludesGuest = """
527 #include "goldfish_vk_extension_structs_guest.h"
528 #include "goldfish_vk_private_defs.h"
529
530 #include <cstring>
531 """
532 countingIncludes = """
533 #include "vk_platform_compat.h"
534 #include "goldfish_vk_private_defs.h"
535 """
536
537 countingIncludeGuest = """
538 #include <cstdlib>
539 """
540
541 dispatchImplIncludes = """
542 #include <stdio.h>
543 #include <stdlib.h>
544 #include <string.h>
545 """
546
547 decoderSnapshotHeaderIncludes = f"""
548 #include <memory>
549
550 #include "gfxstream/host/GfxApiLogger.h"
551 #include "goldfish_vk_private_defs.h"
552 #include "vk_snapshot_handles.h"
553 """
554 decoderSnapshotImplIncludes = f"""
555 #include <mutex>
556
557 #include "gfxstream/ThreadAnnotations.h"
558 #include "vk_decoder_global_state.h"
559 #include "vk_reconstruction.h"
560 #include "vulkan_boxed_handles.h"
561 #include "vulkan_handle_mapping.h"
562 """
563
564 decoderHeaderIncludes = f"""
565 #include "vk_decoder_context.h"
566 #include "gfxstream/host/process_resources.h"
567
568 #include <memory>
569
570 namespace gfxstream {{
571 namespace base {{
572 class BumpPool;
573 }} // namespace base
574 }} // namespace gfxstream
575
576 """
577
578 decoderImplIncludes = f"""
579 #include <cstring>
580 #include <functional>
581 #include <optional>
582 #include <unordered_map>
583
584 #include "common/goldfish_vk_marshaling.h"
585 #include "common/goldfish_vk_reserved_marshaling.h"
586 #include "common/goldfish_vk_transform.h"
587 #include "frame_buffer.h"
588 #include "gfxstream/BumpPool.h"
589 #include "gfxstream/common/logging.h"
590 #include "gfxstream/host/iostream.h"
591 #include "gfxstream/host/tracing.h"
592 #include "gfxstream/system/System.h"
593 #include "gfxstream/threads/Thread.h"
594 #include "goldfish_vk_private_defs.h"
595 #include "vk_decoder_global_state.h"
596 #include "vk_decoder_snapshot.h"
597 #include "vulkan_boxed_handles.h"
598 #include "vulkan_dispatch.h"
599 #include "vulkan_stream.h"
600
601 """
602
603 def createVkExtensionStructureTypePreamble(extensionName: str) -> str:
604 return f"""
605 #define {extensionName}_ENUM(type,id) \
606 ((type)(1000000000 + (1000 * ({extensionName}_NUMBER - 1)) + (id)))
607 """
608 self.guest_encoder_tag = "guest_encoder"
609 self.host_tag = "host"
610
611 default_guest_abs_encoder_destination = \
612 os.path.join(
613 os.getcwd(),
614 "..", "..",
615 "device", "generic", "goldfish-opengl",
616 "system", "vulkan_enc")
617 self.guest_abs_encoder_destination = \
618 envGetOrDefault("GFXSTREAM_GUEST_ENCODER_DIR",
619 default_guest_abs_encoder_destination)
620
621 default_host_abs_decoder_destination = \
622 os.path.join(
623 os.getcwd(),
624 "android", "android-emugl", "host",
625 "libs", "libOpenglRender", "vulkan")
626 self.host_abs_decoder_destination = \
627 envGetOrDefault("GFXSTREAM_HOST_DECODER_DIR",
628 default_host_abs_decoder_destination)
629 self.host_script_destination = envGetOrDefault("GFXSTREAM_SCRIPTS_DIR")
630
631 if self.cerealFlags & VK_CEREAL_FLAG_GUEST:
632 self.addGuestEncoderModule(
633 "VkEncoder",
634 extraHeader = encoderInclude,
635 extraImpl = encoderImplInclude)
636
637 self.addGuestEncoderModule("goldfish_vk_extension_structs_guest",
638 extraHeader=extensionStructsIncludeGuest)
639 self.addGuestEncoderModule("goldfish_vk_marshaling_guest",
640 extraHeader=commonCerealIncludesGuest + marshalIncludeGuest,
641 extraImpl=commonCerealImplIncludesGuest)
642 self.addGuestEncoderModule("goldfish_vk_reserved_marshaling_guest",
643 extraHeader=commonCerealIncludesGuest + reservedmarshalIncludeGuest,
644 extraImpl=commonCerealImplIncludesGuest + reservedmarshalImplIncludeGuest)
645 self.addGuestEncoderModule("goldfish_vk_deepcopy_guest",
646 extraHeader=commonCerealIncludesGuest + poolIncludeGuest,
647 extraImpl=commonCerealImplIncludesGuest + deepcopyInclude)
648 self.addGuestEncoderModule("goldfish_vk_counting_guest",
649 extraHeader=countingIncludes,
650 extraImpl=commonCerealImplIncludesGuest + countingIncludeGuest)
651 self.addGuestEncoderModule("goldfish_vk_transform_guest",
652 extraHeader=commonCerealIncludesGuest + transformIncludeGuest,
653 extraImpl=commonCerealImplIncludesGuest + transformImplIncludeGuest)
654 self.addGuestEncoderModule(
655 "vulkan_gfxstream_structure_type", headerOnly=True, suppressFeatureGuards=True,
656 moduleName="vulkan_gfxstream_structure_type_guest", useNamespace=False,
657 suppressVulkanHeaders=True,
658 extraHeader=createVkExtensionStructureTypePreamble('VK_GOOGLE_GFXSTREAM'))
659
660 self.addGuestEncoderModule("func_table", extraImpl=functableImplInclude, implOnly = True,
661 useNamespace = False)
662
663 self.addWrapper(cereal.VulkanEncoder, "VkEncoder")
664 self.addWrapper(cereal.VulkanExtensionStructs, "goldfish_vk_extension_structs_guest", variant = "guest")
665 self.addWrapper(cereal.VulkanMarshaling, "goldfish_vk_marshaling_guest", variant = "guest")
666 self.addWrapper(cereal.VulkanReservedMarshaling, "goldfish_vk_reserved_marshaling_guest", variant = "guest")
667 self.addWrapper(cereal.VulkanDeepcopy, "goldfish_vk_deepcopy_guest")
668 self.addWrapper(cereal.VulkanCounting, "goldfish_vk_counting_guest")
669 self.addWrapper(cereal.VulkanTransform, "goldfish_vk_transform_guest")
670 self.addWrapper(cereal.VulkanFuncTable, "func_table")
671 self.addWrapper(cereal.VulkanGfxstreamStructureType,
672 "vulkan_gfxstream_structure_type_guest")
673
674 if self.cerealFlags & VK_CEREAL_FLAG_HOST:
675 self.addCppModule("common", "goldfish_vk_extension_structs",
676 extraHeader=extensionStructsInclude)
677 self.addCppModule("common", "goldfish_vk_marshaling",
678 extraHeader=vulkanStreamIncludeHost,
679 extraImpl=commonCerealImplIncludes)
680 self.addCppModule("common", "goldfish_vk_reserved_marshaling",
681 extraHeader=vulkanStreamIncludeHost,
682 extraImpl=commonCerealImplIncludes + reservedMarshalingHostIncludes)
683 self.addCppModule("common", "goldfish_vk_deepcopy",
684 extraHeader=poolInclude,
685 extraImpl=commonCerealImplIncludes + deepcopyHostInclude)
686 self.addCppModule("common", "goldfish_vk_dispatch",
687 extraHeader=dispatchHeaderDefs,
688 extraImpl=dispatchImplIncludes)
689 self.addCppModule("common", "goldfish_vk_transform",
690 extraHeader=transformInclude,
691 extraImpl=transformImplInclude)
692 self.addHostModule("vk_decoder",
693 extraHeader=decoderHeaderIncludes,
694 extraImpl=decoderImplIncludes,
695 useNamespace=False)
696 self.addHostModule("vk_decoder_snapshot",
697 extraHeader=decoderSnapshotHeaderIncludes,
698 extraImpl=decoderSnapshotImplIncludes,
699 useNamespace=False)
700 self.addHostModule("vk_sub_decoder",
701 extraHeader="",
702 extraImpl="",
703 useNamespace=False,
704 implOnly=True)
705
706 self.addModule(cereal.PyScript(self.host_tag, "vulkan_printer", customAbsDir=Path(
707 self.host_script_destination) / "print_gfx_logs"), moduleName="ApiLogDecoder",
708 extraHeader=coprightHeaderPy)
709 self.addHostModule(
710 "vulkan_gfxstream_structure_type", headerOnly=True, suppressFeatureGuards=True,
711 moduleName="vulkan_gfxstream_structure_type_host", useNamespace=False,
712 suppressVulkanHeaders=True,
713 extraHeader=createVkExtensionStructureTypePreamble('VK_GOOGLE_GFXSTREAM'))
714 self.addHostModule(
715 "vk_android_native_buffer_structure_type", headerOnly=True, suppressFeatureGuards=True,
716 useNamespace=False, suppressVulkanHeaders=True,
717 extraHeader=createVkExtensionStructureTypePreamble('VK_ANDROID_NATIVE_BUFFER'))
718
719 self.addWrapper(cereal.VulkanExtensionStructs, "goldfish_vk_extension_structs", variant = "host")
720 self.addWrapper(cereal.VulkanMarshaling, "goldfish_vk_marshaling")
721 self.addWrapper(cereal.VulkanReservedMarshaling, "goldfish_vk_reserved_marshaling", variant = "host")
722 self.addWrapper(cereal.VulkanDeepcopy, "goldfish_vk_deepcopy")
723 self.addWrapper(cereal.VulkanDispatch, "goldfish_vk_dispatch")
724 self.addWrapper(cereal.VulkanTransform, "goldfish_vk_transform", resourceTrackerTypeName="VkDecoderGlobalState")
725 self.addWrapper(cereal.VulkanDecoder, "vk_decoder")
726 self.addWrapper(cereal.VulkanDecoderSnapshot, "vk_decoder_snapshot")
727 self.addWrapper(cereal.VulkanSubDecoder, "vk_sub_decoder")
728 self.addWrapper(cereal.ApiLogDecoder, "ApiLogDecoder")
729 self.addWrapper(cereal.VulkanGfxstreamStructureType, "vulkan_gfxstream_structure_type_host")
730 self.addWrapper(cereal.VulkanAndroidNativeBufferStructureType,
731 "vk_android_native_buffer_structure_type")
732
733 def addGuestEncoderModule(
734 self, basename, extraHeader="", extraImpl="", useNamespace=True, headerOnly=False,
735 suppressFeatureGuards=False, moduleName=None, suppressVulkanHeaders=False, implOnly=False):
736 if not os.path.exists(self.guest_abs_encoder_destination):
737 print("Path [%s] not found (guest encoder path), skipping" % self.guest_abs_encoder_destination)
738 return
739 self.addCppModule(self.guest_encoder_tag, basename, extraHeader=extraHeader,
740 extraImpl=extraImpl, customAbsDir=self.guest_abs_encoder_destination,
741 useNamespace=useNamespace, implOnly=implOnly, headerOnly=headerOnly,
742 suppressFeatureGuards=suppressFeatureGuards, moduleName=moduleName,
743 suppressVulkanHeaders=suppressVulkanHeaders)
744
745 def addHostModule(
746 self, basename, extraHeader="", extraImpl="", useNamespace=True, implOnly=False,
747 suppress=False, headerOnly=False, suppressFeatureGuards=False, moduleName=None,
748 suppressVulkanHeaders=False):
749 if not os.path.exists(self.host_abs_decoder_destination):
750 print("Path [%s] not found (host encoder path), skipping" %
751 self.host_abs_decoder_destination)
752 return
753 if not suppressVulkanHeaders:
754 extraHeader = self.hostCommonExtraVulkanHeaders + '\n' + extraHeader
755 self.addCppModule(
756 self.host_tag, basename, extraHeader=extraHeader, extraImpl=extraImpl,
757 customAbsDir=self.host_abs_decoder_destination, useNamespace=useNamespace,
758 implOnly=implOnly, suppress=suppress, headerOnly=headerOnly,
759 suppressFeatureGuards=suppressFeatureGuards, moduleName=moduleName,
760 suppressVulkanHeaders=suppressVulkanHeaders)
761
762 def addModule(self, module, moduleName=None, extraHeader=None):
763 if moduleName is None:
764 moduleName = module.basename
765 self.moduleList.append(moduleName)
766 if extraHeader:
767 module.preamble = extraHeader
768 self.modules[moduleName] = module
769
770 def addCppModule(
771 self, directory, basename, extraHeader="", extraImpl="", customAbsDir=None,
772 useNamespace=True, implOnly=False, suppress=False, headerOnly=False,
773 suppressFeatureGuards=False, moduleName=None, suppressVulkanHeaders=False):
774 module = cereal.Module(
775 directory, basename, customAbsDir=customAbsDir, suppress=suppress, implOnly=implOnly,
776 headerOnly=headerOnly, suppressFeatureGuards=suppressFeatureGuards)
777 self.addModule(module, moduleName=moduleName)
778 module.headerPreamble = copyrightHeader
779 module.headerPreamble += \
780 autogeneratedHeaderTemplate % \
781 (basename, "(header) generated by %s" % banner_command(sys.argv))
782
783 module.headerPreamble += "#pragma once\n"
784 if (not suppressVulkanHeaders):
785 module.headerPreamble += "#include <vulkan/vulkan.h>\n"
786 module.headerPreamble += '#include "vulkan_gfxstream.h"\n'
787 module.headerPreamble += '#include "vk_android_native_buffer_gfxstream.h"\n'
788 module.headerPreamble += extraHeader + '\n'
789
790 usedNamespaceBegin = namespaceHostBegin if self.cerealFlags & VK_CEREAL_FLAG_HOST else namespaceBegin
791 usedNamespaceEnd = namespaceHostEnd if self.cerealFlags & VK_CEREAL_FLAG_HOST else namespaceEnd
792
793 if useNamespace:
794 module.headerPreamble += usedNamespaceBegin
795
796 module.implPreamble = copyrightHeader
797 module.implPreamble += \
798 autogeneratedHeaderTemplate % \
799 (basename, "(impl) generated by %s" % \
800 banner_command(sys.argv))
801 if not implOnly:
802 module.implPreamble += '\n#include "%s.h"' % \
803 (basename)
804
805 module.implPreamble += extraImpl
806
807 if useNamespace:
808
809 module.implPreamble += usedNamespaceBegin
810 module.implPostamble += usedNamespaceEnd
811 module.headerPostamble += usedNamespaceEnd
812
813 def addWrapper(self, moduleType, moduleName, **kwargs):
814 if moduleName not in self.modules:
815 print(f'Unknown module: {moduleName}. All known modules are: {", ".join(self.modules)}.')
816 return
817 self.wrappers.append(
818 (moduleType(
819 self.modules[moduleName],
820 self.typeInfo, **kwargs),
821 moduleName)
822 )
823
824 def forEachModule(self, func):
825 for moduleName in self.moduleList:
826 func(self.modules[moduleName])
827
828 def forEachWrapper(self, func, supportedModules):
829 for wrapper in self.wrappers:
830 if supportedModules is None:
831 func(wrapper[0])
832 elif wrapper[1] in supportedModules:
833 func(wrapper[0])
834
835 ## Overrides####################################################################
836
837 def beginFile(self, genOpts):
838 OutputGenerator.beginFile(self, genOpts)
839
840 self.forEachModule(lambda m: m.begin(self.genOpts.directory))
841 self.forEachWrapper(lambda w: w.onBegin(), None)
842
843 def endFile(self):
844 OutputGenerator.endFile(self)
845
846 self.typeInfo.onEnd()
847
848 self.forEachWrapper(lambda w: w.onEnd(), None)
849 self.forEachModule(lambda m: m.end())
850
851 def beginFeature(self, interface, emit):
852 # Start processing in superclass
853 OutputGenerator.beginFeature(self, interface, emit)
854
855 for supportedFeature in SUPPORTED_FEATURES:
856 if self.featureName == supportedFeature:
857 self.featureSupported = True
858
859 if self.featureSupported == False and UNSUPPORTED_FEATURE_MODULES:
860 self.featureSupported = True
861 self.supportedModules = UNSUPPORTED_FEATURE_MODULES
862 elif self.featureSupported == False:
863 return
864 else:
865 self.supportedModules = SUPPORTED_MODULES.get(self.featureName)
866
867 self.typeInfo.onBeginFeature(self.featureName, self.featureType)
868
869 self.forEachModule(
870 lambda m: m.appendHeader("#ifdef %s\n" % self.featureName)
871 if isinstance(m, cereal.Module) and not m.suppressFeatureGuards else None)
872 self.forEachModule(
873 lambda m: m.appendImpl("#ifdef %s\n" % self.featureName)
874 if isinstance(m, cereal.Module) and not m.suppressFeatureGuards else None)
875 self.forEachWrapper(lambda w: w.onBeginFeature(self.featureName, self.featureType), self.supportedModules)
876 # functable needs to understand the feature type (device vs instance) of each cmd
877 for features in interface.findall('require'):
878 for c in features.findall('command'):
879 self.forEachWrapper(lambda w: w.onFeatureNewCmd(c.get('name')), self.supportedModules)
880
881 def endFeature(self):
882 # Finish processing in superclass
883 OutputGenerator.endFeature(self)
884
885 if self.featureSupported == False:
886 return
887
888 self.featureSupported = False
889
890 self.typeInfo.onEndFeature()
891
892 self.forEachModule(lambda m: m.appendHeader("#endif\n") if isinstance(
893 m, cereal.Module) and not m.suppressFeatureGuards else None)
894 self.forEachModule(lambda m: m.appendImpl("#endif\n") if isinstance(
895 m, cereal.Module) and not m.suppressFeatureGuards else None)
896 self.forEachWrapper(lambda w: w.onEndFeature(), self.supportedModules)
897
898 def genType(self, typeinfo: TypeInfo, name, alias):
899 OutputGenerator.genType(self, typeinfo, name, alias)
900
901 # Maybe this check can be removed if we refactor other things inside
902 # the cereal subdirectory.
903 if self.featureSupported == False and name in REQUIRED_TYPES:
904 self.typeInfo.onGenType(typeinfo, name, alias)
905 return
906
907 if self.featureSupported == False:
908 return
909
910 self.typeInfo.onGenType(typeinfo, name, alias)
911 self.forEachWrapper(lambda w: w.onGenType(typeinfo, name, alias), self.supportedModules)
912
913 def genStruct(self, typeinfo, typeName, alias):
914 OutputGenerator.genStruct(self, typeinfo, typeName, alias)
915 if self.featureSupported == False:
916 return
917
918 self.typeInfo.onGenStruct(typeinfo, typeName, alias)
919 self.forEachWrapper(lambda w: w.onGenStruct(typeinfo, typeName, alias), self.supportedModules)
920
921 def genGroup(self, groupinfo: GroupInfo, groupName, alias = None):
922 OutputGenerator.genGroup(self, groupinfo, groupName, alias)
923 if self.featureSupported == False:
924 return
925
926 self.typeInfo.onGenGroup(groupinfo, groupName, alias)
927 self.forEachWrapper(lambda w: w.onGenGroup(groupinfo, groupName, alias), self.supportedModules)
928
929 def genEnum(self, enuminfo: EnumInfo, name, alias):
930 OutputGenerator.genEnum(self, enuminfo, name, alias)
931 if self.featureSupported == False:
932 return
933 self.typeInfo.onGenEnum(enuminfo, name, alias)
934 self.forEachWrapper(lambda w: w.onGenEnum(enuminfo, name, alias), self.supportedModules)
935
936 def genCmd(self, cmdinfo, name, alias):
937 OutputGenerator.genCmd(self, cmdinfo, name, alias)
938 if self.featureSupported == False:
939 return
940
941 self.typeInfo.onGenCmd(cmdinfo, name, alias)
942 self.forEachWrapper(lambda w: w.onGenCmd(cmdinfo, name, alias), self.supportedModules)