Blender  V3.3
kernel/geom/attribute.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #pragma once
5 
7 
8 /* Attributes
9  *
10  * We support an arbitrary number of attributes on various mesh elements.
11  * On vertices, triangles, curve keys, curves, meshes and volume grids.
12  * Most of the code for attribute reading is in the primitive files.
13  *
14  * Lookup of attributes is different between OSL and SVM, as OSL is ustring
15  * based while for SVM we use integer ids. */
16 
17 /* Patch index for triangle, -1 if not subdivision triangle */
18 
20 {
21  return (sd->prim != PRIM_NONE) ? kernel_data_fetch(tri_patch, sd->prim) : ~0;
22 }
23 
25 {
26  if ((sd->type & PRIMITIVE_TRIANGLE) && subd_triangle_patch(kg, sd) != ~0) {
27  return ATTR_PRIM_SUBD;
28  }
29  else {
30  return ATTR_PRIM_GEOMETRY;
31  }
32 }
33 
35 {
36  const AttributeDescriptor desc = {
38  return desc;
39 }
40 
41 /* Find attribute based on ID */
42 
44 {
45  return kernel_data_fetch(objects, object).attribute_map_offset;
46 }
47 
49  ccl_private const ShaderData *sd,
50  uint id)
51 {
52  if (sd->object == OBJECT_NONE) {
53  return attribute_not_found();
54  }
55 
56  /* for SVM, find attribute by unique id */
57  uint attr_offset = object_attribute_map_offset(kg, sd->object);
58  attr_offset += attribute_primitive_type(kg, sd);
59  AttributeMap attr_map = kernel_data_fetch(attributes_map, attr_offset);
60 
61  while (attr_map.id != id) {
62  if (UNLIKELY(attr_map.id == ATTR_STD_NONE)) {
63  if (UNLIKELY(attr_map.element == 0)) {
64  return attribute_not_found();
65  }
66  else {
67  /* Chain jump to a different part of the table. */
68  attr_offset = attr_map.offset;
69  }
70  }
71  else {
72  attr_offset += ATTR_PRIM_TYPES;
73  }
74  attr_map = kernel_data_fetch(attributes_map, attr_offset);
75  }
76 
78  desc.element = (AttributeElement)attr_map.element;
79 
80  if (sd->prim == PRIM_NONE && desc.element != ATTR_ELEMENT_MESH &&
82  return attribute_not_found();
83  }
84 
85  /* return result */
86  desc.offset = (attr_map.element == ATTR_ELEMENT_NONE) ? (int)ATTR_STD_NOT_FOUND :
87  (int)attr_map.offset;
88  desc.type = (NodeAttributeType)attr_map.type;
89  desc.flags = (AttributeFlag)attr_map.flags;
90 
91  return desc;
92 }
93 
94 /* Transform matrix attribute on meshes */
95 
97  ccl_private const ShaderData *sd,
98  const AttributeDescriptor desc)
99 {
100  Transform tfm;
101 
102  tfm.x = kernel_data_fetch(attributes_float4, desc.offset + 0);
103  tfm.y = kernel_data_fetch(attributes_float4, desc.offset + 1);
104  tfm.z = kernel_data_fetch(attributes_float4, desc.offset + 2);
105 
106  return tfm;
107 }
108 
unsigned int uint
Definition: BLI_sys_types.h:67
#define UNLIKELY(x)
#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_device_inline uint attribute_primitive_type(KernelGlobals kg, ccl_private const ShaderData *sd)
ccl_device_inline AttributeDescriptor attribute_not_found()
ccl_device_inline AttributeDescriptor find_attribute(KernelGlobals kg, ccl_private const ShaderData *sd, uint id)
ccl_device_inline uint object_attribute_map_offset(KernelGlobals kg, int object)
CCL_NAMESPACE_BEGIN ccl_device_inline uint subd_triangle_patch(KernelGlobals kg, ccl_private const ShaderData *sd)
ccl_device Transform primitive_attribute_matrix(KernelGlobals kg, ccl_private const ShaderData *sd, const AttributeDescriptor desc)
NodeAttributeType
@ PRIMITIVE_TRIANGLE
Definition: kernel/types.h:551
@ ATTR_STD_NOT_FOUND
Definition: kernel/types.h:647
@ ATTR_STD_NONE
Definition: kernel/types.h:613
#define PRIM_NONE
Definition: kernel/types.h:41
AttributeFlag
Definition: kernel/types.h:650
#define OBJECT_NONE
Definition: kernel/types.h:40
ShaderData
Definition: kernel/types.h:925
AttributeElement
Definition: kernel/types.h:597
@ ATTR_ELEMENT_NONE
Definition: kernel/types.h:598
@ ATTR_ELEMENT_VOXEL
Definition: kernel/types.h:609
@ ATTR_ELEMENT_OBJECT
Definition: kernel/types.h:599
@ ATTR_ELEMENT_MESH
Definition: kernel/types.h:600
@ ATTR_PRIM_SUBD
Definition: kernel/types.h:592
@ ATTR_PRIM_TYPES
Definition: kernel/types.h:594
@ ATTR_PRIM_GEOMETRY
Definition: kernel/types.h:591
AttributeElement element
Definition: kernel/types.h:656
NodeAttributeType type
Definition: kernel/types.h:657
uint8_t flags
Definition: kernel/types.h:668
uint8_t type
Definition: kernel/types.h:667