Blender  V3.3
kernel/geom/volume.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 /* Volume Primitive
5  *
6  * Volumes are just regions inside meshes with the mesh surface as boundaries.
7  * There isn't as much data to access as for surfaces, there is only a position
8  * to do lookups in 3D voxel or procedural textures.
9  *
10  * 3D voxel textures can be assigned as attributes per mesh, which means the
11  * same shader can be used for volume objects with different densities, etc. */
12 
13 #pragma once
14 
16 
17 #ifdef __VOLUME__
18 
19 /* Return position normalized to 0..1 in mesh bounds */
20 
21 ccl_device_inline float3 volume_normalized_position(KernelGlobals kg,
22  ccl_private const ShaderData *sd,
23  float3 P)
24 {
25  /* todo: optimize this so it's just a single matrix multiplication when
26  * possible (not motion blur), or perhaps even just translation + scale */
28 
30 
31  if (desc.offset != ATTR_STD_NOT_FOUND) {
32  Transform tfm = primitive_attribute_matrix(kg, sd, desc);
33  P = transform_point(&tfm, P);
34  }
35 
36  return P;
37 }
38 
39 ccl_device float volume_attribute_value_to_float(const float4 value)
40 {
41  return average(float4_to_float3(value));
42 }
43 
44 ccl_device float volume_attribute_value_to_alpha(const float4 value)
45 {
46  return value.w;
47 }
48 
49 ccl_device float3 volume_attribute_value_to_float3(const float4 value)
50 {
51  if (value.w > 1e-6f && value.w != 1.0f) {
52  /* For RGBA colors, unpremultiply after interpolation. */
53  return float4_to_float3(value) / value.w;
54  }
55  else {
56  return float4_to_float3(value);
57  }
58 }
59 
60 ccl_device float4 volume_attribute_float4(KernelGlobals kg,
61  ccl_private const ShaderData *sd,
62  const AttributeDescriptor desc)
63 {
65  return kernel_data_fetch(attributes_float4, desc.offset);
66  }
67  else if (desc.element == ATTR_ELEMENT_VOXEL) {
68  /* todo: optimize this so we don't have to transform both here and in
69  * kernel_tex_image_interp_3d when possible. Also could optimize for the
70  * common case where transform is translation/scale only. */
71  float3 P = sd->P;
75  return kernel_tex_image_interp_3d(kg, desc.offset, P, interp);
76  }
77  else {
78  return zero_float4();
79  }
80 }
81 
82 #endif
83 
float float4[4]
#define ccl_device
Definition: cuda/compat.h:32
#define ccl_private
Definition: cuda/compat.h:48
#define ccl_device_inline
Definition: cuda/compat.h:34
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
const KernelGlobalsCPU *ccl_restrict KernelGlobals
#define kernel_data_fetch(name, index)
CCL_NAMESPACE_END CCL_NAMESPACE_BEGIN ccl_device_inline float3 transform_point(ccl_private const Transform *t, const float3 a)
ccl_device_inline AttributeDescriptor find_attribute(KernelGlobals kg, ccl_private const ShaderData *sd, uint id)
ccl_device Transform primitive_attribute_matrix(KernelGlobals kg, ccl_private const ShaderData *sd, const AttributeDescriptor desc)
ccl_device_inline void object_inverse_position_transform(KernelGlobals kg, ccl_private const ShaderData *sd, ccl_private float3 *P)
@ SD_VOLUME_CUBIC
Definition: kernel/types.h:780
@ ATTR_STD_GENERATED_TRANSFORM
Definition: kernel/types.h:621
@ ATTR_STD_NOT_FOUND
Definition: kernel/types.h:647
ShaderData
Definition: kernel/types.h:925
@ ATTR_ELEMENT_VOXEL
Definition: kernel/types.h:609
@ ATTR_ELEMENT_OBJECT
Definition: kernel/types.h:599
@ ATTR_ELEMENT_MESH
Definition: kernel/types.h:600
ccl_device_inline float2 interp(const float2 &a, const float2 &b, float t)
Definition: math_float2.h:232
ccl_device_inline float average(const float2 &a)
Definition: math_float2.h:170
ccl_device_inline float4 zero_float4()
Definition: math_float4.h:92
static float P(float k)
Definition: math_interp.c:25
ccl_device float4 kernel_tex_image_interp_3d(KernelGlobals kg, int id, float3 P, InterpolationType interp)
AttributeElement element
Definition: kernel/types.h:656
ccl_device_inline float3 float4_to_float3(const float4 a)
Definition: util/math.h:500
InterpolationType
Definition: util/texture.h:19
@ INTERPOLATION_NONE
Definition: util/texture.h:20
@ INTERPOLATION_CUBIC
Definition: util/texture.h:23