summaryrefslogtreecommitdiff
path: root/indra/newview/llgltfmateriallist.h
blob: e79da3592a713e3db6d50a2d8f076952446613bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
 * @file   llgltfmateriallist.h
 *
 * $LicenseInfo:firstyear=2022&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2022, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */


#pragma once

#include "llassettype.h"
#include "llextendedstatus.h"
#include "llfetchedgltfmaterial.h"
#include "llgltfmaterial.h"
#include "llpointer.h"

#include <unordered_map>

class LLFetchedGLTFMaterial;
class LLGLTFOverrideCacheEntry;

class LLGLTFMaterialList
{
public:
    LLGLTFMaterialList() {}


    LLFetchedGLTFMaterial* getMaterial(const LLUUID& id);

    void addMaterial(const LLUUID& id, LLFetchedGLTFMaterial* material);
    void removeMaterial(const LLUUID& id);

    void flushMaterials();

    // Queue an modification of a material that we want to send to the simulator.  Call "flushUpdates" to flush pending updates.
    //  id - ID of object to modify
    //  side - TexureEntry index to modify, or -1 for all sides
    //  mat - material to apply as override, or nullptr to remove existing overrides and revert to asset
    //
    // NOTE: do not use to revert to asset when applying a new asset id, use queueApply below
    static void queueModify(const LLViewerObject* obj, S32 side, const LLGLTFMaterial* mat);

    // Queue an application of a material asset we want to send to the simulator.
    //  Call "flushUpdates" to flush pending updates immediately.
    //  Will be flushed automatically if queue is full.
    //  object_id - ID of object to apply material asset to
    //  side - TextureEntry index to apply material to, or -1 for all sides
    //  asset_id - ID of material asset to apply, or LLUUID::null to disassociate current material asset
    //
    // NOTE: Implicitly clears most override data if present
    static void queueApply(const LLViewerObject* obj, S32 side, const LLUUID& asset_id);

    // Queue an application of a material asset we want to send to the simulator.
    //  Call "flushUpdates" to flush pending updates immediately.
    //  Will be flushed automatically if queue is full.
    //  object_id - ID of object to apply material asset to
    //  side - TextureEntry index to apply material to, or -1 for all sides
    //  asset_id - ID of material asset to apply, or LLUUID::null to disassociate current material asset
    //  mat - override material, if null, will clear most override data
    static void queueApply(const LLViewerObject* obj, S32 side, const LLUUID& asset_id, const LLGLTFMaterial* mat);

    // flush pending material updates to the simulator
    // Automatically called once per frame, but may be called explicitly
    // for cases that care about the done_callback forwarded to LLCoros::instance().launch
    static void flushUpdates(void(*done_callback)(bool) = nullptr);

    static void addSelectionUpdateCallback(void(*update_callback)(const LLUUID& object_id, S32 side));

    // Queue an explicit LLSD ModifyMaterialParams update apply given override data
    //  overrides -- LLSD map (or array of maps) in the format:
    //      object_id   UUID(required)      id of object
    //      side        integer(required)   TE index of face to set, or -1 for all faces
    //      gltf_json   string(optional)    override data to set, empty string nulls out override data, omissions of this parameter keeps existing data
    //      asset_id    UUID(optional)      id of material asset to set, omission of this parameter keeps existing material asset id
    //
    // NOTE: Unless you already have a gltf_json string you want to send, strongly prefer using queueModify
    // If the queue/flush API is insufficient, extend it.
    static void queueUpdate(const LLSD& data);

    // Called by batch builder to give LLGLTMaterialList an opportunity to apply
    // any override data that arrived before the object was ready to receive it
    void applyQueuedOverrides(LLViewerObject* obj);

    // Apply an override update with the given data
    void applyOverrideMessage(LLMessageSystem* msg, const std::string& data);

private:
    friend class LLGLTFMaterialOverrideDispatchHandler;
    // save an override update that we got from the simulator for later (for example, if an override arrived for an unknown object)
    // NOTE: this is NOT for applying overrides from the UI, see queueModifyMaterial above
    void queueOverrideUpdate(const LLUUID& id, S32 side, LLGLTFMaterial* override_data);


    class CallbackHolder
    {
    public:
        CallbackHolder(void(*done_callback)(bool))
            : mCallback(done_callback)
        {}
        ~CallbackHolder()
        {
            if (mCallback) mCallback(mSuccess);
        }
        std::function<void(bool)> mCallback = nullptr;
        bool mSuccess = true;
    };
    static void flushUpdatesOnce(std::shared_ptr<CallbackHolder> callback_holder);
    static void modifyMaterialCoro(std::string cap_url, LLSD overrides, std::shared_ptr<CallbackHolder> callback_holder);

protected:
    static void onAssetLoadComplete(
        const LLUUID& asset_uuid,
        LLAssetType::EType type,
        void* user_data,
        S32 status,
        LLExtStat ext_status);

    typedef std::unordered_map<LLUUID, LLPointer<LLFetchedGLTFMaterial > > uuid_mat_map_t;
    uuid_mat_map_t mList;

    typedef std::vector<LLPointer<LLGLTFMaterial> > override_list_t;
    typedef std::unordered_map<LLUUID, override_list_t > queued_override_map_t;
    queued_override_map_t mQueuedOverrides;

    LLUUID mLastUpdateKey;

    struct ModifyMaterialData
    {
        LLUUID object_id;
        S32 side = -1;
        LLGLTFMaterial override_data;

        bool has_override = false;
    };

    typedef std::list<ModifyMaterialData> modify_queue_t;
    static modify_queue_t sModifyQueue;

    struct ApplyMaterialAssetData
    {
        LLUUID object_id;
        S32 side = -1;
        LLUUID asset_id;
        LLPointer<LLGLTFMaterial> override_data;
    };

    typedef std::list<ApplyMaterialAssetData> apply_queue_t;
    static apply_queue_t sApplyQueue;

    // data to be flushed to ModifyMaterialParams capability
    static LLSD    sUpdates;
};

extern LLGLTFMaterialList gGLTFMaterialList;