2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:29:41 +00:00
|
|
|
// Refer to the license.txt file included.
|
2009-04-03 14:35:49 +00:00
|
|
|
|
2017-02-01 15:56:13 +00:00
|
|
|
#include "VideoBackends/OGL/VertexManager.h"
|
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
#include <fstream>
|
2015-12-21 15:15:17 +00:00
|
|
|
#include <memory>
|
2014-06-03 05:08:54 +00:00
|
|
|
#include <string>
|
2008-12-08 05:25:12 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2016-01-02 20:01:12 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2016-01-02 20:01:12 +00:00
|
|
|
#include "Common/GL/GLExtensions/GLExtensions.h"
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
|
|
|
|
#include "VideoBackends/OGL/ProgramShaderCache.h"
|
|
|
|
#include "VideoBackends/OGL/Render.h"
|
|
|
|
#include "VideoBackends/OGL/StreamBuffer.h"
|
|
|
|
|
|
|
|
#include "VideoCommon/IndexGenerator.h"
|
|
|
|
#include "VideoCommon/Statistics.h"
|
2014-07-25 23:10:44 +00:00
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2010-08-04 21:02:32 +00:00
|
|
|
|
2010-10-03 00:41:06 +00:00
|
|
|
namespace OGL
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
// This are the initially requested size for the buffers expressed in bytes
|
|
|
|
const u32 MAX_IBUFFER_SIZE = 2 * 1024 * 1024;
|
|
|
|
const u32 MAX_VBUFFER_SIZE = 32 * 1024 * 1024;
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2015-12-21 15:15:17 +00:00
|
|
|
static std::unique_ptr<StreamBuffer> s_vertexBuffer;
|
|
|
|
static std::unique_ptr<StreamBuffer> s_indexBuffer;
|
2013-11-14 08:11:40 +00:00
|
|
|
static size_t s_baseVertex;
|
2014-01-15 20:44:46 +00:00
|
|
|
static size_t s_index_offset;
|
2010-09-30 15:24:34 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
VertexManager::VertexManager() : m_cpu_v_buffer(MAX_VBUFFER_SIZE), m_cpu_i_buffer(MAX_IBUFFER_SIZE)
|
2010-09-28 02:15:02 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
CreateDeviceObjects();
|
2012-10-26 14:34:02 +00:00
|
|
|
}
|
2013-01-07 19:47:34 +00:00
|
|
|
|
2012-10-26 14:34:02 +00:00
|
|
|
VertexManager::~VertexManager()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
DestroyDeviceObjects();
|
2009-09-26 12:39:12 +00:00
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2012-10-20 13:22:15 +00:00
|
|
|
void VertexManager::CreateDeviceObjects()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
s_vertexBuffer = StreamBuffer::Create(GL_ARRAY_BUFFER, MAX_VBUFFER_SIZE);
|
|
|
|
m_vertex_buffers = s_vertexBuffer->m_buffer;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
s_indexBuffer = StreamBuffer::Create(GL_ELEMENT_ARRAY_BUFFER, MAX_IBUFFER_SIZE);
|
|
|
|
m_index_buffers = s_indexBuffer->m_buffer;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
m_last_vao = 0;
|
2012-10-20 13:22:15 +00:00
|
|
|
}
|
2013-02-22 09:25:38 +00:00
|
|
|
|
2012-10-20 13:22:15 +00:00
|
|
|
void VertexManager::DestroyDeviceObjects()
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
s_vertexBuffer.reset();
|
|
|
|
s_indexBuffer.reset();
|
2012-10-20 13:22:15 +00:00
|
|
|
}
|
|
|
|
|
2012-10-26 14:34:02 +00:00
|
|
|
void VertexManager::PrepareDrawBuffers(u32 stride)
|
2010-06-16 10:12:57 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
u32 vertex_data_size = IndexGenerator::GetNumVerts() * stride;
|
|
|
|
u32 index_data_size = IndexGenerator::GetIndexLen() * sizeof(u16);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
s_vertexBuffer->Unmap(vertex_data_size);
|
|
|
|
s_indexBuffer->Unmap(index_data_size);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
ADDSTAT(stats.thisFrame.bytesVertexStreamed, vertex_data_size);
|
|
|
|
ADDSTAT(stats.thisFrame.bytesIndexStreamed, index_data_size);
|
2012-10-26 14:34:02 +00:00
|
|
|
}
|
|
|
|
|
2014-01-23 12:41:53 +00:00
|
|
|
void VertexManager::ResetBuffer(u32 stride)
|
|
|
|
{
|
2016-08-22 03:46:52 +00:00
|
|
|
if (m_cull_all)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
// This buffer isn't getting sent to the GPU. Just allocate it on the cpu.
|
2016-08-22 03:46:52 +00:00
|
|
|
m_cur_buffer_pointer = m_base_buffer_pointer = m_cpu_v_buffer.data();
|
|
|
|
m_end_buffer_pointer = m_base_buffer_pointer + m_cpu_v_buffer.size();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
IndexGenerator::Start((u16*)m_cpu_i_buffer.data());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto buffer = s_vertexBuffer->Map(MAXVBUFFERSIZE, stride);
|
2016-08-22 03:46:52 +00:00
|
|
|
m_cur_buffer_pointer = m_base_buffer_pointer = buffer.first;
|
|
|
|
m_end_buffer_pointer = buffer.first + MAXVBUFFERSIZE;
|
2016-06-24 08:43:46 +00:00
|
|
|
s_baseVertex = buffer.second / stride;
|
|
|
|
|
|
|
|
buffer = s_indexBuffer->Map(MAXIBUFFERSIZE * sizeof(u16));
|
|
|
|
IndexGenerator::Start((u16*)buffer.first);
|
|
|
|
s_index_offset = buffer.second;
|
|
|
|
}
|
2014-01-23 12:41:53 +00:00
|
|
|
}
|
|
|
|
|
2012-12-15 13:43:01 +00:00
|
|
|
void VertexManager::Draw(u32 stride)
|
2012-10-27 02:18:09 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
u32 index_size = IndexGenerator::GetIndexLen();
|
|
|
|
u32 max_index = IndexGenerator::GetNumVerts();
|
|
|
|
GLenum primitive_mode = 0;
|
|
|
|
|
2016-08-22 03:46:52 +00:00
|
|
|
switch (m_current_primitive_type)
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
case PRIMITIVE_POINTS:
|
|
|
|
primitive_mode = GL_POINTS;
|
|
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
break;
|
|
|
|
case PRIMITIVE_LINES:
|
|
|
|
primitive_mode = GL_LINES;
|
|
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
break;
|
|
|
|
case PRIMITIVE_TRIANGLES:
|
|
|
|
primitive_mode =
|
|
|
|
g_ActiveConfig.backend_info.bSupportsPrimitiveRestart ? GL_TRIANGLE_STRIP : GL_TRIANGLES;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_ogl_config.bSupportsGLBaseVertex)
|
|
|
|
{
|
|
|
|
glDrawRangeElementsBaseVertex(primitive_mode, 0, max_index, index_size, GL_UNSIGNED_SHORT,
|
|
|
|
(u8*)nullptr + s_index_offset, (GLint)s_baseVertex);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glDrawRangeElements(primitive_mode, 0, max_index, index_size, GL_UNSIGNED_SHORT,
|
|
|
|
(u8*)nullptr + s_index_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
INCSTAT(stats.thisFrame.numDrawCalls);
|
|
|
|
|
2016-08-22 03:46:52 +00:00
|
|
|
if (m_current_primitive_type != PRIMITIVE_TRIANGLES)
|
2016-06-24 08:43:46 +00:00
|
|
|
static_cast<Renderer*>(g_renderer.get())->SetGenerationMode();
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 00:37:41 +00:00
|
|
|
void VertexManager::vFlush()
|
2008-12-08 05:25:12 +00:00
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
GLVertexFormat* nativeVertexFmt = (GLVertexFormat*)VertexLoaderManager::GetCurrentVertexFormat();
|
|
|
|
u32 stride = nativeVertexFmt->GetVertexStride();
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
if (m_last_vao != nativeVertexFmt->VAO)
|
|
|
|
{
|
|
|
|
glBindVertexArray(nativeVertexFmt->VAO);
|
|
|
|
m_last_vao = nativeVertexFmt->VAO;
|
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
PrepareDrawBuffers(stride);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2016-12-28 00:37:41 +00:00
|
|
|
ProgramShaderCache::SetShader(m_current_primitive_type);
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// upload global constants
|
|
|
|
ProgramShaderCache::UploadConstants();
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
// setup the pointers
|
|
|
|
nativeVertexFmt->SetupVertexPointers();
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
Draw(stride);
|
2010-09-30 15:24:34 +00:00
|
|
|
|
2010-06-16 10:12:57 +00:00
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
2016-06-24 08:43:46 +00:00
|
|
|
if (g_ActiveConfig.iLog & CONF_SAVESHADERS)
|
|
|
|
{
|
|
|
|
// save the shaders
|
|
|
|
ProgramShaderCache::PCacheEntry prog = ProgramShaderCache::GetShaderProgram();
|
|
|
|
std::string filename = StringFromFormat(
|
|
|
|
"%sps%.3d.txt", File::GetUserPath(D_DUMPFRAMES_IDX).c_str(), g_ActiveConfig.iSaveTargetId);
|
|
|
|
std::ofstream fps;
|
|
|
|
OpenFStream(fps, filename, std::ios_base::out);
|
2016-09-09 10:26:52 +00:00
|
|
|
fps << prog.shader.strpprog;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
filename = StringFromFormat("%svs%.3d.txt", File::GetUserPath(D_DUMPFRAMES_IDX).c_str(),
|
|
|
|
g_ActiveConfig.iSaveTargetId);
|
|
|
|
std::ofstream fvs;
|
|
|
|
OpenFStream(fvs, filename, std::ios_base::out);
|
2016-09-09 10:26:52 +00:00
|
|
|
fvs << prog.shader.strvprog;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2008-12-08 05:25:12 +00:00
|
|
|
#endif
|
2016-06-24 08:43:46 +00:00
|
|
|
g_Config.iSaveTargetId++;
|
2008-12-08 05:25:12 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
ClearEFBCache();
|
2008-12-08 05:25:12 +00:00
|
|
|
}
|
2010-10-03 00:41:06 +00:00
|
|
|
|
2008-12-08 05:25:12 +00:00
|
|
|
} // namespace
|