-
Notifications
You must be signed in to change notification settings - Fork 26
/
texheap.cpp
153 lines (133 loc) · 3.55 KB
/
texheap.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
#include <malloc.h>
#include <string.h>
#include "texheap.h"
void TexHeap::Create(int page_cap_x, int page_cap_y, int numtex, const TexDesc* texdesc, int page_user_bytes)
{
allocs = 0;
cap_x = page_cap_x;
cap_y = page_cap_y;
user = page_user_bytes;
head = 0;
tail = 0;
// item_w = alloc_w;
// item_h = alloc_h;
// ifmt = internal_format;
num = numtex;
memcpy(tex, texdesc, num * sizeof(TexDesc));
}
void TexHeap::Destroy()
{
if (!head)
return;
int cap = cap_x * cap_y;
TexPage* p = head;
while (p!=tail)
{
TexPage* n = p->next;
glDeleteTextures(num, p->tex);
for (int i = 0; i < cap; i++)
free(p->alloc[i]);
free(p);
p = n;
}
cap = allocs % cap;
glDeleteTextures(num, p->tex);
for (int i = 0; i < cap; i++)
free(p->alloc[i]);
free(p);
}
TexAlloc* TexHeap::Alloc(const TexData data[])
{
int cap = cap_x * cap_y;
TexPage* page = tail;
if (allocs % cap == 0)
{
int alloc_bytes = sizeof(TexPage) + sizeof(TexAlloc*)*(cap - 1);
int page_bytes = alloc_bytes + user;
page = (TexPage*)malloc(page_bytes);
if (!page)
return 0;
page->user = (char*)page + alloc_bytes;
memset(page->user, 0, user);
page->heap = this;
page->prev = tail;
page->next = 0;
if (tail)
tail->next = page;
else
head = page;
tail = page;
glCreateTextures(GL_TEXTURE_2D, num, page->tex);
for (int t = 0; t < num; t++)
{
glTextureStorage2D(page->tex[t], 1, tex[t].ifmt, cap_x * tex[t].item_w, cap_y * tex[t].item_h);
glTextureParameteri(page->tex[t], GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTextureParameteri(page->tex[t], GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTextureParameteri(page->tex[t], GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextureParameteri(page->tex[t], GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
}
int on_page = allocs % cap;
TexAlloc* a = (TexAlloc*)malloc(sizeof(TexAlloc));
page->alloc[on_page] = a;
a->page = page;
a->x = on_page % cap_x;
a->y = on_page / cap_x;
allocs++;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
for (int t = 0; t < num; t++)
{
glTextureSubImage2D(page->tex[t], 0, a->x * tex[t].item_w, a->y * tex[t].item_h,
tex[t].item_w, tex[t].item_h, data[t].format, data[t].type, data[t].data);
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
return a;
}
void TexAlloc::Free()
{
TexHeap* h = page->heap;
int cap = h->cap_x * h->cap_y;
int on_page = y * h->cap_x + x;
// not last alloc on last page?
if (page != h->tail || on_page != (h->allocs-1) % cap)
{
TexAlloc* last = h->tail->alloc[(h->allocs-1) % cap];
for (int t = 0; t < h->num; t++)
{
glCopyImageSubData(
last->page->tex[t], GL_TEXTURE_2D, 0, last->x * h->tex[t].item_w, last->y * h->tex[t].item_h, 0,
page->tex[t], GL_TEXTURE_2D, 0, x * h->tex[t].item_w, y * h->tex[t].item_h, 0,
h->tex[t].item_w, h->tex[t].item_h, 1);
}
page->alloc[on_page] = last;
last->page = page;
last->x = x;
last->y = y;
}
h->allocs--;
free(this);
// now check if we can delete last page
if (h->allocs % cap == 0)
{
glDeleteTextures(h->num, h->tail->tex);
if (h->tail->prev)
h->tail->prev->next = 0;
else
h->head = 0;
h->tail = h->tail->prev;
free(h->tail);
}
}
void TexAlloc::Update(int first, int count, const TexData data[])
{
TexHeap* h = page->heap;
int end = first + count;
end = h->num < end ? h->num : end;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
for (int t = first; t < end; t++)
{
glTextureSubImage2D(page->tex[t], 0, x * h->tex[t].item_w, y * h->tex[t].item_h,
h->tex[t].item_w, h->tex[t].item_h, data[t-first].format, data[t-first].type, data[t-first].data);
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
}