blender/intern/cycles/render/curves.cpp
Brecht Van Lommel 207338bb58 Cycles: port curve-ray intersection from Embree for use in Cycles GPU
This keeps render results compatible for combined CPU + GPU rendering.
Peformance and quality primitives is quite different than before. There
are now two options:

* Rounded Ribbon: render hair as flat ribbon with (fake) rounded normals, for
  fast rendering. Hair curves are subdivided with a fixed number of user
  specified subdivisions.

  This gives relatively good results, especially when used with the Principled
  Hair BSDF and hair viewed from a typical distance. There are artifacts when
  viewed closed up, though this was also the case with all previous primitives
  (but different ones).

* 3D Curve: render hair as 3D curve, for accurate results when viewing hair
  close up. This automatically subdivides the curve until it is smooth.

  This gives higher quality than any of the previous primitives, but does come
  at a performance cost and is somewhat slower than our previous Thick curves.

The main problem here is performance. For CPU and OpenCL rendering performance
seems usually quite close or better for similar quality results.

However for CUDA and Optix, performance of 3D curve intersection is problematic,
with e.g. 1.45x longer render time in Koro (though there is no equivalent quality
and rounded ribbons seem fine for that scene). Any help or ideas to optimize this
are welcome.

Ref T73778

Depends on D8012

Maniphest Tasks: T73778

Differential Revision: https://developer.blender.org/D8013
2020-06-22 13:28:01 +02:00

152 lines
3.8 KiB
C++

/*
* Copyright 2011-2013 Blender Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "render/curves.h"
#include "device/device.h"
#include "render/mesh.h"
#include "render/object.h"
#include "render/scene.h"
#include "util/util_foreach.h"
#include "util/util_map.h"
#include "util/util_progress.h"
#include "util/util_vector.h"
CCL_NAMESPACE_BEGIN
/* Curve functions */
void curvebounds(float *lower, float *upper, float3 *p, int dim)
{
float *p0 = &p[0].x;
float *p1 = &p[1].x;
float *p2 = &p[2].x;
float *p3 = &p[3].x;
/* Catmull-Rom weights. */
float curve_coef[4];
curve_coef[0] = p1[dim];
curve_coef[1] = 0.5f * (-p0[dim] + p2[dim]);
curve_coef[2] = 0.5f * (2 * p0[dim] - 5 * p1[dim] + 4 * p2[dim] - p3[dim]);
curve_coef[3] = 0.5f * (-p0[dim] + 3 * p1[dim] - 3 * p2[dim] + p3[dim]);
float discroot = curve_coef[2] * curve_coef[2] - 3 * curve_coef[3] * curve_coef[1];
float ta = -1.0f;
float tb = -1.0f;
if (discroot >= 0) {
discroot = sqrtf(discroot);
ta = (-curve_coef[2] - discroot) / (3 * curve_coef[3]);
tb = (-curve_coef[2] + discroot) / (3 * curve_coef[3]);
ta = (ta > 1.0f || ta < 0.0f) ? -1.0f : ta;
tb = (tb > 1.0f || tb < 0.0f) ? -1.0f : tb;
}
*upper = max(p1[dim], p2[dim]);
*lower = min(p1[dim], p2[dim]);
float exa = p1[dim];
float exb = p2[dim];
if (ta >= 0.0f) {
float t2 = ta * ta;
float t3 = t2 * ta;
exa = curve_coef[3] * t3 + curve_coef[2] * t2 + curve_coef[1] * ta + curve_coef[0];
}
if (tb >= 0.0f) {
float t2 = tb * tb;
float t3 = t2 * tb;
exb = curve_coef[3] * t3 + curve_coef[2] * t2 + curve_coef[1] * tb + curve_coef[0];
}
*upper = max(*upper, max(exa, exb));
*lower = min(*lower, min(exa, exb));
}
/* Hair System Manager */
CurveSystemManager::CurveSystemManager()
{
curve_shape = CURVE_THICK;
subdivisions = 3;
use_curves = true;
need_update = true;
need_mesh_update = false;
}
CurveSystemManager::~CurveSystemManager()
{
}
void CurveSystemManager::device_update(Device *device,
DeviceScene *dscene,
Scene * /*scene*/,
Progress &progress)
{
if (!need_update)
return;
device_free(device, dscene);
progress.set_status("Updating Hair settings", "Copying Hair settings to device");
KernelCurves *kcurve = &dscene->data.curve;
kcurve->curveflags = 0;
if (use_curves) {
if (curve_shape == CURVE_RIBBON) {
kcurve->curveflags |= CURVE_KN_RIBBONS;
}
/* Matching the tesselation rate limit in Embree. */
kcurve->subdivisions = clamp(1 << subdivisions, 1, 16);
}
if (progress.get_cancel())
return;
need_update = false;
}
void CurveSystemManager::device_free(Device * /*device*/, DeviceScene * /*dscene*/)
{
}
bool CurveSystemManager::modified(const CurveSystemManager &CurveSystemManager)
{
return !(use_curves == CurveSystemManager.use_curves &&
subdivisions == CurveSystemManager.subdivisions);
}
bool CurveSystemManager::modified_mesh(const CurveSystemManager &CurveSystemManager)
{
return !(use_curves == CurveSystemManager.use_curves);
}
void CurveSystemManager::tag_update(Scene * /*scene*/)
{
need_update = true;
}
void CurveSystemManager::tag_update_mesh()
{
need_mesh_update = true;
}
CCL_NAMESPACE_END