-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspv_webgpu_transform.c
More file actions
111 lines (92 loc) · 4.05 KB
/
Copy pathspv_webgpu_transform.c
File metadata and controls
111 lines (92 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "../spirv_webgpu_transform.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define BAD_FILE_PATH "./bad.spv"
void print_set_binding(SpvTransformCorrectionMap map, uint32_t set, uint32_t binding);
int main() {
// 1. Read the SPIRV file
FILE *file = fopen(BAD_FILE_PATH, "rb");
fseek(file, 0, SEEK_END);
int spirv_bytes = ftell(file);
fseek(file, 0, SEEK_SET);
uint8_t *spirv = (uint8_t *)malloc(spirv_bytes);
fread(spirv, 1, spirv_bytes, file);
fclose(file);
// 2. Run the transformations
SpvTransformCorrectionMap correction_map = {};
uint32_t *comb_out_spv;
uint32_t comb_out_count;
spirv_webgpu_transform_combimgsampsplitter_alloc((uint32_t *)spirv, spirv_bytes / 4, &comb_out_spv, &comb_out_count, &correction_map);
uint32_t *dref_out_spv;
uint32_t dref_out_count;
spirv_webgpu_transform_drefsplitter_alloc(comb_out_spv, comb_out_count, &dref_out_spv, &dref_out_count, &correction_map);
uint32_t *isnanisinf_out_spv;
uint32_t isnanisinf_out_count;
spirv_webgpu_transform_isnanisinfpatch_alloc(dref_out_spv, dref_out_count, &isnanisinf_out_spv, &isnanisinf_out_count);
uint32_t *storagecube_out_spv;
uint32_t storagecube_out_count;
spirv_webgpu_transform_storagecubepatch_alloc(isnanisinf_out_spv, isnanisinf_out_count, &storagecube_out_spv, &storagecube_out_count, &correction_map);
uint32_t *pruneunuseddref_out_spv;
uint32_t pruneunuseddref_out_count;
spirv_webgpu_transform_pruneunuseddref_alloc(storagecube_out_spv, storagecube_out_count, &pruneunuseddref_out_spv, &pruneunuseddref_out_count);
uint32_t *immediates_out_spv;
uint32_t immediates_out_count;
spirv_webgpu_transform_immediatespatch_alloc(pruneunuseddref_out_spv, pruneunuseddref_out_count, &immediates_out_spv, &immediates_out_count, &correction_map);
uint32_t *splitbindingarray_out_spv;
uint32_t splitbindingarray_out_count;
spirv_webgpu_transform_splitbindingarray_alloc(immediates_out_spv, immediates_out_count, &splitbindingarray_out_spv, &splitbindingarray_out_count, &correction_map);
// 3. Observe the patched variables
print_set_binding(correction_map, 0, 0);
print_set_binding(correction_map, 0, 1);
print_set_binding(correction_map, 1, 0);
print_set_binding(correction_map, 2, 0);
// Fluke values should return None
print_set_binding(correction_map, 1, 1);
print_set_binding(correction_map, 3, 0);
// Test linking
spirv_webgpu_transform_correction_read_immediates_set(correction_map);
spirv_webgpu_transform_correction_write_immediates_set(&correction_map, 128);
// 4. Free memory
spirv_webgpu_transform_splitbindingarray_free(splitbindingarray_out_spv);
spirv_webgpu_transform_immediatespatch_free(immediates_out_spv);
spirv_webgpu_transform_pruneunuseddref_free(pruneunuseddref_out_spv);
spirv_webgpu_transform_storagecubepatch_free(storagecube_out_spv);
spirv_webgpu_transform_isnanisinfpatch_free(isnanisinf_out_spv);
spirv_webgpu_transform_drefsplitter_free(dref_out_spv);
spirv_webgpu_transform_combimgsampsplitter_free(comb_out_spv);
spirv_webgpu_transform_correction_map_free(correction_map);
free(spirv);
}
void print_set_binding(SpvTransformCorrectionMap map, uint32_t set, uint32_t binding) {
uint16_t *corrections;
uint32_t correction_count;
uint8_t status = spirv_webgpu_transform_correction_sets_index(map, set, binding, &corrections, &correction_count);
printf("For set %d, binding %d:\n", set, binding);
if (status) {
printf("\tSome\n");
} else {
printf("\tNone\n");
}
printf("\t");
for (int i = 0; i < correction_count; i++) {
switch (corrections[i]) {
case SPIRV_WEBGPU_TRANSFORM_CORRECTION_TYPE_SPLIT_COMBINED:
printf("SPLIT_COMBINED ");
break;
case SPIRV_WEBGPU_TRANSFORM_CORRECTION_TYPE_SPLIT_DREF_REGULAR:
printf("SPLIT_DREF_REGULAR ");
break;
case SPIRV_WEBGPU_TRANSFORM_CORRECTION_TYPE_SPLIT_DREF_COMPARISON:
printf("SPLIT_DREF_COMPARISON ");
break;
case SPIRV_WEBGPU_TRANSFORM_CORRECTION_TYPE_CONVERT_STORAGE_CUBE:
printf("CONVERT_STORAGE_CUBE ");
break;
case SPIRV_WEBGPU_TRANSFORM_CORRECTION_TYPE_SPLIT_BINDING_ARRAY:
printf("SPLIT_BINDING_ARRAY ");
break;
}
}
printf("\n");
}