forked from tmj-fstate/maszyna
-
Notifications
You must be signed in to change notification settings - Fork 16
/
PyInt.cpp
379 lines (328 loc) · 12.4 KB
/
PyInt.cpp
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
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
#include "PyInt.h"
#include "dictionary.h"
#include "application.h"
#include "Logs.h"
#include "Globals.h"
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wwrite-strings"
#endif
void render_task::run() {
// convert provided input to a python dictionary
auto *input = PyDict_New();
if( input == nullptr ) {
cancel();
return;
}
for( auto const &datapair : m_input->floats ) { PyDict_SetItemString( input, datapair.first.c_str(), PyGetFloat( datapair.second ) ); }
for( auto const &datapair : m_input->integers ) { PyDict_SetItemString( input, datapair.first.c_str(), PyGetInt( datapair.second ) ); }
for( auto const &datapair : m_input->bools ) { PyDict_SetItemString( input, datapair.first.c_str(), PyGetBool( datapair.second ) ); }
for( auto const &datapair : m_input->strings ) { PyDict_SetItemString( input, datapair.first.c_str(), PyGetString( datapair.second.c_str() ) ); }
// call the renderer
auto *output { PyObject_CallMethod( m_renderer, "render", "O", input ) };
Py_DECREF( input );
if( output != nullptr ) {
auto *outputwidth { PyObject_CallMethod( m_renderer, "get_width", nullptr ) };
auto *outputheight { PyObject_CallMethod( m_renderer, "get_height", nullptr ) };
// upload texture data
if( ( outputwidth != nullptr )
&& ( outputheight != nullptr ) ) {
::glBindTexture( GL_TEXTURE_2D, m_target );
// setup texture parameters
::glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE );
::glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
::glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
if( GLEW_EXT_texture_filter_anisotropic ) {
// anisotropic filtering
::glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, Global.AnisotropicFiltering );
}
// build texture
::glTexImage2D(
GL_TEXTURE_2D, 0,
GL_RGB8,
PyInt_AsLong( outputwidth ), PyInt_AsLong( outputheight ), 0,
GL_RGB, GL_UNSIGNED_BYTE, reinterpret_cast<GLubyte const *>( PyString_AsString( output ) ) );
::glFlush();
}
if( outputheight != nullptr ) { Py_DECREF( outputheight ); }
if( outputwidth != nullptr ) { Py_DECREF( outputwidth ); }
Py_DECREF( output );
}
// clean up after yourself
cancel();
}
void render_task::cancel() {
delete m_input;
delete this;
}
// initializes the module. returns true on success
auto python_taskqueue::init() -> bool {
#ifdef _WIN32
if (sizeof(void*) == 8)
Py_SetPythonHome("python64");
else
Py_SetPythonHome("python");
#elif __linux__
if (sizeof(void*) == 8)
Py_SetPythonHome("linuxpython64");
else
Py_SetPythonHome("linuxpython");
#elif __APPLE__
if (sizeof(void*) == 8)
Py_SetPythonHome("macpython64");
else
Py_SetPythonHome("macpython");
#endif
Py_Initialize();
PyEval_InitThreads();
PyObject *stringiomodule { nullptr };
PyObject *stringioclassname { nullptr };
PyObject *stringioobject { nullptr };
// do the setup work while we hold the lock
m_main = PyImport_ImportModule("__main__");
if (m_main == nullptr) {
ErrorLog( "Python Interpreter: __main__ module is missing" );
goto release_and_exit;
}
stringiomodule = PyImport_ImportModule( "cStringIO" );
stringioclassname = (
stringiomodule != nullptr ?
PyObject_GetAttrString( stringiomodule, "StringIO" ) :
nullptr );
stringioobject = (
stringioclassname != nullptr ?
PyObject_CallObject( stringioclassname, nullptr ) :
nullptr );
m_stderr = { (
stringioobject == nullptr ? nullptr :
PySys_SetObject( "stderr", stringioobject ) != 0 ? nullptr :
stringioobject ) };
if( false == run_file( "abstractscreenrenderer" ) ) { goto release_and_exit; }
// release the lock, save the state for future use
m_mainthread = PyEval_SaveThread();
WriteLog( "Python Interpreter setup complete" );
// init workers
for( auto &worker : m_workers ) {
auto *openglcontextwindow { Application.window( -1 ) };
worker = std::thread(
&python_taskqueue::run, this,
openglcontextwindow, std::ref( m_tasks ), std::ref( m_condition ), std::ref( m_exit ) );
if( false == worker.joinable() ) { return false; }
}
return true;
release_and_exit:
PyEval_ReleaseLock();
return false;
}
// shuts down the module
void python_taskqueue::exit() {
// let the workers know we're done with them
m_exit = true;
m_condition.notify_all();
// let them free up their shit before we proceed
for( auto &worker : m_workers ) {
if (worker.joinable())
worker.join();
}
// get rid of the leftover tasks
// with the workers dead we don't have to worry about concurrent access anymore
for( auto *task : m_tasks.data ) {
task->cancel();
}
// take a bow
acquire_lock();
Py_Finalize();
}
// adds specified task along with provided collection of data to the work queue. returns true on success
auto python_taskqueue::insert( task_request const &Task ) -> bool {
if( ( Task.renderer.empty() )
|| ( Task.input == nullptr )
|| ( Task.target == 0 ) ) { return false; }
auto *renderer { fetch_renderer( Task.renderer ) };
if( renderer == nullptr ) { return false; }
auto *newtask { new render_task( renderer, Task.input, Task.target ) };
bool newtaskinserted { false };
// acquire a lock on the task queue and add the new task
{
std::lock_guard<std::mutex> lock( m_tasks.mutex );
// check the task list for a pending request with the same target
for( auto &task : m_tasks.data ) {
if( task->target() == Task.target ) {
// replace pending task in the slot with the more recent one
task->cancel();
task = newtask;
newtaskinserted = true;
break;
}
}
if( false == newtaskinserted ) {
m_tasks.data.emplace_back( newtask );
}
}
// potentially wake a worker to handle the new task
m_condition.notify_one();
// all done
return true;
}
// executes python script stored in specified file. returns true on success
auto python_taskqueue::run_file( std::string const &File, std::string const &Path ) -> bool {
auto const lookup { FileExists( { Path + File, "python/local/" + File }, { ".py" } ) };
if( lookup.first.empty() ) { return false; }
std::ifstream inputfile { lookup.first + lookup.second };
std::string input;
input.assign( std::istreambuf_iterator<char>( inputfile ), std::istreambuf_iterator<char>() );
if( PyRun_SimpleString( input.c_str() ) != 0 ) {
error();
return false;
}
return true;
}
// acquires the python gil and sets the main thread as current
void python_taskqueue::acquire_lock() {
PyEval_RestoreThread( m_mainthread );
}
// releases the python gil and swaps the main thread out
void python_taskqueue::release_lock() {
PyEval_SaveThread();
}
auto python_taskqueue::fetch_renderer( std::string const Renderer ) ->PyObject * {
auto const lookup { m_renderers.find( Renderer ) };
if( lookup != std::end( m_renderers ) ) {
return lookup->second;
}
// try to load specified renderer class
auto const path { substr_path( Renderer ) };
auto const file { Renderer.substr( path.size() ) };
PyObject *renderer { nullptr };
PyObject *rendererarguments { nullptr };
PyObject *renderername { nullptr };
acquire_lock();
{
if( m_main == nullptr ) {
ErrorLog( "Python Renderer: __main__ module is missing" );
goto cache_and_return;
}
if( false == run_file( file, path ) ) {
goto cache_and_return;
}
renderername = PyObject_GetAttrString( m_main, file.c_str() );
if( renderername == nullptr ) {
ErrorLog( "Python Renderer: class \"" + file + "\" not defined" );
goto cache_and_return;
}
rendererarguments = Py_BuildValue( "(s)", path.c_str() );
if( rendererarguments == nullptr ) {
ErrorLog( "Python Renderer: failed to create initialization arguments" );
goto cache_and_return;
}
renderer = PyObject_CallObject( renderername, rendererarguments );
if( PyErr_Occurred() != nullptr ) {
error();
renderer = nullptr;
}
cache_and_return:
// clean up after yourself
if( rendererarguments != nullptr ) {
Py_DECREF( rendererarguments );
}
}
release_lock();
// cache the failures as well so we don't try again on subsequent requests
m_renderers.emplace( Renderer, renderer );
return renderer;
}
void python_taskqueue::run( GLFWwindow *Context, rendertask_sequence &Tasks, threading::condition_variable &Condition, std::atomic<bool> &Exit ) {
glfwMakeContextCurrent( Context );
// create a state object for this thread
PyEval_AcquireLock();
auto *threadstate { PyThreadState_New( m_mainthread->interp ) };
PyEval_ReleaseLock();
render_task *task { nullptr };
while( false == Exit.load() ) {
// regardless of the reason we woke up prime the spurious wakeup flag for the next time
Condition.spurious( true );
// keep working as long as there's any scheduled tasks
do {
task = nullptr;
// acquire a lock on the task queue and potentially grab a task from it
{
std::lock_guard<std::mutex> lock( Tasks.mutex );
if( false == Tasks.data.empty() ) {
// fifo
task = Tasks.data.front();
Tasks.data.pop_front();
}
}
if( task != nullptr ) {
// swap in my thread state
PyEval_RestoreThread( threadstate );
{
// execute python code
task->run();
error();
}
// clear the thread state
PyEval_SaveThread();
}
// TBD, TODO: add some idle time between tasks in case we're on a single thread cpu?
} while( task != nullptr );
// if there's nothing left to do wait until there is
// but check every now and then on your own to minimize potential deadlock situations
Condition.wait_for( std::chrono::seconds( 5 ) );
}
// clean up thread state data
PyEval_AcquireLock();
PyThreadState_Swap( nullptr );
PyThreadState_Clear( threadstate );
PyThreadState_Delete( threadstate );
PyEval_ReleaseLock();
}
void
python_taskqueue::error() {
if( PyErr_Occurred() == nullptr ) { return; }
if( m_stderr != nullptr ) {
// std err pythona jest buforowane
PyErr_Print();
auto *errortext { PyObject_CallMethod( m_stderr, "getvalue", nullptr ) };
ErrorLog( PyString_AsString( errortext ) );
// czyscimy bufor na kolejne bledy
PyObject_CallMethod( m_stderr, "truncate", "i", 0 );
}
else {
// nie dziala buffor pythona
PyObject *type, *value, *traceback;
PyErr_Fetch( &type, &value, &traceback );
if( type == nullptr ) {
ErrorLog( "Python Interpreter: don't know how to handle null exception" );
}
PyErr_NormalizeException( &type, &value, &traceback );
if( type == nullptr ) {
ErrorLog( "Python Interpreter: don't know how to handle null exception" );
}
auto *typetext { PyObject_Str( type ) };
if( typetext != nullptr ) {
ErrorLog( PyString_AsString( typetext ) );
}
if( value != nullptr ) {
ErrorLog( PyString_AsString( value ) );
}
auto *tracebacktext { PyObject_Str( traceback ) };
if( tracebacktext != nullptr ) {
ErrorLog( PyString_AsString( tracebacktext ) );
}
else {
WriteLog( "Python Interpreter: failed to retrieve the stack traceback" );
}
}
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif