Final Fix T41222 Blender gives weird ouput when baking (4096*4096) resolution on GPU
[blender-staging.git] / source / blender / render / intern / source / external_engine.c
1 /*
2
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. 
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2006 Blender Foundation.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/render/intern/source/external_engine.c
30  *  \ingroup render
31  */
32
33 #include <stddef.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "BLF_translation.h"
40
41 #include "BLI_listbase.h"
42 #include "BLI_string.h"
43 #include "BLI_utildefines.h"
44
45 #include "BKE_global.h"
46 #include "BKE_report.h"
47 #include "BKE_scene.h"
48
49 #include "IMB_imbuf.h"
50 #include "IMB_imbuf_types.h"
51
52 #include "RNA_access.h"
53
54 #ifdef WITH_PYTHON
55 #include "BPY_extern.h"
56 #endif
57
58 #include "RE_engine.h"
59 #include "RE_pipeline.h"
60 #include "RE_bake.h"
61
62 #include "initrender.h"
63 #include "renderpipeline.h"
64 #include "render_types.h"
65 #include "render_result.h"
66
67 /* Render Engine Types */
68
69 static RenderEngineType internal_render_type = {
70         NULL, NULL,
71         "BLENDER_RENDER", N_("Blender Render"), RE_INTERNAL,
72         NULL, NULL, NULL, NULL, NULL, NULL,
73         {NULL, NULL, NULL}
74 };
75
76 #ifdef WITH_GAMEENGINE
77
78 static RenderEngineType internal_game_type = {
79         NULL, NULL,
80         "BLENDER_GAME", N_("Blender Game"), RE_INTERNAL | RE_GAME,
81         NULL, NULL, NULL, NULL, NULL, NULL,
82         {NULL, NULL, NULL}
83 };
84
85 #endif
86
87 ListBase R_engines = {NULL, NULL};
88
89 void RE_engines_init(void)
90 {
91         BLI_addtail(&R_engines, &internal_render_type);
92 #ifdef WITH_GAMEENGINE
93         BLI_addtail(&R_engines, &internal_game_type);
94 #endif
95 }
96
97 void RE_engines_exit(void)
98 {
99         RenderEngineType *type, *next;
100
101         for (type = R_engines.first; type; type = next) {
102                 next = type->next;
103
104                 BLI_remlink(&R_engines, type);
105
106                 if (!(type->flag & RE_INTERNAL)) {
107                         if (type->ext.free)
108                                 type->ext.free(type->ext.data);
109
110                         MEM_freeN(type);
111                 }
112         }
113 }
114
115 RenderEngineType *RE_engines_find(const char *idname)
116 {
117         RenderEngineType *type;
118         
119         type = BLI_findstring(&R_engines, idname, offsetof(RenderEngineType, idname));
120         if (!type)
121                 type = &internal_render_type;
122         
123         return type;
124 }
125
126 bool RE_engine_is_external(Render *re)
127 {
128         RenderEngineType *type = RE_engines_find(re->r.engine);
129         return (type && type->render);
130 }
131
132 /* Create, Free */
133
134 RenderEngine *RE_engine_create(RenderEngineType *type)
135 {
136         return RE_engine_create_ex(type, false);
137 }
138
139 RenderEngine *RE_engine_create_ex(RenderEngineType *type, bool use_for_viewport)
140 {
141         RenderEngine *engine = MEM_callocN(sizeof(RenderEngine), "RenderEngine");
142         engine->type = type;
143
144         if (use_for_viewport) {
145                 engine->flag |= RE_ENGINE_USED_FOR_VIEWPORT;
146
147                 BLI_begin_threaded_malloc();
148         }
149
150         return engine;
151 }
152
153 void RE_engine_free(RenderEngine *engine)
154 {
155 #ifdef WITH_PYTHON
156         if (engine->py_instance) {
157                 BPY_DECREF_RNA_INVALIDATE(engine->py_instance);
158         }
159 #endif
160
161         if (engine->flag & RE_ENGINE_USED_FOR_VIEWPORT) {
162                 BLI_end_threaded_malloc();
163         }
164
165         MEM_freeN(engine);
166 }
167
168 /* Render Results */
169
170 static RenderPart *get_part_from_result(Render *re, RenderResult *result)
171 {
172         RenderPart *pa;
173
174         for (pa = re->parts.first; pa; pa = pa->next) {
175                 if (result->tilerect.xmin == pa->disprect.xmin - re->disprect.xmin &&
176                     result->tilerect.ymin == pa->disprect.ymin - re->disprect.ymin &&
177                     result->tilerect.xmax == pa->disprect.xmax - re->disprect.xmin &&
178                     result->tilerect.ymax == pa->disprect.ymax - re->disprect.ymin)
179                 {
180                         return pa;
181                 }
182         }
183
184         return NULL;
185 }
186
187 RenderResult *RE_engine_begin_result(RenderEngine *engine, int x, int y, int w, int h, const char *layername)
188 {
189         Render *re = engine->re;
190         RenderResult *result;
191         rcti disprect;
192
193         /* ensure the coordinates are within the right limits */
194         CLAMP(x, 0, re->result->rectx);
195         CLAMP(y, 0, re->result->recty);
196         CLAMP(w, 0, re->result->rectx);
197         CLAMP(h, 0, re->result->recty);
198
199         if (x + w > re->result->rectx)
200                 w = re->result->rectx - x;
201         if (y + h > re->result->recty)
202                 h = re->result->recty - y;
203
204         /* allocate a render result */
205         disprect.xmin = x;
206         disprect.xmax = x + w;
207         disprect.ymin = y;
208         disprect.ymax = y + h;
209
210         result = render_result_new(re, &disprect, 0, RR_USE_MEM, layername);
211
212         /* todo: make this thread safe */
213
214         /* can be NULL if we CLAMP the width or height to 0 */
215         if (result) {
216                 RenderPart *pa;
217
218                 /* Copy EXR tile settings, so pipeline knows whether this is a result
219                  * for Save Buffers enabled rendering.
220                  */
221                 result->do_exr_tile = re->result->do_exr_tile;
222
223                 BLI_addtail(&engine->fullresult, result);
224
225                 result->tilerect.xmin += re->disprect.xmin;
226                 result->tilerect.xmax += re->disprect.xmin;
227                 result->tilerect.ymin += re->disprect.ymin;
228                 result->tilerect.ymax += re->disprect.ymin;
229
230                 pa = get_part_from_result(re, result);
231
232                 if (pa)
233                         pa->status = PART_STATUS_IN_PROGRESS;
234         }
235
236         return result;
237 }
238
239 void RE_engine_update_result(RenderEngine *engine, RenderResult *result)
240 {
241         Render *re = engine->re;
242
243         if (result) {
244                 result->renlay = result->layers.first; /* weak, draws first layer always */
245                 re->display_update(re->duh, result, NULL);
246         }
247 }
248
249 void RE_engine_end_result(RenderEngine *engine, RenderResult *result, int cancel, int merge_results)
250 {
251         Render *re = engine->re;
252
253         if (!result) {
254                 return;
255         }
256
257         /* merge. on break, don't merge in result for preview renders, looks nicer */
258         if (!cancel) {
259                 /* for exr tile render, detect tiles that are done */
260                 RenderPart *pa = get_part_from_result(re, result);
261
262                 if (pa) {
263                         pa->status = PART_STATUS_READY;
264                 }
265                 else if (re->result->do_exr_tile) {
266                         /* if written result does not match any tile and we are using save
267                          * buffers, we are going to get openexr save errors */
268                         fprintf(stderr, "RenderEngine.end_result: dimensions do not match any OpenEXR tile.\n");
269                 }
270         }
271
272         if (!cancel || merge_results) {
273                 if (re->result->do_exr_tile) {
274                         if (!cancel) {
275                                 render_result_exr_file_merge(re->result, result);
276                         }
277                 }
278                 else if (!(re->test_break(re->tbh) && (re->r.scemode & R_BUTS_PREVIEW)))
279                         render_result_merge(re->result, result);
280
281                 /* draw */
282                 if (!re->test_break(re->tbh)) {
283                         result->renlay = result->layers.first; /* weak, draws first layer always */
284                         re->display_update(re->duh, result, NULL);
285                 }
286         }
287
288         /* free */
289         BLI_remlink(&engine->fullresult, result);
290         render_result_free(result);
291 }
292
293 /* Cancel */
294
295 int RE_engine_test_break(RenderEngine *engine)
296 {
297         Render *re = engine->re;
298
299         if (re)
300                 return re->test_break(re->tbh);
301         
302         return 0;
303 }
304
305 /* Statistics */
306
307 void RE_engine_update_stats(RenderEngine *engine, const char *stats, const char *info)
308 {
309         Render *re = engine->re;
310
311         /* stats draw callback */
312         if (re) {
313                 re->i.statstr = stats;
314                 re->i.infostr = info;
315                 re->stats_draw(re->sdh, &re->i);
316                 re->i.infostr = NULL;
317                 re->i.statstr = NULL;
318         }
319
320         /* set engine text */
321         engine->text[0] = '\0';
322
323         if (stats && stats[0] && info && info[0])
324                 BLI_snprintf(engine->text, sizeof(engine->text), "%s | %s", stats, info);
325         else if (info && info[0])
326                 BLI_strncpy(engine->text, info, sizeof(engine->text));
327         else if (stats && stats[0])
328                 BLI_strncpy(engine->text, stats, sizeof(engine->text));
329 }
330
331 void RE_engine_update_progress(RenderEngine *engine, float progress)
332 {
333         Render *re = engine->re;
334
335         if (re) {
336                 CLAMP(progress, 0.0f, 1.0f);
337                 re->progress(re->prh, progress);
338         }
339 }
340
341 void RE_engine_update_memory_stats(RenderEngine *engine, float mem_used, float mem_peak)
342 {
343         Render *re = engine->re;
344
345         if (re) {
346                 re->i.mem_used = mem_used;
347                 re->i.mem_peak = mem_peak;
348         }
349 }
350
351 void RE_engine_report(RenderEngine *engine, int type, const char *msg)
352 {
353         Render *re = engine->re;
354
355         if (re)
356                 BKE_report(engine->re->reports, type, msg);
357         else if (engine->reports)
358                 BKE_report(engine->reports, type, msg);
359 }
360
361 void RE_engine_get_current_tiles(Render *re, int *total_tiles_r, rcti **tiles_r)
362 {
363         RenderPart *pa;
364         int total_tiles = 0;
365         rcti *tiles = NULL;
366         int allocation_size = 0, allocation_step = BLENDER_MAX_THREADS;
367
368         if (re->engine && (re->engine->flag & RE_ENGINE_HIGHLIGHT_TILES) == 0) {
369                 *total_tiles_r = 0;
370                 *tiles_r = NULL;
371                 return;
372         }
373
374         for (pa = re->parts.first; pa; pa = pa->next) {
375                 if (pa->status == PART_STATUS_IN_PROGRESS) {
376                         if (total_tiles >= allocation_size) {
377                                 if (tiles == NULL)
378                                         tiles = MEM_mallocN(allocation_step * sizeof(rcti), "current engine tiles");
379                                 else
380                                         tiles = MEM_reallocN(tiles, (total_tiles + allocation_step) * sizeof(rcti));
381
382                                 allocation_size += allocation_step;
383                         }
384
385                         tiles[total_tiles] = pa->disprect;
386
387                         if (pa->crop) {
388                                 tiles[total_tiles].xmin += pa->crop;
389                                 tiles[total_tiles].ymin += pa->crop;
390                                 tiles[total_tiles].xmax -= pa->crop;
391                                 tiles[total_tiles].ymax -= pa->crop;
392                         }
393
394                         total_tiles++;
395                 }
396         }
397
398         *total_tiles_r = total_tiles;
399         *tiles_r = tiles;
400 }
401
402 RenderData *RE_engine_get_render_data(Render *re)
403 {
404         return &re->r;
405 }
406
407 /* Bake */
408 void RE_bake_engine_set_engine_parameters(Render *re, Main *bmain, Scene *scene)
409 {
410         re->scene = scene;
411         re->main = bmain;
412         re->r = scene->r;
413
414         /* prevent crash when freeing the scene
415          * but it potentially leaves unfreed memory blocks
416          * not sure how to fix this yet -- dfelinto */
417         BLI_listbase_clear(&re->r.layers);
418 }
419
420 bool RE_bake_has_engine(Render *re)
421 {
422         RenderEngineType *type = RE_engines_find(re->r.engine);
423         return (type->bake != NULL);
424 }
425
426 bool RE_bake_engine(
427         Render *re, Object *object, const BakePixel pixel_array[],
428         const size_t num_pixels, const int depth,
429         const ScenePassType pass_type, float result[])
430 {
431         RenderEngineType *type = RE_engines_find(re->r.engine);
432         RenderEngine *engine;
433         int persistent_data = re->r.mode & R_PERSISTENT_DATA;
434
435         /* set render info */
436         re->i.cfra = re->scene->r.cfra;
437         BLI_strncpy(re->i.scene_name, re->scene->id.name + 2, sizeof(re->i.scene_name) - 2);
438         re->i.totface = re->i.totvert = re->i.totstrand = re->i.totlamp = re->i.tothalo = 0;
439
440         /* render */
441         engine = re->engine;
442
443         if (!engine) {
444                 engine = RE_engine_create(type);
445                 re->engine = engine;
446         }
447
448         engine->flag |= RE_ENGINE_RENDERING;
449
450         /* TODO: actually link to a parent which shouldn't happen */
451         engine->re = re;
452
453         engine->resolution_x = re->winx;
454         engine->resolution_y = re->winy;
455
456         RE_parts_init(re, false);
457         engine->tile_x = re->r.tilex;
458         engine->tile_y = re->r.tiley;
459
460         /* update is only called so we create the engine.session */
461         if (type->update)
462                 type->update(engine, re->main, re->scene);
463
464         if (type->bake)
465                 type->bake(engine, re->scene, object, pass_type, pixel_array, num_pixels, depth, result);
466
467         engine->tile_x = 0;
468         engine->tile_y = 0;
469         engine->flag &= ~RE_ENGINE_RENDERING;
470
471         /* re->engine becomes zero if user changed active render engine during render */
472         if (!persistent_data || !re->engine) {
473                 RE_engine_free(engine);
474                 re->engine = NULL;
475         }
476
477         RE_parts_free(re);
478
479         if (BKE_reports_contain(re->reports, RPT_ERROR))
480                 G.is_break = true;
481
482         return true;
483 }
484
485 void RE_engine_frame_set(RenderEngine *engine, int frame, float subframe)
486 {
487         Render *re = engine->re;
488         Scene *scene = re->scene;
489         double cfra = (double)frame + (double)subframe;
490
491         CLAMP(cfra, MINAFRAME, MAXFRAME);
492         BKE_scene_frame_set(scene, cfra);
493
494 #ifdef WITH_PYTHON
495         BPy_BEGIN_ALLOW_THREADS;
496 #endif
497
498         /* It's possible that here we're including layers which were never visible before. */
499         BKE_scene_update_for_newframe_ex(re->eval_ctx, re->main, scene, (1 << 20) - 1, true);
500
501 #ifdef WITH_PYTHON
502         BPy_END_ALLOW_THREADS;
503 #endif
504
505         BKE_scene_camera_switch_update(scene);
506 }
507
508 /* Render */
509
510 static bool render_layer_exclude_animated(Scene *scene, SceneRenderLayer *srl)
511 {
512         PointerRNA ptr;
513         PropertyRNA *prop;
514
515         RNA_pointer_create(&scene->id, &RNA_SceneRenderLayer, srl, &ptr);
516         prop = RNA_struct_find_property(&ptr, "layers_exclude");
517
518         return RNA_property_animated(&ptr, prop);
519 }
520
521 int RE_engine_render(Render *re, int do_all)
522 {
523         RenderEngineType *type = RE_engines_find(re->r.engine);
524         RenderEngine *engine;
525         int persistent_data = re->r.mode & R_PERSISTENT_DATA;
526
527         /* verify if we can render */
528         if (!type->render)
529                 return 0;
530         if ((re->r.scemode & R_BUTS_PREVIEW) && !(type->flag & RE_USE_PREVIEW))
531                 return 0;
532         if (do_all && !(type->flag & RE_USE_POSTPROCESS))
533                 return 0;
534         if (!do_all && (type->flag & RE_USE_POSTPROCESS))
535                 return 0;
536
537         /* Lock drawing in UI during data phase. */
538         if (re->draw_lock) {
539                 re->draw_lock(re->dlh, 1);
540         }
541
542         /* update animation here so any render layer animation is applied before
543          * creating the render result */
544         if ((re->r.scemode & (R_NO_FRAME_UPDATE | R_BUTS_PREVIEW)) == 0) {
545                 unsigned int lay = re->lay;
546
547                 /* don't update layers excluded on all render layers */
548                 if (type->flag & RE_USE_EXCLUDE_LAYERS) {
549                         SceneRenderLayer *srl;
550                         unsigned int non_excluded_lay = 0;
551
552                         if (re->r.scemode & R_SINGLE_LAYER) {
553                                 srl = BLI_findlink(&re->r.layers, re->r.actlay);
554                                 if (srl) {
555                                         non_excluded_lay |= ~srl->lay_exclude;
556
557                                         /* in this case we must update all because animation for
558                                          * the scene has not been updated yet, and so may not be
559                                          * up to date until after BKE_scene_update_for_newframe */
560                                         if (render_layer_exclude_animated(re->scene, srl))
561                                                 non_excluded_lay |= ~0;
562                                 }
563                         }
564                         else {
565                                 for (srl = re->r.layers.first; srl; srl = srl->next) {
566                                         if (!(srl->layflag & SCE_LAY_DISABLE)) {
567                                                 non_excluded_lay |= ~srl->lay_exclude;
568
569                                                 if (render_layer_exclude_animated(re->scene, srl))
570                                                         non_excluded_lay |= ~0;
571                                         }
572                                 }
573                         }
574
575                         lay &= non_excluded_lay;
576                 }
577
578                 BKE_scene_update_for_newframe(re->eval_ctx, re->main, re->scene, lay);
579                 render_update_anim_renderdata(re, &re->scene->r);
580         }
581
582         /* create render result */
583         BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
584         if (re->result == NULL || !(re->r.scemode & R_BUTS_PREVIEW)) {
585                 int savebuffers = RR_USE_MEM;
586
587                 if (re->result)
588                         render_result_free(re->result);
589
590                 if ((type->flag & RE_USE_SAVE_BUFFERS) && (re->r.scemode & R_EXR_TILE_FILE))
591                         savebuffers = RR_USE_EXR;
592                 re->result = render_result_new(re, &re->disprect, 0, savebuffers, RR_ALL_LAYERS);
593         }
594         BLI_rw_mutex_unlock(&re->resultmutex);
595
596         if (re->result == NULL) {
597                 /* Clear UI drawing locks. */
598                 if (re->draw_lock) {
599                         re->draw_lock(re->dlh, 0);
600                 }
601                 return 1;
602         }
603
604         /* set render info */
605         re->i.cfra = re->scene->r.cfra;
606         BLI_strncpy(re->i.scene_name, re->scene->id.name + 2, sizeof(re->i.scene_name));
607         re->i.totface = re->i.totvert = re->i.totstrand = re->i.totlamp = re->i.tothalo = 0;
608
609         /* render */
610         engine = re->engine;
611
612         if (!engine) {
613                 engine = RE_engine_create(type);
614                 re->engine = engine;
615         }
616
617         engine->flag |= RE_ENGINE_RENDERING;
618
619         /* TODO: actually link to a parent which shouldn't happen */
620         engine->re = re;
621
622         if (re->flag & R_ANIMATION)
623                 engine->flag |= RE_ENGINE_ANIMATION;
624         if (re->r.scemode & R_BUTS_PREVIEW)
625                 engine->flag |= RE_ENGINE_PREVIEW;
626         engine->camera_override = re->camera_override;
627         engine->layer_override = re->layer_override;
628
629         engine->resolution_x = re->winx;
630         engine->resolution_y = re->winy;
631
632         RE_parts_init(re, false);
633         engine->tile_x = re->partx;
634         engine->tile_y = re->party;
635
636         if (re->result->do_exr_tile)
637                 render_result_exr_file_begin(re);
638
639         if (type->update)
640                 type->update(engine, re->main, re->scene);
641
642         /* Clear UI drawing locks. */
643         if (re->draw_lock) {
644                 re->draw_lock(re->dlh, 0);
645         }
646
647         if (type->render)
648                 type->render(engine, re->scene);
649
650         engine->tile_x = 0;
651         engine->tile_y = 0;
652         engine->flag &= ~RE_ENGINE_RENDERING;
653
654         render_result_free_list(&engine->fullresult, engine->fullresult.first);
655
656         /* re->engine becomes zero if user changed active render engine during render */
657         if (!persistent_data || !re->engine) {
658                 RE_engine_free(engine);
659                 re->engine = NULL;
660         }
661
662         if (re->result->do_exr_tile) {
663                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
664                 render_result_exr_file_end(re);
665                 BLI_rw_mutex_unlock(&re->resultmutex);
666         }
667
668         if (re->r.scemode & R_EXR_CACHE_FILE) {
669                 BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
670                 render_result_exr_file_cache_write(re);
671                 BLI_rw_mutex_unlock(&re->resultmutex);
672         }
673
674         RE_parts_free(re);
675
676         if (BKE_reports_contain(re->reports, RPT_ERROR))
677                 G.is_break = true;
678         
679 #ifdef WITH_FREESTYLE
680         if (re->r.mode & R_EDGE_FRS)
681                 RE_RenderFreestyleExternal(re);
682 #endif
683
684         return 1;
685 }
686