Blender  V2.93
tables.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "render/tables.h"
18 #include "device/device.h"
19 #include "render/scene.h"
20 #include "render/stats.h"
21 
22 #include "util/util_logging.h"
23 #include "util/util_time.h"
24 
26 
27 /* Lookup Tables */
28 
30 {
31  need_update_ = true;
32 }
33 
35 {
36  assert(lookup_tables.size() == 0);
37 }
38 
40 {
41  if (!need_update())
42  return;
43 
44  scoped_callback_timer timer([scene](double time) {
45  if (scene->update_stats) {
46  scene->update_stats->tables.times.add_entry({"device_update", time});
47  }
48  });
49 
50  VLOG(1) << "Total " << lookup_tables.size() << " lookup tables.";
51 
52  if (lookup_tables.size() > 0)
53  dscene->lookup_table.copy_to_device();
54 
55  need_update_ = false;
56 }
57 
59 {
60  dscene->lookup_table.free();
61 }
62 
64 {
65  return need_update_;
66 }
67 
68 static size_t round_up_to_multiple(size_t size, size_t chunk)
69 {
70  return ((size + chunk - 1) / chunk) * chunk;
71 }
72 
74 {
75  assert(data.size() > 0);
76 
77  need_update_ = true;
78 
79  Table new_table;
80  new_table.offset = 0;
81  new_table.size = round_up_to_multiple(data.size(), TABLE_CHUNK_SIZE);
82 
83  /* find space to put lookup table */
84  list<Table>::iterator table;
85 
86  for (table = lookup_tables.begin(); table != lookup_tables.end(); table++) {
87  if (new_table.offset + new_table.size <= table->offset) {
88  lookup_tables.insert(table, new_table);
89  break;
90  }
91  else
92  new_table.offset = table->offset + table->size;
93  }
94 
95  if (table == lookup_tables.end()) {
96  /* add at the end */
97  lookup_tables.push_back(new_table);
98  dscene->lookup_table.resize(new_table.offset + new_table.size);
99  }
100 
101  /* copy table data and return offset */
102  float *dtable = dscene->lookup_table.data();
103  memcpy(dtable + new_table.offset, &data[0], sizeof(float) * data.size());
104 
105  return new_table.offset;
106 }
107 
108 void LookupTables::remove_table(size_t *offset)
109 {
110  if (*offset == TABLE_OFFSET_INVALID) {
111  /* The table isn't even allocated, so just return here. */
112  return;
113  }
114 
115  need_update_ = true;
116 
117  list<Table>::iterator table;
118 
119  for (table = lookup_tables.begin(); table != lookup_tables.end(); table++) {
120  if (table->offset == *offset) {
121  lookup_tables.erase(table);
122  *offset = TABLE_OFFSET_INVALID;
123  return;
124  }
125  }
126 
127  assert(table != lookup_tables.end());
128 }
129 
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
device_vector< float > lookup_table
Definition: scene.h:128
Definition: device.h:293
void device_free(Device *device, DeviceScene *dscene)
Definition: tables.cpp:58
size_t add_table(DeviceScene *dscene, vector< float > &data)
Definition: tables.cpp:73
~LookupTables()
Definition: tables.cpp:34
void remove_table(size_t *offset)
Definition: tables.cpp:108
LookupTables()
Definition: tables.cpp:29
list< Table > lookup_tables
Definition: tables.h:41
void device_update(Device *device, DeviceScene *dscene, Scene *scene)
Definition: tables.cpp:39
bool need_update() const
Definition: tables.cpp:63
T * resize(size_t width, size_t height=0, size_t depth=0)
double time
Scene scene
#define CCL_NAMESPACE_END
SceneUpdateStats * update_stats
Definition: scene.h:270
static size_t round_up_to_multiple(size_t size, size_t chunk)
Definition: tables.cpp:68
@ TABLE_CHUNK_SIZE
Definition: tables.h:29
@ TABLE_OFFSET_INVALID
Definition: tables.h:30
#define VLOG(severity)
Definition: util_logging.h:50