-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
102 lines (87 loc) · 3.04 KB
/
main.c
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
#include <vulkan/vulkan.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 读取 SPIR-V 文件
unsigned char* read_spirv_file(const char* filename, size_t* size) {
FILE* file = fopen(filename, "rb");
if (!file) {
perror("Failed to open SPIR-V file");
exit(1);
}
fseek(file, 0, SEEK_END);
*size = ftell(file);
fseek(file, 0, SEEK_SET);
unsigned char* buffer = (unsigned char*)malloc(*size);
fread(buffer, 1, *size, file);
fclose(file);
return buffer;
}
int main() {
// 读取 SPIR-V 文件
size_t spirv_size;
unsigned char* spirv_code = read_spirv_file("hello.spv", &spirv_size);
// 初始化 Vulkan 实例
VkApplicationInfo app_info = {
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
.pApplicationName = "Hello Vulkan",
.applicationVersion = VK_MAKE_VERSION(1, 0, 0),
.pEngineName = "No Engine",
.engineVersion = VK_MAKE_VERSION(1, 0, 0),
.apiVersion = VK_API_VERSION_1_0,
};
VkInstanceCreateInfo create_info = {
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
.pApplicationInfo = &app_info,
};
VkInstance instance;
if (vkCreateInstance(&create_info, NULL, &instance) != VK_SUCCESS) {
fprintf(stderr, "Failed to create Vulkan instance\n");
return 1;
}
// 获取物理设备
uint32_t device_count = 0;
vkEnumeratePhysicalDevices(instance, &device_count, NULL);
if (device_count == 0) {
fprintf(stderr, "Failed to find GPUs with Vulkan support\n");
return 1;
}
VkPhysicalDevice physical_device;
vkEnumeratePhysicalDevices(instance, &device_count, &physical_device);
// 创建逻辑设备
VkDeviceQueueCreateInfo queue_create_info = {
.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
.queueFamilyIndex = 0,
.queueCount = 1,
};
float queue_priority = 1.0f;
queue_create_info.pQueuePriorities = &queue_priority;
VkDeviceCreateInfo device_create_info = {
.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,
.queueCreateInfoCount = 1,
.pQueueCreateInfos = &queue_create_info,
};
VkDevice device;
if (vkCreateDevice(physical_device, &device_create_info, NULL, &device) != VK_SUCCESS) {
fprintf(stderr, "Failed to create logical device\n");
return 1;
}
// 创建着色器模块
VkShaderModuleCreateInfo shader_module_create_info = {
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
.codeSize = spirv_size,
.pCode = (const uint32_t*)spirv_code,
};
VkShaderModule shader_module;
if (vkCreateShaderModule(device, &shader_module_create_info, NULL, &shader_module) != VK_SUCCESS) {
fprintf(stderr, "Failed to create shader module\n");
return 1;
}
// 清理资源
vkDestroyShaderModule(device, shader_module, NULL);
vkDestroyDevice(device, NULL);
vkDestroyInstance(instance, NULL);
free(spirv_code);
printf("Successfully loaded and created shader module\n");
return 0;
}