Welcome to mirror list, hosted at ThFree Co, Russian Federation.

buffer_id.cpp « gawain « gpu « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a7b8d7a394aa4fb2344f0fa2ad4dd0e62ca860c9 (plain)
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

// Gawain buffer IDs
//
// This code is part of the Gawain library, with modifications
// specific to integration with Blender.
//
// Copyright 2016 Mike Erwin
//
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.#include "buffer_id.h"

#include "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::vector<GLuint> orphaned_vao_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 buffer_id_alloc()
	{
#if TRUST_NO_ONE
	assert(thread_is_main());
#endif

	// delete orphaned IDs
	orphan_mutex.lock();
	if (!orphaned_buffer_ids.empty())
		{
		const auto orphaned_buffer_ct = (unsigned)orphaned_buffer_ids.size();
#if ORPHAN_DEBUG
		printf("deleting %u orphaned VBO%s\n", orphaned_buffer_ct, orphaned_buffer_ct == 1 ? "" : "s");
#endif
		glDeleteBuffers(orphaned_buffer_ct, orphaned_buffer_ids.data());
		orphaned_buffer_ids.clear();
		}
	orphan_mutex.unlock();

	GLuint new_buffer_id;
	glGenBuffers(1, &new_buffer_id);
	return new_buffer_id;
	}

void buffer_id_free(GLuint buffer_id)
	{
	if (thread_is_main())
		glDeleteBuffers(1, &buffer_id);
	else
		{
		// add this ID to the orphaned list
		orphan_mutex.lock();
#if ORPHAN_DEBUG
		printf("orphaning VBO %u\n", buffer_id);
#endif
		orphaned_buffer_ids.emplace_back(buffer_id);
		orphan_mutex.unlock();
		}
	}

GLuint vao_id_alloc()
	{
#if TRUST_NO_ONE
	assert(thread_is_main());
#endif

	GLuint new_vao_id;

	// delete orphaned IDs
	orphan_mutex.lock();
	if (!orphaned_vao_ids.empty())
		{
		const auto orphaned_vao_ct = (unsigned)orphaned_vao_ids.size();
#if ORPHAN_DEBUG
		printf("deleting %u orphaned VAO%s\n", orphaned_vao_ct, orphaned_vao_ct == 1 ? "" : "s");
#endif
		glDeleteVertexArrays(orphaned_vao_ct, orphaned_vao_ids.data());
		orphaned_vao_ids.clear();
		}
	orphan_mutex.unlock();

	glGenVertexArrays(1, &new_vao_id);
	return new_vao_id;
	}

void vao_id_free(GLuint vao_id)
	{
	if (thread_is_main())
		glDeleteVertexArrays(1, &vao_id);
	else
		{
		// add this ID to the orphaned list
		orphan_mutex.lock();
#if ORPHAN_DEBUG
		printf("orphaning VAO %u\n", vao_id);
#endif
		orphaned_vao_ids.emplace_back(vao_id);
		orphan_mutex.unlock();
		}
	}