Blender V2.61 - r43446

shader.h

Go to the documentation of this file.
00001 /*
00002  * Copyright 2011, Blender Foundation.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 
00019 #ifndef __SHADER_H__
00020 #define __SHADER_H__
00021 
00022 #include "attribute.h"
00023 #include "kernel_types.h"
00024 
00025 #include "util_map.h"
00026 #include "util_param.h"
00027 #include "util_string.h"
00028 #include "util_types.h"
00029 
00030 CCL_NAMESPACE_BEGIN
00031 
00032 class Device;
00033 class DeviceScene;
00034 class Mesh;
00035 class Progress;
00036 class Scene;
00037 class ShaderGraph;
00038 struct float3;
00039 
00040 /* Shader describing the appearance of a Mesh, Light or Background.
00041  *
00042  * While there is only a single shader graph, it has three outputs: surface,
00043  * volume and displacement, that the shader manager will compile and execute
00044  * separately. */
00045 
00046 class Shader {
00047 public:
00048     /* name */
00049     string name;
00050 
00051     /* shader graph */
00052     ShaderGraph *graph;
00053 
00054     /* shader graph with auto bump mapping included, we compile two shaders,
00055        with and without bump,  because the displacement method is a mesh
00056        level setting, so we need to handle both */
00057     ShaderGraph *graph_bump;
00058 
00059     /* sampling */
00060     bool sample_as_light;
00061     bool homogeneous_volume;
00062 
00063     /* synchronization */
00064     bool need_update;
00065     bool need_update_attributes;
00066 
00067     /* information about shader after compiling */
00068     bool has_surface;
00069     bool has_surface_emission;
00070     bool has_surface_transparent;
00071     bool has_volume;
00072     bool has_displacement;
00073 
00074     /* requested mesh attributes */
00075     AttributeRequestSet attributes;
00076 
00077     Shader();
00078     ~Shader();
00079 
00080     void set_graph(ShaderGraph *graph);
00081     void tag_update(Scene *scene);
00082 };
00083 
00084 /* Shader Manager virtual base class
00085  * 
00086  * From this the SVM and OSL shader managers are derived, that do the actual
00087  * shader compiling and device updating. */
00088 
00089 class ShaderManager {
00090 public:
00091     bool need_update;
00092 
00093     static ShaderManager *create(Scene *scene);
00094     virtual ~ShaderManager();
00095 
00096     /* device update */
00097     virtual void device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress) = 0;
00098     virtual void device_free(Device *device, DeviceScene *dscene) = 0;
00099 
00100     void device_update_common(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress);
00101     void device_free_common(Device *device, DeviceScene *dscene);
00102 
00103     /* get globally unique id for a type of attribute */
00104     uint get_attribute_id(ustring name);
00105     uint get_attribute_id(Attribute::Standard std);
00106 
00107     /* get shader id for mesh faces */
00108     int get_shader_id(uint shader, Mesh *mesh = NULL, bool smooth = false);
00109 
00110     /* add default shaders to scene, to use as default for things that don't
00111        have any shader assigned explicitly */
00112     static void add_default(Scene *scene);
00113 
00114 protected:
00115     ShaderManager();
00116 
00117     typedef unordered_map<ustring, uint, ustringHash> AttributeIDMap;
00118     AttributeIDMap unique_attribute_id;
00119 };
00120 
00121 CCL_NAMESPACE_END
00122 
00123 #endif /* __SHADER_H__ */
00124