CWallpaper.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. #include <algorithm>
  2. #include <cmath>
  3. #include <tuple>
  4. #include <vector>
  5. #include "ImageDecoder.h"
  6. #include "CWallpaper.h"
  7. #include "FrameMaterial.h"
  8. #include "WallpaperEngine/Data/Parsers/TextureParser.h"
  9. #include "WallpaperEngine/Data/Utils/BinaryReader.h"
  10. #include "WallpaperEngine/Logging/Log.h"
  11. #include "WallpaperEngine/Render/Wallpapers/CScene.h"
  12. #include "WallpaperEngine/Render/Wallpapers/CSplat.h"
  13. #include "WallpaperEngine/Render/Wallpapers/CVideo.h"
  14. #include "WallpaperEngine/Render/Wallpapers/CWeb.h"
  15. #include "WallpaperEngine/Data/Model/Project.h"
  16. #include "WallpaperEngine/Data/Model/Wallpaper.h"
  17. #include "WallpaperEngine/Data/Utils/FloatCompare.h"
  18. using namespace WallpaperEngine::Render;
  19. using WallpaperEngine::Data::Utils::BinaryReader;
  20. CWallpaper::CWallpaper (
  21. const Wallpaper& wallpaperData, RenderContext& context, AudioContext& audioContext,
  22. const WallpaperState::TextureUVsScaling& scalingMode, const uint32_t& clampMode
  23. ) :
  24. ContextAware (context), FBOProvider (nullptr), m_wallpaperData (wallpaperData), m_audioContext (audioContext),
  25. m_state (scalingMode, clampMode) {
  26. glGenVertexArrays (1, &this->m_vaoBuffer);
  27. glBindVertexArray (this->m_vaoBuffer);
  28. this->setupShaders ();
  29. constexpr GLfloat texCoords[] = { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f };
  30. constexpr GLfloat position[] = { -1.0f, 1.0f, 0.0f, 1.0, 1.0f, 0.0f, -1.0f, -1.0f, 0.0f,
  31. -1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 0.0f };
  32. glGenBuffers (1, &this->m_texCoordBuffer);
  33. glBindBuffer (GL_ARRAY_BUFFER, this->m_texCoordBuffer);
  34. glBufferData (GL_ARRAY_BUFFER, sizeof (texCoords), texCoords, GL_DYNAMIC_DRAW);
  35. glEnableVertexAttribArray (this->a_TexCoord);
  36. glVertexAttribPointer (this->a_TexCoord, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
  37. glGenBuffers (1, &this->m_positionBuffer);
  38. glBindBuffer (GL_ARRAY_BUFFER, this->m_positionBuffer);
  39. glBufferData (GL_ARRAY_BUFFER, sizeof (position), position, GL_STATIC_DRAW);
  40. glEnableVertexAttribArray (this->a_Position);
  41. glVertexAttribPointer (this->a_Position, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
  42. }
  43. CWallpaper::~CWallpaper () {
  44. if (this->m_backBuffer.texture != GL_NONE) {
  45. glDeleteFramebuffers (1, &this->m_backBuffer.framebuffer);
  46. glDeleteTextures (1, &this->m_backBuffer.texture);
  47. }
  48. GLuint attachedShaders[2];
  49. GLsizei attachedCount = 0;
  50. glGetAttachedShaders (this->m_shader, 2, &attachedCount, attachedShaders);
  51. for (auto i = 0; i < attachedCount; i++) {
  52. glDeleteShader (attachedShaders[i]);
  53. }
  54. glDeleteProgram (this->m_shader);
  55. if (this->m_lutTexture != GL_NONE) {
  56. glDeleteTextures (1, &this->m_lutTexture);
  57. }
  58. if (this->m_outputSampler != GL_NONE) {
  59. glDeleteSamplers (1, &this->m_outputSampler);
  60. }
  61. glDeleteBuffers (1, &this->m_texCoordBuffer);
  62. glDeleteBuffers (1, &this->m_positionBuffer);
  63. glDeleteVertexArrays (1, &this->m_vaoBuffer);
  64. }
  65. const AssetLocator& CWallpaper::getAssetLocator () const { return *this->m_wallpaperData.project.assetLocator; }
  66. const Wallpaper& CWallpaper::getWallpaperData () const { return this->m_wallpaperData; }
  67. GLuint CWallpaper::getWallpaperFramebuffer () const {
  68. return this->m_presentBackBuffer ? this->m_backBuffer.framebuffer : this->m_sceneFBO->getFramebuffer ();
  69. }
  70. GLuint CWallpaper::getWallpaperTexture () const {
  71. return this->m_presentBackBuffer ? this->m_backBuffer.texture : this->m_sceneFBO->getTextureID (0);
  72. }
  73. void CWallpaper::prepareBackBuffer (const glm::ivec2& size, const GLenum format) {
  74. auto& back = this->m_backBuffer;
  75. if (back.size == size && back.format == format) {
  76. return;
  77. }
  78. if (back.texture == GL_NONE) {
  79. glGenTextures (1, &back.texture);
  80. glGenFramebuffers (1, &back.framebuffer);
  81. }
  82. glBindTexture (GL_TEXTURE_2D, back.texture);
  83. glTexImage2D (GL_TEXTURE_2D, 0, static_cast<GLint> (format), size.x, size.y, 0, GL_RGBA, GL_FLOAT, nullptr);
  84. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  85. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  86. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  87. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  88. glBindFramebuffer (GL_FRAMEBUFFER, back.framebuffer);
  89. glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, back.texture, 0);
  90. #if !NDEBUG
  91. glObjectLabel (GL_TEXTURE, back.texture, -1, "back buffer");
  92. #endif
  93. back.size = size;
  94. back.format = format;
  95. }
  96. void CWallpaper::renderImageAdjustments (const glm::ivec2& size, const GLenum format) {
  97. // sub_140181F30: COL when the color options moved, LUT for a filter above 0, otherwise no material
  98. const bool color = this->m_colorEnabled;
  99. const bool lut = this->m_lutTexture != GL_NONE && this->m_lutStrength > 0.0f;
  100. if (!color && !lut) {
  101. this->m_imageAdjustmentsMaterial = nullptr;
  102. return;
  103. }
  104. if (this->m_imageAdjustmentsMaterial == nullptr || this->m_imageAdjustmentsCombos != std::pair (color, lut)) {
  105. ComboMap combos;
  106. if (color) {
  107. combos.emplace ("COL", 1);
  108. }
  109. combos.emplace ("LUT", lut ? 1 : 0);
  110. this->m_imageAdjustmentsMaterial = std::make_unique<FrameMaterial> (
  111. *this, this->m_wallpaperData.project, "materials/util/ccsimple.json", combos
  112. );
  113. this->m_imageAdjustmentsCombos = { color, lut };
  114. }
  115. FrameMaterial& material = *this->m_imageAdjustmentsMaterial;
  116. if (color) {
  117. material.setConstant ("params", this->m_colorParams);
  118. }
  119. if (lut) {
  120. material.setTexture (1, this->m_lutTexture, GL_TEXTURE_3D);
  121. material.setConstant ("lutparams", this->m_lutStrength);
  122. }
  123. // sub_14017FA70 copies the back buffer into _rt_FullFrameBuffer first
  124. this->prepareBackBuffer (size, format);
  125. if (this->m_presentBackBuffer) {
  126. glBindFramebuffer (GL_READ_FRAMEBUFFER, this->m_backBuffer.framebuffer);
  127. glBindFramebuffer (GL_DRAW_FRAMEBUFFER, this->m_sceneFBO->getFramebuffer ());
  128. glBlitFramebuffer (0, 0, size.x, size.y, 0, 0, size.x, size.y, GL_COLOR_BUFFER_BIT, GL_NEAREST);
  129. }
  130. material.draw (this->m_backBuffer.framebuffer, this->m_backBuffer.size);
  131. this->m_presentBackBuffer = true;
  132. }
  133. void CWallpaper::setupShaders () {
  134. const GLuint vertexShaderID = glCreateShader (GL_VERTEX_SHADER);
  135. const char* sourcePointer = "#version 330\n"
  136. "precision highp float;\n"
  137. "in vec3 a_Position;\n"
  138. "in vec2 a_TexCoord;\n"
  139. "out vec2 v_TexCoord;\n"
  140. "void main () {\n"
  141. "gl_Position = vec4 (a_Position, 1.0);\n"
  142. "v_TexCoord = a_TexCoord;\n"
  143. "}";
  144. glShaderSource (vertexShaderID, 1, &sourcePointer, nullptr);
  145. glCompileShader (vertexShaderID);
  146. GLint result = GL_FALSE;
  147. int infoLogLength = 0;
  148. glGetShaderiv (vertexShaderID, GL_COMPILE_STATUS, &result);
  149. glGetShaderiv (vertexShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
  150. if (infoLogLength > 0) {
  151. const auto logBuffer = new char[infoLogLength + 1];
  152. memset (logBuffer, 0, infoLogLength + 1);
  153. glGetShaderInfoLog (vertexShaderID, infoLogLength, nullptr, logBuffer);
  154. const std::string message = logBuffer;
  155. delete[] logBuffer;
  156. sLog.exception (message);
  157. }
  158. const GLuint fragmentShaderID = glCreateShader (GL_FRAGMENT_SHADER);
  159. sourcePointer
  160. = "#version 330\n"
  161. "precision highp float;\n"
  162. "uniform sampler2D g_Texture0;\n"
  163. "uniform sampler3D g_Texture1;\n"
  164. "uniform vec4 g_Params;\n"
  165. "uniform float g_LutParams;\n"
  166. "uniform bool u_ColorEnabled;\n"
  167. "uniform bool u_LutEnabled;\n"
  168. "uniform bool u_InputLinear;\n"
  169. "uniform bool u_InputScRGB;\n"
  170. "uniform bool u_OutputPQ;\n"
  171. "uniform int u_Supersample;\n"
  172. "in vec2 v_TexCoord;\n"
  173. "out vec4 out_FragColor;\n"
  174. // BT.2408 reference white, what the PQ image description of HDR surfaces anchors SDR content to
  175. "const float referenceWhite = 203.0;\n"
  176. "const mat3 bt709to2020 = mat3 (0.6274, 0.0691, 0.0164, 0.3293, 0.9195, 0.0880, 0.0433, 0.0114, 0.8956);\n"
  177. "const mat3 bt2020to709 = mat3 (1.6605, -0.1246, -0.0182, -0.5876, 1.1329, -0.1006, -0.0728, -0.0083, "
  178. "1.1187);\n"
  179. "vec3 srgbToLinear (vec3 c) {\n"
  180. "return mix (c / 12.92, pow ((c + 0.055) / 1.055, vec3 (2.4)), step (0.04045, c));\n"
  181. "}\n"
  182. "vec3 linearToSrgb (vec3 c) {\n"
  183. "return mix (c * 12.92, 1.055 * pow (c, vec3 (1.0 / 2.4)) - 0.055, step (0.0031308, c));\n"
  184. "}\n"
  185. // mpv decodes SDR video with BT.1886, a pure 2.4 power, going back the same way keeps SDR videos
  186. // exactly as they look without --hdr
  187. "vec3 videoToLinear (vec3 c) { return pow (c, vec3 (2.4)); }\n"
  188. "vec3 linearToVideo (vec3 c) { return pow (c, vec3 (1.0 / 2.4)); }\n"
  189. "vec3 pqEncode (vec3 nits) {\n"
  190. "vec3 y = pow (clamp (nits / 10000.0, 0.0, 1.0), vec3 (0.1593017578125));\n"
  191. "return pow ((0.8359375 + 18.8515625 * y) / (1.0 + 18.6875 * y), vec3 (78.84375));\n"
  192. "}\n"
  193. "vec3 softClip (vec3 x) {\n"
  194. "return mix (x, 0.8 + 0.2 * (1.0 - exp (-(x - 0.8) / 0.2)), step (0.8, x));\n"
  195. "}\n"
  196. "vec3 hsv2rgb (vec3 c) {\n"
  197. "vec4 K = vec4 (1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);\n"
  198. "vec3 p = abs (fract (c.xxx + K.xyz) * 6.0 - K.www);\n"
  199. "return c.z * mix (K.xxx, clamp (p - K.xxx, 0.0, 1.0), c.y);\n"
  200. "}\n"
  201. "vec3 rgb2hsv (vec3 RGB) {\n"
  202. "vec4 P = (RGB.g < RGB.b) ? vec4 (RGB.bg, -1.0, 2.0 / 3.0) : vec4 (RGB.gb, 0.0, -1.0 / 3.0);\n"
  203. "vec4 Q = (RGB.r < P.x) ? vec4 (P.xyw, RGB.r) : vec4 (RGB.r, P.yzx);\n"
  204. "float C = Q.x - min (Q.w, Q.y);\n"
  205. "float H = abs ((Q.w - Q.y) / (6.0 * C + 1e-10) + Q.z);\n"
  206. "return vec3 (H, C / (Q.x + 1e-10), Q.x);\n"
  207. "}\n"
  208. "void main () {\n"
  209. "vec4 albedo = vec4 (0.0);\n"
  210. "if (u_Supersample > 1) {\n"
  211. "vec2 stepX = dFdx (v_TexCoord);\n"
  212. "vec2 stepY = dFdy (v_TexCoord);\n"
  213. "for (int y = 0; y < u_Supersample; y++) {\n"
  214. "for (int x = 0; x < u_Supersample; x++) {\n"
  215. "vec2 offset = (vec2 (x, y) + 0.5) / float (u_Supersample) - 0.5;\n"
  216. "albedo += texture (g_Texture0, v_TexCoord + offset.x * stepX + offset.y * stepY);\n"
  217. "}\n"
  218. "}\n"
  219. "albedo /= float (u_Supersample * u_Supersample);\n"
  220. "} else {\n"
  221. "albedo = texture (g_Texture0, v_TexCoord);\n"
  222. "}\n"
  223. "if (u_InputScRGB) {\n"
  224. "albedo.rgb = u_OutputPQ ? bt709to2020 * albedo.rgb : linearToSrgb (clamp (albedo.rgb, 0.0, 1.0));\n"
  225. "}\n"
  226. "bool filtered = u_ColorEnabled || u_LutEnabled;\n"
  227. "vec3 overbright = vec3 (0.0);\n"
  228. // the filters work on SDR-encoded values like in Wallpaper Engine, what's past reference white is kept aside
  229. "if (u_InputLinear && filtered) {\n"
  230. "vec3 light = max (bt2020to709 * albedo.rgb, 0.0);\n"
  231. "overbright = max (light - 1.0, 0.0);\n"
  232. "albedo.rgb = linearToVideo (min (light, 1.0));\n"
  233. "}\n"
  234. "if (u_ColorEnabled) {\n"
  235. "albedo.rgb = mix (vec3 (0.5), albedo.rgb, g_Params.y);\n"
  236. "vec3 hsv = rgb2hsv (albedo.rgb);\n"
  237. "hsv.z *= g_Params.x;\n"
  238. "hsv.y *= g_Params.z;\n"
  239. "hsv.x += g_Params.w;\n"
  240. "albedo.rgb = hsv2rgb (hsv);\n"
  241. "}\n"
  242. "if (u_LutEnabled) {\n"
  243. "albedo.rgb = mix (albedo.rgb, texture (g_Texture1, albedo.rgb).rgb, g_LutParams);\n"
  244. "}\n"
  245. "if (u_InputLinear || u_OutputPQ) {\n"
  246. "vec3 encoded = clamp (albedo.rgb, 0.0, 1.0);\n"
  247. "vec3 linear2020 = !u_InputLinear ? bt709to2020 * srgbToLinear (encoded)\n"
  248. " : filtered ? bt709to2020 * (videoToLinear (encoded) + overbright) : albedo.rgb;\n"
  249. "albedo.rgb = u_OutputPQ ? pqEncode (max (linear2020, 0.0) * referenceWhite)\n"
  250. " : linearToVideo (min (softClip (max (bt2020to709 * linear2020, 0.0)), 1.0));\n"
  251. "}\n"
  252. "out_FragColor = albedo;\n"
  253. "}";
  254. glShaderSource (fragmentShaderID, 1, &sourcePointer, nullptr);
  255. glCompileShader (fragmentShaderID);
  256. result = GL_FALSE;
  257. infoLogLength = 0;
  258. glGetShaderiv (fragmentShaderID, GL_COMPILE_STATUS, &result);
  259. glGetShaderiv (fragmentShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
  260. if (infoLogLength > 0) {
  261. const auto logBuffer = new char[infoLogLength + 1];
  262. memset (logBuffer, 0, infoLogLength + 1);
  263. glGetShaderInfoLog (fragmentShaderID, infoLogLength, nullptr, logBuffer);
  264. const std::string message = logBuffer;
  265. delete[] logBuffer;
  266. sLog.exception (message);
  267. }
  268. this->m_shader = glCreateProgram ();
  269. glAttachShader (this->m_shader, vertexShaderID);
  270. glAttachShader (this->m_shader, fragmentShaderID);
  271. glLinkProgram (this->m_shader);
  272. result = GL_FALSE;
  273. infoLogLength = 0;
  274. glGetProgramiv (this->m_shader, GL_LINK_STATUS, &result);
  275. glGetProgramiv (this->m_shader, GL_INFO_LOG_LENGTH, &infoLogLength);
  276. if (infoLogLength > 0) {
  277. const auto logBuffer = new char[infoLogLength + 1];
  278. memset (logBuffer, 0, infoLogLength + 1);
  279. glGetProgramInfoLog (this->m_shader, infoLogLength, nullptr, logBuffer);
  280. const std::string message = logBuffer;
  281. delete[] logBuffer;
  282. sLog.exception (message);
  283. }
  284. glDetachShader (this->m_shader, vertexShaderID);
  285. glDetachShader (this->m_shader, fragmentShaderID);
  286. glDeleteShader (vertexShaderID);
  287. glDeleteShader (fragmentShaderID);
  288. this->g_Texture0 = glGetUniformLocation (this->m_shader, "g_Texture0");
  289. this->g_Texture1 = glGetUniformLocation (this->m_shader, "g_Texture1");
  290. this->g_Params = glGetUniformLocation (this->m_shader, "g_Params");
  291. this->g_LutParams = glGetUniformLocation (this->m_shader, "g_LutParams");
  292. this->u_ColorEnabled = glGetUniformLocation (this->m_shader, "u_ColorEnabled");
  293. this->u_LutEnabled = glGetUniformLocation (this->m_shader, "u_LutEnabled");
  294. this->u_InputLinear = glGetUniformLocation (this->m_shader, "u_InputLinear");
  295. this->u_InputScRGB = glGetUniformLocation (this->m_shader, "u_InputScRGB");
  296. this->u_OutputPQ = glGetUniformLocation (this->m_shader, "u_OutputPQ");
  297. this->u_Supersample = glGetUniformLocation (this->m_shader, "u_Supersample");
  298. this->a_Position = glGetAttribLocation (this->m_shader, "a_Position");
  299. this->a_TexCoord = glGetAttribLocation (this->m_shader, "a_TexCoord");
  300. }
  301. void CWallpaper::setDestinationFramebuffer (GLuint framebuffer) { this->m_destFramebuffer = framebuffer; }
  302. void CWallpaper::setSpanInfo (const SpanInfo& spanInfo) { this->m_spanInfo = spanInfo; }
  303. const CWallpaper::SpanInfo* CWallpaper::getSpanInfo () const {
  304. return this->m_spanInfo.has_value () ? &this->m_spanInfo.value () : nullptr;
  305. }
  306. void CWallpaper::updateUVs (const glm::ivec4& viewport, const bool vflip) {
  307. if (this->m_state.hasChanged (viewport, vflip, this->getCanvasWidth (), this->getCanvasHeight ())) {
  308. this->m_state.updateState (viewport, vflip, this->getCanvasWidth (), this->getCanvasHeight ());
  309. }
  310. }
  311. void CWallpaper::updateSpanBounds () {
  312. auto& span = *this->m_spanInfo;
  313. const auto& viewports = this->getContext ().getOutput ().getViewports ();
  314. glm::ivec2 low (std::numeric_limits<int>::max ());
  315. glm::ivec2 high (std::numeric_limits<int>::min ());
  316. double scale = 0.0;
  317. for (const auto& screen : span.screens) {
  318. const auto it = viewports.find (screen);
  319. if (it == viewports.end () || it->second->logicalSize.x <= 0 || it->second->logicalSize.y <= 0) {
  320. continue;
  321. }
  322. const auto* output = it->second;
  323. low = glm::min (low, output->globalPosition);
  324. high = glm::max (high, output->globalPosition + output->logicalSize);
  325. scale = std::max (scale, static_cast<double> (output->viewport.z) / output->logicalSize.x);
  326. }
  327. if (scale <= 0.0) {
  328. return;
  329. }
  330. const glm::ivec4 bounds { low, high - low };
  331. GLint maxTexture = 0;
  332. GLint maxViewport[2] = { 0, 0 };
  333. glGetIntegerv (GL_MAX_TEXTURE_SIZE, &maxTexture);
  334. glGetIntegerv (GL_MAX_VIEWPORT_DIMS, maxViewport);
  335. const double limit = std::min ({ maxTexture, maxViewport[0], maxViewport[1] });
  336. if (limit > 0.0) {
  337. scale = std::min (scale, limit / std::max (bounds.z, bounds.w));
  338. }
  339. if (bounds != span.totalBounds || scale != span.scale) {
  340. sLog.out (
  341. "Span ", span.screens.front (), ": ", bounds.z, "x", bounds.w, " at ", bounds.x, ",", bounds.y,
  342. ", rendered at ", scale, "x"
  343. );
  344. }
  345. span.totalBounds = bounds;
  346. span.scale = scale;
  347. }
  348. void CWallpaper::updateSpanCursor (
  349. const glm::ivec4& viewport, const glm::ivec2& globalPosition, const glm::ivec2& logicalSize
  350. ) {
  351. const auto& span = *this->m_spanInfo;
  352. const auto& input = this->getContext ().getInputContext ().getMouseInput ();
  353. auto desktop = input.desktopPosition ();
  354. if (!desktop.has_value () && viewport.z > 0 && viewport.w > 0) {
  355. const glm::dvec2 local
  356. = (input.position () - glm::dvec2 (viewport.x, viewport.y)) / glm::dvec2 (viewport.z, viewport.w);
  357. desktop = glm::dvec2 (globalPosition) + glm::dvec2 (local.x, 1.0 - local.y) * glm::dvec2 (logicalSize);
  358. }
  359. if (!desktop.has_value ()) {
  360. return;
  361. }
  362. // ScreenToClient on the group's window (sub_140110630), in its pixels with y up like MouseInput
  363. this->m_spanCursor = glm::dvec2 (
  364. (desktop->x - span.totalBounds.x) * span.scale,
  365. (span.totalBounds.y + span.totalBounds.w - desktop->y) * span.scale
  366. );
  367. }
  368. glm::dvec2 CWallpaper::cursorPosition () const {
  369. if (this->m_spanInfo.has_value ()) {
  370. return this->m_spanCursor;
  371. }
  372. return this->getContext ().getInputContext ().getMouseInput ().position ();
  373. }
  374. bool CWallpaper::pointerInRegion () const {
  375. if (!this->m_pointerRegion.has_value ()) {
  376. return true;
  377. }
  378. const auto region = this->toCursorSpace (*this->m_pointerRegion);
  379. const glm::dvec2 position = this->cursorPosition ();
  380. return position.x >= region.x && position.x < region.x + region.z && position.y >= region.y
  381. && position.y < region.y + region.w;
  382. }
  383. glm::ivec4 CWallpaper::toCursorSpace (const glm::ivec4& rect) const {
  384. if (!this->m_cursorMirrorHeight.has_value ()) {
  385. return rect;
  386. }
  387. return { rect.x, *this->m_cursorMirrorHeight - rect.y - rect.w, rect.z, rect.w };
  388. }
  389. WallpaperEngine::Input::MouseClickStatus
  390. CWallpaper::regionButton (const Input::MouseClickStatus status, PointerPress& press) const {
  391. if (!this->m_pointerRegion.has_value ()) {
  392. return status;
  393. }
  394. if (status == Input::MouseClickStatus::Released) {
  395. press = {};
  396. return status;
  397. }
  398. // a press belongs to the slot it started on until release
  399. if (!press.held) {
  400. press = { .held = true, .owned = this->pointerInRegion () };
  401. }
  402. return press.owned ? status : Input::MouseClickStatus::Released;
  403. }
  404. WallpaperEngine::Input::MouseClickStatus CWallpaper::cursorLeftClick () const {
  405. const auto& input = this->getContext ().getInputContext ().getMouseInput ();
  406. return this->regionButton (
  407. this->m_spanInfo.has_value () ? input.desktopLeftClick () : input.leftClick (), this->m_leftPress
  408. );
  409. }
  410. WallpaperEngine::Input::MouseClickStatus CWallpaper::cursorRightClick () const {
  411. return this->regionButton (
  412. this->getContext ().getInputContext ().getMouseInput ().rightClick (), this->m_rightPress
  413. );
  414. }
  415. glm::vec4 CWallpaper::getOutputUVs () const {
  416. if (this->rendersAtOutputSize ()) {
  417. return this->m_state.isVFlipped () ? glm::vec4 { 0.0f, 1.0f, 0.0f, 1.0f }
  418. : glm::vec4 { 0.0f, 1.0f, 1.0f, 0.0f };
  419. }
  420. const auto uvs = this->m_state.getTextureUVs ();
  421. return { uvs.ustart, uvs.uend, uvs.vstart, uvs.vend };
  422. }
  423. int CWallpaper::getFramebufferWidth () const {
  424. return this->m_sceneFBO != nullptr ? static_cast<int> (this->m_sceneFBO->getRealWidth ()) : this->getCanvasWidth ();
  425. }
  426. int CWallpaper::getFramebufferHeight () const {
  427. return this->m_sceneFBO != nullptr ? static_cast<int> (this->m_sceneFBO->getRealHeight ())
  428. : this->getCanvasHeight ();
  429. }
  430. void CWallpaper::render (
  431. const glm::ivec4& viewport, const bool vflip, const glm::ivec2& globalPosition, const glm::ivec2& logicalSize
  432. ) {
  433. const uint32_t currentFrame = this->getContext ().getDriver ().getFrameCounter ();
  434. const bool needsSceneRender = (currentFrame != this->m_lastRenderedFrame);
  435. glm::ivec4 sceneViewport = viewport;
  436. // WE makes a stretch group one more monitor whose rect is the bounding box of its monitors (sub_140061B50),
  437. // drawn into one window over that rect: the scene, its cursor and the flip cover the whole group
  438. if (this->m_spanInfo.has_value ()) {
  439. if (needsSceneRender) {
  440. this->updateSpanBounds ();
  441. this->updateSpanCursor (viewport, globalPosition, logicalSize);
  442. }
  443. const auto& span = *this->m_spanInfo;
  444. sceneViewport = { 0, 0, static_cast<int> (std::lround (span.totalBounds.z * span.scale)),
  445. static_cast<int> (std::lround (span.totalBounds.w * span.scale)) };
  446. }
  447. this->m_screenSize = { sceneViewport.z, sceneViewport.w };
  448. // X11 rects count y from the top, the cursor from the bottom; a span's cursor is already in group pixels
  449. this->m_cursorMirrorHeight = vflip || this->m_spanInfo.has_value ()
  450. ? std::nullopt
  451. : std::optional (this->getContext ().getOutput ().getFullHeight ());
  452. this->m_cursorViewport = this->toCursorSpace (sceneViewport);
  453. this->m_supersampling
  454. = this->rendersAtOutputSize () ? this->supersamplingFor ({ sceneViewport.z, sceneViewport.w }) : 1;
  455. // the scene reads the visible region from these while it renders
  456. this->updateUVs (sceneViewport, vflip);
  457. #if !NDEBUG
  458. glPushDebugGroup (GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Rendering scene");
  459. #endif
  460. if (needsSceneRender) {
  461. this->renderFrame (sceneViewport);
  462. this->m_lastRenderedFrame = currentFrame;
  463. // a video learns its real size inside renderFrame, the UVs from before it would crop the first frame
  464. this->updateUVs (sceneViewport, vflip);
  465. }
  466. #if !NDEBUG
  467. glPopDebugGroup ();
  468. #endif
  469. this->present (viewport, globalPosition, logicalSize);
  470. }
  471. void CWallpaper::present (const glm::ivec4& viewport, const glm::ivec2& globalPosition, const glm::ivec2& logicalSize) {
  472. #if !NDEBUG
  473. glPushDebugGroup (GL_DEBUG_SOURCE_APPLICATION, 0, -1, "Rendering scene to output");
  474. #endif
  475. float ustart, uend, vstart, vend;
  476. if (this->m_spanInfo.has_value ()) {
  477. const auto& span = this->m_spanInfo.value ();
  478. const auto spanW = static_cast<float> (span.totalBounds.z);
  479. const auto spanH = static_cast<float> (span.totalBounds.w);
  480. const auto spanX = static_cast<float> (span.totalBounds.x);
  481. const auto spanY = static_cast<float> (span.totalBounds.y);
  482. const glm::vec4 base = this->getOutputUVs ();
  483. const float baseUstart = base.x;
  484. const float baseUend = base.y;
  485. const float baseVstart = base.z;
  486. const float baseVend = base.w;
  487. float relLeft = (static_cast<float> (globalPosition.x) - spanX) / spanW;
  488. float relRight = (static_cast<float> (globalPosition.x + logicalSize.x) - spanX) / spanW;
  489. // a flipped span mirrors the whole group, the leftmost screen shows the right end of the image
  490. if (this->m_flipHorizontal) {
  491. std::tie (relLeft, relRight) = std::make_pair (1.0f - relLeft, 1.0f - relRight);
  492. }
  493. const float relTop = (static_cast<float> (globalPosition.y) - spanY) / spanH;
  494. const float relBottom = (static_cast<float> (globalPosition.y + logicalSize.y) - spanY) / spanH;
  495. const float baseURange = baseUend - baseUstart;
  496. const float baseVRange = baseVend - baseVstart;
  497. ustart = baseUstart + relLeft * baseURange;
  498. uend = baseUstart + relRight * baseURange;
  499. vstart = baseVstart + relTop * baseVRange;
  500. vend = baseVstart + relBottom * baseVRange;
  501. if (this->m_lastRenderedFrame < 5) {
  502. sLog.debug (
  503. "SPAN DEBUG: viewport=", viewport.z, "x", viewport.w, " globalPos=(", globalPosition.x, ",",
  504. globalPosition.y, ")", " span=(", span.totalBounds.x, ",", span.totalBounds.y, ",", span.totalBounds.z,
  505. ",", span.totalBounds.w, ")", " rel=[", relLeft, ",", relRight, "]x[", relTop, ",", relBottom, "]",
  506. " baseUV=[", baseUstart, ",", baseUend, "]x[", baseVstart, ",", baseVend, "]", " finalUV=[", ustart,
  507. ",", uend, "]x[", vstart, ",", vend, "]"
  508. );
  509. }
  510. } else {
  511. const glm::vec4 uvs = this->getOutputUVs ();
  512. ustart = uvs.x;
  513. uend = uvs.y;
  514. vstart = uvs.z;
  515. vend = uvs.w;
  516. if (this->m_flipHorizontal) {
  517. std::swap (ustart, uend);
  518. }
  519. }
  520. const GLfloat texCoords[] = {
  521. ustart, vstart, uend, vstart, ustart, vend, ustart, vend, uend, vstart, uend, vend,
  522. };
  523. glViewport (viewport.x, viewport.y, viewport.z, viewport.w);
  524. glBindFramebuffer (GL_FRAMEBUFFER, this->m_destFramebuffer);
  525. glBindVertexArray (this->m_vaoBuffer);
  526. if (!WallpaperEngine::Data::Utils::sameFloat (ustart, this->m_uploadedUstart)
  527. || !WallpaperEngine::Data::Utils::sameFloat (uend, this->m_uploadedUend)
  528. || !WallpaperEngine::Data::Utils::sameFloat (vstart, this->m_uploadedVstart)
  529. || !WallpaperEngine::Data::Utils::sameFloat (vend, this->m_uploadedVend)) {
  530. glBindBuffer (GL_ARRAY_BUFFER, this->m_texCoordBuffer);
  531. glBufferSubData (GL_ARRAY_BUFFER, 0, sizeof (texCoords), texCoords);
  532. this->m_uploadedUstart = ustart;
  533. this->m_uploadedUend = uend;
  534. this->m_uploadedVstart = vstart;
  535. this->m_uploadedVend = vend;
  536. }
  537. this->drawOutputQuad (this->m_supersampling);
  538. if (this->getContext ().getApp ().getContext ().settings.render.debug.brightnessLog) {
  539. static uint32_t brightnessFrameCounter = 0;
  540. if ((brightnessFrameCounter++ % 30) == 0) {
  541. std::vector<unsigned char> px (static_cast<size_t> (viewport.z) * viewport.w * 4);
  542. glReadPixels (viewport.x, viewport.y, viewport.z, viewport.w, GL_RGBA, GL_UNSIGNED_BYTE, px.data ());
  543. uint64_t sum = 0;
  544. for (size_t i = 0; i < px.size (); i += 4) {
  545. sum += px[i] + px[i + 1] + px[i + 2];
  546. }
  547. const double mean = static_cast<double> (sum) / static_cast<double> (px.size () / 4 * 3);
  548. sLog.out ("[BRIGHTNESS] frame=", brightnessFrameCounter, " mean=", mean);
  549. }
  550. }
  551. #if !NDEBUG
  552. glPopDebugGroup ();
  553. #endif
  554. }
  555. int CWallpaper::supersamplingFor (const glm::ivec2& outputSize) {
  556. const int requested = this->getContext ().getApp ().getContext ().settings.general.supersampling;
  557. if (requested <= 1 || outputSize.x <= 0 || outputSize.y <= 0) {
  558. return 1;
  559. }
  560. GLint maxTexture = 0;
  561. GLint maxRenderbuffer = 0;
  562. GLint maxViewport[2] = { 0, 0 };
  563. glGetIntegerv (GL_MAX_TEXTURE_SIZE, &maxTexture);
  564. glGetIntegerv (GL_MAX_RENDERBUFFER_SIZE, &maxRenderbuffer);
  565. glGetIntegerv (GL_MAX_VIEWPORT_DIMS, maxViewport);
  566. const int limit = std::min ({ maxTexture, maxRenderbuffer, maxViewport[0], maxViewport[1] });
  567. int factor = requested;
  568. while (factor > 1 && std::max (outputSize.x, outputSize.y) * factor > limit) {
  569. factor--;
  570. }
  571. if (factor != requested && factor != this->m_supersamplingLogged) {
  572. sLog.error (
  573. "SSAA x", requested, " would need a ", outputSize.x * requested, "x", outputSize.y * requested,
  574. " scene buffer, more than the GPU allows (", limit, "), using x", factor
  575. );
  576. }
  577. this->m_supersamplingLogged = factor;
  578. return factor;
  579. }
  580. void CWallpaper::drawOutputQuad (const int supersample) {
  581. glDisable (GL_BLEND);
  582. glDisable (GL_DEPTH_TEST);
  583. glDisable (GL_CULL_FACE);
  584. glUseProgram (this->m_shader);
  585. glActiveTexture (GL_TEXTURE0);
  586. glBindTexture (GL_TEXTURE_2D, this->getWallpaperTexture ());
  587. glUniform1i (this->g_Texture0, 0);
  588. if (this->m_outputSampler == GL_NONE) {
  589. glGenSamplers (1, &this->m_outputSampler);
  590. glSamplerParameteri (this->m_outputSampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  591. glSamplerParameteri (this->m_outputSampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  592. }
  593. const uint32_t clamp = this->m_state.getClampingMode ();
  594. const GLint wrap = (clamp & TextureFlags_ClampUVs) ? GL_CLAMP_TO_EDGE
  595. : (clamp & TextureFlags_ClampUVsBorder) ? GL_CLAMP_TO_BORDER
  596. : GL_REPEAT;
  597. glSamplerParameteri (this->m_outputSampler, GL_TEXTURE_WRAP_S, wrap);
  598. glSamplerParameteri (this->m_outputSampler, GL_TEXTURE_WRAP_T, wrap);
  599. glSamplerParameterfv (this->m_outputSampler, GL_TEXTURE_BORDER_COLOR, &this->m_cornerColor.x);
  600. glBindSampler (0, this->m_outputSampler);
  601. // a sampler2D and a sampler3D on the same unit is an invalid draw even when the LUT is never read
  602. glUniform1i (this->g_Texture1, 1);
  603. const bool adjusted = !this->drawsImageAdjustments ();
  604. const bool lutEnabled = adjusted && this->m_lutTexture != GL_NONE && this->m_lutStrength > 0.0f;
  605. glUniform1i (this->u_ColorEnabled, adjusted && this->m_colorEnabled);
  606. glUniform1i (this->u_LutEnabled, lutEnabled);
  607. glUniform1i (this->u_InputLinear, this->m_linearInput || (this->m_scRGBInput && this->m_outputHDR));
  608. glUniform1i (this->u_InputScRGB, this->m_scRGBInput);
  609. glUniform1i (this->u_OutputPQ, this->m_outputHDR);
  610. glUniform1i (this->u_Supersample, supersample);
  611. glUniform4fv (this->g_Params, 1, &this->m_colorParams.x);
  612. glUniform1f (this->g_LutParams, this->m_lutStrength);
  613. if (lutEnabled) {
  614. glActiveTexture (GL_TEXTURE1);
  615. glBindTexture (GL_TEXTURE_3D, this->m_lutTexture);
  616. glActiveTexture (GL_TEXTURE0);
  617. }
  618. glDrawArrays (GL_TRIANGLES, 0, 6);
  619. glBindSampler (0, GL_NONE);
  620. }
  621. void CWallpaper::setPause (bool newState) { }
  622. void CWallpaper::setAudioPolicy (bool muted, std::optional<int> ambientVolume) { }
  623. void CWallpaper::setAudioHushed (bool hushed) { }
  624. void CWallpaper::setupFramebuffers (const bool depth, const TextureFormat format) {
  625. const uint32_t width = this->getCanvasWidth ();
  626. const uint32_t height = this->getCanvasHeight ();
  627. // layers and effects sample the frame clamped to its edges like WE (2.8.42 RenderDoc: _rt_FullFrameBuffer
  628. // reads are ClampEdge), the clamp mode and corner color only apply when it goes to the output
  629. const auto sceneFBO = this->create (
  630. "_rt_FullFrameBuffer", format, TextureFlags_ClampUVs, 1.0, { width, height }, { width, height }
  631. );
  632. if (depth) {
  633. sceneFBO->attachDepthBuffer ();
  634. }
  635. this->m_sceneFBO = sceneFBO;
  636. }
  637. AudioContext& CWallpaper::getAudioContext () const { return this->m_audioContext; }
  638. const WallpaperState& CWallpaper::getState () const { return this->m_state; }
  639. void CWallpaper::setScalingMode (WallpaperState::TextureUVsScaling mode) { this->m_state.setTextureUVsStrategy (mode); }
  640. void CWallpaper::setZoom (float zoom) { this->m_state.setZoom (zoom); }
  641. void CWallpaper::setOffset (float offsetX, float offsetY) { this->m_state.setOffset (offsetX, offsetY); }
  642. void CWallpaper::setAlignment (const WallpaperState::Alignment& alignment) { this->m_state.setAlignment (alignment); }
  643. void CWallpaper::setCornerColor (const glm::vec4& color) { this->m_cornerColor = color; }
  644. void CWallpaper::setImageAdjustments (const ImageAdjustments& adjustments) {
  645. this->m_imageAdjustments = adjustments;
  646. // wallpaper64.exe sub_140181F30: sliders are 0-100 around 50, contrast and saturation go through a square
  647. // root and brightness through a square before reaching ccsimple
  648. const float brightness = adjustments.brightness.value_or (50.0f) / 50.0f;
  649. const float contrast = adjustments.contrast.value_or (50.0f) / 50.0f;
  650. const float saturation = adjustments.saturation.value_or (50.0f) / 50.0f;
  651. const float hue = adjustments.hue.value_or (50.0f) / 100.0f - 0.5f;
  652. this->m_colorEnabled = adjustments.colorEnabled.value_or (false)
  653. && (!WallpaperEngine::Data::Utils::sameFloat (contrast, 1.0f)
  654. || !WallpaperEngine::Data::Utils::sameFloat (brightness, 1.0f)
  655. || !WallpaperEngine::Data::Utils::sameFloat (saturation, 1.0f)
  656. || WallpaperEngine::Data::Utils::isNonZero (hue));
  657. this->m_colorParams = { std::pow (brightness, 2.0f), std::pow (contrast, 0.5f), std::pow (saturation, 0.5f), hue };
  658. this->m_lutStrength = adjustments.filterStrength.value_or (100.0f) / 100.0f;
  659. this->m_flipHorizontal = adjustments.flipHorizontal.value_or (false);
  660. // only scenes and videos have these in Wallpaper Engine
  661. if (this->is<Wallpapers::CWeb> ()) {
  662. this->m_colorEnabled = false;
  663. this->m_flipHorizontal = false;
  664. this->loadLut ("");
  665. return;
  666. }
  667. this->loadLut (adjustments.filter.value_or (""));
  668. }
  669. bool CWallpaper::hasImageAdjustments () const {
  670. return this->m_colorEnabled || this->m_flipHorizontal
  671. || (this->m_lutTexture != GL_NONE && this->m_lutStrength > 0.0f);
  672. }
  673. GLuint CWallpaper::renderAdjustedFramebuffer () {
  674. if (!this->hasImageAdjustments () && !this->m_linearInput && !this->m_scRGBInput) {
  675. return this->getWallpaperFramebuffer ();
  676. }
  677. const auto width = static_cast<uint32_t> (this->getFramebufferWidth ());
  678. const auto height = static_cast<uint32_t> (this->getFramebufferHeight ());
  679. if (this->m_adjustedFBO == nullptr || this->m_adjustedFBO->getRealWidth () != width
  680. || this->m_adjustedFBO->getRealHeight () != height) {
  681. this->m_adjustedFBO = std::make_unique<CFBO> (
  682. "_rt_ImageAdjustments", TextureFormat_ARGB8888, TextureFlags_ClampUVs, 1.0f, width, height, width, height
  683. );
  684. }
  685. // the output quad puts v=0 at the top, the framebuffer keeps row 0 at the bottom
  686. const float ustart = this->m_flipHorizontal ? 1.0f : 0.0f;
  687. const float uend = 1.0f - ustart;
  688. const GLfloat texCoords[] = { ustart, 1.0f, uend, 1.0f, ustart, 0.0f, ustart, 0.0f, uend, 1.0f, uend, 0.0f };
  689. this->m_uploadedUstart = std::numeric_limits<float>::quiet_NaN ();
  690. const GLuint previousDestination = this->m_destFramebuffer;
  691. const bool outputHDR = this->m_outputHDR;
  692. this->m_destFramebuffer = this->m_adjustedFBO->getFramebuffer ();
  693. this->m_outputHDR = false;
  694. glViewport (0, 0, static_cast<GLsizei> (width), static_cast<GLsizei> (height));
  695. glBindFramebuffer (GL_FRAMEBUFFER, this->m_destFramebuffer);
  696. glBindVertexArray (this->m_vaoBuffer);
  697. glBindBuffer (GL_ARRAY_BUFFER, this->m_texCoordBuffer);
  698. glBufferSubData (GL_ARRAY_BUFFER, 0, sizeof (texCoords), texCoords);
  699. this->drawOutputQuad (1);
  700. this->m_destFramebuffer = previousDestination;
  701. this->m_outputHDR = outputHDR;
  702. return this->m_adjustedFBO->getFramebuffer ();
  703. }
  704. void CWallpaper::loadLut (const std::string& name) {
  705. if (name == this->m_lutName) {
  706. return;
  707. }
  708. this->m_lutName = name;
  709. if (this->m_lutTexture != GL_NONE) {
  710. glDeleteTextures (1, &this->m_lutTexture);
  711. this->m_lutTexture = GL_NONE;
  712. }
  713. if (name.empty ()) {
  714. return;
  715. }
  716. try {
  717. const auto stream = this->getAssetLocator ().texture ("lut/" + name);
  718. const auto texture = WallpaperEngine::Data::Parsers::TextureParser::parse (BinaryReader (stream));
  719. const auto& mipmap = texture->images.at (0).at (0);
  720. if (!(texture->flags & TextureFlags_Volume)) {
  721. sLog.error ("Image filter ", name, " is not a volume texture");
  722. return;
  723. }
  724. stbi_uc* decoded = nullptr;
  725. const void* pixels = mipmap->uncompressedData.get ();
  726. const size_t expected = static_cast<size_t> (mipmap->width) * mipmap->height * mipmap->depth * 4;
  727. // the z slices are stacked vertically in the image, the order glTexImage3D wants
  728. if (texture->freeImageFormat != FIF_UNKNOWN) {
  729. int width, height;
  730. decoded = decodeImageRGBA (mipmap->uncompressedData.get (), mipmap->uncompressedSize, width, height);
  731. if (decoded == nullptr || static_cast<size_t> (width) * height * 4 != expected) {
  732. stbi_image_free (decoded);
  733. sLog.error ("Image filter ", name, " has unexpected image data");
  734. return;
  735. }
  736. pixels = decoded;
  737. } else if (
  738. texture->format != TextureFormat_ARGB8888 || static_cast<size_t> (mipmap->uncompressedSize) < expected
  739. ) {
  740. sLog.error ("Image filter ", name, " has an unsupported format");
  741. return;
  742. }
  743. glGenTextures (1, &this->m_lutTexture);
  744. glBindTexture (GL_TEXTURE_3D, this->m_lutTexture);
  745. glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  746. glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  747. glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  748. glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  749. glTexParameteri (GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
  750. glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
  751. glTexImage3D (
  752. GL_TEXTURE_3D, 0, GL_RGBA8, static_cast<GLsizei> (mipmap->width), static_cast<GLsizei> (mipmap->height),
  753. static_cast<GLsizei> (mipmap->depth), 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels
  754. );
  755. glBindTexture (GL_TEXTURE_3D, 0);
  756. stbi_image_free (decoded);
  757. } catch (const std::exception& e) {
  758. sLog.error ("Cannot load image filter ", name, ": ", e.what ());
  759. }
  760. }
  761. std::shared_ptr<const CFBO> CWallpaper::findFBO (const std::string& name) const {
  762. const auto fbo = this->find (name);
  763. if (fbo == nullptr) {
  764. sLog.exception ("Cannot find FBO ", name);
  765. }
  766. return fbo;
  767. }
  768. std::shared_ptr<const CFBO> CWallpaper::getFBO () const { return this->m_sceneFBO; }
  769. const std::filesystem::path& CWallpaper::getSceneFile () const { return this->m_wallpaperData.project.sceneFile; }
  770. std::unique_ptr<CWallpaper> CWallpaper::fromWallpaper (
  771. const Wallpaper& wallpaper, RenderContext& context, AudioContext& audioContext,
  772. const std::filesystem::path& resolvedBackgroundPath, const WallpaperState::TextureUVsScaling& scalingMode,
  773. const uint32_t& clampMode, const glm::ivec2& maxRenderSize
  774. ) {
  775. if (wallpaper.is<Scene> ()) {
  776. return std::make_unique<WallpaperEngine::Render::Wallpapers::CScene> (
  777. wallpaper, context, audioContext, scalingMode, clampMode
  778. );
  779. }
  780. if (wallpaper.is<Video> ()) {
  781. return std::make_unique<WallpaperEngine::Render::Wallpapers::CVideo> (
  782. wallpaper, context, audioContext, scalingMode, clampMode
  783. );
  784. }
  785. // SOG depth wallpapers draw the splat data natively, the CEF viewer renders badly (see CSplat)
  786. if (wallpaper.is<Web> () && WallpaperEngine::Render::Wallpapers::CSplat::supports (wallpaper.project)) {
  787. try {
  788. return std::make_unique<WallpaperEngine::Render::Wallpapers::CSplat> (
  789. wallpaper, context, audioContext, scalingMode, clampMode
  790. );
  791. } catch (const std::exception& e) {
  792. sLog.error ("Native splat renderer failed, falling back to the web page: ", e.what ());
  793. }
  794. }
  795. if (wallpaper.is<Web> ()) {
  796. return std::make_unique<WallpaperEngine::Render::Wallpapers::CWeb> (
  797. wallpaper, context, audioContext, resolvedBackgroundPath, scalingMode, clampMode, maxRenderSize
  798. );
  799. }
  800. sLog.exception ("Unsupported wallpaper type");
  801. }