Blender V4.5
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#include "kernel/image.h"
18
20#include "kernel/geom/object.h"
21
23
24#ifdef __VOLUME__
25
26/* Return position normalized to 0..1 in mesh bounds */
27
28ccl_device_inline float3 volume_normalized_position(KernelGlobals kg,
29 const ccl_private ShaderData *sd,
30 float3 P)
31{
32 /* todo: optimize this so it's just a single matrix multiplication when
33 * possible (not motion blur), or perhaps even just translation + scale */
35
37
38 if (desc.offset != ATTR_STD_NOT_FOUND) {
39 const Transform tfm = primitive_attribute_matrix(kg, desc);
40 P = transform_point(&tfm, P);
41 }
42
43 return P;
44}
45
46template<typename T> ccl_device_inline T volume_attribute_value(const float4 value);
47
48ccl_device_template_spec float volume_attribute_value(const float4 value)
49{
50 return average(make_float3(value));
51}
52
53ccl_device_template_spec float2 volume_attribute_value(const float4 value)
54{
55 kernel_assert(!"Float2 attribute not supported for volumes");
56 return zero_float2();
57}
58
59ccl_device_template_spec float3 volume_attribute_value(const float4 value)
60{
61 if (value.w > 1e-6f && value.w != 1.0f) {
62 /* For RGBA colors, unpremultiply after interpolation. */
63 return make_float3(value) / value.w;
64 }
65 return make_float3(value);
66}
67
68ccl_device_template_spec float4 volume_attribute_value(const float4 value)
69{
70 return value;
71}
72
73ccl_device float volume_attribute_alpha(const float4 value)
74{
75 return value.w;
76}
77
78ccl_device float4 volume_attribute_float4(KernelGlobals kg,
79 const ccl_private ShaderData *sd,
80 const AttributeDescriptor desc)
81{
83 return kernel_data_fetch(attributes_float4, desc.offset);
84 }
85 if (desc.element == ATTR_ELEMENT_VOXEL) {
86 /* todo: optimize this so we don't have to transform both here and in
87 * kernel_tex_image_interp_3d when possible. Also could optimize for the
88 * common case where transform is translation/scale only. */
89 float3 P = sd->P;
93 return kernel_tex_image_interp_3d(kg, desc.offset, P, interp);
94 }
95 return zero_float4();
96}
97
98#endif
99
ccl_device float4 kernel_tex_image_interp_3d(KernelGlobals kg, const int id, float3 P, InterpolationType interp)
#define kernel_assert(cond)
#define kernel_data_fetch(name, index)
#define ccl_device
#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)
VecBase< float, 4 > float4
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_device_inline float average(const float2 a)
CCL_NAMESPACE_BEGIN ccl_device_inline float4 zero_float4()
Definition math_float4.h:13
#define T
static bool find_attribute(const std::string &attributes, const char *search_attribute)
AttributeElement element
ccl_device_inline float3 transform_point(const ccl_private Transform *t, const float3 a)
Definition transform.h:56
InterpolationType
@ INTERPOLATION_NONE
@ INTERPOLATION_CUBIC