@@ -7,26 +7,9 @@ import { Font, arrayCommandsToObjects } from "../type/p5.Font";
7
7
function text ( p5 , fn ) {
8
8
RendererGL . prototype . maxCachedGlyphs = function ( ) {
9
9
// TODO: use more than vibes to find a good value for this
10
- return 200
10
+ return 200 ;
11
11
} ;
12
12
13
- RendererGL . prototype . freeGlyphInfo = function ( gi ) {
14
- const datas = [
15
- gi . strokeImageInfo . imageData ,
16
- gi . rowInfo . cellImageInfo . imageData ,
17
- gi . rowInfo . dimImageInfo . imageData ,
18
- gi . colInfo . cellImageInfo . imageData ,
19
- gi . colInfo . dimImageInfo . imageData ,
20
- ] ;
21
- for ( const data of datas ) {
22
- const tex = this . textures . get ( data ) ;
23
- if ( tex ) {
24
- tex . remove ( ) ;
25
- this . textures . delete ( data ) ;
26
- }
27
- }
28
- }
29
-
30
13
Font . prototype . _getFontInfo = function ( axs ) {
31
14
// For WebGL, a cache of font data to use on the GPU.
32
15
this . _fontInfos = this . _fontInfos || { } ;
@@ -792,7 +775,7 @@ function text(p5, fn) {
792
775
sh . setUniform ( "uMaterialColor" , curFillColor ) ;
793
776
gl . pixelStorei ( gl . UNPACK_PREMULTIPLY_ALPHA_WEBGL , false ) ;
794
777
795
- this . fontCache = this . fontCache || new Map ( ) ;
778
+ this . glyphDataCache = this . glyphDataCache || new Set ( ) ;
796
779
797
780
try {
798
781
// fetch the glyphs in the line of text
@@ -801,20 +784,35 @@ function text(p5, fn) {
801
784
for ( const glyph of glyphs ) {
802
785
const gi = fontInfo . getGlyphInfo ( glyph ) ;
803
786
if ( gi . uGlyphRect ) {
787
+ const rowInfo = gi . rowInfo ;
788
+ const colInfo = gi . colInfo ;
804
789
805
- const cacheKey = JSON . stringify ( { font : font . id , axs, glyph : glyph . shape . g } ) ;
806
- // Bump this font to the end of the cache list by deleting and re-adding it
807
- this . fontCache . delete ( cacheKey ) ;
808
- this . fontCache . set ( cacheKey , gi ) ;
809
- if ( this . fontCache . size > this . maxCachedGlyphs ( ) ) {
810
- const keyToRemove = this . fontCache . keys ( ) . next ( ) . value ;
811
- const val = this . fontCache . get ( keyToRemove ) ;
812
- this . fontCache . delete ( keyToRemove ) ;
813
- this . freeGlyphInfo ( val ) ;
790
+ // Bump the resources for this glyph to the end of the cache list by deleting and re-adding
791
+ const glyphResources = [
792
+ gi . strokeImageInfo . imageData ,
793
+ rowInfo . cellImageInfo . imageData ,
794
+ rowInfo . dimImageInfo . imageData ,
795
+ colInfo . cellImageInfo . imageData ,
796
+ colInfo . dimImageInfo . imageData
797
+ ] ;
798
+ for ( const resource of glyphResources ) {
799
+ this . glyphDataCache . delete ( resource ) ;
800
+ this . glyphDataCache . add ( resource ) ;
801
+ }
802
+
803
+ // If we have too many glyph textures, remove the least recently used
804
+ // ones from GPU memory. The data still exists on the CPU and will be
805
+ // re-uploaded if it gets actively used again.
806
+ while ( this . glyphDataCache . size > this . maxCachedGlyphs ( ) ) {
807
+ const data = this . glyphDataCache . values ( ) . next ( ) . value ;
808
+ this . glyphDataCache . delete ( data ) ;
809
+ const tex = this . textures . get ( data ) ;
810
+ if ( tex ) {
811
+ tex . remove ( ) ;
812
+ this . textures . delete ( data ) ;
813
+ }
814
814
}
815
815
816
- const rowInfo = gi . rowInfo ;
817
- const colInfo = gi . colInfo ;
818
816
sh . setUniform ( "uSamplerStrokes" , gi . strokeImageInfo . imageData ) ;
819
817
sh . setUniform ( "uSamplerRowStrokes" , rowInfo . cellImageInfo . imageData ) ;
820
818
sh . setUniform ( "uSamplerRows" , rowInfo . dimImageInfo . imageData ) ;
0 commit comments