Skip to content

Commit 73407d8

Browse files
committed
GS/DX: Specify shader model directly instead of via feature level
1 parent 20bbb99 commit 73407d8

7 files changed

Lines changed: 80 additions & 49 deletions

File tree

pcsx2/GS/Renderers/DX11/D3D.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,28 +466,52 @@ GSRendererType D3D::GetPreferredRenderer()
466466
}
467467
}
468468

469-
wil::com_ptr_nothrow<ID3DBlob> D3D::CompileShader(D3D::ShaderType type, D3D_FEATURE_LEVEL feature_level, bool debug,
469+
const char* D3D::ShaderModelToCacheString(D3D::ShaderModel shader_model)
470+
{
471+
switch (shader_model)
472+
{
473+
case ShaderModel::SM40:
474+
return "sm40";
475+
case ShaderModel::SM41:
476+
return "sm41";
477+
case ShaderModel::SM50:
478+
return "sm50";
479+
case ShaderModel::SM51:
480+
return "sm51";
481+
default:
482+
return "unk";
483+
}
484+
}
485+
486+
wil::com_ptr_nothrow<ID3DBlob> D3D::CompileShader(D3D::ShaderType type, D3D::ShaderModel shader_model, bool debug,
470487
const std::string_view code, const D3D_SHADER_MACRO* macros /* = nullptr */,
471488
const char* entry_point /* = "main" */)
472489
{
473490
const char* target;
474-
switch (feature_level)
491+
switch (shader_model)
475492
{
476-
case D3D_FEATURE_LEVEL_10_0:
493+
case ShaderModel::SM40:
477494
{
478495
static constexpr std::array<const char*, 4> targets = {{"vs_4_0", "ps_4_0", "cs_4_0"}};
479496
target = targets[static_cast<int>(type)];
480497
}
481498
break;
482499

483-
case D3D_FEATURE_LEVEL_11_0:
500+
case ShaderModel::SM41:
501+
{
502+
static constexpr std::array<const char*, 4> targets = {{"vs_4_1", "ps_4_1", "cs_4_1"}};
503+
target = targets[static_cast<int>(type)];
504+
}
505+
break;
506+
507+
case ShaderModel::SM50:
484508
{
485509
static constexpr std::array<const char*, 4> targets = {{"vs_5_0", "ps_5_0", "cs_5_0"}};
486510
target = targets[static_cast<int>(type)];
487511
}
488512
break;
489513

490-
case D3D_FEATURE_LEVEL_11_1:
514+
case ShaderModel::SM51:
491515
default:
492516
{
493517
static constexpr std::array<const char*, 4> targets = {{"vs_5_1", "ps_5_1", "cs_5_1"}};

pcsx2/GS/Renderers/DX11/D3D.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ namespace D3D
6363
Compute
6464
};
6565

66-
wil::com_ptr_nothrow<ID3DBlob> CompileShader(ShaderType type, D3D_FEATURE_LEVEL feature_level, bool debug,
66+
enum class ShaderModel
67+
{
68+
SM40 = 0x40, // DX11 FL 10_0
69+
SM41 = 0x41, // DX11 FL 10_1
70+
SM50 = 0x50, // DX11 FL 11_0
71+
SM51 = 0x51, // DX12
72+
};
73+
74+
const char* ShaderModelToCacheString(ShaderModel shader_model);
75+
76+
wil::com_ptr_nothrow<ID3DBlob> CompileShader(ShaderType type, ShaderModel shader_model, bool debug,
6777
const std::string_view code, const D3D_SHADER_MACRO* macros = nullptr, const char* entry_point = "main");
6878
}; // namespace D3D

pcsx2/GS/Renderers/DX11/D3D11ShaderCache.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,27 @@ bool D3D11ShaderCache::CacheIndexKey::operator!=(const CacheIndexKey& key) const
5656

5757
bool D3D11ShaderCache::Open(D3D_FEATURE_LEVEL feature_level, bool debug)
5858
{
59-
m_feature_level = feature_level;
59+
switch (feature_level)
60+
{
61+
case D3D_FEATURE_LEVEL_10_0:
62+
m_shader_model = D3D::ShaderModel::SM40;
63+
break;
64+
case D3D_FEATURE_LEVEL_10_1:
65+
m_shader_model = D3D::ShaderModel::SM41;
66+
break;
67+
case D3D_FEATURE_LEVEL_11_0:
68+
m_shader_model = D3D::ShaderModel::SM50;
69+
break;
70+
default:
71+
pxAssert(false);
72+
return false;
73+
}
74+
6075
m_debug = debug;
6176

6277
if (!GSConfig.DisableShaderCache)
6378
{
64-
const std::string base_filename = GetCacheBaseFileName(feature_level, debug);
79+
const std::string base_filename = GetCacheBaseFileName(m_shader_model, debug);
6580
const std::string index_filename = base_filename + ".idx";
6681
const std::string blob_filename = base_filename + ".bin";
6782

@@ -198,19 +213,10 @@ bool D3D11ShaderCache::ReadExisting(const std::string& index_filename, const std
198213
return true;
199214
}
200215

201-
std::string D3D11ShaderCache::GetCacheBaseFileName(D3D_FEATURE_LEVEL feature_level, bool debug)
216+
std::string D3D11ShaderCache::GetCacheBaseFileName(D3D::ShaderModel shader_model, bool debug)
202217
{
203218
std::string base_filename = "d3d_shaders_";
204-
205-
switch (feature_level)
206-
{
207-
case D3D_FEATURE_LEVEL_11_0:
208-
base_filename += "sm50";
209-
break;
210-
default:
211-
base_filename += "unk";
212-
break;
213-
}
219+
base_filename += D3D::ShaderModelToCacheString(shader_model);
214220

215221
if (debug)
216222
base_filename += "_debug";
@@ -400,7 +406,7 @@ wil::com_ptr_nothrow<ID3DBlob> D3D11ShaderCache::CompileAndAddShaderBlob(const C
400406
const std::string_view shader_code, const D3D_SHADER_MACRO* macros, const char* entry_point)
401407
{
402408
wil::com_ptr_nothrow<ID3DBlob> blob =
403-
D3D::CompileShader(key.shader_type, m_feature_level, m_debug, shader_code, macros, entry_point);
409+
D3D::CompileShader(key.shader_type, m_shader_model, m_debug, shader_code, macros, entry_point);
404410
if (!blob)
405411
return {};
406412

pcsx2/GS/Renderers/DX11/D3D11ShaderCache.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class D3D11ShaderCache
1616
D3D11ShaderCache();
1717
~D3D11ShaderCache();
1818

19-
D3D_FEATURE_LEVEL GetFeatureLevel() const { return m_feature_level; }
2019
bool UsingDebugShaders() const { return m_debug; }
2120

2221
bool Open(D3D_FEATURE_LEVEL feature_level, bool debug);
@@ -74,7 +73,7 @@ class D3D11ShaderCache
7473

7574
using CacheIndex = std::unordered_map<CacheIndexKey, CacheIndexData, CacheIndexEntryHasher>;
7675

77-
static std::string GetCacheBaseFileName(D3D_FEATURE_LEVEL feature_level, bool debug);
76+
static std::string GetCacheBaseFileName(D3D::ShaderModel shader_mode, bool debug);
7877
static CacheIndexKey GetCacheKey(D3D::ShaderType type, const std::string_view shader_code,
7978
const D3D_SHADER_MACRO* macros, const char* entry_point);
8079

@@ -89,6 +88,6 @@ class D3D11ShaderCache
8988

9089
CacheIndex m_index;
9190

92-
D3D_FEATURE_LEVEL m_feature_level = D3D_FEATURE_LEVEL_11_0;
91+
D3D::ShaderModel m_shader_model = D3D::ShaderModel::SM50;
9392
bool m_debug = false;
9493
};

pcsx2/GS/Renderers/DX12/D3D12ShaderCache.cpp

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-License-Identifier: GPL-3.0+
33

44
#include "GS/Renderers/DX12/D3D12ShaderCache.h"
5-
#include "GS/Renderers/DX11/D3D.h"
65
#include "GS/GS.h"
76

87
#include "Config.h"
@@ -54,16 +53,18 @@ bool D3D12ShaderCache::CacheIndexKey::operator!=(const CacheIndexKey& key) const
5453
source_length != key.source_length);
5554
}
5655

57-
bool D3D12ShaderCache::Open(D3D_FEATURE_LEVEL feature_level, bool debug)
56+
bool D3D12ShaderCache::Open(D3D::ShaderModel shader_model, bool debug)
5857
{
59-
m_feature_level = feature_level;
58+
// Only support SM5.1 for now, which is the minimum for D3D12.
59+
pxAssert(shader_model >= D3D::ShaderModel::SM51);
60+
m_shader_model = shader_model;
6061
m_debug = debug;
6162

6263
bool result = true;
6364

6465
if (!GSConfig.DisableShaderCache)
6566
{
66-
const std::string base_shader_filename = GetCacheBaseFileName("shaders", feature_level, debug);
67+
const std::string base_shader_filename = GetCacheBaseFileName("shaders", m_shader_model, debug);
6768
const std::string shader_index_filename = base_shader_filename + ".idx";
6869
const std::string shader_blob_filename = base_shader_filename + ".bin";
6970

@@ -75,7 +76,7 @@ bool D3D12ShaderCache::Open(D3D_FEATURE_LEVEL feature_level, bool debug)
7576

7677
if (result)
7778
{
78-
const std::string base_pipelines_filename = GetCacheBaseFileName("pipelines", feature_level, debug);
79+
const std::string base_pipelines_filename = GetCacheBaseFileName("pipelines", m_shader_model, debug);
7980
const std::string pipelines_index_filename = base_pipelines_filename + ".idx";
8081
const std::string pipelines_blob_filename = base_pipelines_filename + ".bin";
8182

@@ -133,7 +134,7 @@ void D3D12ShaderCache::InvalidatePipelineCache()
133134
if (GSConfig.DisableShaderCache)
134135
return;
135136

136-
const std::string base_pipelines_filename = GetCacheBaseFileName("pipelines", m_feature_level, m_debug);
137+
const std::string base_pipelines_filename = GetCacheBaseFileName("pipelines", m_shader_model, m_debug);
137138
const std::string pipelines_index_filename = base_pipelines_filename + ".idx";
138139
const std::string pipelines_blob_filename = base_pipelines_filename + ".bin";
139140
CreateNew(pipelines_index_filename, pipelines_blob_filename, m_pipeline_index_file, m_pipeline_blob_file);
@@ -253,21 +254,12 @@ bool D3D12ShaderCache::ReadExisting(const std::string& index_filename, const std
253254
return true;
254255
}
255256

256-
std::string D3D12ShaderCache::GetCacheBaseFileName(const std::string_view type, D3D_FEATURE_LEVEL feature_level, bool debug)
257+
std::string D3D12ShaderCache::GetCacheBaseFileName(const std::string_view type, D3D::ShaderModel shader_model, bool debug)
257258
{
258259
std::string base_filename = "d3d12_";
259260
base_filename += type;
260261
base_filename += "_";
261-
262-
switch (feature_level)
263-
{
264-
case D3D_FEATURE_LEVEL_11_0:
265-
base_filename += "sm50";
266-
break;
267-
default:
268-
base_filename += "unk";
269-
break;
270-
}
262+
base_filename += D3D::ShaderModelToCacheString(shader_model);
271263

272264
if (debug)
273265
base_filename += "_debug";
@@ -497,15 +489,15 @@ D3D12ShaderCache::ComPtr<ID3DBlob> D3D12ShaderCache::CompileAndAddShaderBlob(
497489
{
498490
case EntryType::VertexShader:
499491
blob =
500-
D3D::CompileShader(D3D::ShaderType::Vertex, m_feature_level, m_debug, shader_code, macros, entry_point);
492+
D3D::CompileShader(D3D::ShaderType::Vertex, m_shader_model, m_debug, shader_code, macros, entry_point);
501493
break;
502494
case EntryType::PixelShader:
503495
blob =
504-
D3D::CompileShader(D3D::ShaderType::Pixel, m_feature_level, m_debug, shader_code, macros, entry_point);
496+
D3D::CompileShader(D3D::ShaderType::Pixel, m_shader_model, m_debug, shader_code, macros, entry_point);
505497
break;
506498
case EntryType::ComputeShader:
507-
blob = D3D::CompileShader(
508-
D3D::ShaderType::Compute, m_feature_level, m_debug, shader_code, macros, entry_point);
499+
blob =
500+
D3D::CompileShader(D3D::ShaderType::Compute, m_shader_model, m_debug, shader_code, macros, entry_point);
509501
break;
510502
default:
511503
break;

pcsx2/GS/Renderers/DX12/D3D12ShaderCache.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: GPL-3.0+
33

44
#pragma once
5+
#include "GS/Renderers/DX11/D3D.h"
56

67
#include "common/Pcsx2Defs.h"
78
#include "common/HashCombine.h"
@@ -32,10 +33,9 @@ class D3D12ShaderCache
3233
D3D12ShaderCache();
3334
~D3D12ShaderCache();
3435

35-
__fi D3D_FEATURE_LEVEL GetFeatureLevel() const { return m_feature_level; }
3636
__fi bool UsingDebugShaders() const { return m_debug; }
3737

38-
bool Open(D3D_FEATURE_LEVEL feature_level, bool debug);
38+
bool Open(D3D::ShaderModel shader_model, bool debug);
3939
void Close();
4040

4141
__fi ComPtr<ID3DBlob> GetVertexShader(
@@ -95,7 +95,7 @@ class D3D12ShaderCache
9595

9696
using CacheIndex = std::unordered_map<CacheIndexKey, CacheIndexData, CacheIndexEntryHasher>;
9797

98-
static std::string GetCacheBaseFileName(const std::string_view type, D3D_FEATURE_LEVEL feature_level, bool debug);
98+
static std::string GetCacheBaseFileName(const std::string_view type, D3D::ShaderModel shader_model, bool debug);
9999
static CacheIndexKey GetShaderCacheKey(
100100
EntryType type, const std::string_view shader_code, const D3D_SHADER_MACRO* macros, const char* entry_point);
101101
static CacheIndexKey GetPipelineCacheKey(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& gpdesc);
@@ -123,6 +123,6 @@ class D3D12ShaderCache
123123
std::FILE* m_pipeline_blob_file = nullptr;
124124
CacheIndex m_pipeline_index;
125125

126-
D3D_FEATURE_LEVEL m_feature_level = D3D_FEATURE_LEVEL_11_0;
126+
D3D::ShaderModel m_shader_model = D3D::ShaderModel::SM51;
127127
bool m_debug = false;
128128
};

pcsx2/GS/Renderers/DX12/GSDevice12.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ bool GSDevice12::Create(GSVSyncMode vsync_mode, bool allow_present_throttle)
885885
m_tfx_source = std::move(*shader);
886886
}
887887

888-
if (!m_shader_cache.Open(m_feature_level, GSConfig.UseDebugDevice))
888+
if (!m_shader_cache.Open(D3D::ShaderModel::SM51, GSConfig.UseDebugDevice))
889889
Console.Warning("D3D12: Shader cache failed to open.");
890890

891891
if (!CreateRootSignatures())

0 commit comments

Comments
 (0)