···44"""Simple script to update vk_helpers.{c,h}."""
5566from pathlib import Path
77-from typing import List, Tuple
77+from typing import List, Optional, Tuple
8899-# Each tuple is a function name, followed optionally by one or more conditions to test in the preprocessor, which will be wrapped in "defined()"
99+# Each tuple is a function name, followed optionally by one or more conditions
1010+# to test in the preprocessor, which will be wrapped in "defined()"
1011# if they aren't already. Empty tuples insert a blank line.
11121213DEVICE_FUNCTIONS = [
···180181]
181182182183EXTENSIONS_TO_CHECK = [
183183-"GOOGLE_display_timing",
184184-"EXT_global_priority",
185185-"EXT_robustness2",
184184+ "VK_GOOGLE_display_timing",
185185+ "VK_EXT_global_priority",
186186+ "VK_EXT_robustness2",
186187]
187188188189ROOT = Path(__file__).resolve().parent.parent
···206207 return " && ".join(wrap_condition(x) for x in pp_conditions)
207208208209210210+class ConditionalGenerator:
211211+ """Keep track of conditions to avoid unneeded repetition of ifdefs."""
212212+213213+ def __init__(self):
214214+ self.current_condition = None
215215+216216+ def process_condition(self, new_condition: Optional[str]) -> Optional[str]:
217217+ """Return a line (or lines) to yield if required based on the new condition state."""
218218+ lines = []
219219+ if self.current_condition and new_condition != self.current_condition:
220220+ # Close current condition if required.
221221+ lines.append("#endif // {}".format(self.current_condition))
222222+ # empty line
223223+ lines.append("")
224224+ self.current_condition = None
225225+226226+ if new_condition != self.current_condition:
227227+ # Open new condition if required
228228+ lines.append("#if {}".format(new_condition))
229229+ self.current_condition = new_condition
230230+231231+ if lines:
232232+ return "\n".join(lines)
233233+234234+ def finish(self) -> Optional[str]:
235235+ """Return a line (or lines) to yield if required at the end of the loop."""
236236+ return self.process_condition(None)
237237+238238+209239def generate_per_function(functions: List[Tuple[str, ...]], per_function_handler):
210210- current_condition = None
240240+ conditional = ConditionalGenerator()
211241 for data in functions:
212242 if not data:
213243 # empty line
214244 yield ""
215245 continue
216216- new_condition = compute_condition(data[1:])
217217- if current_condition and new_condition != current_condition:
218218- # Close current condition if required.
219219- yield "#endif // {}".format(current_condition)
220220- # empty line
221221- yield ""
222222- current_condition = None
223223-224224- if new_condition != current_condition:
225225- # Open new condition if required
226226- yield "#if {}".format(new_condition)
227227- current_condition = new_condition
246246+ condition_line = conditional.process_condition(compute_condition(data[1:]))
247247+ if condition_line:
248248+ yield condition_line
228249229250 yield per_function_handler(data[0])
230251231252 # close any trailing conditions
232232- if current_condition:
233233- yield "#endif // {}".format(current_condition)
253253+ condition_line = conditional.finish()
254254+ if condition_line:
255255+ yield condition_line
234256235257236258def generate_structure_members(functions: List[Tuple[str, ...]]):
···254276 return generate_per_function(functions, per_function)
255277256278279279+def make_ext_member_name(ext: str):
280280+ return "has_{}".format(ext[3:])
281281+282282+283283+def make_ext_name_define(ext: str):
284284+ return "{}_EXTENSION_NAME".format(ext.upper()).replace("2", "_2")
285285+286286+287287+def generate_ext_members():
288288+ for ext in EXTENSIONS_TO_CHECK:
289289+ yield "\tbool {};".format(make_ext_member_name(ext))
290290+291291+292292+def generate_ext_check():
293293+ yield "\t// Reset before filling out."
294294+295295+ for ext in EXTENSIONS_TO_CHECK:
296296+ yield "\tvk->{} = false;".format(make_ext_member_name(ext))
297297+298298+ yield ""
299299+ yield "\tfor (uint32_t i = 0; i < device_extension_count; i++) {"
300300+ yield "\t\tconst char *ext = device_extensions[i];"
301301+ yield ""
302302+303303+ conditional = ConditionalGenerator()
304304+ for ext in EXTENSIONS_TO_CHECK:
305305+ condition_line = conditional.process_condition(compute_condition((ext,)))
306306+ if condition_line:
307307+ yield condition_line
308308+ yield "\t\tif (strcmp(ext, {}) == 0) {{".format(make_ext_name_define(ext))
309309+ yield "\t\t\tvk->{} = true;".format(make_ext_member_name(ext))
310310+ yield "\t\t\tcontinue;"
311311+ yield "\t\t}"
312312+ # close any trailing conditions
313313+ condition_line = conditional.finish()
314314+ if condition_line:
315315+ yield condition_line
316316+ yield "\t}"
317317+318318+257319def replace_middle(
258320 lines: List[str], start_sentinel: str, end_sentinel: str, new_middle: List[str]
259321) -> List[str]:
···293355 DEVICE_TEMPLATES["END"],
294356 list(generate_structure_members(DEVICE_FUNCTIONS)),
295357 )
358358+ lines = replace_middle(
359359+ lines,
360360+ EXT_TEMPLATES["BEGIN"],
361361+ EXT_TEMPLATES["END"],
362362+ list(generate_ext_members()),
363363+ )
296364297365 with open(str(HEADER_FN), "w", encoding="utf-8") as fp:
298366 fp.write("\n".join(lines))
···315383 DEVICE_TEMPLATES["BEGIN"],
316384 DEVICE_TEMPLATES["END"],
317385 list(generate_dev_proc(DEVICE_FUNCTIONS)),
386386+ )
387387+388388+ lines = replace_middle(
389389+ lines,
390390+ EXT_TEMPLATES["BEGIN"],
391391+ EXT_TEMPLATES["END"],
392392+ list(generate_ext_check()),
318393 )
319394320395 with open(str(IMPL_FN), "w", encoding="utf-8") as fp:
+15-3
src/xrt/auxiliary/vk/vk_helpers.c
···884884#if defined(VK_USE_PLATFORM_WIN32_KHR)
885885 vk->vkCreateWin32SurfaceKHR = GET_INS_PROC(vk, vkCreateWin32SurfaceKHR);
886886#endif // defined(VK_USE_PLATFORM_WIN32_KHR)
887887- // end of GENERATED instance loader code - do not modify - used by scripts
887887+888888+ // end of GENERATED instance loader code - do not modify - used by scripts
888889889890 return VK_SUCCESS;
890891}
···12201221static void
12211222fill_in_has_extensions(struct vk_bundle *vk, const char **device_extensions, uint32_t num_device_extensions)
12221223{
12241224+ // beginning of GENERATED extension code - do not modify - used by scripts
12231225 // Reset before filling out.
12241226 vk->has_GOOGLE_display_timing = false;
12251227 vk->has_EXT_global_priority = false;
···12281230 for (uint32_t i = 0; i < num_device_extensions; i++) {
12291231 const char *ext = device_extensions[i];
1230123212331233+#if defined(VK_GOOGLE_display_timing)
12311234 if (strcmp(ext, VK_GOOGLE_DISPLAY_TIMING_EXTENSION_NAME) == 0) {
12321235 vk->has_GOOGLE_display_timing = true;
12361236+ continue;
12331237 }
12381238+#endif // defined(VK_GOOGLE_display_timing)
12391239+12401240+#if defined(VK_EXT_global_priority)
12341241 if (strcmp(ext, VK_EXT_GLOBAL_PRIORITY_EXTENSION_NAME) == 0) {
12351242 vk->has_EXT_global_priority = true;
12431243+ continue;
12361244 }
12371237-#ifdef VK_EXT_robustness2
12451245+#endif // defined(VK_EXT_global_priority)
12461246+12471247+#if defined(VK_EXT_robustness2)
12381248 if (strcmp(ext, VK_EXT_ROBUSTNESS_2_EXTENSION_NAME) == 0) {
12391249 vk->has_EXT_robustness2 = true;
12501250+ continue;
12401251 }
12411241-#endif
12521252+#endif // defined(VK_EXT_robustness2)
12421253 }
12541254+ // end of GENERATED extension code - do not modify - used by scripts
12431255}
1244125612451257static VkResult
+3
src/xrt/auxiliary/vk/vk_helpers.h
···48484949 struct os_mutex queue_mutex;
50505151+ // beginning of GENERATED extension code - do not modify - used by scripts
5152 bool has_GOOGLE_display_timing;
5253 bool has_EXT_global_priority;
5354 bool has_EXT_robustness2;
5555+ // end of GENERATED extension code - do not modify - used by scripts
54565557 bool is_tegra;
5658···129131#if defined(VK_USE_PLATFORM_WIN32_KHR)
130132 PFN_vkCreateWin32SurfaceKHR vkCreateWin32SurfaceKHR;
131133#endif // defined(VK_USE_PLATFORM_WIN32_KHR)
134134+132135 // end of GENERATED instance loader code - do not modify - used by scripts
133136134137 // beginning of GENERATED device loader code - do not modify - used by scripts