2013-04-01 20:26:43 +00:00
|
|
|
/*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Copyright 2011-2013 Blender Foundation
|
2013-04-01 20:26:43 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2013-04-01 20:26:43 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-04-01 20:26:43 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
2014-12-25 01:50:24 +00:00
|
|
|
* limitations under the License.
|
2013-04-01 20:26:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "device.h"
|
|
|
|
#include "scene.h"
|
|
|
|
#include "tables.h"
|
|
|
|
|
|
|
|
#include "util_debug.h"
|
2015-04-10 10:36:56 +00:00
|
|
|
#include "util_logging.h"
|
2013-04-01 20:26:43 +00:00
|
|
|
|
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
2013-04-01 20:26:52 +00:00
|
|
|
/* Lookup Tables */
|
|
|
|
|
2013-04-01 20:26:43 +00:00
|
|
|
LookupTables::LookupTables()
|
|
|
|
{
|
|
|
|
need_update = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
LookupTables::~LookupTables()
|
|
|
|
{
|
|
|
|
assert(lookup_tables.size() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LookupTables::device_update(Device *device, DeviceScene *dscene)
|
|
|
|
{
|
2015-04-10 10:36:56 +00:00
|
|
|
VLOG(1) << "Total " << lookup_tables.size() << " lookup tables.";
|
|
|
|
|
2013-04-01 20:26:43 +00:00
|
|
|
if(!need_update)
|
|
|
|
return;
|
|
|
|
|
2014-03-13 13:16:42 +00:00
|
|
|
device->tex_free(dscene->lookup_table);
|
|
|
|
|
|
|
|
if(lookup_tables.size() > 0)
|
|
|
|
device->tex_alloc("__lookup_table", dscene->lookup_table);
|
2013-04-01 20:26:43 +00:00
|
|
|
|
|
|
|
need_update = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LookupTables::device_free(Device *device, DeviceScene *dscene)
|
|
|
|
{
|
|
|
|
device->tex_free(dscene->lookup_table);
|
|
|
|
dscene->lookup_table.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t round_up_to_multiple(size_t size, size_t chunk)
|
|
|
|
{
|
|
|
|
return ((size + chunk - 1)/chunk) * chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t LookupTables::add_table(DeviceScene *dscene, vector<float>& data)
|
|
|
|
{
|
|
|
|
assert(data.size() > 0);
|
|
|
|
|
|
|
|
need_update = true;
|
|
|
|
|
|
|
|
Table new_table;
|
|
|
|
new_table.offset = 0;
|
|
|
|
new_table.size = round_up_to_multiple(data.size(), TABLE_CHUNK_SIZE);
|
|
|
|
|
|
|
|
/* find space to put lookup table */
|
|
|
|
list<Table>::iterator table;
|
|
|
|
|
|
|
|
for(table = lookup_tables.begin(); table != lookup_tables.end(); table++) {
|
|
|
|
if(new_table.offset + new_table.size <= table->offset) {
|
|
|
|
lookup_tables.insert(table, new_table);
|
|
|
|
break;
|
|
|
|
}
|
2013-04-01 20:26:52 +00:00
|
|
|
else
|
|
|
|
new_table.offset = table->offset + table->size;
|
2013-04-01 20:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(table == lookup_tables.end()) {
|
|
|
|
/* add at the end */
|
|
|
|
lookup_tables.push_back(new_table);
|
|
|
|
dscene->lookup_table.resize(new_table.offset + new_table.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy table data and return offset */
|
|
|
|
dscene->lookup_table.copy_at(&data[0], new_table.offset, data.size());
|
|
|
|
return new_table.offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LookupTables::remove_table(size_t offset)
|
|
|
|
{
|
|
|
|
need_update = true;
|
|
|
|
|
|
|
|
list<Table>::iterator table;
|
|
|
|
|
|
|
|
for(table = lookup_tables.begin(); table != lookup_tables.end(); table++) {
|
|
|
|
if(table->offset == offset) {
|
|
|
|
lookup_tables.erase(table);
|
2013-07-14 13:19:57 +00:00
|
|
|
return;
|
2013-04-01 20:26:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(table != lookup_tables.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|