tables.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include "device/device.h"
  17. #include "render/scene.h"
  18. #include "render/tables.h"
  19. #include "util/util_logging.h"
  20. CCL_NAMESPACE_BEGIN
  21. /* Lookup Tables */
  22. LookupTables::LookupTables()
  23. {
  24. need_update = true;
  25. }
  26. LookupTables::~LookupTables()
  27. {
  28. assert(lookup_tables.size() == 0);
  29. }
  30. void LookupTables::device_update(Device *, DeviceScene *dscene)
  31. {
  32. if (!need_update)
  33. return;
  34. VLOG(1) << "Total " << lookup_tables.size() << " lookup tables.";
  35. if (lookup_tables.size() > 0)
  36. dscene->lookup_table.copy_to_device();
  37. need_update = false;
  38. }
  39. void LookupTables::device_free(Device *, DeviceScene *dscene)
  40. {
  41. dscene->lookup_table.free();
  42. }
  43. static size_t round_up_to_multiple(size_t size, size_t chunk)
  44. {
  45. return ((size + chunk - 1) / chunk) * chunk;
  46. }
  47. size_t LookupTables::add_table(DeviceScene *dscene, vector<float> &data)
  48. {
  49. assert(data.size() > 0);
  50. need_update = true;
  51. Table new_table;
  52. new_table.offset = 0;
  53. new_table.size = round_up_to_multiple(data.size(), TABLE_CHUNK_SIZE);
  54. /* find space to put lookup table */
  55. list<Table>::iterator table;
  56. for (table = lookup_tables.begin(); table != lookup_tables.end(); table++) {
  57. if (new_table.offset + new_table.size <= table->offset) {
  58. lookup_tables.insert(table, new_table);
  59. break;
  60. }
  61. else
  62. new_table.offset = table->offset + table->size;
  63. }
  64. if (table == lookup_tables.end()) {
  65. /* add at the end */
  66. lookup_tables.push_back(new_table);
  67. dscene->lookup_table.resize(new_table.offset + new_table.size);
  68. }
  69. /* copy table data and return offset */
  70. float *dtable = dscene->lookup_table.data();
  71. memcpy(dtable + new_table.offset, &data[0], sizeof(float) * data.size());
  72. return new_table.offset;
  73. }
  74. void LookupTables::remove_table(size_t *offset)
  75. {
  76. if (*offset == TABLE_OFFSET_INVALID) {
  77. /* The table isn't even allocated, so just return here. */
  78. return;
  79. }
  80. need_update = true;
  81. list<Table>::iterator table;
  82. for (table = lookup_tables.begin(); table != lookup_tables.end(); table++) {
  83. if (table->offset == *offset) {
  84. lookup_tables.erase(table);
  85. *offset = TABLE_OFFSET_INVALID;
  86. return;
  87. }
  88. }
  89. assert(table != lookup_tables.end());
  90. }
  91. CCL_NAMESPACE_END