Featured Article

Vulkan Simple Triangle Tutorial

Cynthia Jul 13, 2026

Diving into the world of 3D graphics programming, one of the first steps many developers take is rendering a simple triangle on the screen. Vulkan, a powerful and industry-standard graphics and computing API, facilitates this process in a direct and efficient manner. In this guide, we'll embark on a journey to create and display a simple triangle using Vulkan.

Volcano Line Drawing, Vulkan Tattoo Fineline, Fine Line Volcano Tattoo, Tattoo Volcano, Tattoo Volcan, Lava Tattoo, Simple Volcano Tattoo, Vulcan Tattoo, Volcano Outline
Volcano Line Drawing, Vulkan Tattoo Fineline, Fine Line Volcano Tattoo, Tattoo Volcano, Tattoo Volcan, Lava Tattoo, Simple Volcano Tattoo, Vulcan Tattoo, Volcano Outline

Vulkan, developed by the Khronos Group, is designed to provide low-overhead, cross-platform 3D graphics and compute functionality. It's widely used in games, professional applications, and even in operating systems. By understanding how to draw a simple triangle with Vulkan, you'll gain a solid foundation for more complex graphics programming.

a black and white triangle on a transparent background
a black and white triangle on a transparent background

Setting Up the Development Environment

Before we start, ensure you have the necessary tools installed. You'll need a C++ compiler (like GCC or Clang), CMake for build system generation, and the Vulkan SDK, which includes the VulkanHeaders and Vulkanéoements libraries.

three mountains are shown with the sun in the middle and stars above them, on a white background
three mountains are shown with the sun in the middle and stars above them, on a white background

Once set up, create a new C++ project and initialize it with CMake. Here's a basic CMakeLists.txt to get you started:

```cmake cmake_minimum_required (VERSION 3.14) project (VulkanTriangle) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Vulkan REQUIRED) add_executable(VulkanTriangle main.cpp) target_link_libraries(VulkanTriangle Vulkan::Vulkan) ```

Vulkan Initialization

Mr❤️TIARAS🔥SI❤️ LÝ TƯ TRẤN HOÀNG CỜ KAI SARAKƯ: VUA SẮC TỐ HÀO QUANG SI ☸️🔏🆔⛩️🗜🏛️🫧: LONG MẠCH.
Mr❤️TIARAS🔥SI❤️ LÝ TƯ TRẤN HOÀNG CỜ KAI SARAKƯ: VUA SẮC TỐ HÀO QUANG SI ☸️🔏🆔⛩️🗜🏛️🫧: LONG MẠCH.

Vulkan requires an instance, a physically connected device, a logical device, and a queue to function. Here's how you can initialize them:

```cpp VkInstance instance = VK_NULL_HANDLE; VkDevice device = VK_NULL_HANDLE; VkQueue graphicsQueue = VK_NULL_HANDLE; void initVulkan() { // Check Vulkan version, create instance, and find suitable physical device and logical device... } ```

Creating the Triangle

To create a simple triangle, define its vertices as an array of float. Each vertex consists of position (2D for now) and color data:

Vulkan zeichnen – In 14 Schritten zum Erfolg
Vulkan zeichnen – In 14 Schritten zum Erfolg

```cpp const float vertices[] = { 0.0f, -0.5f, 1.0f, 0.0f, 0.0f, // Vertex 1 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, // Vertex 2 -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, // Vertex 3 }; ```

Rendering the Triangle

To render the triangle, create a buffer holding the vertices, a vertex buffer view (VVB), and specify them in the pipeline:

First, create a buffer holding the vertices:

a white triangle on a black background
a white triangle on a black background

```cpp VkBuffer stagingBuffer; VkDeviceSize bufferSize = sizeof(vertices); createBuffer(device, physicalDeviceMemoryProperties, bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, stagingBuffer); copyBuffer"] ```

Then, create a vertex buffer and view:

```cpp VkBuffer vertexBuffer; VkDeviceSize bufferSize = sizeof(vertices); createBuffer(device, physicalDeviceMemoryProperties, bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, vertexBuffer); copyBuffer"] VkBufferView vertexBufferView; createBufferView(device, vertexBuffer, 0, bufferSize); ```

Finally, set up the pipeline and draw command:

a painting with red and orange paint on it
a painting with red and orange paint on it
Volcano Drawing Outline, Mayon Volcano Simple Drawing, Volcano Outline Drawing, Simple Volcano Sketch, Realistic Volcano Outline, Outline Volcano, Teide Volcano Drawing Simple, Small Volcano Drawing, Volcano Outline
Volcano Drawing Outline, Mayon Volcano Simple Drawing, Volcano Outline Drawing, Simple Volcano Sketch, Realistic Volcano Outline, Outline Volcano, Teide Volcano Drawing Simple, Small Volcano Drawing, Volcano Outline
a red triangle on a white background
a red triangle on a white background
a black and white image of a triangle with one side facing the other, which is smaller than it appears to be
a black and white image of a triangle with one side facing the other, which is smaller than it appears to be
a black triangle on a white background
a black triangle on a white background
a triangle with three sides and one side missing
a triangle with three sides and one side missing
a black and white photo of a triangle with one side facing the other, on a white background
a black and white photo of a triangle with one side facing the other, on a white background
Natural Disaster Destruction, Volcanic Eruption 2004, Natural Disaster Imagery, Man Made Disasters Images, Natural Disaster Volcanic Activity, Volcano Eruption Black And White, Explosion On Mountain Top, Asosan Volcano Eruption History, Natural Disaster Artwork
Natural Disaster Destruction, Volcanic Eruption 2004, Natural Disaster Imagery, Man Made Disasters Images, Natural Disaster Volcanic Activity, Volcano Eruption Black And White, Explosion On Mountain Top, Asosan Volcano Eruption History, Natural Disaster Artwork
TRIÂNGULO
TRIÂNGULO

```cpp // Create pipeline layout, pipeline, and command buffers... std::array attributeDescriptions{}; VkPipelineVertexInputStateCreateInfo vertexInputInfo{}; vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; vertexInputInfo.pNext = nullptr; vertexInputInfo.flags = 0; vertexInputInfo.vertexBindingDescriptionCount = 0; vertexInputInfo.pVertexBindingDescriptions = nullptr; vertexInputInfo.vertexAttributeDescriptionCount = static_cast(attributeDescriptions.size()); vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.data(); VkGraphicsPipelineCreateInfo pipelineInfo{}; pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; pipelineInfo.pNext = nullptr; pipelineInfo.flags = 0; pipelineInfo.layout = pipelineLayout; pipelineInfo.renderPass = renderPass; pipelineInfo.subpass = 0; pipelineInfo.basePipelineHandle = VK_NULL_HANDLE; pipelineInfo.basePipelineIndex = -1; pipelineInfo.inputAssemblyState = inputAssembly; pipelineInfo.vertexInputState = &vertexInputInfo; pipelineInfo.vertexShaderState = vertexShaderStageInfo; pipelineInfo Cadillac propriétaires et services fragments = fragmentsStageInfo; // ... Other pipeline states vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline); VkCommandBuffer commandBuffer = beginSingleTimeCommands(device, commandPool); vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline); vkCmdDraw(commandBuffer, static_cast(vertices.size()) / 5, 1, 0, 0); endSingleTimeCommands(commandBuffer, device, commandPool); ```

Rendering the Triangle in a Window

To display the rendered triangle in a window, create a surface, swap chain, and framebuffers. Then, present the rendered image to the window:

```cpp // Create surface... VkSwapchainCreateInfoKHR swapChainInfo{}; swapChainInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; swapChainInfo.pNext = nullptr; swapChainInfo.flags = 0; swapChainInfo.surface = surface; swapChainInfo.minImageCount = 2; swapChainInfo.imageFormat = surfaceFormat.format; swapChainInfo.imageColorSpace = surfaceFormat.colorSpace; swapChainInfo.imageExtent = swapChainExtent; swapChainInfo.imageArrayLayers = 1; swapChainInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; swapChainInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; swapChainInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; swapChainInfo.presentMode = VK_PRESENT_MODE_FIFO_KHR; swapChainInfo.clipped = VK_TRUE; swapChainInfo.oldSwapchain = VK_NULL_HANDLE; vkCreateSwapchainKHR(device, &swapChainInfo, nullptr, &swapChain); // Create image views, framebuffers, and command buffers... while (true) { // Aquire next image and submit draw command buffer... } ```

And that's it! You've successfully created and displayed a simple triangle using Vulkan. The final step is to clean up the resources and exit the application gracefully:

```cpp void cleanup() { // Clean up Vulkan resources... vkDestroyInstance(instance, nullptr); } int main() { initVulkan(); // Main loop... cleanup(); return 0; } ```

Embarking on this journey, you've gained a solid understanding of Vulkan's core concepts and pipeline creation. As you build upon this foundation, you'll be well on your way to mastering Vulkan and creating more complex graphics and compute applications. Happy rendering!