22 void MTLStateManager::mtl_state_init()
43 this->set_state(this->
state);
54 current_ = ~this->state;
56 current_mutable_ = ~this->mutable_state;
57 this->set_state(this->
state);
65 if (changed.
blend != 0) {
108 void MTLStateManager::mtl_depth_range(
float near,
float far)
113 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
117 ds_state.depth_range_far = far;
121 void MTLStateManager::set_mutable_state(
const GPUStateMutable &
state)
123 GPUStateMutable changed =
state ^ current_mutable_;
124 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
131 if (changed.line_width != 0) {
132 pipeline_state.line_width =
state.line_width;
136 if (changed.depth_range[0] != 0 || changed.depth_range[1] != 0) {
138 mtl_depth_range(
state.depth_range[0],
state.depth_range[1]);
141 if (changed.stencil_compare_mask != 0 || changed.stencil_reference != 0 ||
142 changed.stencil_write_mask != 0) {
146 current_mutable_ =
state;
155 void MTLStateManager::set_write_mask(
const eGPUWriteMask value)
158 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
160 pipeline_state.color_write_mask =
161 (((value &
GPU_WRITE_RED) != 0) ? MTLColorWriteMaskRed : MTLColorWriteMaskNone) |
162 (((value &
GPU_WRITE_GREEN) != 0) ? MTLColorWriteMaskGreen : MTLColorWriteMaskNone) |
163 (((value &
GPU_WRITE_BLUE) != 0) ? MTLColorWriteMaskBlue : MTLColorWriteMaskNone) |
164 (((value &
GPU_WRITE_ALPHA) != 0) ? MTLColorWriteMaskAlpha : MTLColorWriteMaskNone);
170 switch (depth_func) {
172 return MTLCompareFunctionNever;
174 return MTLCompareFunctionLess;
176 return MTLCompareFunctionEqual;
178 return MTLCompareFunctionLessEqual;
180 return MTLCompareFunctionGreater;
182 return MTLCompareFunctionGreaterEqual;
184 return MTLCompareFunctionAlways;
189 return MTLCompareFunctionAlways;
194 switch (stencil_func) {
196 return MTLCompareFunctionAlways;
198 return MTLCompareFunctionEqual;
200 return MTLCompareFunctionNotEqual;
202 return MTLCompareFunctionAlways;
204 BLI_assert(
false &&
"Unrecognised eGPUStencilTest function");
207 return MTLCompareFunctionAlways;
210 void MTLStateManager::set_depth_test(
const eGPUDepthTest value)
213 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
221 void MTLStateManager::mtl_stencil_mask(
uint mask)
224 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
232 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
236 ds_state.stencil_ref = ref;
237 ds_state.stencil_read_mask =
mask;
243 MTLStencilOperation stencil_fail,
244 MTLStencilOperation depth_test_fail,
245 MTLStencilOperation depthstencil_pass)
266 MTLStencilOperation stencil_fail,
267 MTLStencilOperation depth_test_fail,
268 MTLStencilOperation depthstencil_pass)
281 context_, MTLStencilOperationKeep, MTLStencilOperationKeep, MTLStencilOperationReplace);
287 MTLStencilOperationKeep,
288 MTLStencilOperationKeep,
289 MTLStencilOperationIncrementWrap);
292 MTLStencilOperationKeep,
293 MTLStencilOperationKeep,
294 MTLStencilOperationDecrementWrap);
300 MTLStencilOperationKeep,
301 MTLStencilOperationDecrementWrap,
302 MTLStencilOperationKeep);
305 MTLStencilOperationKeep,
306 MTLStencilOperationIncrementWrap,
307 MTLStencilOperationKeep);
312 context_, MTLStencilOperationKeep, MTLStencilOperationKeep, MTLStencilOperationKeep);
316 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
324 mtl_stencil_mask(0x00);
328 mtl_stencil_mask(
state.stencil_write_mask);
329 mtl_stencil_set_func(test,
state.stencil_reference,
state.stencil_compare_mask);
333 void MTLStateManager::set_clip_distances(
const int new_dist_len,
const int old_dist_len)
340 void MTLStateManager::set_logic_op(
const bool enable)
345 void MTLStateManager::set_facing(
const bool invert)
349 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
357 pipeline_state.dirty =
true;
364 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
368 pipeline_state.cull_mode = test;
372 pipeline_state.dirty =
true;
384 void MTLStateManager::set_shadow_bias(
const bool enable)
388 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
394 ds_state.depth_bias_enabled_for_tris =
true;
395 ds_state.depth_bias = 2.0f;
396 ds_state.depth_slope_scale = 1.0f;
399 ds_state.depth_bias_enabled_for_lines =
false;
400 ds_state.depth_bias_enabled_for_tris =
false;
401 ds_state.depth_bias = 0.0f;
402 ds_state.depth_slope_scale = 0.0f;
407 pipeline_state.dirty =
true;
410 void MTLStateManager::set_blend(
const eGPUBlend value)
419 MTLBlendFactor src_rgb;
420 MTLBlendFactor dst_rgb;
421 MTLBlendFactor src_alpha;
422 MTLBlendFactor dst_alpha;
426 src_rgb = MTLBlendFactorSourceAlpha;
427 dst_rgb = MTLBlendFactorOneMinusSourceAlpha;
428 src_alpha = MTLBlendFactorOne;
429 dst_alpha = MTLBlendFactorOneMinusSourceAlpha;
433 src_rgb = MTLBlendFactorOne;
434 dst_rgb = MTLBlendFactorOneMinusSourceAlpha;
435 src_alpha = MTLBlendFactorOne;
436 dst_alpha = MTLBlendFactorOneMinusSourceAlpha;
441 src_rgb = MTLBlendFactorSourceAlpha;
442 dst_rgb = MTLBlendFactorOne;
443 src_alpha = MTLBlendFactorZero;
444 dst_alpha = MTLBlendFactorOne;
450 src_rgb = MTLBlendFactorOne;
451 dst_rgb = MTLBlendFactorOne;
452 src_alpha = MTLBlendFactorOne;
453 dst_alpha = MTLBlendFactorOne;
457 src_rgb = MTLBlendFactorDestinationColor;
458 dst_rgb = MTLBlendFactorZero;
459 src_alpha = MTLBlendFactorDestinationAlpha;
460 dst_alpha = MTLBlendFactorZero;
464 src_rgb = MTLBlendFactorOneMinusDestinationColor;
465 dst_rgb = MTLBlendFactorZero;
466 src_alpha = MTLBlendFactorZero;
467 dst_alpha = MTLBlendFactorOne;
471 src_rgb = MTLBlendFactorOne;
472 dst_rgb = MTLBlendFactorOne;
473 src_alpha = MTLBlendFactorZero;
474 dst_alpha = MTLBlendFactorOneMinusSourceAlpha;
478 src_rgb = MTLBlendFactorOneMinusDestinationAlpha;
479 dst_rgb = MTLBlendFactorSourceAlpha;
480 src_alpha = MTLBlendFactorZero;
481 dst_alpha = MTLBlendFactorSourceAlpha;
485 src_rgb = MTLBlendFactorOneMinusDestinationAlpha;
486 dst_rgb = MTLBlendFactorOne;
487 src_alpha = MTLBlendFactorOneMinusDestinationAlpha;
488 dst_alpha = MTLBlendFactorOne;
492 src_rgb = MTLBlendFactorOne;
493 dst_rgb = MTLBlendFactorSource1Color;
494 src_alpha = MTLBlendFactorOne;
495 dst_alpha = MTLBlendFactorSource1Alpha;
502 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
505 pipeline_state.
rgb_blend_op = MTLBlendOperationReverseSubtract;
506 pipeline_state.alpha_blend_op = MTLBlendOperationReverseSubtract;
509 pipeline_state.rgb_blend_op = MTLBlendOperationAdd;
510 pipeline_state.alpha_blend_op = MTLBlendOperationAdd;
515 pipeline_state.src_rgb_blend_factor = src_rgb;
516 pipeline_state.dest_rgb_blend_factor = dst_rgb;
517 pipeline_state.src_alpha_blend_factor = src_alpha;
518 pipeline_state.dest_alpha_blend_factor = dst_alpha;
522 pipeline_state.dirty =
true;
555 if ([ctx->
device hasUnifiedMemory]) {
MINLINE unsigned int float_as_uint(float f)
GPUContext * GPU_context_active_get(void)
@ 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
bool insert_memory_barrier(eGPUBarrier barrier_bits, eGPUStageBarrierBits before_stages, eGPUStageBarrierBits after_stages)
void texture_bind(gpu::MTLTexture *mtl_texture, uint texture_unit)
void pipeline_state_init()
void texture_unbind_all()
MTLContextGlobalShaderPipelineState pipeline_state
void texture_unbind(gpu::MTLTexture *mtl_texture)
void sampler_bind(MTLSamplerState, uint sampler_unit)
MTLCommandBufferManager main_command_buffer
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_bind(Texture *tex, eGPUSamplerState sampler, int unit) override
void texture_unpack_row_length_set(uint len) override
void apply_state() override
MTLStateManager(MTLContext *ctx)
void issue_barrier(eGPUBarrier barrier_bits) override
void texture_unbind_all() override
GPUStateMutable mutable_state
depth_tx sampler(1, ImageType::FLOAT_2D, "combined_tx") .sampler(2
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 Context * unwrap(GPUContext *ctx)
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