2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 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
|
|
|
|
2017-02-01 08:56:13 -07:00
|
|
|
#include "VideoBackends/OGL/VertexManager.h"
|
|
|
|
|
2008-12-07 22:25:12 -07:00
|
|
|
#include <fstream>
|
2015-12-21 08:15:17 -07:00
|
|
|
#include <memory>
|
2014-06-02 23:08:54 -06:00
|
|
|
#include <string>
|
2008-12-07 22:25:12 -07:00
|
|
|
#include <vector>
|
|
|
|
|
2016-01-02 13:01:12 -07:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/FileUtil.h"
|
2016-01-02 13:01:12 -07:00
|
|
|
#include "Common/GL/GLExtensions/GLExtensions.h"
|
2016-06-24 02:43:46 -06:00
|
|
|
#include "Common/StringUtil.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
|
OGL: implement Bounding Box on systems w/o SSBO
This commit should have zero performance effect if SSBOs are supported.
If they aren't (e.g. on all Macs), this commit alters FramebufferManager
to attach a new stencil buffer and VertexManager to draw to it when
bounding box is active. `BBoxRead` gets the pixel data from the buffer
and dumbly loops through it to find the bounding box.
This patch can run Paper Mario: The Thousand-Year Door at almost full
speed (50–60 FPS) without Dual-Core enabled for all common bounding
box-using actions I tested (going through pipes, Plane Mode, Paper
Mode, Prof. Frankly's gate, combat, walking around the overworld, etc.)
on my computer (macOS 10.12.3, 2.8 GHz Intel Core i7, 16 GB 1600 MHz
DDR3, and Intel Iris 1536 MB).
A few more demanding scenes (e.g. the self-building bridge on the way
to Petalburg) slow to ~15% of their speed without this patch (though
they don't run quite at full speed even on master). The slowdown is
caused almost solely by `glReadPixels` in `OGL::BoundingBox::Get`.
Other implementation ideas:
- Use a stencil buffer that's separate from the depth buffer. This would
require ARB_texture_stencil8 / OpenGL 4.4, which isn't available on
macOS.
- Use `glGetTexImage` instead of `glReadPixels`. This is ~5 FPS slower
on my computer, presumably because it has to transfer the entire
combined depth-stencil buffer instead of only the stencil data.
Getting only stencil data from `glGetTexImage` requires
ARB_texture_stencil8 / OpenGL 4.4, which (again) is not available on
macOS.
- Don't use a PBO, and use `glReadPixels` synchronously. This has no
visible performance effect on my computer, and is theoretically
slower.
2017-03-05 16:34:30 -07:00
|
|
|
#include "VideoBackends/OGL/BoundingBox.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoBackends/OGL/ProgramShaderCache.h"
|
|
|
|
#include "VideoBackends/OGL/Render.h"
|
|
|
|
#include "VideoBackends/OGL/StreamBuffer.h"
|
OGL: implement Bounding Box on systems w/o SSBO
This commit should have zero performance effect if SSBOs are supported.
If they aren't (e.g. on all Macs), this commit alters FramebufferManager
to attach a new stencil buffer and VertexManager to draw to it when
bounding box is active. `BBoxRead` gets the pixel data from the buffer
and dumbly loops through it to find the bounding box.
This patch can run Paper Mario: The Thousand-Year Door at almost full
speed (50–60 FPS) without Dual-Core enabled for all common bounding
box-using actions I tested (going through pipes, Plane Mode, Paper
Mode, Prof. Frankly's gate, combat, walking around the overworld, etc.)
on my computer (macOS 10.12.3, 2.8 GHz Intel Core i7, 16 GB 1600 MHz
DDR3, and Intel Iris 1536 MB).
A few more demanding scenes (e.g. the self-building bridge on the way
to Petalburg) slow to ~15% of their speed without this patch (though
they don't run quite at full speed even on master). The slowdown is
caused almost solely by `glReadPixels` in `OGL::BoundingBox::Get`.
Other implementation ideas:
- Use a stencil buffer that's separate from the depth buffer. This would
require ARB_texture_stencil8 / OpenGL 4.4, which isn't available on
macOS.
- Use `glGetTexImage` instead of `glReadPixels`. This is ~5 FPS slower
on my computer, presumably because it has to transfer the entire
combined depth-stencil buffer instead of only the stencil data.
Getting only stencil data from `glGetTexImage` requires
ARB_texture_stencil8 / OpenGL 4.4, which (again) is not available on
macOS.
- Don't use a PBO, and use `glReadPixels` synchronously. This has no
visible performance effect on my computer, and is theoretically
slower.
2017-03-05 16:34:30 -07:00
|
|
|
#include "VideoCommon/BoundingBox.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
|
|
|
|
#include "VideoCommon/IndexGenerator.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/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
|
|
|
{
|
2016-06-24 02:43:46 -06: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-07 22:25:12 -07:00
|
|
|
|
2015-12-21 08:15:17 -07:00
|
|
|
static std::unique_ptr<StreamBuffer> s_vertexBuffer;
|
|
|
|
static std::unique_ptr<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
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
VertexManager::VertexManager() : m_cpu_v_buffer(MAX_VBUFFER_SIZE), m_cpu_i_buffer(MAX_IBUFFER_SIZE)
|
2010-09-27 20:15:02 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
CreateDeviceObjects();
|
2012-10-26 08:34:02 -06:00
|
|
|
}
|
2013-01-07 12:47:34 -07:00
|
|
|
|
2012-10-26 08:34:02 -06:00
|
|
|
VertexManager::~VertexManager()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
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()
|
|
|
|
{
|
2016-06-24 02:43:46 -06: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
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
s_indexBuffer = StreamBuffer::Create(GL_ELEMENT_ARRAY_BUFFER, MAX_IBUFFER_SIZE);
|
|
|
|
m_index_buffers = s_indexBuffer->m_buffer;
|
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()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
s_vertexBuffer.reset();
|
|
|
|
s_indexBuffer.reset();
|
2012-10-20 07:22:15 -06:00
|
|
|
}
|
|
|
|
|
2017-09-02 16:05:49 -06:00
|
|
|
GLuint VertexManager::GetVertexBufferHandle() const
|
|
|
|
{
|
|
|
|
return m_vertex_buffers;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint VertexManager::GetIndexBufferHandle() const
|
|
|
|
{
|
|
|
|
return m_index_buffers;
|
|
|
|
}
|
|
|
|
|
2012-10-26 08:34:02 -06:00
|
|
|
void VertexManager::PrepareDrawBuffers(u32 stride)
|
2010-06-16 04:12:57 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
u32 vertex_data_size = IndexGenerator::GetNumVerts() * stride;
|
|
|
|
u32 index_data_size = IndexGenerator::GetIndexLen() * sizeof(u16);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
s_vertexBuffer->Unmap(vertex_data_size);
|
|
|
|
s_indexBuffer->Unmap(index_data_size);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
ADDSTAT(stats.thisFrame.bytesVertexStreamed, vertex_data_size);
|
|
|
|
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)
|
|
|
|
{
|
2016-08-21 21:46:52 -06:00
|
|
|
if (m_cull_all)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
|
|
|
// This buffer isn't getting sent to the GPU. Just allocate it on the cpu.
|
2016-08-21 21:46:52 -06: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 02:43:46 -06:00
|
|
|
|
|
|
|
IndexGenerator::Start((u16*)m_cpu_i_buffer.data());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto buffer = s_vertexBuffer->Map(MAXVBUFFERSIZE, stride);
|
2016-08-21 21:46:52 -06:00
|
|
|
m_cur_buffer_pointer = m_base_buffer_pointer = buffer.first;
|
|
|
|
m_end_buffer_pointer = buffer.first + MAXVBUFFERSIZE;
|
2016-06-24 02:43:46 -06: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 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
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
u32 index_size = IndexGenerator::GetIndexLen();
|
|
|
|
u32 max_index = IndexGenerator::GetNumVerts();
|
|
|
|
GLenum primitive_mode = 0;
|
|
|
|
|
2016-08-21 21:46:52 -06:00
|
|
|
switch (m_current_primitive_type)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2017-04-30 02:07:57 -06:00
|
|
|
case PrimitiveType::Points:
|
2016-06-24 02:43:46 -06:00
|
|
|
primitive_mode = GL_POINTS;
|
|
|
|
break;
|
2017-04-30 02:07:57 -06:00
|
|
|
case PrimitiveType::Lines:
|
2016-06-24 02:43:46 -06:00
|
|
|
primitive_mode = GL_LINES;
|
|
|
|
break;
|
2017-04-30 02:07:57 -06:00
|
|
|
case PrimitiveType::Triangles:
|
|
|
|
primitive_mode = GL_TRIANGLES;
|
|
|
|
break;
|
|
|
|
case PrimitiveType::TriangleStrip:
|
|
|
|
primitive_mode = GL_TRIANGLE_STRIP;
|
2016-06-24 02:43:46 -06:00
|
|
|
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);
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
|
|
|
|
2016-12-27 17:37:41 -07:00
|
|
|
void VertexManager::vFlush()
|
2008-12-07 22:25:12 -07:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
GLVertexFormat* nativeVertexFmt = (GLVertexFormat*)VertexLoaderManager::GetCurrentVertexFormat();
|
|
|
|
u32 stride = nativeVertexFmt->GetVertexStride();
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2017-07-19 23:25:29 -06:00
|
|
|
ProgramShaderCache::SetShader(m_current_primitive_type, nativeVertexFmt);
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
PrepareDrawBuffers(stride);
|
2008-12-07 22:25:12 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// upload global constants
|
|
|
|
ProgramShaderCache::UploadConstants();
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2017-03-10 01:07:49 -07:00
|
|
|
if (::BoundingBox::active && !g_Config.BBoxUseFragmentShaderImplementation())
|
OGL: implement Bounding Box on systems w/o SSBO
This commit should have zero performance effect if SSBOs are supported.
If they aren't (e.g. on all Macs), this commit alters FramebufferManager
to attach a new stencil buffer and VertexManager to draw to it when
bounding box is active. `BBoxRead` gets the pixel data from the buffer
and dumbly loops through it to find the bounding box.
This patch can run Paper Mario: The Thousand-Year Door at almost full
speed (50–60 FPS) without Dual-Core enabled for all common bounding
box-using actions I tested (going through pipes, Plane Mode, Paper
Mode, Prof. Frankly's gate, combat, walking around the overworld, etc.)
on my computer (macOS 10.12.3, 2.8 GHz Intel Core i7, 16 GB 1600 MHz
DDR3, and Intel Iris 1536 MB).
A few more demanding scenes (e.g. the self-building bridge on the way
to Petalburg) slow to ~15% of their speed without this patch (though
they don't run quite at full speed even on master). The slowdown is
caused almost solely by `glReadPixels` in `OGL::BoundingBox::Get`.
Other implementation ideas:
- Use a stencil buffer that's separate from the depth buffer. This would
require ARB_texture_stencil8 / OpenGL 4.4, which isn't available on
macOS.
- Use `glGetTexImage` instead of `glReadPixels`. This is ~5 FPS slower
on my computer, presumably because it has to transfer the entire
combined depth-stencil buffer instead of only the stencil data.
Getting only stencil data from `glGetTexImage` requires
ARB_texture_stencil8 / OpenGL 4.4, which (again) is not available on
macOS.
- Don't use a PBO, and use `glReadPixels` synchronously. This has no
visible performance effect on my computer, and is theoretically
slower.
2017-03-05 16:34:30 -07:00
|
|
|
{
|
|
|
|
glEnable(GL_STENCIL_TEST);
|
|
|
|
}
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
Draw(stride);
|
2010-09-30 09:24:34 -06:00
|
|
|
|
2017-03-10 01:07:49 -07:00
|
|
|
if (::BoundingBox::active && !g_Config.BBoxUseFragmentShaderImplementation())
|
OGL: implement Bounding Box on systems w/o SSBO
This commit should have zero performance effect if SSBOs are supported.
If they aren't (e.g. on all Macs), this commit alters FramebufferManager
to attach a new stencil buffer and VertexManager to draw to it when
bounding box is active. `BBoxRead` gets the pixel data from the buffer
and dumbly loops through it to find the bounding box.
This patch can run Paper Mario: The Thousand-Year Door at almost full
speed (50–60 FPS) without Dual-Core enabled for all common bounding
box-using actions I tested (going through pipes, Plane Mode, Paper
Mode, Prof. Frankly's gate, combat, walking around the overworld, etc.)
on my computer (macOS 10.12.3, 2.8 GHz Intel Core i7, 16 GB 1600 MHz
DDR3, and Intel Iris 1536 MB).
A few more demanding scenes (e.g. the self-building bridge on the way
to Petalburg) slow to ~15% of their speed without this patch (though
they don't run quite at full speed even on master). The slowdown is
caused almost solely by `glReadPixels` in `OGL::BoundingBox::Get`.
Other implementation ideas:
- Use a stencil buffer that's separate from the depth buffer. This would
require ARB_texture_stencil8 / OpenGL 4.4, which isn't available on
macOS.
- Use `glGetTexImage` instead of `glReadPixels`. This is ~5 FPS slower
on my computer, presumably because it has to transfer the entire
combined depth-stencil buffer instead of only the stencil data.
Getting only stencil data from `glGetTexImage` requires
ARB_texture_stencil8 / OpenGL 4.4, which (again) is not available on
macOS.
- Don't use a PBO, and use `glReadPixels` synchronously. This has no
visible performance effect on my computer, and is theoretically
slower.
2017-03-05 16:34:30 -07:00
|
|
|
{
|
|
|
|
OGL::BoundingBox::StencilWasUpdated();
|
|
|
|
glDisable(GL_STENCIL_TEST);
|
|
|
|
}
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
g_Config.iSaveTargetId++;
|
|
|
|
ClearEFBCache();
|
2008-12-07 22:25:12 -07:00
|
|
|
}
|
2010-10-02 18:41:06 -06:00
|
|
|
|
2008-12-07 22:25:12 -07:00
|
|
|
} // namespace
|