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
|
#include "glTexture.h"
#include "config/types.h"
#include "filesystem.h"
#include "gfx/models/tga.h"
#include "maths.h"
#include <fcntl.h>
#include <ranges>
#include <sys/mman.h>
void
Impl::glTextureBase::bind(const GLuint unit) const
{
glBindTextureUnit(unit, name);
}
void
Impl::glTextureBase::generateMipmap()
{
glGenerateTextureMipmap(name);
}
template<glm::length_t Dims>
requires(Dims >= 1 && Dims <= 3)
glm::vec<Dims, GLsizei>
Impl::glTextureDims<Dims>::getSize() const
{
static constexpr std::array<GLenum, 3> PARAMS {GL_TEXTURE_WIDTH, GL_TEXTURE_HEIGHT, GL_TEXTURE_DEPTH};
glm::vec<Dims, GLsizei> size {};
for (auto [dim, param] : std::views::enumerate(PARAMS) | std::views::take(Dims)) {
glGetTextureLevelParameteriv(name, 0, param, &size[static_cast<glm::length_t>(dim)]);
}
return size;
}
template<>
void
Impl::glTextureDims<1>::storage(const GLsizei levels, const GLenum internalformat, glm::vec<1, GLsizei> dims)
{
glTextureStorage1D(name, levels, internalformat, dims.x);
}
template<>
void
Impl::glTextureDims<2>::storage(const GLsizei levels, const GLenum internalformat, glm::vec<2, GLsizei> dims)
{
glTextureStorage2D(name, levels, internalformat, dims.x, dims.y);
}
template<>
void
Impl::glTextureDims<3>::storage(const GLsizei levels, const GLenum internalformat, glm::vec<3, GLsizei> dims)
{
glTextureStorage3D(name, levels, internalformat, dims.x, dims.y, dims.z);
}
template<>
void
Impl::glTextureDims<1>::subImage(
glm::vec<1, GLint> offset, glm::vec<1, GLint> size, const GLenum format, const GLenum type, const void * pixels)
{
glTextureSubImage1D(name, 0, offset.x, size.x, format, type, pixels);
}
template<>
void
Impl::glTextureDims<2>::subImage(
glm::vec<2, GLint> offset, glm::vec<2, GLint> size, const GLenum format, const GLenum type, const void * pixels)
{
glTextureSubImage2D(name, 0, offset.x, offset.y, size.x, size.y, format, type, pixels);
}
template<>
void
Impl::glTextureDims<3>::subImage(
glm::vec<3, GLint> offset, glm::vec<3, GLint> size, const GLenum format, const GLenum type, const void * pixels)
{
glTextureSubImage3D(name, 0, offset.x, offset.y, offset.z, size.x, size.y, size.z, format, type, pixels);
}
template<glm::length_t Dims>
requires(Dims >= 1 && Dims <= 3)
void
Impl::glTextureDims<Dims>::image(
glm::vec<Dims, GLint> size, const GLenum format, const GLenum type, const void * pixels)
{
subImage({}, size, format, type, pixels);
}
template<glm::length_t Dims>
requires(Dims >= 1 && Dims <= 3)
void
Impl::glTextureDims<Dims>::image(const GLenum format, const GLenum type, const void * pixels)
{
image(getSize(), format, type, pixels);
}
namespace {
template<glm::length_t Dims>
requires(Dims >= 1 && Dims <= 3)
size_t
areaOf(glm::vec<Dims, GLsizei> size)
{
size_t area = 1;
for (auto dim = 0; dim < Dims; ++dim) {
area *= static_cast<size_t>(size[dim]);
}
return area;
}
template<glm::length_t Dims, glm::length_t channels>
requires(Dims >= 1 && Dims <= 3)
void
save(const Impl::glTextureDims<Dims> & texture, const GLenum format, const GLenum type, const char * path,
uint8_t tgaFormat)
{
const auto size = texture.getSize();
const auto area = areaOf(size);
size_t dataSize = area * channels;
const size_t fileSize = dataSize + sizeof(TGAHead<channels>);
filesystem::fh out {path, O_RDWR | O_CREAT, 0660};
out.truncate(fileSize);
auto tga = out.mmap(fileSize, 0, PROT_WRITE, MAP_SHARED);
auto outTga = tga.get<TGAHead<channels>>();
*outTga = {
.format = tgaFormat,
.size = {size.x, (area / static_cast<size_t>(size.x))},
};
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glGetTextureImage(texture, 0, format, type, static_cast<GLsizei>(dataSize), outTga->data);
tga.msync(MS_ASYNC);
}
}
template<glm::length_t Dims>
requires(Dims >= 1 && Dims <= 3)
void
Impl::glTextureDims<Dims>::saveColour(const char * path) const
{
save<Dims, 3>(*this, GL_BGR, GL_UNSIGNED_BYTE, path, 2);
}
template<glm::length_t Dims>
requires(Dims >= 1 && Dims <= 3)
void
Impl::glTextureDims<Dims>::savePosition(const char * path) const
{
const auto size = getSize();
const auto area = areaOf(size);
size_t dataSize = area * sizeof(TGAHead<3>::PixelType);
const size_t fileSize = dataSize + sizeof(TGAHead<3>);
filesystem::fh out {path, O_RDWR | O_CREAT, 0660};
out.truncate(fileSize);
auto tga = out.mmap(fileSize, 0, PROT_WRITE, MAP_SHARED);
auto outTga = tga.get<TGAHead<3>>();
*outTga = {
.format = 2,
.size = {size.x, (area / static_cast<size_t>(size.x))},
};
glPixelStorei(GL_PACK_ALIGNMENT, 1);
std::vector<GlobalPosition3D> raw {area};
glGetTextureImage(
name, 0, GL_BGR_INTEGER, GL_INT, static_cast<GLsizei>(sizeof(GlobalPosition3D) * area), raw.data());
using Comp = GlobalPosition3D (*)(const GlobalPosition3D &, const GlobalPosition3D &);
auto notZero = std::views::filter([](const GlobalPosition3D & pos) {
return pos != GlobalPosition3D {};
});
const auto minPos = *std::ranges::fold_left_first(raw | notZero, static_cast<Comp>(&glm::min));
const auto maxPos = *std::ranges::fold_left_first(raw | notZero, static_cast<Comp>(&glm::max));
const auto rangePos = difference(maxPos, minPos);
std::ranges::transform(raw, outTga->data, [minPos, rangePos](const GlobalPosition3D & pos) {
return GlobalPosition3D(255.F * (difference(pos, minPos) / rangePos));
});
tga.msync(MS_ASYNC);
}
template<glm::length_t Dims>
requires(Dims >= 1 && Dims <= 3)
void
Impl::glTextureDims<Dims>::saveDepth(const char * path) const
{
save<Dims, 1>(*this, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, path, 3);
}
template<glm::length_t Dims>
requires(Dims >= 1 && Dims <= 3)
void
Impl::glTextureDims<Dims>::saveNormal(const char * path) const
{
save<Dims, 3>(*this, GL_BGR, GL_BYTE, path, 2);
}
template struct Impl::glTextureDims<1>;
template struct Impl::glTextureDims<2>;
template struct Impl::glTextureDims<3>;
|