SFML community forums

Help => Graphics => Topic started by: Qix on November 13, 2012, 05:17:29 am

Title: Debugging a text problem - 'invisible' text
Post by: Qix on November 13, 2012, 05:17:29 am
Alright, I'm stumped. It's been three days and I'm running out of options - I'm to the point of just trying random, seemingly pointless stuff just to get something to work.

I'm writing a program that binds to Lua and allows scripts to draw things to the canvas. The code is completely custom and is too large to simplify down (sorry Laurent, this one won't be easy).

I have tried running a bare-bones text drawing program, which, of course, works.

I have a font preloader (which does exactly what it sounds like - creates a font and loads from a file, stores it in an array, and then returns a raw pointer [Lua uses raw pointers]), as well as an image loader that works pretty much in the same way.

Images have a wrapper class, whereas the sf::Font class is bound directly to lua (no wrapper).

There is also a text class that lightly wraps the sf::Text class.

Text was working intermittently (certain, seemingly similar builds would show/hide text at random) until it just stopped working altogether. The text just seems to be completely invisible.

I have stepped through the program to the point my F10/11 keys are screaming for mercy, and all pointers/references/arguments/etc. seem fine. I have compared runtime break data for Fonts between the working sample program and the actual app - everything looks fine. There are absolutely no errors or exceptions, the text simply does not display.

The other weird thing is that images draw just fine. They also follow the same code-path as text does. It  seems to be an internal thing.

Here is a few code snippets to help visualize the code-path:

Button (which is what holds the text) instantiates the label (wrapper Text class):

Text                            _label;

which instantiates itself using the sf::Text default constructor. The button class then sets the label's string and calls the lua script to set itself up.

_label.setString(std::string("Test"));

-- Get Label
local label = self:GetLabel()
       
-- Set up font/color
label:SetColor(R.Color(255, 0, 0, 255))

-- _buttonFont is a font that was pre-loaded earlier in the script
self:GetLabel():SetFont(_buttonFont)

The above lua code works as far as I can tell; Even deeper into the draw functions, the color values are correct (255,0,0,255) and the font seems to be normal (the pointers are valid, the page count is 1 or so, and the pixel data is reasonable).

The code then continues on to draw, calling the lua callback with a reference to the target (which wraps a pointer to a rendertarget):

-- Draw image then text on top of it
--      Commented out image drawing; It works fine
--target:Draw(_buttonIdle)
target:Draw(self:GetLabel())

The target draw method is as follows:

ITarget&                                draw(Drawable& drawable)
{
        // Is it visible?
        if(!drawable.isVisible())
                return *this;

        // Init
        initStack();

        // Combine transform
        _transformStack->push(_transformStack->top());
        _transformStack->top().combine(drawable.getTransform());

        // Draw
        drawable.drawThis(*this);

        // Pop Transform
        _transformStack->pop();

        // Return
        return *this;
}

It is confirmed the drawable is visible (which is a method that belongs to a wrapper) and the transform stack (used to combine transforms for 'component'-ized drawables) is initialized and working fine.

The code goes on to draw the drawable's wrapper function, which calls the pure virtual method "onDraw" that actually does the drawing.

// Draw
target.drawRaw(*this);

drawRaw() simply calls another target wrapper draw method to call the SFML draw method itself:

// Copy + Combine states with transform
sf::RenderStates s = states;
s.transform.combine(_transformStack->top());

// Lock and load
lock();
_ref->draw(drawable, s);
unlock();
 

Again it pushes the transforms, etc. and locks (thread and GL states, including setting the target to be active) and draws.

This triggers the SFML sf::Text draw method. The m_font information at that point looks like this:
(http://gyazo.com/3c2939e32b1e866cb7731a869a6cde0e.png?1352780123)

Beyond that, everything seems fine. Like I said, the color is there, the font information looks fine, and with the help of notepad+watch lists, I've confirmed all pointers are as they should be.

I seriously don't know why text is just invisible.

---

My question isn't 'how do I fix this' or 'what's wrong with my code' (since condensing this to something simpler would be pointless) - but more of how can I begin to debug text drawing? I have already updated GLEW and Freetype, building from source, and I'm using the latest (as of a few hours ago) dev snapshot of SFML 2.0.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Laurent on November 13, 2012, 08:03:37 am
You can try an OpenGL debugger (gDebugger?), it will show you the current states and textures that are used for drawing.

You can also reduce your code to the minimum. Since a minimal code written from scratch doesn't reproduce the problem, instead try to remove irrelevant parts from your app, until you reach the minimum amount of code that still has the problem.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 13, 2012, 04:04:42 pm
You can try an OpenGL debugger (gDebugger?), it will show you the current states and textures that are used for drawing.

You can also reduce your code to the minimum. Since a minimal code written from scratch doesn't reproduce the problem, instead try to remove irrelevant parts from your app, until you reach the minimum amount of code that still has the problem.

Oh interesting, didn't know such a thing existed.

I will create a new branch and git and see what I come with.

Thank you Laurent!
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 13, 2012, 04:18:34 pm
Alright so it might be nothing, but with gDebugger, two things happened - one, the image texture is fine, but what I'm assuming is the font texture (the program only loads two and gDebugger is reported two) is completely invisible.

Two, I'm getting the error

Quote
Debug String: Detected error: The debugged process asked for an extension function pointer (glUseProgramObjectARB) from one render context, but called this function pointer in another render context (context #3)

Context #3 is where the two textures are being loaded.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Laurent on November 13, 2012, 04:39:10 pm
You can find whether error #2 is part of the problem or not, by not using shaders.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 13, 2012, 04:41:58 pm
Well unless there is some sort of build setting or something to not use shaders, my code doesn't use any shaders at this time, nor does it touch any sort of shader aspect of SFML.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Laurent on November 13, 2012, 04:53:22 pm
Ah, so I guess that the error refers to glUseProgramObjectARB(0) which is called once when you create a render-target.

So it probably has nothing to do with the problem.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 13, 2012, 05:07:06 pm
While I'm shaving down the problem, is there anywhere else I should be looking?
Title: Re: Debugging a text problem - 'invisible' text
Post by: Laurent on November 14, 2012, 07:54:34 am
I have no idea ;D
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 14, 2012, 05:11:07 pm
Is there a way to get gDebugger and Visual Studio to debug the same process at the same time.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 18, 2012, 06:14:45 am
This seems to be a bug with ATI. After almost a week of debugging and testing, I've tested the same program on multiple ATI, nVidia, and integrated Intel graphics cards. So far, every single ATI card has failed to show text, while every single nVidia/Intel integrated has worked.

It's going to be a while more before I can do any opengl debugging on ATI because CodeXL is refusing to work on any of the machines I'm testing on.

Really stressed out about this!

ATI Count: 3
nVidia Count: 3
Integrated Intel Count: 2


EDIT: Just stumbled onto this link (http://en.sfml-dev.org/forums/index.php?topic=9561). The white artifacts aren't showing up on mine, but I need to check out the beta of the catalyst driver.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 18, 2012, 11:01:27 pm
As of the latest freetype build and SFML 2.0 dev snapshot, the sample code (http://pastebin.com/K8ewJUP2) is not working. I've tried with two different fonts, one with both TTF and OTF versions.

Some results:

Working (AMD 6870)
(http://gyazo.com/aa2da6432857babbeddeb7500ec586a2.png)

Not working (ATI Mobility Radeon HD 5000 Series)
(http://gyazo.com/e3c458148056fbb691ddd023d6a300cf.png?1353275529)
Quote
The graphics driver installed in your computer returned information that shows that your graphics adapter should be able to run Topicscape.
-----------------------------------------------------------------------
Graphics Adapter:   Intel(R) HD Graphics
Manufacturer:   ATI Technologies Inc.
GL Version:   4.2.11991 Compatibility Profile Context
Resolution:   1920 X 1080 X 0 bpp
Driver version:   .
-----------------------------------------------------------------------
Windows Version: Windows 7 Professional
CPU: Intel(R) Core(TM) i5 CPU       M 480  @ 2.67GHz(2660MHz)
Physical Memory Usage: 1439/4095MB
Virtual Memory Usage: 4095/4095MB

Working (NVidia Gt630m)
Quote
-----------------------------------------------------------------------
Graphics Adapter: Intel(R) HD Graphics 4000
Manufacturer: Intel
GL Version: 4.0.0 - Build 9.17.10.2867
Resolution: 1920 X 1080 X 32 bpp
Driver version:
Video Memory Size: 2112 MB
-----------------------------------------------------------------------
Windows Version: Windows 8 Pro
CPU: Intel(R) Core(TM) i7-3610QM CPU @ 2.30GHz(2295MHz)
Physical Memory Usage: 4095/4095MB
Virtual Memory Usage: 4095/4095MB

Other working cards (Without info)


Should a bug report be filed?
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 19, 2012, 08:32:49 am
This seems to be a bug in freetype. I just implemented libdrawtext (which also uses freetype) and I get the same result.

Seeing as how Laurent is in the actual credits as a major contributor, hopefully he can have some insight on this.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Laurent on November 19, 2012, 08:48:05 am
Quote
This seems to be a bug in freetype
I don't think so. If the sf::Font's texture is properly filled with glyphs (which you can check with font.getTexture(size).copytoImage().saveToFile()), then FreeType did its job successfully. If it's a rendering issue, it's between SFML and your graphics driver. And I really think it's your driver (hint: it works on nVidia cards) ;)

Quote
Seeing as how Laurent is in the actual credits as a major contributor
A contributor of what? Where?
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 19, 2012, 08:54:50 pm
I don't think so. If the sf::Font's texture is properly filled with glyphs (which you can check with font.getTexture(size).copytoImage().saveToFile()), then FreeType did its job successfully. If it's a rendering issue, it's between SFML and your graphics driver. And I really think it's your driver (hint: it works on nVidia cards) ;)

GDebugger shows the glyphmap as invisible, as well as the saved image (which is attached).

It's freetype.

// DEBUG
        sf::Font fnt;
        if(!fnt.loadFromFile("sys/theme/rippl/fonts/MankSans.ttf"))
        {
                LOGW("Could not load manksans for debug");
        }
        else
        {
                fnt.getTexture(30).copyToImage().saveToFile("glyphsheet_dbg.png");
        }
 

UPDATE: Tried with the latest freetype snapshot (fa22ec1cfba5975c0622e374f8506c742d3f8d18) and the problem still persists.

A contributor of what? Where?

Oops, went back and looked - I think it was the jpeg/png library that had your name in the credit-comments ;) For some reason I was thinking it was freetype.

[attachment deleted by admin]
Title: Re: Debugging a text problem - 'invisible' text
Post by: Laurent on November 19, 2012, 09:01:10 pm
Quote
GDebugger shows the glyphmap as invisible, as well as the saved image.

It's freetype.
Ah, ok.

But a simple code works, so we still don't know if:
- your code contains an undefined behaviour that somehow messes up the font's texture
- your code contains something unusual that exhibits a bug in FreeType

Until you succeed to reproduce it with a simple example, you can't send a bug report to anyone.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 19, 2012, 09:40:12 pm
Laurent, check the link I posted earlier. It's a simple example. (http://pastebin.com/K8ewJUP2) The bug originally manifested in the full program, but the small-scale worked fine. Then after updating/building from source the small scale stopped working as well.

Already submitted a bug report (https://savannah.nongnu.org/bugs/index.php?37777#discussion), as suggested by the people in IRC.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Laurent on November 19, 2012, 10:41:57 pm
Quote
Laurent, check the link I posted earlier. It's a simple example. The bug originally manifested in the full program, but the small-scale worked fine. Then after updating/building from source the small scale stopped working as well.
Sorry, I totally missed it.

But if it works with some graphics cards, how could it be FreeType? FreeType doesn't use the graphics card at all, it only generates pixels, and SFML copies them to a texture and draws them with OpenGL.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 19, 2012, 10:58:03 pm
There is an ATI card where it works. I don't know, I'm so confused as to why it's working on some and not others - I knew that freetype loads the pixel data and that's it; has nothing to do with the graphics card.

Which is why this is so confusing. I would copy the build onto multiple computers and some would work and some wouldn't. All up to date graphics drivers. The loaded texture would be transparent on some and work on the others. Images worked on all of them, however.

I'm trying to build with the trace/error macros enabled on freetype (as they suggested in the bug report I sent off over there) - however, the entire library is a mess and it is requiring a ton of hacks just to get them to work.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Laurent on November 19, 2012, 11:09:48 pm
Quote
I'm trying to build with the trace/error macros enabled on freetype
Don't bother with that, just save the glyphs bitmaps directly to a file (before they are copied to the texture) and you'll see that they are all generated correctly.

At line 474 of Font.cpp, insert something that creates a sf::Image from &m_pixelBuffer[0], w, and h, and save it to a file (a different one for each call).

Trust me, this is most likely a driver bug, there were a lot of similar problems with AMD drivers recently. Look at which version of the driver the working computers use, and try the same version on yours.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 19, 2012, 11:18:08 pm
I will do that now.

Trust me, this is most likely a driver bug, there were a lot of similar problems with AMD drivers recently. Look at which version of the driver the working computers use, and try the same version on yours.

I did ;) There was another thread I think I mentioned earlier that said to upgrade to the .12 beta catalyst drivers. I did that; no beans.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Laurent on November 19, 2012, 11:29:02 pm
I was more thinking about an older version of the driver.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 19, 2012, 11:32:37 pm
I did what you said to do (put that image save in loadGlyph) and just to make sure I set a breakpoint. It never gets called.

Scratch that, I realize why it's not. I'll post back with the images in a sec.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 19, 2012, 11:59:52 pm
The sizes are right, the images are still transparent.

        // Write the pixels to the texture
        unsigned int x = glyph.textureRect.left + padding;
        unsigned int y = glyph.textureRect.top + padding;
        unsigned int w = glyph.textureRect.width - 2 * padding;
        unsigned int h = glyph.textureRect.height - 2 * padding;
               
                // DEBUG
                sf::Image img;
                img.create(w, h, &m_pixelBuffer[0]);
                std::stringstream ss;
                ss << "dbg_pxlbuf_save_" << _saveCount << ".png";
                if(!img.saveToFile(ss.str().c_str()))
                        printf("WARNING: Could not save debug pixel buffer to file\n");
                ++_saveCount;

        page.texture.update(&m_pixelBuffer[0], w, h, x, y);
 

[attachment deleted by admin]
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 20, 2012, 12:26:11 am
Raw pixel data for those is attached. I'm not sure of the format of the raws so photoshop is just spitting out garbage, but maybe they can be of use.

This is a file being opened and writing &m_pixelBuffer[0] (with size = m_pixelBuffer.size()).

[attachment deleted by admin]
Title: Re: Debugging a text problem - 'invisible' text
Post by: Laurent on November 20, 2012, 07:58:42 am
Quote
The sizes are right, the images are still transparent.
Hmm? Did you use the image preview of Windows explorer to watch them? ;D
They are perfectly fine, I can see that the glyphs are 'T, 'e', s', and 't'.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 20, 2012, 10:19:36 pm
Yes laurent... Then why can't I see them?

EDIT: FFS screen brightness was too high. Alright, so it's not freetype.

If images load in fine, why aren't these working? No matter the background color, they won't display. Lua is reporting their bounds to be fine, so it's not a polygon thing as far as I can tell. Further, there aren't any incorrect things in sf::Text such as the vertices being defined backwards... Images work, so should Text >.>
Title: Re: Debugging a text problem - 'invisible' text
Post by: Laurent on November 20, 2012, 10:28:54 pm
Driver issue :P
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 20, 2012, 10:33:29 pm
Driver issue :P

But that makes no sense if images can be loaded/displayed just fine. Aren't image textures the SAME THING as text textures?
Title: Re: Debugging a text problem - 'invisible' text
Post by: Laurent on November 20, 2012, 10:41:43 pm
They are. But the context in which they are used is different. And it's enough to exhibit a strange driver bug. Don't try to understand, there's nothing to understand. Graphics driver bugs are mystical, don't expect something obvious such as "texturing is broken", it's much more subtle.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 20, 2012, 10:45:25 pm
They are. But the context in which they are used is different. And it's enough to exhibit a strange driver bug. Don't try to understand, there's nothing to understand. Graphics driver bugs are mystical.

So should I file a bug report with ATI? I need a fix for this :P Is there any way to modify the text class to fix this? I've been sitting here twiddling my thumb for a week and a half and I'm like a crack addict on withdrawals I'm so desperate.

I don't care if I have to modify SFML itself for this project - what can I do to fix this now while waiting for a bugfix that might take months? This makes me upset seeing as how text works in every other OpenGL application I use, so it can't all be a driver bug.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Laurent on November 20, 2012, 10:55:55 pm
All you can do is to find what makes your code so special compared to the other examples that work. Once you've found that, you'll see if a workaround can be found.

Have you tried older versions of the driver? It may be the easiest solution for you, if you can find a version which works.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 20, 2012, 11:01:27 pm
All you can do is to find what makes your code so special compared to the other examples that work. Once you've found that, you'll see if a workaround can be found.

Have you tried older versions of the driver? It may be the easiest solution for you, if you can find a version which works.

None of the examples work. That is the problem.

I'll try older versions of the driver.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Laurent on November 20, 2012, 11:04:23 pm
You mean that sf::Text never works on your machine? So it's even simpler, just find what's different between sf::Text and sf::Sprite.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 20, 2012, 11:13:22 pm
You mean that sf::Text never works on your machine? So it's even simpler, just find what's different between sf::Text and sf::Sprite.

I thought sf::Text implemented sf::Sprite?
Title: Re: Debugging a text problem - 'invisible' text
Post by: Laurent on November 20, 2012, 11:15:17 pm
Quote
I thought sf::Text implemented sf::Sprite?
Nop, every high-level class directly uses a vertex array internally.
Title: Re: Debugging a text problem - 'invisible' text
Post by: Qix on November 21, 2012, 11:30:50 pm
Quote
I thought sf::Text implemented sf::Sprite?
Nop, every high-level class directly uses a vertex array internally.

Aha, that makes sense.