Files
test2/source/blender/gpu/vulkan/vk_bindable_resource.hh
Jeroen Bakker d84a64900f Vulkan: Device Context Resource Management
The current Vulkan resource management has some issues as context that
are not active can still use resources that are freed via another
context.

When this happens incorrect data can be read on the GPU and even crash
Blender. When trying to bind something that now contains other memory
pointers.

This change introduces that contexts are tracked via the device.
Context will be registered/unregistered with the device instance.
Unbinding of resources must pass the device and the device will check
all registered contexts. Binding of resources will happen via the active
context only.

On user perspective this now allowes:
- Opening/switching files
- Switching workspaces
- Switching render engines

Pull Request: https://projects.blender.org/blender/blender/pulls/108968
2023-06-15 08:14:37 +02:00

93 lines
1.8 KiB
C++

/* SPDX-FileCopyrightText: 2023 Blender Foundation
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup gpu
*/
#pragma once
#include "BLI_array.hh"
#include "gpu_shader_create_info.hh"
namespace blender::gpu {
/**
* Super class for resources that can be bound to a shader.
*/
class VKBindableResource {
protected:
virtual ~VKBindableResource();
public:
/**
* Bind the resource to the shader.
*/
virtual void bind(int binding, shader::ShaderCreateInfo::Resource::BindType bind_type) = 0;
protected:
void unbind_from_active_context();
private:
void unbind_from_all_contexts();
};
/**
* Blender binds resources at context level (VKStateManager). The bindings are organized in
* namespaces.
*/
template<shader::ShaderCreateInfo::Resource::BindType BindType, int MaxBindings = 16>
class VKBindSpace {
Array<VKBindableResource *> bindings_ = Array<VKBindableResource *>(MaxBindings);
public:
VKBindSpace()
{
bindings_.fill(nullptr);
}
/**
* Register a binding to this namespace.
*/
void bind(int binding, VKBindableResource &resource)
{
bindings_[binding] = &resource;
}
/**
* Apply registered bindings to the active shader.
*/
void apply_bindings()
{
for (int binding : IndexRange(MaxBindings)) {
if (bindings_[binding] != nullptr) {
bindings_[binding]->bind(binding, BindType);
}
}
}
/**
* Unregister the given resource from this namespace.
*/
void unbind(VKBindableResource &resource)
{
for (int binding : IndexRange(MaxBindings)) {
if (bindings_[binding] == &resource) {
bindings_[binding] = nullptr;
}
}
}
/**
* Remove all bindings from this namespace.
*/
void unbind_all()
{
bindings_.fill(nullptr);
}
};
} // namespace blender::gpu