CImage.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #pragma once
  2. #include "CRenderable.h"
  3. #include "WallpaperEngine/Render/CObject.h"
  4. #include "WallpaperEngine/Render/Objects/Effects/CPass.h"
  5. #include "WallpaperEngine/Render/Wallpapers/CScene.h"
  6. #include "WallpaperEngine/Data/JSON.h"
  7. #include "WallpaperEngine/Render/Shaders/Shader.h"
  8. #include "../TextureProvider.h"
  9. #include "PuppetClipping.h"
  10. #include "PuppetRig.h"
  11. #include "WallpaperEngine/Scripting/ScriptableObject.h"
  12. #include <glm/gtc/quaternion.hpp>
  13. #include <glm/mat4x4.hpp>
  14. #include <glm/vec3.hpp>
  15. #include <glm/vec4.hpp>
  16. #include <limits>
  17. #include <optional>
  18. #include <set>
  19. #include <vector>
  20. using namespace WallpaperEngine;
  21. using namespace WallpaperEngine::Render;
  22. using namespace WallpaperEngine::Scripting;
  23. namespace WallpaperEngine::Render::Objects::Effects {
  24. class CMaterial;
  25. class CPass;
  26. }
  27. namespace WallpaperEngine::Render::Objects {
  28. /** File offsets of the first flag 2 mesh */
  29. struct PuppetBlendMesh {
  30. std::string material;
  31. uint32_t rows = 0;
  32. uint32_t format = 0;
  33. size_t vertices = 0;
  34. uint32_t vertexBytes = 0;
  35. size_t indices = 0;
  36. uint32_t indexBytes = 0;
  37. };
  38. /** Morph texture of the first mesh (sub_1401FBAE0) */
  39. struct PuppetMorphTargets {
  40. float scale = 0.0f;
  41. uint32_t vertexCount = 0;
  42. std::vector<glm::vec4> texels;
  43. bool alpha = false;
  44. /** mesh flag 0x2000 (MORPHING_MODIFIERS): bone, 1 for an axis rule, smoothstep edges */
  45. struct BoneRule {
  46. uint32_t bone = 0;
  47. float axis = 0.0f;
  48. float edge0 = 0.0f;
  49. float edge1 = 0.0f;
  50. };
  51. std::vector<BoneRule> boneRules;
  52. std::vector<std::string> names;
  53. };
  54. class CImage final : public CRenderable, public ScriptableObject {
  55. friend CObject;
  56. public:
  57. CImage (Wallpapers::CScene& scene, const Image& image);
  58. ~CImage () override;
  59. void setup () override;
  60. void render () override;
  61. void updateTextures () const;
  62. /** Cursor hit test (sub_14019DBB0): the layer's quad as it is drawn, rotation and parallax included, ndc in the
  63. * scene buffer's clip space. Fullscreen layers are always hit */
  64. [[nodiscard]] bool hitTest (const glm::vec2& ndc);
  65. /** A cursor event's localPosition: (u * width, (1 - v) * height) of the unscaled layer, from its top left, where
  66. * the cursor meets the quad's plane (sub_14019DBB0), off the quad too. Zero when it doesn't meet the plane */
  67. [[nodiscard]] glm::vec2 cursorLocalPosition (const glm::vec2& ndc);
  68. /** hitBox: puppet bone box under the cursor (sub_1401FD690) */
  69. [[nodiscard]] std::optional<std::string> cursorHitBox (const glm::vec2& ndc) const;
  70. void renderPassthroughChildren (const std::shared_ptr<const CFBO>& buffer);
  71. /** sub_1401EA500: only layers with renderable flag 0x10 get a named _a buffer, the rest and every _b are pooled */
  72. [[nodiscard]] bool usesPooledBuffer () const;
  73. /** renderable +800 (sub_1401E7170): 1 per visible effect (0 for shapes) plus its compose passes */
  74. [[nodiscard]] int effectBufferPasses () const;
  75. /** sub_1401EA500's buffer name for index, empty when WE has no such buffer */
  76. [[nodiscard]] std::string layerBufferName (int index) const;
  77. [[nodiscard]] const Image& getImage () const;
  78. [[nodiscard]] glm::vec2 getSize () const;
  79. /** Another object samples this layer's composite FBO, so its passes run even while it's hidden */
  80. void markAsDependency ();
  81. [[nodiscard]] bool copiesForReaders () const;
  82. [[nodiscard]] bool drawsPassthroughChildren () const { return this->m_hasPassthroughChildren; }
  83. [[nodiscard]] GLuint getSceneSpacePosition () const;
  84. [[nodiscard]] GLuint getCopySpacePosition () const;
  85. [[nodiscard]] GLuint getPassSpacePosition () const;
  86. [[nodiscard]] GLuint getTexCoordCopy () const;
  87. [[nodiscard]] GLuint getTexCoordPass () const;
  88. /** copy is x, y, right, bottom */
  89. void uploadSceneTexCoords (const glm::vec4& copy);
  90. [[nodiscard]] const float& getBrightness () const override;
  91. [[nodiscard]] const float& getUserAlpha () const override;
  92. [[nodiscard]] const float& getAlpha () const override;
  93. [[nodiscard]] const glm::vec3& getColor () const override;
  94. [[nodiscard]] bool showsUserTextureOnSolidLayer () const;
  95. [[nodiscard]] const glm::vec4& getColor4 () const override;
  96. [[nodiscard]] const glm::vec3& getCompositeColor () const override;
  97. void pinpongFramebuffer (std::shared_ptr<const CFBO>* drawTo, std::shared_ptr<const TextureProvider>* asInput);
  98. /** Bone matrix times the attachment point's matrix in puppet space (sub_1401FD5C0) */
  99. [[nodiscard]] std::optional<glm::mat4> getAttachmentMatrix (const std::string& name) const;
  100. /** Per frame puppet update (WE's image update, sub_1401FDF90): animation, physics and the bone matrices, before
  101. * the scripts run so they read and change this frame's pose */
  102. void updatePuppetPose ();
  103. // IImageLayer bone access (2.8.42 sub_140211070): glm column-vector matrices, same memory as WE's; scene = object
  104. // world * bone, local = relative to the parent bone. Valid while hasPuppetPose()
  105. [[nodiscard]] bool hasPuppetPose () const;
  106. /** sub_140210400: first target of that name, -1 for none */
  107. [[nodiscard]] int getBlendShapeIndex (const std::string& name) const;
  108. /** sub_1402104B0: 0 out of range */
  109. [[nodiscard]] float getBlendShapeWeight (int target) const;
  110. /** sub_1402105C0: holds until the next update resets the weights (sub_14021C480) */
  111. void setBlendShapeWeight (int target, float weight);
  112. [[nodiscard]] const std::vector<PuppetBone>& getPuppetBones () const { return this->m_rig.bones; }
  113. [[nodiscard]] PuppetRig& getRig () { return this->m_rig; }
  114. [[nodiscard]] const PuppetRig& getRig () const { return this->m_rig; }
  115. /** WE's image world (vtable slot 16, sub_1401FD3F0): the object's world moved by the alignment offset */
  116. [[nodiscard]] glm::mat4 worldMatrix () const;
  117. [[nodiscard]] int findPuppetBone (const std::string& name) const;
  118. [[nodiscard]] const glm::mat4& getPuppetBoneTransform (int bone) const;
  119. void setPuppetBoneTransform (int bone, const glm::mat4& transform);
  120. [[nodiscard]] const glm::mat4& getPuppetLocalBoneTransform (int bone) const;
  121. void setPuppetLocalBoneTransform (int bone, const glm::mat4& transform);
  122. void applyPuppetBonePhysicsImpulse (int bone, const glm::vec3& directional, const glm::vec3& angularDegrees);
  123. void resetPuppetBonePhysics (int bone);
  124. /** The animation layer with this serial for IAnimationLayer scripts, null once destroyed or when a scene layer
  125. * plays no clip (WE never creates such a layer) */
  126. [[nodiscard]] PuppetActiveAnimation* findPuppetAnimationLayer (size_t serial);
  127. /**
  128. * IImageLayer animation layer calls (wallpaper64 2.8.42 sub_14020E910..sub_14020EF80), on the layer list in blend
  129. * order. Serials identify layers, nullopt when there is none
  130. */
  131. [[nodiscard]] size_t getPuppetAnimationLayerCount () const;
  132. [[nodiscard]] std::optional<size_t> getPuppetAnimationLayerAt (int64_t index) const;
  133. [[nodiscard]] std::optional<size_t> findPuppetAnimationLayerByName (const std::string& name) const;
  134. std::optional<size_t>
  135. createPuppetAnimationLayer (const Data::JSON::JSON& animation, const Data::JSON::JSON& config, bool autoRemove);
  136. bool destroyPuppetAnimationLayersByName (const std::string& name);
  137. bool destroyPuppetAnimationLayer (size_t serial);
  138. // ITextureAnimation (2.8.42 sub_14020E670): a layer's own clock for its animated texture; pause, stop, setFrame and
  139. // rate != 1 detach it from the shared clock, join reattaches
  140. struct TextureAnimation {
  141. /** +72 */
  142. bool detached = false;
  143. /** +224 */
  144. bool playing = true;
  145. /** +228 */
  146. float rate = 1.0f;
  147. /** +232/+236: the frame and the time spent in it */
  148. int frame = 0;
  149. float time = 0.0f;
  150. };
  151. /** Made on first use, only for an animated texture (texture flag 4), null otherwise */
  152. [[nodiscard]] TextureAnimation* getTextureAnimation ();
  153. [[nodiscard]] std::pair<int, float> sharedTextureFrame () const;
  154. [[nodiscard]] int getTextureFrameCount () const;
  155. [[nodiscard]] float getTextureDuration () const;
  156. /** sub_1401FDF90's tail: a detached, playing animation moves by frametime * rate, before scripts run */
  157. void updateTextureAnimation (float frametime);
  158. [[nodiscard]] std::optional<int> getTextureFrameOverride () const override;
  159. protected:
  160. void setupPasses ();
  161. void rebuildActivePasses ();
  162. /** Fullscreen layers over the scene buffer keep their buffers at the output's size */
  163. [[nodiscard]] bool followsOutputSize () const;
  164. void addEffectPasses (const ImageEffect& effect);
  165. [[nodiscard]] bool effectVisibilityChanged () const;
  166. void updateScreenSpacePosition ();
  167. void updateEffectTextureProjection ();
  168. void updateLightingTransform (
  169. const glm::mat4& sceneTransform, const std::optional<std::pair<glm::mat4, glm::mat4>>& objectTransform,
  170. const glm::mat4& screen
  171. );
  172. [[nodiscard]] glm::mat4 getViewProjection () const;
  173. private:
  174. Effects::CPass* m_fogPass = nullptr;
  175. private:
  176. bool loadPuppetMesh (const glm::vec2& size);
  177. void updatePuppetPositionBuffer (const glm::vec2& size);
  178. [[nodiscard]] glm::mat4 puppetObjectWorld () const;
  179. void updatePuppetSkinning ();
  180. [[nodiscard]] ComboMap puppetVertexAlphaCombos () const;
  181. void updatePuppetVertexAlpha (const std::vector<float>& morphAlpha);
  182. void setupPuppetGeometryCallback (Effects::CPass* pass) const;
  183. [[nodiscard]] bool litDrawsDirect () const;
  184. void setupDirectLitPass ();
  185. void setupPuppetPrelight ();
  186. void uploadPuppetPrelightBuffers ();
  187. void setupPuppetClipping ();
  188. /** sub_1401FDF90 end: sorts the parts by order + their bone's animated draw order, rebuilds the indices */
  189. void updatePuppetDrawOrder ();
  190. /** The mesh pass's draw split up like WE's command list (sub_140208670) */
  191. void renderPuppetClipped (Effects::CPass* meshPass);
  192. /** sub_14020D6A0: one record's mask into _rt_FullAlphaMask, or multiplied into it through the intermediate */
  193. void renderPuppetClipMask (const Effects::CPass& meshPass, int record, int draw, bool clear, bool intermediate);
  194. void selectPuppetDraw (int draw);
  195. void loadPuppetBlendMesh (const std::vector<char>& data);
  196. /** sub_140209540 albedo buffer */
  197. void setupPuppetBlendMap (bool offscreen);
  198. [[nodiscard]] bool hasLitMaterial () const;
  199. void updateGeometryBuffers ();
  200. [[nodiscard]] glm::vec2 resolveGeometrySize () const;
  201. void fillMissingSize (glm::vec2& size) const;
  202. [[nodiscard]] glm::vec2 textureUvScale () const;
  203. void updateScenePosition (const glm::vec2& size);
  204. void uploadGeometryBuffers (const glm::vec2& size);
  205. void uploadShapePointMesh (const glm::vec2& size);
  206. [[nodiscard]] bool shouldRenderFinalPass (bool isLastPass) const;
  207. bool configurePassTarget (
  208. Effects::CPass* pass, std::shared_ptr<const CFBO>& drawTo,
  209. const std::shared_ptr<const TextureProvider>& asInput, std::shared_ptr<const TextureProvider>& effectInput,
  210. bool& inTargetEffectSequence
  211. );
  212. GLuint m_sceneSpacePosition;
  213. GLuint m_copySpacePosition;
  214. GLuint m_passSpacePosition;
  215. GLuint m_texcoordCopy;
  216. GLuint m_texcoordPass;
  217. GLuint m_texcoordDirect = GL_NONE;
  218. GLuint m_texcoordFinal = GL_NONE;
  219. GLuint m_puppetSpacePosition = GL_NONE;
  220. GLuint m_puppetTexCoord = GL_NONE;
  221. GLuint m_puppetIndices = GL_NONE;
  222. GLsizei m_puppetIndexCount = 0;
  223. bool m_hasPuppetMesh = false;
  224. Effects::CPass* m_puppetMeshPass = nullptr;
  225. bool m_puppetMeshLast = false;
  226. GLuint m_puppetDrawBuffer = GL_NONE;
  227. GLsizei m_puppetDrawOffset = 0;
  228. GLsizei m_puppetDrawCount = 0;
  229. /** the mesh pass draws again into what it already drew this frame */
  230. bool m_puppetDrawKeep = false;
  231. std::optional<PuppetClipping> m_puppetClipping = std::nullopt;
  232. GLuint m_puppetClipIndices = GL_NONE;
  233. /** mesh flag 8: the part ranges, the order the animated draw order put them in and the file's indices */
  234. std::vector<PuppetClipping::Part> m_puppetParts = {};
  235. std::vector<uint32_t> m_puppetPartOrder = {};
  236. std::vector<uint16_t> m_puppetMeshIndices = {};
  237. std::vector<Effects::CPass*> m_puppetClipMaskPasses = {};
  238. Effects::CPass* m_puppetClipTargetPass = nullptr;
  239. Effects::CPass* m_puppetClipComposePass = nullptr;
  240. GLuint m_puppetClipComposePosition = GL_NONE;
  241. GLuint m_puppetClipComposeTexCoord = GL_NONE;
  242. glm::vec4 m_puppetClipRenderVar0 { 0.0f };
  243. glm::vec4 m_puppetClipClearColor { 0.0f };
  244. glm::mat4 m_puppetClipIdentity { 1.0f };
  245. float m_puppetClipComposeAlpha = 1.0f;
  246. /** mesh flag 2 blend map */
  247. struct {
  248. GLuint vertices = GL_NONE;
  249. GLuint indices = GL_NONE;
  250. GLsizei indexCount = 0;
  251. uint32_t format = 0;
  252. uint32_t stride = 0;
  253. uint32_t rows = 0;
  254. std::string material;
  255. std::shared_ptr<CFBO> albedo = nullptr;
  256. GLuint quad = GL_NONE;
  257. glm::mat4 projection { 1.0f };
  258. Effects::CPass* copyPass = nullptr;
  259. Effects::CPass* pass = nullptr;
  260. } m_blendMap;
  261. /** WE's prelighting draw (sub_140209540) for lit puppets whose mesh a later pass draws */
  262. struct {
  263. Effects::CPass* pass = nullptr;
  264. GLuint bindPositions = GL_NONE;
  265. GLuint morphedPositions = GL_NONE;
  266. GLuint flatPositions = GL_NONE;
  267. GLuint normals = GL_NONE;
  268. GLuint tangents = GL_NONE;
  269. GLuint blendIndices = GL_NONE;
  270. GLuint blendWeights = GL_NONE;
  271. std::vector<GLfloat> auxPositions = {};
  272. std::vector<GLfloat> normalData = {};
  273. std::vector<GLfloat> tangentData = {};
  274. std::vector<GLfloat> morphedData = {};
  275. std::vector<GLfloat> bones = {};
  276. glm::mat4 model { 1.0f };
  277. glm::mat3 normal { 1.0f };
  278. glm::mat4 projection { 1.0f };
  279. } m_puppetPrelight;
  280. /** WE's model material draw for lit layers that need no buffer (sub_140209540), nullptr otherwise */
  281. Effects::CPass* m_directLitPass = nullptr;
  282. mutable bool m_puppetDrawDiagnosticLogged = false;
  283. mutable bool m_puppetDrawErrorChecked = false;
  284. bool m_puppetPositionDiagnosticLogged = false;
  285. std::vector<GLfloat> m_puppetRawPositions = {};
  286. std::vector<glm::uvec4> m_puppetBlendIndices = {};
  287. std::vector<glm::vec4> m_puppetBlendWeights = {};
  288. PuppetRig m_rig;
  289. std::vector<GLfloat> m_puppetSkinnedPositions = {};
  290. uint32_t m_puppetMeshFlags = 0;
  291. uint32_t m_puppetMeshFormat = 0;
  292. std::optional<PuppetMorphTargets> m_puppetMorph = std::nullopt;
  293. bool m_puppetMorphShader = false;
  294. std::vector<std::optional<uint32_t>> m_puppetMorphIndices = {};
  295. bool m_puppetVertexAlpha = false;
  296. GLuint m_puppetVertexAlphaWeights = GL_NONE;
  297. std::vector<GLfloat> m_puppetVertexAlphaData = {};
  298. glm::mat4 m_modelViewProjectionScreen = {};
  299. glm::mat4 m_modelViewProjectionPass = {};
  300. /** effect passes: their buffer's geometry mapped to where the layer is on screen */
  301. glm::mat4 m_effectModelViewProjectionCopy { 1.0f };
  302. /** WE's object world matrix (y up, scene pixels), shaders read g_LayerModelMatrix for the layer's scale */
  303. glm::mat4 m_layerModelMatrix { 1.0f };
  304. glm::mat4 m_effectModelViewProjectionPass { 1.0f };
  305. glm::mat4 m_modelViewProjectionCopy = {};
  306. glm::mat4 m_modelViewProjectionScreenInverse = {};
  307. glm::mat4 m_modelViewProjectionPassInverse = {};
  308. glm::mat4 m_modelViewProjectionCopyInverse = {};
  309. /** Maps the layer quad's own -1..1 space (+y = texture top) to screen clip space, effects like xray and
  310. * cursorripple use its inverse to bring g_PointerPosition into the layer's texture space */
  311. glm::mat4 m_effectTextureProjection = glm::mat4 (1.0);
  312. glm::mat4 m_effectTextureProjectionInverse = glm::mat4 (1.0);
  313. glm::mat4 m_objectSpaceProjectionInverse = glm::mat4 (1.0);
  314. glm::mat4 m_modelMatrix = {};
  315. glm::mat4 m_viewProjectionMatrix = {};
  316. std::shared_ptr<const CFBO> m_mainFBO = nullptr;
  317. std::shared_ptr<const CFBO> m_subFBO = nullptr;
  318. std::shared_ptr<const CFBO> m_namedMainFBO = nullptr;
  319. std::shared_ptr<const CFBO> m_namedSubFBO = nullptr;
  320. /** pooled buffers of the same name are shared between layers */
  321. struct LayerBufferConfig {
  322. glm::vec2 size {};
  323. TextureFormat format = TextureFormat_ARGB8888;
  324. uint32_t flags = 0;
  325. } m_layerBufferConfig;
  326. /** like sub_1401EA500, redone on every pass rebuild */
  327. void assignLayerBuffers ();
  328. std::shared_ptr<const CFBO> m_currentMainFBO = nullptr;
  329. std::shared_ptr<const CFBO> m_currentSubFBO = nullptr;
  330. const Image& m_image;
  331. mutable glm::vec3 m_colorCache {};
  332. mutable glm::vec4 m_color4Cache {};
  333. mutable float m_alphaCache = 1.0f;
  334. std::vector<Effects::CPass*> m_passes = {};
  335. std::vector<Effects::CPass*> m_allPasses = {};
  336. struct PassState {
  337. const DynamicValue* visible;
  338. BlendingMode blending;
  339. bool fromEffect;
  340. };
  341. std::vector<PassState> m_allPassStates = {};
  342. struct SetupInputs {
  343. std::vector<bool> puppetEffects;
  344. int colorBlendMode = 0;
  345. bool copyForReaders = false;
  346. bool operator== (const SetupInputs&) const = default;
  347. };
  348. [[nodiscard]] SetupInputs currentSetupInputs () const;
  349. void releasePasses ();
  350. SetupInputs m_setupInputs = {};
  351. std::vector<bool> m_activePassMask = {};
  352. bool m_hasActiveEffectPass = false;
  353. bool m_passesDrawToScreen = false;
  354. std::vector<MaterialPassUniquePtr> m_virtualPassess = {};
  355. glm::vec4 m_pos = {};
  356. /** The quad's size before the layer's scale, what m_pos spans */
  357. glm::vec2 m_displaySize = {};
  358. std::optional<TextureAnimation> m_textureAnimation = std::nullopt;
  359. float m_textureAnimationClock = -1.0f;
  360. /** Lit passes: scene/copy space vertices -> WE world (y up, bottom left origin), see CPass::setLightingTransform */
  361. glm::mat4 m_lightingSceneModel { 1.0f };
  362. glm::mat4 m_lightingCopyModel { 1.0f };
  363. glm::mat3 m_lightingNormal { 1.0f };
  364. glm::mat4 m_lightingViewProjection { 1.0f };
  365. glm::vec2 m_size = {};
  366. // last m_pos/size uploaded to the GL geometry buffers; NaN forces the first upload
  367. glm::vec4 m_lastUploadedPos = glm::vec4 (std::numeric_limits<float>::quiet_NaN ());
  368. glm::vec2 m_lastUploadedGeometrySize = glm::vec2 (std::numeric_limits<float>::quiet_NaN ());
  369. bool m_initialized = false;
  370. bool m_isDependency = false;
  371. bool m_readByOtherLayer = false;
  372. bool m_emitterImageSource = false;
  373. bool m_hasPassthroughChildren = false;
  374. struct {
  375. struct {
  376. MaterialUniquePtr material;
  377. ImageEffectPassOverrideUniquePtr override;
  378. } colorBlending;
  379. Effects::CPass* colorBlendingPass = nullptr;
  380. std::vector<MaterialUniquePtr> compatibilityMaterials = {};
  381. std::vector<ImageEffectPassOverrideUniquePtr> compatibilityOverrides = {};
  382. MaterialUniquePtr clippingMask;
  383. MaterialUniquePtr clippingCompose;
  384. std::vector<ImageEffectPassOverrideUniquePtr> clippingOverrides = {};
  385. ImageEffectPassOverrideUniquePtr puppetVertexAlpha;
  386. ImageEffectPassOverrideUniquePtr puppetPrelight;
  387. ImageEffectPassOverrideUniquePtr directLit;
  388. MaterialUniquePtr blendMap;
  389. ImageEffectPassOverrideUniquePtr blendMapOverride;
  390. } m_materials;
  391. };
  392. }