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