device_task.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <stdlib.h>
  17. #include <string.h>
  18. #include "device/device_task.h"
  19. #include "render/buffers.h"
  20. #include "util/util_algorithm.h"
  21. #include "util/util_time.h"
  22. CCL_NAMESPACE_BEGIN
  23. /* Device Task */
  24. DeviceTask::DeviceTask(Type type_)
  25. : type(type_),
  26. x(0),
  27. y(0),
  28. w(0),
  29. h(0),
  30. rgba_byte(0),
  31. rgba_half(0),
  32. buffer(0),
  33. sample(0),
  34. num_samples(1),
  35. shader_input(0),
  36. shader_output(0),
  37. shader_eval_type(0),
  38. shader_filter(0),
  39. shader_x(0),
  40. shader_w(0)
  41. {
  42. last_update_time = time_dt();
  43. }
  44. int DeviceTask::get_subtask_count(int num, int max_size)
  45. {
  46. if (max_size != 0) {
  47. int max_size_num;
  48. if (type == SHADER) {
  49. max_size_num = (shader_w + max_size - 1) / max_size;
  50. }
  51. else {
  52. max_size = max(1, max_size / w);
  53. max_size_num = (h + max_size - 1) / max_size;
  54. }
  55. num = max(max_size_num, num);
  56. }
  57. if (type == SHADER) {
  58. num = min(shader_w, num);
  59. }
  60. else if (type == RENDER) {
  61. }
  62. else {
  63. num = min(h, num);
  64. }
  65. return num;
  66. }
  67. void DeviceTask::split(list<DeviceTask> &tasks, int num, int max_size)
  68. {
  69. num = get_subtask_count(num, max_size);
  70. if (type == SHADER) {
  71. for (int i = 0; i < num; i++) {
  72. int tx = shader_x + (shader_w / num) * i;
  73. int tw = (i == num - 1) ? shader_w - i * (shader_w / num) : shader_w / num;
  74. DeviceTask task = *this;
  75. task.shader_x = tx;
  76. task.shader_w = tw;
  77. tasks.push_back(task);
  78. }
  79. }
  80. else if (type == RENDER) {
  81. for (int i = 0; i < num; i++)
  82. tasks.push_back(*this);
  83. }
  84. else {
  85. for (int i = 0; i < num; i++) {
  86. int ty = y + (h / num) * i;
  87. int th = (i == num - 1) ? h - i * (h / num) : h / num;
  88. DeviceTask task = *this;
  89. task.y = ty;
  90. task.h = th;
  91. tasks.push_back(task);
  92. }
  93. }
  94. }
  95. void DeviceTask::update_progress(RenderTile *rtile, int pixel_samples)
  96. {
  97. if ((type != RENDER) && (type != SHADER))
  98. return;
  99. if (update_progress_sample) {
  100. if (pixel_samples == -1) {
  101. pixel_samples = shader_w;
  102. }
  103. update_progress_sample(pixel_samples, rtile ? rtile->sample : 0);
  104. }
  105. if (update_tile_sample) {
  106. double current_time = time_dt();
  107. if (current_time - last_update_time >= 1.0) {
  108. update_tile_sample(*rtile);
  109. last_update_time = current_time;
  110. }
  111. }
  112. }
  113. CCL_NAMESPACE_END