Blender V4.3
background.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "scene/background.h"
6#include "device/device.h"
7#include "scene/integrator.h"
8#include "scene/light.h"
9#include "scene/scene.h"
10#include "scene/shader.h"
11#include "scene/shader_graph.h"
12#include "scene/shader_nodes.h"
13#include "scene/stats.h"
14
15#include "util/foreach.h"
16#include "util/math.h"
17#include "util/time.h"
18#include "util/types.h"
19
21
23{
24 NodeType *type = NodeType::add("background", create);
25
26 SOCKET_BOOLEAN(use_shader, "Use Shader", true);
27 SOCKET_UINT(visibility, "Visibility", PATH_RAY_ALL_VISIBILITY);
28
29 SOCKET_BOOLEAN(transparent, "Transparent", false);
30 SOCKET_BOOLEAN(transparent_glass, "Transparent Glass", false);
31 SOCKET_FLOAT(transparent_roughness_threshold, "Transparent Roughness Threshold", 0.0f);
32
33 SOCKET_FLOAT(volume_step_size, "Volume Step Size", 0.1f);
34
35 SOCKET_NODE(shader, "Shader", Shader::get_node_type());
36
37 SOCKET_STRING(lightgroup, "Light Group", ustring());
38
39 return type;
40}
41
42Background::Background() : Node(get_node_type())
43{
44 shader = NULL;
45}
46
51
52void Background::device_update(Device *device, DeviceScene *dscene, Scene *scene)
53{
54 if (!is_modified()) {
55 return;
56 }
57
58 scoped_callback_timer timer([scene](double time) {
59 if (scene->update_stats) {
60 scene->update_stats->background.times.add_entry({"device_update", time});
61 }
62 });
63
64 device_free(device, dscene);
65
66 Shader *bg_shader = get_shader(scene);
67
68 /* set shader index and transparent option */
69 KernelBackground *kbackground = &dscene->data.background;
70
71 kbackground->transparent = transparent;
72 kbackground->surface_shader = scene->shader_manager->get_shader_id(bg_shader);
73
74 if (transparent && transparent_glass) {
75 /* Square twice, once for principled BSDF convention, and once for
76 * faster comparison in kernel with anisotropic roughness. */
77 kbackground->transparent_roughness_squared_threshold = sqr(
78 sqr(transparent_roughness_threshold));
79 }
80 else {
81 kbackground->transparent_roughness_squared_threshold = -1.0f;
82 }
83
84 if (bg_shader->has_volume) {
85 kbackground->volume_shader = kbackground->surface_shader;
86 }
87 else {
88 kbackground->volume_shader = SHADER_NONE;
89 }
90
91 kbackground->volume_step_size = volume_step_size * scene->integrator->get_volume_step_rate();
92
93 /* No background node, make world shader invisible to all rays, to skip evaluation in kernel. */
94 if (bg_shader->graph->nodes.size() <= 1) {
95 kbackground->surface_shader |= SHADER_EXCLUDE_ANY;
96 }
97 /* Background present, check visibilities */
98 else {
99 if (!(visibility & PATH_RAY_DIFFUSE)) {
100 kbackground->surface_shader |= SHADER_EXCLUDE_DIFFUSE;
101 }
102 if (!(visibility & PATH_RAY_GLOSSY)) {
103 kbackground->surface_shader |= SHADER_EXCLUDE_GLOSSY;
104 }
105 if (!(visibility & PATH_RAY_TRANSMIT)) {
106 kbackground->surface_shader |= SHADER_EXCLUDE_TRANSMIT;
107 }
108 if (!(visibility & PATH_RAY_VOLUME_SCATTER)) {
109 kbackground->surface_shader |= SHADER_EXCLUDE_SCATTER;
110 }
111 if (!(visibility & PATH_RAY_CAMERA)) {
112 kbackground->surface_shader |= SHADER_EXCLUDE_CAMERA;
113 }
114 }
115
116 /* Light group. */
117 auto it = scene->lightgroups.find(lightgroup);
118 if (it != scene->lightgroups.end()) {
119 kbackground->lightgroup = it->second;
120 }
121 else {
122 kbackground->lightgroup = LIGHTGROUP_NONE;
123 }
124
125 clear_modified();
126}
127
128void Background::device_free(Device * /*device*/, DeviceScene * /*dscene*/) {}
129
131{
132 Shader *bg_shader = get_shader(scene);
133 if (bg_shader && bg_shader->is_modified()) {
134 /* Tag as modified to update the KernelBackground visibility information.
135 * We only tag the use_shader socket as modified as it is related to the shader
136 * and to avoid doing unnecessary updates anywhere else. */
137 tag_use_shader_modified();
138 }
139}
140
142{
143 return (use_shader) ? ((shader) ? shader : scene->default_background) : scene->default_empty;
144}
145
NODE_DECLARE Background()
void device_update(Device *device, DeviceScene *dscene, Scene *scene)
void device_free(Device *device, DeviceScene *dscene)
Shader * get_shader(const Scene *scene)
void tag_update(Scene *scene)
list< ShaderNode * > nodes
bool has_volume
NODE_DECLARE ShaderGraph * graph
#define CCL_NAMESPACE_END
#define NULL
#define SHADER_NONE
@ PATH_RAY_TRANSMIT
@ PATH_RAY_VOLUME_SCATTER
@ PATH_RAY_GLOSSY
@ PATH_RAY_ALL_VISIBILITY
@ PATH_RAY_DIFFUSE
@ PATH_RAY_CAMERA
@ SHADER_EXCLUDE_CAMERA
@ SHADER_EXCLUDE_DIFFUSE
@ SHADER_EXCLUDE_ANY
@ SHADER_EXCLUDE_SCATTER
@ SHADER_EXCLUDE_GLOSSY
@ SHADER_EXCLUDE_TRANSMIT
#define LIGHTGROUP_NONE
#define SOCKET_FLOAT(name, ui_name, default_value,...)
Definition node_type.h:200
#define SOCKET_NODE(name, ui_name, node_type,...)
Definition node_type.h:229
#define SOCKET_UINT(name, ui_name, default_value,...)
Definition node_type.h:196
#define NODE_DEFINE(structname)
Definition node_type.h:148
#define SOCKET_BOOLEAN(name, ui_name, default_value,...)
Definition node_type.h:192
#define SOCKET_STRING(name, ui_name, default_value,...)
Definition node_type.h:212
static NodeType * add(const char *name, CreateFunc create, Type type=NONE, const NodeType *base=NULL)
void dereference_all_used_nodes()
bool is_modified() const
ccl_device_inline float sqr(float a)
Definition util/math.h:782
wmTimer * timer