Posts

Post not yet marked as solved
3 Replies
809 Views
While translating the Accelerating Ray Tracing Using Metal sample project into Swift, I have run into an issue within the createAccelerationStructures function. I am trying to fill in the first three rows of the instance transformation matrix. In the Objective C example, it looks like: for (int column = 0; column < 4; column++) &#9;for (int row = 0; row < 3; row++) &#9;&#9;instanceDescriptors[instanceIndex].transformationMatrix.columns[column][row] = instance.transform.columns[column][row]; The matrix targeted is a MTLPackedFloat4x3. It has defied me pulling out the needed elements from my instance transform and placing them in the descriptor. I have tried the obvious: for column in 0..<( 4 ) &#9;{ &#9;for row in 0..<( 3 ) &#9;&#9;{ &#9;&#9;anInstanceDescriptorPointer.pointee.transformationMatrix.columns[column][row] = aNodeInstance.transform.columns[column][row] &#9;&#9;} &#9;} I have tried more obscure ideas related to simd matrices and Swift. The compiler errors generally warn me that MTLPackedFloat4x3 has no subscripts. Question - Is there an accepted way to assign three rows from the instance transform to the descriptor transformationMatrix in Swift? Thank you for your earlier help with all of this. I should mention that the new intersector feature in Metal has worked really well for me when feeding it a primitiveaccelerationstructure. Very elegant! But using instances seems to offer more flexibility going forward. Thank you very much!
Posted
by John Lobe.
Last updated
.
Post not yet marked as solved
1 Replies
723 Views
I have watched the Enhance Your App with Metal Ray Tracing video ( wwdc21-10149 ) a few times and am very interested in many of the upcoming features described. It seems as though there will be some tweaks ( ie. Instance IDs ) and some major additions ( ie. Keyframe Animation ). I am wondering if there is any sense of the timing of these features becoming available ( beta or otherwise ) and also, if some of the sample code might be provided in Swift. Thanks so much for the work that went into the video and explanations.
Posted
by John Lobe.
Last updated
.
Post not yet marked as solved
2 Replies
1.4k Views
I am working on implementing Image Based Lighting in a ray tracing project. In order to get the sampling to work predictably, I will need to load and process an HDR image into a MTLTexture. While using a MTKTextureLoader to load .jpg and .png files has been super-smooth, loading a .hdr file in Swift seems to have a few barriers. I have tried to follow the Processing HDR Images with Metal sample code [ Objective C ], but it relies on some finessing of pointers that I can't duplicate in Swift. Is there currently a best way to move an HDR image into a Metal texture using Swift? Thanks so much!
Posted
by John Lobe.
Last updated
.
Post not yet marked as solved
2 Replies
502 Views
I have enjoyed looking through the 'Discover Ray Tracing with Metal' video and Sample Code. While trying to recreate the Project in Swift, I have run across an issue with setting up an MTLPrimativeAccelerationStructureDescriptor. In Renderer.mm ( Objective C ), within the createAccelerationStructures function, a loop is created: // Create a primitive acceleration structure for each piece of geometry in the scene &#9;for (NSUInteger i = 0; i < _scene.geometries.count; i++) { &#9;Geometry *mesh = _scene.geometries[i]; &#9;MTLAccelerationStructureGeometryDescriptor *geometryDescriptor = [mesh geometryDescriptor]; &#9;// Assign each piece of geometry a consecutive slot in the intersection function table         geometryDescriptor.intersectionFunctionTableOffset = i; &#9;// Create a primitive acceleration structure descriptor to contain the single piece of acceleration structure geometry &#9;MTLPrimitiveAccelerationStructureDescriptor *accelDescriptor = [MTLPrimitiveAccelerationStructureDescriptor descriptor]; &#9;accelDescriptor.geometryDescriptors = @[ geometryDescriptor ]; &#9;// Build the acceleration structure. &#9;id &lt;MTLAccelerationStructure&gt; accelerationStructure = [self newAccelerationStructureWithDescriptor:accelDescriptor]; &#9;// Add the acceleration structure to the array of primitive acceleration structures. &#9;[_primitiveAccelerationStructures addObject:accelerationStructure]; &#9;} In Swift, I am trying: for i in 0..<( scene.nodes.count ) &#9;{ &#9;let aMesh = scene.nodes[i] &#9;let geometryDescriptor: MTLAccelerationStructureGeometryDescriptor = aMesh.getGeometryDescriptor() &#9;// Assign each piece of geometry a consecutive slot in the intersection function table             geometryDescriptor.intersectionFunctionTableOffset = i &#9;// Create a primitive acceleration structure descriptor to contain the single piece of acceleration structure geometry &#9;var accelDescriptor: MTLPrimitiveAccelerationStructureDescriptor = MTLPrimitiveAccelerationStructureDescriptor() &#9;accelDescriptor.geometryDescriptors.append( geometryDescriptor ) &#9;// Build the acceleration structure. &#9;// To follow... &#9;// Add the acceleration structure to the array of primitive acceleration structures. &#9; // To follow... &#9;} The compiler flags appending the geometryDescriptor with "Cannot use mutating member on immutable value: 'accelDescriptor is immutable'". I have tried a couple of compiler suggested ideas to unwrap the accelDescriptor.geometryDescriptors, but end up with a nil value in the geometryDescriptors array. It may be naive to simply append the geometryDescriptor, but I would appreciate any suggestions to fill in the accelDescriptor. In the larger picture, it would be great to have a Swift Project sample to use as a starting point. The concepts spelled out in the video and Objective C project are very compelling! Thanks so much - John Lobe
Posted
by John Lobe.
Last updated
.
Post not yet marked as solved
2 Replies
2k Views
I am getting the following error when running the introductory Accelerated Ray Tracing project ( rewritten in Swift by Marius Horga ):validateComputeFunctionArguments:831: failed assertion `Compute Function(shadowKernel): Shader uses texture(dstTex[0]) as read-write, but hardware does not support read-write texture of this pixel format.'The MTLTextureDescriptor is set up as:let renderTargetDescriptor = MTLTextureDescriptor() renderTargetDescriptor.pixelFormat = .rgba32Float renderTargetDescriptor.textureType = .type2D renderTargetDescriptor.width = Int(size.width) renderTargetDescriptor.height = Int(size.height) renderTargetDescriptor.storageMode = .private renderTargetDescriptor.usage = [.shaderRead, .shaderWrite] renderTarget = device.makeTexture(descriptor: renderTargetDescriptor)I am running the project with XCode 11 - Beta 3 on Catalina Beta 3. Apple's Objective C project works fine and the Swift rewrite works fine on Mojave / XCode 10.Also, the Animated Ray Tracing example ( Objective C ) from this year's WWDC works ( obviously ).This may be a bug that will be sorted out as the fall approaches, but if I am missing something, please let me know. There are no obvious work arounds for making the textures usable.Thanks very much,John Lobe
Posted
by John Lobe.
Last updated
.