| 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 |
| 10 |
|
| 11 |
class HandleMapCodegen(VulkanTypeIterator): |
| 12 |
def __init__(self, cgen, inputVar, handlemapVarName, prefix, isHandleFunc): |
| 13 |
self.cgen = cgen |
| 14 |
self.inputVar = inputVar |
| 15 |
self.prefix = prefix |
| 16 |
self.handlemapVarName = handlemapVarName |
| 17 |
|
| 18 |
def makeAccess(varName, asPtr = True): |
| 19 |
return lambda t: self.cgen.generalAccess(t, parentVarName = varName, asPtr = asPtr) |
| 20 |
|
| 21 |
def makeLengthAccess(varName): |
| 22 |
return lambda t: self.cgen.generalLengthAccess(t, parentVarName = varName) |
| 23 |
|
| 24 |
def makeLengthAccessGuard(varName): |
| 25 |
return lambda t: self.cgen.generalLengthAccessGuard(t, parentVarName=varName) |
| 26 |
|
| 27 |
self.exprAccessor = makeAccess(self.inputVar) |
| 28 |
self.exprAccessorValue = makeAccess(self.inputVar, asPtr = False) |
| 29 |
self.lenAccessor = makeLengthAccess(self.inputVar) |
| 30 |
self.lenAccessorGuard = makeLengthAccessGuard(self.inputVar) |
| 31 |
|
| 32 |
self.checked = False |
| 33 |
self.isHandleFunc = isHandleFunc |
| 34 |
|
| 35 |
def needSkip(self, vulkanType): |
| 36 |
return False |
| 37 |
|
| 38 |
def makeCastExpr(self, vulkanType): |
| 39 |
return "(%s)" % ( |
| 40 |
self.cgen.makeCTypeDecl(vulkanType, useParamName=False)) |
| 41 |
|
| 42 |
def asNonConstCast(self, access, vulkanType): |
| 43 |
if vulkanType.staticArrExpr: |
| 44 |
casted = "%s(%s)" % (self.makeCastExpr(vulkanType.getForAddressAccess().getForNonConstAccess()), access) |
| 45 |
elif vulkanType.accessibleAsPointer(): |
| 46 |
casted = "%s(%s)" % (self.makeCastExpr(vulkanType.getForNonConstAccess()), access) |
| 47 |
else: |
| 48 |
casted = "%s(%s)" % (self.makeCastExpr(vulkanType.getForAddressAccess().getForNonConstAccess()), access) |
| 49 |
return casted |
| 50 |
|
| 51 |
def onCheck(self, vulkanType): |
| 52 |
pass |
| 53 |
|
| 54 |
def endCheck(self, vulkanType): |
| 55 |
pass |
| 56 |
|
| 57 |
def onCompoundType(self, vulkanType): |
| 58 |
|
| 59 |
if self.needSkip(vulkanType): |
| 60 |
self.cgen.line("// TODO: Unsupported : %s" % |
| 61 |
self.cgen.makeCTypeDecl(vulkanType)) |
| 62 |
return |
| 63 |
access = self.exprAccessor(vulkanType) |
| 64 |
lenAccess = self.lenAccessor(vulkanType) |
| 65 |
lenAccessGuard = self.lenAccessorGuard(vulkanType) |
| 66 |
|
| 67 |
isPtr = vulkanType.pointerIndirectionLevels > 0 |
| 68 |
|
| 69 |
if lenAccessGuard is not None: |
| 70 |
self.cgen.beginIf(lenAccessGuard) |
| 71 |
|
| 72 |
if isPtr: |
| 73 |
self.cgen.beginIf(access) |
| 74 |
|
| 75 |
if lenAccess is not None: |
| 76 |
|
| 77 |
loopVar = "i" |
| 78 |
access = "%s + %s" % (access, loopVar) |
| 79 |
forInit = "uint32_t %s = 0" % loopVar |
| 80 |
forCond = "%s < (uint32_t)%s" % (loopVar, lenAccess) |
| 81 |
forIncr = "++%s" % loopVar |
| 82 |
|
| 83 |
self.cgen.beginFor(forInit, forCond, forIncr) |
| 84 |
|
| 85 |
accessCasted = self.asNonConstCast(access, vulkanType) |
| 86 |
self.cgen.funcCall(None, self.prefix + vulkanType.typeName, |
| 87 |
[self.handlemapVarName, accessCasted]) |
| 88 |
|
| 89 |
if lenAccess is not None: |
| 90 |
self.cgen.endFor() |
| 91 |
|
| 92 |
if isPtr: |
| 93 |
self.cgen.endIf() |
| 94 |
|
| 95 |
if lenAccessGuard is not None: |
| 96 |
self.cgen.endIf() |
| 97 |
|
| 98 |
def onString(self, vulkanType): |
| 99 |
pass |
| 100 |
|
| 101 |
def onStringArray(self, vulkanType): |
| 102 |
pass |
| 103 |
|
| 104 |
def onStaticArr(self, vulkanType): |
| 105 |
if not self.isHandleFunc(vulkanType): |
| 106 |
return |
| 107 |
|
| 108 |
accessLhs = self.exprAccessor(vulkanType) |
| 109 |
lenAccess = self.lenAccessor(vulkanType) |
| 110 |
|
| 111 |
if lenAccess is not None: |
| 112 |
self.cgen.stmt("%s->mapHandles_%s(%s%s, %s)" % \ |
| 113 |
(self.handlemapVarName, vulkanType.typeName, |
| 114 |
self.makeCastExpr(vulkanType.getForAddressAccess().getForNonConstAccess()), |
| 115 |
accessLhs, lenAccess)) |
| 116 |
|
| 117 |
def onStructExtension(self, vulkanType): |
| 118 |
access = self.exprAccessor(vulkanType) |
| 119 |
|
| 120 |
castedAccessExpr = "(%s)(%s)" % ("void*", access) |
| 121 |
self.cgen.beginIf(access) |
| 122 |
self.cgen.funcCall(None, self.prefix + "extension_struct", |
| 123 |
[self.handlemapVarName, castedAccessExpr]) |
| 124 |
self.cgen.endIf() |
| 125 |
|
| 126 |
def onPointer(self, vulkanType): |
| 127 |
if self.needSkip(vulkanType): |
| 128 |
return |
| 129 |
|
| 130 |
if not self.isHandleFunc(vulkanType): |
| 131 |
return |
| 132 |
|
| 133 |
access = self.exprAccessor(vulkanType) |
| 134 |
lenAccess = self.lenAccessor(vulkanType) |
| 135 |
lenAccess = "1" if lenAccess is None else lenAccess |
| 136 |
|
| 137 |
self.cgen.beginIf(access) |
| 138 |
|
| 139 |
self.cgen.stmt( \ |
| 140 |
"%s->mapHandles_%s(%s%s, %s)" % \ |
| 141 |
(self.handlemapVarName, |
| 142 |
vulkanType.typeName, |
| 143 |
self.makeCastExpr(vulkanType.getForNonConstAccess()), |
| 144 |
access, |
| 145 |
lenAccess)) |
| 146 |
|
| 147 |
self.cgen.endIf() |
| 148 |
|
| 149 |
def onValue(self, vulkanType): |
| 150 |
if not self.isHandleFunc(vulkanType): |
| 151 |
return |
| 152 |
access = self.exprAccessor(vulkanType) |
| 153 |
self.cgen.stmt( |
| 154 |
"%s->mapHandles_%s(%s%s)" % \ |
| 155 |
(self.handlemapVarName, vulkanType.typeName, |
| 156 |
self.makeCastExpr(vulkanType.getForAddressAccess().getForNonConstAccess()), |
| 157 |
access)) |
| 158 |
|
| 159 |
class VulkanHandleMap(VulkanWrapperGenerator): |
| 160 |
def __init__(self, module, typeInfo): |
| 161 |
VulkanWrapperGenerator.__init__(self, module, typeInfo) |
| 162 |
|
| 163 |
self.codegen = CodeGen() |
| 164 |
|
| 165 |
self.handlemapPrefix = "handlemap_" |
| 166 |
self.toMapVar = "toMap" |
| 167 |
self.handlemapVarName = "handlemap" |
| 168 |
self.handlemapParam = \ |
| 169 |
makeVulkanTypeSimple(False, "VulkanHandleMapping", 1, |
| 170 |
self.handlemapVarName) |
| 171 |
self.voidType = makeVulkanTypeSimple(False, "void", 0) |
| 172 |
|
| 173 |
self.handlemapCodegen = \ |
| 174 |
HandleMapCodegen( |
| 175 |
None, |
| 176 |
self.toMapVar, |
| 177 |
self.handlemapVarName, |
| 178 |
self.handlemapPrefix, |
| 179 |
lambda vtype : typeInfo.isHandleType(vtype.typeName)) |
| 180 |
|
| 181 |
self.knownDefs = {} |
| 182 |
|
| 183 |
self.extensionHandlemapPrototype = \ |
| 184 |
VulkanAPI(self.handlemapPrefix + "extension_struct", |
| 185 |
self.voidType, |
| 186 |
[self.handlemapParam, STRUCT_EXTENSION_PARAM_FOR_WRITE]) |
| 187 |
|
| 188 |
def onBegin(self,): |
| 189 |
VulkanWrapperGenerator.onBegin(self) |
| 190 |
self.module.appendImpl(self.codegen.makeFuncDecl( |
| 191 |
self.extensionHandlemapPrototype)) |
| 192 |
|
| 193 |
def onGenType(self, typeXml, name, alias): |
| 194 |
VulkanWrapperGenerator.onGenType(self, typeXml, name, alias) |
| 195 |
|
| 196 |
if name in self.knownDefs: |
| 197 |
return |
| 198 |
|
| 199 |
category = self.typeInfo.categoryOf(name) |
| 200 |
|
| 201 |
if category in ["struct", "union"] and alias: |
| 202 |
self.module.appendHeader( |
| 203 |
self.codegen.makeFuncAlias(self.handlemapPrefix + name, |
| 204 |
self.handlemapPrefix + alias)) |
| 205 |
|
| 206 |
if category in ["struct", "union"] and not alias: |
| 207 |
|
| 208 |
structInfo = self.typeInfo.structs[name] |
| 209 |
|
| 210 |
typeFromName = \ |
| 211 |
lambda varname: \ |
| 212 |
makeVulkanTypeSimple(varname == "from", name, 1, varname) |
| 213 |
|
| 214 |
handlemapParams = \ |
| 215 |
[self.handlemapParam] + \ |
| 216 |
list(map(typeFromName, [self.toMapVar])) |
| 217 |
|
| 218 |
handlemapPrototype = \ |
| 219 |
VulkanAPI(self.handlemapPrefix + name, |
| 220 |
self.voidType, |
| 221 |
handlemapParams) |
| 222 |
|
| 223 |
def funcDefGenerator(cgen): |
| 224 |
self.handlemapCodegen.cgen = cgen |
| 225 |
for p in handlemapParams: |
| 226 |
cgen.stmt("(void)%s" % p.paramName) |
| 227 |
for member in structInfo.members: |
| 228 |
iterateVulkanType(self.typeInfo, member, |
| 229 |
self.handlemapCodegen) |
| 230 |
|
| 231 |
self.module.appendHeader( |
| 232 |
self.codegen.makeFuncDecl(handlemapPrototype)) |
| 233 |
self.module.appendImpl( |
| 234 |
self.codegen.makeFuncImpl(handlemapPrototype, funcDefGenerator)) |
| 235 |
|
| 236 |
def onGenCmd(self, cmdinfo, name, alias): |
| 237 |
VulkanWrapperGenerator.onGenCmd(self, cmdinfo, name, alias) |
| 238 |
|
| 239 |
def onEnd(self,): |
| 240 |
VulkanWrapperGenerator.onEnd(self) |
| 241 |
|
| 242 |
def forEachExtensionHandlemap(ext, castedAccess, cgen): |
| 243 |
cgen.funcCall(None, self.handlemapPrefix + ext.name, |
| 244 |
[self.handlemapVarName, castedAccess]) |
| 245 |
|
| 246 |
self.module.appendImpl( |
| 247 |
self.codegen.makeFuncImpl( |
| 248 |
self.extensionHandlemapPrototype, |
| 249 |
lambda cgen: self.emitForEachStructExtension( |
| 250 |
cgen, |
| 251 |
self.voidType, |
| 252 |
STRUCT_EXTENSION_PARAM_FOR_WRITE, |
| 253 |
forEachExtensionHandlemap))) |