Skip to content
This repository was archived by the owner on May 18, 2026. It is now read-only.

Commit 5ba58b1

Browse files
committed
Minor cpu particle emitter optimisations
1 parent 122f989 commit 5ba58b1

1 file changed

Lines changed: 35 additions & 32 deletions

File tree

renderer/cpu-particle.lisp

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,11 @@
164164
matrix velocity rotation lifespan size scaling color
165165
vertex-data vertex-stride faces)
166166
(declare (type (simple-array single-float (*)) particles properties vertex-data))
167-
(declare (type (unsigned-byte 32) pos prop))
167+
(declare (type (unsigned-byte 32) pos prop color))
168168
(declare (type single-float lifespan lifespan-randomness randomness size scaling rotation velocity))
169169
(declare (type vec3 randoms))
170+
(declare (type mat4 matrix))
171+
(declare (optimize speed (safety 0)))
170172
(let* ((location (vec 0 0 0))
171173
(normal (vec 0 0 0))
172174
(velocity (vec velocity velocity velocity))
@@ -181,8 +183,8 @@
181183
(setf (aref properties (+ prop 2)) (+ size (* size randomness (- (vy randoms) 0.5))))
182184
(setf (aref properties (+ prop 3)) (* (aref properties (+ prop 2)) scaling))
183185
;; Randomise flip if activated
184-
(when (logbitp 30 color) (setf (ldb (byte 1 30) color) (round (vx randoms))))
185-
(when (logbitp 31 color) (setf (ldb (byte 1 31) color) (round (vy randoms))))
186+
(when (logbitp 30 color) (setf (ldb (byte 1 30) color) (round (the (single-float 0.0 1.0) (vx randoms)))))
187+
(when (logbitp 31 color) (setf (ldb (byte 1 31) color) (round (the (single-float 0.0 1.0) (vy randoms)))))
186188
(setf (aref properties (+ prop 4)) (float-features:bits-single-float color))
187189
;; We pad the matrix to offset 8 to get things vec4 aligned.
188190
(setf (aref properties (+ prop 5)) 0.0)
@@ -271,35 +273,36 @@
271273

272274
(defmethod emit ((emitter cpu-particle-emitter) count &rest particle-options &key vertex-array location orientation scaling transform &allow-other-keys)
273275
(setf (particle-options emitter) (remf* particle-options :vertex-array :location :orientation :scaling :transform))
274-
;; FIXME: don't permanently change emitter transform or VAO.
275-
(when location (setf (location emitter) location))
276-
(when scaling (setf (scaling emitter) scaling))
277-
(when orientation (setf (orientation emitter) orientation))
278-
(when transform (setf (local-transform emitter) transform))
279-
(when vertex-array (setf (vertex-array emitter) vertex-array))
280-
(with-all-slots-bound (emitter cpu-particle-emitter)
281-
(let ((mat (mat4))
282-
(min-prop most-positive-fixnum)
283-
(max-prop 0))
284-
(declare (dynamic-extent mat))
285-
(global-transform-matrix emitter mat)
286-
(dotimes (i (min count (length free-list)))
287-
(let ((pos (* 8 live-particles))
288-
(prop (vector-pop free-list)))
289-
(when (< prop min-prop) (setf min-prop prop))
290-
(when (< max-prop prop) (setf max-prop prop))
291-
(%emit-particle particles properties pos prop (vrand (vec 0.5 0.5 0.5) (vec3 1))
292-
particle-randomness particle-lifespan-randomness mat
293-
particle-velocity particle-rotation particle-lifespan
294-
particle-size particle-scaling particle-full-color
295-
vertex-data vertex-stride face-data)
296-
(incf live-particles)))
297-
(when (and (< 0 live-particles) (< min-prop most-positive-fixnum))
298-
(let ((src (first (sources particle-property-buffer))))
299-
(setf (nth 0 (texture-source-src src)) (truncate min-prop 4))
300-
(setf (nth 3 (texture-source-dst src)) (truncate (- (+ 24 max-prop) min-prop) 4))
301-
(activate particle-property-buffer)
302-
(upload-texture-source src particle-property-buffer))))))
276+
(let ((local (the transform (local-transform emitter))))
277+
(when location (v<- (tlocation local) location))
278+
(when scaling (v<- (tscaling local) scaling))
279+
(when orientation (q<- (trotation local) orientation))
280+
(when transform (t<- local transform))
281+
;; FIXME: don't permanently change emitter VAO or transform
282+
(when vertex-array (setf (vertex-array emitter) vertex-array))
283+
(with-all-slots-bound (emitter cpu-particle-emitter)
284+
(let ((mat (mat4))
285+
(min-prop most-positive-fixnum)
286+
(max-prop 0))
287+
(declare (dynamic-extent mat))
288+
(global-transform-matrix emitter mat)
289+
(dotimes (i (min count (length free-list)))
290+
(let ((pos (* 8 live-particles))
291+
(prop (vector-pop free-list)))
292+
(when (< prop min-prop) (setf min-prop prop))
293+
(when (< max-prop prop) (setf max-prop prop))
294+
(%emit-particle particles properties pos prop (vrand (vec 0.5 0.5 0.5) (vec3 1))
295+
particle-randomness particle-lifespan-randomness mat
296+
particle-velocity particle-rotation particle-lifespan
297+
particle-size particle-scaling particle-full-color
298+
vertex-data vertex-stride face-data)
299+
(incf live-particles)))
300+
(when (and (< 0 live-particles) (< min-prop most-positive-fixnum))
301+
(let ((src (first (sources particle-property-buffer))))
302+
(setf (nth 0 (texture-source-src src)) (truncate min-prop 4))
303+
(setf (nth 3 (texture-source-dst src)) (truncate (- (+ 24 max-prop) min-prop) 4))
304+
(activate particle-property-buffer)
305+
(upload-texture-source src particle-property-buffer)))))))
303306

304307
(defmethod clear ((emitter cpu-particle-emitter))
305308
(setf (live-particles emitter) 0)

0 commit comments

Comments
 (0)