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