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