Files
test/source/blender/gpu/opengl/gl_storage_buffer.hh
Jeroen Bakker 15d88e544a GPU: Storage buffer allocation alignment
Since the introduction of storage buffers in Blender, the calling
code has been responsible for ensuring the buffer meets allocation
requirements. All backends require the allocation size to be divisible
by 16 bytes. Until now, this was sufficient, but with GPU subdivision
changes, an external library must also adhere to these requirements.

For OpenSubdiv (OSD), some buffers are not 16-byte aligned, leading
to potential misallocation. Currently, this is mitigated by allocating
a few extra bytes, but this approach has the drawback of potentially
reading unintended bytes beyond the source buffer.

This PR adopts a similar approach to vertex buffers: the backend handles
extra byte allocation while ensuring data uploads and downloads function
correctly without requiring those additional bytes.

No changes were needed for Metal, as its allocation size is already
aligned to 256 bytes.

**Alternative solutions considered**:

- Copying the CPU buffer to a larger buffer when needed (performance impact).
- Modifying OSD buffers to allocate extra space (requires changes to an external library).
- Implementing GPU_storagebuf_update_sub.

Ref #135873

Pull Request: https://projects.blender.org/blender/blender/pulls/135716
2025-03-13 15:05:16 +01:00

59 lines
1.3 KiB
C++

/* SPDX-FileCopyrightText: 2022 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup gpu
*/
#pragma once
#include "MEM_guardedalloc.h"
#include "gpu_storage_buffer_private.hh"
namespace blender {
namespace gpu {
/**
* Implementation of Storage Buffers using OpenGL.
*/
class GLStorageBuf : public StorageBuf {
private:
/** Slot to which this UBO is currently bound. -1 if not bound. */
int slot_ = -1;
/** OpenGL Object handle. */
GLuint ssbo_id_ = 0;
/** Usage type. */
GPUUsageType usage_;
/* Read */
GLuint read_ssbo_id_ = 0;
GLsync read_fence_ = 0;
void *persistent_ptr_ = nullptr;
size_t alloc_size_in_bytes_ = 0;
public:
GLStorageBuf(size_t size, GPUUsageType usage, const char *name);
~GLStorageBuf();
void update(const void *data) override;
void bind(int slot) override;
void unbind() override;
void clear(uint32_t clear_value) override;
void copy_sub(VertBuf *src, uint dst_offset, uint src_offset, uint copy_size) override;
void read(void *data) override;
void async_flush_to_host() override;
void sync_as_indirect_buffer() override;
/* Special internal function to bind SSBOs to indirect argument targets. */
void bind_as(GLenum target);
private:
void init();
MEM_CXX_CLASS_ALLOC_FUNCS("GLStorageBuf");
};
} // namespace gpu
} // namespace blender