blender/intern/cycles/render/object.cpp

410 lines
11 KiB
C++
Raw Normal View History

/*
* Copyright 2011, Blender Foundation.
*
* 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.
*/
#include "device.h"
#include "light.h"
#include "mesh.h"
#include "curves.h"
#include "object.h"
#include "scene.h"
#include "util_foreach.h"
#include "util_map.h"
#include "util_progress.h"
#include "util_vector.h"
CCL_NAMESPACE_BEGIN
/* Object */
Object::Object()
{
name = "";
mesh = NULL;
tfm = transform_identity();
visibility = ~0;
random_id = 0;
pass_id = 0;
particle_id = 0;
Cycles: merging features from tomato branch. === BVH build time optimizations === * BVH building was multithreaded. Not all building is multithreaded, packing and the initial bounding/splitting is still single threaded, but recursive splitting is, which was the main bottleneck. * Object splitting now uses binning rather than sorting of all elements, using code from the Embree raytracer from Intel. http://software.intel.com/en-us/articles/embree-photo-realistic-ray-tracing-kernels/ * Other small changes to avoid allocations, pack memory more tightly, avoid some unnecessary operations, ... These optimizations do not work yet when Spatial Splits are enabled, for that more work is needed. There's also other optimizations still needed, in particular for the case of many low poly objects, the packing step and node memory allocation. BVH raytracing time should remain about the same, but BVH build time should be significantly reduced, test here show speedup of about 5x to 10x on a dual core and 5x to 25x on an 8-core machine, depending on the scene. === Threads === Centralized task scheduler for multithreading, which is basically the CPU device threading code wrapped into something reusable. Basic idea is that there is a single TaskScheduler that keeps a pool of threads, one for each core. Other places in the code can then create a TaskPool that they can drop Tasks in to be executed by the scheduler, and wait for them to complete or cancel them early. === Normal ==== Added a Normal output to the texture coordinate node. This currently gives the object space normal, which is the same under object animation. In the future this might become a "generated" normal so it's also stable for deforming objects, but for now it's already useful for non-deforming objects. === Render Layers === Per render layer Samples control, leaving it to 0 will use the common scene setting. Environment pass will now render environment even if film is set to transparent. Exclude Layers" added. Scene layers (all object that influence the render, directly or indirectly) are shared between all render layers. However sometimes it's useful to leave out some object influence for a particular render layer. That's what this option allows you to do. === Filter Glossy === When using a value higher than 0.0, this will blur glossy reflections after blurry bounces, to reduce noise at the cost of accuracy. 1.0 is a good starting value to tweak. Some light paths have a low probability of being found while contributing much light to the pixel. As a result these light paths will be found in some pixels and not in others, causing fireflies. An example of such a difficult path might be a small light that is causing a small specular highlight on a sharp glossy material, which we are seeing through a rough glossy material. With path tracing it is difficult to find the specular highlight, but if we increase the roughness on the material the highlight gets bigger and softer, and so easier to find. Often this blurring will be hardly noticeable, because we are seeing it through a blurry material anyway, but there are also cases where this will lead to a loss of detail in lighting.
2012-04-28 08:53:59 +00:00
bounds = BoundBox::empty;
motion.pre = transform_identity();
motion.post = transform_identity();
use_motion = false;
use_holdout = false;
curverender = false;
}
Object::~Object()
{
}
void Object::compute_bounds(bool motion_blur, float shuttertime)
{
BoundBox mbounds = mesh->bounds;
if(motion_blur && use_motion) {
DecompMotionTransform decomp;
transform_motion_decompose(&decomp, &motion, &tfm);
bounds = BoundBox::empty;
/* todo: this is really terrible. according to pbrt there is a better
* way to find this iteratively, but did not find implementation yet
* or try to implement myself */
float start_t = 0.5f - shuttertime*0.25f;
float end_t = 0.5f + shuttertime*0.25f;
for(float t = start_t; t < end_t; t += (1.0f/128.0f)*shuttertime) {
Transform ttfm;
transform_motion_interpolate(&ttfm, &decomp, t);
bounds.grow(mbounds.transformed(&ttfm));
}
}
else
bounds = mbounds.transformed(&tfm);
}
void Object::apply_transform()
{
if(!mesh || tfm == transform_identity())
return;
for(size_t i = 0; i < mesh->verts.size(); i++)
mesh->verts[i] = transform_point(&tfm, mesh->verts[i]);
for(size_t i = 0; i < mesh->curve_keys.size(); i++)
mesh->curve_keys[i].co = transform_point(&tfm, mesh->curve_keys[i].co);
Attribute *attr_tangent = mesh->curve_attributes.find(ATTR_STD_CURVE_TANGENT);
Attribute *attr_fN = mesh->attributes.find(ATTR_STD_FACE_NORMAL);
Attribute *attr_vN = mesh->attributes.find(ATTR_STD_VERTEX_NORMAL);
Transform ntfm = transform_transpose(transform_inverse(tfm));
/* we keep normals pointing in same direction on negative scale, notify
2012-06-09 17:22:52 +00:00
* mesh about this in it (re)calculates normals */
if(transform_negative_scale(tfm))
mesh->transform_negative_scaled = true;
if(attr_fN) {
float3 *fN = attr_fN->data_float3();
for(size_t i = 0; i < mesh->triangles.size(); i++)
fN[i] = transform_direction(&ntfm, fN[i]);
}
if(attr_vN) {
float3 *vN = attr_vN->data_float3();
for(size_t i = 0; i < mesh->verts.size(); i++)
vN[i] = transform_direction(&ntfm, vN[i]);
}
if(attr_tangent) {
float3 *tangent = attr_tangent->data_float3();
for(size_t i = 0; i < mesh->curve_keys.size(); i++)
tangent[i] = transform_direction(&tfm, tangent[i]);
}
if(bounds.valid()) {
mesh->compute_bounds();
compute_bounds(false, 0.0f);
}
/* tfm is not reset to identity, all code that uses it needs to check the
transform_applied boolean */
}
void Object::tag_update(Scene *scene)
{
if(mesh) {
if(mesh->transform_applied)
mesh->need_update = true;
foreach(uint sindex, mesh->used_shaders) {
Shader *shader = scene->shaders[sindex];
if(shader->sample_as_light && shader->has_surface_emission)
scene->light_manager->need_update = true;
}
}
scene->curve_system_manager->need_update = true;
scene->mesh_manager->need_update = true;
scene->object_manager->need_update = true;
}
/* Object Manager */
ObjectManager::ObjectManager()
{
need_update = true;
}
ObjectManager::~ObjectManager()
{
}
void ObjectManager::device_update_transforms(Device *device, DeviceScene *dscene, Scene *scene, uint *object_flag, Progress& progress)
{
float4 *objects;
float4 *objects_vector = NULL;
int i = 0;
map<Mesh*, float> surface_area_map;
Scene::MotionType need_motion = scene->need_motion(device->info.advanced_shading);
bool have_motion = false;
objects = dscene->objects.resize(OBJECT_SIZE*scene->objects.size());
if(need_motion == Scene::MOTION_PASS)
objects_vector = dscene->objects_vector.resize(OBJECT_VECTOR_SIZE*scene->objects.size());
foreach(Object *ob, scene->objects) {
Mesh *mesh = ob->mesh;
uint flag = 0;
/* compute transformations */
Transform tfm = ob->tfm;
Transform itfm = transform_inverse(tfm);
/* compute surface area. for uniform scale we can do avoid the many
2012-06-09 17:22:52 +00:00
* transform calls and share computation for instances */
/* todo: correct for displacement, and move to a better place */
float uniform_scale;
float surface_area = 0.0f;
float pass_id = ob->pass_id;
float random_number = (float)ob->random_id * (1.0f/(float)0xFFFFFFFF);
if(transform_uniform_scale(tfm, uniform_scale)) {
map<Mesh*, float>::iterator it = surface_area_map.find(mesh);
if(it == surface_area_map.end()) {
foreach(Mesh::Triangle& t, mesh->triangles) {
float3 p1 = mesh->verts[t.v[0]];
float3 p2 = mesh->verts[t.v[1]];
float3 p3 = mesh->verts[t.v[2]];
surface_area += triangle_area(p1, p2, p3);
}
foreach(Mesh::Curve& curve, mesh->curves) {
int first_key = curve.first_key;
for(int i = 0; i < curve.num_segments(); i++) {
float3 p1 = mesh->curve_keys[first_key + i].co;
float r1 = mesh->curve_keys[first_key + i].radius;
float3 p2 = mesh->curve_keys[first_key + i + 1].co;
float r2 = mesh->curve_keys[first_key + i + 1].radius;
/* currently ignores segment overlaps*/
surface_area += M_PI_F *(r1 + r2) * len(p1 - p2);
}
}
surface_area_map[mesh] = surface_area;
}
else
surface_area = it->second;
surface_area *= uniform_scale;
}
else {
foreach(Mesh::Triangle& t, mesh->triangles) {
float3 p1 = transform_point(&tfm, mesh->verts[t.v[0]]);
float3 p2 = transform_point(&tfm, mesh->verts[t.v[1]]);
float3 p3 = transform_point(&tfm, mesh->verts[t.v[2]]);
surface_area += triangle_area(p1, p2, p3);
}
foreach(Mesh::Curve& curve, mesh->curves) {
int first_key = curve.first_key;
for(int i = 0; i < curve.num_segments(); i++) {
float3 p1 = mesh->curve_keys[first_key + i].co;
float r1 = mesh->curve_keys[first_key + i].radius;
float3 p2 = mesh->curve_keys[first_key + i + 1].co;
float r2 = mesh->curve_keys[first_key + i + 1].radius;
p1 = transform_point(&tfm, p1);
p2 = transform_point(&tfm, p2);
/* currently ignores segment overlaps*/
surface_area += M_PI_F *(r1 + r2) * len(p1 - p2);
}
}
}
/* pack in texture */
int offset = i*OBJECT_SIZE;
memcpy(&objects[offset], &tfm, sizeof(float4)*3);
memcpy(&objects[offset+4], &itfm, sizeof(float4)*3);
objects[offset+8] = make_float4(surface_area, pass_id, random_number, __int_as_float(ob->particle_id));
if(need_motion == Scene::MOTION_PASS) {
/* motion transformations, is world/object space depending if mesh
2012-06-09 17:22:52 +00:00
* comes with deformed position in object space, or if we transform
* the shading point in world space */
Transform mtfm_pre = ob->motion.pre;
Transform mtfm_post = ob->motion.post;
if(!mesh->attributes.find(ATTR_STD_MOTION_PRE))
mtfm_pre = mtfm_pre * itfm;
if(!mesh->attributes.find(ATTR_STD_MOTION_POST))
mtfm_post = mtfm_post * itfm;
memcpy(&objects_vector[i*OBJECT_VECTOR_SIZE+0], &mtfm_pre, sizeof(float4)*3);
memcpy(&objects_vector[i*OBJECT_VECTOR_SIZE+3], &mtfm_post, sizeof(float4)*3);
}
#ifdef __OBJECT_MOTION__
else if(need_motion == Scene::MOTION_BLUR) {
if(ob->use_motion) {
/* decompose transformations for interpolation */
DecompMotionTransform decomp;
transform_motion_decompose(&decomp, &ob->motion, &ob->tfm);
memcpy(&objects[offset], &decomp, sizeof(float4)*8);
flag |= SD_OBJECT_MOTION;
have_motion = true;
}
}
#endif
/* dupli object coords */
objects[offset+9] = make_float4(ob->dupli_generated[0], ob->dupli_generated[1], ob->dupli_generated[2], 0.0f);
objects[offset+10] = make_float4(ob->dupli_uv[0], ob->dupli_uv[1], 0.0f, 0.0f);
/* object flag */
if(ob->use_holdout)
flag |= SD_HOLDOUT_MASK;
object_flag[i] = flag;
i++;
if(progress.get_cancel()) return;
}
device->tex_alloc("__objects", dscene->objects);
if(need_motion == Scene::MOTION_PASS)
device->tex_alloc("__objects_vector", dscene->objects_vector);
dscene->data.bvh.have_motion = have_motion;
}
void ObjectManager::device_update(Device *device, DeviceScene *dscene, Scene *scene, Progress& progress)
{
if(!need_update)
return;
device_free(device, dscene);
if(scene->objects.size() == 0)
return;
/* object info flag */
uint *object_flag = dscene->object_flag.resize(scene->objects.size());
/* set object transform matrices, before applying static transforms */
progress.set_status("Updating Objects", "Copying Transformations to device");
device_update_transforms(device, dscene, scene, object_flag, progress);
if(progress.get_cancel()) return;
/* prepare for static BVH building */
/* todo: do before to support getting object level coords? */
if(scene->params.bvh_type == SceneParams::BVH_STATIC) {
progress.set_status("Updating Objects", "Applying Static Transformations");
apply_static_transforms(scene, object_flag, progress);
}
/* allocate object flag */
device->tex_alloc("__object_flag", dscene->object_flag);
need_update = false;
}
void ObjectManager::device_free(Device *device, DeviceScene *dscene)
{
device->tex_free(dscene->objects);
dscene->objects.clear();
device->tex_free(dscene->objects_vector);
dscene->objects_vector.clear();
device->tex_free(dscene->object_flag);
dscene->object_flag.clear();
}
void ObjectManager::apply_static_transforms(Scene *scene, uint *object_flag, Progress& progress)
{
/* todo: normals and displacement should be done before applying transform! */
/* todo: create objects/meshes in right order! */
/* counter mesh users */
map<Mesh*, int> mesh_users;
#ifdef __OBJECT_MOTION__
Scene::MotionType need_motion = scene->need_motion();
bool motion_blur = need_motion == Scene::MOTION_BLUR;
#else
bool motion_blur = false;
#endif
int i = 0;
foreach(Object *object, scene->objects) {
map<Mesh*, int>::iterator it = mesh_users.find(object->mesh);
if(it == mesh_users.end())
mesh_users[object->mesh] = 1;
else
it->second++;
}
if(progress.get_cancel()) return;
/* apply transforms for objects with single user meshes */
foreach(Object *object, scene->objects) {
if(mesh_users[object->mesh] == 1) {
if(!(motion_blur && object->use_motion)) {
if(!object->mesh->transform_applied) {
object->apply_transform();
object->mesh->transform_applied = true;
if(progress.get_cancel()) return;
}
object_flag[i] |= SD_TRANSFORM_APPLIED;
}
}
i++;
}
}
void ObjectManager::tag_update(Scene *scene)
{
need_update = true;
scene->curve_system_manager->need_update = true;
scene->mesh_manager->need_update = true;
scene->light_manager->need_update = true;
}
CCL_NAMESPACE_END