Skip to content

Commit 5e9acb1

Browse files
authored
Renderer update (#46)
* multithread fixes * renderer update * formatting * visualiser menubar * move extrusion settings to popup
1 parent 96ed053 commit 5e9acb1

10 files changed

+592
-339
lines changed

src/MarlinSimulator/RawSocketSerial.h

+82-67
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,27 @@ struct ServerInfo {
1717
class RawSocketSerial {
1818
public:
1919
void stop_listen() {
20-
if(SDLNet_TCP_DelSocket(server.socket_set, server.socket) == -1) {
21-
fprintf(stderr, "RawSocketSerial::stop_listen: SDLNet_TCP_DelSocket: %s\n", SDLNet_GetError());
22-
exit(-1);
20+
if (SDLNet_TCP_DelSocket(server.socket_set, server.socket) == -1) {
21+
fprintf(stderr, "RawSocketSerial::stop_listen: SDLNet_TCP_DelSocket: %s\n", SDLNet_GetError());
22+
exit(-1);
2323
}
2424
SDLNet_TCP_Close(server.socket);
2525

26-
for(auto i = 0; i < ServerInfo::max_connections; ++i) {
27-
if(server.sockets[i] == nullptr) continue;
28-
close_socket(i);
26+
for (auto i = 0; i < ServerInfo::max_connections; ++i) {
27+
if (server.sockets[i] == nullptr) continue;
28+
close_socket(i);
2929
}
3030
}
3131

3232
void listen_on_port(uint16_t port) {
3333
IPaddress ip;
34-
if(SDLNet_ResolveHost(&ip, NULL, port) == -1) {
35-
fprintf(stderr, "RawSocketSerial::start: SDLNet_ResolveHost: %s\n", SDLNet_GetError());
36-
exit(-1);
34+
if (SDLNet_ResolveHost(&ip, NULL, port) == -1) {
35+
fprintf(stderr, "RawSocketSerial::start: SDLNet_ResolveHost: %s\n", SDLNet_GetError());
36+
exit(-1);
3737
}
3838
start_listen(ip);
39-
server_thread = std::thread(&RawSocketSerial::execute, this);
4039
thread_active = true;
40+
server_thread = std::thread(&RawSocketSerial::execute, this);
4141
}
4242

4343
void stop() {
@@ -54,78 +54,79 @@ class RawSocketSerial {
5454

5555
void start_listen(IPaddress ip) {
5656
server.socket = SDLNet_TCP_Open(&ip);
57-
if(server.socket == nullptr) {
58-
fprintf(stderr, "RawSocketSerial::start_listen: SDLNet_TCP_Open: %s\n", SDLNet_GetError());
59-
exit(-1);
57+
if (server.socket == nullptr) {
58+
fprintf(stderr, "RawSocketSerial::start_listen: SDLNet_TCP_Open: %s\n", SDLNet_GetError());
59+
exit(-1);
6060
}
6161

62-
server.socket_set = SDLNet_AllocSocketSet(server.max_connections+1);
63-
if(server.socket_set == nullptr) {
64-
fprintf(stderr, "RawSocketSerial::start_listen: SDLNet_AllocSocketSet: %s\n", SDLNet_GetError());
65-
exit(-1);
62+
server.socket_set = SDLNet_AllocSocketSet(server.max_connections + 1);
63+
if (server.socket_set == nullptr) {
64+
fprintf(stderr, "RawSocketSerial::start_listen: SDLNet_AllocSocketSet: %s\n", SDLNet_GetError());
65+
exit(-1);
6666
}
6767

68-
if(SDLNet_TCP_AddSocket(server.socket_set, server.socket) == -1) {
69-
fprintf(stderr, "RawSocketSerial::start_listen: SDLNet_TCP_AddSocket: %s\n", SDLNet_GetError());
70-
exit(-1);
68+
if (SDLNet_TCP_AddSocket(server.socket_set, server.socket) == -1) {
69+
fprintf(stderr, "RawSocketSerial::start_listen: SDLNet_TCP_AddSocket: %s\n", SDLNet_GetError());
70+
exit(-1);
7171
}
7272
}
7373

7474
void close_socket(int index) {
75-
if(server.sockets[index] == nullptr) {
76-
fprintf(stderr, "RawSocketSerial::close_socket: Attempted to delete a NULL socket.\n");
77-
return;
75+
if (server.sockets[index] == nullptr) {
76+
fprintf(stderr, "RawSocketSerial::close_socket: Attempted to delete a NULL socket.\n");
77+
return;
7878
}
7979

80-
if(SDLNet_TCP_DelSocket(server.socket_set, server.sockets[index]) == -1) {
81-
fprintf(stderr, "RawSocketSerial::close_socket: SDLNet_TCP_DelSocket: %s\n", SDLNet_GetError());
82-
exit(-1);
80+
if (SDLNet_TCP_DelSocket(server.socket_set, server.sockets[index]) == -1) {
81+
fprintf(stderr, "RawSocketSerial::close_socket: SDLNet_TCP_DelSocket: %s\n", SDLNet_GetError());
82+
exit(-1);
8383
}
8484

8585
SDLNet_TCP_Close(server.sockets[index]);
8686
server.sockets[index] = nullptr;
8787
}
8888

8989
int accept_socket(int index) {
90-
if(server.sockets[index]) {
91-
fprintf(stderr, "RawSocketSerial::accept_socket: Overriding socket at index %d.\n", index);
92-
close_socket(index);
93-
}
90+
if (server.sockets[index]) {
91+
fprintf(stderr, "RawSocketSerial::accept_socket: Overriding socket at index %d.\n", index);
92+
close_socket(index);
93+
}
9494

95-
server.sockets[index] = SDLNet_TCP_Accept(server.socket);
96-
if(server.sockets[index] == nullptr) return 0;
95+
server.sockets[index] = SDLNet_TCP_Accept(server.socket);
96+
if (server.sockets[index] == nullptr) return 0;
9797

98-
if(SDLNet_TCP_AddSocket(server.socket_set, server.sockets[index]) == -1) {
99-
fprintf(stderr, "RawSocketSerial::accept_socket: SDLNet_TCP_AddSocket: %s\n", SDLNet_GetError());
100-
exit(-1);
101-
}
98+
if (SDLNet_TCP_AddSocket(server.socket_set, server.sockets[index]) == -1) {
99+
fprintf(stderr, "RawSocketSerial::accept_socket: SDLNet_TCP_AddSocket: %s\n", SDLNet_GetError());
100+
exit(-1);
101+
}
102102

103-
return 1;
103+
return 1;
104104
}
105105

106106
int32_t receive(int index) {
107-
int32_t length = SDLNet_TCP_Recv(server.sockets[index], receive_buffer, ServerInfo::max_packet_size);
108-
if(length <= 0) {
109-
close_socket(index);
110-
const char* err = SDLNet_GetError();
111-
if(strlen(err) == 0) {
112-
printf("RawSocketSerial::receive: client disconnected\n");
113-
} else {
114-
fprintf(stderr, "RawSocketSerial::receive: SDLNet_TCP_Recv: %s\n", err);
115-
}
116-
return 0;
107+
int32_t length = SDLNet_TCP_Recv(server.sockets[index], receive_buffer, ServerInfo::max_packet_size);
108+
if (length <= 0) {
109+
close_socket(index);
110+
char const* err = SDLNet_GetError();
111+
if (strlen(err) == 0) {
112+
printf("RawSocketSerial::receive: client disconnected\n");
117113
} else {
118-
rx_buffer.write(receive_buffer, length);
119-
return length;
114+
fprintf(stderr, "RawSocketSerial::receive: SDLNet_TCP_Recv: %s\n", err);
120115
}
116+
return 0;
117+
} else {
118+
std::scoped_lock buffer_lock(buffer_mutex);
119+
rx_buffer.write(receive_buffer, length);
120+
return length;
121+
}
121122
}
122123

123124
int32_t send(int index) {
124125
std::size_t length = tx_buffer.read(transmit_buffer, ServerInfo::max_packet_size);
125-
int num_sent = SDLNet_TCP_Send(server.sockets[index], transmit_buffer, length);
126-
if(num_sent < length) {
127-
fprintf(stderr, "RawSocketSerial::send: SDLNet_TCP_Send: %s\n", SDLNet_GetError());
128-
close_socket(index);
126+
int num_sent = SDLNet_TCP_Send(server.sockets[index], transmit_buffer, length);
127+
if (num_sent < length) {
128+
fprintf(stderr, "RawSocketSerial::send: SDLNet_TCP_Send: %s\n", SDLNet_GetError());
129+
close_socket(index);
129130
}
130131
return num_sent;
131132
}
@@ -144,48 +145,62 @@ class RawSocketSerial {
144145

145146
if (SDLNet_SocketReady(server.socket)) {
146147
int got_socket = accept_socket(server.next_index);
147-
if(!got_socket) {
148-
num_rdy--;
149-
continue;
148+
if (!got_socket) {
149+
num_rdy--;
150+
continue;
150151
}
151152

152153
int chk_count = 0;
153154
for (; chk_count < ServerInfo::max_connections; ++chk_count) {
154-
if (server.sockets[(server.next_index + chk_count) % ServerInfo::max_connections] == nullptr) break;
155+
if (server.sockets[(server.next_index + chk_count) % ServerInfo::max_connections] == nullptr) break;
155156
}
156157

157158
server.next_index = (server.next_index + chk_count) % ServerInfo::max_connections;
158159
printf("RawSocketSerial::thread_main: new connection (server.next_index = %d)\n", server.next_index);
159-
num_rdy --;
160+
num_rdy--;
160161
}
161162

162-
for (int ind=0; (ind<ServerInfo::max_connections) && num_rdy; ++ind) {
163+
for (int ind = 0; (ind < ServerInfo::max_connections) && num_rdy; ++ind) {
163164
if (server.sockets[ind] == nullptr) continue;
164165
if (!SDLNet_SocketReady(server.sockets[ind])) continue;
165166
receive(ind--);
166167
}
167-
168168
}
169169
}
170170

171-
std::size_t available() { return rx_buffer.available(); }
171+
std::size_t available() {
172+
return rx_buffer.available();
173+
}
172174

173175
int16_t read() {
174176
uint8_t value = 0;
175-
uint32_t ret = rx_buffer.read(&value);
177+
uint32_t ret = rx_buffer.read(&value);
176178
return (ret ? value : -1);
177179
}
178180

179-
size_t readBytes(char* dst, size_t length) { return rx_buffer.read((uint8_t *)dst, length); }
180-
size_t write(char c) { return tx_buffer.write(c); }
181-
void write(const char* str) { while (*str) tx_buffer.write(*str++); }
182-
void write(const uint8_t* buffer, size_t size) { tx_buffer.write((uint8_t *)buffer, size); }
181+
size_t readBytes(char* dst, size_t length) {
182+
return rx_buffer.read((uint8_t*)dst, length);
183+
}
184+
185+
size_t write(char c) {
186+
return tx_buffer.write(c);
187+
}
188+
189+
void write(char const* str) {
190+
while (*str)
191+
tx_buffer.write(*str++);
192+
}
193+
194+
void write(uint8_t const* buffer, size_t size) {
195+
tx_buffer.write((uint8_t*)buffer, size);
196+
}
183197

184198
bool thread_active = false;
185-
ServerInfo server{};
199+
ServerInfo server {};
186200
uint8_t receive_buffer[ServerInfo::max_packet_size];
187201
uint8_t transmit_buffer[ServerInfo::max_packet_size];
188202
RingBuffer<uint8_t, ServerInfo::max_packet_size> rx_buffer;
189203
RingBuffer<uint8_t, ServerInfo::max_packet_size> tx_buffer;
190204
std::thread server_thread;
205+
std::mutex buffer_mutex {};
191206
};

src/MarlinSimulator/application.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,14 @@
1515
Application::Application() {
1616
sim.vis.create();
1717

18-
auto serial1 = user_interface.addElement<SerialMonitor>("Serial Monitor(0)", serial_stream_0);
18+
user_interface.addElement<SerialMonitor>("Serial Monitor(0)", serial_stream_0);
1919
user_interface.addElement<SerialMonitor>("Serial Monitor(1)", serial_stream_1);
2020
user_interface.addElement<SerialMonitor>("Serial Monitor(2)", serial_stream_2);
2121
user_interface.addElement<SerialMonitor>("Serial Monitor(3)", serial_stream_3);
2222

23-
//user_interface.addElement<TextureWindow>("Controller Display", sim.display.texture_id, (float)sim.display.width / (float)sim.display.height, [this](UiWindow* window){ this->sim.display.ui_callback(window); });
2423
user_interface.addElement<StatusWindow>("Status", &clear_color, [this](UiWindow* window){ this->sim.ui_info_callback(window); });
25-
auto components = user_interface.addElement<UiWindow>("Components", [this](UiWindow* window){ this->sim.testPrinter.ui_widgets(); });
26-
user_interface.addElement<Viewport>("Viewport", [this](UiWindow* window){ this->sim.vis.ui_viewport_callback(window); });
27-
//user_interface.addElement<GraphWindow>("graphs", sim.display.texture_id, 128.0 / 64.0, std::bind(&Simulation::ui_callback, &sim, std::placeholders::_1));
24+
user_interface.addElement<UiWindow>("Components", [this](UiWindow* window){ this->sim.testPrinter.ui_widgets(); });
25+
user_interface.addElement<Viewport>("Viewport", [this](UiWindow* window){ this->sim.vis.ui_viewport_callback(window); }, [this](UiWindow* window){ this->sim.vis.ui_viewport_menu_callback(window); });
2826

2927
user_interface.addElement<UiWindow>("Simulation", [this](UiWindow* window){
3028
//Simulation Time
@@ -63,6 +61,10 @@ Application::Application() {
6361
Kernel::TimeControl::realtime_scale.store(ui_realtime_scale);
6462
});
6563

64+
user_interface.addElement<UiPopup>("Preferences", true, [this](UiWindow* window){
65+
if (ImGui::Button("close")) { ImGui::CloseCurrentPopup(); }
66+
});
67+
6668
user_interface.addElement<UiWindow>("Pin List", [this](UiWindow* window){
6769
for (auto p : pin_array) {
6870
bool value = Gpio::get_pin_value(p.pin);
@@ -232,8 +234,6 @@ Application::Application() {
232234
});
233235

234236
user_interface.post_init = [&](){
235-
//serial1->select();
236-
//components->select();
237237
user_interface.ui_elements["Serial Monitor(0)"]->select();
238238
user_interface.ui_elements["Components"]->select();
239239
};

src/MarlinSimulator/execution_control.cpp

+14-7
Original file line numberDiff line numberDiff line change
@@ -40,37 +40,44 @@ bool Kernel::execute_loop( uint64_t max_end_ticks) {
4040
//simulation time lock
4141
TimeControl::realtime_sync();
4242

43-
//todo: investigate dataloss when pulling from SerialMonitor rather than pushing from here
44-
45-
//todo: bad chris, stop just making shit work, look at this mess
4643
if (serial_stream_0.transmit_buffer.available()) {
4744
char buffer[serial_stream_0.transmit_buffer_size];
4845
auto count = serial_stream_0.transmit_buffer.read((uint8_t *)buffer, serial_stream_0.transmit_buffer_size - 1);
4946
buffer[count] = '\0';
50-
std::dynamic_pointer_cast<SerialMonitor>(UserInterface::ui_elements["Serial Monitor(0)"])->insert_text(buffer);
47+
auto terminal = std::dynamic_pointer_cast<SerialMonitor>(UserInterface::ui_elements["Serial Monitor(0)"]);
48+
std::scoped_lock buffer_lock(terminal->buffer_mutex);
49+
terminal->insert_text(buffer);
5150
}
5251
if (serial_stream_1.transmit_buffer.available()) {
5352
char buffer[serial_stream_1.transmit_buffer_size];
5453
auto count = serial_stream_1.transmit_buffer.read((uint8_t *)buffer, serial_stream_1.transmit_buffer_size - 1);
5554
buffer[count] = '\0';
56-
std::dynamic_pointer_cast<SerialMonitor>(UserInterface::ui_elements["Serial Monitor(1)"])->insert_text(buffer);
55+
auto terminal = std::dynamic_pointer_cast<SerialMonitor>(UserInterface::ui_elements["Serial Monitor(1)"]);
56+
std::scoped_lock buffer_lock(terminal->buffer_mutex);
57+
terminal->insert_text(buffer);
5758
}
5859
if (serial_stream_2.transmit_buffer.available()) {
5960
char buffer[serial_stream_2.transmit_buffer_size];
6061
auto count = serial_stream_2.transmit_buffer.read((uint8_t *)buffer, serial_stream_2.transmit_buffer_size - 1);
6162
buffer[count] = '\0';
62-
std::dynamic_pointer_cast<SerialMonitor>(UserInterface::ui_elements["Serial Monitor(2)"])->insert_text(buffer);
63+
auto terminal = std::dynamic_pointer_cast<SerialMonitor>(UserInterface::ui_elements["Serial Monitor(2)"]);
64+
std::scoped_lock buffer_lock(terminal->buffer_mutex);
65+
terminal->insert_text(buffer);
6366
}
6467

6568
if (serial_stream_3.transmit_buffer.available()) {
6669
char buffer[serial_stream_3.transmit_buffer_size];
6770
auto count = serial_stream_3.transmit_buffer.read((uint8_t *)buffer, serial_stream_3.transmit_buffer_size - 1);
6871
net_serial.write((uint8_t*)buffer, count);
6972
buffer[count] = '\0';
70-
std::dynamic_pointer_cast<SerialMonitor>(UserInterface::ui_elements["Serial Monitor(3)"])->insert_text(buffer);
73+
auto terminal = std::dynamic_pointer_cast<SerialMonitor>(UserInterface::ui_elements["Serial Monitor(3)"]);
74+
std::scoped_lock buffer_lock(terminal->buffer_mutex);
75+
terminal->insert_text(buffer);
7176
}
77+
7278
if (net_serial.available()) {
7379
char buffer[512];
80+
std::scoped_lock buffer_lock(net_serial.buffer_mutex);
7481
auto count = net_serial.readBytes(buffer, 512);
7582
serial_stream_3.receive_buffer.write((uint8_t *)buffer, count);
7683
}

src/MarlinSimulator/hardware/SDCard.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class SDCard: public SPISlavePeripheral {
3434
const std::string file_dialog_key = "ChooseSDFileDlgKey";
3535
void ui_widget() {
3636
if (ImGui::Button("Select Image (FAT32)")) {
37-
ImGuiFileDialog::Instance()->OpenDialog(file_dialog_key, "Choose File", "FAT32 Disk Image(*.img){.img},.*", ".");
37+
ImGuiFileDialog::Instance()->OpenModal(file_dialog_key, "Choose File", "FAT32 Disk Image(*.img){.img},.*", ".");
3838
}
3939
if (ImGuiFileDialog::Instance()->Display(file_dialog_key, ImGuiWindowFlags_NoDocking)) {
4040
if (ImGuiFileDialog::Instance()->IsOk()) {

src/MarlinSimulator/renderer/gl.h

+28-10
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,35 @@ template<> struct gl_uniform<GL_FLOAT_MAT2x4> { using value_type = decltype(glUn
9292
template<> struct gl_uniform<GL_FLOAT_MAT3x2> { using value_type = decltype(glUniformMatrix3x2fv); static constexpr bool is_matrix = true; gl_enum_to_type<GL_FLOAT_MAT3x2>::value_type *value; value_type invoke = glUniformMatrix3x2fv; };
9393
template<> struct gl_uniform<GL_FLOAT_MAT3x4> { using value_type = decltype(glUniformMatrix3x4fv); static constexpr bool is_matrix = true; gl_enum_to_type<GL_FLOAT_MAT3x4>::value_type *value; value_type invoke = glUniformMatrix3x4fv; };
9494
template<> struct gl_uniform<GL_FLOAT_MAT4x2> { using value_type = decltype(glUniformMatrix4x2fv); static constexpr bool is_matrix = true; gl_enum_to_type<GL_FLOAT_MAT4x2>::value_type *value; value_type invoke = glUniformMatrix4x2fv; };
95-
template<> struct gl_uniform<GL_FLOAT_MAT4x3> { using value_type = decltype(glUniformMatrix4x3fv); static constexpr bool is_matrix = true; gl_enum_to_type<GL_FLOAT_MAT4x3>::value_type *value; value_type invoke = glUniformMatrix4x3fv; };
95+
template<> struct gl_uniform<GL_FLOAT_MAT4x3> { using value_type = decltype(glUniformMatrix4x3fv); static constexpr bool is_matrix = true; gl_enum_to_type<GL_FLOAT_MAT4x3>::value_type *value; value_type invoke = glUniformMatrix4x3fv;
96+
};
97+
9698
using gl_uniform_t = std::variant<
97-
gl_uniform<GL_FLOAT>, gl_uniform<GL_FLOAT_VEC2>, gl_uniform<GL_FLOAT_VEC3>,
98-
gl_uniform<GL_FLOAT_VEC4>, gl_uniform<GL_INT>, gl_uniform<GL_INT_VEC2>,
99-
gl_uniform<GL_INT_VEC3>, gl_uniform<GL_INT_VEC4>, gl_uniform<GL_UNSIGNED_INT>,
100-
gl_uniform<GL_UNSIGNED_INT_VEC2>, gl_uniform<GL_UNSIGNED_INT_VEC3>, gl_uniform<GL_UNSIGNED_INT_VEC4>,
101-
gl_uniform<GL_BOOL>, gl_uniform<GL_BOOL_VEC2>, gl_uniform<GL_BOOL_VEC3>, gl_uniform<GL_BOOL_VEC4>,
102-
gl_uniform<GL_FLOAT_MAT2>, gl_uniform<GL_FLOAT_MAT3>, gl_uniform<GL_FLOAT_MAT4>,
103-
gl_uniform<GL_FLOAT_MAT2x3>, gl_uniform<GL_FLOAT_MAT2x4>, gl_uniform<GL_FLOAT_MAT3x2>,
104-
gl_uniform<GL_FLOAT_MAT3x4>, gl_uniform<GL_FLOAT_MAT4x2>, gl_uniform<GL_FLOAT_MAT4x3>
105-
>;
99+
gl_uniform<GL_FLOAT>,
100+
gl_uniform<GL_FLOAT_VEC2>,
101+
gl_uniform<GL_FLOAT_VEC3>,
102+
gl_uniform<GL_FLOAT_VEC4>,
103+
gl_uniform<GL_INT>,
104+
gl_uniform<GL_INT_VEC2>,
105+
gl_uniform<GL_INT_VEC3>,
106+
gl_uniform<GL_INT_VEC4>,
107+
gl_uniform<GL_UNSIGNED_INT>,
108+
gl_uniform<GL_UNSIGNED_INT_VEC2>,
109+
gl_uniform<GL_UNSIGNED_INT_VEC3>,
110+
gl_uniform<GL_UNSIGNED_INT_VEC4>,
111+
gl_uniform<GL_BOOL>,
112+
gl_uniform<GL_BOOL_VEC2>,
113+
gl_uniform<GL_BOOL_VEC3>,
114+
gl_uniform<GL_BOOL_VEC4>,
115+
gl_uniform<GL_FLOAT_MAT2>,
116+
gl_uniform<GL_FLOAT_MAT3>,
117+
gl_uniform<GL_FLOAT_MAT4>,
118+
gl_uniform<GL_FLOAT_MAT2x3>,
119+
gl_uniform<GL_FLOAT_MAT2x4>,
120+
gl_uniform<GL_FLOAT_MAT3x2>,
121+
gl_uniform<GL_FLOAT_MAT3x4>,
122+
gl_uniform<GL_FLOAT_MAT4x2>,
123+
gl_uniform<GL_FLOAT_MAT4x3>>;
106124

107125
template<uint32_t V> struct gl_texture_parameter;
108126
template<> struct gl_texture_parameter<GL_FLOAT> { using value_type = decltype(glTexParameterfv); value_type invoke = glTexParameterfv; };

0 commit comments

Comments
 (0)