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
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
|
#include "texture.h"
#include "shader.h"
#include "core/log.h"
#include <glad/glad.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// this file targets OpenGL 4.1 (the maximum macOS exposes). it uses the
// classic bind-based texture API rather than 4.5 Direct State Access
// (glCreateTextures / glTextureStorage2D / glTextureParameteri / glBindTextureUnit),
// none of which exist on macOS. it also hosts the single STB_IMAGE_IMPLEMENTATION
// for the whole build (the Vulkan backend links stbi_loadf from here too).
namespace Donut
{
Texture2D::Texture2D(uint32_t width, uint32_t height)
: m_width(width), m_height(height)
{
m_internal_format = GL_RGBA8;
m_data_format = GL_RGBA;
glGenTextures(1, &m_renderer_id);
glBindTexture(GL_TEXTURE_2D, m_renderer_id);
glTexImage2D(GL_TEXTURE_2D, 0, m_internal_format, m_width, m_height, 0, m_data_format, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
}
Texture2D::Texture2D(const std::string& path)
: m_path(path)
{
m_width = 1;
m_height = 1;
m_internal_format = GL_RGBA8;
m_data_format = GL_RGBA;
glGenTextures(1, &m_renderer_id);
glBindTexture(GL_TEXTURE_2D, m_renderer_id);
glTexImage2D(GL_TEXTURE_2D, 0, m_internal_format, m_width, m_height, 0, m_data_format, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
uint32_t white_pixel = 0xFFFFFFFF;
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_width, m_height, m_data_format, GL_UNSIGNED_BYTE, &white_pixel);
DONUT_INFO("Created default texture (stb_image not available for loading: ", path, ")");
}
Texture2D::~Texture2D()
{
glDeleteTextures(1, &m_renderer_id);
}
auto Texture2D::set_data(void* data, uint32_t size) -> void
{
uint32_t bpp = m_data_format == GL_RGBA ? 4 : 3;
if (size != m_width * m_height * bpp)
{
DONUT_ERROR("Data must be entire texture!");
return;
}
glBindTexture(GL_TEXTURE_2D, m_renderer_id);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_width, m_height, m_data_format, GL_UNSIGNED_BYTE, data);
}
auto Texture2D::bind(uint32_t slot) const -> void
{
glActiveTexture(GL_TEXTURE0 + slot);
glBindTexture(GL_TEXTURE_2D, m_renderer_id);
}
auto Texture2D::bind_as_image(uint32_t slot, bool read_only) const -> void
{
// image load/store is OpenGL 4.2 and unavailable on macOS. guard the
// function pointer so this degrades to a no-op instead of crashing.
if (glBindImageTexture == nullptr)
return;
GLenum access = read_only ? GL_READ_ONLY : GL_WRITE_ONLY;
glBindImageTexture(slot, m_renderer_id, 0, GL_FALSE, 0, access, m_internal_format);
}
CubemapTexture::CubemapTexture(uint32_t width, uint32_t height)
: m_width(width), m_height(height)
{
m_internal_format = GL_RGBA16F;
m_data_format = GL_RGBA;
glGenTextures(1, &m_renderer_id);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_renderer_id);
for (uint32_t i = 0; i < 6; ++i)
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, m_internal_format, m_width, m_height, 0, m_data_format, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
}
CubemapTexture::CubemapTexture(const std::string& path)
: m_path(path)
{
m_width = 1024;
m_height = 1024;
m_internal_format = GL_RGBA16F;
m_data_format = GL_RGBA;
glGenTextures(1, &m_renderer_id);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_renderer_id);
for (uint32_t i = 0; i < 6; ++i)
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, m_internal_format, m_width, m_height, 0, m_data_format, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
LoadHDRI(path);
}
CubemapTexture::~CubemapTexture()
{
glDeleteTextures(1, &m_renderer_id);
}
auto CubemapTexture::LoadHDRI(const std::string& path) -> void
{
stbi_set_flip_vertically_on_load(true);
int width, height, channels;
float* hdr_data = stbi_loadf(path.c_str(), &width, &height, &channels, 3);
if (!hdr_data)
{
DONUT_ERROR("Failed to load HDRI: {}", path);
float default_sky[6 * 4] =
{
0.5f, 0.7f, 1.0f, 1.0f, // right
0.5f, 0.7f, 1.0f, 1.0f, // left
0.5f, 0.7f, 1.0f, 1.0f, // top
0.5f, 0.7f, 1.0f, 1.0f, // bottom
0.5f, 0.7f, 1.0f, 1.0f, // front
0.5f, 0.7f, 1.0f, 1.0f // back
};
glBindTexture(GL_TEXTURE_CUBE_MAP, m_renderer_id);
for (int i = 0; i < 6; ++i)
glTexSubImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, 0, 0, 1, 1, GL_RGBA, GL_FLOAT, &default_sky[i * 4]);
return;
}
convert_equirectangular_to_cubemap(hdr_data, width, height);
stbi_image_free(hdr_data);
DONUT_INFO("Successfully loaded HDRI: {} ({}x{})", path, width, height);
}
auto CubemapTexture::convert_equirectangular_to_cubemap(float* hdr_data, int width, int height) -> void
{
uint32_t capture_fbo, capture_rbo;
glGenFramebuffers(1, &capture_fbo);
glGenRenderbuffers(1, &capture_rbo);
glBindFramebuffer(GL_FRAMEBUFFER, capture_fbo);
glBindRenderbuffer(GL_RENDERBUFFER, capture_rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, m_width, m_height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, capture_rbo);
uint32_t hdr_texture;
glGenTextures(1, &hdr_texture);
glBindTexture(GL_TEXTURE_2D, hdr_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_FLOAT, hdr_data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
auto equirect_shader = Shader::create("assets/shaders/EquirectToCubemap.glsl");
if (!equirect_shader)
{
DONUT_ERROR("Failed to create equirectangular to cubemap shader");
return;
}
uint32_t shader_program = equirect_shader->get_renderer_id();
float vertices[] =
{
-1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f,
1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f
};
uint32_t cube_vao, cube_vbo;
glGenVertexArrays(1, &cube_vao);
glGenBuffers(1, &cube_vbo);
glBindVertexArray(cube_vao);
glBindBuffer(GL_ARRAY_BUFFER, cube_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glm::mat4 capture_projection = glm::perspective(glm::radians(90.0f), 1.0f, 0.1f, 10.0f);
glm::mat4 capture_views[] =
{
glm::lookAt(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3( 1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)),
glm::lookAt(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)),
glm::lookAt(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3( 0.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)),
glm::lookAt(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3( 0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, -1.0f)),
glm::lookAt(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3( 0.0f, 0.0f, 1.0f), glm::vec3(0.0f, -1.0f, 0.0f)),
glm::lookAt(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3( 0.0f, 0.0f, -1.0f), glm::vec3(0.0f, -1.0f, 0.0f))
};
glUseProgram(shader_program);
glUniform1i(glGetUniformLocation(shader_program, "u_EquirectangularMap"), 0);
// EquirectToCubemap is authored in Slang (row-major); transpose glm's
// column-major matrices on upload (GL_TRUE) to match.
glUniformMatrix4fv(glGetUniformLocation(shader_program, "u_Projection"), 1, GL_TRUE, &capture_projection[0][0]);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, hdr_texture);
glViewport(0, 0, m_width, m_height);
glBindFramebuffer(GL_FRAMEBUFFER, capture_fbo);
for (unsigned int i = 0; i < 6; ++i)
{
glUniformMatrix4fv(glGetUniformLocation(shader_program, "u_View"), 1, GL_TRUE, &capture_views[i][0][0]);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, m_renderer_id, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArray(cube_vao);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
glBindVertexArray(0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDeleteVertexArrays(1, &cube_vao);
glDeleteBuffers(1, &cube_vbo);
glDeleteTextures(1, &hdr_texture);
glDeleteFramebuffers(1, &capture_fbo);
glDeleteRenderbuffers(1, &capture_rbo);
}
auto CubemapTexture::set_data(void* data, uint32_t size) -> void
{
DONUT_WARN("set_data not implemented for cubemaps");
}
auto CubemapTexture::bind(uint32_t slot) const -> void
{
glActiveTexture(GL_TEXTURE0 + slot);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_renderer_id);
}
auto CubemapTexture::bind_as_image(uint32_t slot, bool read_only) const -> void
{
if (glBindImageTexture == nullptr)
return;
GLenum access = read_only ? GL_READ_ONLY : GL_WRITE_ONLY;
glBindImageTexture(slot, m_renderer_id, 0, GL_TRUE, 0, access, m_internal_format);
}
auto Texture2D::create(uint32_t width, uint32_t height) -> Ref<Texture2D>
{
return create_ref<Texture2D>(width, height);
}
auto Texture2D::create(const std::string& path) -> Ref<Texture2D>
{
return create_ref<Texture2D>(path);
}
auto CubemapTexture::create(uint32_t width, uint32_t height) -> Ref<CubemapTexture>
{
return create_ref<CubemapTexture>(width, height);
}
auto CubemapTexture::create_from_hdri(const std::string& path) -> Ref<CubemapTexture>
{
return create_ref<CubemapTexture>(path);
}
};
|