Multiple Object Rendering in Vulkan
If you are learning directly Vulkan with less experience with other APIs or without any graphics APIs experience, it can be very complicated. And sometimes don’t seeing the ‘full picture’ of rendering with Vulkan, can cause headache even while adding simple features like multiple different object rendering with Vulkan(not instancing). I saw some people struggle with it, didn’t see any tutorial about it and decided to write something about it. In a very simple way.
- Descriptor Set Layout
Now, look carefully to the function parameters. I define two seperate descritptor set layouts, for each model. If your models share same shader bindings (in correct order), then you can use the same descriptor set layout, but if they differs, then you must use seperate descriptor set layout. Here’s the function code:
I’m using three uniform buffers in my shaders. One is for model-view-projection matrix in vertex shader, and the others is for fragment shader (albedo and normal maps).
Create different descriptor set layouts for each model.
I define two seperate graphics pipelines for each model.
You must create different graphics pipelines for each shader model. Creating the graphics pipeline for each model is the standart graphics pipeline creation procedure. Only things differs that, you pass different shader files and essentially, different descriptor set layouts which we talked about, above. According to descriptor set layout information, we can create pipeline for according shader models.
That’s it in here. We’re creating to different graphics pipelines and pipeline layouts for each model.
Create different pipeline layouts and pipelines for each model.
I create two different descriptor pools for each models. Here’s the function:
My shaders for each model, uses same uniform bindings, so I can create them with the same VkDescriptorPoolSize’s. Be careful on that. Create them according to your shader uniform bindings.
Create different descriptor pools for each model.
Here, I create two different descriptor sets for each model. You can see from parameter names that, I pass uniform buffers for shader (uniform and 2 texture buffer->albedo and normal map) and descriptor pools.
Create different descriptor sets for each model.
After these, you can now render objects on the screen. In vkCmd functions, you just need only pass buffers (vertex, index buffers are seperate for each model) and Vulkan objects which you created seperately for each model.