40#define CASE(a, b, c, blender_enum, d, mtl_pixel_enum, f, g, h) \
41 case TextureFormat::blender_enum: \
42 return MTLPixelFormat##mtl_pixel_enum;
51 return MTLPixelFormatRGBA8Unorm;
57 case MTLPixelFormatRGBA8Uint:
58 case MTLPixelFormatRGBA8Sint:
59 case MTLPixelFormatRGBA8Unorm:
60 case MTLPixelFormatRGBA8Snorm:
61 case MTLPixelFormatRGB10A2Uint:
62 case MTLPixelFormatRGB10A2Unorm:
64 case MTLPixelFormatRGBA32Uint:
65 case MTLPixelFormatRGBA32Sint:
66 case MTLPixelFormatRGBA32Float:
68 case MTLPixelFormatRGBA16Uint:
69 case MTLPixelFormatRGBA16Sint:
70 case MTLPixelFormatRGBA16Float:
71 case MTLPixelFormatRGBA16Unorm:
72 case MTLPixelFormatRGBA16Snorm:
74 case MTLPixelFormatRG8Uint:
75 case MTLPixelFormatRG8Sint:
76 case MTLPixelFormatRG8Unorm:
77 case MTLPixelFormatRG8Snorm:
78 case MTLPixelFormatRG8Unorm_sRGB:
80 case MTLPixelFormatRG32Uint:
81 case MTLPixelFormatRG32Sint:
82 case MTLPixelFormatRG32Float:
84 case MTLPixelFormatRG16Uint:
85 case MTLPixelFormatRG16Sint:
86 case MTLPixelFormatRG16Float:
87 case MTLPixelFormatRG16Unorm:
88 case MTLPixelFormatRG16Snorm:
90 case MTLPixelFormatR8Uint:
91 case MTLPixelFormatR8Sint:
92 case MTLPixelFormatR8Unorm:
93 case MTLPixelFormatR8Snorm:
95 case MTLPixelFormatR32Uint:
96 case MTLPixelFormatR32Sint:
97 case MTLPixelFormatR32Float:
99 case MTLPixelFormatR16Uint:
100 case MTLPixelFormatR16Sint:
101 case MTLPixelFormatR16Float:
102 case MTLPixelFormatR16Snorm:
103 case MTLPixelFormatR16Unorm:
105 case MTLPixelFormatRG11B10Float:
107 case MTLPixelFormatDepth32Float_Stencil8:
109 case MTLPixelFormatRGBA8Unorm_sRGB:
110 case MTLPixelFormatDepth32Float:
111 case MTLPixelFormatDepth24Unorm_Stencil8:
113 case MTLPixelFormatDepth16Unorm:
115 case MTLPixelFormatBC1_RGBA:
116 case MTLPixelFormatBC1_RGBA_sRGB:
118 case MTLPixelFormatBC2_RGBA:
119 case MTLPixelFormatBC2_RGBA_sRGB:
120 case MTLPixelFormatBC3_RGBA:
121 case MTLPixelFormatBC3_RGBA_sRGB:
132 switch (tex_format) {
133 case MTLPixelFormatRGBA8Uint:
134 case MTLPixelFormatRGBA8Sint:
135 case MTLPixelFormatRGBA8Unorm:
136 case MTLPixelFormatRGBA8Snorm:
137 case MTLPixelFormatRGBA32Uint:
138 case MTLPixelFormatRGBA32Sint:
139 case MTLPixelFormatRGBA32Float:
140 case MTLPixelFormatRGBA16Uint:
141 case MTLPixelFormatRGBA16Sint:
142 case MTLPixelFormatRGBA16Float:
143 case MTLPixelFormatRGBA16Unorm:
144 case MTLPixelFormatRGBA16Snorm:
145 case MTLPixelFormatRGBA8Unorm_sRGB:
146 case MTLPixelFormatRGB10A2Uint:
147 case MTLPixelFormatRGB10A2Unorm:
148 case MTLPixelFormatBC1_RGBA_sRGB:
149 case MTLPixelFormatBC2_RGBA_sRGB:
150 case MTLPixelFormatBC3_RGBA_sRGB:
151 case MTLPixelFormatBC1_RGBA:
152 case MTLPixelFormatBC2_RGBA:
153 case MTLPixelFormatBC3_RGBA:
156 case MTLPixelFormatRG11B10Float:
159 case MTLPixelFormatRG8Uint:
160 case MTLPixelFormatRG8Sint:
161 case MTLPixelFormatRG8Unorm:
162 case MTLPixelFormatRG32Uint:
163 case MTLPixelFormatRG32Sint:
164 case MTLPixelFormatRG32Float:
165 case MTLPixelFormatRG16Uint:
166 case MTLPixelFormatRG16Sint:
167 case MTLPixelFormatRG16Float:
168 case MTLPixelFormatDepth32Float_Stencil8:
169 case MTLPixelFormatRG16Snorm:
170 case MTLPixelFormatRG16Unorm:
171 case MTLPixelFormatRG8Snorm:
174 case MTLPixelFormatR8Uint:
175 case MTLPixelFormatR8Sint:
176 case MTLPixelFormatR8Unorm:
177 case MTLPixelFormatR8Snorm:
178 case MTLPixelFormatR32Uint:
179 case MTLPixelFormatR32Sint:
180 case MTLPixelFormatR32Float:
181 case MTLPixelFormatR16Uint:
182 case MTLPixelFormatR16Sint:
183 case MTLPixelFormatR16Float:
184 case MTLPixelFormatR16Unorm:
185 case MTLPixelFormatR16Snorm:
186 case MTLPixelFormatDepth32Float:
187 case MTLPixelFormatDepth16Unorm:
188 case MTLPixelFormatDepth24Unorm_Stencil8:
207 case MTLPixelFormatA8Unorm:
208 case MTLPixelFormatR8Uint:
209 case MTLPixelFormatR8Sint:
210 case MTLPixelFormatR16Uint:
211 case MTLPixelFormatR16Sint:
212 case MTLPixelFormatRG32Uint:
213 case MTLPixelFormatRG32Sint:
214 case MTLPixelFormatRGBA8Uint:
215 case MTLPixelFormatRGBA8Sint:
216 case MTLPixelFormatRGBA32Uint:
217 case MTLPixelFormatRGBA32Sint:
218 case MTLPixelFormatDepth16Unorm:
219 case MTLPixelFormatDepth32Float:
220 case MTLPixelFormatInvalid:
221 case MTLPixelFormatBGR10A2Unorm:
222 case MTLPixelFormatRGB10A2Uint:
230 case MTLPixelFormatA8Unorm:
231 case MTLPixelFormatR8Uint:
232 case MTLPixelFormatR8Sint:
233 case MTLPixelFormatR16Uint:
234 case MTLPixelFormatR16Sint:
235 case MTLPixelFormatRG32Uint:
236 case MTLPixelFormatRG32Sint:
237 case MTLPixelFormatRGBA8Uint:
238 case MTLPixelFormatRGBA8Sint:
239 case MTLPixelFormatRGBA32Uint:
240 case MTLPixelFormatRGBA32Sint:
241 case MTLPixelFormatRGBA32Float:
242 case MTLPixelFormatDepth16Unorm:
243 case MTLPixelFormatDepth32Float:
244 case MTLPixelFormatInvalid:
245 case MTLPixelFormatBGR10A2Unorm:
246 case MTLPixelFormatRGB10A2Uint:
271 id<MTLComputePipelineState> return_pso = nil;
279 NSString *tex_update_kernel_src = [NSString
283 MTLCompileOptions *
options = [[[MTLCompileOptions alloc]
init] autorelease];
284 options.languageVersion = MTLLanguageVersion2_2;
285 options.preprocessorMacros = @{
287 [NSString stringWithUTF8String:specialization_params.
input_data_type.c_str()],
288 @"OUTPUT_DATA_TYPE" :
289 [NSString stringWithUTF8String:specialization_params.
output_data_type.c_str()],
290 @"COMPONENT_COUNT_INPUT" :
292 @"COMPONENT_COUNT_OUTPUT" :
294 @"TEX_TYPE" : [NSNumber numberWithInt:((int)(texture_type))],
295 @"IS_TEXTURE_CLEAR" :
296 [NSNumber numberWithInt:((int)(specialization_params.is_clear ? 1 : 0))]
300 NSError *error =
nullptr;
301 id<MTLLibrary> temp_lib = [[ctx->device newLibraryWithSource:tex_update_kernel_src
303 error:&error] autorelease];
306 if ([[error localizedDescription] rangeOfString:
@"Compilation succeeded"].location ==
310 "Compile Error - Metal Shader Library error %s",
311 [[error localizedDescription] UTF8String]);
319 id<MTLFunction> temp_compute_function = [[temp_lib
320 newFunctionWithName:
@"compute_texture_update"] autorelease];
324 id<MTLComputePipelineState> compute_pso = [ctx->device
325 newComputePipelineStateWithFunction:temp_compute_function
327 if (error || compute_pso == nil) {
329 "Failed to prepare texture_update MTLComputePipelineState %s",
330 [[error localizedDescription] UTF8String]);
335 specialization_cache.add_new(specialization_params, compute_pso);
336 return_pso = compute_pso;
343id<MTLComputePipelineState> gpu::MTLTexture::texture_update_1d_get_kernel(
344 TextureUpdateRoutineSpecialization specialization)
348 return mtl_texture_update_impl(specialization,
349 mtl_context->get_texture_utils().texture_1d_update_compute_psos,
353id<MTLComputePipelineState> gpu::MTLTexture::texture_update_1d_array_get_kernel(
354 TextureUpdateRoutineSpecialization specialization)
358 return mtl_texture_update_impl(
360 mtl_context->get_texture_utils().texture_1d_array_update_compute_psos,
364id<MTLComputePipelineState> gpu::MTLTexture::texture_update_2d_get_kernel(
365 TextureUpdateRoutineSpecialization specialization)
369 return mtl_texture_update_impl(specialization,
370 mtl_context->get_texture_utils().texture_2d_update_compute_psos,
374id<MTLComputePipelineState> gpu::MTLTexture::texture_update_2d_array_get_kernel(
375 TextureUpdateRoutineSpecialization specialization)
379 return mtl_texture_update_impl(
381 mtl_context->get_texture_utils().texture_2d_array_update_compute_psos,
385id<MTLComputePipelineState> gpu::MTLTexture::texture_update_3d_get_kernel(
386 TextureUpdateRoutineSpecialization specialization)
390 return mtl_texture_update_impl(specialization,
391 mtl_context->get_texture_utils().texture_3d_update_compute_psos,
398gpu::Shader *gpu::MTLTexture::depth_2d_update_sh_get(
399 DepthTextureUpdateRoutineSpecialization specialization)
406 gpu::Shader **
result = mtl_context->get_texture_utils().depth_2d_update_shaders.lookup_ptr(
412 const char *depth_2d_info_variant =
nullptr;
413 switch (specialization.data_mode) {
415 depth_2d_info_variant =
"depth_2d_update_float";
418 depth_2d_info_variant =
"depth_2d_update_int24";
421 depth_2d_info_variant =
"depth_2d_update_int32";
429 mtl_context->get_texture_utils().depth_2d_update_shaders.add_new(specialization,
shader);
433gpu::Shader *gpu::MTLTexture::fullscreen_blit_sh_get()
437 if (mtl_context->get_texture_utils().fullscreen_blit_shader ==
nullptr) {
440 mtl_context->get_texture_utils().fullscreen_blit_shader =
shader;
442 return mtl_context->get_texture_utils().fullscreen_blit_shader;
446void gpu::MTLTexture::update_sub_depth_2d(
451 TextureFormat::SFLOAT_32_DEPTH,
452 TextureFormat::UNORM_16_DEPTH,
453 TextureFormat::SFLOAT_32_DEPTH_UINT_8));
462 DepthTextureUpdateRoutineSpecialization specialization;
477 BLI_assert_msg(
false,
"Unsupported eGPUDataFormat being passed to depth texture update\n");
492 gpu::MTLTexture *mtl_tex =
static_cast<gpu::MTLTexture *
>(r32_tex_tmp);
493 mtl_tex->update_sub(mip, offset, extent, type,
data, 0);
499 if (extent[0] == w_ && extent[1] == h_) {
505 gpu::Shader *depth_2d_update_sh = depth_2d_update_sh_get(specialization);
521 GPU_scissor(offset[0], offset[1], extent[0], extent[1]);
536 if (restore_fb !=
nullptr) {
551id<MTLComputePipelineState> gpu::MTLTexture::mtl_texture_read_impl(
552 TextureReadRoutineSpecialization specialization_params,
553 Map<TextureReadRoutineSpecialization, id<MTLComputePipelineState>> &specialization_cache,
557 id<MTLComputePipelineState> *
result = specialization_cache.lookup_ptr(specialization_params);
562 id<MTLComputePipelineState> return_pso = nil;
570 NSString *tex_update_kernel_src = [NSString
574 int64_t depth_scale_factor = 1;
575 if (specialization_params.depth_format_mode > 0) {
576 BLI_assert(specialization_params.component_count_input == 1);
577 BLI_assert(specialization_params.component_count_output == 1);
578 switch (specialization_params.depth_format_mode) {
581 depth_scale_factor = 1;
585 depth_scale_factor = 0xFFFFFFu;
589 depth_scale_factor = 0xFFFFFFFFu;
598 MTLCompileOptions *
options = [[[MTLCompileOptions alloc]
init] autorelease];
599 options.languageVersion = MTLLanguageVersion2_2;
600 options.preprocessorMacros = @{
602 [NSString stringWithUTF8String:specialization_params.input_data_type.c_str()],
603 @"OUTPUT_DATA_TYPE" :
604 [NSString stringWithUTF8String:specialization_params.output_data_type.c_str()],
605 @"COMPONENT_COUNT_INPUT" :
606 [NSNumber numberWithInt:specialization_params.component_count_input],
607 @"COMPONENT_COUNT_OUTPUT" :
608 [NSNumber numberWithInt:specialization_params.component_count_output],
609 @"WRITE_COMPONENT_COUNT" :
610 [NSNumber numberWithInt:
min_ii(specialization_params.component_count_input,
611 specialization_params.component_count_output)],
613 [NSNumber numberWithInt:((specialization_params.depth_format_mode > 0) ? 1 : 0)],
614 @"DEPTH_SCALE_FACTOR" : [NSNumber numberWithLongLong:depth_scale_factor],
615 @"TEX_TYPE" : [NSNumber numberWithInt:((int)(texture_type))],
616 @"IS_DEPTHSTENCIL_24_8" :
617 [NSNumber numberWithInt:(specialization_params.depth_format_mode == 2) ? 1 : 0]
621 NSError *error =
nullptr;
622 id<MTLLibrary> temp_lib = [[ctx->device newLibraryWithSource:tex_update_kernel_src
624 error:&error] autorelease];
627 if ([[error localizedDescription] rangeOfString:
@"Compilation succeeded"].location ==
631 "Compile Error - Metal Shader Library error %s",
632 [[error localizedDescription] UTF8String]);
640 id<MTLFunction> temp_compute_function = [[temp_lib newFunctionWithName:
@"compute_texture_read"]
645 id<MTLComputePipelineState> compute_pso = [ctx->device
646 newComputePipelineStateWithFunction:temp_compute_function
648 if (error || compute_pso == nil) {
650 "Failed to prepare texture_read MTLComputePipelineState %s",
651 [[error localizedDescription] UTF8String]);
657 specialization_cache.add_new(specialization_params, compute_pso);
658 return_pso = compute_pso;
665id<MTLComputePipelineState> gpu::MTLTexture::texture_read_2d_get_kernel(
666 TextureReadRoutineSpecialization specialization)
670 return mtl_texture_read_impl(specialization,
671 mtl_context->get_texture_utils().texture_2d_read_compute_psos,
675id<MTLComputePipelineState> gpu::MTLTexture::texture_read_2d_array_get_kernel(
676 TextureReadRoutineSpecialization specialization)
680 return mtl_texture_read_impl(specialization,
681 mtl_context->get_texture_utils().texture_2d_array_read_compute_psos,
685id<MTLComputePipelineState> gpu::MTLTexture::texture_read_1d_get_kernel(
686 TextureReadRoutineSpecialization specialization)
690 return mtl_texture_read_impl(specialization,
691 mtl_context->get_texture_utils().texture_1d_read_compute_psos,
695id<MTLComputePipelineState> gpu::MTLTexture::texture_read_1d_array_get_kernel(
696 TextureReadRoutineSpecialization specialization)
700 return mtl_texture_read_impl(specialization,
701 mtl_context->get_texture_utils().texture_1d_array_read_compute_psos,
705id<MTLComputePipelineState> gpu::MTLTexture::texture_read_3d_get_kernel(
706 TextureReadRoutineSpecialization specialization)
710 return mtl_texture_read_impl(specialization,
711 mtl_context->get_texture_utils().texture_3d_read_compute_psos,
#define CLOG_ERROR(clg_ref,...)
#define GPU_batch_texture_bind(batch, name, tex)
#define GPU_batch_uniform_2f(batch, name, x, y)
#define GPU_batch_uniform_1i(batch, name, x)
#define GPU_TEXTURE_FORMAT_EXPAND(impl)
#define BLI_assert_msg(a, msg)
static MTLCapabilities & get_capabilities()
static MTLContext * get()
CCL_NAMESPACE_BEGIN struct Options options
char datatoc_compute_texture_update_msl[]
char datatoc_compute_texture_read_msl[]
size_t get_mtl_format_bytesize(MTLPixelFormat tex_format)
MTLPixelFormat gpu_texture_format_to_metal(TextureFormat tex_format)
int get_mtl_format_num_components(MTLPixelFormat tex_format)
constexpr bool validate_data_format(TextureFormat tex_format, eGPUDataFormat data_format)
bool mtl_format_supports_blending(MTLPixelFormat format)
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
void GPU_scissor_test(bool enable)
gpu::FrameBuffer * GPU_framebuffer_active_get()
void GPU_framebuffer_restore()
void GPU_framebuffer_free(gpu::FrameBuffer *fb)
GPUDepthTest GPU_depth_test_get()
@ GPU_DATA_UINT_24_8_DEPRECATED
GPUStencilTest GPU_stencil_test_get()
gpu::Shader * GPU_shader_create_from_info_name(const char *info_name)
@ GPU_SAMPLER_EXTEND_MODE_EXTEND
void GPU_depth_mask(bool depth)
void GPU_batch_set_shader(gpu::Batch *batch, gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_stencil_reference_set(uint reference)
@ MTL_DEPTH_UPDATE_MODE_INT32
@ MTL_DEPTH_UPDATE_MODE_INT24
@ MTL_DEPTH_UPDATE_MODE_FLOAT
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_ATTACHMENT
BMesh const char void * data
uint GPU_stencil_mask_get()
bool GPU_depth_mask_get()
void GPU_texture_filter_mode(gpu::Texture *texture, bool use_filter)
void GPU_stencil_test(GPUStencilTest test)
void GPU_batch_draw(gpu::Batch *batch)
gpu::FrameBuffer * GPU_framebuffer_create(const char *name)
void GPU_framebuffer_clear_depth(gpu::FrameBuffer *fb, float clear_depth)
void GPU_texture_extend_mode(gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
void GPU_scissor(int x, int y, int width, int height)
gpu::Batch * GPU_batch_preset_quad()
void GPU_framebuffer_texture_attach(gpu::FrameBuffer *fb, gpu::Texture *texture, int slot, int mip)
void GPU_texture_free(gpu::Texture *texture)
void GPU_stencil_write_mask_set(uint write_mask)
MINLINE int min_ii(int a, int b)
void GPU_depth_test(GPUDepthTest test)
void GPU_framebuffer_clear_stencil(gpu::FrameBuffer *fb, uint clear_stencil)
gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
std::string format(const std::string &fmt, Args &&...args)
std::string input_data_type
int component_count_input
std::string output_data_type
int component_count_output
bool supports_family_mac1
bool supports_family_mac_catalyst1