Skip to content

Commit 067b148

Browse files
committed
Selection works on one layer, adding more
1 parent fda196e commit 067b148

File tree

6 files changed

+143
-33
lines changed

6 files changed

+143
-33
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,19 @@ Lua51_ROOT = "C:\<path_to_cpp-skill>\windows\lua"
4141

4242
## Build
4343

44-
```
44+
Normally
45+
46+
```bash
4547
cmake -DCMAKE_BUILD_TYPE=Release -S . -B build
4648
cmake --build build --target all
4749
```
4850

51+
On Windows for Visual Studio to have nice stack traces
52+
53+
```shell
54+
cmake -DCMAKE_BUILD_TYPE=Debug -S . -B build_msvc -G "Visual Studio 17 2022"
55+
```
56+
4957
## The book
5058

5159
Run locally:

graphics/CMakeLists.txt

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,14 @@ add_executable(neurons_gui
229229
neurons/render/neuron_render.hpp
230230
neurons/render/neuron_render.cpp
231231
neurons/render/network_render.hpp
232+
# neurons/camera.cpp
233+
# neurons/mouse.cpp
234+
# neurons/settings.cpp
232235
neurons/neurons_gui.cpp
233236
)
234237

235238
target_compile_definitions(neurons_gui PRIVATE BX_CONFIG_DEBUG=0)
239+
target_compile_definitions(neurons_gui PRIVATE USE_ENTRY=1)
236240
target_compile_definitions(neurons_gui PRIVATE ENTRY_CONFIG_USE_SDL=1)
237241
target_compile_definitions(neurons_gui PRIVATE IMGUI_INCLUDE_IMGUI_USER_H)
238242
target_compile_definitions(neurons_gui PRIVATE IMGUI_INCLUDE_IMGUI_USER_INL)
@@ -265,4 +269,75 @@ target_link_libraries(neurons_gui
265269
bimg${CMAKE_BUILD_TYPE}
266270
bimg_decode${CMAKE_BUILD_TYPE}
267271
bx${CMAKE_BUILD_TYPE}
272+
)
273+
274+
###############################################
275+
276+
add_executable(brain
277+
bgfx_ext/bgfx_utils.cpp
278+
bgfx_ext/entry/cmd.cpp
279+
bgfx_ext/entry/dialog.cpp
280+
bgfx_ext/entry/entry.cpp
281+
bgfx_ext/entry/entry_sdl.cpp
282+
bgfx_ext/entry/input.cpp
283+
bgfx_ext/imgui/bgfx_imgui.cpp
284+
bgfx_ext/example-glue.cpp
285+
thirdparty/meshoptimizer/src/allocator.cpp
286+
thirdparty/meshoptimizer/src/clusterizer.cpp
287+
thirdparty/meshoptimizer/src/indexanalyzer.cpp
288+
thirdparty/meshoptimizer/src/indexcodec.cpp
289+
thirdparty/meshoptimizer/src/indexgenerator.cpp
290+
thirdparty/meshoptimizer/src/overdrawoptimizer.cpp
291+
thirdparty/meshoptimizer/src/partition.cpp
292+
thirdparty/meshoptimizer/src/quantization.cpp
293+
thirdparty/meshoptimizer/src/rasterizer.cpp
294+
thirdparty/meshoptimizer/src/simplifier.cpp
295+
thirdparty/meshoptimizer/src/spatialorder.cpp
296+
thirdparty/meshoptimizer/src/stripifier.cpp
297+
thirdparty/meshoptimizer/src/vcacheoptimizer.cpp
298+
thirdparty/meshoptimizer/src/vertexcodec.cpp
299+
thirdparty/meshoptimizer/src/vertexfilter.cpp
300+
thirdparty/meshoptimizer/src/vfetchoptimizer.cpp
301+
thirdparty/imgui/imgui.cpp
302+
thirdparty/imgui/imgui_widgets.cpp
303+
thirdparty/imgui/imgui_tables.cpp
304+
thirdparty/imgui/imgui_draw.cpp
305+
thirdparty/imgui/backends/imgui_impl_opengl3.cpp
306+
thirdparty/imgui/backends/imgui_impl_sdl2.cpp
307+
neurons/brain.cpp
308+
)
309+
310+
target_compile_definitions(brain PRIVATE BX_CONFIG_DEBUG=0)
311+
target_compile_definitions(brain PRIVATE ENTRY_CONFIG_USE_SDL=1)
312+
target_compile_definitions(brain PRIVATE IMGUI_INCLUDE_IMGUI_USER_H)
313+
target_compile_definitions(brain PRIVATE IMGUI_INCLUDE_IMGUI_USER_INL)
314+
315+
target_include_directories(brain PRIVATE
316+
thirdparty
317+
thirdparty/bgfx/include
318+
thirdparty/bimg/include
319+
thirdparty/bx/include
320+
thirdparty/imgui
321+
thirdparty/imgui/backends
322+
bgfx_ext
323+
imgui_ext
324+
neurons
325+
)
326+
327+
if (MSVC)
328+
set_property(TARGET brain PROPERTY
329+
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
330+
endif ()
331+
332+
target_link_directories(brain PUBLIC
333+
thirdparty/bgfx/.build/win64_vs2022/bin)
334+
335+
target_link_libraries(brain
336+
OpenGL::GL
337+
${SDL2_LIBRARIES}
338+
dirent
339+
bgfx${CMAKE_BUILD_TYPE}
340+
bimg${CMAKE_BUILD_TYPE}
341+
bimg_decode${CMAKE_BUILD_TYPE}
342+
bx${CMAKE_BUILD_TYPE}
268343
)

graphics/neurons/neurons_gui.cpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ const int32_t width = 28, height = 28;
152152
, uint16_t(m_height)
153153
);
154154

155-
156155
showExampleDialog(this);
157156

158157
ImGui::SetNextWindowPos(
@@ -250,6 +249,29 @@ const int32_t width = 28, height = 28;
250249
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "Draw call limit reached!");
251250
}
252251

252+
253+
data.set_current_id(current_image);
254+
sim_clock.update(deltaTimeSec);
255+
256+
257+
if (sim_clock.advance()) {
258+
net.step(data.convert_current_to_inputs());
259+
std::vector<float> voltage_state = net.get_current_voltage_state();
260+
float t = sim_clock.store(voltage_state);
261+
262+
}
263+
264+
// float matched_time;
265+
// if (sim_clock.has_seen_state(neuron_state, matched_time)) {
266+
// std::cout << "State repeated from time: " << matched_time << " ms\n";
267+
// }
268+
269+
net.update(time);
270+
net.draw(time);
271+
272+
273+
274+
253275
ImGui::End();
254276

255277
imguiEndFrame();
@@ -341,25 +363,7 @@ const int32_t width = 28, height = 28;
341363

342364
m_lastFrameMissing = 0;
343365

344-
data.set_current_id(current_image);
345-
sim_clock.update(deltaTimeSec);
346-
347-
348-
if (sim_clock.advance()) {
349-
net.step(data.convert_current_to_inputs());
350-
std::vector<float> voltage_state = net.get_current_voltage_state();
351-
float t = sim_clock.store(voltage_state);
352-
353-
}
354-
355-
// float matched_time;
356-
// if (sim_clock.has_seen_state(neuron_state, matched_time)) {
357-
// std::cout << "State repeated from time: " << matched_time << " ms\n";
358-
// }
359-
360-
net.update(time);
361-
net.draw(time);
362-
366+
363367
// Advance to next frame. Rendering thread will be kicked to
364368
// process submitted rendering primitives.
365369
bgfx::frame();

graphics/neurons/render/network_render.hpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ struct NetworkRenderStrategy : RenderStrategy {
2525

2626
NetworkRenderStrategy(std::shared_ptr<NetworkVisualContext> ctx) : ctx(ctx) {}
2727

28+
void define_min_area_size() {
29+
const size_t n_neurons = ctx->net.neurons.size();
30+
area_size_x = bx::ceil(bx::sqrt(float(n_neurons / width)));
31+
area_size_y = area_size_x;
32+
area_size_z = 1;
33+
}
34+
2835
void init() override {
2936
m_lastFrameMissing = 0;
3037
// 80 bytes stride = 64 bytes for 4x4 matrix + 16 bytes for RGBA color.
@@ -95,17 +102,23 @@ struct NetworkRenderStrategy : RenderStrategy {
95102
m_color[0] = 0.70f;
96103
m_color[1] = 0.65f;
97104
m_color[2] = 0.60f;
105+
m_color[3] = 1.0f;
98106

99-
m_program = loadProgram("vs_instancing", "fs_instancing");
100107
m_selection_program = loadProgram("vs_selection", "fs_selection");
108+
u_color = bgfx::createUniform("u_color", bgfx::UniformType::Vec4);
101109

110+
m_program = loadProgram("vs_instancing", "fs_instancing");
102111
}
103112

104113
void update(float dt) override {
105-
if (ImGui::IsKeyPressed(ImGuiKey_W)) selected_y = std::max(0, selected_y - 1);
106-
if (ImGui::IsKeyPressed(ImGuiKey_S)) selected_y = std::min(height - 1, selected_y + 1);
107-
if (ImGui::IsKeyPressed(ImGuiKey_A)) selected_x = std::max(0, selected_x - 1);
108-
if (ImGui::IsKeyPressed(ImGuiKey_D)) selected_x = std::min(width - 1, selected_x + 1);
114+
if (ImGui::IsKeyPressed(ImGuiKey_W))
115+
selected_y = std::max(0, selected_y - 1);
116+
if (ImGui::IsKeyPressed(ImGuiKey_S))
117+
selected_y = std::min(height - 1, selected_y + 1);
118+
if (ImGui::IsKeyPressed(ImGuiKey_A))
119+
selected_x = std::max(0, selected_x - 1);
120+
if (ImGui::IsKeyPressed(ImGuiKey_D))
121+
selected_x = std::min(width - 1, selected_x + 1);
109122

110123
// to total number of instances to draw
111124
uint32_t totalCubes = ctx->net.neurons.size();
@@ -129,6 +142,7 @@ struct NetworkRenderStrategy : RenderStrategy {
129142
const float offset = 3.0f;
130143
const float start_x = - float(n_neurons / width) * offset / 2.0f;
131144
const float start_y = - float(n_neurons / width) * offset / 2.0f;
145+
const float start_z = - float(n_neurons / width) * offset / 2.0f;
132146

133147
for (uint32_t ii = 0; ii < drawnCubes; ++ii)
134148
{
@@ -190,7 +204,7 @@ struct NetworkRenderStrategy : RenderStrategy {
190204
// Compute selection position
191205
float sel_x = start_x + selected_x * offset;
192206
float sel_y = start_y + selected_y * offset;
193-
float sel_z = 0.0f;
207+
float sel_z = start_z + selected_z * offset;
194208

195209
// Create transform matrix
196210
float selMtx[16];
@@ -210,6 +224,8 @@ struct NetworkRenderStrategy : RenderStrategy {
210224
}
211225

212226
void destroy() override {
227+
bgfx::destroy(u_color);
228+
bgfx::destroy(m_selection_program);
213229
bgfx::destroy(m_program);
214230
}
215231

@@ -224,8 +240,13 @@ struct NetworkRenderStrategy : RenderStrategy {
224240
uint32_t m_lastFrameMissing;
225241
uint32_t m_sideSize;
226242

243+
int32_t area_size_x = 0;
244+
int32_t area_size_y = 0;
245+
int32_t area_size_z = 0;
246+
227247
int32_t selected_x = 0;
228248
int32_t selected_y = 0;
249+
int32_t selected_z = 0;
229250

230251
float m_color[4];
231252
bgfx::UniformHandle u_color;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vec3 a_position : POSITION;

src/graphics/README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
Build shaders
44

5-
```powershell
6-
cd graphics\neurons\shaders>
7-
shaderc.exe -i ..\..\thirdparty\bgfx\src -f fs_instancing.sc --varyingdef varying_instancing.def.sc -o fs_instancing.bin --type fragment --platform windows --profile s_5_0 -O 3
8-
shaderc.exe -i ..\..\thirdparty\bgfx\src -f vs_instancing.sc --varyingdef varying_instancing.def.sc -o vs_instancing.bin --type vertex --platform windows --profile s_5_0 -O 3
9-
shaderc.exe -i ..\..\thirdparty\bgfx\src -f fs_cubes.sc -o fs_cubes.bin --varyingdef varying_cubes.def.sc --type fragment --platform windows --profile s_5_0 -O 3
10-
shaderc.exe -i ..\..\thirdparty\bgfx\src -f vs_cubes.sc -o vs_cubes.bin --varyingdef varying_cubes.def.sc --type vertex --platform windows --profile s_5_0 -O 3
5+
```sh
6+
cd graphics/neurons/shaders
7+
shaderc.exe -i ../../thirdparty/bgfx/src -f fs_instancing.sc --varyingdef varying_instancing.def.sc -o fs_instancing.bin --type fragment --platform windows --profile s_5_0 -O 3
8+
shaderc.exe -i ../../thirdparty/bgfx/src -f vs_instancing.sc --varyingdef varying_instancing.def.sc -o vs_instancing.bin --type vertex --platform windows --profile s_5_0 -O 3
9+
10+
shaderc.exe -i ../../thirdparty/bgfx/src -f fs_cubes.sc -o fs_cubes.bin --varyingdef varying_cubes.def.sc --type fragment --platform windows --profile s_5_0 -O 3
11+
shaderc.exe -i ../../thirdparty/bgfx/src -f vs_cubes.sc -o vs_cubes.bin --varyingdef varying_cubes.def.sc --type vertex --platform windows --profile s_5_0 -O 3
1112
```

0 commit comments

Comments
 (0)