Skip to content

Commit

Permalink
fix: (hopefully) fixing opengl exceptions issues with UBOs
Browse files Browse the repository at this point in the history
they happened after many restarts
always seemed to be triggered by an ubo related
something about the "range" of the new ubos was wrong

the "fix" is to have a more aggressive re-use of state

I keep the state, and only free scenes.
  • Loading branch information
azimut committed Oct 31, 2024
1 parent 2ef9cec commit 094cffe
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 34 deletions.
5 changes: 0 additions & 5 deletions src/lights/ibl/brdf.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
(brdf-fbo :reader brdf-fbo))
(:documentation "brdf lookup table"))

(defmethod free :after ((obj brdf))
(free (bs obj))
(free (brdf-fbo obj))
(free (brdf-tex obj)))

(defun-g importance-sample-ggx ((xi :vec2)
(n :vec3)
(roughness :float))
Expand Down
15 changes: 0 additions & 15 deletions src/lights/lights.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,6 @@
(point-ubo :reader point-ubo :documentation "common point lights ubo"))
(:documentation "state global pointlight resources"))

(defmethod free :after ((obj spotlights))
(with-slots (spot-tex spot-ubo) obj
(free spot-tex)
(free spot-ubo)))

(defmethod free :after ((obj pointlights))
(with-slots (point-tex point-ubo) obj
(free point-tex)
(free point-ubo)))

(defmethod free :after ((obj dirlights))
(with-slots (dir-tex dir-ubo) obj
(free dir-tex)
(free dir-ubo)))

(defmethod initialize-instance :before ((obj lights) &key dim)
(check-type dim (integer 256 4096)))

Expand Down
6 changes: 6 additions & 0 deletions src/material/material.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,9 @@
(setf (aref-c (pbr-material-specular (aref-c c 0)) idx) specular)
(setf (aref-c (pbr-material-metallic (aref-c c 0)) idx) metallic)
(setf (aref-c (pbr-material-emissive (aref-c c 0)) idx) emissive))))

(defun set-and-upload-materials (materials materials-ubo)
(mapc (lambda (m)
(setf (slot-value m 'ubo) materials-ubo)
(upload m))
materials))
4 changes: 0 additions & 4 deletions src/screen.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
(setf (slot-value obj 'prev) (make-renderable))
(setf (slot-value obj 'next) (make-renderable))))

(defmethod free :after ((obj screen))
(free (prev obj))
(free (next obj)))

(defmethod handle :after ((e resize) (obj screen))
(setf (dim (prev *state*)) (list (width e) (height e))
(dim (next *state*)) (list (width e) (height e))))
26 changes: 16 additions & 10 deletions src/state.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,26 @@
(defmethod initialize-instance :after ((obj state) &key materials)
(with-slots (materials-ubo) obj
(setf materials-ubo (make-ubo NIL 'pbr-material))
(mapc (lambda (m)
(setf (slot-value m 'ubo) materials-ubo)
(upload m))
materials)))
(set-and-upload-materials materials materials-ubo)))

(defmethod reinitialize-instance :after ((obj state) &key materials)
(with-slots (scenes scene-index last-time materials-ubo) obj
(set-and-upload-materials materials materials-ubo)
(setf last-time (get-internal-real-time))
(setf scene-index -1)
(setf scenes ())))

(defun init-state (materials)
(setf *state* (make-instance 'state :materials materials)))
(if *state*
(reinitialize-instance *state* :materials materials)
(setf *state* (make-instance 'state :materials materials))))

(defun init-default-state ()
(init-state (list (make-material :roughness .8 :metallic .02 :specular .1)
(make-material :roughness .4 :metallic .4 :specular .1))))

(defmethod free ((obj state))
(free (materials-ubo obj))
(reset-material-counter)
(mapc #'free (scenes obj)))

(defmethod (setf scene-index) :before (new-value (obj state))
Expand All @@ -51,7 +61,3 @@
(defmethod handle :around ((e resize) (obj perspective))
(when (equal obj (current-camera))
(call-next-method)))

(defun init-default-state ()
(init-state (list (make-material :roughness .8 :metallic .02 :specular .1)
(make-material :roughness .4 :metallic .4 :specular .1))))

0 comments on commit 094cffe

Please sign in to comment.