kernel_film.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. CCL_NAMESPACE_BEGIN
  17. ccl_device float4 film_map(KernelGlobals *kg, float4 irradiance, float scale)
  18. {
  19. float exposure = kernel_data.film.exposure;
  20. float4 result = irradiance * scale;
  21. /* conversion to srgb */
  22. result.x = color_linear_to_srgb(result.x * exposure);
  23. result.y = color_linear_to_srgb(result.y * exposure);
  24. result.z = color_linear_to_srgb(result.z * exposure);
  25. /* clamp since alpha might be > 1.0 due to russian roulette */
  26. result.w = saturate(result.w);
  27. return result;
  28. }
  29. ccl_device uchar4 film_float_to_byte(float4 color)
  30. {
  31. uchar4 result;
  32. /* simple float to byte conversion */
  33. result.x = (uchar)(saturate(color.x) * 255.0f);
  34. result.y = (uchar)(saturate(color.y) * 255.0f);
  35. result.z = (uchar)(saturate(color.z) * 255.0f);
  36. result.w = (uchar)(saturate(color.w) * 255.0f);
  37. return result;
  38. }
  39. ccl_device void kernel_film_convert_to_byte(KernelGlobals *kg,
  40. ccl_global uchar4 *rgba,
  41. ccl_global float *buffer,
  42. float sample_scale,
  43. int x,
  44. int y,
  45. int offset,
  46. int stride)
  47. {
  48. /* buffer offset */
  49. int index = offset + x + y * stride;
  50. rgba += index;
  51. buffer += index * kernel_data.film.pass_stride;
  52. /* map colors */
  53. float4 irradiance = *((ccl_global float4 *)buffer);
  54. float4 float_result = film_map(kg, irradiance, sample_scale);
  55. uchar4 byte_result = film_float_to_byte(float_result);
  56. *rgba = byte_result;
  57. }
  58. ccl_device void kernel_film_convert_to_half_float(KernelGlobals *kg,
  59. ccl_global uchar4 *rgba,
  60. ccl_global float *buffer,
  61. float sample_scale,
  62. int x,
  63. int y,
  64. int offset,
  65. int stride)
  66. {
  67. /* buffer offset */
  68. int index = offset + x + y * stride;
  69. ccl_global float4 *in = (ccl_global float4 *)(buffer + index * kernel_data.film.pass_stride);
  70. ccl_global half *out = (ccl_global half *)rgba + index * 4;
  71. float exposure = kernel_data.film.exposure;
  72. float4 rgba_in = *in;
  73. if (exposure != 1.0f) {
  74. rgba_in.x *= exposure;
  75. rgba_in.y *= exposure;
  76. rgba_in.z *= exposure;
  77. }
  78. float4_store_half(out, rgba_in, sample_scale);
  79. }
  80. CCL_NAMESPACE_END