Skip to content
systemcoding.dev
GPU

What Makes Vulkan Different From OpenGL for Beginners

Learn how Vulkan differs from OpenGL in state, command recording, memory, synchronization and validation, and whether a beginner should learn it first.

8 min read
Summarize in AI
Flat illustration of a GPU die of compute units with one thread block lit, with the Vulkan, OpenGL and C++ logos.

If you have followed an OpenGL tutorial, you drew a triangle in under a hundred lines. The Vulkan version of the same program is about a thousand. That gap is the first thing every beginner notices, and it is also the whole story in miniature: Vulkan does not do more than OpenGL, it asks you to do things that OpenGL did behind your back. This post explains the five differences that account for the extra code, with a small example of each, so the size of a Vulkan program stops being mysterious. It is the orientation piece for the Vulkan setup posts that follow, and for the longer Vulkan guide.

Two APIs, two philosophies

OpenGL was designed in 1992 around a simple promise: you tell the driver what to draw, and the driver figures out how. Over the decades, the driver grew into a large piece of software. It tracks a giant global state machine, checks your calls for errors, compiles shaders when you first use them, moves memory around, and guesses which operations can overlap. That convenience has a price. The driver's guesses cost CPU time, behave differently between vendors, and make performance hard to predict.

Vulkan, published in 2016, reverses the deal. The driver is thin and does what it is told. You describe the work precisely, in advance, and the driver spends its time executing instead of guessing. The five differences below all follow from that one decision.

Difference 1: state lives in objects, not in a global machine

OpenGL has a context, a big bag of state: the bound texture, the current shader, the blend mode, the viewport. Calls change that state, and a draw call uses whatever state is set at that moment. A bug usually means some earlier call left the wrong setting behind.

gl_state.cc
glUseProgram(program);
glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_BLEND);
glDrawArrays(GL_TRIANGLES, 0, 3);

Vulkan has no global state. Almost everything lives in an explicit object, and you pass it where it is used. The most important one is the pipeline, an object that bundles the shaders and nearly all the fixed state such as blend mode and primitive type. You build it once, ahead of time, and bind it when drawing:

vk_pipeline.cppcpp
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
vkCmdDraw(cb, 3, 1, 0, 0);

Two consequences follow. A pipeline is expensive to create, so a program makes them at load time instead of letting the driver compile on the first draw, which removes the stutter that OpenGL programs sometimes show the first time a new effect appears. And since the state is in the object, there is nothing to leave behind: the pipeline you bind is the state you get.

Difference 2: commands are recorded, then submitted

In OpenGL, glDrawArrays means "draw now", at least as far as your program can tell. The driver queues the work internally and you do not see it.

In Vulkan you record commands into a command buffer, which is just a list stored in memory, and later hand the list to a queue with vkQueueSubmit. Recording does not touch the GPU at all. This one change opens three doors. You can record many command buffers in parallel on different threads, each thread working with its own pool, which is how Vulkan scales across CPU cores. You can record a buffer once and submit it many times, useful for static scenes. And since submission is explicit, you control when work reaches the GPU.

vk_record.cppcpp
vkBeginCommandBuffer(cb, &begin_info);
// ... record binds, draws, copies ...
vkEndCommandBuffer(cb);

vkQueueSubmit(queue, 1, &submit_info, fence);

The cost is bookkeeping. You must create the pool, allocate the buffer, begin and end it, and submit it, where OpenGL needed one line.

Difference 3: you manage memory

An OpenGL buffer is created with glBufferData(target, size, data, usage), and the driver picks where it lives, copies the data and moves it later if it needs to. You never see an address or a heap.

Vulkan separates the object from its storage. A VkBuffer is only a description, and it has no memory until you allocate some and bind it. You ask the device which memory types it offers, pick one that suits the use, such as fast device-local memory for vertex data, allocate a block and bind the buffer to a region inside it:

vk_memory.cppcpp
vkCreateBuffer(device, &buffer_info, nullptr, &buffer);

VkMemoryRequirements req;
vkGetBufferMemoryRequirements(device, buffer, &req);

VkMemoryAllocateInfo alloc{};
alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
alloc.allocationSize = req.size;
alloc.memoryTypeIndex = chosen_type;
vkAllocateMemory(device, &alloc, nullptr, &memory);

vkBindBufferMemory(device, buffer, memory, 0);

This is the most tedious difference, and it is why nearly every project uses the VulkanMemoryAllocator library, which wraps these steps. But it explains why Vulkan can be fast: you decide which buffers share a block, when memory is freed and what is placed in the GPU's own memory.

Difference 4: synchronization is your job

This is the difference that causes the most surprise. In OpenGL, if you draw into a texture and then sample from it, the driver notices the dependency and inserts the waiting for you. In Vulkan, the GPU runs commands in overlapping fashion, and nothing is ordered unless you order it.

Vulkan gives you tools at two scales. Between commands on the GPU, barriers state that earlier writes must finish before later reads. Between the CPU and GPU, and between queues, fences and semaphores express the waits. A typical frame loop waits on a fence before reusing a command buffer, and uses semaphores to order image acquisition, rendering and presentation.

vk_sync.cppcpp
vkWaitForFences(device, 1, &frame_fence, VK_TRUE, UINT64_MAX);
vkResetFences(device, 1, &frame_fence);

Get it wrong and the symptoms are the worst kind: output that is correct on one GPU and flickers on another, or crashes that depend on timing. This is what makes Vulkan hard, and also what makes it predictable once it is right, since the GPU never waits for anything you did not ask it to wait for.

Difference 5: errors are not reported unless you ask

When an OpenGL call is wrong, the driver sets an error flag that glGetError returns, and debug contexts can print messages. Vulkan drivers, in the interest of speed, perform almost no checks. Pass a null pointer where one is not allowed, or use an image in the wrong layout, and the behavior is undefined: a crash, a black screen, or a picture that looks fine on your machine.

The replacement for driver-side checking is the validation layer, a library you enable during development that intercepts every call and checks it against the specification. It prints readable messages that name the exact rule you broke.

vk_validation.cppcpp
const char* layers[] = { "VK_LAYER_KHRONOS_validation" };
instance_info.enabledLayerCount = 1;
instance_info.ppEnabledLayerNames = layers;

Turn it on in every debug build, and treat a message as a bug even when the picture looks right. Turn it off in release builds, because the checking is slow. The layer is the reason Vulkan development is workable: it takes the place of the safety net the driver used to provide.

A side-by-side summary

TopicOpenGLVulkan
StateOne global context, changed by callsExplicit objects, mainly the pipeline
Draw callsExecuted as issuedRecorded into command buffers, then submitted
MemoryDriver allocates and movesYou allocate and bind
SynchronizationDriver inserts itYou write barriers, fences and semaphores
ErrorsglGetError and debug outputValidation layer, opt in
ShadersGLSL source compiled by the driverSPIR-V compiled in advance
ThreadsOne context per thread, awkwardRecord on any thread, one pool each
Typical triangleUnder 100 linesAround 1,000 lines

The last row follows from all the rows above it.

Shaders arrive precompiled

One more difference sits at the edge of the five. OpenGL takes GLSL source text and compiles it in the driver at run time, so a syntax error appears when the program runs, and each vendor's compiler may accept slightly different code. Vulkan takes SPIR-V, a binary intermediate format, and you compile your GLSL to it ahead of time with a tool such as glslc:

Shell
glslc shader.vert -o shader.vert.spv

The driver turns SPIR-V into machine code, but the front-end parsing has already happened in your build. Errors show up at build time, and every vendor sees the same input.

Should a beginner start with Vulkan?

It depends on the goal, and the honest answer for most people is no, not first.

If you want to learn graphics, meaning transformations, lighting, textures, shadows and the graphics pipeline, OpenGL lets you reach those ideas in a few hours. Vulkan buries them under setup. The concepts transfer completely, since a vertex shader is a vertex shader in both, so learning the ideas in OpenGL and moving to Vulkan later loses nothing.

If you want to learn how GPUs and drivers work, or you need performance that OpenGL cannot give, or you are building something that must ship on Android and desktop with one API, Vulkan is the right starting point, and it is worth taking the setup in stages. Write the instance, then the device, then the swapchain, and stop at each stage to understand it before the next. The posts that follow take exactly that path.

If you are on a Mac, remember that Apple deprecated OpenGL and supports Vulkan only through MoltenVK, a translation layer on top of Metal. That fact favors learning concepts in a portable way and not betting on either.

What stays the same

Much of what you know carries over. The graphics pipeline stages, vertex input, vertex shader, rasterization, fragment shader and blending are the same in both. GLSL is the shader language in both, with small differences: Vulkan GLSL requires explicit layout(location = N) on inputs and outputs and does not have the old uniform variables, replacing them with uniform blocks and push constants. Matrices, normals, textures and all the math are identical. Coordinate conventions differ in one detail worth knowing now, since Vulkan's clip space has Y pointing down and a depth range of 0 to 1, where OpenGL's has Y up and depth from -1 to 1, so a projection matrix written for OpenGL renders upside down in Vulkan until you flip it.

The differences described here are documented in the Vulkan specification and the official Vulkan Guide.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.