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_index_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

81 lines
2.4 KiB
C++

/* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup gpu
*/
#pragma once
#include "MEM_guardedalloc.h"
#include "gpu_index_buffer_private.hh"
#include "mtl_context.hh"
#include <Cocoa/Cocoa.h>
#include <Metal/Metal.h>
#include <QuartzCore/QuartzCore.h>
namespace blender::gpu {
class MTLIndexBuf : public IndexBuf {
friend class MTLBatch;
friend class MTLDrawList;
private:
/* Metal buffer resource. */
gpu::MTLBuffer *ibo_ = nullptr;
uint64_t alloc_size_ = 0;
#ifndef NDEBUG
/* Flags whether point index buffer has been compacted
* to remove false restart indices. */
bool point_restarts_stripped_ = false;
#endif
/* Optimized index buffers.
* NOTE(Metal): This optimization encodes a new index buffer following
* #TriangleList topology. Parsing of Index buffers is more optimal
* when not using restart-compatible primitive topology types. */
GPUPrimType optimized_primitive_type_;
gpu::MTLBuffer *optimized_ibo_ = nullptr;
uint32_t emulated_v_count = 0;
void free_optimized_buffer();
/* Flags whether an index buffer can be optimized.
* For index buffers which are partially modified
* on the host, or by the GPU, optimization cannot be performed. */
bool can_optimize_ = true;
public:
~MTLIndexBuf();
void bind_as_ssbo(uint32_t binding) override;
void read(uint32_t *data) const override;
void upload_data() override;
void update_sub(uint32_t start, uint32_t len, const void *data) override;
/* #get_index_buffer can conditionally return an optimized index buffer of a
* differing format, if it is concluded that optimization is preferred
* for the given inputs.
* Index buffer optimization is used to replace restart-compatible
* primitive types with non-restart-compatible ones such as #TriangleList and
* #LineList. This improves GPU execution for these types significantly, while
* only incurring a small performance penalty.
*
* This is also used to emulate unsupported topology types
* such as triangle fan. */
id<MTLBuffer> get_index_buffer(GPUPrimType &in_out_primitive_type, uint &in_out_v_count);
void flag_can_optimize(bool can_optimize);
static MTLIndexType gpu_index_type_to_metal(GPUIndexBufType type)
{
return (type == GPU_INDEX_U16) ? MTLIndexTypeUInt16 : MTLIndexTypeUInt32;
}
private:
void strip_restart_indices() override;
MEM_CXX_CLASS_ALLOC_FUNCS("MTLIndexBuf")
};
} // namespace blender::gpu