Metal Shading Language - (Console) Output?
<p>Can I debug my metal-shading-language code using console output (like <code>print</code> in Swift)? </p>

<p>If yes, how?</p>

<p>If no, are there any other ways to output variables from my .metal file? (Maybe by passing data from the .metal file to my .swift file using the commandEncoder-buffer?)</p>

<p>I have unsuccessfully tried to pass a reference to an Int variable (which is in my .swift file) via the commandEncoder to my .metal file. In the .metal file, I assign a value to the int variable, but if I print the Int in my swift file, the assigned value is not there.</p>

<p>.swift file:</p>

<pre><code>...
var myMetalOutput: Int = 0
...
let printBuffer = device.newBufferWithBytes(&amp;myMetalOutput, length: sizeof(Int), options: MTLResourceOptions.CPUCacheModeDefaultCache)
commandEncoder.setBuffer(printBuffer, offset: 0, atIndex: 8)
...
commandBuffer.commit()
drawable.present()
print("myMetalOutput: \(myMetalOutput)")
...
</code></pre>

<p>.metal file:</p>

<pre><code>...
kernel void shader(..., device int &amp;printBuffer [[8]], ...) {
...
printBuffer = 123;
...
}
</code></pre>

<p>The console output is always <code>myMetalOutput: 0</code></p>