gsKit fontm problem and broken arial.fnt

Discuss the development of software, tools, libraries and anything else that helps make ps2dev happen.

Moderators: cheriff, Herben

Post Reply
Mega Man
Posts: 260
Joined: Sat Jun 18, 2005 3:14 am
Contact:

gsKit fontm problem and broken arial.fnt

Post by Mega Man »

Hello,

I have a problem with gsKit and fontm. I can't print the following string correctly: "host:initrd.gz".
The character "d" is replaced by a strange letter (I think this is japanese).
On every second frame the character "g" is replaced by a dot.
I used the fontm example in gsKit and changed the main loop to:

Code: Select all

while(1)
{
        gsKit_clear(gsGlobal, White);
        gsKit_font_print_scaled(gsGlobal, gsFont, 50, 50, 3, 0.85f, TexCol,
                "host:initrd.gz");
        gsKit_sync_flip(gsGlobal);
        gsKit_queue_exec(gsGlobal);
}
When I remove the characters "." and ":", it is working.

There is a second problem:
The font example is not working, because the file "arial.fnt" is broken. When I start it, I get a page fault. When I replace all linux line ends by DOS line ends in "arial.fnt", I got at least a broken font on the display.
ragnarok2040
Posts: 202
Joined: Wed Aug 09, 2006 1:00 am

Post by ragnarok2040 »

I experienced the same issue with arial.fnt and lucida.fnt and fontm. Reverting to an earlier version of gsKit fixed it though, so I don't think the .fnt files are bad, but perhaps converting from the fio* based functions to stdio file functions is giving different output than what the original code expects. I'm not actually sure why it was changed.. since the fio* functions support "pfs#" based paths just as well as "mc#" and "mass". I never did look into it much since I went a different direction. Are you using the latest revision? A patch recently went in that added checks to see if a FILE pointer was NULL before trying to read from it, which fixed the exception I was getting... though it was weird since I was giving the fopen() function a valid path from the very beginning, at least I remember thinking that I was. It was a bit ago, now.

For the fontm bug, I never found a fix for the recent revision, only a workaround for revision 1386, which didn't actually work well for me. Polo35 mentioned that setting "uploaded = 0;" to "uploaded = 1;" in the gsKit_font_print_scaled() function and then copying and pasting an extra gsKit_texture_send_inline() call reduced the glitch significantly but the gsKit code has changed a lot since then so it no longer applies. He thought maybe the prim_sprite() call was a little too fast after the send_inline() call. I noticed the glitch tended to be less noticeable with the more burdens placed on the GS. The problem I was getting involved c/x (ascii 63/78 fontm 3/233) replacement, and . with a small | (ascii 46/124 fontm 4/47, I believe, but I think it wasn't a ascii mapped fontm character) and a few other anomalies.
ps2devman
Posts: 259
Joined: Mon Oct 09, 2006 3:56 pm

Post by ps2devman »

Try avoiding any dma activity while displaying font.
Sounds strange, but issue cause is strange anyway.
Mega Man
Posts: 260
Joined: Sat Jun 18, 2005 3:14 am
Contact:

Post by Mega Man »

Yes, I am using the latest version.
I see that fread is failing. It seems that the calculation for the variable "size" is wrong. When I remove the check for the command "fread(gsFont->Texture->Mem, size, 1, File)", it is working.
I think the code for fontm is very strange. It is very difficult to understand, how the textures are uploaded. I believe that the wrong fontm page is selected. Why is "aligned" the key for finding a texture line, "idxoffset" would be make more sense.
Mega Man
Posts: 260
Joined: Sat Jun 18, 2005 3:14 am
Contact:

Post by Mega Man »

I looked over the code and now I am understanding what the code does. There are two pages in the VRAM used for the font. The font is not compeltely uploaded, but uploaded in small blocks. The blocks are stored in the 2 pages. The commands for uploading and drawing a char are put into a DMA queue. The queue is executed in FIFO order. So it is possible to reuse each page after a char is drawn. But I remember that I read somewhere that the GS has something like a cache. In the manual is written that a cache is used at least for the palette (CLUT is for every char the same). The chars are very small and would fit into the smallest cache and when memory is reused, we need a cache flush.
So my solution is to use the page which is not recently used for the last char. My patch is as follows:

Code: Select all

--- ee/gs/src/gsFont.c.orig
+++ ee/gs/src/gsFont.c
@@ -708,6 +708,7 @@ void gsKit_font_print_scaled(GSGLOBAL *g
                     if (gsFont->FontM_LastPage[pgindx] == aligned)
                     {
                         gsFont->Texture->Vram = gsFont->FontM_Vram[pgindx];
+                        gsFont->FontM_VramIdx = (pgindx + 1) % GS_FONT_PAGE_COUNT;
                         break;
                     }
                 }
I patched the place where it looks if there is already a block uploaded including the current char. The code part which uploads a font block, already includes the added line. Now I see that "aligned" can be used as a key detect this situation, because it is unique for each block as "idxoffset" is.
I don't know if this really fix the problem, but it seems to work.
ooPo
Site Admin
Posts: 2023
Joined: Sat Jan 17, 2004 9:56 am
Location: Canada
Contact:

Post by ooPo »

I've added this fix to the repository.

Code: Select all

Sending        ee/gs/src/gsFont.c
Transmitting file data .
Committed revision 1449.
ps2devman
Posts: 259
Joined: Mon Oct 09, 2006 3:56 pm

Post by ps2devman »

Wow! Nice brain, Mega man! Grats on finding that fix!
Neovanglist
Site Admin
Posts: 72
Joined: Sat May 22, 2004 9:29 pm
Location: Copenhagen, Denmark
Contact:

Post by Neovanglist »

Thanks!

Yea, I added this vram page feature later for fontm and didn't do extensive testing with other fonts... thus creating this timing issue.

I really appreciate the patch :)
Regards,
Neovanglist
Mega Man
Posts: 260
Joined: Sat Jun 18, 2005 3:14 am
Contact:

Post by Mega Man »

I detected that the texture cache is bigger and my patch didn't fix it. It just reduces the error rate.
So I was looking at the GS manual. There must be a flush function and I found it. We need just to access TEXFLUSH register.
I was suprised as I saw that this is already done in the texture uploading function, but I was still sure that the error must be here.
So I checked the access against the other GS register accesses and I saw the order is incorrect.

The original code does:

*p_data++ = GS_TEXFLUSH;
*p_data++ = 0;

Other code put first the value in and then the register, so I changed it to:

*p_data++ = 0;
*p_data++ = GS_TEXFLUSH;

This solves the problem!

The bug is in 2 functions:
gsKit_texture_send
gsKit_texture_send_inline

We should leave my old patch in the code, because an LRU algorithm should lead to less uploads.
cosmito
Posts: 307
Joined: Sun Mar 04, 2007 4:26 am
Location: Portugal
Contact:

Post by cosmito »

Wow Mega Man

That solved (almost) the glitches I found - see http://forums.ps2dev.org/viewtopic.php?t=9012.

There is still sometimes a glitch but I was getting also some crazy letters (some charaters changing during display) and that was solved.

Thanks!
cosmito
Posts: 307
Joined: Sun Mar 04, 2007 4:26 am
Location: Portugal
Contact:

Post by cosmito »

I guess the changes Mega Man pointed should go into the repository. Anyone with commit rights would like to take the task?
LBGSHI
Posts: 136
Joined: Mon Aug 07, 2006 5:56 am
Contact:

Post by LBGSHI »

Agreed; though I don't have commit rights (Oobles was going to create an account for me, but I suppose never got around to it...).
I may be lazy, but I can...zzzZZZzzzZZZzzz...
ragnarok2040
Posts: 202
Joined: Wed Aug 09, 2006 1:00 am

Post by ragnarok2040 »

Since I've just got done merging ntba2's libjpg interface with dlanor's and Polo's changes with libjpeg 6b-14 (with debian's patches) and a C++ header fix, I need to modify gsKit's Makefile.eeglobal to build with it so I can add the fixes while I'm doing that and make a patch. I'll investigate the reason the arial.fnt example keeps crashing too and see if I can't fix that as well :D.

Found it, division bug in fread...

Code: Select all

ret += _ps2sdk_read(stream->fd, buf, read_len) / r;
but should have been

Code: Select all

ret += _ps2sdk_read(stream->fd, buf, read_len) / n;
which caused the fread() function to return 0 or 1. Before it was number of bytes read divided by the size to be read, and after it's number of bytes read divided by number of records to be read of size r.

The source I just checked out still has all the fio* functions in place of the generic _ps2sdk_* symbols for both fileXio* and fio* functions in stdio.c file functions.

radad released a patch some time ago for fileXio_rpc.c and stdio.c to fix that:
http://forums.ps2dev.org/viewtopic.php?t=4050

I plan on expanding that patch to include _ps2sdk_remove* as well though.
ragnarok2040
Posts: 202
Joined: Wed Aug 09, 2006 1:00 am

Post by ragnarok2040 »

I consolidated all of my updates into a single archive at http://homebrew.thewaffleiron.net/ragnarok2040.The updates.tar.gz package has the _ps2sdk_* stdio symbols for linking against fileXio and fileio, that updated crt0.s that was posted by chp which I've been using extensively, the last fix for gsKit by MegaMan plus a makefile.eeglobal change for libjpeg, libpng-1.2.33, zlib-1.2.3, libjpeg-6b-14 which has debian's patches with ntba2's alignment fix and ntba2's interface functions with Polo and dlanor's additions plus a C++ fix for the libjpg header,

All the gsKit examples work fine for me now :D.
ooPo
Site Admin
Posts: 2023
Joined: Sat Jan 17, 2004 9:56 am
Location: Canada
Contact:

Post by ooPo »

Code: Select all

Sending        ps2sdk/ee/libc/src/stdio.c
Sending        ps2sdk/ee/rpc/filexio/src/fileXio_rpc.c
Sending        ps2sdk/ee/startup/src/crt0.s
Transmitting file data ...
Committed revision 1466.

Code: Select all

Sending        gsKit/Makefile.global
Sending        gsKit/ee/gs/src/gsTexture.c
Transmitting file data ..
Committed revision 1467.
Committed to the repository. Enjoy!
ragnarok2040
Posts: 202
Joined: Wed Aug 09, 2006 1:00 am

Post by ragnarok2040 »

Oops, I had forgotten to add a subdirectory to my local copy of libjpeg using 'svn add' so 'svn diff' didn't include it in the libjpeg patch. I guess I just overlooked the size of the patch. I checked out the latest revision and made a new patch for it with the full update this time called libjpeg.diff.tar.gz. I also included it in updates.tar.gz. Sorry about that :/.

Edit:
fread() actually was fine, so I'll submit a patch to revert my change.I thought it was a division precision error but I guess it wasn't :D. gsKit_texture_fnt() tries to read in 262144 bytes of data (the size of the texture) but there's only 262143 bytes to be read, so fread() returns 0 like it ought have.

Code: Select all

Index: ps2sdk/ee/libc/src/stdio.c
===================================================================
--- ps2sdk/ee/libc/src/stdio.c	(revision 1467)
+++ ps2sdk/ee/libc/src/stdio.c	(working copy)
@@ -752,7 +752,7 @@
         read_len--;
       }
       //ret += fioRead(stream->fd, buf, read_len) / r;
-      ret += _ps2sdk_read(stream->fd, buf, read_len) / n;
+      ret += _ps2sdk_read(stream->fd, buf, read_len) / r;
   }
   return (ret);
 }
As a side note, does anyone know what type of font those .fnt files are? The way gsKit_texture_fnt() reads the header on them doesn't conform to the spec of windows .fnts. I can't get fontforge to recognize the file type either and it supports a good range. From the looks of it, it seems to be something like this http://giesler.biz/bjoern/downloads/dsg ... format.txt.

This patch just lets gsKit work with the bad font read until a better fix is made.

Code: Select all

Index: gsKit/ee/gs/src/gsTexture.c
===================================================================
--- gsKit/ee/gs/src/gsTexture.c	(revision 1467)
+++ gsKit/ee/gs/src/gsTexture.c	(working copy)
@@ -774,8 +774,7 @@
 	
 	if&#40;fread&#40;gsFont->Texture->Mem, size, 1, File&#41; <= 0&#41;
 	&#123;
-		printf&#40;"Could not load font&#58; %s\n", gsFont->Path&#41;;
-		return -1;
+		printf&#40;"Font might be bad&#58; %s\n", gsFont->Path&#41;;
 	&#125;
 	fclose&#40;File&#41;;
 
Last edited by ragnarok2040 on Tue Nov 11, 2008 12:15 am, edited 1 time in total.
ooPo
Site Admin
Posts: 2023
Joined: Sat Jan 17, 2004 9:56 am
Location: Canada
Contact:

Post by ooPo »

Code: Select all

Sending        libjpeg/src/Makefile
Adding         libjpeg/src/jpeg-6b
Adding         libjpeg/src/jpeg-6b/debian
Adding         libjpeg/src/jpeg-6b/debian/changelog
Adding         libjpeg/src/jpeg-6b/debian/copyright
Adding         libjpeg/src/jpeg-6b/debian/libjpeg62-dev.README.Debian
Adding         libjpeg/src/jpeg-6b/docs
Adding         libjpeg/src/jpeg-6b/docs/jconfig.doc
Adding         libjpeg/src/jpeg-6b/docs/libjpeg.doc
Adding         libjpeg/src/jpeg-6b/docs/structure.doc
Adding         libjpeg/src/jpeg-6b/jcapimin.c
Adding         libjpeg/src/jpeg-6b/jcapistd.c
Adding         libjpeg/src/jpeg-6b/jccoefct.c
Adding         libjpeg/src/jpeg-6b/jccolor.c
Adding         libjpeg/src/jpeg-6b/jcdctmgr.c
Adding         libjpeg/src/jpeg-6b/jchuff.c
Adding         libjpeg/src/jpeg-6b/jchuff.h
Adding         libjpeg/src/jpeg-6b/jcinit.c
Adding         libjpeg/src/jpeg-6b/jcmainct.c
Adding         libjpeg/src/jpeg-6b/jcmarker.c
Adding         libjpeg/src/jpeg-6b/jcmaster.c
Adding         libjpeg/src/jpeg-6b/jcomapi.c
Adding         libjpeg/src/jpeg-6b/jcparam.c
Adding         libjpeg/src/jpeg-6b/jcphuff.c
Adding         libjpeg/src/jpeg-6b/jcprepct.c
Adding         libjpeg/src/jpeg-6b/jcsample.c
Adding         libjpeg/src/jpeg-6b/jctrans.c
Adding         libjpeg/src/jpeg-6b/jdapimin.c
Adding         libjpeg/src/jpeg-6b/jdapistd.c
Adding         libjpeg/src/jpeg-6b/jdatadst.c
Adding         libjpeg/src/jpeg-6b/jdatasrc.c
Adding         libjpeg/src/jpeg-6b/jdcoefct.c
Adding         libjpeg/src/jpeg-6b/jdcolor.c
Adding         libjpeg/src/jpeg-6b/jdct.h
Adding         libjpeg/src/jpeg-6b/jddctmgr.c
Adding         libjpeg/src/jpeg-6b/jdhuff.c
Adding         libjpeg/src/jpeg-6b/jdhuff.h
Adding         libjpeg/src/jpeg-6b/jdinput.c
Adding         libjpeg/src/jpeg-6b/jdmainct.c
Adding         libjpeg/src/jpeg-6b/jdmarker.c
Adding         libjpeg/src/jpeg-6b/jdmaster.c
Adding         libjpeg/src/jpeg-6b/jdmerge.c
Adding         libjpeg/src/jpeg-6b/jdphuff.c
Adding         libjpeg/src/jpeg-6b/jdpostct.c
Adding         libjpeg/src/jpeg-6b/jdsample.c
Adding         libjpeg/src/jpeg-6b/jdtrans.c
Adding         libjpeg/src/jpeg-6b/jerror.c
Adding         libjpeg/src/jpeg-6b/jerror.h
Adding         libjpeg/src/jpeg-6b/jfdctflt.c
Adding         libjpeg/src/jpeg-6b/jfdctfst.c
Adding         libjpeg/src/jpeg-6b/jfdctint.c
Adding         libjpeg/src/jpeg-6b/jidctflt.c
Adding         libjpeg/src/jpeg-6b/jidctfst.c
Adding         libjpeg/src/jpeg-6b/jidctint.c
Adding         libjpeg/src/jpeg-6b/jidctred.c
Adding         libjpeg/src/jpeg-6b/jinclude.h
Adding         libjpeg/src/jpeg-6b/jmemmgr.c
Adding         libjpeg/src/jpeg-6b/jmemnobs.c
Adding         libjpeg/src/jpeg-6b/jmemsys.h
Adding         libjpeg/src/jpeg-6b/jpegint.h
Adding         libjpeg/src/jpeg-6b/jquant1.c
Adding         libjpeg/src/jpeg-6b/jquant2.c
Adding         libjpeg/src/jpeg-6b/jutils.c
Adding         libjpeg/src/jpeg-6b/jversion.h
Adding         libjpeg/src/jpeg-6b/samples
Adding         libjpeg/src/jpeg-6b/samples/cderror.h
Adding         libjpeg/src/jpeg-6b/samples/cdjpeg.c
Adding         libjpeg/src/jpeg-6b/samples/cdjpeg.h
Adding         libjpeg/src/jpeg-6b/samples/cjpeg.c
Adding         libjpeg/src/jpeg-6b/samples/ckconfig.c
Adding         libjpeg/src/jpeg-6b/samples/djpeg.c
Adding         libjpeg/src/jpeg-6b/samples/example.c
Adding         libjpeg/src/jpeg-6b/samples/jpegtran.c
Adding         libjpeg/src/jpeg-6b/samples/rdbmp.c
Adding         libjpeg/src/jpeg-6b/samples/rdcolmap.c
Adding         libjpeg/src/jpeg-6b/samples/rdgif.c
Adding         libjpeg/src/jpeg-6b/samples/rdjpgcom.c
Adding         libjpeg/src/jpeg-6b/samples/rdppm.c
Adding         libjpeg/src/jpeg-6b/samples/rdrle.c
Adding         libjpeg/src/jpeg-6b/samples/rdswitch.c
Adding         libjpeg/src/jpeg-6b/samples/rdtarga.c
Adding         libjpeg/src/jpeg-6b/samples/transupp.c
Adding         libjpeg/src/jpeg-6b/samples/transupp.h
Adding         libjpeg/src/jpeg-6b/samples/wrbmp.c
Adding         libjpeg/src/jpeg-6b/samples/wrgif.c
Adding         libjpeg/src/jpeg-6b/samples/wrjpgcom.c
Adding         libjpeg/src/jpeg-6b/samples/wrppm.c
Adding         libjpeg/src/jpeg-6b/samples/wrrle.c
Adding         libjpeg/src/jpeg-6b/samples/wrtarga.c
Transmitting file data .....................................................................................
Committed revision 1468.

Code: Select all

Sending        ps2sdk/ee/libc/src/stdio.c
Transmitting file data .
Committed revision 1469.

Code: Select all

Sending        gsKit/ee/gs/src/gsTexture.c
Transmitting file data .
Committed revision 1470.
All updated. Anything else? :)
ragnarok2040
Posts: 202
Joined: Wed Aug 09, 2006 1:00 am

Post by ragnarok2040 »

That should do it. I'll see if I can't fix the font code a bit. :D
Post Reply