Beispiel #1
0
 private Color lookupShadingCache(ShadingState state, IShader shader)
 {
     lock (lockObj)
     {
         if (state.getNormal() == null)
         {
             return(null);
         }
         cacheLookups++;
         int        cx   = (int)(state.getRasterX() * shadingCacheResolution);
         int        cy   = (int)(state.getRasterY() * shadingCacheResolution);
         int        hash = hashfunc(cx, cy);
         CacheEntry e    = _shadingCache[hash & (_shadingCache.Length - 1)];
         if (e == null)
         {
             cacheEmptyEntryMisses++;
             return(null);
         }
         // entry maps to correct pixel
         if (e.cx == cx && e.cy == cy)
         {
             // search further
             for (Sample s = e.first; s != null; s = s.next)
             {
                 if (s.i != state.getInstance())
                 {
                     continue;
                 }
                 // if (s.prim != state.getPrimitiveID())
                 // continue;
                 if (s.s != shader)
                 {
                     continue;
                 }
                 if (state.getNormal().dot(s.nx, s.ny, s.nz) < 0.95f)
                 {
                     continue;
                 }
                 // we have a match
                 cacheHits++;
                 return(s.c);
             }
         }
         else
         {
             cacheWrongEntryMisses++;
         }
         return(null);
     }
 }
Beispiel #2
0
 private void addShadingCache(ShadingState state, IShader shader, Color c)
 {
     lock (lockObj)
     {
         // don't cache samples with null normals
         if (state.getNormal() == null)
         {
             return;
         }
         cacheEntryAdditions++;
         int        cx = (int)(state.getRasterX() * shadingCacheResolution);
         int        cy = (int)(state.getRasterY() * shadingCacheResolution);
         int        h  = hashfunc(cx, cy) & (_shadingCache.Length - 1);
         CacheEntry e  = _shadingCache[h];
         // new entry ?
         if (e == null)
         {
             e = _shadingCache[h] = new CacheEntry();
         }
         Sample s = new Sample();
         s.i = state.getInstance();
         // s.prim = state.getPrimitiveID();
         s.s  = shader;
         s.c  = c;
         s.nx = state.getNormal().x;
         s.ny = state.getNormal().y;
         s.nz = state.getNormal().z;
         if (e.cx == cx && e.cy == cy)
         {
             // same pixel - just add to the front of the list
             s.next  = e.first;
             e.first = s;
         }
         else
         {
             // different pixel - new list
             e.cx    = cx;
             e.cy    = cy;
             s.next  = null;
             e.first = s;
         }
     }
 }