Timer.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package me.hammerle.snuviengine.api;
  2. import java.nio.FloatBuffer;
  3. import org.lwjgl.BufferUtils;
  4. import static org.lwjgl.opengl.GL11.*;
  5. import static org.lwjgl.opengl.GL15.*;
  6. import static org.lwjgl.opengl.GL20.*;
  7. import static org.lwjgl.opengl.GL30.*;
  8. public class Timer
  9. {
  10. private static final int SIZE = 128;
  11. private int index = -1;
  12. private final long[] times = new long[SIZE];
  13. private long sum = 0;
  14. private double callsPerSecond = 0;
  15. private long lastTime = System.nanoTime();
  16. private int vao;
  17. private int vbo;
  18. private final FloatBuffer buffer = BufferUtils.createFloatBuffer(18);
  19. private boolean active = false;
  20. private float expectedValue;
  21. protected Timer(float expectedValue)
  22. {
  23. this.expectedValue = expectedValue;
  24. Shader.addTask(() ->
  25. {
  26. vao = glGenVertexArrays();
  27. vbo = glGenBuffers();
  28. GLHelper.glBindVertexArray(vao);
  29. GLHelper.glBindBuffer(vbo);
  30. glEnableVertexAttribArray(0);
  31. glEnableVertexAttribArray(2);
  32. glVertexAttribPointer(0, 2, GL_FLOAT, false, 12, 0);
  33. glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, true, 12, 8);
  34. glBufferData(GL_ARRAY_BUFFER, SIZE * buffer.limit() * 4, GL_STATIC_DRAW);
  35. });
  36. }
  37. protected void setActive(boolean active)
  38. {
  39. this.active = active;
  40. }
  41. protected void update()
  42. {
  43. index = (index + 1) & (SIZE - 1);
  44. long time = System.nanoTime();
  45. sum -= times[index];
  46. times[index] = time - lastTime;
  47. sum += times[index];
  48. lastTime = time;
  49. callsPerSecond = (1_000_000_000.0 * SIZE) / sum;
  50. if(active)
  51. {
  52. float minX = index * 3;
  53. float minY = 0.0f;
  54. float maxX = minX + 2;
  55. float maxY = 1_000_000_000.0f / times[index];
  56. float diff = (maxY - expectedValue) / expectedValue;
  57. int r = Math.min((int) (128 * (1 - Math.min(diff, 0.0f) * 16)), 255);
  58. int g = Math.min((int) (128 * (1 + Math.max(diff, 0.0f) * 16)), 255);
  59. int b = (int) (128 * (1 - Math.abs(diff)));
  60. float color = Float.intBitsToFloat(r | (g << 8) | (b << 16) | 0xFF000000);
  61. buffer.put(minX);
  62. buffer.put(maxY);
  63. buffer.put(color);
  64. buffer.put(minX);
  65. buffer.put(minY);
  66. buffer.put(color);
  67. buffer.put(maxX);
  68. buffer.put(maxY);
  69. buffer.put(color);
  70. buffer.put(minX);
  71. buffer.put(minY);
  72. buffer.put(color);
  73. buffer.put(maxX);
  74. buffer.put(maxY);
  75. buffer.put(color);
  76. buffer.put(maxX);
  77. buffer.put(minY);
  78. buffer.put(color);
  79. buffer.flip();
  80. GLHelper.glBindBuffer(vbo);
  81. glBufferSubData(GL_ARRAY_BUFFER, index * buffer.limit() * 4, buffer);
  82. }
  83. }
  84. protected void setExpectedValue(float value)
  85. {
  86. expectedValue = value;
  87. }
  88. protected long getAverageTime()
  89. {
  90. return sum / SIZE;
  91. }
  92. protected long getCurrentTime()
  93. {
  94. return System.nanoTime() - lastTime;
  95. }
  96. protected long getTime()
  97. {
  98. return times[index];
  99. }
  100. protected double getCallsPerSecond()
  101. {
  102. return callsPerSecond;
  103. }
  104. protected void draw()
  105. {
  106. if(active)
  107. {
  108. Shader.setTextureEnabled(false);
  109. Shader.setColorEnabled(true);
  110. GLHelper.glBindBuffer(vbo);
  111. GLHelper.glBindVertexArray(vao);
  112. glDrawArrays(GL_TRIANGLES, 0, SIZE * 6);
  113. }
  114. }
  115. }