shader.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #ifndef __SHADER_H__
  17. #define __SHADER_H__
  18. #ifdef WITH_OSL
  19. /* So no context pollution happens from indirectly included windows.h */
  20. # include "util/util_windows.h"
  21. # include <OSL/oslexec.h>
  22. #endif
  23. #include "render/attribute.h"
  24. #include "kernel/kernel_types.h"
  25. #include "graph/node.h"
  26. #include "util/util_map.h"
  27. #include "util/util_param.h"
  28. #include "util/util_string.h"
  29. #include "util/util_thread.h"
  30. #include "util/util_types.h"
  31. CCL_NAMESPACE_BEGIN
  32. class Device;
  33. class DeviceScene;
  34. class DeviceRequestedFeatures;
  35. class Mesh;
  36. class Progress;
  37. class Scene;
  38. class ShaderGraph;
  39. struct float3;
  40. enum ShadingSystem { SHADINGSYSTEM_OSL, SHADINGSYSTEM_SVM };
  41. /* Keep those in sync with the python-defined enum. */
  42. enum VolumeSampling {
  43. VOLUME_SAMPLING_DISTANCE = 0,
  44. VOLUME_SAMPLING_EQUIANGULAR = 1,
  45. VOLUME_SAMPLING_MULTIPLE_IMPORTANCE = 2,
  46. VOLUME_NUM_SAMPLING,
  47. };
  48. enum VolumeInterpolation {
  49. VOLUME_INTERPOLATION_LINEAR = 0,
  50. VOLUME_INTERPOLATION_CUBIC = 1,
  51. VOLUME_NUM_INTERPOLATION,
  52. };
  53. enum DisplacementMethod {
  54. DISPLACE_BUMP = 0,
  55. DISPLACE_TRUE = 1,
  56. DISPLACE_BOTH = 2,
  57. DISPLACE_NUM_METHODS,
  58. };
  59. /* Shader describing the appearance of a Mesh, Light or Background.
  60. *
  61. * While there is only a single shader graph, it has three outputs: surface,
  62. * volume and displacement, that the shader manager will compile and execute
  63. * separately. */
  64. class Shader : public Node {
  65. public:
  66. NODE_DECLARE
  67. int pass_id;
  68. /* shader graph */
  69. ShaderGraph *graph;
  70. /* sampling */
  71. bool use_mis;
  72. bool use_transparent_shadow;
  73. bool heterogeneous_volume;
  74. VolumeSampling volume_sampling_method;
  75. int volume_interpolation_method;
  76. /* synchronization */
  77. bool need_update;
  78. bool need_update_mesh;
  79. bool need_sync_object;
  80. /* If the shader has only volume components, the surface is assumed to
  81. * be transparent.
  82. * However, graph optimization might remove the volume subgraph, but
  83. * since the user connected something to the volume output the surface
  84. * should still be transparent.
  85. * Therefore, has_volume_connected stores whether some volume subtree
  86. * was connected before optimization. */
  87. bool has_volume_connected;
  88. /* information about shader after compiling */
  89. bool has_surface;
  90. bool has_surface_emission;
  91. bool has_surface_transparent;
  92. bool has_volume;
  93. bool has_displacement;
  94. bool has_surface_bssrdf;
  95. bool has_bump;
  96. bool has_bssrdf_bump;
  97. bool has_surface_spatial_varying;
  98. bool has_volume_spatial_varying;
  99. bool has_object_dependency;
  100. bool has_attribute_dependency;
  101. bool has_integrator_dependency;
  102. /* displacement */
  103. DisplacementMethod displacement_method;
  104. /* requested mesh attributes */
  105. AttributeRequestSet attributes;
  106. /* determined before compiling */
  107. uint id;
  108. bool used;
  109. #ifdef WITH_OSL
  110. /* osl shading state references */
  111. OSL::ShaderGroupRef osl_surface_ref;
  112. OSL::ShaderGroupRef osl_surface_bump_ref;
  113. OSL::ShaderGroupRef osl_volume_ref;
  114. OSL::ShaderGroupRef osl_displacement_ref;
  115. #endif
  116. Shader();
  117. ~Shader();
  118. /* Checks whether the shader consists of just a emission node with fixed inputs that's connected
  119. * directly to the output.
  120. * If yes, it sets the content of emission to the constant value (color * strength), which is
  121. * then used for speeding up light evaluation. */
  122. bool is_constant_emission(float3 *emission);
  123. void set_graph(ShaderGraph *graph);
  124. void tag_update(Scene *scene);
  125. void tag_used(Scene *scene);
  126. };
  127. /* Shader Manager virtual base class
  128. *
  129. * From this the SVM and OSL shader managers are derived, that do the actual
  130. * shader compiling and device updating. */
  131. class ShaderManager {
  132. public:
  133. bool need_update;
  134. static ShaderManager *create(Scene *scene, int shadingsystem);
  135. virtual ~ShaderManager();
  136. virtual void reset(Scene *scene) = 0;
  137. virtual bool use_osl()
  138. {
  139. return false;
  140. }
  141. /* device update */
  142. virtual void device_update(Device *device,
  143. DeviceScene *dscene,
  144. Scene *scene,
  145. Progress &progress) = 0;
  146. virtual void device_free(Device *device, DeviceScene *dscene, Scene *scene) = 0;
  147. void device_update_shaders_used(Scene *scene);
  148. void device_update_common(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress);
  149. void device_free_common(Device *device, DeviceScene *dscene, Scene *scene);
  150. /* get globally unique id for a type of attribute */
  151. uint get_attribute_id(ustring name);
  152. uint get_attribute_id(AttributeStandard std);
  153. /* get shader id for mesh faces */
  154. int get_shader_id(Shader *shader, bool smooth = false);
  155. /* add default shaders to scene, to use as default for things that don't
  156. * have any shader assigned explicitly */
  157. static void add_default(Scene *scene);
  158. /* Selective nodes compilation. */
  159. void get_requested_features(Scene *scene, DeviceRequestedFeatures *requested_features);
  160. static void free_memory();
  161. float linear_rgb_to_gray(float3 c);
  162. string get_cryptomatte_materials(Scene *scene);
  163. protected:
  164. ShaderManager();
  165. typedef unordered_map<ustring, uint, ustringHash> AttributeIDMap;
  166. AttributeIDMap unique_attribute_id;
  167. static thread_mutex lookup_table_mutex;
  168. static vector<float> beckmann_table;
  169. static bool beckmann_table_ready;
  170. size_t beckmann_table_offset;
  171. void get_requested_graph_features(ShaderGraph *graph,
  172. DeviceRequestedFeatures *requested_features);
  173. thread_spin_lock attribute_lock_;
  174. float3 xyz_to_r;
  175. float3 xyz_to_g;
  176. float3 xyz_to_b;
  177. float3 rgb_to_y;
  178. };
  179. CCL_NAMESPACE_END
  180. #endif /* __SHADER_H__ */