Skip to content

Commit 60e2c7b

Browse files
committed
Track created textures
1 parent 136e7bd commit 60e2c7b

File tree

6 files changed

+58
-5
lines changed

6 files changed

+58
-5
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ option(GL_VALIDATION_LAYER_BUILD_TESTS "Build tests for the OpenGL Validation La
88
add_library(gl_validation_layer
99
src/context.cpp
1010
src/shader.cpp
11+
src/texture.cpp
1112
include/gl_layer/context.h
1213
include/gl_layer/private/context.h
1314
include/gl_layer/private/types.h

include/gl_layer/private/context.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ class Context {
2424
void glUseProgram(GLuint program);
2525
void glDeleteProgram(GLuint program);
2626

27+
void glGenTextures(GLsizei count, GLuint* handles);
28+
void glCreateTextures(GLenum target, GLsizei count, GLuint* handles);
29+
void glDeleteTextures(GLsizei count, GLuint* handles);
30+
2731
void validate_program_bound(std::string_view func_name);
2832
bool validate_program_status(GLuint program);
2933

@@ -37,6 +41,7 @@ class Context {
3741

3842
std::unordered_map<GLuint, Shader> shaders{};
3943
std::unordered_map<GLuint, Program> programs{};
44+
std::unordered_map<GLuint, Texture> textures{};
4045

4146
template<typename... Args>
4247
void output_fmt(const char* fmt, Args&& ... args) {

include/gl_layer/private/types.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ struct Program {
8989

9090
// Represents a texture created by glGenTextures()
9191
struct Texture {
92+
unsigned int handle {};
9293
// Texture target, either as specified by first glBindTexture() call, or by
9394
// glCreateTextures().
94-
GLTextureTarget target = GLTextureTarget::NO_TEXTURE_TARGET;
95+
GLTextureTarget target {};
9596
};
9697

9798
}

src/context.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ void gl_layer_callback(const char* name_c, void* func_ptr, int num_args, ...) {
9999
} else if (is_func(name, "glLinkProgram")) {
100100
auto program = va_arg(args, GLuint);
101101
gl_layer::g_context->glLinkProgram(program);
102+
} else if (is_func(name, "glGenTextures")) {
103+
auto count = va_arg(args, GLsizei);
104+
auto textures = va_arg(args, GLuint*);
105+
gl_layer::g_context->glGenTextures(count, textures);
106+
} else if (is_func(name, "glCreateTextures")) {
107+
auto target = va_arg(args, GLenum);
108+
auto count = va_arg(args, GLsizei);
109+
auto textures = va_arg(args, GLuint*);
110+
gl_layer::g_context->glCreateTextures(target, count, textures);
111+
} else if (is_func(name, "glDeleteTextures")) {
112+
auto count = va_arg(args, GLsizei);
113+
auto textures = va_arg(args, GLuint*);
114+
gl_layer::g_context->glDeleteTextures(count, textures);
102115
}
103116

104117
va_end(args);

src/texture.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <gl_layer/private/context.h>
2+
3+
namespace gl_layer {
4+
5+
void Context::glGenTextures(GLsizei count, GLuint* handles) {
6+
for (GLsizei i = 0; i < count; ++i) {
7+
textures.emplace(handles[i], Texture {
8+
handles[i],
9+
NO_TEXTURE_TARGET
10+
});
11+
}
12+
}
13+
14+
void Context::glCreateTextures(GLenum target, GLsizei count, GLuint* handles) {
15+
for (GLsizei i = 0; i < count; ++i) {
16+
textures.emplace(handles[i], Texture {
17+
handles[i],
18+
static_cast<GLTextureTarget>(target)
19+
});
20+
}
21+
}
22+
23+
void Context::glDeleteTextures(GLsizei count, GLuint* handles) {
24+
for (GLsizei i = 0; i < count; ++i) {
25+
textures.erase(handles[i]);
26+
}
27+
}
28+
29+
}

tests/main.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,22 @@ void main() {
157157
o_color = vec4(u_color, u_alpha);
158158
}
159159
)";
160+
GLuint tex[2];
161+
glGenTextures(2, tex);
162+
glBindTexture(GL_TEXTURE_2D, tex[0]);
163+
glDeleteTextures(2, tex);
160164

161-
unsigned int program = create_shader_from_files("shaders/vert.glsl", "shaders/frag.glsl");
162-
unsigned int program2 = create_shader(vtx_source2, frag_source2);
165+
// unsigned int program = create_shader_from_files("shaders/vert.glsl", "shaders/frag.glsl");
166+
// unsigned int program2 = create_shader(vtx_source2, frag_source2);
163167

164168
// Main application loop
165169
while(!glfwWindowShouldClose(window)) {
166170
glfwPollEvents();
167171
glClearColor(0, 0, 0, 1);
168172
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
169173

170-
glUseProgram(program);
171-
glUseProgram(program2);
174+
// glUseProgram(program);
175+
// glUseProgram(program2);
172176

173177
glfwSwapBuffers(window);
174178
}

0 commit comments

Comments
 (0)