Blender  V2.59
render_update.c
Go to the documentation of this file.
00001 /*
00002  * $Id: render_update.c 36774 2011-05-19 11:34:11Z blendix $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version. 
00010  *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  *
00019  * The Original Code is Copyright (C) 2009 Blender Foundation.
00020  * All rights reserved.
00021  *
00022  * Contributor(s): Blender Foundation
00023  *
00024  * ***** END GPL LICENSE BLOCK *****
00025  */
00026 
00031 #include <stdlib.h>
00032 #include <string.h>
00033 
00034 #include "MEM_guardedalloc.h"
00035 
00036 #include "DNA_lamp_types.h"
00037 #include "DNA_material_types.h"
00038 #include "DNA_node_types.h"
00039 #include "DNA_object_types.h"
00040 #include "DNA_scene_types.h"
00041 #include "DNA_screen_types.h"
00042 #include "DNA_space_types.h"
00043 #include "DNA_view3d_types.h"
00044 #include "DNA_world_types.h"
00045 
00046 #include "BLI_utildefines.h"
00047 
00048 #include "BKE_context.h"
00049 #include "BKE_depsgraph.h"
00050 #include "BKE_icons.h"
00051 #include "BKE_image.h"
00052 #include "BKE_main.h"
00053 #include "BKE_material.h"
00054 #include "BKE_node.h"
00055 #include "BKE_scene.h"
00056 #include "BKE_texture.h"
00057 #include "BKE_world.h"
00058 
00059 #include "GPU_material.h"
00060 
00061 #include "ED_render.h"
00062 
00063 #include "render_intern.h"      // own include
00064 
00065 /***************************** Updates ***********************************
00066  * ED_render_id_flush_update gets called from DAG_id_tag_update, to do *
00067  * editor level updates when the ID changes. when these ID blocks are in *
00068  * the dependency graph, we can get rid of the manual dependency checks  */
00069 
00070 static int mtex_use_tex(MTex **mtex, int tot, Tex *tex)
00071 {
00072         int a;
00073 
00074         if(!mtex)
00075                 return 0;
00076 
00077         for(a=0; a<tot; a++)
00078                 if(mtex[a] && mtex[a]->tex == tex)
00079                         return 1;
00080         
00081         return 0;
00082 }
00083 
00084 static int nodes_use_tex(bNodeTree *ntree, Tex *tex)
00085 {
00086         bNode *node;
00087 
00088         for(node=ntree->nodes.first; node; node= node->next) {
00089                 if(node->id) {
00090                         if(node->id == (ID*)tex) {
00091                                 return 1;
00092                         }
00093                         else if(node->type==NODE_GROUP) {
00094                                 if(nodes_use_tex((bNodeTree *)node->id, tex))
00095                                         return 1;
00096                         }
00097                 }
00098         }
00099 
00100         return 0;
00101 }
00102 
00103 static void material_changed(Main *UNUSED(bmain), Material *ma)
00104 {
00105         /* icons */
00106         BKE_icon_changed(BKE_icon_getid(&ma->id));
00107 
00108         /* glsl */
00109         if(ma->gpumaterial.first)
00110                 GPU_material_free(ma);
00111 }
00112 
00113 static void texture_changed(Main *bmain, Tex *tex)
00114 {
00115         Material *ma;
00116         Lamp *la;
00117         World *wo;
00118 
00119         /* icons */
00120         BKE_icon_changed(BKE_icon_getid(&tex->id));
00121 
00122         /* find materials */
00123         for(ma=bmain->mat.first; ma; ma=ma->id.next) {
00124                 if(mtex_use_tex(ma->mtex, MAX_MTEX, tex));
00125                 else if(ma->use_nodes && ma->nodetree && nodes_use_tex(ma->nodetree, tex));
00126                 else continue;
00127 
00128                 BKE_icon_changed(BKE_icon_getid(&ma->id));
00129 
00130                 if(ma->gpumaterial.first)
00131                         GPU_material_free(ma);
00132         }
00133 
00134         /* find lamps */
00135         for(la=bmain->lamp.first; la; la=la->id.next) {
00136                 if(mtex_use_tex(la->mtex, MAX_MTEX, tex));
00137                 else continue;
00138 
00139                 BKE_icon_changed(BKE_icon_getid(&la->id));
00140         }
00141 
00142         /* find worlds */
00143         for(wo=bmain->world.first; wo; wo=wo->id.next) {
00144                 if(mtex_use_tex(wo->mtex, MAX_MTEX, tex));
00145                 else continue;
00146 
00147                 BKE_icon_changed(BKE_icon_getid(&wo->id));
00148         }
00149 }
00150 
00151 static void lamp_changed(Main *bmain, Lamp *la)
00152 {
00153         Object *ob;
00154         Material *ma;
00155 
00156         /* icons */
00157         BKE_icon_changed(BKE_icon_getid(&la->id));
00158 
00159         /* glsl */
00160         for(ob=bmain->object.first; ob; ob=ob->id.next)
00161                 if(ob->data == la && ob->gpulamp.first)
00162                         GPU_lamp_free(ob);
00163 
00164         for(ma=bmain->mat.first; ma; ma=ma->id.next)
00165                 if(ma->gpumaterial.first)
00166                         GPU_material_free(ma);
00167 }
00168 
00169 static void world_changed(Main *bmain, World *wo)
00170 {
00171         Material *ma;
00172 
00173         /* icons */
00174         BKE_icon_changed(BKE_icon_getid(&wo->id));
00175 
00176         /* glsl */
00177         for(ma=bmain->mat.first; ma; ma=ma->id.next)
00178                 if(ma->gpumaterial.first)
00179                         GPU_material_free(ma);
00180 }
00181 
00182 static void image_changed(Main *bmain, Image *ima)
00183 {
00184         Tex *tex;
00185 
00186         /* icons */
00187         BKE_icon_changed(BKE_icon_getid(&ima->id));
00188 
00189         /* textures */
00190         for(tex=bmain->tex.first; tex; tex=tex->id.next)
00191                 if(tex->ima == ima)
00192                         texture_changed(bmain, tex);
00193 }
00194 
00195 static void scene_changed(Main *bmain, Scene *UNUSED(scene))
00196 {
00197         Object *ob;
00198         Material *ma;
00199 
00200         /* glsl */
00201         for(ob=bmain->object.first; ob; ob=ob->id.next)
00202                 if(ob->gpulamp.first)
00203                         GPU_lamp_free(ob);
00204 
00205         for(ma=bmain->mat.first; ma; ma=ma->id.next)
00206                 if(ma->gpumaterial.first)
00207                         GPU_material_free(ma);
00208 }
00209 
00210 void ED_render_id_flush_update(Main *bmain, ID *id)
00211 {
00212         if(!id)
00213                 return;
00214 
00215         switch(GS(id->name)) {
00216                 case ID_MA:
00217                         material_changed(bmain, (Material*)id);
00218                         break;
00219                 case ID_TE:
00220                         texture_changed(bmain, (Tex*)id);
00221                         break;
00222                 case ID_WO:
00223                         world_changed(bmain, (World*)id);
00224                         break;
00225                 case ID_LA:
00226                         lamp_changed(bmain, (Lamp*)id);
00227                         break;
00228                 case ID_IM:
00229                         image_changed(bmain, (Image*)id);
00230                         break;
00231                 case ID_SCE:
00232                         scene_changed(bmain, (Scene*)id);
00233                         break;
00234                 default:
00235                         break;
00236         }
00237 }
00238