【发布时间】:2021-11-27 02:26:17
【问题描述】:
我目前正在使用 C++ 中的 OpenGL 编写类似于地图生成器的 minecraft。我有一个 AMD Rayzen 5 3600 6 核。
所以,当我尝试在我的主循环中添加多线程来调用我的块生成时,它会减慢渲染速度。 我没有使用多线程进行渲染。
我在 Windows 上使用 MinGw 编译代码,多线程在 posix 上工作。
问题是我不知道为什么它会使我的渲染变慢。即使我尝试创建 ONLY ONE 线程,我也会失去 FPS。即使我创建一个线程来执行简单的任务,例如:
std::cout << "Thread #" << i << "\n";
它会减慢渲染速度。
我的编译标志是-pthread -lopengl32 -lglu32 -lgdi32 -luser32 -lkernel32 -lglfw3dll -O3
我要补充一个事实,即我在学校使用 MacOS 并且多线程并没有减慢渲染速度。我假设是 MinGW 问题。
如果您有任何想法可以帮助我,我会接受。感谢您的回复!
这是我的循环:
while (!glfwWindowShouldClose(window))
{
Frustum frustum;
//fps(window);
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput(window);
glClearColor(0.69f, 0.94f, 0.95f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader.use();
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 1000.0f);
shader.setMat4("projection", projection);
glm::mat4 view = camera.GetViewMatrix();
shader.setMat4("view", view);
frustum.Transform(projection, view);
shader.setVec3("lightPos", glm::vec3(0.7, 0.2, 0.5));
shader.setVec3("viewPos", camera.Position);
displayChunk(shader, vox, &chunks, frustum);
glDepthFunc(GL_LEQUAL); // change depth function so depth test passes when values are equal to depth buffer's content
skyboxShader.use();
view = glm::mat4(glm::mat3(camera.GetViewMatrix())); // remove translation from the view matrix
skyboxShader.setMat4("view", view);
skyboxShader.setMat4("projection", projection);
// skybox cube
glBindVertexArray(skybox.skyboxVAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, skybox.cubemapTexture);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glDepthFunc(GL_LESS); // set depth function back to default
glfwSwapBuffers(window);
glfwPollEvents();
for (unsigned i = 0; i < 1; ++i)
{
threads[i] = std::thread([&mtx, i]
{
{
// Use a lexical scope and lock_guard to safely lock the mutex only for
// the duration of std::cout usage.
std::lock_guard<std::mutex> iolock(mtx);
std::cout << "Thread #" << i << " is running\n";
}
});
}
for (auto &t : threads)
{
t.join();
}
}
【问题讨论】:
-
如果没有最小的、可重现的例子,我们所能做的就是猜测。我可能找不到足够的理由。如何向 OGL 发送命令和数据?您的代码中线程与 OGL 的关系如何?
标签: c++ multithreading opengl compilation minecraft