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