2013-04-18 03:29:41 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2013-01-31 22:11:53 +00:00
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "GLUtil.h"
|
|
|
|
#include "StreamBuffer.h"
|
2013-02-02 18:26:29 +00:00
|
|
|
#include "MemoryUtil.h"
|
2013-03-25 14:14:24 +00:00
|
|
|
#include "Render.h"
|
2013-08-26 17:45:19 +00:00
|
|
|
#include "DriverDetails.h"
|
2013-08-27 11:24:23 +00:00
|
|
|
#include "OnScreenDisplay.h"
|
2013-01-31 22:11:53 +00:00
|
|
|
|
|
|
|
namespace OGL
|
|
|
|
{
|
|
|
|
|
2013-02-01 14:15:25 +00:00
|
|
|
static const u32 SYNC_POINTS = 16;
|
2013-02-02 18:39:26 +00:00
|
|
|
static const u32 ALIGN_PINNED_MEMORY = 4096;
|
2013-02-01 14:15:25 +00:00
|
|
|
|
|
|
|
StreamBuffer::StreamBuffer(u32 type, size_t size, StreamType uploadType)
|
2013-02-01 16:04:27 +00:00
|
|
|
: m_uploadtype(uploadType), m_buffertype(type), m_size(size)
|
2013-01-31 22:11:53 +00:00
|
|
|
{
|
|
|
|
glGenBuffers(1, &m_buffer);
|
|
|
|
|
2013-03-25 14:14:24 +00:00
|
|
|
bool nvidia = !strcmp(g_ogl_config.gl_vendor, "NVIDIA Corporation");
|
2013-02-21 20:59:40 +00:00
|
|
|
|
2013-03-23 20:37:01 +00:00
|
|
|
if(m_uploadtype & STREAM_DETECT)
|
2013-02-02 18:26:29 +00:00
|
|
|
{
|
2013-08-27 11:24:23 +00:00
|
|
|
// TODO: move this to InitBackendInfo
|
2013-08-29 10:01:34 +00:00
|
|
|
if(g_ActiveConfig.bHackedBufferUpload && DriverDetails::HasBug(DriverDetails::BUG_BROKENHACKEDBUFFER))
|
2013-08-27 11:24:23 +00:00
|
|
|
{
|
|
|
|
OSD::AddMessage("Vertex Streaming Hack isn't supported by your GPU.", 10000);
|
|
|
|
g_ActiveConfig.bHackedBufferUpload = false;
|
|
|
|
g_Config.bHackedBufferUpload = false;
|
|
|
|
}
|
|
|
|
|
2013-07-27 00:40:16 +00:00
|
|
|
if(!g_ogl_config.bSupportsGLBaseVertex && (m_uploadtype & BUFFERDATA))
|
|
|
|
m_uploadtype = BUFFERDATA;
|
|
|
|
else if(!g_ogl_config.bSupportsGLBaseVertex && (m_uploadtype & BUFFERSUBDATA))
|
2013-02-26 22:15:55 +00:00
|
|
|
m_uploadtype = BUFFERSUBDATA;
|
2013-08-27 11:24:23 +00:00
|
|
|
else if(g_ogl_config.bSupportsGLSync && g_ActiveConfig.bHackedBufferUpload && (m_uploadtype & MAP_AND_RISK))
|
2013-02-19 14:51:48 +00:00
|
|
|
m_uploadtype = MAP_AND_RISK;
|
2013-08-26 17:45:19 +00:00
|
|
|
else if(g_ogl_config.bSupportsGLSync && g_ogl_config.bSupportsGLPinnedMemory && (!DriverDetails::HasBug(DriverDetails::BUG_BROKENPINNEDMEMORY) || type != GL_ELEMENT_ARRAY_BUFFER) && (m_uploadtype & PINNED_MEMORY))
|
2013-03-16 09:08:46 +00:00
|
|
|
m_uploadtype = PINNED_MEMORY;
|
2013-03-23 20:37:01 +00:00
|
|
|
else if(nvidia && (m_uploadtype & BUFFERSUBDATA))
|
2013-02-21 20:59:40 +00:00
|
|
|
m_uploadtype = BUFFERSUBDATA;
|
2013-03-25 14:14:24 +00:00
|
|
|
else if(g_ogl_config.bSupportsGLSync && (m_uploadtype & MAP_AND_SYNC))
|
2013-02-21 17:18:44 +00:00
|
|
|
m_uploadtype = MAP_AND_SYNC;
|
2013-02-06 00:05:19 +00:00
|
|
|
else
|
2013-02-19 14:51:48 +00:00
|
|
|
m_uploadtype = MAP_AND_ORPHAN;
|
2013-02-02 18:26:29 +00:00
|
|
|
}
|
2013-01-31 22:11:53 +00:00
|
|
|
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
StreamBuffer::~StreamBuffer()
|
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
glDeleteBuffers(1, &m_buffer);
|
|
|
|
}
|
|
|
|
|
2013-02-01 15:43:08 +00:00
|
|
|
#define SLOT(x) (x)*SYNC_POINTS/m_size
|
|
|
|
|
2013-02-01 14:15:25 +00:00
|
|
|
void StreamBuffer::Alloc ( size_t size, u32 stride )
|
2013-01-31 22:11:53 +00:00
|
|
|
{
|
2013-02-01 14:15:25 +00:00
|
|
|
size_t m_iterator_aligned = m_iterator;
|
|
|
|
if(m_iterator_aligned && stride) {
|
|
|
|
m_iterator_aligned--;
|
|
|
|
m_iterator_aligned = m_iterator_aligned - (m_iterator_aligned % stride) + stride;
|
2013-01-31 22:11:53 +00:00
|
|
|
}
|
2013-02-01 15:43:08 +00:00
|
|
|
size_t iter_end = m_iterator_aligned + size;
|
2013-02-01 14:15:25 +00:00
|
|
|
|
2013-01-31 22:11:53 +00:00
|
|
|
switch(m_uploadtype) {
|
|
|
|
case MAP_AND_ORPHAN:
|
2013-02-01 15:43:08 +00:00
|
|
|
if(iter_end >= m_size) {
|
2013-01-31 22:11:53 +00:00
|
|
|
glBufferData(m_buffertype, m_size, NULL, GL_STREAM_DRAW);
|
2013-02-01 14:15:25 +00:00
|
|
|
m_iterator_aligned = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MAP_AND_SYNC:
|
2013-02-01 16:04:27 +00:00
|
|
|
case PINNED_MEMORY:
|
2013-02-01 15:43:08 +00:00
|
|
|
|
|
|
|
// insert waiting slots for used memory
|
|
|
|
for(u32 i=SLOT(m_used_iterator); i<SLOT(m_iterator); i++)
|
|
|
|
{
|
|
|
|
fences[i] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
|
|
|
|
}
|
|
|
|
m_used_iterator = m_iterator;
|
|
|
|
|
|
|
|
// wait for new slots to end of buffer
|
|
|
|
for(u32 i=SLOT(m_free_iterator)+1; i<=SLOT(iter_end) && i < SYNC_POINTS; i++)
|
2013-02-01 14:15:25 +00:00
|
|
|
{
|
2013-03-05 15:51:09 +00:00
|
|
|
glClientWaitSync(fences[i], GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED);
|
2013-02-01 14:15:25 +00:00
|
|
|
glDeleteSync(fences[i]);
|
|
|
|
}
|
2013-02-01 15:43:08 +00:00
|
|
|
m_free_iterator = iter_end;
|
2013-02-01 14:15:25 +00:00
|
|
|
|
2013-02-01 15:43:08 +00:00
|
|
|
// if buffer is full
|
|
|
|
if(iter_end >= m_size) {
|
|
|
|
|
|
|
|
// insert waiting slots in unused space at the end of the buffer
|
|
|
|
for(u32 i=SLOT(m_used_iterator); i < SYNC_POINTS; i++)
|
2013-02-01 14:15:25 +00:00
|
|
|
fences[i] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
|
2013-02-01 15:43:08 +00:00
|
|
|
|
|
|
|
// move to the start
|
|
|
|
m_used_iterator = m_iterator_aligned = m_iterator = 0; // offset 0 is always aligned
|
|
|
|
iter_end = size;
|
|
|
|
|
|
|
|
// wait for space at the start
|
|
|
|
for(u32 i=0; i<=SLOT(iter_end); i++)
|
|
|
|
{
|
2013-03-05 15:51:09 +00:00
|
|
|
glClientWaitSync(fences[i], GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED);
|
2013-02-01 15:43:08 +00:00
|
|
|
glDeleteSync(fences[i]);
|
|
|
|
}
|
|
|
|
m_free_iterator = iter_end;
|
2013-01-31 22:11:53 +00:00
|
|
|
}
|
2013-02-01 15:43:08 +00:00
|
|
|
|
2013-03-08 09:23:27 +00:00
|
|
|
break;
|
|
|
|
case MAP_AND_RISK:
|
|
|
|
if(iter_end >= m_size) {
|
|
|
|
m_iterator_aligned = 0;
|
|
|
|
}
|
2013-02-01 11:30:08 +00:00
|
|
|
break;
|
|
|
|
case BUFFERSUBDATA:
|
2013-07-27 00:40:16 +00:00
|
|
|
case BUFFERDATA:
|
2013-02-01 14:15:25 +00:00
|
|
|
m_iterator_aligned = 0;
|
2013-02-01 11:30:08 +00:00
|
|
|
break;
|
2013-02-01 16:04:27 +00:00
|
|
|
case STREAM_DETECT:
|
2013-03-23 20:37:01 +00:00
|
|
|
case DETECT_MASK: // Just to shutup warnings
|
2013-02-01 16:04:27 +00:00
|
|
|
break;
|
2013-02-01 11:30:08 +00:00
|
|
|
}
|
2013-02-01 14:15:25 +00:00
|
|
|
m_iterator = m_iterator_aligned;
|
2013-02-01 11:30:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t StreamBuffer::Upload ( u8* data, size_t size )
|
|
|
|
{
|
|
|
|
switch(m_uploadtype) {
|
2013-02-01 14:15:25 +00:00
|
|
|
case MAP_AND_SYNC:
|
2013-02-01 11:30:08 +00:00
|
|
|
case MAP_AND_ORPHAN:
|
2013-01-31 22:11:53 +00:00
|
|
|
pointer = (u8*)glMapBufferRange(m_buffertype, m_iterator, size, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
|
|
|
|
if(pointer) {
|
|
|
|
memcpy(pointer, data, size);
|
|
|
|
glUnmapBuffer(m_buffertype);
|
|
|
|
} else {
|
2013-03-31 23:10:21 +00:00
|
|
|
ERROR_LOG(VIDEO, "Buffer mapping failed");
|
2013-01-31 22:11:53 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-02-01 16:04:27 +00:00
|
|
|
case PINNED_MEMORY:
|
2013-02-04 23:53:14 +00:00
|
|
|
case MAP_AND_RISK:
|
2013-02-07 09:34:29 +00:00
|
|
|
if(pointer)
|
|
|
|
memcpy(pointer+m_iterator, data, size);
|
2013-02-01 16:04:27 +00:00
|
|
|
break;
|
2013-02-01 11:30:08 +00:00
|
|
|
case BUFFERSUBDATA:
|
|
|
|
glBufferSubData(m_buffertype, m_iterator, size, data);
|
|
|
|
break;
|
2013-07-27 00:40:16 +00:00
|
|
|
case BUFFERDATA:
|
|
|
|
glBufferData(m_buffertype, size, data, GL_STREAM_DRAW);
|
|
|
|
break;
|
2013-02-01 16:04:27 +00:00
|
|
|
case STREAM_DETECT:
|
2013-03-23 20:37:01 +00:00
|
|
|
case DETECT_MASK: // Just to shutup warnings
|
2013-02-01 16:04:27 +00:00
|
|
|
break;
|
2013-01-31 22:11:53 +00:00
|
|
|
}
|
2013-02-01 15:43:08 +00:00
|
|
|
size_t ret = m_iterator;
|
2013-01-31 22:11:53 +00:00
|
|
|
m_iterator += size;
|
2013-02-01 15:43:08 +00:00
|
|
|
return ret;
|
2013-01-31 22:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StreamBuffer::Init()
|
|
|
|
{
|
2013-02-01 16:04:27 +00:00
|
|
|
m_iterator = 0;
|
|
|
|
m_used_iterator = 0;
|
|
|
|
m_free_iterator = 0;
|
|
|
|
|
2013-01-31 22:11:53 +00:00
|
|
|
switch(m_uploadtype) {
|
2013-02-01 14:15:25 +00:00
|
|
|
case MAP_AND_SYNC:
|
|
|
|
fences = new GLsync[SYNC_POINTS];
|
|
|
|
for(u32 i=0; i<SYNC_POINTS; i++)
|
|
|
|
fences[i] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
|
|
|
|
|
2013-01-31 22:11:53 +00:00
|
|
|
case MAP_AND_ORPHAN:
|
2013-02-01 11:30:08 +00:00
|
|
|
case BUFFERSUBDATA:
|
2013-01-31 22:11:53 +00:00
|
|
|
glBindBuffer(m_buffertype, m_buffer);
|
|
|
|
glBufferData(m_buffertype, m_size, NULL, GL_STREAM_DRAW);
|
|
|
|
break;
|
2013-02-01 16:04:27 +00:00
|
|
|
case PINNED_MEMORY:
|
2013-03-16 09:08:46 +00:00
|
|
|
glGetError(); // errors before this allocation should be ignored
|
2013-02-01 16:04:27 +00:00
|
|
|
fences = new GLsync[SYNC_POINTS];
|
|
|
|
for(u32 i=0; i<SYNC_POINTS; i++)
|
|
|
|
fences[i] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
|
|
|
|
|
2013-02-04 19:48:28 +00:00
|
|
|
pointer = (u8*)AllocateAlignedMemory(ROUND_UP(m_size,ALIGN_PINNED_MEMORY), ALIGN_PINNED_MEMORY );
|
2013-02-01 16:04:27 +00:00
|
|
|
glBindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, m_buffer);
|
2013-03-16 09:08:46 +00:00
|
|
|
glBufferData(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, ROUND_UP(m_size,ALIGN_PINNED_MEMORY), pointer, GL_STREAM_COPY);
|
2013-02-01 16:04:27 +00:00
|
|
|
glBindBuffer(GL_EXTERNAL_VIRTUAL_MEMORY_BUFFER_AMD, 0);
|
|
|
|
glBindBuffer(m_buffertype, m_buffer);
|
2013-03-16 09:08:46 +00:00
|
|
|
|
|
|
|
// on error, switch to another backend. some old catalyst seems to have broken pinned memory support
|
|
|
|
if(glGetError() != GL_NO_ERROR) {
|
2013-03-31 23:10:21 +00:00
|
|
|
ERROR_LOG(VIDEO, "Pinned memory detected, but not working. Please report this: %s, %s, %s", g_ogl_config.gl_vendor, g_ogl_config.gl_renderer, g_ogl_config.gl_version);
|
2013-03-16 09:08:46 +00:00
|
|
|
Shutdown();
|
|
|
|
m_uploadtype = MAP_AND_SYNC;
|
|
|
|
Init();
|
|
|
|
}
|
2013-02-01 16:04:27 +00:00
|
|
|
break;
|
2013-02-04 23:53:14 +00:00
|
|
|
case MAP_AND_RISK:
|
|
|
|
glBindBuffer(m_buffertype, m_buffer);
|
|
|
|
glBufferData(m_buffertype, m_size, NULL, GL_STREAM_DRAW);
|
2013-08-19 17:20:10 +00:00
|
|
|
pointer = (u8*)glMapBufferRange(m_buffertype, 0, m_size, GL_MAP_WRITE_BIT);
|
2013-02-04 23:53:14 +00:00
|
|
|
glUnmapBuffer(m_buffertype);
|
2013-02-07 09:34:29 +00:00
|
|
|
if(!pointer)
|
2013-03-31 23:10:21 +00:00
|
|
|
ERROR_LOG(VIDEO, "Buffer allocation failed");
|
2013-07-27 00:40:16 +00:00
|
|
|
break;
|
|
|
|
case BUFFERDATA:
|
|
|
|
glBindBuffer(m_buffertype, m_buffer);
|
|
|
|
break;
|
2013-02-01 16:04:27 +00:00
|
|
|
case STREAM_DETECT:
|
2013-03-23 20:37:01 +00:00
|
|
|
case DETECT_MASK: // Just to shutup warnings
|
2013-02-01 16:04:27 +00:00
|
|
|
break;
|
2013-01-31 22:11:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StreamBuffer::Shutdown()
|
|
|
|
{
|
|
|
|
switch(m_uploadtype) {
|
2013-02-01 14:15:25 +00:00
|
|
|
case MAP_AND_SYNC:
|
2013-08-29 19:03:48 +00:00
|
|
|
DeleteFences();
|
2013-02-01 14:15:25 +00:00
|
|
|
break;
|
2013-03-08 09:23:27 +00:00
|
|
|
case MAP_AND_RISK:
|
2013-01-31 22:11:53 +00:00
|
|
|
case MAP_AND_ORPHAN:
|
2013-02-01 11:30:08 +00:00
|
|
|
case BUFFERSUBDATA:
|
2013-07-27 00:40:16 +00:00
|
|
|
case BUFFERDATA:
|
2013-01-31 22:11:53 +00:00
|
|
|
break;
|
2013-02-01 16:04:27 +00:00
|
|
|
case PINNED_MEMORY:
|
2013-08-29 19:03:48 +00:00
|
|
|
DeleteFences();
|
2013-02-01 16:04:27 +00:00
|
|
|
glBindBuffer(m_buffertype, 0);
|
|
|
|
glFinish(); // ogl pipeline must be flushed, else this buffer can be in use
|
2013-02-02 18:26:29 +00:00
|
|
|
FreeAlignedMemory(pointer);
|
2013-02-01 16:04:27 +00:00
|
|
|
break;
|
|
|
|
case STREAM_DETECT:
|
2013-03-23 20:37:01 +00:00
|
|
|
case DETECT_MASK: // Just to shutup warnings
|
2013-02-01 16:04:27 +00:00
|
|
|
break;
|
2013-01-31 22:11:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-29 19:03:48 +00:00
|
|
|
void StreamBuffer::DeleteFences()
|
|
|
|
{
|
|
|
|
for(u32 i=SLOT(m_free_iterator)+1; i < SYNC_POINTS; i++)
|
|
|
|
glDeleteSync(fences[i]);
|
|
|
|
for(u32 i=0; i<SLOT(m_iterator); i++)
|
|
|
|
glDeleteSync(fences[i]);
|
|
|
|
delete [] fences;
|
|
|
|
}
|
|
|
|
|
2013-01-31 22:11:53 +00:00
|
|
|
}
|