How can translate C++ to Swift

Hello !


How can translate this row in C++ to Swift:

        _buffer = [device newBufferWithLength:_size options:0];
        mpTransform = static_cast<simd::float4x4 *>([_buffer contents]);


where:

_size = sizeof(float4x4)

_buffer = self.devise.newBufferWithLength(size, options: .CPUCacheModeDefaultCache)


how set buffer.contents() to array flat4 ?

You'd better show the type of each property.

        _buffer = device.newBufferWithLength(_size, options: [])
        mpTransform = UnsafeMutablePointer(_buffer!.contents())

Assuming `mpTransform` is a pointer, this code would work with very few modifications.

I have set but

Return error:

Cannot invoke initializer for type 'UnsafeMutablePointer<_>' with an argument list of type '(UnsafeMutablePointer<Void>)'

mpTransform is: float4x4

Adding `mp` prefix to non-pointer member does not seem to be a good habit, as I noted, I assumed `mpTransform` was a pointer.

I recommend you to rename it, if you really make it `float4x4`, which may not be the same as the original C++ code.

How `mpTransform` is declared in the original C++ code?

The source code is the tutorial "Metal N-Body" in Documentation API Reference.

for my tutorial I prefer rewrite it in Swift.

in particular is "MetalNBodyTransform" files.


    simd::float4x4* mpTransform;


- (BOOL) _acquire:(nullable id<MTLDevice>)device
{
    if(device)
    {
        /
        _buffer = [device newBufferWithLength:_size options:0];
     
        if(!_buffer)
        {
            NSLog(@">> ERROR: Failed to instantiate a buffer for transformation matrix!");
         
            return NO;
        } /
     
        /
        mpTransform = static_cast<simd::float4x4 *>([_buffer contents]);
     
        if(!mpTransform)
        {
            NSLog(@">> ERROR: Failed to acquire a host pointer to the transformation matrix buffer!");
         
            return NO;
        } /
     
        return YES;
    } /
    else
    {
        NSLog(@">> ERROR: Metal device is nil!");
    } /

    return NO;
} // _acquire



For moment i have set :

mpTransform = float4x4(0.0)


_buffer pass only the lenght not have value in func _acquire.


func resise and fun setUpdate set the value in mpTransform.

I would simply declare mpTransform as a pointer, e.g.


    var mpTransform : UnsafeMutablePointer<float4x4>
    mpTransform = UnsafeMutablePointer<float4x4>(buffer!.contents)


and then use mpTransform[0] to access the value. Now, if you just want to copy the first value into your mpTransform struct, then


mpTransform = UnsafeMutablePointer<float4x4>(buffer!.contents)[0]


should do the trick

As you wrote:

    simd::float4x4* mpTransform;

In the original MetalNBodyTransform.mm file, `mpTransform` is declared as a pointer. Why do you want to make it non-pointer in Swift?


func resise and fun setUpdate set the value in mpTransform.

To set the value in mpTransform in setUpdate, mpTransform needs to be a pointer.

    private var mpTransform: UnsafeMutablePointer<float4x4> = nil

    func setUpdate(update: Bool) {
        if update {
            transform = m_Projection * m_View
            mpTransform.memory = transform
           
            _update = update
        }
    }
   
    private func _acquire(device: MTLDevice?) -> Bool {
        guard let device = device else {
            NSLog(">> ERROR: Metal device is nil!")
           
            return false
        }
        //
        let buffer = device.newBufferWithLength(size, options: [])
        self.buffer = buffer
       
        //
        mpTransform = UnsafeMutablePointer(buffer.contents())
       
        guard mpTransform != nil else {
            NSLog(">> ERROR: Failed to acquire a host pointer to the transformation matrix buffer!")
           
            return false
        }
       
        return true
       
    }
How can translate C&#43;&#43; to Swift
 
 
Q