| 1 |
# Copyright 2023 Google LLC |
| 2 |
# SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
from .common.codegen import CodeGen, VulkanWrapperGenerator |
| 5 |
from .common.vulkantypes import VulkanAPI, makeVulkanTypeSimple, iterateVulkanType, VulkanTypeInfo,\ |
| 6 |
VulkanType |
| 7 |
|
| 8 |
from .marshaling import VulkanMarshalingCodegen |
| 9 |
from .reservedmarshaling import VulkanReservedMarshalingCodegen |
| 10 |
from .transform import TransformCodegen |
| 11 |
|
| 12 |
from .wrapperdefs import API_PREFIX_MARSHAL |
| 13 |
from .wrapperdefs import API_PREFIX_RESERVEDUNMARSHAL |
| 14 |
from .wrapperdefs import MAX_PACKET_LENGTH |
| 15 |
from .wrapperdefs import VULKAN_STREAM_TYPE |
| 16 |
from .wrapperdefs import ROOT_TYPE_DEFAULT_VALUE |
| 17 |
from .wrapperdefs import RELAXED_APIS |
| 18 |
|
| 19 |
|
| 20 |
SKIPPED_DECODER_DELETES = [ |
| 21 |
"vkFreeDescriptorSets", |
| 22 |
] |
| 23 |
|
| 24 |
DELAYED_DECODER_DELETES = [ |
| 25 |
"vkDestroyPipelineLayout", |
| 26 |
] |
| 27 |
|
| 28 |
DELAYED_DECODER_DELETE_DICT_ENTRIES = [ |
| 29 |
"vkDestroyShaderModule", |
| 30 |
] |
| 31 |
|
| 32 |
GLOBAL_COMMANDS_WITHOUT_DISPATCH = [ |
| 33 |
"vkCreateInstance", |
| 34 |
"vkEnumerateInstanceVersion", |
| 35 |
"vkEnumerateInstanceExtensionProperties", |
| 36 |
"vkEnumerateInstanceLayerProperties", |
| 37 |
"vkTraceAsyncGOOGLE", |
| 38 |
"vkSetDebugMetadataAsyncGOOGLE", |
| 39 |
] |
| 40 |
|
| 41 |
COMMANDS_WITHOUT_TRACE = [ |
| 42 |
# This command is used to set perfetto track names (using the guest process and thread name) |
| 43 |
# and track names should (ideally) be set before any trace events |
| 44 |
'vkSetDebugMetadataAsyncGOOGLE', |
| 45 |
] |
| 46 |
|
| 47 |
SNAPSHOT_API_CALL_HANDLE_VARNAME = "snapshotApiCallHandle" |
| 48 |
|
| 49 |
global_state_prefix = "m_state->on_" |
| 50 |
|
| 51 |
decoder_decl_preamble = """ |
| 52 |
|
| 53 |
namespace gfxstream { |
| 54 |
class IOStream; |
| 55 |
} // namespace gfxstream |
| 56 |
|
| 57 |
namespace gfxstream { |
| 58 |
namespace host { |
| 59 |
namespace vk { |
| 60 |
|
| 61 |
class VkDecoder { |
| 62 |
public: |
| 63 |
VkDecoder(); |
| 64 |
~VkDecoder(); |
| 65 |
void setForSnapshotLoad(bool forSnapshotLoad); |
| 66 |
size_t decode(void* buf, size_t bufsize, IOStream* stream, |
| 67 |
const ProcessResources* processResources, const VkDecoderContext&); |
| 68 |
private: |
| 69 |
class Impl; |
| 70 |
std::unique_ptr<Impl> mImpl; |
| 71 |
}; |
| 72 |
|
| 73 |
} // namespace vk |
| 74 |
} // namespace host |
| 75 |
} // namespace gfxstream |
| 76 |
|
| 77 |
""" |
| 78 |
|
| 79 |
decoder_impl_preamble =""" |
| 80 |
namespace gfxstream { |
| 81 |
namespace host { |
| 82 |
namespace vk { |
| 83 |
|
| 84 |
class VkDecoder::Impl { |
| 85 |
public: |
| 86 |
Impl() : m_logCalls(gfxstream::base::getEnvironmentVariable("ANDROID_EMU_VK_LOG_CALLS") == "1"), |
| 87 |
m_vk(vkDispatch()), |
| 88 |
m_state(VkDecoderGlobalState::get()), |
| 89 |
m_vkStream(nullptr, m_state->getFeatures()), |
| 90 |
m_vkMemReadingStream(nullptr, m_state->getFeatures()), |
| 91 |
m_boxedHandleCreateMapping(m_state), |
| 92 |
m_boxedHandleUnwrapMapping(m_state), |
| 93 |
m_prevSeqno(std::nullopt), |
| 94 |
m_queueSubmitWithCommandsEnabled(m_state->getFeatures().VulkanQueueSubmitWithCommands.enabled), |
| 95 |
m_snapshotsEnabled(m_state->snapshotsEnabled()) {} |
| 96 |
%s* stream() { return &m_vkStream; } |
| 97 |
VulkanMemReadingStream* readStream() { return &m_vkMemReadingStream; } |
| 98 |
|
| 99 |
void setForSnapshotLoad(bool forSnapshotLoad) { |
| 100 |
m_forSnapshotLoad = forSnapshotLoad; |
| 101 |
} |
| 102 |
|
| 103 |
size_t decode(void* buf, size_t bufsize, IOStream* stream, |
| 104 |
const ProcessResources* processResources, const VkDecoderContext&); |
| 105 |
|
| 106 |
private: |
| 107 |
bool m_logCalls; |
| 108 |
bool m_forSnapshotLoad = false; |
| 109 |
VulkanDispatch* m_vk; |
| 110 |
VkDecoderGlobalState* m_state; |
| 111 |
%s m_vkStream; |
| 112 |
VulkanMemReadingStream m_vkMemReadingStream; |
| 113 |
BoxedHandleCreateMapping m_boxedHandleCreateMapping; |
| 114 |
BoxedHandleUnwrapMapping m_boxedHandleUnwrapMapping; |
| 115 |
gfxstream::base::BumpPool m_pool; |
| 116 |
std::optional<uint32_t> m_prevSeqno; |
| 117 |
bool m_queueSubmitWithCommandsEnabled = false; |
| 118 |
const bool m_snapshotsEnabled = false; |
| 119 |
}; |
| 120 |
|
| 121 |
VkDecoder::VkDecoder() : |
| 122 |
mImpl(new VkDecoder::Impl()) { } |
| 123 |
|
| 124 |
VkDecoder::~VkDecoder() = default; |
| 125 |
|
| 126 |
void VkDecoder::setForSnapshotLoad(bool forSnapshotLoad) { |
| 127 |
mImpl->setForSnapshotLoad(forSnapshotLoad); |
| 128 |
} |
| 129 |
|
| 130 |
size_t VkDecoder::decode(void* buf, size_t bufsize, IOStream* stream, |
| 131 |
const ProcessResources* processResources, |
| 132 |
const VkDecoderContext& context) { |
| 133 |
return mImpl->decode(buf, bufsize, stream, processResources, context); |
| 134 |
} |
| 135 |
|
| 136 |
// VkDecoder::Impl::decode to follow |
| 137 |
""" % (VULKAN_STREAM_TYPE, VULKAN_STREAM_TYPE) |
| 138 |
|
| 139 |
decoder_impl_postamble = """ |
| 140 |
|
| 141 |
} // namespace vk |
| 142 |
} // namespace host |
| 143 |
} // namespace gfxstream |
| 144 |
|
| 145 |
""" |
| 146 |
|
| 147 |
READ_STREAM = "vkReadStream" |
| 148 |
WRITE_STREAM = "vkStream" |
| 149 |
|
| 150 |
# Driver workarounds for APIs that don't work well multithreaded |
| 151 |
driver_workarounds_global_lock_apis = [ \ |
| 152 |
"vkCreatePipelineLayout", |
| 153 |
"vkDestroyPipelineLayout", |
| 154 |
] |
| 155 |
|
| 156 |
def emit_param_decl_for_reading(param, cgen): |
| 157 |
if param.staticArrExpr: |
| 158 |
cgen.stmt( |
| 159 |
cgen.makeRichCTypeDecl(param.getForNonConstAccess())) |
| 160 |
else: |
| 161 |
cgen.stmt( |
| 162 |
cgen.makeRichCTypeDecl(param)) |
| 163 |
|
| 164 |
def emit_unmarshal(typeInfo, param, cgen, output = False, destroy = False, noUnbox = False): |
| 165 |
if destroy: |
| 166 |
iterateVulkanType(typeInfo, param, VulkanReservedMarshalingCodegen( |
| 167 |
cgen, |
| 168 |
"host", |
| 169 |
READ_STREAM, |
| 170 |
ROOT_TYPE_DEFAULT_VALUE, |
| 171 |
param.paramName, |
| 172 |
"readStreamPtrPtr", |
| 173 |
API_PREFIX_RESERVEDUNMARSHAL, |
| 174 |
"", |
| 175 |
direction="read", |
| 176 |
dynAlloc=True)) |
| 177 |
lenAccess = cgen.generalLengthAccess(param) |
| 178 |
lenAccessGuard = cgen.generalLengthAccessGuard(param) |
| 179 |
if None == lenAccess or "1" == lenAccess: |
| 180 |
cgen.stmt("boxed_%s_preserve = %s" % (param.paramName, param.paramName)) |
| 181 |
cgen.stmt("%s = try_unbox_%s(%s)" % (param.paramName, param.typeName, param.paramName)) |
| 182 |
else: |
| 183 |
if lenAccessGuard is not None: |
| 184 |
cgen.beginIf(lenAccessGuard) |
| 185 |
cgen.beginFor("uint32_t i = 0", "i < %s" % lenAccess, "++i") |
| 186 |
cgen.stmt("boxed_%s_preserve[i] = %s[i]" % (param.paramName, param.paramName)) |
| 187 |
cgen.stmt("((%s*)(%s))[i] = try_unbox_%s(%s[i])" % (param.typeName, param.paramName, param.typeName, param.paramName)) |
| 188 |
cgen.endFor() |
| 189 |
if lenAccessGuard is not None: |
| 190 |
cgen.endIf() |
| 191 |
else: |
| 192 |
if noUnbox: |
| 193 |
cgen.line("// No unbox for %s" % (param.paramName)) |
| 194 |
iterateVulkanType(typeInfo, param, VulkanReservedMarshalingCodegen( |
| 195 |
cgen, |
| 196 |
"host", |
| 197 |
READ_STREAM, |
| 198 |
ROOT_TYPE_DEFAULT_VALUE, |
| 199 |
param.paramName, |
| 200 |
"readStreamPtrPtr", |
| 201 |
API_PREFIX_RESERVEDUNMARSHAL, |
| 202 |
"" if (output or noUnbox) else "unbox_", |
| 203 |
direction="read", |
| 204 |
dynAlloc=True)) |
| 205 |
|
| 206 |
|
| 207 |
def emit_dispatch_unmarshal(typeInfo: VulkanTypeInfo, param: VulkanType, cgen, globalWrapped): |
| 208 |
cgen.stmt("// Begin {} wrapped dispatchable handle unboxing for {}".format( |
| 209 |
"global" if globalWrapped else "non", |
| 210 |
param.paramName)) |
| 211 |
|
| 212 |
iterateVulkanType(typeInfo, param, VulkanReservedMarshalingCodegen( |
| 213 |
cgen, |
| 214 |
"host", |
| 215 |
READ_STREAM, |
| 216 |
ROOT_TYPE_DEFAULT_VALUE, |
| 217 |
param.paramName, |
| 218 |
"readStreamPtrPtr", |
| 219 |
API_PREFIX_RESERVEDUNMARSHAL, |
| 220 |
"", |
| 221 |
direction="read", |
| 222 |
dynAlloc=True)) |
| 223 |
|
| 224 |
if not globalWrapped: |
| 225 |
cgen.stmt("auto unboxed_%s = unbox_%s(%s)" % |
| 226 |
(param.paramName, param.typeName, param.paramName)) |
| 227 |
cgen.stmt("auto vk = dispatch_%s(%s)" % |
| 228 |
(param.typeName, param.paramName)) |
| 229 |
cgen.stmt("// End manual dispatchable handle unboxing for %s" % param.paramName) |
| 230 |
else: |
| 231 |
# Still need to check dispatcher validity to handle threads with fatal errors |
| 232 |
cgen.stmt("auto vk = dispatch_%s(%s)" % |
| 233 |
(param.typeName, param.paramName)) |
| 234 |
|
| 235 |
|
| 236 |
def emit_transform(typeInfo, param, cgen, variant="tohost"): |
| 237 |
res = iterateVulkanType(typeInfo, param, TransformCodegen( |
| 238 |
cgen, param.paramName, "m_state", "transform_%s_" % variant, variant)) |
| 239 |
if not res: |
| 240 |
cgen.stmt("(void)%s" % param.paramName) |
| 241 |
|
| 242 |
|
| 243 |
def emit_marshal(typeInfo, param, cgen, handleMapOverwrites=False): |
| 244 |
iterateVulkanType(typeInfo, param, VulkanMarshalingCodegen( |
| 245 |
cgen, |
| 246 |
WRITE_STREAM, |
| 247 |
ROOT_TYPE_DEFAULT_VALUE, |
| 248 |
param.paramName, |
| 249 |
API_PREFIX_MARSHAL, |
| 250 |
direction="write", |
| 251 |
handleMapOverwrites=handleMapOverwrites)) |
| 252 |
|
| 253 |
|
| 254 |
class DecodingParameters(object): |
| 255 |
def __init__(self, api: VulkanAPI): |
| 256 |
self.params: list[VulkanType] = [] |
| 257 |
self.toRead: list[VulkanType] = [] |
| 258 |
self.toWrite: list[VulkanType] = [] |
| 259 |
|
| 260 |
for i, param in enumerate(api.parameters): |
| 261 |
if i == 0 and param.isDispatchableHandleType(): |
| 262 |
param.dispatchHandle = True |
| 263 |
|
| 264 |
if param.isNonDispatchableHandleType() and param.isCreatedBy(api): |
| 265 |
param.nonDispatchableHandleCreate = True |
| 266 |
|
| 267 |
if param.isNonDispatchableHandleType() and param.isDestroyedBy(api): |
| 268 |
param.nonDispatchableHandleDestroy = True |
| 269 |
|
| 270 |
if param.isDispatchableHandleType() and param.isCreatedBy(api): |
| 271 |
param.dispatchableHandleCreate = True |
| 272 |
|
| 273 |
if param.isDispatchableHandleType() and param.isDestroyedBy(api): |
| 274 |
param.dispatchableHandleDestroy = True |
| 275 |
|
| 276 |
self.toRead.append(param) |
| 277 |
|
| 278 |
if param.possiblyOutput(): |
| 279 |
self.toWrite.append(param) |
| 280 |
|
| 281 |
self.params.append(param) |
| 282 |
|
| 283 |
|
| 284 |
def emit_call_log(api, cgen): |
| 285 |
decodingParams = DecodingParameters(api) |
| 286 |
paramsToRead = decodingParams.toRead |
| 287 |
|
| 288 |
cgen.beginIf("m_logCalls") |
| 289 |
paramLogFormat = "" |
| 290 |
paramLogArgs = [] |
| 291 |
for p in paramsToRead: |
| 292 |
paramLogFormat += "0x%llx " |
| 293 |
for p in paramsToRead: |
| 294 |
paramLogArgs.append("(unsigned long long)%s" % (p.paramName)) |
| 295 |
cgen.stmt("GFXSTREAM_INFO(\"stream %%p: call %s %s\", ioStream, %s)" % (api.name, paramLogFormat, ", ".join(paramLogArgs))) |
| 296 |
cgen.endIf() |
| 297 |
|
| 298 |
def emit_decode_parameters(typeInfo: VulkanTypeInfo, api: VulkanAPI, cgen, globalWrapped=False): |
| 299 |
decodingParams = DecodingParameters(api) |
| 300 |
|
| 301 |
paramsToRead = decodingParams.toRead |
| 302 |
|
| 303 |
for p in paramsToRead: |
| 304 |
emit_param_decl_for_reading(p, cgen) |
| 305 |
|
| 306 |
for i, p in enumerate(paramsToRead): |
| 307 |
lenAccess = cgen.generalLengthAccess(p) |
| 308 |
|
| 309 |
if p.dispatchHandle: |
| 310 |
if api.name in DELAYED_DECODER_DELETE_DICT_ENTRIES or api.name in DELAYED_DECODER_DELETES: |
| 311 |
emit_dispatch_unmarshal(typeInfo, p, cgen, False) |
| 312 |
else: |
| 313 |
emit_dispatch_unmarshal(typeInfo, p, cgen, globalWrapped) |
| 314 |
else: |
| 315 |
destroy = p.nonDispatchableHandleDestroy or p.dispatchableHandleDestroy |
| 316 |
noUnbox = api.name in ["vkQueueFlushCommandsGOOGLE", "vkQueueFlushCommandsFromAuxMemoryGOOGLE"] and p.paramName == "commandBuffer" |
| 317 |
|
| 318 |
if p.nonDispatchableHandleDestroy or p.dispatchableHandleDestroy: |
| 319 |
destroy = True |
| 320 |
cgen.stmt("// Begin manual non dispatchable handle destroy unboxing for %s" % p.paramName) |
| 321 |
if None == lenAccess or "1" == lenAccess: |
| 322 |
cgen.stmt("%s boxed_%s_preserve" % (p.typeName, p.paramName)) |
| 323 |
else: |
| 324 |
cgen.stmt("%s* boxed_%s_preserve; %s->alloc((void**)&boxed_%s_preserve, %s * sizeof(%s))" % (p.typeName, p.paramName, READ_STREAM, p.paramName, lenAccess, p.typeName)) |
| 325 |
|
| 326 |
if p.possiblyOutput(): |
| 327 |
cgen.stmt("// Begin manual dispatchable handle unboxing for %s" % p.paramName) |
| 328 |
cgen.stmt("%s->unsetHandleMapping()" % READ_STREAM) |
| 329 |
|
| 330 |
emit_unmarshal(typeInfo, p, cgen, output = p.possiblyOutput(), destroy = destroy, noUnbox = noUnbox) |
| 331 |
|
| 332 |
for p in paramsToRead: |
| 333 |
emit_transform(typeInfo, p, cgen, variant="tohost") |
| 334 |
|
| 335 |
emit_call_log(api, cgen) |
| 336 |
|
| 337 |
def emit_dispatch_call(api, cgen): |
| 338 |
|
| 339 |
decodingParams = DecodingParameters(api) |
| 340 |
|
| 341 |
customParams = [] |
| 342 |
|
| 343 |
delay = api.name in DELAYED_DECODER_DELETES |
| 344 |
|
| 345 |
for i, p in enumerate(api.parameters): |
| 346 |
customParam = p.paramName |
| 347 |
if decodingParams.params[i].dispatchHandle: |
| 348 |
customParam = "unboxed_%s" % p.paramName |
| 349 |
customParams.append(customParam) |
| 350 |
|
| 351 |
if delay: |
| 352 |
cgen.line("std::function<void()> delayed_remove_callback = [vk, %s]() {" % ", ".join(customParams)) |
| 353 |
|
| 354 |
if api.name in driver_workarounds_global_lock_apis: |
| 355 |
if delay: |
| 356 |
cgen.stmt("auto state = VkDecoderGlobalState::get()") |
| 357 |
cgen.stmt("// state already locked") |
| 358 |
else: |
| 359 |
cgen.stmt("m_state->lock()") |
| 360 |
|
| 361 |
whichDispatch = "vk->" |
| 362 |
checkDispatcher = "CC_LIKELY(vk)" |
| 363 |
if api.name in GLOBAL_COMMANDS_WITHOUT_DISPATCH: |
| 364 |
whichDispatch = "m_vk->" |
| 365 |
checkDispatcher = None |
| 366 |
|
| 367 |
cgen.vkApiCall(api, customPrefix=whichDispatch, customParameters=customParams, \ |
| 368 |
globalStatePrefix=global_state_prefix, checkForDeviceLost=True, |
| 369 |
checkDispatcher=checkDispatcher) |
| 370 |
|
| 371 |
if api.name in driver_workarounds_global_lock_apis: |
| 372 |
if not delay: |
| 373 |
cgen.stmt("m_state->unlock()") |
| 374 |
# for delayed remove, state is already locked, so we do not need to |
| 375 |
# unlock |
| 376 |
|
| 377 |
if delay: |
| 378 |
cgen.line("};") |
| 379 |
|
| 380 |
def emit_global_state_wrapped_call(api, cgen, context): |
| 381 |
# Delayed deletes will call wrapped call with a callback without pool or snapshot info |
| 382 |
delay = api.name in DELAYED_DECODER_DELETES |
| 383 |
coreCustomParams = list(map(lambda p: p.paramName, api.parameters)) |
| 384 |
|
| 385 |
if delay: |
| 386 |
cgen.line("std::function<void()> delayed_remove_callback = [vk, %s]() {" % ", ".join(coreCustomParams)) |
| 387 |
cgen.stmt("auto m_state = VkDecoderGlobalState::get()") |
| 388 |
customParams = ["nullptr", "kInvalidSnapshotApiCallHandle"] + coreCustomParams |
| 389 |
else: |
| 390 |
customParams = ["&m_pool", SNAPSHOT_API_CALL_HANDLE_VARNAME] + coreCustomParams |
| 391 |
|
| 392 |
if context: |
| 393 |
customParams += ["context"] |
| 394 |
|
| 395 |
checkDispatcher = "CC_LIKELY(vk)" |
| 396 |
if api.name in GLOBAL_COMMANDS_WITHOUT_DISPATCH: |
| 397 |
checkDispatcher = None |
| 398 |
cgen.vkApiCall(api, customPrefix=global_state_prefix, \ |
| 399 |
customParameters=customParams, globalStatePrefix=global_state_prefix, \ |
| 400 |
checkForDeviceLost=True, checkDispatcher=checkDispatcher) |
| 401 |
|
| 402 |
if delay: |
| 403 |
cgen.line("};") |
| 404 |
|
| 405 |
def emit_decode_parameters_writeback(typeInfo, api, cgen, autobox=True): |
| 406 |
decodingParams = DecodingParameters(api) |
| 407 |
|
| 408 |
paramsToWrite = decodingParams.toWrite |
| 409 |
|
| 410 |
cgen.stmt("%s->unsetHandleMapping()" % WRITE_STREAM) |
| 411 |
|
| 412 |
handleMapOverwrites = False |
| 413 |
|
| 414 |
for p in paramsToWrite: |
| 415 |
emit_transform(typeInfo, p, cgen, variant="fromhost") |
| 416 |
|
| 417 |
handleMapOverwrites = False |
| 418 |
|
| 419 |
if p.nonDispatchableHandleCreate or p.dispatchableHandleCreate: |
| 420 |
handleMapOverwrites = True |
| 421 |
|
| 422 |
if autobox and p.nonDispatchableHandleCreate: |
| 423 |
cgen.stmt("// Begin auto non dispatchable handle create for %s" % p.paramName) |
| 424 |
cgen.stmt("if (%s == VK_SUCCESS) %s->setHandleMapping(&m_boxedHandleCreateMapping)" % \ |
| 425 |
(api.getRetVarExpr(), WRITE_STREAM)) |
| 426 |
|
| 427 |
if (not autobox) and p.nonDispatchableHandleCreate: |
| 428 |
cgen.stmt("// Begin manual non dispatchable handle create for %s" % p.paramName) |
| 429 |
cgen.stmt("%s->unsetHandleMapping()" % WRITE_STREAM) |
| 430 |
|
| 431 |
emit_marshal(typeInfo, p, cgen, handleMapOverwrites=handleMapOverwrites) |
| 432 |
|
| 433 |
if autobox and p.nonDispatchableHandleCreate: |
| 434 |
cgen.stmt("// Begin auto non dispatchable handle create for %s" % p.paramName) |
| 435 |
cgen.stmt("%s->setHandleMapping(&m_boxedHandleUnwrapMapping)" % WRITE_STREAM) |
| 436 |
|
| 437 |
if (not autobox) and p.nonDispatchableHandleCreate: |
| 438 |
cgen.stmt("// Begin manual non dispatchable handle create for %s" % p.paramName) |
| 439 |
cgen.stmt("%s->setHandleMapping(&m_boxedHandleUnwrapMapping)" % WRITE_STREAM) |
| 440 |
|
| 441 |
def emit_decode_return_writeback(api, cgen): |
| 442 |
retTypeName = api.getRetTypeExpr() |
| 443 |
if retTypeName != "void": |
| 444 |
retVar = api.getRetVarExpr() |
| 445 |
cgen.stmt("%s->write(&%s, %s)" % |
| 446 |
(WRITE_STREAM, retVar, cgen.sizeofExpr(api.retType))) |
| 447 |
|
| 448 |
def emit_decode_finish(api, cgen): |
| 449 |
decodingParams = DecodingParameters(api) |
| 450 |
retTypeName = api.getRetTypeExpr() |
| 451 |
paramsToWrite = decodingParams.toWrite |
| 452 |
|
| 453 |
if retTypeName != "void" or len(paramsToWrite) != 0: |
| 454 |
cgen.stmt("%s->commitWrite()" % WRITE_STREAM) |
| 455 |
|
| 456 |
def emit_destroyed_handle_cleanup(api, cgen): |
| 457 |
decodingParams = DecodingParameters(api) |
| 458 |
paramsToRead = decodingParams.toRead |
| 459 |
|
| 460 |
skipDelete = api.name in SKIPPED_DECODER_DELETES |
| 461 |
|
| 462 |
if skipDelete: |
| 463 |
cgen.line("// Skipping handle cleanup for %s" % api.name) |
| 464 |
return |
| 465 |
|
| 466 |
for p in paramsToRead: |
| 467 |
if p.dispatchHandle: |
| 468 |
pass |
| 469 |
else: |
| 470 |
lenAccess = cgen.generalLengthAccess(p) |
| 471 |
lenAccessGuard = cgen.generalLengthAccess(p) |
| 472 |
destroy = p.nonDispatchableHandleDestroy or p.dispatchableHandleDestroy |
| 473 |
if destroy: |
| 474 |
if None == lenAccess or "1" == lenAccess: |
| 475 |
if api.name in DELAYED_DECODER_DELETES: |
| 476 |
cgen.stmt("delayed_delete_%s(boxed_%s_preserve, unboxed_device, delayed_remove_callback)" % (p.typeName, p.paramName)) |
| 477 |
elif api.name in DELAYED_DECODER_DELETE_DICT_ENTRIES: |
| 478 |
cgen.stmt("delayed_delete_%s(boxed_%s_preserve, unboxed_device, nullptr)" % (p.typeName, p.paramName)) |
| 479 |
else: |
| 480 |
cgen.stmt("delete_%s(boxed_%s_preserve)" % (p.typeName, p.paramName)) |
| 481 |
else: |
| 482 |
if lenAccessGuard is not None: |
| 483 |
cgen.beginIf(lenAccessGuard) |
| 484 |
cgen.beginFor("uint32_t i = 0", "i < %s" % lenAccess, "++i") |
| 485 |
if api.name in DELAYED_DECODER_DELETES: |
| 486 |
cgen.stmt("delayed_delete_%s(boxed_%s_preserve[i], unboxed_device, delayed_remove_callback)" % (p.typeName, p.paramName)) |
| 487 |
else: |
| 488 |
cgen.stmt("delete_%s(boxed_%s_preserve[i])" % (p.typeName, p.paramName)) |
| 489 |
cgen.endFor() |
| 490 |
if lenAccessGuard is not None: |
| 491 |
cgen.endIf() |
| 492 |
|
| 493 |
def emit_pool_free(cgen): |
| 494 |
cgen.stmt("%s->clearPool()" % READ_STREAM) |
| 495 |
|
| 496 |
def emit_seqno_incr(api, cgen): |
| 497 |
cgen.stmt("if (m_queueSubmitWithCommandsEnabled) seqnoPtr->fetch_add(1, std::memory_order_seq_cst)") |
| 498 |
|
| 499 |
def emit_snapshot(typeInfo, api, cgen): |
| 500 |
additionalParams = [ \ |
| 501 |
makeVulkanTypeSimple(False, "gfxstream::base::BumpPool", 1, "&m_pool"), |
| 502 |
makeVulkanTypeSimple(True, "VkSnapshotApiCallHandle", 1, SNAPSHOT_API_CALL_HANDLE_VARNAME), |
| 503 |
makeVulkanTypeSimple(True, "uint8_t", 1, "packet"), |
| 504 |
makeVulkanTypeSimple(False, "size_t", 0, "packetLen"), |
| 505 |
] |
| 506 |
|
| 507 |
retTypeName = api.getRetTypeExpr() |
| 508 |
if retTypeName != "void": |
| 509 |
retVar = api.getRetVarExpr() |
| 510 |
additionalParams.append(makeVulkanTypeSimple(False, retTypeName, 0, retVar)) |
| 511 |
|
| 512 |
paramsForSnapshot = [] |
| 513 |
|
| 514 |
decodingParams = DecodingParameters(api) |
| 515 |
|
| 516 |
for p in decodingParams.toRead: |
| 517 |
if p.nonDispatchableHandleDestroy or (not p.dispatchHandle and p.dispatchableHandleDestroy): |
| 518 |
paramsForSnapshot.append(p.withModifiedName("boxed_%s_preserve" % p.paramName)) |
| 519 |
else: |
| 520 |
paramsForSnapshot.append(p) |
| 521 |
|
| 522 |
customParams = additionalParams + paramsForSnapshot |
| 523 |
|
| 524 |
apiForSnapshot = \ |
| 525 |
api.withCustomReturnType(makeVulkanTypeSimple(False, "void", 0, "void")). \ |
| 526 |
withCustomParameters(customParams) |
| 527 |
|
| 528 |
cgen.beginIf("m_snapshotsEnabled") |
| 529 |
cgen.vkApiCall(apiForSnapshot, customPrefix="m_state->snapshot()->") |
| 530 |
cgen.endIf() |
| 531 |
|
| 532 |
def emit_decoding(typeInfo, api, cgen, globalWrapped=False, context=False): |
| 533 |
isAcquire = api.name in RELAXED_APIS |
| 534 |
emit_decode_parameters(typeInfo, api, cgen, globalWrapped) |
| 535 |
|
| 536 |
if isAcquire: |
| 537 |
emit_seqno_incr(api, cgen) |
| 538 |
|
| 539 |
if globalWrapped: |
| 540 |
emit_global_state_wrapped_call(api, cgen, context) |
| 541 |
else: |
| 542 |
emit_dispatch_call(api, cgen) |
| 543 |
|
| 544 |
emit_decode_parameters_writeback(typeInfo, api, cgen, autobox=not globalWrapped) |
| 545 |
emit_decode_return_writeback(api, cgen) |
| 546 |
emit_decode_finish(api, cgen) |
| 547 |
emit_snapshot(typeInfo, api, cgen) |
| 548 |
emit_destroyed_handle_cleanup(api, cgen) |
| 549 |
emit_pool_free(cgen) |
| 550 |
|
| 551 |
if not isAcquire: |
| 552 |
emit_seqno_incr(api, cgen) |
| 553 |
|
| 554 |
def emit_default_decoding(typeInfo, api, cgen): |
| 555 |
emit_decoding(typeInfo, api, cgen) |
| 556 |
|
| 557 |
def emit_global_state_wrapped_decoding(typeInfo, api, cgen): |
| 558 |
emit_decoding(typeInfo, api, cgen, globalWrapped=True) |
| 559 |
|
| 560 |
def emit_global_state_wrapped_decoding_with_context(typeInfo, api, cgen): |
| 561 |
emit_decoding(typeInfo, api, cgen, globalWrapped=True, context=True) |
| 562 |
|
| 563 |
## Custom decoding definitions################################################## |
| 564 |
def decode_vkFlushMappedMemoryRanges(typeInfo: VulkanTypeInfo, api, cgen): |
| 565 |
emit_decode_parameters(typeInfo, api, cgen) |
| 566 |
|
| 567 |
cgen.beginIf("!m_state->usingDirectMapping()") |
| 568 |
cgen.stmt("// This is to deal with a deficiency in the encoder,"); |
| 569 |
cgen.stmt("// where usingDirectMapping fails to set the proper packet size,"); |
| 570 |
cgen.stmt("// meaning we can read off the end of the packet."); |
| 571 |
cgen.stmt("uint64_t sizeLeft = end - *readStreamPtrPtr") |
| 572 |
cgen.beginFor("uint32_t i = 0", "i < memoryRangeCount", "++i") |
| 573 |
cgen.beginIf("sizeLeft < sizeof(uint64_t)") |
| 574 |
cgen.beginIf("m_prevSeqno") |
| 575 |
cgen.stmt("m_prevSeqno = m_prevSeqno.value() - 1") |
| 576 |
cgen.endIf() |
| 577 |
cgen.stmt("return ptr - (unsigned char*)buf") |
| 578 |
cgen.endIf() |
| 579 |
cgen.stmt("auto memory = pMemoryRanges[i].memory") |
| 580 |
cgen.stmt("auto offset = pMemoryRanges[i].offset") |
| 581 |
cgen.stmt("uint64_t readStream = 0") |
| 582 |
cgen.stmt("memcpy(&readStream, *readStreamPtrPtr, sizeof(uint64_t)); *readStreamPtrPtr += sizeof(uint64_t)") |
| 583 |
cgen.stmt("sizeLeft -= sizeof(uint64_t)") |
| 584 |
cgen.stmt("auto hostPtr = m_state->getMappedHostPointer(memory)") |
| 585 |
cgen.stmt("if (!hostPtr && readStream > 0) GFXSTREAM_FATAL(\"Unexpected\")") |
| 586 |
cgen.stmt("if (!hostPtr) continue") |
| 587 |
cgen.beginIf("sizeLeft < readStream") |
| 588 |
cgen.beginIf("m_prevSeqno") |
| 589 |
cgen.stmt("m_prevSeqno = m_prevSeqno.value() - 1") |
| 590 |
cgen.endIf() |
| 591 |
cgen.stmt("return ptr - (unsigned char*)buf") |
| 592 |
cgen.endIf() |
| 593 |
cgen.stmt("sizeLeft -= readStream") |
| 594 |
cgen.stmt("uint8_t* targetRange = hostPtr + offset") |
| 595 |
cgen.stmt("memcpy(targetRange, *readStreamPtrPtr, readStream); *readStreamPtrPtr += readStream") |
| 596 |
cgen.stmt("packetLen += 8 + readStream") |
| 597 |
cgen.endFor() |
| 598 |
cgen.endIf() |
| 599 |
|
| 600 |
emit_dispatch_call(api, cgen) |
| 601 |
emit_decode_parameters_writeback(typeInfo, api, cgen) |
| 602 |
emit_decode_return_writeback(api, cgen) |
| 603 |
emit_decode_finish(api, cgen) |
| 604 |
emit_snapshot(typeInfo, api, cgen); |
| 605 |
emit_pool_free(cgen) |
| 606 |
emit_seqno_incr(api, cgen) |
| 607 |
|
| 608 |
def decode_vkInvalidateMappedMemoryRanges(typeInfo, api, cgen): |
| 609 |
emit_decode_parameters(typeInfo, api, cgen) |
| 610 |
emit_dispatch_call(api, cgen) |
| 611 |
emit_decode_parameters_writeback(typeInfo, api, cgen) |
| 612 |
emit_decode_return_writeback(api, cgen) |
| 613 |
|
| 614 |
cgen.beginIf("!m_state->usingDirectMapping()") |
| 615 |
cgen.beginFor("uint32_t i = 0", "i < memoryRangeCount", "++i") |
| 616 |
cgen.stmt("auto range = pMemoryRanges[i]") |
| 617 |
cgen.stmt("auto memory = range.memory") |
| 618 |
cgen.stmt("auto size = range.size") |
| 619 |
cgen.stmt("auto offset = range.offset") |
| 620 |
cgen.stmt("auto hostPtr = m_state->getMappedHostPointer(memory)") |
| 621 |
cgen.stmt("auto actualSize = size == VK_WHOLE_SIZE ? m_state->getDeviceMemorySize(memory) : size") |
| 622 |
cgen.stmt("uint64_t writeStream = 0") |
| 623 |
cgen.stmt("if (!hostPtr) { %s->write(&writeStream, sizeof(uint64_t)); continue; }" % WRITE_STREAM) |
| 624 |
cgen.stmt("uint8_t* targetRange = hostPtr + offset") |
| 625 |
cgen.stmt("writeStream = actualSize") |
| 626 |
cgen.stmt("%s->write(&writeStream, sizeof(uint64_t))" % WRITE_STREAM) |
| 627 |
cgen.stmt("%s->write(targetRange, actualSize)" % WRITE_STREAM) |
| 628 |
cgen.endFor() |
| 629 |
cgen.endIf() |
| 630 |
|
| 631 |
emit_decode_finish(api, cgen) |
| 632 |
emit_snapshot(typeInfo, api, cgen); |
| 633 |
emit_pool_free(cgen) |
| 634 |
emit_seqno_incr(api, cgen) |
| 635 |
|
| 636 |
def decode_unsupported_api(typeInfo, api, cgen): |
| 637 |
cgen.line(f"// Decoding {api.name} is not supported. This should not run.") |
| 638 |
cgen.stmt(f"fprintf(stderr, \"stream %p: fatal: decoding unsupported API {api.name}\\n\", ioStream)"); |
| 639 |
cgen.stmt("__builtin_trap()") |
| 640 |
|
| 641 |
custom_decodes = { |
| 642 |
"vkEnumerateInstanceVersion" : emit_global_state_wrapped_decoding, |
| 643 |
"vkCreateInstance" : emit_global_state_wrapped_decoding, |
| 644 |
"vkDestroyInstance" : emit_global_state_wrapped_decoding, |
| 645 |
"vkEnumeratePhysicalDevices" : emit_global_state_wrapped_decoding, |
| 646 |
"vkEnumerateInstanceExtensionProperties" : emit_global_state_wrapped_decoding, |
| 647 |
|
| 648 |
"vkGetPhysicalDeviceFeatures" : emit_global_state_wrapped_decoding, |
| 649 |
"vkGetPhysicalDeviceFeatures2" : emit_global_state_wrapped_decoding, |
| 650 |
"vkGetPhysicalDeviceFeatures2KHR" : emit_global_state_wrapped_decoding, |
| 651 |
"vkGetPhysicalDeviceFormatProperties" : emit_global_state_wrapped_decoding, |
| 652 |
"vkGetPhysicalDeviceFormatProperties2" : emit_global_state_wrapped_decoding, |
| 653 |
"vkGetPhysicalDeviceFormatProperties2KHR" : emit_global_state_wrapped_decoding, |
| 654 |
"vkGetPhysicalDeviceImageFormatProperties" : emit_global_state_wrapped_decoding, |
| 655 |
"vkGetPhysicalDeviceImageFormatProperties2" : emit_global_state_wrapped_decoding, |
| 656 |
"vkGetPhysicalDeviceImageFormatProperties2KHR" : emit_global_state_wrapped_decoding, |
| 657 |
"vkGetPhysicalDeviceProperties" : emit_global_state_wrapped_decoding, |
| 658 |
"vkGetPhysicalDeviceProperties2" : emit_global_state_wrapped_decoding, |
| 659 |
"vkGetPhysicalDeviceProperties2KHR" : emit_global_state_wrapped_decoding, |
| 660 |
|
| 661 |
"vkGetPhysicalDeviceMemoryProperties" : emit_global_state_wrapped_decoding, |
| 662 |
"vkGetPhysicalDeviceMemoryProperties2" : emit_global_state_wrapped_decoding, |
| 663 |
"vkGetPhysicalDeviceMemoryProperties2KHR" : emit_global_state_wrapped_decoding, |
| 664 |
|
| 665 |
"vkGetPhysicalDeviceExternalSemaphoreProperties" : emit_global_state_wrapped_decoding, |
| 666 |
"vkGetPhysicalDeviceExternalSemaphorePropertiesKHR" : emit_global_state_wrapped_decoding, |
| 667 |
|
| 668 |
"vkEnumerateDeviceExtensionProperties" : emit_global_state_wrapped_decoding, |
| 669 |
|
| 670 |
"vkCreateBuffer" : emit_global_state_wrapped_decoding, |
| 671 |
"vkDestroyBuffer" : emit_global_state_wrapped_decoding, |
| 672 |
|
| 673 |
"vkBindBufferMemory" : emit_global_state_wrapped_decoding, |
| 674 |
"vkBindBufferMemory2" : emit_global_state_wrapped_decoding, |
| 675 |
"vkBindBufferMemory2KHR" : emit_global_state_wrapped_decoding, |
| 676 |
|
| 677 |
"vkCreateDevice" : emit_global_state_wrapped_decoding, |
| 678 |
"vkDestroyDevice" : emit_global_state_wrapped_decoding, |
| 679 |
|
| 680 |
"vkGetDeviceQueue" : emit_global_state_wrapped_decoding, |
| 681 |
"vkGetDeviceQueue2" : emit_global_state_wrapped_decoding, |
| 682 |
|
| 683 |
"vkGetPhysicalDeviceQueueFamilyProperties" : emit_global_state_wrapped_decoding, |
| 684 |
"vkGetPhysicalDeviceQueueFamilyProperties2" : emit_global_state_wrapped_decoding, |
| 685 |
|
| 686 |
"vkQueueBindSparse" : emit_global_state_wrapped_decoding, |
| 687 |
"vkQueuePresentKHR" : emit_global_state_wrapped_decoding, |
| 688 |
|
| 689 |
"vkBindImageMemory" : emit_global_state_wrapped_decoding, |
| 690 |
"vkBindImageMemory2" : emit_global_state_wrapped_decoding, |
| 691 |
"vkBindImageMemory2KHR" : emit_global_state_wrapped_decoding, |
| 692 |
|
| 693 |
"vkCreateImage" : emit_global_state_wrapped_decoding, |
| 694 |
"vkCreateImageView" : emit_global_state_wrapped_decoding, |
| 695 |
"vkCreateSampler" : emit_global_state_wrapped_decoding, |
| 696 |
"vkDestroyImage" : emit_global_state_wrapped_decoding, |
| 697 |
"vkDestroyImageView" : emit_global_state_wrapped_decoding, |
| 698 |
"vkDestroySampler" : emit_global_state_wrapped_decoding, |
| 699 |
"vkCmdCopyBufferToImage" : emit_global_state_wrapped_decoding_with_context, |
| 700 |
"vkCmdCopyImage" : emit_global_state_wrapped_decoding, |
| 701 |
"vkCmdCopyImageToBuffer" : emit_global_state_wrapped_decoding, |
| 702 |
"vkCmdCopyBufferToImage2" : emit_global_state_wrapped_decoding_with_context, |
| 703 |
"vkCmdCopyImage2" : emit_global_state_wrapped_decoding, |
| 704 |
"vkCmdCopyImageToBuffer2" : emit_global_state_wrapped_decoding, |
| 705 |
"vkGetImageMemoryRequirements" : emit_global_state_wrapped_decoding, |
| 706 |
"vkGetImageMemoryRequirements2" : emit_global_state_wrapped_decoding, |
| 707 |
"vkGetImageMemoryRequirements2KHR" : emit_global_state_wrapped_decoding, |
| 708 |
"vkGetBufferMemoryRequirements" : emit_global_state_wrapped_decoding, |
| 709 |
"vkGetBufferMemoryRequirements2": emit_global_state_wrapped_decoding, |
| 710 |
"vkGetBufferMemoryRequirements2KHR": emit_global_state_wrapped_decoding, |
| 711 |
|
| 712 |
"vkCreateDescriptorSetLayout" : emit_global_state_wrapped_decoding, |
| 713 |
"vkDestroyDescriptorSetLayout" : emit_global_state_wrapped_decoding, |
| 714 |
"vkCreateDescriptorPool" : emit_global_state_wrapped_decoding, |
| 715 |
"vkDestroyDescriptorPool" : emit_global_state_wrapped_decoding, |
| 716 |
"vkResetDescriptorPool" : emit_global_state_wrapped_decoding, |
| 717 |
"vkAllocateDescriptorSets" : emit_global_state_wrapped_decoding, |
| 718 |
"vkFreeDescriptorSets" : emit_global_state_wrapped_decoding, |
| 719 |
|
| 720 |
"vkUpdateDescriptorSets" : emit_global_state_wrapped_decoding, |
| 721 |
|
| 722 |
"vkCreateShaderModule": emit_global_state_wrapped_decoding, |
| 723 |
"vkDestroyShaderModule": emit_global_state_wrapped_decoding, |
| 724 |
"vkCreatePipelineCache": emit_global_state_wrapped_decoding, |
| 725 |
"vkDestroyPipelineCache": emit_global_state_wrapped_decoding, |
| 726 |
"vkCreatePipelineLayout": emit_global_state_wrapped_decoding, |
| 727 |
"vkDestroyPipelineLayout": emit_global_state_wrapped_decoding, |
| 728 |
"vkCreateComputePipelines": emit_global_state_wrapped_decoding, |
| 729 |
"vkCreateGraphicsPipelines": emit_global_state_wrapped_decoding, |
| 730 |
"vkDestroyPipeline": emit_global_state_wrapped_decoding, |
| 731 |
|
| 732 |
"vkAllocateMemory" : emit_global_state_wrapped_decoding, |
| 733 |
"vkFreeMemory" : emit_global_state_wrapped_decoding, |
| 734 |
"vkMapMemory" : emit_global_state_wrapped_decoding, |
| 735 |
"vkUnmapMemory" : emit_global_state_wrapped_decoding, |
| 736 |
"vkFlushMappedMemoryRanges" : decode_vkFlushMappedMemoryRanges, |
| 737 |
"vkInvalidateMappedMemoryRanges" : decode_vkInvalidateMappedMemoryRanges, |
| 738 |
|
| 739 |
"vkAllocateCommandBuffers" : emit_global_state_wrapped_decoding, |
| 740 |
"vkCmdExecuteCommands" : emit_global_state_wrapped_decoding, |
| 741 |
"vkQueueSubmit" : emit_global_state_wrapped_decoding, |
| 742 |
"vkQueueSubmit2" : emit_global_state_wrapped_decoding, |
| 743 |
"vkQueueWaitIdle" : emit_global_state_wrapped_decoding, |
| 744 |
"vkBeginCommandBuffer" : emit_global_state_wrapped_decoding_with_context, |
| 745 |
"vkEndCommandBuffer" : emit_global_state_wrapped_decoding_with_context, |
| 746 |
"vkResetCommandBuffer" : emit_global_state_wrapped_decoding, |
| 747 |
"vkFreeCommandBuffers" : emit_global_state_wrapped_decoding, |
| 748 |
"vkCreateCommandPool" : emit_global_state_wrapped_decoding, |
| 749 |
"vkDestroyCommandPool" : emit_global_state_wrapped_decoding, |
| 750 |
"vkResetCommandPool" : emit_global_state_wrapped_decoding, |
| 751 |
"vkCmdPipelineBarrier" : emit_global_state_wrapped_decoding, |
| 752 |
"vkCmdPipelineBarrier2" : emit_global_state_wrapped_decoding, |
| 753 |
"vkCmdBindPipeline" : emit_global_state_wrapped_decoding, |
| 754 |
"vkCmdBindDescriptorSets" : emit_global_state_wrapped_decoding, |
| 755 |
|
| 756 |
"vkCreateRenderPass" : emit_global_state_wrapped_decoding, |
| 757 |
"vkCreateRenderPass2" : emit_global_state_wrapped_decoding, |
| 758 |
"vkCreateRenderPass2KHR" : emit_global_state_wrapped_decoding, |
| 759 |
"vkDestroyRenderPass" : emit_global_state_wrapped_decoding, |
| 760 |
"vkCreateFramebuffer" : emit_global_state_wrapped_decoding, |
| 761 |
"vkDestroyFramebuffer" : emit_global_state_wrapped_decoding, |
| 762 |
"vkCreateEvent" : emit_global_state_wrapped_decoding, |
| 763 |
"vkDestroyEvent" : emit_global_state_wrapped_decoding, |
| 764 |
"vkSetEvent" : emit_global_state_wrapped_decoding, |
| 765 |
"vkResetEvent" : emit_global_state_wrapped_decoding, |
| 766 |
"vkDestroyFramebuffer" : emit_global_state_wrapped_decoding, |
| 767 |
"vkCmdBeginRenderPass" : emit_global_state_wrapped_decoding, |
| 768 |
"vkCmdBeginRenderPass2" : emit_global_state_wrapped_decoding, |
| 769 |
"vkCmdBeginRenderPass2KHR" : emit_global_state_wrapped_decoding, |
| 770 |
|
| 771 |
"vkCreateSamplerYcbcrConversion": emit_global_state_wrapped_decoding, |
| 772 |
"vkDestroySamplerYcbcrConversion": emit_global_state_wrapped_decoding, |
| 773 |
|
| 774 |
# VK_ANDROID_native_buffer |
| 775 |
"vkGetSwapchainGrallocUsageANDROID" : emit_global_state_wrapped_decoding, |
| 776 |
"vkGetSwapchainGrallocUsage2ANDROID" : emit_global_state_wrapped_decoding, |
| 777 |
"vkAcquireImageANDROID" : emit_global_state_wrapped_decoding, |
| 778 |
"vkQueueSignalReleaseImageANDROID" : emit_global_state_wrapped_decoding, |
| 779 |
|
| 780 |
# Semaphores |
| 781 |
"vkCreateSemaphore" : emit_global_state_wrapped_decoding, |
| 782 |
"vkGetSemaphoreFdKHR" : emit_global_state_wrapped_decoding, |
| 783 |
"vkImportSemaphoreFdKHR" : emit_global_state_wrapped_decoding, |
| 784 |
"vkDestroySemaphore" : emit_global_state_wrapped_decoding, |
| 785 |
"vkSignalSemaphore" : emit_global_state_wrapped_decoding, |
| 786 |
"vkWaitSemaphores" : emit_global_state_wrapped_decoding, |
| 787 |
|
| 788 |
# Fences |
| 789 |
"vkCreateFence" : emit_global_state_wrapped_decoding, |
| 790 |
"vkGetFenceStatus" : emit_global_state_wrapped_decoding, |
| 791 |
"vkWaitForFences" : emit_global_state_wrapped_decoding, |
| 792 |
"vkResetFences" : emit_global_state_wrapped_decoding, |
| 793 |
"vkDestroyFence" : emit_global_state_wrapped_decoding, |
| 794 |
|
| 795 |
# VK_GOOGLE_gfxstream |
| 796 |
"vkFreeMemorySyncGOOGLE" : emit_global_state_wrapped_decoding, |
| 797 |
"vkMapMemoryIntoAddressSpaceGOOGLE" : emit_global_state_wrapped_decoding, |
| 798 |
"vkGetMemoryHostAddressInfoGOOGLE" : emit_global_state_wrapped_decoding, |
| 799 |
"vkGetBlobGOOGLE" : emit_global_state_wrapped_decoding, |
| 800 |
"vkGetSemaphoreGOOGLE" : emit_global_state_wrapped_decoding, |
| 801 |
"vkTraceAsyncGOOGLE" : emit_global_state_wrapped_decoding, |
| 802 |
"vkSetDebugMetadataAsyncGOOGLE" : emit_global_state_wrapped_decoding, |
| 803 |
|
| 804 |
# Descriptor update templates |
| 805 |
"vkCreateDescriptorUpdateTemplate" : emit_global_state_wrapped_decoding, |
| 806 |
"vkCreateDescriptorUpdateTemplateKHR" : emit_global_state_wrapped_decoding, |
| 807 |
"vkDestroyDescriptorUpdateTemplate" : emit_global_state_wrapped_decoding, |
| 808 |
"vkDestroyDescriptorUpdateTemplateKHR" : emit_global_state_wrapped_decoding, |
| 809 |
"vkUpdateDescriptorSetWithTemplateSizedGOOGLE" : emit_global_state_wrapped_decoding, |
| 810 |
"vkUpdateDescriptorSetWithTemplateSized2GOOGLE" : emit_global_state_wrapped_decoding, |
| 811 |
|
| 812 |
# VK_GOOGLE_gfxstream |
| 813 |
"vkBeginCommandBufferAsyncGOOGLE" : emit_global_state_wrapped_decoding_with_context, |
| 814 |
"vkEndCommandBufferAsyncGOOGLE" : emit_global_state_wrapped_decoding_with_context, |
| 815 |
"vkResetCommandBufferAsyncGOOGLE" : emit_global_state_wrapped_decoding, |
| 816 |
"vkCommandBufferHostSyncGOOGLE" : emit_global_state_wrapped_decoding, |
| 817 |
"vkCreateImageWithRequirementsGOOGLE" : emit_global_state_wrapped_decoding, |
| 818 |
"vkCreateBufferWithRequirementsGOOGLE" : emit_global_state_wrapped_decoding, |
| 819 |
"vkQueueHostSyncGOOGLE" : emit_global_state_wrapped_decoding, |
| 820 |
"vkQueueSubmitAsyncGOOGLE" : emit_global_state_wrapped_decoding, |
| 821 |
"vkQueueSubmitAsync2GOOGLE" : emit_global_state_wrapped_decoding, |
| 822 |
"vkQueueWaitIdleAsyncGOOGLE" : emit_global_state_wrapped_decoding, |
| 823 |
"vkQueueBindSparseAsyncGOOGLE" : emit_global_state_wrapped_decoding, |
| 824 |
"vkGetLinearImageLayoutGOOGLE" : emit_global_state_wrapped_decoding, |
| 825 |
"vkGetLinearImageLayout2GOOGLE" : emit_global_state_wrapped_decoding, |
| 826 |
"vkQueueFlushCommandsGOOGLE" : emit_global_state_wrapped_decoding_with_context, |
| 827 |
"vkQueueFlushCommandsFromAuxMemoryGOOGLE" : emit_global_state_wrapped_decoding_with_context, |
| 828 |
"vkQueueCommitDescriptorSetUpdatesGOOGLE" : emit_global_state_wrapped_decoding, |
| 829 |
"vkCollectDescriptorPoolIdsGOOGLE" : emit_global_state_wrapped_decoding, |
| 830 |
"vkQueueSignalReleaseImageANDROIDAsyncGOOGLE" : emit_global_state_wrapped_decoding, |
| 831 |
|
| 832 |
"vkQueueBindSparse" : emit_global_state_wrapped_decoding, |
| 833 |
|
| 834 |
# VK_KHR_xcb_surface |
| 835 |
"vkCreateXcbSurfaceKHR": decode_unsupported_api, |
| 836 |
"vkGetPhysicalDeviceXcbPresentationSupportKHR": decode_unsupported_api, |
| 837 |
|
| 838 |
# VK_EXT_metal_surface |
| 839 |
"vkCreateMetalSurfaceEXT": decode_unsupported_api, |
| 840 |
|
| 841 |
# VK_KHR_sampler_ycbcr_conversion |
| 842 |
"vkCreateSamplerYcbcrConversionKHR": emit_global_state_wrapped_decoding, |
| 843 |
"vkDestroySamplerYcbcrConversionKHR": emit_global_state_wrapped_decoding, |
| 844 |
|
| 845 |
#VK_KHR_copy_commands2 |
| 846 |
"vkCmdCopyBufferToImage2KHR" : emit_global_state_wrapped_decoding_with_context, |
| 847 |
"vkCmdCopyImage2KHR" : emit_global_state_wrapped_decoding, |
| 848 |
"vkCmdCopyImageToBuffer2KHR" : emit_global_state_wrapped_decoding, |
| 849 |
|
| 850 |
# VK_KHR_device_group_creation / VK_VERSION_1_1 |
| 851 |
"vkEnumeratePhysicalDeviceGroups" : emit_global_state_wrapped_decoding, |
| 852 |
"vkEnumeratePhysicalDeviceGroupsKHR" : emit_global_state_wrapped_decoding, |
| 853 |
|
| 854 |
# Sparse binding and formats (Support can be disabled from the host) |
| 855 |
"vkGetPhysicalDeviceSparseImageFormatProperties" : emit_global_state_wrapped_decoding, |
| 856 |
"vkGetPhysicalDeviceSparseImageFormatProperties2" : emit_global_state_wrapped_decoding, |
| 857 |
"vkGetPhysicalDeviceSparseImageFormatProperties2KHR" : emit_global_state_wrapped_decoding, |
| 858 |
|
| 859 |
# Image requirements need to be adjusted for compressed textures |
| 860 |
"vkGetDeviceImageMemoryRequirements" : emit_global_state_wrapped_decoding, |
| 861 |
"vkGetDeviceImageMemoryRequirementsKHR" : emit_global_state_wrapped_decoding, |
| 862 |
|
| 863 |
} |
| 864 |
|
| 865 |
class VulkanDecoder(VulkanWrapperGenerator): |
| 866 |
def __init__(self, module, typeInfo): |
| 867 |
VulkanWrapperGenerator.__init__(self, module, typeInfo) |
| 868 |
self.typeInfo: VulkanTypeInfo = typeInfo |
| 869 |
self.cgen = CodeGen() |
| 870 |
|
| 871 |
def onBegin(self,): |
| 872 |
self.module.appendImpl( |
| 873 |
"#define MAX_PACKET_LENGTH %s\n" % MAX_PACKET_LENGTH) |
| 874 |
self.module.appendImpl( |
| 875 |
"#define CC_LIKELY(exp) (__builtin_expect( !!(exp), true ))\n") |
| 876 |
self.module.appendImpl( |
| 877 |
"#define CC_UNLIKELY(exp) (__builtin_expect( !!(exp), false ))\n") |
| 878 |
|
| 879 |
self.module.appendHeader(decoder_decl_preamble) |
| 880 |
self.module.appendImpl(decoder_impl_preamble) |
| 881 |
|
| 882 |
self.module.appendImpl( |
| 883 |
""" |
| 884 |
size_t VkDecoder::Impl::decode(void* buf, size_t len, IOStream* ioStream, |
| 885 |
const ProcessResources* processResources, |
| 886 |
const VkDecoderContext& context) |
| 887 |
""") |
| 888 |
|
| 889 |
self.cgen.beginBlock() # function body |
| 890 |
|
| 891 |
self.cgen.stmt("const char* processName = context.processName") |
| 892 |
self.cgen.stmt("auto& gfx_logger = *context.gfxApiLogger") |
| 893 |
self.cgen.stmt("auto& shouldExit = *context.shouldExit") |
| 894 |
self.cgen.stmt("if (len < 8) return 0") |
| 895 |
self.cgen.stmt("unsigned char *ptr = (unsigned char *)buf") |
| 896 |
self.cgen.stmt("const unsigned char* const end = (const unsigned char*)buf + len") |
| 897 |
|
| 898 |
self.cgen.line("while (end - ptr >= 8)") |
| 899 |
self.cgen.beginBlock() # while loop |
| 900 |
|
| 901 |
self.cgen.stmt("const uint8_t* packet = (const uint8_t *)ptr") |
| 902 |
self.cgen.stmt("uint32_t opcode") |
| 903 |
self.cgen.stmt("std::memcpy(&opcode, ptr, sizeof(uint32_t))") |
| 904 |
self.cgen.line("") |
| 905 |
self.cgen.stmt("uint32_t packetLen") |
| 906 |
self.cgen.stmt("std::memcpy(&packetLen, ptr + 4, sizeof(uint32_t))") |
| 907 |
self.cgen.line("") |
| 908 |
self.cgen.line(""" |
| 909 |
// packetLen should be at least 8 (op code and packet length) and should not be excessively large |
| 910 |
if (packetLen < 8 || packetLen > MAX_PACKET_LENGTH) { |
| 911 |
GFXSTREAM_WARNING("Bad packet length %d detected, decode may fail", packetLen); |
| 912 |
} |
| 913 |
""") |
| 914 |
self.cgen.stmt("if (end - ptr < packetLen) return ptr - (unsigned char*)buf") |
| 915 |
self.cgen.stmt("gfx_logger.record(ptr, std::min(size_t(packetLen + 8), size_t(end - ptr)))") |
| 916 |
|
| 917 |
self.cgen.stmt("stream()->setStream(ioStream)") |
| 918 |
self.cgen.stmt("VulkanStream* %s = stream()" % WRITE_STREAM) |
| 919 |
self.cgen.stmt("VulkanMemReadingStream* %s = readStream()" % READ_STREAM) |
| 920 |
self.cgen.stmt("%s->setBuf((uint8_t*)(ptr + 8))" % READ_STREAM) |
| 921 |
self.cgen.stmt("uint8_t* readStreamPtr = %s->getBuf(); uint8_t** readStreamPtrPtr = &readStreamPtr" % READ_STREAM) |
| 922 |
self.cgen.stmt("%s->setHandleMapping(&m_boxedHandleUnwrapMapping)" % READ_STREAM) |
| 923 |
self.cgen.line(""" |
| 924 |
std::atomic<uint32_t>* seqnoPtr = processResources ? |
| 925 |
processResources->getSequenceNumberPtr() : nullptr; |
| 926 |
|
| 927 |
if (m_queueSubmitWithCommandsEnabled && ((opcode >= OP_vkFirst && opcode < OP_vkLast) || (opcode >= OP_vkFirst_old && opcode < OP_vkLast_old))) { |
| 928 |
uint32_t seqno; |
| 929 |
memcpy(&seqno, *readStreamPtrPtr, sizeof(uint32_t)); *readStreamPtrPtr += sizeof(uint32_t); |
| 930 |
if (m_prevSeqno && seqno == m_prevSeqno.value()) { |
| 931 |
GFXSTREAM_WARNING( |
| 932 |
"Seqno %d is the same as previously processed on thread %d. It might be a " |
| 933 |
"duplicate command.", |
| 934 |
seqno, gfxstream::base::getCurrentThreadId()); |
| 935 |
} |
| 936 |
if (seqnoPtr && !m_forSnapshotLoad) { |
| 937 |
{ |
| 938 |
while ((seqno - seqnoPtr->load(std::memory_order_seq_cst) != 1)) { |
| 939 |
if (shouldExit.load(std::memory_order_relaxed)) { |
| 940 |
GFXSTREAM_WARNING("Process=%s is exitting. Skip processing seqno=%d on thread=0x%x.", |
| 941 |
processName ? processName : "null", seqno, gfxstream::base::getCurrentThreadId()); |
| 942 |
return 0; |
| 943 |
} |
| 944 |
#if (defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))) |
| 945 |
_mm_pause(); |
| 946 |
#elif (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))) |
| 947 |
__asm__ __volatile__("pause;"); |
| 948 |
#endif |
| 949 |
} |
| 950 |
m_prevSeqno = seqno; |
| 951 |
} |
| 952 |
} |
| 953 |
} |
| 954 |
""") |
| 955 |
|
| 956 |
self.cgen.line(""" |
| 957 |
VkSnapshotApiCallHandle %s = kInvalidSnapshotApiCallHandle; |
| 958 |
if (m_snapshotsEnabled) { |
| 959 |
%s = m_state->snapshot()->createApiCallInfo(); |
| 960 |
} |
| 961 |
""" % (SNAPSHOT_API_CALL_HANDLE_VARNAME, SNAPSHOT_API_CALL_HANDLE_VARNAME)) |
| 962 |
|
| 963 |
self.cgen.line(""" |
| 964 |
gfx_logger.recordCommandExecution(); |
| 965 |
""") |
| 966 |
|
| 967 |
self.cgen.line("switch (opcode)") |
| 968 |
self.cgen.beginBlock() # switch stmt |
| 969 |
|
| 970 |
self.module.appendImpl(self.cgen.swapCode()) |
| 971 |
|
| 972 |
def onGenCmd(self, cmdinfo, name, alias): |
| 973 |
typeInfo = self.typeInfo |
| 974 |
cgen = self.cgen |
| 975 |
api: VulkanAPI = typeInfo.apis[name] |
| 976 |
|
| 977 |
cgen.line("case OP_%s:" % name) |
| 978 |
cgen.beginBlock() |
| 979 |
|
| 980 |
if name not in COMMANDS_WITHOUT_TRACE: |
| 981 |
cgen.stmt("GFXSTREAM_TRACE_EVENT(GFXSTREAM_TRACE_DECODER_CATEGORY, \"VkDecoder %s\")" % name) |
| 982 |
|
| 983 |
if api.name in custom_decodes.keys(): |
| 984 |
custom_decodes[api.name](typeInfo, api, cgen) |
| 985 |
else: |
| 986 |
emit_default_decoding(typeInfo, api, cgen) |
| 987 |
|
| 988 |
cgen.stmt("break") |
| 989 |
cgen.endBlock() |
| 990 |
self.module.appendImpl(self.cgen.swapCode()) |
| 991 |
|
| 992 |
def onEnd(self,): |
| 993 |
self.cgen.line("default:") |
| 994 |
self.cgen.beginBlock() |
| 995 |
self.cgen.line(""" |
| 996 |
if (m_snapshotsEnabled) { |
| 997 |
m_state->snapshot()->destroyApiCallInfoIfUnused(%s); |
| 998 |
} |
| 999 |
""" % (SNAPSHOT_API_CALL_HANDLE_VARNAME)) |
| 1000 |
|
| 1001 |
self.cgen.stmt("m_pool.freeAll()") |
| 1002 |
self.cgen.stmt("return ptr - (unsigned char *)buf") |
| 1003 |
self.cgen.endBlock() |
| 1004 |
|
| 1005 |
self.cgen.endBlock() # switch stmt |
| 1006 |
|
| 1007 |
self.cgen.line(""" |
| 1008 |
if (m_snapshotsEnabled) { |
| 1009 |
m_state->snapshot()->destroyApiCallInfoIfUnused(%s); |
| 1010 |
} |
| 1011 |
""" % (SNAPSHOT_API_CALL_HANDLE_VARNAME)) |
| 1012 |
|
| 1013 |
self.cgen.stmt("ptr += packetLen") |
| 1014 |
self.cgen.stmt("vkStream->clearPool()") |
| 1015 |
self.cgen.endBlock() # while loop |
| 1016 |
|
| 1017 |
self.cgen.stmt("m_pool.freeAll()") |
| 1018 |
self.cgen.stmt("return ptr - (unsigned char*)buf;") |
| 1019 |
self.cgen.endBlock() # function body |
| 1020 |
self.module.appendImpl(self.cgen.swapCode()) |
| 1021 |
self.module.appendImpl(decoder_impl_postamble) |