Skip to content

Commit 081cec1

Browse files
committed
Empty field first
1 parent d15da76 commit 081cec1

File tree

8 files changed

+130
-49
lines changed

8 files changed

+130
-49
lines changed

graphics/neurons/data_processor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#ifndef DATA_PROCESSOR_H
22
#define DATA_PROCESSOR_H
33

4-
#include <bx/math.h>
4+
#include <vector>
55

66

77
class DataProcessor {
8-
virtual bx::Vec3 get_area_size() const = 0;
8+
virtual std::vector<size_t> get_area_size() const = 0;
99
};
1010

1111

graphics/neurons/mnist_data_processor.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ bgfx::TextureHandle create_mnist_texture(const uint8_t* pixels) {
2323
rgba[i * 4 + 3] = 255;
2424
}
2525

26-
const bgfx::Memory* mem = bgfx::copy(rgba.data(), rgba.size());
26+
const bgfx::Memory* mem = bgfx::copy(rgba.data(), static_cast<uint32_t>(rgba.size()));
2727
return bgfx::createTexture2D(width, height, false, 1, bgfx::TextureFormat::RGBA8, 0, mem);
2828
}
2929

@@ -37,8 +37,8 @@ size_t MnistDataProcessor::get_input_size() const {
3737
return input_size;
3838
}
3939

40-
bx::Vec3 MnistDataProcessor::get_area_size() const {
41-
return { width, height, 1.0f };
40+
std::vector<size_t> MnistDataProcessor::get_area_size() const {
41+
return { width, height, 1 };
4242
}
4343

4444
size_t MnistDataProcessor::get_max_id() const {
@@ -79,16 +79,16 @@ std::vector<uint8_t> MnistDataProcessor::convert_current_to_inputs() {
7979
return inputs;
8080
}
8181

82-
Network::NeuronLayer MnistDataProcessor::prepare_neurons() const {
83-
Network::NeuronLayer neurons(input_size);
84-
bx::Vec3 area_size = get_area_size();
82+
NeuronLayer MnistDataProcessor::prepare_neurons() const {
83+
NeuronLayer neurons(input_size);
84+
const auto area_size = get_area_size();
8585

8686
for (size_t i = 0; i < input_size; ++i) {
8787
neurons[i] = std::make_shared<Neuron>();
8888
auto ctx = std::make_shared<NeuronVisualContext>(neurons[i]);
8989
ctx->position = {
90-
float(i % int32_t(area_size.x)),
91-
i / area_size.x,
90+
float(i % area_size[0]),
91+
float(i / area_size[0]),
9292
0.0f };
9393
neurons[i]->render = std::make_shared<NeuronRenderStrategy>(ctx);
9494
}

graphics/neurons/mnist_data_processor.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include "data_processor.h"
1414
#include "neuron.hpp"
15-
#include "network.hpp"
15+
#include "network.h"
1616

1717
const size_t input_size = 784;
1818
const uint32_t width = 28, height = 28;
@@ -33,15 +33,15 @@ class MnistDataProcessor : public DataProcessor {
3333
void init();
3434

3535
size_t get_input_size() const;
36-
bx::Vec3 get_area_size() const override;
36+
std::vector<size_t> get_area_size() const override;
3737
size_t get_max_id() const;
3838
size_t get_current_id() const;
3939
uint8_t get_current_label() const;
4040
void set_current_id(size_t id);
4141
bgfx::TextureHandle create_currnet_texture();
4242
std::vector<uint8_t> convert_current_to_inputs();
4343

44-
Network::NeuronLayer prepare_neurons() const;
44+
NeuronLayer prepare_neurons() const;
4545

4646
};
4747

graphics/neurons/network.cpp

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,58 @@
11
#include <iostream>
22

33
#include "network.h"
4+
#include "render/neuron_render.hpp"
5+
#include "render/network_render.h"
46

5-
void Network::addLayer(const NeuronLayer&& layer) {
7+
8+
void Network::addLayer(const NeuronLayer&& layer, const std::vector<size_t>& area_size) {
69
layers.push_back(layer);
7-
neurons.assign(layer.begin(), layer.end());
10+
11+
uint32_t last_id = neurons.back()->idx;
12+
for (auto it = layer.begin(); it != layer.end(); ++it)
13+
{
14+
(*it)->idx = ++last_id;
15+
neurons.emplace_back(std::move(*it));
16+
}
17+
18+
// neurons.assign(layer.begin(), layer.end());
819
if (render && std::dynamic_pointer_cast<NetworkRenderStrategy>(render) != nullptr) {
9-
std::dynamic_pointer_cast<NetworkRenderStrategy>(render)->addLayer(layer);
20+
std::dynamic_pointer_cast<NetworkRenderStrategy>(render)->addLayer(layer, area_size);
1021
}
1122
}
1223

13-
// void setSize(int N) {
14-
// neurons.resize(N);
15-
// // synapses.resize(N, std::vector<Synapse>(N));
16-
// }
24+
void Network::addNeuron(const std::vector<size_t>& pos, const NeuronLayer& connected_to) {
25+
uint32_t layer = pos[2];
26+
if (layer >= layers.size()) {
27+
std::cerr << "wrong layer position\n";
28+
return;
29+
}
30+
31+
std::shared_ptr<Neuron> neuron = std::make_shared<Neuron>();
32+
neuron->idx = create_id(nullptr);
33+
auto ctx = std::make_shared<NeuronVisualContext>(neuron);
34+
ctx->position = { float(pos[0]), float(pos[1]), float(pos[2]) };
35+
neuron->render = std::make_shared<NeuronRenderStrategy>(ctx);
36+
37+
layers[layer].push_back(neuron);
38+
neurons.push_back(neuron);
39+
}
40+
41+
void Network::addConnection(const std::shared_ptr<Neuron>& n1, const std::shared_ptr<Neuron>& n2) {
42+
synapses.emplace(idsToLocation(n1->idx, n2->idx), std::make_shared<Synapse>());
43+
}
44+
45+
std::pair<uint32_t, uint32_t> Network::locationToIds(uint64_t loc) const {
46+
uint32_t pre_idx = loc & 0xffff;
47+
uint32_t post_idx = (loc >> 8) & 0xffff;
48+
return {pre_idx, post_idx};
49+
}
50+
51+
uint64_t Network::idsToLocation(uint32_t pre, uint32_t post) const {
52+
uint64_t loc = 0;
53+
loc = pre | (post << 8);
54+
return loc;
55+
}
1756

1857
std::vector<float> Network::get_current_voltage_state() const {
1958
std::vector<float> state(neurons.size(), 0.0);
@@ -37,16 +76,15 @@ void Network::step(std::vector<uint8_t> inputs) {
3776

3877
// Synaptic input
3978
for (auto& [loc, syn] : synapses) {
40-
uint32_t pre_idx = loc & 0xffff;
41-
uint32_t post_idx = (loc >> 8) & 0xffff;
79+
auto [pre_idx, post_idx] = locationToIds(loc);
4280

43-
syn.update_pre(dt);
44-
syn.update_post(dt);
81+
syn->update_pre(dt);
82+
syn->update_post(dt);
4583

4684
// STDP
4785
if (neurons[pre_idx]->spiked) {
48-
syn.on_pre_spike();
49-
dv[post_idx] += syn.weight;
86+
syn->on_pre_spike();
87+
dv[post_idx] += syn->weight;
5088
}
5189
}
5290

@@ -56,10 +94,9 @@ void Network::step(std::vector<uint8_t> inputs) {
5694

5795
// STDP
5896
for (auto& [loc, syn] : synapses) {
59-
uint32_t pre_idx = loc & 0xffff;
60-
uint32_t post_idx = (loc >> 8) & 0xffff;
61-
syn.apply_stdp(neurons[pre_idx]->spiked, neurons[post_idx]->spiked);
62-
if (neurons[post_idx]->spiked) syn.on_post_spike();
97+
auto [pre_idx, post_idx] = locationToIds(loc);
98+
syn->apply_stdp(neurons[pre_idx]->spiked, neurons[post_idx]->spiked);
99+
if (neurons[post_idx]->spiked) syn->on_post_spike();
63100
}
64101

65102
time += dt;
@@ -85,4 +122,8 @@ void Network::update(float dt) {
85122

86123
void Network::destroy() const {
87124
if (render) render->destroy();
125+
}
126+
127+
uint32_t Network::create_id(std::shared_ptr<Neuron> neuron) const {
128+
return neurons.size();
88129
}

graphics/neurons/network.h

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,42 @@
88
#include "neuron.hpp"
99
#include "synapse.hpp"
1010
#include "render_strategy.h"
11-
#include "render/network_render.h"
1211

1312
using NeuronLayer = std::vector<std::shared_ptr<Neuron>>;
13+
using Neocortex = std::vector<NeuronLayer>; // lol
14+
using FlatNeuronStorage = std::vector<std::shared_ptr<Neuron>>;
15+
using Location = uint64_t;
16+
using SynapsesMap = std::unordered_map<Location, std::shared_ptr<Synapse>>;
1417

1518
struct Network {
16-
std::vector<std::shared_ptr<Neuron>> neurons;
19+
FlatNeuronStorage neurons;
1720
std::vector<NeuronLayer> layers;
18-
19-
using Location = uint64_t;
20-
std::unordered_map<Location, Synapse> synapses;
21+
SynapsesMap synapses;
2122
// std::vector<std::vector<Synapse>> synapses;
23+
2224
float dt = 1.0f;
2325
float time = 0.0f;
2426

2527
std::shared_ptr<RenderStrategy> render;
2628

2729
explicit Network() = default;
2830

29-
void addLayer(const NeuronLayer&& layer);
31+
void addLayer(const NeuronLayer&& layer, const std::vector<size_t>& area_size);
32+
void addNeuron(const std::vector<size_t>& pos, const NeuronLayer& connected_to);
33+
void addConnection(const std::shared_ptr<Neuron>& n1,
34+
const std::shared_ptr<Neuron>& n2);
35+
36+
std::pair<uint32_t, uint32_t> locationToIds(uint64_t loc) const;
37+
uint64_t idsToLocation(uint32_t pre, uint32_t post) const;
38+
3039
std::vector<float> get_current_voltage_state() const;
3140
void step(std::vector<uint8_t> inputs);
3241
void init();
3342
void draw(float time) const;
3443
void update(float dt);
3544
void destroy() const;
45+
46+
uint32_t create_id(std::shared_ptr<Neuron> neuron) const;
3647
};
3748

3849
#endif

graphics/neurons/neurons_gui.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "imgui/bgfx_imgui.h"
1717

1818
#include "neuron.hpp"
19-
#include "network.hpp"
19+
#include "network.h"
2020
#include "render/neuron_render.hpp"
2121
#include "render/network_render.h"
2222
#include "simulation_clock.hpp"
@@ -83,8 +83,11 @@ namespace
8383
net.render = std::make_shared<NetworkRenderStrategy>(ctx);
8484

8585

86-
net.addLayer(data.prepare_neurons());
87-
// bx::Vec3 area_size = data.get_area_size();
86+
bool loadReceptors = false;
87+
if (loadReceptors) {
88+
const auto area_size = data.get_area_size();
89+
net.addLayer(data.prepare_neurons(), area_size);
90+
}
8891

8992
// for (size_t i = 0; i < net.neurons.size(); ++i) {
9093
// auto ctx2 = std::make_shared<NeuronVisualContext>(net.neurons[i]);

graphics/neurons/render/network_render.cpp

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ void NetworkRenderStrategy::init() {
8181
m_program = loadProgram("vs_instancing", "fs_instancing");
8282
}
8383

84-
void NetworkRenderStrategy::addLayer(const NeuronLayer& layer) {}
84+
void NetworkRenderStrategy::addLayer(const NeuronLayer& layer, const std::vector<size_t>& area_size) {
85+
area_size_x = area_size[0];
86+
area_size_y = area_size[1];
87+
area_size_z = area_size[2];
88+
89+
}
8590

8691
void NetworkRenderStrategy::update(float dt) {
8792
if (ImGui::IsKeyPressed(ImGuiKey_W))
@@ -98,19 +103,23 @@ void NetworkRenderStrategy::update(float dt) {
98103
selected_x = std::max(0, selected_z - 1);
99104
if (ImGui::IsKeyPressed(ImGuiKey_F))
100105
selected_x = std::min(area_size_z - 1, selected_z + 1);
101-
102-
// to total number of instances to draw
106+
107+
// to total number of instances to draw
103108
uint32_t totalCubes = ctx->net.neurons.size();
104109

105-
// figure out how big of a buffer is available
106-
drawnCubes = bgfx::getAvailInstanceDataBuffer(totalCubes, instanceStride);
110+
if (totalCubes > 0) {
111+
// figure out how big of a buffer is available
112+
drawnCubes = bgfx::getAvailInstanceDataBuffer(totalCubes, instanceStride);
107113

108-
// save how many we couldn't draw due to buffer room so we can display it
109-
m_lastFrameMissing = totalCubes - drawnCubes;
114+
// save how many we couldn't draw due to buffer room so we can display it
115+
m_lastFrameMissing = totalCubes - drawnCubes;
116+
} else {
117+
drawnCubes = 0;
118+
m_lastFrameMissing = 0;
119+
}
110120
}
111121

112-
void NetworkRenderStrategy::draw(float time) const {
113-
122+
void NetworkRenderStrategy::drawNeurons(float time) const {
114123
bgfx::InstanceDataBuffer idb;
115124
bgfx::allocInstanceDataBuffer(&idb, drawnCubes, instanceStride);
116125

@@ -176,7 +185,14 @@ void NetworkRenderStrategy::draw(float time) const {
176185

177186
// Submit primitive for rendering to view 0.
178187
bgfx::submit(0, m_program);
188+
}
179189

190+
void NetworkRenderStrategy::drawSelection(float time) const {
191+
192+
const float offset = 3.0f;
193+
const float start_x = - area_size_x * offset / 2.0f;
194+
const float start_y = - area_size_y * offset / 2.0f;
195+
const float start_z = - area_size_z * offset / 2.0f;
180196

181197
// Compute selection position
182198
float sel_x = start_x + selected_x * offset;
@@ -197,7 +213,14 @@ void NetworkRenderStrategy::draw(float time) const {
197213
bgfx::setIndexBuffer(m_selection_ibh);
198214
bgfx::setState(BGFX_STATE_DEFAULT | BGFX_STATE_PT_LINES); // Use wireframe cube
199215
bgfx::submit(0, m_selection_program);
216+
}
217+
218+
void NetworkRenderStrategy::draw(float time) const {
219+
if (drawnCubes > 0) {
220+
drawNeurons(time);
221+
}
200222

223+
drawSelection(time);
201224
}
202225

203226
void NetworkRenderStrategy::destroy() {

graphics/neurons/render/network_render.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ struct NetworkRenderStrategy : RenderStrategy {
3232
// }
3333

3434
void init() override;
35-
void addLayer(const NeuronLayer& layer);
35+
void addLayer(const NeuronLayer& layer, const std::vector<size_t>& area_size);
3636
void update(float dt) override;
3737
void draw(float time) const override;
3838
void destroy() override;
3939

40+
void drawNeurons(float time) const;
41+
void drawSelection(float time) const;
42+
4043
bgfx::VertexBufferHandle m_vbh;
4144
bgfx::IndexBufferHandle m_ibh;
4245

0 commit comments

Comments
 (0)