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