Skip to content

Commit 23c8d6d

Browse files
authored
compute and display some debug statistics (#6)
* add DataStructures * display frame number and average time spent per frame * compute and display average drawing time per frame
1 parent 30567d6 commit 23c8d6d

File tree

3 files changed

+100
-4
lines changed

3 files changed

+100
-4
lines changed

Manifest.toml

+60
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,34 @@ uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
1212
[[deps.Base64]]
1313
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
1414

15+
[[deps.Compat]]
16+
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
17+
git-tree-sha1 = "96b0bc6c52df76506efc8a441c6cf1adcb1babc4"
18+
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
19+
version = "3.42.0"
20+
21+
[[deps.CompilerSupportLibraries_jll]]
22+
deps = ["Artifacts", "Libdl"]
23+
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"
24+
25+
[[deps.DataStructures]]
26+
deps = ["Compat", "InteractiveUtils", "OrderedCollections"]
27+
git-tree-sha1 = "3daef5523dd2e769dad2365274f760ff5f282c7d"
28+
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
29+
version = "0.18.11"
30+
1531
[[deps.Dates]]
1632
deps = ["Printf"]
1733
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
1834

35+
[[deps.DelimitedFiles]]
36+
deps = ["Mmap"]
37+
uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"
38+
39+
[[deps.Distributed]]
40+
deps = ["Random", "Serialization", "Sockets"]
41+
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"
42+
1943
[[deps.Downloads]]
2044
deps = ["ArgTools", "LibCURL", "NetworkOptions"]
2145
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
@@ -85,6 +109,10 @@ git-tree-sha1 = "42b62845d70a619f063a7da093d995ec8e15e778"
85109
uuid = "94ce4f54-9a6c-5748-9c1c-f9c7231a4531"
86110
version = "1.16.1+1"
87111

112+
[[deps.LinearAlgebra]]
113+
deps = ["Libdl", "libblastrampoline_jll"]
114+
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
115+
88116
[[deps.Logging]]
89117
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
90118

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

127+
[[deps.Mmap]]
128+
uuid = "a63ad114-7e13-5084-954f-fe012c677804"
129+
99130
[[deps.ModernGL]]
100131
deps = ["Libdl"]
101132
git-tree-sha1 = "344f8896e55541e30d5ccffcbf747c98ad57ca47"
@@ -108,6 +139,15 @@ uuid = "14a3606d-f60d-562e-9121-12d972cd8159"
108139
[[deps.NetworkOptions]]
109140
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"
110141

142+
[[deps.OpenBLAS_jll]]
143+
deps = ["Artifacts", "CompilerSupportLibraries_jll", "Libdl"]
144+
uuid = "4536629a-c528-5b80-bd46-f80d51c5b363"
145+
146+
[[deps.OrderedCollections]]
147+
git-tree-sha1 = "85f8e6578bf1f9ee0d11e7bb1b1456435479d47c"
148+
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
149+
version = "1.4.1"
150+
111151
[[deps.Pkg]]
112152
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"]
113153
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
@@ -136,6 +176,10 @@ uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
136176
[[deps.Serialization]]
137177
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
138178

179+
[[deps.SharedArrays]]
180+
deps = ["Distributed", "Mmap", "Random", "Serialization"]
181+
uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383"
182+
139183
[[deps.SimpleDraw]]
140184
git-tree-sha1 = "c010f60b702e5852f0e7d3071fb647e0955d3262"
141185
uuid = "d1acf6f4-8553-480e-80ae-3c883f0a995a"
@@ -144,6 +188,14 @@ version = "0.3.0"
144188
[[deps.Sockets]]
145189
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"
146190

191+
[[deps.SparseArrays]]
192+
deps = ["LinearAlgebra", "Random"]
193+
uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
194+
195+
[[deps.Statistics]]
196+
deps = ["LinearAlgebra", "SparseArrays"]
197+
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
198+
147199
[[deps.TOML]]
148200
deps = ["Dates"]
149201
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
@@ -152,6 +204,10 @@ uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
152204
deps = ["ArgTools", "SHA"]
153205
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
154206

207+
[[deps.Test]]
208+
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
209+
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
210+
155211
[[deps.UUIDs]]
156212
deps = ["Random", "SHA"]
157213
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
@@ -253,6 +309,10 @@ version = "1.4.0+3"
253309
deps = ["Libdl"]
254310
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"
255311

312+
[[deps.libblastrampoline_jll]]
313+
deps = ["Artifacts", "Libdl", "OpenBLAS_jll"]
314+
uuid = "8e850b90-86db-534c-a0d3-1478176c7d93"
315+
256316
[[deps.nghttp2_jll]]
257317
deps = ["Artifacts", "Libdl"]
258318
uuid = "8e850ede-7688-5339-a07c-302acd2aaf8d"

Project.toml

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[deps]
2+
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
23
GLFW = "f7f18e0c-5ee9-5ccd-a5bf-e8befd85ed98"
34
ModernGL = "66fc600b-dfda-50eb-8b99-91cfa97b1301"
45
SimpleDraw = "d1acf6f4-8553-480e-80ae-3c883f0a995a"

example.jl

+39-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ModernGL as MGL
2+
import DataStructures as DS
23
import GLFW
34
import SimpleDraw as SD
45

@@ -10,13 +11,26 @@ function process_input(window)
1011
return nothing
1112
end
1213

14+
function draw_lines!(image, lines, color)
15+
font = SD.TERMINUS_32_16
16+
height_font = 32
17+
18+
for (i, text) in enumerate(lines)
19+
position = SD.Point(1 + (i - 1) * height_font, 1)
20+
SD.draw!(image, SD.TextLine(position, text, font), color)
21+
end
22+
23+
return nothing
24+
end
25+
1326
GLFW.WindowHint(GLFW.CONTEXT_VERSION_MAJOR, 3)
1427
GLFW.WindowHint(GLFW.CONTEXT_VERSION_MINOR, 3)
1528
GLFW.WindowHint(GLFW.OPENGL_PROFILE, GLFW.OPENGL_CORE_PROFILE)
1629

17-
height_image = 600
18-
width_image = 800
30+
height_image = 720
31+
width_image = 1280
1932
window_name = "Example"
33+
sliding_window_size = 60
2034

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

@@ -138,17 +152,34 @@ image = zeros(MGL.GLuint, height_image, width_image)
138152
background_color = 0x00c0c0c0
139153
text_color = 0x00000000
140154
SD.draw!(image, SD.Background(), background_color)
141-
SD.draw!(image, SD.TextLine(SD.Point(1, 1), "Hello world!", SD.TERMINUS_32_16), text_color)
142155
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)
143156

144157
MGL.glClearColor(0.0f0, 0.0f0, 0.0f0, 1.0f0)
145158
MGL.glClear(MGL.GL_COLOR_BUFFER_BIT)
146159

160+
lines = String[]
161+
time_stamp_buffer = DS.CircularBuffer{typeof(time_ns())}(sliding_window_size)
162+
drawing_time_buffer = DS.CircularBuffer{typeof(time_ns())}(sliding_window_size)
163+
164+
i = 0
165+
166+
push!(time_stamp_buffer, time_ns())
167+
push!(drawing_time_buffer, zero(UInt))
168+
147169
while !GLFW.WindowShouldClose(window)
148170
process_input(window)
149171

172+
empty!(lines)
173+
push!(lines, "previous frame number: $(i)")
174+
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")
175+
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")
176+
177+
drawing_time_start = time_ns()
150178
SD.draw!(image, SD.Background(), background_color)
151-
SD.draw!(image, SD.TextLine(SD.Point(1, 1), "Hello world!", SD.TERMINUS_32_16), text_color)
179+
draw_lines!(image, lines, text_color)
180+
drawing_time_end = time_ns()
181+
push!(drawing_time_buffer, drawing_time_end - drawing_time_start)
182+
152183
MGL.glActiveTexture(MGL.GL_TEXTURE0)
153184
MGL.glBindTexture(MGL.GL_TEXTURE_2D, texture_ref[])
154185
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)
@@ -160,6 +191,10 @@ while !GLFW.WindowShouldClose(window)
160191

161192
GLFW.SwapBuffers(window)
162193
GLFW.PollEvents()
194+
195+
global i = i + 1
196+
197+
push!(time_stamp_buffer, time_ns())
163198
end
164199

165200
MGL.glDeleteVertexArrays(1, VAO_ref)

0 commit comments

Comments
 (0)