Any examples of object picking with Vulkan

Would anyone be able to point me to an object picking example with Vulkan using the render each object using a different color to a texture in a separate rendering pass and read those texture pixels to get the color value approach.

I’m very new to Vulkan, and I’m starting to port my OpenGl apps over, and I use this technique a lot.

In OpenGL, I just create a separate render buffer, and have the fragment shader just pass the object id over, and set the object id render buffer to that color value.

Or, are there any examples of using multiple render targets, (the frame buffer and a texture), where you specify multiple outputs in the fragment shader?

thanks

One approach is using different pipeline. I think someone knows how to use subpasses for this purpose.

  1. You can create different pipeline with special shaders, but use self-created images instead of swapchain images for framebuffers

e.g.
layout(push_constant) uniform PushConsts
{
uint r;
uint g;
uint b;
} pushConsts;

void main()
{
outColor = vec4(pushConsts.r/255.f, pushConsts.g/255.f, pushConsts.b/255.f, 1.0f);
}

  1. push object id using pushconstants before call vkDraw for your object
  2. build command buffer and sumbit it in queue (only graphics queue, it is not necessary for present)
  3. copy self-created image from device to host
  4. get necessary position pixel in your self-created image and the color of this pixel is your object id

You can call described above procedure on demand (e.g. per mouse button click)

@JiuShei yes that was the way with OpenGL - draw to the framebuffer and get the pixel under the mouse pointer. It works. In Vulkan will work as well. There is question though - should be done with pipeline or with subpass in the renderpass?
I have also a concern - full frame picture for 1 pixel. Is there a way that we can do that in just a tile contain the mouse pointer?