create StreamBuffer class for ogl upload

This commit is contained in:
degasus 2013-01-31 23:11:53 +01:00
parent 011e326698
commit 30170575c8
9 changed files with 199 additions and 124 deletions

View File

@ -7,6 +7,7 @@ set(SRCS Src/FramebufferManager.cpp
Src/ProgramShaderCache.cpp
Src/RasterFont.cpp
Src/Render.cpp
Src/StreamBuffer.cpp
Src/TextureCache.cpp
Src/TextureConverter.cpp
Src/VertexShaderCache.cpp

View File

@ -205,6 +205,7 @@
<ClCompile Include="Src\ProgramShaderCache.cpp" />
<ClCompile Include="Src\RasterFont.cpp" />
<ClCompile Include="Src\Render.cpp" />
<ClCompile Include="Src\StreamBuffer.cpp" />
<ClCompile Include="Src\stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
@ -228,6 +229,7 @@
<ClInclude Include="Src\ProgramShaderCache.h" />
<ClInclude Include="Src\RasterFont.h" />
<ClInclude Include="Src\Render.h" />
<ClInclude Include="Src\StreamBuffer.h" />
<ClInclude Include="Src\stdafx.h" />
<ClInclude Include="Src\TextureCache.h" />
<ClInclude Include="Src\TextureConverter.h" />

View File

@ -33,6 +33,9 @@
<ClCompile Include="Src\Render.cpp">
<Filter>Render</Filter>
</ClCompile>
<ClCompile Include="Src\StreamBuffer.cpp">
<Filter>Render</Filter>
</ClCompile>
<ClCompile Include="Src\TextureCache.cpp">
<Filter>Render</Filter>
</ClCompile>

View File

@ -17,6 +17,7 @@
#include "ProgramShaderCache.h"
#include "MathUtil.h"
#include "StreamBuffer.h"
namespace OGL
{
@ -25,10 +26,8 @@ static const u32 UBO_LENGTH = 1024*1024;
GLuint ProgramShaderCache::CurrentProgram = 0;
ProgramShaderCache::PCache ProgramShaderCache::pshaders;
GLuint ProgramShaderCache::s_ps_vs_ubo;
u32 ProgramShaderCache::s_ubo_iterator;
GLintptr ProgramShaderCache::s_vs_data_offset;
float *ProgramShaderCache::s_ubo_buffer;
u8 *ProgramShaderCache::s_ubo_buffer;
u32 ProgramShaderCache::s_ubo_buffer_size;
bool ProgramShaderCache::s_ubo_dirty;
@ -39,6 +38,8 @@ GLuint ProgramShaderCache::PCacheEntry::prog_format = 0;
u64 ProgramShaderCache::CurrentShaderProgram;
static StreamBuffer *s_buffer;
u64 Create_Pair(u32 key1, u32 key2)
{
return (((u64)key1) << 32) | key2;
@ -195,36 +196,23 @@ void ProgramShaderCache::SetBothShaders(GLuint PS, GLuint VS)
void ProgramShaderCache::SetMultiPSConstant4fv(unsigned int offset, const float *f, unsigned int count)
{
s_ubo_dirty = true;
memcpy(s_ubo_buffer+(offset*4), f, count*4*sizeof(float));
memcpy(s_ubo_buffer+(offset*4*sizeof(float)), f, count*4*sizeof(float));
}
void ProgramShaderCache::SetMultiVSConstant4fv(unsigned int offset, const float *f, unsigned int count)
{
s_ubo_dirty = true;
memcpy(s_ubo_buffer+(offset*4)+s_vs_data_offset/sizeof(float), f, count*4*sizeof(float));
memcpy(s_ubo_buffer+(offset*4*sizeof(float))+s_vs_data_offset, f, count*4*sizeof(float));
}
void ProgramShaderCache::UploadConstants()
{
if(s_ubo_dirty) {
if(s_ubo_iterator + s_ubo_buffer_size >= UBO_LENGTH) {
glBufferData(GL_UNIFORM_BUFFER, UBO_LENGTH, NULL, GL_STREAM_READ);
s_ubo_iterator = 0;
}
void *ubo = glMapBufferRange(GL_UNIFORM_BUFFER, s_ubo_iterator, s_ubo_buffer_size, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_INVALIDATE_RANGE_BIT);
if(ubo) {
memcpy(ubo, s_ubo_buffer, s_ubo_buffer_size);
glUnmapBuffer(GL_UNIFORM_BUFFER);
} else {
glBufferSubData(GL_UNIFORM_BUFFER, s_ubo_iterator, s_ubo_buffer_size, s_ubo_buffer);
}
glBindBufferRange(GL_UNIFORM_BUFFER, 1, s_ps_vs_ubo, s_ubo_iterator, s_vs_data_offset);
glBindBufferRange(GL_UNIFORM_BUFFER, 2, s_ps_vs_ubo, s_ubo_iterator + s_vs_data_offset, s_ubo_buffer_size - s_vs_data_offset);
s_ubo_iterator += s_ubo_buffer_size;
size_t offset = s_buffer->Upload(s_ubo_buffer, s_ubo_buffer_size);
glBindBufferRange(GL_UNIFORM_BUFFER, 1, s_buffer->getBuffer(), offset, s_vs_data_offset);
glBindBufferRange(GL_UNIFORM_BUFFER, 2, s_buffer->getBuffer(), offset + s_vs_data_offset, s_ubo_buffer_size - s_vs_data_offset);
s_ubo_dirty = false;
}
s_ubo_dirty = false;
}
GLuint ProgramShaderCache::GetCurrentProgram(void)
@ -255,12 +243,9 @@ void ProgramShaderCache::Init(void)
// We multiply by *4*4 because we need to get down to basic machine units.
// So multiply by four to get how many floats we have from vec4s
// Then once more to get bytes
glGenBuffers(1, &s_ps_vs_ubo);
glBindBuffer(GL_UNIFORM_BUFFER, s_ps_vs_ubo);
glBufferData(GL_UNIFORM_BUFFER, UBO_LENGTH, NULL, GL_STREAM_READ);
s_ubo_iterator = 0;
s_buffer = new StreamBuffer(GL_UNIFORM_BUFFER, UBO_LENGTH);
s_ubo_buffer = new float[s_ubo_buffer_size/sizeof(float)];
s_ubo_buffer = new u8[s_ubo_buffer_size];
memset(s_ubo_buffer, 0, s_ubo_buffer_size);
s_ubo_dirty = true;
}
@ -306,9 +291,8 @@ void ProgramShaderCache::Shutdown(void)
if (g_ActiveConfig.backend_info.bSupportsGLSLUBO)
{
glBindBuffer(GL_UNIFORM_BUFFER, 0);
glDeleteBuffers(1, &s_ps_vs_ubo);
s_ps_vs_ubo = 0;
delete s_buffer;
s_buffer = 0;
delete [] s_ubo_buffer;
s_ubo_buffer = 0;
}

View File

@ -164,10 +164,8 @@ private:
static GLuint CurrentProgram;
static u64 CurrentShaderProgram;
static GLuint s_ps_vs_ubo;
static u32 s_ubo_iterator;
static GLintptr s_vs_data_offset;
static float *s_ubo_buffer;
static u8 *s_ubo_buffer;
static u32 s_ubo_buffer_size;
static bool s_ubo_dirty;
static void SetProgramVariables(PCacheEntry &entry);

View File

@ -0,0 +1,91 @@
// Copyright (C) 2003 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include "Globals.h"
#include "GLUtil.h"
#include "StreamBuffer.h"
namespace OGL
{
StreamBuffer::StreamBuffer(u32 type, size_t size)
: m_buffertype(type), m_size(size), m_iterator(0)
{
glGenBuffers(1, &m_buffer);
m_uploadtype = MAP_AND_ORPHAN;
Init();
}
StreamBuffer::~StreamBuffer()
{
Shutdown();
glDeleteBuffers(1, &m_buffer);
}
void StreamBuffer::Align ( u32 stride )
{
if(m_iterator) {
m_iterator--;
m_iterator = m_iterator - (m_iterator % stride) + stride;
}
}
size_t StreamBuffer::Upload ( u8* data, size_t size )
{
switch(m_uploadtype) {
case MAP_AND_ORPHAN:
if(m_iterator+size >= m_size) {
glBufferData(m_buffertype, m_size, NULL, GL_STREAM_DRAW);
m_iterator = 0;
}
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 {
ERROR_LOG(VIDEO, "buffer mapping failed");
}
break;
}
size_t ret = m_iterator;
m_iterator += size;
return ret;
}
void StreamBuffer::Init()
{
switch(m_uploadtype) {
case MAP_AND_ORPHAN:
glBindBuffer(m_buffertype, m_buffer);
glBufferData(m_buffertype, m_size, NULL, GL_STREAM_DRAW);
break;
}
}
void StreamBuffer::Shutdown()
{
switch(m_uploadtype) {
case MAP_AND_ORPHAN:
break;
}
}
}

View File

@ -0,0 +1,57 @@
// Copyright (C) 2003 Dolphin Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#ifndef STREAMBUFFER_H
#define STREAMBUFFER_H
#include "VideoCommon.h"
#include "FramebufferManager.h"
namespace OGL
{
enum StreamType {
MAP_AND_ORPHAN
};
class StreamBuffer {
public:
StreamBuffer(u32 type, size_t size);
~StreamBuffer();
size_t Upload(u8 *data, size_t size);
u32 getBuffer() { return m_buffer; }
void Align(u32 stride);
private:
void Init();
void Shutdown();
StreamType m_uploadtype;
u32 m_buffer;
u32 m_buffertype;
size_t m_size;
u8 *pointer;
size_t m_iterator;
};
}
#endif // STREAMBUFFER_H

View File

@ -41,6 +41,7 @@
#include "OpcodeDecoding.h"
#include "FileUtil.h"
#include "Debugger.h"
#include "StreamBuffer.h"
#include "main.h"
@ -55,6 +56,11 @@ const u32 MAX_VBUFFER_SIZE = VertexManager::MAXVBUFFERSIZE * 16;
const u32 MIN_IBUFFER_SIZE = VertexManager::MAXIBUFFERSIZE * 1 * sizeof(u16);
const u32 MIN_VBUFFER_SIZE = VertexManager::MAXVBUFFERSIZE * 1;
static StreamBuffer *s_vertexBuffer;
static StreamBuffer *s_indexBuffer;
static u32 s_baseVertex;
static u32 s_offset[3];
VertexManager::VertexManager()
{
CreateDeviceObjects();
@ -75,31 +81,22 @@ void VertexManager::CreateDeviceObjects()
max_Index_size *= sizeof(u16);
GL_REPORT_ERROR();
m_index_buffer_size = std::min(MAX_IBUFFER_SIZE, std::max(max_Index_size, MIN_IBUFFER_SIZE));
m_vertex_buffer_size = std::min(MAX_VBUFFER_SIZE, std::max(max_Vertex_size, MIN_VBUFFER_SIZE));
u32 index_buffer_size = std::min(MAX_IBUFFER_SIZE, std::max(max_Index_size, MIN_IBUFFER_SIZE));
u32 vertex_buffer_size = std::min(MAX_VBUFFER_SIZE, std::max(max_Vertex_size, MIN_VBUFFER_SIZE));
// should be not bigger, but we need it. so try and have luck
if (m_index_buffer_size > max_Index_size) {
if (index_buffer_size > max_Index_size) {
ERROR_LOG(VIDEO, "GL_MAX_ELEMENTS_INDICES to small, so try it anyway. good luck\n");
}
if (m_vertex_buffer_size > max_Vertex_size) {
if (vertex_buffer_size > max_Vertex_size) {
ERROR_LOG(VIDEO, "GL_MAX_ELEMENTS_VERTICES to small, so try it anyway. good luck\n");
}
glGenBuffers(1, &m_vertex_buffers);
GL_REPORT_ERROR();
glGenBuffers(1, &m_index_buffers);
GL_REPORT_ERROR();
glBindBuffer(GL_ARRAY_BUFFER, m_vertex_buffers );
GL_REPORT_ERROR();
glBufferData(GL_ARRAY_BUFFER, m_vertex_buffer_size, NULL, GL_STREAM_READ );
GL_REPORT_ERROR();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_index_buffers );
GL_REPORT_ERROR();
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_index_buffer_size, NULL, GL_STREAM_READ );
GL_REPORT_ERROR();
m_index_buffer_cursor = 0;
m_vertex_buffer_cursor = 0;
s_vertexBuffer = new StreamBuffer(GL_ARRAY_BUFFER, vertex_buffer_size);
m_vertex_buffers = s_vertexBuffer->getBuffer();
s_indexBuffer = new StreamBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer_size);
m_index_buffers = s_indexBuffer->getBuffer();
m_CurrentVertexFmt = NULL;
m_last_vao = 0;
}
@ -110,81 +107,34 @@ void VertexManager::DestroyDeviceObjects()
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0 );
GL_REPORT_ERROR();
glDeleteBuffers(1, &m_vertex_buffers);
GL_REPORT_ERROR();
glDeleteBuffers(1, &m_index_buffers);
delete s_vertexBuffer;
delete s_indexBuffer;
GL_REPORT_ERROR();
}
void VertexManager::PrepareDrawBuffers(u32 stride)
{
u8* pVertices = NULL;
u16* pIndices = NULL;
int vertex_data_size = IndexGenerator::GetNumVerts() * stride;
int triangle_index_size = IndexGenerator::GetTriangleindexLen();
int line_index_size = IndexGenerator::GetLineindexLen();
int point_index_size = IndexGenerator::GetPointindexLen();
int index_data_size = (triangle_index_size + line_index_size + point_index_size) * sizeof(u16);
m_vertex_buffer_cursor--;
m_vertex_buffer_cursor = m_vertex_buffer_cursor - (m_vertex_buffer_cursor % stride) + stride;
s_vertexBuffer->Align(stride);
u32 offset = s_vertexBuffer->Upload(LocalVBuffer, vertex_data_size);
if (m_vertex_buffer_cursor >= m_vertex_buffer_size - vertex_data_size || m_index_buffer_cursor >= m_index_buffer_size - index_data_size)
s_baseVertex = offset / stride;
if(triangle_index_size)
{
m_vertex_buffer_cursor = 0;
m_index_buffer_cursor = 0;
glBufferData(GL_ARRAY_BUFFER, m_vertex_buffer_size, NULL, GL_STREAM_READ);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_index_buffer_size, NULL, GL_STREAM_READ);
s_offset[0] = s_indexBuffer->Upload((u8*)TIBuffer, triangle_index_size * sizeof(u16));
}
pVertices = (u8*)glMapBufferRange(GL_ARRAY_BUFFER, m_vertex_buffer_cursor, vertex_data_size, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_INVALIDATE_RANGE_BIT);
if(pVertices)
if(line_index_size)
{
memcpy(pVertices, LocalVBuffer, vertex_data_size);
glUnmapBuffer(GL_ARRAY_BUFFER);
s_offset[1] = s_indexBuffer->Upload((u8*)LIBuffer, line_index_size * sizeof(u16));
}
else // could that happen? out-of-memory?
if(point_index_size)
{
glBufferSubData(GL_ARRAY_BUFFER, m_vertex_buffer_cursor, vertex_data_size, LocalVBuffer);
}
pIndices = (u16*)glMapBufferRange(GL_ELEMENT_ARRAY_BUFFER, m_index_buffer_cursor , index_data_size, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_INVALIDATE_RANGE_BIT);
if(pIndices)
{
if(triangle_index_size)
{
memcpy(pIndices, TIBuffer, triangle_index_size * sizeof(u16));
pIndices += triangle_index_size;
}
if(line_index_size)
{
memcpy(pIndices, LIBuffer, line_index_size * sizeof(u16));
pIndices += line_index_size;
}
if(point_index_size)
{
memcpy(pIndices, PIBuffer, point_index_size * sizeof(u16));
}
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
}
else // could that happen? out-of-memory?
{
if(triangle_index_size)
{
triangle_index_size *= sizeof(u16);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, m_index_buffer_cursor, triangle_index_size, TIBuffer);
}
if(line_index_size)
{
line_index_size *= sizeof(u16);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, m_index_buffer_cursor + triangle_index_size, line_index_size, LIBuffer);
}
if(point_index_size)
{
point_index_size *= sizeof(u16);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, m_index_buffer_cursor + triangle_index_size + line_index_size, point_index_size, PIBuffer);
}
s_offset[2] = s_indexBuffer->Upload((u8*)PIBuffer, point_index_size * sizeof(u16));
}
}
@ -193,23 +143,19 @@ void VertexManager::Draw(u32 stride)
int triangle_index_size = IndexGenerator::GetTriangleindexLen();
int line_index_size = IndexGenerator::GetLineindexLen();
int point_index_size = IndexGenerator::GetPointindexLen();
int StartIndex = m_index_buffer_cursor;
int basevertex = m_vertex_buffer_cursor / stride;
if (triangle_index_size > 0)
{
glDrawElementsBaseVertex(GL_TRIANGLES, triangle_index_size, GL_UNSIGNED_SHORT, (u8*)NULL+StartIndex, basevertex);
StartIndex += triangle_index_size * sizeof(u16);
glDrawElementsBaseVertex(GL_TRIANGLES, triangle_index_size, GL_UNSIGNED_SHORT, (u8*)NULL+s_offset[0], s_baseVertex);
INCSTAT(stats.thisFrame.numIndexedDrawCalls);
}
if (line_index_size > 0)
{
glDrawElementsBaseVertex(GL_LINES, line_index_size, GL_UNSIGNED_SHORT, (u8*)NULL+StartIndex, basevertex);
StartIndex += line_index_size * sizeof(u16);
glDrawElementsBaseVertex(GL_LINES, line_index_size, GL_UNSIGNED_SHORT, (u8*)NULL+s_offset[1], s_baseVertex);
INCSTAT(stats.thisFrame.numIndexedDrawCalls);
}
if (point_index_size > 0)
{
glDrawElementsBaseVertex(GL_POINTS, point_index_size, GL_UNSIGNED_SHORT, (u8*)NULL+StartIndex, basevertex);
glDrawElementsBaseVertex(GL_POINTS, point_index_size, GL_UNSIGNED_SHORT, (u8*)NULL+s_offset[2], s_baseVertex);
INCSTAT(stats.thisFrame.numIndexedDrawCalls);
}
}
@ -362,9 +308,6 @@ void VertexManager::vFlush()
}
GFX_DEBUGGER_PAUSE_AT(NEXT_FLUSH, true);
m_index_buffer_cursor += (IndexGenerator::GetTriangleindexLen() + IndexGenerator::GetLineindexLen() + IndexGenerator::GetPointindexLen()) * sizeof(u16);
m_vertex_buffer_cursor += IndexGenerator::GetNumVerts() * stride;
ResetBuffer();
#if defined(_DEBUG) || defined(DEBUGFAST)
if (g_ActiveConfig.iLog & CONF_SAVESHADERS)

View File

@ -57,10 +57,6 @@ private:
void Draw(u32 stride);
void vFlush();
void PrepareDrawBuffers(u32 stride);
u32 m_vertex_buffer_cursor;
u32 m_vertex_buffer_size;
u32 m_index_buffer_cursor;
u32 m_index_buffer_size;
NativeVertexFormat *m_CurrentVertexFmt;
};