CaladanBrood
CaladanBrood

Reputation: 192

Initialising texture of MTLPixelFormatR32Float in metal

I have a buffer initialised with a single-channel floating point image, which I need to get into a floating point format texture (MTLPixelFormatR32Float). I've tried creating the texture with that format and doing the following:

float *rawData = (float*)malloc(sizeof(float) * img.cols * img.rows);

for(int i = 0; i < img.rows; i++){
  for(int j = 0; j < img.cols; j++){
     rawData[i * img.cols + j] = img.at<float>(i, j);
     }
 }

MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatR32Float
                                                                                                 width:img.cols
                                                                                                height:img.rows
                                                                                             mipmapped:NO];

[texture replaceRegion:region mipmapLevel:0 withBytes:&rawData bytesPerRow:bytesPerRow];

where rawData is my buffer with the necessary floating point data. This doesn't work, I get an EXC_BAD_ACCESS error on the [texture replaceRegion...] line. I've also tried the MTKTextureLoader, which also returns nil instead of the texture.

Help would be appreciated. I would be most grateful if anyone has a working method of how to initialise the MTLPixelFormatR32Float texture with custom floating point data for data-parallel computation purposes.

Upvotes: 1

Views: 559

Answers (1)

Muzza
Muzza

Reputation: 1256

The bytes that you pass to replaceRegion should point to your data. You are incorrectly passing a pointer to a pointer. To fix it, replace withBytes:&rawData with withBytes:rawData

Upvotes: 2

Related Questions