-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbenchmark_particles.fluid
269 lines (210 loc) · 8.47 KB
/
benchmark_particles.fluid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
--[[
Particle Benchmark
Parameters:
mode: primitive; directvector; vector
--]]
require 'gui'
require 'common'
require 'gui/window'
include 'display'
mVec = mod.load('vector')
glBestFPS = 0
----------------------------------------------------------------------------------------------------------------------
-- Render vector paths directly, bypassing the scene graph.
function drawDirectVector(Surface, Bitmap)
glWinWidth, glWinHeight = Surface.width, Surface.height
local start = mSys.PreciseTime()
for _,p in pairs(glParticles) do
mVec.DrawPath(Bitmap, p.path, 0, nil, p.colour)
end
updateTime(mSys.PreciseTime() - start)
end
----------------------------------------------------------------------------------------------------------------------
-- Render plain rectangles
function drawPrimitive(Surface, Bitmap)
glWinWidth, glWinHeight = Surface.width, Surface.height
local start = mSys.PreciseTime()
for _,p in pairs(glParticles) do
local col = p.colour
local err, pixel = Bitmap.mtGetColour(col.r, col.g, col.b, 255)
Bitmap.mtDrawRectangle(p.x-p.radius, p.y-p.radius, p.radius*2, p.radius*2, pixel, BAF_FILL)
end
updateTime(mSys.PreciseTime() - start)
end
----------------------------------------------------------------------------------------------------------------------
function updateTime(elapsed)
table.insert(glTimes, elapsed)
if (#glTimes > 50) then table.remove(glTimes, 1) end
local totalTime = 0
for _,time in pairs(glTimes) do
totalTime = totalTime + time
end
totalTime = totalTime / #glTimes
local fps = 1000000 / totalTime
if glFPS then glFPS.string = string.format('FPS: %.0f', fps) end
if (#glTimes >= 50) and (fps > glBestFPS) then
glBestFPS = fps
end
end
----------------------------------------------------------------------------------------------------------------------
function newParticle()
local particle = {
angle = math.pi * 2 * math.random(),
velocity = 1 + math.random() * 3,
radius = 6,
x = (glWinWidth / 2) - 6,
y = (glWinHeight / 2) - 6
}
if (glMode == MODE_VECTOR) then
local colour = glColours[1+(math.floor(math.random() * #glColours))]
particle.vector = glViewport.new('VectorEllipse', {
fill = string.format('rgb(%f,%f,%f)', colour.r, colour.g, colour.b),
centerx = particle.x,
centery = particle.y,
radius = particle.radius
})
elseif (glMode == MODE_DIRECTVECTOR) then
particle.colour = glColours[1+(math.floor(math.random() * #glColours))]
local err
err, particle.path = mVec.GenerateEllipse(particle.x, particle.y, particle.radius, particle.radius, 0)
elseif (glMode == MODE_PRIMITIVE) then
particle.colour = glColours[1+(math.floor(math.random() * #glColours))]
end
return particle
end
----------------------------------------------------------------------------------------------------------------------
function timer(Subscriber, Elapsed, Current)
local moveflag = bit.bor(MTF_X, MTF_Y)
glWinWidth = nz(glSurface.width, 1)
glWinHeight = nz(glSurface.height, 1)
for _,p in pairs(glParticles) do
-- Calculate next position of particle
local tx = math.cos(p.angle) * p.velocity
local ty = math.sin(p.angle) * p.velocity
local nextX = p.x + tx
local nextY = p.y + ty
-- If particle is going to move off right side of screen
if (nextX + p.radius * 2 > glWinWidth) then
if ((p.angle >= 0) and (p.angle < math.pi * 0.5)) then
p.angle = math.pi - p.angle
elseif (p.angle > math.pi / 2 * 3) then
p.angle = p.angle - (p.angle - math.pi / 2 * 3) * 2
end
end
-- If particle is going to move off left side of screen
if (nextX < 0) then -- If angle is between 6 o'clock and 9 o'clock
if ((p.angle > math.pi / 2) and (p.angle < math.pi)) then
p.angle = math.pi - p.angle
elseif (p.angle > math.pi) and (p.angle < math.pi * 0.5 * 3) then
p.angle = p.angle + (math.pi * 0.5 * 3 - p.angle) * 2
end
end
-- If particle is going to move off bottom side of screen
if (nextY + p.radius * 2 > glWinHeight) then
if ((p.angle > 0 and p.angle < math.pi)) then
p.angle = math.pi * 2 - p.angle
end
end
-- If particle is going to move off top side of screen
if (nextY < 0) then
if ((p.angle > math.pi) and (p.angle < math.pi * 2)) then
p.angle = p.angle - (p.angle - math.pi) * 2
end
end
if (glMode == MODE_VECTOR) then
p.vector.acMoveToPoint(nextX + p.radius, nextY + p.radius, 0, moveflag)
elseif (glMode == MODE_DIRECTVECTOR) then
mVec.TranslatePath(p.path, tx, ty)
end
p.x = nextX
p.y = nextY
end
glViewport.acDraw()
updateTime(glScene.renderTime)
if (math.round(Current / 1000000) % 4 == 0) then
if not glPrinted then
glPrinted = true
print('Best frame rate: ' .. math.round(glBestFPS))
glBestFPS = 0
end
else
glPrinted = false
end
end
----------------------------------------------------------------------------------------------------------------------
function createWindow()
glWindow = gui.window({
title = 'Particle Benchmark',
insideWidth = 800,
insideHeight = 800,
events = {
close = function(Window)
mSys.UpdateTimer(glTimerHandle, 0)
glWindow = nil
mSys.SendMessage(MSGID_QUIT)
end
}
})
glSurface = glWindow.surface
glViewport = glWindow:clientViewport({ aspectRatio = ARF_MEET })
glScene = glViewport.scene
glScene.flags = bit.bor(glScene.flags, VPF_RENDER_TIME)
--glViewport.new('VectorRectangle', { x=0, y=0, width='100%', height='100%', fill='rgb(255,255,255)' })
--glFPS = glViewport.new('VectorText', { x=10, y = 30, string='FPS:', fill='rgb(0,0,0)' })
glWinWidth = glViewport.width
glWinHeight = glViewport.height
end
----------------------------------------------------------------------------------------------------------------------
-- There are three versions of this benchmark:
MODE_PRIMITIVE = 1 -- Tests the speed of drawing primitive rectangles (this will be much faster than drawVector() but is useful for comparative purposes)
MODE_VECTOR = 2 -- Tests the speed of drawing circles via the scene graph and VectorEllipse objects
MODE_DIRECTVECTOR = 3 -- Tests the drawing speed of DrawPath() and bypasses the scene graph.
function setBenchmarkMode()
local reqMode = arg('mode', 'vector')
if (reqMode == 'primitive') then
glMode = MODE_PRIMITIVE
elseif (reqMode == 'directvector') then
glMode = MODE_DIRECTVECTOR
else
glMode = MODE_VECTOR
end
end
----------------------------------------------------------------------------------------------------------------------
createWindow()
setBenchmarkMode()
glTimes = {}
if (glMode == MODE_PRIMITIVE) then
print('Drawing Mode: Primitive')
glSurface.mtAddCallback(drawPrimitive)
glColours = {
{ r=0xcc, g=0, b=0 }, { r=0xff, g=0xcc, b=0 }, { r=0xaa, g=0xff, b=0 },
{ r=0, g=0x99, b=0xcc }, { r=0x19, g=0x4c, b=0x99 }, { r=0x66, g=0x19, b=0x99 }
}
elseif (glMode == MODE_VECTOR) then
print('Drawing Mode: Vector')
glColours = {
{ r=0xcc, g=0, b=0 }, { r=0xff, g=0xcc, b=0 }, { r=0xaa, g=0xff, b=0 },
{ r=0, g=0x99, b=0xcc }, { r=0x19, g=0x4c, b=0x99 }, { r=0x66, g=0x19, b=0x99 }
}
elseif (glMode == MODE_DIRECTVECTOR) then
print('Drawing Mode: Direct Vector')
glSurface.mtAddCallback(drawDirectVector)
glColours = {
obj.new('VectorColour', { red=0.8, green=0, blue=0 }),
obj.new('VectorColour', { red=1.0, green=0.8, blue=0 }),
obj.new('Vectorcolour', { red=0.666, green=1.0, blue=0 }),
obj.new('Vectorcolour', { red=0, green=0.6, blue=0.8 }),
obj.new('Vectorcolour', { red=0.1, green=0.3, blue=0.6 }),
obj.new('Vectorcolour', { red=0.4, green=0.1, blue=0.6 })
}
end
glParticles = {}
local max = tonumber(nz(arg('maxParticles'),500))
if (max < 100) then max = 100 end
for i=1,max do
table.insert(glParticles, newParticle())
end
-- Run at a high frequency to ensure that the CPU performance is not scaled back
err, glTimerHandle = mSys.SubscribeTimer(0.001, timer)
glWindow:show()
processing.sleep()