2023-06-14 16:52:36 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0 */
|
2017-10-20 23:31:13 +02:00
|
|
|
|
2021-10-24 14:19:19 +02:00
|
|
|
#include "device/memory.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "device/device.h"
|
2017-10-20 23:31:13 +02:00
|
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
|
|
/* Device Memory */
|
|
|
|
|
|
2024-05-15 23:19:24 +02:00
|
|
|
device_memory::device_memory(Device *device, const char *_name, MemoryType type)
|
2017-10-20 23:31:13 +02:00
|
|
|
: data_type(device_type_traits<uchar>::data_type),
|
2021-11-16 14:03:59 +01:00
|
|
|
data_elements(device_type_traits<uchar>::num_elements),
|
2017-10-20 23:31:13 +02:00
|
|
|
data_size(0),
|
|
|
|
|
device_size(0),
|
|
|
|
|
data_width(0),
|
|
|
|
|
data_height(0),
|
|
|
|
|
type(type),
|
2024-05-15 23:19:24 +02:00
|
|
|
name_storage(_name),
|
2017-10-20 23:31:13 +02:00
|
|
|
device(device),
|
2017-11-05 00:34:30 +01:00
|
|
|
device_pointer(0),
|
2024-12-26 17:53:59 +01:00
|
|
|
host_pointer(nullptr),
|
|
|
|
|
shared_pointer(nullptr),
|
2021-05-19 00:55:22 +02:00
|
|
|
shared_counter(0),
|
|
|
|
|
original_device_ptr(0),
|
|
|
|
|
original_device_size(0),
|
2024-12-26 17:53:59 +01:00
|
|
|
original_device(nullptr),
|
2021-05-19 00:55:22 +02:00
|
|
|
need_realloc_(false),
|
|
|
|
|
modified(false)
|
2017-10-20 23:31:13 +02:00
|
|
|
{
|
2024-05-15 23:19:24 +02:00
|
|
|
name = name_storage.c_str();
|
2017-10-20 23:31:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
device_memory::~device_memory()
|
|
|
|
|
{
|
2024-12-26 17:53:59 +01:00
|
|
|
assert(shared_pointer == nullptr);
|
2019-11-05 16:27:52 +01:00
|
|
|
assert(shared_counter == 0);
|
2017-10-20 23:31:13 +02:00
|
|
|
}
|
|
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
void *device_memory::host_alloc(const size_t size)
|
2017-10-21 01:09:59 +02:00
|
|
|
{
|
|
|
|
|
if (!size) {
|
2024-12-26 17:53:59 +01:00
|
|
|
return nullptr;
|
2017-10-21 01:09:59 +02:00
|
|
|
}
|
|
|
|
|
|
2025-02-13 13:11:39 +01:00
|
|
|
void *ptr = device->host_alloc(type, size);
|
2017-10-21 01:09:59 +02:00
|
|
|
|
2025-01-09 12:04:08 +01:00
|
|
|
if (ptr == nullptr) {
|
2017-10-21 01:09:59 +02:00
|
|
|
throw std::bad_alloc();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-17 09:41:34 +01:00
|
|
|
void device_memory::host_and_device_free()
|
2017-10-21 01:09:59 +02:00
|
|
|
{
|
2017-11-05 00:34:30 +01:00
|
|
|
if (host_pointer) {
|
2025-01-17 09:41:34 +01:00
|
|
|
if (host_pointer != shared_pointer) {
|
2025-02-13 13:11:39 +01:00
|
|
|
device->host_free(type, host_pointer, memory_size());
|
2025-01-17 09:41:34 +01:00
|
|
|
}
|
2024-12-26 17:53:59 +01:00
|
|
|
host_pointer = nullptr;
|
2017-10-21 01:09:59 +02:00
|
|
|
}
|
2025-01-17 09:41:34 +01:00
|
|
|
|
|
|
|
|
if (device_pointer) {
|
|
|
|
|
device->mem_free(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data_size = 0;
|
|
|
|
|
data_width = 0;
|
|
|
|
|
data_height = 0;
|
2017-10-21 01:09:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void device_memory::device_alloc()
|
|
|
|
|
{
|
2020-03-12 15:22:18 +01:00
|
|
|
assert(!device_pointer && type != MEM_TEXTURE && type != MEM_GLOBAL);
|
2017-10-21 01:09:59 +02:00
|
|
|
device->mem_alloc(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void device_memory::device_copy_to()
|
|
|
|
|
{
|
2018-01-02 22:56:07 +01:00
|
|
|
if (host_pointer) {
|
2017-10-21 01:09:59 +02:00
|
|
|
device->mem_copy_to(*this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-09 12:04:08 +01:00
|
|
|
void device_memory::device_move_to_host()
|
|
|
|
|
{
|
|
|
|
|
if (host_pointer) {
|
|
|
|
|
device->mem_move_to_host(*this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
void device_memory::device_copy_from(const size_t y, const size_t w, size_t h, const size_t elem)
|
2017-10-21 01:09:59 +02:00
|
|
|
{
|
2020-03-12 15:22:18 +01:00
|
|
|
assert(type != MEM_TEXTURE && type != MEM_READ_ONLY && type != MEM_GLOBAL);
|
2017-10-21 01:09:59 +02:00
|
|
|
device->mem_copy_from(*this, y, w, h, elem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void device_memory::device_zero()
|
|
|
|
|
{
|
|
|
|
|
if (data_size) {
|
|
|
|
|
device->mem_zero(*this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Cycles: merge of cycles-x branch, a major update to the renderer
This includes much improved GPU rendering performance, viewport interactivity,
new shadow catcher, revamped sampling settings, subsurface scattering anisotropy,
new GPU volume sampling, improved PMJ sampling pattern, and more.
Some features have also been removed or changed, breaking backwards compatibility.
Including the removal of the OpenCL backend, for which alternatives are under
development.
Release notes and code docs:
https://wiki.blender.org/wiki/Reference/Release_Notes/3.0/Cycles
https://wiki.blender.org/wiki/Source/Render/Cycles
Credits:
* Sergey Sharybin
* Brecht Van Lommel
* Patrick Mours (OptiX backend)
* Christophe Hery (subsurface scattering anisotropy)
* William Leeson (PMJ sampling pattern)
* Alaska (various fixes and tweaks)
* Thomas Dinges (various fixes)
For the full commit history, see the cycles-x branch. This squashes together
all the changes since intermediate changes would often fail building or tests.
Ref T87839, T87837, T87836
Fixes T90734, T89353, T80267, T80267, T77185, T69800
2021-09-20 17:59:20 +02:00
|
|
|
bool device_memory::device_is_cpu()
|
|
|
|
|
{
|
|
|
|
|
return (device->info.type == DEVICE_CPU);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-04 13:19:12 +02:00
|
|
|
void device_memory::swap_device(Device *new_device,
|
2025-01-01 18:15:54 +01:00
|
|
|
const size_t new_device_size,
|
2018-07-04 13:19:12 +02:00
|
|
|
device_ptr new_device_ptr)
|
|
|
|
|
{
|
|
|
|
|
original_device = device;
|
|
|
|
|
original_device_size = device_size;
|
|
|
|
|
original_device_ptr = device_pointer;
|
|
|
|
|
|
|
|
|
|
device = new_device;
|
|
|
|
|
device_size = new_device_size;
|
|
|
|
|
device_pointer = new_device_ptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void device_memory::restore_device()
|
|
|
|
|
{
|
|
|
|
|
device = original_device;
|
|
|
|
|
device_size = original_device_size;
|
|
|
|
|
device_pointer = original_device_ptr;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 17:16:10 +02:00
|
|
|
bool device_memory::is_resident(Device *sub_device) const
|
|
|
|
|
{
|
|
|
|
|
return device->is_resident(device_pointer, sub_device);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-13 13:11:39 +01:00
|
|
|
bool device_memory::is_shared(Device *sub_device) const
|
2025-01-21 16:07:29 +01:00
|
|
|
{
|
2025-02-13 13:11:39 +01:00
|
|
|
return device->is_shared(shared_pointer, device_pointer, sub_device);
|
2025-01-21 16:07:29 +01:00
|
|
|
}
|
|
|
|
|
|
2023-07-05 13:58:04 +10:00
|
|
|
/* Device Sub `ptr`. */
|
2017-10-20 23:31:13 +02:00
|
|
|
|
2025-01-01 18:15:54 +01:00
|
|
|
device_sub_ptr::device_sub_ptr(device_memory &mem, const size_t offset, const size_t size)
|
|
|
|
|
: device(mem.device)
|
2017-10-20 23:31:13 +02:00
|
|
|
{
|
|
|
|
|
ptr = device->mem_alloc_sub_ptr(mem, offset, size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
device_sub_ptr::~device_sub_ptr()
|
|
|
|
|
{
|
|
|
|
|
device->mem_free_sub_ptr(ptr);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 15:22:18 +01:00
|
|
|
/* Device Texture */
|
|
|
|
|
|
|
|
|
|
device_texture::device_texture(Device *device,
|
|
|
|
|
const char *name,
|
|
|
|
|
const uint slot,
|
|
|
|
|
ImageDataType image_data_type,
|
|
|
|
|
InterpolationType interpolation,
|
|
|
|
|
ExtensionType extension)
|
|
|
|
|
: device_memory(device, name, MEM_TEXTURE), slot(slot)
|
|
|
|
|
{
|
|
|
|
|
switch (image_data_type) {
|
|
|
|
|
case IMAGE_DATA_TYPE_FLOAT4:
|
|
|
|
|
data_type = TYPE_FLOAT;
|
|
|
|
|
data_elements = 4;
|
|
|
|
|
break;
|
|
|
|
|
case IMAGE_DATA_TYPE_FLOAT:
|
|
|
|
|
data_type = TYPE_FLOAT;
|
|
|
|
|
data_elements = 1;
|
|
|
|
|
break;
|
|
|
|
|
case IMAGE_DATA_TYPE_BYTE4:
|
|
|
|
|
data_type = TYPE_UCHAR;
|
|
|
|
|
data_elements = 4;
|
|
|
|
|
break;
|
|
|
|
|
case IMAGE_DATA_TYPE_BYTE:
|
2020-10-02 17:40:28 +02:00
|
|
|
case IMAGE_DATA_TYPE_NANOVDB_FLOAT:
|
|
|
|
|
case IMAGE_DATA_TYPE_NANOVDB_FLOAT3:
|
2025-01-09 14:25:42 +01:00
|
|
|
case IMAGE_DATA_TYPE_NANOVDB_FLOAT4:
|
2022-05-20 18:01:26 +02:00
|
|
|
case IMAGE_DATA_TYPE_NANOVDB_FPN:
|
|
|
|
|
case IMAGE_DATA_TYPE_NANOVDB_FP16:
|
2025-01-11 04:42:13 +01:00
|
|
|
case IMAGE_DATA_TYPE_NANOVDB_EMPTY:
|
2020-03-12 15:22:18 +01:00
|
|
|
data_type = TYPE_UCHAR;
|
|
|
|
|
data_elements = 1;
|
|
|
|
|
break;
|
|
|
|
|
case IMAGE_DATA_TYPE_HALF4:
|
|
|
|
|
data_type = TYPE_HALF;
|
|
|
|
|
data_elements = 4;
|
|
|
|
|
break;
|
|
|
|
|
case IMAGE_DATA_TYPE_HALF:
|
|
|
|
|
data_type = TYPE_HALF;
|
|
|
|
|
data_elements = 1;
|
|
|
|
|
break;
|
|
|
|
|
case IMAGE_DATA_TYPE_USHORT4:
|
|
|
|
|
data_type = TYPE_UINT16;
|
|
|
|
|
data_elements = 4;
|
|
|
|
|
break;
|
|
|
|
|
case IMAGE_DATA_TYPE_USHORT:
|
|
|
|
|
data_type = TYPE_UINT16;
|
|
|
|
|
data_elements = 1;
|
|
|
|
|
break;
|
|
|
|
|
case IMAGE_DATA_NUM_TYPES:
|
|
|
|
|
assert(0);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
info.data_type = image_data_type;
|
|
|
|
|
info.interpolation = interpolation;
|
|
|
|
|
info.extension = extension;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
device_texture::~device_texture()
|
|
|
|
|
{
|
2025-01-17 09:41:34 +01:00
|
|
|
host_and_device_free();
|
2020-03-12 15:22:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Host memory allocation. */
|
2025-01-12 07:40:44 +01:00
|
|
|
void *device_texture::alloc(const size_t width, const size_t height)
|
2020-03-12 15:22:18 +01:00
|
|
|
{
|
2025-01-12 07:40:44 +01:00
|
|
|
const size_t new_size = size(width, height);
|
2020-03-12 15:22:18 +01:00
|
|
|
|
|
|
|
|
if (new_size != data_size) {
|
2025-01-17 09:41:34 +01:00
|
|
|
host_and_device_free();
|
2020-03-12 15:22:18 +01:00
|
|
|
host_pointer = host_alloc(data_elements * datatype_size(data_type) * new_size);
|
|
|
|
|
assert(device_pointer == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data_size = new_size;
|
|
|
|
|
data_width = width;
|
|
|
|
|
data_height = height;
|
|
|
|
|
|
|
|
|
|
info.width = width;
|
|
|
|
|
info.height = height;
|
|
|
|
|
|
|
|
|
|
return host_pointer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void device_texture::copy_to_device()
|
|
|
|
|
{
|
|
|
|
|
device_copy_to();
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-20 23:31:13 +02:00
|
|
|
CCL_NAMESPACE_END
|