osl.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 __OSL_H__
  17. #define __OSL_H__
  18. #include "util/util_array.h"
  19. #include "util/util_set.h"
  20. #include "util/util_string.h"
  21. #include "util/util_thread.h"
  22. #include "render/graph.h"
  23. #include "render/nodes.h"
  24. #include "render/shader.h"
  25. #ifdef WITH_OSL
  26. # include <OSL/llvm_util.h>
  27. # include <OSL/oslcomp.h>
  28. # include <OSL/oslexec.h>
  29. # include <OSL/oslquery.h>
  30. #endif
  31. CCL_NAMESPACE_BEGIN
  32. class Device;
  33. class DeviceScene;
  34. class ImageManager;
  35. class OSLRenderServices;
  36. struct OSLGlobals;
  37. class Scene;
  38. class ShaderGraph;
  39. class ShaderNode;
  40. class ShaderInput;
  41. class ShaderOutput;
  42. #ifdef WITH_OSL
  43. /* OSL Shader Info
  44. * to auto detect closures in the shader for MIS and transparent shadows */
  45. struct OSLShaderInfo {
  46. OSLShaderInfo()
  47. : has_surface_emission(false), has_surface_transparent(false), has_surface_bssrdf(false)
  48. {
  49. }
  50. OSL::OSLQuery query;
  51. bool has_surface_emission;
  52. bool has_surface_transparent;
  53. bool has_surface_bssrdf;
  54. };
  55. /* Shader Manage */
  56. class OSLShaderManager : public ShaderManager {
  57. public:
  58. OSLShaderManager();
  59. ~OSLShaderManager();
  60. static void free_memory();
  61. void reset(Scene *scene);
  62. bool use_osl()
  63. {
  64. return true;
  65. }
  66. void device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress);
  67. void device_free(Device *device, DeviceScene *dscene, Scene *scene);
  68. /* osl compile and query */
  69. static bool osl_compile(const string &inputfile, const string &outputfile);
  70. static bool osl_query(OSL::OSLQuery &query, const string &filepath);
  71. /* shader file loading, all functions return pointer to hash string if found */
  72. const char *shader_test_loaded(const string &hash);
  73. const char *shader_load_bytecode(const string &hash, const string &bytecode);
  74. const char *shader_load_filepath(string filepath);
  75. OSLShaderInfo *shader_loaded_info(const string &hash);
  76. /* create OSL node using OSLQuery */
  77. OSLNode *osl_node(const std::string &filepath,
  78. const std::string &bytecode_hash = "",
  79. const std::string &bytecode = "");
  80. protected:
  81. void texture_system_init();
  82. void texture_system_free();
  83. void shading_system_init();
  84. void shading_system_free();
  85. OSL::ShadingSystem *ss;
  86. OSL::TextureSystem *ts;
  87. OSLRenderServices *services;
  88. OSL::ErrorHandler errhandler;
  89. map<string, OSLShaderInfo> loaded_shaders;
  90. static OSL::TextureSystem *ts_shared;
  91. static thread_mutex ts_shared_mutex;
  92. static int ts_shared_users;
  93. static OSL::ShadingSystem *ss_shared;
  94. static OSLRenderServices *services_shared;
  95. static thread_mutex ss_shared_mutex;
  96. static thread_mutex ss_mutex;
  97. static int ss_shared_users;
  98. };
  99. #endif
  100. /* Graph Compiler */
  101. class OSLCompiler {
  102. public:
  103. #ifdef WITH_OSL
  104. OSLCompiler(OSLShaderManager *manager,
  105. OSLRenderServices *services,
  106. OSL::ShadingSystem *shadingsys,
  107. ImageManager *image_manager,
  108. LightManager *light_manager);
  109. #endif
  110. void compile(Scene *scene, OSLGlobals *og, Shader *shader);
  111. void add(ShaderNode *node, const char *name, bool isfilepath = false);
  112. void parameter(ShaderNode *node, const char *name);
  113. void parameter(const char *name, float f);
  114. void parameter_color(const char *name, float3 f);
  115. void parameter_vector(const char *name, float3 f);
  116. void parameter_normal(const char *name, float3 f);
  117. void parameter_point(const char *name, float3 f);
  118. void parameter(const char *name, int f);
  119. void parameter(const char *name, const char *s);
  120. void parameter(const char *name, ustring str);
  121. void parameter(const char *name, const Transform &tfm);
  122. void parameter_array(const char *name, const float f[], int arraylen);
  123. void parameter_color_array(const char *name, const array<float3> &f);
  124. void parameter_attribute(const char *name, ustring s);
  125. void parameter_texture(const char *name, ustring filename, ustring colorspace);
  126. void parameter_texture(const char *name, int svm_slot);
  127. void parameter_texture_ies(const char *name, int svm_slot);
  128. ShaderType output_type()
  129. {
  130. return current_type;
  131. }
  132. bool background;
  133. ImageManager *image_manager;
  134. LightManager *light_manager;
  135. private:
  136. #ifdef WITH_OSL
  137. string id(ShaderNode *node);
  138. OSL::ShaderGroupRef compile_type(Shader *shader, ShaderGraph *graph, ShaderType type);
  139. bool node_skip_input(ShaderNode *node, ShaderInput *input);
  140. string compatible_name(ShaderNode *node, ShaderInput *input);
  141. string compatible_name(ShaderNode *node, ShaderOutput *output);
  142. void find_dependencies(ShaderNodeSet &dependencies, ShaderInput *input);
  143. void generate_nodes(const ShaderNodeSet &nodes);
  144. OSLShaderManager *manager;
  145. OSLRenderServices *services;
  146. OSL::ShadingSystem *ss;
  147. #endif
  148. ShaderType current_type;
  149. Shader *current_shader;
  150. static int texture_shared_unique_id;
  151. };
  152. CCL_NAMESPACE_END
  153. #endif /* __OSL_H__ */