I think it would be helpful for graphics programmers to specify vertex
data inline without surrounding every value with Float(4.0).
Two pointers:
a. You never actually want to write `Float(4.0)`, which casts a
`FloatLiteralType aka Double` with value `4.0` to `Float`. You want `4 as
Float`. This is an issue which has come up several times before on this
list.
b. You don't need to do this for every value in an array. You only need to
annotate one; probably the first one for your readers' sanity:
let vertexData = [1 as Float, 0, 0.5, 1]
Alternatively, you can use explicit type annotations as shown by others.
Something like this:
···
On Fri, Nov 3, 2017 at 1:26 PM, nick ralabate via swift-evolution < swift-evolution@swift.org> wrote:
let vertexData = [ 1.0f, 0.0f, 0.5f, 1.0f ]
Also, it would be great if Swift had a type for half-floats to match the
iPhone GPU:
If/when 16b floats were added to the standard lib, you would just write:
let vertexData: [Float16] = [ 1, 0, 0.5, 1 ]
I should note that something like vertex coordinates is usually better
modeled with a more specific type than [Float], like SCNVector4 or
simd.float4 or your own type, which also solves this problem:
import SceneKit
let vertexData = SCNVector4(1, 0, 0.5, 1)
import simd
let vertexData = float4(1, 0, 0.5, 1)
(NB both of these frameworks are Apple-specific).
- Steve
Sent from my iPhone
If @_fixed_layout was supported this would be sensible, but most graphics
frameworks expect a plain buffer of floats and the packing order is
implicit. We can’t model vertex vectors with Swift structs because we can’t
safely pointer-cast them to an array of floats.
···
On Fri, Nov 3, 2017 at 2:05 PM, Steve Canon via swift-evolution < swift-evolution@swift.org> wrote:
On Nov 3, 2017, at 14:58, Tony Allevato via swift-evolution < > swift-evolution@swift.org> wrote:
You can write this for the first thing that you want:
let vertexData: [Float] = [1.0, 0.0, 0.5, 1.0]
I don't know enough about 16-bit floats to comment on those.
On Fri, Nov 3, 2017 at 11:26 AM nick ralabate via swift-evolution < > swift-evolution@swift.org> wrote:
I think it would be helpful for graphics programmers to specify vertex
data inline without surrounding every value with Float(4.0).
Something like this:
let vertexData = [ 1.0f, 0.0f, 0.5f, 1.0f ]
Also, it would be great if Swift had a type for half-floats to match the
iPhone GPU:
If/when 16b floats were added to the standard lib, you would just write:
let vertexData: [Float16] = [ 1, 0, 0.5, 1 ]
I should note that something like vertex coordinates is usually better modeled with a more specific type than [Float], like SCNVector4 or simd.float4 or your own type, which also solves this problem:
import SceneKit
let vertexData = SCNVector4(1, 0, 0.5, 1)
import simd
let vertexData = float4(1, 0, 0.5, 1)
(NB both of these frameworks are Apple-specific).
- Steve
···
Sent from my iPhone
On Nov 3, 2017, at 14:58, Tony Allevato via swift-evolution <swift-evolution@swift.org> wrote:
You can write this for the first thing that you want:
let vertexData: [Float] = [1.0, 0.0, 0.5, 1.0]
I don't know enough about 16-bit floats to comment on those.
On Fri, Nov 3, 2017 at 11:26 AM nick ralabate via swift-evolution <swift-evolution@swift.org> wrote:
I think it would be helpful for graphics programmers to specify vertex data inline without surrounding every value with Float(4.0).
Something like this:
let vertexData = [ 1.0f, 0.0f, 0.5f, 1.0f ]
Also, it would be great if Swift had a type for half-floats to match the iPhone GPU:
SCNVector4 is a C struct and hence has fixed layout. simd.float4 fixes its ordering by having a single field that is an LLVM ext-vector type. Both can be safely converted to a buffer of floats.
– Steve
···
On Nov 3, 2017, at 3:33 PM, Kelvin Ma <kelvin13ma@gmail.com> wrote:
On Fri, Nov 3, 2017 at 2:05 PM, Steve Canon via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
If/when 16b floats were added to the standard lib, you would just write:
let vertexData: [Float16] = [ 1, 0, 0.5, 1 ]
I should note that something like vertex coordinates is usually better modeled with a more specific type than [Float], like SCNVector4 or simd.float4 or your own type, which also solves this problem:
import SceneKit
let vertexData = SCNVector4(1, 0, 0.5, 1)
import simd
let vertexData = float4(1, 0, 0.5, 1)
(NB both of these frameworks are Apple-specific).
- Steve
Sent from my iPhone
If @_fixed_layout was supported this would be sensible, but most graphics frameworks expect a plain buffer of floats and the packing order is implicit. We can’t model vertex vectors with Swift structs because we can’t safely pointer-cast them to an array of floats.