diff options
-rw-r--r-- | chickadee/graphics/seagull.scm | 61 |
1 files changed, 47 insertions, 14 deletions
diff --git a/chickadee/graphics/seagull.scm b/chickadee/graphics/seagull.scm index 44965c2..8c1fcd6 100644 --- a/chickadee/graphics/seagull.scm +++ b/chickadee/graphics/seagull.scm @@ -47,6 +47,7 @@ ;; ;;; Code: (define-module (chickadee graphics seagull) + #:use-module (chickadee graphics engine) #:use-module (chickadee graphics shader) #:use-module (ice-9 exceptions) #:use-module (ice-9 format) @@ -2600,10 +2601,18 @@ (list output-temp)) (define (emit:top-level bindings body stage version port level) + (define (glsl-qualifier qualifier) + (case qualifier + ((in) + (if (string>= version "1.3") 'in 'attribute)) + ((out) + (if (string>= version "1.3") 'out 'varying)) + ((uniform) + 'uniform))) (for-each (match-lambda (((? top-level-qualifier? qualifier) type-desc name) (format port "~a ~a ~a;\n" - qualifier + (glsl-qualifier qualifier) (type-descriptor->glsl type-desc) name)) (('function name ('t (type) ('lambda params body))) @@ -2921,8 +2930,36 @@ (cons (assq-ref global-map name) name))) (seagull-module-uniforms module)))) -(define* (link-seagull-modules vertex fragment #:key - (version '330)) +(define (emit-version-preprocessor version port) + (cond + ((string>= version "3.3") + (format port "#version 330\n")) + ((string>= version "1.3") + (format port "#version 130\n")) + ((string>= version "1.2") + (format port "#version 120\n")) + (else + (error "incompatible GLSL version" version)))) + +(define (emit-shims version port) + (when (string<= version "3.3") + (format port " +vec4 texture(sampler2D tex, vec2 coord) { + return texture2D(tex, coord); +} +vec4 texture(samplerCube tex, vec3 coord) { + return textureCube(tex, coord); +} +"))) + +(define (emit-stage exp stage version) + (call-with-output-string + (lambda (port) + (emit-version-preprocessor version port) + (emit-shims version port) + (emit-glsl exp 'fragment version port)))) + +(define* (link-seagull-modules vertex fragment version) (unless (seagull-module-vertex? vertex) (error "not a vertex shader" vertex)) (unless (seagull-module-fragment? fragment) @@ -2936,26 +2973,22 @@ (error "vertex uniforms clash with fragment uniforms")) (define-values (vertex* fragment* uniform-map) (link-vertex-outputs-with-fragment-inputs vertex fragment)) - (define vertex-glsl - (call-with-output-string - (lambda (port) - (emit-glsl vertex* 'fragment version port)))) - (define fragment-glsl - (call-with-output-string - (lambda (port) - (emit-glsl fragment* 'fragment version port)))) + (define vertex-glsl (emit-stage vertex* 'vertex version)) + (define fragment-glsl (emit-stage fragment* 'fragment version)) (display vertex-glsl) (newline) (display fragment-glsl) (newline) (values vertex-glsl fragment-glsl uniform-map))) -(define (compile-shader vertex fragment) +(define* (compile-shader vertex fragment #:key + (version (graphics-engine-glsl-version))) (let-values (((glsl:vertex glsl:fragment uniform-map) - (link-seagull-modules vertex fragment))) + (link-seagull-modules vertex fragment version))) (call-with-input-string glsl:vertex (lambda (vertex-port) (call-with-input-string glsl:fragment (lambda (fragment-port) (make-shader vertex-port fragment-port - #:uniform-map uniform-map))))))) + #:uniform-map uniform-map + #:pre-process? #f))))))) |