[Bf-blender-cvs] [5037dd8abdf] blender2.8: GPU: Add GC to FBOs and UBOs and centralize all GCs

Clément Foucault noreply at git.blender.org
Thu Jul 19 16:13:42 CEST 2018


Commit: 5037dd8abdf9335e998141336d4e15f81580c491
Author: Clément Foucault
Date:   Thu Jul 19 15:48:13 2018 +0200
Branches: blender2.8
https://developer.blender.org/rB5037dd8abdf9335e998141336d4e15f81580c491

GPU: Add GC to FBOs and UBOs and centralize all GCs

GPUFrameBuffers were being free when no context was attached or in the
wrong gl context. This make sure this does not happen again.

You can now safely free any gl resource from any thread (well as long as
it's not used anymore!).

===================================================================

M	source/blender/draw/DRW_engine.h
M	source/blender/draw/intern/draw_manager.c
M	source/blender/gpu/CMakeLists.txt
D	source/blender/gpu/GPU_buffer_id.h
M	source/blender/gpu/GPU_material.h
M	source/blender/gpu/GPU_texture.h
M	source/blender/gpu/intern/gpu_batch.c
M	source/blender/gpu/intern/gpu_batch_private.h
D	source/blender/gpu/intern/gpu_buffer_id.cpp
R052	source/blender/gpu/intern/gpu_vertex_array_id.cpp	source/blender/gpu/intern/gpu_context.cpp
R053	source/blender/gpu/GPU_vertex_array_id.h	source/blender/gpu/intern/gpu_context_private.h
M	source/blender/gpu/intern/gpu_element.c
M	source/blender/gpu/intern/gpu_framebuffer.c
M	source/blender/gpu/intern/gpu_immediate.c
M	source/blender/gpu/intern/gpu_init_exit.c
M	source/blender/gpu/intern/gpu_material.c
M	source/blender/gpu/intern/gpu_private.h
M	source/blender/gpu/intern/gpu_shader.c
M	source/blender/gpu/intern/gpu_shader_interface.c
M	source/blender/gpu/intern/gpu_texture.c
M	source/blender/gpu/intern/gpu_uniformbuffer.c
M	source/blender/gpu/intern/gpu_vertex_buffer.c
M	source/blender/windowmanager/intern/wm_init_exit.c
M	source/blender/windowmanager/intern/wm_window.c

===================================================================

diff --git a/source/blender/draw/DRW_engine.h b/source/blender/draw/DRW_engine.h
index eb037d081e1..4d4b486d247 100644
--- a/source/blender/draw/DRW_engine.h
+++ b/source/blender/draw/DRW_engine.h
@@ -136,6 +136,10 @@ void DRW_opengl_context_destroy(void);
 void DRW_opengl_context_enable(void);
 void DRW_opengl_context_disable(void);
 
+/* Never use this. Only for closing blender. */
+void DRW_opengl_context_enable_ex(bool restore);
+void DRW_opengl_context_disable_ex(bool restore);
+
 void DRW_opengl_render_context_enable(void *re_gl_context);
 void DRW_opengl_render_context_disable(void *re_gl_context);
 void DRW_gawain_render_context_enable(void *re_gpu_context);
diff --git a/source/blender/draw/intern/draw_manager.c b/source/blender/draw/intern/draw_manager.c
index 2793410cd8e..cc4f8ec7947 100644
--- a/source/blender/draw/intern/draw_manager.c
+++ b/source/blender/draw/intern/draw_manager.c
@@ -2374,21 +2374,21 @@ void DRW_opengl_context_destroy(void)
 	}
 }
 
-void DRW_opengl_context_enable(void)
+void DRW_opengl_context_enable_ex(bool restore)
 {
 	if (DST.gl_context != NULL) {
 		/* IMPORTANT: We dont support immediate mode in render mode!
 		 * This shall remain in effect until immediate mode supports
 		 * multiple threads. */
 		BLI_ticket_mutex_lock(DST.gl_context_mutex);
-		if (BLI_thread_is_main()) {
+		if (BLI_thread_is_main() && restore) {
 			if (!G.background) {
 				immDeactivate();
 			}
 		}
 		WM_opengl_context_activate(DST.gl_context);
 		GPU_context_active_set(DST.gpu_context);
-		if (BLI_thread_is_main()) {
+		if (BLI_thread_is_main() && restore) {
 			if (!G.background) {
 				immActivate();
 			}
@@ -2397,7 +2397,7 @@ void DRW_opengl_context_enable(void)
 	}
 }
 
-void DRW_opengl_context_disable(void)
+void DRW_opengl_context_disable_ex(bool restore)
 {
 	if (DST.gl_context != NULL) {
 #ifdef __APPLE__
@@ -2406,7 +2406,7 @@ void DRW_opengl_context_disable(void)
 		glFlush();
 #endif
 
-		if (BLI_thread_is_main()) {
+		if (BLI_thread_is_main() && restore) {
 			wm_window_reset_drawable();
 		}
 		else {
@@ -2418,6 +2418,16 @@ void DRW_opengl_context_disable(void)
 	}
 }
 
+void DRW_opengl_context_enable(void)
+{
+	DRW_opengl_context_enable_ex(true);
+}
+
+void DRW_opengl_context_disable(void)
+{
+	DRW_opengl_context_disable_ex(true);
+}
+
 void DRW_opengl_render_context_enable(void *re_gl_context)
 {
 	/* If thread is main you should use DRW_opengl_context_enable(). */
diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt
index 58295ae9329..8273f3f1992 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -62,9 +62,9 @@ set(SRC
 	intern/gpu_batch.c
 	intern/gpu_batch_presets.c
 	intern/gpu_batch_utils.c
-	intern/gpu_buffer_id.cpp
 	intern/gpu_buffers.c
 	intern/gpu_codegen.c
+	intern/gpu_context.cpp
 	intern/gpu_debug.c
 	intern/gpu_draw.c
 	intern/gpu_element.c
@@ -84,7 +84,6 @@ set(SRC
 	intern/gpu_state.c
 	intern/gpu_texture.c
 	intern/gpu_uniformbuffer.c
-	intern/gpu_vertex_array_id.cpp
 	intern/gpu_vertex_buffer.c
 	intern/gpu_vertex_format.c
 	intern/gpu_viewport.c
@@ -113,7 +112,6 @@ set(SRC
 	GPU_attr_binding.h
 	GPU_basic_shader.h
 	GPU_batch.h
-	GPU_buffer_id.h
 	GPU_buffers.h
 	GPU_common.h
 	GPU_debug.h
@@ -135,7 +133,6 @@ set(SRC
 	GPU_state.h
 	GPU_texture.h
 	GPU_uniformbuffer.h
-	GPU_vertex_array_id.h
 	GPU_vertex_buffer.h
 	GPU_vertex_format.h
 	GPU_viewport.h
@@ -143,6 +140,7 @@ set(SRC
 	intern/gpu_attr_binding_private.h
 	intern/gpu_batch_private.h
 	intern/gpu_codegen.h
+	intern/gpu_context_private.h
 	intern/gpu_primitive_private.h
 	intern/gpu_private.h
 	intern/gpu_select_private.h
diff --git a/source/blender/gpu/GPU_buffer_id.h b/source/blender/gpu/GPU_buffer_id.h
deleted file mode 100644
index 4615e9e2c66..00000000000
--- a/source/blender/gpu/GPU_buffer_id.h
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * ***** BEGIN GPL LICENSE BLOCK *****
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- * The Original Code is Copyright (C) 2016 by Mike Erwin.
- * All rights reserved.
- *
- * Contributor(s): Blender Foundation
- *
- * ***** END GPL LICENSE BLOCK *****
- */
-
-/** \file blender/gpu/GPU_buffer_id.h
- *  \ingroup gpu
- *
- * GPU buffer IDs
- */
-
-#ifndef __GPU_BUFFER_ID_H__
-#define __GPU_BUFFER_ID_H__
-
-/* Manage GL buffer IDs in a thread-safe way
- * Use these instead of glGenBuffers & its friends
- * - alloc must be called from main thread
- * - free can be called from any thread */
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include "GPU_common.h"
-
-GLuint GPU_buf_id_alloc(void);
-void GPU_buf_id_free(GLuint buffer_id);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* __GPU_BUFFER_ID_H__ */
diff --git a/source/blender/gpu/GPU_material.h b/source/blender/gpu/GPU_material.h
index 4e264defbd4..f2c029f643e 100644
--- a/source/blender/gpu/GPU_material.h
+++ b/source/blender/gpu/GPU_material.h
@@ -254,11 +254,6 @@ void GPU_material_free(struct ListBase *gpumaterial);
 
 void GPU_materials_free(struct Main *bmain);
 
-void GPU_material_orphans_init(void);
-void GPU_material_orphans_exit(void);
-/* This has to be called from a thread with an ogl context bound. */
-void GPU_material_orphans_delete(void);
-
 struct Scene *GPU_material_scene(GPUMaterial *material);
 GPUMatType GPU_Material_get_type(GPUMaterial *material);
 struct GPUPass *GPU_material_get_pass(GPUMaterial *material);
diff --git a/source/blender/gpu/GPU_texture.h b/source/blender/gpu/GPU_texture.h
index e3db18f1358..1ddf801e166 100644
--- a/source/blender/gpu/GPU_texture.h
+++ b/source/blender/gpu/GPU_texture.h
@@ -198,11 +198,6 @@ void GPU_invalid_tex_free(void);
 
 void GPU_texture_free(GPUTexture *tex);
 
-void GPU_texture_orphans_init(void);
-void GPU_texture_orphans_exit(void);
-/* This has to be called from a thread with an ogl context bound. */
-void GPU_texture_orphans_delete(void);
-
 void GPU_texture_ref(GPUTexture *tex);
 void GPU_texture_bind(GPUTexture *tex, int number);
 void GPU_texture_unbind(GPUTexture *tex);
diff --git a/source/blender/gpu/intern/gpu_batch.c b/source/blender/gpu/intern/gpu_batch.c
index a11eefee078..9b433a37a72 100644
--- a/source/blender/gpu/intern/gpu_batch.c
+++ b/source/blender/gpu/intern/gpu_batch.c
@@ -32,12 +32,11 @@
 
 #include "GPU_batch.h"
 #include "GPU_batch_presets.h"
-#include "GPU_buffer_id.h"
 #include "GPU_matrix.h"
 #include "GPU_shader.h"
-#include "GPU_vertex_array_id.h"
 
 #include "gpu_batch_private.h"
+#include "gpu_context_private.h"
 #include "gpu_primitive_private.h"
 #include "gpu_shader_private.h"
 
diff --git a/source/blender/gpu/intern/gpu_batch_private.h b/source/blender/gpu/intern/gpu_batch_private.h
index 51040ff751a..3a05e243065 100644
--- a/source/blender/gpu/intern/gpu_batch_private.h
+++ b/source/blender/gpu/intern/gpu_batch_private.h
@@ -43,9 +43,6 @@ extern "C" {
 
 void gpu_batch_remove_interface_ref(GPUBatch *batch, const GPUShaderInterface *interface);
 
-void gpu_context_add_batch(GPUContext *ctx, GPUBatch *batch);
-void gpu_context_remove_batch(GPUContext *ctx, GPUBatch *batch);
-
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/gpu/intern/gpu_buffer_id.cpp b/source/blender/gpu/intern/gpu_buffer_id.cpp
deleted file mode 100644
index f3faba9c766..00000000000
--- a/source/blender/gpu/intern/gpu_buffer_id.cpp
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * ***** BEGIN GPL LICENSE BLOCK *****
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- * The Original Code is Copyright (C) 2016 by Mike Erwin.
- * All rights reserved.
- *
- * Contributor(s): Blender Foundation
- *
- * ***** END GPL LICENSE BLOCK *****
- */
-
-/** \file blender/gpu/intern/gpu_buffer_id.cpp
- *  \ingroup gpu
- *
- * GPU buffer IDs
- */
-
-#include "GPU_buffer_id.h"
-
-#include <mutex>
-#include <vector>
-
-#define ORPHAN_DEBUG 0
-
-#if ORPHAN_DEBUG
-#  include <cstdio>
-#endif
-
-static std::vector<GLuint> orphaned_buffer_ids;
-
-static std::mutex orphan_mutex;
-
-extern "C" {
-extern int BLI_thread_is_main(void); /* Blender-specific function */
-}
-
-static bool thread_is_main()
-{
-	/* "main" here means the GL context's thread */
-	return BLI_thread_is_main();
-}
-
-GLuint GPU_buf_id_alloc()
-{
-	/* delete orphaned IDs */
-	orphan_mutex.lock();
-	if (!orphaned_buffer_ids.empty()) {
-		const auto orphaned_buffer_len = (uint)orphaned_buffer_ids.size();
-#if ORPHAN_DEBUG
-		printf("deleting %u orphaned VBO%s\n", orphaned_buffer_len, orphaned_buffer_len == 1 ? "" : "s");
-#endif
-		glDeleteBuffers(orphaned_buffer_len, orphaned_buffer_ids.data());
-		orphaned_buffer_ids.clear();
-	}
-	orphan_mutex.unlock();
-
-	GLuint new_buffer_id = 0;
-	glGenBuffers(1, &new_buffer_id);
-	return new_buffer_id;
-}
-
-void GPU_buf_id_free(GLuint buffer_id)
-{
-	if (thread_is_main()) {
-		glDeleteBuffers(1, &buffer_id);
-	}
-	else {
-		/* add this ID to the orphaned list */
-		orphan_mutex.lock();
-#

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list