buffers.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 "render/buffers.h"
  18. #include "device/device.h"
  19. #include "util/util_foreach.h"
  20. #include "util/util_hash.h"
  21. #include "util/util_math.h"
  22. #include "util/util_opengl.h"
  23. #include "util/util_time.h"
  24. #include "util/util_types.h"
  25. CCL_NAMESPACE_BEGIN
  26. /* Buffer Params */
  27. BufferParams::BufferParams()
  28. {
  29. width = 0;
  30. height = 0;
  31. full_x = 0;
  32. full_y = 0;
  33. full_width = 0;
  34. full_height = 0;
  35. denoising_data_pass = false;
  36. denoising_clean_pass = false;
  37. denoising_prefiltered_pass = false;
  38. Pass::add(PASS_COMBINED, passes);
  39. }
  40. void BufferParams::get_offset_stride(int &offset, int &stride)
  41. {
  42. offset = -(full_x + full_y * width);
  43. stride = width;
  44. }
  45. bool BufferParams::modified(const BufferParams &params)
  46. {
  47. return !(full_x == params.full_x && full_y == params.full_y && width == params.width &&
  48. height == params.height && full_width == params.full_width &&
  49. full_height == params.full_height && Pass::equals(passes, params.passes));
  50. }
  51. int BufferParams::get_passes_size()
  52. {
  53. int size = 0;
  54. for (size_t i = 0; i < passes.size(); i++)
  55. size += passes[i].components;
  56. if (denoising_data_pass) {
  57. size += DENOISING_PASS_SIZE_BASE;
  58. if (denoising_clean_pass)
  59. size += DENOISING_PASS_SIZE_CLEAN;
  60. if (denoising_prefiltered_pass)
  61. size += DENOISING_PASS_SIZE_PREFILTERED;
  62. }
  63. return align_up(size, 4);
  64. }
  65. int BufferParams::get_denoising_offset()
  66. {
  67. int offset = 0;
  68. for (size_t i = 0; i < passes.size(); i++)
  69. offset += passes[i].components;
  70. return offset;
  71. }
  72. int BufferParams::get_denoising_prefiltered_offset()
  73. {
  74. assert(denoising_prefiltered_pass);
  75. int offset = get_denoising_offset();
  76. offset += DENOISING_PASS_SIZE_BASE;
  77. if (denoising_clean_pass) {
  78. offset += DENOISING_PASS_SIZE_CLEAN;
  79. }
  80. return offset;
  81. }
  82. /* Render Buffer Task */
  83. RenderTile::RenderTile()
  84. {
  85. x = 0;
  86. y = 0;
  87. w = 0;
  88. h = 0;
  89. sample = 0;
  90. start_sample = 0;
  91. num_samples = 0;
  92. resolution = 0;
  93. offset = 0;
  94. stride = 0;
  95. buffer = 0;
  96. buffers = NULL;
  97. }
  98. /* Render Buffers */
  99. RenderBuffers::RenderBuffers(Device *device)
  100. : buffer(device, "RenderBuffers", MEM_READ_WRITE),
  101. map_neighbor_copied(false),
  102. render_time(0.0f)
  103. {
  104. }
  105. RenderBuffers::~RenderBuffers()
  106. {
  107. buffer.free();
  108. }
  109. void RenderBuffers::reset(BufferParams &params_)
  110. {
  111. params = params_;
  112. /* re-allocate buffer */
  113. buffer.alloc(params.width * params.height * params.get_passes_size());
  114. buffer.zero_to_device();
  115. }
  116. void RenderBuffers::zero()
  117. {
  118. buffer.zero_to_device();
  119. }
  120. bool RenderBuffers::copy_from_device()
  121. {
  122. if (!buffer.device_pointer)
  123. return false;
  124. buffer.copy_from_device(0, params.width * params.get_passes_size(), params.height);
  125. return true;
  126. }
  127. bool RenderBuffers::get_denoising_pass_rect(
  128. int type, float exposure, int sample, int components, float *pixels)
  129. {
  130. if (buffer.data() == NULL) {
  131. return false;
  132. }
  133. float scale = 1.0f;
  134. float alpha_scale = 1.0f / sample;
  135. if (type == DENOISING_PASS_PREFILTERED_COLOR || type == DENOISING_PASS_CLEAN ||
  136. type == DENOISING_PASS_PREFILTERED_INTENSITY) {
  137. scale *= exposure;
  138. }
  139. else if (type == DENOISING_PASS_PREFILTERED_VARIANCE) {
  140. scale *= exposure * exposure * (sample - 1);
  141. }
  142. int offset;
  143. if (type == DENOISING_PASS_CLEAN) {
  144. /* The clean pass isn't changed by prefiltering, so we use the original one there. */
  145. offset = type + params.get_denoising_offset();
  146. scale /= sample;
  147. }
  148. else if (type == DENOISING_PASS_PREFILTERED_COLOR && !params.denoising_prefiltered_pass) {
  149. /* If we're not saving the prefiltering result, return the original noisy pass. */
  150. offset = params.get_denoising_offset() + DENOISING_PASS_COLOR;
  151. scale /= sample;
  152. }
  153. else {
  154. offset = type + params.get_denoising_prefiltered_offset();
  155. }
  156. int pass_stride = params.get_passes_size();
  157. int size = params.width * params.height;
  158. float *in = buffer.data() + offset;
  159. if (components == 1) {
  160. for (int i = 0; i < size; i++, in += pass_stride, pixels++) {
  161. pixels[0] = in[0] * scale;
  162. }
  163. }
  164. else if (components == 3) {
  165. for (int i = 0; i < size; i++, in += pass_stride, pixels += 3) {
  166. pixels[0] = in[0] * scale;
  167. pixels[1] = in[1] * scale;
  168. pixels[2] = in[2] * scale;
  169. }
  170. }
  171. else if (components == 4) {
  172. /* Since the alpha channel is not involved in denoising, output the Combined alpha channel. */
  173. assert(params.passes[0].type == PASS_COMBINED);
  174. float *in_combined = buffer.data();
  175. for (int i = 0; i < size; i++, in += pass_stride, in_combined += pass_stride, pixels += 4) {
  176. float3 val = make_float3(in[0], in[1], in[2]);
  177. if (type == DENOISING_PASS_PREFILTERED_COLOR && params.denoising_prefiltered_pass) {
  178. /* Remove highlight compression from the image. */
  179. val = color_highlight_uncompress(val);
  180. }
  181. pixels[0] = val.x * scale;
  182. pixels[1] = val.y * scale;
  183. pixels[2] = val.z * scale;
  184. pixels[3] = saturate(in_combined[3] * alpha_scale);
  185. }
  186. }
  187. else {
  188. return false;
  189. }
  190. return true;
  191. }
  192. bool RenderBuffers::get_pass_rect(
  193. PassType type, float exposure, int sample, int components, float *pixels, const string &name)
  194. {
  195. if (buffer.data() == NULL) {
  196. return false;
  197. }
  198. int pass_offset = 0;
  199. for (size_t j = 0; j < params.passes.size(); j++) {
  200. Pass &pass = params.passes[j];
  201. if (pass.type != type) {
  202. pass_offset += pass.components;
  203. continue;
  204. }
  205. /* Tell Cryptomatte passes apart by their name. */
  206. if (pass.type == PASS_CRYPTOMATTE) {
  207. if (pass.name != name) {
  208. pass_offset += pass.components;
  209. continue;
  210. }
  211. }
  212. float *in = buffer.data() + pass_offset;
  213. int pass_stride = params.get_passes_size();
  214. float scale = (pass.filter) ? 1.0f / (float)sample : 1.0f;
  215. float scale_exposure = (pass.exposure) ? scale * exposure : scale;
  216. int size = params.width * params.height;
  217. if (components == 1 && type == PASS_RENDER_TIME) {
  218. /* Render time is not stored by kernel, but measured per tile. */
  219. float val = (float)(1000.0 * render_time / (params.width * params.height * sample));
  220. for (int i = 0; i < size; i++, pixels++) {
  221. pixels[0] = val;
  222. }
  223. }
  224. else if (components == 1) {
  225. assert(pass.components == components);
  226. /* Scalar */
  227. if (type == PASS_DEPTH) {
  228. for (int i = 0; i < size; i++, in += pass_stride, pixels++) {
  229. float f = *in;
  230. pixels[0] = (f == 0.0f) ? 1e10f : f * scale_exposure;
  231. }
  232. }
  233. else if (type == PASS_MIST) {
  234. for (int i = 0; i < size; i++, in += pass_stride, pixels++) {
  235. float f = *in;
  236. pixels[0] = saturate(f * scale_exposure);
  237. }
  238. }
  239. #ifdef WITH_CYCLES_DEBUG
  240. else if (type == PASS_BVH_TRAVERSED_NODES || type == PASS_BVH_TRAVERSED_INSTANCES ||
  241. type == PASS_BVH_INTERSECTIONS || type == PASS_RAY_BOUNCES) {
  242. for (int i = 0; i < size; i++, in += pass_stride, pixels++) {
  243. float f = *in;
  244. pixels[0] = f * scale;
  245. }
  246. }
  247. #endif
  248. else {
  249. for (int i = 0; i < size; i++, in += pass_stride, pixels++) {
  250. float f = *in;
  251. pixels[0] = f * scale_exposure;
  252. }
  253. }
  254. }
  255. else if (components == 3) {
  256. assert(pass.components == 4);
  257. /* RGBA */
  258. if (type == PASS_SHADOW) {
  259. for (int i = 0; i < size; i++, in += pass_stride, pixels += 3) {
  260. float4 f = make_float4(in[0], in[1], in[2], in[3]);
  261. float invw = (f.w > 0.0f) ? 1.0f / f.w : 1.0f;
  262. pixels[0] = f.x * invw;
  263. pixels[1] = f.y * invw;
  264. pixels[2] = f.z * invw;
  265. }
  266. }
  267. else if (pass.divide_type != PASS_NONE) {
  268. /* RGB lighting passes that need to divide out color */
  269. pass_offset = 0;
  270. for (size_t k = 0; k < params.passes.size(); k++) {
  271. Pass &color_pass = params.passes[k];
  272. if (color_pass.type == pass.divide_type)
  273. break;
  274. pass_offset += color_pass.components;
  275. }
  276. float *in_divide = buffer.data() + pass_offset;
  277. for (int i = 0; i < size; i++, in += pass_stride, in_divide += pass_stride, pixels += 3) {
  278. float3 f = make_float3(in[0], in[1], in[2]);
  279. float3 f_divide = make_float3(in_divide[0], in_divide[1], in_divide[2]);
  280. f = safe_divide_even_color(f * exposure, f_divide);
  281. pixels[0] = f.x;
  282. pixels[1] = f.y;
  283. pixels[2] = f.z;
  284. }
  285. }
  286. else {
  287. /* RGB/vector */
  288. for (int i = 0; i < size; i++, in += pass_stride, pixels += 3) {
  289. float3 f = make_float3(in[0], in[1], in[2]);
  290. pixels[0] = f.x * scale_exposure;
  291. pixels[1] = f.y * scale_exposure;
  292. pixels[2] = f.z * scale_exposure;
  293. }
  294. }
  295. }
  296. else if (components == 4) {
  297. assert(pass.components == components);
  298. /* RGBA */
  299. if (type == PASS_SHADOW) {
  300. for (int i = 0; i < size; i++, in += pass_stride, pixels += 4) {
  301. float4 f = make_float4(in[0], in[1], in[2], in[3]);
  302. float invw = (f.w > 0.0f) ? 1.0f / f.w : 1.0f;
  303. pixels[0] = f.x * invw;
  304. pixels[1] = f.y * invw;
  305. pixels[2] = f.z * invw;
  306. pixels[3] = 1.0f;
  307. }
  308. }
  309. else if (type == PASS_MOTION) {
  310. /* need to normalize by number of samples accumulated for motion */
  311. pass_offset = 0;
  312. for (size_t k = 0; k < params.passes.size(); k++) {
  313. Pass &color_pass = params.passes[k];
  314. if (color_pass.type == PASS_MOTION_WEIGHT)
  315. break;
  316. pass_offset += color_pass.components;
  317. }
  318. float *in_weight = buffer.data() + pass_offset;
  319. for (int i = 0; i < size; i++, in += pass_stride, in_weight += pass_stride, pixels += 4) {
  320. float4 f = make_float4(in[0], in[1], in[2], in[3]);
  321. float w = in_weight[0];
  322. float invw = (w > 0.0f) ? 1.0f / w : 0.0f;
  323. pixels[0] = f.x * invw;
  324. pixels[1] = f.y * invw;
  325. pixels[2] = f.z * invw;
  326. pixels[3] = f.w * invw;
  327. }
  328. }
  329. else if (type == PASS_CRYPTOMATTE) {
  330. for (int i = 0; i < size; i++, in += pass_stride, pixels += 4) {
  331. float4 f = make_float4(in[0], in[1], in[2], in[3]);
  332. /* x and z contain integer IDs, don't rescale them.
  333. y and w contain matte weights, they get scaled. */
  334. pixels[0] = f.x;
  335. pixels[1] = f.y * scale;
  336. pixels[2] = f.z;
  337. pixels[3] = f.w * scale;
  338. }
  339. }
  340. else {
  341. for (int i = 0; i < size; i++, in += pass_stride, pixels += 4) {
  342. float4 f = make_float4(in[0], in[1], in[2], in[3]);
  343. pixels[0] = f.x * scale_exposure;
  344. pixels[1] = f.y * scale_exposure;
  345. pixels[2] = f.z * scale_exposure;
  346. /* clamp since alpha might be > 1.0 due to russian roulette */
  347. pixels[3] = saturate(f.w * scale);
  348. }
  349. }
  350. }
  351. return true;
  352. }
  353. return false;
  354. }
  355. /* Display Buffer */
  356. DisplayBuffer::DisplayBuffer(Device *device, bool linear)
  357. : draw_width(0),
  358. draw_height(0),
  359. transparent(true), /* todo: determine from background */
  360. half_float(linear),
  361. rgba_byte(device, "display buffer byte"),
  362. rgba_half(device, "display buffer half")
  363. {
  364. }
  365. DisplayBuffer::~DisplayBuffer()
  366. {
  367. rgba_byte.free();
  368. rgba_half.free();
  369. }
  370. void DisplayBuffer::reset(BufferParams &params_)
  371. {
  372. draw_width = 0;
  373. draw_height = 0;
  374. params = params_;
  375. /* allocate display pixels */
  376. if (half_float) {
  377. rgba_half.alloc_to_device(params.width, params.height);
  378. }
  379. else {
  380. rgba_byte.alloc_to_device(params.width, params.height);
  381. }
  382. }
  383. void DisplayBuffer::draw_set(int width, int height)
  384. {
  385. assert(width <= params.width && height <= params.height);
  386. draw_width = width;
  387. draw_height = height;
  388. }
  389. void DisplayBuffer::draw(Device *device, const DeviceDrawParams &draw_params)
  390. {
  391. if (draw_width != 0 && draw_height != 0) {
  392. device_memory &rgba = (half_float) ? (device_memory &)rgba_half : (device_memory &)rgba_byte;
  393. device->draw_pixels(rgba,
  394. 0,
  395. draw_width,
  396. draw_height,
  397. params.width,
  398. params.height,
  399. params.full_x,
  400. params.full_y,
  401. params.full_width,
  402. params.full_height,
  403. transparent,
  404. draw_params);
  405. }
  406. }
  407. bool DisplayBuffer::draw_ready()
  408. {
  409. return (draw_width != 0 && draw_height != 0);
  410. }
  411. CCL_NAMESPACE_END