libs/vulkan-compute/test_buffers.c
1/* test_buffers.c - Test buffer upload/download operations
2 *
3 * Verifies that the library can create buffers and transfer data.
4 */
5
6#include "include/vk_compute.h"
7#include <stdio.h>
8#include <stdlib.h>
9#include <math.h>
10
11#define BUFFER_SIZE (1024 * sizeof(float)) /* 1KB test buffer */
12
13int main(void) {
14 printf("=== Vulkan Buffer Test ===\n\n");
15
16 /* Initialize */
17 VkComputeContext* ctx = vkc_init(false);
18 if (!ctx) {
19 fprintf(stderr, "ERROR: Failed to initialize Vulkan\n");
20 return 1;
21 }
22
23 /* Create test data */
24 float* input_data = malloc(BUFFER_SIZE);
25 float* output_data = malloc(BUFFER_SIZE);
26
27 for (size_t i = 0; i < BUFFER_SIZE / sizeof(float); i++) {
28 input_data[i] = (float)i * 0.1f;
29 }
30
31 printf("[TEST 1] Device-local buffer upload/download\n");
32
33 /* Create device-local buffer */
34 VkComputeBuffer* device_buffer = vkc_create_buffer(ctx, BUFFER_SIZE,
35 VKC_BUFFER_DEVICE_LOCAL);
36 if (!device_buffer) {
37 fprintf(stderr, "ERROR: Failed to create device buffer\n");
38 vkc_destroy(ctx);
39 return 1;
40 }
41
42 /* Upload data */
43 VkComputeResult result = vkc_upload_buffer(ctx, device_buffer, input_data, BUFFER_SIZE);
44 if (result != VKC_SUCCESS) {
45 fprintf(stderr, "ERROR: Failed to upload buffer: %s\n", vkc_get_error_string(result));
46 vkc_destroy_buffer(ctx, device_buffer);
47 vkc_destroy(ctx);
48 return 1;
49 }
50
51 printf(" Uploaded %zu bytes to device buffer\n", BUFFER_SIZE);
52
53 /* Download data */
54 result = vkc_download_buffer(ctx, device_buffer, output_data, BUFFER_SIZE);
55 if (result != VKC_SUCCESS) {
56 fprintf(stderr, "ERROR: Failed to download buffer: %s\n", vkc_get_error_string(result));
57 vkc_destroy_buffer(ctx, device_buffer);
58 vkc_destroy(ctx);
59 return 1;
60 }
61
62 printf(" Downloaded %zu bytes from device buffer\n", BUFFER_SIZE);
63
64 /* Verify data */
65 bool verified = true;
66 for (size_t i = 0; i < BUFFER_SIZE / sizeof(float); i++) {
67 if (fabs(input_data[i] - output_data[i]) > 0.0001f) {
68 fprintf(stderr, "ERROR: Data mismatch at index %zu: %.3f != %.3f\n",
69 i, input_data[i], output_data[i]);
70 verified = false;
71 break;
72 }
73 }
74
75 if (verified) {
76 printf(" [SUCCESS] Data verified correctly\n");
77 } else {
78 fprintf(stderr, " [FAILED] Data verification failed\n");
79 vkc_destroy_buffer(ctx, device_buffer);
80 vkc_destroy(ctx);
81 return 1;
82 }
83
84 vkc_destroy_buffer(ctx, device_buffer);
85
86 printf("\n[TEST 2] Host-visible buffer operations\n");
87
88 /* Create host-visible buffer */
89 VkComputeBuffer* host_buffer = vkc_create_buffer(ctx, BUFFER_SIZE,
90 VKC_BUFFER_HOST_VISIBLE);
91 if (!host_buffer) {
92 fprintf(stderr, "ERROR: Failed to create host buffer\n");
93 vkc_destroy(ctx);
94 return 1;
95 }
96
97 /* Upload and download */
98 result = vkc_upload_buffer(ctx, host_buffer, input_data, BUFFER_SIZE);
99 if (result != VKC_SUCCESS) {
100 fprintf(stderr, "ERROR: Failed to upload to host buffer\n");
101 vkc_destroy_buffer(ctx, host_buffer);
102 vkc_destroy(ctx);
103 return 1;
104 }
105
106 result = vkc_download_buffer(ctx, host_buffer, output_data, BUFFER_SIZE);
107 if (result != VKC_SUCCESS) {
108 fprintf(stderr, "ERROR: Failed to download from host buffer\n");
109 vkc_destroy_buffer(ctx, host_buffer);
110 vkc_destroy(ctx);
111 return 1;
112 }
113
114 /* Verify */
115 verified = true;
116 for (size_t i = 0; i < BUFFER_SIZE / sizeof(float); i++) {
117 if (fabs(input_data[i] - output_data[i]) > 0.0001f) {
118 verified = false;
119 break;
120 }
121 }
122
123 if (verified) {
124 printf(" [SUCCESS] Host-visible buffer works correctly\n");
125 } else {
126 fprintf(stderr, " [FAILED] Host buffer verification failed\n");
127 vkc_destroy_buffer(ctx, host_buffer);
128 vkc_destroy(ctx);
129 return 1;
130 }
131
132 vkc_destroy_buffer(ctx, host_buffer);
133
134 /* Cleanup */
135 free(input_data);
136 free(output_data);
137 vkc_destroy(ctx);
138
139 printf("\n[SUCCESS] All buffer tests passed!\n");
140 return 0;
141}
142