| 1 |
# Copyright 2018 Google LLC |
| 2 |
# SPDX-License-Identifier: MIT |
| 3 |
import copy |
| 4 |
|
| 5 |
from .common.codegen import CodeGen, VulkanWrapperGenerator |
| 6 |
from .common.vulkantypes import \ |
| 7 |
VulkanAPI, makeVulkanTypeSimple, iterateVulkanType |
| 8 |
|
| 9 |
from .marshaling import VulkanMarshalingCodegen |
| 10 |
from .reservedmarshaling import VulkanReservedMarshalingCodegen |
| 11 |
from .counting import VulkanCountingCodegen |
| 12 |
from .handlemap import HandleMapCodegen |
| 13 |
from .deepcopy import DeepcopyCodegen |
| 14 |
from .transform import TransformCodegen, genTransformsForVulkanType |
| 15 |
|
| 16 |
from .wrapperdefs import API_PREFIX_RESERVEDMARSHAL |
| 17 |
from .wrapperdefs import API_PREFIX_MARSHAL |
| 18 |
from .wrapperdefs import API_PREFIX_UNMARSHAL |
| 19 |
from .wrapperdefs import ROOT_TYPE_DEFAULT_VALUE |
| 20 |
from .wrapperdefs import VULKAN_STREAM_TYPE_GUEST |
| 21 |
|
| 22 |
encoder_decl_preamble = """ |
| 23 |
|
| 24 |
class VkEncoder { |
| 25 |
public: |
| 26 |
VkEncoder(gfxstream::guest::IOStream* stream); |
| 27 |
~VkEncoder(); |
| 28 |
|
| 29 |
#include "VkEncoder.h.inl" |
| 30 |
""" |
| 31 |
|
| 32 |
encoder_decl_postamble = """ |
| 33 |
private: |
| 34 |
class Impl; |
| 35 |
std::unique_ptr<Impl> mImpl; |
| 36 |
}; |
| 37 |
""" |
| 38 |
|
| 39 |
encoder_impl_preamble =""" |
| 40 |
|
| 41 |
using namespace gfxstream::vk; |
| 42 |
|
| 43 |
using gfxstream::aemu::BumpPool; |
| 44 |
|
| 45 |
#include "VkEncoder.cpp.inl" |
| 46 |
|
| 47 |
#define VALIDATE_RET(retType, success, validate) \\ |
| 48 |
retType goldfish_vk_validateResult = validate; \\ |
| 49 |
if (goldfish_vk_validateResult != success) return goldfish_vk_validateResult; \\ |
| 50 |
|
| 51 |
#define VALIDATE_VOID(validate) \\ |
| 52 |
VkResult goldfish_vk_validateResult = validate; \\ |
| 53 |
if (goldfish_vk_validateResult != VK_SUCCESS) return; \\ |
| 54 |
|
| 55 |
""" |
| 56 |
|
| 57 |
STREAM = "stream" |
| 58 |
RESOURCES = "sResourceTracker" |
| 59 |
POOL = "pool" |
| 60 |
|
| 61 |
ENCODER_PREVALIDATED_APIS = [ |
| 62 |
"vkFlushMappedMemoryRanges", |
| 63 |
"vkInvalidateMappedMemoryRanges", |
| 64 |
] |
| 65 |
|
| 66 |
ENCODER_CUSTOM_RESOURCE_PREPROCESS = [ |
| 67 |
"vkMapMemoryIntoAddressSpaceGOOGLE", |
| 68 |
"vkDestroyDevice", |
| 69 |
] |
| 70 |
|
| 71 |
ENCODER_CUSTOM_RESOURCE_POSTPROCESS = [ |
| 72 |
"vkCreateInstance", |
| 73 |
"vkCreateDevice", |
| 74 |
"vkMapMemoryIntoAddressSpaceGOOGLE", |
| 75 |
"vkGetPhysicalDeviceFeatures2", |
| 76 |
"vkCreateDescriptorUpdateTemplate", |
| 77 |
"vkGetPhysicalDeviceExternalSemaphoreProperties", |
| 78 |
] |
| 79 |
|
| 80 |
ENCODER_EXPLICIT_FLUSHED_APIS = [ |
| 81 |
"vkEndCommandBufferAsyncGOOGLE", |
| 82 |
"vkQueueSubmitAsyncGOOGLE", |
| 83 |
"vkQueueBindSparseAsyncGOOGLE", |
| 84 |
"vkQueueWaitIdleAsyncGOOGLE", |
| 85 |
"vkQueueSignalReleaseImageANDROID", |
| 86 |
"vkDestroyDevice", |
| 87 |
] |
| 88 |
|
| 89 |
SUCCESS_RET_TYPES = { |
| 90 |
"VkResult" : "VK_SUCCESS", |
| 91 |
"void" : None, |
| 92 |
# TODO: Put up success results for other return types here. |
| 93 |
} |
| 94 |
|
| 95 |
ENCODER_THIS_PARAM = makeVulkanTypeSimple(False, "VkEncoder", 1, "this") |
| 96 |
|
| 97 |
# Common components of encoding a Vulkan API call |
| 98 |
def make_event_handler_call( |
| 99 |
handler_access, |
| 100 |
api, |
| 101 |
context_param, |
| 102 |
input_result_param, |
| 103 |
cgen, |
| 104 |
suffix=""): |
| 105 |
extraParams = [context_param.paramName] |
| 106 |
if input_result_param: |
| 107 |
extraParams.append(input_result_param) |
| 108 |
return cgen.makeCallExpr( \ |
| 109 |
"%s->on_%s%s" % (handler_access, api.name, suffix), |
| 110 |
extraParams + \ |
| 111 |
[p.paramName for p in api.parameters[:-1]]) |
| 112 |
|
| 113 |
def emit_custom_pre_validate(typeInfo, api, cgen): |
| 114 |
if api.name in ENCODER_PREVALIDATED_APIS: |
| 115 |
callExpr = \ |
| 116 |
make_event_handler_call( \ |
| 117 |
"mImpl->validation()", api, |
| 118 |
ENCODER_THIS_PARAM, |
| 119 |
SUCCESS_RET_TYPES[api.getRetTypeExpr()], |
| 120 |
cgen) |
| 121 |
|
| 122 |
if api.getRetTypeExpr() == "void": |
| 123 |
cgen.stmt("VALIDATE_VOID(%s)" % callExpr) |
| 124 |
else: |
| 125 |
cgen.stmt("VALIDATE_RET(%s, %s, %s)" % \ |
| 126 |
(api.getRetTypeExpr(), |
| 127 |
SUCCESS_RET_TYPES[api.getRetTypeExpr()], |
| 128 |
callExpr)) |
| 129 |
|
| 130 |
def emit_custom_resource_preprocess(typeInfo, api, cgen): |
| 131 |
if api.name in ENCODER_CUSTOM_RESOURCE_PREPROCESS: |
| 132 |
cgen.stmt( \ |
| 133 |
make_event_handler_call( \ |
| 134 |
"sResourceTracker", api, |
| 135 |
ENCODER_THIS_PARAM, |
| 136 |
SUCCESS_RET_TYPES[api.getRetTypeExpr()], |
| 137 |
cgen, suffix="_pre")) |
| 138 |
|
| 139 |
def emit_custom_resource_postprocess(typeInfo, api, cgen): |
| 140 |
if api.name in ENCODER_CUSTOM_RESOURCE_POSTPROCESS: |
| 141 |
cgen.stmt(make_event_handler_call( \ |
| 142 |
"sResourceTracker", |
| 143 |
api, |
| 144 |
ENCODER_THIS_PARAM, |
| 145 |
api.getRetVarExpr(), |
| 146 |
cgen)) |
| 147 |
|
| 148 |
def emit_count_marshal(typeInfo, param, cgen): |
| 149 |
res = \ |
| 150 |
iterateVulkanType( |
| 151 |
typeInfo, param, |
| 152 |
VulkanCountingCodegen( \ |
| 153 |
cgen, "sFeatureBits", param.paramName, "countPtr", ROOT_TYPE_DEFAULT_VALUE, |
| 154 |
"count_")) |
| 155 |
if not res: |
| 156 |
cgen.stmt("(void)%s" % param.paramName) |
| 157 |
|
| 158 |
def emit_marshal(typeInfo, param, cgen): |
| 159 |
forOutput = param.isHandleType() and ("out" in param.inout) |
| 160 |
if forOutput: |
| 161 |
cgen.stmt("/* is handle, possibly out */") |
| 162 |
|
| 163 |
res = \ |
| 164 |
iterateVulkanType( |
| 165 |
typeInfo, param, |
| 166 |
VulkanReservedMarshalingCodegen( \ |
| 167 |
cgen, "guest", STREAM, ROOT_TYPE_DEFAULT_VALUE, param.paramName, "streamPtrPtr", |
| 168 |
API_PREFIX_RESERVEDMARSHAL, |
| 169 |
"" if forOutput else "get_host_u64_", |
| 170 |
direction="write")) |
| 171 |
if not res: |
| 172 |
cgen.stmt("(void)%s" % param.paramName) |
| 173 |
|
| 174 |
if forOutput: |
| 175 |
cgen.stmt("/* is handle, possibly out */") |
| 176 |
|
| 177 |
def emit_unmarshal(typeInfo, param, cgen): |
| 178 |
iterateVulkanType( |
| 179 |
typeInfo, param, |
| 180 |
VulkanMarshalingCodegen( \ |
| 181 |
cgen, STREAM, ROOT_TYPE_DEFAULT_VALUE, param.paramName, |
| 182 |
API_PREFIX_UNMARSHAL, direction="read")) |
| 183 |
|
| 184 |
def emit_deepcopy(typeInfo, param, cgen): |
| 185 |
res = \ |
| 186 |
iterateVulkanType(typeInfo, param, DeepcopyCodegen( |
| 187 |
cgen, [param.paramName, "local_" + param.paramName], "pool", ROOT_TYPE_DEFAULT_VALUE, "deepcopy_")) |
| 188 |
if not res: |
| 189 |
cgen.stmt("(void)%s" % param.paramName) |
| 190 |
|
| 191 |
def emit_transform(typeInfo, param, cgen, variant="tohost"): |
| 192 |
res = \ |
| 193 |
iterateVulkanType(typeInfo, param, TransformCodegen( \ |
| 194 |
cgen, param.paramName, "sResourceTracker", "transform_%s_" % variant, variant)) |
| 195 |
if not res: |
| 196 |
cgen.stmt("(void)%s" % param.paramName) |
| 197 |
|
| 198 |
def emit_handlemap_create(typeInfo, param, cgen): |
| 199 |
iterateVulkanType(typeInfo, param, HandleMapCodegen( |
| 200 |
cgen, None, "sResourceTracker", "handlemap_", |
| 201 |
lambda vtype: typeInfo.isHandleType(vtype.typeName) |
| 202 |
)) |
| 203 |
|
| 204 |
def custom_encoder_args(api): |
| 205 |
params = ["this"] |
| 206 |
if api.getRetVarExpr() is not None: |
| 207 |
params.append(api.getRetVarExpr()) |
| 208 |
return params |
| 209 |
|
| 210 |
def emit_handlemap_destroy(typeInfo, param, cgen): |
| 211 |
iterateVulkanType(typeInfo, param, HandleMapCodegen( |
| 212 |
cgen, None, "sResourceTracker->destroyMapping()", "handlemap_", |
| 213 |
lambda vtype: typeInfo.isHandleType(vtype.typeName) |
| 214 |
)) |
| 215 |
|
| 216 |
class EncodingParameters(object): |
| 217 |
def __init__(self, api): |
| 218 |
self.localCopied = [] |
| 219 |
self.toWrite = [] |
| 220 |
self.toRead = [] |
| 221 |
self.toCreate = [] |
| 222 |
self.toDestroy = [] |
| 223 |
|
| 224 |
for param in api.parameters: |
| 225 |
param.action = None |
| 226 |
param.inout = "in" |
| 227 |
|
| 228 |
if param.paramName == "doLock": |
| 229 |
continue |
| 230 |
|
| 231 |
if param.possiblyOutput(): |
| 232 |
param.inout += "out" |
| 233 |
self.toWrite.append(param) |
| 234 |
self.toRead.append(param) |
| 235 |
if param.isCreatedBy(api): |
| 236 |
self.toCreate.append(param) |
| 237 |
param.action = "create" |
| 238 |
else: |
| 239 |
|
| 240 |
if param.paramName == "doLock": |
| 241 |
continue |
| 242 |
|
| 243 |
if param.isDestroyedBy(api): |
| 244 |
self.toDestroy.append(param) |
| 245 |
param.action = "destroy" |
| 246 |
localCopyParam = \ |
| 247 |
param.getForNonConstAccess().withModifiedName( \ |
| 248 |
"local_" + param.paramName) |
| 249 |
self.localCopied.append((param, localCopyParam)) |
| 250 |
self.toWrite.append(localCopyParam) |
| 251 |
|
| 252 |
def emit_parameter_encode_preamble_write(typeInfo, api, cgen): |
| 253 |
emit_custom_pre_validate(typeInfo, api, cgen); |
| 254 |
emit_custom_resource_preprocess(typeInfo, api, cgen); |
| 255 |
|
| 256 |
cgen.stmt("auto %s = mImpl->stream()" % STREAM) |
| 257 |
cgen.stmt("auto %s = mImpl->pool()" % POOL) |
| 258 |
# cgen.stmt("%s->setHandleMapping(%s->unwrapMapping())" % (STREAM, RESOURCES)) |
| 259 |
|
| 260 |
encodingParams = EncodingParameters(api) |
| 261 |
for (_, localCopyParam) in encodingParams.localCopied: |
| 262 |
cgen.stmt(cgen.makeRichCTypeDecl(localCopyParam)) |
| 263 |
|
| 264 |
def emit_parameter_encode_copy_unwrap_count(typeInfo, api, cgen, customUnwrap=None): |
| 265 |
encodingParams = EncodingParameters(api) |
| 266 |
|
| 267 |
for (origParam, localCopyParam) in encodingParams.localCopied: |
| 268 |
shouldCustomCopy = \ |
| 269 |
customUnwrap and \ |
| 270 |
origParam.paramName in customUnwrap and \ |
| 271 |
"copyOp" in customUnwrap[origParam.paramName] |
| 272 |
|
| 273 |
shouldCustomMap = \ |
| 274 |
customUnwrap and \ |
| 275 |
origParam.paramName in customUnwrap and \ |
| 276 |
"mapOp" in customUnwrap[origParam.paramName] |
| 277 |
|
| 278 |
if shouldCustomCopy: |
| 279 |
customUnwrap[origParam.paramName]["copyOp"](cgen, origParam, localCopyParam) |
| 280 |
else: |
| 281 |
# if this is a pointer type and we don't do custom copy nor unwrap, |
| 282 |
# and the transform doesn't end up doing anything, |
| 283 |
# don't deepcopy, just cast it. |
| 284 |
|
| 285 |
avoidDeepcopy = False |
| 286 |
|
| 287 |
if origParam.pointerIndirectionLevels > 0: |
| 288 |
testCgen = CodeGen() |
| 289 |
genTransformsForVulkanType("sResourceTracker", origParam, lambda p: testCgen.generalAccess(p, parentVarName = None, asPtr = True), lambda p: testCgen.generalLengthAccess(p, parentVarName = None), testCgen) |
| 290 |
emit_transform(typeInfo, origParam, testCgen, variant="tohost") |
| 291 |
if "" == testCgen.swapCode(): |
| 292 |
avoidDeepcopy = True |
| 293 |
if avoidDeepcopy: |
| 294 |
cgen.line("// Avoiding deepcopy for %s" % origParam.paramName) |
| 295 |
cgen.stmt("%s = (%s%s)%s" % (localCopyParam.paramName, localCopyParam.typeName, "*" * origParam.pointerIndirectionLevels, origParam.paramName)) |
| 296 |
else: |
| 297 |
emit_deepcopy(typeInfo, origParam, cgen) |
| 298 |
|
| 299 |
for (origParam, localCopyParam) in encodingParams.localCopied: |
| 300 |
shouldCustomMap = \ |
| 301 |
customUnwrap and \ |
| 302 |
origParam.paramName in customUnwrap and \ |
| 303 |
"mapOp" in customUnwrap[origParam.paramName] |
| 304 |
|
| 305 |
if shouldCustomMap: |
| 306 |
customUnwrap[origParam.paramName]["mapOp"](cgen, origParam, localCopyParam) |
| 307 |
else: |
| 308 |
if localCopyParam.typeName == "VkAllocationCallbacks": |
| 309 |
cgen.stmt("%s = nullptr" % localCopyParam.paramName) |
| 310 |
|
| 311 |
apiForTransform = \ |
| 312 |
api.withCustomParameters( \ |
| 313 |
map(lambda p: p[1], \ |
| 314 |
encodingParams.localCopied)) |
| 315 |
|
| 316 |
# Apply transforms if applicable. |
| 317 |
# Apply transform to API itself: |
| 318 |
genTransformsForVulkanType( |
| 319 |
"sResourceTracker", |
| 320 |
apiForTransform, |
| 321 |
lambda p: cgen.generalAccess(p, parentVarName = None, asPtr = True), |
| 322 |
lambda p: cgen.generalLengthAccess(p, parentVarName = None), |
| 323 |
cgen) |
| 324 |
|
| 325 |
# For all local copied parameters, run the transforms |
| 326 |
for localParam in apiForTransform.parameters: |
| 327 |
if "doLock" in localParam.paramName: |
| 328 |
continue |
| 329 |
emit_transform(typeInfo, localParam, cgen, variant="tohost") |
| 330 |
|
| 331 |
cgen.stmt("size_t count = 0") |
| 332 |
cgen.stmt("size_t* countPtr = &count") |
| 333 |
cgen.beginBlock() |
| 334 |
|
| 335 |
# Use counting stream to calculate the packet size. |
| 336 |
for p in encodingParams.toWrite: |
| 337 |
emit_count_marshal(typeInfo, p, cgen) |
| 338 |
|
| 339 |
cgen.endBlock() |
| 340 |
|
| 341 |
def is_cmdbuf_dispatch(api): |
| 342 |
return "VkCommandBuffer" == api.parameters[0].typeName |
| 343 |
|
| 344 |
def emit_parameter_encode_write_packet_info(typeInfo, api, cgen): |
| 345 |
# Seqno and skipping dispatch serialize are for use with VULKAN_STREAM_FEATURE_QUEUE_SUBMIT_WITH_COMMANDS_BIT |
| 346 |
doSeqno = True |
| 347 |
doDispatchSerialize = True |
| 348 |
|
| 349 |
if is_cmdbuf_dispatch(api): |
| 350 |
doSeqno = False |
| 351 |
doDispatchSerialize = False |
| 352 |
|
| 353 |
if doSeqno: |
| 354 |
cgen.stmt("uint32_t packetSize_%s = 4 + 4 + (queueSubmitWithCommandsEnabled ? 4 : 0) + count" % (api.name)) |
| 355 |
else: |
| 356 |
cgen.stmt("uint32_t packetSize_%s = 4 + 4 + count" % (api.name)) |
| 357 |
|
| 358 |
if not doDispatchSerialize: |
| 359 |
cgen.stmt("if (queueSubmitWithCommandsEnabled) packetSize_%s -= 8" % api.name) |
| 360 |
|
| 361 |
cgen.stmt("uint8_t* streamPtr = %s->reserve(packetSize_%s)" % (STREAM, api.name)) |
| 362 |
cgen.stmt("uint8_t** streamPtrPtr = &streamPtr") |
| 363 |
cgen.stmt("uint32_t opcode_%s = OP_%s" % (api.name, api.name)) |
| 364 |
|
| 365 |
if doSeqno: |
| 366 |
cgen.stmt("uint32_t seqno; if (queueSubmitWithCommandsEnabled) seqno = ResourceTracker::nextSeqno()") |
| 367 |
|
| 368 |
cgen.stmt("memcpy(streamPtr, &opcode_%s, sizeof(uint32_t)); streamPtr += sizeof(uint32_t)" % api.name) |
| 369 |
cgen.stmt("memcpy(streamPtr, &packetSize_%s, sizeof(uint32_t)); streamPtr += sizeof(uint32_t)" % api.name) |
| 370 |
|
| 371 |
if doSeqno: |
| 372 |
cgen.line("if (queueSubmitWithCommandsEnabled) { memcpy(streamPtr, &seqno, sizeof(uint32_t)); streamPtr += sizeof(uint32_t); }") |
| 373 |
|
| 374 |
def emit_parameter_encode_do_parameter_write(typeInfo, api, cgen): |
| 375 |
encodingParams = EncodingParameters(api) |
| 376 |
|
| 377 |
dispatchDone = False |
| 378 |
|
| 379 |
for p in encodingParams.toWrite: |
| 380 |
if is_cmdbuf_dispatch(api) and not dispatchDone: |
| 381 |
cgen.beginIf("!queueSubmitWithCommandsEnabled") |
| 382 |
emit_marshal(typeInfo, p, cgen) |
| 383 |
cgen.endIf() |
| 384 |
else: |
| 385 |
emit_marshal(typeInfo, p, cgen) |
| 386 |
|
| 387 |
dispatchDone = True |
| 388 |
|
| 389 |
def emit_parameter_encode_read(typeInfo, api, cgen): |
| 390 |
encodingParams = EncodingParameters(api) |
| 391 |
|
| 392 |
for p in encodingParams.toRead: |
| 393 |
if p.action == "create": |
| 394 |
cgen.stmt( |
| 395 |
"%s->setHandleMapping(%s->createMapping())" % \ |
| 396 |
(STREAM, RESOURCES)) |
| 397 |
emit_unmarshal(typeInfo, p, cgen) |
| 398 |
if p.action == "create": |
| 399 |
cgen.stmt( |
| 400 |
"%s->unsetHandleMapping()" % STREAM) |
| 401 |
emit_transform(typeInfo, p, cgen, variant="fromhost") |
| 402 |
|
| 403 |
def emit_post(typeInfo, api, cgen): |
| 404 |
encodingParams = EncodingParameters(api) |
| 405 |
|
| 406 |
emit_custom_resource_postprocess(typeInfo, api, cgen) |
| 407 |
|
| 408 |
for p in encodingParams.toDestroy: |
| 409 |
emit_handlemap_destroy(typeInfo, p, cgen) |
| 410 |
|
| 411 |
doSeqno = True |
| 412 |
if is_cmdbuf_dispatch(api): |
| 413 |
doSeqno = False |
| 414 |
|
| 415 |
retType = api.getRetTypeExpr() |
| 416 |
|
| 417 |
if api.name in ENCODER_EXPLICIT_FLUSHED_APIS: |
| 418 |
cgen.stmt("stream->flush()"); |
| 419 |
return |
| 420 |
|
| 421 |
if doSeqno: |
| 422 |
if retType == "void": |
| 423 |
encodingParams = EncodingParameters(api) |
| 424 |
if 0 == len(encodingParams.toRead): |
| 425 |
cgen.stmt("stream->flush()"); |
| 426 |
|
| 427 |
def emit_pool_free(cgen): |
| 428 |
cgen.stmt("++encodeCount") |
| 429 |
cgen.beginIf("0 == encodeCount % POOL_CLEAR_INTERVAL") |
| 430 |
cgen.stmt("pool->freeAll()") |
| 431 |
cgen.stmt("%s->clearPool()" % STREAM) |
| 432 |
cgen.endIf() |
| 433 |
|
| 434 |
def emit_return_unmarshal(typeInfo, api, cgen): |
| 435 |
|
| 436 |
retType = api.getRetTypeExpr() |
| 437 |
|
| 438 |
if retType == "void": |
| 439 |
return |
| 440 |
|
| 441 |
retVar = api.getRetVarExpr() |
| 442 |
cgen.stmt("%s %s = (%s)0" % (retType, retVar, retType)) |
| 443 |
cgen.stmt("%s->read(&%s, %s)" % \ |
| 444 |
(STREAM, retVar, cgen.sizeofExpr(api.retType))) |
| 445 |
|
| 446 |
def emit_return(typeInfo, api, cgen): |
| 447 |
if api.getRetTypeExpr() == "void": |
| 448 |
return |
| 449 |
|
| 450 |
retVar = api.getRetVarExpr() |
| 451 |
cgen.stmt("return %s" % retVar) |
| 452 |
|
| 453 |
def emit_lock(cgen): |
| 454 |
cgen.stmt("(void)doLock"); |
| 455 |
cgen.stmt("bool queueSubmitWithCommandsEnabled = sFeatureBits & VULKAN_STREAM_FEATURE_QUEUE_SUBMIT_WITH_COMMANDS_BIT") |
| 456 |
cgen.stmt("if (!queueSubmitWithCommandsEnabled && doLock) this->lock()") |
| 457 |
|
| 458 |
def emit_unlock(cgen): |
| 459 |
cgen.stmt("if (!queueSubmitWithCommandsEnabled && doLock) this->unlock()") |
| 460 |
|
| 461 |
def emit_debug_log(typeInfo, api, cgen): |
| 462 |
logFormat = [] |
| 463 |
logVargs = [] |
| 464 |
for param in api.parameters: |
| 465 |
if param.paramName == "doLock": |
| 466 |
continue |
| 467 |
|
| 468 |
paramFormatSpecifier = param.getPrintFormatSpecifier() |
| 469 |
if not paramFormatSpecifier: |
| 470 |
continue |
| 471 |
|
| 472 |
logFormat.append(param.paramName + ":" + paramFormatSpecifier) |
| 473 |
logVargs.append(param.paramName) |
| 474 |
|
| 475 |
logFormatStr = ", ".join(logFormat) |
| 476 |
logVargsStr = ", ".join(logVargs) |
| 477 |
|
| 478 |
def emit_default_encoding(typeInfo, api, cgen): |
| 479 |
emit_debug_log(typeInfo, api, cgen) |
| 480 |
emit_lock(cgen) |
| 481 |
emit_parameter_encode_preamble_write(typeInfo, api, cgen) |
| 482 |
emit_parameter_encode_copy_unwrap_count(typeInfo, api, cgen) |
| 483 |
emit_parameter_encode_write_packet_info(typeInfo, api, cgen) |
| 484 |
emit_parameter_encode_do_parameter_write(typeInfo, api, cgen) |
| 485 |
emit_parameter_encode_read(typeInfo, api, cgen) |
| 486 |
emit_return_unmarshal(typeInfo, api, cgen) |
| 487 |
emit_post(typeInfo, api, cgen) |
| 488 |
emit_pool_free(cgen) |
| 489 |
emit_unlock(cgen) |
| 490 |
emit_return(typeInfo, api, cgen) |
| 491 |
|
| 492 |
## Custom encoding definitions################################################## |
| 493 |
|
| 494 |
def emit_only_goldfish_custom(typeInfo, api, cgen): |
| 495 |
emit_lock(cgen) |
| 496 |
cgen.vkApiCall( \ |
| 497 |
api, |
| 498 |
customPrefix="sResourceTracker->on_", |
| 499 |
customParameters=custom_encoder_args(api) + \ |
| 500 |
[p.paramName for p in api.parameters[:-1]]) |
| 501 |
emit_unlock(cgen) |
| 502 |
emit_return(typeInfo, api, cgen) |
| 503 |
|
| 504 |
def emit_only_resource_event(typeInfo, api, cgen): |
| 505 |
cgen.stmt("(void)doLock"); |
| 506 |
input_result = None |
| 507 |
retExpr = api.getRetVarExpr() |
| 508 |
|
| 509 |
if retExpr: |
| 510 |
retType = api.getRetTypeExpr() |
| 511 |
input_result = SUCCESS_RET_TYPES[retType] |
| 512 |
cgen.stmt("%s %s = (%s)0" % (retType, retExpr, retType)) |
| 513 |
|
| 514 |
cgen.stmt( |
| 515 |
(("%s = " % retExpr) if retExpr else "") + |
| 516 |
make_event_handler_call( |
| 517 |
"sResourceTracker", |
| 518 |
api, |
| 519 |
ENCODER_THIS_PARAM, |
| 520 |
input_result, cgen)) |
| 521 |
|
| 522 |
if retExpr: |
| 523 |
emit_return(typeInfo, api, cgen) |
| 524 |
|
| 525 |
def emit_with_custom_unwrap(custom): |
| 526 |
def call(typeInfo, api, cgen): |
| 527 |
emit_lock(cgen) |
| 528 |
emit_parameter_encode_preamble_write(typeInfo, api, cgen) |
| 529 |
emit_parameter_encode_copy_unwrap_count( |
| 530 |
typeInfo, api, cgen, customUnwrap=custom) |
| 531 |
emit_parameter_encode_write_packet_info(typeInfo, api, cgen) |
| 532 |
emit_parameter_encode_do_parameter_write(typeInfo, api, cgen) |
| 533 |
emit_parameter_encode_read(typeInfo, api, cgen) |
| 534 |
emit_return_unmarshal(typeInfo, api, cgen) |
| 535 |
emit_pool_free(cgen) |
| 536 |
emit_unlock(cgen) |
| 537 |
emit_return(typeInfo, api, cgen) |
| 538 |
return call |
| 539 |
|
| 540 |
def encode_vkFlushMappedMemoryRanges(typeInfo, api, cgen): |
| 541 |
emit_lock(cgen) |
| 542 |
emit_parameter_encode_preamble_write(typeInfo, api, cgen) |
| 543 |
emit_parameter_encode_copy_unwrap_count(typeInfo, api, cgen) |
| 544 |
|
| 545 |
def emit_flush_ranges(streamVar): |
| 546 |
cgen.beginIf("!sResourceTracker->usingDirectMapping()") |
| 547 |
cgen.beginFor("uint32_t i = 0", "i < memoryRangeCount", "++i") |
| 548 |
cgen.stmt("auto range = pMemoryRanges[i]") |
| 549 |
cgen.stmt("auto memory = range.memory") |
| 550 |
cgen.stmt("auto size = range.size") |
| 551 |
cgen.stmt("auto offset = range.offset") |
| 552 |
cgen.stmt("uint64_t streamSize = 0") |
| 553 |
cgen.stmt("if (!memory) { %s->write(&streamSize, sizeof(uint64_t)); continue; }" % streamVar) |
| 554 |
cgen.stmt("auto hostPtr = sResourceTracker->getMappedPointer(memory)") |
| 555 |
cgen.stmt("auto actualSize = size == VK_WHOLE_SIZE ? sResourceTracker->getMappedSize(memory) : size") |
| 556 |
cgen.stmt("if (!hostPtr) { %s->write(&streamSize, sizeof(uint64_t)); continue; }" % streamVar) |
| 557 |
cgen.stmt("streamSize = actualSize") |
| 558 |
cgen.stmt("%s->write(&streamSize, sizeof(uint64_t))" % streamVar) |
| 559 |
cgen.stmt("uint8_t* targetRange = hostPtr + offset") |
| 560 |
cgen.stmt("%s->write(targetRange, actualSize)" % streamVar) |
| 561 |
cgen.endFor() |
| 562 |
cgen.endIf() |
| 563 |
|
| 564 |
emit_parameter_encode_write_packet_info(typeInfo, api, cgen) |
| 565 |
emit_parameter_encode_do_parameter_write(typeInfo, api, cgen) |
| 566 |
|
| 567 |
emit_flush_ranges(STREAM) |
| 568 |
|
| 569 |
emit_parameter_encode_read(typeInfo, api, cgen) |
| 570 |
emit_return_unmarshal(typeInfo, api, cgen) |
| 571 |
emit_pool_free(cgen) |
| 572 |
emit_unlock(cgen) |
| 573 |
emit_return(typeInfo, api, cgen) |
| 574 |
|
| 575 |
def encode_vkInvalidateMappedMemoryRanges(typeInfo, api, cgen): |
| 576 |
emit_lock(cgen) |
| 577 |
emit_parameter_encode_preamble_write(typeInfo, api, cgen) |
| 578 |
emit_parameter_encode_copy_unwrap_count(typeInfo, api, cgen) |
| 579 |
emit_parameter_encode_write_packet_info(typeInfo, api, cgen) |
| 580 |
emit_parameter_encode_do_parameter_write(typeInfo, api, cgen) |
| 581 |
emit_parameter_encode_read(typeInfo, api, cgen) |
| 582 |
emit_return_unmarshal(typeInfo, api, cgen) |
| 583 |
|
| 584 |
def emit_invalidate_ranges(streamVar): |
| 585 |
cgen.beginIf("!sResourceTracker->usingDirectMapping()") |
| 586 |
cgen.beginFor("uint32_t i = 0", "i < memoryRangeCount", "++i") |
| 587 |
cgen.stmt("auto range = pMemoryRanges[i]") |
| 588 |
cgen.stmt("auto memory = range.memory") |
| 589 |
cgen.stmt("auto size = range.size") |
| 590 |
cgen.stmt("auto offset = range.offset") |
| 591 |
cgen.stmt("uint64_t streamSize = 0") |
| 592 |
cgen.stmt("if (!memory) { %s->read(&streamSize, sizeof(uint64_t)); continue; }" % streamVar) |
| 593 |
cgen.stmt("auto hostPtr = sResourceTracker->getMappedPointer(memory)") |
| 594 |
cgen.stmt("auto actualSize = size == VK_WHOLE_SIZE ? sResourceTracker->getMappedSize(memory) : size") |
| 595 |
cgen.stmt("if (!hostPtr) { %s->read(&streamSize, sizeof(uint64_t)); continue; }" % streamVar) |
| 596 |
cgen.stmt("streamSize = actualSize") |
| 597 |
cgen.stmt("%s->read(&streamSize, sizeof(uint64_t))" % streamVar) |
| 598 |
cgen.stmt("uint8_t* targetRange = hostPtr + offset") |
| 599 |
cgen.stmt("%s->read(targetRange, actualSize)" % streamVar) |
| 600 |
cgen.endFor() |
| 601 |
cgen.endIf() |
| 602 |
|
| 603 |
emit_invalidate_ranges(STREAM) |
| 604 |
emit_pool_free(cgen) |
| 605 |
emit_unlock(cgen) |
| 606 |
emit_return(typeInfo, api, cgen) |
| 607 |
|
| 608 |
def emit_manual_inline(typeInfo, api, cgen): |
| 609 |
cgen.line("#include \"%s_encode_impl.cpp.inl\"" % api.name) |
| 610 |
|
| 611 |
def unwrap_vkCreateImage_pCreateInfo(): |
| 612 |
def mapOp(cgen, orig, local): |
| 613 |
cgen.stmt("sResourceTracker->unwrap_vkCreateImage_pCreateInfo(%s, %s)" % |
| 614 |
(orig.paramName, local.paramName)) |
| 615 |
return { "pCreateInfo" : { "mapOp" : mapOp } } |
| 616 |
|
| 617 |
def unwrap_vkBindImageMemory2_pBindInfos(): |
| 618 |
def mapOp(cgen, orig, local): |
| 619 |
cgen.stmt("sResourceTracker->unwrap_VkBindImageMemory2_pBindInfos(bindInfoCount, %s, %s)" % |
| 620 |
(orig.paramName, local.paramName)) |
| 621 |
return { "pBindInfos" : { "mapOp" : mapOp } } |
| 622 |
|
| 623 |
def unwrap_vkAcquireImageANDROID_nativeFenceFd(): |
| 624 |
def mapOp(cgen, orig, local): |
| 625 |
cgen.stmt("sResourceTracker->unwrap_vkAcquireImageANDROID_nativeFenceFd(%s, &%s)" % |
| 626 |
(orig.paramName, local.paramName)) |
| 627 |
return { "nativeFenceFd" : { "mapOp" : mapOp } } |
| 628 |
|
| 629 |
custom_encodes = { |
| 630 |
"vkMapMemory" : emit_only_resource_event, |
| 631 |
"vkUnmapMemory" : emit_only_resource_event, |
| 632 |
"vkFlushMappedMemoryRanges" : encode_vkFlushMappedMemoryRanges, |
| 633 |
"vkInvalidateMappedMemoryRanges" : encode_vkInvalidateMappedMemoryRanges, |
| 634 |
"vkCreateImage" : emit_with_custom_unwrap(unwrap_vkCreateImage_pCreateInfo()), |
| 635 |
"vkCreateImageWithRequirementsGOOGLE" : emit_with_custom_unwrap(unwrap_vkCreateImage_pCreateInfo()), |
| 636 |
"vkBindImageMemory2": emit_with_custom_unwrap(unwrap_vkBindImageMemory2_pBindInfos()), |
| 637 |
"vkAcquireImageANDROID" : emit_with_custom_unwrap(unwrap_vkAcquireImageANDROID_nativeFenceFd()), |
| 638 |
"vkQueueFlushCommandsGOOGLE" : emit_manual_inline, |
| 639 |
} |
| 640 |
|
| 641 |
class VulkanEncoder(VulkanWrapperGenerator): |
| 642 |
def __init__(self, module, typeInfo): |
| 643 |
VulkanWrapperGenerator.__init__(self, module, typeInfo) |
| 644 |
|
| 645 |
self.typeInfo = typeInfo |
| 646 |
|
| 647 |
self.cgenHeader = CodeGen() |
| 648 |
self.cgenHeader.incrIndent() |
| 649 |
|
| 650 |
self.cgenImpl = CodeGen() |
| 651 |
|
| 652 |
def onBegin(self,): |
| 653 |
self.module.appendHeader(encoder_decl_preamble) |
| 654 |
self.module.appendImpl(encoder_impl_preamble) |
| 655 |
|
| 656 |
def onGenCmd(self, cmdinfo, name, alias): |
| 657 |
VulkanWrapperGenerator.onGenCmd(self, cmdinfo, name, alias) |
| 658 |
|
| 659 |
api = copy.deepcopy(self.typeInfo.apis[name]) |
| 660 |
api.parameters.append(makeVulkanTypeSimple(False, "uint32_t", 0, "doLock")) |
| 661 |
|
| 662 |
self.cgenHeader.stmt(self.cgenHeader.makeFuncProto(api)) |
| 663 |
apiImpl = api.withModifiedName("VkEncoder::" + api.name) |
| 664 |
|
| 665 |
self.module.appendHeader(self.cgenHeader.swapCode()) |
| 666 |
|
| 667 |
def emit_function_impl(cgen): |
| 668 |
if api.name in custom_encodes.keys(): |
| 669 |
custom_encodes[api.name](self.typeInfo, api, cgen) |
| 670 |
else: |
| 671 |
emit_default_encoding(self.typeInfo, api, cgen) |
| 672 |
|
| 673 |
self.module.appendImpl(self.cgenImpl.makeFuncImpl(apiImpl, emit_function_impl)) |
| 674 |
|
| 675 |
def onEnd(self,): |
| 676 |
self.module.appendHeader(encoder_decl_postamble) |
| 677 |
self.cgenHeader.decrIndent() |