Quantcast
Jump to content

New Game Changing Vulkan Extensions for Mobile: Buffer Device Address


Recommended Posts

2021-07-06-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.

Android R is enabling a host of useful Vulkan extensions for mobile, with three being key 'game changers'. These are set to improve the state of graphics APIs for modern applications, enabling new use cases and changing how developers can design graphics renderers going forward. You can expect to see these features across a variety of Android smartphones, such as the new Samsung Galaxy S21, and existing Samsung Galaxy S models like the Samsung Galaxy S20. The first blog explored the first game changer extension for Vulkan – ‘Descriptor Indexing'. This blog explores the second game changer extension – ‘Buffer Device Address.’

VK_KHR_buffer_device_address

VK_KHR_buffer_device_address is a monumental extension that adds a unique feature to Vulkan that none of the competing graphics APIs support.

Pointer support is something that has always been limited in graphics APIs, for good reason. Pointers complicate a lot of things, especially for shader compilers. It is also near impossible to deal with plain pointers in legacy graphics APIs, which rely on implicit synchronization.

There are two key aspects to buffer_device_address (BDA). First, it is possible to query a GPU virtual address from a VkBuffer. This is a plain uint64_t. This address can be written anywhere you like, in uniform buffers, push constants, or storage buffers, to name a few.

The key aspect which makes this extension unique is that a SPIR-V shader can load an address from a buffer and treat it as a pointer to storage buffer memory immediately. Pointer casting, pointer arithmetic and all sorts of clever trickery can be done inside the shader. There are many use cases for this feature. Some are performance-related, and some are new use cases that have not been possible before.

Getting the GPU virtual address (VA)

There are some hoops to jump through here. First, when allocating VkDeviceMemory, we must flag that the memory supports BDA:

VkMemoryAllocateInfo info = {…};
VkMemoryAllocateFlagsInfo flags = {…};
flags.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT_KHR;
vkAllocateMemory(device, &info, NULL, &memory);

Similarly, when creating a VkBuffer, we add the VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_KHR usage flag. Once we have created a buffer, we can query the VA:

VkBufferDeviceAddressInfoKHR info = {…};
info.buffer = buffer;
VkDeviceSize va = vkGetBufferDeviceAddressKHR(device, &info);

From here, this 64-bit value can be placed in a buffer. You can of course offset this VA. Alignment is never an issue as shaders specify explicit alignment later.

A note on debugging

When using BDA, there are some extra features that drivers must support. Since a pointer does not necessarily exist when replaying an application capture in a debug tool, the driver must be able to guarantee that virtual addresses returned by the driver remain stable across runs. To that end, debug tools supply the expected VA and the driver allocates that VA range. Applications do not care that much about this, but it is important to note that even if you can use BDA, you might not be able to debug with it.

typedef struct VkPhysicalDeviceBufferDeviceAddressFeatures {
    VkStructureType  sType;
    void*                     pNext;
    VkBool32              bufferDeviceAddress;
    VkBool32              bufferDeviceAddressCaptureReplay;
    VkBool32              bufferDeviceAddressMultiDevice;
} VkPhysicalDeviceBufferDeviceAddressFeatures;

If bufferDeviceAddressCaptureReplay is supported, tools like RenderDoc can support BDA.

Using a pointer in a shader

In Vulkan GLSL, there is the GL_EXT_buffer_reference extension which allows us to declare a pointer type. A pointer like this can be placed in a buffer, or we can convert to and from integers:

#version 450
#extension GL_EXT_buffer_reference : require
#extension GL_EXT_buffer_reference_uvec2 : require
layout(local_size_x = 64) in;

 // These define pointer types.
layout(buffer_reference, std430, buffer_reference_align = 16) readonly buffer ReadVec4
{
    vec4 values[];
};

 layout(buffer_reference, std430, buffer_reference_align = 16) writeonly buffer WriteVec4
{
    vec4 values[];
};

 layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer UnalignedVec4
{
    vec4 value;
};

 layout(push_constant, std430) uniform Registers
{
     ReadVec4 src;
    WriteVec4 dst;
} registers;

Placing raw pointers in push constants avoids all indirection for getting to a buffer. If the driver allows it, the pointers can be placed directly in GPU registers before the shader begins executing.

Not all devices support 64-bit integers, but it is possible to cast uvec2 <-> pointer. Doing address computation like this is fine.

uvec2 uadd_64_32(uvec2 addr, uint offset)
{
    uint carry;
    addr.x = uaddCarry(addr.x, offset, carry);
    addr.y += carry;
    return addr;
}

void main()
{
    uint index = gl_GlobalInvocationID.x;
    registers.dst.values[index] = registers.src.values[index];
     uvec2 addr = uvec2(registers.src);
    addr = uadd_64_32(addr, 20 * index);

Cast a uvec2 to address and load a vec4 from it. This address is aligned to 4 bytes.

    registers.dst.values[index + 1024] = UnalignedVec4(addr).value;
}

Pointer or offsets?

Using raw pointers is not always the best idea. A natural use case you could consider for pointers is that you have tree structures or list structures in GPU memory. With pointers, you can jump around as much as you want, and even write new pointers to buffers. However, a pointer is 64-bit and a typical performance consideration is to use 32-bit offsets (or even 16-bit offsets) if possible. Using offsets is the way to go if you can guarantee that all buffers live inside a single VkBuffer. On the other hand, the pointer approach can access any VkBuffer at any time without having to use descriptors. Therein lies the key strength of BDA.

Extreme hackery: physical pointer as specialization constants

This is a life saver in certain situations where you are desperate to debug something without any available descriptor set.

A black magic hack is to place a BDA inside a specialization constant. This allows for accessing a pointer without using any descriptors. Do note that this breaks all forms of pipeline caching and is only suitable for debug code. Do not ship this kind of code. Perform this dark sorcery at your own risk:

#version 450
#extension GL_EXT_buffer_reference : require
#extension GL_EXT_buffer_reference_uvec2 : require
layout(local_size_x = 64) in;

layout(constant_id = 0) const uint DEBUG_ADDR_LO = 0;
layout(constant_id = 1) const uint DEBUG_ADDR_HI = 0;

layout(buffer_reference, std430, buffer_reference_align = 4) buffer DebugCounter
{
    uint value;
};

void main()
{
    DebugCounter counter = DebugCounter(uvec2(DEBUG_ADDR_LO, DEBUG_ADDR_HI));
    atomicAdd(counter.value, 1u);
}

Emitting SPIR-V with buffer_device_address

In SPIR-V, there are some things to note. BDA is an especially useful feature for layering other APIs due to its extreme flexibility in how we access memory. Therefore, generating BDA code yourself is a reasonable use case to assume as well.

Enables BDA in shaders.

_OpCapability PhysicalStorageBufferAddresses
OpExtension "SPV_KHR_physical_storage_buffer"_

The memory model is PhysicalStorageBuffer64 and not logical anymore.

_OpMemoryModel PhysicalStorageBuffer64 GLSL450_

The buffer reference types are declared basically just like SSBOs.

_OpDecorate %_runtimearr_v4float ArrayStride 16
OpMemberDecorate %ReadVec4 0 NonWritable
OpMemberDecorate %ReadVec4 0 Offset 0
OpDecorate %ReadVec4 Block
OpDecorate %_runtimearr_v4float_0 ArrayStride 16
OpMemberDecorate %WriteVec4 0 NonReadable
OpMemberDecorate %WriteVec4 0 Offset 0
OpDecorate %WriteVec4 Block
OpMemberDecorate %UnalignedVec4 0 NonWritable
OpMemberDecorate %UnalignedVec4 0 Offset 0
OpDecorate %UnalignedVec4 Block_

Declare a pointer to the blocks. PhysicalStorageBuffer is the storage class to use.

OpTypeForwardPointer %_ptr_PhysicalStorageBuffer_WriteVec4 PhysicalStorageBuffer
%_ptr_PhysicalStorageBuffer_ReadVec4 = OpTypePointer PhysicalStorageBuffer %ReadVec4
%_ptr_PhysicalStorageBuffer_WriteVec4 = OpTypePointer PhysicalStorageBuffer %WriteVec4
%_ptr_PhysicalStorageBuffer_UnalignedVec4 = OpTypePointer PhysicalStorageBuffer %UnalignedVec4

Load a physical pointer from PushConstant.

_%55 = OpAccessChain %_ptr_PushConstant__ptr_PhysicalStorageBuffer_WriteVec4 %registers %int_1    
%56 = OpLoad %_ptr_PhysicalStorageBuffer_WriteVec4 %55_

Access chain into it.

_%66 = OpAccessChain %_ptr_PhysicalStorageBuffer_v4float %56 %int_0 %40_

Aligned must be specified when dereferencing physical pointers. Pointers can have any arbitrary address and must be explicitly aligned, so the compiler knows what to do.

OpStore %66 %65 Aligned 16

For pointers, SPIR-V can bitcast between integers and pointers seamlessly, for example:

%61 = OpLoad %_ptr_PhysicalStorageBuffer_ReadVec4 %60
%70 = OpBitcast %v2uint %61

// Do math on %70
%86 = OpBitcast %_ptr_PhysicalStorageBuffer_UnalignedVec4 %some_address

Conclusion

We have already explored two key Vulkan extension game changers through this blog and the previous one. The third and final part of this game changer blog series will explore ‘Timeline Semaphores’ and how developers can use this new extension to improve the development experience and enhance their games.

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



  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Popular Days

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 BGR
      Samsung has been trying to beat the iPhone for years. And it has failed for years. Apple still makes the most coveted flagship phones in town, and Samsung is still a fast follower in the business. Even now, in 2022, Samsung continues to copy Apple’s new iPhone features all the time. The iPhone 14 Pro’s success is another pain point, and it must hurt Samsung even more considering the Galaxy S22 series’ blunders.
      Samsung might criticize and mock the newest iPhones publicly, but it probably loves the success of Apple’s new iPhone series. That’s because Samsung happens to be a massive supplier of a critical component that Apple uses to manufacture iPhones: OLED screens. And the iPhone 14 Pro and Pro Max got even more sophisticated OLED panels this year. The big winner? Samsung Display.
      Even before Apple’s early September iPhone 14 launch event, reports claimed that Samsung would be the main supplier of Apple’s OLED panels. LG and BOE would also supply displays for certain iPhone models, although Samsung would get the largest share.
      A new report from The Elec claims that Samsung will supply over 70% of the OLED panels Apple needs for the iPhone 14 series.
      Apple is expected to need about 120 million OLED screens for the iPhone 14 series, and Samsung will reportedly account for more than 80 million units. LG Display will supposedly make 20+ million units, while BOE will ship 6 million screens to Apple. LG and BOE reportedly encountered setbacks that allowed Samsung to increase its share.
      iPhone 14 Pro on a throw. Image source: Jonathan Geller, BGR The report notes Apple needs two types of panels. The low-temperature polycrystalline silicon (LTPS) thin-film transistor (TFT) OLED equips the iPhone 14 and iPhone 14 Plus.
      The iPhone 14 Pro and iPhone 14 Pro Max feature low-temperature polycrystalline oxide (LTPO) TFT OLED panels. These are the displays that support 120Hz dynamic refresh rates. The refresh drops as low as 1Hz, enabling the always-on display functionality exclusive to the Pro models.
      Samsung supplies OLED panels for all four iPhone 14 models. But some 60 million Samsung OLED panels will serve the iPhone 14 Pro versions. These should be the most expensive OLED panels that Apple utilizes this year.
      A report from Nikkei said recently that the iPhone 14 Pro Max’s cost of parts rose to $501 this year, a $60 increase over the iPhone 13 Pro Max. The A16 Bionic chip is an expensive component, according to the report. But 24.8% of that cost goes to South Korean suppliers.
      Samsung and LG are two of them. It’s unclear how many other South Korean suppliers make parts for the iPhone 14 Pro Max. But the OLED display is one of the iPhone’s most expensive parts no matter what company makes it.
      Per The Elec, LG makes OLED panels for the 6.1-inch iPhone 14 and the 6.7-inch iPhone 14 Pro Max. BOE only scored contracts for the 6.1-inch iPhone 14. This indicates neither company is ready to challenge Samsung in the short term. 
      At the end of the day, the iPhone 14 series will add billions of dollars to Samsung’s earnings. That’s a great perk to have from a smartphone that outsells Samsung’s own flagship Galaxy smartphones by an order of magnitude.
      Of note, Samsung will probably continue to benefit greatly from iPhone sales in the future unless LG and BOE can significantly up their game. All iPhone 15 models launching next year will feature OLED panels.
      The post Samsung secretly loves the iPhone, and that might never change appeared first on BGR.
      View the full article
    • By BGR
      Samsung is obviously one of the biggest consumer electronics brands on the planet. That’s why the company’s Black Friday deals are so popular each year. And you seriously won’t believe all the fantastic Black Friday Samsung deals out there for 2022.
      These prices are so unbelievably low that some of them truly seem like mistakes. We guarantee that several of these incredible deals will definitely sell out!
      Featured deals in this article:

      SAMSUNG Galaxy Z Fold 4 Cell Phone, Factory Unlocked Android Smartphone, 512GB, Flex Mode, Hand…
      Price: $799.99
      You Save: $430.00 (22%)
      Buy Now
      SAMSUNG Galaxy Z Flip 3 5G Factory Unlocked Android Cell Phone US Version Smartphone Flex Mode…
      Price: $749.99
      You Save: $225.00 (25%)
      Buy Now
      SAMSUNG 14" Galaxy Book2 Business Laptop Computer, i7P / 32GB / 1TB, Windows 11 PRO, 12th Gen I…
      Price: $735 Off
      You Save: $627.64 (26%)
      Buy Now
      SAMSUNG 85" Class Neo QLED 4K QN90B Series Mini LED Quantum HDR 32x, Dolby Atmos,Object Trackin…
      Price: $2,500 Off
      Buy Now
      Samsung makes best-selling products across every category, from smartphones and laptops to TVs, home appliances, and so much more.
      One of the only problems with Samsung products might be that the high-end models are often quite expensive. That’s especially true with Samsung TVs, though it makes plenty of sense.
      After all, Samsung TVs are among the best on the planet. And if you’re in the market for one, or if you’re getting anything else from Samsung this season, we have good news.
      Samsung Black Friday deals are now available… and they’re incredible!
      Samsung Black Friday 2022 Deals
      Price: Check the Latest Prices
      Buy Now Big Samsung blowout sale
      Recently, for one day only, Samsung offered a slew of incredible sales in addition to the ones that will be available all month long. We’re also talking about all-time low prices on Samsung devices from across every category. Now, so many of those deals are back at the same or better prices.
      You won’t believe the Samsung Black Friday deals for 2022!
      First and foremost, Samsung Galaxy smartphones and cases have deep discounts. Some of the phones in Samsung’s Galaxy S22 lineup are included in this monster sale. And there are deep discounts available on the Galaxy Z Fold4 and ZFlip4, too!
      Next up, you can save an extra 30% on Samsung smartwatches, Galaxy Buds, and more. All of these incredible smartphone accessories are down to rock-bottom prices today.
      Laptops & Chromebooks with deep discounts

      Samsung laptop deals might be the hottest sales among our readers. The Samsung Chromebook 4 is on sale for under $150, and also the Samsung Galaxy Book2 is up to $735 off!
      Samsung Chromebook 4 Chrome OS 11.6" HD Intel Celeron Processor N4000 4GB RAM 32GB eMMC Gigabit…
      Price: $128.97
      You Save: $101.02 (44%)
      Buy Now SAMSUNG 14" Galaxy Book2 Business Laptop Computer, i7P / 32GB / 1TB, Windows 11 PRO, 12th Gen I…
      Price: $1,822.36
      You Save: $627.64 (26%)
      Buy Now More Samsung Black Friday deals
      It doesn’t matter if you’re in the market for a stunning Samsung TV or a Samsung Chromebook. Whatever you’re looking for, it’s on sale right now with a deep discount. Seriously, the Samsung Black Friday deals for 2022 are crazy!
      Looking for a hot new smartphone? The Galaxy Z Fold4 is up to $350 off right now and the Galaxy S22 is also discounted. Or, step up to the Galaxy S22 Ultra and you’ll save even more.
      Now, if you’re looking for TVs, you won’t believe your eyes. Samsung is offering the deepest discounts of the year right now — like up to $2,500 off class-leading Samsung QN90B 4K QLED smart TVs!
      SAMSUNG 85" Class Neo QLED 4K QN90B Series Mini LED Quantum HDR 32x, Dolby Atmos,Object Trackin…
      Price: Price too low to display
      Buy Now All of these deals are available from right now through the end of the month. Of course, there’s no question whatsoever that some of them will sell out long before then.
      Scroll down for all the amazing Samsung Black Friday deals of 2022.
      All the best Samsung Black Friday deals in 2022
      Here’s the full list of all the best deals Samsung is offering for Black Friday 2022. Many of these sales are available from Samsung’s online store, while others can be found at retailers including Amazon, Best Buy, and Walmart.
      Smartphones
      Galaxy Z Fold3: $600 instant rebate Galaxy Z Fold4: $350 instant rebate and up to $1000 enhanced trade-in Galaxy Z Flip3: $200 instant rebate Galaxy Z Flip4: $150 instant rebate and up to $600 enhanced trade-in S22 Ultra: $225 instant rebate and up to $600 enhanced trade-in S22+: $150 instant rebate and up to $500 enhanced trade-in S21 FE: $100 instant rebate and up to $350 enhanced trade-in S22: $75 instant rebate and up to $400 enhanced trade-in A53: $75 instant rebate and up to $100 enhanced trade-in Laptops and Tablets

      SAMSUNG 14" Galaxy Book2 Business Laptop Computer, i7P / 32GB / 1TB, Windows 11 PRO, 12th Gen I…
      Price: $1,715.00
      You Save: $627.64 (26%)
      Buy Now
      SAMSUNG 14" Galaxy Book2 Business Laptop Computer, i7P / 16GB / 512GB, Windows 11 PRO, 12th Gen…
      Price: $1,505.00
      You Save: $550.79 (26%)
      Buy Now
      SAMSUNG 14" Galaxy Book2 Business Laptop Computer, i5P / 16GB / 256GB, Windows 11 PRO, 12th Gen…
      Price: $1,295.00
      You Save: $473.93 (26%)
      Buy Now
      SAMSUNG Galaxy Book2 Pro 13.3” 256GB Laptop Computer w/ 8GB RAM, 12th Gen Intel Core i5 Evo Cer…
      Price: $749.99
      You Save: $350.00 (32%)
      Buy Now
      SAMSUNG 15.6” Galaxy Book2 Pro Laptop Computer, i5 / 8GB / 512GB, 12th Gen Intel Core Processor…
      Price: $899.99
      You Save: $450.99 (36%)
      Buy Now
      Samsung Chromebook Plus V2, 2-in-1, 4GB RAM, 32GB eMMC, 13MP Camera, Chrome OS, 12.2", 16:10 As…
      Price: $194.99
      You Save: $305.00 (61%)
      Buy Now
      Samsung Galaxy Chromebook Go-Laptop Computer Lightweight Slim Durable Design 12-Hour-Battery Wi…
      Price: $243.99
      You Save: $70.40 (23%)
      Buy Now
      SAMSUNG Galaxy TabActive4 Pro 10.1” 64GB Wi-Fi Android Work Tablet, LTE Unlocked, 4GB RAM, Rugg…
      Price: $454.00
      You Save: $195.99 (30%)
      Buy Now
      Samsung Galaxy Tab A7 10.4 Wi-Fi 32GB Gray (SM-T503NZAAXAR)
      Price: $132.99
      You Save: $57.00 (30%)
      Buy Now
      Samsung Galaxy Tab Active PRO 10.1" | 64GB & WiFi Water-Resistant Rugged Tablet, Black – SM-T54…
      Price: $349.99
      You Save: $250.00 (42%)
      Buy Now
      Watches and Buds
      Watch5 Series: up to $70 instant rebate Watch4: up to $90 instant rebate Buds Live: $70 off Buds2: $50 off Buds2 Pro: $30 off Accessories
      SmartTag+: 2 for $79.98 If that’s somehow not enough, you’ll find even more deals right here:
      Samsung Black Friday 2022 Deals
      Price: Check the Latest Prices
      Buy Now More Black Friday 2022 coverage
      If you’re looking for all the best deals for Black Friday 2022, you’ll find them in our extensive coverage.
      The shopping experts at BGR have combed through all the biggest Black Friday sales online. We have hand-picked all the best deals with the deepest discounts on popular items. The team here at BGR has more than 4 decades of combined experience finding all the best bargains for our readers. With that in mind, you can rest assured that this is your one-stop shop for all of your Black Friday deal-hunting needs.
      Below, you’ll find all of BGR’s best Black Friday 2022 deals roundups. Be sure to browse through them all if you want to see the very best sales that are available this year.
      🚨 Black Friday deals 2022: Our mega-roundup of all the best sales 🚨 Best Black Friday Apple Deals Best Black Friday Smart Home Deals Top Black Friday TV Deals Best Black Friday Best Buy Deals Best Black Friday Amazon Deals Top Black Friday Walmart Deals Best Black Friday Target Deals Top Black Friday Laptop Deals Best Black Friday Video Game Deals Top Black Friday Tablet Deals Best Black Friday Desktop Computer Deals Top Black Friday Headphones Deals Top Black Friday Sonos Deals 🎅🎄 Don’t miss BGR’s big Christmas 2022 gift guide with 100+ gift ideas! 🎄🎅 Go here to see this month’s best deals online!
      The post Samsung Black Friday deals: TVs, laptops, Galaxy phones, more appeared first on BGR.
      View the full article
    • By STF News
      With the Odyssey OLED G8 — the first OLED gaming monitor from Samsung Electronics — you won’t miss a thing, no matter what game you play.
       
      The Odyssey OLED G8 is designed for lightning-fast game play. The screen’s 175Hz refresh rate and 0.1ms response time, combined with a wide 21:9 ratio, will keep you one step ahead of the competition.
       
      You can soak in every detail with the monitor’s VESA DisplayHDR 400 True Black Certification and true RGB, which makes stories come to life on the ultra-thin 34-inch screen. You can surround yourself in every scene with the monitor’s updated CoreSync & Core Lighting+ technology, which matches your game’s onscreen colors for world-blending immersion. What’s more, the Odyssey OLED G8 comes with a 3-year warranty — so you can enjoy all-night gaming sessions for years to come.
       
      To learn more about how the Odyssey OLED G8 offers a next-level gaming experience, check out the infographic below.
       

      View the full article



×
×
  • Create New...