Skip to content

Commit

Permalink
Add pipeline-level defines
Browse files Browse the repository at this point in the history
  • Loading branch information
borodust committed Sep 16, 2018
1 parent f7e1fec commit 89760d9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
16 changes: 10 additions & 6 deletions graphics/pipeline.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,15 @@

(defun %build-shading-program (this &key vertex fragment geometry)
(with-slots (defines) this
(let (program)
(let (program
(combined-defines (append defines (%defines-of this))))
(tagbody start
(restart-case
(setf program (link-shader-libraries :vertex-shader (append vertex defines)
:fragment-shader (append fragment defines)
:geometry-shader (when geometry
(append geometry defines))))
(setf program (link-shader-libraries
:vertex-shader (append vertex combined-defines)
:fragment-shader (append fragment combined-defines)
:geometry-shader (when geometry
(append geometry combined-defines))))
(relink ()
:report "Try relinking the pipeline"
(go start))
Expand All @@ -127,13 +129,15 @@

(defmacro defpipeline (name-and-opts &body shaders)
(destructuring-bind (name &rest opts) (ensure-list name-and-opts)
(destructuring-bind (&key (primitive '(:triangles))) (alist-plist opts)
(destructuring-bind (&key defines (primitive '(:triangles))) (alist-plist opts)
(with-gensyms (this)
`(progn
(defclass ,name (pipeline) ())
(defmethod pipeline-primitive ((,this ,name))
(declare (ignore ,this))
,@primitive)
(defmethod %defines-of ((,this ,name))
(list ,@defines))
(defmethod pipeline-shaders ((,this ,name))
(declare (ignore ,this))
',(loop for (nil shader-opts) on shaders by #'cddr
Expand Down
11 changes: 7 additions & 4 deletions graphics/shader-registry.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,17 @@
compiled-shader
(apply #'refresh-compiled-shader library type shader-opts))))
(nconc (list shader-id) (loop for dependency in (shader-library-dependencies library)
nconc (collect-compiled-libraries dependency type)))))))
nconc (apply #'collect-compiled-libraries
dependency type shader-opts)))))))


(defun link-shader-libraries (&rest libraries &key &allow-other-keys)
(let ((shaders (loop for (type (shader-name . shader-opts)) on libraries by #'cddr
when shader-opts
when shader-name
append (apply #'collect-compiled-libraries
shader-name type shader-opts)))
(program (gl:create-program)))

(handler-bind ((serious-condition (lambda (c)
(declare (ignore c))
(gl:delete-program program)
Expand All @@ -210,5 +212,6 @@


(defun shader-source (shader-library shader-type)
(values (preprocess-shader (shader-library-descriptor (find-shader-library shader-library))
shader-type)))
(if-let ((library (find-shader-library shader-library)))
(preprocess-shader (shader-library-descriptor library) shader-type)
(error "Library ~A not found" shader-library)))
2 changes: 1 addition & 1 deletion shading/phong/phong.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#pragma bodge: import bodge/phong
#pragma bodge: import bodge/math

#ifdef FRAGMENT_SHADER
#ifdef BODGE_FRAGMENT_SHADER

// adapted from https://github.com/stackgl/glsl-lighting-walkthrough
float attenuation(float r, float f, float d) {
Expand Down
8 changes: 4 additions & 4 deletions text/shaders/text.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#pragma bodge: import bodge/text

#ifdef FRAGMENT_SHADER
#ifdef BODGE_FRAGMENT_SHADER

vec4 sdfTest(vec4 baseColor, vec2 sdfCoords, sampler2D atlas) {
float d = texture(atlas, sdfCoords).r;
Expand All @@ -14,10 +14,10 @@ vec4 sdfTest(vec4 baseColor, vec2 sdfCoords, sampler2D atlas) {
return vec4(baseColor.rgb, a);
}

#endif // FRAGMENT_SHADER code
#endif // BODGE_FRAGMENT_SHADER code


#ifdef GEOMETRY_SHADER
#ifdef BODGE_GEOMETRY_SHADER

GlyphVertex[4] makeGlyphVertices (Glyph glyph) {
return GlyphVertex[4](GlyphVertex(glyph.box.zy,
Expand All @@ -30,4 +30,4 @@ GlyphVertex[4] makeGlyphVertices (Glyph glyph) {
glyph.texCoords.sq));
}

#endif // GEOMETRY_SHADER code
#endif // BODGE_GEOMETRY_SHADER code
4 changes: 2 additions & 2 deletions text/shaders/text.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#ifndef _TEXT_H
#define _TEXT_H

#ifdef FRAGMENT_SHADER
#ifdef BODGE_FRAGMENT_SHADER

vec4 sdfTest(vec4 baseColor, vec2 sdfCoords, sampler2D atlas);

#endif // FRAGMENT_SHADER


#ifdef GEOMETRY_SHADER
#ifdef BODGE_GEOMETRY_SHADER

struct Glyph {
vec4 box;
Expand Down

0 comments on commit 89760d9

Please sign in to comment.