Debian Codegraph @

third_party/spirv-cross/samples/cpp/shared.cpp

raw
1 /*
2 * Copyright 2015-2017 ARM Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "spirv_cross/external_interface.h"
18 #include <stdio.h>
19
20 #ifndef GLM_SWIZZLE
21 #define GLM_SWIZZLE
22 #endif
23
24 #ifndef GLM_FORCE_RADIANS
25 #define GLM_FORCE_RADIANS
26 #endif
27
28 #include <glm/glm.hpp>
29 using namespace glm;
30
31 int main()
32 {
33 // First, we get the C interface to the shader.
34 // This can be loaded from a dynamic library, or as here,
35 // linked in as a static library.
36 auto *iface = spirv_cross_get_interface();
37
38 // Create an instance of the shader interface.
39 auto *shader = iface->construct();
40
41 // Build some input data for our compute shader.
42 #define NUM_WORKGROUPS 4
43 float a[128 * NUM_WORKGROUPS];
44 float b[NUM_WORKGROUPS] = {};
45
46 for (int i = 0; i < 128 * NUM_WORKGROUPS; i++)
47 {
48 a[i] = float(i);
49 }
50
51 void *aptr = a;
52 void *bptr = b;
53
54 // Bind resources to the shader.
55 // For resources like samplers and buffers, we provide a list of pointers,
56 // since UBOs, SSBOs and samplers can be arrays, and can point to different types,
57 // which is especially true for samplers.
58 spirv_cross_set_resource(shader, 0, 0, &aptr, sizeof(aptr));
59 spirv_cross_set_resource(shader, 0, 1, &bptr, sizeof(bptr));
60
61 // We also have to set builtins.
62 // The relevant builtins will depend on the shader,
63 // but for compute, there are few builtins, which are gl_NumWorkGroups and gl_WorkGroupID.
64 // LocalInvocationID and GlobalInvocationID are inferred when executing the invocation.
65 uvec3 num_workgroups(NUM_WORKGROUPS, 1, 1);
66 uvec3 work_group_id(0, 0, 0);
67 spirv_cross_set_builtin(shader, SPIRV_CROSS_BUILTIN_NUM_WORK_GROUPS, &num_workgroups, sizeof(num_workgroups));
68 spirv_cross_set_builtin(shader, SPIRV_CROSS_BUILTIN_WORK_GROUP_ID, &work_group_id, sizeof(work_group_id));
69
70 // Execute 4 work groups.
71 for (unsigned i = 0; i < NUM_WORKGROUPS; i++)
72 {
73 work_group_id.x = i;
74 iface->invoke(shader);
75 }
76
77 // Call destructor.
78 iface->destruct(shader);
79
80 // Verify our output.
81 // TODO: Implement a test framework that asserts results computed.
82 for (unsigned i = 0; i < NUM_WORKGROUPS; i++)
83 {
84 float expected_sum = 0.0f;
85 for (unsigned j = i * 128; j < (i + 1) * 128; j++)
86 expected_sum += a[j];
87 fprintf(stderr, "Sum in workgroup #%u = %.1f, expected %.1f\n", i, b[i], expected_sum);
88 }
89 }