twohyjr / Metal-Game-Engine-Tutorial

The resources and source code for my Youtube series on creating a game engine using Apple's Metal Api

Home Page:https://www.youtube.com/playlist?list=PLEXt1-oJUa4BVgjZt9tK2MhV_DW7PVDsg

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Minor Suggestion for MetalTypes

noopz opened this issue · comments

While going over your code to figure out a better design for my ToyEngine for learning Swift/Metal. I was looking over your sizeable protocol (a solid idea), and noticed that I had a usecase where I wanted to apply it to Int as well. It took a little wrangling, but sorted out how it could be accomplished and extended for any Scalar.

I didn't make this a PR as you had mentioned you weren't too keen on them in one of your videos, but wanted to share this in case you found a need for different Scalar in the future.

What exists today:

public typealias float2 = SIMD2<Float>
public typealias float3 = SIMD3<Float>
public typealias float4 = SIMD4<Float>
...
extension float2: sizeable { }
extension float3: sizeable { }
extension float4: sizeable { }

With some generic magic you could cover all types beyond just Float:

private typealias simd2<T>  = SIMD2<T> where T: SIMDScalar
private typealias simd3<T>  = SIMD3<T> where T: SIMDScalar
private typealias simd4<T>  = SIMD4<T> where T: SIMDScalar
...
extension simd2: sizeable { }
extension simd3: sizeable { }
extension simd4: sizeable { }

From there I'm just using the other aliases that are already provided already:

print(simd_float3.size)
print(simd_int2.stride)

Thanks for making some great videos!