texture.h 16.4 KB
Newer Older
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
#ifndef INCLUDE_TEXTURE_H
#define INCLUDE_TEXTURE_H

#include <cassert>

#include "core/res/image.h"
#include "util/opengl.h"

namespace glare::core
{
	struct Texture
	{
		using Target = gl::TextureType;
		using SubTarget = gl::ImageTarget;
		using StoreFormat = gl::TextureInternalFormat;
		using SubTargetMultisample = gl::ImageTargetMultisample;

		struct Parameters
		{
			gl::TextureWrapMode wrap_r = gl::TextureWrapMode::eRepeat;
			gl::TextureWrapMode wrap_s = gl::TextureWrapMode::eRepeat;
			gl::TextureWrapMode wrap_t = gl::TextureWrapMode::eRepeat;

			gl::TextureFilterMin filter_min = gl::TextureFilterMin::eLinearMipmapLinear;
			gl::TextureFilterMag filter_mag = gl::TextureFilterMag::eLinear;

			gl::TextureCompareMode compare_mode = gl::TextureCompareMode::eRToTexture;
28
29
30
31
32
			gl::CompareFunc compare_func = gl::CompareFunc::eLess;

			int base_level = 0;
			int max_level = 8;

33
34
35
36
			float anisotropy = -1.f;
		};

		// General initialization
Johannes Braun's avatar
Johannes Braun committed
37
		Texture(Target target, StoreFormat store_format);
38
39

		// Direct initialization for either 1D, 2D or 3D target.
Johannes Braun's avatar
Johannes Braun committed
40
		template<typename T> Texture(const Image<T>& image);
41

Johannes Braun's avatar
Johannes Braun committed
42
		template<typename T> Image<T> get() const;
43

Johannes Braun's avatar
Johannes Braun committed
44
45
		template<typename T> void set(SubTargetMultisample t, const Image<T>& image);
		template<typename T> void set(SubTargetMultisample t, const Image<T>& image, Parameters parameters);
46

Johannes Braun's avatar
Johannes Braun committed
47
48
49
50
		template<typename T> void set(SubTarget t, const Image<T>& image);
		template<typename T> void set(SubTarget t, const Image<T>& image, bool no_apply);
		template<typename T> void set(SubTarget t, const Image<T>& image, Parameters parameters);
		template<typename T> void set(SubTarget t, int level, const Image<T>& image, Parameters parameters, bool no_apply);
51

Johannes Braun's avatar
Johannes Braun committed
52
		void apply(Parameters parameters);
53

Johannes Braun's avatar
Johannes Braun committed
54
55
56
57
		bool textureResident() const;
		bool imageResident() const;
		uint64_t textureAddress() const;
		uint64_t imageAddress(gl::Access access) const;
58

Johannes Braun's avatar
Johannes Braun committed
59
60
61
62
63
64
65
		const Parameters& parameters() const;
		unsigned id() const;
		gl::Type type() const;
		int width() const;
		int height() const;
		int depth() const;
		int samples() const;
66
67
68
69
70

	private:
		template<typename From, typename To>
		static To clampConvert(const void* data, int index)
		{
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
			if constexpr(std::is_same_v<To, From>)
			{
				return reinterpret_cast<const From*>(data)[index];
			}
			else if constexpr(std::is_same_v<To, float>)
			{
				return static_cast<To>(glm::clamp(static_cast<float>(reinterpret_cast<const From*>(data)[index]), 0.f, 1.f) / static_cast<float>(std::numeric_limits<From>::max()));
			}
			else if constexpr(std::is_same_v<From, float>)
			{
				return static_cast<To>(glm::clamp(static_cast<float>(reinterpret_cast<const From*>(data)[index]), 0.f, 1.f) * static_cast<float>(std::numeric_limits<To>::max()));
			}
			else
			{
				return static_cast<To>(glm::clamp(conversionFactor<From, To>() * reinterpret_cast<const From*>(data)[index], static_cast<float>(std::numeric_limits<From>::lowest()), static_cast<float>(std::numeric_limits<From>::max())));
			}
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
		}

		template<typename T>
		static StoreFormat getFormat(const Image<T>& image)
		{
			switch (image.components())
			{
			case 1:
				if constexpr(std::is_same_v<T, float>) return gl::TextureInternalFormat::eR32Float;
				else if constexpr(std::is_same_v<T, uint8_t>) return gl::TextureInternalFormat::eR8;
				else if constexpr(std::is_same_v<T, uint16_t>) return gl::TextureInternalFormat::eR16UInt;
				else if constexpr(std::is_same_v<T, uint32_t>) return gl::TextureInternalFormat::eR32UInt;
				else if constexpr(std::is_same_v<T, int8_t>) return gl::TextureInternalFormat::eR8Int;
				else if constexpr(std::is_same_v<T, int16_t>) return gl::TextureInternalFormat::eR16Int;
				else if constexpr(std::is_same_v<T, int32_t>) return gl::TextureInternalFormat::eR32Int;

			case 2:
				if constexpr(std::is_same_v<T, float>) return gl::TextureInternalFormat::eRG32Float;
				else if constexpr(std::is_same_v<T, uint8_t>) return gl::TextureInternalFormat::eRG8;
				else if constexpr(std::is_same_v<T, uint16_t>) return gl::TextureInternalFormat::eRG16UInt;
				else if constexpr(std::is_same_v<T, uint32_t>) return gl::TextureInternalFormat::eRG32UInt;
				else if constexpr(std::is_same_v<T, int8_t>) return gl::TextureInternalFormat::eRG8Int;
				else if constexpr(std::is_same_v<T, int16_t>) return gl::TextureInternalFormat::eRG16Int;
				else if constexpr(std::is_same_v<T, int32_t>) return gl::TextureInternalFormat::eRG32Int;

			case 3:
				if constexpr(std::is_same_v<T, float>) return gl::TextureInternalFormat::eRGB32Float;
				else if constexpr(std::is_same_v<T, uint8_t>) return gl::TextureInternalFormat::eRGB8;
				else if constexpr(std::is_same_v<T, uint16_t>) return gl::TextureInternalFormat::eRGB16UInt;
				else if constexpr(std::is_same_v<T, uint32_t>) return gl::TextureInternalFormat::eRGB32UInt;
				else if constexpr(std::is_same_v<T, int8_t>) return gl::TextureInternalFormat::eRGB8Int;
				else if constexpr(std::is_same_v<T, int16_t>) return gl::TextureInternalFormat::eRGB16Int;
				else if constexpr(std::is_same_v<T, int32_t>) return gl::TextureInternalFormat::eRGB32Int;

			case 4:
				if constexpr(std::is_same_v<T, float>) return gl::TextureInternalFormat::eRGBA32Float;
				else if constexpr(std::is_same_v<T, uint8_t>) return gl::TextureInternalFormat::eRGBA8;
				else if constexpr(std::is_same_v<T, uint16_t>) return gl::TextureInternalFormat::eRGBA16UInt;
				else if constexpr(std::is_same_v<T, uint32_t>) return gl::TextureInternalFormat::eRGBA32UInt;
				else if constexpr(std::is_same_v<T, int8_t>) return gl::TextureInternalFormat::eRGBA8Int;
				else if constexpr(std::is_same_v<T, int16_t>) return gl::TextureInternalFormat::eRGBA16Int;
				else if constexpr(std::is_same_v<T, int32_t>) return gl::TextureInternalFormat::eRGBA32Int;

				throw std::invalid_argument("Using images other than 32 bit float and integral types is not yet implemented");
			default:
				throw std::invalid_argument("Malformed image! Component count not in range 1 to 4.");
			}
		}

		int m_components = -1;
		Target m_target;
		StoreFormat m_format;
		Parameters m_latest_parameters;

Johannes Braun's avatar
Johannes Braun committed
141
142
143
144
		int m_width = 0;
		int m_height = 0;
		int m_depth = 0;

145
146
147
148
		gl::handle::texture m_handle;
		mutable uint64_t m_texture_address = 0;
		mutable uint64_t m_image_address = 0;
	};
Johannes Braun's avatar
Johannes Braun committed
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390

	template<typename T> Texture::Texture(const Image<T>& image)
		: Texture([&image]() {
		switch (image.dimensions())
		{
		case 1: return Target::e1D;
		case 2: return Target::e2D;
		case 3: return Target::e3D;
		default: return Target::e2D;
		}
	}(), getFormat(image))
	{
		switch (m_target)
		{
		case Target::e1D:
			set(SubTarget::e1D, image, m_latest_parameters);
			break;
		case Target::e2D:
			set(SubTarget::e2D, image, m_latest_parameters);
			break;
		case Target::e3D:
			set(SubTarget::e3D, image, m_latest_parameters);
			break;
		default: throw std::invalid_argument("Target not defined as 1D, 2D or 3D. How exactly did you manage to come to this point???");
		}
	}
	
	template<typename T> Image<T> Texture::get() const
	{
		if (m_target != gl::TextureType::e2D || m_components == -1)
		{
			throw std::invalid_argument("You cannot download this texture.");
		}

		int texture_width = width();
		int texture_height = height();
		int components = m_components;
		auto texture_type = type();

		gl::TextureFormat texture_format = [](int components, gl::Type type)
		{
			switch (components)
			{
			case 1: if (type == gl::Type::eInt || type == gl::Type::eUInt) return gl::TextureFormat::eRedInt;
					else return gl::TextureFormat::eRed;
			case 2:  if (type == gl::Type::eInt || type == gl::Type::eUInt) return gl::TextureFormat::eRGInt;
					 else return gl::TextureFormat::eRG;
			case 3: if (type == gl::Type::eInt || type == gl::Type::eUInt) return gl::TextureFormat::eRGBInt;
					else return gl::TextureFormat::eRGB;
			case 4: if (type == gl::Type::eInt || type == gl::Type::eUInt) return gl::TextureFormat::eRGBAInt;
					else return gl::TextureFormat::eRGBA;
			default: throw std::invalid_argument("Texture format not valid.");
			}
		}(components, texture_type);


		switch (texture_type)
		{
		case gl::Type::eByte:
		{
			Image<int8_t> image({ texture_width, texture_height }, components, std::vector<int8_t>(texture_width * texture_height * components));
			gl::getTextureImage(m_handle, 0, texture_format, texture_type, image.data().size() * sizeof(int8_t), const_cast<int8_t*>(image.data().data()));
			return image;
		}
		case gl::Type::eUByte:
		case gl::Type::eUInt_8_8_8_8:
		case gl::Type::eUInt_8_8_8_8_rev:
		{
			Image<uint8_t> image({ texture_width, texture_height }, components, std::vector<uint8_t>(texture_width * texture_height * components));
			gl::getTextureImage(m_handle, 0, texture_format, texture_type, image.data().size() * sizeof(uint8_t), const_cast<uint8_t*>(image.data().data()));
			return image;
		}
		case gl::Type::eShort:
		{
			Image<int16_t> image({ texture_width, texture_height }, components, std::vector<int16_t>(texture_width * texture_height * components));
			gl::getTextureImage(m_handle, 0, texture_format, texture_type, image.data().size() * sizeof(int16_t), const_cast<int16_t*>(image.data().data()));
			return image;
		}
		case gl::Type::eUShort:
		{
			Image<uint16_t> image({ texture_width, texture_height }, components, std::vector<uint16_t>(texture_width * texture_height * components));
			gl::getTextureImage(m_handle, 0, texture_format, texture_type, image.data().size() * sizeof(uint16_t), const_cast<uint16_t*>(image.data().data()));
			return image;
		}
		case gl::Type::eInt:
		{
			Image<int32_t> image({ texture_width, texture_height }, components, std::vector<int32_t>(texture_width * texture_height * components));
			gl::getTextureImage(m_handle, 0, texture_format, texture_type, image.data().size() * sizeof(int32_t), const_cast<int32_t*>(image.data().data()));
			return image;
		}
		case gl::Type::eUInt:
		{
			Image<uint32_t> image({ texture_width, texture_height }, components, std::vector<uint32_t>(texture_width * texture_height * components));
			gl::getTextureImage(m_handle, 0, texture_format, texture_type, image.data().size() * sizeof(uint32_t), const_cast<uint32_t*>(image.data().data()));
			return image;
		}
		case gl::Type::eFloat:
		{
			Image<float> image({ texture_width, texture_height }, components, std::vector<float>(texture_width * texture_height * components));
			gl::getTextureImage(m_handle, 0, texture_format, texture_type, image.data().size() * sizeof(float), const_cast<float*>(image.data().data()));
			return image;
		}
		default: throw std::invalid_argument("Texture format not valid.");
		}

		//int type_size = static_cast<int>([](gl::Type type)
		//{
		//	switch (type)
		//	{
		//	case gl::Type::eByte:
		//		return sizeof(int8_t);
		//	case gl::Type::eUByte:
		//	case gl::Type::eUInt_8_8_8_8:
		//	case gl::Type::eUInt_8_8_8_8_rev:
		//		return sizeof(uint8_t);
		//	case gl::Type::eInt:
		//		return sizeof(int32_t);
		//	case gl::Type::eUInt:
		//		return sizeof(uint32_t);
		//	case gl::Type::eFloat:
		//		return sizeof(float);
		//	default: throw std::invalid_argument("Texture format not valid.");
		//	}
		//}(texture_type));

		//gl::TextureFormat texture_format = [](int components, gl::Type type)
		//{
		//	switch (components)
		//	{
		//	case 1: if (type == gl::Type::eInt || type == gl::Type::eUInt) return gl::TextureFormat::eRedInt;
		//			else return gl::TextureFormat::eRed;
		//	case 2:  if (type == gl::Type::eInt || type == gl::Type::eUInt) return gl::TextureFormat::eRGInt;
		//			 else return gl::TextureFormat::eRG;
		//	case 3: if (type == gl::Type::eInt || type == gl::Type::eUInt) return gl::TextureFormat::eRGBInt;
		//			else return gl::TextureFormat::eRGB;
		//	case 4: if (type == gl::Type::eInt || type == gl::Type::eUInt) return gl::TextureFormat::eRGBAInt;
		//			else return gl::TextureFormat::eRGBA;
		//	default: throw std::invalid_argument("Texture format not valid.");
		//	}
		//}(components, texture_type);

		//std::vector<uint8_t> data(texture_width * texture_height * components * type_size);
		//std::vector<T> converted(texture_width*texture_height*components);

		//gl::getTextureImage(m_handle, 0, texture_format, texture_type, data.size(), data.data());

		//for (int i = 0; i < texture_width*texture_height*components; ++i)
		//{
		//	switch (texture_type)
		//	{
		//	case gl::Type::eByte:
		//		converted[i] = clampConvert<int8_t, T>(data.data(), i);
		//		break;
		//	case gl::Type::eUByte:
		//	case gl::Type::eUInt_8_8_8_8:
		//	case gl::Type::eUInt_8_8_8_8_rev:
		//		converted[i] = clampConvert<uint8_t, T>(data.data(), i);
		//		break;
		//	case gl::Type::eShort:
		//		converted[i] = clampConvert<int16_t, T>(data.data(), i);
		//		break;
		//	case gl::Type::eUShort:
		//		converted[i] = clampConvert<uint16_t, T>(data.data(), i);
		//		break;
		//	case gl::Type::eInt:
		//		converted[i] = clampConvert<int32_t, T>(data.data(), i);// static_cast<T>(glm::clamp(conversionFactor<int32_t, T>() * reinterpret_cast<int32_t*>(data.data())[i], std::numeric_limits<int32_t>::lowest(), std::numeric_limits<int32_t>::max()));
		//		break;
		//	case gl::Type::eUInt:
		//		converted[i] = clampConvert<uint32_t, T>(data.data(), i);// static_cast<T>(glm::clamp(conversionFactor<uint32_t, T>() * reinterpret_cast<uint32_t*>(data.data())[i], std::numeric_limits<uint32_t>::lowest(), std::numeric_limits<uint32_t>::max()));
		//		break;
		//	case gl::Type::eFloat:
		//		converted[i] = clampConvert<float, T>(data.data(), i);
		//		break;
		//	default: throw std::invalid_argument("Texture format not valid.");
		//	}
		//}

		//return Image<T>({ texture_width, texture_height }, components, converted);
	}


	template<typename T> void Texture::set(SubTargetMultisample t, const Image<T>& image)
	{
		set(t, image, m_latest_parameters);
	}

	// up until the set methods, no openGL is needed yet.
	// Upload an image
	template<typename T> void Texture::set(SubTargetMultisample t, const Image<T>& image, Parameters parameters)
	{
		assert(image.samples() != 0);
		m_components = image.components();
		gl::pixelStorei(gl::PixelStoreAlignment::eUnpack, 1);
		gl::textureImage(m_handle, t, image.samples(), m_format, { image.width(), image.height(), image.depth() });
		apply(std::move(parameters));
	}

	template<typename T> void Texture::set(SubTarget t, const Image<T>& image)
	{
		set(t, image, m_latest_parameters);
	}

	template<typename T> void Texture::set(SubTarget t, const Image<T>& image, bool no_apply)
	{
		set(t, 0, image, m_latest_parameters, no_apply);
	}

	// up until the set methods, no openGL is needed yet.
	// Upload an image
	template<typename T> void Texture::set(SubTarget t, const Image<T>& image, Parameters parameters)
	{
		set(t, 0, image, std::move(parameters), false);
	}

	// Upload an image
	template<typename T> void Texture::set(SubTarget t, int level, const Image<T>& image, Parameters parameters, bool no_apply)
	{
		// Don't allow level parameter on MS textures.
		// images are multisampled if sample count is not 0.
		assert(image.samples() == 0);
		
		gl::TextureFormat texture_format = [](int components, StoreFormat format)
		{
			switch (components)
			{
			case 1: if constexpr(std::is_same_v<T, int32_t> || std::is_same_v<T, uint32_t>) return gl::TextureFormat::eRedInt;
					else return !(format == StoreFormat::eDepthComponent32Float || format == StoreFormat::eDepthComponent) ? gl::TextureFormat::eRed : gl::TextureFormat::eDepthComponent;
			case 2:  if constexpr(std::is_same_v<T, int32_t> || std::is_same_v<T, uint32_t>) return gl::TextureFormat::eRGInt;
					 else return !(format == StoreFormat::eDepthComponent32Float || format == StoreFormat::eDepthComponent) ? gl::TextureFormat::eRG : gl::TextureFormat::eDepthComponent;
			case 3: if constexpr(std::is_same_v<T, int32_t> || std::is_same_v<T, uint32_t>) return gl::TextureFormat::eRGBInt;
					else return !(format == StoreFormat::eDepthComponent32Float || format == StoreFormat::eDepthComponent) ? gl::TextureFormat::eRGB : gl::TextureFormat::eDepthComponent;
			case 4: if constexpr(std::is_same_v<T, int32_t> || std::is_same_v<T, uint32_t>) return gl::TextureFormat::eRGBAInt;
					else return !(format == StoreFormat::eDepthComponent32Float || format == StoreFormat::eDepthComponent) ? gl::TextureFormat::eRGBA : gl::TextureFormat::eDepthComponent;
			default: throw std::invalid_argument("Texture format not valid.");
			}
		}(image.components(), m_format);
		m_components = image.components();
		gl::pixelStorei(gl::PixelStoreAlignment::eUnpack, 1);
		gl::textureImage(m_handle, t, level, m_format, texture_format, { image.width(), image.height(), image.depth() }, image.data().empty() ? nullptr : image.data().data());
		if (!no_apply)
			apply(std::move(parameters));
	}
391
392
393
}

#endif //!INCLUDE_TEXTURE_H