Repository navigation
Expand file tree
/
Copy pathTextureEncoder.cpp
More file actions
304 lines (271 loc) · 12.3 KB
/
Copy pathTextureEncoder.cpp
File metadata and controls
304 lines (271 loc) · 12.3 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "TextureEncoder.h"
#include "TextureFormat.h"
#include "cuttlefish/Image.h"
#include "cuttlefish/Texture.h"
#include <cstring>
using namespace std;
#if defined(_MSC_VER)
#define EXPORT extern "C" __declspec(dllexport)
#elif defined(__GNUC__)
#define EXPORT extern "C" __attribute__((visibility("default")))
#endif
struct TextureDataMip
{
int width;
int height;
int size;
void* data;
};
struct TextureDataBuffer
{
int width;
int height;
int mipCount;
TextureDataMip* mips;
};
static TextureDataBuffer MakeError(TextureEncoderError code)
{
TextureDataBuffer buffer;
buffer.width = (int)code;
buffer.height = -1;
buffer.mipCount = 0;
buffer.mips = nullptr;
return buffer;
}
static cuttlefish::Texture::Format U2cfFormat(TextureFormat uf)
{
switch (uf)
{
case TextureFormat::Alpha8: return cuttlefish::Texture::Format::A8; // <not supported>
case TextureFormat::ARGB4444: return cuttlefish::Texture::Format::A4R4G4B4;
case TextureFormat::RGB24: return cuttlefish::Texture::Format::R8G8B8;
case TextureFormat::RGBA32: return cuttlefish::Texture::Format::R8G8B8A8;
case TextureFormat::ARGB32: return cuttlefish::Texture::Format::A8R8G8B8; // <not supported>
case TextureFormat::ARGBFloat: return cuttlefish::Texture::Format::A32R32G32B32; // <not supported>
case TextureFormat::RGB565: return cuttlefish::Texture::Format::R5G6B5;
case TextureFormat::BGR24: return cuttlefish::Texture::Format::B8G8R8;
case TextureFormat::R16: return cuttlefish::Texture::Format::R16;
case TextureFormat::DXT1: return cuttlefish::Texture::Format::BC1_RGB;
case TextureFormat::DXT3: return cuttlefish::Texture::Format::BC2;
case TextureFormat::DXT5: return cuttlefish::Texture::Format::BC3;
case TextureFormat::RGBA4444: return cuttlefish::Texture::Format::R4G4B4A4;
case TextureFormat::BGRA32: return cuttlefish::Texture::Format::B8G8R8A8;
case TextureFormat::RHalf: return cuttlefish::Texture::Format::R16;
case TextureFormat::RGHalf: return cuttlefish::Texture::Format::R16G16;
case TextureFormat::RGBAHalf: return cuttlefish::Texture::Format::R16G16B16A16;
case TextureFormat::RFloat: return cuttlefish::Texture::Format::R32;
case TextureFormat::RGFloat: return cuttlefish::Texture::Format::R32G32;
case TextureFormat::RGBAFloat: return cuttlefish::Texture::Format::R32G32B32A32;
case TextureFormat::YUY2: return cuttlefish::Texture::Format::YUY2; // <not supported>
case TextureFormat::RGB9e5Float: return cuttlefish::Texture::Format::E5B9G9R9_UFloat;
case TextureFormat::RGBFloat: return cuttlefish::Texture::Format::R32G32B32;
case TextureFormat::BC6H: return cuttlefish::Texture::Format::BC6H;
case TextureFormat::BC7: return cuttlefish::Texture::Format::BC7;
case TextureFormat::BC4: return cuttlefish::Texture::Format::BC4;
case TextureFormat::BC5: return cuttlefish::Texture::Format::BC5;
case TextureFormat::DXT1Crunched: return cuttlefish::Texture::Format::BC1_RGB; // special case
case TextureFormat::DXT5Crunched: return cuttlefish::Texture::Format::BC3; // special case
case TextureFormat::PVRTC_RGB2: return cuttlefish::Texture::Format::PVRTC1_RGB_2BPP;
case TextureFormat::PVRTC_RGBA2: return cuttlefish::Texture::Format::PVRTC1_RGBA_2BPP;
case TextureFormat::PVRTC_RGB4: return cuttlefish::Texture::Format::PVRTC1_RGB_4BPP;
case TextureFormat::PVRTC_RGBA4: return cuttlefish::Texture::Format::PVRTC1_RGBA_4BPP;
case TextureFormat::ETC_RGB4: return cuttlefish::Texture::Format::ETC1;
// ATC not supported
case TextureFormat::BGRA32Old: return cuttlefish::Texture::Format::B8G8R8A8;
case TextureFormat::EAC_R: return cuttlefish::Texture::Format::EAC_R11;
case TextureFormat::EAC_R_SIGNED: return cuttlefish::Texture::Format::EAC_R11;
case TextureFormat::EAC_RG: return cuttlefish::Texture::Format::EAC_R11G11;
case TextureFormat::EAC_RG_SIGNED: return cuttlefish::Texture::Format::EAC_R11G11;
case TextureFormat::ETC2_RGB4: return cuttlefish::Texture::Format::ETC2_R8G8B8; // ???
case TextureFormat::ETC2_RGBA1: return cuttlefish::Texture::Format::ETC2_R8G8B8A1;
case TextureFormat::ETC2_RGBA8: return cuttlefish::Texture::Format::ETC2_R8G8B8A8;
case TextureFormat::ASTC_RGB_4x4: return cuttlefish::Texture::Format::ASTC_4x4;
case TextureFormat::ASTC_RGB_5x5: return cuttlefish::Texture::Format::ASTC_5x5;
case TextureFormat::ASTC_RGB_6x6: return cuttlefish::Texture::Format::ASTC_6x6;
case TextureFormat::ASTC_RGB_8x8: return cuttlefish::Texture::Format::ASTC_8x8;
case TextureFormat::ASTC_RGB_10x10: return cuttlefish::Texture::Format::ASTC_10x10;
case TextureFormat::ASTC_RGB_12x12: return cuttlefish::Texture::Format::ASTC_12x12;
case TextureFormat::ASTC_RGBA_4x4: return cuttlefish::Texture::Format::ASTC_4x4;
case TextureFormat::ASTC_RGBA_5x5: return cuttlefish::Texture::Format::ASTC_5x5;
case TextureFormat::ASTC_RGBA_6x6: return cuttlefish::Texture::Format::ASTC_6x6;
case TextureFormat::ASTC_RGBA_8x8: return cuttlefish::Texture::Format::ASTC_8x8;
case TextureFormat::ASTC_RGBA_10x10: return cuttlefish::Texture::Format::ASTC_10x10;
case TextureFormat::ASTC_RGBA_12x12: return cuttlefish::Texture::Format::ASTC_12x12;
case TextureFormat::ETC_RGB4_3DS: return cuttlefish::Texture::Format::ETC1;
case TextureFormat::ETC_RGBA8_3DS: return cuttlefish::Texture::Format::ETC2_R8G8B8A8;
case TextureFormat::RG16: return cuttlefish::Texture::Format::R8G8;
case TextureFormat::R8: return cuttlefish::Texture::Format::R8;
case TextureFormat::ETC_RGB4Crunched: return cuttlefish::Texture::Format::ETC1; // special case
case TextureFormat::ETC2_RGBA8Crunched: return cuttlefish::Texture::Format::ETC2_R8G8B8A8; // special case
case TextureFormat::ASTC_HDR_4x4: return cuttlefish::Texture::Format::ASTC_4x4;
case TextureFormat::ASTC_HDR_5x5: return cuttlefish::Texture::Format::ASTC_5x5;
case TextureFormat::ASTC_HDR_6x6: return cuttlefish::Texture::Format::ASTC_6x6;
case TextureFormat::ASTC_HDR_8x8: return cuttlefish::Texture::Format::ASTC_8x8;
case TextureFormat::ASTC_HDR_10x10: return cuttlefish::Texture::Format::ASTC_10x10;
case TextureFormat::ASTC_HDR_12x12: return cuttlefish::Texture::Format::ASTC_12x12;
case TextureFormat::RG32: return cuttlefish::Texture::Format::R16G16;
case TextureFormat::RGB48: return cuttlefish::Texture::Format::R16G16B16; // <not supported>
case TextureFormat::RGBA64: return cuttlefish::Texture::Format::R16G16B16A16; // <not supported>
default: return cuttlefish::Texture::Format::Unknown;
}
}
static cuttlefish::Texture::Type U2cfType(TextureFormat uf)
{
switch (uf)
{
case TextureFormat::ARGBFloat: return cuttlefish::Texture::Type::Float;
case TextureFormat::RHalf: return cuttlefish::Texture::Type::Float;
case TextureFormat::RGHalf: return cuttlefish::Texture::Type::Float;
case TextureFormat::RGBAHalf: return cuttlefish::Texture::Type::Float;
case TextureFormat::RFloat: return cuttlefish::Texture::Type::Float;
case TextureFormat::RGFloat: return cuttlefish::Texture::Type::Float;
case TextureFormat::RGBAFloat: return cuttlefish::Texture::Type::Float;
case TextureFormat::RGBFloat: return cuttlefish::Texture::Type::Float;
case TextureFormat::RGB9e5Float: return cuttlefish::Texture::Type::UFloat;
case TextureFormat::BC6H: return cuttlefish::Texture::Type::UFloat;
case TextureFormat::EAC_R_SIGNED: return cuttlefish::Texture::Type::SNorm;
case TextureFormat::EAC_RG_SIGNED: return cuttlefish::Texture::Type::SNorm;
default: return cuttlefish::Texture::Type::UNorm;
}
}
static cuttlefish::Texture::Quality U2cfQuality(int quality)
{
if (quality <= 1)
return cuttlefish::Texture::Quality::Lowest;
else if (quality <= 2)
return cuttlefish::Texture::Quality::Low;
else if (quality <= 3)
return cuttlefish::Texture::Quality::Normal;
else if (quality <= 4)
return cuttlefish::Texture::Quality::High;
else
return cuttlefish::Texture::Quality::Highest;
}
EXPORT int SanityCheck(int number)
{
return number + 123;
}
EXPORT void FreeTextureDataBuffer(TextureDataMip* mips, int mipCount)
{
for (int i = 0; i < mipCount; i++)
{
void* data = mips[i].data;
if (data != nullptr)
{
free(data);
}
}
free(mips);
}
EXPORT TextureDataBuffer ConvertAndFreeTexture(cuttlefish::Texture* texture, TextureFormat uf, int quality = 3)
{
TextureDataBuffer buffer;
buffer.mipCount = 0;
buffer.mips = nullptr;
bool convertSuccess = texture->convert(U2cfFormat(uf), U2cfType(uf), U2cfQuality(quality));
if (!convertSuccess)
{
delete texture;
return MakeError(TextureEncoderError::EncodeTexture);
}
int mipCount = texture->mipLevelCount();
buffer.width = texture->width();
buffer.height = texture->height();
buffer.mips = (TextureDataMip*)malloc(sizeof(TextureDataMip) * mipCount);
if (buffer.mips == nullptr)
{
delete texture;
return MakeError(TextureEncoderError::AllocateMemory);
}
for (int i = 0; i < mipCount; i++)
{
TextureDataMip* mip = &buffer.mips[i];
mip->width = 0;
mip->height = 0;
mip->size = 0;
mip->data = nullptr;
}
// we could probably just return the buffers from TextureDataMip*
// but we're just going to allocate another buffer for now
for (int i = 0; i < mipCount; i++)
{
TextureDataMip* mip = &buffer.mips[i];
mip->width = texture->width(i);
mip->height = texture->height(i);
mip->size = texture->dataSize(i);
mip->data = malloc(mip->size);
if (mip->data == nullptr)
{
// cleanup already allocated mips
FreeTextureDataBuffer(buffer.mips, i);
delete texture;
return MakeError(TextureEncoderError::AllocateMemory);
}
memcpy(mip->data, texture->data(i), mip->size);
}
buffer.mipCount = mipCount;
delete texture;
return buffer;
}
EXPORT cuttlefish::Texture* LoadTextureFromFile(const char* path, int mips = 1)
{
cuttlefish::Image tmpImage;
bool success = tmpImage.load(path);
if (!success)
{
return nullptr;
}
tmpImage.flipVertical();
int maxMipCount = cuttlefish::Texture::maxMipmapLevels(cuttlefish::Texture::Dimension::Dim2D, tmpImage.width(), tmpImage.height());
if (mips > maxMipCount)
mips = maxMipCount;
auto texture = new cuttlefish::Texture(cuttlefish::Texture::Dimension::Dim2D, tmpImage.width(), tmpImage.height(), 0, mips);
bool setSuccess = texture->setImage(tmpImage);
if (!setSuccess)
{
delete texture;
return nullptr;
}
int mipCount = texture->mipLevelCount();
if (mipCount > 1)
{
bool mipSuccess = texture->generateMipmaps(cuttlefish::Image::ResizeFilter::CatmullRom, mipCount);
if (!mipSuccess)
{
delete texture;
return nullptr;
}
}
return texture;
}
EXPORT cuttlefish::Texture* LoadTextureFromBuffer(void* data, int size, int width, int height, int mips = 1)
{
cuttlefish::Image tmpImage;
bool success = tmpImage.loadRaw(data, size, width, height);
if (!success)
{
return nullptr;
}
tmpImage.flipVertical();
int maxMipCount = cuttlefish::Texture::maxMipmapLevels(cuttlefish::Texture::Dimension::Dim2D, tmpImage.width(), tmpImage.height());
if (mips > maxMipCount)
mips = maxMipCount;
auto texture = new cuttlefish::Texture(cuttlefish::Texture::Dimension::Dim2D, tmpImage.width(), tmpImage.height(), 0, mips);
bool setSuccess = texture->setImage(tmpImage);
if (!setSuccess)
{
delete texture;
return nullptr;
}
int mipCount = texture->mipLevelCount();
if (mipCount > 1)
{
bool mipSuccess = texture->generateMipmaps(cuttlefish::Image::ResizeFilter::CatmullRom, mipCount);
if (!mipSuccess)
{
delete texture;
return nullptr;
}
}
return texture;
}