2010-06-13 19:50:06 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
|
|
|
#include "FileUtil.h"
|
|
|
|
#include "LinearDiskCache.h"
|
|
|
|
|
2011-01-25 16:43:08 +00:00
|
|
|
#include "Debugger.h"
|
|
|
|
#include "Statistics.h"
|
|
|
|
#include "VertexShaderGen.h"
|
|
|
|
|
|
|
|
#include "D3DShader.h"
|
|
|
|
#include "Globals.h"
|
2010-06-13 19:50:06 +00:00
|
|
|
#include "VertexShaderCache.h"
|
|
|
|
|
2011-01-31 01:28:32 +00:00
|
|
|
#include "ConfigManager.h"
|
|
|
|
|
2011-01-29 20:16:51 +00:00
|
|
|
// 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 {
|
|
|
|
|
2010-06-13 19:50:06 +00:00
|
|
|
VertexShaderCache::VSCache VertexShaderCache::vshaders;
|
2010-09-28 02:15:02 +00:00
|
|
|
const VertexShaderCache::VSCacheEntry *VertexShaderCache::last_entry;
|
2010-06-13 19:50:06 +00:00
|
|
|
|
|
|
|
static ID3D11VertexShader* SimpleVertexShader = NULL;
|
|
|
|
static ID3D11VertexShader* ClearVertexShader = NULL;
|
|
|
|
static ID3D11InputLayout* SimpleLayout = NULL;
|
|
|
|
static ID3D11InputLayout* ClearLayout = NULL;
|
|
|
|
|
2010-11-15 05:22:03 +00:00
|
|
|
LinearDiskCache<VERTEXSHADERUID, u8> g_vs_disk_cache;
|
2010-06-13 19:50:06 +00:00
|
|
|
|
|
|
|
ID3D11VertexShader* VertexShaderCache::GetSimpleVertexShader() { return SimpleVertexShader; }
|
|
|
|
ID3D11VertexShader* VertexShaderCache::GetClearVertexShader() { return ClearVertexShader; }
|
|
|
|
ID3D11InputLayout* VertexShaderCache::GetSimpleInputLayout() { return SimpleLayout; }
|
|
|
|
ID3D11InputLayout* VertexShaderCache::GetClearInputLayout() { return ClearLayout; }
|
|
|
|
|
2011-01-25 15:08:30 +00:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
D3D11_MAPPED_SUBRESOURCE map;
|
|
|
|
D3D::context->Map(vscbuf, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
|
|
|
|
memcpy(map.pData, vsconstants, sizeof(vsconstants));
|
|
|
|
D3D::context->Unmap(vscbuf, 0);
|
|
|
|
vscbufchanged = false;
|
|
|
|
}
|
|
|
|
return vscbuf;
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// this class will load the precompiled shaders into our cache
|
2010-11-15 05:22:03 +00:00
|
|
|
class VertexShaderCacheInserter : public LinearDiskCacheReader<VERTEXSHADERUID, u8>
|
|
|
|
{
|
2010-06-13 19:50:06 +00:00
|
|
|
public:
|
2010-11-15 05:22:03 +00:00
|
|
|
void Read(const VERTEXSHADERUID &key, const u8 *value, u32 value_size)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2010-06-16 23:25:19 +00:00
|
|
|
D3DBlob* blob = new D3DBlob(value_size, value);
|
2010-11-15 05:22:03 +00:00
|
|
|
VertexShaderCache::InsertByteCode(key, blob);
|
2010-06-13 19:50:06 +00:00
|
|
|
blob->Release();
|
2010-11-15 05:22:03 +00:00
|
|
|
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const char simple_shader_code[] = {
|
|
|
|
"struct VSOUTPUT\n"
|
|
|
|
"{\n"
|
|
|
|
"float4 vPosition : POSITION;\n"
|
|
|
|
"float2 vTexCoord : TEXCOORD0;\n"
|
2010-12-27 03:18:01 +00:00
|
|
|
"float vTexCoord1 : TEXCOORD1;\n"
|
2010-06-13 19:50:06 +00:00
|
|
|
"};\n"
|
2010-12-27 03:18:01 +00:00
|
|
|
"VSOUTPUT main(float4 inPosition : POSITION,float3 inTEX0 : TEXCOORD0)\n"
|
2010-06-13 19:50:06 +00:00
|
|
|
"{\n"
|
|
|
|
"VSOUTPUT OUT;\n"
|
|
|
|
"OUT.vPosition = inPosition;\n"
|
2010-12-27 03:18:01 +00:00
|
|
|
"OUT.vTexCoord = inTEX0.xy;\n"
|
|
|
|
"OUT.vTexCoord1 = inTEX0.z;\n"
|
2010-06-13 19:50:06 +00:00
|
|
|
"return OUT;\n"
|
|
|
|
"}\n"
|
|
|
|
};
|
|
|
|
|
|
|
|
const char clear_shader_code[] = {
|
|
|
|
"struct VSOUTPUT\n"
|
|
|
|
"{\n"
|
|
|
|
"float4 vPosition : POSITION;\n"
|
|
|
|
"float4 vColor0 : COLOR0;\n"
|
|
|
|
"};\n"
|
|
|
|
"VSOUTPUT main(float4 inPosition : POSITION,float4 inColor0: COLOR0)\n"
|
|
|
|
"{\n"
|
|
|
|
"VSOUTPUT OUT;\n"
|
|
|
|
"OUT.vPosition = inPosition;\n"
|
|
|
|
"OUT.vColor0 = inColor0;\n"
|
|
|
|
"return OUT;\n"
|
|
|
|
"}\n"
|
|
|
|
};
|
|
|
|
|
|
|
|
void VertexShaderCache::Init()
|
|
|
|
{
|
|
|
|
const D3D11_INPUT_ELEMENT_DESC simpleelems[2] =
|
|
|
|
{
|
|
|
|
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
|
2010-12-27 03:18:01 +00:00
|
|
|
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
|
|
|
|
|
2010-06-13 19:50:06 +00:00
|
|
|
};
|
|
|
|
const D3D11_INPUT_ELEMENT_DESC clearelems[2] =
|
|
|
|
{
|
|
|
|
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
|
2010-06-19 01:02:43 +00:00
|
|
|
{ "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
|
2010-06-13 19:50:06 +00:00
|
|
|
};
|
|
|
|
|
2011-01-25 15:08:30 +00:00
|
|
|
unsigned int cbsize = ((sizeof(vsconstants))&(~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);
|
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)vscbuf, "vertex shader constant buffer used to emulate the GX pipeline");
|
|
|
|
|
2010-06-16 23:25:19 +00:00
|
|
|
D3DBlob* blob;
|
2010-06-18 23:33:07 +00:00
|
|
|
D3D::CompileVertexShader(simple_shader_code, sizeof(simple_shader_code), &blob);
|
2010-06-16 23:25:19 +00:00
|
|
|
D3D::device->CreateInputLayout(simpleelems, 2, blob->Data(), blob->Size(), &SimpleLayout);
|
2010-06-13 19:50:06 +00:00
|
|
|
SimpleVertexShader = D3D::CreateVertexShaderFromByteCode(blob);
|
|
|
|
if (SimpleLayout == NULL || SimpleVertexShader == NULL) PanicAlert("Failed to create simple vertex shader or input layout at %s %d\n", __FILE__, __LINE__);
|
|
|
|
blob->Release();
|
2010-06-17 10:42:57 +00:00
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)SimpleVertexShader, "simple vertex shader");
|
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)SimpleLayout, "simple input layout");
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2010-06-18 23:33:07 +00:00
|
|
|
D3D::CompileVertexShader(clear_shader_code, sizeof(clear_shader_code), &blob);
|
2010-06-16 23:25:19 +00:00
|
|
|
D3D::device->CreateInputLayout(clearelems, 2, blob->Data(), blob->Size(), &ClearLayout);
|
2010-06-13 19:50:06 +00:00
|
|
|
ClearVertexShader = D3D::CreateVertexShaderFromByteCode(blob);
|
|
|
|
if (ClearLayout == NULL || ClearVertexShader == NULL) PanicAlert("Failed to create clear vertex shader or input layout at %s %d\n", __FILE__, __LINE__);
|
|
|
|
blob->Release();
|
2010-06-17 10:42:57 +00:00
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)ClearVertexShader, "clear vertex shader");
|
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)ClearLayout, "clear input layout");
|
2010-06-13 19:50:06 +00:00
|
|
|
|
|
|
|
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;
|
2010-07-18 00:18:31 +00:00
|
|
|
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;
|
|
|
|
for (k = 0;k < 4;k++) vs_constant_offset_table[C_DEPTHPARAMS+k] = 952+4*k;
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2011-03-01 03:06:14 +00:00
|
|
|
if (!File::Exists(File::GetUserPath(D_SHADERCACHE_IDX)))
|
2011-02-28 20:40:15 +00:00
|
|
|
File::CreateDir(File::GetUserPath(D_SHADERCACHE_IDX).c_str());
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
SETSTAT(stats.numVertexShadersCreated, 0);
|
|
|
|
SETSTAT(stats.numVertexShadersAlive, 0);
|
|
|
|
|
2010-06-13 19:50:06 +00:00
|
|
|
char cache_filename[MAX_PATH];
|
2011-02-28 20:40:15 +00:00
|
|
|
sprintf(cache_filename, "%sdx11-%s-vs.cache", File::GetUserPath(D_SHADERCACHE_IDX).c_str(),
|
|
|
|
SConfig::GetInstance().m_LocalCoreStartupParameter.m_strUniqueID.c_str());
|
2010-06-13 19:50:06 +00:00
|
|
|
VertexShaderCacheInserter inserter;
|
2010-11-15 05:22:03 +00:00
|
|
|
g_vs_disk_cache.OpenAndRead(cache_filename, inserter);
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VertexShaderCache::Clear()
|
|
|
|
{
|
2010-07-18 10:11:34 +00:00
|
|
|
for (VSCache::iterator iter = vshaders.begin(); iter != vshaders.end(); ++iter)
|
2010-06-13 19:50:06 +00:00
|
|
|
iter->second.Destroy();
|
|
|
|
vshaders.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexShaderCache::Shutdown()
|
|
|
|
{
|
2011-01-25 15:08:30 +00:00
|
|
|
SAFE_RELEASE(vscbuf);
|
|
|
|
|
2010-06-13 19:50:06 +00:00
|
|
|
SAFE_RELEASE(SimpleVertexShader);
|
|
|
|
SAFE_RELEASE(ClearVertexShader);
|
|
|
|
|
|
|
|
SAFE_RELEASE(SimpleLayout);
|
|
|
|
SAFE_RELEASE(ClearLayout);
|
|
|
|
|
|
|
|
Clear();
|
|
|
|
g_vs_disk_cache.Sync();
|
|
|
|
g_vs_disk_cache.Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VertexShaderCache::SetShader(u32 components)
|
|
|
|
{
|
|
|
|
VERTEXSHADERUID uid;
|
|
|
|
GetVertexShaderId(&uid, components);
|
|
|
|
if (uid == last_vertex_shader_uid && vshaders[uid].frameCount == frameCount)
|
2010-12-05 14:15:36 +00:00
|
|
|
{
|
|
|
|
GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true);
|
2010-06-13 19:50:06 +00:00
|
|
|
return (vshaders[uid].shader != NULL);
|
2010-12-05 14:15:36 +00:00
|
|
|
}
|
2010-06-13 19:50:06 +00:00
|
|
|
|
|
|
|
memcpy(&last_vertex_shader_uid, &uid, sizeof(VERTEXSHADERUID));
|
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
VSCache::iterator iter = vshaders.find(uid);
|
2010-06-13 19:50:06 +00:00
|
|
|
if (iter != vshaders.end())
|
|
|
|
{
|
|
|
|
iter->second.frameCount = frameCount;
|
|
|
|
const VSCacheEntry &entry = iter->second;
|
|
|
|
last_entry = &entry;
|
|
|
|
|
2010-12-05 14:15:36 +00:00
|
|
|
GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true);
|
2010-06-13 19:50:06 +00:00
|
|
|
return (entry.shader != NULL);
|
|
|
|
}
|
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
const char *code = GenerateVertexShaderCode(components, API_D3D11);
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2010-06-16 23:25:19 +00:00
|
|
|
D3DBlob* pbytecode = NULL;
|
2010-06-13 19:50:06 +00:00
|
|
|
D3D::CompileVertexShader(code, (int)strlen(code), &pbytecode);
|
|
|
|
|
|
|
|
if (pbytecode == NULL)
|
|
|
|
{
|
|
|
|
PanicAlert("Failed to compile Vertex Shader %s %d:\n\n%s", __FILE__, __LINE__, code);
|
2010-12-05 14:15:36 +00:00
|
|
|
GFX_DEBUGGER_PAUSE_AT(NEXT_ERROR, true);
|
2010-06-13 19:50:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-11-15 05:22:03 +00:00
|
|
|
g_vs_disk_cache.Append(uid, pbytecode->Data(), pbytecode->Size());
|
2010-06-13 19:50:06 +00:00
|
|
|
g_vs_disk_cache.Sync();
|
|
|
|
|
|
|
|
bool result = InsertByteCode(uid, pbytecode);
|
|
|
|
pbytecode->Release();
|
2010-12-05 14:15:36 +00:00
|
|
|
GFX_DEBUGGER_PAUSE_AT(NEXT_VERTEX_SHADER_CHANGE, true);
|
2010-06-13 19:50:06 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-06-16 23:25:19 +00:00
|
|
|
bool VertexShaderCache::InsertByteCode(const VERTEXSHADERUID &uid, D3DBlob* bcodeblob)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
|
|
|
ID3D11VertexShader* shader = D3D::CreateVertexShaderFromByteCode(bcodeblob);
|
|
|
|
if (shader == NULL)
|
|
|
|
{
|
2010-06-16 23:25:19 +00:00
|
|
|
PanicAlert("Failed to create vertex shader from %p size %d at %s %d\n", bcodeblob->Data(), bcodeblob->Size(), __FILE__, __LINE__);
|
2010-06-13 19:50:06 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-06-17 10:42:57 +00:00
|
|
|
// TODO: Somehow make the debug name a bit more specific
|
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)shader, "a vertex shader of VertexShaderCache");
|
2010-06-13 19:50:06 +00:00
|
|
|
|
|
|
|
// Make an entry in the table
|
|
|
|
VSCacheEntry entry;
|
|
|
|
entry.shader = shader;
|
|
|
|
entry.frameCount = frameCount;
|
|
|
|
entry.SetByteCode(bcodeblob);
|
|
|
|
|
|
|
|
vshaders[uid] = entry;
|
|
|
|
last_entry = &vshaders[uid];
|
|
|
|
|
|
|
|
INCSTAT(stats.numVertexShadersCreated);
|
|
|
|
SETSTAT(stats.numVertexShadersAlive, (int)vshaders.size());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-29 20:16:51 +00:00
|
|
|
// 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
|
2011-01-31 01:28:32 +00:00
|
|
|
void Renderer::SetVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4)
|
2011-01-29 20:16:51 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-01-31 01:28:32 +00:00
|
|
|
void Renderer::SetVSConstant4fv(unsigned int const_number, const float* f)
|
2011-01-29 20:16:51 +00:00
|
|
|
{
|
|
|
|
memcpy(&vsconstants[vs_constant_offset_table[const_number]], f, sizeof(float)*4);
|
|
|
|
vscbufchanged = true;
|
|
|
|
}
|
|
|
|
|
2011-01-31 01:28:32 +00:00
|
|
|
void Renderer::SetMultiVSConstant3fv(unsigned int const_number, unsigned int count, const float* f)
|
2011-01-29 20:16:51 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-01-31 01:28:32 +00:00
|
|
|
void Renderer::SetMultiVSConstant4fv(unsigned int const_number, unsigned int count, const float* f)
|
2011-01-29 20:16:51 +00:00
|
|
|
{
|
|
|
|
memcpy(&vsconstants[vs_constant_offset_table[const_number]], f, sizeof(float)*4*count);
|
|
|
|
vscbufchanged = true;
|
2011-01-31 01:28:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace DX11
|