Vulkan: Push constants #104880

Merged
Jeroen Bakker merged 73 commits from Jeroen-Bakker/blender:vulkan-push-constants into main 2023-03-06 12:29:06 +01:00
Member

What are push constants?

Push constants is a way to quickly provide a small amount of uniform data to shaders.
It should be much quicker than UBOs but a huge limitation is the size of data - spec
requires 128 bytes to be available for a push constant range.

What are the challenges with push constants?

The challenge with push constants is that the limited available size. According to
the Vulkan spec each platform should at least have 128 bytes reserved for push
constants. Current Mesa/AMD drivers supports 256 bytes, but Mesa/Intel is only 128
bytes.

What is our solution?

Some shaders of Blender uses more than these boundaries. When more data is needed
push constants will not be used, but the shader will be patched to use an uniform
buffer instead. This mechanism will be part of the Vulkan backend and shader
developers should not see any difference on API level.

Known limitations

Current state of the vulkan backend does not track resources that are in the
command queue. This patch includes some test cases that identified this issue as
well. See #104771.

**What are push constants?** Push constants is a way to quickly provide a small amount of uniform data to shaders. It should be much quicker than UBOs but a huge limitation is the size of data - spec requires 128 bytes to be available for a push constant range. **What are the challenges with push constants?** The challenge with push constants is that the limited available size. According to the Vulkan spec each platform should at least have 128 bytes reserved for push constants. Current Mesa/AMD drivers supports 256 bytes, but Mesa/Intel is only 128 bytes. **What is our solution?** Some shaders of Blender uses more than these boundaries. When more data is needed push constants will not be used, but the shader will be patched to use an uniform buffer instead. This mechanism will be part of the Vulkan backend and shader developers should not see any difference on API level. **Known limitations** Current state of the vulkan backend does not track resources that are in the command queue. This patch includes some test cases that identified this issue as well. See #104771.
Jeroen Bakker added 25 commits 2023-02-17 15:22:37 +01:00
This patch adds initial support for compute shaders and SSBOs to
the vulkan backend. As the development is oriented to the test-
cases we have the implementation is limited to what is used there.

It has been validated that with this patch 2 test cases are running
as expected
- `GPUVulkanTest.gpu_shader_compute_ssbo`
- `GPUVulkanTest.gpu_storage_buffer_create_update_read`

This patch includes:
- Allocating VkBuffer on device.
- Uploading data from CPU to VkBuffer.
- Binding VkBuffer as SSBO to a compute shader.
- Execute compute shader and altering VkBuffer.
- Download the VkBuffer to CPU ram.
- Validate that it worked.

GHOST API has been changed as the original design was created before
we even had support for compute shaders in blender. The function `GHOST_getVulkanBackbuffer` has been separated to retrieve the command
buffer without a backbuffer (`GHOST_getVulkanCommandBuffer`). In order
to do correct command buffer processing we needed access to the queue
owned by GHOST. This is returned as part of the `GHOST_getVulkanHandles`
function.
Added image samplers and uniform buffers. Required for
shader builder.
Jeroen Bakker added this to the 3.6 LTS milestone 2023-02-17 15:46:49 +01:00
Jeroen Bakker added this to the Viewport & EEVEE project 2023-02-17 15:46:53 +01:00
Jeroen Bakker self-assigned this 2023-02-17 15:47:03 +01:00
Jeroen Bakker added a new dependency 2023-02-18 17:51:43 +01:00
Jeroen Bakker removed a dependency 2023-02-18 17:51:56 +01:00
Jeroen Bakker added a new dependency 2023-02-18 17:52:35 +01:00
Jeroen Bakker force-pushed vulkan-push-constants from b904eb6859 to a8dc114efc 2023-02-21 08:02:16 +01:00 Compare
Jeroen Bakker added 1 commit 2023-02-21 14:34:34 +01:00
Eventually we want to use uniform buffers as they are a bit faster.
But they require std140.
Jeroen Bakker added 2 commits 2023-02-21 14:37:54 +01:00
Jeroen Bakker added 1 commit 2023-02-21 15:14:00 +01:00
Jeroen Bakker reviewed 2023-02-21 15:26:06 +01:00
@ -50,2 +53,3 @@
vk_device, nullptr, 1, &pipeline_info, vk_allocation_callbacks, &vk_pipeline) !=
VK_SUCCESS) {
return VKPipeline();
//return VKPipeline();
Author
Member

This should be fixed.

This should be fixed.
Jeroen-Bakker marked this conversation as resolved
Jeroen Bakker added 1 commit 2023-02-21 15:40:35 +01:00
Jeroen Bakker added 1 commit 2023-02-21 16:18:04 +01:00
Jeroen Bakker reviewed 2023-02-21 16:19:08 +01:00
@ -0,0 +112,4 @@
return 0;
}
size_t size_in_bytes() const
Author
Member

Add const VKPushConstantsLayout& layout_get() const as API and clean up the interface.

Add `const VKPushConstantsLayout& layout_get() const` as API and clean up the interface.
Jeroen-Bakker marked this conversation as resolved
Jeroen Bakker added 1 commit 2023-02-21 20:28:34 +01:00
Jeroen Bakker reviewed 2023-02-21 20:32:46 +01:00
@ -889,0 +926,4 @@
/* Add push constants to the descriptor when push constants are stored in a storage buffer.*/
const VKPushConstantsLayout &push_constants_layout = interface.push_constants_layout_get();
if (push_constants_layout.storage_type_get() ==
VKPushConstantsLayout::StorageType::STORAGE_BUFFER) {
Author
Member

Also check for uniform_buffer

Also check for uniform_buffer
Jeroen-Bakker marked this conversation as resolved
Jeroen Bakker reviewed 2023-02-21 20:33:37 +01:00
@ -1033,12 +1096,6 @@ std::string VKShader::vertex_interface_declare(const shader::ShaderCreateInfo &i
ss << "layout(location = " << attr.index << ") ";
ss << "in " << to_string(attr.type) << " " << attr.name << ";\n";
}
/* NOTE(D4490): Fix a bug where shader without any vertex attributes do not behave correctly.
Author
Member

Should be removed outside this PR.

Should be removed outside this PR.
Jeroen-Bakker marked this conversation as resolved
Jeroen Bakker added 1 commit 2023-02-21 21:40:24 +01:00
Jeroen Bakker added 1 commit 2023-02-22 07:05:20 +01:00
Jeroen Bakker added 2 commits 2023-02-22 10:21:23 +01:00
Jeroen Bakker added 1 commit 2023-02-22 12:07:23 +01:00
Jeroen Bakker reviewed 2023-02-22 12:31:28 +01:00
@ -58,0 +80,4 @@
.push_constant(Type::FLOAT, "filler2", 32)
.do_static_compilation(true);
/* It is expected that this shader will use uniform buffers and not push constants.*/
Author
Member

Remove line as this is vulkan specific.

Remove line as this is vulkan specific.
Jeroen-Bakker marked this conversation as resolved
Jeroen Bakker added 1 commit 2023-02-22 12:42:59 +01:00
Jeroen Bakker reviewed 2023-02-22 12:44:58 +01:00
@ -119,6 +137,10 @@ void VKDescriptorSet::update(VkDevice vk_device)
descriptor_writes.append(write_descriptor);
}
BLI_assert_msg(image_infos.size() + buffer_infos.size() == descriptor_writes.size(),
Author
Member

Apply to main directly.

Apply to main directly.
Jeroen-Bakker marked this conversation as resolved
Jeroen Bakker added 1 commit 2023-02-22 13:05:59 +01:00
Jeroen Bakker added a new dependency 2023-02-22 14:40:19 +01:00
Jeroen Bakker added 2 commits 2023-02-22 14:52:51 +01:00
Jeroen Bakker added 2 commits 2023-02-22 17:47:45 +01:00
Jeroen Bakker added 1 commit 2023-02-22 17:54:40 +01:00
Jeroen Bakker added 1 commit 2023-02-22 19:23:54 +01:00
Jeroen Bakker added 3 commits 2023-02-22 21:02:06 +01:00
Jeroen Bakker added 1 commit 2023-02-23 12:56:50 +01:00
Jeroen Bakker changed title from WIP: Vulkan: Push constants to Vulkan: Push constants 2023-02-23 13:02:16 +01:00
Jeroen Bakker requested review from Clément Foucault 2023-02-23 13:02:23 +01:00
Jeroen Bakker requested review from Bastien Montagne 2023-02-23 13:02:36 +01:00
Jeroen Bakker added 3 commits 2023-02-23 14:59:52 +01:00
Jeroen Bakker added 1 commit 2023-02-23 15:03:16 +01:00
Jeroen Bakker added 1 commit 2023-02-23 17:57:49 +01:00
Jeroen Bakker added 1 commit 2023-02-23 18:21:03 +01:00
Jeroen Bakker added 2 commits 2023-02-23 18:25:31 +01:00
Clément Foucault approved these changes 2023-02-27 10:21:04 +01:00
Clément Foucault left a comment
Member

Pretty solid. 👍

Pretty solid. 👍
@ -0,0 +40,4 @@
}
}
void generate_test_data(const float components_mul, const float component_mul)

components_mul > vector_mul
component_mul > scalar_mul

`components_mul > vector_mul` `component_mul > scalar_mul`
Jeroen-Bakker marked this conversation as resolved
@ -0,0 +39,4 @@
static uint32_t array_components_len(const shader::Type type);
};
template<typename Layout> static uint32_t element_stride(const shader::Type type)

I think Layout should be LayoutT to avoid thinking it is the layout itself (as in the content of a UBO/SSBO).

I think `Layout` should be `LayoutT` to avoid thinking it is the layout itself (as in the content of a UBO/SSBO).
Jeroen-Bakker marked this conversation as resolved
@ -0,0 +228,4 @@
* bytes.*/
BLI_assert(sizeof(T) == 4);
const T *src = input_data;
for (const int i : IndexRange(array_size)) {

You could test if the array is tightly packed and do only one memcpy.

You could test if the array is tightly packed and do only one `memcpy`.
Jeroen-Bakker marked this conversation as resolved
Jeroen Bakker added 4 commits 2023-02-27 13:14:25 +01:00
Jeroen Bakker added the
Interest
Vulkan
label 2023-02-27 14:00:45 +01:00
Jeroen Bakker added 1 commit 2023-02-27 14:04:51 +01:00
Jeroen Bakker added 1 commit 2023-02-28 08:14:49 +01:00
Bastien Montagne reviewed 2023-03-02 22:24:12 +01:00
Bastien Montagne left a comment
Owner

From my very (very) limited knowledge, code looks good besides note below.

Am getting this failure in tests on my GTX1080 though:

[ RUN      ] GPUVulkanTest.shader_builtin
Error: VK_LAYER_KHRONOS_validation not supported.
Code marked as unreachable has been executed. Please report this as a bug.
Error found at source/blender/gpu/vulkan/vk_shader.cc:993 in bind.
BLI_assert failed: source/blender/gpu/vulkan/vk_shader.cc:993, bind(), at '0'
  This line of code is marked to be unreachable.
From my very (very) limited knowledge, code looks good besides note below. Am getting this failure in tests on my GTX1080 though: ``` [ RUN ] GPUVulkanTest.shader_builtin Error: VK_LAYER_KHRONOS_validation not supported. Code marked as unreachable has been executed. Please report this as a bug. Error found at source/blender/gpu/vulkan/vk_shader.cc:993 in bind. BLI_assert failed: source/blender/gpu/vulkan/vk_shader.cc:993, bind(), at '0' This line of code is marked to be unreachable. ```
@ -70,0 +70,4 @@
VKPushConstants &push_constants = pipeline.push_constants_get();
/* Update push constants based on their storage type.*/
switch (push_constants.layout_get().storage_type_get()) {

I may be missing something, but this whole switch block feels like it does not belong here. It looks way too specific to me.

Would rather see that logic as part of the VKPushConstants class itself, but no idea if this is doable in practice... At the very least would have it in a dedicated util function of VKBackend otherwise?

I may be missing something, but this whole `switch` block feels like it does not belong here. It looks way too specific to me. Would rather see that logic as part of the `VKPushConstants` class itself, but no idea if this is doable in practice... At the very least would have it in a dedicated util function of `VKBackend` otherwise?
Author
Member

Yes you're right, will move this part into a method of VKPushConstants.

Yes you're right, will move this part into a method of VKPushConstants.
Jeroen-Bakker marked this conversation as resolved
Author
Member

From my very (very) limited knowledge, code looks good besides note below.

Am getting this failure in tests on my GTX1080 though:

[ RUN      ] GPUVulkanTest.shader_builtin
Error: VK_LAYER_KHRONOS_validation not supported.
Code marked as unreachable has been executed. Please report this as a bug.
Error found at source/blender/gpu/vulkan/vk_shader.cc:993 in bind.
BLI_assert failed: source/blender/gpu/vulkan/vk_shader.cc:993, bind(), at '0'
  This line of code is marked to be unreachable.

GPUVulkanTest.shader_builtin test case should be removed as it has been replaced by ShaderBuilder. Will do that in main as this isn't part of this PR. #105482

We should also add a nicer message when running GPU debug without having the Vulkan SDK installed. Will add a task for this as well. #105483

Thanks for the input.

> From my very (very) limited knowledge, code looks good besides note below. > > Am getting this failure in tests on my GTX1080 though: > ``` > [ RUN ] GPUVulkanTest.shader_builtin > Error: VK_LAYER_KHRONOS_validation not supported. > Code marked as unreachable has been executed. Please report this as a bug. > Error found at source/blender/gpu/vulkan/vk_shader.cc:993 in bind. > BLI_assert failed: source/blender/gpu/vulkan/vk_shader.cc:993, bind(), at '0' > This line of code is marked to be unreachable. > ``` GPUVulkanTest.shader_builtin test case should be removed as it has been replaced by ShaderBuilder. Will do that in main as this isn't part of this PR. #105482 We should also add a nicer message when running GPU debug without having the Vulkan SDK installed. Will add a task for this as well. #105483 Thanks for the input.
Jeroen Bakker added 2 commits 2023-03-06 11:08:21 +01:00
* constexpr isn't compiling on all platforms (CLang/Apple)
* Visibility is only in a single function.
Bastien Montagne approved these changes 2023-03-06 11:20:27 +01:00
Bastien Montagne left a comment
Owner

Thanks, LGTM now.

Thanks, LGTM now.
Jeroen Bakker added 1 commit 2023-03-06 12:27:16 +01:00
Jeroen Bakker merged commit 61b457d390 into main 2023-03-06 12:29:06 +01:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Code Documentation
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Viewport & EEVEE
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Module
Viewport & EEVEE
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Severity
High
Severity
Low
Severity
Normal
Severity
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Reference: blender/blender#104880
No description provided.