| 1 |
# Copyright 2018 Google LLC |
| 2 |
# SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
from copy import copy |
| 5 |
import hashlib, sys |
| 6 |
|
| 7 |
from .common.codegen import CodeGen, VulkanAPIWrapper |
| 8 |
from .common.vulkantypes import \ |
| 9 |
VulkanAPI, makeVulkanTypeSimple, iterateVulkanType, VulkanTypeIterator, Atom, FuncExpr, FuncExprVal, FuncLambda |
| 10 |
|
| 11 |
from .wrapperdefs import VulkanWrapperGenerator |
| 12 |
from .wrapperdefs import VULKAN_STREAM_VAR_NAME |
| 13 |
from .wrapperdefs import ROOT_TYPE_VAR_NAME, ROOT_TYPE_PARAM |
| 14 |
from .wrapperdefs import STREAM_RET_TYPE |
| 15 |
from .wrapperdefs import MARSHAL_INPUT_VAR_NAME |
| 16 |
from .wrapperdefs import UNMARSHAL_INPUT_VAR_NAME |
| 17 |
from .wrapperdefs import PARAMETERS_MARSHALING |
| 18 |
from .wrapperdefs import PARAMETERS_MARSHALING_GUEST |
| 19 |
from .wrapperdefs import STYPE_OVERRIDE |
| 20 |
from .wrapperdefs import STRUCT_EXTENSION_PARAM, STRUCT_EXTENSION_PARAM_FOR_WRITE, EXTENSION_SIZE_WITH_STREAM_FEATURES_API_NAME |
| 21 |
from .wrapperdefs import API_PREFIX_MARSHAL |
| 22 |
from .wrapperdefs import API_PREFIX_UNMARSHAL |
| 23 |
|
| 24 |
from .marshalingdefs import KNOWN_FUNCTION_OPCODES, CUSTOM_MARSHAL_TYPES |
| 25 |
|
| 26 |
class VulkanMarshalingCodegen(VulkanTypeIterator): |
| 27 |
|
| 28 |
def __init__(self, |
| 29 |
cgen, |
| 30 |
streamVarName, |
| 31 |
rootTypeVarName, |
| 32 |
inputVarName, |
| 33 |
marshalPrefix, |
| 34 |
direction = "write", |
| 35 |
forApiOutput = False, |
| 36 |
dynAlloc = False, |
| 37 |
mapHandles = True, |
| 38 |
handleMapOverwrites = False, |
| 39 |
doFiltering = True): |
| 40 |
self.cgen = cgen |
| 41 |
self.direction = direction |
| 42 |
self.processSimple = "write" if self.direction == "write" else "read" |
| 43 |
self.forApiOutput = forApiOutput |
| 44 |
|
| 45 |
self.checked = False |
| 46 |
|
| 47 |
self.streamVarName = streamVarName |
| 48 |
self.rootTypeVarName = rootTypeVarName |
| 49 |
self.inputVarName = inputVarName |
| 50 |
self.marshalPrefix = marshalPrefix |
| 51 |
|
| 52 |
self.exprAccessor = lambda t: self.cgen.generalAccess(t, parentVarName = self.inputVarName, asPtr = True) |
| 53 |
self.exprValueAccessor = lambda t: self.cgen.generalAccess(t, parentVarName = self.inputVarName, asPtr = False) |
| 54 |
self.exprPrimitiveValueAccessor = lambda t: self.cgen.generalAccess(t, parentVarName = self.inputVarName, asPtr = False) |
| 55 |
self.lenAccessor = lambda t: self.cgen.generalLengthAccess(t, parentVarName = self.inputVarName) |
| 56 |
self.lenAccessorGuard = lambda t: self.cgen.generalLengthAccessGuard( |
| 57 |
t, parentVarName=self.inputVarName) |
| 58 |
self.filterVarAccessor = lambda t: self.cgen.filterVarAccess(t, parentVarName = self.inputVarName) |
| 59 |
|
| 60 |
self.dynAlloc = dynAlloc |
| 61 |
self.mapHandles = mapHandles |
| 62 |
self.handleMapOverwrites = handleMapOverwrites |
| 63 |
self.doFiltering = doFiltering |
| 64 |
|
| 65 |
def getTypeForStreaming(self, vulkanType): |
| 66 |
res = copy(vulkanType) |
| 67 |
|
| 68 |
if not vulkanType.accessibleAsPointer(): |
| 69 |
res = res.getForAddressAccess() |
| 70 |
|
| 71 |
if vulkanType.staticArrExpr: |
| 72 |
res = res.getForAddressAccess() |
| 73 |
|
| 74 |
if self.direction == "write": |
| 75 |
return res |
| 76 |
else: |
| 77 |
return res.getForNonConstAccess() |
| 78 |
|
| 79 |
def makeCastExpr(self, vulkanType): |
| 80 |
return "(%s)" % ( |
| 81 |
self.cgen.makeCTypeDecl(vulkanType, useParamName=False)) |
| 82 |
|
| 83 |
def genStreamCall(self, vulkanType, toStreamExpr, sizeExpr): |
| 84 |
varname = self.streamVarName |
| 85 |
func = self.processSimple |
| 86 |
cast = self.makeCastExpr(self.getTypeForStreaming(vulkanType)) |
| 87 |
|
| 88 |
self.cgen.stmt( |
| 89 |
"%s->%s(%s%s, %s)" % (varname, func, cast, toStreamExpr, sizeExpr)) |
| 90 |
|
| 91 |
def genPrimitiveStreamCall(self, vulkanType, access): |
| 92 |
varname = self.streamVarName |
| 93 |
|
| 94 |
self.cgen.streamPrimitive( |
| 95 |
self.typeInfo, |
| 96 |
varname, |
| 97 |
access, |
| 98 |
vulkanType, |
| 99 |
direction=self.direction) |
| 100 |
|
| 101 |
def genHandleMappingCall(self, vulkanType, access, lenAccess): |
| 102 |
|
| 103 |
if lenAccess is None: |
| 104 |
lenAccess = "1" |
| 105 |
handle64Bytes = "8" |
| 106 |
else: |
| 107 |
handle64Bytes = "%s * 8" % lenAccess |
| 108 |
|
| 109 |
handle64Var = self.cgen.var() |
| 110 |
if lenAccess != "1": |
| 111 |
self.cgen.beginIf(lenAccess) |
| 112 |
self.cgen.stmt("uint64_t* %s" % handle64Var) |
| 113 |
self.cgen.stmt( |
| 114 |
"%s->alloc((void**)&%s, %s * 8)" % \ |
| 115 |
(self.streamVarName, handle64Var, lenAccess)) |
| 116 |
handle64VarAccess = handle64Var |
| 117 |
handle64VarType = \ |
| 118 |
makeVulkanTypeSimple(False, "uint64_t", 1, paramName=handle64Var) |
| 119 |
else: |
| 120 |
self.cgen.stmt("uint64_t %s" % handle64Var) |
| 121 |
handle64VarAccess = "&%s" % handle64Var |
| 122 |
handle64VarType = \ |
| 123 |
makeVulkanTypeSimple(False, "uint64_t", 0, paramName=handle64Var) |
| 124 |
|
| 125 |
if self.direction == "write": |
| 126 |
if self.handleMapOverwrites: |
| 127 |
self.cgen.stmt( |
| 128 |
"static_assert(8 == sizeof(%s), \"handle map overwrite requires %s to be 8 bytes long\")" % \ |
| 129 |
(vulkanType.typeName, vulkanType.typeName)) |
| 130 |
self.cgen.stmt( |
| 131 |
"%s->handleMapping()->mapHandles_%s((%s*)%s, %s)" % |
| 132 |
(self.streamVarName, vulkanType.typeName, vulkanType.typeName, |
| 133 |
access, lenAccess)) |
| 134 |
self.genStreamCall(vulkanType, access, "8 * %s" % lenAccess) |
| 135 |
else: |
| 136 |
self.cgen.stmt( |
| 137 |
"%s->handleMapping()->mapHandles_%s_u64(%s, %s, %s)" % |
| 138 |
(self.streamVarName, vulkanType.typeName, |
| 139 |
access, |
| 140 |
handle64VarAccess, lenAccess)) |
| 141 |
self.genStreamCall(handle64VarType, handle64VarAccess, handle64Bytes) |
| 142 |
else: |
| 143 |
self.genStreamCall(handle64VarType, handle64VarAccess, handle64Bytes) |
| 144 |
self.cgen.stmt( |
| 145 |
"%s->handleMapping()->mapHandles_u64_%s(%s, %s%s, %s)" % |
| 146 |
(self.streamVarName, vulkanType.typeName, |
| 147 |
handle64VarAccess, |
| 148 |
self.makeCastExpr(vulkanType.getForNonConstAccess()), access, |
| 149 |
lenAccess)) |
| 150 |
|
| 151 |
if lenAccess != "1": |
| 152 |
self.cgen.endIf() |
| 153 |
|
| 154 |
def doAllocSpace(self, vulkanType): |
| 155 |
if self.dynAlloc and self.direction == "read": |
| 156 |
access = self.exprAccessor(vulkanType) |
| 157 |
lenAccess = self.lenAccessor(vulkanType) |
| 158 |
sizeof = self.cgen.sizeofExpr( \ |
| 159 |
vulkanType.getForValueAccess()) |
| 160 |
if lenAccess: |
| 161 |
bytesExpr = "%s * %s" % (lenAccess, sizeof) |
| 162 |
else: |
| 163 |
bytesExpr = sizeof |
| 164 |
|
| 165 |
self.cgen.stmt( \ |
| 166 |
"%s->alloc((void**)&%s, %s)" % |
| 167 |
(self.streamVarName, |
| 168 |
access, bytesExpr)) |
| 169 |
|
| 170 |
def getOptionalStringFeatureExpr(self, vulkanType): |
| 171 |
streamFeature = vulkanType.getProtectStreamFeature() |
| 172 |
if streamFeature is None: |
| 173 |
return None |
| 174 |
return "%s->getFeatureBits() & %s" % (self.streamVarName, streamFeature) |
| 175 |
|
| 176 |
def onCheck(self, vulkanType): |
| 177 |
|
| 178 |
if self.forApiOutput: |
| 179 |
return |
| 180 |
|
| 181 |
featureExpr = self.getOptionalStringFeatureExpr(vulkanType); |
| 182 |
|
| 183 |
self.checked = True |
| 184 |
|
| 185 |
access = self.exprAccessor(vulkanType) |
| 186 |
|
| 187 |
needConsistencyCheck = False |
| 188 |
|
| 189 |
self.cgen.line("// WARNING PTR CHECK") |
| 190 |
if (self.dynAlloc and self.direction == "read") or self.direction == "write": |
| 191 |
checkAccess = self.exprAccessor(vulkanType) |
| 192 |
addrExpr = "&" + checkAccess |
| 193 |
sizeExpr = self.cgen.sizeofExpr(vulkanType) |
| 194 |
else: |
| 195 |
checkName = "check_%s" % vulkanType.paramName |
| 196 |
self.cgen.stmt("%s %s" % ( |
| 197 |
self.cgen.makeCTypeDecl(vulkanType, useParamName = False), checkName)) |
| 198 |
self.cgen.stmt("(void)%s" % checkName) |
| 199 |
checkAccess = checkName |
| 200 |
addrExpr = "&" + checkAccess |
| 201 |
sizeExpr = self.cgen.sizeofExpr(vulkanType) |
| 202 |
needConsistencyCheck = True |
| 203 |
|
| 204 |
if featureExpr is not None: |
| 205 |
self.cgen.beginIf(featureExpr) |
| 206 |
|
| 207 |
self.genPrimitiveStreamCall( |
| 208 |
vulkanType, |
| 209 |
checkAccess) |
| 210 |
|
| 211 |
if featureExpr is not None: |
| 212 |
self.cgen.endIf() |
| 213 |
|
| 214 |
if featureExpr is not None: |
| 215 |
self.cgen.beginIf("(!(%s) || %s)" % (featureExpr, access)) |
| 216 |
else: |
| 217 |
self.cgen.beginIf(access) |
| 218 |
|
| 219 |
if needConsistencyCheck and featureExpr is None: |
| 220 |
self.cgen.beginIf("!(%s)" % checkName) |
| 221 |
self.cgen.stmt( |
| 222 |
"fprintf(stderr, \"fatal: %s inconsistent between guest and host\\n\")" % (access)) |
| 223 |
self.cgen.endIf() |
| 224 |
|
| 225 |
|
| 226 |
def onCheckWithNullOptionalStringFeature(self, vulkanType): |
| 227 |
self.cgen.beginIf("%s->getFeatureBits() & VULKAN_STREAM_FEATURE_NULL_OPTIONAL_STRINGS_BIT" % self.streamVarName) |
| 228 |
self.onCheck(vulkanType) |
| 229 |
|
| 230 |
def endCheckWithNullOptionalStringFeature(self, vulkanType): |
| 231 |
self.endCheck(vulkanType) |
| 232 |
self.cgen.endIf() |
| 233 |
self.cgen.beginElse() |
| 234 |
|
| 235 |
def finalCheckWithNullOptionalStringFeature(self, vulkanType): |
| 236 |
self.cgen.endElse() |
| 237 |
|
| 238 |
def endCheck(self, vulkanType): |
| 239 |
|
| 240 |
if self.checked: |
| 241 |
self.cgen.endIf() |
| 242 |
self.checked = False |
| 243 |
|
| 244 |
def genFilterFunc(self, filterfunc, env): |
| 245 |
|
| 246 |
def loop(expr, lambdaEnv={}): |
| 247 |
def do_func(expr): |
| 248 |
fnamestr = expr.name.name |
| 249 |
if "not" == fnamestr: |
| 250 |
return "!(%s)" % (loop(expr.args[0], lambdaEnv)) |
| 251 |
if "eq" == fnamestr: |
| 252 |
return "(%s == %s)" % (loop(expr.args[0], lambdaEnv), loop(expr.args[1], lambdaEnv)) |
| 253 |
if "and" == fnamestr: |
| 254 |
return "(%s && %s)" % (loop(expr.args[0], lambdaEnv), loop(expr.args[1], lambdaEnv)) |
| 255 |
if "or" == fnamestr: |
| 256 |
return "(%s || %s)" % (loop(expr.args[0], lambdaEnv), loop(expr.args[1], lambdaEnv)) |
| 257 |
if "bitwise_and" == fnamestr: |
| 258 |
return "(%s & %s)" % (loop(expr.args[0], lambdaEnv), loop(expr.args[1], lambdaEnv)) |
| 259 |
if "getfield" == fnamestr: |
| 260 |
ptrlevels = get_ptrlevels(expr.args[0].val.name) |
| 261 |
if ptrlevels == 0: |
| 262 |
return "%s.%s" % (loop(expr.args[0], lambdaEnv), expr.args[1].val) |
| 263 |
else: |
| 264 |
return "(%s(%s)).%s" % ("*" * ptrlevels, loop(expr.args[0], lambdaEnv), expr.args[1].val) |
| 265 |
|
| 266 |
if "if" == fnamestr: |
| 267 |
return "((%s) ? (%s) : (%s))" % (loop(expr.args[0], lambdaEnv), loop(expr.args[1], lambdaEnv), loop(expr.args[2], lambdaEnv)) |
| 268 |
|
| 269 |
return "%s(%s)" % (fnamestr, ", ".join(map(lambda e: loop(e, lambdaEnv), expr.args))) |
| 270 |
|
| 271 |
def do_expratom(atomname, lambdaEnv= {}): |
| 272 |
if lambdaEnv.get(atomname, None) is not None: |
| 273 |
return atomname |
| 274 |
|
| 275 |
enventry = env.get(atomname, None) |
| 276 |
if None != enventry: |
| 277 |
return self.getEnvAccessExpr(atomname) |
| 278 |
return atomname |
| 279 |
|
| 280 |
def get_ptrlevels(atomname, lambdaEnv= {}): |
| 281 |
if lambdaEnv.get(atomname, None) is not None: |
| 282 |
return 0 |
| 283 |
|
| 284 |
enventry = env.get(atomname, None) |
| 285 |
if None != enventry: |
| 286 |
return self.getPointerIndirectionLevels(atomname) |
| 287 |
|
| 288 |
return 0 |
| 289 |
|
| 290 |
def do_exprval(expr, lambdaEnv= {}): |
| 291 |
expratom = expr.val |
| 292 |
|
| 293 |
if Atom == type(expratom): |
| 294 |
return do_expratom(expratom.name, lambdaEnv) |
| 295 |
|
| 296 |
return "%s" % expratom |
| 297 |
|
| 298 |
def do_lambda(expr, lambdaEnv= {}): |
| 299 |
params = expr.vs |
| 300 |
body = expr.body |
| 301 |
newEnv = {} |
| 302 |
|
| 303 |
for (k, v) in lambdaEnv.items(): |
| 304 |
newEnv[k] = v |
| 305 |
|
| 306 |
for p in params: |
| 307 |
newEnv[p.name] = p.typ |
| 308 |
|
| 309 |
return "[](%s) { return %s; }" % (", ".join(list(map(lambda p: "%s %s" % (p.typ, p.name), params))), loop(body, lambdaEnv=newEnv)) |
| 310 |
|
| 311 |
if FuncExpr == type(expr): |
| 312 |
return do_func(expr) |
| 313 |
if FuncLambda == type(expr): |
| 314 |
return do_lambda(expr) |
| 315 |
elif FuncExprVal == type(expr): |
| 316 |
return do_exprval(expr) |
| 317 |
|
| 318 |
return loop(filterfunc) |
| 319 |
|
| 320 |
def beginFilterGuard(self, vulkanType): |
| 321 |
if vulkanType.filterVar == None: |
| 322 |
return |
| 323 |
|
| 324 |
if self.doFiltering == False: |
| 325 |
return |
| 326 |
|
| 327 |
filterVarAccess = self.getEnvAccessExpr(vulkanType.filterVar) |
| 328 |
|
| 329 |
filterValsExpr = None |
| 330 |
filterFuncExpr = None |
| 331 |
filterExpr = None |
| 332 |
|
| 333 |
filterFeature = "%s->getFeatureBits() & VULKAN_STREAM_FEATURE_IGNORED_HANDLES_BIT" % self.streamVarName |
| 334 |
|
| 335 |
if None != vulkanType.filterVals: |
| 336 |
filterValsExpr = " || ".join(map(lambda filterval: "(%s == %s)" % (filterval, filterVarAccess), vulkanType.filterVals)) |
| 337 |
|
| 338 |
if None != vulkanType.filterFunc: |
| 339 |
filterFuncExpr = self.genFilterFunc(vulkanType.filterFunc, self.currentStructInfo.environment) |
| 340 |
|
| 341 |
if None != filterValsExpr and None != filterFuncExpr: |
| 342 |
filterExpr = "%s || %s" % (filterValsExpr, filterFuncExpr) |
| 343 |
elif None == filterValsExpr and None == filterFuncExpr: |
| 344 |
# Assume is bool |
| 345 |
self.cgen.beginIf(filterVarAccess) |
| 346 |
elif None != filterValsExpr: |
| 347 |
self.cgen.beginIf("(!(%s) || (%s))" % (filterFeature, filterValsExpr)) |
| 348 |
elif None != filterFuncExpr: |
| 349 |
self.cgen.beginIf("(!(%s) || (%s))" % (filterFeature, filterFuncExpr)) |
| 350 |
|
| 351 |
def endFilterGuard(self, vulkanType, cleanupExpr=None): |
| 352 |
if vulkanType.filterVar == None: |
| 353 |
return |
| 354 |
|
| 355 |
if self.doFiltering == False: |
| 356 |
return |
| 357 |
|
| 358 |
if cleanupExpr == None: |
| 359 |
self.cgen.endIf() |
| 360 |
else: |
| 361 |
self.cgen.endIf() |
| 362 |
self.cgen.beginElse() |
| 363 |
self.cgen.stmt(cleanupExpr) |
| 364 |
self.cgen.endElse() |
| 365 |
|
| 366 |
def getEnvAccessExpr(self, varName): |
| 367 |
parentEnvEntry = self.currentStructInfo.environment.get(varName, None) |
| 368 |
|
| 369 |
if parentEnvEntry != None: |
| 370 |
isParentMember = parentEnvEntry["structmember"] |
| 371 |
|
| 372 |
if isParentMember: |
| 373 |
envAccess = self.exprValueAccessor(list(filter(lambda member: member.paramName == varName, self.currentStructInfo.members))[0]) |
| 374 |
else: |
| 375 |
envAccess = varName |
| 376 |
return envAccess |
| 377 |
|
| 378 |
return None |
| 379 |
|
| 380 |
def getPointerIndirectionLevels(self, varName): |
| 381 |
parentEnvEntry = self.currentStructInfo.environment.get(varName, None) |
| 382 |
|
| 383 |
if parentEnvEntry != None: |
| 384 |
isParentMember = parentEnvEntry["structmember"] |
| 385 |
|
| 386 |
if isParentMember: |
| 387 |
return list(filter(lambda member: member.paramName == varName, self.currentStructInfo.members))[0].pointerIndirectionLevels |
| 388 |
else: |
| 389 |
return 0 |
| 390 |
return 0 |
| 391 |
|
| 392 |
return 0 |
| 393 |
|
| 394 |
|
| 395 |
def onCompoundType(self, vulkanType): |
| 396 |
|
| 397 |
access = self.exprAccessor(vulkanType) |
| 398 |
lenAccess = self.lenAccessor(vulkanType) |
| 399 |
lenAccessGuard = self.lenAccessorGuard(vulkanType) |
| 400 |
|
| 401 |
self.beginFilterGuard(vulkanType) |
| 402 |
|
| 403 |
if vulkanType.pointerIndirectionLevels > 0: |
| 404 |
self.doAllocSpace(vulkanType) |
| 405 |
|
| 406 |
if lenAccess is not None: |
| 407 |
if lenAccessGuard is not None: |
| 408 |
self.cgen.beginIf(lenAccessGuard) |
| 409 |
loopVar = "i" |
| 410 |
access = "%s + %s" % (access, loopVar) |
| 411 |
forInit = "uint32_t %s = 0" % loopVar |
| 412 |
forCond = "%s < (uint32_t)%s" % (loopVar, lenAccess) |
| 413 |
forIncr = "++%s" % loopVar |
| 414 |
self.cgen.beginFor(forInit, forCond, forIncr) |
| 415 |
|
| 416 |
accessWithCast = "%s(%s)" % (self.makeCastExpr( |
| 417 |
self.getTypeForStreaming(vulkanType)), access) |
| 418 |
|
| 419 |
callParams = [self.streamVarName, self.rootTypeVarName, accessWithCast] |
| 420 |
|
| 421 |
for (bindName, localName) in vulkanType.binds.items(): |
| 422 |
callParams.append(self.getEnvAccessExpr(localName)) |
| 423 |
|
| 424 |
self.cgen.funcCall(None, self.marshalPrefix + vulkanType.typeName, |
| 425 |
callParams) |
| 426 |
|
| 427 |
if lenAccess is not None: |
| 428 |
self.cgen.endFor() |
| 429 |
if lenAccessGuard is not None: |
| 430 |
self.cgen.endIf() |
| 431 |
|
| 432 |
if self.direction == "read": |
| 433 |
self.endFilterGuard(vulkanType, "%s = 0" % self.exprAccessor(vulkanType)) |
| 434 |
else: |
| 435 |
self.endFilterGuard(vulkanType) |
| 436 |
|
| 437 |
def onString(self, vulkanType): |
| 438 |
|
| 439 |
access = self.exprAccessor(vulkanType) |
| 440 |
|
| 441 |
if self.direction == "write": |
| 442 |
self.cgen.stmt("%s->putString(%s)" % (self.streamVarName, access)) |
| 443 |
else: |
| 444 |
castExpr = \ |
| 445 |
self.makeCastExpr( \ |
| 446 |
self.getTypeForStreaming( \ |
| 447 |
vulkanType.getForAddressAccess())) |
| 448 |
|
| 449 |
self.cgen.stmt( \ |
| 450 |
"%s->loadStringInPlace(%s&%s)" % (self.streamVarName, castExpr, access)) |
| 451 |
|
| 452 |
def onStringArray(self, vulkanType): |
| 453 |
|
| 454 |
access = self.exprAccessor(vulkanType) |
| 455 |
lenAccess = self.lenAccessor(vulkanType) |
| 456 |
|
| 457 |
if self.direction == "write": |
| 458 |
if lenAccess is not None: |
| 459 |
self.cgen.stmt("saveStringArray(%s, %s, %s)" % (self.streamVarName, |
| 460 |
access, lenAccess)) |
| 461 |
else: |
| 462 |
castExpr = \ |
| 463 |
self.makeCastExpr( \ |
| 464 |
self.getTypeForStreaming( \ |
| 465 |
vulkanType.getForAddressAccess())) |
| 466 |
|
| 467 |
self.cgen.stmt("%s->loadStringArrayInPlace(%s&%s)" % (self.streamVarName, castExpr, access)) |
| 468 |
|
| 469 |
def onStaticArr(self, vulkanType): |
| 470 |
access = self.exprValueAccessor(vulkanType) |
| 471 |
lenAccess = self.lenAccessor(vulkanType) |
| 472 |
if lenAccess is not None: |
| 473 |
finalLenExpr = "%s * %s" % (lenAccess, self.cgen.sizeofExpr(vulkanType)) |
| 474 |
else: |
| 475 |
finalLenExpr = self.cgen.sizeofExpr(vulkanType) |
| 476 |
self.genStreamCall(vulkanType, access, finalLenExpr) |
| 477 |
|
| 478 |
# Old version VkEncoder may have some sType values conflict with VkDecoder |
| 479 |
# of new versions. For host decoder, it should not carry the incorrect old |
| 480 |
# sType values to the |forUnmarshaling| struct. Instead it should overwrite |
| 481 |
# the sType value. |
| 482 |
def overwriteSType(self, vulkanType): |
| 483 |
if self.direction == "read": |
| 484 |
sTypeParam = copy(vulkanType) |
| 485 |
sTypeParam.paramName = "sType" |
| 486 |
sTypeAccess = self.exprAccessor(sTypeParam) |
| 487 |
|
| 488 |
typeName = vulkanType.parent.typeName |
| 489 |
if typeName in STYPE_OVERRIDE: |
| 490 |
self.cgen.stmt("%s = %s" % |
| 491 |
(sTypeAccess, STYPE_OVERRIDE[typeName])) |
| 492 |
|
| 493 |
def onStructExtension(self, vulkanType): |
| 494 |
self.overwriteSType(vulkanType) |
| 495 |
|
| 496 |
sTypeParam = copy(vulkanType) |
| 497 |
sTypeParam.paramName = "sType" |
| 498 |
|
| 499 |
access = self.exprAccessor(vulkanType) |
| 500 |
sizeVar = "%s_size" % vulkanType.paramName |
| 501 |
|
| 502 |
if self.direction == "read": |
| 503 |
castedAccessExpr = "(%s)(%s)" % ("void*", access) |
| 504 |
else: |
| 505 |
castedAccessExpr = access |
| 506 |
|
| 507 |
sTypeAccess = self.exprAccessor(sTypeParam) |
| 508 |
self.cgen.beginIf("%s == VK_STRUCTURE_TYPE_MAX_ENUM" % |
| 509 |
self.rootTypeVarName) |
| 510 |
self.cgen.stmt("%s = %s" % (self.rootTypeVarName, sTypeAccess)) |
| 511 |
self.cgen.endIf() |
| 512 |
|
| 513 |
if self.direction == "read" and self.dynAlloc: |
| 514 |
self.cgen.stmt("size_t %s" % sizeVar) |
| 515 |
self.cgen.stmt("%s = %s->getBe32()" % \ |
| 516 |
(sizeVar, self.streamVarName)) |
| 517 |
self.cgen.stmt("%s = nullptr" % access) |
| 518 |
self.cgen.beginIf(sizeVar) |
| 519 |
self.cgen.stmt( \ |
| 520 |
"%s->alloc((void**)&%s, sizeof(VkStructureType))" % |
| 521 |
(self.streamVarName, access)) |
| 522 |
|
| 523 |
self.genStreamCall(vulkanType, access, "sizeof(VkStructureType)") |
| 524 |
self.cgen.stmt("VkStructureType extType = *(VkStructureType*)(%s)" % access) |
| 525 |
self.cgen.stmt( \ |
| 526 |
"%s->alloc((void**)&%s, %s(%s->getFeatureBits(), %s, %s))" % |
| 527 |
(self.streamVarName, access, EXTENSION_SIZE_WITH_STREAM_FEATURES_API_NAME, self.streamVarName, self.rootTypeVarName, access)) |
| 528 |
self.cgen.stmt("*(VkStructureType*)%s = extType" % access) |
| 529 |
|
| 530 |
self.cgen.funcCall(None, self.marshalPrefix + "extension_struct", |
| 531 |
[self.streamVarName, self.rootTypeVarName, castedAccessExpr]) |
| 532 |
self.cgen.endIf() |
| 533 |
else: |
| 534 |
|
| 535 |
self.cgen.funcCall(None, self.marshalPrefix + "extension_struct", |
| 536 |
[self.streamVarName, self.rootTypeVarName, castedAccessExpr]) |
| 537 |
|
| 538 |
|
| 539 |
def onPointer(self, vulkanType): |
| 540 |
access = self.exprAccessor(vulkanType) |
| 541 |
|
| 542 |
lenAccess = self.lenAccessor(vulkanType) |
| 543 |
lenAccessGuard = self.lenAccessorGuard(vulkanType) |
| 544 |
|
| 545 |
self.beginFilterGuard(vulkanType) |
| 546 |
self.doAllocSpace(vulkanType) |
| 547 |
|
| 548 |
if vulkanType.isHandleType() and self.mapHandles: |
| 549 |
self.genHandleMappingCall(vulkanType, access, lenAccess) |
| 550 |
else: |
| 551 |
if self.typeInfo.isNonAbiPortableType(vulkanType.typeName): |
| 552 |
if lenAccess is not None: |
| 553 |
if lenAccessGuard is not None: |
| 554 |
self.cgen.beginIf(lenAccessGuard) |
| 555 |
self.cgen.beginFor("uint32_t i = 0", "i < (uint32_t)%s" % lenAccess, "++i") |
| 556 |
self.genPrimitiveStreamCall(vulkanType.getForValueAccess(), "%s[i]" % access) |
| 557 |
self.cgen.endFor() |
| 558 |
if lenAccessGuard is not None: |
| 559 |
self.cgen.endIf() |
| 560 |
else: |
| 561 |
self.genPrimitiveStreamCall(vulkanType.getForValueAccess(), "(*%s)" % access) |
| 562 |
else: |
| 563 |
if lenAccess is not None: |
| 564 |
finalLenExpr = "%s * %s" % ( |
| 565 |
lenAccess, self.cgen.sizeofExpr(vulkanType.getForValueAccess())) |
| 566 |
else: |
| 567 |
finalLenExpr = "%s" % ( |
| 568 |
self.cgen.sizeofExpr(vulkanType.getForValueAccess())) |
| 569 |
self.genStreamCall(vulkanType, access, finalLenExpr) |
| 570 |
|
| 571 |
if self.direction == "read": |
| 572 |
self.endFilterGuard(vulkanType, "%s = 0" % access) |
| 573 |
else: |
| 574 |
self.endFilterGuard(vulkanType) |
| 575 |
|
| 576 |
def onValue(self, vulkanType): |
| 577 |
self.beginFilterGuard(vulkanType) |
| 578 |
|
| 579 |
if vulkanType.isHandleType() and self.mapHandles: |
| 580 |
access = self.exprAccessor(vulkanType) |
| 581 |
self.genHandleMappingCall( |
| 582 |
vulkanType.getForAddressAccess(), access, "1") |
| 583 |
elif self.typeInfo.isNonAbiPortableType(vulkanType.typeName): |
| 584 |
access = self.exprPrimitiveValueAccessor(vulkanType) |
| 585 |
self.genPrimitiveStreamCall(vulkanType, access) |
| 586 |
else: |
| 587 |
access = self.exprAccessor(vulkanType) |
| 588 |
self.genStreamCall(vulkanType, access, self.cgen.sizeofExpr(vulkanType)) |
| 589 |
|
| 590 |
self.endFilterGuard(vulkanType) |
| 591 |
|
| 592 |
def streamLetParameter(self, structInfo, letParamInfo): |
| 593 |
filterFeature = "%s->getFeatureBits() & VULKAN_STREAM_FEATURE_IGNORED_HANDLES_BIT" % self.streamVarName |
| 594 |
self.cgen.stmt("%s %s = 1" % (letParamInfo.typeName, letParamInfo.paramName)) |
| 595 |
|
| 596 |
self.cgen.beginIf(filterFeature) |
| 597 |
|
| 598 |
if self.direction == "write": |
| 599 |
bodyExpr = self.currentStructInfo.environment[letParamInfo.paramName]["body"] |
| 600 |
self.cgen.stmt("%s = %s" % (letParamInfo.paramName, self.genFilterFunc(bodyExpr, self.currentStructInfo.environment))) |
| 601 |
|
| 602 |
self.genPrimitiveStreamCall(letParamInfo, letParamInfo.paramName) |
| 603 |
|
| 604 |
self.cgen.endIf() |
| 605 |
|
| 606 |
|
| 607 |
class VulkanMarshaling(VulkanWrapperGenerator): |
| 608 |
|
| 609 |
def __init__(self, module, typeInfo, variant="host"): |
| 610 |
VulkanWrapperGenerator.__init__(self, module, typeInfo) |
| 611 |
|
| 612 |
self.cgenHeader = CodeGen() |
| 613 |
self.cgenImpl = CodeGen() |
| 614 |
|
| 615 |
self.variant = variant |
| 616 |
|
| 617 |
self.currentFeature = None |
| 618 |
self.apiOpcodes = {} |
| 619 |
self.dynAlloc = self.variant != "guest" |
| 620 |
|
| 621 |
if self.variant == "guest": |
| 622 |
self.marshalingParams = PARAMETERS_MARSHALING_GUEST |
| 623 |
else: |
| 624 |
self.marshalingParams = PARAMETERS_MARSHALING |
| 625 |
|
| 626 |
self.writeCodegen = \ |
| 627 |
VulkanMarshalingCodegen( |
| 628 |
None, |
| 629 |
VULKAN_STREAM_VAR_NAME, |
| 630 |
ROOT_TYPE_VAR_NAME, |
| 631 |
MARSHAL_INPUT_VAR_NAME, |
| 632 |
API_PREFIX_MARSHAL, |
| 633 |
direction = "write") |
| 634 |
|
| 635 |
self.readCodegen = \ |
| 636 |
VulkanMarshalingCodegen( |
| 637 |
None, |
| 638 |
VULKAN_STREAM_VAR_NAME, |
| 639 |
ROOT_TYPE_VAR_NAME, |
| 640 |
UNMARSHAL_INPUT_VAR_NAME, |
| 641 |
API_PREFIX_UNMARSHAL, |
| 642 |
direction = "read", |
| 643 |
dynAlloc=self.dynAlloc) |
| 644 |
|
| 645 |
self.knownDefs = {} |
| 646 |
|
| 647 |
# Begin Vulkan API opcodes from something high |
| 648 |
# that is not going to interfere with renderControl |
| 649 |
# opcodes |
| 650 |
self.beginOpcodeOld = 20000 |
| 651 |
self.endOpcodeOld = 30000 |
| 652 |
|
| 653 |
self.beginOpcode = 200000000 |
| 654 |
self.endOpcode = 300000000 |
| 655 |
self.knownOpcodes = set() |
| 656 |
|
| 657 |
self.extensionMarshalPrototype = \ |
| 658 |
VulkanAPI(API_PREFIX_MARSHAL + "extension_struct", |
| 659 |
STREAM_RET_TYPE, |
| 660 |
self.marshalingParams + |
| 661 |
[STRUCT_EXTENSION_PARAM]) |
| 662 |
|
| 663 |
self.extensionUnmarshalPrototype = \ |
| 664 |
VulkanAPI(API_PREFIX_UNMARSHAL + "extension_struct", |
| 665 |
STREAM_RET_TYPE, |
| 666 |
self.marshalingParams + |
| 667 |
[STRUCT_EXTENSION_PARAM_FOR_WRITE]) |
| 668 |
|
| 669 |
def onBegin(self,): |
| 670 |
VulkanWrapperGenerator.onBegin(self) |
| 671 |
self.module.appendImpl(self.cgenImpl.makeFuncDecl(self.extensionMarshalPrototype)) |
| 672 |
self.module.appendImpl(self.cgenImpl.makeFuncDecl(self.extensionUnmarshalPrototype)) |
| 673 |
|
| 674 |
def onBeginFeature(self, featureName, featureType): |
| 675 |
VulkanWrapperGenerator.onBeginFeature(self, featureName, featureType) |
| 676 |
self.currentFeature = featureName |
| 677 |
|
| 678 |
def onGenType(self, typeXml, name, alias): |
| 679 |
VulkanWrapperGenerator.onGenType(self, typeXml, name, alias) |
| 680 |
|
| 681 |
if name in self.knownDefs: |
| 682 |
return |
| 683 |
|
| 684 |
category = self.typeInfo.categoryOf(name) |
| 685 |
|
| 686 |
if category in ["struct", "union"] and alias: |
| 687 |
self.module.appendHeader( |
| 688 |
self.cgenHeader.makeFuncAlias(API_PREFIX_MARSHAL + name, |
| 689 |
API_PREFIX_MARSHAL + alias)) |
| 690 |
self.module.appendHeader( |
| 691 |
self.cgenHeader.makeFuncAlias(API_PREFIX_UNMARSHAL + name, |
| 692 |
API_PREFIX_UNMARSHAL + alias)) |
| 693 |
|
| 694 |
if category in ["struct", "union"] and not alias: |
| 695 |
|
| 696 |
structInfo = self.typeInfo.structs[name] |
| 697 |
|
| 698 |
marshalParams = self.marshalingParams + \ |
| 699 |
[makeVulkanTypeSimple(True, name, 1, MARSHAL_INPUT_VAR_NAME)] |
| 700 |
|
| 701 |
freeParams = [] |
| 702 |
letParams = [] |
| 703 |
|
| 704 |
for (envname, bindingInfo) in list(sorted(structInfo.environment.items(), key = lambda kv: kv[0])): |
| 705 |
if None == bindingInfo["binding"]: |
| 706 |
freeParams.append(makeVulkanTypeSimple(True, bindingInfo["type"], 0, envname)) |
| 707 |
else: |
| 708 |
if not bindingInfo["structmember"]: |
| 709 |
letParams.append(makeVulkanTypeSimple(True, bindingInfo["type"], 0, envname)) |
| 710 |
|
| 711 |
marshalPrototype = \ |
| 712 |
VulkanAPI(API_PREFIX_MARSHAL + name, |
| 713 |
STREAM_RET_TYPE, |
| 714 |
marshalParams + freeParams) |
| 715 |
|
| 716 |
marshalPrototypeNoFilter = \ |
| 717 |
VulkanAPI(API_PREFIX_MARSHAL + name, |
| 718 |
STREAM_RET_TYPE, |
| 719 |
marshalParams) |
| 720 |
|
| 721 |
def structMarshalingCustom(cgen): |
| 722 |
self.writeCodegen.cgen = cgen |
| 723 |
self.writeCodegen.currentStructInfo = structInfo |
| 724 |
self.writeCodegen.cgen.stmt("(void)%s" % ROOT_TYPE_VAR_NAME) |
| 725 |
|
| 726 |
marshalingCode = \ |
| 727 |
CUSTOM_MARSHAL_TYPES[name]["common"] + \ |
| 728 |
CUSTOM_MARSHAL_TYPES[name]["marshaling"].format( |
| 729 |
streamVarName=self.writeCodegen.streamVarName, |
| 730 |
rootTypeVarName=self.writeCodegen.rootTypeVarName, |
| 731 |
inputVarName=self.writeCodegen.inputVarName, |
| 732 |
newInputVarName=self.writeCodegen.inputVarName + "_new") |
| 733 |
for line in marshalingCode.split('\n'): |
| 734 |
cgen.line(line) |
| 735 |
|
| 736 |
def structMarshalingDef(cgen): |
| 737 |
self.writeCodegen.cgen = cgen |
| 738 |
self.writeCodegen.currentStructInfo = structInfo |
| 739 |
self.writeCodegen.cgen.stmt("(void)%s" % ROOT_TYPE_VAR_NAME) |
| 740 |
|
| 741 |
if category == "struct": |
| 742 |
# marshal 'let' parameters first |
| 743 |
for letp in letParams: |
| 744 |
self.writeCodegen.streamLetParameter(self.typeInfo, letp) |
| 745 |
|
| 746 |
for member in structInfo.members: |
| 747 |
iterateVulkanType(self.typeInfo, member, self.writeCodegen) |
| 748 |
if category == "union": |
| 749 |
iterateVulkanType(self.typeInfo, structInfo.members[0], self.writeCodegen) |
| 750 |
|
| 751 |
def structMarshalingDefNoFilter(cgen): |
| 752 |
self.writeCodegen.cgen = cgen |
| 753 |
self.writeCodegen.currentStructInfo = structInfo |
| 754 |
self.writeCodegen.doFiltering = False |
| 755 |
self.writeCodegen.cgen.stmt("(void)%s" % ROOT_TYPE_VAR_NAME) |
| 756 |
|
| 757 |
if category == "struct": |
| 758 |
# marshal 'let' parameters first |
| 759 |
for letp in letParams: |
| 760 |
self.writeCodegen.streamLetParameter(self.typeInfo, letp) |
| 761 |
|
| 762 |
for member in structInfo.members: |
| 763 |
iterateVulkanType(self.typeInfo, member, self.writeCodegen) |
| 764 |
if category == "union": |
| 765 |
iterateVulkanType(self.typeInfo, structInfo.members[0], self.writeCodegen) |
| 766 |
self.writeCodegen.doFiltering = True |
| 767 |
|
| 768 |
self.module.appendHeader( |
| 769 |
self.cgenHeader.makeFuncDecl(marshalPrototype)) |
| 770 |
|
| 771 |
if name in CUSTOM_MARSHAL_TYPES and CUSTOM_MARSHAL_TYPES[name].get("marshaling"): |
| 772 |
self.module.appendImpl( |
| 773 |
self.cgenImpl.makeFuncImpl( |
| 774 |
marshalPrototype, structMarshalingCustom)) |
| 775 |
else: |
| 776 |
self.module.appendImpl( |
| 777 |
self.cgenImpl.makeFuncImpl( |
| 778 |
marshalPrototype, structMarshalingDef)) |
| 779 |
|
| 780 |
if freeParams != []: |
| 781 |
self.module.appendHeader( |
| 782 |
self.cgenHeader.makeFuncDecl(marshalPrototypeNoFilter)) |
| 783 |
self.module.appendImpl( |
| 784 |
self.cgenImpl.makeFuncImpl( |
| 785 |
marshalPrototypeNoFilter, structMarshalingDefNoFilter)) |
| 786 |
|
| 787 |
unmarshalPrototype = \ |
| 788 |
VulkanAPI(API_PREFIX_UNMARSHAL + name, |
| 789 |
STREAM_RET_TYPE, |
| 790 |
self.marshalingParams + [makeVulkanTypeSimple(False, name, 1, UNMARSHAL_INPUT_VAR_NAME)] + freeParams) |
| 791 |
|
| 792 |
unmarshalPrototypeNoFilter = \ |
| 793 |
VulkanAPI(API_PREFIX_UNMARSHAL + name, |
| 794 |
STREAM_RET_TYPE, |
| 795 |
self.marshalingParams + [makeVulkanTypeSimple(False, name, 1, UNMARSHAL_INPUT_VAR_NAME)]) |
| 796 |
|
| 797 |
def structUnmarshalingCustom(cgen): |
| 798 |
self.readCodegen.cgen = cgen |
| 799 |
self.readCodegen.currentStructInfo = structInfo |
| 800 |
self.writeCodegen.cgen.stmt("(void)%s" % ROOT_TYPE_VAR_NAME) |
| 801 |
|
| 802 |
unmarshalingCode = \ |
| 803 |
CUSTOM_MARSHAL_TYPES[name]["common"] + \ |
| 804 |
CUSTOM_MARSHAL_TYPES[name]["unmarshaling"].format( |
| 805 |
streamVarName=self.readCodegen.streamVarName, |
| 806 |
rootTypeVarName=self.readCodegen.rootTypeVarName, |
| 807 |
inputVarName=self.readCodegen.inputVarName, |
| 808 |
newInputVarName=self.readCodegen.inputVarName + "_new") |
| 809 |
for line in unmarshalingCode.split('\n'): |
| 810 |
cgen.line(line) |
| 811 |
|
| 812 |
def structUnmarshalingDef(cgen): |
| 813 |
self.readCodegen.cgen = cgen |
| 814 |
self.readCodegen.currentStructInfo = structInfo |
| 815 |
self.writeCodegen.cgen.stmt("(void)%s" % ROOT_TYPE_VAR_NAME) |
| 816 |
|
| 817 |
if category == "struct": |
| 818 |
# unmarshal 'let' parameters first |
| 819 |
for letp in letParams: |
| 820 |
self.readCodegen.streamLetParameter(self.typeInfo, letp) |
| 821 |
|
| 822 |
for member in structInfo.members: |
| 823 |
iterateVulkanType(self.typeInfo, member, self.readCodegen) |
| 824 |
if category == "union": |
| 825 |
iterateVulkanType(self.typeInfo, structInfo.members[0], self.readCodegen) |
| 826 |
|
| 827 |
def structUnmarshalingDefNoFilter(cgen): |
| 828 |
self.readCodegen.cgen = cgen |
| 829 |
self.readCodegen.currentStructInfo = structInfo |
| 830 |
self.readCodegen.doFiltering = False |
| 831 |
self.writeCodegen.cgen.stmt("(void)%s" % ROOT_TYPE_VAR_NAME) |
| 832 |
|
| 833 |
if category == "struct": |
| 834 |
# unmarshal 'let' parameters first |
| 835 |
for letp in letParams: |
| 836 |
iterateVulkanType(self.typeInfo, letp, self.readCodegen) |
| 837 |
for member in structInfo.members: |
| 838 |
iterateVulkanType(self.typeInfo, member, self.readCodegen) |
| 839 |
if category == "union": |
| 840 |
iterateVulkanType(self.typeInfo, structInfo.members[0], self.readCodegen) |
| 841 |
self.readCodegen.doFiltering = True |
| 842 |
|
| 843 |
self.module.appendHeader( |
| 844 |
self.cgenHeader.makeFuncDecl(unmarshalPrototype)) |
| 845 |
|
| 846 |
if name in CUSTOM_MARSHAL_TYPES and CUSTOM_MARSHAL_TYPES[name].get("unmarshaling"): |
| 847 |
self.module.appendImpl( |
| 848 |
self.cgenImpl.makeFuncImpl( |
| 849 |
unmarshalPrototype, structUnmarshalingCustom)) |
| 850 |
else: |
| 851 |
self.module.appendImpl( |
| 852 |
self.cgenImpl.makeFuncImpl( |
| 853 |
unmarshalPrototype, structUnmarshalingDef)) |
| 854 |
|
| 855 |
if freeParams != []: |
| 856 |
self.module.appendHeader( |
| 857 |
self.cgenHeader.makeFuncDecl(unmarshalPrototypeNoFilter)) |
| 858 |
self.module.appendImpl( |
| 859 |
self.cgenImpl.makeFuncImpl( |
| 860 |
unmarshalPrototypeNoFilter, structUnmarshalingDefNoFilter)) |
| 861 |
|
| 862 |
def onGenCmd(self, cmdinfo, name, alias): |
| 863 |
VulkanWrapperGenerator.onGenCmd(self, cmdinfo, name, alias) |
| 864 |
if name in KNOWN_FUNCTION_OPCODES: |
| 865 |
opcode = KNOWN_FUNCTION_OPCODES[name] |
| 866 |
else: |
| 867 |
hashCode = hashlib.sha256(name.encode()).hexdigest()[:8] |
| 868 |
hashInt = int(hashCode, 16) |
| 869 |
opcode = self.beginOpcode + hashInt % (self.endOpcode - self.beginOpcode) |
| 870 |
hasHashCollision = False |
| 871 |
while opcode in self.knownOpcodes: |
| 872 |
hasHashCollision = True |
| 873 |
opcode += 1 |
| 874 |
if hasHashCollision: |
| 875 |
print("Hash collision occurred on function '{}'. " |
| 876 |
"Please add the following line to marshalingdefs.py:".format(name), file=sys.stderr) |
| 877 |
print("----------------------", file=sys.stderr) |
| 878 |
print(" \"{}\": {},".format(name, opcode), file=sys.stderr) |
| 879 |
print("----------------------", file=sys.stderr) |
| 880 |
|
| 881 |
self.module.appendHeader( |
| 882 |
"#define OP_%s %d\n" % (name, opcode)) |
| 883 |
self.apiOpcodes[name] = (opcode, self.currentFeature) |
| 884 |
self.knownOpcodes.add(opcode) |
| 885 |
|
| 886 |
def doExtensionStructMarshalingCodegen(self, cgen, retType, extParam, forEach, funcproto, direction): |
| 887 |
accessVar = "structAccess" |
| 888 |
sizeVar = "currExtSize" |
| 889 |
cgen.stmt("VkInstanceCreateInfo* %s = (VkInstanceCreateInfo*)(%s)" % (accessVar, extParam.paramName)) |
| 890 |
cgen.stmt("size_t %s = %s(%s->getFeatureBits(), %s, %s)" % (sizeVar, |
| 891 |
EXTENSION_SIZE_WITH_STREAM_FEATURES_API_NAME, VULKAN_STREAM_VAR_NAME, ROOT_TYPE_VAR_NAME, extParam.paramName)) |
| 892 |
|
| 893 |
cgen.beginIf("!%s && %s" % (sizeVar, extParam.paramName)) |
| 894 |
|
| 895 |
cgen.line("// unknown struct extension; skip and call on its pNext field"); |
| 896 |
cgen.funcCall(None, funcproto.name, [ |
| 897 |
"vkStream", ROOT_TYPE_VAR_NAME, "(void*)%s->pNext" % accessVar]) |
| 898 |
cgen.stmt("return") |
| 899 |
|
| 900 |
cgen.endIf() |
| 901 |
cgen.beginElse() |
| 902 |
|
| 903 |
cgen.line("// known or null extension struct") |
| 904 |
|
| 905 |
if direction == "write": |
| 906 |
cgen.stmt("vkStream->putBe32(%s)" % sizeVar) |
| 907 |
elif not self.dynAlloc: |
| 908 |
cgen.stmt("vkStream->getBe32()"); |
| 909 |
|
| 910 |
cgen.beginIf("!%s" % (sizeVar)) |
| 911 |
cgen.line("// exit if this was a null extension struct (size == 0 in this branch)") |
| 912 |
cgen.stmt("return") |
| 913 |
cgen.endIf() |
| 914 |
|
| 915 |
cgen.endIf() |
| 916 |
|
| 917 |
# Now we can do stream stuff |
| 918 |
if direction == "write": |
| 919 |
cgen.stmt("vkStream->write(%s, sizeof(VkStructureType))" % extParam.paramName) |
| 920 |
elif not self.dynAlloc: |
| 921 |
cgen.stmt("uint64_t pNext_placeholder") |
| 922 |
placeholderAccess = "(&pNext_placeholder)" |
| 923 |
cgen.stmt("vkStream->read((void*)(&pNext_placeholder), sizeof(VkStructureType))") |
| 924 |
cgen.stmt("(void)pNext_placeholder") |
| 925 |
|
| 926 |
def fatalDefault(cgen): |
| 927 |
cgen.line("// fatal; the switch is only taken if the extension struct is known") |
| 928 |
if self.variant != "guest": |
| 929 |
cgen.stmt("fprintf(stderr, \" %s, Unhandled Vulkan structure type %s [%d], aborting.\\n\", __func__, string_VkStructureType(VkStructureType(structType)), structType)") |
| 930 |
cgen.stmt("abort()") |
| 931 |
pass |
| 932 |
|
| 933 |
self.emitForEachStructExtension( |
| 934 |
cgen, |
| 935 |
retType, |
| 936 |
extParam, |
| 937 |
forEach, |
| 938 |
defaultEmit=fatalDefault, |
| 939 |
rootTypeVar=ROOT_TYPE_PARAM) |
| 940 |
|
| 941 |
def onEnd(self,): |
| 942 |
VulkanWrapperGenerator.onEnd(self) |
| 943 |
|
| 944 |
def forEachExtensionMarshal(ext, castedAccess, cgen): |
| 945 |
cgen.funcCall(None, API_PREFIX_MARSHAL + ext.name, |
| 946 |
[VULKAN_STREAM_VAR_NAME, ROOT_TYPE_VAR_NAME, castedAccess]) |
| 947 |
|
| 948 |
def forEachExtensionUnmarshal(ext, castedAccess, cgen): |
| 949 |
cgen.funcCall(None, API_PREFIX_UNMARSHAL + ext.name, |
| 950 |
[VULKAN_STREAM_VAR_NAME, ROOT_TYPE_VAR_NAME, castedAccess]) |
| 951 |
|
| 952 |
self.module.appendImpl( |
| 953 |
self.cgenImpl.makeFuncImpl( |
| 954 |
self.extensionMarshalPrototype, |
| 955 |
lambda cgen: self.doExtensionStructMarshalingCodegen( |
| 956 |
cgen, |
| 957 |
STREAM_RET_TYPE, |
| 958 |
STRUCT_EXTENSION_PARAM, |
| 959 |
forEachExtensionMarshal, |
| 960 |
self.extensionMarshalPrototype, |
| 961 |
"write"))) |
| 962 |
|
| 963 |
self.module.appendImpl( |
| 964 |
self.cgenImpl.makeFuncImpl( |
| 965 |
self.extensionUnmarshalPrototype, |
| 966 |
lambda cgen: self.doExtensionStructMarshalingCodegen( |
| 967 |
cgen, |
| 968 |
STREAM_RET_TYPE, |
| 969 |
STRUCT_EXTENSION_PARAM_FOR_WRITE, |
| 970 |
forEachExtensionUnmarshal, |
| 971 |
self.extensionUnmarshalPrototype, |
| 972 |
"read"))) |
| 973 |
|
| 974 |
opcode2stringPrototype = \ |
| 975 |
VulkanAPI("api_opcode_to_string", |
| 976 |
makeVulkanTypeSimple(True, "char", 1, "none"), |
| 977 |
[ makeVulkanTypeSimple(True, "uint32_t", 0, "opcode") ]) |
| 978 |
|
| 979 |
self.module.appendHeader( |
| 980 |
self.cgenHeader.makeFuncDecl(opcode2stringPrototype)) |
| 981 |
|
| 982 |
def emitOpcode2StringImpl(apiOpcodes, cgen): |
| 983 |
cgen.line("switch(opcode)") |
| 984 |
cgen.beginBlock() |
| 985 |
|
| 986 |
currFeature = None |
| 987 |
|
| 988 |
for (name, (opcodeNum, feature)) in sorted(apiOpcodes.items(), key = lambda x : x[1][0]): |
| 989 |
if not currFeature: |
| 990 |
cgen.leftline("#ifdef %s" % feature) |
| 991 |
currFeature = feature |
| 992 |
|
| 993 |
if currFeature and feature != currFeature: |
| 994 |
cgen.leftline("#endif") |
| 995 |
cgen.leftline("#ifdef %s" % feature) |
| 996 |
currFeature = feature |
| 997 |
|
| 998 |
cgen.line("case OP_%s:" % name) |
| 999 |
cgen.beginBlock() |
| 1000 |
cgen.stmt("return \"OP_%s\"" % name) |
| 1001 |
cgen.endBlock() |
| 1002 |
|
| 1003 |
if currFeature: |
| 1004 |
cgen.leftline("#endif") |
| 1005 |
|
| 1006 |
cgen.line("default:") |
| 1007 |
cgen.beginBlock() |
| 1008 |
cgen.stmt("return \"OP_UNKNOWN_API_CALL\"") |
| 1009 |
cgen.endBlock() |
| 1010 |
|
| 1011 |
cgen.endBlock() |
| 1012 |
|
| 1013 |
self.module.appendImpl( |
| 1014 |
self.cgenImpl.makeFuncImpl( |
| 1015 |
opcode2stringPrototype, |
| 1016 |
lambda cgen: emitOpcode2StringImpl(self.apiOpcodes, cgen))) |
| 1017 |
|
| 1018 |
self.module.appendHeader( |
| 1019 |
"#define OP_vkFirst_old %d\n" % (self.beginOpcodeOld)) |
| 1020 |
self.module.appendHeader( |
| 1021 |
"#define OP_vkLast_old %d\n" % (self.endOpcodeOld)) |
| 1022 |
self.module.appendHeader( |
| 1023 |
"#define OP_vkFirst %d\n" % (self.beginOpcode)) |
| 1024 |
self.module.appendHeader( |
| 1025 |
"#define OP_vkLast %d\n" % (self.endOpcode)) |