This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/gpu/intern/gpu_vertex_buffer_private.hh

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

112 lines
2.5 KiB
C++
Raw Normal View History

/* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright 2016 by Mike Erwin. All rights reserved. */
/** \file
* \ingroup gpu
*
* GPU vertex buffer
*/
#pragma once
#include "GPU_vertex_buffer.h"
namespace blender::gpu {
/**
* Implementation of Vertex Buffers.
* Base class which is then specialized for each implementation (GL, VK, ...).
*/
class VertBuf {
public:
static size_t memory_usage;
GPUVertFormat format = {};
/** Number of verts we want to draw. */
uint vertex_len = 0;
/** Number of verts data. */
uint vertex_alloc = 0;
/** Status flag. */
GPUVertBufStatus flag = GPU_VERTBUF_INVALID;
/** NULL indicates data in VRAM (unmapped) */
uchar *data = NULL;
protected:
2020-09-19 14:18:05 +10:00
/** Usage hint for GL optimization. */
GPUUsageType usage_ = GPU_USAGE_STATIC;
private:
2020-09-19 14:18:05 +10:00
/** This counter will only avoid freeing the #GPUVertBuf, not the data. */
int handle_refcount_ = 1;
public:
VertBuf();
virtual ~VertBuf();
void init(const GPUVertFormat *format, GPUUsageType usage);
void clear();
2021-02-05 16:23:34 +11:00
/* Data management. */
void allocate(uint vert_len);
void resize(uint vert_len);
void upload();
virtual void bind_as_ssbo(uint binding) = 0;
OpenSubDiv: add support for an OpenGL evaluator This evaluator is used in order to evaluate subdivision at render time, allowing for faster renders of meshes with a subdivision surface modifier placed at the last position in the modifier list. When evaluating the subsurf modifier, we detect whether we can delegate evaluation to the draw code. If so, the subdivision is first evaluated on the GPU using our own custom evaluator (only the coarse data needs to be initially sent to the GPU), then, buffers for the final `MeshBufferCache` are filled on the GPU using a set of compute shaders. However, some buffers are still filled on the CPU side, if doing so on the GPU is impractical (e.g. the line adjacency buffer used for x-ray, whose logic is hardly GPU compatible). This is done at the mesh buffer extraction level so that the result can be readily used in the various OpenGL engines, without having to write custom geometry or tesselation shaders. We use our own subdivision evaluation shaders, instead of OpenSubDiv's vanilla one, in order to control the data layout, and interpolation. For example, we store vertex colors as compressed 16-bit integers, while OpenSubDiv's default evaluator only work for float types. In order to still access the modified geometry on the CPU side, for use in modifiers or transform operators, a dedicated wrapper type is added `MESH_WRAPPER_TYPE_SUBD`. Subdivision will be lazily evaluated via `BKE_object_get_evaluated_mesh` which will create such a wrapper if possible. If the final subdivision surface is not needed on the CPU side, `BKE_object_get_evaluated_mesh_no_subsurf` should be used. Enabling or disabling GPU subdivision can be done through the user preferences (under Viewport -> Subdivision). See patch description for benchmarks. Reviewed By: campbellbarton, jbakker, fclem, brecht, #eevee_viewport Differential Revision: https://developer.blender.org/D12406
2021-12-27 16:34:47 +01:00
virtual void wrap_handle(uint64_t handle) = 0;
VertBuf *duplicate();
/* Size of the data allocated. */
size_t size_alloc_get() const
{
BLI_assert(format.packed);
return vertex_alloc * format.stride;
}
/* Size of the data uploaded to the GPU. */
size_t size_used_get() const
{
BLI_assert(format.packed);
return vertex_len * format.stride;
}
void reference_add()
{
handle_refcount_++;
}
void reference_remove()
{
BLI_assert(handle_refcount_ > 0);
handle_refcount_--;
if (handle_refcount_ == 0) {
delete this;
}
}
OpenSubDiv: add support for an OpenGL evaluator This evaluator is used in order to evaluate subdivision at render time, allowing for faster renders of meshes with a subdivision surface modifier placed at the last position in the modifier list. When evaluating the subsurf modifier, we detect whether we can delegate evaluation to the draw code. If so, the subdivision is first evaluated on the GPU using our own custom evaluator (only the coarse data needs to be initially sent to the GPU), then, buffers for the final `MeshBufferCache` are filled on the GPU using a set of compute shaders. However, some buffers are still filled on the CPU side, if doing so on the GPU is impractical (e.g. the line adjacency buffer used for x-ray, whose logic is hardly GPU compatible). This is done at the mesh buffer extraction level so that the result can be readily used in the various OpenGL engines, without having to write custom geometry or tesselation shaders. We use our own subdivision evaluation shaders, instead of OpenSubDiv's vanilla one, in order to control the data layout, and interpolation. For example, we store vertex colors as compressed 16-bit integers, while OpenSubDiv's default evaluator only work for float types. In order to still access the modified geometry on the CPU side, for use in modifiers or transform operators, a dedicated wrapper type is added `MESH_WRAPPER_TYPE_SUBD`. Subdivision will be lazily evaluated via `BKE_object_get_evaluated_mesh` which will create such a wrapper if possible. If the final subdivision surface is not needed on the CPU side, `BKE_object_get_evaluated_mesh_no_subsurf` should be used. Enabling or disabling GPU subdivision can be done through the user preferences (under Viewport -> Subdivision). See patch description for benchmarks. Reviewed By: campbellbarton, jbakker, fclem, brecht, #eevee_viewport Differential Revision: https://developer.blender.org/D12406
2021-12-27 16:34:47 +01:00
virtual void update_sub(uint start, uint len, const void *data) = 0;
virtual const void *read() const = 0;
virtual void *unmap(const void *mapped_data) const = 0;
protected:
virtual void acquire_data() = 0;
virtual void resize_data() = 0;
virtual void release_data() = 0;
virtual void upload_data() = 0;
virtual void duplicate_data(VertBuf *dst) = 0;
};
2021-02-05 16:23:34 +11:00
/* Syntactic sugar. */
static inline GPUVertBuf *wrap(VertBuf *vert)
{
return reinterpret_cast<GPUVertBuf *>(vert);
}
static inline VertBuf *unwrap(GPUVertBuf *vert)
{
return reinterpret_cast<VertBuf *>(vert);
}
static inline const VertBuf *unwrap(const GPUVertBuf *vert)
{
return reinterpret_cast<const VertBuf *>(vert);
}
2020-09-19 14:18:05 +10:00
} // namespace blender::gpu