Blender V5.3
mtl_texture.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 "BLI_math_half.hh"
12
13#include "DNA_userdef_types.h"
14
15#include "GPU_batch.hh"
16#include "GPU_batch_presets.hh"
17#include "GPU_capabilities.hh"
18#include "GPU_framebuffer.hh"
19#include "GPU_immediate.hh"
20#include "GPU_platform.hh"
21#include "GPU_state.hh"
22
23#include "MEM_guardedalloc.h"
24#include "mtl_backend.hh"
25#include "mtl_common.hh"
26#include "mtl_context.hh"
27#include "mtl_debug.hh"
28#include "mtl_storage_buffer.hh"
29#include "mtl_texture.hh"
30#include "mtl_vertex_buffer.hh"
31
32namespace blender::gpu {
33
34/* -------------------------------------------------------------------- */
37
38void gpu::MTLTexture::mtl_texture_init()
39{
40 BLI_assert(MTLContext::get() != nullptr);
41
42 /* Status. */
43 is_baked_ = false;
44 is_dirty_ = false;
45 resource_mode_ = MTL_TEXTURE_MODE_DEFAULT;
46 mtl_max_mips_ = 1;
47
48 /* Metal properties. */
49 texture_ = nil;
50 mip_swizzle_view_ = nil;
51
52 /* VBO. */
53 vert_buffer_ = nullptr;
54 vert_buffer_mtl_ = nil;
55
56 /* Default Swizzle. */
57 tex_swizzle_mask_[0] = 'r';
58 tex_swizzle_mask_[1] = 'g';
59 tex_swizzle_mask_[2] = 'b';
60 tex_swizzle_mask_[3] = 'a';
61 mtl_swizzle_mask_ = MTLTextureSwizzleChannelsMake(
62 MTLTextureSwizzleRed, MTLTextureSwizzleGreen, MTLTextureSwizzleBlue, MTLTextureSwizzleAlpha);
63}
64
65gpu::MTLTexture::MTLTexture(const char *name) : Texture(name)
66{
67 /* Common Initialization. */
68 mtl_texture_init();
69}
70
72 TextureFormat format,
73 GPUTextureType type,
74 id<MTLTexture> metal_texture)
75 : Texture(name)
76{
77 /* Common Initialization. */
78 mtl_texture_init();
79
80 /* Prep texture from METAL handle. */
81 BLI_assert(metal_texture != nil);
83 type_ = type;
84 init_2D((int)metal_texture.width, (int)metal_texture.height, 0, 1, format);
85
86 /* Assign MTLTexture. */
87 texture_ = metal_texture;
88 [texture_ retain];
89 internal_gpu_image_usage_flags_ = gpu_usage_from_mtl(metal_texture.usage);
90 gpu_image_usage_flags_ = internal_gpu_image_usage_flags_;
91
92 /* Flag as Baked. */
93 is_baked_ = true;
94 is_dirty_ = false;
95 resource_mode_ = MTL_TEXTURE_MODE_EXTERNAL;
96}
97
99{
100 /* Unbind if bound. */
102 if (ctx != nullptr) {
103 ctx->state_manager->texture_unbind(this);
104 }
105
106 /* Free memory. */
107 this->reset();
108}
109
111
112/* -------------------------------------------------------------------- */
115
116void gpu::MTLTexture::bake_mip_swizzle_view()
117{
118 if (texture_view_dirty_flags_) {
119
120 /* Optimization: only generate texture view for mipmapped textures if base level > 0
121 * and max level does not match the existing number of mips.
122 * Only apply this if mipmap is the only change, and we have not previously generated
123 * a texture view. For textures which are created as views, this should also be skipped. */
124 if (resource_mode_ != MTL_TEXTURE_MODE_TEXTURE_VIEW &&
125 texture_view_dirty_flags_ == TEXTURE_VIEW_MIP_DIRTY && mip_swizzle_view_ == nil)
126 {
127
128 if (mip_min_ == 0 && mipmaps_ == mtl_max_mips_) {
129 texture_view_dirty_flags_ = TEXTURE_VIEW_NOT_DIRTY;
130 return;
131 }
132 }
133
134 /* if a texture view was previously created we release it. */
135 if (mip_swizzle_view_ != nil) {
136 [mip_swizzle_view_ release];
137 mip_swizzle_view_ = nil;
138 }
139
140 /* Use source texture to determine range limits. If we are using a GPU texture view, the range
141 * check should only validate the range */
142 const gpu::Texture *tex_view_src = this;
143 if (resource_mode_ == MTL_TEXTURE_MODE_TEXTURE_VIEW) {
144 tex_view_src = source_texture_;
145 }
146
147 /* Determine num slices */
148 int max_slices = 1;
149 int num_slices = 1;
150 switch (type_) {
152 max_slices = tex_view_src->height_get();
153 num_slices = h_;
154 break;
156 max_slices = tex_view_src->depth_get();
157 num_slices = d_;
158 break;
159 case GPU_TEXTURE_CUBE:
160 max_slices = 6;
161 num_slices = 6;
162 break;
164 /* d_ is equal to array levels * 6, including face count. */
165 max_slices = tex_view_src->depth_get();
166 num_slices = d_;
167 break;
168 default:
169 num_slices = 1;
170 break;
171 }
172
173 /* Determine texture view format. If texture view is used as a stencil view, we instead provide
174 * the equivalent format for performing stencil reads/samples. */
175 MTLPixelFormat texture_view_pixel_format = gpu_texture_format_to_metal(format_);
176 if (texture_view_stencil_) {
177 switch (texture_view_pixel_format) {
178 case MTLPixelFormatDepth24Unorm_Stencil8:
179 texture_view_pixel_format = MTLPixelFormatX24_Stencil8;
180 break;
181 case MTLPixelFormatDepth32Float_Stencil8:
182 texture_view_pixel_format = MTLPixelFormatX32_Stencil8;
183 break;
184 default:
185 BLI_assert_msg(false, "Texture format does not support stencil views.");
186 break;
187 }
188 }
189
190 /* NOTE: Texture type for cube maps can be overridden as a 2D array. This is done
191 * via modifying this textures type flags. */
192 MTLTextureType texture_view_texture_type = to_metal_type(type_);
193
194 /* Ensure we have texture view usage flagged.
195 * NOTE: This check exists in high level GPU API, however does not cover internal Metal backend
196 * uses of texture views such as when required to support SRGB enablement toggle during
197 * rendering. */
199 (texture_view_pixel_format == texture_.pixelFormat) ||
200 (internal_gpu_image_usage_flags_ & GPU_TEXTURE_USAGE_FORMAT_VIEW),
201 "Usage Flag GPU_TEXTURE_USAGE_FORMAT_VIEW must be specified if a texture view is "
202 "created with a different format to its source texture.");
203
204 int range_len = min_ii((mip_max_ - mip_min_) + 1, (int)texture_.mipmapLevelCount - mip_min_);
205 BLI_assert(range_len > 0);
206 BLI_assert(mip_min_ < texture_.mipmapLevelCount);
207 BLI_assert(view_layer_start_ < max_slices);
208 UNUSED_VARS_NDEBUG(max_slices);
209 mip_swizzle_view_ = [texture_
210 newTextureViewWithPixelFormat:texture_view_pixel_format
211 textureType:texture_view_texture_type
212 levels:NSMakeRange(mip_min_, range_len)
213 slices:NSMakeRange(view_layer_start_, num_slices)
214 swizzle:mtl_swizzle_mask_];
216 "Updating texture view - MIP TEXTURE BASE LEVEL: %d, MAX LEVEL: %d (Range len: %d)",
217 mip_min_,
218 min_ii(mip_max_, (int)texture_.mipmapLevelCount),
219 range_len);
220#ifndef NDEBUG
221 mip_swizzle_view_.label = [NSString
222 stringWithFormat:
223 @"MipSwizzleView_%s__format=%u_type=%u_baselevel=%u_numlevels=%u_swizzle='%c%c%c%c'",
224 [[texture_ label] UTF8String],
225 (uint)texture_view_pixel_format,
226 (uint)texture_view_texture_type,
227 (uint)mip_min_,
228 (uint)range_len,
229 tex_swizzle_mask_[0],
230 tex_swizzle_mask_[1],
231 tex_swizzle_mask_[2],
232 tex_swizzle_mask_[3]];
233#else
234 mip_swizzle_view_.label = [texture_ label];
235#endif
236 texture_view_dirty_flags_ = TEXTURE_VIEW_NOT_DIRTY;
237 }
238}
239
241
242/* -------------------------------------------------------------------- */
245
247{
248
249 /* Verify VBO texture shares same buffer. */
250 if (resource_mode_ == MTL_TEXTURE_MODE_VBO) {
251 id<MTLBuffer> buf = vert_buffer_->get_metal_buffer();
252
253 /* Source vertex buffer has been re-generated, require re-initialization. */
254 if (buf != vert_buffer_mtl_) {
256 "MTLTexture '%p' using MTL_TEXTURE_MODE_VBO requires re-generation due to updated "
257 "Vertex-Buffer.",
258 this);
259 /* Clear state. */
260 this->reset();
261
262 /* Re-initialize. */
263 this->init_internal(vert_buffer_);
264
265 /* Update for assertion check below. */
266 buf = vert_buffer_->get_metal_buffer();
267 }
268
269 /* Ensure buffer is valid.
270 * Fetch-vert buffer handle directly in-case it changed above. */
271 BLI_assert(vert_buffer_mtl_ != nil);
272 BLI_assert(vert_buffer_->get_metal_buffer() == vert_buffer_mtl_);
273 }
274
275 /* ensure up to date and baked. */
276 this->ensure_baked();
277
278 if (is_baked_) {
279 /* For explicit texture views, ensure we always return the texture view. */
280 if (resource_mode_ == MTL_TEXTURE_MODE_TEXTURE_VIEW) {
281 BLI_assert_msg(mip_swizzle_view_, "Texture view should always have a valid handle.");
282 }
283
284 if (mip_swizzle_view_ != nil || texture_view_dirty_flags_) {
285 bake_mip_swizzle_view();
286
287 /* Optimization: If texture view does not change mip parameters, no texture view will be
288 * baked. This is because texture views remove the ability to perform lossless compression.
289 */
290 if (mip_swizzle_view_ != nil) {
291 return mip_swizzle_view_;
292 }
293 }
294 return texture_;
295 }
296 return nil;
297}
298
300{
301
302 /* ensure up to date and baked. */
303 this->ensure_baked();
304
305 /* For explicit texture views, always return the texture view. */
306 if (resource_mode_ == MTL_TEXTURE_MODE_TEXTURE_VIEW) {
307 BLI_assert_msg(mip_swizzle_view_, "Texture view should always have a valid handle.");
308 if (mip_swizzle_view_ != nil || texture_view_dirty_flags_) {
309 bake_mip_swizzle_view();
310 }
311 BLI_assert(mip_swizzle_view_ != nil);
312 return mip_swizzle_view_;
313 }
314
315 /* Return base handle. */
316 if (is_baked_) {
317 return texture_;
318 }
319 return nil;
320}
321
322void gpu::MTLTexture::blit(id<MTLBlitCommandEncoder> blit_encoder,
323 uint src_x_offset,
324 uint src_y_offset,
325 uint src_z_offset,
326 uint src_slice,
327 uint src_mip,
328 gpu::MTLTexture *dst,
329 uint dst_x_offset,
330 uint dst_y_offset,
331 uint dst_z_offset,
332 uint dst_slice,
333 uint dst_mip,
334 uint width,
335 uint height,
336 uint depth)
337{
338
339 BLI_assert(dst);
340 BLI_assert(width > 0 && height > 0 && depth > 0);
341 MTLSize src_size = MTLSizeMake(width, height, depth);
342 MTLOrigin src_origin = MTLOriginMake(src_x_offset, src_y_offset, src_z_offset);
343 MTLOrigin dst_origin = MTLOriginMake(dst_x_offset, dst_y_offset, dst_z_offset);
344
345 if (!((this->format_get() == dst->format_get()) ||
346 (this->format_get() == TextureFormat::SRGBA_8_8_8_8 &&
347 dst->format_get() == TextureFormat::UNORM_8_8_8_8) ||
348 (this->format_get() == TextureFormat::UNORM_8_8_8_8 &&
349 dst->format_get() == TextureFormat::SRGBA_8_8_8_8)))
350 {
352 "gpu::MTLTexture: Cannot copy between two textures of different types using a "
353 "blit encoder. TODO: Support this operation");
354 return;
355 }
356
357 /* TODO(Metal): Verify if we want to use the one with modified base-level/texture view
358 * or not. */
359 [blit_encoder copyFromTexture:this->get_metal_handle_base()
360 sourceSlice:src_slice
361 sourceLevel:src_mip
362 sourceOrigin:src_origin
363 sourceSize:src_size
364 toTexture:dst->get_metal_handle_base()
365 destinationSlice:dst_slice
366 destinationLevel:dst_mip
367 destinationOrigin:dst_origin];
368}
369
370void gpu::MTLTexture::blit(gpu::MTLTexture *dst,
371 uint src_x_offset,
372 uint src_y_offset,
373 uint dst_x_offset,
374 uint dst_y_offset,
375 uint src_mip,
376 uint dst_mip,
377 uint dst_slice,
378 int width,
379 int height)
380{
381 BLI_assert(this->type_get() == dst->type_get());
382
383 gpu::Shader *shader = fullscreen_blit_sh_get();
384 BLI_assert(shader != nullptr);
386
387 /* Fetch restore framebuffer and blit target framebuffer from destination texture. */
389 gpu::FrameBuffer *blit_target_fb = dst->get_blit_framebuffer(dst_slice, dst_mip);
390 BLI_assert(blit_target_fb);
391 GPU_framebuffer_bind(blit_target_fb);
392
393 /* Execute graphics draw call to perform the blit. */
394 Batch *quad = GPU_batch_preset_quad();
396
397 float w = dst->width_get();
398 float h = dst->height_get();
399
400 GPU_shader_uniform_2f(shader, "fullscreen", w, h);
401 GPU_shader_uniform_2f(shader, "src_offset", src_x_offset, src_y_offset);
402 GPU_shader_uniform_2f(shader, "dst_offset", dst_x_offset, dst_y_offset);
403 GPU_shader_uniform_2f(shader, "size", width, height);
404
405 GPU_shader_uniform_1i(shader, "mip", src_mip);
406 GPU_batch_texture_bind(quad, "imageTexture", this);
407
408 /* Caching previous pipeline state. */
409 bool depth_write_prev = GPU_depth_mask_get();
410 uint stencil_mask_prev = GPU_stencil_mask_get();
411 GPUStencilTest stencil_test_prev = GPU_stencil_test_get();
412 GPUFaceCullTest culling_test_prev = GPU_face_culling_get();
413 GPUBlend blend_prev = GPU_blend_get();
414 GPUDepthTest depth_test_prev = GPU_depth_test_get();
415 GPU_scissor_test(false);
416
417 /* Apply state for blit draw call. */
422 GPU_depth_mask(false);
425
426 GPU_batch_draw(quad);
427
428 /* restoring old pipeline state. */
429 GPU_depth_mask(depth_write_prev);
430 GPU_stencil_write_mask_set(stencil_mask_prev);
431 GPU_stencil_test(stencil_test_prev);
432 GPU_face_culling(culling_test_prev);
433 GPU_depth_mask(depth_write_prev);
434 GPU_blend(blend_prev);
435 GPU_depth_test(depth_test_prev);
436
437 if (restore_fb != nullptr) {
438 GPU_framebuffer_bind(restore_fb);
439 }
440 else {
442 }
443}
444
445gpu::FrameBuffer *gpu::MTLTexture::get_blit_framebuffer(int dst_slice, uint dst_mip)
446{
447
448 /* Check if layer has changed. */
449 bool update_attachments = false;
450 if (!blit_fb_) {
451 std::string fb_name = this->name_ + "_blit_fb";
452 blit_fb_ = GPU_framebuffer_create(fb_name.c_str());
453 update_attachments = true;
454 }
455
456 /* Check if current blit FB has the correct attachment properties. */
457 if (blit_fb_) {
458 if (blit_fb_slice_ != dst_slice || blit_fb_mip_ != dst_mip) {
459 update_attachments = true;
460 }
461 }
462
463 if (update_attachments) {
464 if (format_flag_ & GPU_FORMAT_DEPTH || format_flag_ & GPU_FORMAT_STENCIL) {
465 /* DEPTH TEX */
467 &blit_fb_,
468 {GPU_ATTACHMENT_TEXTURE_LAYER_MIP(this, int(dst_slice), int(dst_mip)),
470 }
471 else {
472 /* COLOR TEX */
474 &blit_fb_,
476 GPU_ATTACHMENT_TEXTURE_LAYER_MIP(this, int(dst_slice), int(dst_mip))});
477 }
478 blit_fb_slice_ = dst_slice;
479 blit_fb_mip_ = dst_mip;
480 }
481
482 BLI_assert(blit_fb_);
483 return blit_fb_;
484}
485
487{
489 sampler_state.state = this->sampler_state;
490 /* Add more parameters as needed */
491 return sampler_state;
492}
493
495 int offset[3],
496 int extent[3],
497 eGPUDataFormat type,
498 const void *data,
499 const uint unpack_row_length)
500{
501 /* Fetch active context. */
503 BLI_assert(ctx);
504
505 /* Do not update texture view. */
506 BLI_assert(resource_mode_ != MTL_TEXTURE_MODE_TEXTURE_VIEW);
507
508 /* Ensure mipmaps. */
509 this->ensure_mipmaps(mip);
510
511 /* Ensure texture is baked. */
512 this->ensure_baked();
513
514 /* Safety checks. */
515 BLI_assert(mip >= mip_min_ && mip <= mip_max_);
516 BLI_assert(mip < texture_.mipmapLevelCount);
517 BLI_assert(texture_.mipmapLevelCount >= mip_max_);
518
519 /* If `unpack_row_length` is 0, rows are sequentially stored. Otherwise we unpack data
520 * into a staging block, so the half conversion below doesn't happen on the full input. */
521 const bool do_texture_unpack = !ELEM(unpack_row_length, 0, extent[0]);
522
523 /* Unpack `data` if `unpack_row_length` is set. */
524 std::unique_ptr<uint8_t, MEM_smart_ptr_deleter<uint8_t>> unpack_buffer = nullptr;
525 if (do_texture_unpack) {
527 "Compressed data with unpack_row_length != 0 is not supported.");
528 BLI_assert_msg(extent[2] <= 1,
529 "3D texture data with unpack_row_length != 0 is not supported.");
530
531 size_t src_row_stride = unpack_row_length * to_bytesize(format_, type);
532 size_t dst_row_stride = max_ii(extent[0], 1) * to_bytesize(format_, type);
533 size_t dst_total_count = dst_row_stride * max_ii(extent[1], 1) * max_ii(extent[2], 1);
534
535 /* Allocate buffer to size necessary for gather */
536 unpack_buffer.reset((uint8_t *)MEM_new_uninitialized_aligned(dst_total_count, 128, __func__));
537
538 /* Strided loop; we advance source and destination pointers separately during a gather. */
539 const uint8_t *src_ptr = static_cast<const uint8_t *>(data);
540 uint8_t *dst_ptr = unpack_buffer.get();
541 for (int y = 0; y < max_ii(extent[1], 1); ++y) {
542 std::memcpy(dst_ptr, src_ptr, dst_row_stride);
543 src_ptr += src_row_stride;
544 dst_ptr += dst_row_stride;
545 }
546
547 /* Replace the 'data' ptr with `unpack_buffer`,
548 * which has lifetime in the function scope. */
549 data = unpack_buffer.get();
550 }
551
552 std::unique_ptr<uint16_t, MEM_smart_ptr_deleter<uint16_t>> clamped_half_buffer = nullptr;
553
554 if (data != nullptr && type == GPU_DATA_FLOAT && is_half_float(format_)) {
555 size_t pixel_count = max_ii(extent[0], 1) * max_ii(extent[1], 1) * max_ii(extent[2], 1);
556 size_t total_component_count = to_component_len(format_) * pixel_count;
557
558 clamped_half_buffer.reset(static_cast<uint16_t *>(
559 MEM_new_uninitialized_aligned(sizeof(uint16_t) * total_component_count, 128, __func__)));
560
561 Span<float> src(static_cast<const float *>(data), total_component_count);
562 MutableSpan<uint16_t> dst(static_cast<uint16_t *>(clamped_half_buffer.get()),
563 total_component_count);
564
565 constexpr int64_t chunk_size = 4 * 1024 * 1024;
566
568 IndexRange(total_component_count), chunk_size, [&](const IndexRange range) {
569 /* Doing float to half conversion manually to avoid implementation specific behavior
570 * regarding Inf and NaNs. Use make finite version to avoid unexpected black pixels on
571 * certain implementation. For platform parity we clamp these infinite values to finite
572 * values. */
574 src.slice(range).data(), dst.slice(range).data(), range.size());
575 });
576 data = clamped_half_buffer.get();
577 type = GPU_DATA_HALF_FLOAT;
578 }
579
580 /* DEPTH FLAG - Depth formats cannot use direct BLIT - pass off to their own routine which will
581 * do a depth-only render. */
582 bool is_depth_format = (format_flag_ & GPU_FORMAT_DEPTH);
583 if (is_depth_format) {
584 switch (type_) {
585
586 case GPU_TEXTURE_2D:
587 update_sub_depth_2d(mip, offset, extent, type, data);
588 return;
589 default:
591 "gpu::MTLTexture::update_sub not yet supported for other depth "
592 "configurations");
593 return;
594 }
595 }
596
597 const bool is_compressed = (format_flag_ & GPU_FORMAT_COMPRESSED);
598
599 @autoreleasepool {
600 /* Determine totalsize of INPUT Data. */
601 int num_channels = to_component_len(format_);
602 size_t input_bytes_per_pixel = to_bytesize(format_, type);
603 size_t totalsize = 0;
604
605 size_t expected_update_w = extent[0];
606
607 /* Ensure calculated total size isn't larger than remaining image data size. */
608 if (is_compressed) {
609 /* Calculate size requirement for incoming compressed texture data. */
610 totalsize = ((expected_update_w + 3) / 4) * ((extent[1] + 3) / 4) * to_block_size(format_);
611 }
612 else {
613 switch (this->dimensions_count()) {
614 case 1:
615 totalsize = input_bytes_per_pixel * max_ulul(expected_update_w, 1);
616 break;
617 case 2:
618 totalsize = input_bytes_per_pixel * max_ulul(expected_update_w, 1) * size_t(extent[1]);
619 break;
620 case 3:
621 totalsize = input_bytes_per_pixel * max_ulul(expected_update_w, 1) * size_t(extent[1]) *
622 size_t(extent[2]);
623 break;
624 default:
625 BLI_assert(false);
626 break;
627 }
628 }
629
630 /* Early exit if update size is zero. update_sub sometimes has a zero-sized
631 * extent when called from texture painting. */
632 if (totalsize <= 0 || extent[0] <= 0) {
634 "MTLTexture::update_sub called with extent size of zero for one or more dimensions. "
635 "(%d, %d, %d) - DimCount: %u",
636 extent[0],
637 extent[1],
638 extent[2],
639 this->dimensions_count());
640 return;
641 }
642
643 /* Check */
644 BLI_assert(totalsize > 0);
645
646 /* Determine expected destination data size. */
647 MTLPixelFormat destination_format = gpu_texture_format_to_metal(format_);
648 size_t expected_dst_bytes_per_pixel = get_mtl_format_bytesize(destination_format);
649 int destination_num_channels = get_mtl_format_num_components(destination_format);
650
651 /* Prepare specialization struct (For texture update routine). */
652 TextureUpdateRoutineSpecialization compute_specialization_kernel = {
653 tex_data_format_to_msl_type_str(type), /* INPUT DATA FORMAT */
654 tex_data_format_to_msl_texture_template_type(type), /* TEXTURE DATA FORMAT */
655 num_channels,
656 destination_num_channels,
657 false /* Not a clear. */
658 };
659
660 /* Determine whether we can do direct BLIT or not. For compressed textures,
661 * always assume a direct blit (input data pretends to be float, but it is
662 * not). */
663 bool can_use_direct_blit = true;
664 if (!is_compressed && (expected_dst_bytes_per_pixel != input_bytes_per_pixel ||
665 num_channels != destination_num_channels))
666 {
667 can_use_direct_blit = false;
668 }
669
670 if (is_depth_format) {
672 /* Workaround for crash in validation layer when blitting to sampler2DDepth target with
673 * dimensions (1, 1, 1); */
674 if (extent[0] == 1 && extent[1] == 1 && extent[2] == 1 && totalsize == 4) {
675 can_use_direct_blit = false;
676 }
677 }
678 }
679
680 if (format_ == TextureFormat::SRGBA_8_8_8_8 && !can_use_direct_blit) {
682 "SRGB data upload does not work correctly using compute upload. "
683 "texname '%s'",
684 name_.c_str());
685 }
686
687 /* Safety Checks. */
689 type == GPU_DATA_2_10_10_10_REV || is_compressed)
690 {
691 BLI_assert(can_use_direct_blit &&
692 "Special input data type must be a 1-1 mapping with destination texture as it "
693 "cannot easily be split");
694 }
695
696 /* Debug and verification. */
697 if (!can_use_direct_blit) {
698 /* Check mip compatibility. */
699 if (mip != 0) {
701 "Updating texture layers other than mip=0 when data is mismatched is not "
702 "possible in METAL on macOS using texture->write\n");
703 return;
704 }
705
706 /* Check Format write-ability. */
707 if (mtl_format_get_writeable_view_format(destination_format) == MTLPixelFormatInvalid) {
709 "Updating texture -- destination MTLPixelFormat '%d' does not support write "
710 "operations, and no suitable TextureView format exists.\n",
711 *(int *)(&destination_format));
712 return;
713 }
714 }
715
716 /* Common Properties. */
717 MTLPixelFormat compatible_write_format = mtl_format_get_writeable_view_format(
718 destination_format);
719
720 /* Some texture formats are not writeable so we need to use a texture view. */
721 if (compatible_write_format == MTLPixelFormatInvalid) {
722 MTL_LOG_ERROR("Cannot use compute update blit with texture-view format: %d\n",
723 *((int *)&compatible_write_format));
724 return;
725 }
726
727 /* Fetch allocation from memory pool. */
729 totalsize, true, data);
730 id<MTLBuffer> staging_buffer = temp_allocation->get_metal_buffer();
731 BLI_assert(staging_buffer != nil);
732
733 /* Prepare command encoders. */
734 id<MTLBlitCommandEncoder> blit_encoder = nil;
735 id<MTLComputeCommandEncoder> compute_encoder = nil;
736 id<MTLTexture> staging_texture = nil;
737 id<MTLTexture> texture_handle = nil;
738
739 /* Use staging texture. */
740 bool use_staging_texture = false;
741
742 if (can_use_direct_blit) {
744 BLI_assert(blit_encoder != nil);
745
746 /* If we need to use a texture view to write texture data as the source
747 * format is unwritable, if our texture has not been initialized with
748 * texture view support, use a staging texture. */
749 if ((compatible_write_format != destination_format) &&
750 !(internal_gpu_image_usage_flags_ & GPU_TEXTURE_USAGE_FORMAT_VIEW))
751 {
752 use_staging_texture = true;
753 }
754 }
755 else {
756 compute_encoder = ctx->main_command_buffer.ensure_begin_compute_encoder();
757 BLI_assert(compute_encoder != nil);
758
759 /* For compute, we should use a stating texture to avoid texture write usage,
760 * if it has not been specified for the texture. Using shader-write disables
761 * lossless texture compression, so this is best to avoid where possible. */
762 if (!(internal_gpu_image_usage_flags_ & GPU_TEXTURE_USAGE_SHADER_WRITE)) {
763 use_staging_texture = true;
764 }
765 if (compatible_write_format != destination_format) {
766 if (!(internal_gpu_image_usage_flags_ & GPU_TEXTURE_USAGE_FORMAT_VIEW)) {
767 use_staging_texture = true;
768 }
769 }
770 }
771
772 /* Allocate stating texture if needed. */
773 if (use_staging_texture) {
774 /* Create staging texture to avoid shader-write limiting optimization. */
775 BLI_assert(texture_descriptor_ != nullptr);
776 MTLTextureUsage original_usage = texture_descriptor_.usage;
777 texture_descriptor_.usage = original_usage | MTLTextureUsageShaderWrite |
778 MTLTextureUsagePixelFormatView;
779 staging_texture = [ctx->device newTextureWithDescriptor:texture_descriptor_];
780 staging_texture.label = @"Staging texture";
781 texture_descriptor_.usage = original_usage;
782
783 /* Create texture view if needed. */
784 texture_handle = ((compatible_write_format == destination_format)) ?
785 [staging_texture retain] :
786 [staging_texture newTextureViewWithPixelFormat:compatible_write_format];
787 }
788 else {
789 /* Use texture view. */
790 if (compatible_write_format != destination_format) {
791 BLI_assert(internal_gpu_image_usage_flags_ & GPU_TEXTURE_USAGE_FORMAT_VIEW);
792 texture_handle = [texture_ newTextureViewWithPixelFormat:compatible_write_format];
793 }
794 else {
795 texture_handle = texture_;
796 [texture_handle retain];
797 }
798 }
799
800 switch (type_) {
801
802 /* 1D */
803 case GPU_TEXTURE_1D:
805 if (can_use_direct_blit) {
806 /* Use Blit based update. */
807 size_t bytes_per_row = expected_dst_bytes_per_pixel * extent[0];
808 size_t bytes_per_image = bytes_per_row;
809 if (is_compressed) {
810 size_t block_size = to_block_size(format_);
811 size_t blocks_x = divide_ceil_u(extent[0], 4);
812 bytes_per_row = blocks_x * block_size;
813 bytes_per_image = bytes_per_row;
814 }
815 int max_array_index = ((type_ == GPU_TEXTURE_1D_ARRAY) ? extent[1] : 1);
816 for (int array_index = 0; array_index < max_array_index; array_index++) {
817
818 size_t buffer_array_offset = (bytes_per_image * size_t(array_index));
819 [blit_encoder
820 copyFromBuffer:staging_buffer
821 sourceOffset:buffer_array_offset
822 sourceBytesPerRow:bytes_per_row
823 sourceBytesPerImage:bytes_per_image
824 sourceSize:MTLSizeMake(extent[0], 1, 1)
825 toTexture:texture_handle
826 destinationSlice:((type_ == GPU_TEXTURE_1D_ARRAY) ? (array_index + offset[1]) :
827 0)
828 destinationLevel:mip
829 destinationOrigin:MTLOriginMake(offset[0], 0, 0)];
830 }
831 }
832 else {
833 /* Use Compute Based update. */
834 if (type_ == GPU_TEXTURE_1D) {
835 id<MTLComputePipelineState> pso = texture_update_1d_get_kernel(
836 compute_specialization_kernel);
837 TextureUpdateParams params = {mip, {extent[0], 1, 1}, {offset[0], 0, 0}, extent[0]};
838
839 /* Bind resources via compute state for optimal state caching performance. */
841 cs.bind_pso(pso);
842 cs.bind_compute_bytes(&params, sizeof(params), 0);
843 cs.bind_compute_buffer(staging_buffer, 0, 1);
844 cs.bind_compute_texture(texture_handle, 0);
845 [compute_encoder
846 dispatchThreads:MTLSizeMake(extent[0], 1, 1) /* Width, Height, Layer */
847 threadsPerThreadgroup:MTLSizeMake(64, 1, 1)];
848 }
849 else if (type_ == GPU_TEXTURE_1D_ARRAY) {
850 id<MTLComputePipelineState> pso = texture_update_1d_array_get_kernel(
851 compute_specialization_kernel);
852 TextureUpdateParams params = {
853 mip, {extent[0], extent[1], 1}, {offset[0], offset[1], 0}, extent[0]};
854
855 /* Bind resources via compute state for optimal state caching performance. */
857 cs.bind_pso(pso);
858 cs.bind_compute_bytes(&params, sizeof(params), 0);
859 cs.bind_compute_buffer(staging_buffer, 0, 1);
860 cs.bind_compute_texture(texture_handle, 0);
861 [compute_encoder
862 dispatchThreads:MTLSizeMake(extent[0], extent[1], 1) /* Width, layers, nil */
863 threadsPerThreadgroup:MTLSizeMake(8, 8, 1)];
864 }
865 }
866 } break;
867
868 /* 2D */
869 case GPU_TEXTURE_2D:
871 if (can_use_direct_blit) {
872 /* Use Blit encoder update. */
873 size_t bytes_per_row = expected_dst_bytes_per_pixel * extent[0];
874 size_t bytes_per_image = bytes_per_row * extent[1];
875 if (is_compressed) {
876 size_t block_size = to_block_size(format_);
877 size_t blocks_x = divide_ceil_u(extent[0], 4);
878 size_t blocks_y = divide_ceil_u(extent[1], 4);
879 bytes_per_row = blocks_x * block_size;
880 bytes_per_image = bytes_per_row * blocks_y;
881 }
882
883 size_t texture_array_relative_offset = 0;
884 int base_slice = (type_ == GPU_TEXTURE_2D_ARRAY) ? offset[2] : 0;
885 int final_slice = base_slice + ((type_ == GPU_TEXTURE_2D_ARRAY) ? extent[2] : 1);
886
887 for (int array_slice = base_slice; array_slice < final_slice; array_slice++) {
888
889 if (array_slice > 0) {
891 BLI_assert(array_slice < d_);
892 }
893
894 [blit_encoder copyFromBuffer:staging_buffer
895 sourceOffset:texture_array_relative_offset
896 sourceBytesPerRow:bytes_per_row
897 sourceBytesPerImage:bytes_per_image
898 sourceSize:MTLSizeMake(extent[0], extent[1], 1)
899 toTexture:texture_handle
900 destinationSlice:array_slice
901 destinationLevel:mip
902 destinationOrigin:MTLOriginMake(offset[0], offset[1], 0)];
903
904 texture_array_relative_offset += bytes_per_image;
905 }
906 }
907 else {
908 /* Use Compute texture update. */
909 if (type_ == GPU_TEXTURE_2D) {
910 id<MTLComputePipelineState> pso = texture_update_2d_get_kernel(
911 compute_specialization_kernel);
912 TextureUpdateParams params = {
913 mip, {extent[0], extent[1], 1}, {offset[0], offset[1], 0}, extent[0]};
914
915 /* Bind resources via compute state for optimal state caching performance. */
917 cs.bind_pso(pso);
918 cs.bind_compute_bytes(&params, sizeof(params), 0);
919 cs.bind_compute_buffer(staging_buffer, 0, 1);
920 cs.bind_compute_texture(texture_handle, 0);
921 [compute_encoder
922 dispatchThreads:MTLSizeMake(
923 extent[0], extent[1], 1) /* Width, Height, Layer */
924 threadsPerThreadgroup:MTLSizeMake(8, 8, 1)];
925 }
926 else if (type_ == GPU_TEXTURE_2D_ARRAY) {
927 id<MTLComputePipelineState> pso = texture_update_2d_array_get_kernel(
928 compute_specialization_kernel);
929 TextureUpdateParams params = {mip,
930 {extent[0], extent[1], extent[2]},
931 {offset[0], offset[1], offset[2]},
932 extent[0]};
933
934 /* Bind resources via compute state for optimal state caching performance. */
936 cs.bind_pso(pso);
937 cs.bind_compute_bytes(&params, sizeof(params), 0);
938 cs.bind_compute_buffer(staging_buffer, 0, 1);
939 cs.bind_compute_texture(texture_handle, 0);
940 [compute_encoder dispatchThreads:MTLSizeMake(extent[0],
941 extent[1],
942 extent[2]) /* Width, Height, Layer */
943 threadsPerThreadgroup:MTLSizeMake(4, 4, 4)];
944 }
945 }
946
947 } break;
948
949 /* 3D */
950 case GPU_TEXTURE_3D: {
951 if (can_use_direct_blit) {
952 size_t bytes_per_row = expected_dst_bytes_per_pixel * extent[0];
953 size_t bytes_per_image = bytes_per_row * extent[1];
954 [blit_encoder copyFromBuffer:staging_buffer
955 sourceOffset:0
956 sourceBytesPerRow:bytes_per_row
957 sourceBytesPerImage:bytes_per_image
958 sourceSize:MTLSizeMake(extent[0], extent[1], extent[2])
959 toTexture:texture_handle
960 destinationSlice:0
961 destinationLevel:mip
962 destinationOrigin:MTLOriginMake(offset[0], offset[1], offset[2])];
963 }
964 else {
965 id<MTLComputePipelineState> pso = texture_update_3d_get_kernel(
966 compute_specialization_kernel);
967 TextureUpdateParams params = {mip,
968 {extent[0], extent[1], extent[2]},
969 {offset[0], offset[1], offset[2]},
970 extent[0]};
971
972 /* Bind resources via compute state for optimal state caching performance. */
974 cs.bind_pso(pso);
975 cs.bind_compute_bytes(&params, sizeof(params), 0);
976 cs.bind_compute_buffer(staging_buffer, 0, 1);
977 cs.bind_compute_texture(texture_handle, 0);
978 [compute_encoder
979 dispatchThreads:MTLSizeMake(
980 extent[0], extent[1], extent[2]) /* Width, Height, Depth */
981 threadsPerThreadgroup:MTLSizeMake(4, 4, 4)];
982 }
983 } break;
984
985 /* CUBE */
986 case GPU_TEXTURE_CUBE: {
987 if (can_use_direct_blit) {
988 size_t bytes_per_row = expected_dst_bytes_per_pixel * extent[0];
989 size_t bytes_per_image = bytes_per_row * extent[1];
990 size_t texture_array_relative_offset = 0;
991
992 /* Iterate over all cube faces in range (offset[2], offset[2] + extent[2]). */
993 for (int i = 0; i < extent[2]; i++) {
994 int face_index = offset[2] + i;
995
996 [blit_encoder copyFromBuffer:staging_buffer
997 sourceOffset:texture_array_relative_offset
998 sourceBytesPerRow:bytes_per_row
999 sourceBytesPerImage:bytes_per_image
1000 sourceSize:MTLSizeMake(extent[0], extent[1], 1)
1001 toTexture:texture_handle
1002 destinationSlice:face_index /* = cubeFace+arrayIndex*6 */
1003 destinationLevel:mip
1004 destinationOrigin:MTLOriginMake(offset[0], offset[1], 0)];
1005 texture_array_relative_offset += bytes_per_image;
1006 }
1007 }
1008 else {
1010 "TODO(Metal): Support compute texture update for GPU_TEXTURE_CUBE %d, %d, %d\n",
1011 w_,
1012 h_,
1013 d_);
1014 }
1015 } break;
1016
1018 if (can_use_direct_blit) {
1019
1020 size_t bytes_per_row = expected_dst_bytes_per_pixel * extent[0];
1021 size_t bytes_per_image = bytes_per_row * extent[1];
1022
1023 /* Upload to all faces between offset[2] (which is zero in most cases) AND extent[2]. */
1024 size_t texture_array_relative_offset = 0;
1025 for (int i = 0; i < extent[2]; i++) {
1026 int face_index = offset[2] + i;
1027 [blit_encoder copyFromBuffer:staging_buffer
1028 sourceOffset:texture_array_relative_offset
1029 sourceBytesPerRow:bytes_per_row
1030 sourceBytesPerImage:bytes_per_image
1031 sourceSize:MTLSizeMake(extent[0], extent[1], 1)
1032 toTexture:texture_handle
1033 destinationSlice:face_index /* = cubeFace+arrayIndex*6. */
1034 destinationLevel:mip
1035 destinationOrigin:MTLOriginMake(offset[0], offset[1], 0)];
1036 texture_array_relative_offset += bytes_per_image;
1037 }
1038 }
1039 else {
1041 "TODO(Metal): Support compute texture update for GPU_TEXTURE_CUBE_ARRAY %d, %d, "
1042 "%d\n",
1043 w_,
1044 h_,
1045 d_);
1046 }
1047 } break;
1048
1049 case GPU_TEXTURE_BUFFER: {
1050 /* TODO(Metal): Support Data upload to TEXTURE BUFFER
1051 * Data uploads generally happen via VertBuf instead. */
1052 BLI_assert(false);
1053 } break;
1054
1055 case GPU_TEXTURE_ARRAY:
1056 /* Not an actual format - modifier flag for others. */
1057 return;
1058 }
1059
1060 /* If staging texture was used, copy contents to original texture. */
1061 if (use_staging_texture) {
1062 /* When using staging texture, copy results into existing texture. */
1063 BLI_assert(staging_texture != nil);
1064 blit_encoder = ctx->main_command_buffer.ensure_begin_blit_encoder();
1065
1066 /* Copy modified staging texture region back to original texture.
1067 * Differing blit dimensions based on type. */
1068 switch (type_) {
1069 case GPU_TEXTURE_1D:
1070 case GPU_TEXTURE_1D_ARRAY: {
1071 int base_slice = (type_ == GPU_TEXTURE_1D_ARRAY) ? offset[1] : 0;
1072 int final_slice = base_slice + ((type_ == GPU_TEXTURE_1D_ARRAY) ? extent[1] : 1);
1073 for (int array_index = base_slice; array_index < final_slice; array_index++) {
1074 [blit_encoder copyFromTexture:staging_texture
1075 sourceSlice:array_index
1076 sourceLevel:mip
1077 sourceOrigin:MTLOriginMake(offset[0], 0, 0)
1078 sourceSize:MTLSizeMake(extent[0], 1, 1)
1079 toTexture:texture_
1080 destinationSlice:array_index
1081 destinationLevel:mip
1082 destinationOrigin:MTLOriginMake(offset[0], 0, 0)];
1083 }
1084 } break;
1085 case GPU_TEXTURE_2D:
1086 case GPU_TEXTURE_2D_ARRAY: {
1087 int base_slice = (type_ == GPU_TEXTURE_2D_ARRAY) ? offset[2] : 0;
1088 int final_slice = base_slice + ((type_ == GPU_TEXTURE_2D_ARRAY) ? extent[2] : 1);
1089 for (int array_index = base_slice; array_index < final_slice; array_index++) {
1090 [blit_encoder copyFromTexture:staging_texture
1091 sourceSlice:array_index
1092 sourceLevel:mip
1093 sourceOrigin:MTLOriginMake(offset[0], offset[1], 0)
1094 sourceSize:MTLSizeMake(extent[0], extent[1], 1)
1095 toTexture:texture_
1096 destinationSlice:array_index
1097 destinationLevel:mip
1098 destinationOrigin:MTLOriginMake(offset[0], offset[1], 0)];
1099 }
1100 } break;
1101 case GPU_TEXTURE_3D: {
1102 [blit_encoder copyFromTexture:staging_texture
1103 sourceSlice:0
1104 sourceLevel:mip
1105 sourceOrigin:MTLOriginMake(offset[0], offset[1], offset[2])
1106 sourceSize:MTLSizeMake(extent[0], extent[1], extent[2])
1107 toTexture:texture_
1108 destinationSlice:0
1109 destinationLevel:mip
1110 destinationOrigin:MTLOriginMake(offset[0], offset[1], offset[2])];
1111 } break;
1112 case GPU_TEXTURE_CUBE:
1114 /* Iterate over all cube faces in range (offset[2], offset[2] + extent[2]). */
1115 for (int i = 0; i < extent[2]; i++) {
1116 int face_index = offset[2] + i;
1117 [blit_encoder copyFromTexture:staging_texture
1118 sourceSlice:face_index
1119 sourceLevel:mip
1120 sourceOrigin:MTLOriginMake(offset[0], offset[1], 0)
1121 sourceSize:MTLSizeMake(extent[0], extent[1], 1)
1122 toTexture:texture_
1123 destinationSlice:face_index
1124 destinationLevel:mip
1125 destinationOrigin:MTLOriginMake(offset[0], offset[1], 0)];
1126 }
1127 } break;
1128 case GPU_TEXTURE_ARRAY:
1129 case GPU_TEXTURE_BUFFER:
1131 break;
1132 }
1133
1134 [staging_texture release];
1135 }
1136
1137 /* Finalize Blit Encoder. */
1138 if (can_use_direct_blit) {
1139 /* Textures which use MTLStorageModeManaged need to have updated contents
1140 * synced back to CPU to avoid an automatic flush overwriting contents. */
1141 if (texture_.storageMode == MTLStorageModeManaged) {
1142 [blit_encoder synchronizeResource:texture_];
1143 }
1144 [blit_encoder optimizeContentsForGPUAccess:texture_];
1145 }
1146 else {
1147 /* Textures which use MTLStorageModeManaged need to have updated contents
1148 * synced back to CPU to avoid an automatic flush overwriting contents. */
1149 blit_encoder = ctx->main_command_buffer.ensure_begin_blit_encoder();
1150 if (texture_.storageMode == MTLStorageModeManaged) {
1151
1152 [blit_encoder synchronizeResource:texture_];
1153 }
1154 [blit_encoder optimizeContentsForGPUAccess:texture_];
1155 }
1156
1157 /* Decrement texture reference counts. This ensures temporary texture views are released. */
1158 [texture_handle release];
1159
1160 ctx->main_command_buffer.submit(false);
1161
1162 /* Release temporary staging buffer allocation.
1163 * NOTE: Allocation will be tracked with command submission and released once no longer in use.
1164 */
1165 temp_allocation->free();
1166 }
1167}
1168
1169void MTLTexture::update_sub(int offset[3],
1170 int extent[3],
1171 eGPUDataFormat format,
1172 GPUPixelBuffer *pixbuf)
1173{
1174 /* Update texture from pixel buffer. */
1176 BLI_assert(pixbuf != nullptr);
1177
1178 /* Fetch pixel buffer metal buffer. */
1179 MTLPixelBuffer *mtl_pix_buf = static_cast<MTLPixelBuffer *>(unwrap(pixbuf));
1180 id<MTLBuffer> buffer = mtl_pix_buf->get_metal_buffer();
1181 BLI_assert(buffer != nil);
1182 if (buffer == nil) {
1183 return;
1184 }
1185
1186 /* Ensure texture is ready. */
1187 this->ensure_baked();
1188 BLI_assert(texture_ != nil);
1189
1190 /* Calculate dimensions. */
1191 int num_image_channels = to_component_len(format_);
1192
1193 size_t bits_per_pixel = num_image_channels * to_bytesize(format);
1194 size_t bytes_per_row = bits_per_pixel * extent[0];
1195 size_t bytes_per_image = bytes_per_row * extent[1];
1196
1197 /* Currently only required for 2D textures. */
1198 if (type_ == GPU_TEXTURE_2D) {
1199
1200 /* Create blit command encoder to copy data. */
1201 MTLContext *ctx = MTLContext::get();
1202 BLI_assert(ctx);
1203
1205 [blit_encoder copyFromBuffer:buffer
1206 sourceOffset:0
1207 sourceBytesPerRow:bytes_per_row
1208 sourceBytesPerImage:bytes_per_image
1209 sourceSize:MTLSizeMake(extent[0], extent[1], 1)
1210 toTexture:texture_
1211 destinationSlice:0
1212 destinationLevel:0
1213 destinationOrigin:MTLOriginMake(offset[0], offset[1], 0)];
1214
1215 if (texture_.storageMode == MTLStorageModeManaged) {
1216 [blit_encoder synchronizeResource:texture_];
1217 }
1218 [blit_encoder optimizeContentsForGPUAccess:texture_];
1219 }
1220 else {
1221 BLI_assert(false);
1222 }
1223}
1224
1225void gpu::MTLTexture::ensure_mipmaps(int miplvl)
1226{
1227 /* Do not update texture view. */
1228 BLI_assert(resource_mode_ != MTL_TEXTURE_MODE_TEXTURE_VIEW);
1229
1230 /* Clamp level to maximum. */
1231 int effective_h = (type_ == GPU_TEXTURE_1D_ARRAY) ? 0 : h_;
1232 int effective_d = (type_ != GPU_TEXTURE_3D) ? 0 : d_;
1233 int max_dimension = std::max({w_, effective_h, effective_d});
1234 int max_miplvl = floor(log2(max_dimension));
1235 miplvl = min_ii(max_miplvl, miplvl);
1236
1237 /* Increase mipmap level. */
1238 if (mipmaps_ < miplvl) {
1239 mipmaps_ = miplvl;
1240
1241 /* Check if baked. */
1242 if (is_baked_ && mipmaps_ > mtl_max_mips_) {
1243 BLI_assert_msg(false,
1244 "Texture requires a higher mipmap level count. Please specify the required "
1245 "amount upfront.");
1246 is_dirty_ = true;
1247 MTL_LOG_WARNING("Texture requires regenerating due to increase in mip-count");
1248 }
1249 }
1250 this->mip_range_set(0, mipmaps_ - 1);
1251}
1252
1254{
1255 /* Compressed textures allow users to provide their own custom mipmaps. And
1256 * we can't generate them at runtime anyway. */
1258 return;
1259 }
1260
1261 /* Fetch Active Context. */
1262 MTLContext *ctx = MTLContext::get();
1263 BLI_assert(ctx);
1264
1265 if (!ctx->device) {
1266 MTL_LOG_ERROR("Cannot Generate mip-maps -- metal device invalid\n");
1267 BLI_assert(false);
1268 return;
1269 }
1270
1271 /* Ensure mipmaps. */
1272 this->ensure_mipmaps(mtl_max_mips_);
1273
1274 /* Ensure texture is baked. */
1275 this->ensure_baked();
1276 BLI_assert_msg(is_baked_ && texture_, "MTLTexture is not valid");
1277
1278 if (mipmaps_ == 1 || mtl_max_mips_ == 1) {
1279 /* Nothing to do. */
1280 return;
1281 }
1282
1283 /* Verify if we can perform mipmap generation. */
1284 if (format_ == TextureFormat::SFLOAT_32_DEPTH || format_ == TextureFormat::UNORM_16_DEPTH ||
1285 format_ == TextureFormat::SFLOAT_32_DEPTH_UINT_8)
1286 {
1287 MTL_LOG_WARNING("Cannot generate mipmaps for textures using DEPTH formats");
1288 return;
1289 }
1290
1291 @autoreleasepool {
1292 /* Fetch active BlitCommandEncoder. */
1294 if (G.debug & G_DEBUG_GPU) {
1295 [enc insertDebugSignpost:@"Generate MipMaps"];
1296 }
1297 [enc generateMipmapsForTexture:texture_];
1298 has_generated_mips_ = true;
1299 }
1300}
1301
1303{
1304 gpu::MTLTexture *mt_dst = static_cast<gpu::MTLTexture *>(dst);
1305
1306 /* Safety Checks. */
1307 BLI_assert(w_ == mt_dst->w_ && std::max(h_, 1) == std::max(mt_dst->h_, 1) &&
1308 std::max(d_, 1) == std::max(mt_dst->d_, 1));
1309 BLI_assert((format_ == mt_dst->format_) ||
1310 (format_ == TextureFormat::SRGBA_8_8_8_8 &&
1311 mt_dst->format_ == TextureFormat::UNORM_8_8_8_8) ||
1312 (format_ == TextureFormat::UNORM_8_8_8_8 &&
1313 mt_dst->format_ == TextureFormat::SRGBA_8_8_8_8));
1315
1316 /* Fetch active context. */
1317 MTLContext *ctx = MTLContext::get();
1318 BLI_assert(ctx);
1319
1320 /* Ensure texture is baked. */
1321 this->ensure_baked();
1322
1323 @autoreleasepool {
1324 /* Setup blit encoder. */
1326 BLI_assert(blit_encoder != nil);
1327
1328 switch (mt_dst->type_) {
1331 case GPU_TEXTURE_3D: {
1332 /* Do full texture copy for 3D textures */
1333 BLI_assert(mt_dst->d_ == d_);
1334 [blit_encoder copyFromTexture:this->get_metal_handle_base()
1335 toTexture:mt_dst->get_metal_handle_base()];
1336 [blit_encoder optimizeContentsForGPUAccess:mt_dst->get_metal_handle_base()];
1337 } break;
1338 default: {
1339 for (int mip : mip_levels) {
1340 /* NOTE: mip_size_get() won't override any dimension that is equal to 0. */
1341 int extent[3] = {1, 1, 1};
1342 this->mip_size_get(mip, extent);
1343
1344 int slice = 0;
1345 this->blit(blit_encoder,
1346 0,
1347 0,
1348 0,
1349 slice,
1350 mip,
1351 mt_dst,
1352 0,
1353 0,
1354 0,
1355 slice,
1356 mip,
1357 extent[0],
1358 extent[1],
1359 extent[2]);
1360 }
1361 break;
1362 }
1363 }
1364 }
1365}
1366
1368{
1370 MTL_LOG_ERROR("Clearing 1D texture is not supported");
1371 /* Avoid Metal error in release. */
1372 return;
1373 }
1374
1375 /* Ensure texture is baked. */
1376 this->ensure_baked();
1377
1378 /* If texture is buffer-backed, clear directly on buffer.
1379 * NOTE: This is currently only true for fallback atomic textures. */
1380 if (backing_buffer_ != nullptr) {
1381 /* Assume data is INT32 or UINT32. */
1382 uint4 u32 = uint4(data);
1383 /* Rotate the vector and the bytes to check equality with neighbor. */
1384 bool fast_buf_clear = (u32 == ((u32 << 8) | (u32 >> 24)));
1385 for (int i = to_component_len(format_get()) - 1; i > 0; --i) {
1386 fast_buf_clear &= u32[i] == u32[0];
1387 }
1388
1389 if (fast_buf_clear) {
1390 /* Fetch active context. */
1391 MTLContext *ctx = MTLContext::get();
1392 BLI_assert(ctx);
1393
1394 /* Begin blit encoder. */
1395 id<MTLBlitCommandEncoder> blit_encoder =
1397 [blit_encoder fillBuffer:backing_buffer_->get_metal_buffer()
1398 range:NSMakeRange(0, backing_buffer_->get_size())
1399 value:u32[0]];
1400 }
1401 else {
1402 BLI_assert_msg(false,
1403 "Non-repeating-byte-pattern clear for buffer-backed textures not supported!");
1404 }
1405 return;
1406 }
1407 /* For texture views, the actual Metal handle used for rendering is mip_swizzle_view_,
1408 * not texture_ (which holds the parent's Metal handle). Metal strips
1409 * MTLTextureUsageRenderTarget from cross-format views, so delegate the clear
1410 * to the parent texture which retains full usage flags. */
1411 if (resource_mode_ == MTL_TEXTURE_MODE_TEXTURE_VIEW && source_texture_) {
1412 id<MTLTexture> clear_texture = (mip_swizzle_view_ != nil) ? mip_swizzle_view_ : texture_;
1413 if (!(clear_texture.usage & MTLTextureUsageRenderTarget)) {
1414 gpu::MTLTexture *source_texture = const_cast<gpu::MTLTexture *>(
1415 static_cast<const gpu::MTLTexture *>(source_texture_));
1416 source_texture->clear(data);
1417 return;
1418 }
1419 }
1420
1421 /* Create clear frame-buffer for fast clear. */
1423 FrameBuffer *fb = this->get_blit_framebuffer(-1, 0);
1424 fb->bind(true);
1425 fb->clear_attachment(this->attachment_type(0), data);
1426 GPU_framebuffer_bind(prev_fb);
1427}
1428
1429static MTLTextureSwizzle swizzle_to_mtl(const char swizzle)
1430{
1431 switch (swizzle) {
1432 default:
1433 case 'x':
1434 case 'r':
1435 return MTLTextureSwizzleRed;
1436 case 'y':
1437 case 'g':
1438 return MTLTextureSwizzleGreen;
1439 case 'z':
1440 case 'b':
1441 return MTLTextureSwizzleBlue;
1442 case 'w':
1443 case 'a':
1444 return MTLTextureSwizzleAlpha;
1445 case '0':
1446 return MTLTextureSwizzleZero;
1447 case '1':
1448 return MTLTextureSwizzleOne;
1449 }
1450}
1451
1452void gpu::MTLTexture::swizzle_set(const char swizzle_mask[4])
1453{
1454 if (memcmp(tex_swizzle_mask_, swizzle_mask, 4) != 0) {
1455 memcpy(tex_swizzle_mask_, swizzle_mask, 4);
1456
1457 /* Creating the swizzle mask and flagging as dirty if changed. */
1458 MTLTextureSwizzleChannels new_swizzle_mask = MTLTextureSwizzleChannelsMake(
1459 swizzle_to_mtl(swizzle_mask[0]),
1460 swizzle_to_mtl(swizzle_mask[1]),
1461 swizzle_to_mtl(swizzle_mask[2]),
1462 swizzle_to_mtl(swizzle_mask[3]));
1463
1464 mtl_swizzle_mask_ = new_swizzle_mask;
1465 texture_view_dirty_flags_ |= TEXTURE_VIEW_SWIZZLE_DIRTY;
1466 }
1467}
1468
1469void gpu::MTLTexture::mip_range_set(int min, int max)
1470{
1471 BLI_assert(min <= max && min >= 0 && max <= mipmaps_);
1472
1473 /* NOTE:
1474 * - mip_min_ and mip_max_ are used to Clamp LODs during sampling.
1475 * - For the time being, we are going to just need to generate a FULL mipmap chain
1476 * as we do not know ahead of time whether mipmaps will be used.
1477 *
1478 * TODO(Metal): Add texture initialization flag to determine whether mipmaps are used
1479 * or not. Will be important for saving memory for big textures. */
1480 mip_min_ = min;
1481 mip_max_ = max;
1482
1483 if ((type_ == GPU_TEXTURE_1D || type_ == GPU_TEXTURE_1D_ARRAY || type_ == GPU_TEXTURE_BUFFER) &&
1484 max > 1)
1485 {
1486
1488 " MTLTexture of type TEXTURE_1D_ARRAY or TEXTURE_BUFFER cannot have a mipcount "
1489 "greater than 1\n");
1490 mip_min_ = 0;
1491 mip_max_ = 0;
1492 mipmaps_ = 0;
1493 BLI_assert(false);
1494 }
1495
1496 texture_view_dirty_flags_ |= TEXTURE_VIEW_MIP_DIRTY;
1497}
1498
1500{
1502 BLI_assert(mip <= mipmaps_);
1504
1505 /* NOTE: mip_size_get() won't override any dimension that is equal to 0. */
1506 int extent[3] = {1, 1, 1};
1507 this->mip_size_get(mip, extent);
1508
1509 size_t texture_size = read_size_get(mip, type);
1510 int num_channels = to_component_len(format_);
1511
1512 /* Ensure texture is baked. */
1513 if (is_baked_) {
1514 this->read_internal(
1515 mip, 0, 0, 0, extent[0], extent[1], extent[2], type, num_channels, texture_size, data);
1516 }
1517 else {
1518 /* Clear return values? */
1519 MTL_LOG_WARNING("MTLTexture::read - reading from texture with no image data");
1520 }
1521}
1522
1523/* Fetch the raw buffer data from a texture and copy to CPU host ptr. */
1524void gpu::MTLTexture::read_internal(int mip,
1525 int x_off,
1526 int y_off,
1527 int z_off,
1528 int width,
1529 int height,
1530 int depth,
1531 eGPUDataFormat desired_output_format,
1532 int num_output_components,
1533 size_t debug_data_size,
1534 void *r_data)
1535{
1536 /* Verify textures are baked. */
1537 if (!is_baked_) {
1538 MTL_LOG_WARNING("gpu::MTLTexture::read_internal - Trying to read from a non-baked texture!");
1539 return;
1540 }
1541 /* Fetch active context. */
1542 MTLContext *ctx = MTLContext::get();
1543 BLI_assert(ctx);
1544
1545 /* Calculate Desired output size. */
1546 int num_channels = to_component_len(format_);
1547 BLI_assert(num_output_components <= num_channels);
1548 size_t desired_output_bpp = num_output_components * to_bytesize(desired_output_format);
1549
1550 /* Calculate Metal data output for trivial copy. */
1551 size_t image_bpp = get_mtl_format_bytesize(texture_.pixelFormat);
1552 uint image_components = get_mtl_format_num_components(texture_.pixelFormat);
1553 bool is_depth_format = (format_flag_ & GPU_FORMAT_DEPTH);
1554
1555 /* Verify if we need to use compute read. */
1556 eGPUDataFormat data_format = to_texture_data_format(this->format_get());
1557 bool format_conversion_needed = (data_format != desired_output_format);
1558 bool can_use_simple_read = (desired_output_bpp == image_bpp) && (!format_conversion_needed) &&
1559 (num_output_components == image_components);
1560
1561 /* Depth must be read using the compute shader -- Some safety checks to verify that params are
1562 * correct. */
1563 if (is_depth_format) {
1564 can_use_simple_read = false;
1565 /* TODO(Metal): Stencil data write not yet supported, so force components to one. */
1566 image_components = 1;
1567 BLI_assert(num_output_components == 1);
1568 BLI_assert(image_components == 1);
1569 BLI_assert(data_format == GPU_DATA_FLOAT || data_format == GPU_DATA_UINT_24_8_DEPRECATED);
1570 BLI_assert(validate_data_format(format_, data_format));
1571 }
1572
1573 /* SPECIAL Workaround for R11G11B10, TextureFormat::UNORM_10_10_10_2,
1574 * TextureFormat::UINT_10_10_10_2 textures requesting a read using:
1575 * GPU_DATA_10_11_11_REV. */
1576 if (desired_output_format == GPU_DATA_10_11_11_REV ||
1577 desired_output_format == GPU_DATA_2_10_10_10_REV)
1578 {
1579 BLI_assert(format_ == TextureFormat::UFLOAT_11_11_10 ||
1580 format_ == TextureFormat::UNORM_10_10_10_2 ||
1581 format_ == TextureFormat::UINT_10_10_10_2);
1582
1583 /* override parameters - we'll be able to use simple copy, as bpp will match at 4 bytes. */
1584 image_bpp = sizeof(int);
1585 image_components = 1;
1586 desired_output_bpp = sizeof(int);
1587 num_output_components = 1;
1588
1589 data_format = GPU_DATA_INT;
1590 format_conversion_needed = false;
1591 can_use_simple_read = true;
1592 }
1593
1594 /* Determine size of output data. */
1595 size_t bytes_per_row = desired_output_bpp * width;
1596 size_t bytes_per_image = bytes_per_row * height;
1597 size_t total_bytes = bytes_per_image * max_ii(depth, 1);
1598
1599 if (can_use_simple_read) {
1600 /* DEBUG check that if direct copy is being used, then both the expected output size matches
1601 * the METAL texture size. */
1602 BLI_assert(
1603 ((num_output_components * to_bytesize(desired_output_format)) == desired_output_bpp) &&
1604 (desired_output_bpp == image_bpp));
1605 }
1606 /* DEBUG check that the allocated data size matches the bytes we expect. */
1607 BLI_assert(total_bytes <= debug_data_size);
1608 UNUSED_VARS_NDEBUG(debug_data_size);
1609
1610 /* Fetch allocation from scratch buffer. */
1611 gpu::MTLBuffer *dest_buf = MTLContext::get_global_memory_manager()->allocate_aligned(
1612 total_bytes, 256, true);
1613 BLI_assert(dest_buf != nullptr);
1614
1615 id<MTLBuffer> destination_buffer = dest_buf->get_metal_buffer();
1616 BLI_assert(destination_buffer != nil);
1617 void *destination_buffer_host_ptr = dest_buf->get_host_ptr();
1618 BLI_assert(destination_buffer_host_ptr != nullptr);
1619
1620 /* Prepare specialization struct (For non-trivial texture read routine). */
1621 int depth_format_mode = 0;
1622 if (is_depth_format) {
1623 depth_format_mode = 1;
1624 switch (desired_output_format) {
1625 case GPU_DATA_FLOAT:
1626 depth_format_mode = 1;
1627 break;
1629 depth_format_mode = 2;
1630 break;
1631 case GPU_DATA_UINT:
1632 depth_format_mode = 4;
1633 break;
1634 default:
1635 BLI_assert_msg(false, "Unhandled depth read format case");
1636 break;
1637 }
1638 }
1639
1640 TextureReadRoutineSpecialization compute_specialization_kernel = {
1641 tex_data_format_to_msl_texture_template_type(data_format), /* TEXTURE DATA TYPE */
1642 tex_data_format_to_msl_type_str(desired_output_format), /* OUTPUT DATA TYPE */
1643 num_channels, /* TEXTURE COMPONENT COUNT */
1644 num_output_components, /* OUTPUT DATA COMPONENT COUNT */
1645 depth_format_mode};
1646
1647 bool copy_successful = false;
1648 @autoreleasepool {
1649
1650 /* TODO(Metal): Verify whether we need some form of barrier here to ensure reads
1651 * happen after work with associated texture is finished. */
1652 GPU_finish();
1653
1655 id<MTLTexture> read_texture = texture_;
1656 /* Use texture-view handle if reading from a GPU texture view. */
1657 if (resource_mode_ == MTL_TEXTURE_MODE_TEXTURE_VIEW) {
1658 read_texture = this->get_metal_handle();
1659 }
1660 /* Create Texture View for SRGB special case to bypass internal type conversion. */
1661 if (format_ == TextureFormat::SRGBA_8_8_8_8) {
1662 BLI_assert(internal_gpu_image_usage_flags_ & GPU_TEXTURE_USAGE_FORMAT_VIEW);
1663 read_texture = [read_texture newTextureViewWithPixelFormat:MTLPixelFormatRGBA8Unorm];
1664 }
1665
1666 /* Perform per-texture type read. */
1667 switch (type_) {
1668 case GPU_TEXTURE_1D: {
1669 if (can_use_simple_read) {
1670 /* Use Blit Encoder READ. */
1671 id<MTLBlitCommandEncoder> enc = ctx->main_command_buffer.ensure_begin_blit_encoder();
1672 if (G.debug & G_DEBUG_GPU) {
1673 [enc insertDebugSignpost:@"GPUTextureRead1D"];
1674 }
1675 [enc copyFromTexture:read_texture
1676 sourceSlice:0
1677 sourceLevel:mip
1678 sourceOrigin:MTLOriginMake(x_off, 0, 0)
1679 sourceSize:MTLSizeMake(width, 1, 1)
1680 toBuffer:destination_buffer
1681 destinationOffset:0
1682 destinationBytesPerRow:bytes_per_row
1683 destinationBytesPerImage:bytes_per_image];
1684 copy_successful = true;
1685 }
1686 else {
1687
1688 /* Use Compute READ. */
1689 id<MTLComputeCommandEncoder> compute_encoder =
1691 id<MTLComputePipelineState> pso = texture_read_1d_get_kernel(
1692 compute_specialization_kernel);
1693 TextureReadParams params = {
1694 mip,
1695 {width, 1, 1},
1696 {x_off, 0, 0},
1697 };
1698
1699 /* Bind resources via compute state for optimal state caching performance. */
1701 cs.bind_pso(pso);
1702 cs.bind_compute_bytes(&params, sizeof(params), 0);
1703 cs.bind_compute_buffer(destination_buffer, 0, 1);
1704 cs.bind_compute_texture(read_texture, 0);
1705 [compute_encoder dispatchThreads:MTLSizeMake(width, 1, 1) /* Width, Height, Layer */
1706 threadsPerThreadgroup:MTLSizeMake(8, 8, 1)];
1707 copy_successful = true;
1708 }
1709 } break;
1710
1711 case GPU_TEXTURE_1D_ARRAY: {
1712 if (can_use_simple_read) {
1713 /* Use Blit Encoder READ. */
1714 id<MTLBlitCommandEncoder> enc = ctx->main_command_buffer.ensure_begin_blit_encoder();
1715 if (G.debug & G_DEBUG_GPU) {
1716 [enc insertDebugSignpost:@"GPUTextureRead1DArray"];
1717 }
1718
1719 int base_slice = y_off;
1720 int final_slice = base_slice + height;
1721 size_t texture_array_relative_offset = 0;
1722
1723 for (int array_slice = base_slice; array_slice < final_slice; array_slice++) {
1724 [enc copyFromTexture:read_texture
1725 sourceSlice:base_slice
1726 sourceLevel:mip
1727 sourceOrigin:MTLOriginMake(x_off, 0, 0)
1728 sourceSize:MTLSizeMake(width, 1, 1)
1729 toBuffer:destination_buffer
1730 destinationOffset:texture_array_relative_offset
1731 destinationBytesPerRow:bytes_per_row
1732 destinationBytesPerImage:bytes_per_row];
1733 texture_array_relative_offset += bytes_per_row;
1734 }
1735 copy_successful = true;
1736 }
1737 else {
1738 /* Use Compute READ. */
1739 id<MTLComputeCommandEncoder> compute_encoder =
1741 id<MTLComputePipelineState> pso = texture_read_1d_array_get_kernel(
1742 compute_specialization_kernel);
1743 TextureReadParams params = {
1744 mip,
1745 {width, height, 1},
1746 {x_off, y_off, 0},
1747 };
1748
1749 /* Bind resources via compute state for optimal state caching performance. */
1751 cs.bind_pso(pso);
1752 cs.bind_compute_bytes(&params, sizeof(params), 0);
1753 cs.bind_compute_buffer(destination_buffer, 0, 1);
1754 cs.bind_compute_texture(read_texture, 0);
1755 [compute_encoder dispatchThreads:MTLSizeMake(width, height, 1) /* Width, Height, Layer */
1756 threadsPerThreadgroup:MTLSizeMake(8, 8, 1)];
1757 copy_successful = true;
1758 }
1759 } break;
1760
1761 case GPU_TEXTURE_2D: {
1762 if (can_use_simple_read) {
1763 /* Use Blit Encoder READ. */
1764 id<MTLBlitCommandEncoder> enc = ctx->main_command_buffer.ensure_begin_blit_encoder();
1765 if (G.debug & G_DEBUG_GPU) {
1766 [enc insertDebugSignpost:@"GPUTextureRead2D"];
1767 }
1768 [enc copyFromTexture:read_texture
1769 sourceSlice:0
1770 sourceLevel:mip
1771 sourceOrigin:MTLOriginMake(x_off, y_off, 0)
1772 sourceSize:MTLSizeMake(width, height, 1)
1773 toBuffer:destination_buffer
1774 destinationOffset:0
1775 destinationBytesPerRow:bytes_per_row
1776 destinationBytesPerImage:bytes_per_image];
1777 copy_successful = true;
1778 }
1779 else {
1780
1781 /* Use Compute READ. */
1782 id<MTLComputeCommandEncoder> compute_encoder =
1784 id<MTLComputePipelineState> pso = texture_read_2d_get_kernel(
1785 compute_specialization_kernel);
1786 TextureReadParams params = {
1787 mip,
1788 {width, height, 1},
1789 {x_off, y_off, 0},
1790 };
1791
1792 /* Bind resources via compute state for optimal state caching performance. */
1794 cs.bind_pso(pso);
1795 cs.bind_compute_bytes(&params, sizeof(params), 0);
1796 cs.bind_compute_buffer(destination_buffer, 0, 1);
1797 cs.bind_compute_texture(read_texture, 0);
1798 [compute_encoder dispatchThreads:MTLSizeMake(width, height, 1) /* Width, Height, Layer */
1799 threadsPerThreadgroup:MTLSizeMake(8, 8, 1)];
1800 copy_successful = true;
1801 }
1802 } break;
1803
1804 case GPU_TEXTURE_2D_ARRAY: {
1805 if (can_use_simple_read) {
1806 /* Use Blit Encoder READ. */
1807 id<MTLBlitCommandEncoder> enc = ctx->main_command_buffer.ensure_begin_blit_encoder();
1808 if (G.debug & G_DEBUG_GPU) {
1809 [enc insertDebugSignpost:@"GPUTextureRead2DArray"];
1810 }
1811 int base_slice = z_off;
1812 int final_slice = base_slice + depth;
1813 size_t texture_array_relative_offset = 0;
1814
1815 for (int array_slice = base_slice; array_slice < final_slice; array_slice++) {
1816 [enc copyFromTexture:read_texture
1817 sourceSlice:array_slice
1818 sourceLevel:mip
1819 sourceOrigin:MTLOriginMake(x_off, y_off, 0)
1820 sourceSize:MTLSizeMake(width, height, 1)
1821 toBuffer:destination_buffer
1822 destinationOffset:texture_array_relative_offset
1823 destinationBytesPerRow:bytes_per_row
1824 destinationBytesPerImage:bytes_per_image];
1825 texture_array_relative_offset += bytes_per_image;
1826 }
1827 copy_successful = true;
1828 }
1829 else {
1830
1831 /* Use Compute READ */
1832 id<MTLComputeCommandEncoder> compute_encoder =
1834 id<MTLComputePipelineState> pso = texture_read_2d_array_get_kernel(
1835 compute_specialization_kernel);
1836 TextureReadParams params = {
1837 mip,
1838 {width, height, depth},
1839 {x_off, y_off, z_off},
1840 };
1841
1842 /* Bind resources via compute state for optimal state caching performance. */
1844 cs.bind_pso(pso);
1845 cs.bind_compute_bytes(&params, sizeof(params), 0);
1846 cs.bind_compute_buffer(destination_buffer, 0, 1);
1847 cs.bind_compute_texture(read_texture, 0);
1848 [compute_encoder
1849 dispatchThreads:MTLSizeMake(width, height, depth) /* Width, Height, Layer */
1850 threadsPerThreadgroup:MTLSizeMake(8, 8, 1)];
1851 copy_successful = true;
1852 }
1853 } break;
1854
1855 case GPU_TEXTURE_3D: {
1856 if (can_use_simple_read) {
1857 /* Use Blit Encoder READ. */
1858 id<MTLBlitCommandEncoder> enc = ctx->main_command_buffer.ensure_begin_blit_encoder();
1859 if (G.debug & G_DEBUG_GPU) {
1860 [enc insertDebugSignpost:@"GPUTextureRead3D"];
1861 }
1862 [enc copyFromTexture:read_texture
1863 sourceSlice:0
1864 sourceLevel:mip
1865 sourceOrigin:MTLOriginMake(x_off, y_off, z_off)
1866 sourceSize:MTLSizeMake(width, height, depth)
1867 toBuffer:destination_buffer
1868 destinationOffset:0
1869 destinationBytesPerRow:bytes_per_row
1870 destinationBytesPerImage:bytes_per_image];
1871 copy_successful = true;
1872 }
1873 else {
1874
1875 /* Use Compute READ. */
1876 id<MTLComputeCommandEncoder> compute_encoder =
1878 id<MTLComputePipelineState> pso = texture_read_3d_get_kernel(
1879 compute_specialization_kernel);
1880 TextureReadParams params = {
1881 mip,
1882 {width, height, depth},
1883 {x_off, y_off, z_off},
1884 };
1885
1886 /* Bind resources via compute state for optimal state caching performance. */
1888 cs.bind_pso(pso);
1889 cs.bind_compute_bytes(&params, sizeof(params), 0);
1890 cs.bind_compute_buffer(destination_buffer, 0, 1);
1891 cs.bind_compute_texture(read_texture, 0);
1892 [compute_encoder
1893 dispatchThreads:MTLSizeMake(width, height, depth) /* Width, Height, Layer */
1894 threadsPerThreadgroup:MTLSizeMake(4, 4, 4)];
1895 copy_successful = true;
1896 }
1897 } break;
1898
1899 case GPU_TEXTURE_CUBE:
1901 BLI_assert_msg(z_off == 0 || type_ == GPU_TEXTURE_CUBE_ARRAY,
1902 "z_off > 0 is only supported by TEXTURE CUBE ARRAY reads.");
1903 BLI_assert_msg(depth <= 6 || type_ == GPU_TEXTURE_CUBE_ARRAY,
1904 "depth > 6 is only supported by TEXTURE CUBE ARRAY reads. ");
1905 if (can_use_simple_read) {
1906 id<MTLBlitCommandEncoder> enc = ctx->main_command_buffer.ensure_begin_blit_encoder();
1907 if (G.debug & G_DEBUG_GPU) {
1908 [enc insertDebugSignpost:@"GPUTextureReadCubeArray"];
1909 }
1910
1911 /* NOTE: Depth should have a minimum value of 1 as we read at least one slice. */
1912 int base_slice = z_off;
1913 int final_slice = base_slice + depth;
1914 size_t texture_array_relative_offset = 0;
1915
1916 for (int array_slice = base_slice; array_slice < final_slice; array_slice++) {
1917 [enc copyFromTexture:read_texture
1918 sourceSlice:array_slice
1919 sourceLevel:mip
1920 sourceOrigin:MTLOriginMake(x_off, y_off, 0)
1921 sourceSize:MTLSizeMake(width, height, 1)
1922 toBuffer:destination_buffer
1923 destinationOffset:texture_array_relative_offset
1924 destinationBytesPerRow:bytes_per_row
1925 destinationBytesPerImage:bytes_per_image];
1926
1927 texture_array_relative_offset += bytes_per_image;
1928 }
1929 MTL_LOG_DEBUG("Copying texture data to buffer GPU_TEXTURE_CUBE_ARRAY");
1930 copy_successful = true;
1931 }
1932 else {
1933 MTL_LOG_ERROR("TODO(Metal): unsupported compute copy of texture cube array");
1934 }
1935 } break;
1936
1937 default:
1939 "gpu::MTLTexture::read_internal simple-copy not yet supported for texture "
1940 "type: %d",
1941 (int)type_);
1942 break;
1943 }
1944
1945 if (copy_successful) {
1946
1947 /* Use Blit encoder to synchronize results back to CPU. */
1948 if (dest_buf->get_resource_options() == MTLResourceStorageModeManaged) {
1949 id<MTLBlitCommandEncoder> enc = ctx->main_command_buffer.ensure_begin_blit_encoder();
1950 if (G.debug & G_DEBUG_GPU) {
1951 [enc insertDebugSignpost:@"GPUTextureRead-syncResource"];
1952 }
1953 [enc synchronizeResource:destination_buffer];
1954 }
1955
1956 /* Ensure GPU copy commands have completed. */
1957 GPU_finish();
1958
1959 /* Copy data from Shared Memory into ptr. */
1960 memcpy(r_data, destination_buffer_host_ptr, total_bytes);
1961 MTL_LOG_DEBUG("gpu::MTLTexture::read_internal success! %lu bytes read", total_bytes);
1962 }
1963 else {
1965 "gpu::MTLTexture::read_internal not yet supported for this config -- data "
1966 "format different (src %lu bytes, dst %lu bytes) (src format: %d, dst format: %d), or "
1967 "varying component counts (src %d, dst %d)",
1968 image_bpp,
1969 desired_output_bpp,
1970 (int)data_format,
1971 (int)desired_output_format,
1972 image_components,
1973 num_output_components);
1974 }
1975
1976 /* Release destination buffer. */
1977 dest_buf->free();
1978 }
1979}
1980
1982{
1983 this->prepare_internal();
1984 /* TODO(jbakker): Other limit checks should be added as well. When a texture violates a limit it
1985 * is not backed by a texture and will crash when used. */
1986 const int limit = GPU_max_texture_3d_size();
1987 if ((type_ == GPU_TEXTURE_3D) && (w_ > limit || h_ > limit || d_ > limit)) {
1988 return false;
1989 }
1990 return true;
1991}
1992
1994{
1995 MTLPixelFormat mtl_format = gpu_texture_format_to_metal(this->format_);
1996 mtl_max_mips_ = 1;
1997 mipmaps_ = 0;
1998 this->mip_range_set(0, 0);
1999
2000 /* Create texture from VertBuf's buffer. */
2001 MTLVertBuf *mtl_vbo = static_cast<MTLVertBuf *>(vbo);
2002 mtl_vbo->bind();
2003 mtl_vbo->flag_used();
2004
2005 /* Get Metal Buffer. */
2006 id<MTLBuffer> source_buffer = mtl_vbo->get_metal_buffer();
2007 BLI_assert(source_buffer);
2008
2009 /* Verify size. */
2010 if (w_ <= 0) {
2011 MTL_LOG_WARNING("Allocating texture buffer of width 0!");
2012 w_ = 1;
2013 }
2014
2015 /* Verify Texture and vertex buffer alignment. */
2016 const GPUVertFormat *format = GPU_vertbuf_get_format(vbo);
2017 size_t bytes_per_pixel = get_mtl_format_bytesize(mtl_format);
2018 size_t bytes_per_row = bytes_per_pixel * w_;
2019
2020 MTLContext *mtl_ctx = MTLContext::get();
2021 uint32_t align_requirement = uint32_t(
2022 [mtl_ctx->device minimumLinearTextureAlignmentForPixelFormat:mtl_format]);
2023
2024 /* If stride is larger than bytes per pixel, but format has multiple attributes,
2025 * split attributes across several pixels. */
2026 if (format->stride > bytes_per_pixel && format->attr_len > 1) {
2027
2028 /* We need to increase the number of pixels available to store additional attributes.
2029 * First ensure that the total stride of the vertex format fits uniformly into
2030 * multiple pixels. If these sizes are different, then attributes are of differing
2031 * sizes and this operation is unsupported. */
2032 if (bytes_per_pixel * format->attr_len != format->stride) {
2033 BLI_assert_msg(false,
2034 "Cannot split attributes across multiple pixels as attribute format sizes do "
2035 "not match.");
2036 return false;
2037 }
2038
2039 /* Provide a single pixel per attribute. */
2040 /* Increase bytes per row to ensure there are enough bytes for all vertex attribute data. */
2041 bytes_per_row *= format->attr_len;
2042 BLI_assert(bytes_per_row == format->stride * w_);
2043
2044 /* Multiply width of image to provide one attribute per pixel. */
2045 w_ *= format->attr_len;
2046 BLI_assert(bytes_per_row == bytes_per_pixel * w_);
2047 BLI_assert_msg(w_ == mtl_vbo->vertex_len * format->attr_len,
2048 "Image should contain one pixel for each attribute in every vertex.");
2049 }
2050 else {
2051 /* Verify per-vertex size aligns with texture size. */
2052 BLI_assert(bytes_per_pixel == format->stride &&
2053 "Pixel format stride MUST match the texture format stride -- These being different "
2054 "is likely caused by Metal's VBO padding to a minimum of 4-bytes per-vertex."
2055 " If multiple attributes are used. Each attribute is to be packed into its own "
2056 "individual pixel when stride length is exceeded. ");
2057 }
2058
2059 /* Create texture descriptor. */
2061 texture_descriptor_ = [[MTLTextureDescriptor alloc] init];
2062 texture_descriptor_.pixelFormat = mtl_format;
2063 texture_descriptor_.textureType = MTLTextureTypeTextureBuffer;
2064 texture_descriptor_.width = w_;
2065 texture_descriptor_.height = 1;
2066 texture_descriptor_.depth = 1;
2067 texture_descriptor_.arrayLength = 1;
2068 texture_descriptor_.mipmapLevelCount = mtl_max_mips_;
2069 texture_descriptor_.usage =
2070 MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite |
2071 MTLTextureUsagePixelFormatView; /* TODO(Metal): Optimize usage flags. */
2072 texture_descriptor_.storageMode = [source_buffer storageMode];
2073 texture_descriptor_.sampleCount = 1;
2074 texture_descriptor_.cpuCacheMode = [source_buffer cpuCacheMode];
2075 texture_descriptor_.hazardTrackingMode = [source_buffer hazardTrackingMode];
2076
2077 texture_ = [source_buffer
2078 newTextureWithDescriptor:texture_descriptor_
2079 offset:0
2080 bytesPerRow:ceil_to_multiple_ul(bytes_per_row, align_requirement)];
2081 aligned_w_ = bytes_per_row / bytes_per_pixel;
2082
2083 BLI_assert(texture_);
2084 texture_.label = [NSString stringWithUTF8String:this->get_name()];
2085 is_baked_ = true;
2086 is_dirty_ = false;
2087 resource_mode_ = MTL_TEXTURE_MODE_VBO;
2088
2089 /* Track Status. */
2090 vert_buffer_ = mtl_vbo;
2091 vert_buffer_mtl_ = source_buffer;
2092
2093 return true;
2094}
2095
2097{
2098 BLI_assert(src);
2099
2100 /* Zero initialize. */
2101 this->prepare_internal();
2102
2103 /* Flag as using texture view. */
2104 resource_mode_ = MTL_TEXTURE_MODE_TEXTURE_VIEW;
2105 texture_view_dirty_flags_ |= TEXTURE_VIEW_MIP_DIRTY;
2106
2107 /* Assign usage. */
2108 internal_gpu_image_usage_flags_ = GPU_texture_usage(src);
2109
2110 /* Assign texture as view. */
2111 gpu::MTLTexture *mtltex = static_cast<gpu::MTLTexture *>(src);
2112 mtltex->ensure_baked();
2113 texture_ = mtltex->texture_;
2114 BLI_assert(texture_);
2115 [texture_ retain];
2116
2117 /* Flag texture as baked -- we do not need explicit initialization. */
2118 is_baked_ = true;
2119 is_dirty_ = false;
2120
2121 /* Stencil view support. */
2122 texture_view_stencil_ = false;
2123 if (use_stencil) {
2124 BLI_assert(ELEM(format_, TextureFormat::SFLOAT_32_DEPTH_UINT_8));
2125 texture_view_stencil_ = true;
2126 }
2127
2128 /* Bake mip swizzle view. */
2129 bake_mip_swizzle_view();
2130 return true;
2131}
2132
2134
2135/* -------------------------------------------------------------------- */
2138
2140{
2141 return is_baked_;
2142}
2143
2144/* Prepare texture parameters after initialization, but before baking. */
2145void gpu::MTLTexture::prepare_internal()
2146{
2147 /* Take a copy of the flags so that any modifications we make won't effect the texture
2148 * cache/pool match finding test. */
2149 internal_gpu_image_usage_flags_ = gpu_image_usage_flags_;
2150
2151 /* Metal: Texture clearing is done using frame-buffer clear. This has no performance impact or
2152 * bandwidth implications for lossless compression and is considered best-practice.
2153 *
2154 * Attachment usage also required for depth-stencil attachment targets, for depth-update support.
2155 * NOTE: Emulated atomic textures cannot support render-target usage. For clearing, the backing
2156 * buffer is cleared instead.
2157 */
2158 if (!((internal_gpu_image_usage_flags_ & GPU_TEXTURE_USAGE_ATOMIC) &&
2159 !MTLBackend::get_capabilities().supports_texture_atomics))
2160 {
2161 /* Force attachment usage - see comment above. */
2162 internal_gpu_image_usage_flags_ |= GPU_TEXTURE_USAGE_ATTACHMENT;
2163 }
2164
2165 /* Derive maximum number of mip levels by default.
2166 * TODO(Metal): This can be removed if max mip counts are specified upfront. */
2167 if (type_ == GPU_TEXTURE_1D || type_ == GPU_TEXTURE_1D_ARRAY || type_ == GPU_TEXTURE_BUFFER) {
2168 mtl_max_mips_ = 1;
2169 }
2170 else {
2171 /* Require correct explicit mipmap level counts. */
2172 mtl_max_mips_ = mipmaps_;
2173 }
2174}
2175
2176void gpu::MTLTexture::ensure_baked()
2177{
2178
2179 /* If properties have changed, re-bake. */
2180 id<MTLTexture> previous_texture = nil;
2181 bool copy_previous_contents = false;
2182
2183 if (is_baked_ && is_dirty_) {
2184 copy_previous_contents = true;
2185 previous_texture = texture_;
2186 [previous_texture retain];
2187 this->reset();
2188 }
2189
2190 if (!is_baked_) {
2191 MTLContext *ctx = MTLContext::get();
2192 BLI_assert(ctx);
2193
2194 /* Ensure texture mode is valid. */
2195 BLI_assert(resource_mode_ != MTL_TEXTURE_MODE_EXTERNAL);
2196 BLI_assert(resource_mode_ != MTL_TEXTURE_MODE_TEXTURE_VIEW);
2197 BLI_assert(resource_mode_ != MTL_TEXTURE_MODE_VBO);
2198
2199 /* Format and mip levels (TODO(Metal): Optimize mipmaps counts, specify up-front). */
2200 MTLPixelFormat mtl_format = gpu_texture_format_to_metal(format_);
2201
2202 /* SRGB textures require a texture view for reading data and when rendering with SRGB
2203 * disabled. Enabling the texture_view or texture_read usage flags disables lossless
2204 * compression, so the situations in which it is used should be limited. */
2205 if (format_ == TextureFormat::SRGBA_8_8_8_8) {
2206 internal_gpu_image_usage_flags_ |= GPU_TEXTURE_USAGE_FORMAT_VIEW;
2207 }
2208
2209 /* Create texture descriptor. */
2210 switch (type_) {
2211
2212 /* 1D */
2213 case GPU_TEXTURE_1D:
2214 case GPU_TEXTURE_1D_ARRAY: {
2215 BLI_assert(w_ > 0);
2216 texture_descriptor_ = [[MTLTextureDescriptor alloc] init];
2217 texture_descriptor_.pixelFormat = mtl_format;
2218 texture_descriptor_.textureType = (type_ == GPU_TEXTURE_1D_ARRAY) ? MTLTextureType1DArray :
2219 MTLTextureType1D;
2220 texture_descriptor_.width = w_;
2221 texture_descriptor_.height = 1;
2222 texture_descriptor_.depth = 1;
2223 texture_descriptor_.arrayLength = (type_ == GPU_TEXTURE_1D_ARRAY) ? h_ : 1;
2224 texture_descriptor_.mipmapLevelCount = (mtl_max_mips_ > 0) ? mtl_max_mips_ : 1;
2225 texture_descriptor_.usage = mtl_usage_from_gpu(internal_gpu_image_usage_flags_);
2226 texture_descriptor_.storageMode = MTLStorageModePrivate;
2227 texture_descriptor_.sampleCount = 1;
2228 texture_descriptor_.cpuCacheMode = MTLCPUCacheModeDefaultCache;
2229 texture_descriptor_.hazardTrackingMode = MTLHazardTrackingModeDefault;
2230 } break;
2231
2232 /* 2D */
2233 case GPU_TEXTURE_2D:
2234 case GPU_TEXTURE_2D_ARRAY: {
2235 BLI_assert(w_ > 0 && h_ > 0);
2236 texture_descriptor_ = [[MTLTextureDescriptor alloc] init];
2237 texture_descriptor_.pixelFormat = mtl_format;
2238 texture_descriptor_.textureType = (type_ == GPU_TEXTURE_2D_ARRAY) ? MTLTextureType2DArray :
2239 MTLTextureType2D;
2240 texture_descriptor_.width = w_;
2241 texture_descriptor_.height = h_;
2242 texture_descriptor_.depth = 1;
2243 texture_descriptor_.arrayLength = (type_ == GPU_TEXTURE_2D_ARRAY) ? d_ : 1;
2244 texture_descriptor_.mipmapLevelCount = (mtl_max_mips_ > 0) ? mtl_max_mips_ : 1;
2245 texture_descriptor_.usage = mtl_usage_from_gpu(internal_gpu_image_usage_flags_);
2246 texture_descriptor_.storageMode = MTLStorageModePrivate;
2247 texture_descriptor_.sampleCount = 1;
2248 texture_descriptor_.cpuCacheMode = MTLCPUCacheModeDefaultCache;
2249 texture_descriptor_.hazardTrackingMode = MTLHazardTrackingModeDefault;
2250 } break;
2251
2252 /* 3D */
2253 case GPU_TEXTURE_3D: {
2254 BLI_assert(w_ > 0 && h_ > 0 && d_ > 0);
2255 texture_descriptor_ = [[MTLTextureDescriptor alloc] init];
2256 texture_descriptor_.pixelFormat = mtl_format;
2257 texture_descriptor_.textureType = MTLTextureType3D;
2258 texture_descriptor_.width = w_;
2259 texture_descriptor_.height = h_;
2260 texture_descriptor_.depth = d_;
2261 texture_descriptor_.arrayLength = 1;
2262 texture_descriptor_.mipmapLevelCount = (mtl_max_mips_ > 0) ? mtl_max_mips_ : 1;
2263 texture_descriptor_.usage = mtl_usage_from_gpu(internal_gpu_image_usage_flags_);
2264 texture_descriptor_.storageMode = MTLStorageModePrivate;
2265 texture_descriptor_.sampleCount = 1;
2266 texture_descriptor_.cpuCacheMode = MTLCPUCacheModeDefaultCache;
2267 texture_descriptor_.hazardTrackingMode = MTLHazardTrackingModeDefault;
2268 } break;
2269
2270 /* CUBE TEXTURES */
2271 case GPU_TEXTURE_CUBE:
2273 /* NOTE: For a cube-map 'Texture::d_' refers to total number of faces,
2274 * not just array slices. */
2275 BLI_assert(w_ > 0 && h_ > 0);
2276 texture_descriptor_ = [[MTLTextureDescriptor alloc] init];
2277 texture_descriptor_.pixelFormat = mtl_format;
2278 texture_descriptor_.textureType = (type_ == GPU_TEXTURE_CUBE_ARRAY) ?
2279 MTLTextureTypeCubeArray :
2280 MTLTextureTypeCube;
2281 texture_descriptor_.width = w_;
2282 texture_descriptor_.height = h_;
2283 texture_descriptor_.depth = 1;
2284 texture_descriptor_.arrayLength = (type_ == GPU_TEXTURE_CUBE_ARRAY) ? d_ / 6 : 1;
2285 texture_descriptor_.mipmapLevelCount = (mtl_max_mips_ > 0) ? mtl_max_mips_ : 1;
2286 texture_descriptor_.usage = mtl_usage_from_gpu(internal_gpu_image_usage_flags_);
2287 texture_descriptor_.storageMode = MTLStorageModePrivate;
2288 texture_descriptor_.sampleCount = 1;
2289 texture_descriptor_.cpuCacheMode = MTLCPUCacheModeDefaultCache;
2290 texture_descriptor_.hazardTrackingMode = MTLHazardTrackingModeDefault;
2291 } break;
2292
2293 /* GPU_TEXTURE_BUFFER */
2294 case GPU_TEXTURE_BUFFER: {
2295 texture_descriptor_ = [[MTLTextureDescriptor alloc] init];
2296 texture_descriptor_.pixelFormat = mtl_format;
2297 texture_descriptor_.textureType = MTLTextureTypeTextureBuffer;
2298 texture_descriptor_.width = w_;
2299 texture_descriptor_.height = 1;
2300 texture_descriptor_.depth = 1;
2301 texture_descriptor_.arrayLength = 1;
2302 texture_descriptor_.mipmapLevelCount = (mtl_max_mips_ > 0) ? mtl_max_mips_ : 1;
2303 texture_descriptor_.usage = mtl_usage_from_gpu(internal_gpu_image_usage_flags_);
2304 texture_descriptor_.storageMode = MTLStorageModePrivate;
2305 texture_descriptor_.sampleCount = 1;
2306 texture_descriptor_.cpuCacheMode = MTLCPUCacheModeDefaultCache;
2307 texture_descriptor_.hazardTrackingMode = MTLHazardTrackingModeDefault;
2308 } break;
2309
2310 default: {
2311 MTL_LOG_ERROR("[METAL] Error: Cannot create texture with unknown type: %d\n", type_);
2312 return;
2313 } break;
2314 }
2315
2316 /* Determine Resource Mode. */
2317 resource_mode_ = MTL_TEXTURE_MODE_DEFAULT;
2318
2319 /* Override storage mode if memoryless attachments are being used.
2320 * NOTE: Memoryless textures can only be supported on TBDR GPUs. */
2321 if (internal_gpu_image_usage_flags_ & GPU_TEXTURE_USAGE_MEMORYLESS) {
2322 const bool is_tile_based_arch = (GPU_platform_architecture() == GPU_ARCHITECTURE_TBDR);
2323 if (is_tile_based_arch) {
2324 texture_descriptor_.storageMode = MTLStorageModeMemoryless;
2325 }
2326 }
2327
2334 bool native_texture_atomics = MTLBackend::get_capabilities().supports_texture_atomics;
2335 if ((internal_gpu_image_usage_flags_ & GPU_TEXTURE_USAGE_ATOMIC) && !native_texture_atomics) {
2336
2337 /* Validate format support. */
2339 "Texture atomic fallback support is only available for GPU_TEXTURE_2D, "
2340 "GPU_TEXTURE_2D_ARRAY and GPU_TEXTURE_3D.");
2341
2342 /* Re-assign 2D resolution to encompass all texture layers.
2343 * Texture access is handled by remapping to a linear ID and using this in the destination
2344 * texture. based on original with: LinearPxID = x + y*layer_w + z*(layer_h*layer_w);
2345 * tx_2d.y = LinearPxID/2D_tex_width;
2346 * tx_2d.x = LinearPxID - (tx_2d.y*2D_tex_width); */
2348 /* Maximum 2D texture dimensions will be (16384, 16384) on all target platforms. */
2349 const uint max_width = 16384;
2350 const uint max_height = 16384;
2351 const uint pixels_res = w_ * h_ * d_;
2352
2353 uint new_w = 0, new_h = 0;
2354 if (pixels_res <= max_width) {
2355 new_w = pixels_res;
2356 new_h = 1;
2357 }
2358 else {
2359 new_w = max_width;
2360 new_h = ((pixels_res % new_w) == 0) ? (pixels_res / new_w) : ((pixels_res / new_w) + 1);
2361 }
2362
2363 texture_descriptor_.width = new_w;
2364 texture_descriptor_.height = new_h;
2365
2366 UNUSED_VARS_NDEBUG(max_height);
2367 BLI_assert_msg(texture_descriptor_.width <= max_width &&
2368 texture_descriptor_.height <= max_height,
2369 "Atomic fallback support texture is too large.");
2370 }
2371
2372 /* Allocate buffer for texture data. */
2373 size_t bytes_per_pixel = get_mtl_format_bytesize(mtl_format);
2374 size_t bytes_per_row = bytes_per_pixel * texture_descriptor_.width;
2375 size_t total_bytes = bytes_per_row * texture_descriptor_.height;
2376
2378 total_bytes, (internal_gpu_image_usage_flags_ & GPU_TEXTURE_USAGE_HOST_READ));
2379 BLI_assert(backing_buffer_ != nullptr);
2380
2381 /* NOTE: Fallback buffer-backed texture always set to Texture2D. */
2382 texture_descriptor_.textureType = MTLTextureType2D;
2383 texture_descriptor_.depth = 1;
2384 texture_descriptor_.arrayLength = 1;
2385
2386 /* Write texture dimensions to metadata. This is required to remap 2D Array/3D sample
2387 * coordinates into 2D texture space. */
2388 tex_buffer_metadata_[0] = w_;
2389 tex_buffer_metadata_[1] = h_;
2390 tex_buffer_metadata_[2] = d_;
2391
2392 /* Texture allocation with buffer as backing storage. Bytes per row must satisfy alignment
2393 * rules for device. */
2394 uint32_t align_requirement = uint32_t(
2395 [ctx->device minimumLinearTextureAlignmentForPixelFormat:mtl_format]);
2396 size_t aligned_bytes_per_row = ceil_to_multiple_ul(bytes_per_row, align_requirement);
2397 texture_ = [backing_buffer_->get_metal_buffer()
2398 newTextureWithDescriptor:texture_descriptor_
2399 offset:0
2400 bytesPerRow:aligned_bytes_per_row];
2401 /* Aligned width. */
2402 tex_buffer_metadata_[3] = bytes_per_row / bytes_per_pixel;
2403
2404#ifndef NDEBUG
2405 texture_.label = [NSString
2406 stringWithFormat:@"AtomicBufferBackedTexture_%s", this->get_name()];
2407#endif
2408 }
2409 else {
2410
2411 /* Standard texture allocation. */
2412 texture_ = [ctx->device newTextureWithDescriptor:texture_descriptor_];
2413
2414#ifndef NDEBUG
2415 if (internal_gpu_image_usage_flags_ & GPU_TEXTURE_USAGE_MEMORYLESS) {
2416 texture_.label = [NSString stringWithFormat:@"MemorylessTexture_%s", this->get_name()];
2417 }
2418 else {
2419 texture_.label = [NSString stringWithFormat:@"Texture_%s", this->get_name()];
2420 }
2421#endif
2422 }
2423
2424 BLI_assert(texture_);
2425 is_baked_ = true;
2426 is_dirty_ = false;
2427 }
2428
2429 /* Re-apply previous contents. */
2430 if (copy_previous_contents) {
2431 /* TODO(Metal): May need to copy previous contents of texture into new texture. */
2432 [previous_texture release];
2433 }
2434}
2435
2436void gpu::MTLTexture::reset()
2437{
2438 MTL_LOG_DEBUG("Texture %s reset. Size %d, %d, %d", this->get_name(), w_, h_, d_);
2439 /* Delete associated METAL resources. */
2440 if (texture_ != nil) {
2441 [texture_ release];
2442 texture_ = nil;
2443 is_baked_ = false;
2444 is_dirty_ = true;
2445 }
2446
2447 /* Release backing Metal buffer, if used. */
2448 if (backing_buffer_ != nullptr) {
2449 backing_buffer_->free();
2450 backing_buffer_ = nullptr;
2451 }
2452
2453 /* Release backing storage buffer, if used. */
2454 if (storage_buffer_ != nullptr) {
2455 delete storage_buffer_;
2456 storage_buffer_ = nullptr;
2457 }
2458
2459 if (texture_no_srgb_ != nil) {
2460 [texture_no_srgb_ release];
2461 texture_no_srgb_ = nil;
2462 }
2463
2464 if (mip_swizzle_view_ != nil) {
2465 [mip_swizzle_view_ release];
2466 mip_swizzle_view_ = nil;
2467 }
2468
2469 /* Blit framebuffer. */
2470 if (blit_fb_) {
2471 GPU_framebuffer_free(blit_fb_);
2472 blit_fb_ = nullptr;
2473 }
2474
2475 /* Descriptor. */
2476 if (texture_descriptor_ != nullptr) {
2477 [texture_descriptor_ release];
2478 texture_descriptor_ = nullptr;
2479 }
2480
2481 /* Reset mipmap state. */
2482 has_generated_mips_ = false;
2483
2484 BLI_assert(texture_ == nil);
2485 BLI_assert(mip_swizzle_view_ == nil);
2486}
2487
2489
2490/* -------------------------------------------------------------------- */
2494{
2496 backing_buffer_ != nullptr,
2497 "Resource must have been created as a buffer backed resource to support SSBO wrapping.");
2498 /* Ensure texture resource is up to date. */
2499 this->ensure_baked();
2500 if (storage_buffer_ == nil) {
2501 BLI_assert(texture_ != nullptr);
2502 id<MTLBuffer> backing_buffer = [texture_ buffer];
2503 BLI_assert(backing_buffer != nil);
2504 storage_buffer_ = new MTLStorageBuf(this, [backing_buffer length]);
2505 }
2506 return storage_buffer_;
2507}
2508
2510
2511/* -------------------------------------------------------------------- */
2514
2516{
2517 return (format_ == TextureFormat::SRGBA_8_8_8_8);
2518}
2519
2521{
2523 BLI_assert(base_tex != nil);
2524 if (texture_no_srgb_ == nil) {
2525 texture_no_srgb_ = [base_tex newTextureViewWithPixelFormat:MTLPixelFormatRGBA8Unorm];
2526 }
2527 return texture_no_srgb_;
2528}
2529
2531
2532/* -------------------------------------------------------------------- */
2535
2537{
2538 /* Ensure buffer satisfies the alignment of 256 bytes for copying
2539 * data between buffers and textures. As specified in:
2540 * https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf */
2541 BLI_assert(size >= 256);
2542 buffer_ = nil;
2543}
2544
2546{
2547 if (buffer_) {
2548 [buffer_ release];
2549 buffer_ = nil;
2550 }
2551}
2552
2554{
2555 /* Duplicate the existing buffer and release original to ensure we do not directly modify data
2556 * in-flight on the GPU. */
2557 MTLContext *ctx = MTLContext::get();
2558 BLI_assert(ctx);
2559 MTLResourceOptions resource_options = ([ctx->device hasUnifiedMemory]) ?
2560 MTLResourceStorageModeShared :
2561 MTLResourceStorageModeManaged;
2562
2563 if (buffer_ != nil) {
2564 id<MTLBuffer> new_buffer = [ctx->device newBufferWithBytes:[buffer_ contents]
2566 options:resource_options];
2567 [buffer_ release];
2568 buffer_ = new_buffer;
2569 }
2570 else {
2571 buffer_ = [ctx->device newBufferWithLength:size_ options:resource_options];
2572 }
2573
2574 return [buffer_ contents];
2575}
2576
2578{
2579 if (buffer_ == nil) {
2580 return;
2581 }
2582
2583 /* Ensure changes are synchronized. */
2584 if (buffer_.resourceOptions & MTLResourceStorageModeManaged) {
2585 [buffer_ didModifyRange:NSMakeRange(0, size_)];
2586 }
2587}
2588
2590{
2591 GPUPixelBufferNativeHandle native_handle;
2592
2593 /* Only supported with unified memory currently. */
2594 MTLContext *ctx = MTLContext::get();
2595 BLI_assert(ctx);
2596 if (![ctx->device hasUnifiedMemory]) {
2597 return native_handle;
2598 }
2599
2600 /* Just get pointer to unified memory. No need to unmap. */
2601 map();
2602 native_handle.handle = reinterpret_cast<int64_t>(buffer_);
2603 native_handle.size = size_;
2604
2605 return native_handle;
2606}
2607
2609{
2610 return size_;
2611}
2612
2614{
2615 return buffer_;
2616}
2617
2619
2620} // namespace blender::gpu
#define GPU_batch_texture_bind(batch, name, tex)
Definition GPU_batch.hh:284
#define GPU_ATTACHMENT_NONE
#define GPU_framebuffer_ensure_config(_fb,...)
#define GPU_ATTACHMENT_TEXTURE_LAYER_MIP(_texture, _layer, _mip)
Read Blender memory allocation and freeing.
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert(a)
#define ELEM(...)
#define BLI_assert_msg(a, msg)
btScalar max
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
void reset()
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
constexpr T * data() const
Definition BLI_span.hh:545
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr const T * data() const
Definition BLI_span.hh:219
static MTLCapabilities & get_capabilities()
gpu::MTLBuffer * allocate_with_data(uint64_t size, bool cpu_visible, const void *data=nullptr)
gpu::MTLBuffer * allocate(uint64_t size, bool cpu_visible)
gpu::MTLBuffer * allocate_aligned(uint64_t size, uint alignment, bool cpu_visible)
id< MTLBuffer > get_metal_buffer() const
id< MTLComputeCommandEncoder > ensure_begin_compute_encoder()
id< MTLBlitCommandEncoder > ensure_begin_blit_encoder()
void bind_compute_bytes(const void *bytes, uint64_t length, uint index)
void bind_compute_texture(id< MTLTexture > tex, uint slot)
void bind_compute_buffer(id< MTLBuffer > buffer, uint64_t buffer_offset, uint index)
void bind_pso(id< MTLComputePipelineState > pso)
static MTLContext * get()
MTLCommandBufferManager main_command_buffer
static MTLBufferPool * get_global_memory_manager()
GPUPixelBufferNativeHandle get_native_handle() override
id< MTLBuffer > get_metal_buffer()
MTLStorageBuf(size_t size, GPUUsageType usage, const char *name)
void update_sub(int mip, int offset[3], int extent[3], eGPUDataFormat type, const void *data, const uint unpack_row_length) override
id< MTLTexture > get_non_srgb_handle()
id< MTLTexture > get_metal_handle()
MTLTexture(const char *name)
MTLSamplerState get_sampler_state()
void read(int mip, eGPUDataFormat type, void *data) override
id< MTLTexture > get_metal_handle_base()
MTLStorageBuf * get_storagebuf()
void generate_mipmap() override
bool init_internal() override
void swizzle_set(const char swizzle_mask[4]) override
void clear(const double4 data) override
void copy_to(Texture *dst, IndexRange mip_levels) override
virtual void texture_unbind(Texture *tex)=0
eGPUTextureUsage gpu_image_usage_flags_
size_t read_size_get(int mip, eGPUDataFormat format) const
bool init_2D(int w, int h, int layers, int mip_len, TextureFormat format)
GPUAttachmentType attachment_type(int slot) const
void mip_size_get(int mip, int r_size[3]) const
TextureFormat format_get() const
Texture(const char *name)
GPUTextureFormatFlag format_flag_
CCL_NAMESPACE_BEGIN struct Options options
#define log2
#define floor
float length(VecOp< float, D >) RET
#define MTL_LOG_WARNING(info,...)
Definition mtl_debug.hh:39
#define MTL_LOG_DEBUG(info,...)
Definition mtl_debug.hh:46
#define MTL_LOG_ERROR(info,...)
Definition mtl_debug.hh:31
std::string get_name(const VolumeGridData &grid)
size_t get_mtl_format_bytesize(MTLPixelFormat tex_format)
MTLPixelFormat gpu_texture_format_to_metal(TextureFormat tex_format)
bool is_half_float(TextureFormat format)
std::string tex_data_format_to_msl_type_str(eGPUDataFormat type)
eGPUDataFormat to_texture_data_format(TextureFormat tex_format)
std::string tex_data_format_to_msl_texture_template_type(eGPUDataFormat type)
MTLPixelFormat mtl_format_get_writeable_view_format(MTLPixelFormat format)
static MTLTextureSwizzle swizzle_to_mtl(const char swizzle)
BLI_INLINE MTLTexture * unwrap(Texture *tex)
size_t to_block_size(TextureFormat data_type)
int get_mtl_format_num_components(MTLPixelFormat tex_format)
int to_bytesize(const DataFormat format)
MTLTextureUsage mtl_usage_from_gpu(eGPUTextureUsage usage)
int to_component_len(TextureFormat format)
constexpr bool validate_data_format(TextureFormat tex_format, eGPUDataFormat data_format)
MTLTextureType to_metal_type(GPUTextureType type)
eGPUTextureUsage gpu_usage_from_mtl(MTLTextureUsage mtl_usage)
void float_to_half_make_finite_array(const float *src, uint16_t *dst, size_t length)
Definition math_half.cc:355
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
void GPU_framebuffer_bind(gpu::FrameBuffer *fb)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE unsigned long long max_ulul(unsigned long long a, unsigned long long b)
void GPU_scissor_test(bool enable)
Definition gpu_state.cc:173
@ GPU_CULL_NONE
Definition GPU_state.hh:157
gpu::FrameBuffer * GPU_framebuffer_active_get()
__int64 int64_t
Definition BKE_text.h:90
void GPU_shader_uniform_2f(gpu::Shader *sh, const char *name, float x, float y)
void GPU_framebuffer_restore()
unsigned int uint
Definition BKE_text.h:55
eGPUTextureUsage GPU_texture_usage(const gpu::Texture *texture)
void GPU_framebuffer_free(gpu::FrameBuffer *fb)
GPUDepthTest GPU_depth_test_get()
Definition gpu_state.cc:229
BLI_INLINE float fb(float length, float L)
@ GPU_DATA_2_10_10_10_REV
@ GPU_DATA_HALF_FLOAT
@ GPU_DATA_10_11_11_REV
@ GPU_DATA_UINT_24_8_DEPRECATED
GPUStencilTest GPU_stencil_test_get()
Definition gpu_state.cc:235
VecBase< uint32_t, 4 > uint4
void GPU_depth_mask(bool depth)
Definition gpu_state.cc:107
MINLINE int max_ii(int a, int b)
void GPU_batch_set_shader(gpu::Batch *batch, gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
const GPUVertFormat * GPU_vertbuf_get_format(const gpu::VertBuf *verts)
GPUFaceCullTest GPU_face_culling_get()
Definition gpu_state.cc:54
void GPU_stencil_reference_set(uint reference)
Definition gpu_state.cc:190
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
Global G
Definition blender.cc:57
unsigned short uint16_t
Definition BKE_text.h:80
void * MEM_new_uninitialized_aligned(size_t len, size_t alignment, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(3)
unsigned int uint32_t
Definition BKE_text.h:81
@ GPU_DEPTH_ALWAYS
Definition GPU_state.hh:133
MINLINE uint64_t ceil_to_multiple_ul(uint64_t a, uint64_t b)
@ GPU_ARCHITECTURE_TBDR
@ GPU_TEXTURE_USAGE_FORMAT_VIEW
@ GPU_TEXTURE_USAGE_SHADER_WRITE
@ GPU_TEXTURE_USAGE_HOST_READ
@ GPU_TEXTURE_USAGE_ATOMIC
@ GPU_TEXTURE_USAGE_ATTACHMENT
@ GPU_TEXTURE_USAGE_MEMORYLESS
BMesh const char void * data
GPUArchitectureType GPU_platform_architecture()
uint GPU_stencil_mask_get()
Definition gpu_state.cc:223
bool GPU_depth_mask_get()
Definition gpu_state.cc:272
unsigned char uint8_t
Definition BKE_text.h:79
void GPU_stencil_test(GPUStencilTest test)
Definition gpu_state.cc:75
void GPU_batch_draw(gpu::Batch *batch)
void GPU_shader_uniform_1i(gpu::Shader *sh, const char *name, int value)
@ GPU_STENCIL_ALWAYS
Definition GPU_state.hh:143
gpu::FrameBuffer * GPU_framebuffer_create(const char *name)
gpu::Batch * GPU_batch_preset_quad()
GPUBlend GPU_blend_get()
Definition gpu_state.cc:211
MINLINE uint divide_ceil_u(uint a, uint b)
void GPU_face_culling(GPUFaceCullTest culling)
Definition gpu_state.cc:49
void GPU_stencil_write_mask_set(uint write_mask)
Definition gpu_state.cc:195
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
int GPU_max_texture_3d_size()
MINLINE int min_ii(int a, int b)
void GPU_finish()
Definition gpu_state.cc:289
void GPU_depth_test(GPUDepthTest test)
Definition gpu_state.cc:70
VecBase< double, 4 > double4
#define min(a, b)
Definition sort.cc:38