From 0854a0f3a0aa9d5c51b8d46686ce02723f14a666 Mon Sep 17 00:00:00 2001 From: Jordan Halase Date: Sun, 27 Oct 2019 23:08:04 -0500 Subject: First Commit --- main.c | 162 +++++++++++++++++++++++++++++++++++++++++++++ vkpugltest.sln | 31 +++++++++ vkpugltest.vcxproj | 136 +++++++++++++++++++++++++++++++++++++ vkpugltest.vcxproj.filters | 22 ++++++ vkpugltest.vcxproj.user | 4 ++ 5 files changed, 355 insertions(+) create mode 100644 main.c create mode 100644 vkpugltest.sln create mode 100644 vkpugltest.vcxproj create mode 100644 vkpugltest.vcxproj.filters create mode 100644 vkpugltest.vcxproj.user diff --git a/main.c b/main.c new file mode 100644 index 0000000..cf59f16 --- /dev/null +++ b/main.c @@ -0,0 +1,162 @@ +/* +Copyright (c) 2019, Jordan Halase + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND +FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. +*/ + +// TODO: Create debug report callback + +#include +#include + +#include + +#if defined(_WIN32) +#include +#include +#else +#include +#include +#endif + +struct RenderVulkan { + /** Put `device` first in struct to reduce pointer arithmetic on most Vulkan functions */ + VkDevice device; + VkInstance instance; +}; + +struct RenderVulkan *vk; + +#if defined(_WIN32) +void getRequiredInstanceExtensions(unsigned *nRequired, const char **extensions) +{ + static const char *const required[] = { + VK_KHR_SURFACE_EXTENSION_NAME, + VK_KHR_WIN32_SURFACE_EXTENSION_NAME + }; + static const unsigned num = sizeof(required) / sizeof(required[0]); + if (extensions) { + for (int i = 0; i < num; ++i) { + extensions[i] = required[i]; + } + } else { + *nRequired = num; + } +} +#else +void getRequiredInstanceExtensions(unsigned *nRequired, const char **const extensions) +{ + static const char *const required[] = { + VK_KHR_SURFACE_EXTENSION_NAME, + VK_KHR_XLIB_SURFACE_EXTENSION_NAME + }; + static const unsigned num = sizeof(required) / sizeof(required[0]); + if (extensions) { + for (int i = 0; i < num; ++i) { + extensions[i] = required[i]; + } + } else { + *nRequired = num; + } +} +#endif + +void createInstance(struct RenderVulkan *vk, + const uint32_t nLayers, const char *const *layers, + const uint32_t nAdditional, const char *const *additionalExtensions) +{ + VkApplicationInfo appInfo = { 0 }; + appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; + appInfo.pApplicationName = "Pugl Vulkan Test"; + appInfo.applicationVersion = VK_MAKE_VERSION(0, 1, 0); + appInfo.pEngineName = "Pugl Vulkan Test Engine"; + appInfo.engineVersion = VK_MAKE_VERSION(0, 1, 0); + appInfo.apiVersion = VK_MAKE_VERSION(1, 0, 0); // MoltenVK for macOS only supports Vulkan 1.0 + + unsigned i, j, nRequired; + getRequiredInstanceExtensions(&nRequired, NULL); + + const uint32_t nExtensions = nRequired + nAdditional; + const char **const extensions = malloc(sizeof(const char*) * nExtensions); + + getRequiredInstanceExtensions(NULL, extensions); + for (i = nRequired, j = 0; i < nExtensions; ++i, ++j) { + extensions[i] = additionalExtensions[j]; + } + + for (i = 0; i < nExtensions; ++i) { + printf("Using instance extension:\t%s\n", extensions[i]); + } + + for (i = 0; i < nLayers; ++i) { + printf("Using instance layer:\t%s\n", layers[i]); + } + + VkInstanceCreateInfo createInfo = { 0 }; + createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; + createInfo.pApplicationInfo = &appInfo; + createInfo.enabledLayerCount = nLayers; + createInfo.ppEnabledLayerNames = layers; + createInfo.enabledExtensionCount = nExtensions; + createInfo.ppEnabledExtensionNames = extensions; + + VkResult result; + if ((result = vkCreateInstance(&createInfo, NULL, &vk->instance))) { + exit(result); + } + free(extensions); +} + +/** Must not be called until all derivative objects are destroyed first */ +void destroyInstance(struct RenderVulkan *vk) +{ + vkDestroyInstance(vk->instance, NULL); + vk->instance = NULL; +} + +int main() +{ + static const char *const instanceLayers[] = { + "VK_LAYER_LUNARG_standard_validation" + }; + const uint32_t nInstanceLayers = sizeof(instanceLayers) / sizeof(instanceLayers[0]); + static const char *const instanceExtensions[] = { + VK_EXT_DEBUG_REPORT_EXTENSION_NAME + }; + const uint32_t nInstanceExtensions = sizeof(instanceExtensions) / sizeof(instanceExtensions[0]); + + vk = malloc(sizeof(*vk)); + createInstance(vk, nInstanceLayers, instanceLayers, nInstanceExtensions, instanceExtensions); + printf("Created Vulkan Instance Successfully\n"); + destroyInstance(vk); + free(vk); + vk = NULL; + return 0; +} + +//#if defined(_WIN32) +#if 0 +int WINAPI WinMain( + HINSTANCE hInstance, + HINSTANCE hPrevInstance, + LPSTR lpCmdLine, + int nCmdShow + ) +{ + (void)hInstance; + (void)hPrevInstance; + (void)lpCmdLine; + (void)nCmdShow; + return main(); +} +#endif diff --git a/vkpugltest.sln b/vkpugltest.sln new file mode 100644 index 0000000..279f928 --- /dev/null +++ b/vkpugltest.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29418.71 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "vkpugltest", "vkpugltest.vcxproj", "{D40E96A6-A1AF-4C08-BC54-BA73A37B04C8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D40E96A6-A1AF-4C08-BC54-BA73A37B04C8}.Debug|x64.ActiveCfg = Debug|x64 + {D40E96A6-A1AF-4C08-BC54-BA73A37B04C8}.Debug|x64.Build.0 = Debug|x64 + {D40E96A6-A1AF-4C08-BC54-BA73A37B04C8}.Debug|x86.ActiveCfg = Debug|Win32 + {D40E96A6-A1AF-4C08-BC54-BA73A37B04C8}.Debug|x86.Build.0 = Debug|Win32 + {D40E96A6-A1AF-4C08-BC54-BA73A37B04C8}.Release|x64.ActiveCfg = Release|x64 + {D40E96A6-A1AF-4C08-BC54-BA73A37B04C8}.Release|x64.Build.0 = Release|x64 + {D40E96A6-A1AF-4C08-BC54-BA73A37B04C8}.Release|x86.ActiveCfg = Release|Win32 + {D40E96A6-A1AF-4C08-BC54-BA73A37B04C8}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {6ADF8EEA-80BB-46B0-8E9E-F646BB64DEE1} + EndGlobalSection +EndGlobal diff --git a/vkpugltest.vcxproj b/vkpugltest.vcxproj new file mode 100644 index 0000000..cee3a0b --- /dev/null +++ b/vkpugltest.vcxproj @@ -0,0 +1,136 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + {D40E96A6-A1AF-4C08-BC54-BA73A37B04C8} + vkpugltest + 10.0 + + + + Application + true + v142 + MultiByte + + + Application + false + v142 + true + MultiByte + + + Application + true + v142 + MultiByte + + + Application + false + v142 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + C:\VulkanSDK\1.1.121.2\Include;$(IncludePath) + $(LibraryPath) + + + + Level3 + Disabled + true + true + + + Console + + + + + Level3 + Disabled + true + true + + + Console + C:\VulkanSDK\1.1.121.2\Lib;%(AdditionalLibraryDirectories) + vulkan-1.lib;%(AdditionalDependencies) + + + + + Level3 + MaxSpeed + true + true + true + true + + + Console + true + true + + + + + Level3 + MaxSpeed + true + true + true + true + + + Console + true + true + + + + + + + + + \ No newline at end of file diff --git a/vkpugltest.vcxproj.filters b/vkpugltest.vcxproj.filters new file mode 100644 index 0000000..dac3b05 --- /dev/null +++ b/vkpugltest.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file diff --git a/vkpugltest.vcxproj.user b/vkpugltest.vcxproj.user new file mode 100644 index 0000000..0f14913 --- /dev/null +++ b/vkpugltest.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file -- cgit v1.2.1