2013-04-17 21:29:41 -06:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2011-03-14 03:38:29 -06:00
|
|
|
|
|
|
|
#include <sstream>
|
2014-02-17 03:18:15 -07:00
|
|
|
|
|
|
|
#include "VideoBackends/D3D/D3DBase.h"
|
|
|
|
#include "VideoBackends/D3D/D3DShader.h"
|
2014-10-28 18:19:09 -06:00
|
|
|
#include "VideoBackends/D3D/D3DState.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoBackends/D3D/PointGeometryShader.h"
|
|
|
|
#include "VideoCommon/VertexShaderGen.h"
|
2011-03-14 03:38:29 -06:00
|
|
|
|
|
|
|
namespace DX11
|
|
|
|
{
|
|
|
|
|
2011-04-15 16:34:54 -06:00
|
|
|
struct PointGSParams
|
2011-03-14 03:38:29 -06:00
|
|
|
{
|
2011-04-15 16:34:54 -06:00
|
|
|
FLOAT PointSize; // In units of 1/6 of an EFB pixel
|
|
|
|
FLOAT TexOffset;
|
|
|
|
FLOAT VpWidth; // Width and height of viewport in EFB pixels
|
|
|
|
FLOAT VpHeight;
|
|
|
|
FLOAT TexOffsetEnable[8]; // For each tex coordinate, whether to apply offset to it (1 on, 0 off)
|
|
|
|
};
|
|
|
|
|
|
|
|
union PointGSParams_Padded
|
|
|
|
{
|
|
|
|
PointGSParams params;
|
2011-03-14 03:38:29 -06:00
|
|
|
// Constant buffers must be a multiple of 16 bytes in size.
|
2011-04-15 16:34:54 -06:00
|
|
|
u8 pad[(sizeof(PointGSParams) + 15) & ~15];
|
2011-03-14 03:38:29 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char POINT_GS_COMMON[] =
|
|
|
|
// The struct VS_OUTPUT used by the vertex shader goes here.
|
|
|
|
"// dolphin-emu point geometry shader common part\n"
|
|
|
|
|
|
|
|
"cbuffer cbParams : register(b0)\n"
|
|
|
|
"{\n"
|
|
|
|
"struct\n" // Should match PointGSParams above
|
|
|
|
"{\n"
|
|
|
|
"float PointSize;\n"
|
|
|
|
"float TexOffset;\n"
|
2011-03-27 23:42:21 -06:00
|
|
|
"float VpWidth;\n"
|
|
|
|
"float VpHeight;\n"
|
2011-04-15 16:34:54 -06:00
|
|
|
"float TexOffsetEnable[8];\n"
|
2011-03-14 03:38:29 -06:00
|
|
|
"} Params;\n"
|
|
|
|
"}\n"
|
|
|
|
|
|
|
|
"[maxvertexcount(4)]\n"
|
|
|
|
"void main(point VS_OUTPUT input[1], inout TriangleStream<VS_OUTPUT> outStream)\n"
|
|
|
|
"{\n"
|
|
|
|
"VS_OUTPUT ptLL = input[0];\n"
|
|
|
|
"VS_OUTPUT ptLR = ptLL;\n"
|
|
|
|
"VS_OUTPUT ptUL = ptLL;\n"
|
|
|
|
"VS_OUTPUT ptUR = ptLL;\n"
|
|
|
|
|
2011-03-15 21:02:25 -06:00
|
|
|
// Offset from center to upper right vertex
|
2011-03-27 23:42:21 -06:00
|
|
|
// Lerp Params.PointSize/2 from [0,0..VpWidth,VpHeight] to [-1,1..1,-1]
|
|
|
|
"float2 offset = float2(Params.PointSize/Params.VpWidth, -Params.PointSize/Params.VpHeight) * input[0].pos.w;\n"
|
2011-03-14 03:38:29 -06:00
|
|
|
|
|
|
|
"ptLL.pos.xy += float2(-1,-1) * offset;\n"
|
|
|
|
"ptLR.pos.xy += float2(1,-1) * offset;\n"
|
|
|
|
"ptUL.pos.xy += float2(-1,1) * offset;\n"
|
|
|
|
"ptUR.pos.xy += offset;\n"
|
|
|
|
|
|
|
|
"float2 texOffset = float2(Params.TexOffset, Params.TexOffset);\n"
|
|
|
|
|
|
|
|
"#ifndef NUM_TEXCOORDS\n"
|
|
|
|
"#error NUM_TEXCOORDS not defined\n"
|
|
|
|
"#endif\n"
|
|
|
|
|
|
|
|
// Apply TexOffset to all tex coordinates in the vertex
|
2011-03-15 21:02:25 -06:00
|
|
|
// FIXME: The game may be able to enable TexOffset for some coords and
|
|
|
|
// disable for others, but where is that information stored?
|
2011-03-14 03:38:29 -06:00
|
|
|
"#if NUM_TEXCOORDS >= 1\n"
|
2011-04-15 16:34:54 -06:00
|
|
|
"ptLL.tex0.xy += float2(0,1) * texOffset * Params.TexOffsetEnable[0];\n"
|
|
|
|
"ptLR.tex0.xy += texOffset * Params.TexOffsetEnable[0];\n"
|
|
|
|
"ptUR.tex0.xy += float2(1,0) * texOffset * Params.TexOffsetEnable[0];\n"
|
2011-03-14 03:38:29 -06:00
|
|
|
"#endif\n"
|
|
|
|
"#if NUM_TEXCOORDS >= 2\n"
|
2011-04-15 16:34:54 -06:00
|
|
|
"ptLL.tex1.xy += float2(0,1) * texOffset * Params.TexOffsetEnable[1];\n"
|
|
|
|
"ptLR.tex1.xy += texOffset * Params.TexOffsetEnable[1];\n"
|
|
|
|
"ptUR.tex1.xy += float2(1,0) * texOffset * Params.TexOffsetEnable[1];\n"
|
2011-03-14 03:38:29 -06:00
|
|
|
"#endif\n"
|
|
|
|
"#if NUM_TEXCOORDS >= 3\n"
|
2011-04-15 16:34:54 -06:00
|
|
|
"ptLL.tex2.xy += float2(0,1) * texOffset * Params.TexOffsetEnable[2];\n"
|
|
|
|
"ptLR.tex2.xy += texOffset * Params.TexOffsetEnable[2];\n"
|
|
|
|
"ptUR.tex2.xy += float2(1,0) * texOffset * Params.TexOffsetEnable[2];\n"
|
2011-03-14 03:38:29 -06:00
|
|
|
"#endif\n"
|
|
|
|
"#if NUM_TEXCOORDS >= 4\n"
|
2011-04-15 16:34:54 -06:00
|
|
|
"ptLL.tex3.xy += float2(0,1) * texOffset * Params.TexOffsetEnable[3];\n"
|
|
|
|
"ptLR.tex3.xy += texOffset * Params.TexOffsetEnable[3];\n"
|
|
|
|
"ptUR.tex3.xy += float2(1,0) * texOffset * Params.TexOffsetEnable[3];\n"
|
2011-03-14 03:38:29 -06:00
|
|
|
"#endif\n"
|
|
|
|
"#if NUM_TEXCOORDS >= 5\n"
|
2011-04-15 16:34:54 -06:00
|
|
|
"ptLL.tex4.xy += float2(0,1) * texOffset * Params.TexOffsetEnable[4];\n"
|
|
|
|
"ptLR.tex4.xy += texOffset * Params.TexOffsetEnable[4];\n"
|
|
|
|
"ptUR.tex4.xy += float2(1,0) * texOffset * Params.TexOffsetEnable[4];\n"
|
2011-03-14 03:38:29 -06:00
|
|
|
"#endif\n"
|
|
|
|
"#if NUM_TEXCOORDS >= 6\n"
|
2011-04-15 16:34:54 -06:00
|
|
|
"ptLL.tex5.xy += float2(0,1) * texOffset * Params.TexOffsetEnable[5];\n"
|
|
|
|
"ptLR.tex5.xy += texOffset * Params.TexOffsetEnable[5];\n"
|
|
|
|
"ptUR.tex5.xy += float2(1,0) * texOffset * Params.TexOffsetEnable[5];\n"
|
2011-03-14 03:38:29 -06:00
|
|
|
"#endif\n"
|
|
|
|
"#if NUM_TEXCOORDS >= 7\n"
|
2011-04-15 16:34:54 -06:00
|
|
|
"ptLL.tex6.xy += float2(0,1) * texOffset * Params.TexOffsetEnable[6];\n"
|
|
|
|
"ptLR.tex6.xy += texOffset * Params.TexOffsetEnable[6];\n"
|
|
|
|
"ptUR.tex6.xy += float2(1,0) * texOffset * Params.TexOffsetEnable[6];\n"
|
2011-03-14 03:38:29 -06:00
|
|
|
"#endif\n"
|
|
|
|
"#if NUM_TEXCOORDS >= 8\n"
|
2011-04-15 16:34:54 -06:00
|
|
|
"ptLL.tex7.xy += float2(0,1) * texOffset * Params.TexOffsetEnable[7];\n"
|
|
|
|
"ptLR.tex7.xy += texOffset * Params.TexOffsetEnable[7];\n"
|
|
|
|
"ptUR.tex7.xy += float2(1,0) * texOffset * Params.TexOffsetEnable[7];\n"
|
2011-03-14 03:38:29 -06:00
|
|
|
"#endif\n"
|
|
|
|
|
|
|
|
"outStream.Append(ptLL);\n"
|
|
|
|
"outStream.Append(ptLR);\n"
|
|
|
|
"outStream.Append(ptUL);\n"
|
|
|
|
"outStream.Append(ptUR);\n"
|
|
|
|
"}\n"
|
|
|
|
;
|
|
|
|
|
|
|
|
PointGeometryShader::PointGeometryShader()
|
2014-03-09 14:14:26 -06:00
|
|
|
: m_ready(false), m_paramsBuffer(nullptr)
|
2011-06-11 13:37:21 -06:00
|
|
|
{ }
|
|
|
|
|
|
|
|
void PointGeometryShader::Init()
|
2011-03-14 03:38:29 -06:00
|
|
|
{
|
2011-06-11 13:37:21 -06:00
|
|
|
m_ready = false;
|
|
|
|
|
|
|
|
HRESULT hr;
|
|
|
|
|
2011-03-14 03:38:29 -06:00
|
|
|
// Create constant buffer for uploading data to geometry shader
|
|
|
|
|
2011-04-15 16:34:54 -06:00
|
|
|
D3D11_BUFFER_DESC bd = CD3D11_BUFFER_DESC(sizeof(PointGSParams_Padded),
|
|
|
|
D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
|
2014-03-09 14:14:26 -06:00
|
|
|
hr = D3D::device->CreateBuffer(&bd, nullptr, &m_paramsBuffer);
|
2011-06-11 13:37:21 -06:00
|
|
|
CHECK(SUCCEEDED(hr), "create point geometry shader params buffer");
|
2011-03-14 03:38:29 -06:00
|
|
|
D3D::SetDebugObjectName(m_paramsBuffer, "point geometry shader params buffer");
|
|
|
|
|
|
|
|
m_ready = true;
|
|
|
|
}
|
|
|
|
|
2011-06-11 13:37:21 -06:00
|
|
|
void PointGeometryShader::Shutdown()
|
|
|
|
{
|
|
|
|
m_ready = false;
|
|
|
|
|
2014-02-12 08:00:34 -07:00
|
|
|
for (auto& it : m_shaders)
|
2011-06-11 13:37:21 -06:00
|
|
|
{
|
2014-02-12 08:00:34 -07:00
|
|
|
SAFE_RELEASE(it.second);
|
2011-06-11 13:37:21 -06:00
|
|
|
}
|
|
|
|
m_shaders.clear();
|
|
|
|
|
|
|
|
SAFE_RELEASE(m_paramsBuffer);
|
|
|
|
}
|
|
|
|
|
2011-03-27 23:42:21 -06:00
|
|
|
bool PointGeometryShader::SetShader(u32 components, float pointSize,
|
2011-04-15 16:34:54 -06:00
|
|
|
float texOffset, float vpWidth, float vpHeight, const bool* texOffsetEnable)
|
2011-03-14 03:38:29 -06:00
|
|
|
{
|
|
|
|
if (!m_ready)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Make sure geometry shader for "components" is available
|
|
|
|
ComboMap::iterator shaderIt = m_shaders.find(components);
|
|
|
|
if (shaderIt == m_shaders.end())
|
|
|
|
{
|
|
|
|
// Generate new shader. Warning: not thread-safe.
|
2013-03-26 17:13:23 -06:00
|
|
|
static char buffer[16384];
|
2013-03-29 15:24:49 -06:00
|
|
|
ShaderCode code;
|
2013-03-26 17:13:23 -06:00
|
|
|
code.SetBuffer(buffer);
|
2014-10-29 06:16:24 -06:00
|
|
|
GenerateVSOutputStruct(code, API_D3D);
|
2013-03-26 17:13:23 -06:00
|
|
|
code.Write("\n%s", POINT_GS_COMMON);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2011-10-01 20:03:51 -06:00
|
|
|
std::stringstream numTexCoordsStream;
|
2014-04-27 12:59:04 -06:00
|
|
|
numTexCoordsStream << xfmem.numTexGen.numTexGens;
|
2011-03-14 03:38:29 -06:00
|
|
|
|
|
|
|
INFO_LOG(VIDEO, "Compiling point geometry shader for components 0x%.08X (num texcoords %d)",
|
2014-04-27 12:59:04 -06:00
|
|
|
components, xfmem.numTexGen.numTexGens);
|
2011-03-14 03:38:29 -06:00
|
|
|
|
2011-10-01 20:03:51 -06:00
|
|
|
const std::string& numTexCoordsStr = numTexCoordsStream.str();
|
2011-03-14 03:38:29 -06:00
|
|
|
D3D_SHADER_MACRO macros[] = {
|
2011-10-01 20:03:51 -06:00
|
|
|
{ "NUM_TEXCOORDS", numTexCoordsStr.c_str() },
|
2014-03-09 14:14:26 -06:00
|
|
|
{ nullptr, nullptr }
|
2011-03-14 03:38:29 -06:00
|
|
|
};
|
2014-07-07 17:28:12 -06:00
|
|
|
ID3D11GeometryShader* newShader = D3D::CompileAndCreateGeometryShader(code.GetBuffer(), macros);
|
2011-03-14 03:38:29 -06:00
|
|
|
if (!newShader)
|
|
|
|
{
|
|
|
|
WARN_LOG(VIDEO, "Point geometry shader for components 0x%.08X failed to compile", components);
|
|
|
|
// Add dummy shader to prevent trying to compile again
|
2014-03-09 14:14:26 -06:00
|
|
|
m_shaders[components] = nullptr;
|
2011-03-14 03:38:29 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
shaderIt = m_shaders.insert(std::make_pair(components, newShader)).first;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shaderIt != m_shaders.end())
|
|
|
|
{
|
|
|
|
if (shaderIt->second)
|
|
|
|
{
|
2011-04-15 16:34:54 -06:00
|
|
|
D3D11_MAPPED_SUBRESOURCE map;
|
2011-06-11 13:37:21 -06:00
|
|
|
HRESULT hr = D3D::context->Map(m_paramsBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
|
2011-04-15 16:34:54 -06:00
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
{
|
|
|
|
PointGSParams* params = (PointGSParams*)map.pData;
|
|
|
|
params->PointSize = pointSize;
|
2011-06-11 13:37:21 -06:00
|
|
|
|
2011-04-15 16:34:54 -06:00
|
|
|
params->TexOffset = texOffset;
|
|
|
|
params->VpWidth = vpWidth;
|
|
|
|
params->VpHeight = vpHeight;
|
|
|
|
for (int i = 0; i < 8; ++i)
|
|
|
|
params->TexOffsetEnable[i] = texOffsetEnable[i] ? 1.f : 0.f;
|
|
|
|
|
2011-06-11 13:37:21 -06:00
|
|
|
D3D::context->Unmap(m_paramsBuffer, 0);
|
2011-04-15 16:34:54 -06:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ERROR_LOG(VIDEO, "Failed to map point gs params buffer");
|
2011-06-11 13:37:21 -06:00
|
|
|
|
2011-03-27 23:42:21 -06:00
|
|
|
DEBUG_LOG(VIDEO, "Point params: size %f, texOffset %f, vpWidth %f, vpHeight %f",
|
|
|
|
pointSize, texOffset, vpWidth, vpHeight);
|
2011-03-14 03:38:29 -06:00
|
|
|
|
2014-12-06 06:54:06 -07:00
|
|
|
D3D::stateman->SetGeometryShader(shaderIt->second);
|
|
|
|
D3D::stateman->SetGeometryConstants(m_paramsBuffer);
|
2011-03-14 03:38:29 -06:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|