bake.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright 2011-2014 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 "render/bake.h"
  17. #include "render/mesh.h"
  18. #include "render/object.h"
  19. #include "render/shader.h"
  20. #include "render/integrator.h"
  21. #include "util/util_foreach.h"
  22. CCL_NAMESPACE_BEGIN
  23. BakeData::BakeData(const int object, const size_t tri_offset, const size_t num_pixels)
  24. : m_object(object), m_tri_offset(tri_offset), m_num_pixels(num_pixels)
  25. {
  26. m_primitive.resize(num_pixels);
  27. m_u.resize(num_pixels);
  28. m_v.resize(num_pixels);
  29. m_dudx.resize(num_pixels);
  30. m_dudy.resize(num_pixels);
  31. m_dvdx.resize(num_pixels);
  32. m_dvdy.resize(num_pixels);
  33. }
  34. BakeData::~BakeData()
  35. {
  36. m_primitive.clear();
  37. m_u.clear();
  38. m_v.clear();
  39. m_dudx.clear();
  40. m_dudy.clear();
  41. m_dvdx.clear();
  42. m_dvdy.clear();
  43. }
  44. void BakeData::set(int i, int prim, float uv[2], float dudx, float dudy, float dvdx, float dvdy)
  45. {
  46. m_primitive[i] = (prim == -1 ? -1 : m_tri_offset + prim);
  47. m_u[i] = uv[0];
  48. m_v[i] = uv[1];
  49. m_dudx[i] = dudx;
  50. m_dudy[i] = dudy;
  51. m_dvdx[i] = dvdx;
  52. m_dvdy[i] = dvdy;
  53. }
  54. void BakeData::set_null(int i)
  55. {
  56. m_primitive[i] = -1;
  57. }
  58. int BakeData::object()
  59. {
  60. return m_object;
  61. }
  62. size_t BakeData::size()
  63. {
  64. return m_num_pixels;
  65. }
  66. bool BakeData::is_valid(int i)
  67. {
  68. return m_primitive[i] != -1;
  69. }
  70. uint4 BakeData::data(int i)
  71. {
  72. return make_uint4(m_object, m_primitive[i], __float_as_int(m_u[i]), __float_as_int(m_v[i]));
  73. }
  74. uint4 BakeData::differentials(int i)
  75. {
  76. return make_uint4(__float_as_int(m_dudx[i]),
  77. __float_as_int(m_dudy[i]),
  78. __float_as_int(m_dvdx[i]),
  79. __float_as_int(m_dvdy[i]));
  80. }
  81. BakeManager::BakeManager()
  82. {
  83. m_bake_data = NULL;
  84. m_is_baking = false;
  85. need_update = true;
  86. m_shader_limit = 512 * 512;
  87. }
  88. BakeManager::~BakeManager()
  89. {
  90. if (m_bake_data)
  91. delete m_bake_data;
  92. }
  93. bool BakeManager::get_baking()
  94. {
  95. return m_is_baking;
  96. }
  97. void BakeManager::set_baking(const bool value)
  98. {
  99. m_is_baking = value;
  100. }
  101. BakeData *BakeManager::init(const int object, const size_t tri_offset, const size_t num_pixels)
  102. {
  103. m_bake_data = new BakeData(object, tri_offset, num_pixels);
  104. return m_bake_data;
  105. }
  106. void BakeManager::set_shader_limit(const size_t x, const size_t y)
  107. {
  108. m_shader_limit = x * y;
  109. m_shader_limit = (size_t)pow(2, ceil(log(m_shader_limit) / log(2)));
  110. }
  111. bool BakeManager::bake(Device *device,
  112. DeviceScene *dscene,
  113. Scene *scene,
  114. Progress &progress,
  115. ShaderEvalType shader_type,
  116. const int pass_filter,
  117. BakeData *bake_data,
  118. float result[])
  119. {
  120. size_t num_pixels = bake_data->size();
  121. int num_samples = aa_samples(scene, bake_data, shader_type);
  122. /* calculate the total pixel samples for the progress bar */
  123. total_pixel_samples = 0;
  124. for (size_t shader_offset = 0; shader_offset < num_pixels; shader_offset += m_shader_limit) {
  125. size_t shader_size = (size_t)fminf(num_pixels - shader_offset, m_shader_limit);
  126. total_pixel_samples += shader_size * num_samples;
  127. }
  128. progress.reset_sample();
  129. progress.set_total_pixel_samples(total_pixel_samples);
  130. /* needs to be up to date for baking specific AA samples */
  131. dscene->data.integrator.aa_samples = num_samples;
  132. device->const_copy_to("__data", &dscene->data, sizeof(dscene->data));
  133. for (size_t shader_offset = 0; shader_offset < num_pixels; shader_offset += m_shader_limit) {
  134. size_t shader_size = (size_t)fminf(num_pixels - shader_offset, m_shader_limit);
  135. /* setup input for device task */
  136. device_vector<uint4> d_input(device, "bake_input", MEM_READ_ONLY);
  137. uint4 *d_input_data = d_input.alloc(shader_size * 2);
  138. size_t d_input_size = 0;
  139. for (size_t i = shader_offset; i < (shader_offset + shader_size); i++) {
  140. d_input_data[d_input_size++] = bake_data->data(i);
  141. d_input_data[d_input_size++] = bake_data->differentials(i);
  142. }
  143. if (d_input_size == 0) {
  144. m_is_baking = false;
  145. return false;
  146. }
  147. /* run device task */
  148. device_vector<float4> d_output(device, "bake_output", MEM_READ_WRITE);
  149. d_output.alloc(shader_size);
  150. d_output.zero_to_device();
  151. d_input.copy_to_device();
  152. DeviceTask task(DeviceTask::SHADER);
  153. task.shader_input = d_input.device_pointer;
  154. task.shader_output = d_output.device_pointer;
  155. task.shader_eval_type = shader_type;
  156. task.shader_filter = pass_filter;
  157. task.shader_x = 0;
  158. task.offset = shader_offset;
  159. task.shader_w = d_output.size();
  160. task.num_samples = num_samples;
  161. task.get_cancel = function_bind(&Progress::get_cancel, &progress);
  162. task.update_progress_sample = function_bind(&Progress::add_samples_update, &progress, _1, _2);
  163. device->task_add(task);
  164. device->task_wait();
  165. if (progress.get_cancel()) {
  166. d_input.free();
  167. d_output.free();
  168. m_is_baking = false;
  169. return false;
  170. }
  171. d_output.copy_from_device(0, 1, d_output.size());
  172. d_input.free();
  173. /* read result */
  174. int k = 0;
  175. float4 *offset = d_output.data();
  176. size_t depth = 4;
  177. for (size_t i = shader_offset; i < (shader_offset + shader_size); i++) {
  178. size_t index = i * depth;
  179. float4 out = offset[k++];
  180. if (bake_data->is_valid(i)) {
  181. for (size_t j = 0; j < 4; j++) {
  182. result[index + j] = out[j];
  183. }
  184. }
  185. }
  186. d_output.free();
  187. }
  188. m_is_baking = false;
  189. return true;
  190. }
  191. void BakeManager::device_update(Device * /*device*/,
  192. DeviceScene * /*dscene*/,
  193. Scene * /*scene*/,
  194. Progress &progress)
  195. {
  196. if (!need_update)
  197. return;
  198. if (progress.get_cancel())
  199. return;
  200. need_update = false;
  201. }
  202. void BakeManager::device_free(Device * /*device*/, DeviceScene * /*dscene*/)
  203. {
  204. }
  205. int BakeManager::aa_samples(Scene *scene, BakeData *bake_data, ShaderEvalType type)
  206. {
  207. if (type == SHADER_EVAL_UV || type == SHADER_EVAL_ROUGHNESS) {
  208. return 1;
  209. }
  210. else if (type == SHADER_EVAL_NORMAL) {
  211. /* Only antialias normal if mesh has bump mapping. */
  212. Object *object = scene->objects[bake_data->object()];
  213. if (object->mesh) {
  214. foreach (Shader *shader, object->mesh->used_shaders) {
  215. if (shader->has_bump) {
  216. return scene->integrator->aa_samples;
  217. }
  218. }
  219. }
  220. return 1;
  221. }
  222. else {
  223. return scene->integrator->aa_samples;
  224. }
  225. }
  226. /* Keep it synced with kernel_bake.h logic */
  227. int BakeManager::shader_type_to_pass_filter(ShaderEvalType type, const int pass_filter)
  228. {
  229. const int component_flags = pass_filter &
  230. (BAKE_FILTER_DIRECT | BAKE_FILTER_INDIRECT | BAKE_FILTER_COLOR);
  231. switch (type) {
  232. case SHADER_EVAL_AO:
  233. return BAKE_FILTER_AO;
  234. case SHADER_EVAL_SHADOW:
  235. return BAKE_FILTER_DIRECT;
  236. case SHADER_EVAL_DIFFUSE:
  237. return BAKE_FILTER_DIFFUSE | component_flags;
  238. case SHADER_EVAL_GLOSSY:
  239. return BAKE_FILTER_GLOSSY | component_flags;
  240. case SHADER_EVAL_TRANSMISSION:
  241. return BAKE_FILTER_TRANSMISSION | component_flags;
  242. case SHADER_EVAL_SUBSURFACE:
  243. return BAKE_FILTER_SUBSURFACE | component_flags;
  244. case SHADER_EVAL_COMBINED:
  245. return pass_filter;
  246. default:
  247. return 0;
  248. }
  249. }
  250. CCL_NAMESPACE_END