| 1 | /* |
| 2 | * Copyright (C) 2005-2021 Team Kodi (https://kodi.tv) |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 5 | * See LICENSE.md for more information. |
| 6 | */ |
| 7 | |
| 8 | #include "main.h" |
| 9 | #include "lodepng.h" |
| 10 | |
| 11 | #define _USE_MATH_DEFINES |
| 12 | #include <algorithm> |
| 13 | #include <chrono> |
| 14 | #include <kodi/General.h> |
| 15 | #include <math.h> |
| 16 | |
| 17 | const std::vector<Preset> CScreensaverShadertoy::g_presets = |
| 18 | { |
| 19 | { "Ball", "ball.frag.glsl", { "tex16.png", "tex16.png", "", "" } }, |
| 20 | { "Ball Of Fire", "balloffire.frag.glsl", { "", "", "", "" } }, |
| 21 | { "Bleepy Blocks", "bleepyblocks.frag.glsl", { "tex15.png", "", "", "" } }, |
| 22 | { "Bob Scanline", "blobandscsanline.glsl", { "", "", "", "" } }, |
| 23 | { "Blobs", "blobs.frag.glsl", { "", "", "", "" } }, |
| 24 | { "Blue Fire", "bluefire.frag.glsl", { "tex12.png", "", "", "" } }, |
| 25 | { "Cellular", "cellular.frag.glsl", { "", "", "", "" } }, |
| 26 | { "Classic RGB Plasma", "rgbplasma.frag.glsl", { "", "", "", "" } }, |
| 27 | { "Coloured Circles", "colorcircles.frag.glsl", { "", "", "", "" } }, |
| 28 | { "Dance Floor", "dancefloor.frag.glsl", { "", "", "", "" } }, |
| 29 | { "Dot Dot Dot", "dotdotdot.frag.glsl", { "", "", "", "" } }, |
| 30 | { "Effect Ringblobs", "ringblobs.frag.glsl", { "", "", "", "" } }, |
| 31 | { "Ether", "ether.frag.glsl", { "", "", "", "" } }, |
| 32 | { "Fire", "fire.frag.glsl", { "tex12.png", "", "", "" } }, |
| 33 | { "Flaring 1", "flaringtype1.frag.glsl", { "tex12.png", "", "", "" } }, |
| 34 | { "Flaring 2", "flaringtype2.frag.glsl", { "tex12.png", "", "", "" } }, |
| 35 | { "Flaring 3", "flaringtype3.frag.glsl", { "tex12.png", "", "", "" } }, |
| 36 | { "Flaring 4", "flaringtype4.frag.glsl", { "tex12.png", "", "", "" } }, |
| 37 | { "Flaring 5", "flaringtype5.frag.glsl", { "tex12.png", "", "", "" } }, |
| 38 | { "Fractal Tiling", "fractaltiling.frag.glsl", { "", "", "", "" } }, |
| 39 | { "Infinite Fall", "infinitefall.frag.glsl", { "tex16.png" ,"", "", "" } }, |
| 40 | { "Input Time", "inputtime.frag.glsl", { "", "", "", "" } }, |
| 41 | { "Interstellar", "stellar.frag.glsl", { "tex12.png", "", "", "" } }, |
| 42 | { "Interweaving Sine bands", "sinebands.frag.glsl", { "", "", "", "" } }, |
| 43 | { "Julia Trap", "juliatrap.frag.glsl", { "", "", "", "" } }, |
| 44 | { "Layers Of Plasma", "layersplasma.frag.glsl", { "", "", "", "" } }, |
| 45 | { "Liquid Spectrum", "liquidspectrum.frag.glsl", { "", "", "", "" } }, |
| 46 | { "Noise Animation - Electric", "noiseanimelectric.frag.glsl", { "tex12.png", "", "", "" } }, |
| 47 | { "Noise Animation - Flow", "noiseanimflow.frag.glsl", { "tex12.png", "", "", "" } }, |
| 48 | { "Noise Animation - Lava", "noiseanimlava.frag.glsl", { "tex12.png", "", "", "" } }, |
| 49 | { "Noise Animation - Watery", "noiseanimwatery.frag.glsl", { "tex12.png", "", "", "" } }, |
| 50 | { "Overly Satisfying", "overlysatisfying.frag.glsl", { "", "", "", "" } }, |
| 51 | { "Plasma Circles", "plasmacircles.frag.glsl", { "", "", "", "" } }, |
| 52 | { "Plasma Triangles", "plasmatriangle.frag.glsl", { "", "", "", "" } }, |
| 53 | { "Plasma Matrix Spin", "plasmatrixspin.frag.glsl", { "", "", "", "" } }, |
| 54 | { "Pixellated Plasma", "pixellated.frag.glsl", { "", "", "", "" } }, |
| 55 | { "Silexarst Creation", "silexarst.frag.glsl", { "", "", "", "" } }, |
| 56 | { "Sky at Night", "skyatnight.frag.glsl", { "", "", "", "" } }, |
| 57 | { "Sphere", "sphere.frag.glsl", { "", "", "", "" } }, |
| 58 | { "Star Tunnel", "startunnel.frag.glsl", { "", "", "", "" } }, |
| 59 | { "Vector Field", "vectorfield.frag.glsl", { "tex16.png", "keyboard.png", "tex09.png", "" } }, |
| 60 | { "Venus", "venus.frag.glsl", { "tex16.png", "", "", "" } }, |
| 61 | { "Watch It Burn", "watchitburn.frag.glsl", { "tex16.png", "", "", "" } }, |
| 62 | { "Water Caustic", "watercaustic.frag.glsl", { "", "", "", "" } }, |
| 63 | { "Wiggly (try mouse)", "wiggly.frag.glsl", { "", "", "", "" } }, |
| 64 | { "Worley Noise Waters", "worleynoisewaters.frag.glsl", { "", "", "", "" } }, |
| 65 | }; |
| 66 | |
| 67 | #if defined(HAS_GL) |
| 68 | |
| 69 | std::string fsHeader = |
| 70 | R"shader(#version 150 |
| 71 | |
| 72 | #extension GL_OES_standard_derivatives : enable |
| 73 | |
| 74 | uniform vec3 iResolution; |
| 75 | uniform float iGlobalTime; |
| 76 | uniform float iChannelTime[4]; |
| 77 | uniform vec4 iMouse; |
| 78 | uniform vec4 iDate; |
| 79 | uniform float iSampleRate; |
| 80 | uniform vec3 iChannelResolution[4]; |
| 81 | uniform sampler2D iChannel0; |
| 82 | uniform sampler2D iChannel1; |
| 83 | uniform sampler2D iChannel2; |
| 84 | uniform sampler2D iChannel3; |
| 85 | |
| 86 | out vec4 FragColor; |
| 87 | |
| 88 | #define iTime iGlobalTime |
| 89 | |
| 90 | #ifndef texture2D |
| 91 | #define texture2D texture |
| 92 | #endif |
| 93 | )shader"; |
| 94 | |
| 95 | std::string fsFooter = |
| 96 | R"shader( |
| 97 | void main(void) |
| 98 | { |
| 99 | vec4 color = vec4(0.0, 0.0, 0.0, 1.0); |
| 100 | mainImage(color, gl_FragCoord.xy); |
| 101 | color.w = 1.0; |
| 102 | FragColor = color; |
| 103 | } |
| 104 | )shader"; |
| 105 | |
| 106 | #else |
| 107 | |
| 108 | std::string fsHeader = |
| 109 | R"shader(#version 100 |
| 110 | |
| 111 | #extension GL_OES_standard_derivatives : enable |
| 112 | |
| 113 | precision mediump float; |
| 114 | precision mediump int; |
| 115 | |
| 116 | uniform vec3 iResolution; |
| 117 | uniform float iGlobalTime; |
| 118 | uniform float iChannelTime[4]; |
| 119 | uniform vec4 iMouse; |
| 120 | uniform vec4 iDate; |
| 121 | uniform float iSampleRate; |
| 122 | uniform vec3 iChannelResolution[4]; |
| 123 | uniform sampler2D iChannel0; |
| 124 | uniform sampler2D iChannel1; |
| 125 | uniform sampler2D iChannel2; |
| 126 | uniform sampler2D iChannel3; |
| 127 | |
| 128 | #define iTime iGlobalTime |
| 129 | #ifndef texture |
| 130 | #define texture texture2D |
| 131 | #endif |
| 132 | |
| 133 | #ifndef textureLod |
| 134 | vec4 textureLod(sampler2D sampler, vec2 uv, float lod) |
| 135 | { |
| 136 | return texture2D(sampler, uv, lod); |
| 137 | } |
| 138 | #endif |
| 139 | )shader"; |
| 140 | |
| 141 | std::string fsFooter = |
| 142 | R"shader( |
| 143 | void main(void) |
| 144 | { |
| 145 | vec4 color = vec4(0.0, 0.0, 0.0, 1.0); |
| 146 | mainImage(color, gl_FragCoord.xy); |
| 147 | color.w = 1.0; |
| 148 | gl_FragColor = color; |
| 149 | } |
| 150 | )shader"; |
| 151 | |
| 152 | #endif |
| 153 | |
| 154 | CScreensaverShadertoy::CScreensaverShadertoy() |
| 155 | { |
| 156 | m_settingsUseOwnshader = kodi::addon::GetSettingBoolean("ownshader"); |
| 157 | if (m_settingsUseOwnshader) |
| 158 | { |
| 159 | m_currentPreset = -1; |
| 160 | std::string str = kodi::addon::GetSettingString("shader"); |
| 161 | if (!kodi::vfs::FileExists(str)) |
| 162 | { |
| 163 | kodi::QueueNotification(QUEUE_ERROR, "", kodi::addon::GetLocalizedString(30104)); |
| 164 | m_settingsUseOwnshader = false; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (!m_settingsUseOwnshader) |
| 169 | { |
| 170 | int c = kodi::addon::GetSettingInt("preset"); |
| 171 | if (c < 0) |
| 172 | c = 0; |
| 173 | else if (c > g_presets.size()) |
| 174 | c = g_presets.size(); |
| 175 | |
| 176 | if (c == 0) |
| 177 | m_currentPreset = rand() % m_numberPresets; |
| 178 | else |
| 179 | m_currentPreset = c - 1; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | //-- Render ------------------------------------------------------------------- |
| 184 | // Called once per frame. Do all rendering here. |
| 185 | //----------------------------------------------------------------------------- |
| 186 | void CScreensaverShadertoy::Render() |
| 187 | { |
| 188 | if (m_initialized) |
| 189 | { |
| 190 | if (m_state.fbwidth && m_state.fbheight) |
| 191 | { |
| 192 | RenderTo(m_shadertoyShader.ProgramHandle(), m_state.effect_fb); |
| 193 | RenderTo(m_displayShader.ProgramHandle(), 0); |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | RenderTo(m_shadertoyShader.ProgramHandle(), 0); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | bool CScreensaverShadertoy::Start() |
| 203 | { |
| 204 | #ifdef DEBUG_PRINT |
| 205 | printf("Start\n"); |
| 206 | #endif |
| 207 | |
| 208 | static const GLfloat vertex_data[] = |
| 209 | { |
| 210 | -1.0, 1.0, 1.0, 1.0, |
| 211 | 1.0, 1.0, 1.0, 1.0, |
| 212 | 1.0,-1.0, 1.0, 1.0, |
| 213 | -1.0,-1.0, 1.0, 1.0, |
| 214 | }; |
| 215 | |
| 216 | // Upload vertex data to a buffer |
| 217 | glGenBuffers(1, &m_state.vertex_buffer); |
| 218 | glBindBuffer(GL_ARRAY_BUFFER, m_state.vertex_buffer); |
| 219 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_data), vertex_data, GL_STATIC_DRAW); |
| 220 | |
| 221 | Launch(m_currentPreset); |
| 222 | m_initialized = true; |
| 223 | |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | void CScreensaverShadertoy::Stop() |
| 228 | { |
| 229 | m_initialized = false; |
| 230 | #ifdef DEBUG_PRINT |
| 231 | printf("Stop\n"); |
| 232 | #endif |
| 233 | |
| 234 | UnloadPreset(); |
| 235 | UnloadTextures(); |
| 236 | |
| 237 | glDeleteBuffers(1, &m_state.vertex_buffer); |
| 238 | } |
| 239 | |
| 240 | void CScreensaverShadertoy::RenderTo(GLuint shader, GLuint effect_fb) |
| 241 | { |
| 242 | glUseProgram(shader); |
| 243 | |
| 244 | if (shader == m_shadertoyShader.ProgramHandle()) |
| 245 | { |
| 246 | GLuint w = Width(); |
| 247 | GLuint h = Height(); |
| 248 | if (m_state.fbwidth && m_state.fbheight) |
| 249 | w = m_state.fbwidth, h = m_state.fbheight; |
| 250 | int64_t intt = static_cast<int64_t>(std::chrono::duration<double>(std::chrono::high_resolution_clock::now().time_since_epoch()).count() * 1000.0) - m_initialTime; |
| 251 | if (m_bitsPrecision) |
| 252 | intt &= (1<<m_bitsPrecision)-1; |
| 253 | |
| 254 | float t = intt / 1000.0f; |
| 255 | GLfloat tv[] = { t, t, t, t }; |
| 256 | |
| 257 | glUniform3f(m_attrResolutionLoc, w, h, 0.0f); |
| 258 | glUniform1f(m_attrGlobalTimeLoc, t); |
| 259 | glUniform1f(m_attrSampleRateLoc, 44100); |
| 260 | glUniform1fv(m_attrChannelTimeLoc, 4, tv); |
| 261 | glUniform2f(m_state.uScale, static_cast<GLfloat>(Width()) / m_state.fbwidth, static_cast<GLfloat>(Height()) /m_state.fbheight); |
| 262 | |
| 263 | time_t now = time(NULL); |
| 264 | tm *ltm = localtime(&now); |
| 265 | |
| 266 | float year = 1900 + ltm->tm_year; |
| 267 | float month = ltm->tm_mon; |
| 268 | float day = ltm->tm_mday; |
| 269 | float sec = (ltm->tm_hour * 60 * 60) + (ltm->tm_min * 60) + ltm->tm_sec; |
| 270 | |
| 271 | glUniform4f(m_attrDateLoc, year, month, day, sec); |
| 272 | |
| 273 | for (int i = 0; i < 4; i++) |
| 274 | { |
| 275 | glActiveTexture(GL_TEXTURE0 + i); |
| 276 | glUniform1i(m_attrChannelLoc[i], i); |
| 277 | glBindTexture(GL_TEXTURE_2D, m_channelTextures[i]); |
| 278 | } |
| 279 | } |
| 280 | else |
| 281 | { |
| 282 | glActiveTexture(GL_TEXTURE0); |
| 283 | glBindTexture(GL_TEXTURE_2D, m_state.framebuffer_texture); |
| 284 | glUniform1i(m_state.uTexture, 0); // first currently bound texture "GL_TEXTURE0" |
| 285 | } |
| 286 | |
| 287 | // Draw the effect to a texture or direct to framebuffer |
| 288 | glBindFramebuffer(GL_FRAMEBUFFER, effect_fb); |
| 289 | |
| 290 | GLuint attr_vertex = shader == m_shadertoyShader.ProgramHandle() ? m_state.attr_vertex_e : m_state.attr_vertex_r; |
| 291 | glBindBuffer(GL_ARRAY_BUFFER, m_state.vertex_buffer); |
| 292 | glVertexAttribPointer(attr_vertex, 4, GL_FLOAT, 0, 16, 0); |
| 293 | glEnableVertexAttribArray(attr_vertex); |
| 294 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| 295 | glDisableVertexAttribArray(attr_vertex); |
| 296 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 297 | |
| 298 | for (int i = 0; i < 4; i++) |
| 299 | { |
| 300 | glActiveTexture(GL_TEXTURE0 + i); |
| 301 | glBindTexture(GL_TEXTURE_2D, 0); |
| 302 | } |
| 303 | |
| 304 | glUseProgram(0); |
| 305 | } |
| 306 | |
| 307 | void CScreensaverShadertoy::Launch(int preset) |
| 308 | { |
| 309 | m_bitsPrecision = DetermineBitsPrecision(); |
| 310 | // mali-400 has only 10 bits which means milliseond timer wraps after ~1 second. |
| 311 | // we'll fudge that up a bit as having a larger range is more important than ms accuracy |
| 312 | m_bitsPrecision = std::max(m_bitsPrecision, 13); |
| 313 | #ifdef DEBUG_PRINT |
| 314 | printf("bits=%d\n", m_bitsPrecision); |
| 315 | #endif |
| 316 | |
| 317 | UnloadTextures(); |
| 318 | |
| 319 | if (preset < 0) |
| 320 | { |
| 321 | m_usedShaderFile = kodi::addon::GetSettingString("shader"); |
| 322 | m_shaderTextures[0] = kodi::addon::GetSettingString("texture0"); |
| 323 | m_shaderTextures[1] = kodi::addon::GetSettingString("texture1"); |
| 324 | m_shaderTextures[2] = kodi::addon::GetSettingString("texture2"); |
| 325 | m_shaderTextures[3] = kodi::addon::GetSettingString("texture3"); |
| 326 | } |
| 327 | else |
| 328 | { |
| 329 | m_usedShaderFile = kodi::addon::GetAddonPath("resources/shaders/" + g_presets[preset].file); |
| 330 | |
| 331 | m_usedShaderFile = kodi::addon::GetAddonPath("resources/shaders/" + g_presets[m_currentPreset].file); |
| 332 | m_shaderTextures[0] = !g_presets[m_currentPreset].channel[0].empty() ? kodi::addon::GetAddonPath("resources/" + g_presets[m_currentPreset].channel[0]) : ""; |
| 333 | m_shaderTextures[1] = !g_presets[m_currentPreset].channel[1].empty() ? kodi::addon::GetAddonPath("resources/" + g_presets[m_currentPreset].channel[1]) : ""; |
| 334 | m_shaderTextures[2] = !g_presets[m_currentPreset].channel[2].empty() ? kodi::addon::GetAddonPath("resources/" + g_presets[m_currentPreset].channel[2]) : ""; |
| 335 | m_shaderTextures[3] = !g_presets[m_currentPreset].channel[3].empty() ? kodi::addon::GetAddonPath("resources/" + g_presets[m_currentPreset].channel[3]) : ""; |
| 336 | } |
| 337 | |
| 338 | for (int i = 0; i < 4; i++) |
| 339 | { |
| 340 | if (!m_shaderTextures[i].empty()) |
| 341 | { |
| 342 | GLint format = GL_RGBA; |
| 343 | GLint scaling = GL_LINEAR; |
| 344 | GLint repeat = GL_REPEAT; |
| 345 | m_channelTextures[i] = CreateTexture(m_shaderTextures[i], format, scaling, repeat); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | const int size1 = 256, size2=512; |
| 350 | double t1 = MeasurePerformance(m_usedShaderFile, size1); |
| 351 | double t2 = MeasurePerformance(m_usedShaderFile, size2); |
| 352 | |
| 353 | double expected_fps = 40.0; |
| 354 | // time per pixel for rendering fragment shader |
| 355 | double B = (t2-t1)/(size2*size2-size1*size1); |
| 356 | // time to render to screen |
| 357 | double A = t2 - size2*size2 * B; |
| 358 | // how many pixels get the desired fps |
| 359 | double pixels = (1000.0/expected_fps - A) / B; |
| 360 | m_state.fbwidth = sqrtf(pixels * Width() / Height()); |
| 361 | if (m_state.fbwidth >= Width()) |
| 362 | m_state.fbwidth = 0; |
| 363 | else if (m_state.fbwidth < 320) |
| 364 | m_state.fbwidth = 320; |
| 365 | m_state.fbheight = m_state.fbwidth * Height() / Width(); |
| 366 | |
| 367 | #ifdef DEBUG_PRINT |
| 368 | printf("expected fps=%f, pixels=%f %dx%d (A:%f B:%f t1:%.1f t2:%.1f)\n", expected_fps, pixels, m_state.fbwidth, m_state.fbheight, A, B, t1, t2); |
| 369 | #endif |
| 370 | |
| 371 | LoadPreset(m_usedShaderFile); |
| 372 | } |
| 373 | |
| 374 | void CScreensaverShadertoy::UnloadTextures() |
| 375 | { |
| 376 | for (int i = 0; i < 4; i++) |
| 377 | { |
| 378 | if (m_channelTextures[i]) |
| 379 | { |
| 380 | glDeleteTextures(1, &m_channelTextures[i]); |
| 381 | m_channelTextures[i] = 0; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | void CScreensaverShadertoy::LoadPreset(const std::string& shaderPath) |
| 387 | { |
| 388 | UnloadPreset(); |
| 389 | std::string vertShadertoyShader = kodi::addon::GetAddonPath("resources/shaders/main_shadertoy_" GL_TYPE_STRING ".vert.glsl"); |
| 390 | if (!m_shadertoyShader.LoadShaderFiles(vertShadertoyShader, shaderPath) || |
| 391 | !m_shadertoyShader.CompileAndLink("", "", fsHeader, fsFooter)) |
| 392 | { |
| 393 | kodi::Log(ADDON_LOG_ERROR, "Failed to compile shadertoy shaders (current shadertoy file '%s')", shaderPath.c_str()); |
| 394 | return; |
| 395 | } |
| 396 | |
| 397 | GLuint shadertoyShader = m_shadertoyShader.ProgramHandle(); |
| 398 | |
| 399 | m_attrResolutionLoc = glGetUniformLocation(shadertoyShader, "iResolution"); |
| 400 | m_attrGlobalTimeLoc = glGetUniformLocation(shadertoyShader, "iGlobalTime"); |
| 401 | m_attrChannelTimeLoc = glGetUniformLocation(shadertoyShader, "iChannelTime"); |
| 402 | m_attrMouseLoc = glGetUniformLocation(shadertoyShader, "iMouse"); |
| 403 | m_attrDateLoc = glGetUniformLocation(shadertoyShader, "iDate"); |
| 404 | m_attrSampleRateLoc = glGetUniformLocation(shadertoyShader, "iSampleRate"); |
| 405 | m_attrChannelResolutionLoc = glGetUniformLocation(shadertoyShader, "iChannelResolution"); |
| 406 | m_attrChannelLoc[0] = glGetUniformLocation(shadertoyShader, "iChannel0"); |
| 407 | m_attrChannelLoc[1] = glGetUniformLocation(shadertoyShader, "iChannel1"); |
| 408 | m_attrChannelLoc[2] = glGetUniformLocation(shadertoyShader, "iChannel2"); |
| 409 | m_attrChannelLoc[3] = glGetUniformLocation(shadertoyShader, "iChannel3"); |
| 410 | |
| 411 | m_state.uScale = glGetUniformLocation(shadertoyShader, "uScale"); |
| 412 | m_state.attr_vertex_e = glGetAttribLocation(shadertoyShader, "vertex"); |
| 413 | |
| 414 | std::string vertShader = kodi::addon::GetAddonPath("resources/shaders/main_display_" GL_TYPE_STRING ".vert.glsl"); |
| 415 | std::string fraqShader = kodi::addon::GetAddonPath("resources/shaders/main_display_" GL_TYPE_STRING ".frag.glsl"); |
| 416 | if (!m_displayShader.LoadShaderFiles(vertShader, fraqShader) || |
| 417 | !m_displayShader.CompileAndLink()) |
| 418 | { |
| 419 | kodi::Log(ADDON_LOG_ERROR, "Failed to compile main shaders"); |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | m_state.uTexture = glGetUniformLocation(m_displayShader.ProgramHandle(), "uTexture"); |
| 424 | m_state.attr_vertex_r = glGetAttribLocation(m_displayShader.ProgramHandle(), "vertex"); |
| 425 | |
| 426 | // Prepare a texture to render to |
| 427 | glActiveTexture(GL_TEXTURE0); |
| 428 | glGenTextures(1, &m_state.framebuffer_texture); |
| 429 | glBindTexture(GL_TEXTURE_2D, m_state.framebuffer_texture); |
| 430 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_state.fbwidth, m_state.fbheight, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); |
| 431 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 432 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 433 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 434 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 435 | |
| 436 | // Prepare a framebuffer for rendering |
| 437 | glGenFramebuffers(1, &m_state.effect_fb); |
| 438 | glBindFramebuffer(GL_FRAMEBUFFER, m_state.effect_fb); |
| 439 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_state.framebuffer_texture, 0); |
| 440 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 441 | |
| 442 | m_initialTime = static_cast<int64_t>(std::chrono::duration<double>(std::chrono::high_resolution_clock::now().time_since_epoch()).count() * 1000.0); |
| 443 | } |
| 444 | |
| 445 | void CScreensaverShadertoy::UnloadPreset() |
| 446 | { |
| 447 | if (m_state.framebuffer_texture) |
| 448 | { |
| 449 | glDeleteTextures(1, &m_state.framebuffer_texture); |
| 450 | m_state.framebuffer_texture = 0; |
| 451 | } |
| 452 | if (m_state.effect_fb) |
| 453 | { |
| 454 | glDeleteFramebuffers(1, &m_state.effect_fb); |
| 455 | m_state.effect_fb = 0; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | GLuint CScreensaverShadertoy::CreateTexture(GLint format, unsigned int w, unsigned int h, const GLvoid* data) |
| 460 | { |
| 461 | GLuint texture = 0; |
| 462 | glActiveTexture(GL_TEXTURE0); |
| 463 | glGenTextures(1, &texture); |
| 464 | glBindTexture(GL_TEXTURE_2D, texture); |
| 465 | |
| 466 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 467 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 468 | |
| 469 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 470 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 471 | |
| 472 | glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0, format, GL_UNSIGNED_BYTE, data); |
| 473 | return texture; |
| 474 | } |
| 475 | |
| 476 | GLuint CScreensaverShadertoy::CreateTexture(const GLvoid* data, GLint format, unsigned int w, unsigned int h, GLint internalFormat, GLint scaling, GLint repeat) |
| 477 | { |
| 478 | GLuint texture = 0; |
| 479 | glGenTextures(1, &texture); |
| 480 | glBindTexture(GL_TEXTURE_2D, texture); |
| 481 | |
| 482 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, scaling); |
| 483 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, scaling); |
| 484 | |
| 485 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, repeat); |
| 486 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, repeat); |
| 487 | |
| 488 | glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, format, GL_UNSIGNED_BYTE, data); |
| 489 | glBindTexture(GL_TEXTURE_2D, 0); |
| 490 | |
| 491 | return texture; |
| 492 | } |
| 493 | |
| 494 | GLuint CScreensaverShadertoy::CreateTexture(const std::string& file, GLint internalFormat, GLint scaling, GLint repeat) |
| 495 | { |
| 496 | #ifdef DEBUG_PRINT |
| 497 | printf("creating texture %s\n", file.c_str()); |
| 498 | #endif |
| 499 | |
| 500 | unsigned error; |
| 501 | unsigned char* image; |
| 502 | unsigned width, height; |
| 503 | |
| 504 | error = lodepng_decode32_file(&image, &width, &height, file.c_str()); |
| 505 | if (error) |
| 506 | { |
| 507 | kodi::Log(ADDON_LOG_ERROR, "lodepng_decode32_file error %u: %s", error, lodepng_error_text(error)); |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | GLuint texture = CreateTexture(image, GL_RGBA, width, height, internalFormat, scaling, repeat); |
| 512 | free(image); |
| 513 | return texture; |
| 514 | } |
| 515 | |
| 516 | int CScreensaverShadertoy::DetermineBitsPrecision() |
| 517 | { |
| 518 | m_state.fbwidth = 32, m_state.fbheight = 26*10; |
| 519 | LoadPreset(kodi::addon::GetAddonPath("resources/shaders/main_test.frag.glsl")); |
| 520 | RenderTo(m_shadertoyShader.ProgramHandle(), m_state.effect_fb); |
| 521 | glFinish(); |
| 522 | |
| 523 | unsigned char* buffer = new unsigned char[m_state.fbwidth * m_state.fbheight * 4]; |
| 524 | if (buffer) |
| 525 | glReadPixels(0, 0, m_state.fbwidth, m_state.fbheight, GL_RGBA, GL_UNSIGNED_BYTE, buffer); |
| 526 | |
| 527 | int bits = 0; |
| 528 | unsigned char b = 0; |
| 529 | for (int j=0; j<m_state.fbheight; j++) |
| 530 | { |
| 531 | unsigned char c = buffer[4*(j*m_state.fbwidth+(m_state.fbwidth>>1))]; |
| 532 | if (c && !b) |
| 533 | bits++; |
| 534 | b = c; |
| 535 | } |
| 536 | delete buffer; |
| 537 | UnloadPreset(); |
| 538 | return bits; |
| 539 | } |
| 540 | |
| 541 | double CScreensaverShadertoy::MeasurePerformance(const std::string& shaderPath, int size) |
| 542 | { |
| 543 | int iterations = -1; |
| 544 | m_state.fbwidth = m_state.fbheight = size; |
| 545 | LoadPreset(shaderPath); |
| 546 | |
| 547 | int64_t end, start; |
| 548 | do |
| 549 | { |
| 550 | RenderTo(m_shadertoyShader.ProgramHandle(), m_state.effect_fb); |
| 551 | RenderTo(m_displayShader.ProgramHandle(), m_state.effect_fb); |
| 552 | glFinish(); |
| 553 | if (++iterations == 0) |
| 554 | start = static_cast<int64_t>(std::chrono::duration<double>(std::chrono::high_resolution_clock::now().time_since_epoch()).count() * 1000.0); |
| 555 | end = static_cast<int64_t>(std::chrono::duration<double>(std::chrono::high_resolution_clock::now().time_since_epoch()).count() * 1000.0); |
| 556 | } while (end - start < 50); |
| 557 | double t = (double)(end - start)/iterations; |
| 558 | #ifdef DEBUG_PRINT |
| 559 | printf("%s %dx%d %.1fms = %.2f fps\n", __func__, size, size, t, 1000.0/t); |
| 560 | #endif |
| 561 | UnloadPreset(); |
| 562 | return t; |
| 563 | } |
| 564 | |
| 565 | ADDONCREATOR(CScreensaverShadertoy) // Don't touch this! |