Blender V5.0
kernel/geom/volume.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5/* Volume Primitive
6 *
7 * Volumes are just regions inside meshes with the mesh surface as boundaries.
8 * There isn't as much data to access as for surfaces, there is only a position
9 * to do lookups in 3D voxel or procedural textures.
10 *
11 * 3D voxel textures can be assigned as attributes per mesh, which means the
12 * same shader can be used for volume objects with different densities, etc. */
13
14#pragma once
15
16#include "kernel/globals.h"
17
19#include "kernel/geom/object.h"
20
21#include "kernel/sample/lcg.h"
22
24
26
27#ifdef __VOLUME__
28
29/* Return position normalized to 0..1 in mesh bounds */
30
31ccl_device_inline float3 volume_normalized_position(KernelGlobals kg,
32 const ccl_private ShaderData *sd,
33 float3 P)
34{
35 /* todo: optimize this so it's just a single matrix multiplication when
36 * possible (not motion blur), or perhaps even just translation + scale */
38
40
41 if (desc.offset != ATTR_STD_NOT_FOUND) {
42 const Transform tfm = primitive_attribute_matrix(kg, desc);
43 P = transform_point(&tfm, P);
44 }
45
46 return P;
47}
48
49template<typename T> ccl_device_inline T volume_attribute_value(const float4 value);
50
51ccl_device_template_spec float volume_attribute_value(const float4 value)
52{
53 return average(make_float3(value));
54}
55
56ccl_device_template_spec float2 volume_attribute_value(const float4 /*value*/)
57{
58 kernel_assert(!"Float2 attribute not supported for volumes");
59 return zero_float2();
60}
61
62ccl_device_template_spec float3 volume_attribute_value(const float4 value)
63{
64 if (value.w > 1e-6f && value.w != 1.0f) {
65 /* For RGBA colors, unpremultiply after interpolation. */
66 return make_float3(value) / value.w;
67 }
68 return make_float3(value);
69}
70
71ccl_device_template_spec float4 volume_attribute_value(const float4 value)
72{
73 return value;
74}
75
76ccl_device float volume_attribute_alpha(const float4 value)
77{
78 return value.w;
79}
80
81ccl_device float4 volume_attribute_float4(KernelGlobals kg,
82 ccl_private ShaderData *sd,
83 const AttributeDescriptor desc,
84 const bool stochastic)
85{
87 return kernel_data_fetch(attributes_float4, desc.offset);
88 }
89 if (desc.element == ATTR_ELEMENT_VOXEL) {
90 /* todo: optimize this so we don't have to transform both here and in
91 * kernel_tex_image_interp_3d when possible. Also could optimize for the
92 * common case where transform is translation/scale only. */
93 float3 P = sd->P;
97 return kernel_tex_image_interp_3d(kg, sd, desc.offset, P, interp, stochastic);
98 }
99 return zero_float4();
100}
101
102#endif
103
#define kernel_assert(cond)
#define kernel_data_fetch(name, index)
#define ccl_private
const ThreadKernelGlobalsCPU * KernelGlobals
#define ccl_device_inline
#define ccl_device_template_spec
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
ccl_device Transform primitive_attribute_matrix(KernelGlobals kg, const AttributeDescriptor desc)
ccl_device_inline void object_inverse_position_transform(KernelGlobals kg, const ccl_private ShaderData *sd, ccl_private float3 *P)
@ SD_VOLUME_CUBIC
@ ATTR_STD_GENERATED_TRANSFORM
@ ATTR_STD_NOT_FOUND
@ ATTR_ELEMENT_VOXEL
@ ATTR_ELEMENT_OBJECT
@ ATTR_ELEMENT_MESH
ccl_device_inline float interp(const float a, const float b, const float t)
Definition math_base.h:502
CCL_NAMESPACE_BEGIN ccl_device_inline float2 zero_float2()
Definition math_float2.h:13
CCL_NAMESPACE_BEGIN ccl_device_inline float4 zero_float4()
Definition math_float4.h:13
#define T
ccl_device float4 kernel_tex_image_interp_3d(KernelGlobals kg, ccl_private ShaderData *sd, const int id, float3 P, InterpolationType interp, const bool stochastic)
Definition texture_3d.h:181
float average(point a)
Definition node_math.h:144
#define ccl_device
static bool find_attribute(const std::string &attributes, const char *search_attribute)
AttributeElement element
float w
Definition sky_math.h:225
InterpolationType
Definition texture.h:22
@ INTERPOLATION_NONE
Definition texture.h:23
@ INTERPOLATION_CUBIC
Definition texture.h:26
ccl_device_inline float3 transform_point(const ccl_private Transform *t, const float3 a)
Definition transform.h:56