cbaggers / cepl

Code Evaluate Play Loop
BSD 2-Clause "Simplified" License
860 stars 51 forks source link

Heap Exhausted Error #346

Closed Zaijab closed 4 years ago

Zaijab commented 4 years ago

Goal: Run the code in "cepl/docs/001 - Into the code.md" and perform some of the operations outlined in Baggers YouTube video "0 - CEPL - Lispy OpenGL".

Setup: I made a project using the directions in the README called "graphics", and changed the code to resolve some definition errors like those pointed out in #344 .

(eval-when (:compile-toplevel :load-toplevel :execute) (ql:quickload "graphics"))
(in-package #:graphics)

(defparameter *array* nil)
(defparameter *stream* nil)
(defparameter *running* nil)

;; Lisp data for triangle vertices
(defparameter *triangle-data*
   (list (list (rtg-math:v!  0.5 -0.36 0) (rtg-math:v! 0 1 0 0))
         (list (rtg-math:v!    0   0.5 0) (rtg-math:v! 1 0 0 0))
         (list (rtg-math:v! -0.5 -0.36 0) (rtg-math:v! 0 0 1 0))))

;; A struct that works on gpu and cpu
(defstruct-g pos-col
  (position :vec3 :accessor pos)
  (color :vec4 :accessor col))

;; A GPU vertex shader using a Lisp syntax (see Varjo)
(defun-g vert ((vert pos-col))
  (values (rtg-math:v! (pos vert) 1.0)
          (col vert)))

;; A GPU fragment shader
(defun-g frag ((color :vec4))
  color)

;; Composing those gpu functions into a pipeline
(defpipeline-g prog-1 ()
  (vert pos-col)
  (frag :vec4))

;; Here is what we do each frame:
(defun step-demo ()
  (step-host)        ;; Advance the host environment frame
  (livesupport:update-repl-link) ;; Keep the REPL responsive while running
  (clear)            ;; Clear the drawing buffer
  (map-g #'prog-1 *stream*) ;; Render data from GPU datastream
  (swap))            ;; Display newly rendered buffer

(defun run-loop ()
  (setf *running* t
    ;; Create a gpu array from our Lisp vertex data
        *array* (make-gpu-array *triangle-data* :element-type 'pos-col)
    ;; Create a GPU datastream
        *stream* (make-buffer-stream *array*))
  ;; continue rendering frames until *running* is set to nil
  (loop :while (and  *running*
             (not (shutting-down-p))) :do
           (livesupport:continuable (step-demo))))

(defun stop-loop ()
  (setf *running* nil))

(run-loop)

Steps to reproduce error:

  1. Open file and slime in Emacs
  2. In the slime repl, run the following (ql:quickload :cepl.sdl2) then (cepl:repl)
  3. Compile each top level form in the file (Using slime-compile-defun, C-c C-c)

Expected result: To have a multicolored triangle appear in the sdl2 window Actual result: The following error arises. Heap exhausted (no more space for allocation). 931987456 bytes available, 15089770976 requested.

Other methods: There are other methods to run the code with varying results.

Other method 1: Performing the following yields the expected multicolored triangle.

  1. Open file and slime in Emacs
  2. In the slime repl, run the following (ql:quickload :cepl.sdl2) then (cepl:repl)
  3. Copy and paste the code into the slime repl.

However, this does not allow me to update the triangle using pull-g and push-g as when I hit enter into the repl, an Emacs message pops up in the minibuffer saying REPL is busy.

Other method 2: First insert (cepl:repl) after (in-package #:graphics) then use slime-compile-and-load on the file results in the expected multicolored triangle. However, running (cepl:pull-g *array*) in the file and using slime-compile-defun gives the error `with-gpu-array-as-*: buffer mapped to null pointer Have you defintely got an opengl context?

.(SB-SYS:INT-SAP #X00000000)`

and running (cepl:pull-g *array*) in the slime repl gives the following error The variable *ARRAY* is unbound.

Other method 3: Very similar to method 2, but use slime-eval-buffer instead of slime-compile-and-load. First insert (cepl:repl) after (in-package #:graphics) then use slime-eval-buffer on the file results in the expected multicolored triangle. However, running (cepl:pull-g *array*) in the file and using slime-compile-defun gives the error `with-gpu-array-as-*: buffer mapped to null pointer Have you defintely got an opengl context?

.(SB-SYS:INT-SAP #X00000000)`

and running (cepl:pull-g *array*) in the slime repl gives the following error The variable *ARRAY* is unbound.

Conclusion: So I am unable to continue on with the next part of the video / video series.

Please let me know if I need to put more information or something is wrong stylistically. I would do anything to get this to work.

ghost commented 4 years ago

Try it without applying slime-compile-defun to that final (run-loop) form.

Instead, try entering (run-loop) at the repl after you have done (cepl:repl).

e.g. something like:

CL-USER> (ql:quickload :graphics)
To load "graphics":
  Load 1 ASDF system:
    graphics
; Loading "graphics"
......
(:GRAPHICS)
CL-USER> (in-package :graphics)
#<PACKAGE "GRAPHICS">
GRAPHICS> (cepl:repl)

-----------------
    CEPL-REPL    
-----------------
#<DEFAULT-FBO COLOR-ATTRS (0 1)>
GRAPHICS> (run-loop)
; uploading (PROG-1 ...)

; No value
GRAPHICS>

where the graphics package is your code above, but without that final (run-loop) line.

Zaijab commented 4 years ago

This method works perfect. Thank you so much!