2013-04-17 21:29:41 -06:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:29:41 -06:00
|
|
|
// Refer to the license.txt file included.
|
2009-04-03 08:35:49 -06:00
|
|
|
|
2008-12-07 22:25:12 -07:00
|
|
|
#include <fstream>
|
2014-06-02 23:08:54 -06:00
|
|
|
#include <string>
|
2008-12-07 22:25:12 -07:00
|
|
|
#include <vector>
|
|
|
|
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/MemoryUtil.h"
|
2014-06-02 23:08:54 -06:00
|
|
|
#include "Common/StringUtil.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
|
|
|
|
#include "VideoBackends/OGL/main.h"
|
|
|
|
#include "VideoBackends/OGL/ProgramShaderCache.h"
|
|
|
|
#include "VideoBackends/OGL/Render.h"
|
|
|
|
#include "VideoBackends/OGL/StreamBuffer.h"
|
|
|
|
#include "VideoBackends/OGL/TextureCache.h"
|
|
|
|
#include "VideoBackends/OGL/VertexManager.h"
|
|
|
|
|
|
|
|
#include "VideoCommon/BPMemory.h"
|
|
|
|
#include "VideoCommon/DriverDetails.h"
|
|
|
|
#include "VideoCommon/Fifo.h"
|
|
|
|
#include "VideoCommon/ImageWrite.h"
|
|
|
|
#include "VideoCommon/IndexGenerator.h"
|
|
|
|
#include "VideoCommon/PixelShaderManager.h"
|
|
|
|
#include "VideoCommon/Statistics.h"
|
2014-07-25 17:10:44 -06:00
|
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoCommon/VertexShaderGen.h"
|
|
|
|
#include "VideoCommon/VertexShaderManager.h"
|
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2010-08-04 15:02:32 -06:00
|
|
|
|
2010-10-02 18:41:06 -06:00
|
|
|
namespace OGL
|
2008-12-07 22:25:12 -07:00
|
|
|
{
|
2013-04-19 07:21:45 -06:00
|
|
|
//This are the initially requested size for the buffers expressed in bytes
|
2013-02-22 02:25:38 -07:00
|
|
|
const u32 MAX_IBUFFER_SIZE = 2*1024*1024;
|
2014-01-22 10:02:55 -07:00
|
|
|
const u32 MAX_VBUFFER_SIZE = 32*1024*1024;
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2013-01-31 15:11:53 -07:00
|
|
|
static StreamBuffer *s_vertexBuffer;
|
|
|
|
static StreamBuffer *s_indexBuffer;
|
2013-11-14 01:11:40 -07:00
|
|
|
static size_t s_baseVertex;
|
2014-01-15 13:44:46 -07:00
|
|
|
static size_t s_index_offset;
|
2010-09-30 09:24:34 -06:00
|
|
|
|
2010-10-02 18:41:06 -06:00
|
|
|
VertexManager::VertexManager()
|
2015-01-24 13:12:52 -07:00
|
|
|
: m_cpu_v_buffer(MAX_VBUFFER_SIZE), m_cpu_i_buffer(MAX_IBUFFER_SIZE)
|
2010-09-27 20:15:02 -06:00
|
|
|
{
|
2012-10-26 08:34:02 -06:00
|
|
|
CreateDeviceObjects();
|
|
|
|
}
|
2013-01-07 12:47:34 -07:00
|
|
|
|
2012-10-26 08:34:02 -06:00
|
|
|
VertexManager::~VertexManager()
|
|
|
|
{
|
|
|
|
DestroyDeviceObjects();
|
2009-09-26 06:39:12 -06:00
|
|
|
}
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2012-10-20 07:22:15 -06:00
|
|
|
void VertexManager::CreateDeviceObjects()
|
|
|
|
{
|
2014-01-22 16:47:49 -07:00
|
|
|
s_vertexBuffer = StreamBuffer::Create(GL_ARRAY_BUFFER, MAX_VBUFFER_SIZE);
|
|
|
|
m_vertex_buffers = s_vertexBuffer->m_buffer;
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2014-01-22 16:47:49 -07:00
|
|
|
s_indexBuffer = StreamBuffer::Create(GL_ELEMENT_ARRAY_BUFFER, MAX_IBUFFER_SIZE);
|
|
|
|
m_index_buffers = s_indexBuffer->m_buffer;
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2012-12-15 06:43:01 -07:00
|
|
|
m_last_vao = 0;
|
2012-10-20 07:22:15 -06:00
|
|
|
}
|
2013-02-22 02:25:38 -07:00
|
|
|
|
2012-10-20 07:22:15 -06:00
|
|
|
void VertexManager::DestroyDeviceObjects()
|
|
|
|
{
|
2013-01-31 15:11:53 -07:00
|
|
|
delete s_vertexBuffer;
|
|
|
|
delete s_indexBuffer;
|
2012-10-20 07:22:15 -06:00
|
|
|
}
|
|
|
|
|
2012-10-26 08:34:02 -06:00
|
|
|
void VertexManager::PrepareDrawBuffers(u32 stride)
|
2010-06-16 04:12:57 -06:00
|
|
|
{
|
2013-02-01 04:30:08 -07:00
|
|
|
u32 vertex_data_size = IndexGenerator::GetNumVerts() * stride;
|
2014-01-15 13:44:46 -07:00
|
|
|
u32 index_data_size = IndexGenerator::GetIndexLen() * sizeof(u16);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2014-01-22 16:47:49 -07:00
|
|
|
s_vertexBuffer->Unmap(vertex_data_size);
|
|
|
|
s_indexBuffer->Unmap(index_data_size);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2013-05-23 13:07:01 -06:00
|
|
|
ADDSTAT(stats.thisFrame.bytesVertexStreamed, vertex_data_size);
|
2014-01-15 13:44:46 -07:00
|
|
|
ADDSTAT(stats.thisFrame.bytesIndexStreamed, index_data_size);
|
2012-10-26 08:34:02 -06:00
|
|
|
}
|
|
|
|
|
2014-01-23 05:41:53 -07:00
|
|
|
void VertexManager::ResetBuffer(u32 stride)
|
|
|
|
{
|
2015-01-24 13:12:52 -07:00
|
|
|
if (s_cull_all)
|
2015-01-23 18:37:20 -07:00
|
|
|
{
|
|
|
|
// This buffer isn't getting sent to the GPU. Just allocate it on the cpu.
|
2015-01-24 13:12:52 -07:00
|
|
|
s_pCurBufferPointer = s_pBaseBufferPointer = m_cpu_v_buffer.data();
|
|
|
|
s_pEndBufferPointer = s_pBaseBufferPointer + m_cpu_v_buffer.size();
|
2015-01-23 18:37:20 -07:00
|
|
|
|
2015-01-24 13:12:52 -07:00
|
|
|
IndexGenerator::Start((u16*)m_cpu_i_buffer.data());
|
2015-01-23 18:37:20 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto buffer = s_vertexBuffer->Map(MAXVBUFFERSIZE, stride);
|
|
|
|
s_pCurBufferPointer = s_pBaseBufferPointer = buffer.first;
|
|
|
|
s_pEndBufferPointer = buffer.first + MAXVBUFFERSIZE;
|
|
|
|
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 05:41:53 -07:00
|
|
|
}
|
|
|
|
|
2012-12-15 06:43:01 -07:00
|
|
|
void VertexManager::Draw(u32 stride)
|
2012-10-26 20:18:09 -06:00
|
|
|
{
|
2014-01-15 13:44:46 -07:00
|
|
|
u32 index_size = IndexGenerator::GetIndexLen();
|
2013-08-20 07:25:02 -06:00
|
|
|
u32 max_index = IndexGenerator::GetNumVerts();
|
2014-01-15 13:44:46 -07:00
|
|
|
GLenum primitive_mode = 0;
|
|
|
|
|
2014-03-10 05:30:55 -06:00
|
|
|
switch (current_primitive_type)
|
2014-01-15 13:44:46 -07:00
|
|
|
{
|
|
|
|
case PRIMITIVE_POINTS:
|
|
|
|
primitive_mode = GL_POINTS;
|
2014-12-15 17:49:29 -07:00
|
|
|
glDisable(GL_CULL_FACE);
|
2014-01-15 13:44:46 -07:00
|
|
|
break;
|
|
|
|
case PRIMITIVE_LINES:
|
|
|
|
primitive_mode = GL_LINES;
|
2014-12-15 17:49:29 -07:00
|
|
|
glDisable(GL_CULL_FACE);
|
2014-01-15 13:44:46 -07:00
|
|
|
break;
|
|
|
|
case PRIMITIVE_TRIANGLES:
|
|
|
|
primitive_mode = g_ActiveConfig.backend_info.bSupportsPrimitiveRestart ? GL_TRIANGLE_STRIP : GL_TRIANGLES;
|
|
|
|
break;
|
|
|
|
}
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2014-08-15 12:09:53 -06:00
|
|
|
if (g_ogl_config.bSupportsGLBaseVertex)
|
|
|
|
{
|
2014-03-09 14:14:26 -06:00
|
|
|
glDrawRangeElementsBaseVertex(primitive_mode, 0, max_index, index_size, GL_UNSIGNED_SHORT, (u8*)nullptr+s_index_offset, (GLint)s_baseVertex);
|
2014-08-15 12:09:53 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-09 14:14:26 -06:00
|
|
|
glDrawRangeElements(primitive_mode, 0, max_index, index_size, GL_UNSIGNED_SHORT, (u8*)nullptr+s_index_offset);
|
2013-10-28 23:23:17 -06:00
|
|
|
}
|
2014-08-15 12:09:53 -06:00
|
|
|
|
2014-06-02 11:36:09 -06:00
|
|
|
INCSTAT(stats.thisFrame.numDrawCalls);
|
2014-12-15 17:49:29 -07:00
|
|
|
|
|
|
|
if (current_primitive_type != PRIMITIVE_TRIANGLES)
|
|
|
|
((OGL::Renderer*)g_renderer)->SetGenerationMode();
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
|
|
|
|
2014-02-03 08:56:17 -07:00
|
|
|
void VertexManager::vFlush(bool useDstAlpha)
|
2008-12-07 22:25:12 -07:00
|
|
|
{
|
2014-07-25 17:10:44 -06:00
|
|
|
GLVertexFormat *nativeVertexFmt = (GLVertexFormat*)VertexLoaderManager::GetCurrentVertexFormat();
|
2012-12-15 09:28:58 -07:00
|
|
|
u32 stride = nativeVertexFmt->GetVertexStride();
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2014-08-15 12:09:53 -06:00
|
|
|
if (m_last_vao != nativeVertexFmt->VAO)
|
|
|
|
{
|
2012-12-15 09:28:58 -07:00
|
|
|
glBindVertexArray(nativeVertexFmt->VAO);
|
|
|
|
m_last_vao = nativeVertexFmt->VAO;
|
|
|
|
}
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2012-10-26 08:34:02 -06:00
|
|
|
PrepareDrawBuffers(stride);
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2011-12-08 04:09:48 -07:00
|
|
|
// Makes sure we can actually do Dual source blending
|
2013-01-14 12:00:33 -07:00
|
|
|
bool dualSourcePossible = g_ActiveConfig.backend_info.bSupportsDualSourceBlend;
|
2010-10-23 13:55:19 -06:00
|
|
|
|
2014-03-18 12:44:23 -06:00
|
|
|
// If host supports GL_ARB_blend_func_extended, we can do dst alpha in
|
|
|
|
// the same pass as regular rendering.
|
|
|
|
if (useDstAlpha && dualSourcePossible)
|
2010-10-23 13:55:19 -06:00
|
|
|
{
|
2014-12-14 11:41:16 -07:00
|
|
|
ProgramShaderCache::SetShader(DSTALPHA_DUAL_SOURCE_BLEND, nativeVertexFmt->m_components, current_primitive_type);
|
2010-10-23 13:55:19 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-14 11:41:16 -07:00
|
|
|
ProgramShaderCache::SetShader(DSTALPHA_NONE, nativeVertexFmt->m_components, current_primitive_type);
|
2010-10-23 13:55:19 -06:00
|
|
|
}
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2014-01-21 02:47:00 -07:00
|
|
|
// upload global constants
|
2013-01-14 05:58:11 -07:00
|
|
|
ProgramShaderCache::UploadConstants();
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2011-12-07 21:04:34 -07:00
|
|
|
// setup the pointers
|
2014-07-25 17:10:44 -06:00
|
|
|
nativeVertexFmt->SetupVertexPointers();
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2012-12-15 06:43:01 -07:00
|
|
|
Draw(stride);
|
2010-09-30 09:24:34 -06:00
|
|
|
|
2010-06-16 04:12:57 -06:00
|
|
|
// run through vertex groups again to set alpha
|
2010-10-23 13:55:19 -06:00
|
|
|
if (useDstAlpha && !dualSourcePossible)
|
2009-04-03 08:35:49 -06:00
|
|
|
{
|
2014-12-14 11:41:16 -07:00
|
|
|
ProgramShaderCache::SetShader(DSTALPHA_ALPHA_PASS, nativeVertexFmt->m_components, current_primitive_type);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2012-08-10 10:57:37 -06:00
|
|
|
// only update alpha
|
|
|
|
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE);
|
|
|
|
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
|
2012-12-15 06:43:01 -07:00
|
|
|
Draw(stride);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2012-08-10 10:57:37 -06:00
|
|
|
// restore color mask
|
|
|
|
g_renderer->SetColorMask();
|
2012-03-29 17:56:24 -06:00
|
|
|
|
2013-10-28 23:23:17 -06:00
|
|
|
if (bpmem.blendmode.blendenable || bpmem.blendmode.subtract)
|
2012-08-10 10:57:37 -06:00
|
|
|
glEnable(GL_BLEND);
|
2010-06-16 04:12:57 -06:00
|
|
|
}
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2010-06-16 04:12:57 -06:00
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
2013-10-28 23:23:17 -06:00
|
|
|
if (g_ActiveConfig.iLog & CONF_SAVESHADERS)
|
2009-04-03 08:35:49 -06:00
|
|
|
{
|
2008-12-07 22:25:12 -07:00
|
|
|
// save the shaders
|
2013-03-04 02:20:55 -07:00
|
|
|
ProgramShaderCache::PCacheEntry prog = ProgramShaderCache::GetShaderProgram();
|
2014-06-02 23:08:54 -06:00
|
|
|
std::string filename = StringFromFormat("%sps%.3d.txt", File::GetUserPath(D_DUMPFRAMES_IDX).c_str(), g_ActiveConfig.iSaveTargetId);
|
2013-02-28 18:33:39 -07:00
|
|
|
std::ofstream fps;
|
2014-06-02 23:08:54 -06:00
|
|
|
OpenFStream(fps, filename, std::ios_base::out);
|
2013-03-04 02:20:55 -07:00
|
|
|
fps << prog.shader.strpprog.c_str();
|
2014-06-02 23:08:54 -06:00
|
|
|
|
|
|
|
filename = StringFromFormat("%svs%.3d.txt", File::GetUserPath(D_DUMPFRAMES_IDX).c_str(), g_ActiveConfig.iSaveTargetId);
|
2013-02-28 18:33:39 -07:00
|
|
|
std::ofstream fvs;
|
2014-06-02 23:08:54 -06:00
|
|
|
OpenFStream(fvs, filename, std::ios_base::out);
|
2013-03-04 02:20:55 -07:00
|
|
|
fvs << prog.shader.strvprog.c_str();
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
|
|
|
|
2013-10-28 23:23:17 -06:00
|
|
|
if (g_ActiveConfig.iLog & CONF_SAVETARGETS)
|
2009-04-03 08:35:49 -06:00
|
|
|
{
|
2014-06-02 23:08:54 -06:00
|
|
|
std::string filename = StringFromFormat("%starg%.3d.png", File::GetUserPath(D_DUMPFRAMES_IDX).c_str(), g_ActiveConfig.iSaveTargetId);
|
2010-07-09 14:56:16 -06:00
|
|
|
TargetRectangle tr;
|
|
|
|
tr.left = 0;
|
|
|
|
tr.right = Renderer::GetTargetWidth();
|
|
|
|
tr.top = 0;
|
|
|
|
tr.bottom = Renderer::GetTargetHeight();
|
2014-06-02 23:08:54 -06:00
|
|
|
g_renderer->SaveScreenshot(filename, tr);
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
g_Config.iSaveTargetId++;
|
|
|
|
|
2011-08-21 22:15:02 -06:00
|
|
|
ClearEFBCache();
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
2010-10-02 18:41:06 -06:00
|
|
|
|
2014-01-22 10:02:55 -07:00
|
|
|
|
2008-12-07 22:25:12 -07:00
|
|
|
} // namespace
|