Blender  V2.59
rna_render.c
Go to the documentation of this file.
00001 /*
00002  * $Id: rna_render.c 38092 2011-07-04 19:22:37Z jbakker $
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  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * Contributor(s): Blender Foundation (2009)
00021  *
00022  * ***** END GPL LICENSE BLOCK *****
00023  */
00024 
00030 #include <stdlib.h>
00031 
00032 #include "DNA_scene_types.h"
00033 
00034 #include "RNA_define.h"
00035 #include "RNA_enum_types.h"
00036 
00037 #include "rna_internal.h"
00038 
00039 #include "RE_pipeline.h"
00040 
00041 #include "BKE_utildefines.h"
00042 
00043 #ifdef RNA_RUNTIME
00044 
00045 #include "MEM_guardedalloc.h"
00046 
00047 #include "RNA_access.h"
00048 
00049 #include "BKE_context.h"
00050 #include "BKE_report.h"
00051 
00052 /* RenderEngine */
00053 
00054 static RenderEngineType internal_render_type = {
00055         NULL, NULL, "BLENDER_RENDER", "Blender Render", RE_INTERNAL, NULL, {NULL, NULL, NULL, NULL}};
00056 #ifdef WITH_GAMEENGINE
00057 static RenderEngineType internal_game_type = {
00058         NULL, NULL, "BLENDER_GAME", "Blender Game", RE_INTERNAL|RE_GAME, NULL, {NULL, NULL, NULL, NULL}};
00059 #endif
00060 
00061 ListBase R_engines = {NULL, NULL};
00062 
00063 void RE_engines_init(void)
00064 {
00065         BLI_addtail(&R_engines, &internal_render_type);
00066 #ifdef WITH_GAMEENGINE
00067         BLI_addtail(&R_engines, &internal_game_type);
00068 #endif
00069 }
00070 
00071 void RE_engines_exit(void)
00072 {
00073         RenderEngineType *type, *next;
00074 
00075         for(type=R_engines.first; type; type=next) {
00076                 next= type->next;
00077 
00078                 BLI_remlink(&R_engines, type);
00079 
00080                 if(!(type->flag & RE_INTERNAL)) {
00081                         if(type->ext.free)
00082                                 type->ext.free(type->ext.data);
00083 
00084                         MEM_freeN(type);
00085                 }
00086         }
00087 }
00088 
00089 static void engine_render(RenderEngine *engine, struct Scene *scene)
00090 {
00091         PointerRNA ptr;
00092         ParameterList list;
00093         FunctionRNA *func;
00094 
00095         RNA_pointer_create(NULL, engine->type->ext.srna, engine, &ptr);
00096         func= RNA_struct_find_function(&ptr, "render");
00097 
00098         RNA_parameter_list_create(&list, &ptr, func);
00099         RNA_parameter_set_lookup(&list, "scene", &scene);
00100         engine->type->ext.call(NULL, &ptr, func, &list);
00101 
00102         RNA_parameter_list_free(&list);
00103 }
00104 
00105 static void rna_RenderEngine_unregister(Main *UNUSED(bmain), StructRNA *type)
00106 {
00107         RenderEngineType *et= RNA_struct_blender_type_get(type);
00108 
00109         if(!et)
00110                 return;
00111         
00112         RNA_struct_free_extension(type, &et->ext);
00113         BLI_freelinkN(&R_engines, et);
00114         RNA_struct_free(&BLENDER_RNA, type);
00115 }
00116 
00117 static StructRNA *rna_RenderEngine_register(Main *bmain, ReportList *reports, void *data, const char *identifier, StructValidateFunc validate, StructCallbackFunc call, StructFreeFunc free)
00118 {
00119         RenderEngineType *et, dummyet = {NULL};
00120         RenderEngine dummyengine= {NULL};
00121         PointerRNA dummyptr;
00122         int have_function[1];
00123 
00124         /* setup dummy engine & engine type to store static properties in */
00125         dummyengine.type= &dummyet;
00126         RNA_pointer_create(NULL, &RNA_RenderEngine, &dummyengine, &dummyptr);
00127 
00128         /* validate the python class */
00129         if(validate(&dummyptr, data, have_function) != 0)
00130                 return NULL;
00131 
00132         if(strlen(identifier) >= sizeof(dummyet.idname)) {
00133                 BKE_reportf(reports, RPT_ERROR, "registering render engine class: '%s' is too long, maximum length is %d.", identifier, (int)sizeof(dummyet.idname));
00134                 return NULL;
00135         }
00136 
00137         /* check if we have registered this engine type before, and remove it */
00138         for(et=R_engines.first; et; et=et->next) {
00139                 if(strcmp(et->idname, dummyet.idname) == 0) {
00140                         if(et->ext.srna)
00141                                 rna_RenderEngine_unregister(bmain, et->ext.srna);
00142                         break;
00143                 }
00144         }
00145         
00146         /* create a new engine type */
00147         et= MEM_callocN(sizeof(RenderEngineType), "python buttons engine");
00148         memcpy(et, &dummyet, sizeof(dummyet));
00149 
00150         et->ext.srna= RNA_def_struct(&BLENDER_RNA, et->idname, "RenderEngine"); 
00151         et->ext.data= data;
00152         et->ext.call= call;
00153         et->ext.free= free;
00154         RNA_struct_blender_type_set(et->ext.srna, et);
00155 
00156         et->render= (have_function[0])? engine_render: NULL;
00157 
00158         BLI_addtail(&R_engines, et);
00159 
00160         return et->ext.srna;
00161 }
00162 
00163 static StructRNA* rna_RenderEngine_refine(PointerRNA *ptr)
00164 {
00165         RenderEngine *engine= (RenderEngine*)ptr->data;
00166         return (engine->type && engine->type->ext.srna)? engine->type->ext.srna: &RNA_RenderEngine;
00167 }
00168 
00169 static void rna_RenderResult_layers_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
00170 {
00171         RenderResult *rr= (RenderResult*)ptr->data;
00172         rna_iterator_listbase_begin(iter, &rr->layers, NULL);
00173 }
00174 
00175 static void rna_RenderLayer_passes_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
00176 {
00177         RenderLayer *rl= (RenderLayer*)ptr->data;
00178         rna_iterator_listbase_begin(iter, &rl->passes, NULL);
00179 }
00180 
00181 static int rna_RenderLayer_rect_get_length(PointerRNA *ptr, int length[RNA_MAX_ARRAY_DIMENSION])
00182 {
00183         RenderLayer *rl= (RenderLayer*)ptr->data;
00184 
00185         length[0]= rl->rectx*rl->recty;
00186         length[1]= 4;
00187 
00188         return length[0]*length[1];
00189 }
00190 
00191 static void rna_RenderLayer_rect_get(PointerRNA *ptr, float *values)
00192 {
00193         RenderLayer *rl= (RenderLayer*)ptr->data;
00194         memcpy(values, rl->rectf, sizeof(float)*rl->rectx*rl->recty*4);
00195 }
00196 
00197 static void rna_RenderLayer_rect_set(PointerRNA *ptr, const float *values)
00198 {
00199         RenderLayer *rl= (RenderLayer*)ptr->data;
00200         memcpy(rl->rectf, values, sizeof(float)*rl->rectx*rl->recty*4);
00201 }
00202 
00203 static int rna_RenderPass_rect_get_length(PointerRNA *ptr, int length[RNA_MAX_ARRAY_DIMENSION])
00204 {
00205         RenderPass *rpass= (RenderPass*)ptr->data;
00206 
00207         length[0]= rpass->rectx*rpass->recty;
00208         length[1]= rpass->channels;
00209 
00210         return length[0]*length[1];
00211 }
00212 
00213 static void rna_RenderPass_rect_get(PointerRNA *ptr, float *values)
00214 {
00215         RenderPass *rpass= (RenderPass*)ptr->data;
00216         memcpy(values, rpass->rect, sizeof(float)*rpass->rectx*rpass->recty*rpass->channels);
00217 }
00218 
00219 static void rna_RenderPass_rect_set(PointerRNA *ptr, const float *values)
00220 {
00221         RenderPass *rpass= (RenderPass*)ptr->data;
00222         memcpy(rpass->rect, values, sizeof(float)*rpass->rectx*rpass->recty*rpass->channels);
00223 }
00224 
00225 #else // RNA_RUNTIME
00226 
00227 static void rna_def_render_engine(BlenderRNA *brna)
00228 {
00229         StructRNA *srna;
00230         PropertyRNA *prop;
00231         FunctionRNA *func;
00232         
00233         srna= RNA_def_struct(brna, "RenderEngine", NULL);
00234         RNA_def_struct_sdna(srna, "RenderEngine");
00235         RNA_def_struct_ui_text(srna, "Render Engine", "Render engine");
00236         RNA_def_struct_refine_func(srna, "rna_RenderEngine_refine");
00237         RNA_def_struct_register_funcs(srna, "rna_RenderEngine_register", "rna_RenderEngine_unregister", NULL);
00238 
00239         /* render */
00240         func= RNA_def_function(srna, "render", NULL);
00241         RNA_def_function_ui_description(func, "Render scene into an image.");
00242         RNA_def_function_flag(func, FUNC_REGISTER);
00243         RNA_def_pointer(func, "scene", "Scene", "", "");
00244 
00245         func= RNA_def_function(srna, "begin_result", "RE_engine_begin_result");
00246         prop= RNA_def_int(func, "x", 0, 0, INT_MAX, "X", "", 0, INT_MAX);
00247         RNA_def_property_flag(prop, PROP_REQUIRED);
00248         prop= RNA_def_int(func, "y", 0, 0, INT_MAX, "Y", "", 0, INT_MAX);
00249         RNA_def_property_flag(prop, PROP_REQUIRED);
00250         prop= RNA_def_int(func, "w", 0, 0, INT_MAX, "Width", "", 0, INT_MAX);
00251         RNA_def_property_flag(prop, PROP_REQUIRED);
00252         prop= RNA_def_int(func, "h", 0, 0, INT_MAX, "Height", "", 0, INT_MAX);
00253         RNA_def_property_flag(prop, PROP_REQUIRED);
00254         prop= RNA_def_pointer(func, "result", "RenderResult", "Result", "");
00255         RNA_def_function_return(func, prop);
00256 
00257         func= RNA_def_function(srna, "update_result", "RE_engine_update_result");
00258         prop= RNA_def_pointer(func, "result", "RenderResult", "Result", "");
00259         RNA_def_property_flag(prop, PROP_REQUIRED);
00260 
00261         func= RNA_def_function(srna, "end_result", "RE_engine_end_result");
00262         prop= RNA_def_pointer(func, "result", "RenderResult", "Result", "");
00263         RNA_def_property_flag(prop, PROP_REQUIRED);
00264 
00265         func= RNA_def_function(srna, "test_break", "RE_engine_test_break");
00266         prop= RNA_def_boolean(func, "do_break", 0, "Break", "");
00267         RNA_def_function_return(func, prop);
00268 
00269         func= RNA_def_function(srna, "update_stats", "RE_engine_update_stats");
00270         prop= RNA_def_string(func, "stats", "", 0, "Stats", "");
00271         RNA_def_property_flag(prop, PROP_REQUIRED);
00272         prop= RNA_def_string(func, "info", "", 0, "Info", "");
00273         RNA_def_property_flag(prop, PROP_REQUIRED);
00274 
00275         func= RNA_def_function(srna, "report", "RE_engine_report");
00276         prop= RNA_def_enum_flag(func, "type", wm_report_items, 0, "Type", "");
00277         RNA_def_property_flag(prop, PROP_REQUIRED);
00278         prop= RNA_def_string(func, "message", "", 0, "Report Message", "");
00279         RNA_def_property_flag(prop, PROP_REQUIRED);
00280 
00281         /* registration */
00282         RNA_define_verify_sdna(0);
00283 
00284         prop= RNA_def_property(srna, "bl_idname", PROP_STRING, PROP_NONE);
00285         RNA_def_property_string_sdna(prop, NULL, "type->idname");
00286         RNA_def_property_flag(prop, PROP_REGISTER|PROP_NEVER_CLAMP);
00287 
00288         prop= RNA_def_property(srna, "bl_label", PROP_STRING, PROP_NONE);
00289         RNA_def_property_string_sdna(prop, NULL, "type->name");
00290         RNA_def_property_flag(prop, PROP_REGISTER);
00291 
00292         prop= RNA_def_property(srna, "bl_use_preview", PROP_BOOLEAN, PROP_NONE);
00293         RNA_def_property_boolean_sdna(prop, NULL, "type->flag", RE_DO_PREVIEW);
00294         RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
00295 
00296         prop= RNA_def_property(srna, "bl_use_postprocess", PROP_BOOLEAN, PROP_NONE);
00297         RNA_def_property_boolean_negative_sdna(prop, NULL, "type->flag", RE_DO_ALL);
00298         RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
00299 
00300         RNA_define_verify_sdna(1);
00301 }
00302 
00303 static void rna_def_render_result(BlenderRNA *brna)
00304 {
00305         StructRNA *srna;
00306         FunctionRNA *func;
00307         PropertyRNA *parm;
00308         
00309         srna= RNA_def_struct(brna, "RenderResult", NULL);
00310         RNA_def_struct_ui_text(srna, "Render Result", "Result of rendering, including all layers and passes");
00311 
00312         func= RNA_def_function(srna, "load_from_file", "RE_result_load_from_file");
00313         RNA_def_function_ui_description(func, "Copies the pixels of this render result from an image file.");
00314         RNA_def_function_flag(func, FUNC_USE_REPORTS);
00315         parm= RNA_def_string_file_name(func, "filename", "", FILE_MAX, "File Name", "Filename to load into this render tile, must be no smaller than the render result");
00316         RNA_def_property_flag(parm, PROP_REQUIRED);
00317 
00318         RNA_define_verify_sdna(0);
00319 
00320         parm= RNA_def_property(srna, "resolution_x", PROP_INT, PROP_NONE);
00321         RNA_def_property_int_sdna(parm, NULL, "rectx");
00322         RNA_def_property_clear_flag(parm, PROP_EDITABLE);
00323 
00324         parm= RNA_def_property(srna, "resolution_y", PROP_INT, PROP_NONE);
00325         RNA_def_property_int_sdna(parm, NULL, "recty");
00326         RNA_def_property_clear_flag(parm, PROP_EDITABLE);
00327 
00328         parm= RNA_def_property(srna, "layers", PROP_COLLECTION, PROP_NONE);
00329         RNA_def_property_struct_type(parm, "RenderLayer");
00330         RNA_def_property_collection_funcs(parm, "rna_RenderResult_layers_begin", "rna_iterator_listbase_next", "rna_iterator_listbase_end", "rna_iterator_listbase_get", 0, 0, 0);
00331 
00332         RNA_define_verify_sdna(1);
00333 }
00334 
00335 static void rna_def_render_layer(BlenderRNA *brna)
00336 {
00337         StructRNA *srna;
00338         PropertyRNA *prop;
00339         FunctionRNA *func;
00340         
00341         srna= RNA_def_struct(brna, "RenderLayer", NULL);
00342         RNA_def_struct_ui_text(srna, "Render Layer", "");
00343 
00344         func= RNA_def_function(srna, "load_from_file", "RE_layer_load_from_file");
00345         RNA_def_function_ui_description(func, "Copies the pixels of this renderlayer from an image file.");
00346         RNA_def_function_flag(func, FUNC_USE_REPORTS);
00347         prop= RNA_def_string(func, "filename", "", 0, "Filename", "Filename to load into this render tile, must be no smaller than the renderlayer");
00348         RNA_def_property_flag(prop, PROP_REQUIRED);
00349         RNA_def_int(func, "x", 0, 0, INT_MAX, "Offset X", "Offset the position to copy from if the image is larger than the render layer", 0, INT_MAX);
00350         RNA_def_int(func, "y", 0, 0, INT_MAX, "Offset Y", "Offset the position to copy from if the image is larger than the render layer", 0, INT_MAX);
00351 
00352         RNA_define_verify_sdna(0);
00353 
00354         rna_def_render_layer_common(srna, 0);
00355 
00356         prop= RNA_def_property(srna, "passes", PROP_COLLECTION, PROP_NONE);
00357         RNA_def_property_struct_type(prop, "RenderPass");
00358         RNA_def_property_collection_funcs(prop, "rna_RenderLayer_passes_begin", "rna_iterator_listbase_next", "rna_iterator_listbase_end", "rna_iterator_listbase_get", 0, 0, 0);
00359 
00360         prop= RNA_def_property(srna, "rect", PROP_FLOAT, PROP_NONE);
00361         RNA_def_property_flag(prop, PROP_DYNAMIC);
00362         RNA_def_property_multi_array(prop, 2, NULL);
00363         RNA_def_property_dynamic_array_funcs(prop, "rna_RenderLayer_rect_get_length");
00364         RNA_def_property_float_funcs(prop, "rna_RenderLayer_rect_get", "rna_RenderLayer_rect_set", NULL);
00365 
00366         RNA_define_verify_sdna(1);
00367 }
00368 
00369 static void rna_def_render_pass(BlenderRNA *brna)
00370 {
00371         StructRNA *srna;
00372         PropertyRNA *prop;
00373 
00374         static EnumPropertyItem pass_type_items[]= {
00375                 {SCE_PASS_COMBINED, "COMBINED", 0, "Combined", ""},
00376                 {SCE_PASS_Z, "Z", 0, "Z", ""},
00377                 {SCE_PASS_RGBA, "COLOR", 0, "Color", ""},
00378                 {SCE_PASS_DIFFUSE, "DIFFUSE", 0, "Diffuse", ""},
00379                 {SCE_PASS_SPEC, "SPECULAR", 0, "Specular", ""},
00380                 {SCE_PASS_SHADOW, "SHADOW", 0, "Shadow", ""},
00381                 {SCE_PASS_AO, "AO", 0, "AO", ""},
00382                 {SCE_PASS_REFLECT, "REFLECTION", 0, "Reflection", ""},
00383                 {SCE_PASS_NORMAL, "NORMAL", 0, "Normal", ""},
00384                 {SCE_PASS_VECTOR, "VECTOR", 0, "Vector", ""},
00385                 {SCE_PASS_REFRACT, "REFRACTION", 0, "Refraction", ""},
00386                 {SCE_PASS_INDEXOB, "OBJECT_INDEX", 0, "Object Index", ""},
00387                 {SCE_PASS_UV, "UV", 0, "UV", ""},
00388                 {SCE_PASS_MIST, "MIST", 0, "Mist", ""},
00389                 {SCE_PASS_EMIT, "EMIT", 0, "Emit", ""},
00390                 {SCE_PASS_ENVIRONMENT, "ENVIRONMENT", 0, "Environment", ""},
00391                 {SCE_PASS_INDEXMA, "MATERIAL_INDEX", 0, "Material Index", ""},
00392                 {0, NULL, 0, NULL, NULL}};
00393         
00394         srna= RNA_def_struct(brna, "RenderPass", NULL);
00395         RNA_def_struct_ui_text(srna, "Render Pass", "");
00396 
00397         RNA_define_verify_sdna(0);
00398 
00399         prop= RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
00400         RNA_def_property_string_sdna(prop, NULL, "name");
00401         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
00402         RNA_def_struct_name_property(srna, prop);
00403 
00404         prop= RNA_def_property(srna, "channel_id", PROP_STRING, PROP_NONE);
00405         RNA_def_property_string_sdna(prop, NULL, "chan_id");
00406         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
00407 
00408         prop= RNA_def_property(srna, "channels", PROP_INT, PROP_NONE);
00409         RNA_def_property_int_sdna(prop, NULL, "channels");
00410         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
00411 
00412         prop= RNA_def_property(srna, "type", PROP_ENUM, PROP_NONE);
00413         RNA_def_property_enum_sdna(prop, NULL, "passtype");
00414         RNA_def_property_enum_items(prop, pass_type_items);
00415         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
00416 
00417         prop= RNA_def_property(srna, "rect", PROP_FLOAT, PROP_NONE);
00418         RNA_def_property_flag(prop, PROP_DYNAMIC);
00419         RNA_def_property_multi_array(prop, 2, NULL);
00420         RNA_def_property_dynamic_array_funcs(prop, "rna_RenderPass_rect_get_length");
00421         RNA_def_property_float_funcs(prop, "rna_RenderPass_rect_get", "rna_RenderPass_rect_set", NULL);
00422 
00423         RNA_define_verify_sdna(1);
00424 }
00425 
00426 void RNA_def_render(BlenderRNA *brna)
00427 {
00428         rna_def_render_engine(brna);
00429         rna_def_render_result(brna);
00430         rna_def_render_layer(brna);
00431         rna_def_render_pass(brna);
00432 }
00433 
00434 #endif // RNA_RUNTIME
00435