Glad 2, same problem
#include <iostream>
#include <thread>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <glad\gl.h>
#include <GLFW/glfw3.h>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void free_context(GladGLContext* gl);
void render(GLFWwindow* window, GladGLContext* gl);
GladGLContext* create_context(GLFWwindow* window);
int fbw = 0;
int fbh = 0;
GladGLContext* mgl;
class CountDownLatch {
public:
explicit CountDownLatch(const unsigned int count) : m_count(count) { }
virtual ~CountDownLatch() = default;
void await(void) {
std::unique_lock<std::mutex> lock(m_mutex);
if (m_count > 0) {
m_cv.wait(lock, [this]() { return m_count == 0; });
}
}
template <class Rep, class Period>
bool await(const std::chrono::duration<Rep, Period>& timeout) {
std::unique_lock<std::mutex> lock(m_mutex);
bool result = true;
if (m_count > 0) {
result = m_cv.wait_for(lock, timeout, [this]() { return m_count == 0; });
}
return result;
}
void countDown(void) {
std::unique_lock<std::mutex> lock(m_mutex);
if (m_count > 0) {
m_count--;
m_cv.notify_all();
}
}
unsigned int getCount(void) {
std::unique_lock<std::mutex> lock(m_mutex);
return m_count;
}
protected:
std::mutex m_mutex;
std::condition_variable m_cv;
unsigned int m_count = 0;
};
CountDownLatch thread1latch = CountDownLatch(1);
void thread1(GLFWwindow* window) {
std::cout << "Thread1" << std::endl;
GladGLContext* gl = create_context(window);
GLuint id1;
GLuint id2;
gl->GenTextures(1,&id1);
gl->GenTextures(1,&id2);
gl->BindTexture(GL_TEXTURE_2D, id1);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gl->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
gl->BindTexture(GL_TEXTURE_2D, id2);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gl->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 3, 3, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
gl->CopyImageSubData(id1, GL_TEXTURE_2D, 0, 0, 0, 0, id2, GL_TEXTURE_2D, 0, 0, 0, 0, 2, 2, 1);
gl->DeleteTextures(1, &id1);
gl->DeleteTextures(1, &id2);
gl->Flush();
gl->Finish();
thread1latch.countDown();
free_context(gl);
}
int main() {
std::cout << "Hello" << std::endl;
glfwInit();
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
GLFWwindow* window = glfwCreateWindow(400,400,"test",nullptr,nullptr);
GLFWwindow* secondary = glfwCreateWindow(1,1,"unused",nullptr,window);
glfwGetFramebufferSize(window, &fbw,&fbh);
glfwSetFramebufferSizeCallback(window,framebuffer_size_callback);
mgl = create_context(window);
glfwShowWindow(window);
std::thread th (thread1, secondary);
thread1latch.await();
//glfwMakeContextCurrent(glfwGetCurrentContext());
while (!glfwWindowShouldClose(window)) {
glfwWaitEventsTimeout(0.1);
glfwPollEvents();
render(window, mgl);
}
th.join();
free_context(mgl);
glfwDestroyWindow(secondary);
glfwDestroyWindow(window);
glfwTerminate();
}
void render(GLFWwindow* window,GladGLContext* gl) {
gl->Viewport(0, 0, fbw, fbh);
gl->ClearColor(1, 0, 0, 1);
gl->Clear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
fbw = width;
fbh = height;
render(window, mgl);
}
void free_context(GladGLContext* gl) {
free(gl);
glfwMakeContextCurrent(NULL);
}
GladGLContext* create_context(GLFWwindow* window) {
glfwMakeContextCurrent(window);
GladGLContext* context = (GladGLContext*) malloc(sizeof(GladGLContext));
if (!context)return NULL;
int version = gladLoadGLContext(context, glfwGetProcAddress);
std::cout << "Loaded OpenGL " << GLAD_VERSION_MAJOR(version) << "." << GLAD_VERSION_MINOR(version) << std::endl;
return context;
}