MPSImageIntegral returning all zeros

孤人 提交于 2019-12-11 15:22:02

问题


I am trying to use MPSImageIntegral to calculate the sum of some elements in an MTLTexture. This is what I'm doing:

std::vector<float> integralSumData;
for(int i = 0; i < 10; i++)
    integralSumData.push_back((float)i);

MTLTextureDescriptor *textureDescriptor = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatR32Float
                                                                                             width:(integralSumData.size()) height:1 mipmapped:NO];
textureDescriptor.usage = MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite;
id<MTLTexture> texture = [_device newTextureWithDescriptor:textureDescriptor];

// Calculate the number of bytes per row in the image.
NSUInteger bytesPerRow = integralSumData.size() * sizeof(float);

MTLRegion region =
{
    { 0, 0, 0 },                   // MTLOrigin
    {integralSumData.size(), 1, 1} // MTLSize
};

// Copy the bytes from the data object into the texture
[texture replaceRegion:region
           mipmapLevel:0
             withBytes:integralSumData.data()
           bytesPerRow:bytesPerRow];


MTLTextureDescriptor *textureDescriptor2 = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatR32Float
                                                                                              width:(integralSumData.size()) height:1 mipmapped:NO];
textureDescriptor2.usage = MTLTextureUsageShaderRead | MTLTextureUsageShaderWrite;
id<MTLTexture> outtexture = [_device newTextureWithDescriptor:textureDescriptor2];


// Create a MPS filter.
MPSImageIntegral *integral = [[MPSImageIntegral alloc] initWithDevice: _device];
MPSOffset offset = { 0,0,0};
[integral setOffset:offset];
[integral setEdgeMode:MPSImageEdgeModeZero];
[integral encodeToCommandBuffer:commandBuffer sourceTexture:texture destinationTexture:outtexture];

[commandBuffer commit];
[commandBuffer waitUntilCompleted];

But, when I check my outtexture values, its all zeroes. Am I doing something wrong? Is this a correct way in which I shall use MPSImageIntegral?

I'm using the following code to read values written into the outTexture:

float outData[100];
[outtexture getBytes:outData bytesPerRow:bytesPerRow fromRegion:region mipmapLevel:0];
for(int i = 0; i < 100; i++)
    std::cout << outData[i] << "\n";

Thanks


回答1:


As pointed out by @Matthijis: All I had to do was use an MTLBlitEncoder to make sure I synchronise my MTLTexture before reading it into CPU, and it worked like charm!



来源:https://stackoverflow.com/questions/56429189/mpsimageintegral-returning-all-zeros

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!