Merge pull request #3371 from lioncash/streambuffer

StreamBuffer: Minor changes
This commit is contained in:
Pierre Bourdon 2015-12-21 18:56:08 +01:00
commit 9039cc5860
5 changed files with 45 additions and 42 deletions

View File

@ -2,6 +2,7 @@
// Licensed under GPLv2+ // Licensed under GPLv2+
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <memory>
#include <string> #include <string>
#include "Common/Common.h" #include "Common/Common.h"
@ -28,7 +29,7 @@ static const u32 UBO_LENGTH = 32*1024*1024;
u32 ProgramShaderCache::s_ubo_buffer_size; u32 ProgramShaderCache::s_ubo_buffer_size;
s32 ProgramShaderCache::s_ubo_align; s32 ProgramShaderCache::s_ubo_align;
static StreamBuffer *s_buffer; static std::unique_ptr<StreamBuffer> s_buffer;
static int num_failures = 0; static int num_failures = 0;
static LinearDiskCache<SHADERUID, u8> g_program_disk_cache; static LinearDiskCache<SHADERUID, u8> g_program_disk_cache;
@ -505,8 +506,7 @@ void ProgramShaderCache::Shutdown()
pixel_uid_checker.Invalidate(); pixel_uid_checker.Invalidate();
vertex_uid_checker.Invalidate(); vertex_uid_checker.Invalidate();
delete s_buffer; s_buffer.reset();
s_buffer = nullptr;
} }
void ProgramShaderCache::CreateHeader() void ProgramShaderCache::CreateHeader()

View File

@ -15,7 +15,7 @@ namespace OGL
{ {
// moved out of constructor, so m_buffer is allowed to be const // moved out of constructor, so m_buffer is allowed to be const
static u32 genBuffer() static u32 GenBuffer()
{ {
u32 id; u32 id;
glGenBuffers(1, &id); glGenBuffers(1, &id);
@ -23,7 +23,7 @@ static u32 genBuffer()
} }
StreamBuffer::StreamBuffer(u32 type, u32 size) StreamBuffer::StreamBuffer(u32 type, u32 size)
: m_buffer(genBuffer()), m_buffertype(type), m_size(ROUND_UP_POW2(size)), m_bit_per_slot(IntLog2(ROUND_UP_POW2(size) / SYNC_POINTS)) : m_buffer(GenBuffer()), m_buffertype(type), m_size(ROUND_UP_POW2(size)), m_bit_per_slot(IntLog2(ROUND_UP_POW2(size) / SYNC_POINTS))
{ {
m_iterator = 0; m_iterator = 0;
m_used_iterator = 0; m_used_iterator = 0;
@ -61,36 +61,36 @@ StreamBuffer::~StreamBuffer()
void StreamBuffer::CreateFences() void StreamBuffer::CreateFences()
{ {
for (int i=0; i<SYNC_POINTS; i++) for (int i = 0; i < SYNC_POINTS; i++)
{ {
fences[i] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); m_fences[i] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
} }
} }
void StreamBuffer::DeleteFences() void StreamBuffer::DeleteFences()
{ {
for (int i = SLOT(m_free_iterator) + 1; i < SYNC_POINTS; i++) for (int i = Slot(m_free_iterator) + 1; i < SYNC_POINTS; i++)
{ {
glDeleteSync(fences[i]); glDeleteSync(m_fences[i]);
} }
for (int i = 0; i < SLOT(m_iterator); i++) for (int i = 0; i < Slot(m_iterator); i++)
{ {
glDeleteSync(fences[i]); glDeleteSync(m_fences[i]);
} }
} }
void StreamBuffer::AllocMemory(u32 size) void StreamBuffer::AllocMemory(u32 size)
{ {
// insert waiting slots for used memory // insert waiting slots for used memory
for (int i = SLOT(m_used_iterator); i < SLOT(m_iterator); i++) for (int i = Slot(m_used_iterator); i < Slot(m_iterator); i++)
{ {
fences[i] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); m_fences[i] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
} }
m_used_iterator = m_iterator; m_used_iterator = m_iterator;
// wait for new slots to end of buffer // wait for new slots to end of buffer
for (int i = SLOT(m_free_iterator) + 1; i <= SLOT(m_iterator + size) && i < SYNC_POINTS; i++) for (int i = Slot(m_free_iterator) + 1; i <= Slot(m_iterator + size) && i < SYNC_POINTS; i++)
{ {
glClientWaitSync(fences[i], GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED); glClientWaitSync(m_fences[i], GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED);
glDeleteSync(fences[i]); glDeleteSync(m_fences[i]);
} }
m_free_iterator = m_iterator + size; m_free_iterator = m_iterator + size;
@ -98,19 +98,19 @@ void StreamBuffer::AllocMemory(u32 size)
if (m_iterator + size >= m_size) if (m_iterator + size >= m_size)
{ {
// insert waiting slots in unused space at the end of the buffer // insert waiting slots in unused space at the end of the buffer
for (int i = SLOT(m_used_iterator); i < SYNC_POINTS; i++) for (int i = Slot(m_used_iterator); i < SYNC_POINTS; i++)
{ {
fences[i] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); m_fences[i] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
} }
// move to the start // move to the start
m_used_iterator = m_iterator = 0; // offset 0 is always aligned m_used_iterator = m_iterator = 0; // offset 0 is always aligned
// wait for space at the start // wait for space at the start
for (int i = 0; i <= SLOT(m_iterator + size); i++) for (int i = 0; i <= Slot(m_iterator + size); i++)
{ {
glClientWaitSync(fences[i], GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED); glClientWaitSync(m_fences[i], GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED);
glDeleteSync(fences[i]); glDeleteSync(m_fences[i]);
} }
m_free_iterator = m_iterator + size; m_free_iterator = m_iterator + size;
} }
@ -355,17 +355,17 @@ public:
u8* m_pointer; u8* m_pointer;
}; };
// choose best streaming library based on the supported extensions and known issues // Chooses the best streaming method based on the supported extensions and known issues
StreamBuffer* StreamBuffer::Create(u32 type, u32 size) std::unique_ptr<StreamBuffer> StreamBuffer::Create(u32 type, u32 size)
{ {
// without basevertex support, only streaming methods whith uploads everything to zero works fine: // without basevertex support, only streaming methods whith uploads everything to zero works fine:
if (!g_ogl_config.bSupportsGLBaseVertex) if (!g_ogl_config.bSupportsGLBaseVertex)
{ {
if (!DriverDetails::HasBug(DriverDetails::BUG_BROKENBUFFERSTREAM)) if (!DriverDetails::HasBug(DriverDetails::BUG_BROKENBUFFERSTREAM))
return new BufferSubData(type, size); return std::make_unique<BufferSubData>(type, size);
// BufferData is by far the worst way, only use it if needed // BufferData is by far the worst way, only use it if needed
return new BufferData(type, size); return std::make_unique<BufferData>(type, size);
} }
// Prefer the syncing buffers over the orphaning one // Prefer the syncing buffers over the orphaning one
@ -374,25 +374,25 @@ StreamBuffer* StreamBuffer::Create(u32 type, u32 size)
// pinned memory is much faster than buffer storage on AMD cards // pinned memory is much faster than buffer storage on AMD cards
if (g_ogl_config.bSupportsGLPinnedMemory && if (g_ogl_config.bSupportsGLPinnedMemory &&
!(DriverDetails::HasBug(DriverDetails::BUG_BROKENPINNEDMEMORY) && type == GL_ELEMENT_ARRAY_BUFFER)) !(DriverDetails::HasBug(DriverDetails::BUG_BROKENPINNEDMEMORY) && type == GL_ELEMENT_ARRAY_BUFFER))
return new PinnedMemory(type, size); return std::make_unique<PinnedMemory>(type, size);
// buffer storage works well in most situations // buffer storage works well in most situations
if (g_ogl_config.bSupportsGLBufferStorage && if (g_ogl_config.bSupportsGLBufferStorage &&
!(DriverDetails::HasBug(DriverDetails::BUG_BROKENBUFFERSTORAGE) && type == GL_ARRAY_BUFFER) && !(DriverDetails::HasBug(DriverDetails::BUG_BROKENBUFFERSTORAGE) && type == GL_ARRAY_BUFFER) &&
!(DriverDetails::HasBug(DriverDetails::BUG_INTELBROKENBUFFERSTORAGE) && type == GL_ELEMENT_ARRAY_BUFFER)) !(DriverDetails::HasBug(DriverDetails::BUG_INTELBROKENBUFFERSTORAGE) && type == GL_ELEMENT_ARRAY_BUFFER))
return new BufferStorage(type, size); return std::make_unique<BufferStorage>(type, size);
// don't fall back to MapAnd* for Nvidia drivers // don't fall back to MapAnd* for Nvidia drivers
if (DriverDetails::HasBug(DriverDetails::BUG_BROKENUNSYNCMAPPING)) if (DriverDetails::HasBug(DriverDetails::BUG_BROKENUNSYNCMAPPING))
return new BufferSubData(type, size); return std::make_unique<BufferSubData>(type, size);
// mapping fallback // mapping fallback
if (g_ogl_config.bSupportsGLSync) if (g_ogl_config.bSupportsGLSync)
return new MapAndSync(type, size); return std::make_unique<MapAndSync>(type, size);
} }
// default fallback, should work everywhere, but isn't the best way to do this job // default fallback, should work everywhere, but isn't the best way to do this job
return new MapAndOrphan(type, size); return std::make_unique<MapAndOrphan>(type, size);
} }
} }

View File

@ -4,6 +4,8 @@
#pragma once #pragma once
#include <array>
#include <memory>
#include <utility> #include <utility>
#include "Common/GL/GLUtil.h" #include "Common/GL/GLUtil.h"
@ -19,7 +21,7 @@ class StreamBuffer
{ {
public: public:
static StreamBuffer* Create(u32 type, u32 size); static std::unique_ptr<StreamBuffer> Create(u32 type, u32 size);
virtual ~StreamBuffer(); virtual ~StreamBuffer();
/* This mapping function will return a pair of: /* This mapping function will return a pair of:
@ -33,7 +35,7 @@ public:
virtual std::pair<u8*, u32> Map(u32 size) = 0; virtual std::pair<u8*, u32> Map(u32 size) = 0;
virtual void Unmap(u32 used_size) = 0; virtual void Unmap(u32 used_size) = 0;
inline std::pair<u8*, u32> Map(u32 size, u32 stride) std::pair<u8*, u32> Map(u32 size, u32 stride)
{ {
u32 padding = m_iterator % stride; u32 padding = m_iterator % stride;
if (padding) if (padding)
@ -59,11 +61,11 @@ protected:
u32 m_free_iterator; u32 m_free_iterator;
private: private:
static const int SYNC_POINTS = 16; static constexpr int SYNC_POINTS = 16;
inline int SLOT(u32 x) const { return x >> m_bit_per_slot; } int Slot(u32 x) const { return x >> m_bit_per_slot; }
const int m_bit_per_slot; const int m_bit_per_slot;
GLsync fences[SYNC_POINTS]; std::array<GLsync, SYNC_POINTS> m_fences{};
}; };
} }

View File

@ -5,6 +5,7 @@
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include <fstream> #include <fstream>
#include <memory>
#include <vector> #include <vector>
#include "Common/Common.h" #include "Common/Common.h"
@ -51,7 +52,7 @@ static u32 s_Textures[8];
static u32 s_ActiveTexture; static u32 s_ActiveTexture;
static SHADER s_palette_pixel_shader[3]; static SHADER s_palette_pixel_shader[3];
static StreamBuffer* s_palette_stream_buffer = nullptr; static std::unique_ptr<StreamBuffer> s_palette_stream_buffer;
static GLuint s_palette_resolv_texture; static GLuint s_palette_resolv_texture;
static GLuint s_palette_buffer_offset_uniform[3]; static GLuint s_palette_buffer_offset_uniform[3];
static GLuint s_palette_multiplier_uniform[3]; static GLuint s_palette_multiplier_uniform[3];
@ -318,8 +319,7 @@ TextureCache::~TextureCache()
if (g_ActiveConfig.backend_info.bSupportsPaletteConversion) if (g_ActiveConfig.backend_info.bSupportsPaletteConversion)
{ {
delete s_palette_stream_buffer; s_palette_stream_buffer.reset();
s_palette_stream_buffer = nullptr;
glDeleteTextures(1, &s_palette_resolv_texture); glDeleteTextures(1, &s_palette_resolv_texture);
} }
} }

View File

@ -3,6 +3,7 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <fstream> #include <fstream>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@ -36,8 +37,8 @@ namespace OGL
const u32 MAX_IBUFFER_SIZE = 2*1024*1024; const u32 MAX_IBUFFER_SIZE = 2*1024*1024;
const u32 MAX_VBUFFER_SIZE = 32*1024*1024; const u32 MAX_VBUFFER_SIZE = 32*1024*1024;
static StreamBuffer *s_vertexBuffer; static std::unique_ptr<StreamBuffer> s_vertexBuffer;
static StreamBuffer *s_indexBuffer; static std::unique_ptr<StreamBuffer> s_indexBuffer;
static size_t s_baseVertex; static size_t s_baseVertex;
static size_t s_index_offset; static size_t s_index_offset;
@ -65,8 +66,8 @@ void VertexManager::CreateDeviceObjects()
void VertexManager::DestroyDeviceObjects() void VertexManager::DestroyDeviceObjects()
{ {
delete s_vertexBuffer; s_vertexBuffer.reset();
delete s_indexBuffer; s_indexBuffer.reset();
} }
void VertexManager::PrepareDrawBuffers(u32 stride) void VertexManager::PrepareDrawBuffers(u32 stride)