Skip to content

Commit

Permalink
DxEngine: Fix shader compilation on pre-D3D11 hardware (#12677)
Browse files Browse the repository at this point in the history
* Drop engine support for DirectX 9.1
  Practically no one has such old hardware anymore and AtlasEngine additionally
  drops support for 10.0. The fallback also didn't work properly,
  because the `FeatureLevels` array failed to include 9.2 and 9.3.
  We'll simply fall back to WARP on all such devices.
* Optimize shaders during compilation
  The two new flags increase shader performance sometimes significantly.
* Fix shader feature level flags
  D3D feature level 10.0 only support 4.0 and 10.1 only 4.1 shaders.

## PR Checklist
* [x] Closes #12655
* [x] I work here
* [x] Tests added/passed

## Validation Steps Performed
* Add `WindowsTerminal.exe` in `dxcpl.exe`
* Add a basic `experimental.pixelShaderPath`
* All forced feature levels between `9_1` and `11_1` render as expected ✅

(cherry picked from commit d5fb736)
  • Loading branch information
lhecker authored and DHowett committed Mar 15, 2022
1 parent c903c0e commit 8aeb525
Showing 1 changed file with 33 additions and 18 deletions.
51 changes: 33 additions & 18 deletions src/renderer/dx/DxRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,7 @@ DxEngine::~DxEngine()
// - entry - Entry function of shader
// Return Value:
// - Compiled binary. Errors are thrown and logged.
inline Microsoft::WRL::ComPtr<ID3DBlob>
_CompileShader(
std::string source,
std::string target,
std::string entry = "main")
static Microsoft::WRL::ComPtr<ID3DBlob> _CompileShader(const std::string_view& source, const char* target)
{
#if !TIL_FEATURE_DXENGINESHADERSUPPORT_ENABLED
THROW_HR(E_UNEXPECTED);
Expand All @@ -202,24 +198,24 @@ _CompileShader(
Microsoft::WRL::ComPtr<ID3DBlob> error{};

const HRESULT hr = D3DCompile(
source.c_str(),
source.data(),
source.size(),
nullptr,
nullptr,
nullptr,
entry.c_str(),
target.c_str(),
0,
"main",
target,
D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR | D3DCOMPILE_OPTIMIZATION_LEVEL3,
0,
&code,
&error);

if (FAILED(hr))
{
LOG_HR_MSG(hr, "D3DCompile failed with %x.", static_cast<int>(hr));
LOG_HR_MSG(hr, "D3DCompile failed with %08x", hr);
if (error)
{
LOG_HR_MSG(hr, "D3DCompile error\n%*S", static_cast<int>(error->GetBufferSize()), static_cast<PWCHAR>(error->GetBufferPointer()));
LOG_HR_MSG(hr, "D3DCompile error\n%S", static_cast<const char*>(error->GetBufferPointer()));
}

THROW_HR(hr);
Expand Down Expand Up @@ -351,15 +347,33 @@ HRESULT DxEngine::_SetupTerminalEffects()
vp.TopLeftY = 0;
_d3dDeviceContext->RSSetViewports(1, &vp);

const char* shaderTargetVS = nullptr;
const char* shaderTargetPS = nullptr;
switch (_d3dDevice->GetFeatureLevel())
{
case D3D_FEATURE_LEVEL_10_0:
shaderTargetVS = "vs_4_0";
shaderTargetPS = "ps_4_0";
break;
case D3D_FEATURE_LEVEL_10_1:
shaderTargetVS = "vs_4_1";
shaderTargetPS = "ps_4_1";
break;
default:
shaderTargetVS = "vs_5_0";
shaderTargetPS = "ps_5_0";
break;
}

// Prepare shaders.
auto vertexBlob = _CompileShader(screenVertexShaderString, "vs_5_0");
auto vertexBlob = _CompileShader(&screenVertexShaderString[0], shaderTargetVS);
Microsoft::WRL::ComPtr<ID3DBlob> pixelBlob;
// As the pixel shader source is user provided it's possible there's a problem with it
// so load it inside a try catch, on any error log and fallback on the error pixel shader
// If even the error pixel shader fails to load rely on standard exception handling
try
{
pixelBlob = _CompileShader(pixelShaderSource, "ps_5_0");
pixelBlob = _CompileShader(pixelShaderSource, shaderTargetPS);
}
catch (...)
{
Expand Down Expand Up @@ -542,11 +556,12 @@ try
// D3D11_CREATE_DEVICE_DEBUG |
D3D11_CREATE_DEVICE_SINGLETHREADED;

const std::array<D3D_FEATURE_LEVEL, 5> FeatureLevels{ D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_1 };
static constexpr std::array FeatureLevels{
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
};

// Trying hardware first for maximum performance, then trying WARP (software) renderer second
// in case we're running inside a downlevel VM where hardware passthrough isn't enabled like
Expand Down

1 comment on commit 8aeb525

@github-actions
Copy link

@github-actions github-actions bot commented on 8aeb525 Mar 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@check-spelling-bot Report

Unrecognized words, please review:

  • hwinsta
  • Lsa
  • lsass
  • ofstream
  • tmpdir
  • UOI
  • USEROBJECTFLAGS
  • winsta
  • winstamin
  • WSF
Previously acknowledged words that are now absent adaa carlos coffgroup coffgrp datetime DCompile deconstructed devicefamily dpg eae emplate GENPROFILE GTR guardxfg HHmm Hostx installationpath MMdd MSDL pgorepro pgort PGU redistributable sid SPACEBAR Timeline timelines Unregister UWA UWAs vcvarsall xfg xIcon yIcon zamora
To accept these unrecognized words as correct (and remove the previously acknowledged and now absent words), run the following commands

... in a clone of the [email protected]:microsoft/terminal.git repository
on the release-1.12 branch:

update_files() {
perl -e '
my @expect_files=qw('".github/actions/spelling/expect/alphabet.txt
.github/actions/spelling/expect/expect.txt
.github/actions/spelling/expect/web.txt"');
@ARGV=@expect_files;
my @stale=qw('"$patch_remove"');
my $re=join "|", @stale;
my $suffix=".".time();
my $previous="";
sub maybe_unlink { unlink($_[0]) if $_[0]; }
while (<>) {
if ($ARGV ne $old_argv) { maybe_unlink($previous); $previous="$ARGV$suffix"; rename($ARGV, $previous); open(ARGV_OUT, ">$ARGV"); select(ARGV_OUT); $old_argv = $ARGV; }
next if /^(?:$re)(?:(?:\r|\n)*$| .*)/; print;
}; maybe_unlink($previous);'
perl -e '
my $new_expect_file=".github/actions/spelling/expect/8aeb525d73cb5d6d347695fe4ef195c5af724588.txt";
use File::Path qw(make_path);
use File::Basename qw(dirname);
make_path (dirname($new_expect_file));
open FILE, q{<}, $new_expect_file; chomp(my @words = <FILE>); close FILE;
my @add=qw('"$patch_add"');
my %items; @items{@words} = @words x (1); @items{@add} = @add x (1);
@words = sort {lc($a)."-".$a cmp lc($b)."-".$b} keys %items;
open FILE, q{>}, $new_expect_file; for my $word (@words) { print FILE "$word\n" if $word =~ /\w/; };
close FILE;
system("git", "add", $new_expect_file);
'
}

comment_json=$(mktemp)
curl -L -s -S \
  --header "Content-Type: application/json" \
  "https://api.github.com/repos/microsoft/terminal/comments/68764245" > "$comment_json"
comment_body=$(mktemp)
jq -r .body < "$comment_json" > $comment_body
rm $comment_json

patch_remove=$(perl -ne 'next unless s{^</summary>(.*)</details>$}{$1}; print' < "$comment_body")
  

patch_add=$(perl -e '$/=undef;
$_=<>;
s{<details>.*}{}s;
s{^#.*}{};
s{\n##.*}{};
s{(?:^|\n)\s*\*}{}g;
s{\s+}{ }g;
print' < "$comment_body")
  
update_files
rm $comment_body
git add -u
✏️ Contributor please read this

By default the command suggestion will generate a file named based on your commit. That's generally ok as long as you add the file to your commit. Someone can reorganize it later.

⚠️ The command is written for posix shells. You can copy the contents of each perl command excluding the outer ' marks and dropping any '"/"' quotation mark pairs into a file and then run perl file.pl from the root of the repository to run the code. Alternatively, you can manually insert the items...

If the listed items are:

  • ... misspelled, then please correct them instead of using the command.
  • ... names, please add them to .github/actions/spelling/allow/names.txt.
  • ... APIs, you can add them to a file in .github/actions/spelling/allow/.
  • ... just things you're using, please add them to an appropriate file in .github/actions/spelling/expect/.
  • ... tokens you only need in one place and shouldn't generally be used, you can add an item in an appropriate file in .github/actions/spelling/patterns/.

See the README.md in each directory for more information.

🔬 You can test your commits without appending to a PR by creating a new branch with that extra change and pushing it to your fork. The check-spelling action will run in response to your push -- it doesn't require an open pull request. By using such a branch, you can limit the number of typos your peers see you make. 😉

🗜️ If you see a bunch of garbage

If it relates to a ...

well-formed pattern

See if there's a pattern that would match it.

If not, try writing one and adding it to a patterns/{file}.txt.

Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

Note that patterns can't match multiline strings.

binary-ish string

Please add a file path to the excludes.txt file instead of just accepting the garbage.

File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

Please sign in to comment.