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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
|
/*
* Copyright 2014 Stanlo Slasinski. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "common.h"
#include "bgfx_utils.h"
#include "imgui/imgui.h"
#include "camera.h"
#include <bgfx/bgfx.h>
namespace
{
static const char* s_shapeNames[] =
{
"Point",
"Sphere",
"Box",
"Donut"
};
struct ParamsData
{
float timeStep;
int32_t dispatchSize;
float gravity;
float damping;
float particleIntensity;
float particleSize;
int32_t baseSeed;
float particlePower;
float initialSpeed;
int32_t initialShape;
float maxAccel;
};
void initializeParams(int32_t _mode, ParamsData* _params)
{
switch(_mode)
{
case 0:
_params->timeStep = 0.0067f;
_params->dispatchSize = 32;
_params->gravity = 0.069f;
_params->damping = 0.0f;
_params->particleIntensity = 0.35f;
_params->particleSize = 0.925f;
_params->baseSeed = 0;
_params->particlePower = 5.0f;
_params->initialSpeed = 122.6f;
_params->initialShape = 0;
_params->maxAccel = 30.0;
break;
case 1:
_params->timeStep = 0.0157f;
_params->dispatchSize = 32;
_params->gravity = 0.109f;
_params->damping = 0.25f;
_params->particleIntensity = 0.64f;
_params->particleSize = 0.279f;
_params->baseSeed = 57;
_params->particlePower = 3.5f;
_params->initialSpeed = 3.2f;
_params->initialShape = 1;
_params->maxAccel = 100.0;
break;
case 2:
_params->timeStep = 0.02f;
_params->dispatchSize = 32;
_params->gravity = 0.24f;
_params->damping = 0.12f;
_params->particleIntensity = 1.0f;
_params->particleSize = 1.0f;
_params->baseSeed = 23;
_params->particlePower = 4.0f;
_params->initialSpeed = 31.1f;
_params->initialShape = 2;
_params->maxAccel = 39.29f;
break;
case 3:
_params->timeStep = 0.0118f;
_params->dispatchSize = 32;
_params->gravity = 0.141f;
_params->damping = 1.0f;
_params->particleIntensity = 0.64f;
_params->particleSize = 0.28f;
_params->baseSeed = 60;
_params->particlePower = 1.97f;
_params->initialSpeed = 69.7f;
_params->initialShape = 3;
_params->maxAccel = 3.21f;
break;
}
}
static const float s_quadVertices[] =
{
1.0f, 1.0f,
-1.0f, 1.0f,
-1.0f, -1.0f,
1.0f, -1.0f,
};
static const uint16_t s_quadIndices[] = { 0, 1, 2, 2, 3, 0, };
const uint32_t kThreadGroupUpdateSize = 512;
const uint32_t kMaxParticleCount = 32 * 1024;
class ExampleNbody : public entry::AppI
{
public:
ExampleNbody(const char* _name, const char* _description)
: entry::AppI(_name, _description)
{
}
void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
{
Args args(_argc, _argv);
m_width = _width;
m_height = _height;
m_debug = BGFX_DEBUG_NONE;
m_reset = BGFX_RESET_VSYNC;
bgfx::init(args.m_type, args.m_pciId);
bgfx::reset(m_width, m_height, m_reset);
// Enable debug text.
bgfx::setDebug(m_debug);
// Set view 0 clear state.
bgfx::setViewClear(0
, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
, 0x303030ff
, 1.0f
, 0
);
const bgfx::Caps* caps = bgfx::getCaps();
m_computeSupported = !!(caps->supported & BGFX_CAPS_COMPUTE);
m_indirectSupported = !!(caps->supported & BGFX_CAPS_DRAW_INDIRECT);
imguiCreate();
if (m_computeSupported)
{
bgfx::VertexDecl quadVertexDecl;
quadVertexDecl.begin()
.add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
.end();
// Create static vertex buffer.
m_vbh = bgfx::createVertexBuffer(
// Static data can be passed with bgfx::makeRef
bgfx::makeRef(s_quadVertices, sizeof(s_quadVertices) )
, quadVertexDecl
);
// Create static index buffer.
m_ibh = bgfx::createIndexBuffer(
// Static data can be passed with bgfx::makeRef
bgfx::makeRef(s_quadIndices, sizeof(s_quadIndices) )
);
// Create particle program from shaders.
m_particleProgram = loadProgram("vs_particle", "fs_particle");
// Setup compute buffers
bgfx::VertexDecl computeVertexDecl;
computeVertexDecl.begin()
.add(bgfx::Attrib::TexCoord0, 4, bgfx::AttribType::Float)
.end();
m_currPositionBuffer0 = bgfx::createDynamicVertexBuffer(1 << 15, computeVertexDecl, BGFX_BUFFER_COMPUTE_READ_WRITE);
m_currPositionBuffer1 = bgfx::createDynamicVertexBuffer(1 << 15, computeVertexDecl, BGFX_BUFFER_COMPUTE_READ_WRITE);
m_prevPositionBuffer0 = bgfx::createDynamicVertexBuffer(1 << 15, computeVertexDecl, BGFX_BUFFER_COMPUTE_READ_WRITE);
m_prevPositionBuffer1 = bgfx::createDynamicVertexBuffer(1 << 15, computeVertexDecl, BGFX_BUFFER_COMPUTE_READ_WRITE);
u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4, 3);
m_initInstancesProgram = bgfx::createProgram(loadShader("cs_init_instances"), true);
m_updateInstancesProgram = bgfx::createProgram(loadShader("cs_update_instances"), true);
m_indirectProgram = BGFX_INVALID_HANDLE;
m_indirectBuffer = BGFX_INVALID_HANDLE;
if (m_indirectSupported)
{
m_indirectProgram = bgfx::createProgram(loadShader("cs_indirect"), true);
m_indirectBuffer = bgfx::createIndirectBuffer(2);
}
initializeParams(0, &m_paramsData);
bgfx::setUniform(u_params, &m_paramsData, 3);
bgfx::setBuffer(0, m_prevPositionBuffer0, bgfx::Access::Write);
bgfx::setBuffer(1, m_currPositionBuffer0, bgfx::Access::Write);
bgfx::dispatch(0, m_initInstancesProgram, kMaxParticleCount / kThreadGroupUpdateSize, 1, 1);
float initialPos[3] = { 0.0f, 0.0f, -45.0f };
cameraCreate();
cameraSetPosition(initialPos);
cameraSetVerticalAngle(0.0f);
m_useIndirect = false;
m_timeOffset = bx::getHPCounter();
}
}
virtual int shutdown() override
{
// Cleanup.
cameraDestroy();
imguiDestroy();
if (m_computeSupported)
{
if (m_indirectSupported)
{
bgfx::destroy(m_indirectProgram);
bgfx::destroy(m_indirectBuffer);
}
bgfx::destroy(u_params);
bgfx::destroy(m_currPositionBuffer0);
bgfx::destroy(m_currPositionBuffer1);
bgfx::destroy(m_prevPositionBuffer0);
bgfx::destroy(m_prevPositionBuffer1);
bgfx::destroy(m_updateInstancesProgram);
bgfx::destroy(m_initInstancesProgram);
bgfx::destroy(m_ibh);
bgfx::destroy(m_vbh);
bgfx::destroy(m_particleProgram);
}
// Shutdown bgfx.
bgfx::shutdown();
return 0;
}
bool update() override
{
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
{
imguiBeginFrame(
m_mouseState.m_mx
, m_mouseState.m_my
, (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
| (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
| (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
, m_mouseState.m_mz
, uint16_t(m_width)
, uint16_t(m_height)
);
showExampleDialog(this
, !m_computeSupported
? "Compute is not supported."
: NULL
);
int64_t now = bx::getHPCounter();
static int64_t last = now;
const int64_t frameTime = now - last;
last = now;
const double freq = double(bx::getHPFrequency() );
const float deltaTime = float(frameTime/freq);
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
if (m_computeSupported)
{
ImGui::SetNextWindowPos(
ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
, ImGuiSetCond_FirstUseEver
);
ImGui::Begin("Settings"
, NULL
, ImVec2(m_width / 5.0f, m_height / 1.5f)
, ImGuiWindowFlags_AlwaysAutoResize
);
bool reset = false;
int32_t shape = m_paramsData.initialShape;
if (ImGui::Combo("Initial shape", &shape, s_shapeNames, BX_COUNTOF(s_shapeNames) ) )
{
// Modify parameters and reset if shape is changed
initializeParams(shape, &m_paramsData);
reset = true;
}
ImGui::SliderInt("Random seed", &m_paramsData.baseSeed, 0, 100);
if (ImGui::Button("Reset") )
{
reset = true;
}
ImGui::Separator();
ImGui::SliderInt("Particle count (x512)", &m_paramsData.dispatchSize, 1, 64);
ImGui::SliderFloat("Gravity", &m_paramsData.gravity, 0.0f, 0.3f);
ImGui::SliderFloat("Damping", &m_paramsData.damping, 0.0f, 1.0f);
ImGui::SliderFloat("Max acceleration", &m_paramsData.maxAccel, 0.0f, 100.0f);
ImGui::SliderFloat("Time step", &m_paramsData.timeStep, 0.0f, 0.02f);
ImGui::Separator();
ImGui::SliderFloat("Particle intensity", &m_paramsData.particleIntensity, 0.0f, 1.0f);
ImGui::SliderFloat("Particle size", &m_paramsData.particleSize, 0.0f, 1.0f);
ImGui::SliderFloat("Particle power", &m_paramsData.particlePower, 0.001f, 16.0f);
ImGui::Separator();
if (m_indirectSupported)
{
ImGui::Checkbox("Use draw/dispatch indirect", &m_useIndirect);
}
ImGui::End();
if (reset)
{
bgfx::setBuffer(0, m_prevPositionBuffer0, bgfx::Access::Write);
bgfx::setBuffer(1, m_currPositionBuffer0, bgfx::Access::Write);
bgfx::setUniform(u_params, &m_paramsData, 3);
bgfx::dispatch(0, m_initInstancesProgram, kMaxParticleCount / kThreadGroupUpdateSize, 1, 1);
}
if (m_useIndirect)
{
bgfx::setUniform(u_params, &m_paramsData, 3);
bgfx::setBuffer(0, m_indirectBuffer, bgfx::Access::Write);
bgfx::dispatch(0, m_indirectProgram);
}
bgfx::setBuffer(0, m_prevPositionBuffer0, bgfx::Access::Read);
bgfx::setBuffer(1, m_currPositionBuffer0, bgfx::Access::Read);
bgfx::setBuffer(2, m_prevPositionBuffer1, bgfx::Access::Write);
bgfx::setBuffer(3, m_currPositionBuffer1, bgfx::Access::Write);
bgfx::setUniform(u_params, &m_paramsData, 3);
if (m_useIndirect)
{
bgfx::dispatch(0, m_updateInstancesProgram, m_indirectBuffer, 1);
}
else
{
bgfx::dispatch(0, m_updateInstancesProgram, uint16_t(m_paramsData.dispatchSize), 1, 1);
}
bx::xchg(m_currPositionBuffer0, m_currPositionBuffer1);
bx::xchg(m_prevPositionBuffer0, m_prevPositionBuffer1);
// Update camera.
cameraUpdate(deltaTime, m_mouseState);
float view[16];
cameraGetViewMtx(view);
// Set view and projection matrix for view 0.
const bgfx::HMD* hmd = bgfx::getHMD();
if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
{
float viewHead[16];
float eye[3] = {};
bx::mtxQuatTranslationHMD(viewHead, hmd->eye[0].rotation, eye);
float tmp[16];
bx::mtxMul(tmp, view, viewHead);
bgfx::setViewTransform(
0
, tmp
, hmd->eye[0].projection
, BGFX_VIEW_STEREO
, hmd->eye[1].projection
);
// Set view 0 default viewport.
//
// Use HMD's width/height since HMD's internal frame buffer size
// might be much larger than window size.
bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
}
else
{
float proj[16];
bx::mtxProj(
proj
, 90.0f
, float(m_width)/float(m_height)
, 0.1f
, 10000.0f
, bgfx::getCaps()->homogeneousDepth
);
bgfx::setViewTransform(0, view, proj);
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
}
// Set vertex and index buffer.
bgfx::setVertexBuffer(0, m_vbh);
bgfx::setIndexBuffer(m_ibh);
bgfx::setInstanceDataBuffer(m_currPositionBuffer0
, 0
, m_paramsData.dispatchSize * kThreadGroupUpdateSize
);
// Set render states.
bgfx::setState(0
| BGFX_STATE_RGB_WRITE
| BGFX_STATE_BLEND_ADD
| BGFX_STATE_DEPTH_TEST_ALWAYS
);
// Submit primitive for rendering to view 0.
if (m_useIndirect)
{
bgfx::submit(0, m_particleProgram, m_indirectBuffer, 0);
}
else
{
bgfx::submit(0, m_particleProgram);
}
}
imguiEndFrame();
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
return true;
}
return false;
}
entry::MouseState m_mouseState;
uint32_t m_width;
uint32_t m_height;
uint32_t m_debug;
uint32_t m_reset;
bool m_useIndirect;
bool m_computeSupported;
bool m_indirectSupported;
ParamsData m_paramsData;
bgfx::VertexBufferHandle m_vbh;
bgfx::IndexBufferHandle m_ibh;
bgfx::ProgramHandle m_particleProgram;
bgfx::ProgramHandle m_indirectProgram;
bgfx::ProgramHandle m_initInstancesProgram;
bgfx::ProgramHandle m_updateInstancesProgram;
bgfx::IndirectBufferHandle m_indirectBuffer;
bgfx::DynamicVertexBufferHandle m_currPositionBuffer0;
bgfx::DynamicVertexBufferHandle m_currPositionBuffer1;
bgfx::DynamicVertexBufferHandle m_prevPositionBuffer0;
bgfx::DynamicVertexBufferHandle m_prevPositionBuffer1;
bgfx::UniformHandle u_params;
int64_t m_timeOffset;
};
} // namespace
ENTRY_IMPLEMENT_MAIN(ExampleNbody, "24-nbody", "N-body simulation with compute shaders using buffers.");
|