From 084d8950d6ec0a3dfa68fffd6b92795c985cd950 Mon Sep 17 00:00:00 2001 From: Hane Date: Wed, 30 Oct 2024 18:05:52 +0100 Subject: [PATCH] lunarg4 - first commit --- .gitignore | 3 + build.bat | 4 + src/main.cpp | 264 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 271 insertions(+) create mode 100644 .gitignore create mode 100644 build.bat create mode 100644 src/main.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c15bfd --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.rdbg +*.pdb +*.exe \ No newline at end of file diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..9ddb773 --- /dev/null +++ b/build.bat @@ -0,0 +1,4 @@ +REM cd build 2>NUL && cd .. || mkdir build +if not exist build\ mkdir build || goto :EOF + +clang++ src/main.cpp -o build/window.exe -O0 -g -gcodeview -lgdi32 -lvulkan-1 -I%VULKAN_SDK%/Include -L%VULKAN_SDK%/Lib -Wl,-pdb= diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..d5a2b2e --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,264 @@ +#define WIN32_LEAN_AND_MEAN +#define UNICODE + +#include +#include +#include +#include + +#include +#include +#include +#include +//LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); + +typedef struct StateInfo { + int windowNum; +} stif; + +/* + * stif* GetAppState(HWND hwnd) { + * return state; + * } + */ + +LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { + stif *stateInfo; + if(uMsg == WM_CREATE) { + CREATESTRUCT *startingStruct = (CREATESTRUCT*)lParam; + stateInfo = (stif*)(startingStruct->lpCreateParams); + SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)stateInfo); + } else { + LONG_PTR ptr = GetWindowLongPtr(hwnd, GWLP_USERDATA); + stateInfo = (stif*)ptr; + switch (uMsg) { + case WM_PAINT: + { /**/ + HGDIOBJ brush = GetStockObject(BLACK_BRUSH); + PAINTSTRUCT ps; + HDC hdc = BeginPaint(hwnd, &ps); + + FillRect(hdc, &ps.rcPaint, (HBRUSH) brush ); + EndPaint(hwnd, &ps); + return 0; + } + case WM_DESTROY: + PostQuitMessage(0); + return 0; + + } + } + return DefWindowProcW(hwnd, uMsg, wParam, lParam); +} + +//HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd +int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { + // Register the window class. + const wchar_t className[] = L"LA ventana"; + + WNDCLASSEXW wc = {0 }; + + wc.lpfnWndProc = WindowProc; + wc.hInstance = hInstance; + wc.lpszClassName = className; + wc.cbSize = sizeof(WNDCLASSEXW); + + uint32_t edrror; + + //atom = 16bit id + ATOM windowClass = RegisterClassExW(&wc); + if(!windowClass) { edrror = GetLastError(); + return 2; } + + // Window creation + + //Optional state info + stif *state = new stif{ 0 }; + if (!state) return 3; + + HWND hwnd = CreateWindowExW(WS_EX_CLIENTEDGE, // Optional window styles. + //(LPCWSTR)windowClass, + className, // Window class + L"Tremendo ventanote", // Window text + WS_CAPTION | WS_OVERLAPPEDWINDOW, // Window style + + // Size and position + CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, + + NULL, // Parent window + NULL, // Menu + hInstance, // Instance handle + state // Additional application data + ); + edrror = GetLastError(); + + + if (hwnd == NULL){ + return 1; + } + + /* Vulkan start */ + + /* Instance init */ + // initialize the VkApplicationInfo structure + VkApplicationInfo app_info = {}; + app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO; + app_info.pNext = NULL; + app_info.pApplicationName = "aplicasao"; + app_info.applicationVersion = 1; + app_info.pEngineName = "motor"; + app_info.engineVersion = 1; + app_info.apiVersion = VK_API_VERSION_1_0; + + // initialize the VkInstanceCreateInfo structure with appropiate SURFACE extensions + std::vector instanceExtensionNames; + instanceExtensionNames.push_back(VK_KHR_SURFACE_EXTENSION_NAME); + /* + * #ifdef _WIN32 + * instanceExtensionNames.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME); + * #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) + * instanceExtensionNames.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME); + * #endif + */ + VkInstanceCreateInfo inst_info = {}; + inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO; + inst_info.pNext = NULL; + inst_info.flags = 0; + inst_info.pApplicationInfo = &app_info; + inst_info.enabledExtensionCount = 0;//2; + inst_info.ppEnabledExtensionNames = NULL;//instanceExtensionName.data(); + inst_info.enabledLayerCount = 0; + inst_info.ppEnabledLayerNames = NULL; + + VkInstance inst; + VkResult result; + + result = vkCreateInstance(&inst_info, NULL, &inst); + if (result == VK_ERROR_INCOMPATIBLE_DRIVER) { + printf("cannot find a compatible Vulkan ICD\n"); + exit(-1); + } else if (result) { + printf("unknown error\n"); + exit(-2); + } + + /* Enumerating physical devices */ + uint32_t gpuCount = 0; //U_ASSERT_ONLY + std::vector gpus; + result = vkEnumeratePhysicalDevices(inst, &gpuCount, NULL); + //assert(gpuCount <= 0); + gpus.resize(gpuCount); + result = vkEnumeratePhysicalDevices(inst, &gpuCount, gpus.data()); + assert(!result && gpuCount >= 1); + + /* Creating logical device(with SWAPCHAIN extension) + queues (todo insspect all gpus, not assuume 1st) */ + VkDeviceQueueCreateInfo queueInfo = {}; + uint32_t queueFamilyCount = 0; + + vkGetPhysicalDeviceQueueFamilyProperties(gpus[0], &queueFamilyCount, NULL); + assert(queueFamilyCount >= 1); + + std::vector queueProperties; + queueProperties.resize(queueFamilyCount); + vkGetPhysicalDeviceQueueFamilyProperties(gpus[0], &queueFamilyCount, queueProperties.data()); + assert(queueFamilyCount >= 1); + + bool found = false; //U_ASSERT_ONLY + for (unsigned int i = 0; i < queueFamilyCount; i++) { + if (queueProperties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { + queueInfo.queueFamilyIndex = i; + found = true; + break; + } + } + assert(found); + assert(queueFamilyCount >= 1); + + float queuePriorities[1] = {0.0}; + queueInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; + queueInfo.pNext = NULL; + queueInfo.queueCount = 1; + queueInfo.pQueuePriorities = queuePriorities; + + std::vector deviceExtensionNames; + deviceExtensionNames.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); + VkDeviceCreateInfo deviceInfo = {}; + deviceInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; + deviceInfo.pNext = NULL; + deviceInfo.queueCreateInfoCount = 1; + deviceInfo.pQueueCreateInfos = &queueInfo; + deviceInfo.enabledExtensionCount = 0;//1; + deviceInfo.ppEnabledExtensionNames = NULL;//deviceExtensionNames.data(); + deviceInfo.enabledLayerCount = 0; + deviceInfo.ppEnabledLayerNames = NULL; + deviceInfo.pEnabledFeatures = NULL; + + VkDevice device; + result = vkCreateDevice(gpus[0], &deviceInfo, NULL, &device); //U_ASSERT_ONLY + assert(result == VK_SUCCESS); + + //Creating command buffer pool & command buffer + VkCommandPool cmdPool; + VkCommandPoolCreateInfo cmdPoolInfo = {}; + cmdPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; + cmdPoolInfo.pNext = NULL; + cmdPoolInfo.queueFamilyIndex = queueInfo.queueFamilyIndex; //u sure bro???? + cmdPoolInfo.flags = 0; + + result = vkCreateCommandPool(device, &cmdPoolInfo, NULL, &cmdPool); + assert(result == VK_SUCCESS); + + VkCommandBuffer cmd; + VkCommandBufferAllocateInfo cmdInfo = {}; + cmdInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; + cmdInfo.pNext = NULL; + cmdInfo.commandPool = cmdPool; + cmdInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; + cmdInfo.commandBufferCount = 1; + + result = vkAllocateCommandBuffers(device, &cmdInfo, &cmd); + assert(result == VK_SUCCESS); + + //Swapchain setup (requires Instance SURFACEs and Device SWAPCHAIN extensions) + /* + * VkSurfaceKHR surface; + * #ifdef _WIN32 + * VkWin32SurfaceCreateInfoKHR createInfo = {}; + * createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR; + * createInfo.pNext = NULL; + * createInfo.hinstance = hInstance; + * createInfo.hwnd = hwnd; + * result = vkCreateWin32SurfaceKHR(inst, &createInfo, NULL, &surface); + */ +/* + * #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) + * VkWaylandSurfaceCreateInfoKHR createInfo = {}; + * createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR; + * createInfo.pNext = NULL; + * createInfo.display = info.display; + * createInfo.surface = info.window; + * res = vkCreateWaylandSurfaceKHR(inst, &createInfo, NULL, &surface); + * + * #endif + * assert(result == VK_SUCCESS); + */ + // + + + //Window show and event loop + + ShowWindow(hwnd, nShowCmd); + + MSG msg = { }; + while (GetMessage(&msg, NULL, 0, 0) > 0) { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + + VkCommandBuffer cmdBufs[1] = {cmd}; + vkFreeCommandBuffers(device, cmdPool, 1, cmdBufs); + vkDestroyCommandPool(device, cmdPool, NULL); + vkDestroyDevice(device, NULL); + vkDestroyInstance(inst, NULL); + return 0; +}