texture.cpp 3.14 KB
Newer Older
Johannes Braun's avatar
Johannes Braun committed
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
#include "texture.h"

namespace glare::core
{

	Texture::Texture(Target target, StoreFormat store_format) 
		: m_target(target), m_handle(std::move(target)), m_format(store_format)
	{ }

	const Texture::Parameters& Texture::parameters() const
	{
		return m_latest_parameters;
	}

	void Texture::apply(Parameters parameters)
	{
		m_latest_parameters = std::move(parameters);

		float anisotropy = m_latest_parameters.anisotropy;
		if (anisotropy == -1.f) gl::getFloatv(gl::GetParameter::eMaxTextureMaxAnisotropy, &anisotropy);
		gl::textureParameter(m_handle, gl::TextureParameter::eMaxAnisotropy, anisotropy);

		gl::textureParameter(m_handle, gl::TextureParameter::eWrapR, m_latest_parameters.wrap_r);
		gl::textureParameter(m_handle, gl::TextureParameter::eWrapS, m_latest_parameters.wrap_s);
		gl::textureParameter(m_handle, gl::TextureParameter::eWrapT, m_latest_parameters.wrap_t);
		gl::textureParameter(m_handle, gl::TextureParameter::eMagFilter, m_latest_parameters.filter_mag);
		gl::textureParameter(m_handle, gl::TextureParameter::eMinFilter, m_latest_parameters.filter_min);
		gl::textureParameter(m_handle, gl::TextureParameter::eCompareMode, m_latest_parameters.compare_mode);
		gl::textureParameter(m_handle, gl::TextureParameter::eCompareFunc, m_latest_parameters.compare_func);

		gl::textureParameter(m_handle, gl::TextureParameter::eBaseLevel, m_latest_parameters.base_level);
		gl::textureParameter(m_handle, gl::TextureParameter::eMaxLevel, m_latest_parameters.max_level);

		gl::generateTextureMipmap(m_handle);
	}

	unsigned Texture::id() const
	{
		return m_handle;
	}

	bool Texture::textureResident() const
	{
		return m_texture_address != 0 && gl::isTextureHandleResident(m_texture_address);
	}

	bool Texture::imageResident() const
	{
		return m_image_address != 0 && gl::isImageHandleResident(m_image_address);
	}

	uint64_t Texture::textureAddress() const
	{
		if (!textureResident())
		{
			m_texture_address = gl::getTextureHandle(m_handle);
			gl::makeTextureHandleResident(m_texture_address);
		}
		assert(textureResident());
		return m_texture_address;
	}

	uint64_t Texture::imageAddress(gl::Access access) const
	{
		if (!imageResident())
		{
			m_image_address = gl::getImageHandle(m_handle, 0, false, 0, gl::ImageUnitFormat(m_format));
			gl::makeImageHandleResident(m_image_address, access);
		}
		assert(imageResident());
		return m_image_address;
	}

	int Texture::samples() const
	{
		int s;
		gl::getTextureLevelParameteriv(m_handle, 0, gl::TextureLevelParameter::eSamples, &s);
		return s;
	}

	gl::Type Texture::type() const
	{
		gl::Type type;
		gl::getInternalFormatParameter(m_target, m_format, gl::TextureInternalFormatParameter::eTextureImageType, reinterpret_cast<int*>(&type));
		return type;
	}

	int Texture::width() const
	{
		int width;
		gl::getTextureLevelParameteriv(m_handle, 0, gl::TextureLevelParameter::eWidth, &width);
		return width;
	}

	int Texture::height() const
	{
		int height;
		gl::getTextureLevelParameteriv(m_handle, 0, gl::TextureLevelParameter::eHeight, &height);
		return height;
	}

	int Texture::depth() const
	{
		int depth;
		gl::getTextureLevelParameteriv(m_handle, 0, gl::TextureLevelParameter::eDepth, &depth);
		return depth;
	}
}