-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextureManager.h
234 lines (175 loc) · 6.33 KB
/
TextureManager.h
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
//
// Created by Marrony Neris on 11/23/15.
//
#ifndef TEXTURE_MANAGER_H
#define TEXTURE_MANAGER_H
#include <type_traits>
void loadImage(HeapAllocator& allocator, const char* filename, Image& image) {
FILE* file = fopen(filename, "rb");
assert(file != nullptr);
fread(&image.width, sizeof(int), 1, file);
fread(&image.height, sizeof(int), 1, file);
fread(&image.format, sizeof(int), 1, file);
image.pixels = (uint8_t*)allocator.allocate(image.width*image.height*image.format);
fread(image.pixels, sizeof(uint8_t), image.width*image.height*image.format, file);
fclose(file);
}
void saveImage(const char* filename, const Image& image) {
FILE* file = fopen(filename, "wb");
assert(file != nullptr);
fwrite(&image.width, sizeof(int), 1, file);
fwrite(&image.height, sizeof(int), 1, file);
fwrite(&image.format, sizeof(int), 1, file);
fwrite(image.pixels, sizeof(uint8_t), image.width*image.height*image.format, file);
fclose(file);
}
void loadCube(HeapAllocator& allocator, const char* filename, ImageCube& cube) {
FILE* file = fopen(filename, "rb");
assert(file != nullptr);
int width, height, format;
fread(&width, sizeof(int), 1, file);
fread(&height, sizeof(int), 1, file);
fread(&format, sizeof(int), 1, file);
for(int i = 0; i < 6; i++) {
cube.faces[i].width = width;
cube.faces[i].height = height;
cube.faces[i].format = format;
cube.faces[i].pixels = (uint8_t*)allocator.allocate(width*height*format);
fread(cube.faces[i].pixels, sizeof(uint8_t), width*height*format, file);
}
fclose(file);
}
void saveCube(const char* filename, const ImageCube& cube) {
FILE* file = fopen(filename, "wb");
assert(file != nullptr);
int width = cube.faces[0].width;
int height = cube.faces[0].height;
int format = cube.faces[0].format;
fwrite(&width, sizeof(int), 1, file);
fwrite(&height, sizeof(int), 1, file);
fwrite(&format, sizeof(int), 1, file);
for(int i = 0; i < 6; i++)
fwrite(cube.faces[i].pixels, sizeof(uint8_t), width*height*format, file);
fclose(file);
}
Texture2D createTexture2D(Device& device, const Image& image) {
if(image.format == 1)
return device.createRTexture(image.width, image.height, image.pixels);
if(image.format == 3)
return device.createRGBTexture(image.width, image.height, image.pixels);
if(image.format == 4)
return device.createRGBATexture(image.width, image.height, image.pixels);
return {0};
}
TextureCube createTextureCube(Device& device, const ImageCube cube[], int mipLevels) {
if(cube[0].faces[0].format == 3)
return device.createRGBCubeTexture(cube, mipLevels);
return {0};
}
#include "TgaReader.h"
#include "JpegReader.h"
class TextureManager {
public:
TextureManager(HeapAllocator& allocator, Device& device) : allocator(allocator), device(device) {
linear = device.createSampler(GL_LINEAR, GL_LINEAR);
nearest = device.createSampler(GL_NEAREST, GL_NEAREST);
textureCount = 0;
textureAllocated = 16;
textures = (Resource*) allocator.allocate(textureAllocated * sizeof(Resource));
memset(textures, 0, textureAllocated * sizeof(Resource));
}
~TextureManager() {
assert(textureCount == 0);
allocator.deallocate(textures);
device.destroySampler(linear);
device.destroySampler(nearest);
}
Texture2D loadTexture(const char* filename) {
uint32_t index;
if(findTexture(filename, index)) {
textures[index].refs++;
return textures[index].texture;
}
if(textureCount >= textureAllocated) {
textureAllocated = textureAllocated * 3 / 2;
textures = (Resource*) allocator.reallocate(textures, textureAllocated * sizeof(Resource));
}
index = textureCount++;
Image image;
FILE* stream = fopen(filename, "rb");
assert(stream != nullptr);
if(strstr(filename, ".tga") != nullptr)
readTga(allocator, stream, image);
else if(strstr(filename, ".jpg") != nullptr)
readJpeg(allocator, stream, image);
fclose(stream);
assert(image.format == 1 || image.format == 3 || image.format == 4);
textures[index].refs = 1;
textures[index].filename = filename;
switch(image.format) {
case 1:
textures[index].texture = device.createRTexture(image.width, image.height, image.pixels);
break;
case 3:
textures[index].texture = device.createRGBTexture(image.width, image.height, image.pixels);
break;
case 4:
textures[index].texture = device.createRGBATexture(image.width, image.height, image.pixels);
break;
}
allocator.deallocate(image.pixels);
return textures[index].texture;
}
void unloadTexture(Texture2D texture) {
uint32_t index;
if (findTexture(texture, index)) {
textures[index].refs--;
if (textures[index].refs == 0) {
destroy(&textures[index]);
textureCount--;
std::swap(textures[index], textures[textureCount]);
}
}
}
Sampler getLinear() {
return linear;
}
Sampler getNearest() {
return nearest;
}
private:
struct Resource {
const char* filename;
Texture2D texture;
uint32_t refs;
};
void destroy(Resource* resource) {
device.destroyTexture(resource->texture);
}
bool findTexture(Texture2D texture, uint32_t& index) {
for(uint32_t i = 0; i < textureCount; i++) {
if(textures[i].texture.id == texture.id) {
index = i;
return true;
}
}
return false;
}
bool findTexture(const char* filename, uint32_t& index) {
for(uint32_t i = 0; i < textureCount; i++) {
if(textures[i].filename != nullptr && strcmp(textures[i].filename, filename) == 0) {
index = i;
return true;
}
}
return false;
}
HeapAllocator& allocator;
Device& device;
Resource* textures;
uint32_t textureCount;
uint32_t textureAllocated;
Sampler linear;
Sampler nearest;
};
#endif //TEXTURE_MANAGER_H