Blender V5.3
mtl_context.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8#pragma once
9
11
12#include "GPU_common_types.hh"
13
14/* Don't generate OpenGL deprecation warning. This is a known thing, and is not something easily
15 * solvable in a short term. */
16#ifdef __clang__
17# pragma clang diagnostic ignored "-Wdeprecated-declarations"
18#endif
19
20#include "GHOST_Types.hh"
24
25#include "mtl_backend.hh"
26#include "mtl_capabilities.hh"
27#include "mtl_command_buffer.hh"
28#include "mtl_common.hh"
29#include "mtl_framebuffer.hh"
30#include "mtl_memory.hh"
31#include "mtl_shader.hh"
34#include "mtl_texture.hh"
35
36#include <Cocoa/Cocoa.h>
37#include <Metal/Metal.h>
38#include <QuartzCore/QuartzCore.h>
39#include <chrono>
40#include <mutex>
41
42@class CAMetalLayer;
43@class MTLCommandQueue;
44@class MTLRenderPipelineState;
45
46namespace blender::gpu {
47
48/* Forward Declarations */
49class MTLContext;
51class MTLUniformBuf;
52class MTLStorageBuf;
53class MTLTopLevelAS;
54
55/* Caching of resource bindings for active MTLRenderCommandEncoder.
56 * In Metal, resource bindings are local to the MTLCommandEncoder,
57 * not globally to the whole pipeline/cmd buffer. */
59 MTLShader *shader = nullptr;
61
62 bool operator==(MTLBoundShaderState const &other) const
63 {
64 return (shader == other.shader && pso_index == other.pso_index);
65 }
66 bool operator!=(MTLBoundShaderState const &other) const
67 {
68 return !(*this == other);
69 }
70};
71
72/* Metal Context Render Pass State -- Used to track active RenderCommandEncoder state based on
73 * bound MTLFrameBuffer's.Owned by MTLContext. */
75 friend class MTLContext;
76
77 public:
78 MTLRenderPassState(MTLContext &context, MTLCommandBufferManager &command_buffer_manager)
79 : ctx(context), cmd(command_buffer_manager) {};
80
81 /* Given a RenderPassState is associated with a live RenderCommandEncoder,
82 * this state sits within the MTLCommandBufferManager. */
85
90 MTLScissorRect last_scissor_rect;
91
94
95 /* Reset RenderCommandEncoder binding state. */
96 void reset_state();
97
98 /* Texture Binding (RenderCommandEncoder). */
101
102 /* Sampler Binding (RenderCommandEncoder). */
103 void bind_vertex_sampler(MTLSamplerBinding &sampler_binding,
104 bool use_samplers_argument_buffer,
105 uint slot);
106 void bind_fragment_sampler(MTLSamplerBinding &sampler_binding,
107 bool use_samplers_argument_buffer,
108 uint slot);
109
110 /* Buffer binding (RenderCommandEncoder). */
111 void bind_vertex_buffer(id<MTLBuffer> buffer, uint64_t buffer_offset, uint index);
112 void bind_fragment_buffer(id<MTLBuffer> buffer, uint64_t buffer_offset, uint index);
113 void bind_vertex_bytes(const void *bytes, uint64_t length, uint index);
114 void bind_fragment_bytes(const void *bytes, uint64_t length, uint index);
115};
116
117/* Metal Context Compute Pass State -- Used to track active ComputeCommandEncoder state. */
119 friend class MTLContext;
120
121 public:
122 MTLComputeState(MTLContext &context, MTLCommandBufferManager &command_buffer_manager)
123 : ctx(context), cmd(command_buffer_manager) {};
124
125 /* Given a ComputePassState is associated with a live ComputeCommandEncoder,
126 * this state sits within the MTLCommandBufferManager. */
129
131
133
134 /* Reset ComputeCommandEncoder binding state. */
135 void reset_state();
136
137 /* PSO Binding. */
139
141 void bind_compute_sampler(MTLSamplerBinding &sampler_binding,
142 bool use_samplers_argument_buffer,
143 uint slot);
144 void bind_compute_buffer(id<MTLBuffer> buffer, uint64_t buffer_offset, uint index);
145 void bind_compute_bytes(const void *bytes, uint64_t length, uint index);
146};
147
148/* Depth Stencil State */
150
151 /* Depth State. */
154 MTLCompareFunction depth_function;
155
156 /* Stencil State. */
161 MTLCompareFunction stencil_func;
162
163 MTLStencilOperation stencil_op_front_stencil_fail;
164 MTLStencilOperation stencil_op_front_depth_fail;
166
167 MTLStencilOperation stencil_op_back_stencil_fail;
168 MTLStencilOperation stencil_op_back_depth_fail;
170
171 /* Frame-buffer State -- We need to mark this, in case stencil state remains unchanged,
172 * but attachment state has changed. */
175
176 /* TODO(Metal): Consider optimizing this function using `memcmp`.
177 * Un-used, but differing, stencil state leads to over-generation
178 * of state objects when doing trivial compare. */
180 {
181 bool depth_state_equality = (has_depth_target == other.has_depth_target &&
185
186 bool stencil_state_equality = true;
187 if (has_stencil_target) {
188 stencil_state_equality =
199 }
200
201 return depth_state_equality && stencil_state_equality;
202 }
203
204 /* Depth stencil state will get hashed in order to prepare
205 * MTLDepthStencilState objects. The hash should comprise of
206 * all elements which fill the MTLDepthStencilDescriptor.
207 * These are bound when [rec setDepthStencilState:...] is called.
208 * Depth bias and stencil reference value are set dynamically on the RenderCommandEncoder:
209 * - setStencilReferenceValue:
210 * - setDepthBias:slopeScale:clamp:
211 */
212 std::size_t hash() const
213 {
214 std::size_t boolean_bitmask = (this->depth_write_enable ? 1 : 0) |
215 ((this->depth_test_enabled ? 1 : 0) << 1) |
216 ((this->stencil_test_enabled ? 1 : 0) << 2) |
217 ((this->has_depth_target ? 1 : 0) << 3) |
218 ((this->has_stencil_target ? 1 : 0) << 4);
219
220 std::size_t stencilop_bitmask = (std::size_t(this->stencil_op_front_stencil_fail)) |
221 (std::size_t(this->stencil_op_front_depth_fail) << 3) |
222 (std::size_t(this->stencil_op_front_depthstencil_pass) << 6) |
223 (std::size_t(this->stencil_op_back_stencil_fail) << 9) |
224 (std::size_t(this->stencil_op_back_depth_fail) << 12) |
225 (std::size_t(this->stencil_op_back_depthstencil_pass) << 15);
226
227 std::size_t main_hash = std::size_t(this->depth_function);
228 if (this->has_stencil_target) {
229 main_hash += std::size_t(this->stencil_read_mask & 0xFF) << 8;
230 main_hash += std::size_t(this->stencil_write_mask & 0xFF) << 16;
231 }
232 main_hash ^= std::size_t(this->stencil_func) << 16;
233 main_hash ^= stencilop_bitmask;
234
235 std::size_t final_hash = (main_hash << 8) | boolean_bitmask;
236 return final_hash;
237 }
238};
239
241
242 /* Depth Update Utilities */
243 /* Depth texture updates are not directly supported with Blit operations, similarly, we cannot
244 * use a compute shader to write to depth, so we must instead render to a depth target.
245 * These processes use vertex/fragment shaders to render texture data from an intermediate
246 * source, in order to prime the depth buffer. */
249
250 /* Texture Read/Update routines */
264
281
282 template<typename T> void free_cached_pso_map(Map<T, id<MTLComputePipelineState>> &map)
283 {
284 for (typename blender::MutableMapItem<T, id<MTLComputePipelineState>> item : map.items()) {
285 [item.value release];
286 }
287 map.clear();
288 }
289
290 void init()
291 {
292 fullscreen_blit_shader = nullptr;
293 }
294
326};
327
329 private:
330 id<MTLComputePipelineState> buffer_clear_pso_ = nil;
331
332 public:
334 void cleanup()
335 {
336 if (buffer_clear_pso_) {
337 [buffer_clear_pso_ release];
338 buffer_clear_pso_ = nil;
339 }
340 }
341};
342
345 /* Whether we need to call setViewport. */
347 /* Whether we need to call setScissor. */
349 /* Whether we need to update/rebind active depth stencil state. */
351 /* Whether we need to update/rebind active PSO. */
353 /* Whether we need to update the frontFacingWinding state. */
355 /* Whether we need to update the culling state. */
357 /* Full pipeline state needs applying. Occurs when beginning a new render pass. */
362};
363
365
367 bool bound = false;
368 MTLUniformBuf *ubo = nullptr;
369};
370
372 bool bound = false;
373 MTLStorageBuf *ssbo = nullptr;
374};
375
377 bool bound = false;
378 MTLTopLevelAS *tlas = nullptr;
379};
380
382 /* Whether the pipeline state has been modified since application.
383 * `dirty_flags` is a bitmask of the types of state which have been updated.
384 * This is in order to optimize calls and only re-apply state as needed.
385 * Some state parameters are dynamically applied on the RenderCommandEncoder,
386 * others may be encapsulated in GPU-resident state objects such as
387 * MTLDepthStencilState or MTLRenderPipelineState. */
388 bool dirty = true;
390
391 /* Active Shader State. */
393
394 /* Global Uniform Buffers. */
395 std::array<MTLUniformBufferBinding, MTL_MAX_UBO> ubo_bindings = {};
396 /* Storage buffer. */
397 std::array<MTLStorageBufferBinding, MTL_MAX_SSBO> ssbo_bindings = {};
398 /* Acceleration structure (single slot). */
399 std::array<MTLAccelerationStructureBinding, 1> acceleration_structure_bindings = {};
400 /* Context Texture bindings. */
401 std::array<MTLTextureBinding, MTL_MAX_SAMPLER_SLOTS> texture_bindings = {};
402 std::array<MTLSamplerBinding, MTL_MAX_SAMPLER_SLOTS> sampler_bindings = {};
403 /* Image bindings. */
404 std::array<MTLTextureBinding, MTL_MAX_IMAGE_SLOTS> image_bindings = {};
405
406 /*** --- Render Pipeline State --- ***/
407 /* Track global render pipeline state for the current context. The functions in GPU_state.hh
408 * modify these parameters. Certain values, tagged [PSO], are parameters which are required to be
409 * passed into PSO creation, rather than dynamic state functions on the RenderCommandEncoder.
410 */
411
412 /* Blending State. */
413 MTLColorWriteMask color_write_mask; /* [PSO] */
414 bool blending_enabled; /* [PSO] */
415 MTLBlendOperation alpha_blend_op; /* [PSO] */
416 MTLBlendOperation rgb_blend_op; /* [PSO] */
417 MTLBlendFactor dest_alpha_blend_factor; /* [PSO] */
418 MTLBlendFactor dest_rgb_blend_factor; /* [PSO] */
419 MTLBlendFactor src_alpha_blend_factor; /* [PSO] */
420 MTLBlendFactor src_rgb_blend_factor; /* [PSO] */
421
422 /* Culling State. */
426
427 /* Depth State. */
429
430 /* Viewport/Scissor Region. */
432 int viewport_offset_x[GPU_MAX_VIEWPORTS];
433 int viewport_offset_y[GPU_MAX_VIEWPORTS];
434 int viewport_width[GPU_MAX_VIEWPORTS];
435 int viewport_height[GPU_MAX_VIEWPORTS];
441
442 /* Render parameters. */
443 float point_size = 1.0f;
444 float line_width = 1.0f;
445
446 /* Clipping plane enablement. */
447 bool clip_distance_enabled[6] = {false};
448};
449
450/* Command Buffer Manager - Owned by MTLContext.
451 * The MTLCommandBufferManager represents all work associated with
452 * a command buffer of a given identity. This manager is a fixed-state
453 * on the context, which coordinates the lifetime of command buffers
454 * for particular categories of work.
455 *
456 * This ensures operations on command buffers, and the state associated,
457 * is correctly tracked and managed. Workload submission and MTLCommandEncoder
458 * coordination is managed from here.
459 *
460 * There is currently only one MTLCommandBufferManager for managing submission
461 * of the "main" rendering commands. A secondary upload command buffer track,
462 * or asynchronous compute command buffer track may be added in the future. */
464 friend class MTLContext;
465
466 public:
467 /* Counter for all active command buffers. */
468 static volatile std::atomic<int> num_active_cmd_bufs_in_system;
469
470 private:
471 /* Associated Context and properties. */
472 MTLContext &context_;
473 bool supports_render_ = false;
474
475 /* CommandBuffer tracking. */
476 id<MTLCommandBuffer> active_command_buffer_ = nil;
477 id<MTLCommandBuffer> last_submitted_command_buffer_ = nil;
478 volatile std::atomic<int> num_active_cmd_bufs = 0;
479 /* Safe free list referenced by the active command buffer from its creation until it has
480 * completed, so that buffers freed while it is encoded aren't reused before it is executed. */
481 MTLSafeFreeList *active_safe_free_list_ = nullptr;
482
483 /* Active MTLCommandEncoders. */
484 enum {
485 MTL_NO_COMMAND_ENCODER = 0,
486 MTL_RENDER_COMMAND_ENCODER = 1,
487 MTL_BLIT_COMMAND_ENCODER = 2,
488 MTL_COMPUTE_COMMAND_ENCODER = 3,
489 MTL_ACCELERATION_STRUCTURE_COMMAND_ENCODER = 4
490 } active_command_encoder_type_ = MTL_NO_COMMAND_ENCODER;
491
492 id<MTLRenderCommandEncoder> active_render_command_encoder_ = nil;
493 id<MTLBlitCommandEncoder> active_blit_command_encoder_ = nil;
494 id<MTLComputeCommandEncoder> active_compute_command_encoder_ = nil;
495 id<MTLAccelerationStructureCommandEncoder> active_acceleration_structure_command_encoder_ = nil;
496
497 /* State associated with active RenderCommandEncoder. */
498 MTLRenderPassState render_pass_state_;
499 MTLFrameBuffer *active_frame_buffer_ = nullptr;
500 MTLRenderPassDescriptor *active_pass_descriptor_ = nullptr;
501
502 /* State associated with active ComputeCommandEncoder. */
503 MTLComputeState compute_state_;
504
505 /* Workload heuristics - We may need to split command buffers to optimize workload and balancing.
506 */
507 int current_draw_call_count_ = 0;
508 int encoder_count_ = 0;
509 int vertex_submitted_count_ = 0;
510 bool empty_ = true;
511
513 /* Copy of the debug stack to keep track of which group have been pushed to the debug layers.
514 * This is needed because we do JIT push and pop the debug groups to better accommodate the
515 * Metal API structure. */
516 DebugStack mtl_debug_stack_;
517
518 public:
520 : context_(context), render_pass_state_(context, *this), compute_state_(context, *this) {};
521 void prepare(bool supports_render = true);
522
523 /* If wait is true, CPU will stall until GPU work has completed. */
524 bool submit(bool wait);
525
526 /* Fetch/query current encoder. */
528 bool is_inside_blit();
529 bool is_inside_compute();
534
535 /* RenderPassState for RenderCommandEncoder. */
537 {
538 /* Render pass state should only be valid if we are inside a render pass. */
540 return render_pass_state_;
541 }
542
543 /* RenderPassState for RenderCommandEncoder. */
545 {
546 /* Render pass state should only be valid if we are inside a compute encoder. */
548 return compute_state_;
549 }
550
551 /* Rendering Heuristics. */
552 void register_draw_counters(int vertex_submission);
553 void reset_counters();
554 bool do_break_submission();
555
556 /* Encoder and Pass management. */
557 /* End currently active MTLCommandEncoder. */
558 bool end_active_command_encoder(bool retain_framebuffers = false);
560 bool force_begin,
561 bool *r_new_pass);
565
566 /* Workload Synchronization. */
567 bool insert_memory_barrier(GPUBarrier barrier_bits,
568 GPUStageBarrierBits before_stages,
569 GPUStageBarrierBits after_stages);
570 void encode_signal_event(id<MTLEvent> event, uint64_t value);
572 /* TODO(Metal): Support fences in command buffer class. */
573
574 /* Debug. */
575 void push_debug_group(const char *name, int index);
576 void pop_debug_group();
577
579 {
581 num_active_cmd_bufs++;
582 }
583
585 {
586 BLI_assert(num_active_cmd_bufs_in_system > 0 && num_active_cmd_bufs > 0);
588 num_active_cmd_bufs--;
589 }
590
592 {
593 return num_active_cmd_bufs;
594 }
595
597 {
599 std::this_thread::yield();
600 }
601 }
602
603 /* Debug group management. To be called before any low-level `pushDebugGroup`. */
605
606 private:
607 /* Begin new command buffer. */
608 id<MTLCommandBuffer> ensure_begin();
609
610 void register_encoder_counters();
611 void fold_remaining_debug_groups();
612};
613
620class MTLContext : public Context {
621 friend class MTLBackend;
622 friend class MTLRenderPassState;
623 friend class MTLComputeState;
624
625 public:
626 /* Swap-chain and latency management. */
627 static std::atomic<int> max_drawables_in_flight;
628 static std::atomic<int64_t> avg_drawable_latency_us;
630
631 public:
632 /* Shaders and Pipeline state. */
634
635 /* Metal API Resource Handles. */
638
639#ifndef NDEBUG
640 /* Label for Context debug name assignment. */
641 NSString *label = nil;
642#endif
643
644 /* Memory Management. */
649
650 /* CommandBuffer managers. */
652
653 private:
654 /* Parent Context. */
655 GHOST_ContextMTL *ghost_context_;
656
657 /* Render Passes and Frame-buffers. */
658 id<MTLTexture> default_fbo_mtltexture_ = nil;
659 gpu::MTLTexture *default_fbo_gputexture_ = nullptr;
660
661 /* Depth-stencil state cache. */
663
664 /* Compute and specialization caches. */
665 MTLContextTextureUtils texture_utils_;
666 MTLContextComputeUtils compute_utils_;
667
668 /* Texture Samplers. */
669 /* Cache of generated #MTLSamplerState objects based on permutations of the members of
670 * `GPUSamplerState`. */
673 [GPU_SAMPLER_FILTERING_TYPES_COUNT];
674 id<MTLSamplerState> custom_sampler_state_cache_[GPU_SAMPLER_CUSTOM_TYPES_COUNT];
675 id<MTLSamplerState> default_sampler_state_ = nil;
676
677 /* When texture sampler count exceeds the resource bind limit, an
678 * argument buffer is used to pass samplers to the shader.
679 * Each unique configurations of multiple samplers can be cached, so as to not require
680 * re-generation. `samplers_` stores the current list of bound sampler objects.
681 * `cached_sampler_buffers_` is a cache of encoded argument buffers which can be re-used. */
682 MTLSamplerArray samplers_;
683 Map<MTLSamplerArray, gpu::MTLBuffer *> cached_sampler_buffers_;
684
685 /* Frame. */
686 bool is_inside_frame_ = false;
687 uint current_frame_index_;
688
689 /* Visibility buffer for MTLQuery results. */
690 gpu::MTLBuffer *visibility_buffer_ = nullptr;
691 bool visibility_is_dirty_ = false;
692
693 /* Null buffers for empty/uninitialized bindings.
694 * Null attribute buffer follows default attribute format of OpenGL Backend. */
695 id<MTLBuffer> null_buffer_; /* All zero's. */
696 id<MTLBuffer> null_attribute_buffer_; /* Value float4(0.0,0.0,0.0,1.0). */
697
699 /* Maximum of 32 texture types. Though most combinations invalid. */
700 gpu::MTLTexture *dummy_textures_[GPU_SAMPLER_TYPE_MAX][GPU_TEXTURE_BUFFER] = {{nullptr}};
701 GPUVertFormat dummy_vertformat_[GPU_SAMPLER_TYPE_MAX];
702 VertBuf *dummy_verts_[GPU_SAMPLER_TYPE_MAX] = {nullptr};
703
704 /* Debug scope timings. Adapted form GLContext::TimeQuery.
705 * Only supports CPU timings for now. */
706 struct ScopeTimings {
707 using Clock = std::chrono::steady_clock;
708 using TimePoint = Clock::time_point;
709 using Nanoseconds = std::chrono::nanoseconds;
710
711 static TimePoint epoch;
712
713 std::string name;
714 bool finished;
715 TimePoint cpu_start, cpu_end;
716 };
717 Vector<ScopeTimings> scope_timings;
718
719 void process_frame_timings();
720
721 public:
722 /* GPUContext interface. */
723 MTLContext(GHOST_IWindow *ghost_window, GHOST_IContext *ghost_context);
724 ~MTLContext();
725
726 static void check_error(const char *info);
727
728 void activate() override;
729 void deactivate() override;
730 void begin_frame() override;
731 void end_frame() override;
732
733 void flush() override;
734 void finish() override;
735
736 void memory_statistics_get(int *r_total_mem, int *r_free_mem) override;
737
738 static MTLContext *get()
739 {
740 return static_cast<MTLContext *>(Context::get());
741 }
742
743 void debug_group_begin(const char *name, int index) override;
744 void debug_group_end() override;
745 bool debug_capture_begin(const char *title) override;
746 void debug_capture_end() override;
747 void *debug_capture_scope_create(const char *name) override;
748 bool debug_capture_scope_begin(void *scope) override;
749 void debug_capture_scope_end(void *scope) override;
750
751 void debug_unbind_all_ubo() override {};
752 void debug_unbind_all_ssbo() override {};
753
754 /*** MTLContext Utility functions. */
755 /*
756 * All below functions modify the global state for the context, controlling the flow of
757 * rendering, binding resources, setting global state, resource management etc;
758 */
759
761
762 /* Bind frame-buffer to context. */
763 void framebuffer_bind(MTLFrameBuffer *framebuffer);
764
765 /* Restore frame-buffer used by active context to default back-buffer. */
766 void framebuffer_restore();
767
768 /* Ensure a render-pass using the Context frame-buffer (active_fb_) is in progress. */
770
773
774 /* Context Global-State Texture Binding. */
775 void texture_bind(gpu::MTLTexture *mtl_texture, uint texture_unit, bool is_image);
776 void sampler_bind(MTLSamplerState, uint sampler_unit);
777 void texture_unbind(gpu::MTLTexture *mtl_texture, bool is_image, StateManager *state_manager);
778 void texture_unbind_all(bool is_image);
782
783 /* Active shader specialization constants state. */
785
787
788 /* Metal Context pipeline state. */
789 void pipeline_state_init();
791
792 /* These functions ensure that the current RenderCommandEncoder has
793 * the correct global state assigned. This should be called prior
794 * to every draw call, to ensure that all state is applied and up
795 * to date. We handle:
796 *
797 * - Buffer bindings (Vertex buffers, Uniforms, UBOs)
798 * - Texture bindings
799 * - Sampler bindings (+ argument buffer bindings)
800 * - Dynamic Render pipeline state (on encoder)
801 * - Baking Pipeline State Objects (PSOs) for current shader, based
802 * on final pipeline state.
803 *
804 * `ensure_render_pipeline_state` will return false if the state is
805 * invalid and cannot be applied. This should cancel a draw call. */
806 bool ensure_render_pipeline_state(MTLPrimitiveType prim_type);
808
813
814 /* Compute. */
815 /* Ensure compute pipeline state for current config is compiled and return PSO instance. */
817 void compute_dispatch(int groups_x_len, int groups_y_len, int groups_z_len);
818 void compute_dispatch_indirect(StorageBuf *indirect_buf);
819
820 /* State assignment. */
821 void set_viewport(int origin_x, int origin_y, int width, int height);
822 void set_viewports(int count, const int (&viewports)[GPU_MAX_VIEWPORTS][4]);
823 void set_scissor(int scissor_x, int scissor_y, int scissor_width, int scissor_height);
824 void set_scissor_enabled(bool scissor_enabled);
825
826 /* Visibility buffer control. */
829
830 /* Flag whether the visibility buffer for query results
831 * has changed. This requires a new RenderPass in order
832 * to update. */
833 bool is_visibility_dirty() const;
834
835 /* Reset dirty flag state for visibility buffer. */
837
838 /* Texture utilities. */
840 {
841 return texture_utils_;
842 }
843
844 /* Compute utilities. */
846 {
847 return compute_utils_;
848 }
849
851 {
852 return is_active_;
853 }
854
856 {
857 return is_inside_frame_;
858 }
859
861 {
862 return current_frame_index_;
863 }
864
866 {
867 return this->memory_manager;
868 }
869
880
894
900
902 {
903 return samplers_;
904 }
905
907 {
908 return cached_sampler_buffers_;
909 }
910
911 /* Swap-chain and latency management. */
912 static void latency_resolve_average(int64_t frame_latency_us)
913 {
914 int64_t avg = 0;
915 int64_t frame_c = 0;
916 for (int i = MTL_FRAME_AVERAGE_COUNT - 1; i > 0; i--) {
919 frame_c += (MTLContext::frame_latency[i] > 0) ? 1 : 0;
920 }
921 MTLContext::frame_latency[0] = frame_latency_us;
923 if (frame_c > 0) {
924 avg /= frame_c;
925 }
926 else {
927 avg = 0;
928 }
930 }
931
932 private:
933 void set_ghost_context(GHOST_IContext *ghostCtxHandle);
934 void set_ghost_window(GHOST_IWindow *ghostWinHandle);
935};
936
937/* GHOST Context callbacks. */
938void present(MTLRenderPassDescriptor *blit_descriptor,
940 id<MTLTexture> swapchain_texture,
941 id<CAMetalDrawable> drawable);
942
943void xr_blit(id<MTLTexture> metal_xr_texture, int ofsx, int ofsy, int width, int height);
944
945} // namespace blender::gpu
#define GPU_SAMPLER_CUSTOM_TYPES_COUNT
#define GPU_SAMPLER_EXTEND_MODES_COUNT
void activate(bool forceActivation=false) const
void clear()
Definition BLI_map.hh:1038
ItemIterator items() const &
Definition BLI_map.hh:902
static Context * get()
bool end_active_command_encoder(bool retain_framebuffers=false)
void encode_signal_event(id< MTLEvent > event, uint64_t value)
id< MTLComputeCommandEncoder > get_active_compute_command_encoder()
MTLCommandBufferManager(MTLContext &context)
void register_draw_counters(int vertex_submission)
void prepare(bool supports_render=true)
MTLRenderPassState & get_render_pass_state()
id< MTLRenderCommandEncoder > ensure_begin_render_command_encoder(MTLFrameBuffer *ctx_framebuffer, bool force_begin, bool *r_new_pass)
id< MTLComputeCommandEncoder > ensure_begin_compute_encoder()
bool insert_memory_barrier(GPUBarrier barrier_bits, GPUStageBarrierBits before_stages, GPUStageBarrierBits after_stages)
id< MTLBlitCommandEncoder > ensure_begin_blit_encoder()
void push_debug_group(const char *name, int index)
id< MTLAccelerationStructureCommandEncoder > ensure_begin_acceleration_structure_encoder()
id< MTLRenderCommandEncoder > get_active_render_command_encoder()
static volatile std::atomic< int > num_active_cmd_bufs_in_system
void encode_wait_for_event(id< MTLEvent > event, uint64_t value)
id< MTLBlitCommandEncoder > get_active_blit_command_encoder()
MTLComputeState(MTLContext &context, MTLCommandBufferManager &command_buffer_manager)
void bind_compute_bytes(const void *bytes, uint64_t length, uint index)
id< MTLComputePipelineState > bound_pso
MTLCommandBufferManager & cmd
void bind_compute_texture(id< MTLTexture > tex, uint slot)
MTLBindingCache< gpu::MTLComputeCommandEncoder > compute_bindings
void bind_compute_buffer(id< MTLBuffer > buffer, uint64_t buffer_offset, uint index)
void bind_compute_sampler(MTLSamplerBinding &sampler_binding, bool use_samplers_argument_buffer, uint slot)
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()
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 debug_unbind_all_ubo() override
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
MTLContextGlobalShaderPipelineState pipeline_state
bool debug_capture_begin(const char *title) override
Definition mtl_debug.mm:127
void * debug_capture_scope_create(const char *name) override
Definition mtl_debug.mm:162
void debug_capture_scope_end(void *scope) override
Definition mtl_debug.mm:191
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 set_viewports(int count, const int(&viewports)[GPU_MAX_VIEWPORTS][4])
void compute_dispatch_indirect(StorageBuf *indirect_buf)
void debug_group_begin(const char *name, int index) override
Definition mtl_debug.mm:52
static void check_error(const char *info)
void set_scissor(int scissor_x, int scissor_y, int scissor_width, int scissor_height)
bool debug_capture_scope_begin(void *scope) override
Definition mtl_debug.mm:177
MTLCommandBufferManager main_command_buffer
void debug_group_end() override
Definition mtl_debug.mm:70
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
Map< MTLSamplerArray, gpu::MTLBuffer * > & get_sampler_arg_buf_cache()
id< MTLCommandQueue > queue
gpu::MTLTexture * get_dummy_texture(GPUTextureType type, GPUSamplerFormat sampler_format)
void debug_unbind_all_ssbo() override
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()
MTLContext(GHOST_IWindow *ghost_window, GHOST_IContext *ghost_context)
void debug_capture_end() override
Definition mtl_debug.mm:152
static std::atomic< int > max_drawables_in_flight
MTLRenderPassState(MTLContext &context, MTLCommandBufferManager &command_buffer_manager)
void bind_fragment_bytes(const void *bytes, uint64_t length, uint index)
void bind_vertex_buffer(id< MTLBuffer > buffer, uint64_t buffer_offset, uint index)
MTLBindingCache< gpu::MTLVertexCommandEncoder > vertex_bindings
void bind_vertex_texture(id< MTLTexture > tex, uint slot)
void bind_vertex_bytes(const void *bytes, uint64_t length, uint index)
id< MTLDepthStencilState > bound_ds_state
MTLCommandBufferManager & cmd
MTLBindingCache< gpu::MTLFragmentCommandEncoder > fragment_bindings
void bind_vertex_sampler(MTLSamplerBinding &sampler_binding, bool use_samplers_argument_buffer, uint slot)
id< MTLRenderPipelineState > bound_pso
void bind_fragment_sampler(MTLSamplerBinding &sampler_binding, bool use_samplers_argument_buffer, uint slot)
MTLBoundShaderState last_bound_shader_state
void bind_fragment_texture(id< MTLTexture > tex, uint slot)
void bind_fragment_buffer(id< MTLBuffer > buffer, uint64_t buffer_offset, uint index)
float length(VecOp< float, D >) RET
const int state
#define T
#define MTL_FRAME_AVERAGE_COUNT
Definition mtl_common.hh:13
std::chrono::high_resolution_clock Clock
void present(MTLRenderPassDescriptor *blit_descriptor, id< MTLRenderPipelineState > blit_pso, id< MTLTexture > swapchain_texture, id< CAMetalDrawable > drawable)
ENUM_OPERATORS(ShaderStage)
@ MTL_PIPELINE_STATE_CULLMODE_FLAG
@ MTL_PIPELINE_STATE_PSO_FLAG
@ MTL_PIPELINE_STATE_SCISSOR_FLAG
@ MTL_PIPELINE_STATE_FRONT_FACING_FLAG
@ MTL_PIPELINE_STATE_DEPTHSTENCIL_FLAG
@ MTL_PIPELINE_STATE_NULL_FLAG
@ MTL_PIPELINE_STATE_VIEWPORT_FLAG
@ MTL_PIPELINE_STATE_ALL_FLAG
void xr_blit(id< MTLTexture > metal_xr_texture, int ofsx, int ofsy, int width, int height)
Vector< StringRef > DebugStack
std::chrono::nanoseconds Nanoseconds
Definition BLI_timeit.hh:21
Clock::time_point TimePoint
Definition BLI_timeit.hh:20
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
GPUStageBarrierBits
Definition GPU_state.hh:68
unsigned int uint
Definition BKE_text.h:55
void GPU_shader_free(gpu::Shader *shader)
bool operator==(MTLBoundShaderState const &other) const
bool operator!=(MTLBoundShaderState const &other) const
bool operator==(const MTLContextDepthStencilState &other) const
MTLStencilOperation stencil_op_back_depthstencil_pass
MTLStencilOperation stencil_op_front_stencil_fail
MTLStencilOperation stencil_op_front_depthstencil_pass
std::array< MTLStorageBufferBinding, MTL_MAX_SSBO > ssbo_bindings
std::array< MTLTextureBinding, MTL_MAX_SAMPLER_SLOTS > texture_bindings
std::array< MTLAccelerationStructureBinding, 1 > acceleration_structure_bindings
std::array< MTLUniformBufferBinding, MTL_MAX_UBO > ubo_bindings
std::array< MTLSamplerBinding, MTL_MAX_SAMPLER_SLOTS > sampler_bindings
std::array< MTLTextureBinding, MTL_MAX_IMAGE_SLOTS > image_bindings
Map< TextureReadRoutineSpecialization, id< MTLComputePipelineState > > texture_3d_read_compute_psos
Map< DepthTextureUpdateRoutineSpecialization, gpu::Shader * > depth_2d_update_shaders
Map< TextureReadRoutineSpecialization, id< MTLComputePipelineState > > texture_cube_read_compute_psos
void free_cached_pso_map(Map< T, id< MTLComputePipelineState > > &map)
Map< TextureReadRoutineSpecialization, id< MTLComputePipelineState > > texture_2d_array_read_compute_psos
Map< TextureReadRoutineSpecialization, id< MTLComputePipelineState > > texture_cube_array_read_compute_psos
Map< TextureUpdateRoutineSpecialization, id< MTLComputePipelineState > > texture_cube_update_compute_psos
Map< TextureUpdateRoutineSpecialization, id< MTLComputePipelineState > > texture_1d_update_compute_psos
Map< TextureReadRoutineSpecialization, id< MTLComputePipelineState > > texture_2d_read_compute_psos
Map< TextureUpdateRoutineSpecialization, id< MTLComputePipelineState > > texture_buffer_update_compute_psos
Map< TextureReadRoutineSpecialization, id< MTLComputePipelineState > > texture_1d_read_compute_psos
Map< TextureUpdateRoutineSpecialization, id< MTLComputePipelineState > > texture_1d_array_update_compute_psos
Map< TextureUpdateRoutineSpecialization, id< MTLComputePipelineState > > texture_2d_array_update_compute_psos
Map< TextureUpdateRoutineSpecialization, id< MTLComputePipelineState > > texture_3d_update_compute_psos
Map< TextureReadRoutineSpecialization, id< MTLComputePipelineState > > texture_1d_array_read_compute_psos
Map< TextureReadRoutineSpecialization, id< MTLComputePipelineState > > texture_buffer_read_compute_psos
Map< TextureUpdateRoutineSpecialization, id< MTLComputePipelineState > > texture_cube_array_update_compute_psos
Map< TextureUpdateRoutineSpecialization, id< MTLComputePipelineState > > texture_2d_update_compute_psos