Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'const_buffer_rework'
  • Loading branch information
degasus committed Oct 13, 2013
2 parents 0753ce5 + 2754c11 commit 4dc1881
Show file tree
Hide file tree
Showing 24 changed files with 392 additions and 893 deletions.
61 changes: 6 additions & 55 deletions Source/Core/VideoBackends/D3D/Src/PixelShaderCache.cpp
Expand Up @@ -14,15 +14,12 @@
#include "Globals.h"
#include "PixelShaderGen.h"
#include "PixelShaderCache.h"
#include "PixelShaderManager.h"

#include "ConfigManager.h"

extern int frameCount;

// See comment near the bottom of this file.
float psconstants[C_PENVCONST_END*4];
bool pscbufchanged = true;

namespace DX11
{

Expand Down Expand Up @@ -339,15 +336,15 @@ ID3D11PixelShader* PixelShaderCache::GetClearProgram()
ID3D11Buffer* &PixelShaderCache::GetConstantBuffer()
{
// TODO: divide the global variables of the generated shaders into about 5 constant buffers to speed this up
if (pscbufchanged)
if (PixelShaderManager::dirty)
{
D3D11_MAPPED_SUBRESOURCE map;
D3D::context->Map(pscbuf, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
memcpy(map.pData, psconstants, sizeof(psconstants));
memcpy(map.pData, &PixelShaderManager::constants, sizeof(PixelShaderConstants));
D3D::context->Unmap(pscbuf, 0);
pscbufchanged = false;
PixelShaderManager::dirty = false;

ADDSTAT(stats.thisFrame.bytesUniformStreamed, sizeof(psconstants));
ADDSTAT(stats.thisFrame.bytesUniformStreamed, sizeof(PixelShaderConstants));
}
return pscbuf;
}
Expand All @@ -364,7 +361,7 @@ class PixelShaderCacheInserter : public LinearDiskCacheReader<PixelShaderUid, u8

void PixelShaderCache::Init()
{
unsigned int cbsize = ((sizeof(psconstants))&(~0xf))+0x10; // must be a multiple of 16
unsigned int cbsize = ((sizeof(PixelShaderConstants))&(~0xf))+0x10; // must be a multiple of 16
D3D11_BUFFER_DESC cbdesc = CD3D11_BUFFER_DESC(cbsize, D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
D3D::device->CreateBuffer(&cbdesc, NULL, &pscbuf);
CHECK(pscbuf!=NULL, "Create pixel shader constant buffer");
Expand Down Expand Up @@ -536,50 +533,4 @@ bool PixelShaderCache::InsertByteCode(const PixelShaderUid &uid, const void* byt
return true;
}

// These are "callbacks" from VideoCommon and thus must be outside namespace DX11.
// This will have to be changed when we merge.

// HACK to avoid some invasive VideoCommon changes
// these values are hardcoded, they depend on internal D3DCompile behavior; TODO: Solve this with D3DReflect or something
// offset given in floats, table index is float4
static const unsigned int ps_constant_offset_table[] = {
0, 4, 8, 12, // C_COLORS, 16
16, 20, 24, 28, // C_KCOLORS, 16
32, // C_ALPHA, 4
36, 40, 44, 48, 52, 56, 60, 64, // C_TEXDIMS, 32
68, 72, // C_ZBIAS, 8
76, 80, // C_INDTEXSCALE, 8
84, 88, 92, 96, 100, 104, // C_INDTEXMTX, 24
108, 112, 116, // C_FOG, 12
120, 124, 128, 132, 136, // C_PLIGHTS0, 20
140, 144, 148, 152, 156, // C_PLIGHTS1, 20
160, 164, 168, 172, 176, // C_PLIGHTS2, 20
180, 184, 188, 192, 196, // C_PLIGHTS3, 20
200, 204, 208, 212, 216, // C_PLIGHTS4, 20
220, 224, 228, 232, 236, // C_PLIGHTS5, 20
240, 244, 248, 252, 256, // C_PLIGHTS6, 20
260, 264, 268, 272, 276, // C_PLIGHTS7, 20
280, 284, 288, 292 // C_PMATERIALS, 16
};
void Renderer::SetPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4)
{
psconstants[ps_constant_offset_table[const_number] ] = f1;
psconstants[ps_constant_offset_table[const_number]+1] = f2;
psconstants[ps_constant_offset_table[const_number]+2] = f3;
psconstants[ps_constant_offset_table[const_number]+3] = f4;
pscbufchanged = true;
}

void Renderer::SetPSConstant4fv(unsigned int const_number, const float* f)
{
memcpy(&psconstants[ps_constant_offset_table[const_number]], f, sizeof(float)*4);
pscbufchanged = true;
}

void Renderer::SetMultiPSConstant4fv(unsigned int const_number, unsigned int count, const float* f)
{
memcpy(&psconstants[ps_constant_offset_table[const_number]], f, sizeof(float)*4*count);
pscbufchanged = true;
}

} // DX11
9 changes: 0 additions & 9 deletions Source/Core/VideoBackends/D3D/Src/Render.h
Expand Up @@ -51,15 +51,6 @@ class Renderer : public ::Renderer
bool SaveScreenshot(const std::string &filename, const TargetRectangle &rc);

static bool CheckForResize();

void SetPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4);
void SetPSConstant4fv(unsigned int const_number, const float *f);
void SetMultiPSConstant4fv(unsigned int const_number, unsigned int count, const float *f);

void SetVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4);
void SetVSConstant4fv(unsigned int const_number, const float *f);
void SetMultiVSConstant3fv(unsigned int const_number, unsigned int count, const float *f);
void SetMultiVSConstant4fv(unsigned int const_number, unsigned int count, const float *f);
};

}
Expand Down
64 changes: 6 additions & 58 deletions Source/Core/VideoBackends/D3D/Src/VertexShaderCache.cpp
Expand Up @@ -12,14 +12,10 @@
#include "D3DShader.h"
#include "Globals.h"
#include "VertexShaderCache.h"
#include "VertexShaderManager.h"

#include "ConfigManager.h"

// See comment near the bottom of this file
static unsigned int vs_constant_offset_table[C_VENVCONST_END];
float vsconstants[C_VENVCONST_END*4];
bool vscbufchanged = true;

namespace DX11 {

VertexShaderCache::VSCache VertexShaderCache::vshaders;
Expand All @@ -44,15 +40,15 @@ ID3D11Buffer* vscbuf = NULL;
ID3D11Buffer* &VertexShaderCache::GetConstantBuffer()
{
// TODO: divide the global variables of the generated shaders into about 5 constant buffers to speed this up
if (vscbufchanged)
if (VertexShaderManager::dirty)
{
D3D11_MAPPED_SUBRESOURCE map;
D3D::context->Map(vscbuf, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
memcpy(map.pData, vsconstants, sizeof(vsconstants));
memcpy(map.pData, &VertexShaderManager::constants, sizeof(VertexShaderConstants));
D3D::context->Unmap(vscbuf, 0);
vscbufchanged = false;
VertexShaderManager::dirty = false;

ADDSTAT(stats.thisFrame.bytesUniformStreamed, sizeof(vsconstants));
ADDSTAT(stats.thisFrame.bytesUniformStreamed, sizeof(VertexShaderConstants));
}
return vscbuf;
}
Expand Down Expand Up @@ -116,7 +112,7 @@ void VertexShaderCache::Init()
{ "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};

unsigned int cbsize = ((sizeof(vsconstants))&(~0xf))+0x10; // must be a multiple of 16
unsigned int cbsize = ((sizeof(VertexShaderConstants))&(~0xf))+0x10; // must be a multiple of 16
D3D11_BUFFER_DESC cbdesc = CD3D11_BUFFER_DESC(cbsize, D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
HRESULT hr = D3D::device->CreateBuffer(&cbdesc, NULL, &vscbuf);
CHECK(hr==S_OK, "Create vertex shader constant buffer (size=%u)", cbsize);
Expand All @@ -141,19 +137,6 @@ void VertexShaderCache::Init()

Clear();

// these values are hardcoded, they depend on internal D3DCompile behavior
// TODO: Do this with D3DReflect or something instead
unsigned int k;
for (k = 0;k < 6;k++) vs_constant_offset_table[C_POSNORMALMATRIX+k] = 0+4*k;
for (k = 0;k < 4;k++) vs_constant_offset_table[C_PROJECTION+k] = 24+4*k;
for (k = 0;k < 4;k++) vs_constant_offset_table[C_MATERIALS+k] = 40+4*k;
for (k = 0;k < 40;k++) vs_constant_offset_table[C_LIGHTS+k] = 56+4*k;
for (k = 0;k < 24;k++) vs_constant_offset_table[C_TEXMATRICES+k] = 216+4*k;
for (k = 0;k < 64;k++) vs_constant_offset_table[C_TRANSFORMMATRICES+k] = 312+4*k;
for (k = 0;k < 32;k++) vs_constant_offset_table[C_NORMALMATRICES+k] = 568+4*k;
for (k = 0;k < 64;k++) vs_constant_offset_table[C_POSTTRANSFORMMATRICES+k] = 696+4*k;
vs_constant_offset_table[C_DEPTHPARAMS] = 952;

if (!File::Exists(File::GetUserPath(D_SHADERCACHE_IDX)))
File::CreateDir(File::GetUserPath(D_SHADERCACHE_IDX).c_str());

Expand Down Expand Up @@ -277,39 +260,4 @@ bool VertexShaderCache::InsertByteCode(const VertexShaderUid &uid, D3DBlob* bcod
return true;
}

// These are "callbacks" from VideoCommon and thus must be outside namespace DX11.
// This will have to be changed when we merge.

// maps the constant numbers to float indices in the constant buffer
void Renderer::SetVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4)
{
vsconstants[vs_constant_offset_table[const_number] ] = f1;
vsconstants[vs_constant_offset_table[const_number]+1] = f2;
vsconstants[vs_constant_offset_table[const_number]+2] = f3;
vsconstants[vs_constant_offset_table[const_number]+3] = f4;
vscbufchanged = true;
}

void Renderer::SetVSConstant4fv(unsigned int const_number, const float* f)
{
memcpy(&vsconstants[vs_constant_offset_table[const_number]], f, sizeof(float)*4);
vscbufchanged = true;
}

void Renderer::SetMultiVSConstant3fv(unsigned int const_number, unsigned int count, const float* f)
{
for (unsigned int i = 0; i < count; i++)
{
memcpy(&vsconstants[vs_constant_offset_table[const_number+i]], f+3*i, sizeof(float)*3);
vsconstants[vs_constant_offset_table[const_number+i]+3] = 0.f;
}
vscbufchanged = true;
}

void Renderer::SetMultiVSConstant4fv(unsigned int const_number, unsigned int count, const float* f)
{
memcpy(&vsconstants[vs_constant_offset_table[const_number]], f, sizeof(float)*4*count);
vscbufchanged = true;
}

} // namespace DX11
2 changes: 0 additions & 2 deletions Source/Core/VideoBackends/OGL/CMakeLists.txt
Expand Up @@ -3,7 +3,6 @@ set(SRCS Src/FramebufferManager.cpp
Src/main.cpp
Src/NativeVertexFormat.cpp
Src/PerfQuery.cpp
Src/PixelShaderCache.cpp
Src/PostProcessing.cpp
Src/ProgramShaderCache.cpp
Src/RasterFont.cpp
Expand All @@ -12,7 +11,6 @@ set(SRCS Src/FramebufferManager.cpp
Src/StreamBuffer.cpp
Src/TextureCache.cpp
Src/TextureConverter.cpp
Src/VertexShaderCache.cpp
Src/VertexManager.cpp)

set(LIBS videocommon
Expand Down
4 changes: 1 addition & 3 deletions Source/Core/VideoBackends/OGL/OGL.vcxproj
Expand Up @@ -151,7 +151,6 @@
<ClCompile Include="Src\main.cpp" />
<ClCompile Include="Src\NativeVertexFormat.cpp" />
<ClCompile Include="Src\PerfQuery.cpp" />
<ClCompile Include="Src\PixelShaderCache.cpp" />
<ClCompile Include="Src\PostProcessing.cpp" />
<ClCompile Include="Src\ProgramShaderCache.cpp" />
<ClCompile Include="Src\RasterFont.cpp" />
Expand All @@ -169,7 +168,6 @@
<ClCompile Include="Src\TextureCache.cpp" />
<ClCompile Include="Src\TextureConverter.cpp" />
<ClCompile Include="Src\VertexManager.cpp" />
<ClCompile Include="Src\VertexShaderCache.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Src\FramebufferManager.h" />
Expand Down Expand Up @@ -203,4 +201,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>
104 changes: 0 additions & 104 deletions Source/Core/VideoBackends/OGL/Src/PixelShaderCache.cpp

This file was deleted.

0 comments on commit 4dc1881

Please sign in to comment.