1 | #include "ZydecoCommon.hpp" |
2 | #include "GLTexture.hpp" |
3 |
|
4 |
|
5 | static Logger LOGGER("GLTexture"); |
6 |
|
7 |
|
8 | std::map<uint32_t, std::array<uint32_t, 3>> TEXTURE_FORMAT_LOOKUP = { |
9 | {GL_RGB32F, {GL_RGB, GL_FLOAT}}, |
10 | {GL_RGBA32F, {GL_RGBA, GL_FLOAT}}, |
11 | }; |
12 |
|
13 |
|
14 | GLTexture::GLTexture(uint32_t sized_format, void *data_source, uint32_t width, uint32_t height): |
15 | m_sizedFormat(sized_format), |
16 | m_baseFormat(TEXTURE_FORMAT_LOOKUP[sized_format][0]), |
17 | m_dataType(TEXTURE_FORMAT_LOOKUP[sized_format][1]) |
18 | { |
19 | glGenTextures(1, &m_glTextureID); |
20 | SetDataSourceAndReload(data_source, width, height); |
21 | assert(glIsTexture(m_glTextureID) == GL_TRUE); |
22 | assert(m_baseFormat == GL_RGBA); |
23 | assert(m_sizedFormat == GL_RGBA32F); |
24 | } |
25 |
|
26 | GLTexture::~GLTexture() |
27 | { |
28 | glDeleteTextures(1, &m_glTextureID); |
29 | } |
30 |
|
31 | uint32_t GLTexture::GetID() |
32 | { |
33 | return m_glTextureID; |
34 | } |
35 |
|
36 | void GLTexture::SetDataSourceAndReload(void *data_source, int width, int height) |
37 | { |
38 | m_pDataSource = data_source; |
39 | m_width = width; |
40 | m_height = height; |
41 | ReloadFromDataSource(); |
42 | } |
43 |
|
44 | void GLTexture::ReloadFromDataSource() |
45 | { |
46 | glBindTexture(GL_TEXTURE_2D, m_glTextureID); |
47 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); |
48 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); |
49 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
50 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
51 | glTexImage2D(GL_TEXTURE_2D, 0, m_sizedFormat, m_width, m_height, 0, m_baseFormat, m_dataType, m_pDataSource); |
52 | } |
53 |
|
54 | void GLTexture::BindAsTexture(uint64_t texture_unit) |
55 | { |
56 | glActiveTexture(GL_TEXTURE0 + texture_unit); |
57 | glBindTexture(GL_TEXTURE_2D, m_glTextureID); |
58 | } |
59 |
|
60 | void GLTexture::BindAsImage(uint64_t texture_unit) |
61 | { |
62 | glActiveTexture(GL_TEXTURE0); |
63 | glBindImageTexture(texture_unit, m_glTextureID, 0, false, 0, GL_READ_WRITE, m_sizedFormat); |
64 | } |
65 |
|