|
1 | 1 | #include "artifacts.h" |
2 | 2 | #include "wrapper_objects.h" |
3 | 3 | #include "wrapper_private.h" |
| 4 | +#include "wrapper_entrypoints.h" |
| 5 | +#include "wrapper_checks.h" |
| 6 | +#include "vk_printers.h" |
4 | 7 |
|
5 | 8 | #include <string> |
| 9 | +#include <sstream> |
| 10 | +#include <iomanip> |
6 | 11 |
|
7 | | -static FILE* open_log_file(const char* postfix, int id) { |
8 | | - static char dir[256]; |
| 12 | +static FILE* open_log_file(const std::string postfix, VkFormat original_format, int id, const char* mode) { |
| 13 | + static std::string dir; |
9 | 14 | static bool initialized = false; |
10 | 15 | if (!initialized) { |
11 | 16 | initialized = true; |
12 | 17 | char time_str[20]; |
13 | 18 | get_current_time_string(time_str, sizeof(time_str)); |
14 | | - sprintf(dir, "/sdcard/Documents/Wrapper/artifacts_%s.%s.%d", time_str, getprogname(), getpid()); |
15 | | - if (mkdir(dir, 0777) == 0) { |
16 | | - WLOGE("Failed to create the artifacts directory %s", dir); |
| 19 | + dir = std::string("/sdcard/Documents/Wrapper/artifacts_") + time_str + "." + getprogname() + "." + std::to_string(getpid()); |
| 20 | + if (mkdir(dir.c_str(), 0777) == 0) { |
| 21 | + WLOGE("Failed to create the artifacts directory %s", dir.c_str()); |
17 | 22 | } else { |
18 | | - WLOGD("Logging artifacts to %s", dir); |
| 23 | + WLOGD("Logging artifacts to %s", dir.c_str()); |
19 | 24 | } |
20 | 25 | } |
21 | | - std::string path = std::string(dir) + "/" + std::to_string(id) + "_" + postfix; |
22 | | - return fopen(path.c_str(), "w"); |
| 26 | + std::stringstream padded_id; |
| 27 | + padded_id << std::setw(5) << std::setfill('0') << id; |
| 28 | + |
| 29 | + std::string path = dir + "/" + padded_id.str() + "_fmt" + std::to_string(original_format) + postfix; |
| 30 | + return fopen(path.c_str(), mode); |
23 | 31 | } |
24 | 32 |
|
25 | 33 | extern "C" |
26 | | -void RecordBCnArtifacts(struct wrapper_device* device, const VkBufferImageCopy* region, VkBuffer srcBuffer, VkBuffer stagingBuffer, int decode_id) { |
27 | | - // auto fd = open_log_file("region.txt", decode_id); |
| 34 | +void RecordBCnArtifacts(struct wrapper_device* device, VkFormat original_format, |
| 35 | + const VkBufferImageCopy* region, VkBuffer srcBuffer, VkBuffer stagingBuffer, int decode_id) { |
| 36 | + if (region) { |
| 37 | + auto fd = open_log_file("_region.txt", original_format, decode_id, "w"); |
| 38 | + fprintf(fd, "src: %p\n", srcBuffer); |
| 39 | + vk_print_VkBufferImageCopy(0, 0, fd, "", region); |
| 40 | + fclose(fd); |
| 41 | + } |
| 42 | + |
28 | 43 | struct wrapper_buffer* wbuf = get_wrapper_buffer(device, srcBuffer); |
29 | 44 | if (!wbuf) { |
30 | 45 | WLOGE("srcBuffer not tracked, skipping (decode_id=%d)", decode_id); |
| 46 | + } else if (wbuf->memory == VK_NULL_HANDLE) { |
| 47 | + WLOGE("srcBuffer not bound, skipping (decode_id=%d)", decode_id); |
| 48 | + } else { |
| 49 | + void* srcData; |
| 50 | + VkMemoryMapInfoKHR mapInfoSrc = { |
| 51 | + .sType = VK_STRUCTURE_TYPE_MEMORY_MAP_INFO_KHR, |
| 52 | + .memory = wbuf->memory, |
| 53 | + .offset = wbuf->memoryOffset, |
| 54 | + .size = VK_WHOLE_SIZE, |
| 55 | + }; |
| 56 | + VkResult result = WCHECK(MapMemory2KHR((VkDevice) device, &mapInfoSrc, &srcData)); |
| 57 | + if (result != VK_SUCCESS) { |
| 58 | + WLOGE("ERROR: Failed to map srcBuffer memory: %d", result); |
| 59 | + } else { |
| 60 | + int w = region->bufferRowLength ? region->bufferRowLength : region->imageExtent.width; |
| 61 | + int h = region->bufferImageHeight ? region->bufferImageHeight : region->imageExtent.height; |
| 62 | + int blocks_stride = (w + 3) / 4; |
| 63 | + int block_rows = (h + 3) / 4; |
| 64 | + int blocks = blocks_stride * block_rows; |
| 65 | + int block_size = get_bc_block_size(original_format); |
| 66 | + auto fd = open_log_file("_src.dat", original_format, decode_id, "wb"); |
| 67 | + fwrite(srcData, block_size, blocks, fd); |
| 68 | + fclose(fd); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + wbuf = get_wrapper_buffer(device, stagingBuffer); |
| 73 | + if (!wbuf) { |
| 74 | + WLOGE("dstBuffer not tracked, skipping (decode_id=%d)", decode_id); |
| 75 | + } else if (wbuf->memory == VK_NULL_HANDLE) { |
| 76 | + WLOGE("dstBuffer not bound, skipping (decode_id=%d)", decode_id); |
| 77 | + } else { |
| 78 | + void* dstData; |
| 79 | + VkMemoryMapInfoKHR mapInfoSrc = { |
| 80 | + .sType = VK_STRUCTURE_TYPE_MEMORY_MAP_INFO_KHR, |
| 81 | + .memory = wbuf->memory, |
| 82 | + .offset = 0, |
| 83 | + .size = VK_WHOLE_SIZE, |
| 84 | + }; |
| 85 | + VkResult result = WCHECK(MapMemory2KHR((VkDevice) device, &mapInfoSrc, &dstData)); |
| 86 | + if (result != VK_SUCCESS) { |
| 87 | + WLOGE("ERROR: Failed to map dstBuffer memory: %d", result); |
| 88 | + } else { |
| 89 | + int block_size = get_bc_target_size(device->physical, original_format); |
| 90 | + auto fd = open_log_file("_dst.dat", original_format, decode_id, "wb"); |
| 91 | + fwrite(dstData, block_size, region->imageExtent.width * region->imageExtent.height, fd); |
| 92 | + fclose(fd); |
| 93 | + WCHECKV(UnmapMemory((VkDevice) device, wbuf->memory)); |
| 94 | + } |
31 | 95 | } |
32 | | - // TODO: Implement this |
33 | 96 | } |
0 commit comments