| 1 |
# Copyright 2018 Google LLC |
| 2 |
# SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
from .common.codegen import CodeGen |
| 5 |
from .common.vulkantypes import \ |
| 6 |
VulkanAPI, makeVulkanTypeSimple, iterateVulkanType, VulkanTypeIterator |
| 7 |
|
| 8 |
from .wrapperdefs import VulkanWrapperGenerator |
| 9 |
from .wrapperdefs import STRUCT_EXTENSION_PARAM, STRUCT_EXTENSION_PARAM_FOR_WRITE, EXTENSION_SIZE_API_NAME |
| 10 |
|
| 11 |
class DeepcopyCodegen(VulkanTypeIterator): |
| 12 |
def __init__(self, cgen, inputVars, poolVarName, rootVarName, prefix, skipValues=False): |
| 13 |
self.cgen = cgen |
| 14 |
self.inputVars = inputVars |
| 15 |
self.prefix = prefix |
| 16 |
self.poolVarName = poolVarName |
| 17 |
self.rootVarName = rootVarName |
| 18 |
self.skipValues = skipValues |
| 19 |
|
| 20 |
def makeAccess(varName, asPtr = True): |
| 21 |
return lambda t: self.cgen.generalAccess(t, parentVarName = varName, asPtr = asPtr) |
| 22 |
|
| 23 |
def makeLengthAccess(varName): |
| 24 |
return lambda t: self.cgen.generalLengthAccess(t, parentVarName = varName) |
| 25 |
|
| 26 |
def makeLengthAccessGuard(varName): |
| 27 |
return lambda t: self.cgen.generalLengthAccessGuard(t, parentVarName=varName) |
| 28 |
|
| 29 |
self.exprAccessorLhs = makeAccess(self.inputVars[0]) |
| 30 |
self.exprAccessorRhs = makeAccess(self.inputVars[1]) |
| 31 |
|
| 32 |
self.exprAccessorValueLhs = makeAccess(self.inputVars[0], asPtr = False) |
| 33 |
self.exprAccessorValueRhs = makeAccess(self.inputVars[1], asPtr = False) |
| 34 |
|
| 35 |
self.lenAccessorLhs = makeLengthAccess(self.inputVars[0]) |
| 36 |
self.lenAccessorRhs = makeLengthAccess(self.inputVars[1]) |
| 37 |
|
| 38 |
self.lenAccessorGuardLhs = makeLengthAccessGuard(self.inputVars[0]) |
| 39 |
self.lenAccessorGuardRhs = makeLengthAccessGuard(self.inputVars[1]) |
| 40 |
|
| 41 |
self.checked = False |
| 42 |
|
| 43 |
def needSkip(self, vulkanType): |
| 44 |
return False |
| 45 |
|
| 46 |
def makeCastExpr(self, vulkanType): |
| 47 |
return "(%s)" % ( |
| 48 |
self.cgen.makeCTypeDecl(vulkanType, useParamName=False)) |
| 49 |
|
| 50 |
def makeNonConstCastForCopy(self, access, vulkanType): |
| 51 |
if vulkanType.staticArrExpr: |
| 52 |
casted = "%s(%s)" % (self.makeCastExpr(vulkanType.getForAddressAccess().getForNonConstAccess()), access) |
| 53 |
elif vulkanType.accessibleAsPointer(): |
| 54 |
casted = "%s(%s)" % (self.makeCastExpr(vulkanType.getForNonConstAccess()), access) |
| 55 |
else: |
| 56 |
casted = "%s(%s)" % (self.makeCastExpr(vulkanType.getForAddressAccess().getForNonConstAccess()), access) |
| 57 |
return casted |
| 58 |
|
| 59 |
def makeAllocBytesExpr(self, lenAccess, vulkanType): |
| 60 |
sizeof = self.cgen.sizeofExpr( \ |
| 61 |
vulkanType.getForValueAccess()) |
| 62 |
if lenAccess: |
| 63 |
bytesExpr = "%s * %s" % (lenAccess, sizeof) |
| 64 |
else: |
| 65 |
bytesExpr = sizeof |
| 66 |
|
| 67 |
return bytesExpr |
| 68 |
|
| 69 |
def onCheck(self, vulkanType): |
| 70 |
pass |
| 71 |
|
| 72 |
def endCheck(self, vulkanType): |
| 73 |
pass |
| 74 |
|
| 75 |
def onCompoundType(self, vulkanType): |
| 76 |
|
| 77 |
if self.needSkip(vulkanType): |
| 78 |
self.cgen.line("// TODO: Unsupported : %s" % |
| 79 |
self.cgen.makeCTypeDecl(vulkanType)) |
| 80 |
return |
| 81 |
|
| 82 |
accessLhs = self.exprAccessorLhs(vulkanType) |
| 83 |
accessRhs = self.exprAccessorRhs(vulkanType) |
| 84 |
|
| 85 |
lenAccessLhs = self.lenAccessorLhs(vulkanType) |
| 86 |
lenAccessRhs = self.lenAccessorRhs(vulkanType) |
| 87 |
|
| 88 |
lenAccessorGuardLhs = self.lenAccessorGuardLhs(vulkanType) |
| 89 |
lenAccessorGuardRhs = self.lenAccessorGuardRhs(vulkanType) |
| 90 |
|
| 91 |
isPtr = vulkanType.pointerIndirectionLevels > 0 |
| 92 |
|
| 93 |
if lenAccessorGuardLhs is not None: |
| 94 |
self.cgen.beginIf(lenAccessorGuardLhs) |
| 95 |
|
| 96 |
if isPtr: |
| 97 |
self.cgen.stmt("%s = nullptr" % accessRhs) |
| 98 |
self.cgen.beginIf(accessLhs) |
| 99 |
|
| 100 |
self.cgen.stmt( \ |
| 101 |
"%s = %s%s->alloc(%s)" % \ |
| 102 |
(accessRhs, self.makeCastExpr(vulkanType.getForNonConstAccess()), |
| 103 |
self.poolVarName, self.makeAllocBytesExpr(lenAccessLhs, vulkanType))) |
| 104 |
|
| 105 |
if lenAccessLhs is not None: |
| 106 |
|
| 107 |
loopVar = "i" |
| 108 |
accessLhs = "%s + %s" % (accessLhs, loopVar) |
| 109 |
forInit = "uint32_t %s = 0" % loopVar |
| 110 |
forCond = "%s < (uint32_t)%s" % (loopVar, lenAccessLhs) |
| 111 |
forIncr = "++%s" % loopVar |
| 112 |
|
| 113 |
if isPtr: |
| 114 |
# Avoid generating a self-assign. |
| 115 |
if lenAccessRhs != lenAccessLhs: |
| 116 |
self.cgen.stmt("%s = %s" % (lenAccessRhs, lenAccessLhs)) |
| 117 |
|
| 118 |
accessRhs = "%s + %s" % (accessRhs, loopVar) |
| 119 |
self.cgen.beginFor(forInit, forCond, forIncr) |
| 120 |
|
| 121 |
|
| 122 |
accessRhsCasted = self.makeNonConstCastForCopy(accessRhs, vulkanType) |
| 123 |
|
| 124 |
self.cgen.funcCall(None, self.prefix + vulkanType.typeName, |
| 125 |
[self.poolVarName, self.rootVarName, accessLhs, accessRhsCasted]) |
| 126 |
|
| 127 |
if lenAccessLhs is not None: |
| 128 |
self.cgen.endFor() |
| 129 |
|
| 130 |
if isPtr: |
| 131 |
self.cgen.endIf() |
| 132 |
|
| 133 |
if lenAccessorGuardLhs is not None: |
| 134 |
self.cgen.endIf() |
| 135 |
|
| 136 |
def onString(self, vulkanType): |
| 137 |
accessLhs = self.exprAccessorLhs(vulkanType) |
| 138 |
accessRhs = self.exprAccessorRhs(vulkanType) |
| 139 |
|
| 140 |
self.cgen.stmt("%s = nullptr" % accessRhs) |
| 141 |
self.cgen.beginIf(accessLhs) |
| 142 |
|
| 143 |
self.cgen.stmt( \ |
| 144 |
"%s = %s->strDup(%s)" % \ |
| 145 |
(accessRhs, |
| 146 |
self.poolVarName, |
| 147 |
accessLhs)) |
| 148 |
|
| 149 |
self.cgen.endIf() |
| 150 |
|
| 151 |
def onStringArray(self, vulkanType): |
| 152 |
accessLhs = self.exprAccessorLhs(vulkanType) |
| 153 |
accessRhs = self.exprAccessorRhs(vulkanType) |
| 154 |
|
| 155 |
lenAccessLhs = self.lenAccessorLhs(vulkanType) |
| 156 |
|
| 157 |
self.cgen.stmt("%s = nullptr" % accessRhs) |
| 158 |
if lenAccessLhs is not None: |
| 159 |
self.cgen.beginIf("%s && %s" % (accessLhs, lenAccessLhs)) |
| 160 |
|
| 161 |
self.cgen.stmt( \ |
| 162 |
"%s = %s->strDupArray(%s, %s)" % \ |
| 163 |
(accessRhs, |
| 164 |
self.poolVarName, |
| 165 |
accessLhs, |
| 166 |
lenAccessLhs)) |
| 167 |
|
| 168 |
self.cgen.endIf() |
| 169 |
|
| 170 |
def onStaticArr(self, vulkanType): |
| 171 |
accessLhs = self.exprAccessorValueLhs(vulkanType) |
| 172 |
accessRhs = self.exprAccessorValueRhs(vulkanType) |
| 173 |
|
| 174 |
lenAccessLhs = self.lenAccessorLhs(vulkanType) |
| 175 |
|
| 176 |
bytesExpr = self.makeAllocBytesExpr(lenAccessLhs, vulkanType) |
| 177 |
self.cgen.stmt("memcpy(%s, %s, %s)" % (accessRhs, accessLhs, bytesExpr)) |
| 178 |
|
| 179 |
def onStructExtension(self, vulkanType): |
| 180 |
|
| 181 |
lhs = self.exprAccessorLhs(vulkanType) |
| 182 |
rhs = self.exprAccessorRhs(vulkanType) |
| 183 |
|
| 184 |
rhsExpr = "(%s)(%s)" % ("void*", rhs) |
| 185 |
|
| 186 |
nextVar = "from_%s" % vulkanType.paramName |
| 187 |
sizeVar = "%s_size" % vulkanType.paramName |
| 188 |
|
| 189 |
self.cgen.beginIf("%s == VK_STRUCTURE_TYPE_MAX_ENUM" % |
| 190 |
self.rootVarName) |
| 191 |
self.cgen.stmt("%s = from->sType" % self.rootVarName) |
| 192 |
self.cgen.endIf() |
| 193 |
|
| 194 |
self.cgen.stmt("const void* %s = %s" % (nextVar, self.inputVars[0])) |
| 195 |
self.cgen.stmt("size_t %s = 0u" % sizeVar) |
| 196 |
self.cgen.beginWhile("!%s && %s" % (sizeVar, nextVar)) |
| 197 |
self.cgen.stmt("%s = static_cast<const VkBaseOutStructure*>(%s)->%s" % ( |
| 198 |
nextVar, nextVar, vulkanType.paramName |
| 199 |
)) |
| 200 |
self.cgen.stmt("%s = %s(%s, %s)" % ( |
| 201 |
sizeVar, EXTENSION_SIZE_API_NAME, self.rootVarName, nextVar)) |
| 202 |
self.cgen.endWhile() |
| 203 |
|
| 204 |
self.cgen.stmt("%s = nullptr" % rhs) |
| 205 |
|
| 206 |
self.cgen.beginIf(sizeVar) |
| 207 |
|
| 208 |
self.cgen.stmt( \ |
| 209 |
"%s = %s%s->alloc(%s)" % \ |
| 210 |
(rhs, self.makeCastExpr(vulkanType.getForNonConstAccess()), self.poolVarName, sizeVar)) |
| 211 |
|
| 212 |
self.cgen.funcCall(None, self.prefix + "extension_struct", |
| 213 |
[self.poolVarName, self.rootVarName, nextVar, rhsExpr]) |
| 214 |
|
| 215 |
self.cgen.endIf() |
| 216 |
|
| 217 |
def onPointer(self, vulkanType): |
| 218 |
|
| 219 |
accessLhs = self.exprAccessorLhs(vulkanType) |
| 220 |
accessRhs = self.exprAccessorRhs(vulkanType) |
| 221 |
|
| 222 |
if self.needSkip(vulkanType): |
| 223 |
self.cgen.stmt("%s = nullptr" % accessRhs) |
| 224 |
return |
| 225 |
|
| 226 |
lenAccessLhs = self.lenAccessorLhs(vulkanType) |
| 227 |
|
| 228 |
self.cgen.stmt("%s = nullptr" % accessRhs) |
| 229 |
self.cgen.beginIf(accessLhs) |
| 230 |
|
| 231 |
bytesExpr = self.makeAllocBytesExpr(lenAccessLhs, vulkanType) |
| 232 |
|
| 233 |
self.cgen.stmt( \ |
| 234 |
"%s = %s%s->dupArray(%s, %s)" % \ |
| 235 |
(accessRhs, |
| 236 |
self.makeCastExpr(vulkanType.getForNonConstAccess()), |
| 237 |
self.poolVarName, |
| 238 |
accessLhs, |
| 239 |
bytesExpr)) |
| 240 |
|
| 241 |
self.cgen.endIf() |
| 242 |
|
| 243 |
def onValue(self, vulkanType): |
| 244 |
if self.skipValues: |
| 245 |
return |
| 246 |
|
| 247 |
accessLhs = self.exprAccessorValueLhs(vulkanType) |
| 248 |
accessRhs = self.exprAccessorValueRhs(vulkanType) |
| 249 |
|
| 250 |
self.cgen.stmt("%s = %s" % (accessRhs, accessLhs)) |
| 251 |
|
| 252 |
class VulkanDeepcopy(VulkanWrapperGenerator): |
| 253 |
|
| 254 |
def __init__(self, module, typeInfo): |
| 255 |
VulkanWrapperGenerator.__init__(self, module, typeInfo) |
| 256 |
|
| 257 |
self.codegen = CodeGen() |
| 258 |
|
| 259 |
self.deepcopyPrefix = "deepcopy_" |
| 260 |
self.deepcopyVars = ["from", "to"] |
| 261 |
self.deepcopyAllocatorVarName = "alloc" |
| 262 |
self.deepcopyAllocatorParam = \ |
| 263 |
makeVulkanTypeSimple(False, "Allocator", 1, |
| 264 |
self.deepcopyAllocatorVarName) |
| 265 |
self.deepcopyRootVarName = "rootType" |
| 266 |
self.deepcopyRootParam = \ |
| 267 |
makeVulkanTypeSimple(False, "VkStructureType", |
| 268 |
0, self.deepcopyRootVarName) |
| 269 |
self.voidType = makeVulkanTypeSimple(False, "void", 0) |
| 270 |
|
| 271 |
self.deepcopyCodegen = \ |
| 272 |
DeepcopyCodegen( |
| 273 |
None, |
| 274 |
self.deepcopyVars, |
| 275 |
self.deepcopyAllocatorVarName, |
| 276 |
self.deepcopyRootVarName, |
| 277 |
self.deepcopyPrefix, |
| 278 |
skipValues=True) |
| 279 |
|
| 280 |
self.knownDefs = {} |
| 281 |
|
| 282 |
self.extensionDeepcopyPrototype = \ |
| 283 |
VulkanAPI(self.deepcopyPrefix + "extension_struct", |
| 284 |
self.voidType, |
| 285 |
[self.deepcopyAllocatorParam, |
| 286 |
self.deepcopyRootParam, |
| 287 |
STRUCT_EXTENSION_PARAM, |
| 288 |
STRUCT_EXTENSION_PARAM_FOR_WRITE]) |
| 289 |
|
| 290 |
def onBegin(self,): |
| 291 |
VulkanWrapperGenerator.onBegin(self) |
| 292 |
self.module.appendImpl(self.codegen.makeFuncDecl( |
| 293 |
self.extensionDeepcopyPrototype)) |
| 294 |
|
| 295 |
def onGenType(self, typeXml, name, alias): |
| 296 |
VulkanWrapperGenerator.onGenType(self, typeXml, name, alias) |
| 297 |
|
| 298 |
if name in self.knownDefs: |
| 299 |
return |
| 300 |
|
| 301 |
category = self.typeInfo.categoryOf(name) |
| 302 |
|
| 303 |
if category in ["struct", "union"] and alias: |
| 304 |
self.module.appendHeader( |
| 305 |
self.codegen.makeFuncAlias(self.deepcopyPrefix + name, |
| 306 |
self.deepcopyPrefix + alias)) |
| 307 |
|
| 308 |
if category in ["struct", "union"] and not alias: |
| 309 |
|
| 310 |
structInfo = self.typeInfo.structs[name] |
| 311 |
|
| 312 |
typeFromName = \ |
| 313 |
lambda varname: \ |
| 314 |
makeVulkanTypeSimple(varname == "from", name, 1, varname) |
| 315 |
|
| 316 |
deepcopyParams = \ |
| 317 |
[self.deepcopyAllocatorParam, self.deepcopyRootParam] + \ |
| 318 |
list(map(typeFromName, self.deepcopyVars)) |
| 319 |
|
| 320 |
deepcopyPrototype = \ |
| 321 |
VulkanAPI(self.deepcopyPrefix + name, |
| 322 |
self.voidType, |
| 323 |
deepcopyParams) |
| 324 |
|
| 325 |
def structDeepcopyDef(cgen): |
| 326 |
self.deepcopyCodegen.cgen = cgen |
| 327 |
canSimplyAssign = True |
| 328 |
for member in structInfo.members: |
| 329 |
if not member.isSimpleValueType(self.typeInfo): |
| 330 |
canSimplyAssign = False |
| 331 |
|
| 332 |
cgen.stmt("(void)%s" % self.deepcopyAllocatorVarName) |
| 333 |
cgen.stmt("(void)%s" % self.deepcopyRootVarName) |
| 334 |
cgen.stmt("*to = *from") |
| 335 |
if canSimplyAssign: |
| 336 |
pass |
| 337 |
else: |
| 338 |
for member in structInfo.members: |
| 339 |
iterateVulkanType(self.typeInfo, member, |
| 340 |
self.deepcopyCodegen) |
| 341 |
|
| 342 |
self.module.appendHeader( |
| 343 |
self.codegen.makeFuncDecl(deepcopyPrototype)) |
| 344 |
self.module.appendImpl( |
| 345 |
self.codegen.makeFuncImpl(deepcopyPrototype, structDeepcopyDef)) |
| 346 |
|
| 347 |
def onGenCmd(self, cmdinfo, name, alias): |
| 348 |
VulkanWrapperGenerator.onGenCmd(self, cmdinfo, name, alias) |
| 349 |
|
| 350 |
def onEnd(self,): |
| 351 |
VulkanWrapperGenerator.onEnd(self) |
| 352 |
|
| 353 |
def deepcopyDstExpr(cgen, typeName): |
| 354 |
return cgen.makeReinterpretCast( \ |
| 355 |
STRUCT_EXTENSION_PARAM_FOR_WRITE.paramName, |
| 356 |
typeName, const=False) |
| 357 |
|
| 358 |
def forEachExtensionDeepcopy(ext, castedAccess, cgen): |
| 359 |
cgen.funcCall(None, self.deepcopyPrefix + ext.name, |
| 360 |
[self.deepcopyAllocatorVarName, |
| 361 |
self.deepcopyRootVarName, |
| 362 |
castedAccess, deepcopyDstExpr(cgen, ext.name)]) |
| 363 |
|
| 364 |
self.module.appendImpl( |
| 365 |
self.codegen.makeFuncImpl( |
| 366 |
self.extensionDeepcopyPrototype, |
| 367 |
lambda cgen: self.emitForEachStructExtension( |
| 368 |
cgen, |
| 369 |
self.voidType, |
| 370 |
STRUCT_EXTENSION_PARAM, |
| 371 |
forEachExtensionDeepcopy, |
| 372 |
rootTypeVar=self.deepcopyRootParam))) |