-
Notifications
You must be signed in to change notification settings - Fork 8
/
TextureLoadQueue.cpp
227 lines (195 loc) · 5.81 KB
/
TextureLoadQueue.cpp
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
/*
Copyright (c) 2021 Ed Harry, Wellcome Sanger Institute
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#define Number_Of_Texture_Buffers_Per_Queue 8
#define Number_Of_Texture_Buffer_Queues 8
struct
texture_buffer
{
u08 *texture;
u08 *compressionBuffer;
libdeflate_decompressor *decompressor;
FILE *file;
u32 homeIndex;
u16 x;
u16 y;
texture_buffer *prev;
};
struct
single_texture_buffer_queue
{
u32 queueLength;
u32 pad;
mutex rwMutex;
texture_buffer *front;
texture_buffer *rear;
};
struct
texture_buffer_queue
{
single_texture_buffer_queue **queues;
threadSig index;
u32 pad;
};
global_function
void
InitialiseSingleTextureBufferQueue(single_texture_buffer_queue *queue)
{
InitialiseMutex(queue->rwMutex);
queue->queueLength = 0;
}
global_function
void
AddSingleTextureBufferToQueue(single_texture_buffer_queue *queue, texture_buffer *buffer);
#define Compression_Header_Size 128
global_function
void
InitialiseTextureBufferQueue(memory_arena *arena, texture_buffer_queue *queue, u32 nBytesForTextureBuffer, const char *fileName)
{
queue->queues = PushArrayP(arena, single_texture_buffer_queue *, Number_Of_Texture_Buffer_Queues);
queue->index = 0;
u32 nAdded = 0;
u32 nFileHandles = 0;
ForLoop(Number_Of_Texture_Buffer_Queues)
{
queue->queues[index] = PushStructP(arena, single_texture_buffer_queue);
InitialiseSingleTextureBufferQueue(queue->queues[index]);
ForLoop2(Number_Of_Texture_Buffers_Per_Queue)
{
texture_buffer *buffer = PushStructP(arena, texture_buffer);
buffer->texture = PushArrayP(arena, u08, nBytesForTextureBuffer);
buffer->compressionBuffer = PushArrayP(arena, u08, nBytesForTextureBuffer + Compression_Header_Size);
buffer->decompressor = libdeflate_alloc_decompressor();
buffer->file = fopen(fileName, "rb");
buffer->homeIndex = index;
if (buffer->decompressor)
{
++nAdded;
if (buffer->file)
{
++nFileHandles;
AddSingleTextureBufferToQueue(queue->queues[index], buffer);
}
}
}
}
if (!nAdded)
{
fprintf(stderr, "Could not allocate memory for libdeflate decompressors\n");
exit(1);
}
if (!nFileHandles)
{
fprintf(stderr, "Could not open input file %s\n", (const char *)fileName);
}
}
global_function
void
AddSingleTextureBufferToQueue(single_texture_buffer_queue *queue, texture_buffer *buffer)
{
LockMutex(queue->rwMutex);
buffer->prev = 0;
switch (queue->queueLength)
{
case 0:
queue->front = buffer;
queue->rear = buffer;
break;
default:
queue->rear->prev = buffer;
queue->rear = buffer;
}
++queue->queueLength;
UnlockMutex(queue->rwMutex);
}
global_function
void
CloseSingleTextureBufferQueueFiles(single_texture_buffer_queue *queue)
{
LockMutex(queue->rwMutex);
for ( texture_buffer *buffer = queue->front;
buffer;
buffer = buffer->prev )
{
fclose(buffer->file);
free(buffer->decompressor);
}
UnlockMutex(queue->rwMutex);
}
global_function
void
CloseTextureBufferQueueFiles(texture_buffer_queue *queue)
{
ForLoop(Number_Of_Texture_Buffer_Queues)
{
CloseSingleTextureBufferQueueFiles(queue->queues[index]);
}
}
global_function
void
AddTextureBufferToQueue(texture_buffer_queue *queue, texture_buffer *buffer)
{
single_texture_buffer_queue *singleQueue = queue->queues[buffer->homeIndex];
AddSingleTextureBufferToQueue(singleQueue, buffer);
}
global_function
texture_buffer *
TakeSingleTextureBufferFromQueue(single_texture_buffer_queue *queue)
{
LockMutex(queue->rwMutex);
texture_buffer *buffer = queue->front;
switch (queue->queueLength)
{
case 0:
break;
case 1:
queue->front = 0;
queue->rear = 0;
queue->queueLength = 0;
break;
default:
queue->front = buffer->prev;
--queue->queueLength;
}
UnlockMutex(queue->rwMutex);
return(buffer);
}
global_function
single_texture_buffer_queue *
GetSingleTextureBufferQueue(texture_buffer_queue *queue)
{
u32 index = __atomic_fetch_add(&queue->index, 1, 0) % Number_Of_Texture_Buffer_Queues;
return(queue->queues[index]);
}
global_function
texture_buffer *
TakeTextureBufferFromQueue(texture_buffer_queue *queue)
{
return(TakeSingleTextureBufferFromQueue(GetSingleTextureBufferQueue(queue)));
}
global_function
texture_buffer *
TakeTextureBufferFromQueue_Wait(texture_buffer_queue *queue)
{
texture_buffer *buffer = 0;
while (!buffer)
{
buffer = TakeTextureBufferFromQueue(queue);
}
return(buffer);
}