| 334 | } |
| 335 | |
| 336 | static void _sg_metal_encode_texture_pixels(int x, int y, int w, int h, id<MTLTexture> mtl_src_texture, void* pixels) { |
| 337 | SOKOL_ASSERT(!_sg.mtl.in_pass); |
| 338 | _sg_metal_commit_command_buffer(); |
| 339 | MTLTextureDescriptor* mtl_dst_texture_desc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:mtl_src_texture.pixelFormat width:w height:h mipmapped:NO]; |
| 340 | mtl_dst_texture_desc.storageMode = MTLStorageModeManaged; |
| 341 | mtl_dst_texture_desc.resourceOptions = MTLResourceStorageModeManaged; |
| 342 | mtl_dst_texture_desc.usage = MTLTextureUsageShaderRead + MTLTextureUsageShaderWrite; |
| 343 | id<MTLTexture> mtl_dst_texture = [mtl_src_texture.device newTextureWithDescriptor:mtl_dst_texture_desc]; |
| 344 | id<MTLCommandBuffer> cmd_buffer = [_sg.mtl.cmd_queue commandBuffer]; |
| 345 | id<MTLBlitCommandEncoder> blit_encoder = [cmd_buffer blitCommandEncoder]; |
| 346 | [blit_encoder copyFromTexture:mtl_src_texture |
| 347 | sourceSlice:0 |
| 348 | sourceLevel:0 |
| 349 | sourceOrigin:MTLOriginMake(x,y,0) |
| 350 | sourceSize:MTLSizeMake(w,h,1) |
| 351 | toTexture:mtl_dst_texture |
| 352 | destinationSlice:0 |
| 353 | destinationLevel:0 |
| 354 | destinationOrigin:MTLOriginMake(0,0,0) |
| 355 | ]; |
| 356 | [blit_encoder synchronizeTexture:mtl_dst_texture slice:0 level:0]; |
| 357 | [blit_encoder endEncoding]; |
| 358 | [cmd_buffer commit]; |
| 359 | [cmd_buffer waitUntilCompleted]; |
| 360 | |
| 361 | MTLRegion mtl_region = MTLRegionMake2D(0, 0, w, h); |
| 362 | void* temp_pixels = (void*)SOKOL_MALLOC(w * 4 * h); |
| 363 | SOKOL_ASSERT(temp_pixels); |
| 364 | [mtl_dst_texture getBytes:temp_pixels bytesPerRow:w * 4 fromRegion:mtl_region mipmapLevel:0]; |
| 365 | int res = SDL_ConvertPixels(w, h, _sg_metal_texture_format_to_sdl_pixel_format(mtl_dst_texture_desc.pixelFormat), temp_pixels, w * 4, SDL_PIXELFORMAT_RGBA32, pixels, w * 4); |
| 366 | SOKOL_FREE(temp_pixels); |
| 367 | SOKOL_ASSERT(res == 0); |
| 368 | _SOKOL_UNUSED(res); |
| 369 | } |
| 370 | |
| 371 | static void _sg_metal_query_image_pixels(_sg_image_t* img, void* pixels) { |
| 372 | id<MTLTexture> mtl_src_texture = _sg.mtl.idpool.pool[img->mtl.tex[0]]; |
no test coverage detected