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