Skip to content

Commit

Permalink
Improved location caching.
Browse files Browse the repository at this point in the history
  • Loading branch information
skrat committed Aug 26, 2014
1 parent cc36456 commit 33fd823
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 33 deletions.
2 changes: 1 addition & 1 deletion project.clj
@@ -1,4 +1,4 @@
(defproject accent "0.1.3"
(defproject accent "0.1.4"
:description "WebGL utility belt"
:url "http://github.com/skrat/accent"
:author "skrat"
Expand Down
4 changes: 2 additions & 2 deletions src/accent/nodes.cljs
Expand Up @@ -137,8 +137,8 @@
(defn draw!
[{:keys [program drawables] :as node} uniforms]
(shaders/set-uniforms! program
(assoc uniforms
:viewport [:val2 (drop 2 (-> node :props :viewport))]))
(assoc uniforms :viewport
[:val2 (drop 2 (-> node :props :viewport))]))
(doseq [d drawables]
(drawables/set-pointers-for-shader! program d)
(drawables/draw! program d)))
Expand Down
52 changes: 22 additions & 30 deletions src/accent/shaders.cljs
Expand Up @@ -4,6 +4,26 @@
[accent.context :refer [gl]]
[accent.textures :as textures]))

(defprotocol IProgram
(get-attribute-location [this attribute])
(get-uniform-location [this uniform])
(get-program [this]))

(deftype Program [program cache]
IProgram
(get-attribute-location [_ attribute]
(when-not (aget cache attribute)
(let [loc (.getAttribLocation gl program attribute)]
(when (>= loc 0)
(aset cache attribute loc)
(.enableVertexAttribArray gl loc))))
(aget cache attribute))
(get-uniform-location [_ uniform]
(when-not (aget cache uniform)
(aset cache uniform (.getUniformLocation gl program uniform)))
(aget cache uniform))
(get-program [_] program))

(defn get-shader-info-log
"Return the information log for a shader object."
[shader]
Expand All @@ -24,21 +44,6 @@
[program pname]
(.getProgramParameter gl program pname))

(def get-attribute-location
"Return the location of an attribute variable."
(memoize (fn
[program attrib-name]
(let [loc (.getAttribLocation gl program attrib-name)]
(when (>= loc 0)
(.enableVertexAttribArray gl loc))
loc))))

(def get-uniform-location
"Return the location of a uniform variable."
(memoize (fn
[program uniform-name]
(.getUniformLocation gl program uniform-name))))

(defn check-shader
"Checks for shader (compilation) error and throws if one is found."
[shader]
Expand Down Expand Up @@ -78,24 +83,11 @@
(dorun (map (fn [shader] (.attachShader gl program shader)) shaders))
(.linkProgram gl program)
(check-program program)
program)))
(Program. program #js {}))))

(defn use!
[program]
(.useProgram gl program))

(defn memoize-uniform
[f]
(let [mem (atom {})]
(fn [program name type value]
(if (ta/typed-array? value)
(f program name type value)
(let [k [program name]
v [type value]
c (get @mem k)]
(when (not= c v)
(f program name type value)
(swap! mem assoc k v)))))))
(.useProgram gl (get-program program)))

(defn set-uniform!
[program attr type value]
Expand Down

0 comments on commit 33fd823

Please sign in to comment.