Skip to content

Commit

Permalink
compute and display some debug statistics (#6)
Browse files Browse the repository at this point in the history
* add DataStructures

* display frame number and average time spent per frame

* compute and display average drawing time per frame
  • Loading branch information
Sid-Bhatia-0 authored Mar 27, 2022
1 parent 30567d6 commit 23c8d6d
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 4 deletions.
60 changes: 60 additions & 0 deletions Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,34 @@ uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
[[deps.Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[deps.Compat]]
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
git-tree-sha1 = "96b0bc6c52df76506efc8a441c6cf1adcb1babc4"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.42.0"

[[deps.CompilerSupportLibraries_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"

[[deps.DataStructures]]
deps = ["Compat", "InteractiveUtils", "OrderedCollections"]
git-tree-sha1 = "3daef5523dd2e769dad2365274f760ff5f282c7d"
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.18.11"

[[deps.Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"

[[deps.DelimitedFiles]]
deps = ["Mmap"]
uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"

[[deps.Distributed]]
deps = ["Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[[deps.Downloads]]
deps = ["ArgTools", "LibCURL", "NetworkOptions"]
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
Expand Down Expand Up @@ -85,6 +109,10 @@ git-tree-sha1 = "42b62845d70a619f063a7da093d995ec8e15e778"
uuid = "94ce4f54-9a6c-5748-9c1c-f9c7231a4531"
version = "1.16.1+1"

[[deps.LinearAlgebra]]
deps = ["Libdl", "libblastrampoline_jll"]
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[[deps.Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

Expand All @@ -96,6 +124,9 @@ uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
deps = ["Artifacts", "Libdl"]
uuid = "c8ffd9c3-330d-5841-b78e-0817d7145fa1"

[[deps.Mmap]]
uuid = "a63ad114-7e13-5084-954f-fe012c677804"

[[deps.ModernGL]]
deps = ["Libdl"]
git-tree-sha1 = "344f8896e55541e30d5ccffcbf747c98ad57ca47"
Expand All @@ -108,6 +139,15 @@ uuid = "14a3606d-f60d-562e-9121-12d972cd8159"
[[deps.NetworkOptions]]
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"

[[deps.OpenBLAS_jll]]
deps = ["Artifacts", "CompilerSupportLibraries_jll", "Libdl"]
uuid = "4536629a-c528-5b80-bd46-f80d51c5b363"

[[deps.OrderedCollections]]
git-tree-sha1 = "85f8e6578bf1f9ee0d11e7bb1b1456435479d47c"
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
version = "1.4.1"

[[deps.Pkg]]
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"]
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
Expand Down Expand Up @@ -136,6 +176,10 @@ uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
[[deps.Serialization]]
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"

[[deps.SharedArrays]]
deps = ["Distributed", "Mmap", "Random", "Serialization"]
uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383"

[[deps.SimpleDraw]]
git-tree-sha1 = "c010f60b702e5852f0e7d3071fb647e0955d3262"
uuid = "d1acf6f4-8553-480e-80ae-3c883f0a995a"
Expand All @@ -144,6 +188,14 @@ version = "0.3.0"
[[deps.Sockets]]
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"

[[deps.SparseArrays]]
deps = ["LinearAlgebra", "Random"]
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

[[deps.Statistics]]
deps = ["LinearAlgebra", "SparseArrays"]
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[[deps.TOML]]
deps = ["Dates"]
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
Expand All @@ -152,6 +204,10 @@ uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
deps = ["ArgTools", "SHA"]
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"

[[deps.Test]]
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[[deps.UUIDs]]
deps = ["Random", "SHA"]
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
Expand Down Expand Up @@ -253,6 +309,10 @@ version = "1.4.0+3"
deps = ["Libdl"]
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"

[[deps.libblastrampoline_jll]]
deps = ["Artifacts", "Libdl", "OpenBLAS_jll"]
uuid = "8e850b90-86db-534c-a0d3-1478176c7d93"

[[deps.nghttp2_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"
Expand Down
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
GLFW = "f7f18e0c-5ee9-5ccd-a5bf-e8befd85ed98"
ModernGL = "66fc600b-dfda-50eb-8b99-91cfa97b1301"
SimpleDraw = "d1acf6f4-8553-480e-80ae-3c883f0a995a"
43 changes: 39 additions & 4 deletions example.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ModernGL as MGL
import DataStructures as DS
import GLFW
import SimpleDraw as SD

Expand All @@ -10,13 +11,26 @@ function process_input(window)
return nothing
end

function draw_lines!(image, lines, color)
font = SD.TERMINUS_32_16
height_font = 32

for (i, text) in enumerate(lines)
position = SD.Point(1 + (i - 1) * height_font, 1)
SD.draw!(image, SD.TextLine(position, text, font), color)
end

return nothing
end

GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, 3)
GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, 3)
GLFW.WindowHint(GLFW.OPENGL_PROFILE, GLFW.OPENGL_CORE_PROFILE)

height_image = 600
width_image = 800
height_image = 720
width_image = 1280
window_name = "Example"
sliding_window_size = 60

window = GLFW.CreateWindow(width_image, height_image, window_name)

Expand Down Expand Up @@ -138,17 +152,34 @@ image = zeros(MGL.GLuint, height_image, width_image)
background_color = 0x00c0c0c0
text_color = 0x00000000
SD.draw!(image, SD.Background(), background_color)
SD.draw!(image, SD.TextLine(SD.Point(1, 1), "Hello world!", SD.TERMINUS_32_16), text_color)
MGL.glTexImage2D(MGL.GL_TEXTURE_2D, 0, MGL.GL_RGBA, height_image, width_image, 0, MGL.GL_BGRA, MGL.GL_UNSIGNED_INT_8_8_8_8_REV, image)

MGL.glClearColor(0.0f0, 0.0f0, 0.0f0, 1.0f0)
MGL.glClear(MGL.GL_COLOR_BUFFER_BIT)

lines = String[]
time_stamp_buffer = DS.CircularBuffer{typeof(time_ns())}(sliding_window_size)
drawing_time_buffer = DS.CircularBuffer{typeof(time_ns())}(sliding_window_size)

i = 0

push!(time_stamp_buffer, time_ns())
push!(drawing_time_buffer, zero(UInt))

while !GLFW.WindowShouldClose(window)
process_input(window)

empty!(lines)
push!(lines, "previous frame number: $(i)")
push!(lines, "average time spent per frame (averaged over previous $(length(time_stamp_buffer)) frames): $(round((last(time_stamp_buffer) - first(time_stamp_buffer)) / (1e6 * length(time_stamp_buffer)), digits = 2)) ms")
push!(lines, "average drawing time spent per frame (averaged over previous $(length(drawing_time_buffer)) frames): $(round(sum(drawing_time_buffer) / (1e6 * length(drawing_time_buffer)), digits = 2)) ms")

drawing_time_start = time_ns()
SD.draw!(image, SD.Background(), background_color)
SD.draw!(image, SD.TextLine(SD.Point(1, 1), "Hello world!", SD.TERMINUS_32_16), text_color)
draw_lines!(image, lines, text_color)
drawing_time_end = time_ns()
push!(drawing_time_buffer, drawing_time_end - drawing_time_start)

MGL.glActiveTexture(MGL.GL_TEXTURE0)
MGL.glBindTexture(MGL.GL_TEXTURE_2D, texture_ref[])
MGL.glTexSubImage2D(MGL.GL_TEXTURE_2D, 0, MGL.GLint(0), MGL.GLint(0), MGL.GLsizei(height_image), MGL.GLsizei(width_image), MGL.GL_BGRA, MGL.GL_UNSIGNED_INT_8_8_8_8_REV, image)
Expand All @@ -160,6 +191,10 @@ while !GLFW.WindowShouldClose(window)

GLFW.SwapBuffers(window)
GLFW.PollEvents()

global i = i + 1

push!(time_stamp_buffer, time_ns())
end

MGL.glDeleteVertexArrays(1, VAO_ref)
Expand Down

0 comments on commit 23c8d6d

Please sign in to comment.