image.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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/image.h"
  17. #include "device/device.h"
  18. #include "render/colorspace.h"
  19. #include "render/scene.h"
  20. #include "render/stats.h"
  21. #include "util/util_foreach.h"
  22. #include "util/util_image_impl.h"
  23. #include "util/util_logging.h"
  24. #include "util/util_path.h"
  25. #include "util/util_progress.h"
  26. #include "util/util_texture.h"
  27. #include "util/util_unique_ptr.h"
  28. #ifdef WITH_OSL
  29. # include <OSL/oslexec.h>
  30. #endif
  31. CCL_NAMESPACE_BEGIN
  32. namespace {
  33. /* Some helpers to silence warning in templated function. */
  34. bool isfinite(uchar /*value*/)
  35. {
  36. return true;
  37. }
  38. bool isfinite(half /*value*/)
  39. {
  40. return true;
  41. }
  42. bool isfinite(uint16_t /*value*/)
  43. {
  44. return true;
  45. }
  46. /* The lower three bits of a device texture slot number indicate its type.
  47. * These functions convert the slot ids from ImageManager "images" ones
  48. * to device ones and vice verse.
  49. */
  50. int type_index_to_flattened_slot(int slot, ImageDataType type)
  51. {
  52. return (slot << IMAGE_DATA_TYPE_SHIFT) | (type);
  53. }
  54. int flattened_slot_to_type_index(int flat_slot, ImageDataType *type)
  55. {
  56. *type = (ImageDataType)(flat_slot & IMAGE_DATA_TYPE_MASK);
  57. return flat_slot >> IMAGE_DATA_TYPE_SHIFT;
  58. }
  59. const char *name_from_type(ImageDataType type)
  60. {
  61. switch (type) {
  62. case IMAGE_DATA_TYPE_FLOAT4:
  63. return "float4";
  64. case IMAGE_DATA_TYPE_BYTE4:
  65. return "byte4";
  66. case IMAGE_DATA_TYPE_HALF4:
  67. return "half4";
  68. case IMAGE_DATA_TYPE_FLOAT:
  69. return "float";
  70. case IMAGE_DATA_TYPE_BYTE:
  71. return "byte";
  72. case IMAGE_DATA_TYPE_HALF:
  73. return "half";
  74. case IMAGE_DATA_TYPE_USHORT4:
  75. return "ushort4";
  76. case IMAGE_DATA_TYPE_USHORT:
  77. return "ushort";
  78. case IMAGE_DATA_NUM_TYPES:
  79. assert(!"System enumerator type, should never be used");
  80. return "";
  81. }
  82. assert(!"Unhandled image data type");
  83. return "";
  84. }
  85. } // namespace
  86. ImageManager::ImageManager(const DeviceInfo &info)
  87. {
  88. need_update = true;
  89. osl_texture_system = NULL;
  90. animation_frame = 0;
  91. /* Set image limits */
  92. max_num_images = TEX_NUM_MAX;
  93. has_half_images = info.has_half_images;
  94. for (size_t type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
  95. tex_num_images[type] = 0;
  96. }
  97. }
  98. ImageManager::~ImageManager()
  99. {
  100. for (size_t type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
  101. for (size_t slot = 0; slot < images[type].size(); slot++)
  102. assert(!images[type][slot]);
  103. }
  104. }
  105. void ImageManager::set_osl_texture_system(void *texture_system)
  106. {
  107. osl_texture_system = texture_system;
  108. }
  109. bool ImageManager::set_animation_frame_update(int frame)
  110. {
  111. if (frame != animation_frame) {
  112. animation_frame = frame;
  113. for (size_t type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
  114. for (size_t slot = 0; slot < images[type].size(); slot++) {
  115. if (images[type][slot] && images[type][slot]->animated)
  116. return true;
  117. }
  118. }
  119. }
  120. return false;
  121. }
  122. device_memory *ImageManager::image_memory(int flat_slot)
  123. {
  124. ImageDataType type;
  125. int slot = flattened_slot_to_type_index(flat_slot, &type);
  126. Image *img = images[type][slot];
  127. return img->mem;
  128. }
  129. bool ImageManager::get_image_metadata(int flat_slot, ImageMetaData &metadata)
  130. {
  131. if (flat_slot == -1) {
  132. return false;
  133. }
  134. ImageDataType type;
  135. int slot = flattened_slot_to_type_index(flat_slot, &type);
  136. Image *img = images[type][slot];
  137. if (img) {
  138. metadata = img->metadata;
  139. return true;
  140. }
  141. return false;
  142. }
  143. void ImageManager::metadata_detect_colorspace(ImageMetaData &metadata, const char *file_format)
  144. {
  145. /* Convert used specified color spaces to one we know how to handle. */
  146. metadata.colorspace = ColorSpaceManager::detect_known_colorspace(
  147. metadata.colorspace, file_format, metadata.is_float || metadata.is_half);
  148. if (metadata.colorspace == u_colorspace_raw) {
  149. /* Nothing to do. */
  150. }
  151. else if (metadata.colorspace == u_colorspace_srgb) {
  152. /* Keep sRGB colorspace stored as sRGB, to save memory and/or loading time
  153. * for the common case of 8bit sRGB images like PNG. */
  154. metadata.compress_as_srgb = true;
  155. }
  156. else {
  157. /* Always compress non-raw 8bit images as scene linear + sRGB, as a
  158. * heuristic to keep memory usage the same without too much data loss
  159. * due to quantization in common cases. */
  160. metadata.compress_as_srgb = (metadata.type == IMAGE_DATA_TYPE_BYTE ||
  161. metadata.type == IMAGE_DATA_TYPE_BYTE4);
  162. /* If colorspace conversion needed, use half instead of short so we can
  163. * represent HDR values that might result from conversion. */
  164. if (metadata.type == IMAGE_DATA_TYPE_USHORT) {
  165. metadata.type = IMAGE_DATA_TYPE_HALF;
  166. }
  167. else if (metadata.type == IMAGE_DATA_TYPE_USHORT4) {
  168. metadata.type = IMAGE_DATA_TYPE_HALF4;
  169. }
  170. }
  171. }
  172. bool ImageManager::get_image_metadata(const string &filename,
  173. void *builtin_data,
  174. ustring colorspace,
  175. ImageMetaData &metadata)
  176. {
  177. metadata = ImageMetaData();
  178. metadata.colorspace = colorspace;
  179. if (builtin_data) {
  180. if (builtin_image_info_cb) {
  181. builtin_image_info_cb(filename, builtin_data, metadata);
  182. }
  183. else {
  184. return false;
  185. }
  186. if (metadata.is_float) {
  187. metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_FLOAT4 : IMAGE_DATA_TYPE_FLOAT;
  188. }
  189. else {
  190. metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_BYTE4 : IMAGE_DATA_TYPE_BYTE;
  191. }
  192. metadata_detect_colorspace(metadata, "");
  193. return true;
  194. }
  195. /* Perform preliminary checks, with meaningful logging. */
  196. if (!path_exists(filename)) {
  197. VLOG(1) << "File '" << filename << "' does not exist.";
  198. return false;
  199. }
  200. if (path_is_directory(filename)) {
  201. VLOG(1) << "File '" << filename << "' is a directory, can't use as image.";
  202. return false;
  203. }
  204. unique_ptr<ImageInput> in(ImageInput::create(filename));
  205. if (!in) {
  206. return false;
  207. }
  208. ImageSpec spec;
  209. if (!in->open(filename, spec)) {
  210. return false;
  211. }
  212. metadata.width = spec.width;
  213. metadata.height = spec.height;
  214. metadata.depth = spec.depth;
  215. metadata.compress_as_srgb = false;
  216. /* Check the main format, and channel formats. */
  217. size_t channel_size = spec.format.basesize();
  218. if (spec.format.is_floating_point()) {
  219. metadata.is_float = true;
  220. }
  221. for (size_t channel = 0; channel < spec.channelformats.size(); channel++) {
  222. channel_size = max(channel_size, spec.channelformats[channel].basesize());
  223. if (spec.channelformats[channel].is_floating_point()) {
  224. metadata.is_float = true;
  225. }
  226. }
  227. /* check if it's half float */
  228. if (spec.format == TypeDesc::HALF) {
  229. metadata.is_half = true;
  230. }
  231. /* set type and channels */
  232. metadata.channels = spec.nchannels;
  233. if (metadata.is_half) {
  234. metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_HALF4 : IMAGE_DATA_TYPE_HALF;
  235. }
  236. else if (metadata.is_float) {
  237. metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_FLOAT4 : IMAGE_DATA_TYPE_FLOAT;
  238. }
  239. else if (spec.format == TypeDesc::USHORT) {
  240. metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_USHORT4 : IMAGE_DATA_TYPE_USHORT;
  241. }
  242. else {
  243. metadata.type = (metadata.channels > 1) ? IMAGE_DATA_TYPE_BYTE4 : IMAGE_DATA_TYPE_BYTE;
  244. }
  245. metadata_detect_colorspace(metadata, in->format_name());
  246. in->close();
  247. return true;
  248. }
  249. static bool image_equals(ImageManager::Image *image,
  250. const string &filename,
  251. void *builtin_data,
  252. InterpolationType interpolation,
  253. ExtensionType extension,
  254. ImageAlphaType alpha_type,
  255. ustring colorspace)
  256. {
  257. return image->filename == filename && image->builtin_data == builtin_data &&
  258. image->interpolation == interpolation && image->extension == extension &&
  259. image->alpha_type == alpha_type && image->colorspace == colorspace;
  260. }
  261. int ImageManager::add_image(const string &filename,
  262. void *builtin_data,
  263. bool animated,
  264. float frame,
  265. InterpolationType interpolation,
  266. ExtensionType extension,
  267. ImageAlphaType alpha_type,
  268. ustring colorspace,
  269. ImageMetaData &metadata)
  270. {
  271. Image *img;
  272. size_t slot;
  273. get_image_metadata(filename, builtin_data, colorspace, metadata);
  274. ImageDataType type = metadata.type;
  275. thread_scoped_lock device_lock(device_mutex);
  276. /* No half textures on OpenCL, use full float instead. */
  277. if (!has_half_images) {
  278. if (type == IMAGE_DATA_TYPE_HALF4) {
  279. type = IMAGE_DATA_TYPE_FLOAT4;
  280. }
  281. else if (type == IMAGE_DATA_TYPE_HALF) {
  282. type = IMAGE_DATA_TYPE_FLOAT;
  283. }
  284. }
  285. /* Fnd existing image. */
  286. for (slot = 0; slot < images[type].size(); slot++) {
  287. img = images[type][slot];
  288. if (img &&
  289. image_equals(
  290. img, filename, builtin_data, interpolation, extension, alpha_type, colorspace)) {
  291. if (img->frame != frame) {
  292. img->frame = frame;
  293. img->need_load = true;
  294. }
  295. if (img->alpha_type != alpha_type) {
  296. img->alpha_type = alpha_type;
  297. img->need_load = true;
  298. }
  299. if (img->colorspace != colorspace) {
  300. img->colorspace = colorspace;
  301. img->need_load = true;
  302. }
  303. if (!(img->metadata == metadata)) {
  304. img->metadata = metadata;
  305. img->need_load = true;
  306. }
  307. img->users++;
  308. return type_index_to_flattened_slot(slot, type);
  309. }
  310. }
  311. /* Find free slot. */
  312. for (slot = 0; slot < images[type].size(); slot++) {
  313. if (!images[type][slot])
  314. break;
  315. }
  316. /* Count if we're over the limit.
  317. * Very unlikely, since max_num_images is insanely big. But better safe
  318. * than sorry.
  319. */
  320. int tex_count = 0;
  321. for (int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
  322. tex_count += tex_num_images[type];
  323. }
  324. if (tex_count > max_num_images) {
  325. printf(
  326. "ImageManager::add_image: Reached image limit (%d), "
  327. "skipping '%s'\n",
  328. max_num_images,
  329. filename.c_str());
  330. return -1;
  331. }
  332. if (slot == images[type].size()) {
  333. images[type].resize(images[type].size() + 1);
  334. }
  335. /* Add new image. */
  336. img = new Image();
  337. img->filename = filename;
  338. img->builtin_data = builtin_data;
  339. img->metadata = metadata;
  340. img->need_load = true;
  341. img->animated = animated;
  342. img->frame = frame;
  343. img->interpolation = interpolation;
  344. img->extension = extension;
  345. img->users = 1;
  346. img->alpha_type = alpha_type;
  347. img->colorspace = colorspace;
  348. img->mem = NULL;
  349. images[type][slot] = img;
  350. ++tex_num_images[type];
  351. need_update = true;
  352. return type_index_to_flattened_slot(slot, type);
  353. }
  354. void ImageManager::add_image_user(int flat_slot)
  355. {
  356. ImageDataType type;
  357. int slot = flattened_slot_to_type_index(flat_slot, &type);
  358. Image *image = images[type][slot];
  359. assert(image && image->users >= 1);
  360. image->users++;
  361. }
  362. void ImageManager::remove_image(int flat_slot)
  363. {
  364. ImageDataType type;
  365. int slot = flattened_slot_to_type_index(flat_slot, &type);
  366. Image *image = images[type][slot];
  367. assert(image && image->users >= 1);
  368. /* decrement user count */
  369. image->users--;
  370. /* don't remove immediately, rather do it all together later on. one of
  371. * the reasons for this is that on shader changes we add and remove nodes
  372. * that use them, but we do not want to reload the image all the time. */
  373. if (image->users == 0)
  374. need_update = true;
  375. }
  376. void ImageManager::remove_image(const string &filename,
  377. void *builtin_data,
  378. InterpolationType interpolation,
  379. ExtensionType extension,
  380. ImageAlphaType alpha_type,
  381. ustring colorspace)
  382. {
  383. size_t slot;
  384. for (int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
  385. for (slot = 0; slot < images[type].size(); slot++) {
  386. if (images[type][slot] && image_equals(images[type][slot],
  387. filename,
  388. builtin_data,
  389. interpolation,
  390. extension,
  391. alpha_type,
  392. colorspace)) {
  393. remove_image(type_index_to_flattened_slot(slot, (ImageDataType)type));
  394. return;
  395. }
  396. }
  397. }
  398. }
  399. /* TODO(sergey): Deduplicate with the iteration above, but make it pretty,
  400. * without bunch of arguments passing around making code readability even
  401. * more cluttered.
  402. */
  403. void ImageManager::tag_reload_image(const string &filename,
  404. void *builtin_data,
  405. InterpolationType interpolation,
  406. ExtensionType extension,
  407. ImageAlphaType alpha_type,
  408. ustring colorspace)
  409. {
  410. for (size_t type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
  411. for (size_t slot = 0; slot < images[type].size(); slot++) {
  412. if (images[type][slot] && image_equals(images[type][slot],
  413. filename,
  414. builtin_data,
  415. interpolation,
  416. extension,
  417. alpha_type,
  418. colorspace)) {
  419. images[type][slot]->need_load = true;
  420. break;
  421. }
  422. }
  423. }
  424. }
  425. static bool image_associate_alpha(ImageManager::Image *img)
  426. {
  427. /* For typical RGBA images we let OIIO convert to associated alpha,
  428. * but some types we want to leave the RGB channels untouched. */
  429. return !(ColorSpaceManager::colorspace_is_data(img->colorspace) ||
  430. img->alpha_type == IMAGE_ALPHA_IGNORE || img->alpha_type == IMAGE_ALPHA_CHANNEL_PACKED);
  431. }
  432. bool ImageManager::file_load_image_generic(Image *img, unique_ptr<ImageInput> *in)
  433. {
  434. if (img->filename == "")
  435. return false;
  436. if (!img->builtin_data) {
  437. /* NOTE: Error logging is done in meta data acquisition. */
  438. if (!path_exists(img->filename) || path_is_directory(img->filename)) {
  439. return false;
  440. }
  441. /* load image from file through OIIO */
  442. *in = unique_ptr<ImageInput>(ImageInput::create(img->filename));
  443. if (!*in)
  444. return false;
  445. ImageSpec spec = ImageSpec();
  446. ImageSpec config = ImageSpec();
  447. if (!image_associate_alpha(img)) {
  448. config.attribute("oiio:UnassociatedAlpha", 1);
  449. }
  450. if (!(*in)->open(img->filename, spec, config)) {
  451. return false;
  452. }
  453. }
  454. else {
  455. /* load image using builtin images callbacks */
  456. if (!builtin_image_info_cb || !builtin_image_pixels_cb)
  457. return false;
  458. }
  459. /* we only handle certain number of components */
  460. if (!(img->metadata.channels >= 1 && img->metadata.channels <= 4)) {
  461. if (*in) {
  462. (*in)->close();
  463. }
  464. return false;
  465. }
  466. return true;
  467. }
  468. template<TypeDesc::BASETYPE FileFormat, typename StorageType, typename DeviceType>
  469. bool ImageManager::file_load_image(Image *img,
  470. ImageDataType type,
  471. int texture_limit,
  472. device_vector<DeviceType> &tex_img)
  473. {
  474. unique_ptr<ImageInput> in = NULL;
  475. if (!file_load_image_generic(img, &in)) {
  476. return false;
  477. }
  478. /* Get metadata. */
  479. int width = img->metadata.width;
  480. int height = img->metadata.height;
  481. int depth = img->metadata.depth;
  482. int components = img->metadata.channels;
  483. /* Read pixels. */
  484. vector<StorageType> pixels_storage;
  485. StorageType *pixels;
  486. const size_t max_size = max(max(width, height), depth);
  487. if (max_size == 0) {
  488. /* Don't bother with empty images. */
  489. return false;
  490. }
  491. /* Allocate memory as needed, may be smaller to resize down. */
  492. if (texture_limit > 0 && max_size > texture_limit) {
  493. pixels_storage.resize(((size_t)width) * height * depth * 4);
  494. pixels = &pixels_storage[0];
  495. }
  496. else {
  497. thread_scoped_lock device_lock(device_mutex);
  498. pixels = (StorageType *)tex_img.alloc(width, height, depth);
  499. }
  500. if (pixels == NULL) {
  501. /* Could be that we've run out of memory. */
  502. return false;
  503. }
  504. bool cmyk = false;
  505. const size_t num_pixels = ((size_t)width) * height * depth;
  506. if (in) {
  507. /* Read pixels through OpenImageIO. */
  508. StorageType *readpixels = pixels;
  509. vector<StorageType> tmppixels;
  510. if (components > 4) {
  511. tmppixels.resize(((size_t)width) * height * components);
  512. readpixels = &tmppixels[0];
  513. }
  514. if (depth <= 1) {
  515. size_t scanlinesize = ((size_t)width) * components * sizeof(StorageType);
  516. in->read_image(FileFormat,
  517. (uchar *)readpixels + (height - 1) * scanlinesize,
  518. AutoStride,
  519. -scanlinesize,
  520. AutoStride);
  521. }
  522. else {
  523. in->read_image(FileFormat, (uchar *)readpixels);
  524. }
  525. if (components > 4) {
  526. size_t dimensions = ((size_t)width) * height;
  527. for (size_t i = dimensions - 1, pixel = 0; pixel < dimensions; pixel++, i--) {
  528. pixels[i * 4 + 3] = tmppixels[i * components + 3];
  529. pixels[i * 4 + 2] = tmppixels[i * components + 2];
  530. pixels[i * 4 + 1] = tmppixels[i * components + 1];
  531. pixels[i * 4 + 0] = tmppixels[i * components + 0];
  532. }
  533. tmppixels.clear();
  534. }
  535. cmyk = strcmp(in->format_name(), "jpeg") == 0 && components == 4;
  536. in->close();
  537. }
  538. else {
  539. /* Read pixels through callback. */
  540. if (FileFormat == TypeDesc::FLOAT) {
  541. builtin_image_float_pixels_cb(img->filename,
  542. img->builtin_data,
  543. (float *)&pixels[0],
  544. num_pixels * components,
  545. image_associate_alpha(img),
  546. img->metadata.builtin_free_cache);
  547. }
  548. else if (FileFormat == TypeDesc::UINT8) {
  549. builtin_image_pixels_cb(img->filename,
  550. img->builtin_data,
  551. (uchar *)&pixels[0],
  552. num_pixels * components,
  553. image_associate_alpha(img),
  554. img->metadata.builtin_free_cache);
  555. }
  556. else {
  557. /* TODO(dingto): Support half for ImBuf. */
  558. }
  559. }
  560. /* The kernel can handle 1 and 4 channel images. Anything that is not a single
  561. * channel image is converted to RGBA format. */
  562. bool is_rgba = (type == IMAGE_DATA_TYPE_FLOAT4 || type == IMAGE_DATA_TYPE_HALF4 ||
  563. type == IMAGE_DATA_TYPE_BYTE4 || type == IMAGE_DATA_TYPE_USHORT4);
  564. if (is_rgba) {
  565. const StorageType one = util_image_cast_from_float<StorageType>(1.0f);
  566. if (cmyk) {
  567. /* CMYK to RGBA. */
  568. for (size_t i = num_pixels - 1, pixel = 0; pixel < num_pixels; pixel++, i--) {
  569. float c = util_image_cast_to_float(pixels[i * 4 + 0]);
  570. float m = util_image_cast_to_float(pixels[i * 4 + 1]);
  571. float y = util_image_cast_to_float(pixels[i * 4 + 2]);
  572. float k = util_image_cast_to_float(pixels[i * 4 + 3]);
  573. pixels[i * 4 + 0] = util_image_cast_from_float<StorageType>((1.0f - c) * (1.0f - k));
  574. pixels[i * 4 + 1] = util_image_cast_from_float<StorageType>((1.0f - m) * (1.0f - k));
  575. pixels[i * 4 + 2] = util_image_cast_from_float<StorageType>((1.0f - y) * (1.0f - k));
  576. pixels[i * 4 + 3] = one;
  577. }
  578. }
  579. else if (components == 2) {
  580. /* Grayscale + alpha to RGBA. */
  581. for (size_t i = num_pixels - 1, pixel = 0; pixel < num_pixels; pixel++, i--) {
  582. pixels[i * 4 + 3] = pixels[i * 2 + 1];
  583. pixels[i * 4 + 2] = pixels[i * 2 + 0];
  584. pixels[i * 4 + 1] = pixels[i * 2 + 0];
  585. pixels[i * 4 + 0] = pixels[i * 2 + 0];
  586. }
  587. }
  588. else if (components == 3) {
  589. /* RGB to RGBA. */
  590. for (size_t i = num_pixels - 1, pixel = 0; pixel < num_pixels; pixel++, i--) {
  591. pixels[i * 4 + 3] = one;
  592. pixels[i * 4 + 2] = pixels[i * 3 + 2];
  593. pixels[i * 4 + 1] = pixels[i * 3 + 1];
  594. pixels[i * 4 + 0] = pixels[i * 3 + 0];
  595. }
  596. }
  597. else if (components == 1) {
  598. /* Grayscale to RGBA. */
  599. for (size_t i = num_pixels - 1, pixel = 0; pixel < num_pixels; pixel++, i--) {
  600. pixels[i * 4 + 3] = one;
  601. pixels[i * 4 + 2] = pixels[i];
  602. pixels[i * 4 + 1] = pixels[i];
  603. pixels[i * 4 + 0] = pixels[i];
  604. }
  605. }
  606. /* Disable alpha if requested by the user. */
  607. if (img->alpha_type == IMAGE_ALPHA_IGNORE) {
  608. for (size_t i = num_pixels - 1, pixel = 0; pixel < num_pixels; pixel++, i--) {
  609. pixels[i * 4 + 3] = one;
  610. }
  611. }
  612. if (img->metadata.colorspace != u_colorspace_raw &&
  613. img->metadata.colorspace != u_colorspace_srgb) {
  614. /* Convert to scene linear. */
  615. ColorSpaceManager::to_scene_linear(
  616. img->metadata.colorspace, pixels, width, height, depth, img->metadata.compress_as_srgb);
  617. }
  618. }
  619. /* Make sure we don't have buggy values. */
  620. if (FileFormat == TypeDesc::FLOAT) {
  621. /* For RGBA buffers we put all channels to 0 if either of them is not
  622. * finite. This way we avoid possible artifacts caused by fully changed
  623. * hue. */
  624. if (is_rgba) {
  625. for (size_t i = 0; i < num_pixels; i += 4) {
  626. StorageType *pixel = &pixels[i * 4];
  627. if (!isfinite(pixel[0]) || !isfinite(pixel[1]) || !isfinite(pixel[2]) ||
  628. !isfinite(pixel[3])) {
  629. pixel[0] = 0;
  630. pixel[1] = 0;
  631. pixel[2] = 0;
  632. pixel[3] = 0;
  633. }
  634. }
  635. }
  636. else {
  637. for (size_t i = 0; i < num_pixels; ++i) {
  638. StorageType *pixel = &pixels[i];
  639. if (!isfinite(pixel[0])) {
  640. pixel[0] = 0;
  641. }
  642. }
  643. }
  644. }
  645. /* Scale image down if needed. */
  646. if (pixels_storage.size() > 0) {
  647. float scale_factor = 1.0f;
  648. while (max_size * scale_factor > texture_limit) {
  649. scale_factor *= 0.5f;
  650. }
  651. VLOG(1) << "Scaling image " << img->filename << " by a factor of " << scale_factor << ".";
  652. vector<StorageType> scaled_pixels;
  653. size_t scaled_width, scaled_height, scaled_depth;
  654. util_image_resize_pixels(pixels_storage,
  655. width,
  656. height,
  657. depth,
  658. is_rgba ? 4 : 1,
  659. scale_factor,
  660. &scaled_pixels,
  661. &scaled_width,
  662. &scaled_height,
  663. &scaled_depth);
  664. StorageType *texture_pixels;
  665. {
  666. thread_scoped_lock device_lock(device_mutex);
  667. texture_pixels = (StorageType *)tex_img.alloc(scaled_width, scaled_height, scaled_depth);
  668. }
  669. memcpy(texture_pixels, &scaled_pixels[0], scaled_pixels.size() * sizeof(StorageType));
  670. }
  671. return true;
  672. }
  673. void ImageManager::device_load_image(
  674. Device *device, Scene *scene, ImageDataType type, int slot, Progress *progress)
  675. {
  676. if (progress->get_cancel())
  677. return;
  678. Image *img = images[type][slot];
  679. if (osl_texture_system && !img->builtin_data)
  680. return;
  681. string filename = path_filename(images[type][slot]->filename);
  682. progress->set_status("Updating Images", "Loading " + filename);
  683. const int texture_limit = scene->params.texture_limit;
  684. /* Slot assignment */
  685. int flat_slot = type_index_to_flattened_slot(slot, type);
  686. img->mem_name = string_printf("__tex_image_%s_%03d", name_from_type(type), flat_slot);
  687. /* Free previous texture in slot. */
  688. if (img->mem) {
  689. thread_scoped_lock device_lock(device_mutex);
  690. delete img->mem;
  691. img->mem = NULL;
  692. }
  693. /* Create new texture. */
  694. if (type == IMAGE_DATA_TYPE_FLOAT4) {
  695. device_vector<float4> *tex_img = new device_vector<float4>(
  696. device, img->mem_name.c_str(), MEM_TEXTURE);
  697. if (!file_load_image<TypeDesc::FLOAT, float>(img, type, texture_limit, *tex_img)) {
  698. /* on failure to load, we set a 1x1 pixels pink image */
  699. thread_scoped_lock device_lock(device_mutex);
  700. float *pixels = (float *)tex_img->alloc(1, 1);
  701. pixels[0] = TEX_IMAGE_MISSING_R;
  702. pixels[1] = TEX_IMAGE_MISSING_G;
  703. pixels[2] = TEX_IMAGE_MISSING_B;
  704. pixels[3] = TEX_IMAGE_MISSING_A;
  705. }
  706. img->mem = tex_img;
  707. img->mem->interpolation = img->interpolation;
  708. img->mem->extension = img->extension;
  709. thread_scoped_lock device_lock(device_mutex);
  710. tex_img->copy_to_device();
  711. }
  712. else if (type == IMAGE_DATA_TYPE_FLOAT) {
  713. device_vector<float> *tex_img = new device_vector<float>(
  714. device, img->mem_name.c_str(), MEM_TEXTURE);
  715. if (!file_load_image<TypeDesc::FLOAT, float>(img, type, texture_limit, *tex_img)) {
  716. /* on failure to load, we set a 1x1 pixels pink image */
  717. thread_scoped_lock device_lock(device_mutex);
  718. float *pixels = (float *)tex_img->alloc(1, 1);
  719. pixels[0] = TEX_IMAGE_MISSING_R;
  720. }
  721. img->mem = tex_img;
  722. img->mem->interpolation = img->interpolation;
  723. img->mem->extension = img->extension;
  724. thread_scoped_lock device_lock(device_mutex);
  725. tex_img->copy_to_device();
  726. }
  727. else if (type == IMAGE_DATA_TYPE_BYTE4) {
  728. device_vector<uchar4> *tex_img = new device_vector<uchar4>(
  729. device, img->mem_name.c_str(), MEM_TEXTURE);
  730. if (!file_load_image<TypeDesc::UINT8, uchar>(img, type, texture_limit, *tex_img)) {
  731. /* on failure to load, we set a 1x1 pixels pink image */
  732. thread_scoped_lock device_lock(device_mutex);
  733. uchar *pixels = (uchar *)tex_img->alloc(1, 1);
  734. pixels[0] = (TEX_IMAGE_MISSING_R * 255);
  735. pixels[1] = (TEX_IMAGE_MISSING_G * 255);
  736. pixels[2] = (TEX_IMAGE_MISSING_B * 255);
  737. pixels[3] = (TEX_IMAGE_MISSING_A * 255);
  738. }
  739. img->mem = tex_img;
  740. img->mem->interpolation = img->interpolation;
  741. img->mem->extension = img->extension;
  742. thread_scoped_lock device_lock(device_mutex);
  743. tex_img->copy_to_device();
  744. }
  745. else if (type == IMAGE_DATA_TYPE_BYTE) {
  746. device_vector<uchar> *tex_img = new device_vector<uchar>(
  747. device, img->mem_name.c_str(), MEM_TEXTURE);
  748. if (!file_load_image<TypeDesc::UINT8, uchar>(img, type, texture_limit, *tex_img)) {
  749. /* on failure to load, we set a 1x1 pixels pink image */
  750. thread_scoped_lock device_lock(device_mutex);
  751. uchar *pixels = (uchar *)tex_img->alloc(1, 1);
  752. pixels[0] = (TEX_IMAGE_MISSING_R * 255);
  753. }
  754. img->mem = tex_img;
  755. img->mem->interpolation = img->interpolation;
  756. img->mem->extension = img->extension;
  757. thread_scoped_lock device_lock(device_mutex);
  758. tex_img->copy_to_device();
  759. }
  760. else if (type == IMAGE_DATA_TYPE_HALF4) {
  761. device_vector<half4> *tex_img = new device_vector<half4>(
  762. device, img->mem_name.c_str(), MEM_TEXTURE);
  763. if (!file_load_image<TypeDesc::HALF, half>(img, type, texture_limit, *tex_img)) {
  764. /* on failure to load, we set a 1x1 pixels pink image */
  765. thread_scoped_lock device_lock(device_mutex);
  766. half *pixels = (half *)tex_img->alloc(1, 1);
  767. pixels[0] = TEX_IMAGE_MISSING_R;
  768. pixels[1] = TEX_IMAGE_MISSING_G;
  769. pixels[2] = TEX_IMAGE_MISSING_B;
  770. pixels[3] = TEX_IMAGE_MISSING_A;
  771. }
  772. img->mem = tex_img;
  773. img->mem->interpolation = img->interpolation;
  774. img->mem->extension = img->extension;
  775. thread_scoped_lock device_lock(device_mutex);
  776. tex_img->copy_to_device();
  777. }
  778. else if (type == IMAGE_DATA_TYPE_USHORT) {
  779. device_vector<uint16_t> *tex_img = new device_vector<uint16_t>(
  780. device, img->mem_name.c_str(), MEM_TEXTURE);
  781. if (!file_load_image<TypeDesc::USHORT, uint16_t>(img, type, texture_limit, *tex_img)) {
  782. /* on failure to load, we set a 1x1 pixels pink image */
  783. thread_scoped_lock device_lock(device_mutex);
  784. uint16_t *pixels = (uint16_t *)tex_img->alloc(1, 1);
  785. pixels[0] = (TEX_IMAGE_MISSING_R * 65535);
  786. }
  787. img->mem = tex_img;
  788. img->mem->interpolation = img->interpolation;
  789. img->mem->extension = img->extension;
  790. thread_scoped_lock device_lock(device_mutex);
  791. tex_img->copy_to_device();
  792. }
  793. else if (type == IMAGE_DATA_TYPE_USHORT4) {
  794. device_vector<ushort4> *tex_img = new device_vector<ushort4>(
  795. device, img->mem_name.c_str(), MEM_TEXTURE);
  796. if (!file_load_image<TypeDesc::USHORT, uint16_t>(img, type, texture_limit, *tex_img)) {
  797. /* on failure to load, we set a 1x1 pixels pink image */
  798. thread_scoped_lock device_lock(device_mutex);
  799. uint16_t *pixels = (uint16_t *)tex_img->alloc(1, 1);
  800. pixels[0] = (TEX_IMAGE_MISSING_R * 65535);
  801. pixels[1] = (TEX_IMAGE_MISSING_G * 65535);
  802. pixels[2] = (TEX_IMAGE_MISSING_B * 65535);
  803. pixels[3] = (TEX_IMAGE_MISSING_A * 65535);
  804. }
  805. img->mem = tex_img;
  806. img->mem->interpolation = img->interpolation;
  807. img->mem->extension = img->extension;
  808. thread_scoped_lock device_lock(device_mutex);
  809. tex_img->copy_to_device();
  810. }
  811. else if (type == IMAGE_DATA_TYPE_HALF) {
  812. device_vector<half> *tex_img = new device_vector<half>(
  813. device, img->mem_name.c_str(), MEM_TEXTURE);
  814. if (!file_load_image<TypeDesc::HALF, half>(img, type, texture_limit, *tex_img)) {
  815. /* on failure to load, we set a 1x1 pixels pink image */
  816. thread_scoped_lock device_lock(device_mutex);
  817. half *pixels = (half *)tex_img->alloc(1, 1);
  818. pixels[0] = TEX_IMAGE_MISSING_R;
  819. }
  820. img->mem = tex_img;
  821. img->mem->interpolation = img->interpolation;
  822. img->mem->extension = img->extension;
  823. thread_scoped_lock device_lock(device_mutex);
  824. tex_img->copy_to_device();
  825. }
  826. img->need_load = false;
  827. }
  828. void ImageManager::device_free_image(Device *, ImageDataType type, int slot)
  829. {
  830. Image *img = images[type][slot];
  831. if (img) {
  832. if (osl_texture_system && !img->builtin_data) {
  833. #ifdef WITH_OSL
  834. ustring filename(images[type][slot]->filename);
  835. ((OSL::TextureSystem *)osl_texture_system)->invalidate(filename);
  836. #endif
  837. }
  838. if (img->mem) {
  839. thread_scoped_lock device_lock(device_mutex);
  840. delete img->mem;
  841. }
  842. delete img;
  843. images[type][slot] = NULL;
  844. --tex_num_images[type];
  845. }
  846. }
  847. void ImageManager::device_update(Device *device, Scene *scene, Progress &progress)
  848. {
  849. if (!need_update) {
  850. return;
  851. }
  852. TaskPool pool;
  853. for (int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
  854. for (size_t slot = 0; slot < images[type].size(); slot++) {
  855. if (!images[type][slot])
  856. continue;
  857. if (images[type][slot]->users == 0) {
  858. device_free_image(device, (ImageDataType)type, slot);
  859. }
  860. else if (images[type][slot]->need_load) {
  861. if (!osl_texture_system || images[type][slot]->builtin_data)
  862. pool.push(function_bind(&ImageManager::device_load_image,
  863. this,
  864. device,
  865. scene,
  866. (ImageDataType)type,
  867. slot,
  868. &progress));
  869. }
  870. }
  871. }
  872. pool.wait_work();
  873. need_update = false;
  874. }
  875. void ImageManager::device_update_slot(Device *device,
  876. Scene *scene,
  877. int flat_slot,
  878. Progress *progress)
  879. {
  880. ImageDataType type;
  881. int slot = flattened_slot_to_type_index(flat_slot, &type);
  882. Image *image = images[type][slot];
  883. assert(image != NULL);
  884. if (image->users == 0) {
  885. device_free_image(device, type, slot);
  886. }
  887. else if (image->need_load) {
  888. if (!osl_texture_system || image->builtin_data)
  889. device_load_image(device, scene, type, slot, progress);
  890. }
  891. }
  892. void ImageManager::device_load_builtin(Device *device, Scene *scene, Progress &progress)
  893. {
  894. /* Load only builtin images, Blender needs this to load evaluated
  895. * scene data from depsgraph before it is freed. */
  896. if (!need_update) {
  897. return;
  898. }
  899. TaskPool pool;
  900. for (int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
  901. for (size_t slot = 0; slot < images[type].size(); slot++) {
  902. if (!images[type][slot])
  903. continue;
  904. if (images[type][slot]->need_load) {
  905. if (images[type][slot]->builtin_data) {
  906. pool.push(function_bind(&ImageManager::device_load_image,
  907. this,
  908. device,
  909. scene,
  910. (ImageDataType)type,
  911. slot,
  912. &progress));
  913. }
  914. }
  915. }
  916. }
  917. pool.wait_work();
  918. }
  919. void ImageManager::device_free_builtin(Device *device)
  920. {
  921. for (int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
  922. for (size_t slot = 0; slot < images[type].size(); slot++) {
  923. if (images[type][slot] && images[type][slot]->builtin_data)
  924. device_free_image(device, (ImageDataType)type, slot);
  925. }
  926. }
  927. }
  928. void ImageManager::device_free(Device *device)
  929. {
  930. for (int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
  931. for (size_t slot = 0; slot < images[type].size(); slot++) {
  932. device_free_image(device, (ImageDataType)type, slot);
  933. }
  934. images[type].clear();
  935. }
  936. }
  937. void ImageManager::collect_statistics(RenderStats *stats)
  938. {
  939. for (int type = 0; type < IMAGE_DATA_NUM_TYPES; type++) {
  940. foreach (const Image *image, images[type]) {
  941. stats->image.textures.add_entry(
  942. NamedSizeEntry(path_filename(image->filename), image->mem->memory_size()));
  943. }
  944. }
  945. }
  946. CCL_NAMESPACE_END