This repository has been archived on 2023-10-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
blender-archive/source/blender/gpu/metal/mtl_vertex_buffer.hh
Jeroen Bakker f828ecf4ba GPU: Use same read back API as SSBOs
The GPU module has 2 different styles when reading back data from
GPU buffers. The SSBOs used a memcpy to copy the data to a
pre-allocated buffer. IndexBuf/VertBuf gave back a driver/platform
controlled pointer to the memory.

Readback is done for test cases returning mapped pointers is not safe.
For this reason we settled on using the same approach as the SSBO.
Copy the data to a caller pre-allocated buffer.

Reason why this API is currently changed is that the Vulkan API is more
strict on mapping/unmapping buffers that can lead to potential issues
down the road.

Pull Request #104571
2023-02-13 08:34:19 +01:00

76 lines
2.0 KiB
C++

/* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup gpu
*/
#pragma once
#include <Cocoa/Cocoa.h>
#include <Metal/Metal.h>
#include <QuartzCore/QuartzCore.h>
#include "MEM_guardedalloc.h"
#include "GPU_vertex_buffer.h"
#include "gpu_vertex_buffer_private.hh"
#include "mtl_context.hh"
namespace blender::gpu {
class MTLVertBuf : public VertBuf {
friend class gpu::MTLTexture; /* For buffer texture. */
friend class MTLShader; /* For transform feedback. */
friend class MTLBatch;
friend class MTLContext; /* For transform feedback. */
private:
/** Metal buffer allocation. **/
gpu::MTLBuffer *vbo_ = nullptr;
/** Texture used if the buffer is bound as buffer texture. Init on first use. */
struct ::GPUTexture *buffer_texture_ = nullptr;
/** Defines whether the buffer handle is wrapped by this MTLVertBuf, i.e. we do not own it and
* should not free it. */
bool is_wrapper_ = false;
/** Requested allocation size for Metal buffer.
* Differs from raw buffer size as alignment is not included. */
uint64_t alloc_size_ = 0;
/** Whether existing allocation has been submitted for use by the GPU. */
bool contents_in_flight_ = false;
/* Fetch Metal buffer and offset into allocation if necessary.
* Access limited to friend classes. */
id<MTLBuffer> get_metal_buffer()
{
BLI_assert(vbo_ != nullptr);
vbo_->debug_ensure_used();
return vbo_->get_metal_buffer();
}
public:
MTLVertBuf();
~MTLVertBuf();
void bind();
void flag_used();
void update_sub(uint start, uint len, const void *data) override;
void read(void *data) const override;
void wrap_handle(uint64_t handle) override;
protected:
void acquire_data() override;
void resize_data() override;
void release_data() override;
void upload_data() override;
void duplicate_data(VertBuf *dst) override;
void bind_as_ssbo(uint binding) override;
void bind_as_texture(uint binding) override;
MEM_CXX_CLASS_ALLOC_FUNCS("MTLVertBuf");
};
} // namespace blender::gpu