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