Blender V5.3
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
22
24
25#ifdef __VOLUME__
26
27/* Return position normalized to 0..1 in mesh bounds */
28
29template<typename Float3Type>
30ccl_device_inline Float3Type volume_normalized_position(KernelGlobals kg,
31 const ccl_private ShaderData *sd,
32 Float3Type P)
33{
35
37
38 if (is_attribute_found(desc)) {
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 return make_float2(value.x, value.y);
56}
57
58ccl_device_template_spec float3 volume_attribute_value(const float4 value)
59{
60 return make_float3(value);
61}
62
63ccl_device_template_spec float4 volume_attribute_value(const float4 value)
64{
65 return value;
66}
67
68ccl_device_template_spec Quaternion volume_attribute_value(const float4 value)
69{
70 /* Attribute sampling code uses float4 as an intermediate format, and quaternion in stored as
71 * make_float4(q.w, q.x, q.y, q.z). */
72 return make_quaternion(value.x, value.y, value.z, value.w);
73}
74
75ccl_device float volume_attribute_alpha(const float4 value)
76{
77 return value.w;
78}
79
80ccl_device float4 volume_attribute_float4(KernelGlobals kg,
81 ccl_private ShaderData *sd,
82 const AttributeDescriptor desc,
83 const bool stochastic)
84{
86 switch (desc.type) {
87 case NODE_ATTR_FLOAT: {
88 const float f = kernel_data_fetch(attributes_float, desc.offset);
89 return make_float4(f, f, f, 1.0f);
90 }
91 case NODE_ATTR_FLOAT2: {
92 const float2 f = kernel_data_fetch(attributes_float2, desc.offset);
93 return make_float4(f.x, f.y, 0.0f, 1.0f);
94 }
95 case NODE_ATTR_FLOAT3: {
96 const float3 f = kernel_data_fetch(attributes_float3, desc.offset);
97 return make_float4(f.x, f.y, f.z, 1.0f);
98 }
100 case NODE_ATTR_RGBA:
101 return kernel_data_fetch(attributes_float4, desc.offset);
103 return make_float4(kernel_data_fetch(attributes_quaternion, desc.offset));
105 return make_float4(0.0f, 0.0f, 0.0f, 1.0f);
106 case NODE_ATTR_MATRIX:
107 return zero_float4();
108 }
109 }
110 if (desc.element & ATTR_ELEMENT_VOXEL) {
111 /* todo: optimize this so we don't have to transform both here and in
112 * kernel_image_interp_3d when possible. Also could optimize for the
113 * common case where transform is translation/scale only. */
114 float3 P = sd->P;
116 const InterpolationType interp = (sd->shader_flag & SD_VOLUME_CUBIC) ? INTERPOLATION_CUBIC :
118 const float4 value = kernel_image_interp_3d(kg, sd, desc.offset, P, interp, stochastic);
119 if (value.w > 1e-6f && value.w != 1.0f) {
120 /* For RGBA colors, unpremultiply after interpolation. */
121 return make_float4(make_float3(value) / value.w, value.w);
122 }
123 return value;
124 }
125 return zero_float4();
126}
127
128#endif
129
#define kernel_data_fetch(name, index)
#define ccl_private
#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)
float3 transform_point(float3x3 mat, float3 point)
float average(float2 a)
CCL_NAMESPACE_BEGIN ccl_device_forceinline bool is_attribute_found(const ccl_private AttributeDescriptor &desc)
ccl_device Transform primitive_attribute_matrix(KernelGlobals kg, const AttributeDescriptor desc)
ccl_device_inline AttributeDescriptor find_attribute(const ccl_global AttributeMap *attributes_map, uint attr_offset, const int prim, const uint64_t id)
ccl_device_inline void object_inverse_position_transform_if_object(KernelGlobals kg, const ccl_private ShaderData *sd, ccl_private Float3Type *P)
ccl_device_inline void object_inverse_position_transform(KernelGlobals kg, const ccl_private ShaderData *sd, ccl_private T *P)
@ NODE_ATTR_FLOAT
@ NODE_ATTR_QUATERNION
@ NODE_ATTR_FLOAT3
@ NODE_ATTR_RGBA
@ NODE_ATTR_FLOAT2
@ NODE_ATTR_SPHERICAL_HARMONICS_REST
@ NODE_ATTR_FLOAT4
@ NODE_ATTR_MATRIX
@ SD_VOLUME_CUBIC
@ ATTR_ELEMENT_VOXEL
@ ATTR_ELEMENT_OBJECT
@ ATTR_ELEMENT_MESH
@ ATTR_STD_GENERATED_TRANSFORM
ccl_device_inline float interp(const float a, const float b, const float t)
Definition math_base.h:549
CCL_NAMESPACE_BEGIN ccl_device_inline float4 zero_float4()
Definition math_float4.h:13
#define T
ccl_device float4 kernel_image_interp_3d(KernelGlobals kg, ccl_private ShaderData *sd, const int image_texture_id, float3 P, InterpolationType interp, const bool stochastic)
Definition image_3d.h:203
#define ccl_device
#define make_float2
#define make_float4
AttributeElement element
NodeAttributeType type
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
InterpolationType
Definition types_image.h:24
@ INTERPOLATION_NONE
Definition types_image.h:25
@ INTERPOLATION_CUBIC
Definition types_image.h:28
ccl_device_inline Quaternion make_quaternion(const float w, const float x, const float y, const float z)