Blender V5.3
device/metal/queue.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#ifdef WITH_METAL
8
9# include "device/kernel.h"
10# include "device/memory.h"
11# include "device/queue.h"
12
13# include "device/metal/util.h"
15
16# define MAX_SAMPLE_BUFFER_LENGTH 4096
17
18/* The number of resources to be contiguously encoded into the MetalAncillaries struct. */
19# define ANCILLARY_SLOT_COUNT 11
20
22
23class MetalDevice;
24
25/* Base class for Metal queues. */
26class MetalDeviceQueue : public DeviceQueue {
27 public:
28 MetalDeviceQueue(MetalDevice *device);
29 ~MetalDeviceQueue() override;
30
31 int num_sort_partitions(int max_num_paths, uint max_scene_shaders) const override;
32 bool supports_local_atomic_sort() const override;
33
34 void init_execution() override;
35 void load_image_info() override;
36
37 bool enqueue(DeviceKernel kernel,
38 const int work_size,
39 const DeviceKernelArguments &args) override;
40
41 bool synchronize() override;
42
43 void zero_to_device(device_memory &mem) override;
44 void copy_to_device(device_memory &mem) override;
45 void copy_from_device(device_memory &mem) override;
46 void *copy_from_device_synchronized(device_memory &mem, vector<uint8_t> &storage) override;
47
48 void *native_queue() override;
49
50 unique_ptr<DeviceGraphicsInterop> graphics_interop_create() override;
51
52 protected:
53 void setup_capture();
54 void update_capture(DeviceKernel kernel);
55 void begin_capture();
56 void end_capture();
57 void prepare_resources();
58
59 id<MTLComputeCommandEncoder> get_compute_encoder(DeviceKernel kernel);
60 id<MTLBlitCommandEncoder> get_blit_encoder();
61
62 MetalDevice *metal_device_;
63
64 ConcurrentStatesParams concurrent_states_params() const override;
65 void get_memory_info(size_t &total, size_t &free) const override;
66
67 API_AVAILABLE(macos(11.0), ios(14.0))
68 MTLCommandBufferDescriptor *command_buffer_desc_ = nullptr;
69 id<MTLDevice> mtlDevice_ = nil;
70 id<MTLCommandQueue> mtlCommandQueue_ = nil;
71 id<MTLCommandBuffer> mtlCommandBuffer_ = nil;
72 id<MTLComputeCommandEncoder> mtlComputeEncoder_ = nil;
73 id<MTLBlitCommandEncoder> mtlBlitEncoder_ = nil;
74 API_AVAILABLE(macos(10.14), ios(14.0))
75 id<MTLSharedEvent> shared_event_ = nil;
76 API_AVAILABLE(macos(10.14), ios(14.0))
77 MTLSharedEventListener *shared_event_listener_ = nil;
78 MetalDispatchPipeline active_pipelines_[DEVICE_KERNEL_NUM];
79
80 dispatch_queue_t event_queue_;
81 dispatch_semaphore_t wait_semaphore_;
82
83 uint64_t shared_event_id_;
84 uint64_t command_buffers_submitted_ = 0;
85 uint64_t command_buffers_completed_ = 0;
86 Stats &stats_;
87
88 void close_compute_encoder();
89 void close_blit_encoder();
90
91 bool verbose_tracing_ = false;
92 bool label_command_encoders_ = false;
93
94 /* Per-kernel profiling (see CYCLES_METAL_PROFILING). */
95
96 struct TimingData {
97 DeviceKernel kernel;
98 int work_size;
99 uint64_t timing_id;
100 };
101 std::vector<TimingData> command_encoder_labels_;
102 bool profiling_enabled_ = false;
103 uint64_t current_encoder_idx_ = 0;
104
105 std::atomic<uint64_t> counter_sample_buffer_curr_idx_ = 0;
106
107 void flush_timing_stats();
108
109 struct TimingStats {
110 double total_time = 0.0;
111 uint64_t total_work_size = 0;
112 uint64_t num_dispatches = 0;
113 };
114 TimingStats timing_stats_[DEVICE_KERNEL_NUM];
115 double last_completion_time_ = 0.0;
116
117 /* .gputrace capture (see CYCLES_DEBUG_METAL_CAPTURE_...). */
118
119 id<MTLCaptureScope> mtlCaptureScope_ = nil;
120 DeviceKernel capture_kernel_;
121 int capture_dispatch_counter_ = 0;
122 bool capture_samples_ = false;
123 int capture_reset_counter_ = 0;
124 bool is_capturing_ = false;
125 bool is_capturing_to_disk_ = false;
126 bool has_captured_to_disk_ = false;
127};
128
130
131#endif /* WITH_METAL */
unsigned long long int uint64_t
#define CCL_NAMESPACE_END
#define storage(slot, qualifiers)
ccl_gpu_kernel_postfix const ccl_global int ccl_global float const int work_size
DeviceKernel
@ DEVICE_KERNEL_NUM