VideoCommon: Add a separate constants buffer for the geometry shader.

This commit is contained in:
Jules Blok 2014-12-14 21:23:13 +01:00
parent 21ac9aa715
commit b406e4e1f2
19 changed files with 189 additions and 23 deletions

View File

@ -17,6 +17,8 @@
#include "VideoCommon/Debugger.h"
#include "VideoCommon/GeometryShaderGen.h"
#include "VideoCommon/GeometryShaderManager.h"
#include "VideoCommon/Statistics.h"
#include "VideoCommon/VideoConfig.h"
namespace DX11
@ -37,6 +39,22 @@ ID3D11GeometryShader* GeometryShaderCache::GetCopyGeometryShader() { return Copy
ID3D11Buffer* gscbuf = nullptr;
ID3D11Buffer* &GeometryShaderCache::GetConstantBuffer()
{
// TODO: divide the global variables of the generated shaders into about 5 constant buffers to speed this up
if (GeometryShaderManager::dirty)
{
D3D11_MAPPED_SUBRESOURCE map;
D3D::context->Map(gscbuf, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
memcpy(map.pData, &GeometryShaderManager::constants, sizeof(GeometryShaderConstants));
D3D::context->Unmap(gscbuf, 0);
GeometryShaderManager::dirty = false;
ADDSTAT(stats.thisFrame.bytesUniformStreamed, sizeof(GeometryShaderConstants));
}
return gscbuf;
}
// this class will load the precompiled shaders into our cache
class GeometryShaderCacheInserter : public LinearDiskCacheReader<GeometryShaderUid, u8>
{
@ -113,6 +131,12 @@ const char copy_shader_code[] = {
void GeometryShaderCache::Init()
{
unsigned int gbsize = ((sizeof(GeometryShaderConstants))&(~0xf)) + 0x10; // must be a multiple of 16
D3D11_BUFFER_DESC gbdesc = CD3D11_BUFFER_DESC(gbsize, D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
HRESULT hr = D3D::device->CreateBuffer(&gbdesc, nullptr, &gscbuf);
CHECK(hr == S_OK, "Create geometry shader constant buffer (size=%u)", gbsize);
D3D::SetDebugObjectName((ID3D11DeviceChild*)gscbuf, "geometry shader constant buffer used to emulate the GX pipeline");
// used when drawing clear quads
ClearGeometryShader = D3D::CompileAndCreateGeometryShader(clear_shader_code);
CHECK(ClearGeometryShader != nullptr, "Create clear geometry shader");
@ -152,6 +176,8 @@ void GeometryShaderCache::Clear()
void GeometryShaderCache::Shutdown()
{
SAFE_RELEASE(gscbuf);
SAFE_RELEASE(ClearGeometryShader);
SAFE_RELEASE(CopyGeometryShader);

View File

@ -25,6 +25,7 @@ public:
static ID3D11GeometryShader* GeometryShaderCache::GetCopyGeometryShader();
static ID3D11GeometryShader* GetActiveShader() { return last_entry->shader; }
static ID3D11Buffer* &GetConstantBuffer();
private:
struct GSCacheEntry

View File

@ -140,7 +140,7 @@ void VertexManager::Draw(u32 stride)
if (current_primitive_type == PRIMITIVE_TRIANGLES)
{
D3D::stateman->SetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
D3D::stateman->SetGeometryConstants(VertexShaderCache::GetConstantBuffer());
D3D::stateman->SetGeometryConstants(GeometryShaderCache::GetConstantBuffer());
D3D::stateman->SetGeometryShader(g_ActiveConfig.iStereoMode > 0 ? GeometryShaderCache::GetActiveShader() : nullptr);
D3D::stateman->Apply();

View File

@ -28,6 +28,7 @@
#include "VideoCommon/BPStructs.h"
#include "VideoCommon/CommandProcessor.h"
#include "VideoCommon/Fifo.h"
#include "VideoCommon/GeometryShaderManager.h"
#include "VideoCommon/IndexGenerator.h"
#include "VideoCommon/OnScreenDisplay.h"
#include "VideoCommon/OpcodeDecoding.h"
@ -187,6 +188,7 @@ void VideoBackend::Video_Prepare()
OpcodeDecoder_Init();
VertexShaderManager::Init();
PixelShaderManager::Init();
GeometryShaderManager::Init();
CommandProcessor::Init();
PixelEngine::Init();
BBox::Init();
@ -205,6 +207,7 @@ void VideoBackend::Shutdown()
// VideoCommon
Fifo_Shutdown();
CommandProcessor::Shutdown();
GeometryShaderManager::Shutdown();
PixelShaderManager::Shutdown();
VertexShaderManager::Shutdown();
OpcodeDecoder_Shutdown();

View File

@ -13,6 +13,7 @@
#include "VideoCommon/Debugger.h"
#include "VideoCommon/DriverDetails.h"
#include "VideoCommon/GeometryShaderManager.h"
#include "VideoCommon/ImageWrite.h"
#include "VideoCommon/PixelShaderManager.h"
#include "VideoCommon/Statistics.h"
@ -71,11 +72,14 @@ void SHADER::SetProgramVariables()
GLint PSBlock_id = glGetUniformBlockIndex(glprogid, "PSBlock");
GLint VSBlock_id = glGetUniformBlockIndex(glprogid, "VSBlock");
GLint GSBlock_id = glGetUniformBlockIndex(glprogid, "GSBlock");
if (PSBlock_id != -1)
glUniformBlockBinding(glprogid, PSBlock_id, 1);
if (VSBlock_id != -1)
glUniformBlockBinding(glprogid, VSBlock_id, 2);
if (GSBlock_id != -1)
glUniformBlockBinding(glprogid, GSBlock_id, 3);
// Bind Texture Sampler
for (int a = 0; a <= 9; ++a)
@ -133,7 +137,7 @@ void SHADER::Bind()
void ProgramShaderCache::UploadConstants()
{
if (PixelShaderManager::dirty || VertexShaderManager::dirty)
if (PixelShaderManager::dirty || VertexShaderManager::dirty || GeometryShaderManager::dirty)
{
auto buffer = s_buffer->Map(s_ubo_buffer_size, s_ubo_align);
@ -143,14 +147,20 @@ void ProgramShaderCache::UploadConstants()
memcpy(buffer.first + ROUND_UP(sizeof(PixelShaderConstants), s_ubo_align),
&VertexShaderManager::constants, sizeof(VertexShaderConstants));
memcpy(buffer.first + ROUND_UP(sizeof(PixelShaderConstants), s_ubo_align) + ROUND_UP(sizeof(VertexShaderConstants), s_ubo_align),
&GeometryShaderManager::constants, sizeof(GeometryShaderConstants));
s_buffer->Unmap(s_ubo_buffer_size);
glBindBufferRange(GL_UNIFORM_BUFFER, 1, s_buffer->m_buffer, buffer.second,
sizeof(PixelShaderConstants));
glBindBufferRange(GL_UNIFORM_BUFFER, 2, s_buffer->m_buffer, buffer.second + ROUND_UP(sizeof(PixelShaderConstants), s_ubo_align),
sizeof(VertexShaderConstants));
glBindBufferRange(GL_UNIFORM_BUFFER, 3, s_buffer->m_buffer, buffer.second + ROUND_UP(sizeof(PixelShaderConstants), s_ubo_align) + ROUND_UP(sizeof(VertexShaderConstants), s_ubo_align),
sizeof(GeometryShaderConstants));
PixelShaderManager::dirty = false;
VertexShaderManager::dirty = false;
GeometryShaderManager::dirty = false;
ADDSTAT(stats.thisFrame.bytesUniformStreamed, s_ubo_buffer_size);
}
@ -419,7 +429,7 @@ void ProgramShaderCache::Init()
// then the UBO will fail.
glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &s_ubo_align);
s_ubo_buffer_size = ROUND_UP(sizeof(PixelShaderConstants), s_ubo_align) + ROUND_UP(sizeof(VertexShaderConstants), s_ubo_align);
s_ubo_buffer_size = ROUND_UP(sizeof(PixelShaderConstants), s_ubo_align) + ROUND_UP(sizeof(VertexShaderConstants), s_ubo_align) + ROUND_UP(sizeof(GeometryShaderConstants), s_ubo_align);
// We multiply by *4*4 because we need to get down to basic machine units.
// So multiply by four to get how many floats we have from vec4s

View File

@ -65,6 +65,7 @@ Make AA apply instantly during gameplay if possible
#include "VideoCommon/BPStructs.h"
#include "VideoCommon/CommandProcessor.h"
#include "VideoCommon/Fifo.h"
#include "VideoCommon/GeometryShaderManager.h"
#include "VideoCommon/ImageWrite.h"
#include "VideoCommon/IndexGenerator.h"
#include "VideoCommon/LookUpTables.h"
@ -202,6 +203,7 @@ void VideoBackend::Video_Prepare()
IndexGenerator::Init();
VertexShaderManager::Init();
PixelShaderManager::Init();
GeometryShaderManager::Init();
ProgramShaderCache::Init();
g_texture_cache = new TextureCache();
g_sampler_cache = new SamplerCache();
@ -243,6 +245,7 @@ void VideoBackend::Video_Cleanup()
ProgramShaderCache::Shutdown();
VertexShaderManager::Shutdown();
PixelShaderManager::Shutdown();
GeometryShaderManager::Shutdown();
delete g_perf_query;
g_perf_query = nullptr;
delete g_vertex_manager;

View File

@ -14,6 +14,7 @@
#include "VideoCommon/BPFunctions.h"
#include "VideoCommon/BPStructs.h"
#include "VideoCommon/Fifo.h"
#include "VideoCommon/GeometryShaderManager.h"
#include "VideoCommon/PerfQueryBase.h"
#include "VideoCommon/PixelEngine.h"
#include "VideoCommon/PixelShaderManager.h"
@ -125,6 +126,7 @@ static void BPWritten(const BPCmd& bp)
case BPMEM_SCISSOROFFSET: // Scissor Offset
SetScissor();
VertexShaderManager::SetViewportChanged();
GeometryShaderManager::SetViewportChanged();
return;
case BPMEM_LINEPTWIDTH: // Line Width
SetLineWidth();

View File

@ -10,6 +10,7 @@ set(SRCS BoundingBox.cpp
FPSCounter.cpp
FramebufferManagerBase.cpp
GeometryShaderGen.cpp
GeometryShaderManager.cpp
HiresTextures.cpp
ImageWrite.cpp
IndexGenerator.cpp

View File

@ -43,6 +43,9 @@ struct VertexShaderConstants
float4 normalmatrices[32];
float4 posttransformmatrices[64];
float4 pixelcentercorrection;
float4 stereoparams;
};
struct GeometryShaderConstants
{
float4 stereoparams;
};

View File

@ -43,11 +43,12 @@ static inline void GenerateGeometryShader(T& out, u32 components, API_TYPE ApiTy
// uniforms
if (ApiType == API_OPENGL)
out.Write("layout(std140%s) uniform VSBlock {\n", g_ActiveConfig.backend_info.bSupportsBindingLayout ? ", binding = 2" : "");
out.Write("layout(std140%s) uniform GSBlock {\n", g_ActiveConfig.backend_info.bSupportsBindingLayout ? ", binding = 3" : "");
else
out.Write("cbuffer VSBlock {\n");
out.Write(s_shader_uniforms);
out.Write("};\n");
out.Write("cbuffer GSBlock {\n");
out.Write(
"\tfloat4 " I_STEREOPARAMS";\n"
"};\n");
uid_data->numTexGens = xfmem.numTexGen.numTexGens;
uid_data->pixel_lighting = g_ActiveConfig.bEnablePixelLighting;

View File

@ -0,0 +1,82 @@
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
#include <cfloat>
#include <cmath>
#include "VideoCommon/GeometryShaderGen.h"
#include "VideoCommon/GeometryShaderManager.h"
#include "VideoCommon/VideoCommon.h"
#include "VideoCommon/VideoConfig.h"
#include "VideoCommon/XFMemory.h"
// track changes
static bool s_projection_changed, s_viewport_changed;
GeometryShaderConstants GeometryShaderManager::constants;
bool GeometryShaderManager::dirty;
void GeometryShaderManager::Init()
{
memset(&constants, 0, sizeof(constants));
Dirty();
}
void GeometryShaderManager::Shutdown()
{
}
void GeometryShaderManager::Dirty()
{
s_projection_changed = true;
s_viewport_changed = true;
dirty = true;
}
// Syncs the shader constant buffers with xfmem
void GeometryShaderManager::SetConstants()
{
if (s_projection_changed)
{
s_projection_changed = false;
if (g_ActiveConfig.iStereoMode > 0 && xfmem.projection.type == GX_PERSPECTIVE)
{
float offset = (g_ActiveConfig.iStereoSeparation / 1000.0f) * (g_ActiveConfig.iStereoSeparationPercent / 100.0f);
constants.stereoparams[0] = (g_ActiveConfig.bStereoSwapEyes) ? offset : -offset;
constants.stereoparams[1] = (g_ActiveConfig.bStereoSwapEyes) ? -offset : offset;
constants.stereoparams[2] = (g_ActiveConfig.iStereoConvergence / 10.0f) * (g_ActiveConfig.iStereoConvergencePercent / 100.0f);
}
else
{
constants.stereoparams[0] = constants.stereoparams[1] = 0;
}
}
dirty = true;
}
void GeometryShaderManager::SetViewportChanged()
{
s_viewport_changed = true;
}
void GeometryShaderManager::SetProjectionChanged()
{
s_projection_changed = true;
}
void GeometryShaderManager::DoState(PointerWrap &p)
{
p.Do(dirty);
if (p.GetMode() == PointerWrap::MODE_READ)
{
// Reload current state from global GPU state
// NOTE: This requires that all GPU memory has been loaded already.
Dirty();
}
}

View File

@ -0,0 +1,29 @@
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
#pragma once
#include "VideoCommon/ConstantManager.h"
#include "VideoCommon/GeometryShaderGen.h"
class PointerWrap;
// The non-API dependent parts.
class GeometryShaderManager
{
public:
static void Init();
static void Dirty();
static void Shutdown();
static void DoState(PointerWrap &p);
// constant management
static void SetConstants();
static void SetViewportChanged();
static void SetProjectionChanged();
static GeometryShaderConstants constants;
static bool dirty;
};

View File

@ -279,7 +279,8 @@ static inline void GenerateVSOutputStruct(T& object, API_TYPE api_type)
#define I_NORMALMATRICES "cnmtx"
#define I_POSTTRANSFORMMATRICES "cpostmtx"
#define I_PIXELCENTERCORRECTION "cpixelcenter"
#define I_STEREOPARAMS "cstereo"
#define I_STEREOPARAMS "cstereo"
static const char s_shader_uniforms[] =
"\tfloat4 " I_POSNORMALMATRIX"[6];\n"
@ -290,5 +291,4 @@ static const char s_shader_uniforms[] =
"\tfloat4 " I_TRANSFORMMATRICES"[64];\n"
"\tfloat4 " I_NORMALMATRICES"[32];\n"
"\tfloat4 " I_POSTTRANSFORMMATRICES"[64];\n"
"\tfloat4 " I_PIXELCENTERCORRECTION";\n"
"\tfloat4 " I_STEREOPARAMS";\n";
"\tfloat4 " I_PIXELCENTERCORRECTION";\n";

View File

@ -2,6 +2,7 @@
#include "VideoCommon/BPStructs.h"
#include "VideoCommon/Debugger.h"
#include "VideoCommon/GeometryShaderManager.h"
#include "VideoCommon/IndexGenerator.h"
#include "VideoCommon/MainBase.h"
#include "VideoCommon/NativeVertexFormat.h"
@ -223,6 +224,7 @@ void VertexManager::Flush()
// set global constants
VertexShaderManager::SetConstants();
PixelShaderManager::SetConstants();
GeometryShaderManager::SetConstants();
bool useDstAlpha = !g_ActiveConfig.bDstAlphaPass &&
bpmem.dstalpha.enable &&

View File

@ -512,18 +512,6 @@ void VertexShaderManager::SetConstants()
memcpy(constants.projection, correctedMtx.data, 4*16);
}
if (g_ActiveConfig.iStereoMode > 0 && xfmem.projection.type == GX_PERSPECTIVE)
{
float offset = (g_ActiveConfig.iStereoSeparation / 1000.0f) * (g_ActiveConfig.iStereoSeparationPercent / 100.0f);
constants.stereoparams[0] = (g_ActiveConfig.bStereoSwapEyes) ? offset : -offset;
constants.stereoparams[1] = (g_ActiveConfig.bStereoSwapEyes) ? -offset : offset;
constants.stereoparams[2] = (g_ActiveConfig.iStereoConvergence / 10.0f) * (g_ActiveConfig.iStereoConvergencePercent / 100.0f);
}
else
{
constants.stereoparams[0] = constants.stereoparams[1] = 0;
}
dirty = true;
}
}

View File

@ -61,6 +61,7 @@
<ClCompile Include="RenderBase.cpp" />
<ClCompile Include="Statistics.cpp" />
<ClCompile Include="GeometryShaderGen.cpp" />
<ClCompile Include="GeometryShaderManager.cpp" />
<ClCompile Include="TextureCacheBase.cpp" />
<ClCompile Include="TextureConversionShader.cpp" />
<ClCompile Include="VertexLoader.cpp" />
@ -112,6 +113,7 @@
<ClInclude Include="ShaderGenCommon.h" />
<ClInclude Include="Statistics.h" />
<ClInclude Include="GeometryShaderGen.h" />
<ClInclude Include="GeometryShaderManager.h" />
<ClInclude Include="TextureCacheBase.h" />
<ClInclude Include="TextureConversionShader.h" />
<ClInclude Include="TextureDecoder.h" />

View File

@ -146,6 +146,9 @@
<ClCompile Include="GeometryShaderGen.cpp">
<Filter>Shader Generators</Filter>
</ClCompile>
<ClCompile Include="GeometryShaderManager.cpp">
<Filter>Shader Managers</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="CommandProcessor.h" />
@ -284,6 +287,9 @@
<ClInclude Include="GeometryShaderGen.h">
<Filter>Shader Generators</Filter>
</ClInclude>
<ClInclude Include="GeometryShaderManager.h">
<Filter>Shader Managers</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Text Include="CMakeLists.txt" />

View File

@ -8,6 +8,7 @@
#include "VideoCommon/CommandProcessor.h"
#include "VideoCommon/CPMemory.h"
#include "VideoCommon/Fifo.h"
#include "VideoCommon/GeometryShaderManager.h"
#include "VideoCommon/PixelEngine.h"
#include "VideoCommon/PixelShaderManager.h"
#include "VideoCommon/TextureDecoder.h"
@ -50,6 +51,9 @@ static void DoState(PointerWrap &p)
VertexShaderManager::DoState(p);
p.DoMarker("VertexShaderManager");
GeometryShaderManager::DoState(p);
p.DoMarker("GeometryShaderManager");
VertexManager::DoState(p);
p.DoMarker("VertexManager");

View File

@ -7,6 +7,7 @@
#include "VideoCommon/CPMemory.h"
#include "VideoCommon/DataReader.h"
#include "VideoCommon/Fifo.h"
#include "VideoCommon/GeometryShaderManager.h"
#include "VideoCommon/PixelShaderManager.h"
#include "VideoCommon/VertexManagerBase.h"
#include "VideoCommon/VertexShaderManager.h"
@ -110,6 +111,7 @@ static void XFRegWritten(int transferSize, u32 baseAddress, DataReader src)
VertexManager::Flush();
VertexShaderManager::SetViewportChanged();
PixelShaderManager::SetViewportChanged();
GeometryShaderManager::SetViewportChanged();
nextAddress = XFMEM_SETVIEWPORT + 6;
break;
@ -123,6 +125,7 @@ static void XFRegWritten(int transferSize, u32 baseAddress, DataReader src)
case XFMEM_SETPROJECTION+6:
VertexManager::Flush();
VertexShaderManager::SetProjectionChanged();
GeometryShaderManager::SetProjectionChanged();
nextAddress = XFMEM_SETPROJECTION + 7;
break;