Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Text rendering broken on my machine when using sf::Shape  (Read 26052 times)

0 Members and 1 Guest are viewing this topic.

opatut

  • Newbie
  • *
  • Posts: 25
    • View Profile
Text rendering broken on my machine when using sf::Shape
« on: November 25, 2012, 06:30:24 pm »
This is what I have reduced my problem down to (example code). [Sorry, uses Linux font path, edit to provide your own font for testing.]

I am trying to render text as always using SFML 2.0-rc1 with an sf::Text in an sf::RenderWindow with a specified sf::Font (see the code). On my machine, I get two different results:

First frame, good


Following frames, bad


All the other machines I tested work fine, they all show the stuff from my first frame in all frames. I am running ArchLinux with an ATI graphics card and catalyst drivers and/or mesa, I tested on several machines with Intel/NVidia chips. I sadly do not have another ATI card to test with.

The problem does not occur when I do not draw the shapes, or when I draw the text first and the shapes afterwards. It does not matter whether they are rectangles, circles, have alpha values or not. Also, only drawing one shape and the text works, more than one (i.e. two and up) break the text.

I have no idea where this may come from or how to solve it. Probably it is a driver problem (ATI on linux, yay) or some OpenGL states that are not reset correctly. I would appreciate any help :)
« Last Edit: November 25, 2012, 07:10:28 pm by opatut »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text rendering broken on my machine when using sf::Shape
« Reply #1 on: November 25, 2012, 07:09:15 pm »
Quote
Probably it is a driver problem
Most likely. There were a lot of similar issues with ATI drivers recently (you can search on this forum, there are at least 3 or 4 topics).
Laurent Gomila - SFML developer

opatut

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Text rendering broken on my machine when using sf::Shape
« Reply #2 on: November 25, 2012, 07:18:35 pm »
No luck with LIBGL_ALWAYS_SOFTWARE=1 either - which should mean it is not a catalyst problem, but either an error in the fglrx opengl implementation or SFML. Can you imagine what the problem might be? Seems like the glyph sizes are corrent (so the font is being used as normal), only the glyph rendering fails... is SFML using the old fixed pipeline or custom shaders for rendering text?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text rendering broken on my machine when using sf::Shape
« Reply #3 on: November 25, 2012, 08:31:25 pm »
Can you try the latest sources?
Laurent Gomila - SFML developer

opatut

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Text rendering broken on my machine when using sf::Shape
« Reply #4 on: November 26, 2012, 08:18:57 pm »
Yes, error still occurs with the current git master :(

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text rendering broken on my machine when using sf::Shape
« Reply #5 on: November 26, 2012, 08:50:05 pm »
So it's most likely a driver issue.
Laurent Gomila - SFML developer

opatut

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Text rendering broken on my machine when using sf::Shape
« Reply #6 on: November 26, 2012, 08:56:48 pm »
How comes all other libraries render text just fine? While I agree with you on that it is a driver-specific issue, the driver can definitely be used to render text as usual. It must be a problem with the combination of fglrx + SFML, which will probably not be concerned about int fglrx, making SFML the point where it should be addressed.

I would really appreciate this to be fixed real soon, as it probably does not only occur on my machine, and it can definitely be done correctly. Unfortunately I do not understand the problem, so I cannot help fixing it, only provide a testing machine.

Another interesting fact: I have no problem when drawing a sprite instead of a shape, even though one would expect a sprite to work exactly like a shape, only with texture handling on top... I'll keep investigating.

And one more thing: when I create the sf::Text only once before the loop, it is rendered correctly in all frames. Seems like there is a problem when rendering the text to a buffer texture (SFML is doing this, right), not when rendering it out onto the target.

Even more interesting: when rendering the font's texture directly into a Sprite (see code below), I have a nice glyph page in the first frame, and a white square in the following. This happens even when I only create and modify both the font and the sprite before the main loop, so it means the font's texture is being corrupted somehow by something that should not even touch it. And it does not seem to be the sf::Text class, since I can completely uncomment that one, giving me an empty glyph page (only a small white dot in the upper left corner) in the first frame, and again the big white square in the following ones. Uncommenting the draw command of the rectangle(s) however leaves the font texture untouched. Now that is weird, since shapes really don't have anything to do with fonts. My guess: does the shape class "construct" a white texture to use the same rendering mechanisms as the sprite class, but overwrites the font glyph page texture in this case?

sf::Sprite sprite(font.getTexture(20));

And more edits: when I use the font texture twice (e.g. render 2 sf::Texts or render one sf::Text and the sf::Sprite with the font's texture) the second (and third and probably all following) render works fine and displays the correct texture. Only the first render after 2 shapes is broken (so yes, I can break the above chain by drawing two shapes in between), these 2 shapes can be separated by other drawing calls to keep it working (either of sf::Text or sf::Sprite with other texture). Rendering another object (e.g. a test sprite) between the shapes and the text fixes the text, the other object however is not being rendered at all. [Hope you understand all of this ;)]

Shape 1, Shape 2, Text // broken text
Shape 1, Shape 2, Sprite, Shape 3, Text  // renders Shape 1-3 and Text, but not Sprite
Shape 1, Sprite, Shape 2, Text // renders everything

Wow, this is confusing. But from my observations above I guess one can see how complicated this problem is, and that is probably has something to do with how textures are handled in SFML.
« Last Edit: November 26, 2012, 10:06:23 pm by opatut »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text rendering broken on my machine when using sf::Shape
« Reply #7 on: November 26, 2012, 10:42:27 pm »
Quote
Wow, this is confusing. But from my observations above I guess one can see how complicated this problem is, and that is probably has something to do with how textures are handled in SFML.
To be honest, I don't think so. Because:
1. there have been really a lot of similar problems recently caused by those crappy ATI drivers (have you tried to install a different version by the way?)
2. SFML text rendering works fine for everyone else

If I was you I wouldn't bother trying to understand what happens, because there's nothing to understand. ATI drivers are crappy and cause all kinds of weird bugs. Have a closer look at this forum if you don't trust me.
Laurent Gomila - SFML developer

opatut

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Text rendering broken on my machine when using sf::Shape
« Reply #8 on: November 26, 2012, 11:00:29 pm »
I completely trust you, but... there must be a way to avoid this, even if it means some minor hacks. And since I am really not the only one with an ATI graphics card, I guess it is somehow of interest for a (possibly small but existent) group of people who share this common problem. I don't expect you, Laurent, to fix this right now or in foreseeable future, but I think it should be worked upon in general.

I am not trying to convince you that this is the most important problem in SFML 2 right now, nor do I think switching the driver is a satisfying solution.

Quote
Wow, this is confusing. But from my observations above I guess one can see how complicated this problem is, and that is probably has something to do with how textures are handled in SFML.
To be honest, I don't think so.

I am not saying it is your (or SFML's) fault, only it has to do with texture handling in SFML. I've been using SFML for a very long time now, and I admire basically all about SFML. I really believe myself ATI drivers are causing the problem in the first place, but there is not much I or you can do about it. But seeing a lot of other libraries and opengl software working fine, there is definitely another way to do it, which works with the current OpenGL implementation of the ATI drivers.

This is basically my reason for discussing here: I can't go any deeper than SFML at this problem. I could resolve the problem using my above investigations, but only for myself, and the solution would not be satisfying at all. What I do instead is posting here, initiating a discussing with the developers of SFML (you) and users of it, hoping to find a one-fits-all solution for this problem. I could not go onto ATI driver level with this problem, I honestly don't know shit about it and wouldn't even know where to start asking questions or who to address. So I went as deep as I could, at a level where I think the problem could be solved.

I am still happy about suggestions what might probably go wrong here -  and hopefully how to fix this.

opatut

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Text rendering broken on my machine when using sf::Shape
« Reply #9 on: November 26, 2012, 11:14:28 pm »
And more observations:

When the font glyph page initializes its texture, it sets the top left 2x2 pixels to white (for the underline style), see src/SFML/Graphics/Font.cpp:581. When I set the top left color to something else (i.e. setPixel(0, 0, Color::Red); ) , the "wrong" square and all the wrong glyph rectangles have this color (red in this case)... Are probably the texture coords broken?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Text rendering broken on my machine when using sf::Shape
« Reply #10 on: November 26, 2012, 11:35:23 pm »
Try copy/paste text.hpp and cpp and change namespace to avoid nameclash and put lots of break points in updateGeometry to see if glyps bounds have nonsensical values.
« Last Edit: November 26, 2012, 11:37:36 pm by FRex »
Back to C++ gamedev with SFML in May 2023

opatut

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Text rendering broken on my machine when using sf::Shape
« Reply #11 on: November 27, 2012, 12:39:03 am »
I am working directly in SFML code, but anyway.... seems like the texture coordinates are fine, for the first quad I get (0,2) (14x17) on both the broken and the correct glyph...

I also found a workaround by enabling the vertex cache for all cases, even when we have more than 4 vertices. Did this by replacing RenderTarget.cpp:165 with

// bool useVertexCache = (vertexCount <= StatesCache::VertexCacheSize);
bool useVertexCache = true;

I still don't know what is going wrong, but like this it works. I will see into more detail what the vertex cache is doing tomorrow.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text rendering broken on my machine when using sf::Shape
« Reply #12 on: November 27, 2012, 08:19:37 am »
Quote
I also found a workaround by enabling the vertex cache for all cases
Hum... are you really sure?
I was expecting a problem with texture coordinates, and the vertex caches changes nothing to texture coordinates; and more important: storing more than 4 vertices in it is undefined behaviour because it is declared as an array of 4 sf::Vertex...
Laurent Gomila - SFML developer

opatut

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Text rendering broken on my machine when using sf::Shape
« Reply #13 on: November 27, 2012, 02:25:51 pm »
Hum... are you really sure?
Yes, pretty sure. I reset my clone to master and changed only the one line, and it worked.

I was expecting a problem with texture coordinates, and the vertex caches changes nothing to texture coordinates; and more important: storing more than 4 vertices in it is undefined behaviour because it is declared as an array of 4 sf::Vertex...

[I wrote a lot of stuff here, then I found you told me storing more than 4 vertices was undefined behaviour, and so I removed it again... I was thinking stupid :P]

Also, I noticed that Rectangles (and Shapes in general) use triangles (a rectangle has 6 vertices), and sprites and font glyphs only 4. Maybe that is where it breaks...
« Last Edit: November 27, 2012, 02:38:56 pm by opatut »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Text rendering broken on my machine when using sf::Shape
« Reply #14 on: November 27, 2012, 02:46:01 pm »
Quote
Also, I noticed that Rectangles (and Shapes in general) use triangles (a rectangle has 6 vertices), and sprites and font glyphs only 4. Maybe that is where it breaks...
If it was that, sprites wouldn't work either on your PC.

If only text is broken, the cause is not that trivial.
Laurent Gomila - SFML developer

 

anything