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

Author Topic: Initial draw call slow  (Read 3460 times)

0 Members and 1 Guest are viewing this topic.

Mörkö

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Initial draw call slow
« on: March 09, 2016, 01:12:06 am »
Why does the first call to draw with some vertices cause a huge lag?

Example:
int main() {
    sf::RenderWindow window{sf::VideoMode{640, 480}, "SFML"};
    for (auto n : {1, 2, 3}) {
        Timer p{"Draw"};
        if (n > 1)  window.draw(sf::RectangleShape{});
        else        window.draw(sf::VertexArray{sf::Triangles});
    }
}

Typical output:
Draw 0.031ms
Draw 167.891ms
Draw 0.006ms

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Initial draw call slow
« Reply #1 on: March 09, 2016, 04:18:33 am »
Why does the first call to draw with some vertices cause a huge lag?
Isn't the vertex array being drawn before both of the rectangle shapes?
It actually seems to delay on the first rectangle shape drawn.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Mörkö

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: Initial draw call slow
« Reply #2 on: March 09, 2016, 05:18:01 am »
Yes that's what I was trying to show, that it doesn't lag on the empty draw call, then it does lag on the first, and only on the first rectangleshape draw call. More generally, the first call to draw some vertices.

I haven't looked into the source to see what happens when drawing a default rectangleshape, but I conjecture it draws some vertices because that would be consistent with how the lag appears in all other circumstances, I discovered it not by using rectangleshape but by using vertexarray.

Other example:
int main() {
    sf::RenderWindow window{sf::VideoMode{640, 480}, "SFML"};
    sf::VertexArray vs{sf::Points};
    vs.append(sf::Vertex{sf::Vector2f{0, 0}});
    for (auto n : {1, 2, 3}) {
        Timer p{"Draw"};
        if (n > 1)  window.draw(vs); // lag once on first call
        else        window.draw(sf::VertexArray{sf::Points}); // no lag
    }
}
« Last Edit: March 09, 2016, 05:33:23 am by Mörkö »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Initial draw call slow
« Reply #3 on: March 09, 2016, 08:00:43 am »
Many OpenGL states are configured at the first draw call (and often just reused for other calls), but the whole process is totally skipped if there's nothing to draw.

I haven't checked the code so I can't tell you exactly what happens, but you get the idea. If you're really interested, you can have a look at RenderTarget::draw(const Vertex*, ...).
Laurent Gomila - SFML developer

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: Initial draw call slow
« Reply #4 on: March 12, 2016, 02:54:39 pm »
to start up with: sfml is using opengl 1.2 for rendering. On big aomunt of gpus this version is not implemented: instead it is emulated with opengl 3.3 functionality.
Because of that, every call of glVetexPointer(which sfml does) causes upload of certex data to gpu memory or vram and I can say to you that this operation is not the fastest. SFML escapes this problem by using rendertarget cache, which helps it to not do unneeded openGL state changes.
To sum up with: if you had old gpu with opengl 1.1 implemented than there will be probably no lag.
« Last Edit: March 12, 2016, 02:59:37 pm by Mr_Blame »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Initial draw call slow
« Reply #5 on: March 12, 2016, 03:16:14 pm »
OpenGL 2.1*
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: AW: Initial draw call slow
« Reply #6 on: March 12, 2016, 03:21:54 pm »
OpenGL 2.1*
Okay but still glVertexPointer.

 

anything