-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathTexture.cpp
More file actions
240 lines (193 loc) · 6.98 KB
/
Copy pathTexture.cpp
File metadata and controls
240 lines (193 loc) · 6.98 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "Texture.h"
#include <bgfx/bgfx.h>
#include <engine/BaseEngine.h>
#include <utils/logger.h>
#include <vdfs/fileIndex.h>
#include <zenload/ztex2dds.h>
using namespace Textures;
// These are compiled inside bgfx
typedef unsigned char stbi_uc;
extern "C" stbi_uc* stbi_load_from_memory(stbi_uc const* _buffer, int _len, int* _x, int* _y, int* _comp, int _req_comp);
extern "C" void stbi_image_free(void* _ptr);
TextureAllocator::TextureAllocator(Engine::BaseEngine& engine)
: m_Engine(engine)
{
}
TextureAllocator::~TextureAllocator()
{
// Delete all textures
for (size_t i = 0; i < m_Allocator.getNumObtainedElements(); i++)
{
bgfx::TextureHandle h = m_Allocator.getElements()[i].m_TextureHandle;
bgfx::destroy(h);
}
m_EstimatedGPUBytes = 0;
}
Handle::TextureHandle TextureAllocator::loadTextureDDS(const std::vector<uint8_t>& data, const std::string& name)
{
// Check if this was already loaded
auto it = m_TexturesByName.find(name);
if (it != m_TexturesByName.end())
return (*it).second;
// Make wrapper-object
Handle::TextureHandle h = m_Allocator.createObject();
m_Allocator.getElement(h).textureFormat = bgfx::TextureFormat::Unknown;
m_Allocator.getElement(h).imageData = data;
//m_Allocator.getElement(h).m_TextureHandle = bth;
m_Allocator.getElement(h).m_TextureHandle.idx = bgfx::kInvalidHandle;
m_Allocator.getElement(h).m_TextureName = name;
ZenLoad::DDSURFACEDESC2 desc = ZenLoad::getSurfaceDesc(data);
m_Allocator.getElement(h).m_Width = desc.dwWidth;
m_Allocator.getElement(h).m_Height = desc.dwHeight;
// Add handle to name-map, if it got one
if (!name.empty())
m_TexturesByName[name] = h;
// Flush the pipeline
// TODO: There must be something better than "frame"?
//bgfx::touch(0);
//bgfx::frame();
return h;
}
Handle::TextureHandle TextureAllocator::loadTextureRGBA8(const std::vector<uint8_t>& data, uint16_t width, uint16_t height, const std::string& name)
{
// Check if this was already loaded
auto it = m_TexturesByName.find(name);
if (it != m_TexturesByName.end())
return (*it).second;
// Load image
//int width, height, comp;
//void* out = stbi_load_from_memory( data.data(), data.size(), (int*)&width, (int*)&height, &comp, 4);
// Try to load the texture first, so we don't have to clean up if this fails
//TODO: Avoid the second copy here
// Make wrapper-object
Handle::TextureHandle h = m_Allocator.createObject();
m_Allocator.getElement(h).textureFormat = bgfx::TextureFormat::RGBA8;
m_Allocator.getElement(h).imageData = data;
//m_Allocator.getElement(h).m_TextureHandle = bth;
m_Allocator.getElement(h).m_TextureHandle.idx = bgfx::kInvalidHandle;
m_Allocator.getElement(h).m_TextureName = name;
m_Allocator.getElement(h).m_Width = width;
m_Allocator.getElement(h).m_Height = height;
// Add handle to name-map, if it got one
if (!name.empty())
m_TexturesByName[name] = h;
// Flush the pipeline
// TODO: There must be something better than "frame"?
//bgfx::touch(0);
//bgfx::frame();
return h;
}
Handle::TextureHandle TextureAllocator::loadTextureVDF(const VDFS::FileIndex& idx, const std::string& name)
{
// Check if this was already loaded
auto it = m_TexturesByName.find(name);
if (it != m_TexturesByName.end())
return (*it).second;
std::string vname = name;
std::vector<uint8_t> ztex;
std::vector<uint8_t> dds;
// Check if this isn't the compiled version
if (vname.find("-C") == std::string::npos)
{
// Strip the ".TGA"
vname = vname.substr(0, vname.size() - 4);
// Add "compiled"-extension
vname += "-C.TEX";
}
// Load from archive
bool asDDS = true;
idx.getFileData(vname, ztex);
// No compiled version? Try again as TGA
if (ztex.empty())
{
vname = name;
idx.getFileData(vname, ztex);
asDDS = false;
}
// Failed?
if (ztex.empty())
return Handle::TextureHandle::makeInvalidHandle();
if (asDDS)
{
// Convert to usual DDS
ZenLoad::convertZTEX2DDS(ztex, dds);
}
#if EMSCRIPTEN
if (asDDS)
{
LogInfo() << "Converting DDS to RGBA8 for: " << name;
// Android doesn't support DDS for the most part
ztex.clear();
ZenLoad::convertDDSToRGBA8(dds, ztex);
asDDS = false;
}
#endif
Handle::TextureHandle h;
if (asDDS)
{
// Proceed to load as usual dds-file and the input-name
h = loadTextureDDS(dds, name);
}
else
{
ZenLoad::DDSURFACEDESC2 desc = ZenLoad::getSurfaceDesc(dds);
h = loadTextureRGBA8(ztex, (uint16_t)desc.dwWidth, (uint16_t)desc.dwHeight, name);
}
m_Engine.getJobManager().executeInMainThread<void>([this, h](Engine::BaseEngine* pEngine) {
finalizeLoad(h);
});
return h;
}
Handle::TextureHandle TextureAllocator::loadTextureVDF(const std::string& name)
{
return loadTextureVDF(m_Engine.getVDFSIndex(), name);
}
void TextureAllocator::asyncFinalizeLoad(Handle::TextureHandle h)
{
m_Engine.getJobManager().executeInMainThread<void>([this, h](Engine::BaseEngine* pEngine) {
finalizeLoad(h);
});
}
bool TextureAllocator::finalizeLoad(Handle::TextureHandle h)
{
Texture& tx = m_Allocator.getElement(h);
// if there's already a texture loaded for the object, destroy it
if (tx.m_TextureHandle.idx != bgfx::kInvalidHandle) {
bgfx::destroy(tx.m_TextureHandle);
tx.m_TextureHandle.idx = bgfx::kInvalidHandle;
}
std::uint32_t textureFlags = BGFX_TEXTURE_NONE;
if (this->m_Engine.getEngineArgs().noTextureFiltering)
{
textureFlags = BGFX_TEXTURE_MIN_POINT | BGFX_TEXTURE_MAG_POINT | BGFX_TEXTURE_MIP_POINT;
}
if (tx.textureFormat == bgfx::TextureFormat::RGBA8)
{
const bgfx::Memory* mem = bgfx::alloc(tx.imageData.size());
memcpy(mem->data, tx.imageData.data(), tx.imageData.size());
bgfx::TextureHandle bth = bgfx::createTexture2D(tx.m_Width, tx.m_Height, false, 1, bgfx::TextureFormat::RGBA8, textureFlags, mem);
m_EstimatedGPUBytes += tx.imageData.size();
// Free imange
//stbi_image_free(out);
// Couldn't load this one?
if (bth.idx == bgfx::kInvalidHandle) {
LogWarn() << "Could not finalize texture load";
return false;
}
tx.m_TextureHandle = bth;
}
else
{
// Try to load the texture first, so we don't have to clean up if this fails
//TODO: Avoid the second copy here
const bgfx::Memory* mem = bgfx::alloc(tx.imageData.size());
memcpy(mem->data, tx.imageData.data(), tx.imageData.size());
bgfx::TextureHandle bth = bgfx::createTexture(mem, textureFlags);
m_EstimatedGPUBytes += tx.imageData.size();
// Couldn't load this one?
if (bth.idx == bgfx::kInvalidHandle)
return false;
tx.m_TextureHandle = bth;
}
return true;
}