kernel_globals.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. /* Constant Globals */
  17. #ifndef __KERNEL_GLOBALS_H__
  18. #define __KERNEL_GLOBALS_H__
  19. #include "kernel/kernel_profiling.h"
  20. #ifdef __KERNEL_CPU__
  21. # include "util/util_vector.h"
  22. # include "util/util_map.h"
  23. #endif
  24. #ifdef __KERNEL_OPENCL__
  25. # include "util/util_atomic.h"
  26. #endif
  27. CCL_NAMESPACE_BEGIN
  28. /* On the CPU, we pass along the struct KernelGlobals to nearly everywhere in
  29. * the kernel, to access constant data. These are all stored as "textures", but
  30. * these are really just standard arrays. We can't use actually globals because
  31. * multiple renders may be running inside the same process. */
  32. #ifdef __KERNEL_CPU__
  33. # ifdef __OSL__
  34. struct OSLGlobals;
  35. struct OSLThreadData;
  36. struct OSLShadingSystem;
  37. # endif
  38. typedef unordered_map<float, float> CoverageMap;
  39. struct Intersection;
  40. struct VolumeStep;
  41. typedef struct KernelGlobals {
  42. # define KERNEL_TEX(type, name) texture<type> name;
  43. # include "kernel/kernel_textures.h"
  44. KernelData __data;
  45. # ifdef __OSL__
  46. /* On the CPU, we also have the OSL globals here. Most data structures are shared
  47. * with SVM, the difference is in the shaders and object/mesh attributes. */
  48. OSLGlobals *osl;
  49. OSLShadingSystem *osl_ss;
  50. OSLThreadData *osl_tdata;
  51. # endif
  52. /* **** Run-time data **** */
  53. /* Heap-allocated storage for transparent shadows intersections. */
  54. Intersection *transparent_shadow_intersections;
  55. /* Storage for decoupled volume steps. */
  56. VolumeStep *decoupled_volume_steps[2];
  57. int decoupled_volume_steps_index;
  58. /* A buffer for storing per-pixel coverage for Cryptomatte. */
  59. CoverageMap *coverage_object;
  60. CoverageMap *coverage_material;
  61. CoverageMap *coverage_asset;
  62. /* split kernel */
  63. SplitData split_data;
  64. SplitParams split_param_data;
  65. int2 global_size;
  66. int2 global_id;
  67. ProfilingState profiler;
  68. } KernelGlobals;
  69. #endif /* __KERNEL_CPU__ */
  70. /* For CUDA, constant memory textures must be globals, so we can't put them
  71. * into a struct. As a result we don't actually use this struct and use actual
  72. * globals and simply pass along a NULL pointer everywhere, which we hope gets
  73. * optimized out. */
  74. #ifdef __KERNEL_CUDA__
  75. __constant__ KernelData __data;
  76. typedef struct KernelGlobals {
  77. /* NOTE: Keep the size in sync with SHADOW_STACK_MAX_HITS. */
  78. Intersection hits_stack[64];
  79. } KernelGlobals;
  80. # define KERNEL_TEX(type, name) const __constant__ __device__ type *name;
  81. # include "kernel/kernel_textures.h"
  82. #endif /* __KERNEL_CUDA__ */
  83. /* OpenCL */
  84. #ifdef __KERNEL_OPENCL__
  85. # define KERNEL_TEX(type, name) typedef type name##_t;
  86. # include "kernel/kernel_textures.h"
  87. typedef ccl_addr_space struct KernelGlobals {
  88. ccl_constant KernelData *data;
  89. ccl_global char *buffers[8];
  90. # define KERNEL_TEX(type, name) TextureInfo name;
  91. # include "kernel/kernel_textures.h"
  92. # ifdef __SPLIT_KERNEL__
  93. SplitData split_data;
  94. SplitParams split_param_data;
  95. # endif
  96. } KernelGlobals;
  97. # define KERNEL_BUFFER_PARAMS \
  98. ccl_global char *buffer0, ccl_global char *buffer1, ccl_global char *buffer2, \
  99. ccl_global char *buffer3, ccl_global char *buffer4, ccl_global char *buffer5, \
  100. ccl_global char *buffer6, ccl_global char *buffer7
  101. # define KERNEL_BUFFER_ARGS buffer0, buffer1, buffer2, buffer3, buffer4, buffer5, buffer6, buffer7
  102. ccl_device_inline void kernel_set_buffer_pointers(KernelGlobals *kg, KERNEL_BUFFER_PARAMS)
  103. {
  104. # ifdef __SPLIT_KERNEL__
  105. if (ccl_local_id(0) + ccl_local_id(1) == 0)
  106. # endif
  107. {
  108. kg->buffers[0] = buffer0;
  109. kg->buffers[1] = buffer1;
  110. kg->buffers[2] = buffer2;
  111. kg->buffers[3] = buffer3;
  112. kg->buffers[4] = buffer4;
  113. kg->buffers[5] = buffer5;
  114. kg->buffers[6] = buffer6;
  115. kg->buffers[7] = buffer7;
  116. }
  117. # ifdef __SPLIT_KERNEL__
  118. ccl_barrier(CCL_LOCAL_MEM_FENCE);
  119. # endif
  120. }
  121. ccl_device_inline void kernel_set_buffer_info(KernelGlobals *kg)
  122. {
  123. # ifdef __SPLIT_KERNEL__
  124. if (ccl_local_id(0) + ccl_local_id(1) == 0)
  125. # endif
  126. {
  127. ccl_global TextureInfo *info = (ccl_global TextureInfo *)kg->buffers[0];
  128. # define KERNEL_TEX(type, name) kg->name = *(info++);
  129. # include "kernel/kernel_textures.h"
  130. }
  131. # ifdef __SPLIT_KERNEL__
  132. ccl_barrier(CCL_LOCAL_MEM_FENCE);
  133. # endif
  134. }
  135. #endif /* __KERNEL_OPENCL__ */
  136. /* Interpolated lookup table access */
  137. ccl_device float lookup_table_read(KernelGlobals *kg, float x, int offset, int size)
  138. {
  139. x = saturate(x) * (size - 1);
  140. int index = min(float_to_int(x), size - 1);
  141. int nindex = min(index + 1, size - 1);
  142. float t = x - index;
  143. float data0 = kernel_tex_fetch(__lookup_table, index + offset);
  144. if (t == 0.0f)
  145. return data0;
  146. float data1 = kernel_tex_fetch(__lookup_table, nindex + offset);
  147. return (1.0f - t) * data0 + t * data1;
  148. }
  149. ccl_device float lookup_table_read_2D(
  150. KernelGlobals *kg, float x, float y, int offset, int xsize, int ysize)
  151. {
  152. y = saturate(y) * (ysize - 1);
  153. int index = min(float_to_int(y), ysize - 1);
  154. int nindex = min(index + 1, ysize - 1);
  155. float t = y - index;
  156. float data0 = lookup_table_read(kg, x, offset + xsize * index, xsize);
  157. if (t == 0.0f)
  158. return data0;
  159. float data1 = lookup_table_read(kg, x, offset + xsize * nindex, xsize);
  160. return (1.0f - t) * data0 + t * data1;
  161. }
  162. CCL_NAMESPACE_END
  163. #endif /* __KERNEL_GLOBALS_H__ */