2023-08-16 00:20:26 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2016 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2018-01-09 14:09:14 +01:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup draw
|
2018-01-09 14:09:14 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* DRW Instance Data Manager
|
2020-09-07 15:57:12 +10:00
|
|
|
* This is a special memory manager that keeps memory blocks ready to send as VBO data in one
|
2024-03-26 03:06:25 +01:00
|
|
|
* continuous allocation. This way we avoid feeding #gpu::Batch each instance data one by one and
|
2024-12-09 09:14:44 +11:00
|
|
|
* unnecessary `memcpy`. Since we lose which memory block was used each #DRWShadingGroup we need to
|
2019-05-01 10:35:46 +10:00
|
|
|
* redistribute them in the same order/size to avoid to realloc each frame. This is why
|
|
|
|
|
* #DRWInstanceDatas are sorted in a list for each different data size.
|
2019-03-19 15:17:46 +11:00
|
|
|
*/
|
2018-01-09 14:09:14 +01:00
|
|
|
|
2024-03-22 23:51:35 -04:00
|
|
|
#include "draw_instance_data.hh"
|
Materials: add custom object properties as uniform attributes.
This patch allows the user to type a property name into the
Attribute node, which will then output the value of the property
for each individual object, allowing to e.g. customize shaders
by object without duplicating the shader.
In order to make supporting this easier for Eevee, it is necessary
to explicitly choose whether the attribute is varying or uniform
via a dropdown option of the Attribute node. The dropdown also
allows choosing whether instancing should be taken into account.
The Cycles design treats all attributes as one common namespace,
so the Blender interface converts the enum to a name prefix that
can't be entered using keyboard.
In Eevee, the attributes are provided to the shader via a UBO indexed
with resource_id, similar to the existing Object Info data. Unlike it,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.
This is done using a hash table with the attribute set as the key,
as it is expected that technically different but similar materials
may use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't contain any data don't have to be allocated.
The back-end Cycles code is already refactored and committed by Brecht.
Differential Revision: https://developer.blender.org/D2057
2020-08-05 19:14:40 +03:00
|
|
|
|
2024-01-05 11:16:57 -05:00
|
|
|
#include "DRW_engine.hh"
|
|
|
|
|
#include "DRW_render.hh" /* For DRW_shgroup_get_instance_count() */
|
2018-01-09 14:09:14 +01:00
|
|
|
|
2025-01-22 19:37:38 +01:00
|
|
|
#include "BLI_listbase.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_mempool.h"
|
2025-01-22 19:37:38 +01:00
|
|
|
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "MEM_guardedalloc.h"
|
2018-01-09 14:09:14 +01:00
|
|
|
|
|
|
|
|
struct DRWInstanceData {
|
2023-06-03 08:36:28 +10:00
|
|
|
DRWInstanceData *next;
|
2018-01-09 14:09:14 +01:00
|
|
|
bool used; /* If this data is used or not. */
|
|
|
|
|
size_t data_size; /* Size of one instance data. */
|
2018-05-30 12:19:20 +02:00
|
|
|
BLI_mempool *mempool;
|
2018-01-09 14:09:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct DRWInstanceDataList {
|
2023-06-03 08:36:28 +10:00
|
|
|
DRWInstanceDataList *next, *prev;
|
2018-01-09 14:09:14 +01:00
|
|
|
/* Linked lists for all possible data pool size */
|
|
|
|
|
DRWInstanceData *idata_head[MAX_INSTANCE_DATA_SIZE];
|
|
|
|
|
DRWInstanceData *idata_tail[MAX_INSTANCE_DATA_SIZE];
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-27 14:16:58 +02:00
|
|
|
struct DRWTempBufferHandle {
|
2024-03-24 16:38:30 +01:00
|
|
|
blender::gpu::VertBuf *buf;
|
2019-05-13 17:56:20 +02:00
|
|
|
/** Format pointer for reuse. */
|
|
|
|
|
GPUVertFormat *format;
|
|
|
|
|
/** Touched vertex length for resize. */
|
2019-05-31 01:45:41 +02:00
|
|
|
int *vert_len;
|
2023-07-27 14:16:58 +02:00
|
|
|
};
|
2019-05-13 17:56:20 +02:00
|
|
|
|
2023-07-27 14:16:58 +02:00
|
|
|
struct DRWTempInstancingHandle {
|
2020-08-10 01:43:50 +02:00
|
|
|
/** Copy of geom but with the per-instance attributes. */
|
2024-03-26 03:06:25 +01:00
|
|
|
blender::gpu::Batch *batch;
|
2020-08-10 01:43:50 +02:00
|
|
|
/** Batch containing instancing attributes. */
|
2024-03-26 03:06:25 +01:00
|
|
|
blender::gpu::Batch *instancer;
|
2022-01-28 13:59:08 +11:00
|
|
|
/** Call-buffer to be used instead of instancer. */
|
2024-03-24 16:38:30 +01:00
|
|
|
blender::gpu::VertBuf *buf;
|
2020-08-10 01:43:50 +02:00
|
|
|
/** Original non-instanced batch pointer. */
|
2024-03-26 03:06:25 +01:00
|
|
|
blender::gpu::Batch *geom;
|
2023-07-27 14:16:58 +02:00
|
|
|
};
|
2018-02-20 01:55:19 +01:00
|
|
|
|
2023-07-27 14:16:58 +02:00
|
|
|
static ListBase g_idatalists = {nullptr, nullptr};
|
2018-02-14 18:16:52 +01:00
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
2018-01-09 14:09:14 +01:00
|
|
|
/** \name Instance Data (DRWInstanceData)
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2019-01-29 07:46:25 +11:00
|
|
|
static DRWInstanceData *drw_instance_data_create(DRWInstanceDataList *idatalist, uint attr_size)
|
2018-01-09 14:09:14 +01:00
|
|
|
{
|
2023-07-27 14:16:58 +02:00
|
|
|
DRWInstanceData *idata = static_cast<DRWInstanceData *>(
|
|
|
|
|
MEM_callocN(sizeof(DRWInstanceData), "DRWInstanceData"));
|
|
|
|
|
idata->next = nullptr;
|
2018-01-09 14:09:14 +01:00
|
|
|
idata->used = true;
|
2019-01-29 07:46:25 +11:00
|
|
|
idata->data_size = attr_size;
|
2018-05-30 12:19:20 +02:00
|
|
|
idata->mempool = BLI_mempool_create(sizeof(float) * idata->data_size, 0, 16, 0);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-01-29 07:46:25 +11:00
|
|
|
BLI_assert(attr_size > 0);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-01-09 14:09:14 +01:00
|
|
|
/* Push to linked list. */
|
2023-07-27 14:16:58 +02:00
|
|
|
if (idatalist->idata_head[attr_size - 1] == nullptr) {
|
2019-01-29 07:46:25 +11:00
|
|
|
idatalist->idata_head[attr_size - 1] = idata;
|
2018-01-09 14:09:14 +01:00
|
|
|
}
|
|
|
|
|
else {
|
2019-01-29 07:46:25 +11:00
|
|
|
idatalist->idata_tail[attr_size - 1]->next = idata;
|
2018-01-09 14:09:14 +01:00
|
|
|
}
|
2019-01-29 07:46:25 +11:00
|
|
|
idatalist->idata_tail[attr_size - 1] = idata;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-01-09 14:09:14 +01:00
|
|
|
return idata;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void DRW_instance_data_free(DRWInstanceData *idata)
|
|
|
|
|
{
|
2018-05-30 12:19:20 +02:00
|
|
|
BLI_mempool_destroy(idata->mempool);
|
2018-01-09 14:09:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *DRW_instance_data_next(DRWInstanceData *idata)
|
|
|
|
|
{
|
2018-05-30 12:19:20 +02:00
|
|
|
return BLI_mempool_alloc(idata->mempool);
|
2018-01-09 14:09:14 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-29 07:46:25 +11:00
|
|
|
DRWInstanceData *DRW_instance_data_request(DRWInstanceDataList *idatalist, uint attr_size)
|
2018-01-09 14:09:14 +01:00
|
|
|
{
|
2019-01-29 07:46:25 +11:00
|
|
|
BLI_assert(attr_size > 0 && attr_size <= MAX_INSTANCE_DATA_SIZE);
|
2018-01-09 14:09:14 +01:00
|
|
|
|
2019-01-29 07:46:25 +11:00
|
|
|
DRWInstanceData *idata = idatalist->idata_head[attr_size - 1];
|
2018-01-09 14:09:14 +01:00
|
|
|
|
|
|
|
|
/* Search for an unused data chunk. */
|
|
|
|
|
for (; idata; idata = idata->next) {
|
|
|
|
|
if (idata->used == false) {
|
|
|
|
|
idata->used = true;
|
|
|
|
|
return idata;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-29 07:46:25 +11:00
|
|
|
return drw_instance_data_create(idatalist, attr_size);
|
2018-01-09 14:09:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
/** \name Instance Data List (DRWInstanceDataList)
|
|
|
|
|
* \{ */
|
|
|
|
|
|
2023-07-28 09:38:07 +10:00
|
|
|
DRWInstanceDataList *DRW_instance_data_list_create()
|
2018-01-09 14:09:14 +01:00
|
|
|
{
|
2023-07-27 14:16:58 +02:00
|
|
|
DRWInstanceDataList *idatalist = static_cast<DRWInstanceDataList *>(
|
|
|
|
|
MEM_callocN(sizeof(DRWInstanceDataList), "DRWInstanceDataList"));
|
2019-05-13 17:56:20 +02:00
|
|
|
|
2018-02-20 01:55:19 +01:00
|
|
|
BLI_addtail(&g_idatalists, idatalist);
|
2018-02-14 18:16:52 +01:00
|
|
|
|
|
|
|
|
return idatalist;
|
2018-01-09 14:09:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DRW_instance_data_list_free(DRWInstanceDataList *idatalist)
|
|
|
|
|
{
|
|
|
|
|
DRWInstanceData *idata, *next_idata;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-09-08 00:12:26 +10:00
|
|
|
for (int i = 0; i < MAX_INSTANCE_DATA_SIZE; i++) {
|
2018-01-09 14:09:14 +01:00
|
|
|
for (idata = idatalist->idata_head[i]; idata; idata = next_idata) {
|
|
|
|
|
next_idata = idata->next;
|
|
|
|
|
DRW_instance_data_free(idata);
|
|
|
|
|
MEM_freeN(idata);
|
|
|
|
|
}
|
2023-07-27 14:16:58 +02:00
|
|
|
idatalist->idata_head[i] = nullptr;
|
|
|
|
|
idatalist->idata_tail[i] = nullptr;
|
2018-01-09 14:09:14 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-02-20 01:55:19 +01:00
|
|
|
BLI_remlink(&g_idatalists, idatalist);
|
2021-10-05 09:36:11 +02:00
|
|
|
|
|
|
|
|
MEM_freeN(idatalist);
|
2018-01-09 14:09:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DRW_instance_data_list_reset(DRWInstanceDataList *idatalist)
|
|
|
|
|
{
|
|
|
|
|
DRWInstanceData *idata;
|
|
|
|
|
|
2019-09-08 00:12:26 +10:00
|
|
|
for (int i = 0; i < MAX_INSTANCE_DATA_SIZE; i++) {
|
2018-01-09 14:09:14 +01:00
|
|
|
for (idata = idatalist->idata_head[i]; idata; idata = idata->next) {
|
|
|
|
|
idata->used = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DRW_instance_data_list_free_unused(DRWInstanceDataList *idatalist)
|
|
|
|
|
{
|
|
|
|
|
DRWInstanceData *idata, *next_idata;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-01-09 14:09:14 +01:00
|
|
|
/* Remove unused data blocks and sanitize each list. */
|
2019-09-08 00:12:26 +10:00
|
|
|
for (int i = 0; i < MAX_INSTANCE_DATA_SIZE; i++) {
|
2023-07-27 14:16:58 +02:00
|
|
|
idatalist->idata_tail[i] = nullptr;
|
2018-01-09 14:09:14 +01:00
|
|
|
for (idata = idatalist->idata_head[i]; idata; idata = next_idata) {
|
|
|
|
|
next_idata = idata->next;
|
|
|
|
|
if (idata->used == false) {
|
|
|
|
|
if (idatalist->idata_head[i] == idata) {
|
|
|
|
|
idatalist->idata_head[i] = next_idata;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2018-09-19 18:19:49 +02:00
|
|
|
/* idatalist->idata_tail[i] is guaranteed not to be null in this case. */
|
2018-01-09 14:09:14 +01:00
|
|
|
idatalist->idata_tail[i]->next = next_idata;
|
|
|
|
|
}
|
|
|
|
|
DRW_instance_data_free(idata);
|
|
|
|
|
MEM_freeN(idata);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2023-07-27 14:16:58 +02:00
|
|
|
if (idatalist->idata_tail[i] != nullptr) {
|
2018-01-09 14:09:14 +01:00
|
|
|
idatalist->idata_tail[i]->next = idata;
|
|
|
|
|
}
|
|
|
|
|
idatalist->idata_tail[i] = idata;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DRW_instance_data_list_resize(DRWInstanceDataList *idatalist)
|
|
|
|
|
{
|
|
|
|
|
DRWInstanceData *idata;
|
|
|
|
|
|
2019-09-08 00:12:26 +10:00
|
|
|
for (int i = 0; i < MAX_INSTANCE_DATA_SIZE; i++) {
|
2018-01-09 14:09:14 +01:00
|
|
|
for (idata = idatalist->idata_head[i]; idata; idata = idata->next) {
|
2018-05-30 12:19:20 +02:00
|
|
|
BLI_mempool_clear_ex(idata->mempool, BLI_mempool_len(idata->mempool));
|
2018-01-09 14:09:14 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** \} */
|