summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2015-11-14 22:36:53 -0500
committerDavid Thompson <dthompson2@worcester.edu>2015-11-14 22:36:53 -0500
commit9ff05910ecdaceed3b36aa29c75f99637a439cd0 (patch)
treeeee5cb56f73772b8902d84666e3c91fb66b6227c
parent62080c1a2c87e1d856c1a4611c874d1d6ee63a9a (diff)
render: shader: Remove 'attributes' field.
Attributes are fixed so the field is useless. * sly/render/shader.scm (shader-program-attribute-location): Delete. (<shader-program>): Delete 'attributes' field. (make-shader-program): Remove 'attributes' argument. (load-shader-program): Likewise. Change arguments from positional to keyword. (%default-shader): New variable. (load-default-shader): Reimplement in terms of promises.
-rw-r--r--sly/render/shader.scm58
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))