Convert glfwinfo to C99

This commit is contained in:
Camilla Löwy 2019-05-19 19:10:35 +02:00
parent 700d1f28d8
commit fa025d8f80

View File

@ -30,6 +30,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdbool.h>
#include "getopt.h" #include "getopt.h"
@ -175,22 +176,19 @@ static const char* get_strategy_name_glfw(int strategy)
static void list_context_extensions(int client, int major, int minor) static void list_context_extensions(int client, int major, int minor)
{ {
int i;
GLint count;
const GLubyte* extensions;
printf("%s context extensions:\n", get_api_name(client)); printf("%s context extensions:\n", get_api_name(client));
if (client == GLFW_OPENGL_API && major > 2) if (client == GLFW_OPENGL_API && major > 2)
{ {
GLint count;
glGetIntegerv(GL_NUM_EXTENSIONS, &count); glGetIntegerv(GL_NUM_EXTENSIONS, &count);
for (i = 0; i < count; i++) for (int i = 0; i < count; i++)
printf(" %s\n", (const char*) glGetStringi(GL_EXTENSIONS, i)); printf(" %s\n", (const char*) glGetStringi(GL_EXTENSIONS, i));
} }
else else
{ {
extensions = glGetString(GL_EXTENSIONS); const GLubyte* extensions = glGetString(GL_EXTENSIONS);
while (*extensions != '\0') while (*extensions != '\0')
{ {
putchar(' '); putchar(' ');
@ -211,15 +209,13 @@ static void list_context_extensions(int client, int major, int minor)
static void list_vulkan_instance_extensions(void) static void list_vulkan_instance_extensions(void)
{ {
uint32_t i, ep_count = 0;
VkExtensionProperties* ep;
printf("Vulkan instance extensions:\n"); printf("Vulkan instance extensions:\n");
uint32_t ep_count;
if (vkEnumerateInstanceExtensionProperties(NULL, &ep_count, NULL) != VK_SUCCESS) if (vkEnumerateInstanceExtensionProperties(NULL, &ep_count, NULL) != VK_SUCCESS)
return; return;
ep = calloc(ep_count, sizeof(VkExtensionProperties)); VkExtensionProperties* ep = calloc(ep_count, sizeof(VkExtensionProperties));
if (vkEnumerateInstanceExtensionProperties(NULL, &ep_count, ep) != VK_SUCCESS) if (vkEnumerateInstanceExtensionProperties(NULL, &ep_count, ep) != VK_SUCCESS)
{ {
@ -227,7 +223,7 @@ static void list_vulkan_instance_extensions(void)
return; return;
} }
for (i = 0; i < ep_count; i++) for (uint32_t i = 0; i < ep_count; i++)
printf(" %s (v%u)\n", ep[i].extensionName, ep[i].specVersion); printf(" %s (v%u)\n", ep[i].extensionName, ep[i].specVersion);
free(ep); free(ep);
@ -235,15 +231,13 @@ static void list_vulkan_instance_extensions(void)
static void list_vulkan_instance_layers(void) static void list_vulkan_instance_layers(void)
{ {
uint32_t i, lp_count = 0;
VkLayerProperties* lp;
printf("Vulkan instance layers:\n"); printf("Vulkan instance layers:\n");
uint32_t lp_count;
if (vkEnumerateInstanceLayerProperties(&lp_count, NULL) != VK_SUCCESS) if (vkEnumerateInstanceLayerProperties(&lp_count, NULL) != VK_SUCCESS)
return; return;
lp = calloc(lp_count, sizeof(VkLayerProperties)); VkLayerProperties* lp = calloc(lp_count, sizeof(VkLayerProperties));
if (vkEnumerateInstanceLayerProperties(&lp_count, lp) != VK_SUCCESS) if (vkEnumerateInstanceLayerProperties(&lp_count, lp) != VK_SUCCESS)
{ {
@ -251,7 +245,7 @@ static void list_vulkan_instance_layers(void)
return; return;
} }
for (i = 0; i < lp_count; i++) for (uint32_t i = 0; i < lp_count; i++)
{ {
printf(" %s (v%u) \"%s\"\n", printf(" %s (v%u) \"%s\"\n",
lp[i].layerName, lp[i].layerName,
@ -264,15 +258,13 @@ static void list_vulkan_instance_layers(void)
static void list_vulkan_device_extensions(VkInstance instance, VkPhysicalDevice device) static void list_vulkan_device_extensions(VkInstance instance, VkPhysicalDevice device)
{ {
uint32_t i, ep_count;
VkExtensionProperties* ep;
printf("Vulkan device extensions:\n"); printf("Vulkan device extensions:\n");
uint32_t ep_count;
if (vkEnumerateDeviceExtensionProperties(device, NULL, &ep_count, NULL) != VK_SUCCESS) if (vkEnumerateDeviceExtensionProperties(device, NULL, &ep_count, NULL) != VK_SUCCESS)
return; return;
ep = calloc(ep_count, sizeof(VkExtensionProperties)); VkExtensionProperties* ep = calloc(ep_count, sizeof(VkExtensionProperties));
if (vkEnumerateDeviceExtensionProperties(device, NULL, &ep_count, ep) != VK_SUCCESS) if (vkEnumerateDeviceExtensionProperties(device, NULL, &ep_count, ep) != VK_SUCCESS)
{ {
@ -280,7 +272,7 @@ static void list_vulkan_device_extensions(VkInstance instance, VkPhysicalDevice
return; return;
} }
for (i = 0; i < ep_count; i++) for (uint32_t i = 0; i < ep_count; i++)
printf(" %s (v%u)\n", ep[i].extensionName, ep[i].specVersion); printf(" %s (v%u)\n", ep[i].extensionName, ep[i].specVersion);
free(ep); free(ep);
@ -288,15 +280,13 @@ static void list_vulkan_device_extensions(VkInstance instance, VkPhysicalDevice
static void list_vulkan_device_layers(VkInstance instance, VkPhysicalDevice device) static void list_vulkan_device_layers(VkInstance instance, VkPhysicalDevice device)
{ {
uint32_t i, lp_count;
VkLayerProperties* lp;
printf("Vulkan device layers:\n"); printf("Vulkan device layers:\n");
uint32_t lp_count;
if (vkEnumerateDeviceLayerProperties(device, &lp_count, NULL) != VK_SUCCESS) if (vkEnumerateDeviceLayerProperties(device, &lp_count, NULL) != VK_SUCCESS)
return; return;
lp = calloc(lp_count, sizeof(VkLayerProperties)); VkLayerProperties* lp = calloc(lp_count, sizeof(VkLayerProperties));
if (vkEnumerateDeviceLayerProperties(device, &lp_count, lp) != VK_SUCCESS) if (vkEnumerateDeviceLayerProperties(device, &lp_count, lp) != VK_SUCCESS)
{ {
@ -304,7 +294,7 @@ static void list_vulkan_device_layers(VkInstance instance, VkPhysicalDevice devi
return; return;
} }
for (i = 0; i < lp_count; i++) for (uint32_t i = 0; i < lp_count; i++)
{ {
printf(" %s (v%u) \"%s\"\n", printf(" %s (v%u) \"%s\"\n",
lp[i].layerName, lp[i].layerName,
@ -352,11 +342,8 @@ static GLADapiproc glad_vulkan_callback(const char* name, void* user)
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
int ch, client, major, minor, revision, profile; int ch;
GLint redbits, greenbits, bluebits, alphabits, depthbits, stencilbits; bool list_extensions = false, list_layers = false;
int list_extensions = GLFW_FALSE, list_layers = GLFW_FALSE;
GLenum error;
GLFWwindow* window;
enum { CLIENT, CONTEXT, BEHAVIOR, DEBUG, FORWARD, HELP, EXTENSIONS, LAYERS, enum { CLIENT, CONTEXT, BEHAVIOR, DEBUG, FORWARD, HELP, EXTENSIONS, LAYERS,
MAJOR, MINOR, PROFILE, ROBUSTNESS, VERSION, MAJOR, MINOR, PROFILE, ROBUSTNESS, VERSION,
@ -623,7 +610,7 @@ int main(int argc, char** argv)
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
window = glfwCreateWindow(200, 200, "Version", NULL, NULL); GLFWwindow* window = glfwCreateWindow(200, 200, "Version", NULL, NULL);
if (!window) if (!window)
{ {
glfwTerminate(); glfwTerminate();
@ -633,17 +620,17 @@ int main(int argc, char** argv)
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
gladLoadGL(glfwGetProcAddress); gladLoadGL(glfwGetProcAddress);
error = glGetError(); const GLenum error = glGetError();
if (error != GL_NO_ERROR) if (error != GL_NO_ERROR)
printf("*** OpenGL error after make current: 0x%08x ***\n", error); printf("*** OpenGL error after make current: 0x%08x ***\n", error);
// Report client API version // Report client API version
client = glfwGetWindowAttrib(window, GLFW_CLIENT_API); const int client = glfwGetWindowAttrib(window, GLFW_CLIENT_API);
major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR); const int major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR); const int minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
revision = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION); const int revision = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION);
profile = glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE); const int profile = glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE);
printf("%s context version string: \"%s\"\n", printf("%s context version string: \"%s\"\n",
get_api_name(client), get_api_name(client),
@ -735,6 +722,8 @@ int main(int argc, char** argv)
printf("%s framebuffer:\n", get_api_name(client)); printf("%s framebuffer:\n", get_api_name(client));
GLint redbits, greenbits, bluebits, alphabits, depthbits, stencilbits;
if (client == GLFW_OPENGL_API && profile == GLFW_OPENGL_CORE_PROFILE) if (client == GLFW_OPENGL_API && profile == GLFW_OPENGL_CORE_PROFILE)
{ {
glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER,
@ -809,16 +798,10 @@ int main(int argc, char** argv)
if (glfwVulkanSupported()) if (glfwVulkanSupported())
{ {
uint32_t loader_version = VK_API_VERSION_1_0;
uint32_t i, re_count, pd_count;
const char** re;
VkApplicationInfo ai = {0};
VkInstanceCreateInfo ici = {0};
VkInstance instance;
VkPhysicalDevice* pd;
gladLoadVulkanUserPtr(NULL, glad_vulkan_callback, NULL); gladLoadVulkanUserPtr(NULL, glad_vulkan_callback, NULL);
uint32_t loader_version = VK_API_VERSION_1_0;
if (vkEnumerateInstanceVersion) if (vkEnumerateInstanceVersion)
{ {
uint32_t version; uint32_t version;
@ -830,12 +813,13 @@ int main(int argc, char** argv)
VK_VERSION_MAJOR(loader_version), VK_VERSION_MAJOR(loader_version),
VK_VERSION_MINOR(loader_version)); VK_VERSION_MINOR(loader_version));
re = glfwGetRequiredInstanceExtensions(&re_count); uint32_t re_count;
const char** re = glfwGetRequiredInstanceExtensions(&re_count);
printf("Vulkan required instance extensions:"); printf("Vulkan required instance extensions:");
if (re) if (re)
{ {
for (i = 0; i < re_count; i++) for (uint32_t i = 0; i < re_count; i++)
printf(" %s", re[i]); printf(" %s", re[i]);
putchar('\n'); putchar('\n');
} }
@ -848,7 +832,7 @@ int main(int argc, char** argv)
if (list_layers) if (list_layers)
list_vulkan_instance_layers(); list_vulkan_instance_layers();
ai.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; VkApplicationInfo ai = { VK_STRUCTURE_TYPE_APPLICATION_INFO };
ai.pApplicationName = "glfwinfo"; ai.pApplicationName = "glfwinfo";
ai.applicationVersion = VK_MAKE_VERSION(GLFW_VERSION_MAJOR, ai.applicationVersion = VK_MAKE_VERSION(GLFW_VERSION_MAJOR,
GLFW_VERSION_MINOR, GLFW_VERSION_MINOR,
@ -859,11 +843,13 @@ int main(int argc, char** argv)
else else
ai.apiVersion = VK_API_VERSION_1_0; ai.apiVersion = VK_API_VERSION_1_0;
ici.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; VkInstanceCreateInfo ici = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO };
ici.pApplicationInfo = &ai; ici.pApplicationInfo = &ai;
ici.enabledExtensionCount = re_count; ici.enabledExtensionCount = re_count;
ici.ppEnabledExtensionNames = re; ici.ppEnabledExtensionNames = re;
VkInstance instance = VK_NULL_HANDLE;
if (vkCreateInstance(&ici, NULL, &instance) != VK_SUCCESS) if (vkCreateInstance(&ici, NULL, &instance) != VK_SUCCESS)
{ {
glfwTerminate(); glfwTerminate();
@ -872,6 +858,7 @@ int main(int argc, char** argv)
gladLoadVulkanUserPtr(NULL, glad_vulkan_callback, instance); gladLoadVulkanUserPtr(NULL, glad_vulkan_callback, instance);
uint32_t pd_count;
if (vkEnumeratePhysicalDevices(instance, &pd_count, NULL) != VK_SUCCESS) if (vkEnumeratePhysicalDevices(instance, &pd_count, NULL) != VK_SUCCESS)
{ {
vkDestroyInstance(instance, NULL); vkDestroyInstance(instance, NULL);
@ -879,7 +866,7 @@ int main(int argc, char** argv)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
pd = calloc(pd_count, sizeof(VkPhysicalDevice)); VkPhysicalDevice* pd = calloc(pd_count, sizeof(VkPhysicalDevice));
if (vkEnumeratePhysicalDevices(instance, &pd_count, pd) != VK_SUCCESS) if (vkEnumeratePhysicalDevices(instance, &pd_count, pd) != VK_SUCCESS)
{ {
@ -889,7 +876,7 @@ int main(int argc, char** argv)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
for (i = 0; i < pd_count; i++) for (uint32_t i = 0; i < pd_count; i++)
{ {
VkPhysicalDeviceProperties pdp; VkPhysicalDeviceProperties pdp;