43#define MTL_DEBUG_SINGLE_DISPATCH_PER_ENCODER 0
46#define DEBUG_BIND_NULL_BUFFER_FOR_MISSING_UBO 0
50#define DEBUG_BIND_NULL_BUFFER_FOR_MISSING_SSBO 0
53#if DEBUG_BIND_NULL_BUFFER_FOR_MISSING_UBO == 1
54# define MTL_LOG_UBO_ERROR MTL_LOG_WARNING
56# define MTL_LOG_UBO_ERROR MTL_LOG_ERROR
59#if DEBUG_BIND_NULL_BUFFER_FOR_MISSING_SSBO == 1
60# define MTL_LOG_SSBO_ERROR MTL_LOG_WARNING
62# define MTL_LOG_SSBO_ERROR MTL_LOG_ERROR
89 if (default_fbo_mtltexture_) {
90 [default_fbo_mtltexture_ release];
91 default_fbo_mtltexture_ = nil;
95 MTLFrameBuffer *mtl_front_left =
static_cast<MTLFrameBuffer *
>(this->
front_left);
96 MTLFrameBuffer *mtl_back_left =
static_cast<MTLFrameBuffer *
>(this->
back_left);
100 GHOST_ContextMTL *ghost_mtl_ctx =
dynamic_cast<GHOST_ContextMTL *
>(ghost_ctx);
101 if (ghost_mtl_ctx !=
nullptr) {
105 "Binding GHOST context MTL %p to GPU context %p. (Device: %p, queue: %p, texture: %p)",
110 default_fbo_gputexture_);
113 if (default_fbo_mtltexture_) {
116 if (default_fbo_gputexture_) {
118 default_fbo_gputexture_ =
nullptr;
122 [default_fbo_mtltexture_ retain];
126 default_fbo_gputexture_ =
new gpu::MTLTexture(
"MTL_BACKBUFFER",
127 TextureFormat::SFLOAT_16_16_16_16,
129 default_fbo_mtltexture_);
135 this->
label = default_fbo_mtltexture_.label;
141 if (!default_fbo_gputexture_) {
142 default_fbo_gputexture_ =
static_cast<gpu::MTLTexture *
>(
147 TextureFormat::SFLOAT_16_16_16_16,
154 "-- Bound context %p for GPU context: %p is offscreen and does not have a default "
159 this->
label =
@"Offscreen Metal Context";
165 " Failed to bind GHOST context to MTLContext -- GHOST_ContextMTL is null "
166 "(GhostContext: %p, GhostContext_MTL: %p)",
173void MTLContext::set_ghost_window(GHOST_IWindow *ghostWinHandle)
175 GHOST_Window *ghostWin =
static_cast<GHOST_Window *
>(ghostWinHandle);
176 this->set_ghost_context(ghostWin ? ghostWin->
getContext() :
nullptr);
199 is_inside_frame_ =
false;
200 current_frame_index_ = 0;
204 null_attribute_buffer_ = nil;
207 default_fbo_mtltexture_ = nil;
208 default_fbo_gputexture_ =
nullptr;
217 [this->
queue retain];
220#pragma clang diagnostic push
221#pragma clang diagnostic ignored "-Wobjc-method-access"
225 if (@available(macOS 13.3, *)) {
226 [this->
device setShouldMaximizeConcurrentCompilation:YES];
228#pragma clang diagnostic pop
231 this->ghost_context_->metalRegisterPresentCallback(&
present);
232 this->ghost_context_->metalRegisterXrBlitCallback(&
xr_blit);
243 MTLBackend::platform_init(
this);
244 MTLBackend::capabilities_init(
this);
260 samplers_.mtl_sampler[i] = nil;
288 if (default_fbo_gputexture_) {
290 default_fbo_gputexture_ =
nullptr;
292 if (default_fbo_mtltexture_) {
293 [default_fbo_mtltexture_ release];
294 default_fbo_mtltexture_ = nil;
309 if (ubo_bind.bound && ubo_bind.ubo !=
nullptr) {
316 if (ssbo_bind.bound && ssbo_bind.ssbo !=
nullptr) {
317 ssbo_bind.ssbo->unbind();
327 for (
int filtering_i = 0; filtering_i < GPU_SAMPLER_FILTERING_TYPES_COUNT; filtering_i++) {
328 if (sampler_state_cache_[extend_yz_i][extend_x_i][filtering_i] != nil) {
329 [sampler_state_cache_[extend_yz_i][extend_x_i][filtering_i] release];
330 sampler_state_cache_[extend_yz_i][extend_x_i][filtering_i] = nil;
338 if (custom_sampler_state_cache_[i] != nil) {
339 [custom_sampler_state_cache_[i] release];
340 custom_sampler_state_cache_[i] = nil;
345 for (
auto *entry : cached_sampler_buffers_.values()) {
348 cached_sampler_buffers_.clear();
352 [null_buffer_ release];
354 if (null_attribute_buffer_) {
355 [null_attribute_buffer_ release];
363 [this->
queue release];
369 this->process_frame_timings();
380 is_inside_frame_ =
true;
391 is_inside_frame_ =
false;
393 this->process_frame_timings();
415 else if (ghost_context_) {
416 this->set_ghost_context(ghost_context_);
421 if (ssbo_bind.bound && ssbo_bind.ubo !=
nullptr) {
422 ssbo_bind.bound =
false;
423 ssbo_bind.ubo =
nullptr;
429 if (ssbo_bind.bound && ssbo_bind.ssbo !=
nullptr) {
430 ssbo_bind.bound =
false;
431 ssbo_bind.ssbo =
nullptr;
490 BLI_assert(
false &&
"No framebuffer is bound!");
505 this->active_fb != this->main_command_buffer.get_active_framebuffer() ||
506 this->main_command_buffer.get_active_framebuffer()->get_dirty() ||
507 this->is_visibility_dirty())
512 MTL_LOG_WARNING(
"Framebuffer validation failed, falling back to default framebuffer");
516 MTL_LOG_ERROR(
"CRITICAL: DEFAULT FRAMEBUFFER FAIL VALIDATION!!");
521 bool new_render_pass =
false;
525 if (new_render_pass) {
553 if (null_buffer_ != nil) {
562 static const int null_buffer_size = 20480;
563 null_buffer_ = [this->
device newBufferWithLength:null_buffer_size
564 options:MTLResourceStorageModeManaged];
565 [null_buffer_ retain];
567 memcpy([null_buffer_ contents], null_data, null_buffer_size);
568 [null_buffer_ didModifyRange:NSMakeRange(0, null_buffer_size)];
577 if (null_attribute_buffer_ != nil) {
578 return null_attribute_buffer_;
583 static const int null_buffer_size = 256;
584 null_attribute_buffer_ = [this->
device newBufferWithLength:null_buffer_size
585 options:MTLResourceStorageModeManaged];
587 [null_attribute_buffer_ retain];
588 float data[4] = {0.0f, 0.0f, 0.0f, 1.0f};
589 memcpy([null_attribute_buffer_ contents],
data,
sizeof(
float) * 4);
590 [null_attribute_buffer_ didModifyRange:NSMakeRange(0, null_buffer_size)];
592 return null_attribute_buffer_;
599 gpu::MTLTexture *dummy_tex = dummy_textures_[sampler_format][type - 1];
600 if (dummy_tex !=
nullptr) {
605 switch (sampler_format) {
607 format = TextureFormat::UNORM_8_8_8_8;
610 format = TextureFormat::SINT_8_8_8_8;
613 format = TextureFormat::UINT_8_8_8_8;
616 format = TextureFormat::SFLOAT_32_DEPTH_UINT_8;
648 if (!dummy_verts_[sampler_format]) {
651 VertAttrType attr_type = VertAttrType::SFLOAT_32_32_32_32;
653 switch (sampler_format) {
656 attr_type = VertAttrType::SFLOAT_32_32_32_32;
659 attr_type = VertAttrType::SINT_32_32_32_32;
662 attr_type = VertAttrType::UINT_32_32_32_32;
670 dummy_vertformat_[sampler_format],
681 dummy_textures_[sampler_format][type - 1] = metal_tex;
689 if (dummy_textures_[format][tex]) {
691 static_cast<Texture *
>(dummy_textures_[format][tex])));
692 dummy_textures_[format][tex] =
nullptr;
695 if (dummy_verts_[format]) {
722 std::string buf_name = texture_name + std::string(
"_buf_");
723 std::string metadata_name = texture_name + std::string(
"_metadata_");
732template<
typename CommandEncoderT>
760 size_t size = [argument_encoder encodedLength];
761 size_t alignment =
max_uu([argument_encoder alignment], 256);
762 size_t size_align_delta = (size % alignment);
763 size_t aligned_alloc_size = ((alignment > 1) && (size_align_delta > 0)) ?
764 size + (alignment - (size % alignment)) :
772 [argument_encoder setArgumentBuffer:encoder_buf->
get_metal_buffer() offset:0];
773 [argument_encoder setSamplerStates:sampler_array.
mtl_sampler
774 withRange:NSMakeRange(0, max_sampler_index + 1)];
775 encoder_buf->
flush();
786template<
typename CommandEncoderT>
808 uint16_t bind_image = dirty_enabled_image_mask & stage_ima_mask;
812 if (gpu_tex ==
nullptr) {
816 shader.name_get().c_str(),
837 uint64_t bind_sampler = dirty_enabled_sampler_mask & stage_tex_mask;
842 if (gpu_tex ==
nullptr) {
845 MTL_LOG_ERROR(
"Shader %s: Missing texture bind: %s slot(%d).",
846 shader.name_get().c_str(),
886template<
typename CommandEncoderT>
911template<
typename CommandEncoderT>
936 shader.name_get().c_str(),
951 shader.name_get().c_str(),
967 MTL_LOG_ERROR(
"Shader %s: Missing acceleration structure bind at slot(%d).",
968 shader.name_get().c_str(),
980 MTLColorWriteMaskBlue | MTLColorWriteMaskAlpha;
990 for (
int v = 0;
v < GPU_MAX_VIEWPORTS;
v++) {
1021 MTLStencilOperationKeep;
1025 MTLStencilOperationKeep;
1056 for (
int v = 0;
v < count;
v++) {
1057 const int (&viewport_info)[4] = viewports[
v];
1140 "Bound active shader is not valid (Missing/invalid implementation for Metal).", );
1160 this, mtl_prim_type_to_topology_class(mtl_prim_type));
1163 MTL_LOG_ERROR(
"Failed to bake Metal pipeline state for shader: %s",
1178 MTL_LOG_ERROR(
"ensure_render_pipeline_state called while render pass is not active.");
1191 [rec setRenderPipelineState:psi->
pso];
1199 if (active_shader_changed && pc_buf) {
1204 [rec pushDebugGroup:
@"ApplyState"];
1226 if (pc_buf && pc_buf->
is_dirty()) {
1245 MTLViewport viewports[GPU_MAX_VIEWPORTS];
1247 MTLViewport &viewport = viewports[
v];
1252 viewport.znear = 0.0f;
1253 viewport.zfar = 1.0f;
1259 MTLViewport viewport;
1264 viewport.znear = 0.0f;
1265 viewport.zfar = 1.0f;
1266 [rec setViewport:viewport];
1275 MTLScissorRect scissor;
1301 bool is_attachmentless = (default_w == 0) && (default_h == 0);
1304 scissor.width = (is_attachmentless) ? render_fb->
get_width() : default_w;
1305 scissor.height = (is_attachmentless) ? render_fb->
get_height() : default_h;
1311 [rec setScissorRect:scissor];
1323 MTLWindingClockwise :
1324 MTLWindingCounterClockwise;
1325 [rec setFrontFacingWinding:winding];
1333 MTLCullMode mode = MTLCullModeNone;
1337 mode = MTLCullModeNone;
1340 mode = MTLCullModeFront;
1343 mode = MTLCullModeBack;
1350 [rec setCullMode:mode];
1356 [rec popDebugGroup];
1380 bool hasDepthTarget =
fb->has_depth_attachment();
1381 bool hasStencilTarget =
fb->has_stencil_attachment();
1383 if (hasDepthTarget || hasStencilTarget) {
1395 if (depth_stencil_state_lookup ==
nullptr) {
1397 MTLDepthStencilDescriptor *ds_state_desc = [[[MTLDepthStencilDescriptor alloc]
init]
1400 if (hasDepthTarget) {
1401 ds_state_desc.depthWriteEnabled =
1403 ds_state_desc.depthCompareFunction =
1406 MTLCompareFunctionAlways;
1409 if (hasStencilTarget) {
1410 ds_state_desc.backFaceStencil.readMask =
1412 ds_state_desc.backFaceStencil.writeMask =
1414 ds_state_desc.backFaceStencil.stencilFailureOperation =
1416 ds_state_desc.backFaceStencil.depthFailureOperation =
1418 ds_state_desc.backFaceStencil.depthStencilPassOperation =
1420 ds_state_desc.backFaceStencil.stencilCompareFunction =
1423 MTLCompareFunctionAlways;
1425 ds_state_desc.frontFaceStencil.readMask =
1427 ds_state_desc.frontFaceStencil.writeMask =
1429 ds_state_desc.frontFaceStencil.stencilFailureOperation =
1431 ds_state_desc.frontFaceStencil.depthFailureOperation =
1433 ds_state_desc.frontFaceStencil.depthStencilPassOperation =
1435 ds_state_desc.frontFaceStencil.stencilCompareFunction =
1438 MTLCompareFunctionAlways;
1442 ds_state = [this->
device newDepthStencilStateWithDescriptor:ds_state_desc];
1449 ds_state = *depth_stencil_state_lookup;
1455 if (ds_state != nil) {
1457 [rec setDepthStencilState:ds_state];
1463 if (hasStencilTarget) {
1469 [rec setStencilReferenceValue:stencil_ref_value];
1474 if (hasDepthTarget) {
1475 [rec setDepthBias:0 slopeScale:0
clamp:0];
1497 "Bound active shader is not valid (Missing/invalid implementation for Metal).", );
1509 if (compute_pso_inst ==
nullptr || compute_pso_inst->
pso == nil) {
1513 return compute_pso_inst;
1521 if (pipe_state_inst ==
nullptr) {
1525#if MTL_DEBUG_SINGLE_DISPATCH_PER_ENCODER == 1
1544 [compute_encoder pushDebugGroup:[NSString stringWithFormat:
@"Dispatch(Shader:%s)",
1553 [compute_encoder pushDebugGroup:
@"ApplyState"];
1560 if (pc_buf && pc_buf->
is_dirty()) {
1566 [compute_encoder popDebugGroup];
1572 [compute_encoder dispatchThreadgroups:MTLSizeMake(
max_ii(groups_x_len, 1),
1580 [compute_encoder popDebugGroup];
1583#if MTL_DEBUG_SINGLE_DISPATCH_PER_ENCODER == 1
1590#if MTL_DEBUG_SINGLE_DISPATCH_PER_ENCODER == 1
1613 [compute_encoder pushDebugGroup:[NSString stringWithFormat:
@"DispatchIndirect(Shader:%s)",
1622 [compute_encoder pushDebugGroup:
@"ApplyState"];
1629 if (pc_buf && pc_buf->
is_dirty()) {
1635 [compute_encoder popDebugGroup];
1642 if (mtl_indirect_buf == nil) {
1643 MTL_LOG_WARNING(
"Metal Indirect Compute dispatch storage buffer does not exist.");
1645 [compute_encoder popDebugGroup];
1654 dispatchThreadgroupsWithIndirectBuffer:mtl_indirect_buf
1655 indirectBufferOffset:0
1661 [compute_encoder popDebugGroup];
1663#if MTL_DEBUG_SINGLE_DISPATCH_PER_ENCODER == 1
1680 visibility_is_dirty_ = (buffer != visibility_buffer_) || visibility_is_dirty_;
1681 visibility_buffer_ = buffer;
1687 visibility_is_dirty_ = (visibility_buffer_ !=
nullptr) || visibility_is_dirty_;
1688 visibility_buffer_ =
nullptr;
1694 return visibility_buffer_;
1699 visibility_is_dirty_ =
false;
1704 return visibility_is_dirty_;
1721 MTL_LOG_ERROR(
"Attempting to bind texture '%s' to invalid texture unit %d",
1742 MTL_LOG_ERROR(
"Attempting to bind sampler to invalid sampler unit %d", sampler_unit);
1761 if (resource_bind.texture_resource == mtl_texture) {
1762 resource_bind.texture_resource =
nullptr;
1771 if (resource_bind.texture_resource == mtl_texture) {
1772 resource_bind.texture_resource =
nullptr;
1784 if (resource_bind.texture_resource) {
1785 resource_bind.texture_resource =
nullptr;
1793 if (resource_bind.texture_resource) {
1794 resource_bind.texture_resource =
nullptr;
1816 switch (wrap_mode) {
1818 return MTLSamplerAddressModeClampToEdge;
1820 return MTLSamplerAddressModeRepeat;
1822 return MTLSamplerAddressModeMirrorRepeat;
1824 return MTLSamplerAddressModeClampToBorderColor;
1827 return MTLSamplerAddressModeClampToEdge;
1835 const MTLSamplerAddressMode extend_t =
to_mtl_type(extend_yz);
1839 const MTLSamplerAddressMode extend_s =
to_mtl_type(extend_x);
1841 for (
int filtering_i = 0; filtering_i < GPU_SAMPLER_FILTERING_TYPES_COUNT; filtering_i++) {
1844 MTLSamplerDescriptor *
descriptor = [[MTLSamplerDescriptor alloc]
init];
1849 descriptor.borderColor = MTLSamplerBorderColorTransparentBlack;
1851 MTLSamplerMinMagFilterLinear :
1852 MTLSamplerMinMagFilterNearest;
1854 MTLSamplerMinMagFilterLinear :
1855 MTLSamplerMinMagFilterNearest;
1857 MTLSamplerMipFilterLinear :
1858 MTLSamplerMipFilterNotMipmapped;
1861 float aniso_filter =
min_ff(
float(GPU_anisotropic_samples_get(filtering)), 16.0f);
1863 descriptor.compareFunction = MTLCompareFunctionAlways;
1867 sampler_state_cache_[extend_yz_i][extend_x_i][filtering_i] =
state;
1877 MTLSamplerDescriptor *
descriptor = [[MTLSamplerDescriptor alloc]
init];
1878 descriptor.minFilter = MTLSamplerMinMagFilterLinear;
1879 descriptor.magFilter = MTLSamplerMinMagFilterLinear;
1880 descriptor.compareFunction = MTLCompareFunctionLessEqual;
1895 MTLSamplerDescriptor *
descriptor = [[MTLSamplerDescriptor alloc]
init];
1896 descriptor.minFilter = MTLSamplerMinMagFilterLinear;
1897 descriptor.magFilter = MTLSamplerMinMagFilterLinear;
1898 descriptor.mipFilter = MTLSamplerMipFilterNearest;
1912 if (default_sampler_state_ == nil) {
1915 return default_sampler_state_;
1926 if (buffer_clear_pso_ != nil) {
1927 return buffer_clear_pso_;
1938 struct BufferClearParams {\
1941 kernel void compute_buffer_clear(constant BufferClearParams ¶ms [[buffer(0)]],\
1942 device uint32_t* output_data [[buffer(1)]],\
1943 uint position [[thread_position_in_grid]])\
1945 output_data[position] = params.clear_value;\
1947 NSString *compute_buffer_clear_src = [NSString stringWithUTF8String:src];
1950 MTLCompileOptions *
options = [[[MTLCompileOptions alloc]
init] autorelease];
1951 options.languageVersion = MTLLanguageVersion2_2;
1953 NSError *error =
nullptr;
1956 error:&error] autorelease];
1959 if ([[error localizedDescription] rangeOfString:
@"Compilation succeeded"].location ==
1963 "Compile Error - Metal Shader Library error %s",
1964 [[error localizedDescription] UTF8String]);
1972 id<MTLFunction> temp_compute_function = [[temp_lib newFunctionWithName:
@"compute_buffer_clear"]
1977 buffer_clear_pso_ = [ctx->
device newComputePipelineStateWithFunction:temp_compute_function
1979 if (error || buffer_clear_pso_ == nil) {
1981 "Failed to prepare compute_buffer_clear MTLComputePipelineState %s",
1982 [[error localizedDescription] UTF8String]);
1987 [buffer_clear_pso_ retain];
1991 return buffer_clear_pso_;
2000void present(MTLRenderPassDescriptor *blit_descriptor,
2023 perf_max_drawables = 1;
2026 perf_max_drawables = 2;
2048 [enc setRenderPipelineState:blit_pso];
2049 [enc setFragmentTexture:swapchain_texture atIndex:0];
2050 [enc drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3];
2055 [cmdbuf presentDrawable:drawable];
2059 std::chrono::time_point submission_time = std::chrono::high_resolution_clock::now();
2074 std::chrono::time_point completion_time = std::chrono::high_resolution_clock::now();
2075 int64_t microseconds_per_frame = std::chrono::duration_cast<std::chrono::microseconds>(
2076 completion_time - submission_time)
2080 MTL_LOG_DEBUG(
"Frame Latency: %f ms (Rolling avg: %f ms Drawables: %d)",
2081 ((
float)microseconds_per_frame) / 1000.0f,
2083 perf_max_drawables);
2090 [cmdbuf waitUntilCompleted];
2091 NSError *error = [cmdbuf error];
2093 CLOG_ERROR(&
LOG,
"Command buffer error: %s", [[error localizedDescription] UTF8String]);
2117 MTLOrigin origin = MTLOriginMake(ofsx, ofsy, 0);
2118 MTLSize size = MTLSizeMake(width, height, 1);
2121 [blit_encoder copyFromTexture:src_texture
2126 toTexture:metal_xr_texture
2129 destinationOrigin:origin];
Platform independent time functions.
#define CLOG_ERROR(clg_ref,...)
#define GPU_SAMPLER_CUSTOM_TYPES_COUNT
#define GPU_SAMPLER_EXTEND_MODES_COUNT
#define BLI_assert_unreachable()
#define BLI_assert_msg(a, msg)
id< MTLTexture > metalOverlayTexture()
static GHOST_IContext * getActiveDrawingContext()
GHOST_Context * getContext()
Value & lookup_or_add_cb(const Key &key, const CreateValueF &create_value)
constexpr const char * c_str() const
GHOST_IWindow * ghost_window_
StateManager * state_manager
bool is_active_on_thread()
static MTLCapabilities & get_capabilities()
static MTLBackend * get()
void init(id< MTLDevice > device)
MTLSafeFreeList * acquire_current_safe_list()
gpu::MTLBuffer * allocate(uint64_t size, bool cpu_visible)
id< MTLBuffer > get_metal_buffer() const
MTLFrameBuffer * get_active_framebuffer()
void inc_active_command_buffer_count()
bool do_break_submission()
void prepare(bool supports_render=true)
void wait_until_active_command_buffers_complete()
MTLRenderPassState & get_render_pass_state()
id< MTLRenderCommandEncoder > ensure_begin_render_command_encoder(MTLFrameBuffer *ctx_framebuffer, bool force_begin, bool *r_new_pass)
bool is_inside_render_pass()
id< MTLComputeCommandEncoder > ensure_begin_compute_encoder()
MTLComputeState & get_compute_state()
id< MTLBlitCommandEncoder > ensure_begin_blit_encoder()
void dec_active_command_buffer_count()
id< MTLRenderCommandEncoder > get_active_render_command_encoder()
void unfold_pending_debug_groups()
static volatile std::atomic< int > num_active_cmd_bufs_in_system
MTLBindingCache< gpu::MTLComputeCommandEncoder > compute_bindings
void bind_pso(id< MTLComputePipelineState > pso)
id< MTLComputePipelineState > get_buffer_clear_pso()
bool ensure_render_pipeline_state(MTLPrimitiveType prim_type)
MTLFrameBuffer * get_current_framebuffer()
MTLFrameBuffer * get_default_framebuffer()
static std::atomic< int64_t > avg_drawable_latency_us
MTLContextComputeUtils & get_compute_utils()
const MTLComputePipelineStateInstance * ensure_compute_pipeline_state()
friend class MTLComputeState
void clear_visibility_dirty()
id< MTLSamplerState > get_sampler_from_state(MTLSamplerState state)
shader::SpecializationConstants constants_state
void deactivate() override
static void latency_resolve_average(int64_t frame_latency_us)
gpu::MTLBuffer * get_visibility_buffer() const
void set_visibility_buffer(gpu::MTLBuffer *buffer)
void set_scissor_enabled(bool scissor_enabled)
void framebuffer_bind(MTLFrameBuffer *framebuffer)
id< MTLRenderCommandEncoder > ensure_begin_render_pass()
static MTLContext * get()
id< MTLBuffer > get_null_buffer()
friend class MTLRenderPassState
id< MTLBuffer > get_null_attribute_buffer()
static void global_memory_manager_release_ref()
MTLScratchBufferManager memory_manager
void pipeline_state_init()
MTLContextGlobalShaderPipelineState pipeline_state
MTLSamplerArray & get_sampler_array()
static MTLBufferPool * global_memory_manager
void end_frame() override
static int global_memory_manager_refcount
MTLScratchBufferManager & get_scratch_buffer_manager()
MTLShader * get_active_shader()
void texture_unbind(gpu::MTLTexture *mtl_texture, bool is_image, StateManager *state_manager)
void set_viewport(int origin_x, int origin_y, int width, int height)
static int64_t frame_latency[MTL_FRAME_AVERAGE_COUNT]
static std::mutex global_memory_manager_reflock
void sampler_bind(MTLSamplerState, uint sampler_unit)
void texture_bind(gpu::MTLTexture *mtl_texture, uint texture_unit, bool is_image)
void specialization_constants_set(const shader::SpecializationConstants *constants_state)
void begin_frame() override
void framebuffer_restore()
void set_viewports(int count, const int(&viewports)[GPU_MAX_VIEWPORTS][4])
void compute_dispatch_indirect(StorageBuf *indirect_buf)
void sampler_state_cache_init()
static void check_error(const char *info)
void set_scissor(int scissor_x, int scissor_y, int scissor_width, int scissor_height)
MTLCommandBufferManager main_command_buffer
id< MTLSamplerState > get_default_sampler_state()
bool is_visibility_dirty() const
void texture_unbind_all(bool is_image)
static void global_memory_manager_acquire_ref()
void memory_statistics_get(int *r_total_mem, int *r_free_mem) override
void free_dummy_resources()
Map< MTLSamplerArray, gpu::MTLBuffer * > & get_sampler_arg_buf_cache()
id< MTLCommandQueue > queue
gpu::MTLTexture * get_dummy_texture(GPUTextureType type, GPUSamplerFormat sampler_format)
void compute_dispatch(int groups_x_len, int groups_y_len, int groups_z_len)
MTLContextTextureUtils & get_texture_utils()
static MTLBufferPool * get_global_memory_manager()
void ensure_depth_stencil_state()
MTLContext(GHOST_IWindow *ghost_window, GHOST_IContext *ghost_context)
static std::atomic< int > max_drawables_in_flight
MTLAttachment get_color_attachment(uint slot)
void remove_all_attachments()
int get_attachment_height()
bool add_color_attachment(gpu::MTLTexture *texture, uint slot, int miplevel, int layer)
MTLFrameBuffer(MTLContext *ctx, const char *name)
int get_attachment_width()
void bind_vertex_buffer(id< MTLBuffer > buffer, uint64_t buffer_offset, uint index)
MTLBindingCache< gpu::MTLVertexCommandEncoder > vertex_bindings
id< MTLDepthStencilState > bound_ds_state
MTLScissorRect last_scissor_rect
uint last_used_stencil_ref_value
MTLBindingCache< gpu::MTLFragmentCommandEncoder > fragment_bindings
id< MTLRenderPipelineState > bound_pso
MTLBoundShaderState last_bound_shader_state
void decrement_reference()
const char * sampler_name_get(int slot) const
const char * name_get() const
uint32_t enabled_accel_mask() const
const char * image_name_get(int slot) const
bool use_samplers_argument_buffer() const
id< MTLArgumentEncoder > ensure_argument_encoder(id< MTLFunction > mtl_function)
bool use_texture_atomic() const
const char * name_at_offset(uint32_t offset) const
const MTLComputePipelineStateCommon & get_compute_common_state()
MTLComputePipelineStateInstance * bake_compute_pipeline_state(MTLContext *ctx, MTLComputePipelineStateDescriptor &compute_pipeline_descriptor)
void uniform_int(int location, int comp_len, int array_size, const int *data) override
MTLShaderInterface & get_interface()
MTLRenderPipelineStateInstance * bake_current_pipeline_state(MTLContext *ctx, MTLPrimitiveTopologyClass prim_type)
MTLPushConstantBuf * get_push_constant_buf()
MTLStateManager(MTLContext *ctx)
id< MTLBuffer > get_metal_buffer()
void bind(int slot) override
bool has_custom_swizzle()
id< MTLTexture > get_metal_handle()
friend void xr_blit(id< MTLTexture > metal_xr_texture, int ofsx, int ofsy, int width, int height)
id< MTLTexture > get_metal_handle_base()
MTLStorageBuf * get_storagebuf()
const int * get_texture_metadata_ptr() const
void make_resident(Encoder &enc)
id< MTLAccelerationStructure > acceleration_structure() const
uint16_t enabled_ssbo_mask_
uint64_t enabled_tex_mask_
const ShaderInput * uniform_get(const StringRefNull name) const
const ShaderInput * texture_get(const int binding) const
uint8_t enabled_ima_mask_
const ShaderInput * ubo_get(const StringRefNull name) const
const ShaderInput * ssbo_get(const StringRefNull name) const
uint16_t enabled_ubo_mask_
StringRefNull name_get() const
virtual void apply_state()=0
GPUStateMutable mutable_state
std::array< TextureWriteFormat, GPU_MAX_IMAGE > image_formats
eGPUTextureUsage usage_get() const
CCL_NAMESPACE_BEGIN struct Options options
constexpr T clamp(T, U, U) RET
#define MTL_MAX_SAMPLER_SLOTS
#define MTL_MAX_TEXTURE_SLOTS
#define MTL_MAX_DRAWABLES
#define MTL_FRAME_AVERAGE_COUNT
#define MTL_LOG_WARNING(info,...)
#define MTL_LOG_DEBUG(info,...)
#define MTL_LOG_ERROR(info,...)
#define MTL_SAMPLER_ARGUMENT_BUFFER_SLOT
#define MTL_ACCELERATION_STRUCTURE_SLOT
#define MTL_PUSH_CONSTANT_BUFFER_SLOT
#define MTL_UBO_SLOT_OFFSET
#define MTL_IMAGE_SLOT_OFFSET
#define MTL_SAMPLER_SLOT_OFFSET
#define MTL_SSBO_SLOT_OFFSET
OneBitIteratorRange iter_1_indices(BitInt value)
void free(Subdiv *subdiv)
void present(MTLRenderPassDescriptor *blit_descriptor, id< MTLRenderPipelineState > blit_pso, id< MTLTexture > swapchain_texture, id< CAMetalDrawable > drawable)
static void ensure_buffer_bindings(MTLContext &ctx, MTLShader &shader, CommandEncoderT enc, MTLBindingCache< CommandEncoderT > &bindings, uint32_t stage_buf_mask=uint32_t(-1))
static void ensure_push_constant(MTLContext &ctx, MTLShader &shader, CommandEncoderT enc, MTLBindingCache< CommandEncoderT > &bindings, uint32_t stage_buf_mask=uint32_t(-1))
static void ensure_texture_bindings(MTLContext &ctx, MTLShader &shader, CommandEncoderT enc, MTLBindingCache< CommandEncoderT > &bindings, id< MTLFunction > mtl_function, uint16_t stage_ima_mask=uint16_t(-1), uint64_t stage_tex_mask=uint64_t(-1))
static void bind_atomic_workaround_buffer(StringRefNull texture_name, MTLTexture *gpu_tex, MTLShaderInterface &shader_interface, MTLShader &active_shader)
static void bind_sampler_argument_buffer(CommandEncoderT enc, MTLSamplerArray &sampler_array, Map< MTLSamplerArray, gpu::MTLBuffer * > &sampler_buffers_cache, MTLShaderInterface &shader_interface, id< MTLFunction > mtl_function, MTLBindingCache< CommandEncoderT > &bindings)
const MTLSamplerState DEFAULT_SAMPLER_STATE
@ MTL_PIPELINE_STATE_CULLMODE_FLAG
@ MTL_PIPELINE_STATE_SCISSOR_FLAG
@ MTL_PIPELINE_STATE_FRONT_FACING_FLAG
@ MTL_PIPELINE_STATE_DEPTHSTENCIL_FLAG
@ MTL_PIPELINE_STATE_VIEWPORT_FLAG
@ MTL_PIPELINE_STATE_ALL_FLAG
static MTLSamplerAddressMode to_mtl_type(GPUSamplerExtendMode wrap_mode)
void xr_blit(id< MTLTexture > metal_xr_texture, int ofsx, int ofsy, int width, int height)
MINLINE float min_ff(float a, float b)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
gpu::Texture * GPU_texture_create_cube_array(const char *name, int width, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
unsigned __int64 uint64_t
gpu::Texture * GPU_texture_create_1d(const char *name, int width, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
bool assign_if_different(T &old_value, T &&new_value)
BLI_INLINE float fb(float length, float L)
void BLI_time_sleep_ms(int ms)
@ GPU_SAMPLER_EXTEND_MODE_MIRRORED_REPEAT
@ GPU_SAMPLER_EXTEND_MODE_CLAMP_TO_BORDER
@ GPU_SAMPLER_EXTEND_MODE_REPEAT
@ GPU_SAMPLER_EXTEND_MODE_EXTEND
void GPU_texture_unbind_all()
gpu::Texture * GPU_texture_create_2d_array(const char *name, int width, int height, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
@ GPU_SAMPLER_FILTERING_LINEAR
@ GPU_SAMPLER_FILTERING_MIPMAP
MINLINE uint max_uu(uint a, uint b)
MINLINE int max_ii(int a, int b)
ATTR_WARN_UNUSED_RESULT const BMVert * v
@ GPU_TEXTURE_USAGE_GENERAL
@ GPU_TEXTURE_USAGE_ATOMIC
gpu::Texture * GPU_texture_create_from_vertbuf(const char *name, gpu::VertBuf *vertex_buf)
BMesh const char void * data
gpu::Texture * GPU_texture_create_3d(const char *name, int width, int height, int depth, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const void *data)
gpu::VertBuf * GPU_vertbuf_create_with_format_ex(const GPUVertFormat &format, GPUUsageType usage)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
gpu::Texture * GPU_texture_create_cube(const char *name, int width, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_shader_uniform_1f(gpu::Shader *sh, const char *name, float value)
@ GPU_SAMPLER_CUSTOM_ICON
@ GPU_SAMPLER_CUSTOM_COMPARE
void GPU_texture_free(gpu::Texture *texture)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
@ GPU_SAMPLER_STATE_TYPE_CUSTOM
@ GPU_SAMPLER_STATE_TYPE_INTERNAL
void GPU_vertformat_clear(GPUVertFormat *)
MINLINE unsigned int bitscan_reverse_uint64(unsigned long long a)
void GPU_uniformbuf_unbind(gpu::UniformBuf *ubo)
void GPU_vertbuf_discard(gpu::VertBuf *)
MINLINE int min_ii(int a, int b)
@ GPU_USAGE_FLAG_BUFFER_TEXTURE_ONLY
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
gpu::Texture * GPU_texture_create_1d_array(const char *name, int width, int layer_len, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
GPUSamplerExtendMode extend_yz
GPUSamplerFiltering filtering
GPUSamplerCustomType custom_type
GPUSamplerExtendMode extend_x
static constexpr GPUSamplerState default_sampler()
gpu::MTLTexture * texture
void bind_buffer(CommandEncoderT enc, id< MTLBuffer > buf, size_t offset, uint index)
void bind_texture(CommandEncoderT enc, id< MTLTexture > tex, uint index)
void bind_sampler(CommandEncoderT enc, gpu::MTLSamplerArray &sampler_array, id< MTLSamplerState > sampler_state, gpu::MTLSamplerState binding_state, bool use_samplers_argument_buffer, uint index)
void bind_bytes(CommandEncoderT enc, MTLScratchBufferManager &scratch_buffer, const void *bytes, size_t length, uint index)
id< MTLFunction > compute
id< MTLComputePipelineState > pso
MTLCompareFunction depth_function
MTLStencilOperation stencil_op_back_depthstencil_pass
MTLCompareFunction stencil_func
MTLStencilOperation stencil_op_back_stencil_fail
MTLStencilOperation stencil_op_front_stencil_fail
MTLStencilOperation stencil_op_front_depthstencil_pass
MTLStencilOperation stencil_op_front_depth_fail
MTLStencilOperation stencil_op_back_depth_fail
bool stencil_test_enabled
std::array< MTLStorageBufferBinding, MTL_MAX_SSBO > ssbo_bindings
std::array< MTLTextureBinding, MTL_MAX_SAMPLER_SLOTS > texture_bindings
MTLColorWriteMask color_write_mask
MTLBlendFactor src_rgb_blend_factor
MTLBlendOperation alpha_blend_op
MTLBlendFactor src_alpha_blend_factor
MTLShader * active_shader
MTLPipelineStateDirtyFlag dirty_flags
MTLBlendFactor dest_alpha_blend_factor
int viewport_width[GPU_MAX_VIEWPORTS]
MTLContextDepthStencilState depth_stencil_state
int viewport_height[GPU_MAX_VIEWPORTS]
std::array< MTLAccelerationStructureBinding, 1 > acceleration_structure_bindings
int viewport_offset_x[GPU_MAX_VIEWPORTS]
MTLBlendOperation rgb_blend_op
GPUFaceCullTest cull_mode
std::array< MTLUniformBufferBinding, MTL_MAX_UBO > ubo_bindings
int viewport_offset_y[GPU_MAX_VIEWPORTS]
std::array< MTLSamplerBinding, MTL_MAX_SAMPLER_SLOTS > sampler_bindings
MTLBlendFactor dest_rgb_blend_factor
std::array< MTLTextureBinding, MTL_MAX_IMAGE_SLOTS > image_bindings
int null_attribute_buffer_index
id< MTLRenderPipelineState > pso
uint16_t used_ima_vert_mask
uint64_t used_tex_vert_mask
uint16_t used_ima_frag_mask
uint64_t used_tex_frag_mask
uint32_t used_buf_frag_mask
uint32_t used_buf_vert_mask
id< MTLSamplerState > mtl_sampler[MTL_MAX_TEXTURE_SLOTS]
gpu::MTLSamplerState state
gpu::MTLTexture * texture_resource
Vector< SpecializationConstant::Value, 8 > values