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 "Common.h"
|
|
|
|
#include "FileUtil.h"
|
|
|
|
#include "LinearDiskCache.h"
|
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "D3DBase.h"
|
|
|
|
#include "D3Dcompiler.h"
|
|
|
|
#include "D3DShader.h"
|
|
|
|
#include "Statistics.h"
|
|
|
|
#include "VideoConfig.h"
|
|
|
|
#include "PixelShaderGen.h"
|
|
|
|
#include "PixelShaderManager.h"
|
|
|
|
#include "PixelShaderCache.h"
|
|
|
|
#include "VertexLoader.h"
|
|
|
|
#include "BPMemory.h"
|
|
|
|
#include "XFMemory.h"
|
|
|
|
#include "ImageWrite.h"
|
|
|
|
|
|
|
|
extern int frameCount;
|
|
|
|
|
|
|
|
PixelShaderCache::PSCache PixelShaderCache::PixelShaders;
|
|
|
|
const PixelShaderCache::PSCacheEntry* PixelShaderCache::last_entry;
|
|
|
|
|
2010-11-15 05:22:03 +00:00
|
|
|
LinearDiskCache<PIXELSHADERUID, u8> g_ps_disk_cache;
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2010-11-27 11:11:05 +00:00
|
|
|
ID3D11PixelShader* s_ColorMatrixProgram[2] = {NULL};
|
|
|
|
ID3D11PixelShader* s_ColorCopyProgram[2] = {NULL};
|
|
|
|
ID3D11PixelShader* s_DepthMatrixProgram[2] = {NULL};
|
2010-06-13 19:50:06 +00:00
|
|
|
ID3D11PixelShader* s_ClearProgram = NULL;
|
|
|
|
|
|
|
|
const char clear_program_code[] = {
|
|
|
|
"void main(\n"
|
2010-06-14 14:36:01 +00:00
|
|
|
"out float4 ocol0 : SV_Target,\n"
|
|
|
|
"in float4 pos : SV_Position,\n"
|
2010-06-13 19:50:06 +00:00
|
|
|
"in float4 incol0 : COLOR0){\n"
|
|
|
|
"ocol0 = incol0;\n"
|
|
|
|
"}\n"
|
|
|
|
};
|
|
|
|
|
|
|
|
const char color_copy_program_code[] = {
|
2010-06-14 14:36:01 +00:00
|
|
|
"sampler samp0 : register(s0);\n"
|
|
|
|
"Texture2D Tex0 : register(t0);\n"
|
2010-06-13 19:50:06 +00:00
|
|
|
"void main(\n"
|
2010-06-14 14:36:01 +00:00
|
|
|
"out float4 ocol0 : SV_Target,\n"
|
|
|
|
"in float4 pos : SV_Position,\n"
|
2010-06-13 19:50:06 +00:00
|
|
|
"in float2 uv0 : TEXCOORD0){\n"
|
2010-06-14 14:36:01 +00:00
|
|
|
"ocol0 = Tex0.Sample(samp0,uv0);\n"
|
2010-06-13 19:50:06 +00:00
|
|
|
"}\n"
|
|
|
|
};
|
|
|
|
|
2010-11-27 11:11:05 +00:00
|
|
|
// TODO: Improve sampling algorithm!
|
|
|
|
const char color_copy_program_code_msaa[] = {
|
|
|
|
"sampler samp0 : register(s0);\n"
|
|
|
|
"Texture2DMS<float4, %d> Tex0 : register(t0);\n"
|
|
|
|
"void main(\n"
|
|
|
|
"out float4 ocol0 : SV_Target,\n"
|
|
|
|
"in float4 pos : SV_Position,\n"
|
|
|
|
"in float2 uv0 : TEXCOORD0){\n"
|
|
|
|
"int width, height, samples;\n"
|
|
|
|
"Tex0.GetDimensions(width, height, samples);\n"
|
|
|
|
"ocol0 = 0;\n"
|
|
|
|
"for(int i = 0; i < samples; ++i)\n"
|
|
|
|
" ocol0 += Tex0.Load(int2(uv0.x*(width), uv0.y*(height)), i);\n"
|
|
|
|
"ocol0 /= samples;\n"
|
|
|
|
"}\n"
|
|
|
|
};
|
|
|
|
|
2010-06-13 19:50:06 +00:00
|
|
|
const char color_matrix_program_code[] = {
|
2010-06-14 14:36:01 +00:00
|
|
|
"sampler samp0 : register(s0);\n"
|
|
|
|
"Texture2D Tex0 : register(t0);\n"
|
2010-06-13 19:50:06 +00:00
|
|
|
"uniform float4 cColMatrix[5] : register(c0);\n"
|
|
|
|
"void main(\n"
|
2010-06-14 14:36:01 +00:00
|
|
|
"out float4 ocol0 : SV_Target,\n"
|
|
|
|
"in float4 pos : SV_Position,\n"
|
2010-06-13 19:50:06 +00:00
|
|
|
" in float2 uv0 : TEXCOORD0){\n"
|
2010-06-14 14:36:01 +00:00
|
|
|
"float4 texcol = Tex0.Sample(samp0,uv0);\n"
|
2010-06-13 19:50:06 +00:00
|
|
|
"ocol0 = float4(dot(texcol,cColMatrix[0]),dot(texcol,cColMatrix[1]),dot(texcol,cColMatrix[2]),dot(texcol,cColMatrix[3])) + cColMatrix[4];\n"
|
|
|
|
"}\n"
|
|
|
|
};
|
|
|
|
|
2010-11-27 11:11:05 +00:00
|
|
|
const char color_matrix_program_code_msaa[] = {
|
|
|
|
"sampler samp0 : register(s0);\n"
|
|
|
|
"Texture2DMS<float4, %d> Tex0 : register(t0);\n"
|
|
|
|
"uniform float4 cColMatrix[5] : register(c0);\n"
|
|
|
|
"void main(\n"
|
|
|
|
"out float4 ocol0 : SV_Target,\n"
|
|
|
|
"in float4 pos : SV_Position,\n"
|
|
|
|
" in float2 uv0 : TEXCOORD0){\n"
|
|
|
|
"int width, height, samples;\n"
|
|
|
|
"Tex0.GetDimensions(width, height, samples);\n"
|
|
|
|
"float4 texcol = 0;\n"
|
|
|
|
"for(int i = 0; i < samples; ++i)\n"
|
|
|
|
" texcol += Tex0.Load(int2(uv0.x*(width), uv0.y*(height)), i);\n"
|
|
|
|
"texcol /= samples;\n"
|
|
|
|
"ocol0 = float4(dot(texcol,cColMatrix[0]),dot(texcol,cColMatrix[1]),dot(texcol,cColMatrix[2]),dot(texcol,cColMatrix[3])) + cColMatrix[4];\n"
|
|
|
|
"}\n"
|
|
|
|
};
|
|
|
|
|
2010-06-13 19:50:06 +00:00
|
|
|
const char depth_matrix_program[] = {
|
2010-06-14 14:36:01 +00:00
|
|
|
"sampler samp0 : register(s0);\n"
|
|
|
|
"Texture2D Tex0 : register(t0);\n"
|
2010-06-13 19:50:06 +00:00
|
|
|
"uniform float4 cColMatrix[5] : register(c0);\n"
|
|
|
|
"void main(\n"
|
2010-06-14 14:36:01 +00:00
|
|
|
"out float4 ocol0 : SV_Target,\n"
|
|
|
|
" in float4 pos : SV_Position,\n"
|
2010-06-13 19:50:06 +00:00
|
|
|
" in float2 uv0 : TEXCOORD0){\n"
|
2010-06-14 14:36:01 +00:00
|
|
|
"float4 texcol = Tex0.Sample(samp0,uv0);\n"
|
2010-06-13 19:50:06 +00:00
|
|
|
"float4 EncodedDepth = frac((texcol.r * (16777215.0f/16777216.0f)) * float4(1.0f,255.0f,255.0f*255.0f,255.0f*255.0f*255.0f));\n"
|
|
|
|
"texcol = float4((EncodedDepth.rgb * (16777216.0f/16777215.0f)),1.0f);\n"
|
|
|
|
"ocol0 = float4(dot(texcol,cColMatrix[0]),dot(texcol,cColMatrix[1]),dot(texcol,cColMatrix[2]),dot(texcol,cColMatrix[3])) + cColMatrix[4];\n"
|
|
|
|
"}\n"
|
|
|
|
};
|
|
|
|
|
2010-11-27 11:11:05 +00:00
|
|
|
const char depth_matrix_program_msaa[] = {
|
|
|
|
"sampler samp0 : register(s0);\n"
|
|
|
|
"Texture2DMS<float4, %d> Tex0 : register(t0);\n"
|
|
|
|
"uniform float4 cColMatrix[5] : register(c0);\n"
|
|
|
|
"void main(\n"
|
|
|
|
"out float4 ocol0 : SV_Target,\n"
|
|
|
|
" in float4 pos : SV_Position,\n"
|
|
|
|
" in float2 uv0 : TEXCOORD0){\n"
|
|
|
|
"int width, height, samples;\n"
|
|
|
|
"Tex0.GetDimensions(width, height, samples);\n"
|
|
|
|
"float4 texcol = 0;\n"
|
|
|
|
"for(int i = 0; i < samples; ++i)\n"
|
|
|
|
" texcol += Tex0.Load(int2(uv0.x*(width), uv0.y*(height)), i);\n"
|
|
|
|
"texcol /= samples;\n"
|
|
|
|
"float4 EncodedDepth = frac((texcol.r * (16777215.0f/16777216.0f)) * float4(1.0f,255.0f,255.0f*255.0f,255.0f*255.0f*255.0f));\n"
|
|
|
|
"texcol = float4((EncodedDepth.rgb * (16777216.0f/16777215.0f)),1.0f);\n"
|
|
|
|
"ocol0 = float4(dot(texcol,cColMatrix[0]),dot(texcol,cColMatrix[1]),dot(texcol,cColMatrix[2]),dot(texcol,cColMatrix[3])) + cColMatrix[4];\n"
|
|
|
|
"}\n"
|
|
|
|
};
|
|
|
|
|
|
|
|
ID3D11PixelShader* PixelShaderCache::GetColorCopyProgram(bool multisampled)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2010-11-27 11:37:57 +00:00
|
|
|
if (!multisampled || D3D::GetAAMode(g_ActiveConfig.iMultisampleMode).Count == 1) return s_ColorCopyProgram[0];
|
|
|
|
else if (s_ColorCopyProgram[1]) return s_ColorCopyProgram[1];
|
2010-11-27 11:11:05 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// create MSAA shader for current AA mode
|
|
|
|
char buf[1024];
|
|
|
|
int l = sprintf_s(buf, 1024, color_copy_program_code_msaa, D3D::GetAAMode(g_ActiveConfig.iMultisampleMode));
|
|
|
|
s_ColorCopyProgram[1] = D3D::CompileAndCreatePixelShader(buf, l);
|
|
|
|
CHECK(s_ColorCopyProgram[1]!=NULL, "Create color copy MSAA pixel shader");
|
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)s_ColorCopyProgram[1], "color copy MSAA pixel shader");
|
|
|
|
return s_ColorCopyProgram[1];
|
|
|
|
}
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
2010-11-27 11:11:05 +00:00
|
|
|
ID3D11PixelShader* PixelShaderCache::GetColorMatrixProgram(bool multisampled)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2010-11-27 11:37:57 +00:00
|
|
|
if (!multisampled || D3D::GetAAMode(g_ActiveConfig.iMultisampleMode).Count == 1) return s_ColorMatrixProgram[0];
|
|
|
|
else if (s_ColorMatrixProgram[1]) return s_ColorMatrixProgram[1];
|
2010-11-27 11:11:05 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// create MSAA shader for current AA mode
|
|
|
|
char buf[1024];
|
|
|
|
int l = sprintf_s(buf, 1024, color_matrix_program_code_msaa, D3D::GetAAMode(g_ActiveConfig.iMultisampleMode));
|
|
|
|
s_ColorMatrixProgram[1] = D3D::CompileAndCreatePixelShader(buf, l);
|
|
|
|
CHECK(s_ColorMatrixProgram[1]!=NULL, "Create color matrix MSAA pixel shader");
|
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)s_ColorMatrixProgram[1], "color matrix MSAA pixel shader");
|
|
|
|
return s_ColorMatrixProgram[1];
|
|
|
|
}
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
2010-11-27 11:11:05 +00:00
|
|
|
ID3D11PixelShader* PixelShaderCache::GetDepthMatrixProgram(bool multisampled)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2010-11-27 11:37:57 +00:00
|
|
|
if (!multisampled || D3D::GetAAMode(g_ActiveConfig.iMultisampleMode).Count == 1) return s_DepthMatrixProgram[0];
|
|
|
|
else if (s_DepthMatrixProgram[1]) return s_DepthMatrixProgram[1];
|
2010-11-27 11:11:05 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// create MSAA shader for current AA mode
|
|
|
|
char buf[1024];
|
|
|
|
int l = sprintf_s(buf, 1024, depth_matrix_program_msaa, D3D::GetAAMode(g_ActiveConfig.iMultisampleMode));
|
|
|
|
s_DepthMatrixProgram[1] = D3D::CompileAndCreatePixelShader(buf, l);
|
|
|
|
CHECK(s_DepthMatrixProgram[1]!=NULL, "Create depth matrix MSAA pixel shader");
|
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)s_DepthMatrixProgram[1], "depth matrix MSAA pixel shader");
|
|
|
|
return s_DepthMatrixProgram[1];
|
|
|
|
}
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ID3D11PixelShader* PixelShaderCache::GetClearProgram()
|
|
|
|
{
|
|
|
|
return s_ClearProgram;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
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, // C_FOG, 8
|
2010-09-23 02:17:48 +00:00
|
|
|
116, 120, 124 ,128, // C_COLORMATRIX, 16
|
|
|
|
132, 136, 140, 144, 148, // C_PLIGHTS0, 20
|
|
|
|
152, 156, 160, 164, 168, // C_PLIGHTS1, 20
|
|
|
|
172, 176, 180, 184, 188, // C_PLIGHTS2, 20
|
|
|
|
192, 196, 200, 204, 208, // C_PLIGHTS3, 20
|
|
|
|
212, 216, 220, 224, 228, // C_PLIGHTS4, 20
|
|
|
|
232, 236, 240, 244, 248, // C_PLIGHTS5, 20
|
|
|
|
252, 256, 260, 264, 268, // C_PLIGHTS6, 20
|
|
|
|
272, 276, 280, 284, 288, // C_PLIGHTS7, 20
|
|
|
|
292, 296, 300, 304, // C_PMATERIALS, 16
|
2010-06-13 19:50:06 +00:00
|
|
|
};
|
|
|
|
void SetPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4)
|
|
|
|
{
|
2010-06-14 22:38:47 +00:00
|
|
|
D3D::gfxstate->psconstants[ps_constant_offset_table[const_number] ] = f1;
|
|
|
|
D3D::gfxstate->psconstants[ps_constant_offset_table[const_number]+1] = f2;
|
|
|
|
D3D::gfxstate->psconstants[ps_constant_offset_table[const_number]+2] = f3;
|
|
|
|
D3D::gfxstate->psconstants[ps_constant_offset_table[const_number]+3] = f4;
|
|
|
|
D3D::gfxstate->pscbufchanged = true;
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetPSConstant4fv(unsigned int const_number, const float* f)
|
|
|
|
{
|
2010-06-14 22:38:47 +00:00
|
|
|
memcpy(&D3D::gfxstate->psconstants[ps_constant_offset_table[const_number]], f, sizeof(float)*4);
|
|
|
|
D3D::gfxstate->pscbufchanged = true;
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetMultiPSConstant4fv(unsigned int const_number, unsigned int count, const float* f)
|
|
|
|
{
|
2010-06-14 22:38:47 +00:00
|
|
|
memcpy(&D3D::gfxstate->psconstants[ps_constant_offset_table[const_number]], f, sizeof(float)*4*count);
|
|
|
|
D3D::gfxstate->pscbufchanged = true;
|
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 PixelShaderCacheInserter : public LinearDiskCacheReader<PIXELSHADERUID, u8>
|
|
|
|
{
|
2010-06-13 19:50:06 +00:00
|
|
|
public:
|
2010-11-15 05:22:03 +00:00
|
|
|
void Read(const PIXELSHADERUID &key, const u8 *value, u32 value_size)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
2010-11-15 05:22:03 +00:00
|
|
|
PixelShaderCache::InsertByteCode(key, value, value_size);
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void PixelShaderCache::Init()
|
|
|
|
{
|
|
|
|
// used when drawing clear quads
|
2010-06-18 23:33:07 +00:00
|
|
|
s_ClearProgram = D3D::CompileAndCreatePixelShader(clear_program_code, sizeof(clear_program_code));
|
2010-06-18 18:40:58 +00:00
|
|
|
CHECK(s_ClearProgram!=NULL, "Create clear pixel shader");
|
2010-06-17 10:42:57 +00:00
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)s_ClearProgram, "clear pixel shader");
|
2010-06-13 19:50:06 +00:00
|
|
|
|
|
|
|
// used when copying/resolving the color buffer
|
2010-11-27 11:11:05 +00:00
|
|
|
s_ColorCopyProgram[0] = D3D::CompileAndCreatePixelShader(color_copy_program_code, sizeof(color_copy_program_code));
|
|
|
|
CHECK(s_ColorCopyProgram[0]!=NULL, "Create color copy pixel shader");
|
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)s_ColorCopyProgram[0], "color copy pixel shader");
|
2010-06-13 19:50:06 +00:00
|
|
|
|
|
|
|
// used for color conversion
|
2010-11-27 11:11:05 +00:00
|
|
|
s_ColorMatrixProgram[0] = D3D::CompileAndCreatePixelShader(color_matrix_program_code, sizeof(color_matrix_program_code));
|
|
|
|
CHECK(s_ColorMatrixProgram[0]!=NULL, "Create color matrix pixel shader");
|
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)s_ColorMatrixProgram[0], "color matrix pixel shader");
|
2010-06-13 19:50:06 +00:00
|
|
|
|
|
|
|
// used for depth copy
|
2010-11-27 11:11:05 +00:00
|
|
|
s_DepthMatrixProgram[0] = D3D::CompileAndCreatePixelShader(depth_matrix_program, sizeof(depth_matrix_program));
|
|
|
|
CHECK(s_DepthMatrixProgram[0]!=NULL, "Create depth matrix pixel shader");
|
|
|
|
D3D::SetDebugObjectName((ID3D11DeviceChild*)s_DepthMatrixProgram[0], "depth matrix pixel shader");
|
2010-06-13 19:50:06 +00:00
|
|
|
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
if (!File::Exists(File::GetUserPath(D_SHADERCACHE_IDX)))
|
|
|
|
File::CreateDir(File::GetUserPath(D_SHADERCACHE_IDX));
|
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
SETSTAT(stats.numPixelShadersCreated, 0);
|
|
|
|
SETSTAT(stats.numPixelShadersAlive, 0);
|
|
|
|
|
2010-06-13 19:50:06 +00:00
|
|
|
char cache_filename[MAX_PATH];
|
|
|
|
sprintf(cache_filename, "%sdx11-%s-ps.cache", File::GetUserPath(D_SHADERCACHE_IDX), globals->unique_id);
|
|
|
|
PixelShaderCacheInserter inserter;
|
2010-11-15 05:22:03 +00:00
|
|
|
g_ps_disk_cache.OpenAndRead(cache_filename, inserter);
|
2010-06-13 19:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ONLY to be used during shutdown.
|
|
|
|
void PixelShaderCache::Clear()
|
|
|
|
{
|
|
|
|
for (PSCache::iterator iter = PixelShaders.begin(); iter != PixelShaders.end(); iter++)
|
|
|
|
iter->second.Destroy();
|
|
|
|
PixelShaders.clear();
|
|
|
|
}
|
|
|
|
|
2010-11-27 11:11:05 +00:00
|
|
|
// Used in Swap() when AA mode has changed
|
|
|
|
void PixelShaderCache::InvalidateMSAAShaders()
|
|
|
|
{
|
|
|
|
SAFE_RELEASE(s_ColorCopyProgram[1]);
|
|
|
|
SAFE_RELEASE(s_ColorMatrixProgram[1]);
|
|
|
|
SAFE_RELEASE(s_DepthMatrixProgram[1]);
|
|
|
|
}
|
|
|
|
|
2010-06-13 19:50:06 +00:00
|
|
|
void PixelShaderCache::Shutdown()
|
|
|
|
{
|
|
|
|
SAFE_RELEASE(s_ClearProgram);
|
2010-11-27 11:11:05 +00:00
|
|
|
for (int i = 0; i < 2; ++i)
|
|
|
|
{
|
|
|
|
SAFE_RELEASE(s_ColorCopyProgram[i]);
|
|
|
|
SAFE_RELEASE(s_ColorMatrixProgram[i]);
|
|
|
|
SAFE_RELEASE(s_DepthMatrixProgram[i]);
|
|
|
|
}
|
2010-06-13 19:50:06 +00:00
|
|
|
|
|
|
|
Clear();
|
|
|
|
g_ps_disk_cache.Sync();
|
|
|
|
g_ps_disk_cache.Close();
|
|
|
|
}
|
|
|
|
|
2010-10-21 05:22:18 +00:00
|
|
|
bool PixelShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 components)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
|
|
|
PIXELSHADERUID uid;
|
2010-10-21 05:22:18 +00:00
|
|
|
GetPixelShaderId(&uid, dstAlphaMode);
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
// Check if the shader is already set
|
2010-06-13 19:50:06 +00:00
|
|
|
if (uid == last_pixel_shader_uid && PixelShaders[uid].frameCount == frameCount)
|
|
|
|
{
|
|
|
|
PSCache::const_iterator iter = PixelShaders.find(uid);
|
|
|
|
return (iter != PixelShaders.end() && iter->second.shader);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&last_pixel_shader_uid, &uid, sizeof(PIXELSHADERUID));
|
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
// Check if the shader is already in the cache
|
2010-06-13 19:50:06 +00:00
|
|
|
PSCache::iterator iter;
|
|
|
|
iter = PixelShaders.find(uid);
|
|
|
|
if (iter != PixelShaders.end())
|
|
|
|
{
|
|
|
|
iter->second.frameCount = frameCount;
|
|
|
|
const PSCacheEntry &entry = iter->second;
|
|
|
|
last_entry = &entry;
|
|
|
|
|
|
|
|
D3D::gfxstate->SetPShader(entry.shader);
|
|
|
|
return (entry.shader != NULL);
|
|
|
|
}
|
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
// Need to compile a new shader
|
2010-10-21 05:22:18 +00:00
|
|
|
const char* code = GeneratePixelShaderCode(dstAlphaMode, API_D3D11, components);
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2010-06-16 23:25:19 +00:00
|
|
|
D3DBlob* pbytecode;
|
2010-06-13 19:50:06 +00:00
|
|
|
if (!D3D::CompilePixelShader(code, strlen(code), &pbytecode))
|
|
|
|
{
|
|
|
|
PanicAlert("Failed to compile Pixel Shader:\n\n%s", code);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
// Insert the bytecode into the caches
|
2010-11-15 05:22:03 +00:00
|
|
|
g_ps_disk_cache.Append(uid, pbytecode->Data(), pbytecode->Size());
|
2010-06-13 19:50:06 +00:00
|
|
|
g_ps_disk_cache.Sync();
|
|
|
|
|
2010-06-16 23:25:19 +00:00
|
|
|
bool result = InsertByteCode(uid, pbytecode->Data(), pbytecode->Size());
|
2010-06-13 19:50:06 +00:00
|
|
|
D3D::gfxstate->SetPShader(last_entry->shader);
|
|
|
|
pbytecode->Release();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-11-15 05:22:03 +00:00
|
|
|
bool PixelShaderCache::InsertByteCode(const PIXELSHADERUID &uid, const void* bytecode, unsigned int bytecodelen)
|
2010-06-13 19:50:06 +00:00
|
|
|
{
|
|
|
|
ID3D11PixelShader* shader = D3D::CreatePixelShaderFromByteCode(bytecode, bytecodelen);
|
|
|
|
if (shader == NULL)
|
|
|
|
{
|
|
|
|
PanicAlert("Failed to create pixel shader at %s %d\n", __FILE__, __LINE__);
|
|
|
|
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 pixel shader of PixelShaderCache");
|
2010-06-13 19:50:06 +00:00
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
// Make an entry in the table
|
2010-06-13 19:50:06 +00:00
|
|
|
PSCacheEntry newentry;
|
|
|
|
newentry.shader = shader;
|
|
|
|
newentry.frameCount = frameCount;
|
|
|
|
PixelShaders[uid] = newentry;
|
|
|
|
last_entry = &PixelShaders[uid];
|
|
|
|
|
2010-09-28 02:15:02 +00:00
|
|
|
if (!shader) {
|
|
|
|
// INCSTAT(stats.numPixelShadersFailed);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-06-13 19:50:06 +00:00
|
|
|
INCSTAT(stats.numPixelShadersCreated);
|
|
|
|
SETSTAT(stats.numPixelShadersAlive, PixelShaders.size());
|
|
|
|
return true;
|
2010-06-17 10:42:57 +00:00
|
|
|
}
|