2023-06-14 16:52:36 +10:00
|
|
|
/* SPDX-FileCopyrightText: 2009-2010 Sony Pictures Imageworks Inc., et al. All Rights Reserved.
|
|
|
|
|
* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2023-06-14 16:52:36 +10:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2023-06-14 16:52:36 +10:00
|
|
|
* Adapted code from Open Shading Language. */
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
|
#include <OSL/genclosure.h>
|
|
|
|
|
#include <OSL/oslclosure.h>
|
|
|
|
|
|
2022-09-08 19:31:44 +02:00
|
|
|
#include "kernel/types.h"
|
|
|
|
|
|
|
|
|
|
#include "kernel/osl/globals.h"
|
|
|
|
|
#include "kernel/osl/services.h"
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2021-10-24 14:19:19 +02:00
|
|
|
#include "util/math.h"
|
|
|
|
|
#include "util/param.h"
|
2011-04-27 11:58:34 +00:00
|
|
|
|
Cycles: merge of cycles-x branch, a major update to the renderer
This includes much improved GPU rendering performance, viewport interactivity,
new shadow catcher, revamped sampling settings, subsurface scattering anisotropy,
new GPU volume sampling, improved PMJ sampling pattern, and more.
Some features have also been removed or changed, breaking backwards compatibility.
Including the removal of the OpenCL backend, for which alternatives are under
development.
Release notes and code docs:
https://wiki.blender.org/wiki/Reference/Release_Notes/3.0/Cycles
https://wiki.blender.org/wiki/Source/Render/Cycles
Credits:
* Sergey Sharybin
* Brecht Van Lommel
* Patrick Mours (OptiX backend)
* Christophe Hery (subsurface scattering anisotropy)
* William Leeson (PMJ sampling pattern)
* Alaska (various fixes and tweaks)
* Thomas Dinges (various fixes)
For the full commit history, see the cycles-x branch. This squashes together
all the changes since intermediate changes would often fail building or tests.
Ref T87839, T87837, T87836
Fixes T90734, T89353, T80267, T80267, T77185, T69800
2021-09-20 17:59:20 +02:00
|
|
|
#include "kernel/device/cpu/compat.h"
|
|
|
|
|
#include "kernel/device/cpu/globals.h"
|
|
|
|
|
|
2022-09-12 18:46:20 +02:00
|
|
|
#include "kernel/geom/object.h"
|
|
|
|
|
#include "kernel/util/differential.h"
|
|
|
|
|
|
|
|
|
|
#include "kernel/osl/osl.h"
|
|
|
|
|
|
|
|
|
|
#define TO_VEC3(v) OSL::Vec3(v.x, v.y, v.z)
|
|
|
|
|
#define TO_FLOAT3(v) make_float3(v[0], v[1], v[2])
|
2012-10-20 12:18:00 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
2022-11-09 14:25:32 +01:00
|
|
|
static_assert(sizeof(OSLClosure) == sizeof(OSL::ClosureColor) &&
|
|
|
|
|
sizeof(OSLClosureAdd) == sizeof(OSL::ClosureAdd) &&
|
|
|
|
|
sizeof(OSLClosureMul) == sizeof(OSL::ClosureMul) &&
|
|
|
|
|
sizeof(OSLClosureComponent) == sizeof(OSL::ClosureComponent));
|
|
|
|
|
static_assert(sizeof(ShaderGlobals) == sizeof(OSL::ShaderGlobals) &&
|
|
|
|
|
offsetof(ShaderGlobals, Ci) == offsetof(OSL::ShaderGlobals, Ci));
|
|
|
|
|
|
2012-10-20 12:18:00 +00:00
|
|
|
/* Registration */
|
|
|
|
|
|
2022-09-08 19:31:44 +02:00
|
|
|
#define OSL_CLOSURE_STRUCT_BEGIN(Upper, lower) \
|
|
|
|
|
static OSL::ClosureParam *osl_closure_##lower##_params() \
|
|
|
|
|
{ \
|
|
|
|
|
static OSL::ClosureParam params[] = {
|
|
|
|
|
#define OSL_CLOSURE_STRUCT_END(Upper, lower) \
|
|
|
|
|
CLOSURE_STRING_KEYPARAM(Upper##Closure, label, "label"), CLOSURE_FINISH_PARAM(Upper##Closure) \
|
|
|
|
|
} \
|
|
|
|
|
; \
|
|
|
|
|
return params; \
|
2017-04-18 11:43:09 +02:00
|
|
|
}
|
2022-09-08 19:31:44 +02:00
|
|
|
#define OSL_CLOSURE_STRUCT_MEMBER(Upper, TYPE, type, name, key) \
|
|
|
|
|
CLOSURE_##TYPE##_KEYPARAM(Upper##Closure, name, key),
|
|
|
|
|
#define OSL_CLOSURE_STRUCT_ARRAY_MEMBER(Upper, TYPE, type, name, key, size) \
|
|
|
|
|
CLOSURE_##TYPE##_ARRAY_PARAM(Upper##Closure, name, size),
|
2017-04-18 11:43:09 +02:00
|
|
|
|
2022-09-08 19:31:44 +02:00
|
|
|
#include "closures_template.h"
|
2022-05-17 16:27:37 +02:00
|
|
|
|
2023-08-10 23:53:37 +02:00
|
|
|
static OSL::ClosureParam *osl_closure_layer_params()
|
|
|
|
|
{
|
|
|
|
|
static OSL::ClosureParam params[] = {CLOSURE_CLOSURE_PARAM(LayerClosure, top),
|
|
|
|
|
CLOSURE_CLOSURE_PARAM(LayerClosure, base),
|
|
|
|
|
CLOSURE_FINISH_PARAM(LayerClosure)};
|
|
|
|
|
return params;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-08 19:31:44 +02:00
|
|
|
void OSLRenderServices::register_closures(OSL::ShadingSystem *ss)
|
2017-04-18 11:43:09 +02:00
|
|
|
{
|
2022-09-08 19:31:44 +02:00
|
|
|
#define OSL_CLOSURE_STRUCT_BEGIN(Upper, lower) \
|
|
|
|
|
ss->register_closure( \
|
|
|
|
|
#lower, OSL_CLOSURE_##Upper##_ID, osl_closure_##lower##_params(), nullptr, nullptr);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2022-09-08 19:31:44 +02:00
|
|
|
#include "closures_template.h"
|
2023-08-10 23:53:37 +02:00
|
|
|
ss->register_closure(
|
|
|
|
|
"layer", OSL_CLOSURE_LAYER_ID, osl_closure_layer_params(), nullptr, nullptr);
|
2017-04-18 11:43:09 +02:00
|
|
|
}
|
2017-11-01 21:07:15 +01:00
|
|
|
|
2022-11-09 14:25:32 +01:00
|
|
|
/* Surface & Background */
|
2022-09-12 18:46:20 +02:00
|
|
|
|
2022-11-09 14:25:32 +01:00
|
|
|
template<>
|
|
|
|
|
void osl_eval_nodes<SHADER_TYPE_SURFACE>(const KernelGlobalsCPU *kg,
|
|
|
|
|
const void *state,
|
|
|
|
|
ShaderData *sd,
|
|
|
|
|
uint32_t path_flag)
|
2022-09-12 18:46:20 +02:00
|
|
|
{
|
2022-11-09 14:25:32 +01:00
|
|
|
/* setup shader globals from shader data */
|
|
|
|
|
OSLThreadData *tdata = kg->osl_tdata;
|
|
|
|
|
shaderdata_to_shaderglobals(
|
|
|
|
|
kg, sd, path_flag, reinterpret_cast<ShaderGlobals *>(&tdata->globals));
|
2022-09-12 18:46:20 +02:00
|
|
|
|
|
|
|
|
/* clear trace data */
|
|
|
|
|
tdata->tracedata.init = false;
|
|
|
|
|
|
|
|
|
|
/* Used by render-services. */
|
|
|
|
|
sd->osl_globals = kg;
|
|
|
|
|
if (path_flag & PATH_RAY_SHADOW) {
|
|
|
|
|
sd->osl_path_state = nullptr;
|
|
|
|
|
sd->osl_shadow_path_state = (const IntegratorShadowStateCPU *)state;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
sd->osl_path_state = (const IntegratorStateCPU *)state;
|
|
|
|
|
sd->osl_shadow_path_state = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* execute shader for this point */
|
|
|
|
|
OSL::ShadingSystem *ss = (OSL::ShadingSystem *)kg->osl_ss;
|
|
|
|
|
OSL::ShaderGlobals *globals = &tdata->globals;
|
|
|
|
|
OSL::ShadingContext *octx = tdata->context;
|
|
|
|
|
int shader = sd->shader & SHADER_MASK;
|
|
|
|
|
|
2022-11-09 14:25:32 +01:00
|
|
|
if (sd->object == OBJECT_NONE && sd->lamp == LAMP_NONE) {
|
|
|
|
|
/* background */
|
|
|
|
|
if (kg->osl->background_state) {
|
2023-12-10 17:08:47 +01:00
|
|
|
#if OSL_LIBRARY_VERSION_CODE >= 11304
|
|
|
|
|
ss->execute(*octx,
|
|
|
|
|
*(kg->osl->background_state),
|
|
|
|
|
kg->osl_thread_index,
|
|
|
|
|
0,
|
|
|
|
|
*globals,
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr);
|
|
|
|
|
#else
|
2022-11-09 14:25:32 +01:00
|
|
|
ss->execute(octx, *(kg->osl->background_state), *globals);
|
2023-12-10 17:08:47 +01:00
|
|
|
#endif
|
2022-09-12 18:46:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
2022-11-09 14:25:32 +01:00
|
|
|
else {
|
|
|
|
|
/* automatic bump shader */
|
|
|
|
|
if (kg->osl->bump_state[shader]) {
|
|
|
|
|
/* save state */
|
|
|
|
|
const float3 P = sd->P;
|
|
|
|
|
const float dP = sd->dP;
|
|
|
|
|
const OSL::Vec3 dPdx = globals->dPdx;
|
|
|
|
|
const OSL::Vec3 dPdy = globals->dPdy;
|
|
|
|
|
|
|
|
|
|
/* set state as if undisplaced */
|
|
|
|
|
if (sd->flag & SD_HAS_DISPLACEMENT) {
|
|
|
|
|
float data[9];
|
|
|
|
|
bool found = kg->osl->services->get_attribute(sd,
|
|
|
|
|
true,
|
|
|
|
|
OSLRenderServices::u_empty,
|
|
|
|
|
TypeDesc::TypeVector,
|
|
|
|
|
OSLRenderServices::u_geom_undisplaced,
|
|
|
|
|
data);
|
|
|
|
|
(void)found;
|
|
|
|
|
assert(found);
|
|
|
|
|
|
|
|
|
|
differential3 tmp_dP;
|
|
|
|
|
memcpy(&sd->P, data, sizeof(float) * 3);
|
|
|
|
|
memcpy(&tmp_dP.dx, data + 3, sizeof(float) * 3);
|
|
|
|
|
memcpy(&tmp_dP.dy, data + 6, sizeof(float) * 3);
|
|
|
|
|
|
|
|
|
|
object_position_transform(kg, sd, &sd->P);
|
|
|
|
|
object_dir_transform(kg, sd, &tmp_dP.dx);
|
|
|
|
|
object_dir_transform(kg, sd, &tmp_dP.dy);
|
|
|
|
|
|
|
|
|
|
sd->dP = differential_make_compact(tmp_dP);
|
|
|
|
|
|
|
|
|
|
globals->P = TO_VEC3(sd->P);
|
|
|
|
|
globals->dPdx = TO_VEC3(tmp_dP.dx);
|
|
|
|
|
globals->dPdy = TO_VEC3(tmp_dP.dy);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-10 17:08:47 +01:00
|
|
|
/* execute bump shader */
|
|
|
|
|
#if OSL_LIBRARY_VERSION_CODE >= 11304
|
|
|
|
|
ss->execute(*octx,
|
|
|
|
|
*(kg->osl->bump_state[shader]),
|
|
|
|
|
kg->osl_thread_index,
|
|
|
|
|
0,
|
|
|
|
|
*globals,
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr);
|
|
|
|
|
#else
|
2022-11-09 14:25:32 +01:00
|
|
|
ss->execute(octx, *(kg->osl->bump_state[shader]), *globals);
|
2023-12-10 17:08:47 +01:00
|
|
|
#endif
|
2022-11-09 14:25:32 +01:00
|
|
|
|
|
|
|
|
/* reset state */
|
|
|
|
|
sd->P = P;
|
|
|
|
|
sd->dP = dP;
|
|
|
|
|
|
|
|
|
|
globals->P = TO_VEC3(P);
|
|
|
|
|
globals->dPdx = TO_VEC3(dPdx);
|
|
|
|
|
globals->dPdy = TO_VEC3(dPdy);
|
|
|
|
|
}
|
2022-09-12 18:46:20 +02:00
|
|
|
|
2022-11-09 14:25:32 +01:00
|
|
|
/* surface shader */
|
|
|
|
|
if (kg->osl->surface_state[shader]) {
|
2023-12-10 17:08:47 +01:00
|
|
|
#if OSL_LIBRARY_VERSION_CODE >= 11304
|
|
|
|
|
ss->execute(*octx,
|
|
|
|
|
*(kg->osl->surface_state[shader]),
|
|
|
|
|
kg->osl_thread_index,
|
|
|
|
|
0,
|
|
|
|
|
*globals,
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr);
|
|
|
|
|
#else
|
2022-11-09 14:25:32 +01:00
|
|
|
ss->execute(octx, *(kg->osl->surface_state[shader]), *globals);
|
2023-12-10 17:08:47 +01:00
|
|
|
#endif
|
2022-11-09 14:25:32 +01:00
|
|
|
}
|
2022-09-12 18:46:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* flatten closure tree */
|
|
|
|
|
if (globals->Ci) {
|
2022-11-09 14:25:32 +01:00
|
|
|
flatten_closure_tree(kg, sd, path_flag, reinterpret_cast<OSLClosure *>(globals->Ci));
|
2022-09-12 18:46:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-09 14:25:32 +01:00
|
|
|
/* Volume */
|
2022-09-12 18:46:20 +02:00
|
|
|
|
2022-11-09 14:25:32 +01:00
|
|
|
template<>
|
|
|
|
|
void osl_eval_nodes<SHADER_TYPE_VOLUME>(const KernelGlobalsCPU *kg,
|
|
|
|
|
const void *state,
|
|
|
|
|
ShaderData *sd,
|
|
|
|
|
uint32_t path_flag)
|
2022-09-12 18:46:20 +02:00
|
|
|
{
|
|
|
|
|
/* setup shader globals from shader data */
|
|
|
|
|
OSLThreadData *tdata = kg->osl_tdata;
|
2022-11-09 14:25:32 +01:00
|
|
|
shaderdata_to_shaderglobals(
|
|
|
|
|
kg, sd, path_flag, reinterpret_cast<ShaderGlobals *>(&tdata->globals));
|
2022-09-12 18:46:20 +02:00
|
|
|
|
2022-11-09 14:25:32 +01:00
|
|
|
/* clear trace data */
|
|
|
|
|
tdata->tracedata.init = false;
|
2022-09-12 18:46:20 +02:00
|
|
|
|
2022-11-09 14:25:32 +01:00
|
|
|
/* Used by render-services. */
|
|
|
|
|
sd->osl_globals = kg;
|
|
|
|
|
if (path_flag & PATH_RAY_SHADOW) {
|
|
|
|
|
sd->osl_path_state = nullptr;
|
|
|
|
|
sd->osl_shadow_path_state = (const IntegratorShadowStateCPU *)state;
|
2022-09-12 18:46:20 +02:00
|
|
|
}
|
2022-11-09 14:25:32 +01:00
|
|
|
else {
|
|
|
|
|
sd->osl_path_state = (const IntegratorStateCPU *)state;
|
|
|
|
|
sd->osl_shadow_path_state = nullptr;
|
2022-09-12 18:46:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* execute shader */
|
|
|
|
|
OSL::ShadingSystem *ss = (OSL::ShadingSystem *)kg->osl_ss;
|
|
|
|
|
OSL::ShaderGlobals *globals = &tdata->globals;
|
|
|
|
|
OSL::ShadingContext *octx = tdata->context;
|
|
|
|
|
int shader = sd->shader & SHADER_MASK;
|
|
|
|
|
|
|
|
|
|
if (kg->osl->volume_state[shader]) {
|
2023-12-10 17:08:47 +01:00
|
|
|
#if OSL_LIBRARY_VERSION_CODE >= 11304
|
|
|
|
|
ss->execute(*octx,
|
|
|
|
|
*(kg->osl->volume_state[shader]),
|
|
|
|
|
kg->osl_thread_index,
|
|
|
|
|
0,
|
|
|
|
|
*globals,
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr);
|
|
|
|
|
#else
|
2022-09-12 18:46:20 +02:00
|
|
|
ss->execute(octx, *(kg->osl->volume_state[shader]), *globals);
|
2023-12-10 17:08:47 +01:00
|
|
|
#endif
|
2022-09-12 18:46:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* flatten closure tree */
|
|
|
|
|
if (globals->Ci) {
|
2022-11-09 14:25:32 +01:00
|
|
|
flatten_closure_tree(kg, sd, path_flag, reinterpret_cast<OSLClosure *>(globals->Ci));
|
2022-09-12 18:46:20 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Displacement */
|
|
|
|
|
|
2022-11-09 14:25:32 +01:00
|
|
|
template<>
|
|
|
|
|
void osl_eval_nodes<SHADER_TYPE_DISPLACEMENT>(const KernelGlobalsCPU *kg,
|
|
|
|
|
const void *state,
|
|
|
|
|
ShaderData *sd,
|
|
|
|
|
uint32_t path_flag)
|
2022-09-12 18:46:20 +02:00
|
|
|
{
|
|
|
|
|
/* setup shader globals from shader data */
|
|
|
|
|
OSLThreadData *tdata = kg->osl_tdata;
|
2022-11-09 14:25:32 +01:00
|
|
|
shaderdata_to_shaderglobals(
|
|
|
|
|
kg, sd, path_flag, reinterpret_cast<ShaderGlobals *>(&tdata->globals));
|
|
|
|
|
|
|
|
|
|
/* clear trace data */
|
|
|
|
|
tdata->tracedata.init = false;
|
|
|
|
|
|
|
|
|
|
/* Used by render-services. */
|
|
|
|
|
sd->osl_globals = kg;
|
|
|
|
|
sd->osl_path_state = (const IntegratorStateCPU *)state;
|
|
|
|
|
sd->osl_shadow_path_state = nullptr;
|
2022-09-12 18:46:20 +02:00
|
|
|
|
|
|
|
|
/* execute shader */
|
|
|
|
|
OSL::ShadingSystem *ss = (OSL::ShadingSystem *)kg->osl_ss;
|
|
|
|
|
OSL::ShaderGlobals *globals = &tdata->globals;
|
|
|
|
|
OSL::ShadingContext *octx = tdata->context;
|
|
|
|
|
int shader = sd->shader & SHADER_MASK;
|
|
|
|
|
|
|
|
|
|
if (kg->osl->displacement_state[shader]) {
|
2023-12-10 17:08:47 +01:00
|
|
|
#if OSL_LIBRARY_VERSION_CODE >= 11304
|
|
|
|
|
ss->execute(*octx,
|
|
|
|
|
*(kg->osl->displacement_state[shader]),
|
|
|
|
|
kg->osl_thread_index,
|
|
|
|
|
0,
|
|
|
|
|
*globals,
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr);
|
|
|
|
|
#else
|
2022-09-12 18:46:20 +02:00
|
|
|
ss->execute(octx, *(kg->osl->displacement_state[shader]), *globals);
|
2023-12-10 17:08:47 +01:00
|
|
|
#endif
|
2022-09-12 18:46:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* get back position */
|
|
|
|
|
sd->P = TO_FLOAT3(globals->P);
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_END
|