88722a1d30570ae87988c7bba4abf3c52edbf440
[blender-staging.git] / source / blender / makesrna / intern / rna_render.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s): Blender Foundation (2009)
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/makesrna/intern/rna_render.c
26  *  \ingroup RNA
27  */
28
29
30 #include <stdlib.h>
31
32 #include "DNA_scene_types.h"
33
34 #include "RNA_define.h"
35
36 #include "rna_internal.h"
37
38 #include "RE_pipeline.h"
39
40 #include "BKE_utildefines.h"
41
42 #ifdef RNA_RUNTIME
43
44 #include "MEM_guardedalloc.h"
45
46 #include "RNA_access.h"
47
48 #include "BKE_context.h"
49 #include "BKE_report.h"
50
51 /* RenderEngine */
52
53 static RenderEngineType internal_render_type = {
54         NULL, NULL, "BLENDER_RENDER", "Blender Render", RE_INTERNAL, NULL, {NULL, NULL, NULL, NULL}};
55 #ifdef WITH_GAMEENGINE
56 static RenderEngineType internal_game_type = {
57         NULL, NULL, "BLENDER_GAME", "Blender Game", RE_INTERNAL|RE_GAME, NULL, {NULL, NULL, NULL, NULL}};
58 #endif
59
60 ListBase R_engines = {NULL, NULL};
61
62 void RE_engines_init(void)
63 {
64         BLI_addtail(&R_engines, &internal_render_type);
65 #ifdef WITH_GAMEENGINE
66         BLI_addtail(&R_engines, &internal_game_type);
67 #endif
68 }
69
70 void RE_engines_exit(void)
71 {
72         RenderEngineType *type, *next;
73
74         for(type=R_engines.first; type; type=next) {
75                 next= type->next;
76
77                 BLI_remlink(&R_engines, type);
78
79                 if(!(type->flag & RE_INTERNAL)) {
80                         if(type->ext.free)
81                                 type->ext.free(type->ext.data);
82
83                         MEM_freeN(type);
84                 }
85         }
86 }
87
88 static void engine_render(RenderEngine *engine, struct Scene *scene)
89 {
90         PointerRNA ptr;
91         ParameterList list;
92         FunctionRNA *func;
93
94         RNA_pointer_create(NULL, engine->type->ext.srna, engine, &ptr);
95         func= RNA_struct_find_function(&ptr, "render");
96
97         RNA_parameter_list_create(&list, &ptr, func);
98         RNA_parameter_set_lookup(&list, "scene", &scene);
99         engine->type->ext.call(NULL, &ptr, func, &list);
100
101         RNA_parameter_list_free(&list);
102 }
103
104 static void rna_RenderEngine_unregister(Main *bmain, StructRNA *type)
105 {
106         RenderEngineType *et= RNA_struct_blender_type_get(type);
107
108         if(!et)
109                 return;
110         
111         RNA_struct_free_extension(type, &et->ext);
112         BLI_freelinkN(&R_engines, et);
113         RNA_struct_free(&BLENDER_RNA, type);
114 }
115
116 static StructRNA *rna_RenderEngine_register(Main *bmain, ReportList *reports, void *data, const char *identifier, StructValidateFunc validate, StructCallbackFunc call, StructFreeFunc free)
117 {
118         RenderEngineType *et, dummyet = {NULL};
119         RenderEngine dummyengine= {NULL};
120         PointerRNA dummyptr;
121         int have_function[1];
122
123         /* setup dummy engine & engine type to store static properties in */
124         dummyengine.type= &dummyet;
125         RNA_pointer_create(NULL, &RNA_RenderEngine, &dummyengine, &dummyptr);
126
127         /* validate the python class */
128         if(validate(&dummyptr, data, have_function) != 0)
129                 return NULL;
130
131         if(strlen(identifier) >= sizeof(dummyet.idname)) {
132                 BKE_reportf(reports, RPT_ERROR, "registering render engine class: '%s' is too long, maximum length is %d.", identifier, (int)sizeof(dummyet.idname));
133                 return NULL;
134         }
135
136         /* check if we have registered this engine type before, and remove it */
137         for(et=R_engines.first; et; et=et->next) {
138                 if(strcmp(et->idname, dummyet.idname) == 0) {
139                         if(et->ext.srna)
140                                 rna_RenderEngine_unregister(bmain, et->ext.srna);
141                         break;
142                 }
143         }
144         
145         /* create a new engine type */
146         et= MEM_callocN(sizeof(RenderEngineType), "python buttons engine");
147         memcpy(et, &dummyet, sizeof(dummyet));
148
149         et->ext.srna= RNA_def_struct(&BLENDER_RNA, et->idname, "RenderEngine"); 
150         et->ext.data= data;
151         et->ext.call= call;
152         et->ext.free= free;
153         RNA_struct_blender_type_set(et->ext.srna, et);
154
155         et->render= (have_function[0])? engine_render: NULL;
156
157         BLI_addtail(&R_engines, et);
158
159         return et->ext.srna;
160 }
161
162 static StructRNA* rna_RenderEngine_refine(PointerRNA *ptr)
163 {
164         RenderEngine *engine= (RenderEngine*)ptr->data;
165         return (engine->type && engine->type->ext.srna)? engine->type->ext.srna: &RNA_RenderEngine;
166 }
167
168 static void rna_RenderResult_layers_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
169 {
170         RenderResult *rr= (RenderResult*)ptr->data;
171         rna_iterator_listbase_begin(iter, &rr->layers, NULL);
172 }
173
174 static void rna_RenderLayer_passes_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
175 {
176         RenderLayer *rl= (RenderLayer*)ptr->data;
177         rna_iterator_listbase_begin(iter, &rl->passes, NULL);
178 }
179
180 static int rna_RenderLayer_rect_get_length(PointerRNA *ptr, int length[RNA_MAX_ARRAY_DIMENSION])
181 {
182         RenderLayer *rl= (RenderLayer*)ptr->data;
183
184         length[0]= rl->rectx*rl->recty;
185         length[1]= 4;
186
187         return length[0]*length[1];
188 }
189
190 static void rna_RenderLayer_rect_get(PointerRNA *ptr, float *values)
191 {
192         RenderLayer *rl= (RenderLayer*)ptr->data;
193         memcpy(values, rl->rectf, sizeof(float)*rl->rectx*rl->recty*4);
194 }
195
196 static void rna_RenderLayer_rect_set(PointerRNA *ptr, const float *values)
197 {
198         RenderLayer *rl= (RenderLayer*)ptr->data;
199         memcpy(rl->rectf, values, sizeof(float)*rl->rectx*rl->recty*4);
200 }
201
202 static int rna_RenderPass_rect_get_length(PointerRNA *ptr, int length[RNA_MAX_ARRAY_DIMENSION])
203 {
204         RenderPass *rpass= (RenderPass*)ptr->data;
205
206         length[0]= rpass->rectx*rpass->recty;
207         length[1]= rpass->channels;
208
209         return length[0]*length[1];
210 }
211
212 static void rna_RenderPass_rect_get(PointerRNA *ptr, float *values)
213 {
214         RenderPass *rpass= (RenderPass*)ptr->data;
215         memcpy(values, rpass->rect, sizeof(float)*rpass->rectx*rpass->recty*rpass->channels);
216 }
217
218 static void rna_RenderPass_rect_set(PointerRNA *ptr, const float *values)
219 {
220         RenderPass *rpass= (RenderPass*)ptr->data;
221         memcpy(rpass->rect, values, sizeof(float)*rpass->rectx*rpass->recty*rpass->channels);
222 }
223
224 #else // RNA_RUNTIME
225
226 static void rna_def_render_engine(BlenderRNA *brna)
227 {
228         StructRNA *srna;
229         PropertyRNA *prop;
230         FunctionRNA *func;
231         
232         srna= RNA_def_struct(brna, "RenderEngine", NULL);
233         RNA_def_struct_sdna(srna, "RenderEngine");
234         RNA_def_struct_ui_text(srna, "Render Engine", "Render engine");
235         RNA_def_struct_refine_func(srna, "rna_RenderEngine_refine");
236         RNA_def_struct_register_funcs(srna, "rna_RenderEngine_register", "rna_RenderEngine_unregister");
237
238         /* render */
239         func= RNA_def_function(srna, "render", NULL);
240         RNA_def_function_ui_description(func, "Render scene into an image.");
241         RNA_def_function_flag(func, FUNC_REGISTER);
242         RNA_def_pointer(func, "scene", "Scene", "", "");
243
244         func= RNA_def_function(srna, "begin_result", "RE_engine_begin_result");
245         prop= RNA_def_int(func, "x", 0, 0, INT_MAX, "X", "", 0, INT_MAX);
246         RNA_def_property_flag(prop, PROP_REQUIRED);
247         prop= RNA_def_int(func, "y", 0, 0, INT_MAX, "Y", "", 0, INT_MAX);
248         RNA_def_property_flag(prop, PROP_REQUIRED);
249         prop= RNA_def_int(func, "w", 0, 0, INT_MAX, "Width", "", 0, INT_MAX);
250         RNA_def_property_flag(prop, PROP_REQUIRED);
251         prop= RNA_def_int(func, "h", 0, 0, INT_MAX, "Height", "", 0, INT_MAX);
252         RNA_def_property_flag(prop, PROP_REQUIRED);
253         prop= RNA_def_pointer(func, "result", "RenderResult", "Result", "");
254         RNA_def_function_return(func, prop);
255
256         func= RNA_def_function(srna, "update_result", "RE_engine_update_result");
257         prop= RNA_def_pointer(func, "result", "RenderResult", "Result", "");
258         RNA_def_property_flag(prop, PROP_REQUIRED);
259
260         func= RNA_def_function(srna, "end_result", "RE_engine_end_result");
261         prop= RNA_def_pointer(func, "result", "RenderResult", "Result", "");
262         RNA_def_property_flag(prop, PROP_REQUIRED);
263
264         func= RNA_def_function(srna, "test_break", "RE_engine_test_break");
265         prop= RNA_def_boolean(func, "do_break", 0, "Break", "");
266         RNA_def_function_return(func, prop);
267
268         func= RNA_def_function(srna, "update_stats", "RE_engine_update_stats");
269         prop= RNA_def_string(func, "stats", "", 0, "Stats", "");
270         RNA_def_property_flag(prop, PROP_REQUIRED);
271         prop= RNA_def_string(func, "info", "", 0, "Info", "");
272         RNA_def_property_flag(prop, PROP_REQUIRED);
273
274         /* registration */
275         RNA_define_verify_sdna(0);
276
277         prop= RNA_def_property(srna, "bl_idname", PROP_STRING, PROP_NONE);
278         RNA_def_property_string_sdna(prop, NULL, "type->idname");
279         RNA_def_property_flag(prop, PROP_REGISTER|PROP_NEVER_CLAMP);
280
281         prop= RNA_def_property(srna, "bl_label", PROP_STRING, PROP_NONE);
282         RNA_def_property_string_sdna(prop, NULL, "type->name");
283         RNA_def_property_flag(prop, PROP_REGISTER);
284
285         prop= RNA_def_property(srna, "bl_use_preview", PROP_BOOLEAN, PROP_NONE);
286         RNA_def_property_boolean_sdna(prop, NULL, "type->flag", RE_DO_PREVIEW);
287         RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
288
289         prop= RNA_def_property(srna, "bl_use_postprocess", PROP_BOOLEAN, PROP_NONE);
290         RNA_def_property_boolean_negative_sdna(prop, NULL, "type->flag", RE_DO_ALL);
291         RNA_def_property_flag(prop, PROP_REGISTER_OPTIONAL);
292
293         RNA_define_verify_sdna(1);
294 }
295
296 static void rna_def_render_result(BlenderRNA *brna)
297 {
298         StructRNA *srna;
299         FunctionRNA *func;
300         PropertyRNA *parm;
301         
302         srna= RNA_def_struct(brna, "RenderResult", NULL);
303         RNA_def_struct_ui_text(srna, "Render Result", "Result of rendering, including all layers and passes");
304
305         func= RNA_def_function(srna, "load_from_file", "RE_result_load_from_file");
306         RNA_def_function_ui_description(func, "Copies the pixels of this render result from an image file.");
307         RNA_def_function_flag(func, FUNC_USE_REPORTS);
308         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");
309         RNA_def_property_flag(parm, PROP_REQUIRED);
310
311         RNA_define_verify_sdna(0);
312
313         parm= RNA_def_property(srna, "resolution_x", PROP_INT, PROP_NONE);
314         RNA_def_property_int_sdna(parm, NULL, "rectx");
315         RNA_def_property_clear_flag(parm, PROP_EDITABLE);
316
317         parm= RNA_def_property(srna, "resolution_y", PROP_INT, PROP_NONE);
318         RNA_def_property_int_sdna(parm, NULL, "recty");
319         RNA_def_property_clear_flag(parm, PROP_EDITABLE);
320
321         parm= RNA_def_property(srna, "layers", PROP_COLLECTION, PROP_NONE);
322         RNA_def_property_struct_type(parm, "RenderLayer");
323         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);
324
325         RNA_define_verify_sdna(1);
326 }
327
328 static void rna_def_render_layer(BlenderRNA *brna)
329 {
330         StructRNA *srna;
331         PropertyRNA *prop;
332         FunctionRNA *func;
333         
334         srna= RNA_def_struct(brna, "RenderLayer", NULL);
335         RNA_def_struct_ui_text(srna, "Render Layer", "");
336
337         func= RNA_def_function(srna, "load_from_file", "RE_layer_load_from_file");
338         RNA_def_function_ui_description(func, "Copies the pixels of this renderlayer from an image file.");
339         RNA_def_function_flag(func, FUNC_USE_REPORTS);
340         prop= RNA_def_string(func, "filename", "", 0, "Filename", "Filename to load into this render tile, must be no smaller than the renderlayer");
341         RNA_def_property_flag(prop, PROP_REQUIRED);
342         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);
343         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);
344
345         RNA_define_verify_sdna(0);
346
347         rna_def_render_layer_common(srna, 0);
348
349         prop= RNA_def_property(srna, "passes", PROP_COLLECTION, PROP_NONE);
350         RNA_def_property_struct_type(prop, "RenderPass");
351         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);
352
353         prop= RNA_def_property(srna, "rect", PROP_FLOAT, PROP_NONE);
354         RNA_def_property_flag(prop, PROP_DYNAMIC);
355         RNA_def_property_multi_array(prop, 2, NULL);
356         RNA_def_property_dynamic_array_funcs(prop, "rna_RenderLayer_rect_get_length");
357         RNA_def_property_float_funcs(prop, "rna_RenderLayer_rect_get", "rna_RenderLayer_rect_set", NULL);
358
359         RNA_define_verify_sdna(1);
360 }
361
362 static void rna_def_render_pass(BlenderRNA *brna)
363 {
364         StructRNA *srna;
365         PropertyRNA *prop;
366
367         static EnumPropertyItem pass_type_items[]= {
368                 {SCE_PASS_COMBINED, "COMBINED", 0, "Combined", ""},
369                 {SCE_PASS_Z, "Z", 0, "Z", ""},
370                 {SCE_PASS_RGBA, "COLOR", 0, "Color", ""},
371                 {SCE_PASS_DIFFUSE, "DIFFUSE", 0, "Diffuse", ""},
372                 {SCE_PASS_SPEC, "SPECULAR", 0, "Specular", ""},
373                 {SCE_PASS_SHADOW, "SHADOW", 0, "Shadow", ""},
374                 {SCE_PASS_AO, "AO", 0, "AO", ""},
375                 {SCE_PASS_REFLECT, "REFLECTION", 0, "Reflection", ""},
376                 {SCE_PASS_NORMAL, "NORMAL", 0, "Normal", ""},
377                 {SCE_PASS_VECTOR, "VECTOR", 0, "Vector", ""},
378                 {SCE_PASS_REFRACT, "REFRACTION", 0, "Refraction", ""},
379                 {SCE_PASS_INDEXOB, "OBJECT_INDEX", 0, "Object Index", ""},
380                 {SCE_PASS_UV, "UV", 0, "UV", ""},
381                 {SCE_PASS_MIST, "MIST", 0, "Mist", ""},
382                 {SCE_PASS_EMIT, "EMIT", 0, "Emit", ""},
383                 {SCE_PASS_ENVIRONMENT, "ENVIRONMENT", 0, "Environment", ""},
384                 {0, NULL, 0, NULL, NULL}};
385         
386         srna= RNA_def_struct(brna, "RenderPass", NULL);
387         RNA_def_struct_ui_text(srna, "Render Pass", "");
388
389         RNA_define_verify_sdna(0);
390
391         prop= RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
392         RNA_def_property_string_sdna(prop, NULL, "name");
393         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
394         RNA_def_struct_name_property(srna, prop);
395
396         prop= RNA_def_property(srna, "channel_id", PROP_STRING, PROP_NONE);
397         RNA_def_property_string_sdna(prop, NULL, "chan_id");
398         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
399
400         prop= RNA_def_property(srna, "channels", PROP_INT, PROP_NONE);
401         RNA_def_property_int_sdna(prop, NULL, "channels");
402         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
403
404         prop= RNA_def_property(srna, "type", PROP_ENUM, PROP_NONE);
405         RNA_def_property_enum_sdna(prop, NULL, "passtype");
406         RNA_def_property_enum_items(prop, pass_type_items);
407         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
408
409         prop= RNA_def_property(srna, "rect", PROP_FLOAT, PROP_NONE);
410         RNA_def_property_flag(prop, PROP_DYNAMIC);
411         RNA_def_property_multi_array(prop, 2, NULL);
412         RNA_def_property_dynamic_array_funcs(prop, "rna_RenderPass_rect_get_length");
413         RNA_def_property_float_funcs(prop, "rna_RenderPass_rect_get", "rna_RenderPass_rect_set", NULL);
414
415         RNA_define_verify_sdna(1);
416 }
417
418 void RNA_def_render(BlenderRNA *brna)
419 {
420         rna_def_render_engine(brna);
421         rna_def_render_result(brna);
422         rna_def_render_layer(brna);
423         rna_def_render_pass(brna);
424 }
425
426 #endif // RNA_RUNTIME
427