CImage.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. } // namespace WallpaperEngine::Render::Objects::Effects
  27. namespace WallpaperEngine::Render::Objects {
  28. class CImage final : public CRenderable, public ScriptableObject {
  29. friend CObject;
  30. public:
  31. CImage (Wallpapers::CScene& scene, const Image& image);
  32. ~CImage () override;
  33. void setup () override;
  34. void render () override;
  35. /** Refreshes the image's own texture plus any video a pass pulled in from a user texture slot */
  36. void updateTextures () const;
  37. /** Cursor hit test (sub_14019DBB0): the layer's quad as it is drawn, rotation and parallax included, ndc in the
  38. * scene buffer's clip space. Fullscreen layers are always hit */
  39. [[nodiscard]] bool hitTest (const glm::vec2& ndc);
  40. /** A cursor event's localPosition: (u * width, (1 - v) * height) of the unscaled layer, from its top left, where
  41. * the cursor meets the quad's plane (sub_14019DBB0), off the quad too. Zero when it doesn't meet the plane */
  42. [[nodiscard]] glm::vec2 cursorLocalPosition (const glm::vec2& ndc);
  43. void renderPassthroughChildren (const std::shared_ptr<const CFBO>& buffer);
  44. [[nodiscard]] const Image& getImage () const;
  45. [[nodiscard]] glm::vec2 getSize () const;
  46. /** Another object samples this layer's composite FBO, so its passes run even while it's hidden */
  47. void markAsDependency ();
  48. [[nodiscard]] GLuint getSceneSpacePosition () const;
  49. [[nodiscard]] GLuint getCopySpacePosition () const;
  50. [[nodiscard]] GLuint getPassSpacePosition () const;
  51. [[nodiscard]] GLuint getTexCoordCopy () const;
  52. [[nodiscard]] GLuint getTexCoordPass () const;
  53. [[nodiscard]] const float& getBrightness () const override;
  54. [[nodiscard]] const float& getUserAlpha () const override;
  55. [[nodiscard]] const float& getAlpha () const override;
  56. [[nodiscard]] const glm::vec3& getColor () const override;
  57. /** a solid layer instance whose user texture slot the user filled */
  58. [[nodiscard]] bool showsUserTextureOnSolidLayer () const;
  59. [[nodiscard]] const glm::vec4& getColor4 () const override;
  60. [[nodiscard]] const glm::vec3& getCompositeColor () const override;
  61. void pinpongFramebuffer (std::shared_ptr<const CFBO>* drawTo, std::shared_ptr<const TextureProvider>* asInput);
  62. /** A puppet attachment point's current animated position, rotation and scale, in this puppet's own
  63. * local mesh space (same space as PuppetAttachmentPoint's position/localTransform). A negative
  64. * scale component means the bone's transform includes a reflection (a mirrored bone). */
  65. struct AttachmentPointTransform {
  66. glm::vec3 position;
  67. float angle;
  68. glm::vec2 scale;
  69. };
  70. /**
  71. * @param name A named attachment point on this puppet's rig (see PuppetAttachmentPoint)
  72. * @return The point's current animated transform, or nullopt if there's no such point (or no puppet mesh)
  73. */
  74. [[nodiscard]] std::optional<AttachmentPointTransform>
  75. getAttachmentPointMeshTransform (const std::string& name) const;
  76. /** Per frame puppet update (WE's image update, sub_1401FDF90): animation, physics and the bone matrices, before
  77. * the scripts run so they read and change this frame's pose */
  78. void updatePuppetPose ();
  79. /**
  80. * Bone access for IImageLayer scripts (wallpaper64 2.8.42 sub_140211070). Matrices are column-vector glm, the
  81. * transpose of WE's row-vector ones with the same 16 floats in memory. Scene matrices are object world * bone,
  82. * local ones relative to the parent bone. Only valid while hasPuppetPose(), for bones below the bone count.
  83. */
  84. [[nodiscard]] bool hasPuppetPose () const;
  85. [[nodiscard]] const std::vector<PuppetBone>& getPuppetBones () const { return this->m_rig.bones; }
  86. [[nodiscard]] PuppetRig& getRig () { return this->m_rig; }
  87. [[nodiscard]] const PuppetRig& getRig () const { return this->m_rig; }
  88. /** WE's image world (vtable slot 16, sub_1401FD3F0): the object's world moved by the alignment offset */
  89. [[nodiscard]] glm::mat4 worldMatrix () const;
  90. [[nodiscard]] int findPuppetBone (const std::string& name) const;
  91. [[nodiscard]] const glm::mat4& getPuppetBoneTransform (int bone) const;
  92. void setPuppetBoneTransform (int bone, const glm::mat4& transform);
  93. [[nodiscard]] const glm::mat4& getPuppetLocalBoneTransform (int bone) const;
  94. void setPuppetLocalBoneTransform (int bone, const glm::mat4& transform);
  95. void applyPuppetBonePhysicsImpulse (int bone, const glm::vec3& directional, const glm::vec3& angularDegrees);
  96. void resetPuppetBonePhysics (int bone);
  97. /** The animation layer with this serial for IAnimationLayer scripts, null once destroyed or when a scene layer
  98. * plays no clip (WE never creates such a layer) */
  99. [[nodiscard]] PuppetActiveAnimation* findPuppetAnimationLayer (size_t serial);
  100. /**
  101. * IImageLayer animation layer calls (wallpaper64 2.8.42 sub_14020E910..sub_14020EF80), on the layer list in blend
  102. * order. Serials identify layers, nullopt when there is none
  103. */
  104. [[nodiscard]] size_t getPuppetAnimationLayerCount () const;
  105. [[nodiscard]] std::optional<size_t> getPuppetAnimationLayerAt (int64_t index) const;
  106. [[nodiscard]] std::optional<size_t> findPuppetAnimationLayerByName (const std::string& name) const;
  107. /** animation is a clip name (string) or a layer config (object); config's keys go over it */
  108. std::optional<size_t>
  109. createPuppetAnimationLayer (const Data::JSON::JSON& animation, const Data::JSON::JSON& config, bool autoRemove);
  110. /** removes every layer called name */
  111. bool destroyPuppetAnimationLayersByName (const std::string& name);
  112. bool destroyPuppetAnimationLayer (size_t serial);
  113. /**
  114. * ITextureAnimation (wallpaper64 2.8.42 sub_14020E670, members sub_1402131A0): a layer's own clock for its
  115. * animated texture. Attached it follows the texture's shared clock; pause, stop, setFrame and a rate other
  116. * than 1 detach it, join attaches it again. Frames are indices in the texture's frame list
  117. */
  118. struct TextureAnimation {
  119. /** +72 */
  120. bool detached = false;
  121. /** +224 */
  122. bool playing = true;
  123. /** +228 */
  124. float rate = 1.0f;
  125. /** +232/+236: the frame and the time spent in it */
  126. int frame = 0;
  127. float time = 0.0f;
  128. };
  129. /** Made on first use, only for an animated texture (texture flag 4), null otherwise */
  130. [[nodiscard]] TextureAnimation* getTextureAnimation ();
  131. /** The texture clock's frame and the time spent in it, what pausing a layer's animation starts from */
  132. [[nodiscard]] std::pair<int, float> sharedTextureFrame () const;
  133. [[nodiscard]] int getTextureFrameCount () const;
  134. [[nodiscard]] float getTextureDuration () const;
  135. /** sub_1401FDF90's tail: a detached, playing animation moves by frametime * rate, before scripts run */
  136. void updateTextureAnimation (float frametime);
  137. [[nodiscard]] std::optional<int> getTextureFrameOverride () const override;
  138. protected:
  139. void setupPasses ();
  140. void rebuildActivePasses ();
  141. /** Fullscreen layers over the scene buffer keep their buffers at the output's size */
  142. [[nodiscard]] bool followsOutputSize () const;
  143. void addEffectPasses (const ImageEffect& effect);
  144. [[nodiscard]] bool effectVisibilityChanged () const;
  145. void updateScreenSpacePosition ();
  146. [[nodiscard]] glm::mat4 ancestorTiltCorrection () const;
  147. void updateEffectTextureProjection ();
  148. void updateLightingTransform (const glm::mat4& sceneTransform);
  149. struct ResolvedTransform {
  150. glm::vec3 origin;
  151. glm::vec3 scale;
  152. float angle;
  153. };
  154. [[nodiscard]] ResolvedTransform resolveTransform (const WallpaperEngine::Data::Model::Object& object) const;
  155. /** Scene camera of this layer, the perspective layer camera when "perspective" is set */
  156. [[nodiscard]] glm::mat4 getViewProjection () const;
  157. private:
  158. /** Composite pass that applies the scene's fog, when there is fog */
  159. Effects::CPass* m_fogPass = nullptr;
  160. public:
  161. /**
  162. * Computes the object's own transform (origin/scale/angle) without walking the
  163. * parent chain. Used as the per-node step of resolveTransform.
  164. */
  165. [[nodiscard]] static ResolvedTransform localTransform (const WallpaperEngine::Data::Model::Object& object);
  166. private:
  167. bool loadPuppetMesh (const glm::vec2& size);
  168. void updatePuppetPositionBuffer (const glm::vec2& size);
  169. [[nodiscard]] glm::mat4 puppetObjectWorld () const;
  170. /** Skins the puppet vertices with the current bone matrices and re-uploads them */
  171. void updatePuppetSkinning ();
  172. void setupPuppetGeometryCallback (Effects::CPass* pass) const;
  173. /** The mask and clipping target passes of a puppet with clipping records, once the mesh pass exists */
  174. void setupPuppetClipping ();
  175. /** The mesh pass's draw split up like WE's command list (sub_140208670) */
  176. void renderPuppetClipped (Effects::CPass* meshPass);
  177. /** sub_14020D6A0: one record's mask into _rt_FullAlphaMask, or multiplied into it through the intermediate */
  178. void renderPuppetClipMask (const Effects::CPass& meshPass, int record, int draw, bool clear, bool intermediate);
  179. void selectPuppetDraw (int draw);
  180. ResolvedTransform updateGeometryBuffers ();
  181. [[nodiscard]] glm::vec2 resolveGeometrySize (float sceneWidth, float sceneHeight, glm::vec3& origin) const;
  182. void updateScenePosition (
  183. const glm::vec3& origin, const glm::vec2& size, const glm::vec3& scale, float sceneWidth, float sceneHeight
  184. );
  185. void uploadGeometryBuffers (const glm::vec2& size);
  186. [[nodiscard]] bool shouldRenderFinalPass (bool isLastPass) const;
  187. bool configurePassTarget (
  188. Effects::CPass* pass, std::shared_ptr<const CFBO>& drawTo,
  189. const std::shared_ptr<const TextureProvider>& asInput, std::shared_ptr<const TextureProvider>& effectInput,
  190. bool& inTargetEffectSequence
  191. );
  192. GLuint m_sceneSpacePosition;
  193. GLuint m_copySpacePosition;
  194. GLuint m_passSpacePosition;
  195. GLuint m_texcoordCopy;
  196. GLuint m_texcoordPass;
  197. GLuint m_puppetSpacePosition = GL_NONE;
  198. GLuint m_puppetTexCoord = GL_NONE;
  199. GLuint m_puppetIndices = GL_NONE;
  200. GLsizei m_puppetIndexCount = 0;
  201. bool m_hasPuppetMesh = false;
  202. // the pass that draws the warped mesh, and whether it is the last one (straight into the scene FBO)
  203. Effects::CPass* m_puppetMeshPass = nullptr;
  204. bool m_puppetMeshLast = false;
  205. /** what the puppet geometry callback draws: the whole mesh, or one of the clipping draws */
  206. GLuint m_puppetDrawBuffer = GL_NONE;
  207. GLsizei m_puppetDrawOffset = 0;
  208. GLsizei m_puppetDrawCount = 0;
  209. /** the mesh pass draws again into what it already drew this frame */
  210. bool m_puppetDrawKeep = false;
  211. std::optional<PuppetClipping> m_puppetClipping = std::nullopt;
  212. GLuint m_puppetClipIndices = GL_NONE;
  213. /** clippingmaskimage4 per record, each with its mask texture */
  214. std::vector<Effects::CPass*> m_puppetClipMaskPasses = {};
  215. /** the mesh pass's material with CLIPPINGUVS and CLIPPINGTARGET */
  216. Effects::CPass* m_puppetClipTargetPass = nullptr;
  217. /** flattexture of the intermediate mask multiplied into _rt_FullAlphaMask, for nested masks */
  218. Effects::CPass* m_puppetClipComposePass = nullptr;
  219. GLuint m_puppetClipComposePosition = GL_NONE;
  220. GLuint m_puppetClipComposeTexCoord = GL_NONE;
  221. glm::vec4 m_puppetClipRenderVar0 { 0.0f };
  222. glm::vec4 m_puppetClipClearColor { 0.0f };
  223. glm::mat4 m_puppetClipIdentity { 1.0f };
  224. float m_puppetClipComposeAlpha = 1.0f;
  225. mutable bool m_puppetDrawDiagnosticLogged = false;
  226. mutable bool m_puppetDrawErrorChecked = false;
  227. bool m_puppetPositionDiagnosticLogged = false;
  228. mutable std::set<int> m_attachmentDiagnosticLogged = {};
  229. std::vector<GLfloat> m_puppetRawPositions = {};
  230. /** This object's current resolved scale, mirrored here so updatePuppetSkinning() (called after
  231. * updateGeometryBuffers() each frame, see render()) can fold it into puppet vertex positions
  232. * without needing resolveTransform() run twice */
  233. glm::vec3 m_puppetScale { 1.0f };
  234. std::vector<glm::uvec4> m_puppetBlendIndices = {};
  235. std::vector<glm::vec4> m_puppetBlendWeights = {};
  236. PuppetRig m_rig;
  237. std::vector<GLfloat> m_puppetSkinnedPositions = {};
  238. glm::mat4 m_modelViewProjectionScreen = {};
  239. glm::mat4 m_modelViewProjectionPass = {};
  240. /** effect passes: their buffer's geometry mapped to where the layer is on screen */
  241. glm::mat4 m_effectModelViewProjectionCopy { 1.0f };
  242. /** WE's object world matrix (y up, scene pixels), shaders read g_LayerModelMatrix for the layer's scale */
  243. glm::mat4 m_layerModelMatrix { 1.0f };
  244. glm::mat4 m_effectModelViewProjectionPass { 1.0f };
  245. glm::mat4 m_modelViewProjectionCopy = {};
  246. glm::mat4 m_modelViewProjectionScreenInverse = {};
  247. glm::mat4 m_modelViewProjectionPassInverse = {};
  248. glm::mat4 m_modelViewProjectionCopyInverse = {};
  249. /** Maps the layer quad's own -1..1 space (+y = texture top) to screen clip space, effects like xray and
  250. * cursorripple use its inverse to bring g_PointerPosition into the layer's texture space */
  251. glm::mat4 m_effectTextureProjection = glm::mat4 (1.0);
  252. glm::mat4 m_effectTextureProjectionInverse = glm::mat4 (1.0);
  253. glm::mat4 m_objectSpaceProjectionInverse = glm::mat4 (1.0);
  254. glm::mat4 m_modelMatrix = {};
  255. glm::mat4 m_viewProjectionMatrix = {};
  256. std::shared_ptr<const CFBO> m_mainFBO = nullptr;
  257. std::shared_ptr<const CFBO> m_subFBO = nullptr;
  258. std::shared_ptr<const CFBO> m_currentMainFBO = nullptr;
  259. std::shared_ptr<const CFBO> m_currentSubFBO = nullptr;
  260. const Image& m_image;
  261. mutable glm::vec3 m_colorCache {};
  262. mutable glm::vec4 m_color4Cache {};
  263. mutable float m_alphaCache = 1.0f;
  264. std::vector<Effects::CPass*> m_passes = {};
  265. std::vector<Effects::CPass*> m_allPasses = {};
  266. struct PassState {
  267. /** nullptr for passes that always render */
  268. const DynamicValue* visible;
  269. BlendingMode blending;
  270. bool fromEffect;
  271. };
  272. std::vector<PassState> m_allPassStates = {};
  273. std::vector<bool> m_activePassMask = {};
  274. bool m_hasActiveEffectPass = false;
  275. bool m_passesDrawToScreen = false;
  276. std::vector<MaterialPassUniquePtr> m_virtualPassess = {};
  277. glm::vec4 m_pos = {};
  278. /** The quad's size before the layer's scale, what m_pos spans */
  279. glm::vec2 m_displaySize = {};
  280. /** The object's origin in m_pos's space, what the rotation turns around: WE puts the alignment offset inside
  281. * the rotated and scaled frame (sub_1401FD3F0), not around it */
  282. glm::vec3 m_scenePivot = {};
  283. std::optional<TextureAnimation> m_textureAnimation = std::nullopt;
  284. float m_textureAnimationClock = -1.0f;
  285. /** Lit passes: scene/copy space vertices -> WE world (y up, bottom left origin), see CPass::setLightingTransform */
  286. glm::mat4 m_lightingSceneModel { 1.0f };
  287. glm::mat4 m_lightingCopyModel { 1.0f };
  288. glm::mat3 m_lightingNormal { 1.0f };
  289. glm::mat4 m_lightingViewProjection { 1.0f };
  290. glm::vec2 m_size = {};
  291. // last m_pos/size uploaded to the GL geometry buffers; NaN forces the first upload
  292. glm::vec4 m_lastUploadedPos = glm::vec4 (std::numeric_limits<float>::quiet_NaN ());
  293. glm::vec2 m_lastUploadedGeometrySize = glm::vec2 (std::numeric_limits<float>::quiet_NaN ());
  294. bool m_initialized = false;
  295. bool m_isDependency = false;
  296. bool m_readByOtherLayer = false;
  297. /** A passthrough layer with objects under it, it draws them into its buffer after the base pass */
  298. bool m_hasPassthroughChildren = false;
  299. struct {
  300. struct {
  301. MaterialUniquePtr material;
  302. ImageEffectPassOverrideUniquePtr override;
  303. } colorBlending;
  304. std::vector<MaterialUniquePtr> compatibilityMaterials = {};
  305. std::vector<ImageEffectPassOverrideUniquePtr> compatibilityOverrides = {};
  306. MaterialUniquePtr clippingMask;
  307. MaterialUniquePtr clippingCompose;
  308. std::vector<ImageEffectPassOverrideUniquePtr> clippingOverrides = {};
  309. } m_materials;
  310. };
  311. } // namespace WallpaperEngine::Render::Objects