Debian Codegraph @

src/gfxstream/codegen/scripts/cereal/decodersnapshot.py

raw
1 # Copyright 2018 Google LLC
2 # SPDX-License-Identifier: MIT
3
4 from .common.codegen import CodeGen, VulkanWrapperGenerator, VulkanAPIWrapper
5 from .common.vulkantypes import \
6 VulkanAPI, makeVulkanTypeSimple, iterateVulkanType, DISPATCHABLE_HANDLE_TYPES, NON_DISPATCHABLE_HANDLE_TYPES
7
8 from .transform import TransformCodegen, genTransformsForVulkanType
9
10 from .wrapperdefs import API_PREFIX_MARSHAL
11 from .wrapperdefs import API_PREFIX_UNMARSHAL
12 from .wrapperdefs import VULKAN_STREAM_TYPE
13
14 from copy import copy
15 from dataclasses import dataclass
16
17 decoder_snapshot_decl_preamble = """
18
19 namespace gfxstream {
20 class Stream;
21 } // namespace gfxstream
22
23 namespace gfxstream {
24 namespace base {
25 class BumpPool;
26 } // namespace base
27 } // namespace gfxstream
28
29 namespace gfxstream {
30 namespace host {
31 namespace vk {
32
33 class VkDecoderSnapshot {
34 public:
35 VkDecoderSnapshot();
36 ~VkDecoderSnapshot();
37
38 void clear();
39
40 void saveReplayBuffers(gfxstream::Stream* stream);
41 static void loadReplayBuffers(gfxstream::Stream* stream, std::vector<uint64_t>* outHandleBuffer, std::vector<uint8_t>* outDecoderBuffer);
42
43 VkSnapshotApiCallHandle createApiCallInfo();
44 void destroyApiCallInfoIfUnused(VkSnapshotApiCallHandle handle);
45
46 // Performs bookkeeping to track that a given api call created the given VkObject handles.
47 // This is a public function so that `VkDecoderGlobalState` can inform snapshot of any
48 // additional handles created while emulating features.
49 void addOrderedBoxedHandlesCreatedByCall(VkSnapshotApiCallHandle apiCallHandle, VkObjectHandle* boxedHandles, uint32_t boxedHandlesCount);
50 """
51
52 decoder_snapshot_decl_postamble = """
53 private:
54 class Impl;
55 std::unique_ptr<Impl> mImpl;
56
57 };
58
59 } // namespace vk
60 } // namespace host
61 } // namespace gfxstream
62 """
63
64 decoder_snapshot_impl_preamble ="""
65
66 namespace gfxstream {
67 namespace host {
68 namespace vk {
69
70 class VkDecoderSnapshot::Impl {
71 public:
72 Impl() { }
73
74 void clear() {
75 std::lock_guard<std::mutex> lock(mReconstructionMutex);
76 mReconstruction.clear();
77 }
78
79 void saveReplayBuffers(gfxstream::Stream* stream) {
80 std::lock_guard<std::mutex> lock(mReconstructionMutex);
81 mReconstruction.saveReplayBuffers(stream);
82 }
83
84 static void loadReplayBuffers(gfxstream::Stream* stream, std::vector<uint64_t>* outHandleBuffer, std::vector<uint8_t>* outDecoderBuffer) {
85 VkReconstruction::loadReplayBuffers(stream, outHandleBuffer, outDecoderBuffer);
86 }
87
88 VkSnapshotApiCallHandle createApiCallInfo() {
89 std::lock_guard<std::mutex> lock(mReconstructionMutex);
90 return mReconstruction.createApiCallInfo();
91 }
92
93 void destroyApiCallInfoIfUnused(VkSnapshotApiCallHandle apiCallHandle) {
94 std::lock_guard<std::mutex> lock(mReconstructionMutex);
95 return mReconstruction.destroyApiCallInfoIfUnused(apiCallHandle);
96 }
97
98 void addOrderedBoxedHandlesCreatedByCall(VkSnapshotApiCallHandle apiCallHandle, VkObjectHandle* boxedHandles, uint32_t boxedHandlesCount) {
99 std::lock_guard<std::mutex> lock(mReconstructionMutex);
100 return mReconstruction.addOrderedBoxedHandlesCreatedByCall(apiCallHandle, boxedHandles, boxedHandlesCount);
101 }
102
103 """
104
105 decoder_snapshot_impl_postamble = """
106 private:
107 std::mutex mReconstructionMutex;
108 VkReconstruction mReconstruction GUARDED_BY(mReconstructionMutex);
109 };
110
111 VkDecoderSnapshot::VkDecoderSnapshot() :
112 mImpl(new VkDecoderSnapshot::Impl()) { }
113
114 void VkDecoderSnapshot::clear() {
115 mImpl->clear();
116 }
117
118 void VkDecoderSnapshot::saveReplayBuffers(gfxstream::Stream* stream) {
119 mImpl->saveReplayBuffers(stream);
120 }
121
122 /*static*/
123 void VkDecoderSnapshot::loadReplayBuffers(gfxstream::Stream* stream, std::vector<uint64_t>* outHandleBuffer, std::vector<uint8_t>* outDecoderBuffer) {
124 VkDecoderSnapshot::Impl::loadReplayBuffers(stream, outHandleBuffer, outDecoderBuffer);
125 }
126
127 VkSnapshotApiCallHandle VkDecoderSnapshot::createApiCallInfo() {
128 return mImpl->createApiCallInfo();
129 }
130
131 void VkDecoderSnapshot::destroyApiCallInfoIfUnused(VkSnapshotApiCallHandle handle) {
132 mImpl->destroyApiCallInfoIfUnused(handle);
133 }
134
135 void VkDecoderSnapshot::addOrderedBoxedHandlesCreatedByCall(VkSnapshotApiCallHandle apiCallHandle, VkObjectHandle* boxedHandles, uint32_t boxedHandlesCount) {
136 mImpl->addOrderedBoxedHandlesCreatedByCall(apiCallHandle, boxedHandles, boxedHandlesCount);
137 }
138
139 VkDecoderSnapshot::~VkDecoderSnapshot() = default;
140 """
141
142 decoder_snapshot_namespace_postamble = """
143
144 } // namespace vk
145 } // namespace host
146 } // namespace gfxstream
147
148 """
149
150
151 AUXILIARY_SNAPSHOT_API_BASE_PARAM_COUNT = 3
152
153 AUXILIARY_SNAPSHOT_API_PARAM_NAMES = [
154 "input_result",
155 ]
156
157 # Vulkan handle dependencies.
158 # (a, b): a depends on b
159 SNAPSHOT_HANDLE_DEPENDENCIES = [
160 # Dispatchable handle types
161 ("VkCommandBuffer", "VkCommandPool"),
162 ("VkCommandPool", "VkDevice"),
163 ("VkQueue", "VkDevice"),
164 ("VkDevice", "VkPhysicalDevice"),
165 ("VkPhysicalDevice", "VkInstance")] + \
166 list(map(lambda handleType : (handleType, "VkDevice"), NON_DISPATCHABLE_HANDLE_TYPES))
167
168 handleDependenciesDict = dict(SNAPSHOT_HANDLE_DEPENDENCIES)
169
170 def extract_deps_vkAllocateMemory(param, access, lenExpr, api, cgen):
171 cgen.stmt("const VkMemoryDedicatedAllocateInfo* dedicatedAllocateInfo = vk_find_struct<VkMemoryDedicatedAllocateInfo>(pAllocateInfo)");
172 cgen.beginIf("dedicatedAllocateInfo");
173 cgen.beginIf("dedicatedAllocateInfo->image")
174 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)%s, %s, (uint64_t)(uintptr_t)%s)" % \
175 (access, lenExpr, "unboxed_to_boxed_non_dispatchable_VkImage(dedicatedAllocateInfo->image)"))
176 cgen.endIf()
177 cgen.beginIf("dedicatedAllocateInfo->buffer")
178 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)%s, %s, (uint64_t)(uintptr_t)%s)" % \
179 (access, lenExpr, "unboxed_to_boxed_non_dispatchable_VkBuffer(dedicatedAllocateInfo->buffer)"))
180 cgen.endIf()
181 cgen.endIf()
182
183 def extract_deps_vkAllocateCommandBuffers(param, access, lenExpr, api, cgen):
184 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)%s, %s, (uint64_t)(uintptr_t)%s)" % \
185 (access, lenExpr, "unboxed_to_boxed_non_dispatchable_VkCommandPool(pAllocateInfo->commandPool)"))
186
187 def extract_deps_vkAllocateDescriptorSets(param, access, lenExpr, api, cgen):
188 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)%s, %s, (uint64_t)(uintptr_t)%s)" % \
189 (access, lenExpr, "unboxed_to_boxed_non_dispatchable_VkDescriptorPool(pAllocateInfo->descriptorPool)"))
190
191 def extract_deps_vkUpdateDescriptorSets(param, access, lenExpr, api, cgen):
192 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)(&handle), 1, (uint64_t)(uintptr_t)device)")
193 cgen.beginFor("uint32_t i = 0", "i < descriptorWriteCount", "++i")
194 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)(&handle), 1, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkDescriptorSet( pDescriptorWrites[i].dstSet))")
195 cgen.beginFor("uint32_t j = 0", "j < pDescriptorWrites[i].descriptorCount", "++j")
196 cgen.beginIf("(pDescriptorWrites[i].pImageInfo)")
197 cgen.beginIf("pDescriptorWrites[i].descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER")
198 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)(&handle), 1, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkSampler( pDescriptorWrites[i].pImageInfo[j].sampler))")
199 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)(&handle), 1, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkImageView( pDescriptorWrites[i].pImageInfo[j].imageView))")
200 cgen.endIf()
201 cgen.beginIf("pDescriptorWrites[i].pImageInfo[j].imageView")
202 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)(&handle), 1, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkImageView( pDescriptorWrites[i].pImageInfo[j].imageView))")
203 cgen.endIf()
204 cgen.beginIf("pDescriptorWrites[i].descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER")
205 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)(&handle), 1, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkSampler( pDescriptorWrites[i].pImageInfo[j].sampler))")
206 cgen.endIf()
207 cgen.endIf()
208 cgen.beginIf("pDescriptorWrites[i].pBufferInfo");
209 cgen.beginIf("pDescriptorWrites[i].descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER");
210 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)(&handle), 1, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkBuffer( pDescriptorWrites[i].pBufferInfo[j].buffer))")
211 cgen.endIf()
212 cgen.endIf()
213 cgen.endFor()
214 cgen.endFor()
215
216 def extract_deps_vkCreateImageView(param, access, lenExpr, api, cgen):
217 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)%s, %s, (uint64_t)(uintptr_t)%s)" % \
218 (access, lenExpr, "unboxed_to_boxed_non_dispatchable_VkImage(pCreateInfo->image)"))
219
220 def extract_deps_vkCreateGraphicsPipelines(param, access, lenExpr, api, cgen):
221 cgen.beginFor("uint32_t i = 0", "i < createInfoCount", "++i")
222 cgen.beginFor("uint32_t j = 0", "j < pCreateInfos[i].stageCount", "++j")
223 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)(%s + i), %s, (uint64_t)(uintptr_t)%s)" % \
224 (access, 1, "unboxed_to_boxed_non_dispatchable_VkShaderModule(pCreateInfos[i].pStages[j].module)"))
225 cgen.endFor()
226 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)(%s + i), %s, (uint64_t)(uintptr_t)%s)" % \
227 (access, 1, "unboxed_to_boxed_non_dispatchable_VkRenderPass(pCreateInfos[i].renderPass)"))
228 cgen.endFor()
229
230 def extract_deps_vkCreateFramebuffer(param, access, lenExpr, api, cgen):
231 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)%s, %s, (uint64_t)(uintptr_t)%s)" % \
232 (access, lenExpr, "unboxed_to_boxed_non_dispatchable_VkRenderPass(pCreateInfo->renderPass)"))
233 cgen.beginIf("(pCreateInfo->flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) == 0")
234 cgen.beginFor("uint32_t i = 0", "i < pCreateInfo->attachmentCount" , "++i")
235 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)%s, %s, (uint64_t)(uintptr_t)%s)" % \
236 (access, lenExpr, "unboxed_to_boxed_non_dispatchable_VkImageView(pCreateInfo->pAttachments[i])"))
237 cgen.endFor()
238 cgen.endIf();
239
240 def extract_deps_vkBindImageMemory(param, access, lenExpr, api, cgen):
241 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)%s, %s, (uint64_t)(uintptr_t)%s, VkReconstruction::BOUND_MEMORY)" % \
242 (access, lenExpr, "unboxed_to_boxed_non_dispatchable_VkDeviceMemory(memory)"))
243 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)%s, %s, (uint64_t)(uintptr_t)((%s)[0]), VkReconstruction::BOUND_MEMORY)" % \
244 (access, lenExpr, access))
245
246 def extract_deps_vkBindBufferMemory(param, access, lenExpr, api, cgen):
247 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)%s, %s, (uint64_t)(uintptr_t)%s, VkReconstruction::BOUND_MEMORY)" % \
248 (access, lenExpr, "unboxed_to_boxed_non_dispatchable_VkDeviceMemory(memory)"))
249 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)%s, %s, (uint64_t)(uintptr_t)((%s)[0]), VkReconstruction::BOUND_MEMORY)" % \
250 (access, lenExpr, access))
251
252 specialCaseDependencyExtractors = {
253 "vkAllocateCommandBuffers" : extract_deps_vkAllocateCommandBuffers,
254 "vkAllocateDescriptorSets" : extract_deps_vkAllocateDescriptorSets,
255 "vkAllocateMemory" : extract_deps_vkAllocateMemory,
256 "vkCreateImageView" : extract_deps_vkCreateImageView,
257 "vkCreateGraphicsPipelines" : extract_deps_vkCreateGraphicsPipelines,
258 "vkCreateFramebuffer" : extract_deps_vkCreateFramebuffer,
259 "vkUpdateDescriptorSets" : extract_deps_vkUpdateDescriptorSets,
260 }
261
262 apiSequences = {
263 "vkAllocateMemory" : ["vkAllocateMemory", "vkMapMemoryIntoAddressSpaceGOOGLE"]
264 }
265
266 @dataclass(frozen=True)
267 class VkObjectState:
268 vk_object : str
269 state : str = "VkReconstruction::CREATED"
270
271 # TODO: add vkBindImageMemory2 and vkBindBufferMemory2 into this list
272 apiChangeState = {
273 }
274
275 def api_special_implementation_common(api, cgen, tag_vk):
276 cgen.line("// Note: special implementation");
277 cgen.stmt("std::lock_guard<std::mutex> lock(mReconstructionMutex)")
278 cgen.stmt("VkDecoderGlobalState* m_state = VkDecoderGlobalState::get()")
279 cgen.stmt("uint64_t handle = m_state->newGlobalVkGenericHandle(%s)" % tag_vk)
280 cgen.stmt("mReconstruction.addHandles((const uint64_t*)(&handle), 1)")
281 cgen.stmt("mReconstruction.forEachHandleAddApi((const uint64_t*)(&handle), 1, apiCallHandle, VkReconstruction::CREATED)")
282 cgen.stmt("mReconstruction.setCreatedHandlesForApi(apiCallHandle, (const uint64_t*)(&handle), 1)")
283 cgen.stmt("mReconstruction.setApiTrace(apiCallHandle, apiCallPacket, apiCallPacketSize)")
284
285 def api_special_implementation_vkCmdPipelineBarrier(api, cgen):
286 cgen.stmt("std::lock_guard<std::mutex> lock(mReconstructionMutex)")
287 cgen.beginFor("uint32_t i = 0", "i < bufferMemoryBarrierCount", "++i")
288 cgen.stmt("mReconstruction.addApiCallDependencyOnVkObject(apiCallHandle, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkBuffer( pBufferMemoryBarriers[i].buffer))")
289 cgen.endFor()
290 cgen.beginFor("uint32_t i = 0", "i < imageMemoryBarrierCount", "++i")
291 cgen.stmt("mReconstruction.addApiCallDependencyOnVkObject(apiCallHandle, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkImage( pImageMemoryBarriers[i].image))")
292 cgen.endFor()
293
294 def api_special_implementation_vkUpdateDescriptorSetWithTemplateSizedGOOGLE(api, cgen):
295 cgen.stmt("std::lock_guard<std::mutex> lock(mReconstructionMutex)")
296 cgen.stmt("VkDecoderGlobalState* m_state = VkDecoderGlobalState::get()")
297 cgen.beginIf("m_state->batchedDescriptorSetUpdateEnabled()")
298 cgen.stmt("return")
299 cgen.endIf();
300 cgen.stmt("uint64_t handle = m_state->newGlobalVkGenericHandle(Tag_VkUpdateDescriptorSets)")
301 cgen.stmt("mReconstruction.addHandles((const uint64_t*)(&handle), 1)")
302 cgen.stmt("mReconstruction.setApiTrace(apiCallHandle, apiCallPacket, apiCallPacketSize)")
303 cgen.stmt("mReconstruction.addHandleDependency( (const uint64_t*)(&handle), 1, (uint64_t)(uintptr_t)device)")
304 cgen.stmt("mReconstruction.forEachHandleAddApi((const uint64_t*)(&handle), 1, apiCallHandle, VkReconstruction::CREATED)")
305 cgen.stmt("mReconstruction.setCreatedHandlesForApi(apiCallHandle, (const uint64_t*)(&handle), 1)")
306
307 def api_special_implementation_vkCmdBeginRenderPass(api, cgen):
308 cgen.stmt("std::lock_guard<std::mutex> lock(mReconstructionMutex)")
309 cgen.stmt("mReconstruction.addApiCallDependencyOnVkObject(apiCallHandle, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkFramebuffer( pRenderPassBegin->framebuffer))")
310
311 def api_special_implementation_vkMapMemoryIntoAddressSpaceGOOGLE(api, cgen):
312 cgen.stmt("std::lock_guard<std::mutex> lock(mReconstructionMutex)")
313 cgen.stmt("VkDecoderGlobalState* m_state = VkDecoderGlobalState::get()")
314 cgen.stmt("uint64_t handle = m_state->newGlobalVkGenericHandle(Tag_VkMapMemory)")
315 cgen.stmt("mReconstruction.addHandles((const uint64_t*)(&handle), 1)")
316 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)(&handle), 1, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkDeviceMemory(memory))")
317 cgen.stmt("mReconstruction.forEachHandleAddApi((const uint64_t*)(&handle), 1, apiCallHandle, VkReconstruction::CREATED)")
318 cgen.stmt("mReconstruction.setCreatedHandlesForApi(apiCallHandle, (const uint64_t*)(&handle), 1)")
319 cgen.stmt("mReconstruction.setApiTrace(apiCallHandle, apiCallPacket, apiCallPacketSize)")
320
321 def api_special_implementation_vkCmdBeginRenderPass(api, cgen):
322 cgen.stmt("std::lock_guard<std::mutex> lock(mReconstructionMutex)")
323 cgen.stmt("mReconstruction.addApiCallDependencyOnVkObject(apiCallHandle, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkFramebuffer( pRenderPassBegin->framebuffer))")
324
325 def api_special_implementation_vkCmdCopyBufferToImage(api, cgen):
326 cgen.stmt("std::lock_guard<std::mutex> lock(mReconstructionMutex)")
327 cgen.stmt("mReconstruction.addApiCallDependencyOnVkObject(apiCallHandle, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkBuffer(srcBuffer))")
328 cgen.stmt("mReconstruction.addApiCallDependencyOnVkObject(apiCallHandle, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkImage(dstImage))")
329
330 def api_special_implementation_vkCmdCopyImageToBuffer(api, cgen):
331 cgen.stmt("std::lock_guard<std::mutex> lock(mReconstructionMutex)")
332 cgen.stmt("mReconstruction.addApiCallDependencyOnVkObject(apiCallHandle, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkImage(srcImage))")
333 cgen.stmt("mReconstruction.addApiCallDependencyOnVkObject(apiCallHandle, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkBuffer(dstBuffer))")
334
335 def api_special_implementation_vkCmdCopyBuffer(api, cgen):
336 cgen.stmt("std::lock_guard<std::mutex> lock(mReconstructionMutex)")
337 cgen.stmt("mReconstruction.addApiCallDependencyOnVkObject(apiCallHandle, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkBuffer(srcBuffer))")
338 cgen.stmt("mReconstruction.addApiCallDependencyOnVkObject(apiCallHandle, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkBuffer(dstBuffer))")
339
340 def api_special_implementation_vkCmdBindVertexBuffers(api, cgen):
341 cgen.stmt("std::lock_guard<std::mutex> lock(mReconstructionMutex)")
342 cgen.beginFor("uint32_t i = 0", "i < bindingCount", "++i")
343 cgen.stmt("mReconstruction.addApiCallDependencyOnVkObject(apiCallHandle, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkBuffer(pBuffers[i]))")
344 cgen.endFor()
345
346 def api_special_implementation_vkCmdBindPipeline(api, cgen):
347 cgen.stmt("std::lock_guard<std::mutex> lock(mReconstructionMutex)")
348 cgen.stmt("mReconstruction.addApiCallDependencyOnVkObject(apiCallHandle, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkPipeline(pipeline))")
349
350 def api_special_implementation_vkResetCommandPool(api, cgen):
351 cgen.line("// Note: special implementation");
352 cgen.stmt("std::lock_guard<std::mutex> lock(mReconstructionMutex)")
353 cgen.stmt("mReconstruction.removeGrandChildren((uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkCommandPool(commandPool))")
354
355 def api_special_implementation_vkResetCommandBuffer(api, cgen):
356 cgen.line("// Note: special implementation");
357 cgen.stmt("std::lock_guard<std::mutex> lock(mReconstructionMutex)")
358 cgen.stmt("mReconstruction.removeDescendantsOfHandle((uint64_t)(uintptr_t)commandBuffer)")
359
360 def api_special_implementation_vkQueueFlushCommandsGOOGLE(api, cgen):
361 api_special_implementation_common(api, cgen, "Tag_VkCmdOp")
362 cgen.stmt("mReconstruction.removeDescendantsOfHandle((uint64_t)(uintptr_t)commandBuffer)")
363 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)(&handle), 1, (uint64_t)(uintptr_t)commandBuffer)")
364
365 cgen.line("// Track that `handle` depends on previously tracked dependencies (e.g. the handle for this `vkQueueFlushCommandsGOOGLE()` call depends on the `VkPipeline` handle from `vkCmdBindPipeline()`).")
366 cgen.stmt("mReconstruction.addHandleDependenciesForApiCallDependencies(apiCallHandle, handle)")
367
368
369 def api_special_implementation_vkBindBufferMemory(api, cgen):
370 api_special_implementation_common(api, cgen, "Tag_VkBindMemory")
371 cgen.stmt("mReconstruction.addHandleDependency( (const uint64_t*)(&handle), 1, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkDeviceMemory(memory))")
372 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)(&handle), 1, (uint64_t)(uintptr_t)(unboxed_to_boxed_non_dispatchable_VkBuffer(buffer)))")
373
374 def api_special_implementation_vkBindImageMemory(api, cgen):
375 api_special_implementation_common(api, cgen, "Tag_VkBindMemory")
376 cgen.stmt("mReconstruction.addHandleDependency( (const uint64_t*)(&handle), 1, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkDeviceMemory(memory))")
377 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)(&handle), 1, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkImage(image))")
378
379 def api_special_implementation_vkBindImageMemory2(api, cgen):
380 api_special_implementation_common(api, cgen, "Tag_VkBindMemory")
381 cgen.beginFor("uint32_t i = 0", "i < bindInfoCount", "++i")
382 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)&handle, 1, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkDeviceMemory(pBindInfos[i].memory))")
383 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)&handle, 1, (uint64_t)(uintptr_t)unboxed_to_boxed_non_dispatchable_VkImage(pBindInfos[i].image))")
384 cgen.endFor()
385
386 apiSpecialImplementation = {
387 "vkBindBufferMemory": api_special_implementation_vkBindBufferMemory,
388 "vkBindImageMemory": api_special_implementation_vkBindImageMemory,
389 "vkBindImageMemory2": api_special_implementation_vkBindImageMemory2,
390 "vkBindImageMemory2KHR": api_special_implementation_vkBindImageMemory2,
391 "vkMapMemoryIntoAddressSpaceGOOGLE": api_special_implementation_vkMapMemoryIntoAddressSpaceGOOGLE,
392 "vkGetBlobGOOGLE": api_special_implementation_vkMapMemoryIntoAddressSpaceGOOGLE,
393 "vkQueueFlushCommandsGOOGLE": api_special_implementation_vkQueueFlushCommandsGOOGLE,
394 "vkResetCommandBuffer": api_special_implementation_vkResetCommandBuffer,
395 "vkResetCommandPool": api_special_implementation_vkResetCommandPool,
396 "vkCmdBindVertexBuffers": api_special_implementation_vkCmdBindVertexBuffers,
397 "vkCmdBindPipeline": api_special_implementation_vkCmdBindPipeline,
398 "vkCmdCopyBufferToImage": api_special_implementation_vkCmdCopyBufferToImage,
399 "vkCmdCopyImageToBuffer": api_special_implementation_vkCmdCopyImageToBuffer,
400 "vkCmdCopyBuffer": api_special_implementation_vkCmdCopyBuffer,
401 "vkCmdPipelineBarrier": api_special_implementation_vkCmdPipelineBarrier,
402 "vkCmdBeginRenderPass": api_special_implementation_vkCmdBeginRenderPass,
403 "vkCmdBeginRenderPass2": api_special_implementation_vkCmdBeginRenderPass,
404 "vkUpdateDescriptorSetWithTemplateSizedGOOGLE": api_special_implementation_vkUpdateDescriptorSetWithTemplateSizedGOOGLE,
405 "vkUpdateDescriptorSetWithTemplateSized2GOOGLE": api_special_implementation_vkUpdateDescriptorSetWithTemplateSizedGOOGLE,
406 }
407
408 apiModifies = {
409 "vkBeginCommandBuffer" : ["commandBuffer"],
410 "vkEndCommandBuffer" : ["commandBuffer"],
411 }
412
413 apiActions = {
414 }
415
416 apiActionsTag = {
417 }
418
419 apiClearModifiers = {
420 }
421
422 delayedDestroys = [
423 "vkDestroyShaderModule",
424 ]
425
426 # The following types are created and cached by other commands.
427 # Thus we should not snapshot their "create" commands.
428 skipCreatorSnapshotTypes = [
429 "VkQueue", # created by vkCreateDevice
430 ]
431
432 def is_state_change_operation(api, param):
433 if param.isCreatedBy(api) and param.typeName not in skipCreatorSnapshotTypes:
434 return True
435 if api.name in apiChangeState:
436 if param.paramName == apiChangeState[api.name].vk_object:
437 return True
438 return False
439
440 def get_target_state(api, param):
441 if param.isCreatedBy(api):
442 return "VkReconstruction::CREATED"
443 if api.name in apiActions:
444 return "VkReconstruction::CREATED"
445 if api.name in apiChangeState:
446 if param.paramName == apiChangeState[api.name].vk_object:
447 return apiChangeState[api.name].state
448 return None
449
450 def is_action_operation(api, param):
451 if api.name in apiActions:
452 if param.paramName in apiActions[api.name]:
453 return True
454 return False
455
456 def is_modify_operation(api, param):
457 if api.name in apiModifies:
458 if param.paramName in apiModifies[api.name]:
459 return True
460 return False
461
462 def emit_impl(typeInfo, api, cgen):
463 if api.name in apiSpecialImplementation:
464 apiSpecialImplementation[api.name](api, cgen)
465 for p in api.parameters:
466 if not (p.isHandleType):
467 continue
468
469 lenExpr = cgen.generalLengthAccess(p)
470 lenAccessGuard = cgen.generalLengthAccessGuard(p)
471
472 if lenExpr is None:
473 lenExpr = "1"
474
475 # Note that in vkCreate*, the last parameter (the output) is boxed. But all input parameters are unboxed.
476
477 if p.pointerIndirectionLevels > 0:
478 access = p.paramName
479 else:
480 access = "(&%s)" % p.paramName
481
482 if is_state_change_operation(api, p):
483 if p.isCreatedBy(api):
484 boxed_access = access
485 else:
486 cgen.stmt("%s boxed_%s = unboxed_to_boxed_non_dispatchable_%s(%s[0])" % (p.typeName, p.typeName, p.typeName, access))
487 boxed_access = "&boxed_%s" % p.typeName
488 if p.pointerIndirectionLevels > 0:
489 cgen.stmt("if (!%s) return" % access)
490 cgen.stmt("if (input_result != VK_SUCCESS) return")
491
492 cgen.stmt("std::lock_guard<std::mutex> lock(mReconstructionMutex)")
493 cgen.line("// %s create" % p.paramName)
494 if p.isCreatedBy(api):
495 cgen.stmt("mReconstruction.addHandles((const uint64_t*)%s, %s)" % (boxed_access, lenExpr));
496
497 if p.isCreatedBy(api) and p.typeName in handleDependenciesDict:
498 dependsOnType = handleDependenciesDict[p.typeName];
499 for p2 in api.parameters:
500 if p2.typeName == dependsOnType:
501 cgen.stmt("mReconstruction.addHandleDependency((const uint64_t*)%s, %s, (uint64_t)(uintptr_t)%s)" % (boxed_access, lenExpr, p2.paramName))
502 if api.name in specialCaseDependencyExtractors:
503 specialCaseDependencyExtractors[api.name](p, boxed_access, lenExpr, api, cgen)
504
505 cgen.stmt("mReconstruction.setApiTrace(apiCallHandle, apiCallPacket, apiCallPacketSize)")
506 if lenAccessGuard is not None:
507 cgen.beginIf(lenAccessGuard)
508 cgen.stmt(f"mReconstruction.forEachHandleAddApi((const uint64_t*){boxed_access}, {lenExpr}, apiCallHandle, {get_target_state(api, p)})")
509 if p.isCreatedBy(api):
510 cgen.stmt("mReconstruction.setCreatedHandlesForApi(apiCallHandle, (const uint64_t*)%s, %s)" % (boxed_access, lenExpr))
511 if lenAccessGuard is not None:
512 cgen.endIf()
513
514 if p.isDestroyedBy(api):
515 cgen.stmt("std::lock_guard<std::mutex> lock(mReconstructionMutex)")
516 cgen.line("// %s destroy" % p.paramName)
517 if lenAccessGuard is not None:
518 cgen.beginIf(lenAccessGuard)
519 shouldRecursiveDestroy = "false" if api.name in delayedDestroys else "true"
520 cgen.stmt("mReconstruction.removeHandles((const uint64_t*)%s, %s, %s)" % (access, lenExpr, shouldRecursiveDestroy));
521 if lenAccessGuard is not None:
522 cgen.endIf()
523
524 if is_action_operation(api, p):
525 cgen.stmt("std::lock_guard<std::mutex> lock(mReconstructionMutex)")
526 cgen.line("// %s action" % p.paramName)
527 cgen.stmt("VkDecoderGlobalState* m_state = VkDecoderGlobalState::get()")
528 cgen.beginIf("m_state->batchedDescriptorSetUpdateEnabled()")
529 cgen.stmt("return")
530 cgen.endIf();
531 cgen.stmt("uint64_t handle = m_state->newGlobalVkGenericHandle(%s)" % apiActionsTag[api.name])
532 cgen.stmt("mReconstruction.addHandles((const uint64_t*)(&handle), 1)");
533 cgen.stmt("mReconstruction.setApiTrace(apiCallHandle, apiCallPacket, apiCallPacketSize)")
534 if api.name in specialCaseDependencyExtractors:
535 specialCaseDependencyExtractors[api.name](p, None, None, api, cgen)
536 cgen.stmt(f"mReconstruction.forEachHandleAddApi((const uint64_t*)(&handle), 1, apiCallHandle, {get_target_state(api, p)})")
537 cgen.stmt("mReconstruction.setCreatedHandlesForApi(apiCallHandle, (const uint64_t*)(&handle), 1)")
538
539 elif is_modify_operation(api, p):
540 cgen.stmt("std::lock_guard<std::mutex> lock(mReconstructionMutex)")
541 cgen.line("// %s modify" % p.paramName)
542 cgen.stmt("mReconstruction.setApiTrace(apiCallHandle, apiCallPacket, apiCallPacketSize)")
543 if lenAccessGuard is not None:
544 cgen.beginIf(lenAccessGuard)
545 cgen.beginFor("uint32_t i = 0", "i < %s" % lenExpr, "++i")
546 if p.isNonDispatchableHandleType():
547 cgen.stmt("%s boxed = unboxed_to_boxed_non_dispatchable_%s(%s[i])" % (p.typeName, p.typeName, access))
548 else:
549 cgen.line("// %s is already boxed, no need to box again" % p.paramName)
550 cgen.stmt("%s boxed = %s(%s[i])" % (p.typeName, p.typeName, access))
551 if is_modify_operation(api, p):
552 cgen.stmt("mReconstruction.forEachHandleAddModifyApi((const uint64_t*)(&boxed), 1, apiCallHandle)")
553 else: # is clear modifier operation
554 cgen.stmt("mReconstruction.forEachHandleClearModifyApi((const uint64_t*)(&boxed), 1)")
555 cgen.endFor()
556 if lenAccessGuard is not None:
557 cgen.endIf()
558
559 def emit_passthrough_to_impl(typeInfo, api, cgen):
560 cgen.vkApiCall(api, customPrefix = "mImpl->")
561
562 class VulkanDecoderSnapshot(VulkanWrapperGenerator):
563 def __init__(self, module, typeInfo):
564 VulkanWrapperGenerator.__init__(self, module, typeInfo)
565
566 self.typeInfo = typeInfo
567
568 self.cgenHeader = CodeGen()
569 self.cgenHeader.incrIndent()
570
571 self.cgenImpl = CodeGen()
572
573 self.currentFeature = None
574
575 self.feature_apis = []
576
577 def onBegin(self,):
578 self.module.appendHeader(decoder_snapshot_decl_preamble)
579 self.module.appendImpl(decoder_snapshot_impl_preamble)
580
581 def onBeginFeature(self, featureName, featureType):
582 VulkanWrapperGenerator.onBeginFeature(self, featureName, featureType)
583 self.currentFeature = featureName
584
585 def onGenCmd(self, cmdinfo, name, alias):
586 VulkanWrapperGenerator.onGenCmd(self, cmdinfo, name, alias)
587
588 api = self.typeInfo.apis[name]
589
590 additionalParams = [ \
591 makeVulkanTypeSimple(False, "gfxstream::base::BumpPool", 1, "pool"),
592 makeVulkanTypeSimple(False, "VkSnapshotApiCallHandle", 0, "apiCallHandle"),
593 makeVulkanTypeSimple(True, "uint8_t", 1, "apiCallPacket"),
594 makeVulkanTypeSimple(False, "size_t", 0, "apiCallPacketSize"),
595 ]
596
597 if api.retType.typeName != "void":
598 additionalParams.append( \
599 makeVulkanTypeSimple(False, api.retType.typeName, 0, "input_result"))
600
601 apiForSnapshot = \
602 api.withCustomParameters( \
603 additionalParams + \
604 api.parameters).withCustomReturnType( \
605 makeVulkanTypeSimple(False, "void", 0, "void"))
606
607 self.feature_apis.append((self.currentFeature, apiForSnapshot))
608
609 self.cgenHeader.stmt(self.cgenHeader.makeFuncProto(apiForSnapshot))
610 self.module.appendHeader(self.cgenHeader.swapCode())
611
612 self.cgenImpl.emitFuncImpl( \
613 apiForSnapshot, lambda cgen: emit_impl(self.typeInfo, apiForSnapshot, cgen))
614 self.module.appendImpl(self.cgenImpl.swapCode())
615
616 def onEnd(self,):
617 self.module.appendHeader(decoder_snapshot_decl_postamble)
618 self.module.appendImpl(decoder_snapshot_impl_postamble)
619 self.cgenHeader.decrIndent()
620
621 for feature, api in self.feature_apis:
622 if feature is not None:
623 self.cgenImpl.line("#ifdef %s" % feature)
624
625 apiImplShell = \
626 api.withModifiedName("VkDecoderSnapshot::" + api.name)
627
628 self.cgenImpl.emitFuncImpl( \
629 apiImplShell, lambda cgen: emit_passthrough_to_impl(self.typeInfo, api, cgen))
630
631 if feature is not None:
632 self.cgenImpl.line("#endif")
633
634 self.module.appendImpl(self.cgenImpl.swapCode())
635 self.module.appendImpl(decoder_snapshot_namespace_postamble)