BlockEngine hat schlechte Performance

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 17 Antworten in diesem Thema. Der letzte Beitrag () ist von Twometer.

    BlockEngine hat schlechte Performance

    Hi Leute,
    ich bin dabei, eine Minecraft-Renderengine zu programmieren. Leider bekomme ich, wenn ich nach vorne gucke, also sehr viele Blöcke in meinem Sichtfeld habe und 3x10x3 Blöcke rendere, nur so um die 8-10fps. Ich hab es bereits so, dass ich nur mein Sichtfeld rendere (Frustum) und, dass die nicht sichtbaren Vertices zwischen den Blöcken nicht gerendert werden. Trotzdem hab ich so ne schlechte Performance. Habt ihr da Ideen wie ich das verbessern könnte?
    Ich benutze OpenTK als Renderer. Mein Code zum Rendern ist der hier:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports OpenTK
    2. Imports OpenTK.Graphics.OpenGL
    3. Imports OpenTK.Input
    4. Public Class CubeRenderer
    5. Public Sub RenderCube(name As String, pos As Vector3, Optional top As Boolean = True, Optional bottom As Boolean = True, Optional north As Boolean = True, Optional south As Boolean = True, Optional east As Boolean = True, Optional west As Boolean = True)
    6. Dim x = pos.X
    7. Dim y = pos.Y
    8. Dim z = pos.Z
    9. Dim x0 As Single = x + 0.0F
    10. Dim x1 As Single = x + 1.0F
    11. Dim y0 As Single = y + 0.0F
    12. Dim y1 As Single = y + 1.0F
    13. Dim z0 As Single = z + 0.0F
    14. Dim z1 As Single = z + 1.0F
    15. 'GL.PushMatrix()
    16. TheTextureManager.BindTexture(name)
    17. GL.Begin(PrimitiveType.Quads)
    18. If top Then
    19. GL.Normal3(0.0, 0.0, 1.0)
    20. GL.TexCoord2(0.0, 1.0)
    21. GLVertex(x0, y0, z1)
    22. GL.TexCoord2(1.0, 1.0)
    23. GLVertex(x0, y0, z0)
    24. GL.TexCoord2(1.0, 0.0)
    25. GLVertex(x1, y0, z0)
    26. GL.TexCoord2(0.0, 0.0)
    27. GLVertex(x1, y0, z1)
    28. End If
    29. If bottom Then
    30. GL.Normal3(0.0, 0.0, -1.0)
    31. GL.TexCoord2(0.0, 1.0)
    32. GLVertex(x1, y1, z1)
    33. GL.TexCoord2(1.0, 1.0)
    34. GLVertex(x1, y1, z0)
    35. GL.TexCoord2(1.0, 0.0)
    36. GLVertex(x0, y1, z0)
    37. GL.TexCoord2(0.0, 0.0)
    38. GLVertex(x0, y1, z1)
    39. End If
    40. If east Then
    41. GL.Normal3(4.0, 0.0, 0.0)
    42. GL.TexCoord2(0.0, 1.0) : GLVertex(x0, y1, z0)
    43. GL.TexCoord2(1.0, 1.0) : GLVertex(x1, y1, z0)
    44. GL.TexCoord2(1.0, 0.0) : GLVertex(x1, y0, z0)
    45. GL.TexCoord2(0.0, 0.0) : GLVertex(x0, y0, z0)
    46. End If
    47. If west Then
    48. GL.Normal3(-1.0, 0.0, 0.0)
    49. GL.TexCoord2(0.0, 1.0) : GLVertex(x0, y1, z1)
    50. GL.TexCoord2(0.0, 0.0) : GLVertex(x0, y0, z1)
    51. GL.TexCoord2(1.0, 0.0) : GLVertex(x1, y0, z1)
    52. GL.TexCoord2(1.0, 1.0) : GLVertex(x1, y1, z1)
    53. End If
    54. If north Then
    55. GL.Normal3(0.0, 1.0, 0.0)
    56. GL.TexCoord2(0.0, 1.0) : GLVertex(x0, y1, z1)
    57. GL.TexCoord2(1.0, 1.0) : GLVertex(x0, y1, z0)
    58. GL.TexCoord2(1.0, 0.0) : GLVertex(x0, y0, z0)
    59. GL.TexCoord2(0.0, 0.0) : GLVertex(x0, y0, z1)
    60. End If
    61. If south Then
    62. GL.Normal3(0.0, -1.0, 0.0)
    63. GL.TexCoord2(0.0, 0.0) : GLVertex(x1, y0, z1)
    64. GL.TexCoord2(0.0, 1.0) : GLVertex(x1, y0, z0)
    65. GL.TexCoord2(1.0, 1.0) : GLVertex(x1, y1, z0)
    66. GL.TexCoord2(1.0, 0.0) : GLVertex(x1, y1, z1)
    67. End If
    68. '
    69. 'GL.PopMatrix()
    70. GL.End()
    71. 'GL.EndList()
    72. 'GL.EndQuery(QueryTarget.SamplesPassed)
    73. 'Dim ResultReady As UInteger = 0
    74. 'While ResultReady = 0
    75. ' GL.GetQueryObject(MyOcclusionQuery, GetQueryObjectParam.QueryResultAvailable, ResultReady)
    76. 'End While
    77. 'Dim MyOcclusionQueryResult As UInteger = 0
    78. 'GL.GetQueryObject(MyOcclusionQuery, GetQueryObjectParam.QueryResult, MyOcclusionQueryResult)
    79. End Sub
    80. Public Sub GLVertex(x As Single, y As Single, z As Single)
    81. GL.Vertex3(x, y, z)
    82. End Sub
    83. Private Function getDistance(x1, y1, z1, x2, y2, z2) As Double
    84. Dim xd = x2 - x1
    85. Dim yd = y2 - y1
    86. Dim zd = z2 - z1
    87. Return (xd ^ 2 + yd ^ 2 + zd ^ 2)
    88. End Function
    89. End Class



    Hoffe ihr könnt mir helfen :thumbsup:

    LG
    Twometer
    An Error 404 occurred while loading signature...

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Twometer“ ()

    Twometer schrieb:

    VB.NET-Quellcode

    1. Return (xd ^ 2 + yd ^ 2 + zd ^ 2)
    in getDistance machst Du

    VB.NET-Quellcode

    1. Return (xd * xd + yd * yd + zd * zd)
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    Twometer schrieb:

    immer noch
    Wie oft wird das aufgerufen?
    Public Sub GLVertex(x As Single, y As Single, z As Single) kannst Du rausschmeißen und durch den direkten Aufruf ersetzen, das bringt aber auch nicht die Welt.
    Was machen denn die anderen Aufrufe nach GL?
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Die anderen Aufrufe (GL.TexCoord2) sind für die Texturen. Das GL.Normal3 hab ich rausgeschmissen, sieht ohne besser aus, und die GLVertex-Methode is auch geflogen, aber die Performance ist immernoch genauso mies.

    Twometer

    @ThuCommix ich verwende Frustum Culling, also ich rendere nur die Blöcke, die auch in meinem Sichtfeld sind. Die hinter mir nicht.

    //EDIT: Ich habe mal nur die Blöcke im Umkreis von 10 Blöcken gerendert, und die FPS sind immernoch bei 20, obwohl ich fast garnix rendere, wenn ich in die Richtung gucke, wo keine Blöcke sind, werden es auf einmal 30!
    An Error 404 occurred while loading signature...

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „Twometer“ ()

    Twometer schrieb:

    @Trade ich verwende Frustum Culling

    hm? Meinst Du @ThuCommix?

    Grüße
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!:
    Ja meine ich, mist...

    Ich hatte die Idee, es mit Tesselation zu machen und mit GL.DrawArrays, statt direkt mit Vertices, aber das funktioniert bei mir nicht (nur 1/3 der Vertices werden angezeigt und das auch noch falsch). Habt ihr ne Idee oder ein Codebeispiel wie sowas geht?
    An Error 404 occurred while loading signature...
    Sicher, dass es sinnvoll ist, glbegin pro cube aufzurufen (wird aber auch nicht die Welt ausmachen)? Und wie schin gesagt gldrawarray ist natürlich der bessere weg. Was ist wenn du mal die Texturen weglässt? Würde probieren das Problem zu isolieren. Sonst häng halt einen profiler rein.


    Opensource Audio-Bibliothek auf github: KLICK, im Showroom oder auf NuGet.
    Hab zum verschnellern von Code, dass gefunden(VB6): en.wikibooks.org/wiki/Visual_Basic/Optimizing_Visual_Basic
    Außerdem hab ich herausgefunden, dass der Code:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Class gen
    2. Public Function test(s As Integer)
    3. Dim x As Integer
    4. Dim y As Integer
    5. x = s
    6. y = x
    7. Return y
    8. End Function
    9. End Class

    Bei 10000 Aufrufen um ca. 0,002 sek. (Bei meine I7) langsamer ist als dieser Code:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Class gen
    2. Dim x As Integer
    3. Dim y As Integer
    4. Public Function test(s As Integer)
    5. x = s
    6. y = x
    7. Return y
    8. End Function
    9. End Class

    ich hab mit vb-paradise.de/index.php/Thread/?postID=101851#post101851 die Zeit gemessen.
    ​Smartnotr - ein intelligentes Notizprogramm
    zum Thread

    Und was genau soll uns das sagen @Mokki?
    a) Geht es nicht um die Performance von VB.NET
    b) Was soll das Codeschnippsel? Die beiden Codes haben nix gemeinsam. Wenn du Felder mit lokalen Variabeln vergleichst kannst auch gleich Autos mit Birnen vergleichen.


    Opensource Audio-Bibliothek auf github: KLICK, im Showroom oder auf NuGet.
    @thefiloe Ich habe jetzt die Texturen weggelassen und bekomme immernoch nur ~12fps. Wenn ich nicht bei jedem Würfel Gl.Begin mache, dann lädt er eine falsche Textur (irgendwie immer die gleiche, auch wenn ich BindTexture weglasse, kommt diese Textur...), aber die Performance ist trotzdem nicht besser geworden.

    @Mokki Ich habe die Zuweisungen jetzt so gemacht:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Dim x
    2. Dim y
    3. Dim z
    4. Dim x0 As Single
    5. Dim x1 As Single
    6. Dim y0 As Single
    7. Dim y1 As Single
    8. Dim z0 As Single
    9. Dim z1 As Single
    10. Public Sub RenderCube(id As Integer, pos As Vector3, Optional top As Boolean = True, Optional bottom As Boolean = True, Optional north As Boolean = True, Optional south As Boolean = True, Optional east As Boolean = True, Optional west As Boolean = True)
    11. x = pos.X
    12. y = pos.Y
    13. z = pos.Z
    14. x0 = x + 0.0F
    15. x1 = x + 1.0F
    16. y0 = y + 0.0F
    17. y1 = y + 1.0F
    18. z0 = z + 0.0F
    19. z1 = z + 1.0F
    20. ...



    Aber die Performance ist immernoch genauso.

    LG
    Twometer
    An Error 404 occurred while loading signature...
    @thefiloe Und wie mach ich das? Mit Profilern habe ich bisher noch nicht gearbeitet

    //Edit: Ich habe VSync aktiv gehabt, ohne kriege ich, wenn ich nix angucke >5000fps, aber wenn ich Würfel angucke bekomme ich nur noch die üblichen 8-10fps

    LG
    Twometer
    An Error 404 occurred while loading signature...
    @thefiloe Der .NET Profiler funzt bei mir nicht (siehe Anhang). Der sagt die ganze zeit 60fps, obwohl ich nur 10 habe und Frames kann er auch keine Erfassen (während er läuft)
    Und ich habe festgestellt, dass mein Frustum Culling auch das rendert, was ich nicht angucke

    > | --- \
    Das Wird gerendert >| | >Kamera
    > |__ |----/

    Also rendert er alles, was in meinem Sichtfeld ist, auch das, was auf der Rückseite des Chunks ist (MC-Chunk 16x64x16 Cubes)

    Das ist mein Frustum:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports OpenTK
    2. Public Class Frustum
    3. Private _clipMatrix As Single() = New Single(15) {}
    4. Private _frustum As Single(,) = New Single(5, 3) {}
    5. Const RIGHT As Integer = 0, LEFT As Integer = 1, BOTTOM As Integer = 2, TOP As Integer = 3, BACK As Integer = 4, FRONT As Integer = 5
    6. Private Sub NormalizePlane(frustum As Single(,), side As Integer)
    7. Dim magnitude As Single = CSng(System.Math.Sqrt((frustum(side, 0) * frustum(side, 0)) + (frustum(side, 1) * frustum(side, 1)) + (frustum(side, 2) * frustum(side, 2))))
    8. frustum(side, 0) /= magnitude
    9. frustum(side, 1) /= magnitude
    10. frustum(side, 2) /= magnitude
    11. frustum(side, 3) /= magnitude
    12. End Sub
    13. Public Function SphereInFrustum(x As Single, y As Single, z As Single, radius As Single) As Boolean
    14. Dim d As Single = 0
    15. For p As Integer = 0 To 5
    16. d = _frustum(p, 0) * x + _frustum(p, 1) * y + _frustum(p, 2) * z + _frustum(p, 3)
    17. If d <= -radius Then
    18. Return False
    19. End If
    20. Next
    21. Return True
    22. End Function
    23. Public Function VoxelWithinFrustum(x1 As Single, y1 As Single, z1 As Single, x2 As Single, y2 As Single, z2 As Single) As Boolean
    24. For i As Integer = 0 To 5
    25. If (Me._frustum(i, 0) * x1 + Me._frustum(i, 1) * y1 + Me._frustum(i, 2) * z1 + Me._frustum(i, 3) <= 0.0F) AndAlso (Me._frustum(i, 0) * x2 + Me._frustum(i, 1) * y1 + Me._frustum(i, 2) * z1 + Me._frustum(i, 3) <= 0.0F) AndAlso (Me._frustum(i, 0) * x1 + Me._frustum(i, 1) * y2 + Me._frustum(i, 2) * z1 + Me._frustum(i, 3) <= 0.0F) AndAlso (Me._frustum(i, 0) * x2 + Me._frustum(i, 1) * y2 + Me._frustum(i, 2) * z1 + Me._frustum(i, 3) <= 0.0F) AndAlso (Me._frustum(i, 0) * x1 + Me._frustum(i, 1) * y1 + Me._frustum(i, 2) * z2 + Me._frustum(i, 3) <= 0.0F) AndAlso (Me._frustum(i, 0) * x2 + Me._frustum(i, 1) * y1 + Me._frustum(i, 2) * z2 + Me._frustum(i, 3) <= 0.0F) AndAlso (Me._frustum(i, 0) * x1 + Me._frustum(i, 1) * y2 + Me._frustum(i, 2) * z2 + Me._frustum(i, 3) <= 0.0F) AndAlso (Me._frustum(i, 0) * x2 + Me._frustum(i, 1) * y2 + Me._frustum(i, 2) * z2 + Me._frustum(i, 3) <= 0.0F) Then
    26. Return False
    27. End If
    28. Next
    29. Return True
    30. End Function
    31. Public Sub CalculateFrustum(projectionMatrix As Matrix4, modelViewMatrix As Matrix4)
    32. _clipMatrix(0) = (modelViewMatrix.M11 * projectionMatrix.M11) + (modelViewMatrix.M12 * projectionMatrix.M21) + (modelViewMatrix.M13 * projectionMatrix.M31) + (modelViewMatrix.M14 * projectionMatrix.M41)
    33. _clipMatrix(1) = (modelViewMatrix.M11 * projectionMatrix.M12) + (modelViewMatrix.M12 * projectionMatrix.M22) + (modelViewMatrix.M13 * projectionMatrix.M32) + (modelViewMatrix.M14 * projectionMatrix.M42)
    34. _clipMatrix(2) = (modelViewMatrix.M11 * projectionMatrix.M13) + (modelViewMatrix.M12 * projectionMatrix.M23) + (modelViewMatrix.M13 * projectionMatrix.M33) + (modelViewMatrix.M14 * projectionMatrix.M43)
    35. _clipMatrix(3) = (modelViewMatrix.M11 * projectionMatrix.M14) + (modelViewMatrix.M12 * projectionMatrix.M24) + (modelViewMatrix.M13 * projectionMatrix.M34) + (modelViewMatrix.M14 * projectionMatrix.M44)
    36. _clipMatrix(4) = (modelViewMatrix.M21 * projectionMatrix.M11) + (modelViewMatrix.M22 * projectionMatrix.M21) + (modelViewMatrix.M23 * projectionMatrix.M31) + (modelViewMatrix.M24 * projectionMatrix.M41)
    37. _clipMatrix(5) = (modelViewMatrix.M21 * projectionMatrix.M12) + (modelViewMatrix.M22 * projectionMatrix.M22) + (modelViewMatrix.M23 * projectionMatrix.M32) + (modelViewMatrix.M24 * projectionMatrix.M42)
    38. _clipMatrix(6) = (modelViewMatrix.M21 * projectionMatrix.M13) + (modelViewMatrix.M22 * projectionMatrix.M23) + (modelViewMatrix.M23 * projectionMatrix.M33) + (modelViewMatrix.M24 * projectionMatrix.M43)
    39. _clipMatrix(7) = (modelViewMatrix.M21 * projectionMatrix.M14) + (modelViewMatrix.M22 * projectionMatrix.M24) + (modelViewMatrix.M23 * projectionMatrix.M34) + (modelViewMatrix.M24 * projectionMatrix.M44)
    40. _clipMatrix(8) = (modelViewMatrix.M31 * projectionMatrix.M11) + (modelViewMatrix.M32 * projectionMatrix.M21) + (modelViewMatrix.M33 * projectionMatrix.M31) + (modelViewMatrix.M34 * projectionMatrix.M41)
    41. _clipMatrix(9) = (modelViewMatrix.M31 * projectionMatrix.M12) + (modelViewMatrix.M32 * projectionMatrix.M22) + (modelViewMatrix.M33 * projectionMatrix.M32) + (modelViewMatrix.M34 * projectionMatrix.M42)
    42. _clipMatrix(10) = (modelViewMatrix.M31 * projectionMatrix.M13) + (modelViewMatrix.M32 * projectionMatrix.M23) + (modelViewMatrix.M33 * projectionMatrix.M33) + (modelViewMatrix.M34 * projectionMatrix.M43)
    43. _clipMatrix(11) = (modelViewMatrix.M31 * projectionMatrix.M14) + (modelViewMatrix.M32 * projectionMatrix.M24) + (modelViewMatrix.M33 * projectionMatrix.M34) + (modelViewMatrix.M34 * projectionMatrix.M44)
    44. _clipMatrix(12) = (modelViewMatrix.M41 * projectionMatrix.M11) + (modelViewMatrix.M42 * projectionMatrix.M21) + (modelViewMatrix.M43 * projectionMatrix.M31) + (modelViewMatrix.M44 * projectionMatrix.M41)
    45. _clipMatrix(13) = (modelViewMatrix.M41 * projectionMatrix.M12) + (modelViewMatrix.M42 * projectionMatrix.M22) + (modelViewMatrix.M43 * projectionMatrix.M32) + (modelViewMatrix.M44 * projectionMatrix.M42)
    46. _clipMatrix(14) = (modelViewMatrix.M41 * projectionMatrix.M13) + (modelViewMatrix.M42 * projectionMatrix.M23) + (modelViewMatrix.M43 * projectionMatrix.M33) + (modelViewMatrix.M44 * projectionMatrix.M43)
    47. _clipMatrix(15) = (modelViewMatrix.M41 * projectionMatrix.M14) + (modelViewMatrix.M42 * projectionMatrix.M24) + (modelViewMatrix.M43 * projectionMatrix.M34) + (modelViewMatrix.M44 * projectionMatrix.M44)
    48. _frustum(RIGHT, 0) = _clipMatrix(3) - _clipMatrix(0)
    49. _frustum(RIGHT, 1) = _clipMatrix(7) - _clipMatrix(4)
    50. _frustum(RIGHT, 2) = _clipMatrix(11) - _clipMatrix(8)
    51. _frustum(RIGHT, 3) = _clipMatrix(15) - _clipMatrix(12)
    52. NormalizePlane(_frustum, RIGHT)
    53. _frustum(LEFT, 0) = _clipMatrix(3) + _clipMatrix(0)
    54. _frustum(LEFT, 1) = _clipMatrix(7) + _clipMatrix(4)
    55. _frustum(LEFT, 2) = _clipMatrix(11) + _clipMatrix(8)
    56. _frustum(LEFT, 3) = _clipMatrix(15) + _clipMatrix(12)
    57. NormalizePlane(_frustum, LEFT)
    58. _frustum(BOTTOM, 0) = _clipMatrix(3) + _clipMatrix(1)
    59. _frustum(BOTTOM, 1) = _clipMatrix(7) + _clipMatrix(5)
    60. _frustum(BOTTOM, 2) = _clipMatrix(11) + _clipMatrix(9)
    61. _frustum(BOTTOM, 3) = _clipMatrix(15) + _clipMatrix(13)
    62. NormalizePlane(_frustum, BOTTOM)
    63. _frustum(TOP, 0) = _clipMatrix(3) - _clipMatrix(1)
    64. _frustum(TOP, 1) = _clipMatrix(7) - _clipMatrix(5)
    65. _frustum(TOP, 2) = _clipMatrix(11) - _clipMatrix(9)
    66. _frustum(TOP, 3) = _clipMatrix(15) - _clipMatrix(13)
    67. NormalizePlane(_frustum, TOP)
    68. _frustum(BACK, 0) = _clipMatrix(3) - _clipMatrix(2)
    69. _frustum(BACK, 1) = _clipMatrix(7) - _clipMatrix(6)
    70. _frustum(BACK, 2) = _clipMatrix(11) - _clipMatrix(10)
    71. _frustum(BACK, 3) = _clipMatrix(15) - _clipMatrix(14)
    72. NormalizePlane(_frustum, BACK)
    73. _frustum(FRONT, 0) = _clipMatrix(3) + _clipMatrix(2)
    74. _frustum(FRONT, 1) = _clipMatrix(7) + _clipMatrix(6)
    75. _frustum(FRONT, 2) = _clipMatrix(11) + _clipMatrix(10)
    76. _frustum(FRONT, 3) = _clipMatrix(15) + _clipMatrix(14)
    77. NormalizePlane(_frustum, FRONT)
    78. End Sub
    79. End Class



    Der Code ist nicht von mir, ich habe ihn nur implementiert, weil ich nicht wusste wie sowas geht :whistling: ..

    Aber woran liegt das, das er auch die Rückseite rendert?

    LG
    Twometer
    Bilder
    • Unbenannt.png

      20,46 kB, 963×525, 99 mal angesehen
    An Error 404 occurred while loading signature...
    @thefiloe Und wie mach ich das? Muss ich das da einstellen??

    LG
    Twometer



    @thefiloe Ich habe es jezt mit glDrawArray gemacht, aber habe trotzdem nur 10 fps :( bin langsam echt am verzweifeln



    Leute ich habe es geschafft. Ich habs so gemacht, dass ich das einmal render und dann immer wieder lade mit glList. >120 FPS :thumbup:
    ==> Als erledigt markiert

    Danke für eure Hilfe :)

    LG
    Twometer

    Beiträge zusammengefügt. Für nachträgliche Änderungen gibt es die "Bearbeiten"-Funktion! ~Thunderbolt
    An Error 404 occurred while loading signature...

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Thunderbolt“ ()