2.5: Render
[blender-staging.git] / source / blender / makesrna / intern / rna_render.c
1 /**
2  * $Id: rna_render.c 21648 2009-07-17 02:31:28Z campbellbarton $
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include "RNA_types.h"
31
32 #include "rna_internal.h"
33
34 #include "WM_types.h"
35
36 #include "RE_pipeline.h"
37
38 #ifdef RNA_RUNTIME
39
40 #include "MEM_guardedalloc.h"
41
42 #include "RNA_access.h"
43
44 #include "BKE_context.h"
45 #include "BKE_report.h"
46
47 #include "WM_api.h"
48
49 /* RenderEngine */
50
51 static void engine_render(RenderEngine *engine, struct Scene *scene)
52 {
53         PointerRNA ptr;
54         ParameterList list;
55         FunctionRNA *func;
56
57         RNA_pointer_create(NULL, engine->type->ext.srna, engine, &ptr);
58         func= RNA_struct_find_function(&ptr, "render");
59
60         RNA_parameter_list_create(&list, &ptr, func);
61         RNA_parameter_set_lookup(&list, "scene", &scene);
62         engine->type->ext.call(&ptr, func, &list);
63
64         RNA_parameter_list_free(&list);
65 }
66
67 static void rna_RenderEngine_unregister(const bContext *C, StructRNA *type)
68 {
69         RenderEngineType *et= RNA_struct_blender_type_get(type);
70
71         if(!et)
72                 return;
73         
74         BLI_freelinkN(&R_engines, et);
75         RNA_struct_free(&BLENDER_RNA, type);
76 }
77
78 static StructRNA *rna_RenderEngine_register(const bContext *C, ReportList *reports, void *data, StructValidateFunc validate, StructCallbackFunc call, StructFreeFunc free)
79 {
80         RenderEngineType *et, dummyet = {0};
81         RenderEngine dummyengine= {0};
82         PointerRNA dummyptr;
83         int have_function[1];
84
85         /* setup dummy engine & engine type to store static properties in */
86         dummyengine.type= &dummyet;
87         RNA_pointer_create(NULL, &RNA_RenderEngine, &dummyengine, &dummyptr);
88
89         /* validate the python class */
90         if(validate(&dummyptr, data, have_function) != 0)
91                 return NULL;
92         
93         /* check if we have registered this engine type before, and remove it */
94         for(et=R_engines.first; et; et=et->next) {
95                 if(strcmp(et->idname, dummyet.idname) == 0) {
96                         if(et->ext.srna)
97                                 rna_RenderEngine_unregister(C, et->ext.srna);
98                         break;
99                 }
100         }
101         
102         /* create a new engine type */
103         et= MEM_callocN(sizeof(RenderEngineType), "python buttons engine");
104         memcpy(et, &dummyet, sizeof(dummyet));
105
106         et->ext.srna= RNA_def_struct(&BLENDER_RNA, et->idname, "RenderEngine"); 
107         et->ext.data= data;
108         et->ext.call= call;
109         et->ext.free= free;
110         RNA_struct_blender_type_set(et->ext.srna, et);
111
112         et->render= (have_function[0])? engine_render: NULL;
113
114         BLI_addtail(&R_engines, et);
115
116         return et->ext.srna;
117 }
118
119 static StructRNA* rna_RenderEngine_refine(PointerRNA *ptr)
120 {
121         RenderEngine *engine= (RenderEngine*)ptr->data;
122         return (engine->type && engine->type->ext.srna)? engine->type->ext.srna: &RNA_RenderEngine;
123 }
124
125 static void rna_RenderResult_layers_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
126 {
127         RenderResult *rr= (RenderResult*)ptr->data;
128         rna_iterator_listbase_begin(iter, &rr->layers, NULL);
129 }
130
131 static void rna_RenderLayer_passes_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
132 {
133         RenderLayer *rl= (RenderLayer*)ptr->data;
134         rna_iterator_listbase_begin(iter, &rl->passes, NULL);
135 }
136
137 static float rna_RenderValue_value_get(PointerRNA *ptr)
138 {
139         return *(float*)ptr->data;
140 }
141
142 static void rna_RenderValue_value_set(PointerRNA *ptr, float value)
143 {
144         *(float*)ptr->data= value;
145 }
146
147 static void rna_RenderLayer_rect_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
148 {
149         RenderLayer *rl= (RenderLayer*)ptr->data;
150         rna_iterator_array_begin(iter, (void*)rl->rectf, sizeof(float), rl->rectx*rl->recty*4, 0, NULL);
151 }
152
153 static int rna_RenderLayer_rect_length(PointerRNA *ptr)
154 {
155         RenderLayer *rl= (RenderLayer*)ptr->data;
156         return rl->rectx*rl->recty*4;
157 }
158
159 static void rna_RenderPass_rect_begin(CollectionPropertyIterator *iter, PointerRNA *ptr)
160 {
161         RenderPass *rpass= (RenderPass*)ptr->data;
162         rna_iterator_array_begin(iter, (void*)rpass->rect, sizeof(float), rpass->rectx*rpass->recty*rpass->channels, 0, NULL);
163 }
164
165 static int rna_RenderPass_rect_length(PointerRNA *ptr)
166 {
167         RenderPass *rpass= (RenderPass*)ptr->data;
168         return rpass->rectx*rpass->recty*rpass->channels;
169 }
170
171
172 #else // RNA_RUNTIME
173
174 static void rna_def_render_engine(BlenderRNA *brna)
175 {
176         StructRNA *srna;
177         PropertyRNA *prop;
178         FunctionRNA *func;
179         
180         srna= RNA_def_struct(brna, "RenderEngine", NULL);
181         RNA_def_struct_sdna(srna, "RenderEngine");
182         RNA_def_struct_ui_text(srna, "Render Engine", "Render engine.");
183         RNA_def_struct_refine_func(srna, "rna_RenderEngine_refine");
184         RNA_def_struct_register_funcs(srna, "rna_RenderEngine_register", "rna_RenderEngine_unregister");
185
186         /* render */
187         func= RNA_def_function(srna, "render", NULL);
188         RNA_def_function_ui_description(func, "Render scene into an image.");
189         RNA_def_function_flag(func, FUNC_REGISTER);
190         RNA_def_pointer(func, "scene", "Scene", "", "");
191
192         func= RNA_def_function(srna, "begin_result", "RE_engine_begin_result");
193         prop= RNA_def_int(func, "x", 0, 0, INT_MAX, "X", "", 0, INT_MAX);
194         RNA_def_property_flag(prop, PROP_REQUIRED);
195         prop= RNA_def_int(func, "y", 0, 0, INT_MAX, "Y", "", 0, INT_MAX);
196         RNA_def_property_flag(prop, PROP_REQUIRED);
197         prop= RNA_def_int(func, "w", 0, 0, INT_MAX, "Width", "", 0, INT_MAX);
198         RNA_def_property_flag(prop, PROP_REQUIRED);
199         prop= RNA_def_int(func, "h", 0, 0, INT_MAX, "Height", "", 0, INT_MAX);
200         RNA_def_property_flag(prop, PROP_REQUIRED);
201         prop= RNA_def_pointer(func, "result", "RenderResult", "Result", "");
202         RNA_def_function_return(func, prop);
203
204         func= RNA_def_function(srna, "update_result", "RE_engine_update_result");
205         prop= RNA_def_pointer(func, "result", "RenderResult", "Result", "");
206         RNA_def_property_flag(prop, PROP_REQUIRED);
207
208         func= RNA_def_function(srna, "end_result", "RE_engine_end_result");
209         prop= RNA_def_pointer(func, "result", "RenderResult", "Result", "");
210         RNA_def_property_flag(prop, PROP_REQUIRED);
211
212         func= RNA_def_function(srna, "test_break", "RE_engine_test_break");
213         prop= RNA_def_boolean(func, "do_break", 0, "Break", "");
214         RNA_def_function_return(func, prop);
215
216         func= RNA_def_function(srna, "update_stats", "RE_engine_update_stats");
217         prop= RNA_def_string(func, "stats", "", 0, "Stats", "");
218         RNA_def_property_flag(prop, PROP_REQUIRED);
219         prop= RNA_def_string(func, "info", "", 0, "Info", "");
220         RNA_def_property_flag(prop, PROP_REQUIRED);
221
222         /* registration */
223         RNA_define_verify_sdna(0);
224
225         prop= RNA_def_property(srna, "idname", PROP_STRING, PROP_NONE);
226         RNA_def_property_string_sdna(prop, NULL, "type->idname");
227         RNA_def_property_flag(prop, PROP_REGISTER);
228
229         prop= RNA_def_property(srna, "label", PROP_STRING, PROP_NONE);
230         RNA_def_property_string_sdna(prop, NULL, "type->name");
231         RNA_def_property_flag(prop, PROP_REGISTER);
232
233         RNA_define_verify_sdna(1);
234 }
235
236 static void rna_def_render_result(BlenderRNA *brna)
237 {
238         StructRNA *srna;
239         PropertyRNA *prop;
240         
241         srna= RNA_def_struct(brna, "RenderResult", NULL);
242         RNA_def_struct_ui_text(srna, "Render Result", "Result of rendering, including all layers and passes.");
243
244         RNA_define_verify_sdna(0);
245
246         prop= RNA_def_property(srna, "resolution_x", PROP_INT, PROP_NONE);
247         RNA_def_property_int_sdna(prop, NULL, "rectx");
248         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
249
250         prop= RNA_def_property(srna, "resolution_y", PROP_INT, PROP_NONE);
251         RNA_def_property_int_sdna(prop, NULL, "recty");
252         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
253
254         prop= RNA_def_property(srna, "layers", PROP_COLLECTION, PROP_NONE);
255         RNA_def_property_struct_type(prop, "RenderLayer");
256         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, 0, 0);
257
258         RNA_define_verify_sdna(1);
259 }
260
261 static void rna_def_render_layer(BlenderRNA *brna)
262 {
263         StructRNA *srna;
264         PropertyRNA *prop;
265         
266         srna= RNA_def_struct(brna, "RenderLayer", NULL);
267         RNA_def_struct_ui_text(srna, "Render Layer", "");
268
269         RNA_define_verify_sdna(0);
270
271         rna_def_render_layer_common(srna, 0);
272
273         prop= RNA_def_property(srna, "passes", PROP_COLLECTION, PROP_NONE);
274         RNA_def_property_struct_type(prop, "RenderPass");
275         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, 0, 0);
276
277         prop= RNA_def_property(srna, "rect", PROP_COLLECTION, PROP_NONE);
278         RNA_def_property_struct_type(prop, "RenderValue");
279         RNA_def_property_collection_funcs(prop, "rna_RenderLayer_rect_begin", "rna_iterator_array_next", "rna_iterator_array_end", "rna_iterator_array_get", "rna_RenderLayer_rect_length", 0, 0, 0, 0);
280
281         /* value */
282         srna= RNA_def_struct(brna, "RenderValue", NULL);
283         RNA_def_struct_ui_text(srna, "Render Value", "");
284
285         prop= RNA_def_property(srna, "value", PROP_FLOAT, PROP_NONE);
286         RNA_def_property_float_funcs(prop, "rna_RenderValue_value_get", "rna_RenderValue_value_set", NULL);
287
288         RNA_define_verify_sdna(1);
289 }
290
291 static void rna_def_render_pass(BlenderRNA *brna)
292 {
293         StructRNA *srna;
294         PropertyRNA *prop;
295
296         static EnumPropertyItem pass_type_items[]= {
297                 {SCE_PASS_COMBINED, "COMBINED", 0, "Combined", ""},
298                 {SCE_PASS_Z, "Z", 0, "Z", ""},
299                 {SCE_PASS_RGBA, "COLOR", 0, "Color", ""},
300                 {SCE_PASS_DIFFUSE, "DIFFUSE", 0, "Diffuse", ""},
301                 {SCE_PASS_SPEC, "SPECULAR", 0, "Specular", ""},
302                 {SCE_PASS_SHADOW, "SHADOW", 0, "Shadow", ""},
303                 {SCE_PASS_AO, "AO", 0, "AO", ""},
304                 {SCE_PASS_REFLECT, "REFLECTION", 0, "Reflection", ""},
305                 {SCE_PASS_NORMAL, "NORMAL", 0, "Normal", ""},
306                 {SCE_PASS_VECTOR, "VECTOR", 0, "Vecotr", ""},
307                 {SCE_PASS_REFRACT, "REFRACTION", 0, "Refraction", ""},
308                 {SCE_PASS_INDEXOB, "OBJECT_INDEX", 0, "Object Index", ""},
309                 {SCE_PASS_UV, "UV", 0, "UV", ""},
310                 {SCE_PASS_MIST, "MIST", 0, "Mist", ""},
311                 {0, NULL, 0, NULL, NULL}};
312         
313         srna= RNA_def_struct(brna, "RenderPass", NULL);
314         RNA_def_struct_ui_text(srna, "Render Pass", "");
315
316         RNA_define_verify_sdna(0);
317
318         prop= RNA_def_property(srna, "name", PROP_STRING, PROP_NONE);
319         RNA_def_property_string_sdna(prop, NULL, "name");
320         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
321         RNA_def_struct_name_property(srna, prop);
322
323         prop= RNA_def_property(srna, "channel_id", PROP_STRING, PROP_NONE);
324         RNA_def_property_string_sdna(prop, NULL, "chan_id");
325         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
326
327         prop= RNA_def_property(srna, "channels", PROP_INT, PROP_NONE);
328         RNA_def_property_int_sdna(prop, NULL, "channels");
329         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
330
331         prop= RNA_def_property(srna, "type", PROP_ENUM, PROP_NONE);
332         RNA_def_property_enum_sdna(prop, NULL, "passtype");
333         RNA_def_property_enum_items(prop, pass_type_items);
334         RNA_def_property_clear_flag(prop, PROP_EDITABLE);
335
336         prop= RNA_def_property(srna, "rect", PROP_COLLECTION, PROP_NONE);
337         RNA_def_property_struct_type(prop, "RenderValue");
338         RNA_def_property_collection_funcs(prop, "rna_RenderPass_rect_begin", "rna_iterator_array_next", "rna_iterator_array_end", "rna_iterator_array_get", "rna_RenderPass_rect_length", 0, 0, 0, 0);
339
340         RNA_define_verify_sdna(1);
341 }
342
343 void RNA_def_render(BlenderRNA *brna)
344 {
345         rna_def_render_engine(brna);
346         rna_def_render_result(brna);
347         rna_def_render_layer(brna);
348         rna_def_render_pass(brna);
349 }
350
351 #endif // RNA_RUNTIME
352