Debian Codegraph @

third_party/spirv-cross/samples/cpp/atomics.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[64 * NUM_WORKGROUPS];
44 float b[64 * NUM_WORKGROUPS] = {};
45 uint32_t counter = 0;
46
47 for (int i = 0; i < 64 * NUM_WORKGROUPS; i++)
48 {
49 a[i] = i * 0.46f;
50 }
51
52 void *aptr = a;
53 void *bptr = b;
54 void *cptr = &counter;
55
56 // Bind resources to the shader.
57 // For resources like samplers and buffers, we provide a list of pointers,
58 // since UBOs, SSBOs and samplers can be arrays, and can point to different types,
59 // which is especially true for samplers.
60 spirv_cross_set_resource(shader, 0, 0, &aptr, sizeof(aptr));
61 spirv_cross_set_resource(shader, 0, 1, &bptr, sizeof(bptr));
62 spirv_cross_set_resource(shader, 0, 2, &cptr, sizeof(cptr));
63
64 // We also have to set builtins.
65 // The relevant builtins will depend on the shader,
66 // but for compute, there are few builtins, which are gl_NumWorkGroups and gl_WorkGroupID.
67 // LocalInvocationID and GlobalInvocationID are inferred when executing the invocation.
68 uvec3 num_workgroups(NUM_WORKGROUPS, 1, 1);
69 uvec3 work_group_id(0, 0, 0);
70 spirv_cross_set_builtin(shader, SPIRV_CROSS_BUILTIN_NUM_WORK_GROUPS, &num_workgroups, sizeof(num_workgroups));
71 spirv_cross_set_builtin(shader, SPIRV_CROSS_BUILTIN_WORK_GROUP_ID, &work_group_id, sizeof(work_group_id));
72
73 // Execute 4 work groups.
74 for (unsigned i = 0; i < NUM_WORKGROUPS; i++)
75 {
76 work_group_id.x = i;
77 iface->invoke(shader);
78 }
79
80 // Call destructor.
81 iface->destruct(shader);
82
83 // Verify our output.
84 // TODO: Implement a test framework that asserts results computed.
85 fprintf(stderr, "Counter = %u\n", counter);
86 for (unsigned i = 0; i < counter; i++)
87 {
88 fprintf(stderr, "[%3u] = %.1f\n", i, b[i]);
89 }
90 }