Skip to content

Instantly share code, notes, and snippets.

@psilord
Created April 20, 2018 04:45
Show Gist options
  • Save psilord/0d45069524732cd42c8ea8ff5dd1ca21 to your computer and use it in GitHub Desktop.
Save psilord/0d45069524732cd42c8ea8ff5dd1ca21 to your computer and use it in GitHub Desktop.
(shadow:defstruct-gpu texture2 ()
(image :sampler-2d :accessor image)
(sprite :int :accessor sprite))
(shadow:defstruct-gpu sprite-sheet-data ()
(x (:float 4) :accessor x)
(y (:float 4) :accessor y)
(w (:float 4) :accessor w)
(h (:float 4) :accessor h))
(shadow:defun-gpu sprite/vert ()
(values))
(shadow:defun-gpu sprite/geom (&uniform
(model :mat4)
(view :mat4)
(proj :mat4)
(tex texture2)
(sprite-sheet sprite-sheet-data :ssbo :std-430))
(declare (output-primitive :kind :triangle-strip :max-vertices 6))
(let* ((mvp (* proj view model))
(extents (vec4 (aref (x sprite-sheet) (sprite tex))
(aref (y sprite-sheet) (sprite tex))
(aref (w sprite-sheet) (sprite tex))
(aref (h sprite-sheet) (sprite tex))))
(size (.xyxy (texture-size (image tex) 0)))
(offsets (* size (vec4 (* 0.5 (.zw extents)) (* -0.5 (.zw extents))))))
(setf (.zw extents) (+ (.xy extents) (.zw extents)))
(emit ()
(* mvp (vec4 (.xy offsets) 1 1))
(.xy extents))
(emit ()
(* mvp (vec4 (.xw offsets) 1 1))
(.xw extents))
(emit ()
(* mvp (vec4 (.zy offsets) 1 1))
(.zy extents))
(emit ()
(* mvp (vec4 (.zw offsets) 1 1))
(.zw extents))
(end-primitive))
(values))
(shadow:defun-gpu sprite/frag ((uv :vec2)
&uniform
(tex texture2))
(let ((color (texture (image tex) uv)))
color))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment