github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/particle/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"unsafe"
     7  )
     8  
     9  func main() {
    10  	particles := Particles{}
    11  	defer runtime.KeepAlive(particles)
    12  
    13  	for i := 0; i < ParticleCount; i++ {
    14  		p := particles.Ref(i)
    15  		p.X = float32(i)
    16  		p.Y = float32(-i)
    17  		p.Z = float32(i)
    18  	}
    19  
    20  	fmt.Println(particles.X)
    21  	fmt.Println(particles.Y)
    22  	fmt.Println(particles.Z)
    23  }
    24  
    25  const ParticleCount = 256
    26  
    27  type Particles struct {
    28  	X [ParticleCount]float32
    29  	Y [ParticleCount]float32
    30  	Z [ParticleCount]float32
    31  
    32  	VX [ParticleCount]float32
    33  	VY [ParticleCount]float32
    34  	VZ [ParticleCount]float32
    35  
    36  	CA [ParticleCount]float32
    37  	CR [ParticleCount]float32
    38  	CG [ParticleCount]float32
    39  	CB [ParticleCount]float32
    40  }
    41  
    42  func (p *Particles) Ref(i int) *Particle {
    43  	return (*Particle)(unsafe.Pointer(&p.X[i]))
    44  }
    45  
    46  type Particle struct {
    47  	X float32
    48  	_ [ParticleCount - 1]float32
    49  	Y float32
    50  	_ [ParticleCount - 1]float32
    51  	Z float32
    52  	_ [ParticleCount - 1]float32
    53  
    54  	VX float32
    55  	_  [ParticleCount - 1]float32
    56  	VY float32
    57  	_  [ParticleCount - 1]float32
    58  	VZ float32
    59  
    60  	CA float32
    61  	_  [ParticleCount - 1]float32
    62  	CR float32
    63  	_  [ParticleCount - 1]float32
    64  	CG float32
    65  	_  [ParticleCount - 1]float32
    66  	CB float32
    67  }