mi_shader_if.h

Go to the documentation of this file.
00001 
00002 // Copyright 1986-2007 by mental images GmbH, Fasanenstr. 81, D-10623 Berlin,
00003 // Germany. All rights reserved.
00005 // Author:      kjs
00006 // Created:     09.01.06
00007 // Module:      api
00008 // Purpose:     mental ray C++ shader interface extensions
00010 
00026 
00027 #ifndef MI_SHADER_IF_H
00028 #define MI_SHADER_IF_H
00029 
00030 #if !defined(SHADER_H)
00031 #include <mi_raylib.h>
00032 #include <mi_lib.h>
00033 #include <mi_shader.h>
00034 #endif
00035 
00036 #ifndef __cplusplus
00037 #error mi_shader_if.h requires C++ compilation
00038 #endif
00039 
00040 
00042 static const int mi_ray_interface_version = 2;
00043 
00044 
00045 // forward declarations
00046 namespace mi {
00047     namespace shader {
00048         struct Interface;
00049     }
00050 }
00051 
00052 
00070 
00071 extern "C" mi::shader::Interface *mi_get_shader_interface(
00072     int version = mi_ray_interface_version);
00073 
00075 namespace mi {
00076 
00077 
00083 namespace shader {
00084 
00085 
00087 struct Options;
00088 class LightList;
00089 
00101 struct Framebuffer {
00102     public:
00107         virtual bool set(
00108             const char  *buffer, 
00109             const char  *param, 
00110             const       bool value) = 0;
00111 
00116         virtual bool set(
00117             const char  *buffer, 
00118             const char  *param, 
00119             const float value) = 0;
00120 
00125         virtual bool set(
00126             const char  *buffer, 
00127             const char  *param, 
00128             const int   value) = 0;
00129 
00134         virtual bool set(
00135             const char  *buffer, 
00136             const char  *param, 
00137             const char  *value) = 0;
00138 
00145         virtual bool get(
00146             const char  *buffer, 
00147             const char  *param, 
00148             bool        &value) const = 0;
00149 
00156         virtual bool get(
00157             const char  *buffer, 
00158             const char  *param, 
00159             float       &value) const = 0;
00160 
00167         virtual bool get(
00168             const char  *buffer, 
00169             const char  *param, 
00170             int         &value) const = 0;
00171 
00178         virtual bool get(
00179             const char  *buffer, 
00180             const char  *param, 
00181             const char  *&value) const = 0;
00182 
00189         virtual bool get_index(const char *buffer, size_t &index) const = 0;
00190 
00194         virtual bool reset() = 0;
00195 };
00196 
00198 
00202 class Access_fb_prim {
00203     public:
00204         virtual const Framebuffer * operator->() = 0;
00205         virtual void release() = 0;
00206 };
00207 
00209 
00214 class Access_fb {
00215     public:
00218         Access_fb(
00219             miTag tag);
00220 
00222         ~Access_fb();
00223 
00225         const Framebuffer * operator->();
00226     private:
00227         Access_fb();
00228         Access_fb(const Access_fb &);
00229         const Access_fb & operator=(const Access_fb &);
00230         Access_fb_prim *m_afb;
00231 };
00232 
00234 
00238 
00239 class Edit_fb_prim {
00240     public:
00241         virtual Framebuffer * operator->() = 0;
00242         virtual void release() = 0;
00243 };
00244 
00246 
00250 
00251 class Edit_fb {
00252     public:
00255         Edit_fb(
00256             miTag tag);
00257 
00259         ~Edit_fb();
00260 
00262         Framebuffer * operator->();
00263     private:
00264         Edit_fb();
00265         Edit_fb(const Edit_fb &);
00266         const Edit_fb & operator=(const Edit_fb &);
00267         Edit_fb_prim *m_efb;
00268 };
00269 
00289 
00290 struct Interface {
00307     static inline Interface* get(
00308         int             version = mi_ray_interface_version)
00309     { 
00310         return mi_get_shader_interface(version); 
00311     }
00312 
00326     virtual Options *getOptions(
00327         miTag           string_options);
00328 
00332     virtual miTag createFramebuffer();
00333 
00338     virtual miTag copyFramebuffer(
00339         miTag           old_buffer_tag);
00340 
00342 
00349     virtual Access_fb_prim *accessFramebuffer(
00350         miTag           buffer_tag);
00351 
00358     virtual Edit_fb_prim *editFramebuffer(
00359         miTag           buffer_tag);
00360 
00362 
00374     virtual LightList *createLightList(
00375         miState         *state, 
00376         miTag           *slist = 0, 
00377         int             n = 0);
00378 
00385     virtual void release();
00386 
00387 
00401     virtual LightList *createLightList(
00402         miState         *state,
00403         const miVector  &axis,
00404         miScalar        spread_cos,
00405         miTag           *slist = 0, 
00406         int             n = 0);
00407 };
00408 
00409 
00448 
00449 struct Options {
00451 
00455     virtual void set(
00456         const char      *name, 
00457         bool            value) = 0;
00458 
00464     virtual void set(
00465         const char      *name, 
00466         const char *    value) = 0;
00467 
00472     virtual void set(
00473         const char      *name, 
00474         int             value) = 0;
00475 
00479     virtual void set(
00480         const char      *name, 
00481         float           value) = 0;
00482 
00488     virtual void set(
00489         const char      *name,
00490         float           value1, 
00491         float           value2, 
00492         float           value3) = 0;
00493 
00500     virtual void set(
00501         const char      *name, 
00502         float           value1, 
00503         float           value2, 
00504         float           value3, 
00505         float           value4) = 0;
00506 
00512     virtual bool get(
00513         const char      *name, 
00514         bool            *value) const = 0;
00515 
00523     virtual bool get(
00524         const char      *name, 
00525         const char      **value) const = 0;
00526 
00532     virtual bool get(
00533         const char      *name, 
00534         int             *value) const = 0;
00535 
00544     virtual bool get(
00545         const char      *name, 
00546         float           *value) const = 0;
00547 
00560     virtual bool get(
00561         const char      *name, 
00562         float           *value1, 
00563         float           *value2, 
00564         float           *value3) const = 0;
00565 
00580     virtual bool get(
00581         const char      *name,
00582         float           *value1,
00583         float           *value2,
00584         float           *value3,
00585         float           *value4) const = 0;
00586 
00592     virtual bool remove(
00593         const char      *name) = 0;
00594 
00598     virtual void release() = 0;
00599 };
00600 
00601 
00627 class LightIterator {
00628 public:
00645     LightIterator(                                           
00646         miState*        state,                               
00647         miTag*          shader_light_list = 0,               
00648         int             n_shader_lights = 0,                 
00649         int             version = mi_ray_interface_version); 
00650 
00664     LightIterator(                                           
00665         miState*        state,                               
00666         const miVector  &axis,                               
00667         miScalar        spread_cos,                          
00668         miTag*          shader_light_list = 0,               
00669         int             n_shader_lights = 0,                 
00670         int             version = mi_ray_interface_version); 
00671 
00672 
00678     LightIterator(const LightIterator &liter);      
00679 
00684     ~LightIterator();                               
00685 
00692     miTag operator*() const;                        
00693 
00706     LightList* operator->() const;                  
00707 
00716     const LightIterator& operator++();              
00717 
00727     LightIterator operator++(int);                  
00728 
00733     bool at_end() const;                            
00734 
00738     const LightIterator &operator=(const LightIterator &iter); 
00739 
00745     bool operator==(const LightIterator &iter) const; 
00746 
00752     bool operator!=(const LightIterator &iter) const; 
00753 
00754 private:
00758     LightList* m_list;                              
00759 
00763     size_t m_current;                               
00764 };
00765 
00775 class LightList {
00776 public:
00777 
00785     virtual size_t set_current(size_t current)          = 0; 
00786 
00791     virtual size_t get_current() const                  = 0; 
00792 
00798     virtual bool sample()                               = 0; 
00799 
00804     virtual miScalar get_dot_nl() const                 = 0; 
00805 
00810     virtual const miVector& get_direction() const       = 0; 
00811 
00818     virtual void get_contribution(miColor *c) const     = 0; 
00819 
00826     virtual void get_contribution(miSpectrum *s) const  = 0; 
00827 
00834     virtual int get_number_of_samples() const           = 0; 
00835 
00842     virtual miTag get_light_tag(size_t current) const   = 0; 
00843 
00847     virtual size_t get_number_of_lights() const         = 0; 
00848 
00854     virtual void connect()                              = 0; 
00855 
00861     virtual void release()                              = 0; 
00862 }; 
00863 
00864 //-----------------------------------------------------------------------------
00865 // inline implementation for LightIterator methods
00866 //-----------------------------------------------------------------------------
00867 
00881 inline LightIterator::LightIterator(
00882     miState             *state,
00883     const miVector      &axis,
00884     miScalar            spread_cos,
00885     miTag               *shader_light_list, 
00886     int                 n_shader_lights,
00887     int                 version)
00888 : m_list(0)
00889 , m_current(0)
00890 {
00891     Interface* iface = Interface::get(version);
00892     m_list = iface->createLightList(state, axis, spread_cos, shader_light_list, n_shader_lights);
00893     iface->release();
00894 }
00895 
00896 
00907 inline LightIterator::LightIterator(
00908     miState             *state,
00909     miTag               *shader_light_list, 
00910     int                 n_shader_lights,
00911     int                 version)
00912 : m_list(0)
00913 , m_current(0)
00914 {
00915     Interface* iface = Interface::get(version);
00916     m_list = iface->createLightList(state, state->normal, 0, shader_light_list, n_shader_lights);
00917     iface->release();
00918 }
00919 
00924 inline LightIterator::LightIterator(const LightIterator &iter) 
00925 : m_list(iter.m_list)
00926 , m_current(iter.m_current)
00927 { 
00928     if(m_list) m_list->connect();   
00929 }
00930 
00934 inline LightIterator::~LightIterator() 
00935 {
00936     if(m_list) m_list->release();  
00937 }
00938 
00947 inline const LightIterator& LightIterator::operator=(const LightIterator &iter)
00948 {
00949     if(m_list != iter.m_list) {
00950         // need the compare above, otherwise the release might delete the list
00951         // and the connect below would be invalid.
00952         m_list->release();
00953         m_list = iter.m_list;
00954         m_list->connect();
00955     }
00956     m_current = iter.m_current;
00957     return *this;
00958 }
00959 
00965 inline miTag LightIterator::operator*() const 
00966 {
00967     return m_list->get_light_tag(m_current);
00968 }
00969 
00975 inline LightList* LightIterator::operator->() const
00976 {
00977     m_list->set_current(m_current);
00978     return m_list;
00979 }
00980 
00985 inline const LightIterator& LightIterator::operator++() 
00986 {
00987     ++m_current;
00988     return *this;
00989 }
00990 
00995 inline LightIterator LightIterator::operator++(int)
00996 {
00997     LightIterator res(*this);
00998     ++m_current;
00999     return res;
01000 }
01001 
01005 inline bool LightIterator::at_end() const
01006 {
01007     return m_current == m_list->get_number_of_lights();
01008 }
01009 
01013 inline bool LightIterator::operator==(const LightIterator &iter) const 
01014 {
01015     return m_list == iter.m_list && m_current == iter.m_current;
01016 }
01017 
01021 inline bool LightIterator::operator!=(const LightIterator &iter) const 
01022 {
01023     return !this->operator==(iter);
01024 }
01025 
01028 inline Access_fb::Access_fb(
01029     miTag tag)
01030     : m_afb(0)
01031 {
01032     mi::shader::Interface *iface = mi_get_shader_interface();
01033     m_afb = iface->accessFramebuffer(tag);
01034     iface->release();
01035     iface = 0;
01036 }
01037 
01038 inline Access_fb::~Access_fb()
01039 {
01040     m_afb->release();
01041 }
01042 
01043 inline const Framebuffer * Access_fb::operator->()
01044 {
01045     return (*m_afb).operator->();
01046 }
01047 
01050 inline Edit_fb::Edit_fb(
01051     miTag tag)
01052     : m_efb(0)
01053 {
01054     mi::shader::Interface *iface = mi_get_shader_interface();
01055     m_efb = iface->editFramebuffer(tag);
01056     iface->release();
01057     iface = 0;
01058 }
01059 
01060 inline Edit_fb::~Edit_fb()
01061 {
01062     m_efb->release();
01063 }
01064 
01065 inline Framebuffer * Edit_fb::operator->()
01066 {
01067     return (*m_efb).operator->();
01068 }
01069 
01070 } // namespace shader
01071 } // namespace mi
01072 
01073 #endif // MI_SHADER_IF_H



Copyright © 1986-2007 by mental images GmbH