Hi I am trying to convert below objective c code into swift but struggling to convert unions which are supported into C but not directly in swift.
I am not sure how I can convert below union type and pass it to MTLTexture getbytes?
union { float f[2]; unsigned char bytes[8]; } u; Also last part where I want to print these float values with log statement.
It would be great if I get working swift conversion for below code snippet.
id<MTLDevice> device = MTLCreateSystemDefaultDevice(); id<MTLCommandQueue> queue = [device newCommandQueue]; id<MTLCommandBuffer> commandBuffer = [queue commandBuffer]; MTKTextureLoader *textureLoader = [[MTKTextureLoader alloc] initWithDevice:device]; id<MTLTexture> sourceTexture = [textureLoader newTextureWithCGImage:image.CGImage options:nil error:nil]; CGColorSpaceRef srcColorSpace = CGColorSpaceCreateDeviceRGB(); CGColorSpaceRef dstColorSpace = CGColorSpaceCreateDeviceGray(); CGColorConversionInfoRef conversionInfo = CGColorConversionInfoCreate(srcColorSpace, dstColorSpace); MPSImageConversion *conversion = [[MPSImageConversion alloc] initWithDevice:device srcAlpha:MPSAlphaTypeAlphaIsOne destAlpha:MPSAlphaTypeAlphaIsOne backgroundColor:nil conversionInfo:conversionInfo]; MTLTextureDescriptor *grayTextureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatR16Unorm width:sourceTexture.width height:sourceTexture.height mipmapped:false]; grayTextureDescriptor.usage = MTLTextureUsageShaderWrite | MTLTextureUsageShaderRead; id<MTLTexture> grayTexture = [device newTextureWithDescriptor:grayTextureDescriptor]; [conversion encodeToCommandBuffer:commandBuffer sourceTexture:sourceTexture destinationTexture:grayTexture]; MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:grayTexture.pixelFormat width:sourceTexture.width height:sourceTexture.height mipmapped:false]; textureDescriptor.usage = MTLTextureUsageShaderWrite | MTLTextureUsageShaderRead; id<MTLTexture> texture = [device newTextureWithDescriptor:textureDescriptor]; MPSImageLaplacian *imageKernel = [[MPSImageLaplacian alloc] initWithDevice:device]; [imageKernel encodeToCommandBuffer:commandBuffer sourceTexture:grayTexture destinationTexture:texture]; MPSImageStatisticsMeanAndVariance *meanAndVariance = [[MPSImageStatisticsMeanAndVariance alloc] initWithDevice:device]; MTLTextureDescriptor *varianceTextureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatR32Float width:2 height:1 mipmapped:NO]; varianceTextureDescriptor.usage = MTLTextureUsageShaderWrite; id<MTLTexture> varianceTexture = [device newTextureWithDescriptor:varianceTextureDescriptor]; [meanAndVariance encodeToCommandBuffer:commandBuffer sourceTexture:texture destinationTexture:varianceTexture]; [commandBuffer commit]; [commandBuffer waitUntilCompleted]; union { float f[2]; unsigned char bytes[8]; } u; MTLRegion region = MTLRegionMake2D(0, 0, 2, 1); [varianceTexture getBytes:u.bytes bytesPerRow:2 * 4 fromRegion:region mipmapLevel: 0]; NSLog(@"mean: %f", u.f[0] * 255); NSLog(@"variance: %f", u.f[1] * 255 * 255); It will be great if I get swift representation for this?
https://stackoverflow.com/questions/66558088/objective-c-to-swift-conversion-for-unions March 10, 2021 at 11:51AM
没有评论:
发表评论