Quantcast
Jump to content

New Game Changing Vulkan Extensions for Mobile: Timeline Semaphores


STF News
 Share

Recommended Posts

2021-06-28-01-banner.jpg

The Samsung Developers team works with many companies in the mobile and gaming ecosystems. We're excited to support our partner, Arm, as they bring timely and relevant content to developers looking to build games and high-performance experiences. This Vulkan Extensions series will help developers get the most out of the new and game-changing Vulkan extensions on Samsung mobile devices.

In previous blogs, we have already explored two key Vulkan extension game changers that will be enabled by Android R. These are Descriptor Indexing and Buffer Device Address. In this blog, we explore the third and final game changer, which is 'Timeline Semaphores'.

The introduction of timeline semaphores is a large improvement to the synchronization model of Vulkan and is a required feature in Vulkan 1.2. It solves some fundamental grievances with the existing synchronization APIs in Vulkan.

The problems with VkFence and VkSemaphore

In earlier Vulkan extensions, there are two distinct synchronization objects for dealing with CPU <-> GPU synchronization and GPU queue <-> GPU queue synchronization.

The VkFence object only deals with GPU -> CPU synchronization. Due to the explicit nature of Vulkan, you must keep track of when the GPU completes the work you submit to it.

vkQueueSubmit(queue, …, fence);

The previous code is the way we would use a fence, and later this fence can be waited on. When the fence signals, we know it is safe to free resources, read back data written by GPU, and so on. Overall, the VkFence interface was never a real problem in practice, except that it feels strange to have two entirely different API objects which essentially do the same thing.

VkSemaphore on the other hand has some quirks which makes it difficult to use properly in sophisticated applications. VkSemaphore by default is a binary semaphore. The fundamental problem with binary semaphores is that we can only wait for a semaphore once. After we have waited for it, it automatically becomes unsignaled again. This binary nature is very annoying to deal with when we use multiple queues. For example, consider a scenario where we perform some work in the graphics queue, and want to synchronize that work with two different compute queues. If we know this scenario is coming up, we will then have to allocate two VkSemaphore objects, signal both objects, and wait for each of them in the different compute queues. This works, but we might not have the knowledge up front that this scenario will play out. Often where we are dealing with multiple queues, we have to be somewhat conservative and signal semaphore objects we never end up waiting for. This leads to another problem …

A signaled semaphore, which is never waited for, is basically a dead and useless semaphore and should be destroyed. We cannot reset a VkSemaphore object on the CPU, so we cannot ever signal it again if we want to recycle VkSemaphore objects. A workaround would be to wait for the semaphore on the GPU in a random queue just to unsignal it, but this feels like a gross hack. It could also potentially cause performance issues, as waiting for a semaphore is a full GPU memory barrier.

Object bloat is another considerable pitfall of the existing APIs. For every synchronization point we need, we require a new object. All these objects must be managed, and their lifetimes must be considered. This creates a lot of annoying “bloat” for engines.

The timeline – fixing object bloat – fixing multiple waits

The first observation we can make of a Vulkan queue is that submissions should generally complete in-order. To signal a synchronization object in vkQueueSubmit, the GPU waits for all previously submitted work to the queue, which includes the signaling operation of previous synchronization objects. Rather than assigning one object per submission, we synchronize in terms of number of submissions. A plain uint64_t counter can be used for each queue. When a submission completes, the number is monotonically increased, usually by one each time. This counter is contained inside a single timeline semaphore object. Rather than waiting for a specific synchronization object which matches a particular submission, we could wait for a single object and specify “wait until graphics queue submission #157 completes.”

We can wait for any value multiple times as we wish, so there is no binary semaphore problem. Essentially, for each VkQueue we can create a single timeline semaphore on startup and leave it alone (uint64_t will not overflow until the heat death of the sun, do not worry about it). This is extremely convenient and makes it so much easier to implement complicated dependency management schemes.

Unifying VkFence and VkSemaphore

Timeline semaphores can be used very effectively on CPU as well:

VkSemaphoreWaitInfoKHR info = { VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO_KHR };
info.semaphoreCount = 1;
info.pSemaphores = &semaphore;
info.pValues = &value;
vkWaitSemaphoresKHR(device, &info, timeout);

This completely removes the need to use VkFence. Another advantage of this method is that multiple threads can wait for a timeline semaphore. With VkFence, only one thread could access a VkFence at any one time.

A timeline semaphore can even be signaled from the CPU as well, although this feature feels somewhat niche. It allows use cases where you submit work to the GPU early, but then 'kick' the submission using vkSignalSemaphoreKHR. The accompanying sample demonstrates a particular scenario where this function might be useful:

VkSemaphoreSignalInfoKHR info = { VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO_KHR };
info.semaphore = semaphore;
info.value = value;
vkSignalSemaphoreKHR(device, &info);

Creating a timeline semaphore

When creating a semaphore, you can specify the type of semaphore and give it an initial value:

VkSemaphoreCreateInfo info = { VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO };
VkSemaphoreTypeCreateInfoKHR type_info = { VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO_KHR };
type_info.semaphoreType = VK_SEMAPHORE_TYPE_TIMELINE_KHR;
type_info.initialValue = 0;
info.pNext = &type_info;
vkCreateSemaphore(device, &info, NULL, &semaphore);

Signaling and waiting on timeline semaphores

When submitting work with vkQueueSubmit, you can chain another struct which provides counter values when using timeline semaphores, for example:

VkSubmitInfo submit = { VK_STRUCTURE_TYPE_SUBMIT_INFO };
submit.waitSemaphoreCount = 1;
submit.pWaitSemaphores = &compute_queue_semaphore;
submit.pWaitDstStageMask = &wait_stage;
submit.commandBufferCount = 1;
submit.pCommandBuffers = &cmd;
submit.signalSemaphoreCount = 1;
submit.pSignalSemaphores = &graphics_queue_semaphore;
 VkTimelineSemaphoreSubmitInfoKHR timeline = {
VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO_KHR };
timeline.waitSemaphoreValueCount = 1;
timeline.pWaitSemaphoreValues = &wait_value;
timeline.signalSemaphoreValueCount = 1;
timeline.pSignalSemaphoreValues = &signal_value;
submit.pNext = &timeline;
 signal_value++; // Generally, you bump the timeline value once per submission.
 vkQueueSubmit(queue, 1, &submit, VK_NULL_HANDLE);

Out of order signal and wait

A strong requirement of Vulkan binary semaphores is that signals must be submitted before a wait on a semaphore can be submitted. This makes it easy to guarantee that deadlocks do not occur on the GPU, but it is also somewhat inflexible. In an application with many Vulkan queues and a task-based architecture, it is reasonable to submit work that is somewhat out of order. However, this still uses synchronization objects to ensure the right ordering when executing on the GPU. With timeline semaphores, the application can agree on the timeline values to use ahead of time, then go ahead and build commands and submit out of order. The driver is responsible for figuring out the submission order required to make it work. However, the application gets more ways to shoot itself in the foot with this approach. This is because it is possible to create a deadlock with multiple queues where queue A waits for queue B, and queue B waits for queue A at the same time.

Ease of porting

It is no secret that timeline semaphores are inherited largely from D3D12’s fence objects. From a portability angle, timeline semaphores make it much easier to have compatibility across the APIs.

Caveats

As the specification stands right now, you cannot use timeline semaphores with swap chains. This is generally not a big problem as synchronization with the swap chain tends to be explicit operations renderers need to take care of.

Another potential caveat to consider is that the timeline semaphore might not have a direct kernel equivalent on current platforms, which means some extra emulation to handle it, especially the out-of-order submission feature. As the timeline synchronization model becomes the de-facto standard, I expect platforms to get more native support for it.

Conclusion

All three key Vulkan extension game changers improve the overall development and gaming experience through improving graphics and enabling new gaming use cases. We hope that we gave you enough samples to get you started as you try out these new Vulkan extensions to help bring your games to life

Follow Up

Thanks to Hans-Kristian Arntzen and the team at Arm for bringing this great content to the Samsung Developers community. We hope you find this information about Vulkan extensions useful for developing your upcoming mobile games.

The Samsung Developers site has many resources for developers looking to build for and integrate with Samsung devices and services. Stay in touch with the latest news by creating a free account or by subscribing to our monthly newsletter. Visit the Marketing Resources page for information on promoting and distributing your apps and games. Finally, our developer forum is an excellent way to stay up-to-date on all things related to the Galaxy ecosystem.

View the full blog at its source

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Similar Topics

    • By STF News
      Samsung Electronics today announced that its 2022 QLED and Lifestyle TVs have been recognized by leading global certification institutes for eye safety and color technology. The news comes as the company announced its newest QLED and Lifestyle TVs at CES 2022.
       

       
      The 2022 Samsung Lifestyle TVs won the ‘Eye Care’ Certification from Verband Deutscher Elektrotechniker (VDE) in Germany, one of Europe’s largest technical-scientific associations with more than 36,000 members. The certification applies to Samsung’s 2022 Lifestyle TV models including The Frame, The Serif and The Sero. The screens are evaluated on various categories, including ‘Safety’, ‘Gentle to the eyes’, flicker level, uniformity and color fidelity.
       
      (From left) Seokwoo Yong, EVP and Head of R&D Team, Visual Display Business at Samsung Electronics and Cherif Kedir, President & CEO of VDE
       
      The new Lifestyle TVs were assessed for safety from blue light emission and melatonin inhibition levels based on a light hazard classification method set by the International Electrotechnical Commission (IEC). Samsung’s 2022 Lifestyle TVs satisfy the IEC’s standards for screen flickering, which can cause eye fatigue or headache for viewers. They were also recognized for excellence in color fidelity and picture quality uniformity, both elements of which contribute to eye comfort level while watching TV.
       
      (From left) Seokwoo Yong, EVP and Head of R&D Team, Visual Display Business at Samsung Electronics and Wyatt Brannan, Vice President of North America Consumer, Medical & Information Technology of UL
       
      Samsung’s 2022 Lifestyle TVs1 were also verified as ‘Glare-Free’ by Underwriters Laboratories (UL), a leading independent safety science company. UL’s verification validates the ‘Glare-Free’ claim by assessing the products against Unified Glare Rating (UGR) testing standard set by the International Commission on Illumination (CIE). Samsung’s new Lifestyle TV models use a new Matte Display with anti-glare, anti-reflection and anti-fingerprint properties to deliver the optimal brightness and provide the best picture quality without glare.
       
      Reflected Glare, which determines whether the objects on a TV screen are visible even when external light is reflected on the surface Discomfort Glare, which determines whether a TV screen is too bright Disability Glare, which determines whether a TV screen is overly bright when watching TV in a dark room  
      These glare assessments were calculated based on test results of watching TV in both 300 lux, which is equivalent to a brightly lit work area, and in 150 lux, which is usually the value for a dimly lit work area.
       
      (From left) Seokwoo Yong, EVP and Head of R&D Team, Visual Display Business at Samsung Electronics and Raj Shah, Vice President at Marketing of Pantone
       
      Additionally, Samsung’s all new 2022 QLED models received the world’s first ‘Pantone Validated’ certification from Pantone, the world-famous brand in the global color industry and creator of the Pantone Matching System (PMS).
       
      These models receiving this recognition from Pantone include all 20 newly released models – 15 QLED TVs in both 4K and 8K and five monitors. Samsung’s 2022 QLED TV line-up was recognized for its accurate expression of 2,030 Pantone colors and newly added 110 skin tone shades.
       
      “As TVs become more of an entertainment hub in the home, there’s an increased demand for screens with top-tier picture quality that minimize eye strain,” said Seokwoo Yong, Executive Vice President and Head of R&D Team, Visual Display Business at Samsung Electronics. “This recognition from leading global institutes validates our technology that delivers best-in-class images along with the most comfortable watching experience.”
       
       
      1 Applicable to Samsung’s 2022 Lifestyle TVs consisting of The Frame, The Serif and The Sero.
      View the full article
    • By regenitin
      Hello everyone.
      I recently purchased Samsung The Frame 2020 (QA55LS03TAKXXL) in India 2 very important observations from my end.
      I am planning to purchase the Samsung Q600A Soundbar to pair with my TV and wanted to know if my TV supports Q-Symphony. I reached out the support team and they confirmed me that the Q-Symphony function is not a feature of my TV. I wanted to know if Samsung plans to update the operating System to support Q Symphony as this feature is present in Samsung The Frame 2021 which has exact same hardware as Samsung The Frame 2020. Since the support did not have any idea i thought of reaching out to folks on this forum. I am not able to find Amazon Prime Music & Youtube Music apps on the Samsung App Store. These are supposed to be pretty necessary and widely used apps and I am not able to figure out the reason they are not in the store. I would like to know if there are any plans to launch these apps soon. Also let me know if there is any work around to get the apps installed. Thanks in advance for your inputs.
      Best Regards
      Nitin
    • By Alex
      Starting this year, Samsung's Tizen app store is no longer accessible, both to new and existing users. Last year in June, the company closed registrations and made the store available only to existing users and they could only get previously downloaded apps.

      After December 31, 2021, however, the Tizen app store is permanently closed. So in case you are using a Samsung Z series smartphone, it might be time to switch over to Android or iOS. The last Samsung Z4 phone running Tizen OS was released back in 2017 so it was kind of an expected turn of events.
      It seems like the company is dropping its Tizen project after this year's Galaxy Watch4 series is running on Google's Wear OS and all future Galaxy watches will do the same.
      Source: https://www.gsmarena.com/samsung_shuts_down_the_tizen_app_store-news-52598.php
    • By STF News
      Samsung Electronics announced that its 32-inch Odyssey Neo G8 monitor earned a Best of Innovation Award in the Gaming category at CES 2022.
       
      Samsung’s lineup received 2022 Honoree accolades across the board, including for the 55-inch Odyssey Ark, the 49-inch Odyssey Neo G9, the 32-inch Odyssey Neo G8, the 34-inch Odyssey G8, the 32-inch Smart Monitor M8 and the 32-inch High Resolution Monitor S8. This marks the ninth accolade that Samsung monitors have received at this year’s CES, setting a new record for the lineup at the world’s largest electronics show.
       
      Sponsored by the CTA, which owns and organizes CES, the CES Innovation Awards program spotlights standout examples of design and engineering across multiple consumer product categories. This marks the sixth straight year that Samsung monitors have earned CES Innovation Awards, solidifying the company’s position as the global leader in the gaming monitor market.
       
      Check out the infographic below to view this year’s complete list of award-winning monitors.
       

      View the full article
    • By STF News
      Want to see everything that’s going on at Samsung’s CES booth all in one interactive image? Then look no further!
       
      In the image below, simply hover the cursor over the product or event you want to know more about, then click. Videos providing more details will play right away, while you can then read articles that feature even more information on the various products and events. The content will continue to be updated as the event goes on.
       
      Have fun!
       
      View the full article
×
×
  • Create New...