Blender  V3.3
metal/device_impl.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2021-2022 Blender Foundation */
3 
4 #pragma once
5 
6 #ifdef WITH_METAL
7 
8 # include "bvh/bvh.h"
9 # include "device/device.h"
10 # include "device/metal/bvh.h"
11 # include "device/metal/device.h"
12 # include "device/metal/kernel.h"
13 # include "device/metal/queue.h"
14 # include "device/metal/util.h"
15 
16 # include <Metal/Metal.h>
17 
19 
20 class DeviceQueue;
21 
22 class MetalDevice : public Device {
23  public:
24  id<MTLDevice> mtlDevice = nil;
25  id<MTLLibrary> mtlLibrary[PSO_NUM] = {nil};
26  id<MTLArgumentEncoder> mtlBufferKernelParamsEncoder =
27  nil; /* encoder used for fetching device pointers from MTLBuffers */
28  id<MTLCommandQueue> mtlGeneralCommandQueue = nil;
29  id<MTLArgumentEncoder> mtlAncillaryArgEncoder =
30  nil; /* encoder used for fetching device pointers from MTLBuffers */
31  string source[PSO_NUM];
32  string source_md5[PSO_NUM];
33 
34  bool capture_enabled = false;
35 
36  KernelParamsMetal launch_params = {0};
37 
38  /* MetalRT members ----------------------------------*/
39  BVHMetal *bvhMetalRT = nullptr;
40  bool motion_blur = false;
41  id<MTLArgumentEncoder> mtlASArgEncoder =
42  nil; /* encoder used for fetching device pointers from MTLAccelerationStructure */
43  /*---------------------------------------------------*/
44 
45  MetalGPUVendor device_vendor;
46 
47  uint kernel_features;
48  MTLResourceOptions default_storage_mode;
49  int max_threads_per_threadgroup;
50 
51  int mtlDevId = 0;
52  bool first_error = true;
53 
54  struct MetalMem {
55  device_memory *mem = nullptr;
56  int pointer_index = -1;
57  id<MTLBuffer> mtlBuffer = nil;
58  id<MTLTexture> mtlTexture = nil;
59  uint64_t offset = 0;
60  uint64_t size = 0;
61  void *hostPtr = nullptr;
62  bool use_UMA = false; /* If true, UMA memory in shared_pointer is being used. */
63  };
64  typedef map<device_memory *, unique_ptr<MetalMem>> MetalMemMap;
65  MetalMemMap metal_mem_map;
66  std::vector<id<MTLResource>> delayed_free_list;
67  std::recursive_mutex metal_mem_map_mutex;
68 
69  /* Bindless Textures */
70  device_vector<TextureInfo> texture_info;
71  bool need_texture_info;
72  id<MTLArgumentEncoder> mtlTextureArgEncoder = nil;
73  id<MTLBuffer> texture_bindings_2d = nil;
74  id<MTLBuffer> texture_bindings_3d = nil;
75  std::vector<id<MTLTexture>> texture_slot_map;
76 
77  bool use_metalrt = false;
78  MetalPipelineType kernel_specialization_level = PSO_GENERIC;
79  std::atomic_bool async_compile_and_load = false;
80 
81  virtual BVHLayoutMask get_bvh_layout_mask() const override;
82 
83  void set_error(const string &error) override;
84 
85  MetalDevice(const DeviceInfo &info, Stats &stats, Profiler &profiler);
86 
87  virtual ~MetalDevice();
88 
89  bool support_device(const uint /*kernel_features*/);
90 
91  bool check_peer_access(Device *peer_device) override;
92 
93  bool use_adaptive_compilation();
94 
95  void make_source(MetalPipelineType pso_type, const uint kernel_features);
96 
97  virtual bool load_kernels(const uint kernel_features) override;
98 
99  void reserve_local_memory(const uint kernel_features);
100 
101  void init_host_memory();
102 
103  void load_texture_info();
104 
105  void erase_allocation(device_memory &mem);
106 
107  virtual bool should_use_graphics_interop() override;
108 
109  virtual unique_ptr<DeviceQueue> gpu_queue_create() override;
110 
111  virtual void build_bvh(BVH *bvh, Progress &progress, bool refit) override;
112 
113  virtual void optimize_for_scene(Scene *scene) override;
114 
115  bool compile_and_load(MetalPipelineType pso_type);
116 
117  /* ------------------------------------------------------------------ */
118  /* low-level memory management */
119 
120  MetalMem *generic_alloc(device_memory &mem);
121 
122  void generic_copy_to(device_memory &mem);
123 
124  void generic_free(device_memory &mem);
125 
126  void mem_alloc(device_memory &mem) override;
127 
128  void mem_copy_to(device_memory &mem) override;
129 
130  void mem_copy_from(device_memory &mem)
131  {
132  mem_copy_from(mem, -1, -1, -1, -1);
133  }
134  void mem_copy_from(device_memory &mem, size_t y, size_t w, size_t h, size_t elem) override;
135 
136  void mem_zero(device_memory &mem) override;
137 
138  void mem_free(device_memory &mem) override;
139 
140  device_ptr mem_alloc_sub_ptr(device_memory &mem, size_t offset, size_t /*size*/) override;
141 
142  virtual void const_copy_to(const char *name, void *host, size_t size) override;
143 
144  void global_alloc(device_memory &mem);
145 
146  void global_free(device_memory &mem);
147 
148  void tex_alloc(device_texture &mem);
149 
150  void tex_alloc_as_buffer(device_texture &mem);
151 
152  void tex_free(device_texture &mem);
153 
154  void flush_delayed_free_list();
155 };
156 
158 
159 #endif
unsigned int uint
Definition: BLI_sys_types.h:67
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
void refit(btStridingMeshInterface *triangles, const btVector3 &aabbMin, const btVector3 &aabbMax)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition: btQuadWord.h:119
Definition: bvh/bvh.h:63
virtual void optimize_for_scene(Scene *)
virtual BVHLayoutMask get_bvh_layout_mask() const =0
virtual void const_copy_to(const char *name, void *host, size_t size)=0
virtual void mem_zero(device_memory &mem)=0
virtual void mem_copy_from(device_memory &mem, size_t y, size_t w, size_t h, size_t elem)=0
virtual unique_ptr< DeviceQueue > gpu_queue_create()
virtual void build_bvh(BVH *bvh, Progress &progress, bool refit)
virtual bool load_kernels(uint)
virtual bool check_peer_access(Device *)
virtual void mem_free(device_memory &mem)=0
virtual void set_error(const string &error)
virtual void mem_copy_to(device_memory &mem)=0
virtual device_ptr mem_alloc_sub_ptr(device_memory &, size_t, size_t)
virtual void mem_alloc(device_memory &mem)=0
virtual bool should_use_graphics_interop()
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
Scene scene
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
static void error(const char *str)
Definition: meshlaplacian.c:51
int BVHLayoutMask
Definition: params.h:47
unsigned __int64 uint64_t
Definition: stdint.h:90
uint64_t device_ptr
Definition: util/types.h:43