170 lines
5.5 KiB
C++
170 lines
5.5 KiB
C++
// Copyright 2010 Dolphin Emulator Project
|
|
// Licensed under GPLv2+
|
|
// Refer to the license.txt file included.
|
|
|
|
#include <string>
|
|
|
|
#include "Common/Align.h"
|
|
#include "Common/CommonTypes.h"
|
|
#include "Common/FileUtil.h"
|
|
#include "Common/MsgHandler.h"
|
|
#include "Common/StringUtil.h"
|
|
|
|
#include "Core/ConfigManager.h"
|
|
#include "Core/Host.h"
|
|
|
|
#include "VideoBackends/D3D/D3DShader.h"
|
|
#include "VideoBackends/D3D/D3DState.h"
|
|
#include "VideoBackends/D3D/VertexManager.h"
|
|
#include "VideoBackends/D3D/VertexShaderCache.h"
|
|
|
|
#include "VideoCommon/Debugger.h"
|
|
#include "VideoCommon/Statistics.h"
|
|
#include "VideoCommon/UberShaderVertex.h"
|
|
#include "VideoCommon/VertexLoaderManager.h"
|
|
#include "VideoCommon/VertexShaderGen.h"
|
|
#include "VideoCommon/VertexShaderManager.h"
|
|
|
|
namespace DX11
|
|
{
|
|
static ID3D11VertexShader* SimpleVertexShader = nullptr;
|
|
static ID3D11VertexShader* ClearVertexShader = nullptr;
|
|
static ID3D11InputLayout* SimpleLayout = nullptr;
|
|
static ID3D11InputLayout* ClearLayout = nullptr;
|
|
|
|
ID3D11VertexShader* VertexShaderCache::GetSimpleVertexShader()
|
|
{
|
|
return SimpleVertexShader;
|
|
}
|
|
ID3D11VertexShader* VertexShaderCache::GetClearVertexShader()
|
|
{
|
|
return ClearVertexShader;
|
|
}
|
|
ID3D11InputLayout* VertexShaderCache::GetSimpleInputLayout()
|
|
{
|
|
return SimpleLayout;
|
|
}
|
|
ID3D11InputLayout* VertexShaderCache::GetClearInputLayout()
|
|
{
|
|
return ClearLayout;
|
|
}
|
|
|
|
ID3D11Buffer* vscbuf = nullptr;
|
|
|
|
ID3D11Buffer*& VertexShaderCache::GetConstantBuffer()
|
|
{
|
|
// TODO: divide the global variables of the generated shaders into about 5 constant buffers to
|
|
// speed this up
|
|
if (VertexShaderManager::dirty)
|
|
{
|
|
D3D11_MAPPED_SUBRESOURCE map;
|
|
D3D::context->Map(vscbuf, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
|
|
memcpy(map.pData, &VertexShaderManager::constants, sizeof(VertexShaderConstants));
|
|
D3D::context->Unmap(vscbuf, 0);
|
|
VertexShaderManager::dirty = false;
|
|
|
|
ADDSTAT(stats.thisFrame.bytesUniformStreamed, sizeof(VertexShaderConstants));
|
|
}
|
|
return vscbuf;
|
|
}
|
|
|
|
// this class will load the precompiled shaders into our cache
|
|
template <typename UidType>
|
|
class VertexShaderCacheInserter : public LinearDiskCacheReader<UidType, u8>
|
|
{
|
|
public:
|
|
void Read(const UidType& key, const u8* value, u32 value_size)
|
|
{
|
|
D3DBlob* blob = new D3DBlob(value_size, value);
|
|
VertexShaderCache::InsertByteCode(key, blob);
|
|
blob->Release();
|
|
}
|
|
};
|
|
|
|
const char simple_shader_code[] = {
|
|
"struct VSOUTPUT\n"
|
|
"{\n"
|
|
"float4 vPosition : POSITION;\n"
|
|
"float3 vTexCoord : TEXCOORD0;\n"
|
|
"float vTexCoord1 : TEXCOORD1;\n"
|
|
"};\n"
|
|
"VSOUTPUT main(float4 inPosition : POSITION,float4 inTEX0 : TEXCOORD0)\n"
|
|
"{\n"
|
|
"VSOUTPUT OUT;\n"
|
|
"OUT.vPosition = inPosition;\n"
|
|
"OUT.vTexCoord = inTEX0.xyz;\n"
|
|
"OUT.vTexCoord1 = inTEX0.w;\n"
|
|
"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},
|
|
{"TEXCOORD", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
|
|
|
|
};
|
|
const D3D11_INPUT_ELEMENT_DESC clearelems[2] = {
|
|
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0},
|
|
{"COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0},
|
|
};
|
|
|
|
unsigned int cbsize = Common::AlignUp(static_cast<unsigned int>(sizeof(VertexShaderConstants)),
|
|
16); // 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, nullptr, &vscbuf);
|
|
CHECK(hr == S_OK, "Create vertex shader constant buffer (size=%u)", cbsize);
|
|
D3D::SetDebugObjectName(vscbuf, "vertex shader constant buffer used to emulate the GX pipeline");
|
|
|
|
D3DBlob* blob;
|
|
D3D::CompileVertexShader(simple_shader_code, &blob);
|
|
D3D::device->CreateInputLayout(simpleelems, 2, blob->Data(), blob->Size(), &SimpleLayout);
|
|
SimpleVertexShader = D3D::CreateVertexShaderFromByteCode(blob);
|
|
if (SimpleLayout == nullptr || SimpleVertexShader == nullptr)
|
|
PanicAlert("Failed to create simple vertex shader or input layout at %s %d\n", __FILE__,
|
|
__LINE__);
|
|
blob->Release();
|
|
D3D::SetDebugObjectName(SimpleVertexShader, "simple vertex shader");
|
|
D3D::SetDebugObjectName(SimpleLayout, "simple input layout");
|
|
|
|
D3D::CompileVertexShader(clear_shader_code, &blob);
|
|
D3D::device->CreateInputLayout(clearelems, 2, blob->Data(), blob->Size(), &ClearLayout);
|
|
ClearVertexShader = D3D::CreateVertexShaderFromByteCode(blob);
|
|
if (ClearLayout == nullptr || ClearVertexShader == nullptr)
|
|
PanicAlert("Failed to create clear vertex shader or input layout at %s %d\n", __FILE__,
|
|
__LINE__);
|
|
blob->Release();
|
|
D3D::SetDebugObjectName(ClearVertexShader, "clear vertex shader");
|
|
D3D::SetDebugObjectName(ClearLayout, "clear input layout");
|
|
|
|
SETSTAT(stats.numVertexShadersCreated, 0);
|
|
SETSTAT(stats.numVertexShadersAlive, 0);
|
|
}
|
|
|
|
void VertexShaderCache::Shutdown()
|
|
{
|
|
SAFE_RELEASE(vscbuf);
|
|
|
|
SAFE_RELEASE(SimpleVertexShader);
|
|
SAFE_RELEASE(ClearVertexShader);
|
|
|
|
SAFE_RELEASE(SimpleLayout);
|
|
SAFE_RELEASE(ClearLayout);
|
|
}
|
|
} // namespace DX11
|