Blender V5.3
ray_tracing_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "testing/testing.h"
6
7#include "MEM_guardedalloc.h"
8
9#include "GPU_batch.hh"
10#include "GPU_capabilities.hh"
11#include "GPU_compute.hh"
12#include "GPU_context.hh"
13#include "GPU_framebuffer.hh"
14#include "GPU_index_buffer.hh"
15#include "GPU_ray_tracing.hh"
16#include "GPU_shader.hh"
17#include "GPU_state.hh"
18#include "GPU_texture.hh"
19#include "GPU_vertex_buffer.hh"
20
21#include "gpu_testing.hh"
22
23#include "BLI_math_matrix.hh"
24#include "BLI_math_vector.hh"
25namespace blender::gpu::tests {
26
27#define SUPPORTS_RAY_QUERY() \
28 if (!GPU_ray_query_support()) { \
29 GTEST_SKIP() << "Platform doesn't support ray tracing"; \
30 }
31
33{
34 /* Build a cube (vertex + index buffer). */
35 static const GPUVertFormat pos_format = GPU_vertformat_from_attribute(
36 "pos", gpu::VertAttrType::SFLOAT_32_32_32);
37 VertBufPtr vertex_buf(GPU_vertbuf_create_with_format(pos_format));
38 GPU_vertbuf_data_alloc(*vertex_buf, 8);
39 MutableSpan<float3> vertex_buf_data = vertex_buf->data<float3>();
40 vertex_buf_data[0] = float3(-1.0, -1.0, -1.0);
41 vertex_buf_data[1] = float3(1.0, -1.0, -1.0);
42 vertex_buf_data[2] = float3(-1.0, 1.0, -1.0);
43 vertex_buf_data[3] = float3(1.0, 1.0, -1.0);
44 vertex_buf_data[4] = float3(-1.0, -1.0, 1.0);
45 vertex_buf_data[5] = float3(1.0, -1.0, 1.0);
46 vertex_buf_data[6] = float3(-1.0, 1.0, 1.0);
47 vertex_buf_data[7] = float3(1.0, 1.0, 1.0);
48 GPU_vertbuf_use(vertex_buf.get());
49 return vertex_buf;
50}
51
53{
55 Vector<uint32_t> indices_src({
56 0, 1, 2, 2, 1, 3, 4, 6, 5, 5, 6, 7, 0, 2, 4, 4, 2, 6,
57 1, 5, 3, 3, 5, 7, 2, 3, 6, 6, 3, 7, 0, 4, 1, 1, 4, 5,
58 });
59 uint32_t *indices = MEM_new_array<uint32_t>(indices_src.size(), __func__);
60 memcpy(indices, indices_src.data(), indices_src.size() * sizeof(uint32_t));
61 index_buf->init(indices_src.size(), indices, 0, 7, GPU_PRIM_TRIS, false);
62 GPU_indexbuf_use(index_buf.get());
63 return index_buf;
64}
65
71
72static void hit_test(TopLevelAS &tlas, Span<Ray> rays, uint intersection_mask = 0xFF)
73{
74 const int ray_count = rays.size();
75
76 Array<float4> ray_pos(ray_count);
77 Array<float4> ray_dir(ray_count);
78 Array<uint32_t> expected(ray_count);
80 for (int index : rays.index_range()) {
81 const Ray &ray = rays[index];
82 ray_pos[index] = float4(ray.pos, 0.0f);
83 ray_dir[index] = float4(ray.dir, 0.0f);
84 expected[index] = ray.expected_hit;
85 }
86
87 gpu::StorageBuf *ray_pos_buf = GPU_storagebuf_create(ray_count * sizeof(float4));
88 gpu::StorageBuf *ray_dir_buf = GPU_storagebuf_create(ray_count * sizeof(float4));
89 gpu::StorageBuf *hit_buf = GPU_storagebuf_create(ray_count * sizeof(uint32_t));
90 GPU_storagebuf_update(ray_pos_buf, ray_pos.data());
91 GPU_storagebuf_update(ray_dir_buf, ray_dir.data());
93
95 EXPECT_NE(shader, nullptr);
96 int intersection_mask_in = GPU_shader_get_constant(shader, "intersection_mask_in");
98 constants.set_value(intersection_mask_in, intersection_mask);
99 GPU_shader_bind(shader, &constants);
100 GPU_storagebuf_bind(ray_pos_buf, 0);
101 GPU_storagebuf_bind(ray_dir_buf, 1);
102 GPU_storagebuf_bind(hit_buf, 2);
103 tlas.bind(0);
104
105 GPU_compute_dispatch(shader, ray_count, 1, 1, &constants);
107
108 GPU_storagebuf_read(hit_buf, result.data());
109
110 GPU_storagebuf_free(ray_pos_buf);
111 GPU_storagebuf_free(ray_dir_buf);
112 GPU_storagebuf_free(hit_buf);
114
115 EXPECT_EQ_SPAN(expected.as_span(), result.as_span());
116}
117
119{
121
123 tlas->build();
124
125 Vector<Ray> rays;
126 /* All rays from inside the cube should hit the cube. */
127 for (int x : IndexRange(19)) {
128 for (int y : IndexRange(19)) {
129 for (int z : IndexRange(19)) {
130 float3 pos((x - 9) * 0.1f, (y - 9) * 0.1f, (z - 9) * 0.1f);
131 rays.append({pos, float3(-1.0f, 0.0f, 0.0f), false});
132 rays.append({pos, float3(1.0f, 0.0f, 0.0f), false});
133 rays.append({pos, float3(0.0f, -1.0f, 0.0f), false});
134 rays.append({pos, float3(0.0f, 1.0f, 0.0f), false});
135 rays.append({pos, float3(0.0f, 0.0f, -1.0f), false});
136 rays.append({pos, float3(0.0f, 0.0f, 1.0f), false});
137 }
138 }
139 }
140
141 hit_test(*tlas, rays);
142}
143GPU_TEST(ray_tracing_empty_tlas)
144
146{
148
149 VertBufPtr vertex_buf = build_vertices();
150 IndexBufPtr index_buf = build_indices();
151
153 blas->add_geometry(*index_buf, *vertex_buf);
154 blas->build();
155
157 float4x4 identity = float4x4::identity();
158 tlas->add_instance(*blas, identity);
159 tlas->build();
160
161 Vector<Ray> rays;
162 /* All rays from inside the cube should hit the cube. */
163 for (int x : IndexRange(19)) {
164 for (int y : IndexRange(19)) {
165 for (int z : IndexRange(19)) {
166 float3 pos((x - 9) * 0.1f, (y - 9) * 0.1f, (z - 9) * 0.1f);
167 rays.append({pos, float3(-1.0f, 0.0f, 0.0f), true});
168 rays.append({pos, float3(1.0f, 0.0f, 0.0f), true});
169 rays.append({pos, float3(0.0f, -1.0f, 0.0f), true});
170 rays.append({pos, float3(0.0f, 1.0f, 0.0f), true});
171 rays.append({pos, float3(0.0f, 0.0f, -1.0f), true});
172 rays.append({pos, float3(0.0f, 0.0f, 1.0f), true});
173 }
174 }
175 }
176
177 hit_test(*tlas, rays);
178}
179GPU_TEST(ray_tracing_inside_cube)
180
182{
184
185 VertBufPtr vertex_buf = build_vertices();
186 IndexBufPtr index_buf = build_indices();
187
189 blas->add_geometry(*index_buf, *vertex_buf);
190 blas->build();
191
193 float4x4 mat_x_neg = math::from_location<float4x4>(float3(-2.0f, 0.0f, 0.0f));
194 float4x4 mat_x_pos = math::from_location<float4x4>(float3(2.0f, 0.0f, 0.0f));
195 float4x4 mat_y_neg = math::from_location<float4x4>(float3(0.0f, -2.0f, 0.0f));
196 float4x4 mat_y_pos = math::from_location<float4x4>(float3(0.0f, 2.0f, 0.0f));
197 float4x4 mat_z_neg = math::from_location<float4x4>(float3(0.0f, 0.0f, -2.0f));
198 float4x4 mat_z_pos = math::from_location<float4x4>(float3(0.0f, 0.0f, 2.0f));
199 tlas->add_instance(*blas, mat_x_neg, 0x01);
200 tlas->add_instance(*blas, mat_x_pos, 0x02);
201 tlas->add_instance(*blas, mat_y_neg, 0x04);
202 tlas->add_instance(*blas, mat_y_pos, 0x08);
203 tlas->add_instance(*blas, mat_z_neg, 0x10);
204 tlas->add_instance(*blas, mat_z_pos, 0x20);
205 tlas->build();
206
207 Vector<Ray> rays;
208 rays.append({float3(0.0f, 0.0f, 0.0f), float3(-1.0f, 0.0f, 0.0f), true});
209 rays.append({float3(0.0f, 0.0f, 0.0f), float3(1.0f, 0.0f, 0.0f), true});
210 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, -1.0f, 0.0f), true});
211 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), true});
212 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, -1.0f), true});
213 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, 1.0f), true});
214 hit_test(*tlas, rays, 0xFF);
215
216 rays.clear();
217 rays.append({float3(0.0f, 0.0f, 0.0f), float3(-1.0f, 0.0f, 0.0f), true});
218 rays.append({float3(0.0f, 0.0f, 0.0f), float3(1.0f, 0.0f, 0.0f), false});
219 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, -1.0f, 0.0f), false});
220 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), false});
221 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, -1.0f), false});
222 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, 1.0f), false});
223 hit_test(*tlas, rays, 0x01);
224
225 rays.clear();
226 rays.append({float3(0.0f, 0.0f, 0.0f), float3(-1.0f, 0.0f, 0.0f), false});
227 rays.append({float3(0.0f, 0.0f, 0.0f), float3(1.0f, 0.0f, 0.0f), true});
228 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, -1.0f, 0.0f), false});
229 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), false});
230 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, -1.0f), false});
231 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, 1.0f), false});
232 hit_test(*tlas, rays, 0x02);
233
234 rays.clear();
235 rays.append({float3(0.0f, 0.0f, 0.0f), float3(-1.0f, 0.0f, 0.0f), false});
236 rays.append({float3(0.0f, 0.0f, 0.0f), float3(1.0f, 0.0f, 0.0f), false});
237 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, -1.0f, 0.0f), true});
238 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), false});
239 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, -1.0f), false});
240 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, 1.0f), false});
241 hit_test(*tlas, rays, 0x04);
242
243 rays.clear();
244 rays.append({float3(0.0f, 0.0f, 0.0f), float3(-1.0f, 0.0f, 0.0f), false});
245 rays.append({float3(0.0f, 0.0f, 0.0f), float3(1.0f, 0.0f, 0.0f), false});
246 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, -1.0f, 0.0f), false});
247 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), true});
248 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, -1.0f), false});
249 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, 1.0f), false});
250 hit_test(*tlas, rays, 0x08);
251
252 rays.clear();
253 rays.append({float3(0.0f, 0.0f, 0.0f), float3(-1.0f, 0.0f, 0.0f), false});
254 rays.append({float3(0.0f, 0.0f, 0.0f), float3(1.0f, 0.0f, 0.0f), false});
255 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, -1.0f, 0.0f), false});
256 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), false});
257 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, -1.0f), true});
258 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, 1.0f), false});
259 hit_test(*tlas, rays, 0x10);
260
261 rays.clear();
262 rays.append({float3(0.0f, 0.0f, 0.0f), float3(-1.0f, 0.0f, 0.0f), false});
263 rays.append({float3(0.0f, 0.0f, 0.0f), float3(1.0f, 0.0f, 0.0f), false});
264 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, -1.0f, 0.0f), false});
265 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), false});
266 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, -1.0f), false});
267 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, 1.0f), true});
268 hit_test(*tlas, rays, 0x20);
269
270 rays.clear();
271 rays.append({float3(0.0f, 0.0f, 0.0f), float3(-1.0f, 0.0f, 0.0f), false});
272 rays.append({float3(0.0f, 0.0f, 0.0f), float3(1.0f, 0.0f, 0.0f), false});
273 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, -1.0f, 0.0f), false});
274 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), false});
275 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, -1.0f), false});
276 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, 1.0f), false});
277 hit_test(*tlas, rays, 0x40);
278}
279GPU_TEST(ray_tracing_instance_mask)
280
282{
284
285 VertBufPtr vertex_buf = build_vertices();
286 IndexBufPtr index_buf = build_indices();
287
289 blas->add_geometry(*index_buf, *vertex_buf);
290 blas->build();
291
293 float4x4 mat_x_neg = math::from_location<float4x4>(float3(-2.0f, 0.0f, 0.0f));
294 float4x4 mat_x_pos = math::from_location<float4x4>(float3(2.0f, 0.0f, 0.0f));
295 float4x4 mat_y_neg = math::from_location<float4x4>(float3(0.0f, -2.0f, 0.0f));
296 float4x4 mat_y_pos = math::from_location<float4x4>(float3(0.0f, 2.0f, 0.0f));
297 float4x4 mat_z_neg = math::from_location<float4x4>(float3(0.0f, 0.0f, -2.0f));
298 float4x4 mat_z_pos = math::from_location<float4x4>(float3(0.0f, 0.0f, 2.0f));
299 InstanceID instance_id = tlas->add_instance(*blas, mat_x_neg);
300 tlas->build();
301
302 Vector<Ray> rays;
303 rays.append({float3(0.0f, 0.0f, 0.0f), float3(-1.0f, 0.0f, 0.0f), true});
304 rays.append({float3(0.0f, 0.0f, 0.0f), float3(1.0f, 0.0f, 0.0f), false});
305 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, -1.0f, 0.0f), false});
306 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), false});
307 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, -1.0f), false});
308 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, 1.0f), false});
309 hit_test(*tlas, rays);
310
311 tlas->update_instance(instance_id, mat_x_pos);
312 tlas->build();
313 rays.clear();
314 rays.append({float3(0.0f, 0.0f, 0.0f), float3(-1.0f, 0.0f, 0.0f), false});
315 rays.append({float3(0.0f, 0.0f, 0.0f), float3(1.0f, 0.0f, 0.0f), true});
316 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, -1.0f, 0.0f), false});
317 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), false});
318 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, -1.0f), false});
319 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, 1.0f), false});
320 hit_test(*tlas, rays);
321
322 tlas->update_instance(instance_id, mat_y_neg);
323 tlas->build();
324 rays.clear();
325 rays.append({float3(0.0f, 0.0f, 0.0f), float3(-1.0f, 0.0f, 0.0f), false});
326 rays.append({float3(0.0f, 0.0f, 0.0f), float3(1.0f, 0.0f, 0.0f), false});
327 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, -1.0f, 0.0f), true});
328 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), false});
329 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, -1.0f), false});
330 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, 1.0f), false});
331 hit_test(*tlas, rays);
332
333 tlas->update_instance(instance_id, mat_y_pos);
334 tlas->build();
335 rays.clear();
336 rays.append({float3(0.0f, 0.0f, 0.0f), float3(-1.0f, 0.0f, 0.0f), false});
337 rays.append({float3(0.0f, 0.0f, 0.0f), float3(1.0f, 0.0f, 0.0f), false});
338 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, -1.0f, 0.0f), false});
339 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), true});
340 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, -1.0f), false});
341 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, 1.0f), false});
342 hit_test(*tlas, rays);
343
344 tlas->update_instance(instance_id, mat_z_neg);
345 tlas->build();
346 rays.clear();
347 rays.append({float3(0.0f, 0.0f, 0.0f), float3(-1.0f, 0.0f, 0.0f), false});
348 rays.append({float3(0.0f, 0.0f, 0.0f), float3(1.0f, 0.0f, 0.0f), false});
349 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, -1.0f, 0.0f), false});
350 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), false});
351 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, -1.0f), true});
352 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, 1.0f), false});
353 hit_test(*tlas, rays);
354
355 tlas->update_instance(instance_id, mat_z_pos);
356 tlas->build();
357 rays.clear();
358 rays.append({float3(0.0f, 0.0f, 0.0f), float3(-1.0f, 0.0f, 0.0f), false});
359 rays.append({float3(0.0f, 0.0f, 0.0f), float3(1.0f, 0.0f, 0.0f), false});
360 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, -1.0f, 0.0f), false});
361 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), false});
362 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, -1.0f), false});
363 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, 1.0f), true});
364 hit_test(*tlas, rays);
365}
366GPU_TEST(ray_tracing_instance_update)
367
368/* Trace one ray per fragment, proving ray queries work outside compute shaders.
369 * First ray-query test to use a render pass: under Xcode with GPU Frame Capture
370 * enabled the suite-level capture (gpu_testing.cc) surfaces a frame here. */
372{
374
376
377 VertBufPtr vertex_buf = build_vertices();
378 IndexBufPtr index_buf = build_indices();
379
381 blas->add_geometry(*index_buf, *vertex_buf);
382 blas->build();
383
385 tlas->add_instance(*blas, float4x4::identity());
386 tlas->build();
387
388 /* Rays from the cube center hit; rays outside pointing away miss. */
389 Vector<Ray> rays;
390 rays.append({float3(0.0f, 0.0f, 0.0f), float3(-1.0f, 0.0f, 0.0f), true});
391 rays.append({float3(0.0f, 0.0f, 0.0f), float3(1.0f, 0.0f, 0.0f), true});
392 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, -1.0f, 0.0f), true});
393 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), true});
394 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, -1.0f), true});
395 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, 1.0f), true});
396 rays.append({float3(5.0f, 5.0f, 5.0f), float3(1.0f, 0.0f, 0.0f), false});
397 rays.append({float3(5.0f, 5.0f, 5.0f), float3(0.0f, 1.0f, 0.0f), false});
398
399 const int ray_count = rays.size();
400 Array<float4> ray_pos(ray_count);
401 Array<float4> ray_dir(ray_count);
402 Array<int32_t> expected(ray_count);
403 for (int index : rays.index_range()) {
404 ray_pos[index] = float4(rays[index].pos, 0.0f);
405 ray_dir[index] = float4(rays[index].dir, 0.0f);
406 expected[index] = rays[index].expected_hit;
407 }
408
409 gpu::StorageBuf *ray_pos_buf = GPU_storagebuf_create(ray_count * sizeof(float4));
410 gpu::StorageBuf *ray_dir_buf = GPU_storagebuf_create(ray_count * sizeof(float4));
411 GPU_storagebuf_update(ray_pos_buf, ray_pos.data());
412 GPU_storagebuf_update(ray_dir_buf, ray_dir.data());
413
414 /* One fragment per ray: a `ray_count` x 1 attachment covered by a full-screen triangle. */
417 __func__, ray_count, 1, 1, TextureFormat::SINT_32, usage, nullptr);
418
419 gpu::FrameBuffer *framebuffer = GPU_framebuffer_create(__func__);
422 GPU_framebuffer_bind(framebuffer);
423 /* Sentinel so a column whose fragment never ran can't masquerade as a valid miss (0). */
424 GPU_framebuffer_clear_color(framebuffer, double4(-1.0));
425
426 gpu::Shader *shader = GPU_shader_create_from_info_name("gpu_ray_query_raster_test");
427 EXPECT_NE(shader, nullptr);
428
429 GPU_storagebuf_bind(ray_pos_buf, 0);
430 GPU_storagebuf_bind(ray_dir_buf, 1);
431 tlas->bind(0);
432
435 GPU_batch_draw(batch);
436 GPU_batch_discard(batch);
437
438 GPU_finish();
439
440 int32_t *result = static_cast<int32_t *>(GPU_texture_read(hit_tex, GPU_DATA_INT, 0));
441 EXPECT_EQ_SPAN(expected.as_span(), Span<int32_t>(result, ray_count));
442 MEM_delete(result);
443
445 GPU_storagebuf_free(ray_pos_buf);
446 GPU_storagebuf_free(ray_dir_buf);
447 GPU_framebuffer_free(framebuffer);
448 GPU_texture_free(hit_tex);
450
452}
453GPU_TEST(ray_tracing_fragment)
454
455/* Verify the per-instance custom index (Metal userID) is readable and 0 for a default instance. */
457{
459
460 VertBufPtr vertex_buf = build_vertices();
461 IndexBufPtr index_buf = build_indices();
462
464 blas->add_geometry(*index_buf, *vertex_buf);
465 blas->build();
466
468 tlas->add_instance(*blas, float4x4::identity());
469 tlas->build();
470
471 /* All rays start at the cube center and hit it. */
472 Vector<Ray> rays;
473 rays.append({float3(0.0f, 0.0f, 0.0f), float3(-1.0f, 0.0f, 0.0f), true});
474 rays.append({float3(0.0f, 0.0f, 0.0f), float3(1.0f, 0.0f, 0.0f), true});
475 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, -1.0f, 0.0f), true});
476 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), true});
477 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, -1.0f), true});
478 rays.append({float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, 1.0f), true});
479
480 const int ray_count = rays.size();
481 Array<float4> ray_pos(ray_count);
482 Array<float4> ray_dir(ray_count);
483 for (int index : rays.index_range()) {
484 ray_pos[index] = float4(rays[index].pos, 0.0f);
485 ray_dir[index] = float4(rays[index].dir, 0.0f);
486 }
487
488 gpu::StorageBuf *ray_pos_buf = GPU_storagebuf_create(ray_count * sizeof(float4));
489 gpu::StorageBuf *ray_dir_buf = GPU_storagebuf_create(ray_count * sizeof(float4));
490 gpu::StorageBuf *custom_index_buf = GPU_storagebuf_create(ray_count * sizeof(uint32_t));
491 GPU_storagebuf_update(ray_pos_buf, ray_pos.data());
492 GPU_storagebuf_update(ray_dir_buf, ray_dir.data());
493
494 /* Sentinel so an unwritten entry can't masquerade as the expected 0. */
495 Array<uint32_t> sentinel(ray_count, 0xFFFFFFFFu);
496 GPU_storagebuf_update(custom_index_buf, sentinel.data());
497
498 gpu::Shader *shader = GPU_shader_create_from_info_name("gpu_ray_query_custom_index_test");
499 EXPECT_NE(shader, nullptr);
501 GPU_storagebuf_bind(ray_pos_buf, 0);
502 GPU_storagebuf_bind(ray_dir_buf, 1);
503 GPU_storagebuf_bind(custom_index_buf, 2);
504 tlas->bind(0);
505
506 GPU_compute_dispatch(shader, ray_count, 1, 1);
508
509 Array<uint32_t> result(ray_count, 0xFFFFFFFFu);
510 GPU_storagebuf_read(custom_index_buf, result.data());
511
512 GPU_storagebuf_free(ray_pos_buf);
513 GPU_storagebuf_free(ray_dir_buf);
514 GPU_storagebuf_free(custom_index_buf);
516
517 /* Each hit returns the instance userID, hardcoded to 0. */
518 Array<uint32_t> expected(ray_count, 0);
519 EXPECT_EQ_SPAN(expected.as_span(), result.as_span());
520}
521GPU_TEST(ray_tracing_instance_custom_index)
522
523} // namespace blender::gpu::tests
#define UINT32_MAX
#define GPU_ATTACHMENT_TEXTURE(_texture)
#define GPU_ATTACHMENT_NONE
#define GPU_framebuffer_ensure_config(_fb,...)
blender::gpu::TopLevelAS * GPU_ray_tracing_tlas_alloc(const char *name)
blender::gpu::BottomLevelAS * GPU_ray_tracing_blas_alloc(const char *name)
#define GPU_storagebuf_create(size)
Read Blender memory allocation and freeing.
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
Span< T > as_span() const
Definition BLI_array.hh:254
const T * data() const
Definition BLI_array.hh:323
constexpr IndexRange()=default
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
virtual void bind(int slot)=0
#define instance_id
#define EXPECT_NE(result, expect)
#define GPU_TEST(test_name)
static void test_ray_tracing_empty_tlas()
static void test_ray_tracing_instance_mask()
static VertBufPtr build_vertices()
static void test_ray_tracing_instance_update()
static void test_ray_tracing_inside_cube()
static void test_ray_tracing_instance_custom_index()
static void test_ray_tracing_fragment()
static void hit_test(TopLevelAS &tlas, Span< Ray > rays, uint intersection_mask=0xFF)
static IndexBufPtr build_indices()
std::unique_ptr< BottomLevelAS, BottomLevelASDeleter > BottomLevelASPtr
std::unique_ptr< IndexBuf, IndexBufDeleter > IndexBufPtr
std::unique_ptr< gpu::VertBuf, gpu::VertBufDeleter > VertBufPtr
std::unique_ptr< TopLevelAS, TopLevelASDeleter > TopLevelASPtr
MatT from_location(const typename MatT::loc_type &location)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
void GPU_storagebuf_update(gpu::StorageBuf *ssbo, const void *data)
gpu::Batch * GPU_batch_create_procedural(GPUPrimType primitive_type, int32_t vertex_count)
Definition gpu_batch.cc:85
void GPU_storagebuf_clear_to_zero(gpu::StorageBuf *ssbo)
unsigned int uint
Definition BKE_text.h:55
void GPU_indexbuf_use(gpu::IndexBuf *elem)
void GPU_framebuffer_free(gpu::FrameBuffer *fb)
int GPU_shader_get_constant(gpu::Shader *shader, const char *name)
gpu::Shader * GPU_shader_create_from_info_name(const char *info_name)
GPUVertFormat GPU_vertformat_from_attribute(StringRef name, gpu::VertAttrType type)
void GPU_framebuffer_clear_color(gpu::FrameBuffer *fb, const double4 clear_col)
void GPU_batch_discard(gpu::Batch *batch)
void GPU_vertbuf_use(gpu::VertBuf *)
void GPU_render_begin()
void GPU_batch_set_shader(gpu::Batch *batch, gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_compute_dispatch(gpu::Shader *shader, uint groups_x_len, uint groups_y_len, uint groups_z_len, const gpu::shader::SpecializationConstants *constants_state=nullptr)
unsigned int uint32_t
Definition BKE_text.h:81
@ GPU_TEXTURE_USAGE_HOST_READ
@ GPU_TEXTURE_USAGE_ATTACHMENT
void GPU_render_end()
void GPU_shader_unbind()
void GPU_batch_draw(gpu::Batch *batch)
gpu::IndexBuf * GPU_indexbuf_calloc()
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_storagebuf_bind(gpu::StorageBuf *ssbo, int slot)
gpu::FrameBuffer * GPU_framebuffer_create(const char *name)
void GPU_texture_free(gpu::Texture *texture)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
signed int int32_t
Definition BKE_text.h:78
void GPU_shader_free(gpu::Shader *shader)
void * GPU_texture_read(gpu::Texture *texture, eGPUDataFormat data_format, int mip_level)
void GPU_storagebuf_free(gpu::StorageBuf *ssbo)
void GPU_finish()
Definition gpu_state.cc:289
VecBase< double, 4 > double4
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_storagebuf_read(gpu::StorageBuf *ssbo, void *data)
const gpu::shader::SpecializationConstants & GPU_shader_get_default_constant_state(gpu::Shader *sh)
#define SUPPORTS_RAY_QUERY()
int size() const
Definition vector.hh:130