summaryrefslogtreecommitdiff
path: root/Source/Core/VideoBackends/OGL/OGLBoundingBox.cpp
blob: cee200d4ad5190e020049bd36b751ee1a4f18bd4 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2014 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#include <algorithm>
#include <array>
#include <cstring>

#include "Common/GL/GLUtil.h"

#include "VideoBackends/OGL/OGLBoundingBox.h"
#include "VideoBackends/OGL/OGLRender.h"

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

enum : u32
{
  NUM_BBOX_VALUES = 4,
};

static GLuint s_bbox_buffer_id;
static std::array<s32, NUM_BBOX_VALUES> s_bbox_values;
static std::array<bool, NUM_BBOX_VALUES> s_bbox_dirty;
static bool s_bbox_valid = false;

namespace OGL
{
void BoundingBox::Init()
{
  if (!g_ActiveConfig.backend_info.bSupportsBBox)
    return;

  const s32 initial_values[NUM_BBOX_VALUES] = {0, 0, 0, 0};
  std::memcpy(s_bbox_values.data(), initial_values, sizeof(s_bbox_values));
  s_bbox_dirty = {};
  s_bbox_valid = true;

  glGenBuffers(1, &s_bbox_buffer_id);
  glBindBuffer(GL_SHADER_STORAGE_BUFFER, s_bbox_buffer_id);
  glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(initial_values), initial_values, GL_DYNAMIC_DRAW);
  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, s_bbox_buffer_id);
}

void BoundingBox::Shutdown()
{
  if (!g_ActiveConfig.backend_info.bSupportsBBox)
    return;

  glDeleteBuffers(1, &s_bbox_buffer_id);
}

void BoundingBox::Flush()
{
  s_bbox_valid = false;

  if (std::none_of(s_bbox_dirty.begin(), s_bbox_dirty.end(), [](bool dirty) { return dirty; }))
    return;

  glBindBuffer(GL_SHADER_STORAGE_BUFFER, s_bbox_buffer_id);

  for (u32 start = 0; start < NUM_BBOX_VALUES;)
  {
    if (!s_bbox_dirty[start])
    {
      start++;
      continue;
    }

    u32 end = start + 1;
    s_bbox_dirty[start] = false;
    for (; end < NUM_BBOX_VALUES; end++)
    {
      if (!s_bbox_dirty[end])
        break;

      s_bbox_dirty[end] = false;
    }

    glBufferSubData(GL_SHADER_STORAGE_BUFFER, start * sizeof(s32), (end - start) * sizeof(s32),
                    &s_bbox_values[start]);
  }

  glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
}

void BoundingBox::Readback()
{
  glBindBuffer(GL_SHADER_STORAGE_BUFFER, s_bbox_buffer_id);
  if (!DriverDetails::HasBug(DriverDetails::BUG_SLOW_GETBUFFERSUBDATA) &&
      !static_cast<Renderer*>(g_renderer.get())->IsGLES())
  {
    // 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.
    std::array<s32, NUM_BBOX_VALUES> gpu_values;
    glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, sizeof(s32) * NUM_BBOX_VALUES,
                       gpu_values.data());
    for (u32 i = 0; i < NUM_BBOX_VALUES; i++)
    {
      if (!s_bbox_dirty[i])
        s_bbox_values[i] = gpu_values[i];
    }
  }
  else
  {
    // Using glMapBufferRange is faster on AMD cards by a measurable margin.
    void* ptr = glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, sizeof(s32) * NUM_BBOX_VALUES,
                                 GL_MAP_READ_BIT);
    if (ptr)
    {
      for (u32 i = 0; i < NUM_BBOX_VALUES; i++)
      {
        if (!s_bbox_dirty[i])
        {
          std::memcpy(&s_bbox_values[i], reinterpret_cast<const u8*>(ptr) + sizeof(s32) * i,
                      sizeof(s32));
        }
      }

      glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
    }
  }
  glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
  s_bbox_valid = true;
}

void BoundingBox::Set(int index, int value)
{
  if (s_bbox_valid && s_bbox_values[index] == value)
    return;

  s_bbox_values[index] = value;
  s_bbox_dirty[index] = true;
}

int BoundingBox::Get(int index)
{
  if (!s_bbox_valid)
    Readback();

  return s_bbox_values[index];
}
};  // namespace OGL