Renderer: Add support for storage buffers

This commit is contained in:
SirLynix
2022-06-17 20:15:16 +02:00
parent 0978feafbc
commit 093d9d344e
16 changed files with 160 additions and 18 deletions

View File

@@ -41,17 +41,26 @@ namespace Nz
private:
struct DescriptorPool;
struct StorageBufferDescriptor;
struct TextureDescriptor;
struct UniformBufferDescriptor;
DescriptorPool& AllocatePool();
ShaderBindingPtr AllocateFromPool(std::size_t poolIndex, UInt32 setIndex);
template<typename F> void ForEachDescriptor(std::size_t poolIndex, std::size_t bindingIndex, F&& functor);
StorageBufferDescriptor& GetStorageBufferDescriptor(std::size_t poolIndex, std::size_t bindingIndex, std::size_t descriptorIndex);
TextureDescriptor& GetTextureDescriptor(std::size_t poolIndex, std::size_t bindingIndex, std::size_t descriptorIndex);
UniformBufferDescriptor& GetUniformBufferDescriptor(std::size_t poolIndex, std::size_t bindingIndex, std::size_t descriptorIndex);
void Release(ShaderBinding& binding);
inline void TryToShrink();
struct StorageBufferDescriptor
{
GLuint buffer;
GLintptr offset;
GLsizeiptr size;
};
struct TextureDescriptor
{
GLuint texture;
@@ -66,7 +75,7 @@ namespace Nz
GLsizeiptr size;
};
using Descriptor = std::variant<std::monostate, TextureDescriptor, UniformBufferDescriptor>;
using Descriptor = std::variant<std::monostate, StorageBufferDescriptor, TextureDescriptor, UniformBufferDescriptor>;
struct DescriptorPool
{

View File

@@ -265,6 +265,7 @@ namespace Nz
case GL::BufferTarget::ElementArray: return GL_ELEMENT_ARRAY_BUFFER;
case GL::BufferTarget::PixelPack: return GL_PIXEL_PACK_BUFFER;
case GL::BufferTarget::PixelUnpack: return GL_PIXEL_UNPACK_BUFFER;
case GL::BufferTarget::Storage: return GL_SHADER_STORAGE_BUFFER;
case GL::BufferTarget::TransformFeedback: return GL_TRANSFORM_FEEDBACK_BUFFER;
case GL::BufferTarget::Uniform: return GL_UNIFORM_BUFFER;
}

View File

@@ -36,6 +36,7 @@ namespace Nz::GL
ElementArray,
PixelPack,
PixelUnpack,
Storage,
TransformFeedback,
Uniform,
@@ -52,6 +53,7 @@ namespace Nz::GL
{
DepthClamp,
SpirV,
StorageBuffers,
TextureCompressionS3tc,
TextureFilterAnisotropic,
@@ -120,6 +122,7 @@ namespace Nz::GL
void BindFramebuffer(FramebufferTarget target, GLuint fbo) const;
void BindProgram(GLuint program) const;
void BindSampler(UInt32 textureUnit, GLuint sampler) const;
void BindStorageBuffer(UInt32 storageUnit, GLuint buffer, GLintptr offset, GLsizeiptr size) const;
void BindTexture(TextureTarget target, GLuint texture) const;
void BindTexture(UInt32 textureUnit, TextureTarget target, GLuint texture) const;
void BindUniformBuffer(UInt32 uboUnit, GLuint buffer, GLintptr offset, GLsizeiptr size) const;
@@ -212,22 +215,23 @@ namespace Nz::GL
GLsizei width, height;
};
struct TextureUnit
{
GLuint sampler = 0;
std::array<GLuint, UnderlyingCast(TextureTarget::Max) + 1> textureTargets = { 0 };
};
struct UniformBufferUnit
struct BufferBinding
{
GLuint buffer = 0;
GLintptr offset = 0;
GLsizeiptr size = 0;
};
struct TextureUnit
{
GLuint sampler = 0;
std::array<GLuint, UnderlyingCast(TextureTarget::Max) + 1> textureTargets = { 0 };
};
std::array<GLuint, UnderlyingCast(BufferTarget::Max) + 1> bufferTargets = { 0 };
std::vector<TextureUnit> textureUnits;
std::vector<UniformBufferUnit> uboUnits;
std::vector<BufferBinding> storageUnits;
std::vector<BufferBinding> uboUnits;
Box scissorBox;
Box viewport;
GLuint boundProgram = 0;

View File

@@ -136,6 +136,7 @@ namespace Nz
enum class ShaderBindingType
{
StorageBuffer,
Texture,
UniformBuffer,

View File

@@ -18,11 +18,14 @@ namespace Nz
bool anisotropicFiltering = false;
bool depthClamping = false;
bool nonSolidFaceFilling = false;
bool storageBuffers = false;
};
struct RenderDeviceLimits
{
UInt64 minUniformBufferOffsetAlignment;
UInt64 maxStorageBufferSize;
UInt64 maxUniformBufferSize;
};
struct RenderDeviceInfo

View File

@@ -40,6 +40,13 @@ namespace Nz
ShaderBinding& operator=(const ShaderBinding&) = delete;
ShaderBinding& operator=(ShaderBinding&&) = delete;
struct StorageBufferBinding
{
RenderBuffer* buffer;
UInt64 offset;
UInt64 range;
};
struct TextureBinding
{
const Texture* texture;
@@ -56,7 +63,7 @@ namespace Nz
struct Binding
{
std::size_t bindingIndex;
std::variant<TextureBinding, UniformBufferBinding> content;
std::variant<StorageBufferBinding, TextureBinding, UniformBufferBinding> content;
};
protected:

View File

@@ -60,6 +60,7 @@ namespace Nz
{
Index,
Vertex,
Storage,
Uniform,
Max = Uniform

View File

@@ -98,6 +98,7 @@ namespace Nz
switch (bufferType)
{
case BufferType::Index: return VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
case BufferType::Storage: return VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
case BufferType::Vertex: return VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
case BufferType::Uniform: return VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
}
@@ -381,6 +382,7 @@ namespace Nz
{
switch (bindingType)
{
case ShaderBindingType::StorageBuffer: return VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
case ShaderBindingType::Texture: return VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
case ShaderBindingType::UniformBuffer: return VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
}