代码之家  ›  专栏  ›  技术社区  ›  zzyzy

产生奇怪种族条件的金属渲染

  •  0
  • zzyzy  · 技术社区  · 6 年前

    我打开xcode 9.4.1中的线程消毒剂,现在我的旋转缓冲区(大小高达2)上出现了一个奇怪的竞争条件警告。我本以为在这里正确使用信号量可以消除这个问题。我应该提到,这个缓冲区来自另一个渲染编码的输出,用单独的“first”进行编码。 MTKView . 我有一个信号量,我已经初始化通过 dispatch_semaphore_create(1) 在第二个下游视图中。

    在我的第一 MTKVIEW 提交后,我按照如下方式获取渲染的纹理,然后使用下游视图的信号量将其排入下游缓冲区:

    [commandBuffer presentDrawable:self.currentDrawable];
    [commandBuffer commit];
    //[commandBuffer waitUntilCompleted]; // (doesn't matter if this is in or out)
    ...
    id obj = [self.renderedQueue firstObject];
    for (MonitorMTKView *v in self.downstreamOutputs) {
       dispatch_semaphore_wait(v.bufferSemaphore,DISPATCH_TIME_FOREVER);
       [v.textureQueue addObject:inputTexture];
       if ([v.textureQueue count]>2)
          [v.textureQueue removeObjectAtIndex:0];
       dispatch_semaphore_signal(v.bufferSemaphore);
    }
    

    现在到下游的渲染循环 MTKVIEW . 我提交了命令缓冲区,并有这个完成处理程序:

    __block __weak __typeof__(self) weakSelf = self;
    [commandBuffer addCompletedHandler:^(id<MTLCommandBuffer> buffer) {
       dispatch_semaphore_wait(weakSelf.bufferSemaphore, DISPATCH_TIME_FOREVER);
       id obj = [weakSelf.textureQueue firstObject];
    
    **// thread sanitizer issue on this next line of code "Race on a library object detected" **
       [weakSelf.textureQueue removeObject:obj];
    
       dispatch_semaphore_signal(weakSelf.bufferSemaphore);
    }
    


    为什么信号量受到保护的周围的竞争条件? 我做错什么了吗? . 缓冲区本身不是基于GPU的,因此不存在干扰。

    一种思路是三重缓冲,但这并不能缓解问题,所以我不认为它的GPU干扰。

    1 回复  |  直到 6 年前
        1
  •  0
  •   MoDJ    6 年前

    Apple金属文档指出,您应该使用3个缓冲区,以避免填充缓冲区和传递给GPU之间的竞争条件。