This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/render/intern/source/external_engine.c

673 lines
16 KiB
C
Raw Normal View History

2012-04-30 14:24:11 +00:00
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2006 Blender Foundation.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/render/intern/source/external_engine.c
* \ingroup render
*/
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "MEM_guardedalloc.h"
#include "BLF_translation.h"
#include "BLI_listbase.h"
#include "BLI_string.h"
#include "BLI_utildefines.h"
#include "BKE_global.h"
#include "BKE_report.h"
#include "BKE_scene.h"
#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"
#include "RNA_access.h"
#ifdef WITH_PYTHON
#include "BPY_extern.h"
#endif
#include "RE_engine.h"
#include "RE_pipeline.h"
Bake API - bpy.ops.object.bake() New operator that can calls a bake function to the current render engine when available. This commit provides no feature for the users, but allows external engines to be accessed by the operator and be integrated with the baking api. The API itself is simple. Blender sends a populated array of BakePixels to the renderer, and gets back an array of floats with the result. The Blender Internal (and multires) system is still running independent, but we eventually will pipe it through the API as well. Cycles baking will come next as a separated commit Python Operator: ---------------- The operator can be called with some arguments, or a user interface can be created for it. In that case the arguments can be ommited and the interface can expose the settings from bpy.context.scene.render.bake bpy.ops.object.bake(type='COMBINED', filepath="", width=512, height=512, margin=16, use_selected_to_active=False, cage_extrusion=0, cage="", normal_space='TANGENT', normal_r='POS_X', normal_g='POS_Y', normal_b='POS_Z', save_mode='INTERNAL', use_clear=False, use_split_materials=False, use_automatic_name=False) Note: external save mode is currently disabled. Supported Features: ------------------ * Margin - Baked result is extended this many pixels beyond the border of each UV "island," to soften seams in the texture. * Selected to Active - bake shading on the surface of selected object to the active object. The rays are cast from the lowpoly object inwards towards the highpoly object. If the highpoly object is not entirely involved by the lowpoly object, you can tweak the rays start point with Cage Extrusion. For even more control of the cage you can use a Cage object. * Cage Extrusion - distance to use for the inward ray cast when using selected to active * Custom Cage - object to use as cage (instead of the lowpoly object). * Normal swizzle - change the axis that gets mapped to RGB * Normal space - save as tangent or object normal spaces Supported Passes: ----------------- Any pass that is supported by Blender renderlayer system. Though it's up to the external engine to provide a valid enum with its supported passes. Normal passes get a special treatment since we post-process them to converted and "swizzled" Development Notes for External Engines: --------------------------------------- (read them in bake_api.c) * For a complete implementation example look at the Cycles Bake commit (next). Review: D421 Reviewed by: Campbell Barton, Brecht van Lommel, Sergey Sharybin, Thomas Dinge Normal map pipeline "consulting" by Andy Davies (metalliandy) Original design by Brecht van Lommel. The entire commit history can be found on the branch: bake-cycles
2014-01-02 19:05:07 -02:00
#include "RE_bake.h"
#include "initrender.h"
#include "render_types.h"
#include "render_result.h"
/* Render Engine Types */
static RenderEngineType internal_render_type = {
NULL, NULL,
"BLENDER_RENDER", N_("Blender Render"), RE_INTERNAL,
Bake API - bpy.ops.object.bake() New operator that can calls a bake function to the current render engine when available. This commit provides no feature for the users, but allows external engines to be accessed by the operator and be integrated with the baking api. The API itself is simple. Blender sends a populated array of BakePixels to the renderer, and gets back an array of floats with the result. The Blender Internal (and multires) system is still running independent, but we eventually will pipe it through the API as well. Cycles baking will come next as a separated commit Python Operator: ---------------- The operator can be called with some arguments, or a user interface can be created for it. In that case the arguments can be ommited and the interface can expose the settings from bpy.context.scene.render.bake bpy.ops.object.bake(type='COMBINED', filepath="", width=512, height=512, margin=16, use_selected_to_active=False, cage_extrusion=0, cage="", normal_space='TANGENT', normal_r='POS_X', normal_g='POS_Y', normal_b='POS_Z', save_mode='INTERNAL', use_clear=False, use_split_materials=False, use_automatic_name=False) Note: external save mode is currently disabled. Supported Features: ------------------ * Margin - Baked result is extended this many pixels beyond the border of each UV "island," to soften seams in the texture. * Selected to Active - bake shading on the surface of selected object to the active object. The rays are cast from the lowpoly object inwards towards the highpoly object. If the highpoly object is not entirely involved by the lowpoly object, you can tweak the rays start point with Cage Extrusion. For even more control of the cage you can use a Cage object. * Cage Extrusion - distance to use for the inward ray cast when using selected to active * Custom Cage - object to use as cage (instead of the lowpoly object). * Normal swizzle - change the axis that gets mapped to RGB * Normal space - save as tangent or object normal spaces Supported Passes: ----------------- Any pass that is supported by Blender renderlayer system. Though it's up to the external engine to provide a valid enum with its supported passes. Normal passes get a special treatment since we post-process them to converted and "swizzled" Development Notes for External Engines: --------------------------------------- (read them in bake_api.c) * For a complete implementation example look at the Cycles Bake commit (next). Review: D421 Reviewed by: Campbell Barton, Brecht van Lommel, Sergey Sharybin, Thomas Dinge Normal map pipeline "consulting" by Andy Davies (metalliandy) Original design by Brecht van Lommel. The entire commit history can be found on the branch: bake-cycles
2014-01-02 19:05:07 -02:00
NULL, NULL, NULL, NULL, NULL, NULL,
2012-06-16 16:57:16 +00:00
{NULL, NULL, NULL}
};
#ifdef WITH_GAMEENGINE
static RenderEngineType internal_game_type = {
NULL, NULL,
2012-06-16 16:57:16 +00:00
"BLENDER_GAME", N_("Blender Game"), RE_INTERNAL | RE_GAME,
Bake API - bpy.ops.object.bake() New operator that can calls a bake function to the current render engine when available. This commit provides no feature for the users, but allows external engines to be accessed by the operator and be integrated with the baking api. The API itself is simple. Blender sends a populated array of BakePixels to the renderer, and gets back an array of floats with the result. The Blender Internal (and multires) system is still running independent, but we eventually will pipe it through the API as well. Cycles baking will come next as a separated commit Python Operator: ---------------- The operator can be called with some arguments, or a user interface can be created for it. In that case the arguments can be ommited and the interface can expose the settings from bpy.context.scene.render.bake bpy.ops.object.bake(type='COMBINED', filepath="", width=512, height=512, margin=16, use_selected_to_active=False, cage_extrusion=0, cage="", normal_space='TANGENT', normal_r='POS_X', normal_g='POS_Y', normal_b='POS_Z', save_mode='INTERNAL', use_clear=False, use_split_materials=False, use_automatic_name=False) Note: external save mode is currently disabled. Supported Features: ------------------ * Margin - Baked result is extended this many pixels beyond the border of each UV "island," to soften seams in the texture. * Selected to Active - bake shading on the surface of selected object to the active object. The rays are cast from the lowpoly object inwards towards the highpoly object. If the highpoly object is not entirely involved by the lowpoly object, you can tweak the rays start point with Cage Extrusion. For even more control of the cage you can use a Cage object. * Cage Extrusion - distance to use for the inward ray cast when using selected to active * Custom Cage - object to use as cage (instead of the lowpoly object). * Normal swizzle - change the axis that gets mapped to RGB * Normal space - save as tangent or object normal spaces Supported Passes: ----------------- Any pass that is supported by Blender renderlayer system. Though it's up to the external engine to provide a valid enum with its supported passes. Normal passes get a special treatment since we post-process them to converted and "swizzled" Development Notes for External Engines: --------------------------------------- (read them in bake_api.c) * For a complete implementation example look at the Cycles Bake commit (next). Review: D421 Reviewed by: Campbell Barton, Brecht van Lommel, Sergey Sharybin, Thomas Dinge Normal map pipeline "consulting" by Andy Davies (metalliandy) Original design by Brecht van Lommel. The entire commit history can be found on the branch: bake-cycles
2014-01-02 19:05:07 -02:00
NULL, NULL, NULL, NULL, NULL, NULL,
2012-06-16 16:57:16 +00:00
{NULL, NULL, NULL}
};
#endif
ListBase R_engines = {NULL, NULL};
void RE_engines_init(void)
{
BLI_addtail(&R_engines, &internal_render_type);
#ifdef WITH_GAMEENGINE
BLI_addtail(&R_engines, &internal_game_type);
#endif
}
void RE_engines_exit(void)
{
RenderEngineType *type, *next;
2012-06-16 16:57:16 +00:00
for (type = R_engines.first; type; type = next) {
next = type->next;
BLI_remlink(&R_engines, type);
if (!(type->flag & RE_INTERNAL)) {
if (type->ext.free)
type->ext.free(type->ext.data);
MEM_freeN(type);
}
}
}
RenderEngineType *RE_engines_find(const char *idname)
{
RenderEngineType *type;
2012-06-16 16:57:16 +00:00
type = BLI_findstring(&R_engines, idname, offsetof(RenderEngineType, idname));
if (!type)
2012-06-16 16:57:16 +00:00
type = &internal_render_type;
return type;
}
2014-02-03 18:55:59 +11:00
bool RE_engine_is_external(Render *re)
{
2012-06-16 16:57:16 +00:00
RenderEngineType *type = RE_engines_find(re->r.engine);
return (type && type->render);
}
/* Create, Free */
RenderEngine *RE_engine_create(RenderEngineType *type)
{
return RE_engine_create_ex(type, false);
}
RenderEngine *RE_engine_create_ex(RenderEngineType *type, bool use_for_viewport)
{
RenderEngine *engine = MEM_callocN(sizeof(RenderEngine), "RenderEngine");
2012-06-16 16:57:16 +00:00
engine->type = type;
if (use_for_viewport) {
engine->flag |= RE_ENGINE_USED_FOR_VIEWPORT;
BLI_begin_threaded_malloc();
}
return engine;
}
void RE_engine_free(RenderEngine *engine)
{
#ifdef WITH_PYTHON
if (engine->py_instance) {
BPY_DECREF_RNA_INVALIDATE(engine->py_instance);
}
#endif
if (engine->flag & RE_ENGINE_USED_FOR_VIEWPORT) {
BLI_end_threaded_malloc();
}
MEM_freeN(engine);
}
/* Render Results */
static RenderPart *get_part_from_result(Render *re, RenderResult *result)
{
RenderPart *pa;
for (pa = re->parts.first; pa; pa = pa->next) {
if (result->tilerect.xmin == pa->disprect.xmin - re->disprect.xmin &&
result->tilerect.ymin == pa->disprect.ymin - re->disprect.ymin &&
result->tilerect.xmax == pa->disprect.xmax - re->disprect.xmin &&
result->tilerect.ymax == pa->disprect.ymax - re->disprect.ymin)
{
return pa;
}
}
return NULL;
}
RenderResult *RE_engine_begin_result(RenderEngine *engine, int x, int y, int w, int h, const char *layername)
{
2012-06-16 16:57:16 +00:00
Render *re = engine->re;
RenderResult *result;
rcti disprect;
/* ensure the coordinates are within the right limits */
CLAMP(x, 0, re->result->rectx);
CLAMP(y, 0, re->result->recty);
CLAMP(w, 0, re->result->rectx);
CLAMP(h, 0, re->result->recty);
if (x + w > re->result->rectx)
2012-06-16 16:57:16 +00:00
w = re->result->rectx - x;
if (y + h > re->result->recty)
2012-06-16 16:57:16 +00:00
h = re->result->recty - y;
/* allocate a render result */
disprect.xmin = x;
2012-06-16 16:57:16 +00:00
disprect.xmax = x + w;
disprect.ymin = y;
2012-06-16 16:57:16 +00:00
disprect.ymax = y + h;
result = render_result_new(re, &disprect, 0, RR_USE_MEM, layername);
/* todo: make this thread safe */
/* can be NULL if we CLAMP the width or height to 0 */
if (result) {
RenderPart *pa;
/* Copy EXR tile settings, so pipeline knows whether this is a result
* for Save Buffers enabled rendering.
*/
result->do_exr_tile = re->result->do_exr_tile;
BLI_addtail(&engine->fullresult, result);
result->tilerect.xmin += re->disprect.xmin;
result->tilerect.xmax += re->disprect.xmin;
result->tilerect.ymin += re->disprect.ymin;
result->tilerect.ymax += re->disprect.ymin;
pa = get_part_from_result(re, result);
if (pa)
pa->status = PART_STATUS_IN_PROGRESS;
}
return result;
}
void RE_engine_update_result(RenderEngine *engine, RenderResult *result)
{
2012-06-16 16:57:16 +00:00
Render *re = engine->re;
if (result) {
2012-07-07 22:51:57 +00:00
result->renlay = result->layers.first; /* weak, draws first layer always */
Changes to partial update during rendering Summary: Mainly addressed to solve old TODO with color managed fallback to CPU mode when displaying render result during rendering. That fallback was caused by the fact that partial image update was always acquiring image buffer for composite output and was only modifying display buffer directly. This was a big issue for Cycles rendering which renders layers one by one and wanted to display progress of each individual layer. This lead to situations when display buffer was based on what Cycles passes via RenderResult and didn't take layer/pass from image editor header into account. Now made it so image buffer which partial update is operating with always corresponds to what is set in image editor header. To make Cycles displaying progress of all the layers one by one made it so image_rect_update switches image editor user to newly rendering render layer. It happens only once when render engine starts rendering next render layer, so should not be annoying for navigation during rendering. Additional change to render engines was done to make it so they're able to merge composite output to final result without marking tile as done. This is done via do_merge_result argument to end_result() callback. This argument is optional so should not break script compatibility. Additional changes: - Partial display update for Blender Internal now happens from the same thread as tile rendering. This makes it so display conversion (which could be pretty heavy actually) is done in separate threads. Also gives better UI feedback when rendering easy scene with small tiles. - Avoid freeing/allocating byte buffer for render result if it's owned by the image buffer. Only mark it as invalid for color management. Saves loads of buffer re-allocations in cases when having several image editors opened with render result. This change in conjunction with the rest of the patch gave around 50%-100% speedup of render time when displaying non-combined pass during rendering on my laptop. - Partial display buffer update was wrong for buffers with number of channels different from 4. - Remove unused window from RenderJob. - Made image_buffer_rect_update static since it's only used in single file. Reviewers: brecht Reviewed By: brecht CC: dingto Differential Revision: http://developer.blender.org/D98
2013-12-17 23:42:38 +06:00
re->display_update(re->duh, result, NULL);
}
}
Changes to partial update during rendering Summary: Mainly addressed to solve old TODO with color managed fallback to CPU mode when displaying render result during rendering. That fallback was caused by the fact that partial image update was always acquiring image buffer for composite output and was only modifying display buffer directly. This was a big issue for Cycles rendering which renders layers one by one and wanted to display progress of each individual layer. This lead to situations when display buffer was based on what Cycles passes via RenderResult and didn't take layer/pass from image editor header into account. Now made it so image buffer which partial update is operating with always corresponds to what is set in image editor header. To make Cycles displaying progress of all the layers one by one made it so image_rect_update switches image editor user to newly rendering render layer. It happens only once when render engine starts rendering next render layer, so should not be annoying for navigation during rendering. Additional change to render engines was done to make it so they're able to merge composite output to final result without marking tile as done. This is done via do_merge_result argument to end_result() callback. This argument is optional so should not break script compatibility. Additional changes: - Partial display update for Blender Internal now happens from the same thread as tile rendering. This makes it so display conversion (which could be pretty heavy actually) is done in separate threads. Also gives better UI feedback when rendering easy scene with small tiles. - Avoid freeing/allocating byte buffer for render result if it's owned by the image buffer. Only mark it as invalid for color management. Saves loads of buffer re-allocations in cases when having several image editors opened with render result. This change in conjunction with the rest of the patch gave around 50%-100% speedup of render time when displaying non-combined pass during rendering on my laptop. - Partial display buffer update was wrong for buffers with number of channels different from 4. - Remove unused window from RenderJob. - Made image_buffer_rect_update static since it's only used in single file. Reviewers: brecht Reviewed By: brecht CC: dingto Differential Revision: http://developer.blender.org/D98
2013-12-17 23:42:38 +06:00
void RE_engine_end_result(RenderEngine *engine, RenderResult *result, int cancel, int merge_results)
{
2012-06-16 16:57:16 +00:00
Render *re = engine->re;
if (!result) {
return;
}
/* merge. on break, don't merge in result for preview renders, looks nicer */
if (!cancel) {
/* for exr tile render, detect tiles that are done */
RenderPart *pa = get_part_from_result(re, result);
if (pa) {
pa->status = PART_STATUS_READY;
}
else if (re->result->do_exr_tile) {
/* if written result does not match any tile and we are using save
* buffers, we are going to get openexr save errors */
fprintf(stderr, "RenderEngine.end_result: dimensions do not match any OpenEXR tile.\n");
}
Changes to partial update during rendering Summary: Mainly addressed to solve old TODO with color managed fallback to CPU mode when displaying render result during rendering. That fallback was caused by the fact that partial image update was always acquiring image buffer for composite output and was only modifying display buffer directly. This was a big issue for Cycles rendering which renders layers one by one and wanted to display progress of each individual layer. This lead to situations when display buffer was based on what Cycles passes via RenderResult and didn't take layer/pass from image editor header into account. Now made it so image buffer which partial update is operating with always corresponds to what is set in image editor header. To make Cycles displaying progress of all the layers one by one made it so image_rect_update switches image editor user to newly rendering render layer. It happens only once when render engine starts rendering next render layer, so should not be annoying for navigation during rendering. Additional change to render engines was done to make it so they're able to merge composite output to final result without marking tile as done. This is done via do_merge_result argument to end_result() callback. This argument is optional so should not break script compatibility. Additional changes: - Partial display update for Blender Internal now happens from the same thread as tile rendering. This makes it so display conversion (which could be pretty heavy actually) is done in separate threads. Also gives better UI feedback when rendering easy scene with small tiles. - Avoid freeing/allocating byte buffer for render result if it's owned by the image buffer. Only mark it as invalid for color management. Saves loads of buffer re-allocations in cases when having several image editors opened with render result. This change in conjunction with the rest of the patch gave around 50%-100% speedup of render time when displaying non-combined pass during rendering on my laptop. - Partial display buffer update was wrong for buffers with number of channels different from 4. - Remove unused window from RenderJob. - Made image_buffer_rect_update static since it's only used in single file. Reviewers: brecht Reviewed By: brecht CC: dingto Differential Revision: http://developer.blender.org/D98
2013-12-17 23:42:38 +06:00
}
Changes to partial update during rendering Summary: Mainly addressed to solve old TODO with color managed fallback to CPU mode when displaying render result during rendering. That fallback was caused by the fact that partial image update was always acquiring image buffer for composite output and was only modifying display buffer directly. This was a big issue for Cycles rendering which renders layers one by one and wanted to display progress of each individual layer. This lead to situations when display buffer was based on what Cycles passes via RenderResult and didn't take layer/pass from image editor header into account. Now made it so image buffer which partial update is operating with always corresponds to what is set in image editor header. To make Cycles displaying progress of all the layers one by one made it so image_rect_update switches image editor user to newly rendering render layer. It happens only once when render engine starts rendering next render layer, so should not be annoying for navigation during rendering. Additional change to render engines was done to make it so they're able to merge composite output to final result without marking tile as done. This is done via do_merge_result argument to end_result() callback. This argument is optional so should not break script compatibility. Additional changes: - Partial display update for Blender Internal now happens from the same thread as tile rendering. This makes it so display conversion (which could be pretty heavy actually) is done in separate threads. Also gives better UI feedback when rendering easy scene with small tiles. - Avoid freeing/allocating byte buffer for render result if it's owned by the image buffer. Only mark it as invalid for color management. Saves loads of buffer re-allocations in cases when having several image editors opened with render result. This change in conjunction with the rest of the patch gave around 50%-100% speedup of render time when displaying non-combined pass during rendering on my laptop. - Partial display buffer update was wrong for buffers with number of channels different from 4. - Remove unused window from RenderJob. - Made image_buffer_rect_update static since it's only used in single file. Reviewers: brecht Reviewed By: brecht CC: dingto Differential Revision: http://developer.blender.org/D98
2013-12-17 23:42:38 +06:00
if (!cancel || merge_results) {
if (re->result->do_exr_tile) {
if (!cancel) {
render_result_exr_file_merge(re->result, result);
}
}
else if (!(re->test_break(re->tbh) && (re->r.scemode & R_BUTS_PREVIEW)))
render_result_merge(re->result, result);
/* draw */
if (!re->test_break(re->tbh)) {
result->renlay = result->layers.first; /* weak, draws first layer always */
Changes to partial update during rendering Summary: Mainly addressed to solve old TODO with color managed fallback to CPU mode when displaying render result during rendering. That fallback was caused by the fact that partial image update was always acquiring image buffer for composite output and was only modifying display buffer directly. This was a big issue for Cycles rendering which renders layers one by one and wanted to display progress of each individual layer. This lead to situations when display buffer was based on what Cycles passes via RenderResult and didn't take layer/pass from image editor header into account. Now made it so image buffer which partial update is operating with always corresponds to what is set in image editor header. To make Cycles displaying progress of all the layers one by one made it so image_rect_update switches image editor user to newly rendering render layer. It happens only once when render engine starts rendering next render layer, so should not be annoying for navigation during rendering. Additional change to render engines was done to make it so they're able to merge composite output to final result without marking tile as done. This is done via do_merge_result argument to end_result() callback. This argument is optional so should not break script compatibility. Additional changes: - Partial display update for Blender Internal now happens from the same thread as tile rendering. This makes it so display conversion (which could be pretty heavy actually) is done in separate threads. Also gives better UI feedback when rendering easy scene with small tiles. - Avoid freeing/allocating byte buffer for render result if it's owned by the image buffer. Only mark it as invalid for color management. Saves loads of buffer re-allocations in cases when having several image editors opened with render result. This change in conjunction with the rest of the patch gave around 50%-100% speedup of render time when displaying non-combined pass during rendering on my laptop. - Partial display buffer update was wrong for buffers with number of channels different from 4. - Remove unused window from RenderJob. - Made image_buffer_rect_update static since it's only used in single file. Reviewers: brecht Reviewed By: brecht CC: dingto Differential Revision: http://developer.blender.org/D98
2013-12-17 23:42:38 +06:00
re->display_update(re->duh, result, NULL);
}
}
/* free */
BLI_remlink(&engine->fullresult, result);
render_result_free(result);
}
/* Cancel */
int RE_engine_test_break(RenderEngine *engine)
{
2012-06-16 16:57:16 +00:00
Render *re = engine->re;
if (re)
return re->test_break(re->tbh);
return 0;
}
/* Statistics */
void RE_engine_update_stats(RenderEngine *engine, const char *stats, const char *info)
{
2012-06-16 16:57:16 +00:00
Render *re = engine->re;
/* stats draw callback */
if (re) {
2012-06-16 16:57:16 +00:00
re->i.statstr = stats;
re->i.infostr = info;
re->stats_draw(re->sdh, &re->i);
2012-06-16 16:57:16 +00:00
re->i.infostr = NULL;
re->i.statstr = NULL;
}
/* set engine text */
engine->text[0] = '\0';
if (stats && stats[0] && info && info[0])
BLI_snprintf(engine->text, sizeof(engine->text), "%s | %s", stats, info);
else if (info && info[0])
BLI_strncpy(engine->text, info, sizeof(engine->text));
else if (stats && stats[0])
BLI_strncpy(engine->text, stats, sizeof(engine->text));
}
void RE_engine_update_progress(RenderEngine *engine, float progress)
{
2012-06-16 16:57:16 +00:00
Render *re = engine->re;
if (re) {
CLAMP(progress, 0.0f, 1.0f);
re->progress(re->prh, progress);
}
}
void RE_engine_update_memory_stats(RenderEngine *engine, float mem_used, float mem_peak)
{
Render *re = engine->re;
if (re) {
re->i.mem_used = mem_used;
re->i.mem_peak = mem_peak;
}
}
void RE_engine_report(RenderEngine *engine, int type, const char *msg)
{
Render *re = engine->re;
if (re)
BKE_report(engine->re->reports, type, msg);
else if (engine->reports)
BKE_report(engine->reports, type, msg);
}
void RE_engine_get_current_tiles(Render *re, int *total_tiles_r, rcti **tiles_r)
{
RenderPart *pa;
int total_tiles = 0;
rcti *tiles = NULL;
int allocation_size = 0, allocation_step = BLENDER_MAX_THREADS;
if (re->engine && (re->engine->flag & RE_ENGINE_HIGHLIGHT_TILES) == 0) {
*total_tiles_r = 0;
*tiles_r = NULL;
return;
}
for (pa = re->parts.first; pa; pa = pa->next) {
if (pa->status == PART_STATUS_IN_PROGRESS) {
if (total_tiles >= allocation_size) {
if (tiles == NULL)
tiles = MEM_mallocN(allocation_step * sizeof(rcti), "current engine tiles");
else
tiles = MEM_reallocN(tiles, (total_tiles + allocation_step) * sizeof(rcti));
allocation_size += allocation_step;
}
tiles[total_tiles] = pa->disprect;
if (pa->crop) {
tiles[total_tiles].xmin += pa->crop;
tiles[total_tiles].ymin += pa->crop;
tiles[total_tiles].xmax -= pa->crop;
tiles[total_tiles].ymax -= pa->crop;
}
total_tiles++;
}
}
*total_tiles_r = total_tiles;
*tiles_r = tiles;
}
RenderData *RE_engine_get_render_data(Render *re)
{
return &re->r;
}
Bake API - bpy.ops.object.bake() New operator that can calls a bake function to the current render engine when available. This commit provides no feature for the users, but allows external engines to be accessed by the operator and be integrated with the baking api. The API itself is simple. Blender sends a populated array of BakePixels to the renderer, and gets back an array of floats with the result. The Blender Internal (and multires) system is still running independent, but we eventually will pipe it through the API as well. Cycles baking will come next as a separated commit Python Operator: ---------------- The operator can be called with some arguments, or a user interface can be created for it. In that case the arguments can be ommited and the interface can expose the settings from bpy.context.scene.render.bake bpy.ops.object.bake(type='COMBINED', filepath="", width=512, height=512, margin=16, use_selected_to_active=False, cage_extrusion=0, cage="", normal_space='TANGENT', normal_r='POS_X', normal_g='POS_Y', normal_b='POS_Z', save_mode='INTERNAL', use_clear=False, use_split_materials=False, use_automatic_name=False) Note: external save mode is currently disabled. Supported Features: ------------------ * Margin - Baked result is extended this many pixels beyond the border of each UV "island," to soften seams in the texture. * Selected to Active - bake shading on the surface of selected object to the active object. The rays are cast from the lowpoly object inwards towards the highpoly object. If the highpoly object is not entirely involved by the lowpoly object, you can tweak the rays start point with Cage Extrusion. For even more control of the cage you can use a Cage object. * Cage Extrusion - distance to use for the inward ray cast when using selected to active * Custom Cage - object to use as cage (instead of the lowpoly object). * Normal swizzle - change the axis that gets mapped to RGB * Normal space - save as tangent or object normal spaces Supported Passes: ----------------- Any pass that is supported by Blender renderlayer system. Though it's up to the external engine to provide a valid enum with its supported passes. Normal passes get a special treatment since we post-process them to converted and "swizzled" Development Notes for External Engines: --------------------------------------- (read them in bake_api.c) * For a complete implementation example look at the Cycles Bake commit (next). Review: D421 Reviewed by: Campbell Barton, Brecht van Lommel, Sergey Sharybin, Thomas Dinge Normal map pipeline "consulting" by Andy Davies (metalliandy) Original design by Brecht van Lommel. The entire commit history can be found on the branch: bake-cycles
2014-01-02 19:05:07 -02:00
/* Bake */
void RE_bake_engine_set_engine_parameters(Render *re, Main *bmain, Scene *scene)
{
re->scene = scene;
re->main = bmain;
re->r = scene->r;
/* prevent crash when freeing the scene
but it potentially leaves unfreed memory blocks
not sure how to fix this yet -- dfelinto */
re->r.layers.first = re->r.layers.last = NULL;
}
bool RE_bake_has_engine(Render *re)
{
RenderEngineType *type = RE_engines_find(re->r.engine);
return (bool)(type->bake);
}
bool RE_bake_engine(
Render *re, Object *object, const BakePixel pixel_array[],
const int num_pixels, const int depth,
const ScenePassType pass_type, float result[])
{
RenderEngineType *type = RE_engines_find(re->r.engine);
RenderEngine *engine;
int persistent_data = re->r.mode & R_PERSISTENT_DATA;
/* set render info */
re->i.cfra = re->scene->r.cfra;
BLI_strncpy(re->i.scene_name, re->scene->id.name + 2, sizeof(re->i.scene_name) - 2);
re->i.totface = re->i.totvert = re->i.totstrand = re->i.totlamp = re->i.tothalo = 0;
/* render */
engine = re->engine;
if (!engine) {
engine = RE_engine_create(type);
re->engine = engine;
}
engine->flag |= RE_ENGINE_RENDERING;
/* TODO: actually link to a parent which shouldn't happen */
engine->re = re;
engine->resolution_x = re->winx;
engine->resolution_y = re->winy;
RE_parts_init(re, false);
engine->tile_x = re->partx;
engine->tile_y = re->party;
/* update is only called so we create the engine.session */
if (type->update)
type->update(engine, re->main, re->scene);
if (type->bake)
type->bake(engine, re->scene, object, pass_type, pixel_array, num_pixels, depth, result);
engine->tile_x = 0;
engine->tile_y = 0;
engine->flag &= ~RE_ENGINE_RENDERING;
/* re->engine becomes zero if user changed active render engine during render */
if (!persistent_data || !re->engine) {
RE_engine_free(engine);
re->engine = NULL;
}
RE_parts_free(re);
if (BKE_reports_contain(re->reports, RPT_ERROR))
G.is_break = true;
return true;
}
void RE_engine_frame_set(RenderEngine *engine, int frame, float subframe)
{
Render *re = engine->re;
Scene *scene = re->scene;
double cfra = (double)frame + (double)subframe;
CLAMP(cfra, MINAFRAME, MAXFRAME);
BKE_scene_frame_set(scene, cfra);
#ifdef WITH_PYTHON
BPy_BEGIN_ALLOW_THREADS;
#endif
/* It's possible that here we're including layers which were never visible before. */
BKE_scene_update_for_newframe_ex(re->eval_ctx, re->main, scene, (1 << 20) - 1, true);
#ifdef WITH_PYTHON
BPy_END_ALLOW_THREADS;
#endif
BKE_scene_camera_switch_update(scene);
}
/* Render */
static bool render_layer_exclude_animated(Scene *scene, SceneRenderLayer *srl)
{
PointerRNA ptr;
PropertyRNA *prop;
RNA_pointer_create(&scene->id, &RNA_SceneRenderLayer, srl, &ptr);
prop = RNA_struct_find_property(&ptr, "layers_exclude");
return RNA_property_animated(&ptr, prop);
}
int RE_engine_render(Render *re, int do_all)
{
2012-06-16 16:57:16 +00:00
RenderEngineType *type = RE_engines_find(re->r.engine);
RenderEngine *engine;
int persistent_data = re->r.mode & R_PERSISTENT_DATA;
/* verify if we can render */
if (!type->render)
return 0;
if ((re->r.scemode & R_BUTS_PREVIEW) && !(type->flag & RE_USE_PREVIEW))
return 0;
if (do_all && !(type->flag & RE_USE_POSTPROCESS))
return 0;
if (!do_all && (type->flag & RE_USE_POSTPROCESS))
return 0;
/* Lock drawing in UI during data phase. */
if (re->draw_lock) {
re->draw_lock(re->dlh, 1);
}
/* update animation here so any render layer animation is applied before
* creating the render result */
if ((re->r.scemode & (R_NO_FRAME_UPDATE | R_BUTS_PREVIEW)) == 0) {
unsigned int lay = re->lay;
/* don't update layers excluded on all render layers */
if (type->flag & RE_USE_EXCLUDE_LAYERS) {
SceneRenderLayer *srl;
unsigned int non_excluded_lay = 0;
if (re->r.scemode & R_SINGLE_LAYER) {
srl = BLI_findlink(&re->r.layers, re->r.actlay);
if (srl) {
non_excluded_lay |= ~srl->lay_exclude;
/* in this case we must update all because animation for
* the scene has not been updated yet, and so may not be
* up to date until after BKE_scene_update_for_newframe */
if (render_layer_exclude_animated(re->scene, srl))
non_excluded_lay |= ~0;
}
}
else {
for (srl = re->r.layers.first; srl; srl = srl->next) {
if (!(srl->layflag & SCE_LAY_DISABLE)) {
non_excluded_lay |= ~srl->lay_exclude;
if (render_layer_exclude_animated(re->scene, srl))
non_excluded_lay |= ~0;
}
}
}
lay &= non_excluded_lay;
}
Threaded object update and EvaluationContext Summary: Made objects update happening from multiple threads. It is a task-based scheduling system which uses current dependency graph for spawning new tasks. This means threading happens on object level, but the system is flexible enough for higher granularity. Technical details: - Uses task scheduler which was recently committed to trunk (that one which Brecht ported from Cycles). - Added two utility functions to dependency graph: * DAG_threaded_update_begin, which is called to initialize threaded objects update. It will also schedule root DAG node to the queue, hence starting evaluation process. Initialization will calculate how much parents are to be evaluation before current DAG node can be scheduled. This value is used by task threads for faster detecting which nodes might be scheduled. * DAG_threaded_update_handle_node_updated which is called from task thread function when node was fully handled. This function decreases num_pending_parents of node children and schedules children with zero valency. As it might have become clear, task thread receives DAG nodes and decides which callback to call for it. Currently only BKE_object_handle_update is called for object nodes. In the future it'll call node->callback() from Ali's new DAG. - This required adding some workarounds to the render pipeline. Mainly to stop using get_object_dm() from modifiers' apply callback. Such a call was only a workaround for dependency graph glitch when rendering scene with, say, boolean modifiers before displaying this scene. Such change moves workaround from one place to another, so overall hackentropy remains the same. - Added paradigm of EvaluaitonContext. Currently it's more like just a more reliable replacement for G.is_rendering which fails in some circumstances. Future idea of this context is to also store all the local data needed for objects evaluation such as local time, Copy-on-Write data and so. There're two types of EvaluationContext: * Context used for viewport updated and owned by Main. In the future this context might be easily moved to Window or Screen to allo per-window/per-screen local time. * Context used by render engines to evaluate objects for render purposes. Render engine is an owner of this context. This context is passed to all object update routines. Reviewers: brecht, campbellbarton Reviewed By: brecht CC: lukastoenne Differential Revision: https://developer.blender.org/D94
2013-12-26 17:24:42 +06:00
BKE_scene_update_for_newframe(re->eval_ctx, re->main, re->scene, lay);
}
/* create render result */
BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
if (re->result == NULL || !(re->r.scemode & R_BUTS_PREVIEW)) {
int savebuffers = RR_USE_MEM;
if (re->result)
render_result_free(re->result);
if ((type->flag & RE_USE_SAVE_BUFFERS) && (re->r.scemode & R_EXR_TILE_FILE))
savebuffers = RR_USE_EXR;
re->result = render_result_new(re, &re->disprect, 0, savebuffers, RR_ALL_LAYERS);
}
BLI_rw_mutex_unlock(&re->resultmutex);
if (re->result == NULL) {
/* Clear UI drawing locks. */
if (re->draw_lock) {
re->draw_lock(re->dlh, 0);
}
return 1;
}
/* set render info */
2012-06-16 16:57:16 +00:00
re->i.cfra = re->scene->r.cfra;
BLI_strncpy(re->i.scene_name, re->scene->id.name + 2, sizeof(re->i.scene_name));
2012-06-16 16:57:16 +00:00
re->i.totface = re->i.totvert = re->i.totstrand = re->i.totlamp = re->i.tothalo = 0;
/* render */
engine = re->engine;
if (!engine) {
engine = RE_engine_create(type);
re->engine = engine;
}
engine->flag |= RE_ENGINE_RENDERING;
/* TODO: actually link to a parent which shouldn't happen */
2012-06-16 16:57:16 +00:00
engine->re = re;
if (re->flag & R_ANIMATION)
engine->flag |= RE_ENGINE_ANIMATION;
if (re->r.scemode & R_BUTS_PREVIEW)
engine->flag |= RE_ENGINE_PREVIEW;
engine->camera_override = re->camera_override;
engine->resolution_x = re->winx;
engine->resolution_y = re->winy;
RE_parts_init(re, false);
engine->tile_x = re->partx;
engine->tile_y = re->party;
if (re->result->do_exr_tile)
render_result_exr_file_begin(re);
if (type->update)
type->update(engine, re->main, re->scene);
/* Clear UI drawing locks. */
if (re->draw_lock) {
re->draw_lock(re->dlh, 0);
}
if (type->render)
type->render(engine, re->scene);
engine->tile_x = 0;
engine->tile_y = 0;
engine->flag &= ~RE_ENGINE_RENDERING;
render_result_free_list(&engine->fullresult, engine->fullresult.first);
/* re->engine becomes zero if user changed active render engine during render */
if (!persistent_data || !re->engine) {
RE_engine_free(engine);
re->engine = NULL;
}
if (re->result->do_exr_tile) {
BLI_rw_mutex_lock(&re->resultmutex, THREAD_LOCK_WRITE);
render_result_exr_file_end(re);
BLI_rw_mutex_unlock(&re->resultmutex);
}
RE_parts_free(re);
if (BKE_reports_contain(re->reports, RPT_ERROR))
G.is_break = true;
return 1;
}