<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Hi Jacob - </div><div class=""><br class=""></div>I think you’re right. “capacity” should be the count of type T elements in my buffer. So in my case that line should read<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> shadowPtr = constantBufferForFrame.contents().bindMemory(to: ShadowPass.</span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">, capacity: shadowPassData.count)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">The withMemoryRebound calls need similar adjustments. The pointer to MainPass is actually a single structure to it should be safe to do this</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> mainPtr : </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UnsafeMutablePointer</span><span style="font-variant-ligatures: no-common-ligatures" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">MainPass</span><span style="font-variant-ligatures: no-common-ligatures" class="">> = shadowPtr.advanced(by: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span><span style="font-variant-ligatures: no-common-ligatures" class="">).withMemoryRebound(to: MainPass.</span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">, capacity: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span><span style="font-variant-ligatures: no-common-ligatures" class="">) {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> $0.pointee = mainPassFrameData</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Whereas the unsafe pointer to <ObjectData> is actually a buffer of renderable objects, so this should work:</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> ptr : </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UnsafeMutablePointer</span><span style="font-variant-ligatures: no-common-ligatures" class=""><</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">ObjectData</span><span style="font-variant-ligatures: no-common-ligatures" class="">> = mainPtr.advanced(by: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8" class="">1</span><span style="font-variant-ligatures: no-common-ligatures" class="">).withMemoryRebound(to: ObjectData.</span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">, capacity: objectsToRender) {_ in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</span></div><div style="margin: 0px; line-height: normal;" class=""><br class=""></div></span></div></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">There are surely ways to refactor and simplify this, but I’m trying to retain as much of the original sample code approach as possible.</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">However, the compiler still segs badly. </span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Xcode also borks an internal error often. </span>Only cleaning or restarting the project can clear up that state.</div><div class="">Compilers (or Playgrounds for that matter) should never crash, and I’m not sure where to file this bug : Swift or Apple radar against Xcode or both? I now Xcode 8 is beta but…it’s been doing this for quite a while now...</div><div class=""><br class=""></div><div class="">In both our “close to the metal” (no pun intended) cases, it seems like a lot of churning for very little gain. Don’t you think? The easier, but “unsafe” casting afforded previously did the trick with the normal caveats.</div><div class="">Don’t get me wrong, I love Swift and “get" all the neat type safety features. Guess we can’t have our cake and eat it too, especially when interfacing with “unsafe” C APIs.</div><div class=""><br class=""></div><div class="">Anyway, back to rtfm … maybe some of the Swift Gods can chime in? ;-) </div><div class=""><br class=""></div><div class="">I must be doing something stupid...Patrice</div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On Sep 3, 2016, at 2:32 AM, Jacob Bandes-Storch <<a href="mailto:jtbandes@gmail.com" class="">jtbandes@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hi Patrice,<div class="">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 class=""><br class=""></div><div class=""><span style="font-family:menlo;font-size:11px" class="">constantBufferForFrame.<wbr class="">contents().bindMemory(to: ShadowPass.</span><span style="font-family:menlo;font-size:11px;color:rgb(186,45,162)" class="">self</span><span style="font-family:menlo;font-size:11px" class="">, capacity: MemoryLayout<</span><span style="font-family:menlo;font-size:11px;color:rgb(112,61,170)" class="">ShadowPass</span><span style="font-family:menlo;font-size:11px" class="">>.size)</span><br class=""></div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">More info at <a href="https://developer.apple.com/reference/swift/unsaferawpointer/2428875-bindmemory" class="">https://developer.apple.com/reference/swift/unsaferawpointer/2428875-bindmemory</a><br class=""></div><div class=""><br class=""></div><div class="">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" class="">https://github.com/jtbandes/Metalbrot.playground</a></div><div class="gmail_extra"><br clear="all" class=""><div class=""><div class="gmail_signature"><div dir="ltr" class=""><div class="">Jacob<br class=""></div></div></div></div>
<br class=""><div class="gmail_quote">On Fri, Sep 2, 2016 at 11:00 AM, Patrice Kouame via swift-users <span dir="ltr" class=""><<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>></span> wrote:<br class=""><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" class=""><span class="">Hi all - </span><div class=""><span class=""><br class=""></span></div><div class=""><span class="">I’m converting Apple’s Swift Sample "</span><span class="">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 class=""><span class="">Changes are necessary in MetalView.swift (Apple hasn’t updated their sample code for the latest beta yet…) </span></div><div class=""><span class="">The Swift Compiler crashes (Segmentation Fault: </span>11) on the attempt:<div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)" class=""><span style="" class=""> </span><span class="">// 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)" class=""><span style="" class=""> </span><span class="">// 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)" class=""><span style="" class=""> </span><span class="">// 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" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)" class=""><span class="">// let shadowPtr = UnsafeMutablePointer<<wbr class="">ShadowPass>(<wbr class="">constantBufferForFrame.<wbr class="">contents())</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> </span><span style="color:rgb(186,45,162)" class="">let</span><span class=""> shadowPtr = constantBufferForFrame.<wbr class="">contents().bindMemory(to: ShadowPass.</span><span style="color:rgb(186,45,162)" class="">self</span><span class="">, capacity: MemoryLayout<</span><span style="color:rgb(112,61,170)" class="">ShadowPass</span><span class="">>.size)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> shadowPtr.pointee = shadowPassData[</span><span style="color:rgb(39,42,216)" class="">0</span><span class="">]</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)" class=""><span style="" class=""> </span><span class="">//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" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)" class=""><span class="">// let mainPtr = UnsafeMutablePointer<MainPass><wbr class="">(shadowPtr.advanced(by: 1))</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)" class=""><span class="">// mainPtr.pointee = mainPassFrameData</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> </span><span style="color:rgb(186,45,162)" class="">let</span><span class=""> mainPtr : </span><span style="color:rgb(112,61,170)" class="">UnsafeMutablePointer</span><span class=""><</span><span style="color:rgb(112,61,170)" class="">MainPass</span><span class="">><wbr class=""> = shadowPtr.advanced(by: </span><span style="color:rgb(39,42,216)" class="">1</span><span class="">).withMemoryRebound(to: MainPass.</span><span style="color:rgb(186,45,162)" class="">self</span><span class="">, capacity: MemoryLayout<</span><span style="color:rgb(112,61,170)" class="">MainPass</span><span class="">>.size) {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> $0.pointee = mainPassFrameData</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo; min-height: 13px;" class=""><span class=""> </span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)" class=""><span style="" class=""> </span><span class="">//Advance and cast to ObjectData</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo; min-height: 13px;" class=""><span class=""> </span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)" class=""><span class="">// var ptr = UnsafeMutablePointer<<wbr class="">ObjectData>(mainPtr.advanced(<wbr class="">by: 1))</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> </span><span style="color:rgb(186,45,162)" class="">var</span><span class=""> ptr : </span><span style="color:rgb(112,61,170)" class="">UnsafeMutablePointer</span><span class=""><</span><span style="color:rgb(112,61,170)" class="">ObjectDat<wbr class="">a</span><span class="">> = mainPtr.advanced(by: </span><span style="color:rgb(39,42,216)" class="">1</span><span class="">).withMemoryRebound(to: ObjectData.</span><span style="color:rgb(186,45,162)" class="">self</span><span class="">, capacity: MemoryLayout<</span><span style="color:rgb(112,61,170)" class="">ObjectData</span><span class="">>.size) {</span><span style="color:rgb(186,45,162)" class="">_</span><span class=""> </span><span style="color:rgb(186,45,162)" class="">in</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> </span><span style="color:rgb(186,45,162)" class="">let</span><span class=""> shadowOffset = </span><span style="color:rgb(39,42,216)" class="">0</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> </span><span style="color:rgb(186,45,162)" class="">let</span><span class=""> mainPassOffset = MemoryLayout<</span><span style="color:rgb(112,61,170)" class="">ShadowPass</span><span class="">>.size + shadowOffset</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> </span><span style="color:rgb(186,45,162)" class="">let</span><span class=""> objectDataOffset = MemoryLayout<</span><span style="color:rgb(112,61,170)" class="">MainPass</span><span class="">>.size + mainPassOffset</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px" class=""><span class=""></span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)" class=""><span style="" class=""> </span><span class="">// Update position of all the objects</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> </span><span style="color:rgb(186,45,162)" class="">if</span><span class=""> multithreadedUpdate {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> DispatchQueue.<wbr class="">concurrentPerform(iterations: objectsToRender) { i </span><span style="color:rgb(186,45,162)" class="">in</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> </span><span style="color:rgb(186,45,162)" class="">let</span><span class=""> thisPtr = ptr.advanced(by: i)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> </span><span style="color:rgb(186,45,162)" class="">_</span><span class=""> = </span><span style="color:rgb(186,45,162)" class="">self</span><span class="">.renderables[i].<wbr class="">UpdateData(ptr, deltaTime: </span><span style="color:rgb(39,42,216)" class="">1.0</span><span class="">/</span><span style="color:rgb(39,42,216)" class="">60.0</span><span class="">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> </span><span style="color:rgb(186,45,162)" class="">else</span><span class=""> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> </span><span style="color:rgb(186,45,162)" class="">for</span><span class=""> index </span><span style="color:rgb(186,45,162)" class="">in</span><span class=""> </span><span style="color:rgb(39,42,216)" class="">0</span><span class="">..<objectsToRender {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> ptr = renderables[index].UpdateData(<wbr class="">ptr, deltaTime: </span><span style="color:rgb(39,42,216)" class="">1.0</span><span class="">/</span><span style="color:rgb(39,42,216)" class="">60.0</span><span class="">)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo; min-height: 13px;" class=""><span class=""> </span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> ptr = ptr.advanced(by: objectsToRender)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo; min-height: 13px;" class=""><span class=""> </span><br class=""></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo" class=""><span class=""> </span><span style="color:rgb(186,45,162)" class="">_</span><span class=""> = groundPlane!.UpdateData(ptr, deltaTime: </span><span style="color:rgb(39,42,216)" class="">1.0</span><span class="">/</span><span style="color:rgb(39,42,216)" class="">60.0</span><span class="">)</span></div><div class=""><span class=""><br class=""></span></div><div class="">Any help is appreciated. I have the latest Xcode log handy if necessary. Here’s a clip of the stack trace.</div><div class=""><br class=""></div><div class=""><div class="">0 swift 0x000000010714a99d PrintStackTraceSignalHandler(<wbr class="">void*) + 45</div><div class="">1 swift 0x000000010714a3e6 SignalHandler(int) + 470</div><div class="">2 libsystem_platform.dylib 0x00007fff91461bba _sigtramp + 26</div><div class="">3 libsystem_platform.dylib 000000000000000000 _sigtramp + 1857676384</div><div class="">4 swift 0x00000001047207b3 (anonymous namespace)::SILGenApply::<wbr class="">visitExpr(swift::Expr*) + 51</div><div class="">5 swift 0x0000000104723ace (anonymous namespace)::SILGenApply::<wbr class="">visitApplyExpr(swift::<wbr class="">ApplyExpr*) + 5182</div><div class="">6 swift 0x0000000104711cc1 prepareApplyExpr(swift::<wbr class="">Lowering::SILGenFunction&, swift::Expr*) + 273</div><div class="">7 swift 0x00000001047624e7 swift::ASTVisitor<(anonymous namespace)::RValueEmitter, swift::Lowering::RValue, void, void, void, void, void, swift::Lowering::SGFContext>::<wbr class="">visit(swift::Expr*, swift::Lowering::SGFContext) + 103</div><div class="">8 swift 0x0000000104762313 swift::Lowering::<wbr class="">SILGenFunction::emitExprInto(<wbr class="">swift::Expr*, swift::Lowering::<wbr class="">Initialization*) + 195</div><div class="">9 swift 0x000000010474fbc3 swift::Lowering::<wbr class="">SILGenFunction::<wbr class="">emitPatternBinding(swift::<wbr class="">PatternBindingDecl*, unsigned int) + 195</div><div class="">10 swift 0x00000001047077bd swift::ASTVisitor<swift::<wbr class="">Lowering::SILGenFunction, void, void, void, void, void, void>::visit(swift::Decl*) + 125</div><div class="">11 swift 0x00000001047c0019 swift::ASTVisitor<(anonymous namespace)::StmtEmitter, void, void, void, void, void, void>::visit(swift::Stmt*) + 4169</div><div class="">12 swift 0x00000001047809ba swift::Lowering::<wbr class="">SILGenFunction::emitFunction(<wbr class="">swift::FuncDecl*) + 314</div><div class="">13 swift 0x00000001046fd775 swift::Lowering::SILGenModule:<wbr class="">:emitFunction(swift::FuncDecl*<wbr class="">)::$_1::operator()(swift::<wbr class="">SILFunction*) const + 1877</div><div class="">14 swift 0x00000001046fc322 swift::Lowering::SILGenModule:<wbr class="">:emitFunction(swift::FuncDecl*<wbr class="">) + 626</div><div class="">15 swift 0x00000001047c7007 (anonymous namespace)::SILGenType::<wbr class="">emitType() + 1271</div><div class="">16 swift 0x00000001047c6a9e swift::Lowering::SILGenModule:<wbr class="">:visitNominalTypeDecl(swift::<wbr class="">NominalTypeDecl*) + 30</div><div class="">17 swift 0x0000000104709093 swift::Lowering::SILGenModule:<wbr class="">:emitSourceFile(swift::<wbr class="">SourceFile*, unsigned int) + 1795</div><div class="">18 swift 0x000000010470ad4d swift::SILModule::<wbr class="">constructSIL(swift::<wbr class="">ModuleDecl*, swift::SILOptions&, swift::FileUnit*, llvm::Optional<unsigned int>, bool, bool) + 1629</div><div class="">19 swift 0x00000001045621bf performCompile(swift::<wbr class="">CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*) + 19487</div><div class="">20 swift 0x000000010455b2c5 swift::performFrontend(llvm::<wbr class="">ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 17029</div><div class="">21 swift 0x000000010451888d main + 8685</div><div class="">22 libdyld.dylib 0x00007fff91255255 start + 1</div></div><span class="gmail-HOEnZb"><font color="#888888" class=""><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Patrice</div><div class=""><span class=""><br class=""></span></div></font></span></div></div></div></div><br class="">______________________________<wbr class="">_________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-users</a><br class="">
<br class=""></blockquote></div><br class=""></div></div>
</div></blockquote></div><br class=""></div></body></html>