Skip to content

Commit d3d2de2

Browse files
committed
Finish DUMP_BCN_ARTIFACTS implementation
1 parent 6101f03 commit d3d2de2

File tree

4 files changed

+98
-19
lines changed

4 files changed

+98
-19
lines changed

src/vulkan/wrapper/artifacts.cpp

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,96 @@
11
#include "artifacts.h"
22
#include "wrapper_objects.h"
33
#include "wrapper_private.h"
4+
#include "wrapper_entrypoints.h"
5+
#include "wrapper_checks.h"
6+
#include "vk_printers.h"
47

58
#include <string>
9+
#include <sstream>
10+
#include <iomanip>
611

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;
914
static bool initialized = false;
1015
if (!initialized) {
1116
initialized = true;
1217
char time_str[20];
1318
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());
1722
} else {
18-
WLOGD("Logging artifacts to %s", dir);
23+
WLOGD("Logging artifacts to %s", dir.c_str());
1924
}
2025
}
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);
2331
}
2432

2533
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+
2843
struct wrapper_buffer* wbuf = get_wrapper_buffer(device, srcBuffer);
2944
if (!wbuf) {
3045
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+
}
3195
}
32-
// TODO: Implement this
3396
}

src/vulkan/wrapper/artifacts.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ extern "C" {
66

77
#include <vulkan/vulkan_core.h>
88

9-
void RecordBCnArtifacts(struct wrapper_device* device, const VkBufferImageCopy* region, VkBuffer srcBuffer, VkBuffer stagingBuffer, int decode_id);
9+
void RecordBCnArtifacts(struct wrapper_device* device, VkFormat original_format, const VkBufferImageCopy* region, VkBuffer srcBuffer, VkBuffer stagingBuffer, int decode_id);
1010

1111
#ifdef __cplusplus
1212
}

src/vulkan/wrapper/wrapper_device.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ WRAPPER_CreateDevice(VkPhysicalDevice physicalDevice,
336336
device->dispatch_handle);
337337

338338
// Initialize the BCn interceptor states
339-
bool record_artifacts = CHECK_FLAG("RECORD_ARTIFACTS");
340-
bool use_image_view = use_image_view_mode() && !record_artifacts;
339+
bool dump_artifacts = CHECK_FLAG("DUMP_BCN_ARTIFACTS");
340+
bool use_image_view = use_image_view_mode() && !dump_artifacts;
341341

342342
result = InterceptorState_Init(&device->s3tc,
343343
wrapper_device_to_handle(device),
@@ -1414,6 +1414,7 @@ static VkResult HostSideDecompression(
14141414
.sType = VK_STRUCTURE_TYPE_MEMORY_MAP_INFO_KHR,
14151415
.memory = dstMemory,
14161416
.offset = 0, // Assuming dstMemory is large enough to hold the decompressed
1417+
// TODO: FIX THIS for non rgba8 data
14171418
.size = region->imageExtent.width * region->imageExtent.height * 4, // 4 bytes per pixel for RGBA
14181419
};
14191420
result = WCHECK(MapMemory2KHR((VkDevice) _device, &mapInfoDst, &dstData));
@@ -1804,10 +1805,10 @@ WRAPPER_CmdCopyBufferToImage(VkCommandBuffer commandBuffer,
18041805
int decode_id = counter++;
18051806
WLOG("Emulating support for format=%d, decode_id=%d", wimg->original_format, decode_id);
18061807

1807-
bool record_artifacts = CHECK_FLAG("RECORD_ARTIFACTS");
1808+
bool dump_artifacts = CHECK_FLAG("DUMP_BCN_ARTIFACTS");
18081809
bool use_cpu_bcn = (get_host_decoding_bcn_masks() & (1 << (wimg->original_format - 131))) != 0;
18091810
bool use_compute_shader = use_compute_shader_mode() && !use_cpu_bcn;
1810-
bool use_image_view = use_image_view_mode() && !use_cpu_bcn && !record_artifacts;
1811+
bool use_image_view = use_image_view_mode() && !use_cpu_bcn && !dump_artifacts;
18111812

18121813
// Check if the queues are the same
18131814
struct wrapper_command_pool *pool = get_wrapper_command_pool(_device, wcb->pool);
@@ -1877,7 +1878,7 @@ WRAPPER_CmdCopyBufferToImage(VkCommandBuffer commandBuffer,
18771878
args.stagingBuffer = stagingBuffer;
18781879
}
18791880

1880-
if (CHECK_FLAG("WRAPPER_ONE_BY_ONE") || record_artifacts) {
1881+
if (CHECK_FLAG("WRAPPER_ONE_BY_ONE") || dump_artifacts) {
18811882
WLOGD("Submitting decode_id %d", decode_id);
18821883
result = SubmitOneTimeCommands(
18831884
_device,
@@ -1900,9 +1901,9 @@ WRAPPER_CmdCopyBufferToImage(VkCommandBuffer commandBuffer,
19001901
}
19011902
}
19021903

1903-
if (record_artifacts) {
1904+
if (dump_artifacts) {
19041905
// Invariant: srcBuffer contains the BCn blocks, stagingBuffer contains the output
1905-
RecordBCnArtifacts(_device, region, srcBuffer, stagingBuffer, decode_id);
1906+
RecordBCnArtifacts(_device, wimg->original_format, region, srcBuffer, stagingBuffer, decode_id);
19061907
}
19071908

19081909
if (!use_image_view) {

src/vulkan/wrapper/wrapper_private.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,21 @@ static VkFormat unwrap_vk_format(struct wrapper_device* device, VkFormat in_form
214214
return unwrap_vk_format_physical_device(device->physical, in_format);
215215
}
216216

217+
static inline uint32_t get_bc_target_size(struct wrapper_physical_device* pdevice, VkFormat in_format) {
218+
VkFormat out_format = unwrap_vk_format_physical_device(pdevice, in_format);
219+
switch (out_format) {
220+
case VK_FORMAT_R8G8B8A8_UNORM:
221+
case VK_FORMAT_R8G8B8A8_SNORM:
222+
return 4;
223+
case VK_FORMAT_R16G16B16A16_SFLOAT:
224+
return 8;
225+
default:
226+
WLOGE("Unknown out_format: %d", out_format);
227+
return 4;
228+
break;
229+
}
230+
}
231+
217232
typedef struct {
218233
uint32_t srcFormat;
219234
uint32_t srcRowLength;

0 commit comments

Comments
 (0)