Blender V4.3
tables.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "scene/tables.h"
6#include "device/device.h"
7#include "scene/scene.h"
8#include "scene/stats.h"
9
10#include "util/log.h"
11#include "util/time.h"
12
14
15/* Lookup Tables */
16
18{
19 need_update_ = true;
20}
21
23{
24 assert(lookup_tables.size() == 0);
25}
26
28{
29 if (!need_update()) {
30 return;
31 }
32
33 scoped_callback_timer timer([scene](double time) {
34 if (scene->update_stats) {
35 scene->update_stats->tables.times.add_entry({"device_update", time});
36 }
37 });
38
39 VLOG_INFO << "Total " << lookup_tables.size() << " lookup tables.";
40
41 if (lookup_tables.size() > 0) {
42 dscene->lookup_table.copy_to_device();
43 }
44
45 need_update_ = false;
46}
47
49{
50 dscene->lookup_table.free();
51}
52
54{
55 return need_update_;
56}
57
58static size_t round_up_to_multiple(size_t size, size_t chunk)
59{
60 return ((size + chunk - 1) / chunk) * chunk;
61}
62
64{
65 assert(data.size() > 0);
66
67 need_update_ = true;
68
69 Table new_table;
70 new_table.offset = 0;
71 new_table.size = round_up_to_multiple(data.size(), TABLE_CHUNK_SIZE);
72
73 /* find space to put lookup table */
74 list<Table>::iterator table;
75
76 for (table = lookup_tables.begin(); table != lookup_tables.end(); table++) {
77 if (new_table.offset + new_table.size <= table->offset) {
78 lookup_tables.insert(table, new_table);
79 break;
80 }
81 else {
82 new_table.offset = table->offset + table->size;
83 }
84 }
85
86 if (table == lookup_tables.end()) {
87 /* add at the end */
88 lookup_tables.push_back(new_table);
89 dscene->lookup_table.resize(new_table.offset + new_table.size);
90 }
91
92 /* copy table data and return offset */
93 float *dtable = dscene->lookup_table.data();
94 memcpy(dtable + new_table.offset, &data[0], sizeof(float) * data.size());
95
96 return new_table.offset;
97}
98
99void LookupTables::remove_table(size_t *offset)
100{
101 if (*offset == TABLE_OFFSET_INVALID) {
102 /* The table isn't even allocated, so just return here. */
103 return;
104 }
105
106 need_update_ = true;
107
108 list<Table>::iterator table;
109
110 for (table = lookup_tables.begin(); table != lookup_tables.end(); table++) {
111 if (table->offset == *offset) {
112 lookup_tables.erase(table);
113 *offset = TABLE_OFFSET_INVALID;
114 return;
115 }
116 }
117
118 assert(table != lookup_tables.end());
119}
120
device_vector< float > lookup_table
Definition devicescene.h:87
void device_free(Device *device, DeviceScene *dscene)
Definition tables.cpp:48
size_t add_table(DeviceScene *dscene, vector< float > &data)
Definition tables.cpp:63
void remove_table(size_t *offset)
Definition tables.cpp:99
list< Table > lookup_tables
void device_update(Device *device, DeviceScene *dscene, Scene *scene)
Definition tables.cpp:27
bool need_update() const
Definition tables.cpp:53
T * resize(size_t width, size_t height=0, size_t depth=0)
#define CCL_NAMESPACE_END
#define VLOG_INFO
Definition log.h:72
@ TABLE_CHUNK_SIZE
@ TABLE_OFFSET_INVALID
static size_t round_up_to_multiple(size_t size, size_t chunk)
Definition tables.cpp:58
wmTimer * timer