summaryrefslogtreecommitdiff
path: root/Source/Core/VideoBackends/OGL/BoundingBox.cpp
blob: 2923b9c2c41097f6b551005d05341fbd3c4dff1b (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
// Copyright 2014 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <cstring>

#include "Common/GL/GLUtil.h"

#include "VideoBackends/OGL/BoundingBox.h"

#include "VideoCommon/DriverDetails.h"
#include "VideoCommon/VideoConfig.h"

static GLuint s_bbox_buffer_id;

namespace OGL
{

void BoundingBox::Init()
{
	if (g_ActiveConfig.backend_info.bSupportsBBox)
	{
		int initial_values[4] = {0,0,0,0};
		glGenBuffers(1, &s_bbox_buffer_id);
		glBindBuffer(GL_SHADER_STORAGE_BUFFER, s_bbox_buffer_id);
		glBufferData(GL_SHADER_STORAGE_BUFFER, 4 * sizeof(s32), initial_values, GL_DYNAMIC_DRAW);
		glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, s_bbox_buffer_id);
	}
}

void BoundingBox::Shutdown()
{
	if (g_ActiveConfig.backend_info.bSupportsBBox)
		glDeleteBuffers(1, &s_bbox_buffer_id);
}

void BoundingBox::Set(int index, int value)
{
	glBindBuffer(GL_SHADER_STORAGE_BUFFER, s_bbox_buffer_id);
	glBufferSubData(GL_SHADER_STORAGE_BUFFER, index * sizeof(int), sizeof(int), &value);
	glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
}

int BoundingBox::Get(int index)
{
	int data = 0;
	glBindBuffer(GL_SHADER_STORAGE_BUFFER, s_bbox_buffer_id);

	if (!DriverDetails::HasBug(DriverDetails::BUG_SLOWGETBUFFERSUBDATA))
	{
		// Using glMapBufferRange to read back the contents of the SSBO is extremely slow
		// on nVidia drivers. This is more noticeable at higher internal resolutions.
		// Using glGetBufferSubData instead does not seem to exhibit this slowdown.
		glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, index * sizeof(int), sizeof(int), &data);
	}
	else
	{
		// Using glMapBufferRange is faster on AMD cards by a measurable margin.
		void* ptr = glMapBufferRange(GL_SHADER_STORAGE_BUFFER, index * sizeof(int), sizeof(int), GL_MAP_READ_BIT);
		if (ptr)
		{
			memcpy(&data, ptr, sizeof(int));
			glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
		}
	}

	glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
	return data;
}

};