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
9 changed files with 175 additions and 6 deletions
Showing only changes of commit afa2d25db0 - Show all commits

View File

@ -39,8 +39,8 @@ void VKDescriptorPools::add_new_pool()
{
VK_ALLOCATION_CALLBACKS
Vector<VkDescriptorPoolSize> pool_sizes = {
{VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, POOL_SIZE_UNIFORM_BUFFER},
{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, POOL_SIZE_STORAGE_BUFFER},
{VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, POOL_SIZE_STORAGE_IMAGE},
};
VkDescriptorPoolCreateInfo pool_info = {};
pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;

View File

@ -36,9 +36,9 @@ class VKDescriptorPools {
*
* Better defaults should be set later on, when we know more about our resource usage.
*/
static constexpr uint32_t POOL_SIZE_UNIFORM_BUFFER = 1000;
static constexpr uint32_t POOL_SIZE_STORAGE_BUFFER = 1000;
static constexpr uint32_t POOL_SIZE_DESCRIPTOR_SETS = 1000;
static constexpr uint32_t POOL_SIZE_STORAGE_IMAGE = 1000;
VkDevice vk_device_ = VK_NULL_HANDLE;
Vector<VkDescriptorPool> pools_;

View File

@ -8,6 +8,7 @@
#include "vk_descriptor_set.hh"
#include "vk_index_buffer.hh"
#include "vk_storage_buffer.hh"
#include "vk_texture.hh"
#include "vk_vertex_buffer.hh"
#include "BLI_assert.h"
@ -53,6 +54,10 @@ void VKDescriptorSet::bind_as_ssbo(VKIndexBuffer &buffer, int location)
binding.buffer_size = buffer.size_get();
}
void VKDescriptorSet::image_bind(VKTexture &texture, int location)
{
}
VKDescriptorSet::Binding &VKDescriptorSet::ensure_location(int location)
{
for (Binding &binding : bindings_) {

View File

@ -20,6 +20,7 @@ namespace blender::gpu {
class VKStorageBuffer;
class VKVertexBuffer;
class VKIndexBuffer;
class VKTexture;
class VKDescriptorSet : NonCopyable {
struct Binding {
@ -63,6 +64,7 @@ class VKDescriptorSet : NonCopyable {
void bind_as_ssbo(VKVertexBuffer &buffer, int location);
void bind_as_ssbo(VKIndexBuffer &buffer, int location);
void bind(VKStorageBuffer &buffer, int location);
void image_bind(VKTexture &texture, int location);
/**
* Update the descriptor set on the device.

View File

@ -743,7 +743,7 @@ static VkDescriptorType descriptor_type(
{
switch (bind_type) {
case shader::ShaderCreateInfo::Resource::BindType::IMAGE:
return VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
return VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
case shader::ShaderCreateInfo::Resource::BindType::SAMPLER:
return VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
case shader::ShaderCreateInfo::Resource::BindType::STORAGE_BUFFER:

View File

@ -13,6 +13,7 @@ void VKShaderInterface::init(const shader::ShaderCreateInfo &info)
{
using namespace blender::gpu::shader;
uniform_len_ = 0;
ssbo_len_ = 0;
Vector<ShaderCreateInfo::Resource> all_resources;
@ -22,6 +23,8 @@ void VKShaderInterface::init(const shader::ShaderCreateInfo &info)
for (ShaderCreateInfo::Resource &res : all_resources) {
switch (res.bind_type) {
case ShaderCreateInfo::Resource::BindType::IMAGE:
uniform_len_++;
break;
case ShaderCreateInfo::Resource::BindType::SAMPLER:
case ShaderCreateInfo::Resource::BindType::UNIFORM_BUFFER:
// BLI_assert_msg(false, "not implemented yet");
@ -32,14 +35,25 @@ void VKShaderInterface::init(const shader::ShaderCreateInfo &info)
}
}
int32_t input_tot_len = ssbo_len_;
int32_t input_tot_len = ssbo_len_ + uniform_len_;
inputs_ = static_cast<ShaderInput *>(
MEM_calloc_arrayN(input_tot_len, sizeof(ShaderInput), __func__));
ShaderInput *input = inputs_;
name_buffer_ = (char *)MEM_mallocN(info.interface_names_size_, "name_buffer");
uint32_t name_buffer_offset = 0;
/* Images */
/* NOTE: should be extended with uniforms and samplers.*/
for (const ShaderCreateInfo::Resource &res : all_resources) {
if (res.bind_type == ShaderCreateInfo::Resource::BindType::IMAGE) {
copy_input_name(input, res.image.name, name_buffer_, name_buffer_offset);
input->location = input->binding = res.slot;
enabled_ima_mask_ |= (1 << input->binding);
input++;
}
}
/* Storage buffers */
for (const ShaderCreateInfo::Resource &res : all_resources) {
if (res.bind_type == ShaderCreateInfo::Resource::BindType::STORAGE_BUFFER) {
copy_input_name(input, res.storagebuf.name, name_buffer_, name_buffer_offset);

View File

@ -6,6 +6,7 @@
*/
#include "vk_state_manager.hh"
#include "vk_texture.hh"
namespace blender::gpu {
void VKStateManager::apply_state()
@ -32,8 +33,10 @@ void VKStateManager::texture_unbind_all()
{
}
void VKStateManager::image_bind(Texture * /*tex*/, int /*unit*/)
void VKStateManager::image_bind(Texture *tex, int binding)
{
VKTexture *texture = unwrap(tex);
texture->image_bind(binding);
}
void VKStateManager::image_unbind(Texture * /*tex*/)

View File

@ -7,8 +7,17 @@
#include "vk_texture.hh"
#include "vk_context.hh"
#include "vk_shader.hh"
namespace blender::gpu {
VKTexture::~VKTexture()
{
VKContext &context = *VKContext::get();
vmaDestroyImage(context.mem_allocator_get(), vk_image_, allocation_);
}
void VKTexture::generate_mipmap()
{
}
@ -59,9 +68,73 @@ uint VKTexture::gl_bindcode_get() const
return 0;
}
static VkFormat to_vk_format(const eGPUTextureFormat format)
{
switch (format) {
case GPU_RGBA32F:
return VK_FORMAT_R32G32B32A32_SFLOAT;
case GPU_RGBA8UI:
case GPU_RGBA8I:
case GPU_RGBA8:
case GPU_RGBA32UI:
case GPU_RGBA32I:
case GPU_RGBA16UI:
case GPU_RGBA16I:
case GPU_RGBA16F:
case GPU_RGBA16:
case GPU_RG8UI:
case GPU_RG8I:
case GPU_RG8:
case GPU_RG32UI:
case GPU_RG32I:
case GPU_RG32F:
case GPU_RG16UI:
case GPU_RG16I:
case GPU_RG16F:
case GPU_RG16:
case GPU_R8UI:
case GPU_R8I:
case GPU_R8:
case GPU_R32UI:
case GPU_R32I:
case GPU_R32F:
case GPU_R16UI:
case GPU_R16I:
case GPU_R16F:
case GPU_R16:
/*
case GPU_RGB10_A2:
case GPU_R11F_G11F_B10F:
case GPU_DEPTH32F_STENCIL8:
case GPU_DEPTH24_STENCIL8:
case GPU_SRGB8_A8:*/
/* Texture only format */
case GPU_RGB16F:
/* Special formats texture only */
case GPU_SRGB8_A8_DXT1:
case GPU_SRGB8_A8_DXT3:
case GPU_SRGB8_A8_DXT5:
case GPU_RGBA8_DXT1:
case GPU_RGBA8_DXT3:
case GPU_RGBA8_DXT5:
/* Depth Formats */
case GPU_DEPTH_COMPONENT32F:
case GPU_DEPTH_COMPONENT24:
case GPU_DEPTH_COMPONENT16:
default:
BLI_assert_unreachable();
}
return VK_FORMAT_UNDEFINED;
}
bool VKTexture::init_internal()
{
return false;
/* TODO: add some pre-initialization to reduce some work later on.*/
return true;
}
bool VKTexture::init_internal(GPUVertBuf * /*vbo*/)
@ -74,4 +147,55 @@ bool VKTexture::init_internal(const GPUTexture * /*src*/, int /*mip_offset*/, in
return false;
}
bool VKTexture::is_allocated()
{
return vk_image_ != VK_NULL_HANDLE && allocation_ != VK_NULL_HANDLE;
}
bool VKTexture::allocate()
{
BLI_assert(!is_allocated());
VKContext &context = *VKContext::get();
VkImageCreateInfo imgCreateInfo = {VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
imgCreateInfo.imageType = VK_IMAGE_TYPE_1D;
imgCreateInfo.extent.width = width_get();
imgCreateInfo.extent.height = 1;
imgCreateInfo.extent.depth = 1;
imgCreateInfo.mipLevels = 1;
imgCreateInfo.arrayLayers = 1;
imgCreateInfo.format = to_vk_format(format_);
imgCreateInfo.tiling = VK_IMAGE_TILING_LINEAR;
imgCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imgCreateInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT;
imgCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
VmaAllocationCreateInfo allocCreateInfo = {};
allocCreateInfo.usage = VMA_MEMORY_USAGE_AUTO;
allocCreateInfo.flags = static_cast<VmaAllocationCreateFlagBits>(
VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT);
allocCreateInfo.priority = 1.0f;
VkResult result = vmaCreateImage(context.mem_allocator_get(),
&imgCreateInfo,
&allocCreateInfo,
&vk_image_,
&allocation_,
nullptr);
if (result != VK_SUCCESS) {
return false;
}
return true;
}
void VKTexture::image_bind(int location)
{
if (!is_allocated()) {
allocate();
}
VKContext &context = *VKContext::get();
VKShader *shader = static_cast<VKShader *>(context.shader);
shader->pipeline_get().descriptor_set_get().image_bind(*this, location);
}
} // namespace blender::gpu

View File

@ -8,14 +8,19 @@
#pragma once
#include "gpu_texture_private.hh"
#include "vk_context.hh"
namespace blender::gpu {
class VKTexture : public Texture {
VkImage vk_image_ = VK_NULL_HANDLE;
VmaAllocation allocation_ = VK_NULL_HANDLE;
public:
VKTexture(const char *name) : Texture(name)
{
}
virtual ~VKTexture() override;
void generate_mipmap() override;
void copy_to(Texture *tex) override;
@ -34,10 +39,26 @@ class VKTexture : public Texture {
/* TODO(fclem): Legacy. Should be removed at some point. */
uint gl_bindcode_get() const override;
void image_bind(int location);
protected:
bool init_internal() override;
bool init_internal(GPUVertBuf *vbo) override;
bool init_internal(const GPUTexture *src, int mip_offset, int layer_offset) override;
private:
/** Is this texture already allocated on device.*/
bool is_allocated();
/**
* Allocate the texture of the device. Result is `true` when texture is successfully allocated
* on the device.
*/
bool allocate();
};
static inline VKTexture *unwrap(Texture *tex)
{
return static_cast<VKTexture *>(tex);
}
} // namespace blender::gpu