17# pragma clang diagnostic ignored "-Wdeprecated-declarations"
36#include <Cocoa/Cocoa.h>
37#include <Metal/Metal.h>
38#include <QuartzCore/QuartzCore.h>
43@class MTLCommandQueue;
44@class MTLRenderPipelineState;
68 return !(*
this == other);
79 :
ctx(context),
cmd(command_buffer_manager) {};
104 bool use_samplers_argument_buffer,
107 bool use_samplers_argument_buffer,
123 :
ctx(context),
cmd(command_buffer_manager) {};
142 bool use_samplers_argument_buffer,
186 bool stencil_state_equality =
true;
188 stencil_state_equality =
201 return depth_state_equality && stencil_state_equality;
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);
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);
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;
232 main_hash ^= std::size_t(this->stencil_func) << 16;
233 main_hash ^= stencilop_bitmask;
235 std::size_t final_hash = (main_hash << 8) | boolean_bitmask;
285 [item.value release];
336 if (buffer_clear_pso_) {
337 [buffer_clear_pso_ release];
338 buffer_clear_pso_ = nil;
473 bool supports_render_ =
false;
478 volatile std::atomic<int> num_active_cmd_bufs = 0;
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;
500 MTLRenderPassDescriptor *active_pass_descriptor_ =
nullptr;
507 int current_draw_call_count_ = 0;
508 int encoder_count_ = 0;
509 int vertex_submitted_count_ = 0;
520 : context_(context), render_pass_state_(context, *this), compute_state_(context, *this) {};
521 void prepare(
bool supports_render =
true);
540 return render_pass_state_;
548 return compute_state_;
581 num_active_cmd_bufs++;
588 num_active_cmd_bufs--;
593 return num_active_cmd_bufs;
599 std::this_thread::yield();
610 void register_encoder_counters();
611 void fold_remaining_debug_groups();
673 [GPU_SAMPLER_FILTERING_TYPES_COUNT];
686 bool is_inside_frame_ =
false;
687 uint current_frame_index_;
691 bool visibility_is_dirty_ =
false;
706 struct ScopeTimings {
707 using Clock = std::chrono::steady_clock;
717 Vector<ScopeTimings> scope_timings;
719 void process_frame_timings();
733 void flush()
override;
817 void compute_dispatch(
int groups_x_len,
int groups_y_len,
int groups_z_len);
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);
841 return texture_utils_;
847 return compute_utils_;
857 return is_inside_frame_;
862 return current_frame_index_;
867 return this->memory_manager;
908 return cached_sampler_buffers_;
938void present(MTLRenderPassDescriptor *blit_descriptor,
#define GPU_SAMPLER_CUSTOM_TYPES_COUNT
#define GPU_SAMPLER_EXTEND_MODES_COUNT
void activate(bool forceActivation=false) const
ItemIterator items() const &
StateManager * state_manager
MTLFrameBuffer * get_active_framebuffer()
bool end_active_command_encoder(bool retain_framebuffers=false)
void inc_active_command_buffer_count()
void encode_signal_event(id< MTLEvent > event, uint64_t value)
id< MTLComputeCommandEncoder > get_active_compute_command_encoder()
MTLCommandBufferManager(MTLContext &context)
bool do_break_submission()
void register_draw_counters(int vertex_submission)
void prepare(bool supports_render=true)
void wait_until_active_command_buffers_complete()
int get_active_command_buffer_count()
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()
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)
void dec_active_command_buffer_count()
id< MTLAccelerationStructureCommandEncoder > ensure_begin_acceleration_structure_encoder()
id< MTLRenderCommandEncoder > get_active_render_command_encoder()
void unfold_pending_debug_groups()
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()
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 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
void pipeline_state_init()
MTLContextGlobalShaderPipelineState pipeline_state
bool debug_capture_begin(const char *title) override
void * debug_capture_scope_create(const char *name) override
void debug_capture_scope_end(void *scope) override
MTLSamplerArray & get_sampler_array()
static MTLBufferPool * global_memory_manager
void end_frame() override
static int global_memory_manager_refcount
MTLScratchBufferManager & get_scratch_buffer_manager()
uint get_current_frame_index()
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 debug_group_begin(const char *name, int index) override
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)
bool debug_capture_scope_begin(void *scope) override
MTLCommandBufferManager main_command_buffer
void debug_group_end() override
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 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()
void ensure_depth_stencil_state()
MTLContext(GHOST_IWindow *ghost_window, GHOST_IContext *ghost_context)
void debug_capture_end() override
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
MTLScissorRect last_scissor_rect
uint last_used_stencil_ref_value
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
#define MTL_FRAME_AVERAGE_COUNT
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)
MTLPipelineStateDirtyFlag
@ 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
Clock::time_point TimePoint
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
void GPU_shader_free(gpu::Shader *shader)
bool operator==(MTLBoundShaderState const &other) const
bool operator!=(MTLBoundShaderState const &other) const
MTLCompareFunction depth_function
bool operator==(const MTLContextDepthStencilState &other) const
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
bool clip_distance_enabled[6]
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
gpu::Shader * fullscreen_blit_shader
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