25void MTLStateManager::mtl_state_init()
39 current_mutable_ = ~mutable_state;
52 this->set_state(this->
state);
64 current_ = ~this->state;
66 current_mutable_ = ~this->mutable_state;
67 this->set_state(this->
state);
75 if (changed.
blend != 0) {
118void MTLStateManager::mtl_depth_range(
float near,
float far)
123 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
127 ds_state.depth_range_far = far;
131void MTLStateManager::set_mutable_state(
const GPUStateMutable &
state)
133 GPUStateMutable changed =
state ^ current_mutable_;
134 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
141 if (changed.line_width != 0) {
142 pipeline_state.line_width =
state.line_width;
148 mtl_depth_range(
state.depth_range[0],
state.depth_range[1]);
151 if (changed.stencil_compare_mask != 0 || changed.stencil_reference != 0 ||
152 changed.stencil_write_mask != 0)
157 current_mutable_ =
state;
166void MTLStateManager::set_write_mask(
const eGPUWriteMask value)
169 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
171 pipeline_state.color_write_mask =
172 (((value &
GPU_WRITE_RED) != 0) ? MTLColorWriteMaskRed : MTLColorWriteMaskNone) |
173 (((value &
GPU_WRITE_GREEN) != 0) ? MTLColorWriteMaskGreen : MTLColorWriteMaskNone) |
174 (((value &
GPU_WRITE_BLUE) != 0) ? MTLColorWriteMaskBlue : MTLColorWriteMaskNone) |
175 (((value &
GPU_WRITE_ALPHA) != 0) ? MTLColorWriteMaskAlpha : MTLColorWriteMaskNone);
181 switch (depth_func) {
183 return MTLCompareFunctionNever;
185 return MTLCompareFunctionLess;
187 return MTLCompareFunctionEqual;
189 return MTLCompareFunctionLessEqual;
191 return MTLCompareFunctionGreater;
193 return MTLCompareFunctionGreaterEqual;
195 return MTLCompareFunctionAlways;
200 return MTLCompareFunctionAlways;
205 switch (stencil_func) {
207 return MTLCompareFunctionAlways;
209 return MTLCompareFunctionEqual;
211 return MTLCompareFunctionNotEqual;
213 return MTLCompareFunctionAlways;
215 BLI_assert(
false &&
"Unrecognized eGPUStencilTest function");
218 return MTLCompareFunctionAlways;
221void MTLStateManager::set_depth_test(
const eGPUDepthTest value)
224 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
232void MTLStateManager::mtl_stencil_mask(
uint mask)
235 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
240void MTLStateManager::mtl_stencil_set_func(
eGPUStencilTest stencil_func,
int ref,
uint mask)
243 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
247 ds_state.stencil_ref = ref;
248 ds_state.stencil_read_mask =
mask;
254 MTLStencilOperation stencil_fail,
255 MTLStencilOperation depth_test_fail,
256 MTLStencilOperation depthstencil_pass)
277 MTLStencilOperation stencil_fail,
278 MTLStencilOperation depth_test_fail,
279 MTLStencilOperation depthstencil_pass)
282 context,
GPU_CULL_FRONT, stencil_fail, depth_test_fail, depthstencil_pass);
284 context,
GPU_CULL_BACK, stencil_fail, depth_test_fail, depthstencil_pass);
292 context_, MTLStencilOperationKeep, MTLStencilOperationKeep, MTLStencilOperationReplace);
297 MTLStencilOperationKeep,
298 MTLStencilOperationKeep,
299 MTLStencilOperationIncrementWrap);
302 MTLStencilOperationKeep,
303 MTLStencilOperationKeep,
304 MTLStencilOperationDecrementWrap);
309 MTLStencilOperationKeep,
310 MTLStencilOperationDecrementWrap,
311 MTLStencilOperationKeep);
314 MTLStencilOperationKeep,
315 MTLStencilOperationIncrementWrap,
316 MTLStencilOperationKeep);
321 context_, MTLStencilOperationKeep, MTLStencilOperationKeep, MTLStencilOperationKeep);
325 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
333 mtl_stencil_mask(0x00);
337 mtl_stencil_mask(
state.stencil_write_mask);
338 mtl_stencil_set_func(test,
state.stencil_reference,
state.stencil_compare_mask);
342void MTLStateManager::mtl_clip_plane_enable(
uint i)
345 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
350void MTLStateManager::mtl_clip_plane_disable(
uint i)
353 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
358void MTLStateManager::set_clip_distances(
const int new_dist_len,
const int old_dist_len)
362 for (
uint i = 0; i < new_dist_len; i++) {
363 mtl_clip_plane_enable(i);
365 for (
uint i = new_dist_len; i < old_dist_len; i++) {
366 mtl_clip_plane_disable(i);
370void MTLStateManager::set_logic_op(
const bool )
375void MTLStateManager::set_facing(
const bool invert)
379 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
387 pipeline_state.dirty =
true;
394 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
398 pipeline_state.cull_mode = test;
402 pipeline_state.dirty =
true;
414void MTLStateManager::set_shadow_bias(
const bool enable)
418 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
424 ds_state.depth_bias_enabled_for_tris =
true;
425 ds_state.depth_bias = 2.0f;
426 ds_state.depth_slope_scale = 1.0f;
429 ds_state.depth_bias_enabled_for_lines =
false;
430 ds_state.depth_bias_enabled_for_tris =
false;
431 ds_state.depth_bias = 0.0f;
432 ds_state.depth_slope_scale = 0.0f;
437 pipeline_state.dirty =
true;
440void MTLStateManager::set_blend(
const eGPUBlend value)
449 MTLBlendFactor src_rgb;
450 MTLBlendFactor dst_rgb;
451 MTLBlendFactor src_alpha;
452 MTLBlendFactor dst_alpha;
456 src_rgb = MTLBlendFactorSourceAlpha;
457 dst_rgb = MTLBlendFactorOneMinusSourceAlpha;
458 src_alpha = MTLBlendFactorOne;
459 dst_alpha = MTLBlendFactorOneMinusSourceAlpha;
463 src_rgb = MTLBlendFactorOne;
464 dst_rgb = MTLBlendFactorOneMinusSourceAlpha;
465 src_alpha = MTLBlendFactorOne;
466 dst_alpha = MTLBlendFactorOneMinusSourceAlpha;
471 src_rgb = MTLBlendFactorSourceAlpha;
472 dst_rgb = MTLBlendFactorOne;
473 src_alpha = MTLBlendFactorZero;
474 dst_alpha = MTLBlendFactorOne;
480 src_rgb = MTLBlendFactorOne;
481 dst_rgb = MTLBlendFactorOne;
482 src_alpha = MTLBlendFactorOne;
483 dst_alpha = MTLBlendFactorOne;
487 src_rgb = MTLBlendFactorDestinationColor;
488 dst_rgb = MTLBlendFactorZero;
489 src_alpha = MTLBlendFactorDestinationAlpha;
490 dst_alpha = MTLBlendFactorZero;
494 src_rgb = MTLBlendFactorOneMinusDestinationColor;
495 dst_rgb = MTLBlendFactorZero;
496 src_alpha = MTLBlendFactorZero;
497 dst_alpha = MTLBlendFactorOne;
501 src_rgb = MTLBlendFactorOne;
502 dst_rgb = MTLBlendFactorOne;
503 src_alpha = MTLBlendFactorZero;
504 dst_alpha = MTLBlendFactorOneMinusSourceAlpha;
508 src_rgb = MTLBlendFactorOneMinusDestinationAlpha;
509 dst_rgb = MTLBlendFactorSourceAlpha;
510 src_alpha = MTLBlendFactorZero;
511 dst_alpha = MTLBlendFactorSourceAlpha;
515 src_rgb = MTLBlendFactorOneMinusDestinationAlpha;
516 dst_rgb = MTLBlendFactorOne;
517 src_alpha = MTLBlendFactorOneMinusDestinationAlpha;
518 dst_alpha = MTLBlendFactorOne;
522 src_rgb = MTLBlendFactorOne;
523 dst_rgb = MTLBlendFactorSource1Color;
524 src_alpha = MTLBlendFactorOne;
525 dst_alpha = MTLBlendFactorSource1Alpha;
532 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
535 pipeline_state.
rgb_blend_op = MTLBlendOperationReverseSubtract;
536 pipeline_state.alpha_blend_op = MTLBlendOperationReverseSubtract;
539 pipeline_state.rgb_blend_op = MTLBlendOperationAdd;
540 pipeline_state.alpha_blend_op = MTLBlendOperationAdd;
545 pipeline_state.src_rgb_blend_factor = src_rgb;
546 pipeline_state.dest_rgb_blend_factor = dst_rgb;
547 pipeline_state.src_alpha_blend_factor = src_alpha;
548 pipeline_state.dest_alpha_blend_factor = dst_alpha;
552 pipeline_state.dirty =
true;
587 [mtl_event_ release];
594 if (mtl_event_ == nil) {
597 mtl_event_ = [ctx->
device newEvent];
609 if (mtl_event_ == nil) {
MINLINE unsigned int float_as_uint(float f)
@ GPU_BLEND_ADDITIVE_PREMULT
@ GPU_BLEND_ALPHA_UNDER_PREMUL
@ GPU_BLEND_ALPHA_PREMULT
@ GPU_STENCIL_OP_COUNT_DEPTH_FAIL
@ GPU_STENCIL_OP_COUNT_DEPTH_PASS
@ GPU_DEPTH_GREATER_EQUAL
void encode_signal_event(id< MTLEvent > event, uint64_t value)
bool insert_memory_barrier(eGPUBarrier barrier_bits, eGPUStageBarrierBits before_stages, eGPUStageBarrierBits after_stages)
void encode_wait_for_event(id< MTLEvent > event, uint64_t value)
static MTLContext * get()
void pipeline_state_init()
MTLContextGlobalShaderPipelineState pipeline_state
void sampler_bind(MTLSamplerState, uint sampler_unit)
void texture_bind(gpu::MTLTexture *mtl_texture, uint texture_unit, bool is_image)
void texture_unbind(gpu::MTLTexture *mtl_texture, bool is_image)
MTLCommandBufferManager main_command_buffer
void texture_unbind_all(bool is_image)
void image_unbind_all() override
void texture_unbind(Texture *tex) override
void image_unbind(Texture *tex) override
void force_state() override
void image_bind(Texture *tex, int unit) override
void texture_unpack_row_length_set(uint len) override
void texture_bind(Texture *tex, GPUSamplerState sampler, int unit) override
void apply_state() override
MTLStateManager(MTLContext *ctx)
void issue_barrier(eGPUBarrier barrier_bits) override
void texture_unbind_all() override
GPUStateMutable mutable_state
local_group_size(16, 16) .push_constant(Type local_group_size(16, 16) .push_constant(Type input_tx sampler(1, ImageType::FLOAT_2D, "matte_tx") .image(0
CCL_NAMESPACE_BEGIN ccl_device float invert(float color, float factor)
ccl_device_inline float4 mask(const int4 mask, const float4 a)
static void mtl_stencil_set_op(MTLContext *context, MTLStencilOperation stencil_fail, MTLStencilOperation depth_test_fail, MTLStencilOperation depthstencil_pass)
static MTLCompareFunction gpu_depth_function_to_metal(eGPUDepthTest depth_func)
static MTLCompareFunction gpu_stencil_func_to_metal(eGPUStencilTest stencil_func)
@ MTL_PIPELINE_STATE_CULLMODE_FLAG
@ MTL_PIPELINE_STATE_PSO_FLAG
@ MTL_PIPELINE_STATE_FRONT_FACING_FLAG
@ MTL_PIPELINE_STATE_DEPTHSTENCIL_FLAG
@ MTL_PIPELINE_STATE_VIEWPORT_FLAG
static void mtl_stencil_set_op_separate(MTLContext *context, eGPUFaceCullTest face, MTLStencilOperation stencil_fail, MTLStencilOperation depth_test_fail, MTLStencilOperation depthstencil_pass)
bool depth_bias_enabled_for_lines
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
MTLPipelineStateDirtyFlag dirty_flags
MTLContextDepthStencilState depth_stencil_state
MTLBlendOperation rgb_blend_op
bool clip_distance_enabled[6]