aboutsummaryrefslogtreecommitdiff
path: root/src/experimental/renderer_cache_hash.diff
blob: 469e344678d2414d1f575b23f16644c10a7b9c9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
diff --git a/src/library_gl.js b/src/library_gl.js
index 4bce0ef..094a961 100644
--- a/src/library_gl.js
+++ b/src/library_gl.js
@@ -1386,6 +1386,7 @@ var LibraryGL = {
     mode: 0,
 
     rendererCache: {},
+    rendererCacheTemp: [], // temporary item for renderer cache lookups
     rendererComponents: {}, // small cache for calls inside glBegin/end. counts how many times the element was seen
     rendererComponentPointer: 0, // next place to start a glBegin/end component
     lastRenderer: null, // used to avoid cleaning up and re-preparing the same renderer
@@ -1520,9 +1521,71 @@ var LibraryGL = {
     },
 
     getRenderer: function() {
-      // return a renderer object given the liveClientAttributes
-      // we maintain a cache of renderers, optimized to not generate garbage
+      // return a renderer object given the liveClientAttributes and other relevant information about rendering state
       var attributes = GL.immediate.liveClientAttributes;
+
+
+
+
+
+      var temp = GL.immediate.rendererCacheTemp;
+      temp.length = 0;
+      var hash = 1000;
+      var salt = 0x0149F37A;
+      function add(value) {
+        temp.push(value);
+        hash = (hash*(value+3))^salt;
+      }
+      for (var i = 0; i < attributes.length; i++) {
+        var attribute = attributes[i];
+        add(attribute.name);
+        add(attribute.size);
+        add(attribute.type);
+      }
+      add(GLEmulation.fogEnabled ? GLEmulation.fogMode : 0);
+      if (GL.currProgram) { // Note the order here; this one is last, and optional
+        add(GL.currProgram);
+      }
+      var cache = GL.immediate.rendererCache;
+      function isCorrect(item) {
+        if (item.hash != hash) return false;
+        if (item.id.length != temp.length) return false;
+        var length = temp.length;
+        var existing = item.id;
+        for (var i = 0; i < length; i++) {
+          if (existing[i] != temp[i]) {
+            return false;
+          }
+        }
+        return true;
+      }
+      var seek = hash;
+      var item;
+      while ((item = cache[seek]) && !isCorrect(item)) {
+        Module.printErr('cache collision!');
+        seek++;
+      }
+      if (item) return item.renderer;
+#if GL_DEBUG
+      Module.printErr('generating renderer for ' + JSON.stringify(attributes));
+#endif
+      var renderer = this.createRenderer();
+      cache[seek] = {
+        id: temp.slice(0),
+        hash: hash,
+        renderer: renderer
+      };
+      return renderer;
+
+
+
+
+
+
+
+
+
+      // we maintain a cache of renderers, optimized to not generate garbage
       var cacheItem = GL.immediate.rendererCache;
       for (var i = 0; i < attributes.length; i++) {
         var attribute = attributes[i];
@@ -1544,6 +1607,12 @@ var LibraryGL = {
         if (!cacheItem[GL.currProgram]) cacheItem[GL.currProgram] = {};
         cacheItem = cacheItem[GL.currProgram];
       }
+
+
+
+
+
+
       if (!cacheItem.renderer) {
 #if GL_DEBUG
         Module.printErr('generating renderer for ' + JSON.stringify(attributes));