From 82f71b0d13788b1cff9a24c5b652effd11631523 Mon Sep 17 00:00:00 2001 From: Colin Okay Date: Wed, 29 Jun 2022 11:54:24 -0500 Subject: [refactor] [structure] modularized project file structure --- src/gl/shader.lisp | 42 ++++++++++++++++++++++++++++++++++++++++++ src/gl/texture.lisp | 12 ++++++++++++ src/gl/util.lisp | 19 +++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 src/gl/shader.lisp create mode 100644 src/gl/texture.lisp create mode 100644 src/gl/util.lisp (limited to 'src/gl') diff --git a/src/gl/shader.lisp b/src/gl/shader.lisp new file mode 100644 index 0000000..4bba7b8 --- /dev/null +++ b/src/gl/shader.lisp @@ -0,0 +1,42 @@ +;;;; shader.lisp + +(in-package #:wheelwork) + +(defun shader-by-type (type) + (case type + (:vertex :vertex-shader) + (:geometry :geometry-shader) + (:fragment :fragment-shader))) + +(defun gl-shader (type stage) + (let ((shader (gl:create-shader type))) + (gl:shader-source shader (varjo:glsl-code stage)) + (gl:compile-shader shader) + (unless (gl:get-shader shader :compile-status) + (error "failed to compile ~a shader:~%~a~%" + type (gl:get-shader-info-log shader))) + shader)) + +(defun create-shader (&rest sources) + (let* ((stages + (varjo:rolling-translate + (mapcar (lambda (source) + (destructuring-bind (type inputs uniforms code) source + (varjo:make-stage type inputs uniforms '(:330) code))) + sources))) + (shaders + (loop + :for stage :in stages + :for source :in sources + :collect (gl-shader (shader-by-type (car source)) + stage))) + (program (gl:create-program))) + (dolist (shader shaders) (gl:attach-shader program shader)) + (gl:link-program program) + (unless (gl:get-program program :link-status) + (error "failed to link program: ~%~a~%" + (gl:get-program-info-log program))) + (dolist (shader shaders) + (gl:detach-shader program shader) + (gl:delete-shader shader)) + program)) diff --git a/src/gl/texture.lisp b/src/gl/texture.lisp new file mode 100644 index 0000000..ad753a1 --- /dev/null +++ b/src/gl/texture.lisp @@ -0,0 +1,12 @@ +;;;; texture.lisp + +(in-package #:wheelwork) + +(defclass/std texture () + ((width height id mipmap :with :r) + (internal-format image-format :ri :with :std :rgba) + (wrap-s wrap-t :ri :with :std :repeat) + (min-filter mag-filter :ri :with :std :nearest))) + +(defmethod cleanup ((texture texture)) + (gl:delete-texture (texture-id texture))) diff --git a/src/gl/util.lisp b/src/gl/util.lisp new file mode 100644 index 0000000..bff2f88 --- /dev/null +++ b/src/gl/util.lisp @@ -0,0 +1,19 @@ +;;;; gl/util.lisp + +(in-package #:wheelwork) + +(define-symbol-macro +float-size+ + (cffi:foreign-type-size :float)) + +(defun gl-array (type &rest contents) + (let ((array (gl:alloc-gl-array type (length contents)))) + (dotimes (i (length contents) array) + (setf (gl:glaref array i) (elt contents i))))) + +(defmacro with-gl-array ((var type &rest contents) &body body) + `(let ((,var (gl-array ,type ,@contents))) + (unwind-protect (progn ,@body) + (gl:free-gl-array ,var)))) + + + -- cgit v1.2.3