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