<div dir="ltr">Hi Patrice,<div>I don't have a solution for you, but I just wanted to point out what I think may be an error with your use of the new UnsafeRawPointer APIs:</div><div><br></div><div><span style="font-family:menlo;font-size:11px">constantBufferForFrame.<wbr>contents().bindMemory(to: ShadowPass.</span><span style="font-family:menlo;font-size:11px;color:rgb(186,45,162)">self</span><span style="font-family:menlo;font-size:11px">, capacity: MemoryLayout<</span><span style="font-family:menlo;font-size:11px;color:rgb(112,61,170)">ShadowPass</span><span style="font-family:menlo;font-size:11px">>.size)</span><br></div><div><br></div><div>I believe the `capacity` should actually be the number of ShadowPass elements in the buffer, not the size of each element. Using `bindMemory(to: ShadowPass.self` already implies that MemoryLayout<ShadowPass>.size is the size of each element.</div><div><br></div><div>More info at <a href="https://developer.apple.com/reference/swift/unsaferawpointer/2428875-bindmemory">https://developer.apple.com/reference/swift/unsaferawpointer/2428875-bindmemory</a><br></div><div><br></div><div>I just updated a small Metal project of mine to Swift 3. I ran into some compiler (playground) crashes, but it does seem to work most of the time. Although I only have 1 buffer :-) <a href="https://github.com/jtbandes/Metalbrot.playground">https://github.com/jtbandes/Metalbrot.playground</a></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div>Jacob<br></div></div></div></div>
<br><div class="gmail_quote">On Fri, Sep 2, 2016 at 11:00 AM, Patrice Kouame via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><span>Hi all - </span><div><span><br></span></div><div><span>I’m converting Apple’s Swift Sample "</span><span>Adopting Metal II: Designing and Implementing a Real-World Metal Renderer” in Xcode 8 beta6 to the latest UnsafeMutablePointer API for untyped memory access. </span><div><span>Changes are necessary in MetalView.swift (Apple hasn’t updated their sample code for the latest beta yet…) </span></div><div><span>The Swift Compiler crashes (Segmentation Fault: </span>11) on the attempt:<div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)"> </span><span>// Grab a pointer to the constant buffer's data store</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)"> </span><span>// Since we are using Swift, it is easier to cast the pointer to the ShadowPass type to fill the constant buffer</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)"> </span><span>// We need to make a copy of these so the block captures the correct data</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span>// let shadowPtr = UnsafeMutablePointer<<wbr>ShadowPass>(<wbr>constantBufferForFrame.<wbr>contents())</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> </span><span style="color:rgb(186,45,162)">let</span><span> shadowPtr = constantBufferForFrame.<wbr>contents().bindMemory(to: ShadowPass.</span><span style="color:rgb(186,45,162)">self</span><span>, capacity: MemoryLayout<</span><span style="color:rgb(112,61,170)">ShadowPass</span><span>>.size)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> shadowPtr.pointee = shadowPassData[</span><span style="color:rgb(39,42,216)">0</span><span>]</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)"> </span><span>//More Swift specific stuff - advance pointer and cast to MainPass</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span>// let mainPtr = UnsafeMutablePointer<MainPass><wbr>(shadowPtr.advanced(by: 1))</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span>// mainPtr.pointee = mainPassFrameData</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> </span><span style="color:rgb(186,45,162)">let</span><span> mainPtr : </span><span style="color:rgb(112,61,170)">UnsafeMutablePointer</span><span><</span><span style="color:rgb(112,61,170)">MainPass</span><span>><wbr> = shadowPtr.advanced(by: </span><span style="color:rgb(39,42,216)">1</span><span>).withMemoryRebound(to: MainPass.</span><span style="color:rgb(186,45,162)">self</span><span>, capacity: MemoryLayout<</span><span style="color:rgb(112,61,170)">MainPass</span><span>>.size) {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> $0.pointee = mainPassFrameData</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> }</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span> </span><br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)"> </span><span>//Advance and cast to ObjectData</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span> </span><br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span>// var ptr = UnsafeMutablePointer<<wbr>ObjectData>(mainPtr.advanced(<wbr>by: 1))</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> </span><span style="color:rgb(186,45,162)">var</span><span> ptr : </span><span style="color:rgb(112,61,170)">UnsafeMutablePointer</span><span><</span><span style="color:rgb(112,61,170)">ObjectDat<wbr>a</span><span>> = mainPtr.advanced(by: </span><span style="color:rgb(39,42,216)">1</span><span>).withMemoryRebound(to: ObjectData.</span><span style="color:rgb(186,45,162)">self</span><span>, capacity: MemoryLayout<</span><span style="color:rgb(112,61,170)">ObjectData</span><span>>.size) {</span><span style="color:rgb(186,45,162)">_</span><span> </span><span style="color:rgb(186,45,162)">in</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> </span><span style="color:rgb(186,45,162)">let</span><span> shadowOffset = </span><span style="color:rgb(39,42,216)">0</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> </span><span style="color:rgb(186,45,162)">let</span><span> mainPassOffset = MemoryLayout<</span><span style="color:rgb(112,61,170)">ShadowPass</span><span>>.size + shadowOffset</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> </span><span style="color:rgb(186,45,162)">let</span><span> objectDataOffset = MemoryLayout<</span><span style="color:rgb(112,61,170)">MainPass</span><span>>.size + mainPassOffset</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span style="color:rgb(0,0,0)"> </span><span>// Update position of all the objects</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> </span><span style="color:rgb(186,45,162)">if</span><span> multithreadedUpdate {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> DispatchQueue.<wbr>concurrentPerform(iterations: objectsToRender) { i </span><span style="color:rgb(186,45,162)">in</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> </span><span style="color:rgb(186,45,162)">let</span><span> thisPtr = ptr.advanced(by: i)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> </span><span style="color:rgb(186,45,162)">_</span><span> = </span><span style="color:rgb(186,45,162)">self</span><span>.renderables[i].<wbr>UpdateData(ptr, deltaTime: </span><span style="color:rgb(39,42,216)">1.0</span><span>/</span><span style="color:rgb(39,42,216)">60.0</span><span>)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> </span><span style="color:rgb(186,45,162)">else</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> </span><span style="color:rgb(186,45,162)">for</span><span> index </span><span style="color:rgb(186,45,162)">in</span><span> </span><span style="color:rgb(39,42,216)">0</span><span>..<objectsToRender {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> ptr = renderables[index].UpdateData(<wbr>ptr, deltaTime: </span><span style="color:rgb(39,42,216)">1.0</span><span>/</span><span style="color:rgb(39,42,216)">60.0</span><span>)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> }</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span> </span><br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> ptr = ptr.advanced(by: objectsToRender)</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span> </span><br></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span> </span><span style="color:rgb(186,45,162)">_</span><span> = groundPlane!.UpdateData(ptr, deltaTime: </span><span style="color:rgb(39,42,216)">1.0</span><span>/</span><span style="color:rgb(39,42,216)">60.0</span><span>)</span></div><div><span><br></span></div><div>Any help is appreciated. I have the latest Xcode log handy if necessary. Here’s a clip of the stack trace.</div><div><br></div><div><div>0 swift 0x000000010714a99d PrintStackTraceSignalHandler(<wbr>void*) + 45</div><div>1 swift 0x000000010714a3e6 SignalHandler(int) + 470</div><div>2 libsystem_platform.dylib 0x00007fff91461bba _sigtramp + 26</div><div>3 libsystem_platform.dylib 000000000000000000 _sigtramp + 1857676384</div><div>4 swift 0x00000001047207b3 (anonymous namespace)::SILGenApply::<wbr>visitExpr(swift::Expr*) + 51</div><div>5 swift 0x0000000104723ace (anonymous namespace)::SILGenApply::<wbr>visitApplyExpr(swift::<wbr>ApplyExpr*) + 5182</div><div>6 swift 0x0000000104711cc1 prepareApplyExpr(swift::<wbr>Lowering::SILGenFunction&, swift::Expr*) + 273</div><div>7 swift 0x00000001047624e7 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::<wbr>visit(swift::Expr*, swift::Lowering::SGFContext) + 103</div><div>8 swift 0x0000000104762313 swift::Lowering::<wbr>SILGenFunction::emitExprInto(<wbr>swift::Expr*, swift::Lowering::<wbr>Initialization*) + 195</div><div>9 swift 0x000000010474fbc3 swift::Lowering::<wbr>SILGenFunction::<wbr>emitPatternBinding(swift::<wbr>PatternBindingDecl*, unsigned int) + 195</div><div>10 swift 0x00000001047077bd swift::ASTVisitor<swift::<wbr>Lowering::SILGenFunction, void, void, void, void, void, void>::visit(swift::Decl*) + 125</div><div>11 swift 0x00000001047c0019 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 4169</div><div>12 swift 0x00000001047809ba swift::Lowering::<wbr>SILGenFunction::emitFunction(<wbr>swift::FuncDecl*) + 314</div><div>13 swift 0x00000001046fd775 swift::Lowering::SILGenModule:<wbr>:emitFunction(swift::FuncDecl*<wbr>)::$_1::operator()(swift::<wbr>SILFunction*) const + 1877</div><div>14 swift 0x00000001046fc322 swift::Lowering::SILGenModule:<wbr>:emitFunction(swift::FuncDecl*<wbr>) + 626</div><div>15 swift 0x00000001047c7007 (anonymous namespace)::SILGenType::<wbr>emitType() + 1271</div><div>16 swift 0x00000001047c6a9e swift::Lowering::SILGenModule:<wbr>:visitNominalTypeDecl(swift::<wbr>NominalTypeDecl*) + 30</div><div>17 swift 0x0000000104709093 swift::Lowering::SILGenModule:<wbr>:emitSourceFile(swift::<wbr>SourceFile*, unsigned int) + 1795</div><div>18 swift 0x000000010470ad4d swift::SILModule::<wbr>constructSIL(swift::<wbr>ModuleDecl*, swift::SILOptions&, swift::FileUnit*, llvm::Optional<unsigned int>, bool, bool) + 1629</div><div>19 swift 0x00000001045621bf performCompile(swift::<wbr>CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*) + 19487</div><div>20 swift 0x000000010455b2c5 swift::performFrontend(llvm::<wbr>ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 17029</div><div>21 swift 0x000000010451888d main + 8685</div><div>22 libdyld.dylib 0x00007fff91255255 start + 1</div></div><span class="gmail-HOEnZb"><font color="#888888"><div><br></div><div><br></div><div>Patrice</div><div><span><br></span></div></font></span></div></div></div></div><br>______________________________<wbr>_________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div></div>