25void MTLStateManager::mtl_state_init()
28 context_->pipeline_state_init();
44 current_.clip_distances = 6;
52 this->set_state(this->
state);
62 uint32_t clip_distances = current_.clip_distances;
64 current_ = ~this->state;
65 current_.clip_distances = clip_distances;
66 current_mutable_ = ~this->mutable_state;
67 this->set_state(this->
state);
75 if (changed.blend != 0) {
78 if (changed.write_mask != 0) {
81 if (changed.depth_test != 0) {
84 if (changed.stencil_test != 0 || changed.stencil_op != 0) {
88 if (changed.clip_distances != 0) {
89 set_clip_distances(
state.clip_distances, current_.clip_distances);
91 if (changed.culling_test != 0) {
95 if (changed.invert_facing != 0) {
96 set_facing(
state.invert_facing);
98 if (changed.provoking_vert != 0) {
103 if (changed.polygon_smooth) {
106 if (changed.line_smooth) {
115 GPUStateMutable changed =
state ^ current_mutable_;
116 MTLContextGlobalShaderPipelineState &pipeline_state = context_->pipeline_state;
119 pipeline_state.point_size =
state.point_size;
123 if (changed.line_width != 0) {
124 pipeline_state.line_width =
state.line_width;
128 if (changed.stencil_compare_mask != 0 || changed.stencil_reference != 0 ||
129 changed.stencil_write_mask != 0)
134 current_mutable_ =
state;
143void MTLStateManager::set_write_mask(
const GPUWriteMask value)
146 MTLContextGlobalShaderPipelineState &pipeline_state = context_->pipeline_state;
147 pipeline_state.depth_stencil_state.depth_write_enable = ((value &
GPU_WRITE_DEPTH) != 0);
148 pipeline_state.color_write_mask =
149 (((value &
GPU_WRITE_RED) != 0) ? MTLColorWriteMaskRed : MTLColorWriteMaskNone) |
150 (((value &
GPU_WRITE_GREEN) != 0) ? MTLColorWriteMaskGreen : MTLColorWriteMaskNone) |
151 (((value &
GPU_WRITE_BLUE) != 0) ? MTLColorWriteMaskBlue : MTLColorWriteMaskNone) |
152 (((value &
GPU_WRITE_ALPHA) != 0) ? MTLColorWriteMaskAlpha : MTLColorWriteMaskNone);
158 switch (depth_func) {
160 return MTLCompareFunctionNever;
162 return MTLCompareFunctionLess;
164 return MTLCompareFunctionEqual;
166 return MTLCompareFunctionLessEqual;
168 return MTLCompareFunctionGreater;
170 return MTLCompareFunctionGreaterEqual;
172 return MTLCompareFunctionAlways;
177 return MTLCompareFunctionAlways;
182 switch (stencil_func) {
184 return MTLCompareFunctionAlways;
186 return MTLCompareFunctionEqual;
188 return MTLCompareFunctionNotEqual;
190 return MTLCompareFunctionAlways;
192 BLI_assert(
false &&
"Unrecognized GPUStencilTest function");
195 return MTLCompareFunctionAlways;
198void MTLStateManager::set_depth_test(
const GPUDepthTest value)
209void MTLStateManager::mtl_stencil_mask(
uint mask)
212 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
217void MTLStateManager::mtl_stencil_set_func(
GPUStencilTest stencil_func,
int ref,
uint mask)
220 MTLContextGlobalShaderPipelineState &pipeline_state = context_->pipeline_state;
221 MTLContextDepthStencilState &ds_state = pipeline_state.depth_stencil_state;
224 ds_state.stencil_ref = ref;
225 ds_state.stencil_read_mask =
mask;
231 MTLStencilOperation stencil_fail,
232 MTLStencilOperation depth_test_fail,
233 MTLStencilOperation depthstencil_pass)
254 MTLStencilOperation stencil_fail,
255 MTLStencilOperation depth_test_fail,
256 MTLStencilOperation depthstencil_pass)
259 context,
GPU_CULL_FRONT, stencil_fail, depth_test_fail, depthstencil_pass);
261 context,
GPU_CULL_BACK, stencil_fail, depth_test_fail, depthstencil_pass);
269 context_, MTLStencilOperationKeep, MTLStencilOperationKeep, MTLStencilOperationReplace);
274 MTLStencilOperationKeep,
275 MTLStencilOperationKeep,
276 MTLStencilOperationIncrementWrap);
279 MTLStencilOperationKeep,
280 MTLStencilOperationKeep,
281 MTLStencilOperationDecrementWrap);
286 MTLStencilOperationKeep,
287 MTLStencilOperationDecrementWrap,
288 MTLStencilOperationKeep);
291 MTLStencilOperationKeep,
292 MTLStencilOperationIncrementWrap,
293 MTLStencilOperationKeep);
298 context_, MTLStencilOperationKeep, MTLStencilOperationKeep, MTLStencilOperationKeep);
302 MTLContextGlobalShaderPipelineState &pipeline_state = context_->
pipeline_state;
310 mtl_stencil_mask(0x00);
314 mtl_stencil_mask(
state.stencil_write_mask);
315 mtl_stencil_set_func(test,
state.stencil_reference,
state.stencil_compare_mask);
319void MTLStateManager::mtl_clip_plane_enable(
uint i)
322 MTLContextGlobalShaderPipelineState &pipeline_state = context_->pipeline_state;
323 pipeline_state.clip_distance_enabled[i] =
true;
327void MTLStateManager::mtl_clip_plane_disable(
uint i)
330 MTLContextGlobalShaderPipelineState &pipeline_state = context_->pipeline_state;
331 pipeline_state.clip_distance_enabled[i] =
false;
335void MTLStateManager::set_clip_distances(
const int new_dist_len,
const int old_dist_len)
339 for (
uint i = 0; i < new_dist_len; i++) {
340 mtl_clip_plane_enable(i);
342 for (
uint i = new_dist_len; i < old_dist_len; i++) {
343 mtl_clip_plane_disable(i);
347void MTLStateManager::set_facing(
const bool invert)
351 MTLContextGlobalShaderPipelineState &pipeline_state = context_->pipeline_state;
359 pipeline_state.dirty =
true;
366 MTLContextGlobalShaderPipelineState &pipeline_state = context_->pipeline_state;
370 pipeline_state.cull_mode = test;
374 pipeline_state.dirty =
true;
386void MTLStateManager::set_blend(
const GPUBlend value)
395 MTLBlendFactor src_rgb;
396 MTLBlendFactor dst_rgb;
397 MTLBlendFactor src_alpha;
398 MTLBlendFactor dst_alpha;
402 src_rgb = MTLBlendFactorSourceAlpha;
403 dst_rgb = MTLBlendFactorOneMinusSourceAlpha;
404 src_alpha = MTLBlendFactorOne;
405 dst_alpha = MTLBlendFactorOneMinusSourceAlpha;
409 src_rgb = MTLBlendFactorOne;
410 dst_rgb = MTLBlendFactorOneMinusSourceAlpha;
411 src_alpha = MTLBlendFactorOne;
412 dst_alpha = MTLBlendFactorOneMinusSourceAlpha;
417 src_rgb = MTLBlendFactorSourceAlpha;
418 dst_rgb = MTLBlendFactorOne;
419 src_alpha = MTLBlendFactorZero;
420 dst_alpha = MTLBlendFactorOne;
429 src_rgb = MTLBlendFactorOne;
430 dst_rgb = MTLBlendFactorOne;
431 src_alpha = MTLBlendFactorOne;
432 dst_alpha = MTLBlendFactorOne;
436 src_rgb = MTLBlendFactorDestinationColor;
437 dst_rgb = MTLBlendFactorZero;
438 src_alpha = MTLBlendFactorDestinationAlpha;
439 dst_alpha = MTLBlendFactorZero;
443 src_rgb = MTLBlendFactorOneMinusDestinationColor;
444 dst_rgb = MTLBlendFactorZero;
445 src_alpha = MTLBlendFactorZero;
446 dst_alpha = MTLBlendFactorOne;
450 src_rgb = MTLBlendFactorOne;
451 dst_rgb = MTLBlendFactorOne;
452 src_alpha = MTLBlendFactorZero;
453 dst_alpha = MTLBlendFactorOneMinusSourceAlpha;
457 src_rgb = MTLBlendFactorOneMinusDestinationAlpha;
458 dst_rgb = MTLBlendFactorSourceAlpha;
459 src_alpha = MTLBlendFactorZero;
460 dst_alpha = MTLBlendFactorSourceAlpha;
464 src_rgb = MTLBlendFactorOneMinusDestinationAlpha;
465 dst_rgb = MTLBlendFactorOne;
466 src_alpha = MTLBlendFactorOneMinusDestinationAlpha;
467 dst_alpha = MTLBlendFactorOne;
471 src_rgb = MTLBlendFactorOne;
472 dst_rgb = MTLBlendFactorSource1Color;
473 src_alpha = MTLBlendFactorOne;
474 dst_alpha = MTLBlendFactorSource1Alpha;
478 src_rgb = MTLBlendFactorZero;
479 dst_rgb = MTLBlendFactorOneMinusSourceAlpha;
480 src_alpha = MTLBlendFactorZero;
481 dst_alpha = MTLBlendFactorOneMinusSourceAlpha;
485 src_rgb = MTLBlendFactorOne;
486 dst_rgb = MTLBlendFactorSourceAlpha;
487 src_alpha = MTLBlendFactorZero;
488 dst_alpha = MTLBlendFactorSourceAlpha;
495 MTLContextGlobalShaderPipelineState &pipeline_state = context_->pipeline_state;
498 pipeline_state.rgb_blend_op = MTLBlendOperationMin;
499 pipeline_state.alpha_blend_op = MTLBlendOperationMin;
502 pipeline_state.rgb_blend_op = MTLBlendOperationMax;
503 pipeline_state.alpha_blend_op = MTLBlendOperationMax;
506 pipeline_state.rgb_blend_op = MTLBlendOperationReverseSubtract;
507 pipeline_state.alpha_blend_op = MTLBlendOperationReverseSubtract;
510 pipeline_state.rgb_blend_op = MTLBlendOperationAdd;
511 pipeline_state.alpha_blend_op = MTLBlendOperationAdd;
516 pipeline_state.src_rgb_blend_factor = src_rgb;
517 pipeline_state.dest_rgb_blend_factor = dst_rgb;
518 pipeline_state.src_alpha_blend_factor = src_alpha;
519 pipeline_state.dest_alpha_blend_factor = dst_alpha;
523 pipeline_state.dirty =
true;
558 [mtl_event_ release];
565 if (mtl_event_ == nil) {
568 mtl_event_ = [ctx->
device newEvent];
580 if (mtl_event_ == nil) {
655 if (format == TextureFormat::SRGBA_8_8_8_8) {
656 format = TextureFormat::UNORM_8_8_8_8;
void encode_signal_event(id< MTLEvent > event, uint64_t value)
bool insert_memory_barrier(GPUBarrier barrier_bits, GPUStageBarrierBits before_stages, GPUStageBarrierBits after_stages)
void encode_wait_for_event(id< MTLEvent > event, uint64_t value)
static MTLContext * get()
MTLContextGlobalShaderPipelineState pipeline_state
void texture_unbind(gpu::MTLTexture *mtl_texture, bool is_image, StateManager *state_manager)
void sampler_bind(MTLSamplerState, uint sampler_unit)
void texture_bind(gpu::MTLTexture *mtl_texture, uint texture_unit, 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 issue_barrier(GPUBarrier barrier_bits) override
void image_bind(Texture *tex, int unit) override
void texture_bind(Texture *tex, GPUSamplerState sampler, int unit) override
void apply_state() override
MTLStateManager(MTLContext *ctx)
void texture_unbind_all() override
MTLSamplerState get_sampler_state()
GPUStateMutable mutable_state
std::array< TextureWriteFormat, GPU_MAX_IMAGE > image_formats
TextureFormat format_get() const
float2x2 invert(float2x2 mat)
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
static void mtl_stencil_set_op_separate(MTLContext *context, GPUFaceCullTest face, MTLStencilOperation stencil_fail, MTLStencilOperation depth_test_fail, MTLStencilOperation depthstencil_pass)
static void mtl_stencil_set_op(MTLContext *context, MTLStencilOperation stencil_fail, MTLStencilOperation depth_test_fail, MTLStencilOperation depthstencil_pass)
@ MTL_PIPELINE_STATE_CULLMODE_FLAG
@ MTL_PIPELINE_STATE_PSO_FLAG
@ MTL_PIPELINE_STATE_FRONT_FACING_FLAG
@ MTL_PIPELINE_STATE_DEPTHSTENCIL_FLAG
static MTLCompareFunction gpu_stencil_func_to_metal(GPUStencilTest stencil_func)
static MTLCompareFunction gpu_depth_function_to_metal(GPUDepthTest depth_func)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
@ GPU_STENCIL_OP_COUNT_DEPTH_FAIL
@ GPU_STENCIL_OP_COUNT_DEPTH_PASS
MINLINE unsigned int float_as_uint(float f)
@ GPU_BLEND_OVERLAY_MASK_FROM_ALPHA
@ GPU_BLEND_ALPHA_UNDER_PREMUL
@ GPU_BLEND_ADDITIVE_PREMULT
@ GPU_BLEND_ALPHA_PREMULT
@ GPU_DEPTH_GREATER_EQUAL
void mask(PageMask &srt, TileMaps &tilemaps, const uint3 global_id, const uint local_tile_index)
MTLCompareFunction depth_function
MTLStencilOperation stencil_op_back_depthstencil_pass
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