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