light.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  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 "render/background.h"
  17. #include "device/device.h"
  18. #include "render/integrator.h"
  19. #include "render/film.h"
  20. #include "render/graph.h"
  21. #include "render/light.h"
  22. #include "render/mesh.h"
  23. #include "render/nodes.h"
  24. #include "render/object.h"
  25. #include "render/scene.h"
  26. #include "render/shader.h"
  27. #include "util/util_foreach.h"
  28. #include "util/util_hash.h"
  29. #include "util/util_path.h"
  30. #include "util/util_progress.h"
  31. #include "util/util_logging.h"
  32. CCL_NAMESPACE_BEGIN
  33. static void shade_background_pixels(Device *device,
  34. DeviceScene *dscene,
  35. int width,
  36. int height,
  37. vector<float3> &pixels,
  38. Progress &progress)
  39. {
  40. /* create input */
  41. device_vector<uint4> d_input(device, "background_input", MEM_READ_ONLY);
  42. device_vector<float4> d_output(device, "background_output", MEM_READ_WRITE);
  43. uint4 *d_input_data = d_input.alloc(width * height);
  44. for (int y = 0; y < height; y++) {
  45. for (int x = 0; x < width; x++) {
  46. float u = (x + 0.5f) / width;
  47. float v = (y + 0.5f) / height;
  48. uint4 in = make_uint4(__float_as_int(u), __float_as_int(v), 0, 0);
  49. d_input_data[x + y * width] = in;
  50. }
  51. }
  52. /* compute on device */
  53. d_output.alloc(width * height);
  54. d_output.zero_to_device();
  55. d_input.copy_to_device();
  56. device->const_copy_to("__data", &dscene->data, sizeof(dscene->data));
  57. DeviceTask main_task(DeviceTask::SHADER);
  58. main_task.shader_input = d_input.device_pointer;
  59. main_task.shader_output = d_output.device_pointer;
  60. main_task.shader_eval_type = SHADER_EVAL_BACKGROUND;
  61. main_task.shader_x = 0;
  62. main_task.shader_w = width * height;
  63. main_task.num_samples = 1;
  64. main_task.get_cancel = function_bind(&Progress::get_cancel, &progress);
  65. /* disabled splitting for now, there's an issue with multi-GPU mem_copy_from */
  66. list<DeviceTask> split_tasks;
  67. main_task.split(split_tasks, 1, 128 * 128);
  68. foreach (DeviceTask &task, split_tasks) {
  69. device->task_add(task);
  70. device->task_wait();
  71. d_output.copy_from_device(task.shader_x, 1, task.shader_w);
  72. }
  73. d_input.free();
  74. float4 *d_output_data = d_output.data();
  75. pixels.resize(width * height);
  76. for (int y = 0; y < height; y++) {
  77. for (int x = 0; x < width; x++) {
  78. pixels[y * width + x].x = d_output_data[y * width + x].x;
  79. pixels[y * width + x].y = d_output_data[y * width + x].y;
  80. pixels[y * width + x].z = d_output_data[y * width + x].z;
  81. }
  82. }
  83. d_output.free();
  84. }
  85. /* Light */
  86. NODE_DEFINE(Light)
  87. {
  88. NodeType *type = NodeType::add("light", create);
  89. static NodeEnum type_enum;
  90. type_enum.insert("point", LIGHT_POINT);
  91. type_enum.insert("distant", LIGHT_DISTANT);
  92. type_enum.insert("background", LIGHT_BACKGROUND);
  93. type_enum.insert("area", LIGHT_AREA);
  94. type_enum.insert("spot", LIGHT_SPOT);
  95. SOCKET_ENUM(type, "Type", type_enum, LIGHT_POINT);
  96. SOCKET_COLOR(strength, "Strength", make_float3(1.0f, 1.0f, 1.0f));
  97. SOCKET_POINT(co, "Co", make_float3(0.0f, 0.0f, 0.0f));
  98. SOCKET_VECTOR(dir, "Dir", make_float3(0.0f, 0.0f, 0.0f));
  99. SOCKET_FLOAT(size, "Size", 0.0f);
  100. SOCKET_VECTOR(axisu, "Axis U", make_float3(0.0f, 0.0f, 0.0f));
  101. SOCKET_FLOAT(sizeu, "Size U", 1.0f);
  102. SOCKET_VECTOR(axisv, "Axis V", make_float3(0.0f, 0.0f, 0.0f));
  103. SOCKET_FLOAT(sizev, "Size V", 1.0f);
  104. SOCKET_BOOLEAN(round, "Round", false);
  105. SOCKET_INT(map_resolution, "Map Resolution", 0);
  106. SOCKET_FLOAT(spot_angle, "Spot Angle", M_PI_4_F);
  107. SOCKET_FLOAT(spot_smooth, "Spot Smooth", 0.0f);
  108. SOCKET_TRANSFORM(tfm, "Transform", transform_identity());
  109. SOCKET_BOOLEAN(cast_shadow, "Cast Shadow", true);
  110. SOCKET_BOOLEAN(use_mis, "Use Mis", false);
  111. SOCKET_BOOLEAN(use_diffuse, "Use Diffuse", true);
  112. SOCKET_BOOLEAN(use_glossy, "Use Glossy", true);
  113. SOCKET_BOOLEAN(use_transmission, "Use Transmission", true);
  114. SOCKET_BOOLEAN(use_scatter, "Use Scatter", true);
  115. SOCKET_INT(samples, "Samples", 1);
  116. SOCKET_INT(max_bounces, "Max Bounces", 1024);
  117. SOCKET_UINT(random_id, "Random ID", 0);
  118. SOCKET_BOOLEAN(is_portal, "Is Portal", false);
  119. SOCKET_BOOLEAN(is_enabled, "Is Enabled", true);
  120. SOCKET_NODE(shader, "Shader", &Shader::node_type);
  121. return type;
  122. }
  123. Light::Light() : Node(node_type)
  124. {
  125. }
  126. void Light::tag_update(Scene *scene)
  127. {
  128. scene->light_manager->need_update = true;
  129. }
  130. bool Light::has_contribution(Scene *scene)
  131. {
  132. if (strength == make_float3(0.0f, 0.0f, 0.0f)) {
  133. return false;
  134. }
  135. if (is_portal) {
  136. return false;
  137. }
  138. if (type == LIGHT_BACKGROUND) {
  139. return true;
  140. }
  141. return (shader) ? shader->has_surface_emission : scene->default_light->has_surface_emission;
  142. }
  143. /* Light Manager */
  144. LightManager::LightManager()
  145. {
  146. need_update = true;
  147. use_light_visibility = false;
  148. }
  149. LightManager::~LightManager()
  150. {
  151. foreach (IESSlot *slot, ies_slots) {
  152. delete slot;
  153. }
  154. }
  155. bool LightManager::has_background_light(Scene *scene)
  156. {
  157. foreach (Light *light, scene->lights) {
  158. if (light->type == LIGHT_BACKGROUND && light->is_enabled) {
  159. return true;
  160. }
  161. }
  162. return false;
  163. }
  164. void LightManager::disable_ineffective_light(Scene *scene)
  165. {
  166. /* Make all lights enabled by default, and perform some preliminary checks
  167. * needed for finer-tuning of settings (for example, check whether we've
  168. * got portals or not).
  169. */
  170. bool has_portal = false, has_background = false;
  171. foreach (Light *light, scene->lights) {
  172. light->is_enabled = light->has_contribution(scene);
  173. has_portal |= light->is_portal;
  174. has_background |= light->type == LIGHT_BACKGROUND;
  175. }
  176. if (has_background) {
  177. /* Ignore background light if:
  178. * - If unsupported on a device
  179. * - If we don't need it (no HDRs etc.)
  180. */
  181. Shader *shader = (scene->background->shader) ? scene->background->shader :
  182. scene->default_background;
  183. bool disable_mis = !(has_portal || shader->has_surface_spatial_varying);
  184. if (disable_mis) {
  185. VLOG(1) << "Background MIS has been disabled.\n";
  186. foreach (Light *light, scene->lights) {
  187. if (light->type == LIGHT_BACKGROUND) {
  188. light->is_enabled = false;
  189. }
  190. }
  191. }
  192. }
  193. }
  194. bool LightManager::object_usable_as_light(Object *object)
  195. {
  196. Mesh *mesh = object->mesh;
  197. /* Skip objects with NaNs */
  198. if (!object->bounds.valid()) {
  199. return false;
  200. }
  201. /* Skip if we are not visible for BSDFs. */
  202. if (!(object->visibility & (PATH_RAY_DIFFUSE | PATH_RAY_GLOSSY | PATH_RAY_TRANSMIT))) {
  203. return false;
  204. }
  205. /* Skip if we have no emission shaders. */
  206. /* TODO(sergey): Ideally we want to avoid such duplicated loop, since it'll
  207. * iterate all mesh shaders twice (when counting and when calculating
  208. * triangle area.
  209. */
  210. foreach (const Shader *shader, mesh->used_shaders) {
  211. if (shader->use_mis && shader->has_surface_emission) {
  212. return true;
  213. }
  214. }
  215. return false;
  216. }
  217. void LightManager::device_update_distribution(Device *,
  218. DeviceScene *dscene,
  219. Scene *scene,
  220. Progress &progress)
  221. {
  222. progress.set_status("Updating Lights", "Computing distribution");
  223. /* count */
  224. size_t num_lights = 0;
  225. size_t num_portals = 0;
  226. size_t num_background_lights = 0;
  227. size_t num_triangles = 0;
  228. bool background_mis = false;
  229. foreach (Light *light, scene->lights) {
  230. if (light->is_enabled) {
  231. num_lights++;
  232. }
  233. if (light->is_portal) {
  234. num_portals++;
  235. }
  236. }
  237. foreach (Object *object, scene->objects) {
  238. if (progress.get_cancel())
  239. return;
  240. if (!object_usable_as_light(object)) {
  241. continue;
  242. }
  243. /* Count triangles. */
  244. Mesh *mesh = object->mesh;
  245. size_t mesh_num_triangles = mesh->num_triangles();
  246. for (size_t i = 0; i < mesh_num_triangles; i++) {
  247. int shader_index = mesh->shader[i];
  248. Shader *shader = (shader_index < mesh->used_shaders.size()) ?
  249. mesh->used_shaders[shader_index] :
  250. scene->default_surface;
  251. if (shader->use_mis && shader->has_surface_emission) {
  252. num_triangles++;
  253. }
  254. }
  255. }
  256. size_t num_distribution = num_triangles + num_lights;
  257. VLOG(1) << "Total " << num_distribution << " of light distribution primitives.";
  258. /* emission area */
  259. KernelLightDistribution *distribution = dscene->light_distribution.alloc(num_distribution + 1);
  260. float totarea = 0.0f;
  261. /* triangles */
  262. size_t offset = 0;
  263. int j = 0;
  264. foreach (Object *object, scene->objects) {
  265. if (progress.get_cancel())
  266. return;
  267. if (!object_usable_as_light(object)) {
  268. j++;
  269. continue;
  270. }
  271. /* Sum area. */
  272. Mesh *mesh = object->mesh;
  273. bool transform_applied = mesh->transform_applied;
  274. Transform tfm = object->tfm;
  275. int object_id = j;
  276. int shader_flag = 0;
  277. if (!(object->visibility & PATH_RAY_DIFFUSE)) {
  278. shader_flag |= SHADER_EXCLUDE_DIFFUSE;
  279. use_light_visibility = true;
  280. }
  281. if (!(object->visibility & PATH_RAY_GLOSSY)) {
  282. shader_flag |= SHADER_EXCLUDE_GLOSSY;
  283. use_light_visibility = true;
  284. }
  285. if (!(object->visibility & PATH_RAY_TRANSMIT)) {
  286. shader_flag |= SHADER_EXCLUDE_TRANSMIT;
  287. use_light_visibility = true;
  288. }
  289. if (!(object->visibility & PATH_RAY_VOLUME_SCATTER)) {
  290. shader_flag |= SHADER_EXCLUDE_SCATTER;
  291. use_light_visibility = true;
  292. }
  293. size_t mesh_num_triangles = mesh->num_triangles();
  294. for (size_t i = 0; i < mesh_num_triangles; i++) {
  295. int shader_index = mesh->shader[i];
  296. Shader *shader = (shader_index < mesh->used_shaders.size()) ?
  297. mesh->used_shaders[shader_index] :
  298. scene->default_surface;
  299. if (shader->use_mis && shader->has_surface_emission) {
  300. distribution[offset].totarea = totarea;
  301. distribution[offset].prim = i + mesh->tri_offset;
  302. distribution[offset].mesh_light.shader_flag = shader_flag;
  303. distribution[offset].mesh_light.object_id = object_id;
  304. offset++;
  305. Mesh::Triangle t = mesh->get_triangle(i);
  306. if (!t.valid(&mesh->verts[0])) {
  307. continue;
  308. }
  309. float3 p1 = mesh->verts[t.v[0]];
  310. float3 p2 = mesh->verts[t.v[1]];
  311. float3 p3 = mesh->verts[t.v[2]];
  312. if (!transform_applied) {
  313. p1 = transform_point(&tfm, p1);
  314. p2 = transform_point(&tfm, p2);
  315. p3 = transform_point(&tfm, p3);
  316. }
  317. totarea += triangle_area(p1, p2, p3);
  318. }
  319. }
  320. j++;
  321. }
  322. float trianglearea = totarea;
  323. /* point lights */
  324. float lightarea = (totarea > 0.0f) ? totarea / num_lights : 1.0f;
  325. bool use_lamp_mis = false;
  326. int light_index = 0;
  327. foreach (Light *light, scene->lights) {
  328. if (!light->is_enabled)
  329. continue;
  330. distribution[offset].totarea = totarea;
  331. distribution[offset].prim = ~light_index;
  332. distribution[offset].lamp.pad = 1.0f;
  333. distribution[offset].lamp.size = light->size;
  334. totarea += lightarea;
  335. if (light->type == LIGHT_DISTANT) {
  336. use_lamp_mis |= (light->angle > 0.0f && light->use_mis);
  337. }
  338. else if (light->type == LIGHT_POINT || light->type == LIGHT_SPOT) {
  339. use_lamp_mis |= (light->size > 0.0f && light->use_mis);
  340. }
  341. else if (light->type == LIGHT_AREA) {
  342. use_lamp_mis |= light->use_mis;
  343. }
  344. else if (light->type == LIGHT_BACKGROUND) {
  345. num_background_lights++;
  346. background_mis |= light->use_mis;
  347. }
  348. light_index++;
  349. offset++;
  350. }
  351. /* normalize cumulative distribution functions */
  352. distribution[num_distribution].totarea = totarea;
  353. distribution[num_distribution].prim = 0.0f;
  354. distribution[num_distribution].lamp.pad = 0.0f;
  355. distribution[num_distribution].lamp.size = 0.0f;
  356. if (totarea > 0.0f) {
  357. for (size_t i = 0; i < num_distribution; i++)
  358. distribution[i].totarea /= totarea;
  359. distribution[num_distribution].totarea = 1.0f;
  360. }
  361. if (progress.get_cancel())
  362. return;
  363. /* update device */
  364. KernelIntegrator *kintegrator = &dscene->data.integrator;
  365. KernelFilm *kfilm = &dscene->data.film;
  366. kintegrator->use_direct_light = (totarea > 0.0f);
  367. if (kintegrator->use_direct_light) {
  368. /* number of emissives */
  369. kintegrator->num_distribution = num_distribution;
  370. /* precompute pdfs */
  371. kintegrator->pdf_triangles = 0.0f;
  372. kintegrator->pdf_lights = 0.0f;
  373. /* sample one, with 0.5 probability of light or triangle */
  374. kintegrator->num_all_lights = num_lights;
  375. if (trianglearea > 0.0f) {
  376. kintegrator->pdf_triangles = 1.0f / trianglearea;
  377. if (num_lights)
  378. kintegrator->pdf_triangles *= 0.5f;
  379. }
  380. if (num_lights) {
  381. kintegrator->pdf_lights = 1.0f / num_lights;
  382. if (trianglearea > 0.0f)
  383. kintegrator->pdf_lights *= 0.5f;
  384. }
  385. kintegrator->use_lamp_mis = use_lamp_mis;
  386. /* bit of an ugly hack to compensate for emitting triangles influencing
  387. * amount of samples we get for this pass */
  388. kfilm->pass_shadow_scale = 1.0f;
  389. if (kintegrator->pdf_triangles != 0.0f)
  390. kfilm->pass_shadow_scale *= 0.5f;
  391. if (num_background_lights < num_lights)
  392. kfilm->pass_shadow_scale *= (float)(num_lights - num_background_lights) / (float)num_lights;
  393. /* CDF */
  394. dscene->light_distribution.copy_to_device();
  395. /* Portals */
  396. if (num_portals > 0) {
  397. kintegrator->portal_offset = light_index;
  398. kintegrator->num_portals = num_portals;
  399. kintegrator->portal_pdf = background_mis ? 0.5f : 1.0f;
  400. }
  401. else {
  402. kintegrator->num_portals = 0;
  403. kintegrator->portal_offset = 0;
  404. kintegrator->portal_pdf = 0.0f;
  405. }
  406. }
  407. else {
  408. dscene->light_distribution.free();
  409. kintegrator->num_distribution = 0;
  410. kintegrator->num_all_lights = 0;
  411. kintegrator->pdf_triangles = 0.0f;
  412. kintegrator->pdf_lights = 0.0f;
  413. kintegrator->use_lamp_mis = false;
  414. kintegrator->num_portals = 0;
  415. kintegrator->portal_offset = 0;
  416. kintegrator->portal_pdf = 0.0f;
  417. kfilm->pass_shadow_scale = 1.0f;
  418. }
  419. }
  420. static void background_cdf(
  421. int start, int end, int res_x, int res_y, const vector<float3> *pixels, float2 *cond_cdf)
  422. {
  423. int cdf_width = res_x + 1;
  424. /* Conditional CDFs (rows, U direction). */
  425. for (int i = start; i < end; i++) {
  426. float sin_theta = sinf(M_PI_F * (i + 0.5f) / res_y);
  427. float3 env_color = (*pixels)[i * res_x];
  428. float ave_luminance = average(env_color);
  429. cond_cdf[i * cdf_width].x = ave_luminance * sin_theta;
  430. cond_cdf[i * cdf_width].y = 0.0f;
  431. for (int j = 1; j < res_x; j++) {
  432. env_color = (*pixels)[i * res_x + j];
  433. ave_luminance = average(env_color);
  434. cond_cdf[i * cdf_width + j].x = ave_luminance * sin_theta;
  435. cond_cdf[i * cdf_width + j].y = cond_cdf[i * cdf_width + j - 1].y +
  436. cond_cdf[i * cdf_width + j - 1].x / res_x;
  437. }
  438. float cdf_total = cond_cdf[i * cdf_width + res_x - 1].y +
  439. cond_cdf[i * cdf_width + res_x - 1].x / res_x;
  440. float cdf_total_inv = 1.0f / cdf_total;
  441. /* stuff the total into the brightness value for the last entry, because
  442. * we are going to normalize the CDFs to 0.0 to 1.0 afterwards */
  443. cond_cdf[i * cdf_width + res_x].x = cdf_total;
  444. if (cdf_total > 0.0f)
  445. for (int j = 1; j < res_x; j++)
  446. cond_cdf[i * cdf_width + j].y *= cdf_total_inv;
  447. cond_cdf[i * cdf_width + res_x].y = 1.0f;
  448. }
  449. }
  450. void LightManager::device_update_background(Device *device,
  451. DeviceScene *dscene,
  452. Scene *scene,
  453. Progress &progress)
  454. {
  455. KernelIntegrator *kintegrator = &dscene->data.integrator;
  456. Light *background_light = NULL;
  457. /* find background light */
  458. foreach (Light *light, scene->lights) {
  459. if (light->type == LIGHT_BACKGROUND) {
  460. background_light = light;
  461. break;
  462. }
  463. }
  464. /* no background light found, signal renderer to skip sampling */
  465. if (!background_light || !background_light->is_enabled) {
  466. kintegrator->pdf_background_res_x = 0;
  467. kintegrator->pdf_background_res_y = 0;
  468. return;
  469. }
  470. progress.set_status("Updating Lights", "Importance map");
  471. assert(kintegrator->use_direct_light);
  472. /* get the resolution from the light's size (we stuff it in there) */
  473. int2 res = make_int2(background_light->map_resolution, background_light->map_resolution / 2);
  474. /* If the resolution isn't set manually, try to find an environment texture. */
  475. if (res.x == 0) {
  476. Shader *shader = (scene->background->shader) ? scene->background->shader :
  477. scene->default_background;
  478. foreach (ShaderNode *node, shader->graph->nodes) {
  479. if (node->type == EnvironmentTextureNode::node_type) {
  480. EnvironmentTextureNode *env = (EnvironmentTextureNode *)node;
  481. ImageMetaData metadata;
  482. if (env->image_manager && env->image_manager->get_image_metadata(env->slot, metadata)) {
  483. res.x = max(res.x, metadata.width);
  484. res.y = max(res.y, metadata.height);
  485. }
  486. }
  487. }
  488. if (res.x > 0 && res.y > 0) {
  489. VLOG(2) << "Automatically set World MIS resolution to " << res.x << " by " << res.y << "\n";
  490. }
  491. }
  492. /* If it's still unknown, just use the default. */
  493. if (res.x == 0 || res.y == 0) {
  494. res = make_int2(1024, 512);
  495. VLOG(2) << "Setting World MIS resolution to default\n";
  496. }
  497. kintegrator->pdf_background_res_x = res.x;
  498. kintegrator->pdf_background_res_y = res.y;
  499. vector<float3> pixels;
  500. shade_background_pixels(device, dscene, res.x, res.y, pixels, progress);
  501. if (progress.get_cancel())
  502. return;
  503. /* build row distributions and column distribution for the infinite area environment light */
  504. int cdf_width = res.x + 1;
  505. float2 *marg_cdf = dscene->light_background_marginal_cdf.alloc(res.y + 1);
  506. float2 *cond_cdf = dscene->light_background_conditional_cdf.alloc(cdf_width * res.y);
  507. double time_start = time_dt();
  508. if (max(res.x, res.y) < 512) {
  509. /* Small enough resolution, faster to do single-threaded. */
  510. background_cdf(0, res.y, res.x, res.y, &pixels, cond_cdf);
  511. }
  512. else {
  513. /* Threaded evaluation for large resolution. */
  514. const int num_blocks = TaskScheduler::num_threads();
  515. const int chunk_size = res.y / num_blocks;
  516. int start_row = 0;
  517. TaskPool pool;
  518. for (int i = 0; i < num_blocks; ++i) {
  519. const int current_chunk_size = (i != num_blocks - 1) ? chunk_size : (res.y - i * chunk_size);
  520. pool.push(function_bind(&background_cdf,
  521. start_row,
  522. start_row + current_chunk_size,
  523. res.x,
  524. res.y,
  525. &pixels,
  526. cond_cdf));
  527. start_row += current_chunk_size;
  528. }
  529. pool.wait_work();
  530. }
  531. /* marginal CDFs (column, V direction, sum of rows) */
  532. marg_cdf[0].x = cond_cdf[res.x].x;
  533. marg_cdf[0].y = 0.0f;
  534. for (int i = 1; i < res.y; i++) {
  535. marg_cdf[i].x = cond_cdf[i * cdf_width + res.x].x;
  536. marg_cdf[i].y = marg_cdf[i - 1].y + marg_cdf[i - 1].x / res.y;
  537. }
  538. float cdf_total = marg_cdf[res.y - 1].y + marg_cdf[res.y - 1].x / res.y;
  539. marg_cdf[res.y].x = cdf_total;
  540. if (cdf_total > 0.0f)
  541. for (int i = 1; i < res.y; i++)
  542. marg_cdf[i].y /= cdf_total;
  543. marg_cdf[res.y].y = 1.0f;
  544. VLOG(2) << "Background MIS build time " << time_dt() - time_start << "\n";
  545. /* update device */
  546. dscene->light_background_marginal_cdf.copy_to_device();
  547. dscene->light_background_conditional_cdf.copy_to_device();
  548. }
  549. void LightManager::device_update_points(Device *, DeviceScene *dscene, Scene *scene)
  550. {
  551. int num_scene_lights = scene->lights.size();
  552. int num_lights = 0;
  553. foreach (Light *light, scene->lights) {
  554. if (light->is_enabled || light->is_portal) {
  555. num_lights++;
  556. }
  557. }
  558. KernelLight *klights = dscene->lights.alloc(num_lights);
  559. if (num_lights == 0) {
  560. VLOG(1) << "No effective light, ignoring points update.";
  561. return;
  562. }
  563. int light_index = 0;
  564. foreach (Light *light, scene->lights) {
  565. if (!light->is_enabled) {
  566. continue;
  567. }
  568. float3 co = light->co;
  569. Shader *shader = (light->shader) ? light->shader : scene->default_light;
  570. int shader_id = scene->shader_manager->get_shader_id(shader);
  571. int max_bounces = light->max_bounces;
  572. float random = (float)light->random_id * (1.0f / (float)0xFFFFFFFF);
  573. if (!light->cast_shadow)
  574. shader_id &= ~SHADER_CAST_SHADOW;
  575. if (!light->use_diffuse) {
  576. shader_id |= SHADER_EXCLUDE_DIFFUSE;
  577. use_light_visibility = true;
  578. }
  579. if (!light->use_glossy) {
  580. shader_id |= SHADER_EXCLUDE_GLOSSY;
  581. use_light_visibility = true;
  582. }
  583. if (!light->use_transmission) {
  584. shader_id |= SHADER_EXCLUDE_TRANSMIT;
  585. use_light_visibility = true;
  586. }
  587. if (!light->use_scatter) {
  588. shader_id |= SHADER_EXCLUDE_SCATTER;
  589. use_light_visibility = true;
  590. }
  591. klights[light_index].type = light->type;
  592. klights[light_index].samples = light->samples;
  593. klights[light_index].strength[0] = light->strength.x;
  594. klights[light_index].strength[1] = light->strength.y;
  595. klights[light_index].strength[2] = light->strength.z;
  596. if (light->type == LIGHT_POINT) {
  597. shader_id &= ~SHADER_AREA_LIGHT;
  598. float radius = light->size;
  599. float invarea = (radius > 0.0f) ? 1.0f / (M_PI_F * radius * radius) : 1.0f;
  600. if (light->use_mis && radius > 0.0f)
  601. shader_id |= SHADER_USE_MIS;
  602. klights[light_index].co[0] = co.x;
  603. klights[light_index].co[1] = co.y;
  604. klights[light_index].co[2] = co.z;
  605. klights[light_index].spot.radius = radius;
  606. klights[light_index].spot.invarea = invarea;
  607. }
  608. else if (light->type == LIGHT_DISTANT) {
  609. shader_id &= ~SHADER_AREA_LIGHT;
  610. float angle = light->angle / 2.0f;
  611. float radius = tanf(angle);
  612. float cosangle = cosf(angle);
  613. float area = M_PI_F * radius * radius;
  614. float invarea = (area > 0.0f) ? 1.0f / area : 1.0f;
  615. float3 dir = light->dir;
  616. dir = safe_normalize(dir);
  617. if (light->use_mis && area > 0.0f)
  618. shader_id |= SHADER_USE_MIS;
  619. klights[light_index].co[0] = dir.x;
  620. klights[light_index].co[1] = dir.y;
  621. klights[light_index].co[2] = dir.z;
  622. klights[light_index].distant.invarea = invarea;
  623. klights[light_index].distant.radius = radius;
  624. klights[light_index].distant.cosangle = cosangle;
  625. }
  626. else if (light->type == LIGHT_BACKGROUND) {
  627. uint visibility = scene->background->visibility;
  628. shader_id &= ~SHADER_AREA_LIGHT;
  629. shader_id |= SHADER_USE_MIS;
  630. if (!(visibility & PATH_RAY_DIFFUSE)) {
  631. shader_id |= SHADER_EXCLUDE_DIFFUSE;
  632. use_light_visibility = true;
  633. }
  634. if (!(visibility & PATH_RAY_GLOSSY)) {
  635. shader_id |= SHADER_EXCLUDE_GLOSSY;
  636. use_light_visibility = true;
  637. }
  638. if (!(visibility & PATH_RAY_TRANSMIT)) {
  639. shader_id |= SHADER_EXCLUDE_TRANSMIT;
  640. use_light_visibility = true;
  641. }
  642. if (!(visibility & PATH_RAY_VOLUME_SCATTER)) {
  643. shader_id |= SHADER_EXCLUDE_SCATTER;
  644. use_light_visibility = true;
  645. }
  646. }
  647. else if (light->type == LIGHT_AREA) {
  648. float3 axisu = light->axisu * (light->sizeu * light->size);
  649. float3 axisv = light->axisv * (light->sizev * light->size);
  650. float area = len(axisu) * len(axisv);
  651. if (light->round) {
  652. area *= -M_PI_4_F;
  653. }
  654. float invarea = (area != 0.0f) ? 1.0f / area : 1.0f;
  655. float3 dir = light->dir;
  656. dir = safe_normalize(dir);
  657. if (light->use_mis && area != 0.0f)
  658. shader_id |= SHADER_USE_MIS;
  659. klights[light_index].co[0] = co.x;
  660. klights[light_index].co[1] = co.y;
  661. klights[light_index].co[2] = co.z;
  662. klights[light_index].area.axisu[0] = axisu.x;
  663. klights[light_index].area.axisu[1] = axisu.y;
  664. klights[light_index].area.axisu[2] = axisu.z;
  665. klights[light_index].area.axisv[0] = axisv.x;
  666. klights[light_index].area.axisv[1] = axisv.y;
  667. klights[light_index].area.axisv[2] = axisv.z;
  668. klights[light_index].area.invarea = invarea;
  669. klights[light_index].area.dir[0] = dir.x;
  670. klights[light_index].area.dir[1] = dir.y;
  671. klights[light_index].area.dir[2] = dir.z;
  672. }
  673. else if (light->type == LIGHT_SPOT) {
  674. shader_id &= ~SHADER_AREA_LIGHT;
  675. float radius = light->size;
  676. float invarea = (radius > 0.0f) ? 1.0f / (M_PI_F * radius * radius) : 1.0f;
  677. float spot_angle = cosf(light->spot_angle * 0.5f);
  678. float spot_smooth = (1.0f - spot_angle) * light->spot_smooth;
  679. float3 dir = light->dir;
  680. dir = safe_normalize(dir);
  681. if (light->use_mis && radius > 0.0f)
  682. shader_id |= SHADER_USE_MIS;
  683. klights[light_index].co[0] = co.x;
  684. klights[light_index].co[1] = co.y;
  685. klights[light_index].co[2] = co.z;
  686. klights[light_index].spot.radius = radius;
  687. klights[light_index].spot.invarea = invarea;
  688. klights[light_index].spot.spot_angle = spot_angle;
  689. klights[light_index].spot.spot_smooth = spot_smooth;
  690. klights[light_index].spot.dir[0] = dir.x;
  691. klights[light_index].spot.dir[1] = dir.y;
  692. klights[light_index].spot.dir[2] = dir.z;
  693. }
  694. klights[light_index].shader_id = shader_id;
  695. klights[light_index].max_bounces = max_bounces;
  696. klights[light_index].random = random;
  697. klights[light_index].tfm = light->tfm;
  698. klights[light_index].itfm = transform_inverse(light->tfm);
  699. light_index++;
  700. }
  701. /* TODO(sergey): Consider moving portals update to their own function
  702. * keeping this one more manageable.
  703. */
  704. foreach (Light *light, scene->lights) {
  705. if (!light->is_portal)
  706. continue;
  707. assert(light->type == LIGHT_AREA);
  708. float3 co = light->co;
  709. float3 axisu = light->axisu * (light->sizeu * light->size);
  710. float3 axisv = light->axisv * (light->sizev * light->size);
  711. float area = len(axisu) * len(axisv);
  712. if (light->round) {
  713. area *= -M_PI_4_F;
  714. }
  715. float invarea = (area != 0.0f) ? 1.0f / area : 1.0f;
  716. float3 dir = light->dir;
  717. dir = safe_normalize(dir);
  718. klights[light_index].co[0] = co.x;
  719. klights[light_index].co[1] = co.y;
  720. klights[light_index].co[2] = co.z;
  721. klights[light_index].area.axisu[0] = axisu.x;
  722. klights[light_index].area.axisu[1] = axisu.y;
  723. klights[light_index].area.axisu[2] = axisu.z;
  724. klights[light_index].area.axisv[0] = axisv.x;
  725. klights[light_index].area.axisv[1] = axisv.y;
  726. klights[light_index].area.axisv[2] = axisv.z;
  727. klights[light_index].area.invarea = invarea;
  728. klights[light_index].area.dir[0] = dir.x;
  729. klights[light_index].area.dir[1] = dir.y;
  730. klights[light_index].area.dir[2] = dir.z;
  731. klights[light_index].tfm = light->tfm;
  732. klights[light_index].itfm = transform_inverse(light->tfm);
  733. light_index++;
  734. }
  735. VLOG(1) << "Number of lights sent to the device: " << light_index;
  736. VLOG(1) << "Number of lights without contribution: " << num_scene_lights - light_index;
  737. dscene->lights.copy_to_device();
  738. }
  739. void LightManager::device_update(Device *device,
  740. DeviceScene *dscene,
  741. Scene *scene,
  742. Progress &progress)
  743. {
  744. if (!need_update)
  745. return;
  746. VLOG(1) << "Total " << scene->lights.size() << " lights.";
  747. device_free(device, dscene);
  748. use_light_visibility = false;
  749. disable_ineffective_light(scene);
  750. device_update_points(device, dscene, scene);
  751. if (progress.get_cancel())
  752. return;
  753. device_update_distribution(device, dscene, scene, progress);
  754. if (progress.get_cancel())
  755. return;
  756. device_update_background(device, dscene, scene, progress);
  757. if (progress.get_cancel())
  758. return;
  759. device_update_ies(dscene);
  760. if (progress.get_cancel())
  761. return;
  762. if (use_light_visibility != scene->film->use_light_visibility) {
  763. scene->film->use_light_visibility = use_light_visibility;
  764. scene->film->tag_update(scene);
  765. }
  766. need_update = false;
  767. }
  768. void LightManager::device_free(Device *, DeviceScene *dscene)
  769. {
  770. dscene->light_distribution.free();
  771. dscene->lights.free();
  772. dscene->light_background_marginal_cdf.free();
  773. dscene->light_background_conditional_cdf.free();
  774. dscene->ies_lights.free();
  775. }
  776. void LightManager::tag_update(Scene * /*scene*/)
  777. {
  778. need_update = true;
  779. }
  780. int LightManager::add_ies_from_file(ustring filename)
  781. {
  782. string content;
  783. /* If the file can't be opened, call with an empty line */
  784. if (filename.empty() || !path_read_text(filename.c_str(), content)) {
  785. content = "\n";
  786. }
  787. return add_ies(ustring(content));
  788. }
  789. int LightManager::add_ies(ustring content)
  790. {
  791. uint hash = hash_string(content.c_str());
  792. thread_scoped_lock ies_lock(ies_mutex);
  793. /* Check whether this IES already has a slot. */
  794. size_t slot;
  795. for (slot = 0; slot < ies_slots.size(); slot++) {
  796. if (ies_slots[slot]->hash == hash) {
  797. ies_slots[slot]->users++;
  798. return slot;
  799. }
  800. }
  801. /* Try to find an empty slot for the new IES. */
  802. for (slot = 0; slot < ies_slots.size(); slot++) {
  803. if (ies_slots[slot]->users == 0 && ies_slots[slot]->hash == 0) {
  804. break;
  805. }
  806. }
  807. /* If there's no free slot, add one. */
  808. if (slot == ies_slots.size()) {
  809. ies_slots.push_back(new IESSlot());
  810. }
  811. ies_slots[slot]->ies.load(content);
  812. ies_slots[slot]->users = 1;
  813. ies_slots[slot]->hash = hash;
  814. need_update = true;
  815. return slot;
  816. }
  817. void LightManager::remove_ies(int slot)
  818. {
  819. thread_scoped_lock ies_lock(ies_mutex);
  820. if (slot < 0 || slot >= ies_slots.size()) {
  821. assert(false);
  822. return;
  823. }
  824. assert(ies_slots[slot]->users > 0);
  825. ies_slots[slot]->users--;
  826. /* If the slot has no more users, update the device to remove it. */
  827. need_update |= (ies_slots[slot]->users == 0);
  828. }
  829. void LightManager::device_update_ies(DeviceScene *dscene)
  830. {
  831. /* Clear empty slots. */
  832. foreach (IESSlot *slot, ies_slots) {
  833. if (slot->users == 0) {
  834. slot->hash = 0;
  835. slot->ies.clear();
  836. }
  837. }
  838. /* Shrink the slot table by removing empty slots at the end. */
  839. int slot_end;
  840. for (slot_end = ies_slots.size(); slot_end; slot_end--) {
  841. if (ies_slots[slot_end - 1]->users > 0) {
  842. /* If the preceding slot has users, we found the new end of the table. */
  843. break;
  844. }
  845. else {
  846. /* The slot will be past the new end of the table, so free it. */
  847. delete ies_slots[slot_end - 1];
  848. }
  849. }
  850. ies_slots.resize(slot_end);
  851. if (ies_slots.size() > 0) {
  852. int packed_size = 0;
  853. foreach (IESSlot *slot, ies_slots) {
  854. packed_size += slot->ies.packed_size();
  855. }
  856. /* ies_lights starts with an offset table that contains the offset of every slot,
  857. * or -1 if the slot is invalid.
  858. * Following that table, the packed valid IES lights are stored. */
  859. float *data = dscene->ies_lights.alloc(ies_slots.size() + packed_size);
  860. int offset = ies_slots.size();
  861. for (int i = 0; i < ies_slots.size(); i++) {
  862. int size = ies_slots[i]->ies.packed_size();
  863. if (size > 0) {
  864. data[i] = __int_as_float(offset);
  865. ies_slots[i]->ies.pack(data + offset);
  866. offset += size;
  867. }
  868. else {
  869. data[i] = __int_as_float(-1);
  870. }
  871. }
  872. dscene->ies_lights.copy_to_device();
  873. }
  874. }
  875. CCL_NAMESPACE_END