| 1 |
# Copyright 2023 Google LLC |
| 2 |
# SPDX-License-Identifier: MIT |
| 3 |
from .vulkantypes import VulkanType, VulkanTypeInfo, VulkanCompoundType, VulkanAPI |
| 4 |
from collections import OrderedDict |
| 5 |
from copy import copy |
| 6 |
from pathlib import Path, PurePosixPath |
| 7 |
|
| 8 |
import os |
| 9 |
import sys |
| 10 |
import shutil |
| 11 |
import subprocess |
| 12 |
import re |
| 13 |
|
| 14 |
# Class capturing a single file |
| 15 |
|
| 16 |
|
| 17 |
class SingleFileModule(object): |
| 18 |
def __init__(self, suffix, directory, basename, customAbsDir=None, suppress=False): |
| 19 |
self.directory = directory |
| 20 |
self.basename = basename |
| 21 |
self.customAbsDir = customAbsDir |
| 22 |
self.suffix = suffix |
| 23 |
self.file = None |
| 24 |
|
| 25 |
self.preamble = "" |
| 26 |
self.postamble = "" |
| 27 |
|
| 28 |
self.suppress = suppress |
| 29 |
|
| 30 |
def begin(self, globalDir): |
| 31 |
if self.suppress: |
| 32 |
return |
| 33 |
|
| 34 |
# Create subdirectory, if needed |
| 35 |
if self.customAbsDir: |
| 36 |
absDir = self.customAbsDir |
| 37 |
else: |
| 38 |
absDir = os.path.join(globalDir, self.directory) |
| 39 |
|
| 40 |
filename = os.path.join(absDir, self.basename) |
| 41 |
|
| 42 |
self.file = open(filename + self.suffix, "w", encoding="utf-8") |
| 43 |
self.file.write(self.preamble) |
| 44 |
|
| 45 |
def append(self, toAppend): |
| 46 |
if self.suppress: |
| 47 |
return |
| 48 |
|
| 49 |
self.file.write(toAppend) |
| 50 |
|
| 51 |
def end(self): |
| 52 |
if self.suppress: |
| 53 |
return |
| 54 |
|
| 55 |
self.file.write(self.postamble) |
| 56 |
self.file.close() |
| 57 |
|
| 58 |
def getMakefileSrcEntry(self): |
| 59 |
return "" |
| 60 |
|
| 61 |
def getCMakeSrcEntry(self): |
| 62 |
return "" |
| 63 |
|
| 64 |
# Class capturing a .cpp file and a .h file (a "C++ module") |
| 65 |
|
| 66 |
|
| 67 |
class Module(object): |
| 68 |
|
| 69 |
def __init__( |
| 70 |
self, directory, basename, customAbsDir=None, suppress=False, implOnly=False, |
| 71 |
headerOnly=False, suppressFeatureGuards=False): |
| 72 |
self._headerFileModule = SingleFileModule( |
| 73 |
".h", directory, basename, customAbsDir, suppress or implOnly) |
| 74 |
self._implFileModule = SingleFileModule( |
| 75 |
".cpp", directory, basename, customAbsDir, suppress or headerOnly) |
| 76 |
|
| 77 |
self._headerOnly = headerOnly |
| 78 |
self._implOnly = implOnly |
| 79 |
|
| 80 |
self.directory = directory |
| 81 |
self.basename = basename |
| 82 |
self._customAbsDir = customAbsDir |
| 83 |
|
| 84 |
self.suppressFeatureGuards = suppressFeatureGuards |
| 85 |
|
| 86 |
@property |
| 87 |
def suppress(self): |
| 88 |
raise AttributeError("suppress is write only") |
| 89 |
|
| 90 |
@suppress.setter |
| 91 |
def suppress(self, value: bool): |
| 92 |
self._headerFileModule.suppress = self._implOnly or value |
| 93 |
self._implFileModule.suppress = self._headerOnly or value |
| 94 |
|
| 95 |
@property |
| 96 |
def headerPreamble(self) -> str: |
| 97 |
return self._headerFileModule.preamble |
| 98 |
|
| 99 |
@headerPreamble.setter |
| 100 |
def headerPreamble(self, value: str): |
| 101 |
self._headerFileModule.preamble = value |
| 102 |
|
| 103 |
@property |
| 104 |
def headerPostamble(self) -> str: |
| 105 |
return self._headerFileModule.postamble |
| 106 |
|
| 107 |
@headerPostamble.setter |
| 108 |
def headerPostamble(self, value: str): |
| 109 |
self._headerFileModule.postamble = value |
| 110 |
|
| 111 |
@property |
| 112 |
def implPreamble(self) -> str: |
| 113 |
return self._implFileModule.preamble |
| 114 |
|
| 115 |
@implPreamble.setter |
| 116 |
def implPreamble(self, value: str): |
| 117 |
self._implFileModule.preamble = value |
| 118 |
|
| 119 |
@property |
| 120 |
def implPostamble(self) -> str: |
| 121 |
return self._implFileModule.postamble |
| 122 |
|
| 123 |
@implPostamble.setter |
| 124 |
def implPostamble(self, value: str): |
| 125 |
self._implFileModule.postamble = value |
| 126 |
|
| 127 |
def getMakefileSrcEntry(self): |
| 128 |
if self._customAbsDir: |
| 129 |
return self.basename + ".cpp \\\n" |
| 130 |
dirName = self.directory |
| 131 |
baseName = self.basename |
| 132 |
joined = os.path.join(dirName, baseName) |
| 133 |
return " " + joined + ".cpp \\\n" |
| 134 |
|
| 135 |
def getCMakeSrcEntry(self): |
| 136 |
if self._customAbsDir: |
| 137 |
return "\n" + self.basename + ".cpp " |
| 138 |
dirName = Path(self.directory) |
| 139 |
baseName = Path(self.basename) |
| 140 |
joined = PurePosixPath(dirName / baseName) |
| 141 |
return "\n " + str(joined) + ".cpp " |
| 142 |
|
| 143 |
def begin(self, globalDir): |
| 144 |
self._headerFileModule.begin(globalDir) |
| 145 |
self._implFileModule.begin(globalDir) |
| 146 |
|
| 147 |
def appendHeader(self, toAppend): |
| 148 |
self._headerFileModule.append(toAppend) |
| 149 |
|
| 150 |
def appendImpl(self, toAppend): |
| 151 |
self._implFileModule.append(toAppend) |
| 152 |
|
| 153 |
def end(self): |
| 154 |
self._headerFileModule.end() |
| 155 |
self._implFileModule.end() |
| 156 |
|
| 157 |
# Removes empty ifdef blocks with a regex query over the file |
| 158 |
# which are mainly introduced by extensions with no functions or variables |
| 159 |
def remove_empty_ifdefs(filename: Path): |
| 160 |
"""Removes empty #ifdef blocks from a C++ file.""" |
| 161 |
|
| 162 |
# Load file contents |
| 163 |
with open(filename, 'r') as file: |
| 164 |
content = file.read() |
| 165 |
|
| 166 |
# Regular Expression Pattern |
| 167 |
pattern = r"#ifdef\s+(\w+)\s*(?://.*)?\s*\n\s*#endif\s*(?://.*)?\s*" |
| 168 |
|
| 169 |
# Replace Empty Blocks |
| 170 |
modified_content = re.sub(pattern, "", content) |
| 171 |
|
| 172 |
# Save file back |
| 173 |
with open(filename, 'w') as file: |
| 174 |
file.write(modified_content) |
| 175 |
|
| 176 |
clang_format_command = shutil.which('clang-format') |
| 177 |
|
| 178 |
def formatFile(filename: Path): |
| 179 |
if "GFXSTREAM_NO_CLANG_FMT" in os.environ: |
| 180 |
return |
| 181 |
assert (clang_format_command is not None) |
| 182 |
assert (subprocess.call([clang_format_command, "-i", |
| 183 |
"--style=file", str(filename.resolve())]) == 0) |
| 184 |
|
| 185 |
if not self._headerFileModule.suppress: |
| 186 |
filename = Path(self._headerFileModule.file.name) |
| 187 |
remove_empty_ifdefs(filename) |
| 188 |
formatFile(filename) |
| 189 |
|
| 190 |
if not self._implFileModule.suppress: |
| 191 |
filename = Path(self._implFileModule.file.name) |
| 192 |
remove_empty_ifdefs(filename) |
| 193 |
formatFile(filename) |
| 194 |
|
| 195 |
|
| 196 |
class PyScript(SingleFileModule): |
| 197 |
def __init__(self, directory, basename, customAbsDir=None, suppress=False): |
| 198 |
super().__init__(".py", directory, basename, customAbsDir, suppress) |
| 199 |
|
| 200 |
|
| 201 |
# Class capturing a .proto protobuf definition file |
| 202 |
class Proto(SingleFileModule): |
| 203 |
|
| 204 |
def __init__(self, directory, basename, customAbsDir=None, suppress=False): |
| 205 |
super().__init__(".proto", directory, basename, customAbsDir, suppress) |
| 206 |
|
| 207 |
def getMakefileSrcEntry(self): |
| 208 |
super().getMakefileSrcEntry() |
| 209 |
if self.customAbsDir: |
| 210 |
return self.basename + ".proto \\\n" |
| 211 |
dirName = self.directory |
| 212 |
baseName = self.basename |
| 213 |
joined = os.path.join(dirName, baseName) |
| 214 |
return " " + joined + ".proto \\\n" |
| 215 |
|
| 216 |
def getCMakeSrcEntry(self): |
| 217 |
super().getCMakeSrcEntry() |
| 218 |
if self.customAbsDir: |
| 219 |
return "\n" + self.basename + ".proto " |
| 220 |
|
| 221 |
dirName = self.directory |
| 222 |
baseName = self.basename |
| 223 |
joined = os.path.join(dirName, baseName) |
| 224 |
return "\n " + joined + ".proto " |
| 225 |
|
| 226 |
class CodeGen(object): |
| 227 |
|
| 228 |
def __init__(self,): |
| 229 |
self.code = "" |
| 230 |
self.indentLevel = 0 |
| 231 |
self.gensymCounter = [-1] |
| 232 |
|
| 233 |
def var(self, prefix="cgen_var"): |
| 234 |
self.gensymCounter[-1] += 1 |
| 235 |
res = "%s_%s" % (prefix, '_'.join(str(i) for i in self.gensymCounter if i >= 0)) |
| 236 |
return res |
| 237 |
|
| 238 |
def swapCode(self,): |
| 239 |
res = "%s" % self.code |
| 240 |
self.code = "" |
| 241 |
return res |
| 242 |
|
| 243 |
def indent(self,extra=0): |
| 244 |
return "".join(" " * (self.indentLevel + extra)) |
| 245 |
|
| 246 |
def incrIndent(self,): |
| 247 |
self.indentLevel += 1 |
| 248 |
|
| 249 |
def decrIndent(self,): |
| 250 |
if self.indentLevel > 0: |
| 251 |
self.indentLevel -= 1 |
| 252 |
|
| 253 |
def beginBlock(self, bracketPrint=True): |
| 254 |
if bracketPrint: |
| 255 |
self.code += self.indent() + "{\n" |
| 256 |
self.indentLevel += 1 |
| 257 |
self.gensymCounter.append(-1) |
| 258 |
|
| 259 |
def endBlock(self,bracketPrint=True): |
| 260 |
self.indentLevel -= 1 |
| 261 |
if bracketPrint: |
| 262 |
self.code += self.indent() + "}\n" |
| 263 |
del self.gensymCounter[-1] |
| 264 |
|
| 265 |
def beginIf(self, cond): |
| 266 |
self.code += self.indent() + "if (" + cond + ")\n" |
| 267 |
self.beginBlock() |
| 268 |
|
| 269 |
def beginElse(self, cond = None): |
| 270 |
if cond is not None: |
| 271 |
self.code += \ |
| 272 |
self.indent() + \ |
| 273 |
"else if (" + cond + ")\n" |
| 274 |
else: |
| 275 |
self.code += self.indent() + "else\n" |
| 276 |
self.beginBlock() |
| 277 |
|
| 278 |
def endElse(self): |
| 279 |
self.endBlock() |
| 280 |
|
| 281 |
def endIf(self): |
| 282 |
self.endBlock() |
| 283 |
|
| 284 |
def beginSwitch(self, switchvar): |
| 285 |
self.code += self.indent() + "switch (" + switchvar + ")\n" |
| 286 |
self.beginBlock() |
| 287 |
|
| 288 |
def switchCase(self, switchval, blocked = False): |
| 289 |
self.code += self.indent() + "case %s:" % switchval |
| 290 |
self.beginBlock(bracketPrint = blocked) |
| 291 |
|
| 292 |
def switchCaseBreak(self, switchval, blocked = False): |
| 293 |
self.code += self.indent() + "case %s:" % switchval |
| 294 |
self.endBlock(bracketPrint = blocked) |
| 295 |
|
| 296 |
def switchCaseDefault(self, blocked = False): |
| 297 |
self.code += self.indent() + "default:" % switchval |
| 298 |
self.beginBlock(bracketPrint = blocked) |
| 299 |
|
| 300 |
def endSwitch(self): |
| 301 |
self.endBlock() |
| 302 |
|
| 303 |
def beginWhile(self, cond): |
| 304 |
self.code += self.indent() + "while (" + cond + ")\n" |
| 305 |
self.beginBlock() |
| 306 |
|
| 307 |
def endWhile(self): |
| 308 |
self.endBlock() |
| 309 |
|
| 310 |
def beginFor(self, initial, condition, increment): |
| 311 |
self.code += \ |
| 312 |
self.indent() + "for (" + \ |
| 313 |
"; ".join([initial, condition, increment]) + \ |
| 314 |
")\n" |
| 315 |
self.beginBlock() |
| 316 |
|
| 317 |
def endFor(self): |
| 318 |
self.endBlock() |
| 319 |
|
| 320 |
def beginLoop(self, loopVarType, loopVar, loopInit, loopBound): |
| 321 |
self.beginFor( |
| 322 |
"%s %s = %s" % (loopVarType, loopVar, loopInit), |
| 323 |
"%s < %s" % (loopVar, loopBound), |
| 324 |
"++%s" % (loopVar)) |
| 325 |
|
| 326 |
def endLoop(self): |
| 327 |
self.endBlock() |
| 328 |
|
| 329 |
def stmt(self, code): |
| 330 |
self.code += self.indent() + code + ";\n" |
| 331 |
|
| 332 |
def line(self, code): |
| 333 |
self.code += self.indent() + code + "\n" |
| 334 |
|
| 335 |
def leftline(self, code): |
| 336 |
self.code += code + "\n" |
| 337 |
|
| 338 |
def makeCallExpr(self, funcName, parameters): |
| 339 |
return funcName + "(%s)" % (", ".join(parameters)) |
| 340 |
|
| 341 |
def funcCall(self, lhs, funcName, parameters): |
| 342 |
res = self.indent() |
| 343 |
|
| 344 |
if lhs is not None: |
| 345 |
res += lhs + " = " |
| 346 |
|
| 347 |
res += self.makeCallExpr(funcName, parameters) + ";\n" |
| 348 |
self.code += res |
| 349 |
|
| 350 |
def funcCallRet(self, _lhs, funcName, parameters): |
| 351 |
res = self.indent() |
| 352 |
res += "return " + self.makeCallExpr(funcName, parameters) + ";\n" |
| 353 |
self.code += res |
| 354 |
|
| 355 |
# Given a VulkanType object, generate a C type declaration |
| 356 |
# with optional parameter name: |
| 357 |
# [const] [typename][*][const*] [paramName] |
| 358 |
def makeCTypeDecl(self, vulkanType, useParamName=True): |
| 359 |
constness = "const " if vulkanType.isConst else "" |
| 360 |
typeName = vulkanType.typeName |
| 361 |
|
| 362 |
if vulkanType.pointerIndirectionLevels == 0: |
| 363 |
ptrSpec = "" |
| 364 |
elif vulkanType.isPointerToConstPointer: |
| 365 |
ptrSpec = "* const*" if vulkanType.isConst else "**" |
| 366 |
if vulkanType.pointerIndirectionLevels > 2: |
| 367 |
ptrSpec += "*" * (vulkanType.pointerIndirectionLevels - 2) |
| 368 |
else: |
| 369 |
ptrSpec = "*" * vulkanType.pointerIndirectionLevels |
| 370 |
|
| 371 |
if useParamName and (vulkanType.paramName is not None): |
| 372 |
paramStr = (" " + vulkanType.paramName) |
| 373 |
else: |
| 374 |
paramStr = "" |
| 375 |
|
| 376 |
return "%s%s%s%s" % (constness, typeName, ptrSpec, paramStr) |
| 377 |
|
| 378 |
def makeRichCTypeDecl(self, vulkanType, useParamName=True): |
| 379 |
constness = "const " if vulkanType.isConst else "" |
| 380 |
typeName = vulkanType.typeName |
| 381 |
|
| 382 |
if vulkanType.pointerIndirectionLevels == 0: |
| 383 |
ptrSpec = "" |
| 384 |
elif vulkanType.isPointerToConstPointer: |
| 385 |
ptrSpec = "* const*" if vulkanType.isConst else "**" |
| 386 |
if vulkanType.pointerIndirectionLevels > 2: |
| 387 |
ptrSpec += "*" * (vulkanType.pointerIndirectionLevels - 2) |
| 388 |
else: |
| 389 |
ptrSpec = "*" * vulkanType.pointerIndirectionLevels |
| 390 |
|
| 391 |
if useParamName and (vulkanType.paramName is not None): |
| 392 |
paramStr = (" " + vulkanType.paramName) |
| 393 |
else: |
| 394 |
paramStr = "" |
| 395 |
|
| 396 |
if vulkanType.staticArrExpr: |
| 397 |
staticArrInfo = "[%s]" % vulkanType.staticArrExpr |
| 398 |
else: |
| 399 |
staticArrInfo = "" |
| 400 |
|
| 401 |
return "%s%s%s%s%s" % (constness, typeName, ptrSpec, paramStr, staticArrInfo) |
| 402 |
|
| 403 |
# Given a VulkanAPI object, generate the C function protype: |
| 404 |
# <returntype> <funcname>(<parameters>) |
| 405 |
def makeFuncProto(self, vulkanApi, useParamName=True): |
| 406 |
|
| 407 |
protoBegin = "%s %s" % (self.makeCTypeDecl( |
| 408 |
vulkanApi.retType, useParamName=False), vulkanApi.name) |
| 409 |
|
| 410 |
def getFuncArgDecl(param): |
| 411 |
if param.staticArrExpr: |
| 412 |
return self.makeCTypeDecl(param, useParamName=useParamName) + ("[%s]" % param.staticArrExpr) |
| 413 |
else: |
| 414 |
return self.makeCTypeDecl(param, useParamName=useParamName) |
| 415 |
|
| 416 |
protoParams = "(\n %s)" % ((",\n%s" % self.indent(1)).join( |
| 417 |
list(map( |
| 418 |
getFuncArgDecl, |
| 419 |
vulkanApi.parameters)))) |
| 420 |
|
| 421 |
return protoBegin + protoParams |
| 422 |
|
| 423 |
def makeFuncAlias(self, nameDst, nameSrc): |
| 424 |
return "DEFINE_ALIAS_FUNCTION({}, {})\n\n".format(nameSrc, nameDst) |
| 425 |
|
| 426 |
def makeFuncDecl(self, vulkanApi): |
| 427 |
return self.makeFuncProto(vulkanApi) + ";\n\n" |
| 428 |
|
| 429 |
def makeFuncImpl(self, vulkanApi, codegenFunc): |
| 430 |
self.swapCode() |
| 431 |
|
| 432 |
self.line(self.makeFuncProto(vulkanApi)) |
| 433 |
self.beginBlock() |
| 434 |
codegenFunc(self) |
| 435 |
self.endBlock() |
| 436 |
|
| 437 |
return self.swapCode() + "\n" |
| 438 |
|
| 439 |
def emitFuncImpl(self, vulkanApi, codegenFunc): |
| 440 |
self.line(self.makeFuncProto(vulkanApi)) |
| 441 |
self.beginBlock() |
| 442 |
codegenFunc(self) |
| 443 |
self.endBlock() |
| 444 |
|
| 445 |
def makeStructAccess(self, |
| 446 |
vulkanType, |
| 447 |
structVarName, |
| 448 |
asPtr=True, |
| 449 |
structAsPtr=True, |
| 450 |
accessIndex=None): |
| 451 |
|
| 452 |
deref = "->" if structAsPtr else "." |
| 453 |
|
| 454 |
indexExpr = (" + %s" % accessIndex) if accessIndex else "" |
| 455 |
|
| 456 |
addrOfExpr = "" if vulkanType.accessibleAsPointer() or ( |
| 457 |
not asPtr) else "&" |
| 458 |
|
| 459 |
return "%s%s%s%s%s" % (addrOfExpr, structVarName, deref, |
| 460 |
vulkanType.paramName, indexExpr) |
| 461 |
|
| 462 |
def makeRawLengthAccess(self, vulkanType): |
| 463 |
lenExpr = vulkanType.getLengthExpression() |
| 464 |
|
| 465 |
if not lenExpr: |
| 466 |
return None, None |
| 467 |
|
| 468 |
if lenExpr == "null-terminated": |
| 469 |
return "strlen(%s)" % vulkanType.paramName, None |
| 470 |
|
| 471 |
return lenExpr, None |
| 472 |
|
| 473 |
def makeLengthAccessFromStruct(self, |
| 474 |
structInfo, |
| 475 |
vulkanType, |
| 476 |
structVarName, |
| 477 |
asPtr=True): |
| 478 |
# Handle special cases first |
| 479 |
# Mostly when latexmath is involved |
| 480 |
def handleSpecialCases(structInfo, vulkanType, structVarName, asPtr): |
| 481 |
cases = [ |
| 482 |
{ |
| 483 |
"structName": "VkShaderModuleCreateInfo", |
| 484 |
"field": "pCode", |
| 485 |
"lenExprMember": "codeSize", |
| 486 |
"postprocess": lambda expr: "(%s / 4)" % expr |
| 487 |
}, |
| 488 |
{ |
| 489 |
"structName": "VkPipelineMultisampleStateCreateInfo", |
| 490 |
"field": "pSampleMask", |
| 491 |
"lenExprMember": "rasterizationSamples", |
| 492 |
"postprocess": lambda expr: "(((%s) + 31) / 32)" % expr |
| 493 |
}, |
| 494 |
{ |
| 495 |
"structName": "VkAccelerationStructureVersionInfoKHR", |
| 496 |
"field": "pVersionData", |
| 497 |
"lenExprMember": "", |
| 498 |
"postprocess": lambda _: "2*VK_UUID_SIZE" |
| 499 |
}, |
| 500 |
] |
| 501 |
|
| 502 |
for c in cases: |
| 503 |
if (structInfo.name, vulkanType.paramName) == (c["structName"], |
| 504 |
c["field"]): |
| 505 |
deref = "->" if asPtr else "." |
| 506 |
expr = "%s%s%s" % (structVarName, deref, |
| 507 |
c["lenExprMember"]) |
| 508 |
lenAccessGuardExpr = "%s" % structVarName |
| 509 |
return c["postprocess"](expr), lenAccessGuardExpr |
| 510 |
|
| 511 |
return None, None |
| 512 |
|
| 513 |
specialCaseAccess = \ |
| 514 |
handleSpecialCases( |
| 515 |
structInfo, vulkanType, structVarName, asPtr) |
| 516 |
|
| 517 |
if specialCaseAccess != (None, None): |
| 518 |
return specialCaseAccess |
| 519 |
|
| 520 |
lenExpr = vulkanType.getLengthExpression() |
| 521 |
|
| 522 |
if not lenExpr: |
| 523 |
return None, None |
| 524 |
|
| 525 |
deref = "->" if asPtr else "." |
| 526 |
lenAccessGuardExpr = "%s" % ( |
| 527 |
|
| 528 |
structVarName) if deref else None |
| 529 |
if lenExpr == "null-terminated": |
| 530 |
return "strlen(%s%s%s)" % (structVarName, deref, |
| 531 |
vulkanType.paramName), lenAccessGuardExpr |
| 532 |
|
| 533 |
if not structInfo.getMember(lenExpr): |
| 534 |
return self.makeRawLengthAccess(vulkanType) |
| 535 |
|
| 536 |
return "%s%s%s" % (structVarName, deref, lenExpr), lenAccessGuardExpr |
| 537 |
|
| 538 |
def makeLengthAccessFromApi(self, api, vulkanType): |
| 539 |
# Handle special cases first |
| 540 |
# Mostly when :: is involved |
| 541 |
def handleSpecialCases(vulkanType): |
| 542 |
lenExpr = vulkanType.getLengthExpression() |
| 543 |
|
| 544 |
if lenExpr is None: |
| 545 |
return None, None |
| 546 |
|
| 547 |
if "::" in lenExpr: |
| 548 |
structVarName, memberVarName = lenExpr.split("::") |
| 549 |
lenAccessGuardExpr = "%s" % (structVarName) |
| 550 |
return "%s->%s" % (structVarName, memberVarName), lenAccessGuardExpr |
| 551 |
return None, None |
| 552 |
|
| 553 |
specialCaseAccess = handleSpecialCases(vulkanType) |
| 554 |
|
| 555 |
if specialCaseAccess != (None, None): |
| 556 |
return specialCaseAccess |
| 557 |
|
| 558 |
lenExpr = vulkanType.getLengthExpression() |
| 559 |
|
| 560 |
if not lenExpr: |
| 561 |
return None, None |
| 562 |
|
| 563 |
lenExprInfo = api.getParameter(lenExpr) |
| 564 |
|
| 565 |
if not lenExprInfo: |
| 566 |
return self.makeRawLengthAccess(vulkanType) |
| 567 |
|
| 568 |
if lenExpr == "null-terminated": |
| 569 |
return "strlen(%s)" % vulkanType.paramName(), None |
| 570 |
else: |
| 571 |
retVal = "%s" % lenExpr |
| 572 |
needDeref = (lenExprInfo.pointerIndirectionLevels > 0) |
| 573 |
lenAccessGuardExpr = retVal if needDeref else None |
| 574 |
if not retVal.isalnum(): |
| 575 |
retVal = "(%s)" % (retVal) |
| 576 |
if needDeref > 0: |
| 577 |
retVal = "(*%s)" % retVal |
| 578 |
return retVal, lenAccessGuardExpr |
| 579 |
|
| 580 |
def accessParameter(self, param, asPtr=True): |
| 581 |
if asPtr: |
| 582 |
if param.pointerIndirectionLevels > 0: |
| 583 |
return param.paramName |
| 584 |
else: |
| 585 |
return "&%s" % param.paramName |
| 586 |
else: |
| 587 |
return param.paramName |
| 588 |
|
| 589 |
def sizeofExpr(self, vulkanType): |
| 590 |
return "sizeof(%s)" % ( |
| 591 |
self.makeCTypeDecl(vulkanType, useParamName=False)) |
| 592 |
|
| 593 |
def generalAccess(self, |
| 594 |
vulkanType, |
| 595 |
parentVarName=None, |
| 596 |
asPtr=True, |
| 597 |
structAsPtr=True): |
| 598 |
if vulkanType.parent is None: |
| 599 |
if parentVarName is None: |
| 600 |
return self.accessParameter(vulkanType, asPtr=asPtr) |
| 601 |
else: |
| 602 |
return self.accessParameter(vulkanType.withModifiedName(parentVarName), asPtr=asPtr) |
| 603 |
|
| 604 |
if isinstance(vulkanType.parent, VulkanCompoundType): |
| 605 |
return self.makeStructAccess( |
| 606 |
vulkanType, parentVarName, asPtr=asPtr, structAsPtr=structAsPtr) |
| 607 |
|
| 608 |
if isinstance(vulkanType.parent, VulkanAPI): |
| 609 |
if parentVarName is None: |
| 610 |
return self.accessParameter(vulkanType, asPtr=asPtr) |
| 611 |
else: |
| 612 |
return self.accessParameter(vulkanType.withModifiedName(parentVarName), asPtr=asPtr) |
| 613 |
|
| 614 |
os.abort("Could not find a way to access Vulkan type %s" % |
| 615 |
vulkanType.name) |
| 616 |
|
| 617 |
def makeLengthAccess(self, vulkanType, parentVarName="parent"): |
| 618 |
if vulkanType.parent is None: |
| 619 |
return self.makeRawLengthAccess(vulkanType) |
| 620 |
|
| 621 |
if isinstance(vulkanType.parent, VulkanCompoundType): |
| 622 |
return self.makeLengthAccessFromStruct( |
| 623 |
vulkanType.parent, vulkanType, parentVarName, asPtr=True) |
| 624 |
|
| 625 |
if isinstance(vulkanType.parent, VulkanAPI): |
| 626 |
return self.makeLengthAccessFromApi(vulkanType.parent, vulkanType) |
| 627 |
|
| 628 |
os.abort("Could not find a way to access length of Vulkan type %s" % |
| 629 |
vulkanType.name) |
| 630 |
|
| 631 |
def generalLengthAccess(self, vulkanType, parentVarName="parent"): |
| 632 |
return self.makeLengthAccess(vulkanType, parentVarName)[0] |
| 633 |
|
| 634 |
def generalLengthAccessGuard(self, vulkanType, parentVarName="parent"): |
| 635 |
return self.makeLengthAccess(vulkanType, parentVarName)[1] |
| 636 |
|
| 637 |
def vkApiCall(self, api, customPrefix="", globalStatePrefix="", |
| 638 |
customParameters=None, checkForDeviceLost=False, |
| 639 |
checkDispatcher=None): |
| 640 |
callLhs = None |
| 641 |
|
| 642 |
retTypeName = api.getRetTypeExpr() |
| 643 |
retVar = None |
| 644 |
|
| 645 |
if retTypeName != "void": |
| 646 |
retVar = api.getRetVarExpr() |
| 647 |
defaultReturn = "(%s)0" % retTypeName |
| 648 |
if retTypeName == "VkResult": |
| 649 |
# TODO: return a valid error code based on the call |
| 650 |
# This is used to handle invalid dispatcher and snapshot states |
| 651 |
deviceLostFunctions = ["vkQueueSubmit", |
| 652 |
"vkQueueWaitIdle", |
| 653 |
"vkWaitForFences"] |
| 654 |
defaultReturn = "VK_ERROR_OUT_OF_HOST_MEMORY" |
| 655 |
if api in deviceLostFunctions: |
| 656 |
defaultReturn = "VK_ERROR_DEVICE_LOST" |
| 657 |
self.stmt("%s %s = %s" % (retTypeName, retVar, defaultReturn)) |
| 658 |
callLhs = retVar |
| 659 |
|
| 660 |
if (checkDispatcher): |
| 661 |
self.beginIf(checkDispatcher) |
| 662 |
|
| 663 |
if customParameters is None: |
| 664 |
self.funcCall( |
| 665 |
callLhs, customPrefix + api.name, [p.paramName for p in api.parameters]) |
| 666 |
else: |
| 667 |
self.funcCall( |
| 668 |
callLhs, customPrefix + api.name, customParameters) |
| 669 |
|
| 670 |
if (checkDispatcher): |
| 671 |
self.endIf() |
| 672 |
|
| 673 |
if retTypeName == "VkResult" and checkForDeviceLost: |
| 674 |
self.stmt("if ((%s) == VK_ERROR_DEVICE_LOST) %sDeviceLost()" % (callLhs, globalStatePrefix)) |
| 675 |
|
| 676 |
return (retTypeName, retVar) |
| 677 |
|
| 678 |
def makeCheckVkSuccess(self, expr): |
| 679 |
return "((%s) == VK_SUCCESS)" % expr |
| 680 |
|
| 681 |
def makeReinterpretCast(self, varName, typeName, const=True): |
| 682 |
return "reinterpret_cast<%s%s*>(%s)" % \ |
| 683 |
("const " if const else "", typeName, varName) |
| 684 |
|
| 685 |
def validPrimitive(self, typeInfo, typeName): |
| 686 |
size = typeInfo.getPrimitiveEncodingSize(typeName) |
| 687 |
return size != None |
| 688 |
|
| 689 |
def makePrimitiveStreamMethod(self, typeInfo, typeName, direction="write"): |
| 690 |
if not self.validPrimitive(typeInfo, typeName): |
| 691 |
return None |
| 692 |
|
| 693 |
size = typeInfo.getPrimitiveEncodingSize(typeName) |
| 694 |
prefix = "put" if direction == "write" else "get" |
| 695 |
suffix = None |
| 696 |
if size == 1: |
| 697 |
suffix = "Byte" |
| 698 |
elif size == 2: |
| 699 |
suffix = "Be16" |
| 700 |
elif size == 4: |
| 701 |
suffix = "Be32" |
| 702 |
elif size == 8: |
| 703 |
suffix = "Be64" |
| 704 |
|
| 705 |
if suffix: |
| 706 |
return prefix + suffix |
| 707 |
|
| 708 |
return None |
| 709 |
|
| 710 |
def makePrimitiveStreamMethodInPlace(self, typeInfo, typeName, direction="write"): |
| 711 |
if not self.validPrimitive(typeInfo, typeName): |
| 712 |
return None |
| 713 |
|
| 714 |
size = typeInfo.getPrimitiveEncodingSize(typeName) |
| 715 |
prefix = "to" if direction == "write" else "from" |
| 716 |
suffix = None |
| 717 |
if size == 1: |
| 718 |
suffix = "Byte" |
| 719 |
elif size == 2: |
| 720 |
suffix = "Be16" |
| 721 |
elif size == 4: |
| 722 |
suffix = "Be32" |
| 723 |
elif size == 8: |
| 724 |
suffix = "Be64" |
| 725 |
|
| 726 |
if suffix: |
| 727 |
return prefix + suffix |
| 728 |
|
| 729 |
return None |
| 730 |
|
| 731 |
def streamPrimitive(self, typeInfo, streamVar, accessExpr, accessType, direction="write"): |
| 732 |
accessTypeName = accessType.typeName |
| 733 |
|
| 734 |
if accessType.pointerIndirectionLevels == 0 and not self.validPrimitive(typeInfo, accessTypeName): |
| 735 |
print("Tried to stream a non-primitive type: %s" % accessTypeName) |
| 736 |
os.abort() |
| 737 |
|
| 738 |
needPtrCast = False |
| 739 |
|
| 740 |
if accessType.pointerIndirectionLevels > 0: |
| 741 |
streamSize = 8 |
| 742 |
streamStorageVarType = "uint64_t" |
| 743 |
needPtrCast = True |
| 744 |
streamMethod = "putBe64" if direction == "write" else "getBe64" |
| 745 |
else: |
| 746 |
streamSize = typeInfo.getPrimitiveEncodingSize(accessTypeName) |
| 747 |
if streamSize == 1: |
| 748 |
streamStorageVarType = "uint8_t" |
| 749 |
elif streamSize == 2: |
| 750 |
streamStorageVarType = "uint16_t" |
| 751 |
elif streamSize == 4: |
| 752 |
streamStorageVarType = "uint32_t" |
| 753 |
elif streamSize == 8: |
| 754 |
streamStorageVarType = "uint64_t" |
| 755 |
streamMethod = self.makePrimitiveStreamMethod( |
| 756 |
typeInfo, accessTypeName, direction=direction) |
| 757 |
|
| 758 |
streamStorageVar = self.var() |
| 759 |
|
| 760 |
accessCast = self.makeRichCTypeDecl(accessType, useParamName=False) |
| 761 |
|
| 762 |
ptrCast = "(uintptr_t)" if needPtrCast else "" |
| 763 |
|
| 764 |
if direction == "read": |
| 765 |
self.stmt("%s = (%s)%s%s->%s()" % |
| 766 |
(accessExpr, |
| 767 |
accessCast, |
| 768 |
ptrCast, |
| 769 |
streamVar, |
| 770 |
streamMethod)) |
| 771 |
else: |
| 772 |
self.stmt("%s %s = (%s)%s%s" % |
| 773 |
(streamStorageVarType, streamStorageVar, |
| 774 |
streamStorageVarType, ptrCast, accessExpr)) |
| 775 |
self.stmt("%s->%s(%s)" % |
| 776 |
(streamVar, streamMethod, streamStorageVar)) |
| 777 |
|
| 778 |
def memcpyPrimitive(self, typeInfo, streamVar, accessExpr, accessType, variant, direction="write"): |
| 779 |
accessTypeName = accessType.typeName |
| 780 |
|
| 781 |
if accessType.pointerIndirectionLevels == 0 and not self.validPrimitive(typeInfo, accessTypeName): |
| 782 |
print("Tried to stream a non-primitive type: %s" % accessTypeName) |
| 783 |
os.abort() |
| 784 |
|
| 785 |
needPtrCast = False |
| 786 |
|
| 787 |
streamSize = 8 |
| 788 |
|
| 789 |
if accessType.pointerIndirectionLevels > 0: |
| 790 |
streamSize = 8 |
| 791 |
streamStorageVarType = "uint64_t" |
| 792 |
needPtrCast = True |
| 793 |
streamMethod = "toBe64" if direction == "write" else "fromBe64" |
| 794 |
else: |
| 795 |
streamSize = typeInfo.getPrimitiveEncodingSize(accessTypeName) |
| 796 |
if streamSize == 1: |
| 797 |
streamStorageVarType = "uint8_t" |
| 798 |
elif streamSize == 2: |
| 799 |
streamStorageVarType = "uint16_t" |
| 800 |
elif streamSize == 4: |
| 801 |
streamStorageVarType = "uint32_t" |
| 802 |
elif streamSize == 8: |
| 803 |
streamStorageVarType = "uint64_t" |
| 804 |
streamMethod = self.makePrimitiveStreamMethodInPlace( |
| 805 |
typeInfo, accessTypeName, direction=direction) |
| 806 |
|
| 807 |
streamStorageVar = self.var() |
| 808 |
|
| 809 |
accessCast = self.makeRichCTypeDecl(accessType, useParamName=False) |
| 810 |
|
| 811 |
if direction == "read": |
| 812 |
accessCast = self.makeRichCTypeDecl( |
| 813 |
accessType.getForNonConstAccess(), useParamName=False) |
| 814 |
|
| 815 |
ptrCast = "(uintptr_t)" if needPtrCast else "" |
| 816 |
if variant == "guest": |
| 817 |
streamNamespace = "gfxstream::aemu" |
| 818 |
else: |
| 819 |
streamNamespace = "gfxstream" |
| 820 |
|
| 821 |
if direction == "read": |
| 822 |
self.stmt("memcpy((%s*)&%s, %s, %s)" % |
| 823 |
(accessCast, |
| 824 |
accessExpr, |
| 825 |
streamVar, |
| 826 |
str(streamSize))) |
| 827 |
self.stmt("%s::Stream::%s((uint8_t*)&%s)" % ( |
| 828 |
streamNamespace, |
| 829 |
streamMethod, |
| 830 |
accessExpr)) |
| 831 |
else: |
| 832 |
self.stmt("%s %s = (%s)%s%s" % |
| 833 |
(streamStorageVarType, streamStorageVar, |
| 834 |
streamStorageVarType, ptrCast, accessExpr)) |
| 835 |
self.stmt("memcpy(%s, &%s, %s)" % |
| 836 |
(streamVar, streamStorageVar, str(streamSize))) |
| 837 |
self.stmt("%s::Stream::%s((uint8_t*)%s)" % ( |
| 838 |
streamNamespace, |
| 839 |
streamMethod, |
| 840 |
streamVar)) |
| 841 |
|
| 842 |
def countPrimitive(self, typeInfo, accessType): |
| 843 |
accessTypeName = accessType.typeName |
| 844 |
|
| 845 |
if accessType.pointerIndirectionLevels == 0 and not self.validPrimitive(typeInfo, accessTypeName): |
| 846 |
print("Tried to count a non-primitive type: %s" % accessTypeName) |
| 847 |
os.abort() |
| 848 |
|
| 849 |
needPtrCast = False |
| 850 |
|
| 851 |
if accessType.pointerIndirectionLevels > 0: |
| 852 |
streamSize = 8 |
| 853 |
else: |
| 854 |
streamSize = typeInfo.getPrimitiveEncodingSize(accessTypeName) |
| 855 |
|
| 856 |
return streamSize |
| 857 |
|
| 858 |
# Class to wrap a Vulkan API call. |
| 859 |
# |
| 860 |
# The user gives a generic callback, |codegenDef|, |
| 861 |
# that takes a CodeGen object and a VulkanAPI object as arguments. |
| 862 |
# codegenDef uses CodeGen along with the VulkanAPI object |
| 863 |
# to generate the function body. |
| 864 |
class VulkanAPIWrapper(object): |
| 865 |
|
| 866 |
def __init__(self, |
| 867 |
customApiPrefix, |
| 868 |
extraParameters=None, |
| 869 |
returnTypeOverride=None, |
| 870 |
codegenDef=None): |
| 871 |
self.customApiPrefix = customApiPrefix |
| 872 |
self.extraParameters = extraParameters |
| 873 |
self.returnTypeOverride = returnTypeOverride |
| 874 |
|
| 875 |
self.codegen = CodeGen() |
| 876 |
|
| 877 |
self.definitionFunc = codegenDef |
| 878 |
|
| 879 |
# Private function |
| 880 |
|
| 881 |
def makeApiFunc(self, typeInfo, apiName): |
| 882 |
customApi = copy(typeInfo.apis[apiName]) |
| 883 |
customApi.name = self.customApiPrefix + customApi.name |
| 884 |
if self.extraParameters is not None: |
| 885 |
if isinstance(self.extraParameters, list): |
| 886 |
customApi.parameters = \ |
| 887 |
self.extraParameters + customApi.parameters |
| 888 |
else: |
| 889 |
os.abort( |
| 890 |
"Type of extra parameters to custom API not valid. Expected list, got %s" % type( |
| 891 |
self.extraParameters)) |
| 892 |
|
| 893 |
if self.returnTypeOverride is not None: |
| 894 |
customApi.retType = self.returnTypeOverride |
| 895 |
return customApi |
| 896 |
|
| 897 |
self.makeApi = makeApiFunc |
| 898 |
|
| 899 |
def setCodegenDef(self, codegenDefFunc): |
| 900 |
self.definitionFunc = codegenDefFunc |
| 901 |
|
| 902 |
def makeDecl(self, typeInfo, apiName): |
| 903 |
return self.codegen.makeFuncProto( |
| 904 |
self.makeApi(self, typeInfo, apiName)) + ";\n\n" |
| 905 |
|
| 906 |
def makeDefinition(self, typeInfo, apiName, isStatic=False): |
| 907 |
vulkanApi = self.makeApi(self, typeInfo, apiName) |
| 908 |
|
| 909 |
self.codegen.swapCode() |
| 910 |
self.codegen.beginBlock() |
| 911 |
|
| 912 |
if self.definitionFunc is None: |
| 913 |
print("ERROR: No definition found for (%s, %s)" % |
| 914 |
(vulkanApi.name, self.customApiPrefix)) |
| 915 |
sys.exit(1) |
| 916 |
|
| 917 |
self.definitionFunc(self.codegen, vulkanApi) |
| 918 |
|
| 919 |
self.codegen.endBlock() |
| 920 |
|
| 921 |
return ("static " if isStatic else "") + self.codegen.makeFuncProto( |
| 922 |
vulkanApi) + "\n" + self.codegen.swapCode() + "\n" |
| 923 |
|
| 924 |
# Base class for wrapping all Vulkan API objects. These work with Vulkan |
| 925 |
# Registry generators and have gen* triggers. They tend to contain |
| 926 |
# VulkanAPIWrapper objects to make it easier to generate the code. |
| 927 |
class VulkanWrapperGenerator(object): |
| 928 |
|
| 929 |
def __init__(self, module: Module, typeInfo: VulkanTypeInfo): |
| 930 |
self.module: Module = module |
| 931 |
self.typeInfo: VulkanTypeInfo = typeInfo |
| 932 |
self.extensionStructTypes = OrderedDict() |
| 933 |
|
| 934 |
def onBegin(self): |
| 935 |
pass |
| 936 |
|
| 937 |
def onEnd(self): |
| 938 |
pass |
| 939 |
|
| 940 |
def onBeginFeature(self, featureName, featureType): |
| 941 |
pass |
| 942 |
|
| 943 |
def onFeatureNewCmd(self, cmdName): |
| 944 |
pass |
| 945 |
|
| 946 |
def onEndFeature(self): |
| 947 |
pass |
| 948 |
|
| 949 |
def onGenType(self, typeInfo, name, alias): |
| 950 |
category = self.typeInfo.categoryOf(name) |
| 951 |
if category in ["struct", "union"] and not alias: |
| 952 |
structInfo = self.typeInfo.structs[name] |
| 953 |
if structInfo.structExtendsExpr: |
| 954 |
self.extensionStructTypes[name] = structInfo |
| 955 |
pass |
| 956 |
|
| 957 |
def onGenStruct(self, typeInfo, name, alias): |
| 958 |
pass |
| 959 |
|
| 960 |
def onGenGroup(self, groupinfo, groupName, alias=None): |
| 961 |
pass |
| 962 |
|
| 963 |
def onGenEnum(self, enuminfo, name, alias): |
| 964 |
pass |
| 965 |
|
| 966 |
def onGenCmd(self, cmdinfo, name, alias): |
| 967 |
pass |
| 968 |
|
| 969 |
# Below Vulkan structure types may correspond to multiple Vulkan structs |
| 970 |
# due to a conflict between different Vulkan registries. In order to get |
| 971 |
# the correct Vulkan struct type, we need to check the type of its "root" |
| 972 |
# struct as well. |
| 973 |
ROOT_TYPE_MAPPING = { |
| 974 |
"VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_DENSITY_MAP_FEATURES_EXT": { |
| 975 |
"VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2": "VkPhysicalDeviceFragmentDensityMapFeaturesEXT", |
| 976 |
"VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO": "VkPhysicalDeviceFragmentDensityMapFeaturesEXT", |
| 977 |
"VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO": "VkImportColorBufferGOOGLE", |
| 978 |
"default": "VkPhysicalDeviceFragmentDensityMapFeaturesEXT", |
| 979 |
}, |
| 980 |
"VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_DENSITY_MAP_PROPERTIES_EXT": { |
| 981 |
"VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2": "VkPhysicalDeviceFragmentDensityMapPropertiesEXT", |
| 982 |
"VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO": "VkCreateBlobGOOGLE", |
| 983 |
"default": "VkPhysicalDeviceFragmentDensityMapPropertiesEXT", |
| 984 |
}, |
| 985 |
"VK_STRUCTURE_TYPE_RENDER_PASS_FRAGMENT_DENSITY_MAP_CREATE_INFO_EXT": { |
| 986 |
"VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO": "VkRenderPassFragmentDensityMapCreateInfoEXT", |
| 987 |
"VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2": "VkRenderPassFragmentDensityMapCreateInfoEXT", |
| 988 |
"VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO": "VkImportBufferGOOGLE", |
| 989 |
"default": "VkRenderPassFragmentDensityMapCreateInfoEXT", |
| 990 |
}, |
| 991 |
} |
| 992 |
|
| 993 |
def emitForEachStructExtension(self, cgen, retType, triggerVar, forEachFunc, autoBreak=True, defaultEmit=None, nullEmit=None, rootTypeVar=None): |
| 994 |
def readStructType(structTypeName, structVarName, cgen): |
| 995 |
cgen.stmt("uint32_t %s = (uint32_t)%s(%s)" % \ |
| 996 |
(structTypeName, "goldfish_vk_struct_type", structVarName)) |
| 997 |
|
| 998 |
def castAsStruct(varName, typeName, const=True): |
| 999 |
return "reinterpret_cast<%s%s*>(%s)" % \ |
| 1000 |
("const " if const else "", typeName, varName) |
| 1001 |
|
| 1002 |
def doDefaultReturn(cgen): |
| 1003 |
if retType.typeName == "void": |
| 1004 |
cgen.stmt("return") |
| 1005 |
else: |
| 1006 |
cgen.stmt("return (%s)0" % retType.typeName) |
| 1007 |
|
| 1008 |
cgen.beginIf("!%s" % triggerVar.paramName) |
| 1009 |
if nullEmit is None: |
| 1010 |
doDefaultReturn(cgen) |
| 1011 |
else: |
| 1012 |
nullEmit(cgen) |
| 1013 |
cgen.endIf() |
| 1014 |
|
| 1015 |
readStructType("structType", triggerVar.paramName, cgen) |
| 1016 |
|
| 1017 |
cgen.line("switch(structType)") |
| 1018 |
cgen.beginBlock() |
| 1019 |
|
| 1020 |
currFeature = None |
| 1021 |
|
| 1022 |
for ext in self.extensionStructTypes.values(): |
| 1023 |
if not currFeature: |
| 1024 |
cgen.leftline("#ifdef %s" % ext.feature) |
| 1025 |
currFeature = ext.feature |
| 1026 |
|
| 1027 |
if currFeature and ext.feature != currFeature: |
| 1028 |
cgen.leftline("#endif") |
| 1029 |
cgen.leftline("#ifdef %s" % ext.feature) |
| 1030 |
currFeature = ext.feature |
| 1031 |
|
| 1032 |
enum = ext.structEnumExpr |
| 1033 |
protect = None |
| 1034 |
if enum in self.typeInfo.enumElem: |
| 1035 |
protect = self.typeInfo.enumElem[enum].get("protect", default=None) |
| 1036 |
if protect is not None: |
| 1037 |
cgen.leftline("#ifdef %s" % protect) |
| 1038 |
|
| 1039 |
cgen.line("case %s:" % enum) |
| 1040 |
cgen.beginBlock() |
| 1041 |
|
| 1042 |
if rootTypeVar is not None and enum in VulkanWrapperGenerator.ROOT_TYPE_MAPPING: |
| 1043 |
cgen.line("switch(%s)" % rootTypeVar.paramName) |
| 1044 |
cgen.beginBlock() |
| 1045 |
kv = VulkanWrapperGenerator.ROOT_TYPE_MAPPING[enum] |
| 1046 |
for k in kv: |
| 1047 |
v = self.extensionStructTypes[kv[k]] |
| 1048 |
if k == "default": |
| 1049 |
cgen.line("%s:" % k) |
| 1050 |
else: |
| 1051 |
cgen.line("case %s:" % k) |
| 1052 |
cgen.beginBlock() |
| 1053 |
castedAccess = castAsStruct( |
| 1054 |
triggerVar.paramName, v.name, const=triggerVar.isConst) |
| 1055 |
forEachFunc(v, castedAccess, cgen) |
| 1056 |
cgen.line("break;") |
| 1057 |
cgen.endBlock() |
| 1058 |
cgen.endBlock() |
| 1059 |
else: |
| 1060 |
castedAccess = castAsStruct( |
| 1061 |
triggerVar.paramName, ext.name, const=triggerVar.isConst) |
| 1062 |
forEachFunc(ext, castedAccess, cgen) |
| 1063 |
|
| 1064 |
if autoBreak: |
| 1065 |
cgen.stmt("break") |
| 1066 |
cgen.endBlock() |
| 1067 |
|
| 1068 |
if protect is not None: |
| 1069 |
cgen.leftline("#endif // %s" % protect) |
| 1070 |
|
| 1071 |
if currFeature: |
| 1072 |
cgen.leftline("#endif") |
| 1073 |
|
| 1074 |
cgen.line("default:") |
| 1075 |
cgen.beginBlock() |
| 1076 |
if defaultEmit is None: |
| 1077 |
doDefaultReturn(cgen) |
| 1078 |
else: |
| 1079 |
defaultEmit(cgen) |
| 1080 |
cgen.endBlock() |
| 1081 |
|
| 1082 |
cgen.endBlock() |
| 1083 |
|
| 1084 |
def emitForEachStructExtensionGeneral(self, cgen, forEachFunc, doFeatureIfdefs=False): |
| 1085 |
currFeature = None |
| 1086 |
|
| 1087 |
for (i, ext) in enumerate(self.extensionStructTypes.values()): |
| 1088 |
if doFeatureIfdefs: |
| 1089 |
if not currFeature: |
| 1090 |
cgen.leftline("#ifdef %s" % ext.feature) |
| 1091 |
currFeature = ext.feature |
| 1092 |
|
| 1093 |
if currFeature and ext.feature != currFeature: |
| 1094 |
cgen.leftline("#endif") |
| 1095 |
cgen.leftline("#ifdef %s" % ext.feature) |
| 1096 |
currFeature = ext.feature |
| 1097 |
|
| 1098 |
forEachFunc(i, ext, cgen) |
| 1099 |
|
| 1100 |
if doFeatureIfdefs: |
| 1101 |
if currFeature: |
| 1102 |
cgen.leftline("#endif") |