Advertisement
Guest User

oslexe.h

a guest
May 29th, 2011
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.91 KB | None | 0 0
  1. /*
  2. Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
  3. All Rights Reserved.
  4.  
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are
  7. met:
  8. * Redistributions of source code must retain the above copyright
  9.   notice, this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright
  11.   notice, this list of conditions and the following disclaimer in the
  12.   documentation and/or other materials provided with the distribution.
  13. * Neither the name of Sony Pictures Imageworks nor the names of its
  14.   contributors may be used to endorse or promote products derived from
  15.   this software without specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28.  
  29. #ifndef OSLEXEC_H
  30. #define OSLEXEC_H
  31.  
  32.  
  33. #include "oslconfig.h"
  34. // osl_pvt.h is required for 'Runflags' definition
  35. #include "osl_pvt.h"
  36.  
  37. #include "OpenImageIO/refcnt.h"             // just to get shared_ptr from boost ?!
  38. #include "OpenImageIO/ustring.h"
  39.  
  40.  
  41. #ifdef OSL_NAMESPACE
  42. namespace OSL_NAMESPACE {
  43. #endif
  44.  
  45. namespace OSL {
  46.  
  47. class RendererServices;
  48. class ShadingAttribState;
  49. typedef shared_ptr<ShadingAttribState> ShadingAttribStateRef;
  50. struct ShaderGlobals;
  51. struct ClosureColor;
  52. struct ClosureParam;
  53.  
  54. namespace pvt {
  55. class ShadingContext;
  56. };
  57.  
  58.  
  59. /// Opaque pointer to whatever the renderer uses to represent a
  60. /// (potentially motion-blurred) coordinate transformation.
  61. typedef const void * TransformationPtr;
  62.  
  63.  
  64. // Callbacks for closure creation
  65. typedef void (*PrepareClosureFunc)(RendererServices *, int id, void *data);
  66. typedef void (*SetupClosureFunc)(RendererServices *, int id, void *data);
  67. typedef bool (*CompareClosureFunc)(int id, const void *dataA, const void *dataB);
  68.  
  69.  
  70. class OSLEXECPUBLIC ShadingSystem
  71. {
  72. protected:
  73.     /// ShadingSystem is an abstract class, its constructor is protected
  74.     /// so that ordinary users can't make an instance, but instead are
  75.     /// forced to request one via ShadingSystem::create().
  76.     ShadingSystem ();
  77.     virtual ~ShadingSystem ();
  78.  
  79. public:
  80.     static ShadingSystem *create (RendererServices *renderer=NULL,
  81.                                   TextureSystem *texturesystem=NULL,
  82.                                   ErrorHandler *err=NULL);
  83.     static void destroy (ShadingSystem *x);
  84.  
  85.     /// Set an attribute controlling the texture system.  Return true
  86.     /// if the name and type were recognized and the attrib was set.
  87.     /// Documented attributes:
  88.     ///
  89.     virtual bool attribute (const std::string &name, TypeDesc type,
  90.                             const void *val) = 0;
  91.     // Shortcuts for common types
  92.     bool attribute (const std::string &name, int val) {
  93.         return attribute (name, TypeDesc::INT, &val);
  94.     }
  95.     bool attribute (const std::string &name, float val) {
  96.         return attribute (name, TypeDesc::FLOAT, &val);
  97.     }
  98.     bool attribute (const std::string &name, double val) {
  99.         float f = (float) val;
  100.         return attribute (name, TypeDesc::FLOAT, &f);
  101.     }
  102.     bool attribute (const std::string &name, const char *val) {
  103.         return attribute (name, TypeDesc::STRING, &val);
  104.     }
  105.     bool attribute (const std::string &name, const std::string &val) {
  106.         const char *s = val.c_str();
  107.         return attribute (name, TypeDesc::STRING, &s);
  108.     }
  109.  
  110.     /// Get the named attribute, store it in value.
  111.     ///
  112.     virtual bool getattribute (const std::string &name, TypeDesc type,
  113.                                void *val) = 0;
  114.     // Shortcuts for common types
  115.     bool getattribute (const std::string &name, int &val) {
  116.         return getattribute (name, TypeDesc::INT, &val);
  117.     }
  118.     bool getattribute (const std::string &name, float &val) {
  119.         return getattribute (name, TypeDesc::FLOAT, &val);
  120.     }
  121.     bool getattribute (const std::string &name, double &val) {
  122.         float f;
  123.         bool ok = getattribute (name, TypeDesc::FLOAT, &f);
  124.         if (ok)
  125.             val = f;
  126.         return ok;
  127.     }
  128.     bool getattribute (const std::string &name, char **val) {
  129.         return getattribute (name, TypeDesc::STRING, val);
  130.     }
  131.     bool getattribute (const std::string &name, std::string &val) {
  132.         const char *s = NULL;
  133.         bool ok = getattribute (name, TypeDesc::STRING, &s);
  134.         if (ok)
  135.             val = s;
  136.         return ok;
  137.     }
  138.  
  139.     /// Set a parameter of the next shader.
  140.     ///
  141.     virtual bool Parameter (const char *name, TypeDesc t, const void *val)
  142.         { return true; }
  143. #if 0
  144.     virtual bool Parameter (const char *name, int val) {
  145.         Parameter (name, TypeDesc::IntType, &val);
  146.     }
  147.     virtual bool Parameter (const char *name, float val) {
  148.         Parameter (name, TypeDesc::FloatType, &val);
  149.     }
  150.     virtual bool Parameter (const char *name, double val) {}
  151.     virtual bool Parameter (const char *name, const char *val) {}
  152.     virtual bool Parameter (const char *name, const std::string &val) {}
  153.     virtual bool Parameter (const char *name, TypeDesc t, const int *val) {}
  154.     virtual bool Parameter (const char *name, TypeDesc t, const float *val) {}
  155.     virtual bool Parameter (const char *name, TypeDesc t, const char **val) {}
  156. #endif
  157.  
  158.     /// Create a new shader instance, either replacing the one for the
  159.     /// specified usage (if not within a group) or appending to the
  160.     /// current group (if a group has been started).
  161.     virtual bool Shader (const char *shaderusage,
  162.                          const char *shadername=NULL,
  163.                          const char *layername=NULL) = 0;
  164.  
  165.     /// Signal the start of a new shader group.
  166.     ///
  167.     virtual bool ShaderGroupBegin (void) = 0;
  168.  
  169.     /// Signal the end of a new shader group.
  170.     ///
  171.     virtual bool ShaderGroupEnd (void) = 0;
  172.  
  173.     /// Connect two shaders within the current group
  174.     ///
  175.     virtual bool ConnectShaders (const char *srclayer, const char *srcparam,
  176.                                  const char *dstlayer, const char *dstparam)=0;
  177.  
  178.     /// Return a reference-counted (but opaque) reference to the current
  179.     /// shading attribute state maintained by the ShadingSystem.
  180.     virtual ShadingAttribStateRef state () const = 0;
  181.  
  182.     /// Clear the current shading attribute state, i.e., no shaders
  183.     /// specified.
  184.     virtual void clear_state () = 0;
  185.  
  186.     /// Return the statistics output as a huge string.
  187.     ///
  188.     virtual std::string getstats (int level=1) const = 0;
  189.  
  190.     virtual void register_closure(const char *name, int id, const ClosureParam *params, int size,
  191.                                   PrepareClosureFunc prepare, SetupClosureFunc setup, CompareClosureFunc compare) = 0;
  192.  
  193.     void register_builtin_closures();
  194.  
  195. private:
  196.     // Make delete private and unimplemented in order to prevent apps
  197.     // from calling it.  Instead, they should call ShadingSystem::destroy().
  198.     void operator delete (void *todel) { }
  199. };
  200.  
  201.  
  202.  
  203. /// This struct represents the global variables accessible from a shader, note
  204. /// that not all fields will be valid in all contexts.
  205. ///
  206. /// All points, vectors and normals are given in "common" space.
  207. struct ShaderGlobals {
  208.     Vec3 P, dPdx, dPdy;              /**< Position */
  209.     Vec3 I, dIdx, dIdy;              /**< Incident ray */
  210.     Vec3 N;                          /**< Shading normal */
  211.     Vec3 Ng;                         /**< True geometric normal */
  212.     float u, dudx, dudy;             /**< Surface parameter u */
  213.     float v, dvdx, dvdy;             /**< Surface parameter v */
  214.     Vec3 dPdu, dPdv;                 /**< Tangents on the surface */
  215.     float time;                      /**< Time for each sample */
  216.     float dtime;                     /**< Time interval for each sample */
  217.     Vec3 dPdtime;                    /**< Velocity */
  218.     Vec3 Ps, dPsdx, dPsdy;           /**< Point being lit (valid only in light
  219.                                           attenuation shaders */
  220.     void* renderstate;               /**< Opaque pointer to renderer state (can
  221.                                           be used to retrieve renderer specific
  222.                                           details like userdata) */
  223.     void* tracedata;                 /**< Opaque pointer to renderer state
  224.                                           resuling from a trace() call. */
  225.     pvt::ShadingContext* context;    /**< ShadingContext (this will be set by
  226.                                           OSL itself) */
  227.     TransformationPtr object2common; /**< Object->common xform */
  228.     TransformationPtr shader2common; /**< Shader->common xform */
  229.     ClosureColor *Ci;                /**< Output closure (should be initialized
  230.                                           to NULL) */
  231.     float surfacearea;               /**< Total area of the object (defined by
  232.                                           light shaders for energy normalization) */
  233.     int raytype;                     /**< Bit field of ray type flags */
  234.     int flipHandedness;              /**< flips the result of calculatenormal() */
  235.     int backfacing;                  /**< True if we want are shading the
  236.                                           backside of the surface */
  237. };
  238.  
  239.  
  240.  
  241. /// RendererServices defines an abstract interface through which a
  242. /// renderer may provide callback to the ShadingSystem.
  243. class OSLEXECPUBLIC RendererServices {
  244. public:
  245.     RendererServices () { }
  246.     virtual ~RendererServices () { }
  247.  
  248.     /// Get the 4x4 matrix that transforms by the specified
  249.     /// transformation at the given time.
  250.     virtual bool get_matrix (Matrix44 &result, TransformationPtr xform,
  251.                              float time) = 0;
  252.  
  253.     /// Get the 4x4 matrix that transforms by the specified
  254.     /// transformation at the given time.  The default implementation is
  255.     /// to use get_matrix and invert it, but a particular renderer may
  256.     /// have a better technique and overload the implementation.
  257.     virtual bool get_inverse_matrix (Matrix44 &result, TransformationPtr xform,
  258.                                      float time);
  259.  
  260.     /// Get the 4x4 matrix that transforms points from the named
  261.     /// 'from' coordinate system to "common" space at the given time.
  262.     virtual bool get_matrix (Matrix44 &result, ustring from, float time) = 0;
  263.  
  264.     /// Get the named attribute from the renderer and if found then
  265.     /// write it into 'val'.  Otherwise, return false.  If no object is
  266.     /// specified (object == ustring()), then the renderer should search *first*
  267.     /// for the attribute on the currently shaded object, and next, if
  268.     /// unsuccessful, on the currently shaded "scene".
  269.     virtual bool get_attribute (void *renderstate, bool derivatives,
  270.                                 ustring object, TypeDesc type, ustring name,
  271.                                 void *val ) = 0;
  272.  
  273.     /// Similar to get_attribute();  this method will return the 'index'
  274.     /// element of an attribute array.
  275.     virtual bool get_array_attribute (void *renderstate, bool derivatives,
  276.                                       ustring object, TypeDesc type,
  277.                                       ustring name, int index, void *val ) = 0;
  278.  
  279.     /// Get the named user-data from the current object and write it into
  280.     /// 'val'. If derivatives is true, the derivatives should be written into val
  281.     /// as well. Return false if no user-data with the given name and type was
  282.     /// found.
  283.     virtual bool get_userdata (bool derivatives, ustring name, TypeDesc type,
  284.                                void *renderstate, void *val) = 0;
  285.  
  286.     /// Does the current object have the named user-data associated with it?
  287.     virtual bool has_userdata (ustring name, TypeDesc type, void *renderstate) = 0;
  288.  
  289.     /// Get the 4x4 matrix that transforms points from "common" space to
  290.     /// the named 'to' coordinate system to at the given time.  The
  291.     /// default implementation is to use get_matrix and invert it, but a
  292.     /// particular renderer may have a better technique and overload the
  293.     /// implementation.
  294.     virtual bool get_inverse_matrix (Matrix44 &result, ustring to, float time);
  295.  
  296.     /// Filtered 2D texture lookup for a single point.
  297.     ///
  298.     /// s,t are the texture coordinates; dsdx, dtdx, dsdy, and dtdy are
  299.     /// the differentials of s and t change in some canonical directions
  300.     /// x and y.  The choice of x and y are not important to the
  301.     /// implementation; it can be any imposed 2D coordinates, such as
  302.     /// pixels in screen space, adjacent samples in parameter space on a
  303.     /// surface, etc.
  304.     ///
  305.     /// Return true if the file is found and could be opened, otherwise
  306.     /// return false.
  307.     virtual bool texture (ustring filename, TextureOpt &options,
  308.                           ShaderGlobals *sg,
  309.                           float s, float t, float dsdx, float dtdx,
  310.                           float dsdy, float dtdy, float *result);
  311.  
  312.     /// Filtered 3D texture lookup for a single point.
  313.     ///
  314.     /// P is the volumetric texture coordinate; dPd{x,y,z} are the
  315.     /// differentials of P in some canonical directions x, y, and z.
  316.     /// The choice of x,y,z are not important to the implementation; it
  317.     /// can be any imposed 3D coordinates, such as pixels in screen
  318.     /// space and depth along the ray, etc.
  319.     ///
  320.     /// Return true if the file is found and could be opened, otherwise
  321.     /// return false.
  322.     virtual bool texture3d (ustring filename, TextureOpt &options,
  323.                             ShaderGlobals *sg, const Vec3 &P,
  324.                             const Vec3 &dPdx, const Vec3 &dPdy,
  325.                             const Vec3 &dPdz, float *result);
  326.  
  327.     /// Filtered environment lookup for a single point.
  328.     ///
  329.     /// R is the directional texture coordinate; dRd[xy] are the
  330.     /// differentials of R in canonical directions x, y.
  331.     ///
  332.     /// Return true if the file is found and could be opened, otherwise
  333.     /// return false.
  334.     virtual bool environment (ustring filename, TextureOpt &options,
  335.                               ShaderGlobals *sg, const Vec3 &R,
  336.                               const Vec3 &dRdx, const Vec3 &dRdy, float *result);
  337.  
  338.     /// Get information about the given texture.  Return true if found
  339.     /// and the data has been put in *data.  Return false if the texture
  340.     /// doesn't exist, doesn't have the requested data, if the data
  341.     /// doesn't match the type requested. or some other failure.
  342.     virtual bool get_texture_info (ustring filename, int subimage,
  343.                                    ustring dataname, TypeDesc datatype,
  344.                                    void *data);
  345.  
  346.     /// Get a handle to a query object. A query is a list of attribute names
  347.     /// given in the attr_names array, and their corresponding types given in
  348.     /// attr_types. The returned handle will be valid until RendererServices
  349.     /// is destroyed and can be used to perform queries with the pointcloud
  350.     /// method below.
  351.     ///
  352.     /// Be aware this is a function we never call during the render. It is
  353.     /// not used from the shader. LLVM gen code calls it once for each specific
  354.     /// pointcloud call it finds in the code. That provides a handle that is
  355.     /// used as a constant in the shader throughout the rest of the render. It
  356.     /// never changes since a pointcloud call has always the same type profile
  357.     /// for the returned data in that specific line of code.
  358.     ///
  359.     /// So the renderer can cache and optimize any possible things associated
  360.     /// with this call and link it to the handle. From that point, the shader
  361.     /// will always use it when calling pointcloud. Even if the renderer
  362.     /// doesn't optimize anything, we already save some arguments.
  363.     ///
  364.     ///   For more insight look at llvm_gen_pointcloud in llvm_instance.cpp
  365.     ///
  366.     virtual void *get_pointcloud_attr_query (ustring *attr_names,
  367.                                              TypeDesc *attr_types, int nattrs) = 0;
  368.  
  369.     /// Lookup nearest points in a point cloud. It will search for points
  370.     /// around the given center within the specified radius. attr_outdata
  371.     /// is an array of pointers to arrays of elements of the appropiate type.
  372.     /// Its length has to be the same as the number of attributes passed
  373.     /// when the query object was created with get_pointcloud_attr_query, and
  374.     /// the type of the referenced data arrays has to match the types of the
  375.     /// query object too. Those arrays will be filled with the found points
  376.     /// up to max_points. So they have to be allocated with enough space.
  377.     ///
  378.     /// attr_query is a special handle created by get_pointcloud_attr_query
  379.     /// When we find a call to pointcloud in the shader we get one of those
  380.     /// handlers and then compile this call with it in attr_query as a constant
  381.     virtual int pointcloud (ustring filename, const OSL::Vec3 &center, float radius,
  382.                             int max_points, void *attr_query, void **attr_outdata) = 0;
  383.  
  384.     /// Options for the trace call.
  385.     struct TraceOpt {
  386.         float mindist;   ///< ignore hits closer than this
  387.         float maxdist;   ///< ignore hits farther than this
  388.         bool shade;      ///< whether to shade what is hit
  389.         TraceOpt () : mindist(0.0f), maxdist(1.0e30), shade(false) { }
  390.     };
  391.  
  392.     /// Immediately trace a ray from P in the direction R.  Return true
  393.     /// if anything hit, otherwise false.
  394.     virtual bool trace (TraceOpt &options, ShaderGlobals *sg,
  395.                         const OSL::Vec3 &P, const OSL::Vec3 &dPdx,
  396.                         const OSL::Vec3 &dPdy, const OSL::Vec3 &R,
  397.                         const OSL::Vec3 &dRdx, const OSL::Vec3 &dRdy) {
  398.         return false;
  399.     }
  400.  
  401.     /// Get the named message from the renderer and if found then
  402.     /// write it into 'val'.  Otherwise, return false.  This is only
  403.     /// called for "sourced" messages, not ordinary intra-group messages.
  404.     virtual bool getmessage (ShaderGlobals *sg, ustring source, ustring name,
  405.                              TypeDesc type, void *val, bool derivatives) {
  406.         return false;
  407.     }
  408.  
  409. private:
  410.     TextureSystem *m_texturesys;   // For default texture implementation
  411. };
  412.  
  413.  
  414. }; // namespace OSL
  415.  
  416. #ifdef OSL_NAMESPACE
  417. }; // end namespace OSL_NAMESPACE
  418. using namespace OSL_NAMESPACE;
  419. #endif
  420.  
  421.  
  422. #endif /* OSLEXEC_H */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement