From b1a3cb03097aac591a1fcdeef3e8d807b233a52c Mon Sep 17 00:00:00 2001 From: omigamedev Date: Fri, 31 Mar 2017 15:39:51 +0100 Subject: [PATCH] add remote logging system using curl, normalize log messages removing \n, fix fra shaders precision issue, enable core and forward comp. in WGL, disable multisampling, --- android/CMakeLists.txt | 1 + android/src/main/cpp/main.cpp | 36 +----------- engine.vcxproj | 18 +++--- engine.vcxproj.filters | 6 ++ engine/app.cpp | 108 +++++++++++++++++++--------------- engine/brush.cpp | 14 ++--- engine/font.cpp | 4 -- engine/layout.cpp | 6 +- engine/layout.h | 2 +- engine/log.cpp | 71 ++++++++++++++++++++++ engine/log.h | 31 ++++++++++ engine/main.cpp | 13 ++-- engine/pch.cpp | 7 +++ engine/pch.h | 12 ++-- engine/rtt.cpp | 20 +++---- engine/texture.cpp | 4 +- engine/util.cpp | 29 +++++++++ engine/util.h | 99 +++++++++++++++++++++++++++++++ 18 files changed, 352 insertions(+), 129 deletions(-) create mode 100644 engine/log.cpp create mode 100644 engine/log.h diff --git a/android/CMakeLists.txt b/android/CMakeLists.txt index 5a65385..2599bcd 100644 --- a/android/CMakeLists.txt +++ b/android/CMakeLists.txt @@ -39,6 +39,7 @@ add_library( ../engine/app.cpp ../engine/brush.cpp ../engine/canvas.cpp + ../engine/log.cpp ) target_include_directories(native-lib PRIVATE diff --git a/android/src/main/cpp/main.cpp b/android/src/main/cpp/main.cpp index 42b3796..0e70904 100755 --- a/android/src/main/cpp/main.cpp +++ b/android/src/main/cpp/main.cpp @@ -138,7 +138,7 @@ static int engine_init_display(struct engine* engine) { engine->state.angle = 0; // Check openGL on the system - auto opengl_info = {GL_VENDOR, GL_RENDERER, GL_VERSION, GL_EXTENSIONS}; + auto opengl_info = {GL_VENDOR, GL_RENDERER, GL_VERSION/*, GL_EXTENSIONS*/}; for (auto name : opengl_info) { auto info = glGetString(name); LOG("OpenGL Info: %s", info); @@ -294,46 +294,12 @@ static void engine_handle_cmd(struct android_app* app, int32_t cmd) { * android_native_app_glue. It runs in its own thread, with its own * event loop for receiving input events and doing other things. */ -#include -static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) -{ - ((std::string*)userp)->append((char*)contents, size * nmemb); - LOG("READ SOMETHIND"); - return size * nmemb; -} -int curl_test() -{ - CURL *curl; - CURLcode res; - std::string readBuffer; - - curl = curl_easy_init(); - if(curl) { - curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com"); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); - curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); - res = curl_easy_perform(curl); - curl_easy_cleanup(curl); - - LOG("READ: %s", readBuffer.c_str()); - } - else - { - LOG("READ FAILED"); - } - return 0; -} void android_main(struct android_app* state) { struct engine engine; // Make sure glue isn't stripped. app_dummy(); - LOG("NETWORK TESTING..."); - //curl_test(); - LOG("NETWORK TESTED"); - memset(&engine, 0, sizeof(engine)); state->userData = &engine; state->onAppCmd = engine_handle_cmd; diff --git a/engine.vcxproj b/engine.vcxproj index 2ec798f..2381738 100644 --- a/engine.vcxproj +++ b/engine.vcxproj @@ -71,23 +71,23 @@ true - libs\glm;libs\glew-2.0.0\include;libs\stb;libs\tinyxml2;libs\yoga;$(IncludePath) - libs\glew-2.0.0\lib\Release\$(Platform);$(LibraryPath) + libs\glm;libs\glew-2.0.0\include;libs\stb;libs\tinyxml2;libs\yoga;libs\curl-win\include;$(IncludePath) + libs\curl-win\lib\dll-$(Configuration)-$(PlatformShortName);libs\glew-2.0.0\lib\Release\$(Platform);$(LibraryPath) true - libs\glm;libs\glew-2.0.0\include;libs\stb;libs\tinyxml2;libs\yoga;$(IncludePath) - libs\glew-2.0.0\lib\Release\$(Platform);$(LibraryPath) + libs\glm;libs\glew-2.0.0\include;libs\stb;libs\tinyxml2;libs\yoga;libs\curl-win\include;$(IncludePath) + libs\curl-win\lib\dll-$(Configuration)-$(PlatformShortName);libs\glew-2.0.0\lib\Release\$(Platform);$(LibraryPath) false - libs\glm;libs\glew-2.0.0\include;libs\stb;libs\tinyxml2;libs\yoga;$(IncludePath) - libs\glew-2.0.0\lib\Release\$(Platform);$(LibraryPath) + libs\glm;libs\glew-2.0.0\include;libs\stb;libs\tinyxml2;libs\yoga;libs\curl-win\include;$(IncludePath) + libs\curl-win\lib\dll-$(Configuration)-$(PlatformShortName);libs\glew-2.0.0\lib\Release\$(Platform);$(LibraryPath) false - libs\glm;libs\glew-2.0.0\include;libs\stb;libs\tinyxml2;libs\yoga;$(IncludePath) - libs\glew-2.0.0\lib\Release\$(Platform);$(LibraryPath) + libs\glm;libs\glew-2.0.0\include;libs\stb;libs\tinyxml2;libs\yoga;libs\curl-win\include;$(IncludePath) + libs\curl-win\lib\dll-$(Configuration)-$(PlatformShortName);libs\glew-2.0.0\lib\Release\$(Platform);$(LibraryPath) @@ -158,6 +158,7 @@ + Create @@ -198,6 +199,7 @@ + diff --git a/engine.vcxproj.filters b/engine.vcxproj.filters index 9f8558c..dbfab69 100644 --- a/engine.vcxproj.filters +++ b/engine.vcxproj.filters @@ -69,6 +69,9 @@ Source Files + + Source Files + @@ -113,5 +116,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/engine/app.cpp b/engine/app.cpp index 56887f3..59dcaca 100644 --- a/engine/app.cpp +++ b/engine/app.cpp @@ -33,8 +33,8 @@ void App::initShaders() static const char* shader_f = SHADER_VERSION "uniform sampler2D tex;" - "in vec3 uv;" - "out vec4 frag;" + "in mediump vec3 uv;" + "out mediump vec4 frag;" "void main(){" //" frag = texture(tex, uv.xy/uv.z);" " frag = texture(tex, uv.xy);" @@ -42,8 +42,8 @@ void App::initShaders() static const char* shader_uv_f = SHADER_VERSION "uniform sampler2D tex;" - "in vec3 uv;" - "out vec4 frag;" + "in mediump vec3 uv;" + "out mediump vec4 frag;" "void main(){" " frag = vec4(uv.xy, 0.0, 1.0);" "}"; @@ -64,8 +64,8 @@ void App::initShaders() static const char* shader_atlas_f = SHADER_VERSION "uniform sampler2D tex;" - "in vec2 uv;" - "out vec4 frag;" + "in mediump vec2 uv;" + "out mediump vec4 frag;" "void main(){" " frag = texture(tex, uv);" "}"; @@ -80,8 +80,8 @@ void App::initShaders() "}"; static const char* shader_color_f = SHADER_VERSION - "uniform vec4 col;" - "out vec4 frag;" + "uniform mediump vec4 col;" + "out mediump vec4 frag;" "void main(){" " frag = col;" "}"; @@ -99,11 +99,11 @@ void App::initShaders() "}"; static const char* shader_color_quad_f = SHADER_VERSION - "uniform vec4 col;" - "in vec3 uv;" - "out vec4 frag;" + "uniform mediump vec4 col;" + "in mediump vec3 uv;" + "out mediump vec4 frag;" "void main(){" - " vec4 gradient_x = mix(col, vec4(1.0, 1.0, 1.0, 1.0), uv.x);" + " mediump vec4 gradient_x = mix(col, vec4(1.0, 1.0, 1.0, 1.0), uv.x);" " frag = mix(gradient_x, vec4(0.0, 0.0, 0.0, 1.0), uv.y);" "}"; @@ -120,12 +120,12 @@ void App::initShaders() "}"; static const char* shader_color_hue_f = SHADER_VERSION - "uniform vec4 col;" - "in vec3 uv;" - "out vec4 frag;" - "vec3 hsv2rgb(vec3 c) {" - " vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);" - " vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);" + "uniform mediump vec4 col;" + "in mediump vec3 uv;" + "out mediump vec4 frag;" + "mediump vec3 hsv2rgb(mediump vec3 c) {" + " mediump vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);" + " mediump vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);" " return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);" "}" "void main(){" @@ -145,12 +145,12 @@ void App::initShaders() "}"; static const char* shader_font_f = SHADER_VERSION - "uniform sampler2D tex;" - "uniform vec4 col;" - "in vec2 uv;" - "out vec4 frag;" + "uniform mediump sampler2D tex;" + "uniform mediump vec4 col;" + "in mediump vec2 uv;" + "out mediump vec4 frag;" "void main(){" - " float a = texture(tex, uv).r;" + " mediump float a = texture(tex, uv).r;" " frag = vec4(col.rgb, a);" "}"; @@ -167,13 +167,13 @@ void App::initShaders() "}"; static const char* shader_stroke_f = SHADER_VERSION - "uniform sampler2D tex;" - "uniform vec4 col;" - "uniform float alpha;" - "in vec3 uv;" - "out vec4 frag;" + "uniform mediump sampler2D tex;" + "uniform mediump vec4 col;" + "uniform mediump float alpha;" + "in mediump vec3 uv;" + "out mediump vec4 frag;" "void main(){" - " float a = (1.0 - texture(tex, uv.xy).r) * alpha;" + " mediump float a = (1.0 - texture(tex, uv.xy).r) * alpha;" " frag = vec4(col.rgb, a);" "}"; @@ -209,7 +209,7 @@ void App::initAssets() sampler.create(GL_NEAREST); LOG("initializing assets load uvs texture"); if (!tex.load("data/uvs.jpg")) - LOG("error loading image\n"); + LOG("error loading image"); LOG("initializing assets completed"); } @@ -444,6 +444,8 @@ void App::initLayout() void App::init() { + LogRemote::I.start(); + #ifdef _WIN32 static CONSOLE_SCREEN_BUFFER_INFO info; GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &info); @@ -457,15 +459,33 @@ void App::init() { GL_DEBUG_SEVERITY_MEDIUM, FOREGROUND_GREEN | FOREGROUND_INTENSITY }, { GL_DEBUG_SEVERITY_HIGH, FOREGROUND_RED | FOREGROUND_INTENSITY }, }; - SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors[severity]); - LOG("%.*s\n", length, message); - FlushConsoleInputBuffer(GetStdHandle(STD_OUTPUT_HANDLE)); - SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), info.wAttributes); + if (severity == GL_DEBUG_SEVERITY_MEDIUM || severity == GL_DEBUG_SEVERITY_HIGH) + { + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colors[severity]); + LOG("%.*s", length, message); + FlushConsoleInputBuffer(GetStdHandle(STD_OUTPUT_HANDLE)); + SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), info.wAttributes); + __debugbreak(); + } }, nullptr); - glEnable(GL_DEBUG_OUTPUT); + glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); #endif - glEnable(GL_TEXTURE_2D); + //int n; + //glGetIntegerv(GL_NUM_EXTENSIONS, &n); + //for (int i = 0; i < n; i++) + //{ + // const unsigned char* s = glGetStringi(GL_EXTENSIONS, i); + // LOG("GL ext %03d: %s\n", i, s); + //} + LOG("GL version: %s", glGetString(GL_VERSION)); + LOG("GL vendor: %s", glGetString(GL_VENDOR)); + LOG("GL renderer: %s", glGetString(GL_RENDERER)); + LOG("Screen Resolution: %dx%d", (int)width, (int)height); + + zoom = ceilf(width / 1000.f); + + //glEnable(GL_TEXTURE_2D); glDisable(GL_DEPTH_TEST); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -475,20 +495,10 @@ void App::init() initAssets(); initLayout(); - //int n; - //glGetIntegerv(GL_NUM_EXTENSIONS, &n); - //for (int i = 0; i < n; i++) - //{ - // const unsigned char* s = glGetStringi(GL_EXTENSIONS, i); - // LOG("GL ext %03d: %s\n", i, s); - //} - LOG("GL version: %s\n", glGetString(GL_VERSION)); - LOG("GL vendor: %s\n", glGetString(GL_VENDOR)); - LOG("GL renderer: %s\n", glGetString(GL_RENDERER)); GLfloat width_range[2]; glGetFloatv(GL_ALIASED_LINE_WIDTH_RANGE, width_range); - LOG("GL line range: %f - %f\n", width_range[0], width_range[1]); + LOG("GL line range: %f - %f", width_range[0], width_range[1]); LOG("Screen Size: %f %f", width, height); } @@ -540,7 +550,7 @@ bool App::mouse_down(int button, float x, float y) e.m_type = button ? kEventType::MouseDownR : kEventType::MouseDownL; e.m_pos = { x / zoom, y / zoom }; auto ret = layout[main_id]->on_event(&e); - LOG("mouse click button%d pos %f %f\n", button, x, y); + LOG("mouse click button%d pos %f %f", button, x, y); // if (popup) // { @@ -574,7 +584,7 @@ bool App::mouse_up(int button, float x, float y) e.m_pos = { x / zoom, y / zoom }; auto ret = layout[main_id]->on_event(&e); layout[main_id]->update(); - LOG("mouse up button%d pos %f %f\n", button, x, y); + LOG("mouse up button%d pos %f %f", button, x, y); return ret == kEventResult::Consumed; } bool App::key_down(int key) diff --git a/engine/brush.cpp b/engine/brush.cpp index 50cd019..afcd1c4 100644 --- a/engine/brush.cpp +++ b/engine/brush.cpp @@ -106,19 +106,19 @@ bool ui::BrushMesh::create() "}"; static const char* shader_stroke_inst_f = SHADER_VERSION - "uniform sampler2D tex;" - "uniform vec4 col;" - "in float alpha;" - "in vec3 uv;" - "out vec4 frag;" + "uniform mediump sampler2D tex;" + "uniform mediump vec4 col;" + "in mediump float alpha;" + "in mediump vec3 uv;" + "out mediump vec4 frag;" "void main(){" - " float a = (1.0 - texture(tex, uv.xy).r) * alpha;" + " mediump float a = (1.0 - texture(tex, uv.xy).r) * alpha;" " frag = vec4(col.rgb, a);" "}"; if (!shader.create(shader_stroke_inst_v, shader_stroke_inst_f)) - LOG("Failed to create shader Texture"); + LOG("Failed to create shader BrushMesh Stroke"); loc_flow = shader.GetAttribLocation("a_flow"); loc_mvp = shader.GetAttribLocation("a_mvp"); diff --git a/engine/font.cpp b/engine/font.cpp index 10ecddc..9cdfa20 100644 --- a/engine/font.cpp +++ b/engine/font.cpp @@ -16,11 +16,7 @@ bool Font::load(const char* ttf, int font_size) auto bitmap = std::make_unique(w*h); chars.resize(num_chars); int ret = stbtt_BakeFontBitmap(file.m_data, 0, (float)font_size, bitmap.get(), w, h, start_char, num_chars, chars.data()); -#ifdef __APPLE__ font_tex.create(w, h, GL_RED, bitmap.get()); -#else - font_tex.create(w, h, GL_LUMINANCE, bitmap.get()); -#endif // __APPLE__ file.close(); return true; } diff --git a/engine/layout.cpp b/engine/layout.cpp index 4034a02..e0a16b0 100644 --- a/engine/layout.cpp +++ b/engine/layout.cpp @@ -559,10 +559,10 @@ bool LayoutManager::load(const char* path) auto id_str = current->Attribute("id"); if (!id_str) { - LOG("Layout node without id\n"); + LOG("Layout node without id"); return false; } - LOG("Parsing layout: %s\n", id_str); + LOG("Parsing layout: %s", id_str); uint16_t id = const_hash(id_str); auto p = m_layouts.find(id); if (p == m_layouts.end()) @@ -589,7 +589,7 @@ bool LayoutManager::load(const char* path) } else { - LOG("Layout id \"%s\" duplicated\n", id_str); + LOG("Layout id \"%s\" duplicated", id_str); } current = current->NextSiblingElement("layout"); } diff --git a/engine/layout.h b/engine/layout.h index 1ca9e3c..2b61949 100644 --- a/engine/layout.h +++ b/engine/layout.h @@ -1869,7 +1869,7 @@ public: glGetIntegerv(GL_VIEWPORT, vp); glGetFloatv(GL_COLOR_CLEAR_VALUE, cc); - glClearColor(1, 0, 0, 1); + glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); float zoom = root()->m_zoom; auto box = m_clip * zoom; diff --git a/engine/log.cpp b/engine/log.cpp new file mode 100644 index 0000000..4b432cd --- /dev/null +++ b/engine/log.cpp @@ -0,0 +1,71 @@ +#include "pch.h" +#include "log.h" + +LogRemote LogRemote::I; + +static size_t data_handler(void *contents, size_t size, size_t nmemb, void *userp) +{ + auto buffer = reinterpret_cast(userp); + buffer->append((char*)contents, size * nmemb); + return size * nmemb; +} + +void LogRemote::start() +{ + m_running = true; + m_thread = std::thread([&] { + net_init(); + auto session_string = net_request("/start"); + m_session = atoi(session_string.c_str()); + while (m_running) + { + auto m = m_mq.Get(); + auto escaped = curl_easy_escape(curl, m.c_str(), (int)m.size()); + auto data = std::make_unique(m.size() + 64); + int sz = snprintf(data.get(), m.size() + 64, "session=%d&m=%s", m_session, escaped); + curl_free(escaped); + net_request("/log", std::string(data.get(), sz)); + } + net_close(); + }); +} +void LogRemote::net_init() +{ + if (!(curl = curl_easy_init())) + return; + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &this->readBuffer); + //curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, data_handler); +} +std::string LogRemote::net_request(std::string cmd, std::string data /*= ""*/) +{ + readBuffer.clear(); + curl_easy_setopt(curl, CURLOPT_POST, 1L); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str()); + auto url = m_url + cmd; + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); + res = curl_easy_perform(curl); + return readBuffer; +} +void LogRemote::net_close() +{ + if (curl) + curl_easy_cleanup(curl); + curl = nullptr; +} +void LogRemote::log(const char* format, ...) +{ + static char buffer[4096]; + va_list arglist; + va_start(arglist, format); + int n = vsnprintf(buffer, sizeof(buffer), format, arglist); + va_end(arglist); + m_mq.Post(std::string(buffer, n)); +} +LogRemote::~LogRemote() +{ + m_running = false; + m_mq.UnlockGetters(); + if (m_thread.joinable()) + m_thread.join(); +} \ No newline at end of file diff --git a/engine/log.h b/engine/log.h new file mode 100644 index 0000000..27fe5e8 --- /dev/null +++ b/engine/log.h @@ -0,0 +1,31 @@ +#pragma once +#include "util.h" + +#ifdef __APPLE__ + #define LOG(M,...) { printf(M"\n", ##__VA_ARGS__); LogRemote::I.log(M, ##__VA_ARGS__); } +#elif __ANDROID__ + #define LOG(...) { ((void)__android_log_print(ANDROID_LOG_INFO, "native-engine", __VA_ARGS__)); LogRemote::I.log(__VA_ARGS__); } +#elif _WIN32 + #define LOG(M,...) { printf(M"\n", ##__VA_ARGS__); LogRemote::I.log(M, ##__VA_ARGS__); } +#endif + +class LogRemote +{ +public: + static LogRemote I; + bool m_running; + std::thread m_thread; + ui::BlockingQueue m_mq; + CURL *curl = nullptr; + CURLcode res; + std::string readBuffer; + std::string m_url = "http://omigamedev.ddns.net:8083"; + int m_session; + + void start(); + void net_init(); + std::string net_request(std::string cmd, std::string data = ""); + void net_close(); + void log(const char* format, ...); + ~LogRemote(); +}; diff --git a/engine/main.cpp b/engine/main.cpp index b4e8a83..4a0f4c8 100644 --- a/engine/main.cpp +++ b/engine/main.cpp @@ -367,9 +367,9 @@ int main() if (glewInit() != GLEW_OK) return 0; - LOG("GL version: %s\n", glGetString(GL_VERSION)); - LOG("GL vendor: %s\n", glGetString(GL_VENDOR)); - LOG("GL renderer: %s\n", glGetString(GL_RENDERER)); + LOG("GL version: %s", glGetString(GL_VERSION)); + LOG("GL vendor: %s", glGetString(GL_VENDOR)); + LOG("GL renderer: %s", glGetString(GL_RENDERER)); // If supported create a 3.1 context if (wglewIsSupported("WGL_ARB_create_context")) @@ -378,7 +378,8 @@ int main() { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 1, - WGL_CONTEXT_FLAGS_ARB, 0, + WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, + WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, 0 }; int pixel_attribs[] = @@ -391,8 +392,8 @@ int main() WGL_COLOR_BITS_ARB, 32, WGL_DEPTH_BITS_ARB, 24, WGL_STENCIL_BITS_ARB, 8, - WGL_SAMPLE_BUFFERS_ARB, 1, // Number of buffers (must be 1 at time of writing) - WGL_SAMPLES_ARB, 4, // Number of samples +// WGL_SAMPLE_BUFFERS_ARB, 1, // Number of buffers (must be 1 at time of writing) +// WGL_SAMPLES_ARB, 4, // Number of samples 0 }; UINT numFormat; diff --git a/engine/pch.cpp b/engine/pch.cpp index 96d5754..53b0580 100644 --- a/engine/pch.cpp +++ b/engine/pch.cpp @@ -5,3 +5,10 @@ #define STB_IMAGE_IMPLEMENTATION #include + +#ifdef DEBUG + #pragma comment (lib, "libcurl_debug.lib") +#else + #pragma comment (lib, "libcurl.lib") +#endif // DEBUG + diff --git a/engine/pch.h b/engine/pch.h index a73d97c..67893aa 100644 --- a/engine/pch.h +++ b/engine/pch.h @@ -9,7 +9,6 @@ #include #include - #define LOG(M,...) printf(M"\n", ##__VA_ARGS__) #define SHADER_VERSION "#version 150\n" #elif __ANDROID__ @@ -21,7 +20,6 @@ #include #include - #define LOG(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-engine", __VA_ARGS__)) #define SHADER_VERSION "#version 300 es\n" #elif _WIN32 @@ -32,8 +30,8 @@ #include #include - #define LOG(M,...) printf(M"\n", ##__VA_ARGS__) -#define SHADER_VERSION "#version 150\n" + #define SHADER_VERSION "#version 150\n" + #endif #define NS_START namespace ui { @@ -44,13 +42,16 @@ #include #include #include +#include #include #include #include #include +#include #include #include #include +#include #define GLM_FORCE_RADIANS #define GLM_FORCE_SWIZZLE @@ -68,3 +69,6 @@ #include #include #include +#include + +#include "log.h" diff --git a/engine/rtt.cpp b/engine/rtt.cpp index efc8ed3..985e6b8 100644 --- a/engine/rtt.cpp +++ b/engine/rtt.cpp @@ -57,20 +57,20 @@ bool RTT::create(int width, int height, int tex/* = -1*/) glBindTexture(GL_TEXTURE_2D, texID); if (tex == -1) - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, 0); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); -// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); -// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); -// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); -// glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, 0); // Create a renderbuffer object to store depth info - glGenRenderbuffers(1, &rboID); - glBindRenderbuffer(GL_RENDERBUFFER, rboID); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height); - glBindRenderbuffer(GL_RENDERBUFFER, 0); +// glGenRenderbuffers(1, &rboID); +// glBindRenderbuffer(GL_RENDERBUFFER, rboID); +// glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height); +// glBindRenderbuffer(GL_RENDERBUFFER, 0); // Create a framebuffer object glGenFramebuffers(1, &fboID); @@ -80,7 +80,7 @@ bool RTT::create(int width, int height, int tex/* = -1*/) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texID, 0); // Attach the renderbuffer to depth attachment point - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboID); + //glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboID); // Check FBO status GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); diff --git a/engine/texture.cpp b/engine/texture.cpp index f30dc2f..659eb12 100644 --- a/engine/texture.cpp +++ b/engine/texture.cpp @@ -31,13 +31,13 @@ bool Texture2D::create(int width, int height, GLint format, const uint8_t* data) m_format = format; glGenTextures(1, &m_tex); bind(); - glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, format, GL_UNSIGNED_BYTE, data); unbind(); return true; } bool Texture2D::create(const ui::Image& img) { - static GLint formats[] = { 0, 0, GL_RGB, GL_RGBA }; + static GLint formats[] = { GL_RED, GL_RG, GL_RGB, GL_RGBA }; return create(img.width, img.height, formats[img.comp - 1], img.data()); } diff --git a/engine/util.cpp b/engine/util.cpp index f7a3337..8e77485 100644 --- a/engine/util.cpp +++ b/engine/util.cpp @@ -33,3 +33,32 @@ glm::vec3 convert_rgb2hsv(const glm::vec3 c) float e = 1.0e-10f; return glm::vec3(fabs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); } + +static const char* gl2str(GLenum err) +{ + switch (err) + { + case GL_NO_ERROR: return "GL_NO_ERROR"; + case GL_INVALID_ENUM: return "GL_INVALID_ENUM"; + case GL_INVALID_VALUE: return "GL_INVALID_VALUE"; + case GL_INVALID_OPERATION: return "GL_INVALID_OPERATION"; + case GL_INVALID_FRAMEBUFFER_OPERATION: return "GL_INVALID_FRAMEBUFFER_OPERATION"; + case GL_OUT_OF_MEMORY: return "GL_OUT_OF_MEMORY"; + default: return "Unknown"; + } +} + +void check_OpenGLError(const char* stmt, const char* fname, int line) +{ + GLenum err; + while ((err = glGetError()) != GL_NO_ERROR) + { + LOG("OpenGL error %08x (%s), at %s:%i - for %s", err, gl2str(err), fname, line, stmt); + } +} + +#ifdef _DEBUG +#define GL(stmt) stmt; CheckOpenGLError(#stmt, __FILE__, __LINE__); +#else +#define GL(stmt) stmt +#endif diff --git a/engine/util.h b/engine/util.h index 5dc431c..18773cb 100644 --- a/engine/util.h +++ b/engine/util.h @@ -11,3 +11,102 @@ bool point_in_rect(const glm::vec2& point, const glm::vec4& rect); glm::vec4 rand_color(); glm::vec3 convert_hsv2rgb(const glm::vec3 c); glm::vec3 convert_rgb2hsv(const glm::vec3 c); + +//void check_OpenGLError(const char* stmt, const char* fname, int line) +//{ +// GLenum err; +// while ((err = glGetError()) != GL_NO_ERROR) +// { +// //LOG("OpenGL error %08x (%s), at %s:%i - for %s", err, gluErrorString(err), fname, line, stmt); +// } +//} + +template struct cbuffer +{ + T m_vec[N]; + int m_capacity; + int m_count; + int m_index; + cbuffer() + { + m_capacity = N; + m_index = 0; + m_count = 0; + } + T& head() + { + return m_index == 0 ? m_vec[m_count - 1] : m_vec[m_index - 1]; + } + void add(const T& v) + { + m_vec[m_index] = v; + m_index = (m_index + 1) % m_capacity; + m_count = m_count < m_capacity ? m_count + 1 : m_count; + } + int count() const + { + return m_count; + } + template T2 average() const + { + T2 tot{}; + if (m_count == 0) + return tot; + for (int i = 0; i < m_count; i++) + tot += m_vec[i]; + return tot / (float)m_count; + } +}; + +NS_START +template +class BlockingQueue +{ + std::deque q; + std::condition_variable post_cv; + std::condition_variable get_cv; + mutable std::mutex mutex; +public: + volatile bool unlocked = false; + BlockingQueue() = default; + BlockingQueue(const BlockingQueue& other) = delete; + BlockingQueue& operator=(const BlockingQueue& other) = delete; + BlockingQueue(BlockingQueue&& other) : q(std::move(q)) { } + BlockingQueue& operator=(BlockingQueue&& other) { q = std::move(q); return *this; } + void Post(T&& pkt) + { + std::unique_lock lock(mutex); + if (Max > 0) + { + post_cv.wait(lock, [&]() { return unlocked | (q.size() < Max); }); + if (q.size() >= Max) return; + } + q.push_back(std::forward(pkt)); + get_cv.notify_one(); + } + T Get() + { + static T emptyT{}; + std::unique_lock lock(mutex); + get_cv.wait(lock, [&]() { return unlocked | (q.size() > 0); }); + if (q.empty()) + return std::move(emptyT); + T tmp = std::move(q.front()); + q.pop_front(); + if (Max > 0) post_cv.notify_all(); + return std::move(tmp); + } + void UnlockGetters() + { + unlocked = true; + get_cv.notify_all(); + if (Max > 0) post_cv.notify_all(); + } + int Size() const + { + std::lock_guard lock(mutex); + return (int)q.size(); + } +}; + +NS_END