Skip to content

Instantly share code, notes, and snippets.

@Darianopolis
Last active February 28, 2025 09:01
Show Gist options
  • Save Darianopolis/89098448f8af3bf00f722f4dd279f837 to your computer and use it in GitHub Desktop.
Save Darianopolis/89098448f8af3bf00f722f4dd279f837 to your computer and use it in GitHub Desktop.
vktinyhellotriangle
int main(){SDL_Init(SDL_INIT_VIDEO);
void*window=SDL_CreateWindow("Hello Triangle", 800, 600, SDL_WINDOW_VULKAN),*instanceExtensions,*instanceExtensionCount=new uint32_t,*queueFamily=new uint32_t,*imageIndex=new uint32_t,*count=new uint32_t,*queueFamilies,*formats,*images,*instance,*surface,*physicalDevice,*device,*queue,*swapchain,*renderPass,*pipelineLayout,*pipeline,*fence,*imageView,*framebuffer,*cmdPool,*cmd,*event=new SDL_Event;
(const char*const*&)instanceExtensions=SDL_Vulkan_GetInstanceExtensions((uint32_t*)instanceExtensionCount);
vkCreateInstance(new VkInstanceCreateInfo{.sType=VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,.pApplicationInfo=new VkApplicationInfo{.sType=VK_STRUCTURE_TYPE_APPLICATION_INFO,.apiVersion=VK_API_VERSION_1_3,},.enabledExtensionCount=*(uint32_t*)instanceExtensionCount,.ppEnabledExtensionNames=(const char**)instanceExtensions,},nullptr,(VkInstance*)&instance);
SDL_Vulkan_CreateSurface((SDL_Window*)window, (VkInstance)instance,nullptr,(VkSurfaceKHR*)&surface);
vkEnumeratePhysicalDevices((VkInstance)instance,new uint32_t(1u),(VkPhysicalDevice*)&physicalDevice);
for(vkGetPhysicalDeviceQueueFamilyProperties((VkPhysicalDevice)physicalDevice,&(*(uint32_t*)count=16),(VkQueueFamilyProperties*)(queueFamilies=new VkQueueFamilyProperties[16])), *(uint32_t*)queueFamily=0;*(uint32_t*)queueFamily<(*(uint32_t*)count)&&!(((VkQueueFamilyProperties*)queueFamilies)[*(uint32_t*)queueFamily].queueFlags&VK_QUEUE_GRAPHICS_BIT);++*(uint32_t*)queueFamily);
vkCreateDevice((VkPhysicalDevice)physicalDevice,new VkDeviceCreateInfo{.sType=VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO,.pNext=new VkPhysicalDeviceMaintenance5FeaturesKHR{.sType=VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_5_FEATURES_KHR,.maintenance5=true},.queueCreateInfoCount=1,.pQueueCreateInfos=new VkDeviceQueueCreateInfo{.sType=VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,.queueFamilyIndex=*(uint32_t*)queueFamily,.queueCount=1,.pQueuePriorities=new float(1.f)},.enabledExtensionCount=2,.ppEnabledExtensionNames=new const char*[]{VK_KHR_SWAPCHAIN_EXTENSION_NAME,VK_KHR_MAINTENANCE_5_EXTENSION_NAME},},nullptr,(VkDevice*)&device);
vkGetDeviceQueue((VkDevice)device,*(uint32_t*)queueFamily,0,(VkQueue*)&queue);
vkGetPhysicalDeviceSurfaceFormatsKHR((VkPhysicalDevice)physicalDevice,(VkSurfaceKHR)surface,&(*(uint32_t*)count=16),(VkSurfaceFormatKHR*)(formats=new VkSurfaceFormatKHR[16]));
vkCreateSwapchainKHR((VkDevice)device,new VkSwapchainCreateInfoKHR{.sType=VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,.surface=(VkSurfaceKHR)surface,.minImageCount=2,.imageFormat=((VkSurfaceFormatKHR*)formats)[0].format,.imageColorSpace=((VkSurfaceFormatKHR*)formats)[0].colorSpace,.imageExtent={uint32_t(800),uint32_t(600)},.imageArrayLayers=1,.imageUsage=VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,.preTransform=VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR,.compositeAlpha=VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,.presentMode=VK_PRESENT_MODE_FIFO_KHR,.clipped=VK_TRUE,},nullptr,(VkSwapchainKHR*)&swapchain);
vkGetSwapchainImagesKHR((VkDevice)device,(VkSwapchainKHR)swapchain,&(*(uint32_t*)count=16),(VkImage*)(images=new VkImage[16]));
vkCreateRenderPass((VkDevice)device,new VkRenderPassCreateInfo{.sType=VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,.attachmentCount=1,.pAttachments=new VkAttachmentDescription{.format=((VkSurfaceFormatKHR*)formats)[0].format,.samples=VK_SAMPLE_COUNT_1_BIT,.loadOp=VK_ATTACHMENT_LOAD_OP_CLEAR,.finalLayout=VK_IMAGE_LAYOUT_PRESENT_SRC_KHR},.subpassCount=1,.pSubpasses=new VkSubpassDescription{.pipelineBindPoint=VK_PIPELINE_BIND_POINT_GRAPHICS,.colorAttachmentCount=1,.pColorAttachments=new VkAttachmentReference{.attachment=0,.layout=VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL}}},nullptr,(VkRenderPass*)&renderPass);
vkCreatePipelineLayout((VkDevice)device,new VkPipelineLayoutCreateInfo{.sType=VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO},nullptr,(VkPipelineLayout*)&pipelineLayout);
vkCreateGraphicsPipelines((VkDevice)device,nullptr,1,new VkGraphicsPipelineCreateInfo{.sType=VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,.stageCount=2,.pStages=new VkPipelineShaderStageCreateInfo[2]{{.sType=VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,.pNext=new VkShaderModuleCreateInfo{.sType=VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,.codeSize=1472,.pCode=new uint32_t[384]{119734787,65536,524299,56,0,131089,1,393227,1,1280527431,1685353262,808793134,0,196622,0,1,524303,0,4,1852399981,0,9,21,33,196611,2,450,262149,4,1852399981,0,262149,9,1869377379,114,393221,21,1449094247,1702130277,1684949368,30821,327685,24,1701080681,1818386808,101,393221,31,1348430951,1700164197,2019914866,0,393222,31,0,1348430951,1953067887,7237481,458758,31,1,1348430951,1953393007,1702521171,0,458758,31,2,1130327143,1148217708,1635021673,6644590,458758,31,3,1130327143,1147956341,1635021673,6644590,196613,33,0,327685,44,1701080681,1818386808,101,262215,9,30,0,262215,21,11,42,327752,31,0,11,0,327752,31,1,11,1,327752,31,2,11,3,327752,31,3,11,4,196679,31,2,131091,2,196641,3,2,196630,6,32,262167,7,6,3,262176,8,3,7,262203,8,9,3,262165,10,32,0,262187,10,11,3,262172,12,7,11,262187,6,13,1065353216,262187,6,14,0,393260,7,15,13,14,14,393260,7,16,14,13,14,393260,7,17,14,14,13,393260,12,18,15,16,17,262165,19,32,1,262176,20,1,19,262203,20,21,1,262176,23,7,12,262176,25,7,7,262167,28,6,4,262187,10,29,1,262172,30,6,29,393246,31,28,6,30,30,262176,32,3,31,262203,32,33,3,262187,19,34,0,262167,35,6,2,262172,36,35,11,262187,6,37,3212836864,327724,35,38,37,13,327724,35,39,13,13,327724,35,40,14,37,393260,36,41,38,39,40,262176,43,7,36,262176,45,7,35,262187,6,48,1061158912,327724,35,49,48,48,262176,54,3,28,327734,2,4,0,3,131320,5,262203,23,24,7,262203,43,44,7,262205,19,22,21,196670,24,18,327745,25,26,24,22,262205,7,27,26,196670,9,27,262205,19,42,21,196670,44,41,327745,45,46,44,42,262205,35,47,46,327813,35,50,47,49,327761,6,51,50,0,327761,6,52,50,1,458832,28,53,51,52,14,13,327745,54,55,33,34,196670,55,53,65789,65592}},.stage=VK_SHADER_STAGE_VERTEX_BIT,.pName="main",},{.sType=VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,.pNext=new VkShaderModuleCreateInfo{.sType=VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,.codeSize=496,.pCode=new uint32_t[124]{119734787,65536,524299,19,0,131089,1,393227,1,1280527431,1685353262,808793134,0,196622,0,1,458767,4,4,1852399981,0,9,12,196624,4,7,196611,2,450,262149,4,1852399981,0,327685,9,1734439526,1869377347,114,262149,12,1866690153,7499628,262215,9,30,0,262215,12,30,0,131091,2,196641,3,2,196630,6,32,262167,7,6,4,262176,8,3,7,262203,8,9,3,262167,10,6,3,262176,11,1,10,262203,11,12,1,262187,6,14,1065353216,327734,2,4,0,3,131320,5,262205,10,13,12,327761,6,15,13,0,327761,6,16,13,1,327761,6,17,13,2,458832,7,18,15,16,17,14,196670,9,18,65789,65592}},.stage=VK_SHADER_STAGE_FRAGMENT_BIT,.pName="main"}},.pVertexInputState=new VkPipelineVertexInputStateCreateInfo{.sType=VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO},.pInputAssemblyState=new VkPipelineInputAssemblyStateCreateInfo{.sType=VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,.topology=VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST},.pViewportState=new VkPipelineViewportStateCreateInfo{.sType=VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,.viewportCount=1,.pViewports=new VkViewport{0.f,0.f,float(800),float(600),0.f,1.f},.scissorCount=1,.pScissors=new VkRect2D{{0,0},{uint32_t(800),uint32_t(600)}}},.pRasterizationState=new VkPipelineRasterizationStateCreateInfo{.sType=VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,.lineWidth=1.f},.pMultisampleState=new VkPipelineMultisampleStateCreateInfo{.sType=VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,.rasterizationSamples=VK_SAMPLE_COUNT_1_BIT},.pDepthStencilState=nullptr,.pColorBlendState=new VkPipelineColorBlendStateCreateInfo{.sType=VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,.attachmentCount=1,.pAttachments=new VkPipelineColorBlendAttachmentState{.colorWriteMask=VK_COLOR_COMPONENT_R_BIT|VK_COLOR_COMPONENT_G_BIT|VK_COLOR_COMPONENT_B_BIT|VK_COLOR_COMPONENT_A_BIT},},.pDynamicState=nullptr,.layout=(VkPipelineLayout)pipelineLayout,.renderPass=(VkRenderPass)renderPass,.subpass=0},nullptr,(VkPipeline*)&pipeline);
vkCreateFence((VkDevice)device,new VkFenceCreateInfo{.sType=VK_STRUCTURE_TYPE_FENCE_CREATE_INFO},nullptr,(VkFence*)&fence);
vkAcquireNextImageKHR((VkDevice)device,(VkSwapchainKHR)swapchain,UINT64_MAX,VK_NULL_HANDLE,(VkFence)fence,&*(uint32_t*)imageIndex);
vkWaitForFences((VkDevice)device,1,(VkFence*)&fence,VK_TRUE,UINT64_MAX);
vkCreateImageView((VkDevice)device,new VkImageViewCreateInfo{.sType=VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,.image=((VkImage*)images)[*(uint32_t*)imageIndex],.viewType=VK_IMAGE_VIEW_TYPE_2D,.format=((VkSurfaceFormatKHR*)formats)[0].format,.subresourceRange={VK_IMAGE_ASPECT_COLOR_BIT,0,1,0,1}},nullptr,(VkImageView*)&imageView);
vkCreateFramebuffer((VkDevice)device,new VkFramebufferCreateInfo{.sType=VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,.renderPass=(VkRenderPass)renderPass,.attachmentCount=1,.pAttachments=(VkImageView*)&imageView,.width=uint32_t(800),.height=uint32_t(600),.layers=1},nullptr,(VkFramebuffer*)&framebuffer);
vkCreateCommandPool((VkDevice)device,new VkCommandPoolCreateInfo{.sType=VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,.queueFamilyIndex=*(uint32_t*)queueFamily},nullptr,(VkCommandPool*)&cmdPool);
vkAllocateCommandBuffers((VkDevice)device,new VkCommandBufferAllocateInfo{.sType=VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,.commandPool=(VkCommandPool)cmdPool,.level=VK_COMMAND_BUFFER_LEVEL_PRIMARY,.commandBufferCount=1},(VkCommandBuffer*)&cmd);
vkBeginCommandBuffer((VkCommandBuffer)cmd,new VkCommandBufferBeginInfo{.sType=VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO});
vkCmdBeginRenderPass((VkCommandBuffer)cmd,new VkRenderPassBeginInfo{.sType=VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,.renderPass=(VkRenderPass)renderPass,.framebuffer=(VkFramebuffer)framebuffer,.renderArea={ {},{uint32_t(800),uint32_t(600)} },.clearValueCount=1,.pClearValues=new VkClearValue{.color={{0.1f,0.1f,0.1f,1.f}}}},VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline((VkCommandBuffer)cmd,VK_PIPELINE_BIND_POINT_GRAPHICS,(VkPipeline)pipeline);
vkCmdDraw((VkCommandBuffer)cmd,3,1,0,0);
vkCmdEndRenderPass((VkCommandBuffer)cmd);
vkEndCommandBuffer((VkCommandBuffer)cmd);
vkQueueSubmit((VkQueue)queue,1,new VkSubmitInfo{.sType=VK_STRUCTURE_TYPE_SUBMIT_INFO,.commandBufferCount=1,.pCommandBuffers=(VkCommandBuffer*)&cmd},nullptr);
vkQueueWaitIdle((VkQueue)queue);
vkQueuePresentKHR((VkQueue)queue,new VkPresentInfoKHR{.sType=VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,.swapchainCount=1,.pSwapchains=(VkSwapchainKHR*)&swapchain,.pImageIndices=&*(uint32_t*)imageIndex});
while(SDL_WaitEvent((SDL_Event*)event)&&((SDL_Event*)event)->type!=SDL_EVENT_WINDOW_CLOSE_REQUESTED&&((SDL_Event*)event)->type!=SDL_EVENT_QUIT);}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment