-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexture_linux.cpp
More file actions
173 lines (149 loc) · 4.56 KB
/
Copy pathtexture_linux.cpp
File metadata and controls
173 lines (149 loc) · 4.56 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
#include "detect_os.hpp"
#ifdef PAZ_LINUX
#include "PAZ_Graphics"
#include "util_linux.hpp"
#include "internal_data.hpp"
#include "common.hpp"
#include "gl_core_4_1.h"
#define CASE(a, b) case paz::WrapMode::a: return GL_##b;
static GLint wrap_mode(paz::WrapMode m)
{
switch(m)
{
CASE(Repeat, REPEAT)
CASE(MirrorRepeat, MIRRORED_REPEAT)
CASE(ClampToEdge, CLAMP_TO_EDGE)
CASE(ClampToZero, CLAMP_TO_BORDER)
}
throw std::logic_error("Invalid texture wrapping mode requested.");
}
paz::Texture::Data::~Data()
{
if(_isRenderTarget)
{
unregister_target(this);
}
if(_id)
{
glDeleteTextures(1, &_id);
}
}
paz::Texture::Texture()
{
initialize();
}
paz::Texture::Texture(TextureFormat format, int width, int height, MinMagFilter
minFilter, MinMagFilter magFilter, MipmapFilter mipFilter, WrapMode wrapS,
WrapMode wrapT)
{
initialize();
_data = std::make_shared<Data>();
_data->_width = width;
_data->_height = height;
_data->_format = format;
_data->_minFilter = minFilter;
_data->_magFilter = magFilter;
_data->_mipFilter = mipFilter;
_data->_wrapS = wrapS;
_data->_wrapT = wrapT;
_data->init();
}
paz::Texture::Texture(TextureFormat format, int width, int height, const void*
data, MinMagFilter minFilter, MinMagFilter magFilter, MipmapFilter
mipFilter, WrapMode wrapS, WrapMode wrapT)
{
initialize();
_data = std::make_shared<Data>();
_data->_width = width;
_data->_height = height;
_data->_format = format;
_data->_minFilter = minFilter;
_data->_magFilter = magFilter;
_data->_mipFilter = mipFilter;
_data->_wrapS = wrapS;
_data->_wrapT = wrapT;
_data->init(data);
}
paz::Texture::Texture(const Image& image, MinMagFilter minFilter, MinMagFilter
magFilter, MipmapFilter mipFilter, WrapMode wrapS, WrapMode wrapT)
{
initialize();
_data = std::make_shared<Data>();
_data->_width = image.width();
_data->_height = image.height();
_data->_format = static_cast<TextureFormat>(image.format());
_data->_minFilter = minFilter;
_data->_magFilter = magFilter;
_data->_mipFilter = mipFilter;
_data->_wrapS = wrapS;
_data->_wrapT = wrapT;
_data->init(image.bytes().data());
}
paz::Texture::Texture(RenderTarget&& target) : _data(std::move(target._data)) {}
void paz::Texture::Data::init(const void* data)
{
// Textures not for rendering are static (for now).
if(!_isRenderTarget && !data)
{
throw std::logic_error("Cannot initialize static texture without data."
);
}
// This is because of Metal restrictions.
if((_format == TextureFormat::Depth16UNorm || _format == TextureFormat::
Depth32Float) && _mipFilter != MipmapFilter::None)
{
throw std::runtime_error("Depth/stencil textures do not support mipmapp"
"ing.");
}
glGenTextures(1, &_id);
glBindTexture(GL_TEXTURE_2D, _id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, gl_internal_format(_format), _width, _height,
0, gl_format(_format), gl_type(_format), data);
if(_mipFilter == MipmapFilter::Anisotropic)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, Window::
MaxAnisotropy());
}
else
{
const auto filters = min_mag_filter(_minFilter, _magFilter, _mipFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filters.first);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filters.second);
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_mode(_wrapS));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_mode(_wrapT));
ensureMipmaps();
}
void paz::Texture::Data::resize(int width, int height)
{
if(_scale)
{
_width = _scale*width;
_height = _scale*height;
glBindTexture(GL_TEXTURE_2D, _id);
glTexImage2D(GL_TEXTURE_2D, 0, gl_internal_format(_format), _width,
_height, 0, gl_format(_format), gl_type(_format), nullptr);
ensureMipmaps();
}
}
void paz::Texture::Data::ensureMipmaps()
{
if(_mipFilter != MipmapFilter::None)
{
glBindTexture(GL_TEXTURE_2D, _id);
glGenerateMipmap(GL_TEXTURE_2D);
}
}
int paz::Texture::width() const
{
return _data ? _data->_width : 0;
}
int paz::Texture::height() const
{
return _data ? _data->_height : 0;
}
#endif