diff options
-rw-r--r-- | sly/render/shader.scm | 58 |
1 files changed, 26 insertions, 32 deletions
diff --git a/sly/render/shader.scm b/sly/render/shader.scm index b736560..fe2eddd 100644 --- a/sly/render/shader.scm +++ b/sly/render/shader.scm @@ -49,7 +49,6 @@ vertex-position-location vertex-texture-location shader-program-uniform-location - shader-program-attribute-location shader-program-id shader-program-uniforms shader-program-linked? @@ -201,12 +200,13 @@ in the file FILENAME." (location attribute-location)) (define-record-type <shader-program> - (%make-shader-program id uniforms attributes) + (%make-shader-program id uniforms) shader-program? (id shader-program-id) - (uniforms shader-program-uniforms) - (attributes shader-program-attributes)) + (uniforms shader-program-uniforms)) +;; Hard-coded vertex attribute locations. These are fixed so that all +;; Sly shaders abide by the same interface. (define vertex-position-location 0) (define vertex-texture-location 1) @@ -218,15 +218,6 @@ in the file FILENAME." (uniform-location uniform) (error "Uniform not found: " name)))) -(define (shader-program-attribute-location shader-program attribute-name) - (let ((attribute (find (match-lambda - (($ <attribute> name _) - (string=? attribute-name name))) - (shader-program-attributes shader-program)))) - (if attribute - (attribute-location attribute) - (error "Attribute not found: " attribute-name)))) - (define-guardian shader-program-guardian (lambda (shader-program) (false-if-exception @@ -235,7 +226,7 @@ in the file FILENAME." (define-status shader-program-linked? glGetProgramiv link-status) (define-logger display-linking-error glGetProgramiv glGetProgramInfoLog) -(define (make-shader-program vertex-shader fragment-shader uniforms attributes) +(define (make-shader-program vertex-shader fragment-shader uniforms) "Create a new shader program that has been linked with the given VERTEX-SHADER and FRAGMENT-SHADER." (unless (and (vertex-shader? vertex-shader) @@ -275,8 +266,7 @@ VERTEX-SHADER and FRAGMENT-SHADER." (glDetachShader id (shader-id shader))) shaders) (let* ((uniforms (map build-uniform uniforms)) - (attributes (map string->attribute attributes)) - (shader-program (%make-shader-program id uniforms attributes))) + (shader-program (%make-shader-program id uniforms))) (shader-program-guardian shader-program) shader-program)) throw @@ -284,14 +274,14 @@ VERTEX-SHADER and FRAGMENT-SHADER." ;; Make sure to delete program in case linking fails. (glDeleteProgram id))))) -(define (load-shader-program vertex-shader-file-name fragment-shader-file-name - uniforms attributes) - (make-shader-program (load-vertex-shader vertex-shader-file-name) - (load-fragment-shader fragment-shader-file-name) - uniforms attributes)) +(define* (load-shader-program #:key vertex-source fragment-source + uniforms) + (make-shader-program (load-vertex-shader vertex-source) + (load-fragment-shader fragment-source) + uniforms)) (define null-shader-program - (%make-shader-program 0 '() '())) + (%make-shader-program 0 '())) (define (apply-shader-program shader-program) (glUseProgram (shader-program-id shader-program))) @@ -378,13 +368,17 @@ within SHADER-PROGRAM." ;;; Built-in Shaders ;;; -(define load-default-shader - (memoize - (lambda () - (load-shader-program - (string-append %datadir "/shaders/default-vertex.glsl") - (string-append %datadir "/shaders/default-fragment.glsl") - `((mvp "mvp" ,identity-transform) - (color "color" ,white) - (texture? "use_texture" #f)) - '("position" "tex"))))) +;; Use lazy evaluation because shader loading will fail until there is +;; an OpenGL context available. +(define %default-shader + (delay + (load-shader-program + #:vertex-source (scope-datadir "/shaders/default-vertex.glsl") + #:fragment-source (scopre-datadir "/shaders/default-fragment.glsl") + #:uniforms `((mvp "mvp" ,identity-transform) + (color "color" ,white) + (texture? "use_texture" #f))))) + +(define (load-default-shader) + "Load and return the default shader program." + (force %default-shader)) |