#pragma once #include "CRenderable.h" #include "WallpaperEngine/Render/CObject.h" #include "WallpaperEngine/Render/Objects/Effects/CPass.h" #include "WallpaperEngine/Render/Wallpapers/CScene.h" #include "WallpaperEngine/Data/JSON.h" #include "WallpaperEngine/Render/Shaders/Shader.h" #include "../TextureProvider.h" #include "PuppetClipping.h" #include "PuppetRig.h" #include "WallpaperEngine/Scripting/ScriptableObject.h" #include #include #include #include #include #include #include #include using namespace WallpaperEngine; using namespace WallpaperEngine::Render; using namespace WallpaperEngine::Scripting; namespace WallpaperEngine::Render::Objects::Effects { class CMaterial; class CPass; } namespace WallpaperEngine::Render::Objects { /** File offsets of the first flag 2 mesh */ struct PuppetBlendMesh { std::string material; uint32_t rows = 0; uint32_t format = 0; size_t vertices = 0; uint32_t vertexBytes = 0; size_t indices = 0; uint32_t indexBytes = 0; }; /** Morph texture of the first mesh (sub_1401FBAE0) */ struct PuppetMorphTargets { float scale = 0.0f; uint32_t vertexCount = 0; std::vector texels; bool alpha = false; /** mesh flag 0x2000 (MORPHING_MODIFIERS): bone, 1 for an axis rule, smoothstep edges */ struct BoneRule { uint32_t bone = 0; float axis = 0.0f; float edge0 = 0.0f; float edge1 = 0.0f; }; std::vector boneRules; std::vector names; }; class CImage final : public CRenderable, public ScriptableObject { friend CObject; public: CImage (Wallpapers::CScene& scene, const Image& image); ~CImage () override; void setup () override; void render () override; void updateTextures () const; /** Cursor hit test (sub_14019DBB0): the layer's quad as it is drawn, rotation and parallax included, ndc in the * scene buffer's clip space. Fullscreen layers are always hit */ [[nodiscard]] bool hitTest (const glm::vec2& ndc); /** A cursor event's localPosition: (u * width, (1 - v) * height) of the unscaled layer, from its top left, where * the cursor meets the quad's plane (sub_14019DBB0), off the quad too. Zero when it doesn't meet the plane */ [[nodiscard]] glm::vec2 cursorLocalPosition (const glm::vec2& ndc); /** hitBox: puppet bone box under the cursor (sub_1401FD690) */ [[nodiscard]] std::optional cursorHitBox (const glm::vec2& ndc) const; void renderPassthroughChildren (const std::shared_ptr& buffer); /** sub_1401EA500: only layers with renderable flag 0x10 get a named _a buffer, the rest and every _b are pooled */ [[nodiscard]] bool usesPooledBuffer () const; /** renderable +800 (sub_1401E7170): 1 per visible effect (0 for shapes) plus its compose passes */ [[nodiscard]] int effectBufferPasses () const; /** sub_1401EA500's buffer name for index, empty when WE has no such buffer */ [[nodiscard]] std::string layerBufferName (int index) const; [[nodiscard]] const Image& getImage () const; [[nodiscard]] glm::vec2 getSize () const; /** Another object samples this layer's composite FBO, so its passes run even while it's hidden */ void markAsDependency (); [[nodiscard]] bool copiesForReaders () const; [[nodiscard]] bool drawsPassthroughChildren () const { return this->m_hasPassthroughChildren; } [[nodiscard]] GLuint getSceneSpacePosition () const; [[nodiscard]] GLuint getCopySpacePosition () const; [[nodiscard]] GLuint getPassSpacePosition () const; [[nodiscard]] GLuint getTexCoordCopy () const; [[nodiscard]] GLuint getTexCoordPass () const; /** copy is x, y, right, bottom */ void uploadSceneTexCoords (const glm::vec4& copy); [[nodiscard]] const float& getBrightness () const override; [[nodiscard]] const float& getUserAlpha () const override; [[nodiscard]] const float& getAlpha () const override; [[nodiscard]] const glm::vec3& getColor () const override; [[nodiscard]] bool showsUserTextureOnSolidLayer () const; [[nodiscard]] const glm::vec4& getColor4 () const override; [[nodiscard]] const glm::vec3& getCompositeColor () const override; void pinpongFramebuffer (std::shared_ptr* drawTo, std::shared_ptr* asInput); /** Bone matrix times the attachment point's matrix in puppet space (sub_1401FD5C0) */ [[nodiscard]] std::optional getAttachmentMatrix (const std::string& name) const; /** Per frame puppet update (WE's image update, sub_1401FDF90): animation, physics and the bone matrices, before * the scripts run so they read and change this frame's pose */ void updatePuppetPose (); // IImageLayer bone access (2.8.42 sub_140211070): glm column-vector matrices, same memory as WE's; scene = object // world * bone, local = relative to the parent bone. Valid while hasPuppetPose() [[nodiscard]] bool hasPuppetPose () const; /** sub_140210400: first target of that name, -1 for none */ [[nodiscard]] int getBlendShapeIndex (const std::string& name) const; /** sub_1402104B0: 0 out of range */ [[nodiscard]] float getBlendShapeWeight (int target) const; /** sub_1402105C0: holds until the next update resets the weights (sub_14021C480) */ void setBlendShapeWeight (int target, float weight); [[nodiscard]] const std::vector& getPuppetBones () const { return this->m_rig.bones; } [[nodiscard]] PuppetRig& getRig () { return this->m_rig; } [[nodiscard]] const PuppetRig& getRig () const { return this->m_rig; } /** WE's image world (vtable slot 16, sub_1401FD3F0): the object's world moved by the alignment offset */ [[nodiscard]] glm::mat4 worldMatrix () const; [[nodiscard]] int findPuppetBone (const std::string& name) const; [[nodiscard]] const glm::mat4& getPuppetBoneTransform (int bone) const; void setPuppetBoneTransform (int bone, const glm::mat4& transform); [[nodiscard]] const glm::mat4& getPuppetLocalBoneTransform (int bone) const; void setPuppetLocalBoneTransform (int bone, const glm::mat4& transform); void applyPuppetBonePhysicsImpulse (int bone, const glm::vec3& directional, const glm::vec3& angularDegrees); void resetPuppetBonePhysics (int bone); /** The animation layer with this serial for IAnimationLayer scripts, null once destroyed or when a scene layer * plays no clip (WE never creates such a layer) */ [[nodiscard]] PuppetActiveAnimation* findPuppetAnimationLayer (size_t serial); /** * IImageLayer animation layer calls (wallpaper64 2.8.42 sub_14020E910..sub_14020EF80), on the layer list in blend * order. Serials identify layers, nullopt when there is none */ [[nodiscard]] size_t getPuppetAnimationLayerCount () const; [[nodiscard]] std::optional getPuppetAnimationLayerAt (int64_t index) const; [[nodiscard]] std::optional findPuppetAnimationLayerByName (const std::string& name) const; std::optional createPuppetAnimationLayer (const Data::JSON::JSON& animation, const Data::JSON::JSON& config, bool autoRemove); bool destroyPuppetAnimationLayersByName (const std::string& name); bool destroyPuppetAnimationLayer (size_t serial); // ITextureAnimation (2.8.42 sub_14020E670): a layer's own clock for its animated texture; pause, stop, setFrame and // rate != 1 detach it from the shared clock, join reattaches struct TextureAnimation { /** +72 */ bool detached = false; /** +224 */ bool playing = true; /** +228 */ float rate = 1.0f; /** +232/+236: the frame and the time spent in it */ int frame = 0; float time = 0.0f; }; /** Made on first use, only for an animated texture (texture flag 4), null otherwise */ [[nodiscard]] TextureAnimation* getTextureAnimation (); [[nodiscard]] std::pair sharedTextureFrame () const; [[nodiscard]] int getTextureFrameCount () const; [[nodiscard]] float getTextureDuration () const; /** sub_1401FDF90's tail: a detached, playing animation moves by frametime * rate, before scripts run */ void updateTextureAnimation (float frametime); [[nodiscard]] std::optional getTextureFrameOverride () const override; protected: void setupPasses (); void rebuildActivePasses (); /** Fullscreen layers over the scene buffer keep their buffers at the output's size */ [[nodiscard]] bool followsOutputSize () const; void addEffectPasses (const ImageEffect& effect); [[nodiscard]] bool effectVisibilityChanged () const; void updateScreenSpacePosition (); void updateEffectTextureProjection (); void updateLightingTransform ( const glm::mat4& sceneTransform, const std::optional>& objectTransform, const glm::mat4& screen ); [[nodiscard]] glm::mat4 getViewProjection () const; private: Effects::CPass* m_fogPass = nullptr; private: bool loadPuppetMesh (const glm::vec2& size); void updatePuppetPositionBuffer (const glm::vec2& size); [[nodiscard]] glm::mat4 puppetObjectWorld () const; void updatePuppetSkinning (); [[nodiscard]] ComboMap puppetVertexAlphaCombos () const; void updatePuppetVertexAlpha (const std::vector& morphAlpha); void setupPuppetGeometryCallback (Effects::CPass* pass) const; [[nodiscard]] bool litDrawsDirect () const; void setupDirectLitPass (); void setupPuppetPrelight (); void uploadPuppetPrelightBuffers (); void setupPuppetClipping (); /** sub_1401FDF90 end: sorts the parts by order + their bone's animated draw order, rebuilds the indices */ void updatePuppetDrawOrder (); /** The mesh pass's draw split up like WE's command list (sub_140208670) */ void renderPuppetClipped (Effects::CPass* meshPass); /** sub_14020D6A0: one record's mask into _rt_FullAlphaMask, or multiplied into it through the intermediate */ void renderPuppetClipMask (const Effects::CPass& meshPass, int record, int draw, bool clear, bool intermediate); void selectPuppetDraw (int draw); void loadPuppetBlendMesh (const std::vector& data); /** sub_140209540 albedo buffer */ void setupPuppetBlendMap (bool offscreen); [[nodiscard]] bool hasLitMaterial () const; void updateGeometryBuffers (); [[nodiscard]] glm::vec2 resolveGeometrySize () const; void fillMissingSize (glm::vec2& size) const; [[nodiscard]] glm::vec2 textureUvScale () const; void updateScenePosition (const glm::vec2& size); void uploadGeometryBuffers (const glm::vec2& size); void uploadShapePointMesh (const glm::vec2& size); [[nodiscard]] bool shouldRenderFinalPass (bool isLastPass) const; bool configurePassTarget ( Effects::CPass* pass, std::shared_ptr& drawTo, const std::shared_ptr& asInput, std::shared_ptr& effectInput, bool& inTargetEffectSequence ); GLuint m_sceneSpacePosition; GLuint m_copySpacePosition; GLuint m_passSpacePosition; GLuint m_texcoordCopy; GLuint m_texcoordPass; GLuint m_texcoordDirect = GL_NONE; GLuint m_texcoordFinal = GL_NONE; GLuint m_puppetSpacePosition = GL_NONE; GLuint m_puppetTexCoord = GL_NONE; GLuint m_puppetIndices = GL_NONE; GLsizei m_puppetIndexCount = 0; bool m_hasPuppetMesh = false; Effects::CPass* m_puppetMeshPass = nullptr; bool m_puppetMeshLast = false; GLuint m_puppetDrawBuffer = GL_NONE; GLsizei m_puppetDrawOffset = 0; GLsizei m_puppetDrawCount = 0; /** the mesh pass draws again into what it already drew this frame */ bool m_puppetDrawKeep = false; std::optional m_puppetClipping = std::nullopt; GLuint m_puppetClipIndices = GL_NONE; /** mesh flag 8: the part ranges, the order the animated draw order put them in and the file's indices */ std::vector m_puppetParts = {}; std::vector m_puppetPartOrder = {}; std::vector m_puppetMeshIndices = {}; std::vector m_puppetClipMaskPasses = {}; Effects::CPass* m_puppetClipTargetPass = nullptr; Effects::CPass* m_puppetClipComposePass = nullptr; GLuint m_puppetClipComposePosition = GL_NONE; GLuint m_puppetClipComposeTexCoord = GL_NONE; glm::vec4 m_puppetClipRenderVar0 { 0.0f }; glm::vec4 m_puppetClipClearColor { 0.0f }; glm::mat4 m_puppetClipIdentity { 1.0f }; float m_puppetClipComposeAlpha = 1.0f; /** mesh flag 2 blend map */ struct { GLuint vertices = GL_NONE; GLuint indices = GL_NONE; GLsizei indexCount = 0; uint32_t format = 0; uint32_t stride = 0; uint32_t rows = 0; std::string material; std::shared_ptr albedo = nullptr; GLuint quad = GL_NONE; glm::mat4 projection { 1.0f }; Effects::CPass* copyPass = nullptr; Effects::CPass* pass = nullptr; } m_blendMap; /** WE's prelighting draw (sub_140209540) for lit puppets whose mesh a later pass draws */ struct { Effects::CPass* pass = nullptr; GLuint bindPositions = GL_NONE; GLuint morphedPositions = GL_NONE; GLuint flatPositions = GL_NONE; GLuint normals = GL_NONE; GLuint tangents = GL_NONE; GLuint blendIndices = GL_NONE; GLuint blendWeights = GL_NONE; std::vector auxPositions = {}; std::vector normalData = {}; std::vector tangentData = {}; std::vector morphedData = {}; std::vector bones = {}; glm::mat4 model { 1.0f }; glm::mat3 normal { 1.0f }; glm::mat4 projection { 1.0f }; } m_puppetPrelight; /** WE's model material draw for lit layers that need no buffer (sub_140209540), nullptr otherwise */ Effects::CPass* m_directLitPass = nullptr; mutable bool m_puppetDrawDiagnosticLogged = false; mutable bool m_puppetDrawErrorChecked = false; bool m_puppetPositionDiagnosticLogged = false; std::vector m_puppetRawPositions = {}; std::vector m_puppetBlendIndices = {}; std::vector m_puppetBlendWeights = {}; PuppetRig m_rig; std::vector m_puppetSkinnedPositions = {}; uint32_t m_puppetMeshFlags = 0; uint32_t m_puppetMeshFormat = 0; std::optional m_puppetMorph = std::nullopt; bool m_puppetMorphShader = false; std::vector> m_puppetMorphIndices = {}; bool m_puppetVertexAlpha = false; GLuint m_puppetVertexAlphaWeights = GL_NONE; std::vector m_puppetVertexAlphaData = {}; glm::mat4 m_modelViewProjectionScreen = {}; glm::mat4 m_modelViewProjectionPass = {}; /** effect passes: their buffer's geometry mapped to where the layer is on screen */ glm::mat4 m_effectModelViewProjectionCopy { 1.0f }; /** WE's object world matrix (y up, scene pixels), shaders read g_LayerModelMatrix for the layer's scale */ glm::mat4 m_layerModelMatrix { 1.0f }; glm::mat4 m_effectModelViewProjectionPass { 1.0f }; glm::mat4 m_modelViewProjectionCopy = {}; glm::mat4 m_modelViewProjectionScreenInverse = {}; glm::mat4 m_modelViewProjectionPassInverse = {}; glm::mat4 m_modelViewProjectionCopyInverse = {}; /** Maps the layer quad's own -1..1 space (+y = texture top) to screen clip space, effects like xray and * cursorripple use its inverse to bring g_PointerPosition into the layer's texture space */ glm::mat4 m_effectTextureProjection = glm::mat4 (1.0); glm::mat4 m_effectTextureProjectionInverse = glm::mat4 (1.0); glm::mat4 m_objectSpaceProjectionInverse = glm::mat4 (1.0); glm::mat4 m_modelMatrix = {}; glm::mat4 m_viewProjectionMatrix = {}; std::shared_ptr m_mainFBO = nullptr; std::shared_ptr m_subFBO = nullptr; std::shared_ptr m_namedMainFBO = nullptr; std::shared_ptr m_namedSubFBO = nullptr; /** pooled buffers of the same name are shared between layers */ struct LayerBufferConfig { glm::vec2 size {}; TextureFormat format = TextureFormat_ARGB8888; uint32_t flags = 0; } m_layerBufferConfig; /** like sub_1401EA500, redone on every pass rebuild */ void assignLayerBuffers (); std::shared_ptr m_currentMainFBO = nullptr; std::shared_ptr m_currentSubFBO = nullptr; const Image& m_image; mutable glm::vec3 m_colorCache {}; mutable glm::vec4 m_color4Cache {}; mutable float m_alphaCache = 1.0f; std::vector m_passes = {}; std::vector m_allPasses = {}; struct PassState { const DynamicValue* visible; BlendingMode blending; bool fromEffect; }; std::vector m_allPassStates = {}; struct SetupInputs { std::vector puppetEffects; int colorBlendMode = 0; bool copyForReaders = false; bool operator== (const SetupInputs&) const = default; }; [[nodiscard]] SetupInputs currentSetupInputs () const; void releasePasses (); SetupInputs m_setupInputs = {}; std::vector m_activePassMask = {}; bool m_hasActiveEffectPass = false; bool m_passesDrawToScreen = false; std::vector m_virtualPassess = {}; glm::vec4 m_pos = {}; /** The quad's size before the layer's scale, what m_pos spans */ glm::vec2 m_displaySize = {}; std::optional m_textureAnimation = std::nullopt; float m_textureAnimationClock = -1.0f; /** Lit passes: scene/copy space vertices -> WE world (y up, bottom left origin), see CPass::setLightingTransform */ glm::mat4 m_lightingSceneModel { 1.0f }; glm::mat4 m_lightingCopyModel { 1.0f }; glm::mat3 m_lightingNormal { 1.0f }; glm::mat4 m_lightingViewProjection { 1.0f }; glm::vec2 m_size = {}; // last m_pos/size uploaded to the GL geometry buffers; NaN forces the first upload glm::vec4 m_lastUploadedPos = glm::vec4 (std::numeric_limits::quiet_NaN ()); glm::vec2 m_lastUploadedGeometrySize = glm::vec2 (std::numeric_limits::quiet_NaN ()); bool m_initialized = false; bool m_isDependency = false; bool m_readByOtherLayer = false; bool m_emitterImageSource = false; bool m_hasPassthroughChildren = false; struct { struct { MaterialUniquePtr material; ImageEffectPassOverrideUniquePtr override; } colorBlending; Effects::CPass* colorBlendingPass = nullptr; std::vector compatibilityMaterials = {}; std::vector compatibilityOverrides = {}; MaterialUniquePtr clippingMask; MaterialUniquePtr clippingCompose; std::vector clippingOverrides = {}; ImageEffectPassOverrideUniquePtr puppetVertexAlpha; ImageEffectPassOverrideUniquePtr puppetPrelight; ImageEffectPassOverrideUniquePtr directLit; MaterialUniquePtr blendMap; ImageEffectPassOverrideUniquePtr blendMapOverride; } m_materials; }; }