Blender V5.3
mtl_texture_util.mm
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_global.hh"
10
11#include "DNA_userdef_types.h"
12
13#include "GPU_batch.hh"
14#include "GPU_batch_presets.hh"
15#include "GPU_capabilities.hh"
16#include "GPU_framebuffer.hh"
17#include "GPU_platform.hh"
18#include "GPU_state.hh"
19
20#include "mtl_backend.hh"
21#include "mtl_context.hh"
22#include "mtl_debug.hh"
23#include "mtl_texture.hh"
24
25/* Utility file for secondary functionality which supports mtl_texture.mm. */
26
29
30namespace blender::gpu {
31
32static CLG_LogRef LOG = {"gpu.metal"};
33
34/* -------------------------------------------------------------------- */
37
39{
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;
43
44 switch (tex_format) {
47 break;
48 }
49#undef CASE
50 BLI_assert_msg(false, "Unrecognised GPU pixel format!\n");
51 return MTLPixelFormatRGBA8Unorm;
52}
53
54size_t get_mtl_format_bytesize(MTLPixelFormat tex_format)
55{
56 switch (tex_format) {
57 case MTLPixelFormatRGBA8Uint:
58 case MTLPixelFormatRGBA8Sint:
59 case MTLPixelFormatRGBA8Unorm:
60 case MTLPixelFormatRGBA8Snorm:
61 case MTLPixelFormatRGB10A2Uint:
62 case MTLPixelFormatRGB10A2Unorm:
63 return 4;
64 case MTLPixelFormatRGBA32Uint:
65 case MTLPixelFormatRGBA32Sint:
66 case MTLPixelFormatRGBA32Float:
67 return 16;
68 case MTLPixelFormatRGBA16Uint:
69 case MTLPixelFormatRGBA16Sint:
70 case MTLPixelFormatRGBA16Float:
71 case MTLPixelFormatRGBA16Unorm:
72 case MTLPixelFormatRGBA16Snorm:
73 return 8;
74 case MTLPixelFormatRG8Uint:
75 case MTLPixelFormatRG8Sint:
76 case MTLPixelFormatRG8Unorm:
77 case MTLPixelFormatRG8Snorm:
78 case MTLPixelFormatRG8Unorm_sRGB:
79 return 2;
80 case MTLPixelFormatRG32Uint:
81 case MTLPixelFormatRG32Sint:
82 case MTLPixelFormatRG32Float:
83 return 8;
84 case MTLPixelFormatRG16Uint:
85 case MTLPixelFormatRG16Sint:
86 case MTLPixelFormatRG16Float:
87 case MTLPixelFormatRG16Unorm:
88 case MTLPixelFormatRG16Snorm:
89 return 4;
90 case MTLPixelFormatR8Uint:
91 case MTLPixelFormatR8Sint:
92 case MTLPixelFormatR8Unorm:
93 case MTLPixelFormatR8Snorm:
94 return 1;
95 case MTLPixelFormatR32Uint:
96 case MTLPixelFormatR32Sint:
97 case MTLPixelFormatR32Float:
98 return 4;
99 case MTLPixelFormatR16Uint:
100 case MTLPixelFormatR16Sint:
101 case MTLPixelFormatR16Float:
102 case MTLPixelFormatR16Snorm:
103 case MTLPixelFormatR16Unorm:
104 return 2;
105 case MTLPixelFormatRG11B10Float:
106 return 4;
107 case MTLPixelFormatDepth32Float_Stencil8:
108 return 8;
109 case MTLPixelFormatRGBA8Unorm_sRGB:
110 case MTLPixelFormatDepth32Float:
111 case MTLPixelFormatDepth24Unorm_Stencil8:
112 return 4;
113 case MTLPixelFormatDepth16Unorm:
114 return 2;
115 case MTLPixelFormatBC1_RGBA:
116 case MTLPixelFormatBC1_RGBA_sRGB:
117 return 1; /* NOTE: not quite correct (BC1 is 0.5 BPP). */
118 case MTLPixelFormatBC2_RGBA:
119 case MTLPixelFormatBC2_RGBA_sRGB:
120 case MTLPixelFormatBC3_RGBA:
121 case MTLPixelFormatBC3_RGBA_sRGB:
122 return 1;
123
124 default:
125 BLI_assert_msg(false, "Unrecognised GPU pixel format!\n");
126 return 1;
127 }
128}
129
130int get_mtl_format_num_components(MTLPixelFormat tex_format)
131{
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:
154 return 4;
155
156 case MTLPixelFormatRG11B10Float:
157 return 3;
158
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:
172 return 2;
173
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:
189 /* Treating this format as single-channel for direct data copies -- Stencil component is not
190 * addressable. */
191 return 1;
192
193 default:
194 BLI_assert_msg(false, "Unrecognised GPU pixel format!\n");
195 return 1;
196 }
197}
198
199bool mtl_format_supports_blending(MTLPixelFormat format)
200{
201 /* Add formats as needed -- Verify platforms. */
202 const MTLCapabilities &capabilities = MTLBackend::get_capabilities();
203
204 if (capabilities.supports_family_mac1 || capabilities.supports_family_mac_catalyst1) {
205
206 switch (format) {
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:
223 return false;
224 default:
225 return true;
226 }
227 }
228 else {
229 switch (format) {
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:
247 return false;
248 default:
249 return true;
250 }
251 }
252}
253
255
256/* -------------------------------------------------------------------- */
259
260id<MTLComputePipelineState> gpu::MTLTexture::mtl_texture_update_impl(
261 TextureUpdateRoutineSpecialization specialization_params,
263 GPUTextureType texture_type)
264{
265 /* Check whether the Kernel exists. */
266 id<MTLComputePipelineState> *result = specialization_cache.lookup_ptr(specialization_params);
267 if (result != nullptr) {
268 return *result;
269 }
270
271 id<MTLComputePipelineState> return_pso = nil;
272 @autoreleasepool {
273
274 /* Fetch active context. */
275 MTLContext *ctx = MTLContext::get();
276 BLI_assert(ctx);
277
279 NSString *tex_update_kernel_src = [NSString
280 stringWithUTF8String:datatoc_compute_texture_update_msl];
281
282 /* Prepare options and specializations. */
283 MTLCompileOptions *options = [[[MTLCompileOptions alloc] init] autorelease];
284 options.languageVersion = MTLLanguageVersion2_2;
285 options.preprocessorMacros = @{
286 @"INPUT_DATA_TYPE" :
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" :
291 [NSNumber numberWithInt:specialization_params.component_count_input],
292 @"COMPONENT_COUNT_OUTPUT" :
293 [NSNumber numberWithInt:specialization_params.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))]
297 };
298
299 /* Prepare shader library for conversion routine. */
300 NSError *error = nullptr;
301 id<MTLLibrary> temp_lib = [[ctx->device newLibraryWithSource:tex_update_kernel_src
303 error:&error] autorelease];
304 if (error) {
305 /* Only exit out if genuine error and not warning. */
306 if ([[error localizedDescription] rangeOfString:@"Compilation succeeded"].location ==
307 NSNotFound)
308 {
310 "Compile Error - Metal Shader Library error %s",
311 [[error localizedDescription] UTF8String]);
312 BLI_assert(false);
313 return nil;
314 }
315 }
316
317 /* Fetch compute function. */
318 BLI_assert(temp_lib != nil);
319 id<MTLFunction> temp_compute_function = [[temp_lib
320 newFunctionWithName:@"compute_texture_update"] autorelease];
321 BLI_assert(temp_compute_function);
322
323 /* Otherwise, bake new Kernel. */
324 id<MTLComputePipelineState> compute_pso = [ctx->device
325 newComputePipelineStateWithFunction:temp_compute_function
326 error:&error];
327 if (error || compute_pso == nil) {
329 "Failed to prepare texture_update MTLComputePipelineState %s",
330 [[error localizedDescription] UTF8String]);
331 BLI_assert(false);
332 }
333
334 /* Store PSO. */
335 specialization_cache.add_new(specialization_params, compute_pso);
336 return_pso = compute_pso;
337 }
338
339 BLI_assert(return_pso != nil);
340 return return_pso;
341}
342
343id<MTLComputePipelineState> gpu::MTLTexture::texture_update_1d_get_kernel(
344 TextureUpdateRoutineSpecialization specialization)
345{
346 MTLContext *mtl_context = MTLContext::get();
347 BLI_assert(mtl_context != nullptr);
348 return mtl_texture_update_impl(specialization,
349 mtl_context->get_texture_utils().texture_1d_update_compute_psos,
351}
352
353id<MTLComputePipelineState> gpu::MTLTexture::texture_update_1d_array_get_kernel(
354 TextureUpdateRoutineSpecialization specialization)
355{
356 MTLContext *mtl_context = MTLContext::get();
357 BLI_assert(mtl_context != nullptr);
358 return mtl_texture_update_impl(
359 specialization,
360 mtl_context->get_texture_utils().texture_1d_array_update_compute_psos,
362}
363
364id<MTLComputePipelineState> gpu::MTLTexture::texture_update_2d_get_kernel(
365 TextureUpdateRoutineSpecialization specialization)
366{
367 MTLContext *mtl_context = MTLContext::get();
368 BLI_assert(mtl_context != nullptr);
369 return mtl_texture_update_impl(specialization,
370 mtl_context->get_texture_utils().texture_2d_update_compute_psos,
372}
373
374id<MTLComputePipelineState> gpu::MTLTexture::texture_update_2d_array_get_kernel(
375 TextureUpdateRoutineSpecialization specialization)
376{
377 MTLContext *mtl_context = MTLContext::get();
378 BLI_assert(mtl_context != nullptr);
379 return mtl_texture_update_impl(
380 specialization,
381 mtl_context->get_texture_utils().texture_2d_array_update_compute_psos,
383}
384
385id<MTLComputePipelineState> gpu::MTLTexture::texture_update_3d_get_kernel(
386 TextureUpdateRoutineSpecialization specialization)
387{
388 MTLContext *mtl_context = MTLContext::get();
389 BLI_assert(mtl_context != nullptr);
390 return mtl_texture_update_impl(specialization,
391 mtl_context->get_texture_utils().texture_3d_update_compute_psos,
393}
394
395/* TODO(Metal): Data upload routine kernel for texture cube and texture cube array.
396 * Currently does not appear to be hit. */
397
398gpu::Shader *gpu::MTLTexture::depth_2d_update_sh_get(
399 DepthTextureUpdateRoutineSpecialization specialization)
400{
401
402 /* Check whether the Kernel exists. */
403 MTLContext *mtl_context = MTLContext::get();
404 BLI_assert(mtl_context != nullptr);
405
406 gpu::Shader **result = mtl_context->get_texture_utils().depth_2d_update_shaders.lookup_ptr(
407 specialization);
408 if (result != nullptr) {
409 return *result;
410 }
411
412 const char *depth_2d_info_variant = nullptr;
413 switch (specialization.data_mode) {
415 depth_2d_info_variant = "depth_2d_update_float";
416 break;
418 depth_2d_info_variant = "depth_2d_update_int24";
419 break;
421 depth_2d_info_variant = "depth_2d_update_int32";
422 break;
423 default:
424 BLI_assert(false && "Invalid format mode\n");
425 return nullptr;
426 }
427
428 gpu::Shader *shader = GPU_shader_create_from_info_name(depth_2d_info_variant);
429 mtl_context->get_texture_utils().depth_2d_update_shaders.add_new(specialization, shader);
430 return shader;
431}
432
433gpu::Shader *gpu::MTLTexture::fullscreen_blit_sh_get()
434{
435 MTLContext *mtl_context = MTLContext::get();
436 BLI_assert(mtl_context != nullptr);
437 if (mtl_context->get_texture_utils().fullscreen_blit_shader == nullptr) {
438 gpu::Shader *shader = GPU_shader_create_from_info_name("fullscreen_blit");
439
440 mtl_context->get_texture_utils().fullscreen_blit_shader = shader;
441 }
442 return mtl_context->get_texture_utils().fullscreen_blit_shader;
443}
444
445/* Special routine for updating 2D depth textures using the rendering pipeline. */
446void gpu::MTLTexture::update_sub_depth_2d(
447 int mip, int offset[3], int extent[3], eGPUDataFormat type, const void *data)
448{
449 /* Verify we are in a valid configuration. */
450 BLI_assert(ELEM(format_,
451 TextureFormat::SFLOAT_32_DEPTH,
452 TextureFormat::UNORM_16_DEPTH,
453 TextureFormat::SFLOAT_32_DEPTH_UINT_8));
454 BLI_assert(validate_data_format(format_, type));
456
457 /* Determine whether we are in GPU_DATA_UINT_24_8_DEPRECATED or GPU_DATA_FLOAT mode. */
458 bool is_float = (type == GPU_DATA_FLOAT);
459 TextureFormat format = (is_float) ? TextureFormat::SFLOAT_32 : TextureFormat::SINT_32;
460
461 /* Shader key - Add parameters here for different configurations. */
462 DepthTextureUpdateRoutineSpecialization specialization;
463 switch (type) {
464 case GPU_DATA_FLOAT:
465 specialization.data_mode = MTL_DEPTH_UPDATE_MODE_FLOAT;
466 break;
467
469 specialization.data_mode = MTL_DEPTH_UPDATE_MODE_INT24;
470 break;
471
472 case GPU_DATA_UINT:
473 specialization.data_mode = MTL_DEPTH_UPDATE_MODE_INT32;
474 break;
475
476 default:
477 BLI_assert_msg(false, "Unsupported eGPUDataFormat being passed to depth texture update\n");
478 return;
479 }
480
481 /* Push contents into an r32_tex and render contents to depth using a shader. */
482 gpu::Texture *r32_tex_tmp = GPU_texture_create_2d("depth_intermediate_copy_tex",
483 w_,
484 h_,
485 1,
486 format,
489 nullptr);
490 GPU_texture_filter_mode(r32_tex_tmp, false);
492 gpu::MTLTexture *mtl_tex = static_cast<gpu::MTLTexture *>(r32_tex_tmp);
493 mtl_tex->update_sub(mip, offset, extent, type, data, 0);
494
495 gpu::FrameBuffer *restore_fb = GPU_framebuffer_active_get();
496 gpu::FrameBuffer *depth_fb_temp = GPU_framebuffer_create("depth_intermediate_copy_fb");
497 GPU_framebuffer_texture_attach(depth_fb_temp, this, 0, mip);
498 GPU_framebuffer_bind(depth_fb_temp);
499 if (extent[0] == w_ && extent[1] == h_) {
500 /* Skip load if the whole texture is being updated. */
501 GPU_framebuffer_clear_depth(depth_fb_temp, 0.0);
502 GPU_framebuffer_clear_stencil(depth_fb_temp, 0);
503 }
504
505 gpu::Shader *depth_2d_update_sh = depth_2d_update_sh_get(specialization);
506 BLI_assert(depth_2d_update_sh != nullptr);
507 Batch *quad = GPU_batch_preset_quad();
508 GPU_batch_set_shader(quad, depth_2d_update_sh);
509
510 GPU_batch_texture_bind(quad, "source_data", r32_tex_tmp);
511 GPU_batch_uniform_1i(quad, "mip", mip);
512 GPU_batch_uniform_2f(quad, "extent", (float)extent[0], (float)extent[1]);
513 GPU_batch_uniform_2f(quad, "offset", (float)offset[0], (float)offset[1]);
514 GPU_batch_uniform_2f(quad, "size", (float)w_, (float)h_);
515
516 bool depth_write_prev = GPU_depth_mask_get();
517 uint stencil_mask_prev = GPU_stencil_mask_get();
518 GPUDepthTest depth_test_prev = GPU_depth_test_get();
519 GPUStencilTest stencil_test_prev = GPU_stencil_test_get();
520 GPU_scissor_test(true);
521 GPU_scissor(offset[0], offset[1], extent[0], extent[1]);
522
526 GPU_depth_mask(true);
528
529 GPU_batch_draw(quad);
530
531 GPU_depth_mask(depth_write_prev);
532 GPU_stencil_write_mask_set(stencil_mask_prev);
533 GPU_stencil_test(stencil_test_prev);
534 GPU_depth_test(depth_test_prev);
535
536 if (restore_fb != nullptr) {
537 GPU_framebuffer_bind(restore_fb);
538 }
539 else {
541 }
542 GPU_framebuffer_free(depth_fb_temp);
543 GPU_texture_free(r32_tex_tmp);
544}
546
547/* -------------------------------------------------------------------- */
550
551id<MTLComputePipelineState> gpu::MTLTexture::mtl_texture_read_impl(
552 TextureReadRoutineSpecialization specialization_params,
553 Map<TextureReadRoutineSpecialization, id<MTLComputePipelineState>> &specialization_cache,
554 GPUTextureType texture_type)
555{
556 /* Check whether the Kernel exists. */
557 id<MTLComputePipelineState> *result = specialization_cache.lookup_ptr(specialization_params);
558 if (result != nullptr) {
559 return *result;
560 }
561
562 id<MTLComputePipelineState> return_pso = nil;
563 @autoreleasepool {
564
565 /* Fetch active context. */
567 BLI_assert(ctx);
568
570 NSString *tex_update_kernel_src = [NSString
571 stringWithUTF8String:datatoc_compute_texture_read_msl];
572
573 /* Defensive Debug Checks. */
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) {
579 case 1:
580 /* FLOAT */
581 depth_scale_factor = 1;
582 break;
583 case 2:
584 /* D24 uint */
585 depth_scale_factor = 0xFFFFFFu;
586 break;
587 case 4:
588 /* D32 uint */
589 depth_scale_factor = 0xFFFFFFFFu;
590 break;
591 default:
592 BLI_assert_msg(0, "Unrecognized mode");
593 break;
594 }
595 }
596
597 /* Prepare options and specializations. */
598 MTLCompileOptions *options = [[[MTLCompileOptions alloc] init] autorelease];
599 options.languageVersion = MTLLanguageVersion2_2;
600 options.preprocessorMacros = @{
601 @"INPUT_DATA_TYPE" :
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)],
612 @"IS_DEPTH_FORMAT" :
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]
618 };
619
620 /* Prepare shader library for conversion routine. */
621 NSError *error = nullptr;
622 id<MTLLibrary> temp_lib = [[ctx->device newLibraryWithSource:tex_update_kernel_src
624 error:&error] autorelease];
625 if (error) {
626 /* Only exit out if genuine error and not warning. */
627 if ([[error localizedDescription] rangeOfString:@"Compilation succeeded"].location ==
628 NSNotFound)
629 {
631 "Compile Error - Metal Shader Library error %s",
632 [[error localizedDescription] UTF8String]);
633 BLI_assert(false);
634 return nil;
635 }
636 }
637
638 /* Fetch compute function. */
639 BLI_assert(temp_lib != nil);
640 id<MTLFunction> temp_compute_function = [[temp_lib newFunctionWithName:@"compute_texture_read"]
641 autorelease];
642 BLI_assert(temp_compute_function);
643
644 /* Otherwise, bake new Kernel. */
645 id<MTLComputePipelineState> compute_pso = [ctx->device
646 newComputePipelineStateWithFunction:temp_compute_function
647 error:&error];
648 if (error || compute_pso == nil) {
650 "Failed to prepare texture_read MTLComputePipelineState %s",
651 [[error localizedDescription] UTF8String]);
652 BLI_assert(false);
653 return nil;
654 }
655
656 /* Store PSO. */
657 specialization_cache.add_new(specialization_params, compute_pso);
658 return_pso = compute_pso;
659 }
660
661 BLI_assert(return_pso != nil);
662 return return_pso;
663}
664
665id<MTLComputePipelineState> gpu::MTLTexture::texture_read_2d_get_kernel(
666 TextureReadRoutineSpecialization specialization)
667{
668 MTLContext *mtl_context = MTLContext::get();
669 BLI_assert(mtl_context != nullptr);
670 return mtl_texture_read_impl(specialization,
671 mtl_context->get_texture_utils().texture_2d_read_compute_psos,
673}
674
675id<MTLComputePipelineState> gpu::MTLTexture::texture_read_2d_array_get_kernel(
676 TextureReadRoutineSpecialization specialization)
677{
678 MTLContext *mtl_context = MTLContext::get();
679 BLI_assert(mtl_context != nullptr);
680 return mtl_texture_read_impl(specialization,
681 mtl_context->get_texture_utils().texture_2d_array_read_compute_psos,
683}
684
685id<MTLComputePipelineState> gpu::MTLTexture::texture_read_1d_get_kernel(
686 TextureReadRoutineSpecialization specialization)
687{
688 MTLContext *mtl_context = MTLContext::get();
689 BLI_assert(mtl_context != nullptr);
690 return mtl_texture_read_impl(specialization,
691 mtl_context->get_texture_utils().texture_1d_read_compute_psos,
693}
694
695id<MTLComputePipelineState> gpu::MTLTexture::texture_read_1d_array_get_kernel(
696 TextureReadRoutineSpecialization specialization)
697{
698 MTLContext *mtl_context = MTLContext::get();
699 BLI_assert(mtl_context != nullptr);
700 return mtl_texture_read_impl(specialization,
701 mtl_context->get_texture_utils().texture_1d_array_read_compute_psos,
703}
704
705id<MTLComputePipelineState> gpu::MTLTexture::texture_read_3d_get_kernel(
706 TextureReadRoutineSpecialization specialization)
707{
708 MTLContext *mtl_context = MTLContext::get();
709 BLI_assert(mtl_context != nullptr);
710 return mtl_texture_read_impl(specialization,
711 mtl_context->get_texture_utils().texture_3d_read_compute_psos,
713}
714
716
717} // namespace blender::gpu
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define GPU_batch_texture_bind(batch, name, tex)
Definition GPU_batch.hh:284
#define GPU_batch_uniform_2f(batch, name, x, y)
Definition GPU_batch.hh:268
#define GPU_batch_uniform_1i(batch, name, x)
Definition GPU_batch.hh:265
#define CASE(a, size, c, blender_enum, d, e, f, g, h)
#define GPU_TEXTURE_FORMAT_EXPAND(impl)
#define BLI_assert(a)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
void init()
static MTLCapabilities & get_capabilities()
static MTLContext * get()
CCL_NAMESPACE_BEGIN struct Options options
#define LOG(level)
Definition log.h:97
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)
static CLG_LogRef LOG
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)
Definition gpu_state.cc:173
gpu::FrameBuffer * GPU_framebuffer_active_get()
__int64 int64_t
Definition BKE_text.h:90
void GPU_framebuffer_restore()
unsigned int uint
Definition BKE_text.h:55
void GPU_framebuffer_free(gpu::FrameBuffer *fb)
GPUDepthTest GPU_depth_test_get()
Definition gpu_state.cc:229
@ GPU_DATA_UINT_24_8_DEPRECATED
GPUStencilTest GPU_stencil_test_get()
Definition gpu_state.cc:235
gpu::Shader * GPU_shader_create_from_info_name(const char *info_name)
@ GPU_SAMPLER_EXTEND_MODE_EXTEND
void GPU_depth_mask(bool depth)
Definition gpu_state.cc:107
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)
Definition gpu_state.cc:190
@ GPU_DEPTH_ALWAYS
Definition GPU_state.hh:133
@ 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()
Definition gpu_state.cc:223
bool GPU_depth_mask_get()
Definition gpu_state.cc:272
void GPU_texture_filter_mode(gpu::Texture *texture, bool use_filter)
void GPU_stencil_test(GPUStencilTest test)
Definition gpu_state.cc:75
void GPU_batch_draw(gpu::Batch *batch)
@ GPU_STENCIL_ALWAYS
Definition GPU_state.hh:143
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)
Definition gpu_state.cc:178
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)
Definition gpu_state.cc:195
MINLINE int min_ii(int a, int b)
void GPU_depth_test(GPUDepthTest test)
Definition gpu_state.cc:70
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)
Definition diagnostic.hh:43