From ca8554e7d1d7c77b3c2a9ed8a7fb12de7096c67b Mon Sep 17 00:00:00 2001 From: degasus Date: Fri, 29 Mar 2013 14:27:33 +0100 Subject: [PATCH] first try of primitive restart index generator Convert all quads+triangles into trangle_strip and uses primitive restart to split them. Speed up triangle_strip, but slows down all others primitive formats. Only implemented in ogl. --- .../Core/VideoCommon/Src/IndexGenerator.cpp | 44 +++++++++---- Source/Core/VideoCommon/Src/VertexLoader.cpp | 4 ++ .../VideoCommon/Src/VertexManagerBase.cpp | 64 +++++++++++++------ Source/Core/VideoCommon/Src/VideoConfig.h | 1 + Source/Plugins/Plugin_VideoDX11/Src/main.cpp | 1 + Source/Plugins/Plugin_VideoDX9/Src/main.cpp | 1 + Source/Plugins/Plugin_VideoOGL/Src/Render.cpp | 16 ++++- .../Plugin_VideoOGL/Src/VertexManager.cpp | 4 +- 8 files changed, 102 insertions(+), 33 deletions(-) diff --git a/Source/Core/VideoCommon/Src/IndexGenerator.cpp b/Source/Core/VideoCommon/Src/IndexGenerator.cpp index aa1a971687..05097b7c9a 100644 --- a/Source/Core/VideoCommon/Src/IndexGenerator.cpp +++ b/Source/Core/VideoCommon/Src/IndexGenerator.cpp @@ -18,6 +18,7 @@ #include #include "Common.h" +#include "VideoConfig.h" #include "IndexGenerator.h" /* @@ -90,7 +91,9 @@ __forceinline void IndexGenerator::WriteTriangle(u32 index1, u32 index2, u32 ind *Tptr++ = index1; *Tptr++ = index2; *Tptr++ = index3; - + if(g_Config.backend_info.bSupportsPrimitiveRestart) + *Tptr++ = 65535; + ++numT; } @@ -105,15 +108,25 @@ void IndexGenerator::AddList(u32 const numVerts) void IndexGenerator::AddStrip(u32 const numVerts) { - bool wind = false; - for (u32 i = 2; i < numVerts; ++i) - { - WriteTriangle( - index + i - 2, - index + i - !wind, - index + i - wind); + if(g_Config.backend_info.bSupportsPrimitiveRestart) { + for (u32 i = 0; i < numVerts; ++i) + { + *Tptr++ = index + i; + } + *Tptr++ = 65535; + numT += numVerts - 2; + + } else { + bool wind = false; + for (u32 i = 2; i < numVerts; ++i) + { + WriteTriangle( + index + i - 2, + index + i - !wind, + index + i - wind); - wind ^= true; + wind ^= true; + } } } @@ -130,8 +143,17 @@ void IndexGenerator::AddQuads(u32 numVerts) auto const numQuads = numVerts / 4; for (u32 i = 0; i != numQuads; ++i) { - WriteTriangle(index + i * 4, index + i * 4 + 1, index + i * 4 + 2); - WriteTriangle(index + i * 4, index + i * 4 + 2, index + i * 4 + 3); + if(g_Config.backend_info.bSupportsPrimitiveRestart) { + *Tptr++ = index + i * 4 + 0; + *Tptr++ = index + i * 4 + 1; + *Tptr++ = index + i * 4 + 3; + *Tptr++ = index + i * 4 + 2; + *Tptr++ = 65535; + numT += 2; + } else { + WriteTriangle(index + i * 4, index + i * 4 + 1, index + i * 4 + 2); + WriteTriangle(index + i * 4, index + i * 4 + 2, index + i * 4 + 3); + } } } diff --git a/Source/Core/VideoCommon/Src/VertexLoader.cpp b/Source/Core/VideoCommon/Src/VertexLoader.cpp index 5df495a44c..74c1bd9705 100644 --- a/Source/Core/VideoCommon/Src/VertexLoader.cpp +++ b/Source/Core/VideoCommon/Src/VertexLoader.cpp @@ -659,6 +659,10 @@ void VertexLoader::SetVAT(u32 _group0, u32 _group1, u32 _group2) m_VtxAttr.texCoord[7].Elements = vat.g2.Tex7CoordElements; m_VtxAttr.texCoord[7].Format = vat.g2.Tex7CoordFormat; m_VtxAttr.texCoord[7].Frac = vat.g2.Tex7Frac; + + if(!m_VtxAttr.ByteDequant) { + ERROR_LOG(VIDEO, "ByteDequant is set to zero"); + } }; void VertexLoader::AppendToString(std::string *dest) const diff --git a/Source/Core/VideoCommon/Src/VertexManagerBase.cpp b/Source/Core/VideoCommon/Src/VertexManagerBase.cpp index 87856b5aeb..076c45e4eb 100644 --- a/Source/Core/VideoCommon/Src/VertexManagerBase.cpp +++ b/Source/Core/VideoCommon/Src/VertexManagerBase.cpp @@ -74,28 +74,54 @@ bool VertexManager::IsFlushed() const u32 VertexManager::GetRemainingIndices(int primitive) { - switch (primitive) - { - case GX_DRAW_QUADS: - return (MAXIBUFFERSIZE - IndexGenerator::GetTriangleindexLen()) / 6 * 4; - case GX_DRAW_TRIANGLES: - return (MAXIBUFFERSIZE - IndexGenerator::GetTriangleindexLen()); - case GX_DRAW_TRIANGLE_STRIP: - return (MAXIBUFFERSIZE - IndexGenerator::GetTriangleindexLen()) / 3 + 2; - case GX_DRAW_TRIANGLE_FAN: - return (MAXIBUFFERSIZE - IndexGenerator::GetTriangleindexLen()) / 3 + 2; + + if(g_Config.backend_info.bSupportsPrimitiveRestart) { + switch (primitive) + { + case GX_DRAW_QUADS: + return (MAXIBUFFERSIZE - IndexGenerator::GetTriangleindexLen()) / 5 * 4; + case GX_DRAW_TRIANGLES: + return (MAXIBUFFERSIZE - IndexGenerator::GetTriangleindexLen()) / 4 * 3; + case GX_DRAW_TRIANGLE_STRIP: + return (MAXIBUFFERSIZE - IndexGenerator::GetTriangleindexLen()) / 1 - 1; + case GX_DRAW_TRIANGLE_FAN: + return (MAXIBUFFERSIZE - IndexGenerator::GetTriangleindexLen()) / 4 + 2; - case GX_DRAW_LINES: - return (MAXIBUFFERSIZE - IndexGenerator::GetLineindexLen()); - case GX_DRAW_LINE_STRIP: - return (MAXIBUFFERSIZE - IndexGenerator::GetLineindexLen()) / 2 + 1; + case GX_DRAW_LINES: + return (MAXIBUFFERSIZE - IndexGenerator::GetLineindexLen()); + case GX_DRAW_LINE_STRIP: + return (MAXIBUFFERSIZE - IndexGenerator::GetLineindexLen()) / 2 + 1; - case GX_DRAW_POINTS: - return (MAXIBUFFERSIZE - IndexGenerator::GetPointindexLen()); + case GX_DRAW_POINTS: + return (MAXIBUFFERSIZE - IndexGenerator::GetPointindexLen()); - default: - return 0; - } + default: + return 0; + } + } else { + switch (primitive) + { + case GX_DRAW_QUADS: + return (MAXIBUFFERSIZE - IndexGenerator::GetTriangleindexLen()) / 6 * 4; + case GX_DRAW_TRIANGLES: + return (MAXIBUFFERSIZE - IndexGenerator::GetTriangleindexLen()); + case GX_DRAW_TRIANGLE_STRIP: + return (MAXIBUFFERSIZE - IndexGenerator::GetTriangleindexLen()) / 3 + 2; + case GX_DRAW_TRIANGLE_FAN: + return (MAXIBUFFERSIZE - IndexGenerator::GetTriangleindexLen()) / 3 + 2; + + case GX_DRAW_LINES: + return (MAXIBUFFERSIZE - IndexGenerator::GetLineindexLen()); + case GX_DRAW_LINE_STRIP: + return (MAXIBUFFERSIZE - IndexGenerator::GetLineindexLen()) / 2 + 1; + + case GX_DRAW_POINTS: + return (MAXIBUFFERSIZE - IndexGenerator::GetPointindexLen()); + + default: + return 0; + } + } } void VertexManager::AddVertices(int primitive, u32 numVertices) diff --git a/Source/Core/VideoCommon/Src/VideoConfig.h b/Source/Core/VideoCommon/Src/VideoConfig.h index 442e95f9bf..88326346f2 100644 --- a/Source/Core/VideoCommon/Src/VideoConfig.h +++ b/Source/Core/VideoCommon/Src/VideoConfig.h @@ -153,6 +153,7 @@ struct VideoConfig bool bSupportsDualSourceBlend; // only supported by D3D11 and OpenGL bool bSupportsFormatReinterpretation; bool bSupportsPixelLighting; + bool bSupportsPrimitiveRestart; bool bSupportsGLSLUBO; // needed by pixelShaderGen, so must stay in videoCommon } backend_info; diff --git a/Source/Plugins/Plugin_VideoDX11/Src/main.cpp b/Source/Plugins/Plugin_VideoDX11/Src/main.cpp index 17bbdcf33c..b7c27c895c 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/main.cpp +++ b/Source/Plugins/Plugin_VideoDX11/Src/main.cpp @@ -96,6 +96,7 @@ void InitBackendInfo() g_Config.backend_info.bSupportsDualSourceBlend = true; g_Config.backend_info.bSupportsFormatReinterpretation = true; g_Config.backend_info.bSupportsPixelLighting = true; + g_Config.backend_info.bSupportsPrimitiveRestart = false; // TODO: dx11 does support it, but it isn't implemented IDXGIFactory* factory; IDXGIAdapter* ad; diff --git a/Source/Plugins/Plugin_VideoDX9/Src/main.cpp b/Source/Plugins/Plugin_VideoDX9/Src/main.cpp index 10fab8ea5a..0db667cc66 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/main.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/main.cpp @@ -101,6 +101,7 @@ void InitBackendInfo() g_Config.backend_info.bUseRGBATextures = false; g_Config.backend_info.bUseMinimalMipCount = true; g_Config.backend_info.bSupports3DVision = true; + g_Config.backend_info.bSupportsPrimitiveRestart = false; // TODO: figure out if it does OSVERSIONINFO info; ZeroMemory(&info, sizeof(OSVERSIONINFO)); info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp index 7c950e9e85..c9bfe86190 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/Render.cpp @@ -293,6 +293,7 @@ Renderer::Renderer() g_Config.backend_info.bSupportsDualSourceBlend = GLEW_ARB_blend_func_extended; g_Config.backend_info.bSupportsGLSLUBO = GLEW_ARB_uniform_buffer_object; + g_Config.backend_info.bSupportsPrimitiveRestart = false; //GLEW_VERSION_3_1; g_ogl_config.bSupportsGLSLCache = GLEW_ARB_get_program_binary; g_ogl_config.bSupportsGLPinnedMemory = GLEW_AMD_pinned_memory; @@ -328,9 +329,10 @@ Renderer::Renderer() g_ogl_config.gl_renderer, g_ogl_config.gl_version).c_str(), 5000); - OSD::AddMessage(StringFromFormat("Missing Extensions: %s%s%s%s%s%s%s%s", + OSD::AddMessage(StringFromFormat("Missing Extensions: %s%s%s%s%s%s%s%s%s", g_ActiveConfig.backend_info.bSupportsDualSourceBlend ? "" : "DualSourceBlend ", g_ActiveConfig.backend_info.bSupportsGLSLUBO ? "" : "UniformBuffer ", + g_ActiveConfig.backend_info.bSupportsPrimitiveRestart ? "" : "PrimitiveRestart ", g_ogl_config.bSupportsGLPinnedMemory ? "" : "PinnedMemory ", g_ogl_config.bSupportsGLSLCache ? "" : "ShaderCache ", g_ogl_config.bSupportsGLBaseVertex ? "" : "BaseVertex ", @@ -1391,6 +1393,12 @@ void Renderer::ResetAPIState() glDisable(GL_BLEND); glDepthMask(GL_FALSE); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); + + + if(g_ActiveConfig.backend_info.bSupportsPrimitiveRestart) + { + glDisable(GL_PRIMITIVE_RESTART); + } } void Renderer::RestoreAPIState() @@ -1411,6 +1419,12 @@ void Renderer::RestoreAPIState() vm->m_last_vao = 0; TextureCache::SetStage(); + + if(g_ActiveConfig.backend_info.bSupportsPrimitiveRestart) + { + glEnable(GL_PRIMITIVE_RESTART); + glPrimitiveRestartIndex(65535); + } } void Renderer::SetGenerationMode() diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp index 228eeb757d..ee8e52a265 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp @@ -127,7 +127,7 @@ void VertexManager::Draw(u32 stride) if(g_ogl_config.bSupportsGLBaseVertex) { if (triangle_index_size > 0) { - glDrawElementsBaseVertex(GL_TRIANGLES, triangle_index_size, GL_UNSIGNED_SHORT, (u8*)NULL+s_offset[0], s_baseVertex); + glDrawElementsBaseVertex(g_ActiveConfig.backend_info.bSupportsPrimitiveRestart?GL_TRIANGLE_STRIP:GL_TRIANGLES, triangle_index_size, GL_UNSIGNED_SHORT, (u8*)NULL+s_offset[0], s_baseVertex); INCSTAT(stats.thisFrame.numIndexedDrawCalls); } if (line_index_size > 0) @@ -143,7 +143,7 @@ void VertexManager::Draw(u32 stride) } else { if (triangle_index_size > 0) { - glDrawElements(GL_TRIANGLES, triangle_index_size, GL_UNSIGNED_SHORT, (u8*)NULL+s_offset[0]); + glDrawElements(g_ActiveConfig.backend_info.bSupportsPrimitiveRestart?GL_TRIANGLE_STRIP:GL_TRIANGLES, triangle_index_size, GL_UNSIGNED_SHORT, (u8*)NULL+s_offset[0]); INCSTAT(stats.thisFrame.numIndexedDrawCalls); } if (line_index_size > 0)