Debian Codegraph @

primus_vk_diag.cpp

raw symbols SCIP
1 #include <GL/glx.h>
2 #include <GL/gl.h>
3 #include <unistd.h>
4 #include <iostream>
5
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdio.h>
9
10 #include <vulkan/vulkan.h>
11 #include <vector>
12 #include <memory>
13
14 #include <dlfcn.h>
15
16 #define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
17 #define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
18 typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
19
20 class VulkanContext {
21 VkInstance instance;
22 public:
23 VulkanContext();
24 VulkanContext(const VulkanContext&) = delete;
25 ~VulkanContext();
26 };
27 #define VK_CHECK() if(reply != VK_SUCCESS){ \
28 throw std::runtime_error("Vulkan operation failed with code: " + std::to_string(reply)); \
29 }
30
31
32 #define GL_FUNCTIONS GL_FUNCTION(glClearColor);\
33 GL_FUNCTION(glClear);\
34 GL_FUNCTION(glXChooseFBConfig);\
35 GL_FUNCTION(glXCreateContext);\
36 GL_FUNCTION(glXDestroyContext);\
37 GL_FUNCTION(glXGetProcAddress);\
38 GL_FUNCTION(glXGetVisualFromFBConfig);\
39 GL_FUNCTION(glXMakeCurrent);\
40 GL_FUNCTION(glXQueryExtensionsString);\
41 GL_FUNCTION(glXSwapBuffers);\
42
43
44 struct GLLib {
45 #define GL_FUNCTION(x) decltype(&x) ptr_##x;
46 GL_FUNCTIONS
47 #undef GL_FUNCTION
48 GLLib() {
49 void* handle = dlopen("libGL.so.1", RTLD_NOW|RTLD_GLOBAL);
50 #define GL_FUNCTION(x) ptr_##x = (decltype(&x)) dlsym(handle, #x)
51 GL_FUNCTIONS
52 #undef GL_FUNCTION
53 }
54 };
55
56 const auto self = std::string{"PrimusVK-diagnostic: "};
57
58 VulkanContext::VulkanContext(){
59 std::cout << self << "Creating Vulkan instance" << std::endl;
60 VkInstanceCreateInfo instanceCreateInfo = {};
61 instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
62 instanceCreateInfo.pNext = NULL;
63 instanceCreateInfo.pApplicationInfo = nullptr;
64 auto reply = vkCreateInstance(&instanceCreateInfo, nullptr, &instance);
65 VK_CHECK();
66
67 uint32_t gpuCount;
68 reply = vkEnumeratePhysicalDevices(instance, &gpuCount, nullptr);
69 VK_CHECK();
70 // Enumerate devices
71 std::vector<VkPhysicalDevice> physicalDevices(gpuCount);
72 vkEnumeratePhysicalDevices(instance, &gpuCount, physicalDevices.data());
73 VK_CHECK();
74 for ( auto &device : physicalDevices) {
75 VkPhysicalDeviceProperties deviceProperties;
76 vkGetPhysicalDeviceProperties(device, &deviceProperties);
77 std::cout << self << "Device: " << deviceProperties.deviceName << std::endl;
78 std::cout << self << " Type: " << deviceProperties.deviceType << std::endl;
79 std::cout << self << " API: " << (deviceProperties.apiVersion >> 22) << "." << ((deviceProperties.apiVersion >> 12) & 0x3ff) << "." << (deviceProperties.apiVersion & 0xfff) << std::endl;
80 uint32_t queues;
81 vkGetPhysicalDeviceQueueFamilyProperties(device, &queues, nullptr);
82 std::vector<VkQueueFamilyProperties> data(queues);
83 vkGetPhysicalDeviceQueueFamilyProperties(device, &queues, data.data());
84 std::cout << self << " Queues: " << queues << std::endl;
85
86 VkDeviceQueueCreateInfo queue1{};
87 queue1.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
88 queue1.queueFamilyIndex = 0;
89 queue1.queueCount = 1;
90 float prio = 1;
91 queue1.pQueuePriorities = &prio;
92 VkDeviceCreateInfo createInfo{};
93 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
94 createInfo.pQueueCreateInfos = &queue1;
95 createInfo.queueCreateInfoCount = 1;
96 VkDevice dev;
97 VkResult res = vkCreateDevice(device, &createInfo, nullptr, &dev);
98 if(res == VK_SUCCESS) {
99 std::cout << "Device creation succeeded\n";
100 } else {
101 std::cout << "Device creation failed: " << res << "\n";
102 }
103 vkDestroyDevice(dev, nullptr);
104 }
105 }
106 VulkanContext::~VulkanContext(){
107 std::cout << self << "Destroying Vulkan: " << instance << std::endl;
108 vkDestroyInstance(instance, nullptr);
109 }
110
111 class XWindowContext;
112 class GLContext {
113 GLXContext ctx;
114 XWindowContext &data;
115 std::shared_ptr<GLLib> gl;
116 public:
117 GLContext(XWindowContext &data);
118 void drawSample();
119 ~GLContext();
120 };
121
122 class XWindowContext {
123 public:
124 Display *display;
125 XVisualInfo *vi;
126
127 Window win;
128 GLXFBConfig fbconfig;
129 std::shared_ptr<GLLib> gl = std::make_shared<GLLib>();
130
131 XWindowContext(Display *display): display(display){
132 const char *extensions = gl->ptr_glXQueryExtensionsString(display, DefaultScreen(display));
133 std::cout << self << extensions << std::endl;
134
135 static int visual_attribs[] =
136 {
137 GLX_RENDER_TYPE, GLX_RGBA_BIT,
138 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
139 GLX_DOUBLEBUFFER, true,
140 GLX_RED_SIZE, 1,
141 GLX_GREEN_SIZE, 1,
142 GLX_BLUE_SIZE, 1,
143 None
144 };
145
146 std::cout << self << "Getting framebuffer config" << std::endl;
147 int fbcount;
148 GLXFBConfig *fbc = gl->ptr_glXChooseFBConfig(display, DefaultScreen(display), visual_attribs, &fbcount);
149 if (!fbc) {
150 throw std::runtime_error("Failed to retrieve a framebuffer config");
151 }
152 fbconfig = fbc[0];
153
154 vi = gl->ptr_glXGetVisualFromFBConfig(display, fbconfig);
155
156 XSetWindowAttributes swa;
157 swa.colormap = XCreateColormap(display, RootWindow(display, vi->screen), vi->visual, AllocNone);
158 swa.border_pixel = 0;
159 swa.event_mask = StructureNotifyMask;
160
161 std::cout << self << "Creating window" << std::endl;
162 win = XCreateWindow(display, RootWindow(display, vi->screen), 0, 0, 100, 100, 0, vi->depth, InputOutput, vi->visual, CWBorderPixel|CWColormap|CWEventMask, &swa);
163 if (!win) {
164 throw std::runtime_error("Failed to create window.");
165 }
166 XMapWindow(display, win);
167 }
168 };
169
170 GLContext::GLContext(XWindowContext &data): data(data), gl(data.gl){
171 // Create an oldstyle context first, to get the correct function pointer for glXCreateContextAttribsARB
172 GLXContext ctx_old = gl->ptr_glXCreateContext(data.display, data.vi, 0, GL_TRUE);
173 const auto glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc)gl->ptr_glXGetProcAddress((const GLubyte*)"glXCreateContextAttribsARB");
174 gl->ptr_glXMakeCurrent(data.display, 0, 0);
175 gl->ptr_glXDestroyContext(data.display, ctx_old);
176
177 if (glXCreateContextAttribsARB == NULL) {
178 throw std::runtime_error("glXCreateContextAttribsARB entry point not found. Aborting.");
179 }
180
181 static int context_attribs[] =
182 {
183 GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
184 GLX_CONTEXT_MINOR_VERSION_ARB, 0,
185 None
186 };
187
188 std::cout << self << "Creating context" << std::endl;
189 ctx = glXCreateContextAttribsARB(data.display, data.fbconfig, NULL, true, context_attribs);
190 if (!ctx) {
191 throw std::runtime_error("Failed to create GL3 context.");
192 }
193
194 gl->ptr_glXMakeCurrent(data.display, data.win, ctx);
195 }
196 void GLContext::drawSample(){
197 std::cout << self << "Rendering with GL" << std::endl;
198 gl->ptr_glClearColor (0, 0.5, 1, 1);
199 gl->ptr_glClear (GL_COLOR_BUFFER_BIT);
200 gl->ptr_glXSwapBuffers (data.display, data.win);
201
202 auto toSleep = timespec{};
203 toSleep.tv_nsec=200000000;
204 nanosleep(&toSleep, nullptr);
205
206 gl->ptr_glClearColor (1, 0.5, 0, 1);
207 gl->ptr_glClear (GL_COLOR_BUFFER_BIT);
208 gl->ptr_glXSwapBuffers (data.display, data.win);
209
210 nanosleep(&toSleep, nullptr);
211 }
212 GLContext::~GLContext(){
213 std::cout << self << "Destroying GL context" << std::endl;
214 gl->ptr_glXMakeCurrent(data.display, 0, 0);
215 gl->ptr_glXDestroyContext(data.display, ctx);
216 }
217
218 int main (int argc, char ** argv) {
219 Display *display = XOpenDisplay(0);
220 for(int i = 1; i < argc; i++){
221 std::string arg = argv[i];
222 if(arg == "gl"){
223 std::cout << self << "Loading GL." << std::endl;
224 auto winContext = std::make_shared<XWindowContext>(display);
225 GLContext context = GLContext{*winContext};
226 context.drawSample();
227 } else if(arg == "vulkan") {
228 VulkanContext context;
229 }
230 }
231 return 0;
232 }