kernel_opencl_image.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright 2016 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. /* For OpenCL we do manual lookup and interpolation. */
  17. ccl_device_inline ccl_global TextureInfo *kernel_tex_info(KernelGlobals *kg, uint id)
  18. {
  19. const uint tex_offset = id
  20. #define KERNEL_TEX(type, name) +1
  21. #include "kernel/kernel_textures.h"
  22. ;
  23. return &((ccl_global TextureInfo *)kg->buffers[0])[tex_offset];
  24. }
  25. #define tex_fetch(type, info, index) \
  26. ((ccl_global type *)(kg->buffers[info->cl_buffer] + info->data))[(index)]
  27. ccl_device_inline int svm_image_texture_wrap_periodic(int x, int width)
  28. {
  29. x %= width;
  30. if (x < 0)
  31. x += width;
  32. return x;
  33. }
  34. ccl_device_inline int svm_image_texture_wrap_clamp(int x, int width)
  35. {
  36. return clamp(x, 0, width - 1);
  37. }
  38. ccl_device_inline float4 svm_image_texture_read(KernelGlobals *kg,
  39. const ccl_global TextureInfo *info,
  40. int id,
  41. int offset)
  42. {
  43. const int texture_type = kernel_tex_type(id);
  44. /* Float4 */
  45. if (texture_type == IMAGE_DATA_TYPE_FLOAT4) {
  46. return tex_fetch(float4, info, offset);
  47. }
  48. /* Byte4 */
  49. else if (texture_type == IMAGE_DATA_TYPE_BYTE4) {
  50. uchar4 r = tex_fetch(uchar4, info, offset);
  51. float f = 1.0f / 255.0f;
  52. return make_float4(r.x * f, r.y * f, r.z * f, r.w * f);
  53. }
  54. /* Ushort4 */
  55. else if (texture_type == IMAGE_DATA_TYPE_USHORT4) {
  56. ushort4 r = tex_fetch(ushort4, info, offset);
  57. float f = 1.0f / 65535.f;
  58. return make_float4(r.x * f, r.y * f, r.z * f, r.w * f);
  59. }
  60. /* Float */
  61. else if (texture_type == IMAGE_DATA_TYPE_FLOAT) {
  62. float f = tex_fetch(float, info, offset);
  63. return make_float4(f, f, f, 1.0f);
  64. }
  65. /* UShort */
  66. else if (texture_type == IMAGE_DATA_TYPE_USHORT) {
  67. ushort r = tex_fetch(ushort, info, offset);
  68. float f = r * (1.0f / 65535.0f);
  69. return make_float4(f, f, f, 1.0f);
  70. }
  71. /* Byte */
  72. #ifdef cl_khr_fp16
  73. /* half and half4 are optional in OpenCL */
  74. else if (texture_type == IMAGE_DATA_TYPE_HALF) {
  75. float f = tex_fetch(half, info, offset);
  76. return make_float4(f, f, f, 1.0f);
  77. }
  78. else if (texture_type == IMAGE_DATA_TYPE_HALF4) {
  79. half4 r = tex_fetch(half4, info, offset);
  80. return make_float4(r.x, r.y, r.z, r.w);
  81. }
  82. #endif
  83. else {
  84. uchar r = tex_fetch(uchar, info, offset);
  85. float f = r * (1.0f / 255.0f);
  86. return make_float4(f, f, f, 1.0f);
  87. }
  88. }
  89. ccl_device_inline float4 svm_image_texture_read_2d(KernelGlobals *kg, int id, int x, int y)
  90. {
  91. const ccl_global TextureInfo *info = kernel_tex_info(kg, id);
  92. /* Wrap */
  93. if (info->extension == EXTENSION_REPEAT) {
  94. x = svm_image_texture_wrap_periodic(x, info->width);
  95. y = svm_image_texture_wrap_periodic(y, info->height);
  96. }
  97. else {
  98. x = svm_image_texture_wrap_clamp(x, info->width);
  99. y = svm_image_texture_wrap_clamp(y, info->height);
  100. }
  101. int offset = x + info->width * y;
  102. return svm_image_texture_read(kg, info, id, offset);
  103. }
  104. ccl_device_inline float4 svm_image_texture_read_3d(KernelGlobals *kg, int id, int x, int y, int z)
  105. {
  106. const ccl_global TextureInfo *info = kernel_tex_info(kg, id);
  107. /* Wrap */
  108. if (info->extension == EXTENSION_REPEAT) {
  109. x = svm_image_texture_wrap_periodic(x, info->width);
  110. y = svm_image_texture_wrap_periodic(y, info->height);
  111. z = svm_image_texture_wrap_periodic(z, info->depth);
  112. }
  113. else {
  114. x = svm_image_texture_wrap_clamp(x, info->width);
  115. y = svm_image_texture_wrap_clamp(y, info->height);
  116. z = svm_image_texture_wrap_clamp(z, info->depth);
  117. }
  118. int offset = x + info->width * y + info->width * info->height * z;
  119. return svm_image_texture_read(kg, info, id, offset);
  120. }
  121. ccl_device_inline float svm_image_texture_frac(float x, int *ix)
  122. {
  123. int i = float_to_int(x) - ((x < 0.0f) ? 1 : 0);
  124. *ix = i;
  125. return x - (float)i;
  126. }
  127. #define SET_CUBIC_SPLINE_WEIGHTS(u, t) \
  128. { \
  129. u[0] = (((-1.0f / 6.0f) * t + 0.5f) * t - 0.5f) * t + (1.0f / 6.0f); \
  130. u[1] = ((0.5f * t - 1.0f) * t) * t + (2.0f / 3.0f); \
  131. u[2] = ((-0.5f * t + 0.5f) * t + 0.5f) * t + (1.0f / 6.0f); \
  132. u[3] = (1.0f / 6.0f) * t * t * t; \
  133. } \
  134. (void)0
  135. ccl_device float4 kernel_tex_image_interp(KernelGlobals *kg, int id, float x, float y)
  136. {
  137. const ccl_global TextureInfo *info = kernel_tex_info(kg, id);
  138. if (info->extension == EXTENSION_CLIP) {
  139. if (x < 0.0f || y < 0.0f || x > 1.0f || y > 1.0f) {
  140. return make_float4(0.0f, 0.0f, 0.0f, 0.0f);
  141. }
  142. }
  143. if (info->interpolation == INTERPOLATION_CLOSEST) {
  144. /* Closest interpolation. */
  145. int ix, iy;
  146. svm_image_texture_frac(x * info->width, &ix);
  147. svm_image_texture_frac(y * info->height, &iy);
  148. return svm_image_texture_read_2d(kg, id, ix, iy);
  149. }
  150. else if (info->interpolation == INTERPOLATION_LINEAR) {
  151. /* Bilinear interpolation. */
  152. int ix, iy;
  153. float tx = svm_image_texture_frac(x * info->width - 0.5f, &ix);
  154. float ty = svm_image_texture_frac(y * info->height - 0.5f, &iy);
  155. float4 r;
  156. r = (1.0f - ty) * (1.0f - tx) * svm_image_texture_read_2d(kg, id, ix, iy);
  157. r += (1.0f - ty) * tx * svm_image_texture_read_2d(kg, id, ix + 1, iy);
  158. r += ty * (1.0f - tx) * svm_image_texture_read_2d(kg, id, ix, iy + 1);
  159. r += ty * tx * svm_image_texture_read_2d(kg, id, ix + 1, iy + 1);
  160. return r;
  161. }
  162. else {
  163. /* Bicubic interpolation. */
  164. int ix, iy;
  165. float tx = svm_image_texture_frac(x * info->width - 0.5f, &ix);
  166. float ty = svm_image_texture_frac(y * info->height - 0.5f, &iy);
  167. float u[4], v[4];
  168. SET_CUBIC_SPLINE_WEIGHTS(u, tx);
  169. SET_CUBIC_SPLINE_WEIGHTS(v, ty);
  170. float4 r = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
  171. for (int y = 0; y < 4; y++) {
  172. for (int x = 0; x < 4; x++) {
  173. float weight = u[x] * v[y];
  174. r += weight * svm_image_texture_read_2d(kg, id, ix + x - 1, iy + y - 1);
  175. }
  176. }
  177. return r;
  178. }
  179. }
  180. ccl_device float4
  181. kernel_tex_image_interp_3d(KernelGlobals *kg, int id, float x, float y, float z, int interp)
  182. {
  183. const ccl_global TextureInfo *info = kernel_tex_info(kg, id);
  184. if (info->extension == EXTENSION_CLIP) {
  185. if (x < 0.0f || y < 0.0f || z < 0.0f || x > 1.0f || y > 1.0f || z > 1.0f) {
  186. return make_float4(0.0f, 0.0f, 0.0f, 0.0f);
  187. }
  188. }
  189. uint interpolation = (interp == INTERPOLATION_NONE) ? info->interpolation : interp;
  190. if (interpolation == INTERPOLATION_CLOSEST) {
  191. /* Closest interpolation. */
  192. int ix, iy, iz;
  193. svm_image_texture_frac(x * info->width, &ix);
  194. svm_image_texture_frac(y * info->height, &iy);
  195. svm_image_texture_frac(z * info->depth, &iz);
  196. return svm_image_texture_read_3d(kg, id, ix, iy, iz);
  197. }
  198. else if (interpolation == INTERPOLATION_LINEAR) {
  199. /* Bilinear interpolation. */
  200. int ix, iy, iz;
  201. float tx = svm_image_texture_frac(x * info->width - 0.5f, &ix);
  202. float ty = svm_image_texture_frac(y * info->height - 0.5f, &iy);
  203. float tz = svm_image_texture_frac(z * info->depth - 0.5f, &iz);
  204. float4 r;
  205. r = (1.0f - tz) * (1.0f - ty) * (1.0f - tx) * svm_image_texture_read_3d(kg, id, ix, iy, iz);
  206. r += (1.0f - tz) * (1.0f - ty) * tx * svm_image_texture_read_3d(kg, id, ix + 1, iy, iz);
  207. r += (1.0f - tz) * ty * (1.0f - tx) * svm_image_texture_read_3d(kg, id, ix, iy + 1, iz);
  208. r += (1.0f - tz) * ty * tx * svm_image_texture_read_3d(kg, id, ix + 1, iy + 1, iz);
  209. r += tz * (1.0f - ty) * (1.0f - tx) * svm_image_texture_read_3d(kg, id, ix, iy, iz + 1);
  210. r += tz * (1.0f - ty) * tx * svm_image_texture_read_3d(kg, id, ix + 1, iy, iz + 1);
  211. r += tz * ty * (1.0f - tx) * svm_image_texture_read_3d(kg, id, ix, iy + 1, iz + 1);
  212. r += tz * ty * tx * svm_image_texture_read_3d(kg, id, ix + 1, iy + 1, iz + 1);
  213. return r;
  214. }
  215. else {
  216. /* Bicubic interpolation. */
  217. int ix, iy, iz;
  218. float tx = svm_image_texture_frac(x * info->width - 0.5f, &ix);
  219. float ty = svm_image_texture_frac(y * info->height - 0.5f, &iy);
  220. float tz = svm_image_texture_frac(z * info->depth - 0.5f, &iz);
  221. float u[4], v[4], w[4];
  222. SET_CUBIC_SPLINE_WEIGHTS(u, tx);
  223. SET_CUBIC_SPLINE_WEIGHTS(v, ty);
  224. SET_CUBIC_SPLINE_WEIGHTS(w, tz);
  225. float4 r = make_float4(0.0f, 0.0f, 0.0f, 0.0f);
  226. for (int z = 0; z < 4; z++) {
  227. for (int y = 0; y < 4; y++) {
  228. for (int x = 0; x < 4; x++) {
  229. float weight = u[x] * v[y] * w[z];
  230. r += weight * svm_image_texture_read_3d(kg, id, ix + x - 1, iy + y - 1, iz + z - 1);
  231. }
  232. }
  233. }
  234. return r;
  235. }
  236. }
  237. #undef SET_CUBIC_SPLINE_WEIGHTS