37#define CASE(a, b, c, blender_enum, d, mtl_pixel_enum, f, g, h) \
38 case TextureFormat::blender_enum: \
39 return MTLPixelFormat##mtl_pixel_enum;
48 return MTLPixelFormatRGBA8Unorm;
54 case MTLPixelFormatRGBA8Uint:
55 case MTLPixelFormatRGBA8Sint:
56 case MTLPixelFormatRGBA8Unorm:
57 case MTLPixelFormatRGBA8Snorm:
58 case MTLPixelFormatRGB10A2Uint:
59 case MTLPixelFormatRGB10A2Unorm:
61 case MTLPixelFormatRGBA32Uint:
62 case MTLPixelFormatRGBA32Sint:
63 case MTLPixelFormatRGBA32Float:
65 case MTLPixelFormatRGBA16Uint:
66 case MTLPixelFormatRGBA16Sint:
67 case MTLPixelFormatRGBA16Float:
68 case MTLPixelFormatRGBA16Unorm:
69 case MTLPixelFormatRGBA16Snorm:
71 case MTLPixelFormatRG8Uint:
72 case MTLPixelFormatRG8Sint:
73 case MTLPixelFormatRG8Unorm:
74 case MTLPixelFormatRG8Snorm:
75 case MTLPixelFormatRG8Unorm_sRGB:
77 case MTLPixelFormatRG32Uint:
78 case MTLPixelFormatRG32Sint:
79 case MTLPixelFormatRG32Float:
81 case MTLPixelFormatRG16Uint:
82 case MTLPixelFormatRG16Sint:
83 case MTLPixelFormatRG16Float:
84 case MTLPixelFormatRG16Unorm:
85 case MTLPixelFormatRG16Snorm:
87 case MTLPixelFormatR8Uint:
88 case MTLPixelFormatR8Sint:
89 case MTLPixelFormatR8Unorm:
90 case MTLPixelFormatR8Snorm:
92 case MTLPixelFormatR32Uint:
93 case MTLPixelFormatR32Sint:
94 case MTLPixelFormatR32Float:
96 case MTLPixelFormatR16Uint:
97 case MTLPixelFormatR16Sint:
98 case MTLPixelFormatR16Float:
99 case MTLPixelFormatR16Snorm:
100 case MTLPixelFormatR16Unorm:
102 case MTLPixelFormatRG11B10Float:
104 case MTLPixelFormatDepth32Float_Stencil8:
106 case MTLPixelFormatRGBA8Unorm_sRGB:
107 case MTLPixelFormatDepth32Float:
108 case MTLPixelFormatDepth24Unorm_Stencil8:
110 case MTLPixelFormatDepth16Unorm:
112 case MTLPixelFormatBC1_RGBA:
113 case MTLPixelFormatBC1_RGBA_sRGB:
115 case MTLPixelFormatBC2_RGBA:
116 case MTLPixelFormatBC2_RGBA_sRGB:
117 case MTLPixelFormatBC3_RGBA:
118 case MTLPixelFormatBC3_RGBA_sRGB:
129 switch (tex_format) {
130 case MTLPixelFormatRGBA8Uint:
131 case MTLPixelFormatRGBA8Sint:
132 case MTLPixelFormatRGBA8Unorm:
133 case MTLPixelFormatRGBA8Snorm:
134 case MTLPixelFormatRGBA32Uint:
135 case MTLPixelFormatRGBA32Sint:
136 case MTLPixelFormatRGBA32Float:
137 case MTLPixelFormatRGBA16Uint:
138 case MTLPixelFormatRGBA16Sint:
139 case MTLPixelFormatRGBA16Float:
140 case MTLPixelFormatRGBA16Unorm:
141 case MTLPixelFormatRGBA16Snorm:
142 case MTLPixelFormatRGBA8Unorm_sRGB:
143 case MTLPixelFormatRGB10A2Uint:
144 case MTLPixelFormatRGB10A2Unorm:
145 case MTLPixelFormatBC1_RGBA_sRGB:
146 case MTLPixelFormatBC2_RGBA_sRGB:
147 case MTLPixelFormatBC3_RGBA_sRGB:
148 case MTLPixelFormatBC1_RGBA:
149 case MTLPixelFormatBC2_RGBA:
150 case MTLPixelFormatBC3_RGBA:
153 case MTLPixelFormatRG11B10Float:
156 case MTLPixelFormatRG8Uint:
157 case MTLPixelFormatRG8Sint:
158 case MTLPixelFormatRG8Unorm:
159 case MTLPixelFormatRG32Uint:
160 case MTLPixelFormatRG32Sint:
161 case MTLPixelFormatRG32Float:
162 case MTLPixelFormatRG16Uint:
163 case MTLPixelFormatRG16Sint:
164 case MTLPixelFormatRG16Float:
165 case MTLPixelFormatDepth32Float_Stencil8:
166 case MTLPixelFormatRG16Snorm:
167 case MTLPixelFormatRG16Unorm:
168 case MTLPixelFormatRG8Snorm:
171 case MTLPixelFormatR8Uint:
172 case MTLPixelFormatR8Sint:
173 case MTLPixelFormatR8Unorm:
174 case MTLPixelFormatR8Snorm:
175 case MTLPixelFormatR32Uint:
176 case MTLPixelFormatR32Sint:
177 case MTLPixelFormatR32Float:
178 case MTLPixelFormatR16Uint:
179 case MTLPixelFormatR16Sint:
180 case MTLPixelFormatR16Float:
181 case MTLPixelFormatR16Unorm:
182 case MTLPixelFormatR16Snorm:
183 case MTLPixelFormatDepth32Float:
184 case MTLPixelFormatDepth16Unorm:
185 case MTLPixelFormatDepth24Unorm_Stencil8:
204 case MTLPixelFormatA8Unorm:
205 case MTLPixelFormatR8Uint:
206 case MTLPixelFormatR8Sint:
207 case MTLPixelFormatR16Uint:
208 case MTLPixelFormatR16Sint:
209 case MTLPixelFormatRG32Uint:
210 case MTLPixelFormatRG32Sint:
211 case MTLPixelFormatRGBA8Uint:
212 case MTLPixelFormatRGBA8Sint:
213 case MTLPixelFormatRGBA32Uint:
214 case MTLPixelFormatRGBA32Sint:
215 case MTLPixelFormatDepth16Unorm:
216 case MTLPixelFormatDepth32Float:
217 case MTLPixelFormatInvalid:
218 case MTLPixelFormatBGR10A2Unorm:
219 case MTLPixelFormatRGB10A2Uint:
227 case MTLPixelFormatA8Unorm:
228 case MTLPixelFormatR8Uint:
229 case MTLPixelFormatR8Sint:
230 case MTLPixelFormatR16Uint:
231 case MTLPixelFormatR16Sint:
232 case MTLPixelFormatRG32Uint:
233 case MTLPixelFormatRG32Sint:
234 case MTLPixelFormatRGBA8Uint:
235 case MTLPixelFormatRGBA8Sint:
236 case MTLPixelFormatRGBA32Uint:
237 case MTLPixelFormatRGBA32Sint:
238 case MTLPixelFormatRGBA32Float:
239 case MTLPixelFormatDepth16Unorm:
240 case MTLPixelFormatDepth32Float:
241 case MTLPixelFormatInvalid:
242 case MTLPixelFormatBGR10A2Unorm:
243 case MTLPixelFormatRGB10A2Uint:
257id<MTLComputePipelineState> gpu::MTLTexture::mtl_texture_update_impl(
260 &specialization_cache,
264 id<MTLComputePipelineState> *
result = specialization_cache.lookup_ptr(specialization_params);
269 id<MTLComputePipelineState> return_pso = nil;
277 NSString *tex_update_kernel_src = [NSString
281 MTLCompileOptions *
options = [[[MTLCompileOptions alloc]
init] autorelease];
282 options.languageVersion = MTLLanguageVersion2_2;
283 options.preprocessorMacros = @{
285 [NSString stringWithUTF8String:specialization_params.
input_data_type.c_str()],
286 @"OUTPUT_DATA_TYPE" :
287 [NSString stringWithUTF8String:specialization_params.
output_data_type.c_str()],
288 @"COMPONENT_COUNT_INPUT" :
290 @"COMPONENT_COUNT_OUTPUT" :
292 @"TEX_TYPE" : [NSNumber numberWithInt:((int)(texture_type))],
293 @"IS_TEXTURE_CLEAR" :
294 [NSNumber numberWithInt:((int)(specialization_params.is_clear ? 1 : 0))]
298 NSError *
error =
nullptr;
299 id<MTLLibrary> temp_lib = [[ctx->device newLibraryWithSource:tex_update_kernel_src
304 if ([[
error localizedDescription] rangeOfString:
@"Compilation succeeded"].location ==
307 NSLog(
@"Compile Error - Metal Shader Library error %@ ",
error);
315 id<MTLFunction> temp_compute_function = [[temp_lib
316 newFunctionWithName:
@"compute_texture_update"] autorelease];
320 id<MTLComputePipelineState> compute_pso = [ctx->device
321 newComputePipelineStateWithFunction:temp_compute_function
323 if (
error || compute_pso == nil) {
324 NSLog(
@"Failed to prepare texture_update MTLComputePipelineState %@",
error);
329 specialization_cache.add_new(specialization_params, compute_pso);
330 return_pso = compute_pso;
337id<MTLComputePipelineState> gpu::MTLTexture::texture_update_1d_get_kernel(
338 TextureUpdateRoutineSpecialisation specialization)
342 return mtl_texture_update_impl(specialization,
343 mtl_context->get_texture_utils().texture_1d_update_compute_psos,
347id<MTLComputePipelineState> gpu::MTLTexture::texture_update_1d_array_get_kernel(
348 TextureUpdateRoutineSpecialisation specialization)
352 return mtl_texture_update_impl(
354 mtl_context->get_texture_utils().texture_1d_array_update_compute_psos,
358id<MTLComputePipelineState> gpu::MTLTexture::texture_update_2d_get_kernel(
359 TextureUpdateRoutineSpecialisation specialization)
363 return mtl_texture_update_impl(specialization,
364 mtl_context->get_texture_utils().texture_2d_update_compute_psos,
368id<MTLComputePipelineState> gpu::MTLTexture::texture_update_2d_array_get_kernel(
369 TextureUpdateRoutineSpecialisation specialization)
373 return mtl_texture_update_impl(
375 mtl_context->get_texture_utils().texture_2d_array_update_compute_psos,
379id<MTLComputePipelineState> gpu::MTLTexture::texture_update_3d_get_kernel(
380 TextureUpdateRoutineSpecialisation specialization)
384 return mtl_texture_update_impl(specialization,
385 mtl_context->get_texture_utils().texture_3d_update_compute_psos,
392gpu::Shader *gpu::MTLTexture::depth_2d_update_sh_get(
393 DepthTextureUpdateRoutineSpecialisation specialization)
400 gpu::Shader **
result = mtl_context->get_texture_utils().depth_2d_update_shaders.lookup_ptr(
406 const char *depth_2d_info_variant =
nullptr;
409 depth_2d_info_variant =
"depth_2d_update_float";
412 depth_2d_info_variant =
"depth_2d_update_int24";
415 depth_2d_info_variant =
"depth_2d_update_int32";
423 mtl_context->get_texture_utils().depth_2d_update_shaders.add_new(specialization,
shader);
427gpu::Shader *gpu::MTLTexture::fullscreen_blit_sh_get()
431 if (mtl_context->get_texture_utils().fullscreen_blit_shader ==
nullptr) {
434 mtl_context->get_texture_utils().fullscreen_blit_shader =
shader;
436 return mtl_context->get_texture_utils().fullscreen_blit_shader;
440void gpu::MTLTexture::update_sub_depth_2d(
445 TextureFormat::SFLOAT_32_DEPTH,
446 TextureFormat::UNORM_16_DEPTH,
447 TextureFormat::SFLOAT_32_DEPTH_UINT_8));
456 DepthTextureUpdateRoutineSpecialisation specialization;
471 BLI_assert_msg(
false,
"Unsupported eGPUDataFormat being passed to depth texture update\n");
486 gpu::MTLTexture *mtl_tex =
static_cast<gpu::MTLTexture *
>(r32_tex_tmp);
487 mtl_tex->update_sub(mip, offset, extent, type,
data);
493 if (extent[0] == w_ && extent[1] == h_) {
499 gpu::Shader *depth_2d_update_sh = depth_2d_update_sh_get(specialization);
515 GPU_scissor(offset[0], offset[1], extent[0], extent[1]);
530 if (restore_fb !=
nullptr) {
545id<MTLComputePipelineState> gpu::MTLTexture::mtl_texture_read_impl(
546 TextureReadRoutineSpecialisation specialization_params,
547 blender::Map<TextureReadRoutineSpecialisation, id<MTLComputePipelineState>>
548 &specialization_cache,
552 id<MTLComputePipelineState> *
result = specialization_cache.lookup_ptr(specialization_params);
557 id<MTLComputePipelineState> return_pso = nil;
565 NSString *tex_update_kernel_src = [NSString
569 int64_t depth_scale_factor = 1;
576 depth_scale_factor = 1;
580 depth_scale_factor = 0xFFFFFFu;
584 depth_scale_factor = 0xFFFFFFFFu;
593 MTLCompileOptions *
options = [[[MTLCompileOptions alloc]
init] autorelease];
594 options.languageVersion = MTLLanguageVersion2_2;
595 options.preprocessorMacros = @{
597 [NSString stringWithUTF8String:specialization_params.
input_data_type.c_str()],
598 @"OUTPUT_DATA_TYPE" :
599 [NSString stringWithUTF8String:specialization_params.
output_data_type.c_str()],
600 @"COMPONENT_COUNT_INPUT" :
602 @"COMPONENT_COUNT_OUTPUT" :
604 @"WRITE_COMPONENT_COUNT" :
608 [NSNumber numberWithInt:((specialization_params.
depth_format_mode > 0) ? 1 : 0)],
609 @"DEPTH_SCALE_FACTOR" : [NSNumber numberWithLongLong:depth_scale_factor],
610 @"TEX_TYPE" : [NSNumber numberWithInt:((int)(texture_type))],
611 @"IS_DEPTHSTENCIL_24_8" :
612 [NSNumber numberWithInt:(specialization_params.depth_format_mode == 2) ? 1 : 0]
616 NSError *
error =
nullptr;
617 id<MTLLibrary> temp_lib = [[ctx->device newLibraryWithSource:tex_update_kernel_src
622 if ([[
error localizedDescription] rangeOfString:
@"Compilation succeeded"].location ==
625 NSLog(
@"Compile Error - Metal Shader Library error %@ ",
error);
633 id<MTLFunction> temp_compute_function = [[temp_lib newFunctionWithName:
@"compute_texture_read"]
638 id<MTLComputePipelineState> compute_pso = [ctx->device
639 newComputePipelineStateWithFunction:temp_compute_function
641 if (
error || compute_pso == nil) {
642 NSLog(
@"Failed to prepare texture_read MTLComputePipelineState %@",
error);
648 specialization_cache.add_new(specialization_params, compute_pso);
649 return_pso = compute_pso;
656id<MTLComputePipelineState> gpu::MTLTexture::texture_read_2d_get_kernel(
657 TextureReadRoutineSpecialisation specialization)
661 return mtl_texture_read_impl(specialization,
662 mtl_context->get_texture_utils().texture_2d_read_compute_psos,
666id<MTLComputePipelineState> gpu::MTLTexture::texture_read_2d_array_get_kernel(
667 TextureReadRoutineSpecialisation specialization)
671 return mtl_texture_read_impl(specialization,
672 mtl_context->get_texture_utils().texture_2d_array_read_compute_psos,
676id<MTLComputePipelineState> gpu::MTLTexture::texture_read_1d_get_kernel(
677 TextureReadRoutineSpecialisation specialization)
681 return mtl_texture_read_impl(specialization,
682 mtl_context->get_texture_utils().texture_1d_read_compute_psos,
686id<MTLComputePipelineState> gpu::MTLTexture::texture_read_1d_array_get_kernel(
687 TextureReadRoutineSpecialisation specialization)
691 return mtl_texture_read_impl(specialization,
692 mtl_context->get_texture_utils().texture_1d_array_read_compute_psos,
696id<MTLComputePipelineState> gpu::MTLTexture::texture_read_3d_get_kernel(
697 TextureReadRoutineSpecialisation specialization)
701 return mtl_texture_read_impl(specialization,
702 mtl_context->get_texture_utils().texture_3d_read_compute_psos,
#define BLI_assert_msg(a, msg)
MINLINE int min_ii(int a, int b)
#define GPU_batch_texture_bind(batch, name, tex)
void GPU_batch_draw(blender::gpu::Batch *batch)
void GPU_batch_set_shader(blender::gpu::Batch *batch, blender::gpu::Shader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
#define GPU_batch_uniform_2f(batch, name, x, y)
#define GPU_batch_uniform_1i(batch, name, x)
blender::gpu::Batch * GPU_batch_preset_quad()
blender::gpu::FrameBuffer * GPU_framebuffer_create(const char *name)
void GPU_framebuffer_clear_stencil(blender::gpu::FrameBuffer *fb, uint clear_stencil)
void GPU_framebuffer_restore()
void GPU_framebuffer_free(blender::gpu::FrameBuffer *fb)
void GPU_framebuffer_texture_attach(blender::gpu::FrameBuffer *fb, blender::gpu::Texture *texture, int slot, int mip)
void GPU_framebuffer_clear_depth(blender::gpu::FrameBuffer *fb, float clear_depth)
void GPU_framebuffer_bind(blender::gpu::FrameBuffer *fb)
blender::gpu::FrameBuffer * GPU_framebuffer_active_get()
blender::gpu::Shader * GPU_shader_create_from_info_name(const char *info_name)
GPUStencilTest GPU_stencil_test_get()
void GPU_scissor_test(bool enable)
void GPU_depth_mask(bool depth)
void GPU_stencil_write_mask_set(uint write_mask)
void GPU_depth_test(GPUDepthTest test)
void GPU_stencil_test(GPUStencilTest test)
void GPU_stencil_reference_set(uint reference)
void GPU_scissor(int x, int y, int width, int height)
GPUDepthTest GPU_depth_test_get()
uint GPU_stencil_mask_get()
bool GPU_depth_mask_get()
#define GPU_TEXTURE_FORMAT_EXPAND(impl)
@ GPU_DATA_UINT_24_8_DEPRECATED
void GPU_texture_extend_mode(blender::gpu::Texture *texture, GPUSamplerExtendMode extend_mode)
@ GPU_TEXTURE_USAGE_SHADER_READ
@ GPU_TEXTURE_USAGE_ATTACHMENT
@ GPU_SAMPLER_EXTEND_MODE_EXTEND
blender::gpu::Texture * GPU_texture_create_2d(const char *name, int width, int height, int mip_len, blender::gpu::TextureFormat format, eGPUTextureUsage usage, const float *data)
void GPU_texture_filter_mode(blender::gpu::Texture *texture, bool use_filter)
void GPU_texture_free(blender::gpu::Texture *texture)
BMesh const char void * data
static MTLCapabilities & get_capabilities()
static MTLContext * get()
CCL_NAMESPACE_BEGIN struct Options options
blender::gpu::Batch * quad
static void error(const char *str)
@ MTL_DEPTH_UPDATE_MODE_INT32
@ MTL_DEPTH_UPDATE_MODE_INT24
@ MTL_DEPTH_UPDATE_MODE_FLOAT
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)
static void init(bNodeTree *, bNode *node)
DepthTextureUpdateMode data_mode
std::string output_data_type
std::string input_data_type
int component_count_input
int component_count_output
int component_count_output
int component_count_input
std::string input_data_type
std::string output_data_type
bool supports_family_mac1
bool supports_family_mac_catalyst1