3

I decided to learn Vulkan and started writing a simple program. I then ran the program (which only called vkCreateInstance()) to see if the Vulkan instance was created successfully and was shocked to see that my computer's fan started spinning loudly (and kept doing so for 5 seconds) and I saw a spike of 8% of the computer's processes being used for a second(and the function call took 1.5 billion cycles -- enough to make the program's loading time noticeable). Complaining about a one second CPU-usage spike would seem humorous if the spike were due to a loop doing hundreds of function calls, but I only called a single function once (vkCreateInstance()). Is there a way to reduce the overhead of some of Vulkan's functions and should I be using Vulkan for 2-D rendering?

Here is the code that took 1.5 billion cycles to run:

VkInstance instance;

VkApplicationInfo appInfo = { 0, 0, 0, 0, 0, 0, 0 };
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "Hello Triangle";
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.pEngineName = "No Engine";
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.apiVersion = VK_API_VERSION_1_0;

VkInstanceCreateInfo createInfo = { 0, 0, 0, 0, 0, 0, 0, 0 };
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &appInfo;
createInfo.enabledExtensionCount = 0;
createInfo.ppEnabledExtensionNames = NULL;

VkResult result = vkCreateInstance(&createInfo, NULL, &instance);
Trevor Powell
  • 21,382
  • 1
  • 59
  • 94
Cpp plus 1
  • 289
  • 1
  • 10
  • What do you imagine happens inside vkCreateInstance? It doesn't seem too unreasonable for instance creation to be expensive, nor does it matter - that's something you'll only do once during startup rather than every frame. – Maximus Minimus Jun 18 '18 at 19:27
  • @MaximusMinimus I would imagine that instance creation would just set some data somewhere in memory and possibly set a few graphics card registers. If that were the case, then I would think that the function should take at most a few thousand cycles if there's plenty of data to move around. – Cpp plus 1 Jun 18 '18 at 19:50
  • It has to communicate with the GPU. That's a hard task in itself to solve efficiently – Bálint Jun 18 '18 at 20:39
  • @Bálint But can't GPU registers and memory be mapped to regular RAM addresses? – Cpp plus 1 Jun 18 '18 at 20:42
  • @Cppplus1 It stil has to physically send data through a serial port while keeping the CPU and the GPU in sync – Bálint Jun 18 '18 at 20:49
  • 3
    Isn't this loading the whole Vulkan API and graphics driver? Why don't you think that would be expensive? – user253751 Jun 19 '18 at 01:37
  • Documentation: https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/vkCreateInstance.html - of course it does a lot more than just setting some data and registers. – Maximus Minimus Jun 19 '18 at 06:04

1 Answers1

4

vkCreateInstance does a lot of things, one it looks for all the installed driver ICD dlls by looking through the registry (in linux it looks through pre-agreed folders) and parsing json files the registry refers to and loads those dlls. Then the loader negotiates an API version with each of them. Then it finally calls vkLoadInstance in each of the dlls.

Each ICD will then create and/or load the vkPhysicalDevice it is associated with so you can query its properties.

That's a lot of stuff to do, however it only needs to happen once in each application so it's not worth optimizing that much.

A similarly expensive function is vkCreateGraphicsPipeline which creates a vkPipeline object that encodes all information needed to render a triangle. Optimization is what will take most time there, spending a few milliseconds to save a nanosecond per fragment is worth it when you render to anything above 420p or even more than once.

The functions that are called often and in the critical path are the ones that are cheap. This can happen because a lot of processing is front loaded into specific function calls that you can call on a separate thread to keep the main thread running along.

ratchet freak
  • 7,246
  • 19
  • 15