Commit Graph

24 Commits

Author SHA1 Message Date
Hans Goudey
d094812709 Vulkan: Inline more trivial methods
These functions are trivial and shouldn't add the cost of a call.
They appeared in profiles, which they shouldn't since they mostly
just return access to member variables. Inlining them reduces
the backend's overhead when sculpting.

Also reserve a Vector before repeated appending.

Pull Request: https://projects.blender.org/blender/blender/pulls/138349
2025-05-06 14:27:43 +02:00
Josh Belanich
5cb2b04c5c Fix #130914: Vulkan memory leak while resizing view-port
This PR implements dynamic viewport state for the Vulkan gpu backend.
By doing so, it fixes #130914.

The following high-level changes were made:

1. The pipeline pool no longer uses the viewport and scissor
    states to identify graphics pipelines, only the number of viewports
    and the number of scissors. Graphics pipelines are configured with
    dynamic viewport and scissor states upon construction.
2. The desired viewport and scissor configurations for drawing are set
    in the data of the draw nodes in the render graph.
3. The draw nodes use these viewport and scissors settings in
    `build_commands`. If the viewport and scissor settings have changed
    between nodes, then vkCmdSetViewport and vkCmdSetScissor commands
    are sent to the command buffer.
4. Tests are updated to verify that set_viewport and set_scissor commands
   are executed the correct number of times. (Also note that I needed to
   #136987 in order to avoid skipping some Vulkan tests).

See the attached screencast for verification. The number of graphics pipelines
no longer grow when resizing the viewport.

Pull Request: https://projects.blender.org/blender/blender/pulls/137002
2025-04-07 17:26:13 +02:00
Bastien Montagne
318ae49f1e Cleanup: Remove void * handling from MEM_freen<T>.
Followup to 48e26c3afe, and discussions in !134771 about keeping
'C-style' and 'C++ template type-safe style' implementations of our
guardedalloc separated. And it makes `MEM_freeN<T>` code simpler.

Also skip type-checking in `MEM_freeN<T>` only with MSVC, as clang-cl on
windows-arm64 does work fine with DNA structs using
`DNA_DEFINE_CXX_METHODS`.

Pull Request: https://projects.blender.org/blender/blender/pulls/134861
2025-02-20 16:42:22 +01:00
Jeroen Bakker
27b9173081 Cleanup: Code-style
Remove commented out parameter-name from header.
2025-02-03 08:06:10 +01:00
Jeroen Bakker
ff804882bd Refactor: Vulkan: Store large data in separate vectors
VKRenderGraphNode is 892 bytes and most of the bytes are used for
specific nodes. By storing large structs in separate vectors we can
reduce the needed memory and improve cache pre-fetching.

With this change the VKRenderGraphNode is reduced to 64 bytes.

On a (50 frames shader_balls.blend) the end user performance is improved by
2%.

| **Platform**     | **Before** | **After** |
| ---------------- | ---------- | --------- |
| AMD W7700        | 1409 ms    | 1383 ms   |
| NVIDIA RTX 6000  | 1443 ms    | 1428 ms   |

Pull Request: https://projects.blender.org/blender/blender/pulls/133317
2025-01-21 14:49:23 +01:00
Jeroen Bakker
c480b7ffd3 Vulkan: Incorrect image aspect when transferring depth textures
When copying from/to depth stencil images the aspect of the image was
set incorrectly. It pointed to only one aspect of the full image when
building resource links, which could lead to incorrect decisions in the
driver.

Found when researching #131269

Pull Request: https://projects.blender.org/blender/blender/pulls/131298
2024-12-03 12:36:39 +01:00
Campbell Barton
b9f055459a Cleanup: ensure trailing space around comment blocks 2024-11-27 19:01:00 +11:00
Jeroen Bakker
c2695e2dcc Vulkan: Add support for legacy platforms
Dynamic rendering is a Vulkan 1.3 feature. Most platforms have support
for them, but there are several legacy platforms that don't support dynamic
rendering or have driver bugs that don't allow us to use it.

This change will make dynamic rendering optional allowing legacy
platforms to use Vulkan.

**Limitations**

`GPU_LOADACTION_CLEAR` is implemented as clear attachments.
Render passes do support load clear, but adding support to it would
add complexity as it required multiple pipeline variations to support
suspend/resume rendering. It isn't clear when which variation should
be used what lead to compiling to many pipelines and branches in the
codebase. Using clear attachments doesn't require the complexity
for what is expected to be only used by platforms not supported by
the GPU vendors.

Subpass inputs and dual source blending are not supported as
Subpass inputs can alter the exact binding location of attachments.
Fixing this would add code complexity that is not used.

Ref: #129063

**Current state**

![image](/attachments/9ce012e5-2d88-4775-a636-2b74de812826)

Pull Request: https://projects.blender.org/blender/blender/pulls/129062
2024-11-19 16:30:31 +01:00
Jeroen Bakker
4d8581c7ee Vulkan: Add node to update buffers
This change adds the option to update a buffer via the render
graph via `vkCmdUpdateBuffer`. This is only enabled for
uniform buffers as they are small and aligned/sized correctly.

Pull Request: https://projects.blender.org/blender/blender/pulls/128416
2024-10-01 14:22:56 +02:00
Jeroen Bakker
9dfb49d16b Vulkan: Fix incorrect access flag
Detected by latest vulkan validation layers.

Pull Request: https://projects.blender.org/blender/blender/pulls/128417
2024-10-01 14:06:41 +02:00
Jeroen Bakker
7a7ae5defe Vulkan: Occlusion Queries
Implements occlusion queries using VkQueryPools.

Pull Request: https://projects.blender.org/blender/blender/pulls/123883
2024-08-20 11:27:33 +02:00
Jeroen Bakker
180a257eef Cleanup: Vulkan: Use stamp in stead of version
Pull Request: https://projects.blender.org/blender/blender/pulls/124339
2024-07-08 12:56:21 +02:00
Jeroen Bakker
da4746fe96 Vulkan: Fix copy depth images with stencil
When copying the depth aspect of a depth+stencil image the incorrect
barriers where created. The barrier needs to have the full aspect, even
when we only copy a single aspect from the image.

Pull Request: https://projects.blender.org/blender/blender/pulls/123890
2024-06-28 13:13:33 +02:00
Jeroen Bakker
22d352dacc Vulkan: Render graph drawing
This PR adds drawing support to the render graph. It adds support for
draw, indirect draw, indexed draw and indexed indirect draw.

Draw commands can only be executed within a rendering scope. Data
transfer commands and dispatch commands cannot be executed within a
rendering scope. Blender can still send in commands in any order and
the render graph needs to find out the best order to minimize context
switches (rendering/begin/end). This is the responsibility of the
scheduler.

The scheduler will push data transfer and dispatch commands outside the
rendering scope:
- data transfer and dispatch commands at the beginning are done before
  the rendering begin.
- data transfer and dispatch commands at the end are done after the
  rendering end.
- data transfer and dispatches in between draw commands will be pushed
  to the beginning if they are not yet being used.
- for all other data transfer and dispatch commands the rendering is
  suspenderd and will be continued afterwards.

Within a rendering context it is not allowed to perform synchronization
commands. Any synchronization commands inside a rendering scope will be
performed before the rendering scope begins. Nodes are now organized
in groups to simplify the code around this area.

Pull Request: https://projects.blender.org/blender/blender/pulls/123168
2024-06-13 09:37:17 +02:00
Jeroen Bakker
049d84d36d Vulkan: Texture view clearing
EEVEE-Next requires clearing texture views. This PR adds that.

Pull Request: https://projects.blender.org/blender/blender/pulls/122650
2024-06-03 13:47:15 +02:00
Jeroen Bakker
7fb1c4c573 Vulkan: Incorrect memory barrier when presenting
When synchonizing the image for presentation the incorrect access
mask was used. This PR changes the access mask from data transfer
write to memory write. The data transfer write is not allowed to
change the image layout.

Pull Request: https://projects.blender.org/blender/blender/pulls/122138
2024-05-23 10:04:32 +02:00
Jeroen Bakker
9b531609bd Vulkan: Fix push constants tests
This fixes several issues with push constants.

Push constants test were failing due to setting incorrect parameters.
The pipeline stage was used as if it was a shader stage.

When using push constants fallback the uniform was loaded to the
descriptor set after the descriptor set was checked.

Suppress updating push constants of non render graph, when render
graph is active.

Pull Request: https://projects.blender.org/blender/blender/pulls/121772
2024-05-14 10:44:10 +02:00
Jeroen Bakker
f625c4e63d Vulkan: Fix compute test cases
Compute tests were failing due to recent changes.
* Incorrect image layout was used for writable image bindings.
* Incorrect pipeline stage was used for indirect command buffers.

Pull Request: https://projects.blender.org/blender/blender/pulls/121744
2024-05-14 08:22:18 +02:00
Jeroen Bakker
5c369768d7 Vulkan: Revert incorrect fix for begin rendering
Reverting a quick fix which didn't solve the root cause. The root
cause was fixed by #121723.

Pull Request: https://projects.blender.org/blender/blender/pulls/121726
2024-05-13 13:28:05 +02:00
Jeroen Bakker
b998697d4f Vulkan: Fix out of bound access in begin rendering
There is an implementation flaw in the render graph where local pointers
cannot be updated, but the data it refers to can be reallocated to
another location.

The cause of this is that the nodes use an union, which can only contain
simple constructed structs (eg memcpy). this union is stored in a vector
and can relocate the union. Any local pointers can (and will) become
invalid.

This PR is a quick fix by updating the pointers just before sending
them to the command buffer. In future a better fox needs to be done
as part of #121649.

Pull Request: https://projects.blender.org/blender/blender/pulls/121723
2024-05-13 09:35:36 +02:00
Jeroen Bakker
40f2df1a81 Vulkan: Render graph clear attachments
Adds support for clear attachments to the render graph.
Clearing attachments require that the command buffer is
being set for rendering (vkCmdBeginRendering).

**What works**

- Clear attachments are working with dynamic rendering and the render graph
- `GPUVulkanTest.framebuffer_clear_color_single_attachment`
- `GPUVulkanTest.framebuffer_clear_color_multiple_attachments`
- `GPUVulkanTest.framebuffer_clear_multiple_color_multiple_attachments`
- `GPUVulkanTest.framebuffer_scissor_test`
- `GPUVulkanTest.framebuffer_clear_depth`

**What still needs to be addressed**

When a command buffer is rendering it cannot do any pipeline
barriers. For clearing attachments this isn't needed, but when
we address drawing we might need to register drawing resource
dependencies to the dependency list of the begin rendering node.
This will be solved when drawing will be implemented. [#121648]

The begin rendering node is large as it has to store data for
framebuffers with 8 color attachments as well. This might
become an overhead and we could solve this by splicing the
data of larger nodes into 2 lists. This will be addressed
after we are able to draw a screen so we can collect data
on this topic. [#121649]

Pull Request: https://projects.blender.org/blender/blender/pulls/121073
2024-05-10 15:39:56 +02:00
Jeroen Bakker
c8ccf77564 Vulkan: Render graph dispatch indirect
Add dispatch indirect node. Also refactored the dispatch (direct) node
so more logic could be reused. The context only stores a `VKResourceAccessInfo`
struct which is reused by both the dispatch and dispatch indirect node.

Pull Request: https://projects.blender.org/blender/blender/pulls/120993
2024-04-24 21:28:45 +02:00
Jeroen Bakker
be75f1ac2b Vulkan: Render graph textures
This PR implements render graph for VKTexture. During the
implementation some tweaks to the render graph was done
to support depth and stencil textures.

The render graph will record the image aspect being used
for each node. This will then be used to generate barriers
for the correct aspect.

Also fixes an issue that uploading of array textures didn't
allocate a large enough staging buffer.

Pull Request: https://projects.blender.org/blender/blender/pulls/120821
2024-04-19 14:55:39 +02:00
Jeroen Bakker
adab06bc67 Vulkan: Render graph core
**Design Task**: blender/blender#118330

This PR adds the core of the render graph. The render graph isn't used.
Current implementation of the Vulkan Backend is slow by design. We
focused on stability, before performance. With the new introduced render
graph the focus will shift to performance and keep the stability at where
it is.

Some highlights:
- Every context will get its own render graph. (`VKRenderGraph`).
- Resources (and resource state tracking) is device specific (`VKResourceStateTracker`).
- No node reordering / sub graph execution has been implemented. Currently
  All nodes in the graph is executed in the order they were added. (`VKScheduler`).
- The links inside the graph describe the resources the nodes read from (input links)
  or writes to (output links)
- When resources are written to a resource stamp is incremented allowing keeping
  track of which nodes needs which stamp of a resource.
- At each link the access information (how does the node accesses the resource)
  and image layout (for image resources) are stored. This allows the render graph
  to find out how a resource was used in the past and will be used in the future.
  That is important to construct pipeline barriers that don't stall the whole GPU.

# Defined nodes

This implementation has nodes for:
- Blit image
- Clear color image
- Copy buffers to buffers
- Copy buffers to images
- Copy images to images
- Copy images to buffers
- Dispatch compute shader
- Fill buffers
- Synchronization

Each node has a node info, create info and data struct. The create info
contains all data to construct the node, including the links of the graph.
The data struct only contains the data stored inside the node. The node info
contains the node specific implementation.

> NOTE: Other nodes will be added after this PR lands to main.

# Resources

Before a render graph can be used, the resources should be registered
to `VKResourceStateTracker`. In the final implementation this will be owned by
the `VKDevice`. Registration of resources can be done by calling
`VKResources.add_buffer` or `VKResources.add_image`.

# Render graph

Nodes can be added to the render graph. When adding a node its read/
write dependencies are extracted and converted into links (`VKNodeInfo.
build_links`).
When the caller wants to have a resource up to date the functions
`VKRenderGraph.submit_for_read` or `VKRenderGraph.submit_for_present`
can be called.

These functions will select and order the nodes that are needed
and convert them to `vkCmd*` commands. These commands include pipeline
barrier and image layout transitions.

The `vkCmd` are recorded into a command buffer which is sent to the
device queue.

## Walking the graph

Walking the render graph isn't implemented yet. The idea is to have a
`Map<ResourceWithStamp, Vector<NodeHandle>> consumers` and
`Map<ResourceWithStamp, NodeHandle> producers`. These attributes can
be stored in the render graph and created when building the links, or
can be created inside the VKScheduler as a variable. The exact detail
which one would be better is unclear as there aren't any users yet. At
the moment the scheduler would need them we need to figure out the best
way to store and retrieve the consumers/producers.

# Unit tests

The render graph can be tested by enabling `WITH_GTEST` and use
`vk_render_graph` as a filter.

```
bin/tests/blender_test --gtest_filter="vk_render_graph*"
```

Pull Request: https://projects.blender.org/blender/blender/pulls/120427
2024-04-19 10:46:50 +02:00