Blender V5.3
raycast.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2025 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "kernel/globals.h"
8
10
11#include "kernel/bvh/bvh.h"
12
14
16#include "kernel/svm/util.h"
17
19
21
22#ifdef __SHADER_RAYTRACE__
23
24ccl_device bool svm_raycast(KernelGlobals kg,
25 ConstIntegratorState /*state*/,
26 ccl_private ShaderData *sd,
27 const float3 position,
28 const float3 direction,
29 const float distance,
30 const bool only_local,
31 const float bump_filter_width,
32 ccl_private ShaderData &hit_sd)
33{
34 /* Early out if no sampling needed. */
36 return false;
37 }
38
39 /* Can't ray-trace from shaders like displacement, before BVH exists. */
40 if (kernel_data.bvh.bvh_layout == BVH_LAYOUT_NONE) {
41 return false;
42 }
43
44 float tmin = 0.0f;
45 bool avoid_self_intersection = false;
46 if (bump_filter_width > 0.0f) {
47 /* If evaluating for bump mapping at a shifted position, increase min distance by slightly more
48 * than the shift distance to avoid self intersections. */
49 tmin = bump_filter_width * sd->dP * 1.1f;
50 }
51 else {
52 avoid_self_intersection = isequal(position, sd->P);
53 }
54
55 /* Create ray. */
56 Ray ray;
57 ray.P = position;
58 ray.D = direction;
59 ray.tmin = tmin;
60 ray.tmax = distance;
61 ray.time = sd->time;
62 ray.self.object = avoid_self_intersection ? sd->object : OBJECT_NONE;
63 ray.self.prim = avoid_self_intersection ? sd->prim : PRIM_NONE;
68
69 Intersection isect;
70 if (only_local) {
71 LocalIntersection local_isect;
72 scene_intersect_local(kg, &ray, &local_isect, sd->object, nullptr, 1);
73 if (local_isect.num_hits == 0) {
74 return false;
75 }
76 isect = local_isect.hits[0];
77 }
78 else {
79 if (!scene_intersect(kg, &ray, PATH_RAY_VISIBILITY_RAYCAST, &isect)) {
80 return false;
81 }
82 }
83
84 shader_setup_from_ray(kg, &hit_sd, &ray, &isect);
85
86 return true;
87}
88
89ccl_device_inline void svm_raycast_attr_eval_and_store(
91 ccl_private float *stack,
92 ccl_global const SVMNodeAttr &attribute_node,
93 ccl_private ShaderData &hit_sd)
94{
96 const AttributeDescriptor desc = svm_node_attr_init(kg, &hit_sd, attribute_node, &type);
97
99 kg, &hit_sd, stack, attribute_node, type, desc);
100 svm_node_attr_store(type, stack, attribute_node.out_offset, data);
101}
102
103template<uint64_t node_feature_mask, typename ConstIntegratorGenericState>
104# if defined(__KERNEL_OPTIX__)
106# else
108# endif
109 int svm_node_raycast(KernelGlobals kg,
110 ConstIntegratorGenericState state,
111 ccl_private ShaderData *sd,
112 ccl_private float *ccl_restrict stack,
114 int offset)
115{
116 const float distance = stack_load(stack, node.distance);
117
118 float is_hit = 0.0f;
119 float is_self_hit = 0.0f;
120 float hit_distance = distance;
121 float3 hit_position = make_float3(0.0f);
122 float3 hit_normal = make_float3(0.0f);
123
124 IF_KERNEL_NODES_FEATURE (RAYTRACE) {
125 const float3 position = stack_load(stack, node.position);
126 const float3 direction = stack_load(stack, node.direction);
127
128 ShaderDataTinyStorage hit_sd_storage;
129 ccl_private ShaderData &hit_sd = *AS_SHADER_DATA(&hit_sd_storage);
130
131 if (svm_raycast(kg,
132 state,
133 sd,
134 position,
135 direction,
136 distance,
137 node.only_local,
138 node.bump_filter_width,
139 hit_sd))
140 {
141 is_hit = 1.0f;
142 is_self_hit = (sd->object == hit_sd.object) ? 1.0f : 0.0f;
143 hit_distance = hit_sd.ray_length;
144 hit_position = position + direction * hit_distance;
145 hit_normal = hit_sd.N;
146
147 for (uint16_t i = 0; i < node.num_attributes; i++) {
148 const uint node_type = kernel_data_fetch(svm_nodes, offset++);
149 (void)node_type;
150 kernel_assert(node_type == NODE_ATTR);
151
152 const ccl_global auto &attribute_node = svm_node_get<SVMNodeAttr>(kg, &offset);
153 svm_raycast_attr_eval_and_store(kg, stack, attribute_node, hit_sd);
154 }
155 }
156 }
157
158 if (is_hit == 0.0f) {
159 for (uint16_t i = 0; i < node.num_attributes; i++) {
160 const uint node_type = kernel_data_fetch(svm_nodes, offset++);
161 (void)node_type;
162 kernel_assert(node_type == NODE_ATTR);
163
164 const ccl_global auto &attribute_node = svm_node_get<SVMNodeAttr>(kg, &offset);
166 attribute_node.output_type, stack, attribute_node.out_offset, make_zero<float3>());
167 }
168 }
169
170 if (stack_valid(node.is_hit_offset)) {
171 stack_store_float(stack, node.is_hit_offset, is_hit);
172 }
173 if (stack_valid(node.is_self_hit_offset)) {
174 stack_store_float(stack, node.is_self_hit_offset, is_self_hit);
175 }
176 if (stack_valid(node.hit_distance_offset)) {
177 stack_store_float(stack, node.hit_distance_offset, hit_distance);
178 }
179 if (stack_valid(node.hit_position_offset)) {
180 stack_store_float3(stack, node.hit_position_offset, hit_position);
181 }
182 if (stack_valid(node.hit_normal_offset)) {
183 stack_store_float3(stack, node.hit_normal_offset, hit_normal);
184 }
185
186 return offset;
187}
188
189#endif /* __SHADER_RAYTRACE__ */
190
ccl_device_inline void stack_store_float(ccl_private float *stack, const uint a, const float f)
ccl_device_inline void stack_store_float3(ccl_private float *stack, const uint a, const float3 f)
ccl_device_inline const ccl_global T & svm_node_get(KernelGlobals kg, ccl_private int *const offset)
ccl_device_inline bool stack_valid(const uint a)
ccl_device_inline T stack_load(const ccl_private float *stack, const uint a)
#define kernel_assert(cond)
#define kernel_data
#define ccl_restrict
#define IF_KERNEL_NODES_FEATURE(feature)
#define kernel_data_fetch(name, index)
#define AS_SHADER_DATA(shader_data_tiny_storage)
#define PRIM_NONE
#define OBJECT_NONE
#define ccl_private
#define ccl_device_inline
#define ccl_global
#define ccl_device_noinline
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
ccl_device_forceinline float differential_zero_compact()
#define position
float distance(float, float) RET
ccl_device_intersect bool scene_intersect(KernelGlobals kg, const ccl_private Ray *ray, const uint visibility, ccl_private Intersection *isect)
const int state
CCL_NAMESPACE_BEGIN ccl_device AttributeDescriptor svm_node_attr_init(KernelGlobals kg, ccl_private ShaderData *sd, const ccl_global SVMNodeAttr &ccl_restrict node, ccl_private NodeAttributeOutputType *type)
ccl_device_inline Float3Type svm_node_attr_surface_eval(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *ccl_restrict stack, const ccl_global SVMNodeAttr &ccl_restrict node, const NodeAttributeOutputType type, const AttributeDescriptor desc)
ccl_device_inline void svm_node_attr_store(const NodeAttributeOutputType type, ccl_private float *stack, const uint out_offset, const ccl_private Float3Type &f)
NodeAttributeOutputType
@ NODE_ATTR_OUTPUT_FLOAT
ShaderData ShaderDataTinyStorage
@ PATH_RAY_VISIBILITY_RAYCAST
@ BVH_LAYOUT_NONE
ccl_device_inline T make_zero()
Definition math_dual.h:20
ccl_device_inline bool isequal(const float2 a, const float2 b)
unsigned int uint
Definition BKE_text.h:55
BMesh const char void * data
#define ccl_device
ccl_device_inline void shader_setup_from_ray(KernelGlobals kg, ccl_private ShaderData *ccl_restrict sd, const ccl_private Ray *ccl_restrict ray, const ccl_private Intersection *ccl_restrict isect)
Definition shader_data.h:48
const IntegratorStateCPU * ConstIntegratorState
Definition state.h:228
Intersection hits[LOCAL_MAX_HITS]
float tmax
float tmin
float3 P
float time
RaySelfPrimitives self
float3 D