Skip to content

Commit

Permalink
Implement user-specified pixel shaders, redux (#8565)
Browse files Browse the repository at this point in the history
Co-authored-by: mrange <marten_range@hotmail.com>

I loved the pixel shaders in #7058, but that PR needed a bit of polish
to be ready for ingestion. This PR is almost _exactly_ that PR, with
some small changes.

* It adds a new pre-profile setting `"experimental.pixelShaderPath"`,
  which lets the user set a pixel shader to use with the Terminal.
    - CHANGED FROM #7058: It does _not_ add any built-in shaders.
    - CHANGED FROM #7058: it will _override_
      `experimental.retroTerminalEffect`
* It adds a bunch of sample shaders in `samples/shaders`. Included: 
    - A NOP shader as a base to build from.
    - An "invert" shader that inverts the colors, as a simple example
    - An "grayscale" shader that converts all colors to grayscale, as a
      simple example
    - An "raster bars" shader that draws some colored bars on the screen
      with a drop shadow, as a more involved example
    - The original retro terminal effects, as a more involved example
    - It also includes a broken shader, as an example of what heppens
      when the shader fails to compile
    - CHANGED FROM #7058: It does _not_ add the "retroII" shader we were
      all worried about.
* When a shader fails to be found or fails to compile, we'll display an
  error dialog to the user with a relevant error message.
    - CHANGED FROM #7058: Originally, #7058 would display "error bars"
      on the screen. I've removed that, and had the Terminal disable the
      shader entirely then.
* Renames the `toggleRetroEffect` action to `toggleShaderEffect`.
  (`toggleRetroEffect` is now an alias to `toggleShaderEffect`). This
  action will turn the shader OR the retro effects on/off. 

`toggleShaderEffect` works the way you'd expect it to, but the mental
math on _how_ is a little weird. The logic is basically:

```
useShader = shaderEffectsEnabled ? 
                (pixelShaderProvided ? 
                    pixelShader : 
                    (retroEffectEnabled ? 
                        retroEffect : null
                    )
                ) : 
                null
```

and `toggleShaderEffect` toggles `shaderEffectsEnabled`.

* If you've got both a shader and retro enabled, `toggleShaderEffect`
  will toggle between the shader on/off.
* If you've got a shader and retro disabled, `toggleShaderEffect` will
  toggle between the shader on/off.

References #6191
References #7058

Closes #7013

Closes #3930 "Add setting to retro terminal shader to control blur
radius, color" 
Closes #3929 "Add setting to retro terminal shader to enable drawing
scanlines" 
     - At this point, just roll your own version of the shader.
  • Loading branch information
zadjii-msft authored Dec 15, 2020
1 parent e943785 commit b140299
Show file tree
Hide file tree
Showing 36 changed files with 843 additions and 116 deletions.
6 changes: 5 additions & 1 deletion .github/actions/spell-check/dictionary/apis.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ cmdletbinding
COLORPROPERTY
CXICON
CYICON
D2DERR_SHADER_COMPILE_FAILED
DERR
environstrings
EXPCMDFLAGS
EXPCMDSTATE
frac
fullkbd
futex
GETDESKWALLPAPER
Expand All @@ -34,7 +37,6 @@ IObject
IStorage
ITab
ITaskbar
llabs
LCID
llabs
localtime
Expand Down Expand Up @@ -64,6 +66,7 @@ semver
serializer
shobjidl
SIZENS
smoothstep
GETDESKWALLPAPER
snprintf
spsc
Expand All @@ -83,6 +86,7 @@ UPDATEINIFILE
userenv
wcsstr
wcstoui
wpc
wsregex
XDocument
XElement
Expand Down
9 changes: 8 additions & 1 deletion .github/actions/spell-check/dictionary/names.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ ekg
ethanschoonover
Firefox
Gatta
glsl
Grie
Griese
Hernan
Howett
Illhardt
iquilezles
jantari
jerrysh
Kaiyu
kimwalisch
KMehrain
KODELIFE
Kodelife
Kourosh
kowalczyk
leonmsft
Expand All @@ -30,6 +34,7 @@ lukesampson
Manandhar
mbadolato
Mehrain
menger
mgravell
michaelniksa
michkap
Expand All @@ -43,6 +48,7 @@ nvaccess
nvda
oising
oldnewthing
opengl
osgwiki
paulcam
pauldotknopf
Expand All @@ -51,6 +57,7 @@ Pham
Rincewind
rprichard
Schoonover
shadertoy
Somuah
sonph
sonpham
Expand All @@ -60,8 +67,8 @@ Wirt
Wojciech
zadjii
Zamor
Zamora
zamora
Zamora
Zoey
zorio
Zverovich
27 changes: 26 additions & 1 deletion .github/actions/spell-check/expect/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ ADDREF
addressof
ADDSTRING
ADDTOOL
AEnd
aef
AEnd
AFew
AFill
AFX
Expand Down Expand Up @@ -628,6 +628,7 @@ doskey
dotnet
doubleclick
downlevel
DOWNSCALE
dpg
dpi
DPIAPI
Expand Down Expand Up @@ -832,6 +833,7 @@ fuzzwrapper
fwdecl
fwe
fwlink
GAUSSIAN
gb
gci
gcx
Expand Down Expand Up @@ -909,6 +911,7 @@ Goldmine
gonce
Google
goutput
GPUs
Gravell's
grayscale
GREENSCROLL
Expand Down Expand Up @@ -1109,6 +1112,7 @@ INTERCEPTCOPYPASTE
INTERNALNAME
interop
interoperability
intersectors
inthread
intptr
intsafe
Expand Down Expand Up @@ -1207,6 +1211,7 @@ KJ
KLF
KLMNOPQRST
KLMNOPQRSTQQQQQ
Kode
KU
KVM
KX
Expand Down Expand Up @@ -1591,6 +1596,7 @@ OACR
oauth
objbase
ocf
ocolor
odl
oem
oemcp
Expand Down Expand Up @@ -1789,6 +1795,7 @@ pragma
prc
prealigned
prebuilt
precendence
precomp
prect
prefast
Expand Down Expand Up @@ -1834,6 +1841,7 @@ pshn
PSHNOTIFY
PSHORT
pshpack
psin
PSINGLE
psl
psldl
Expand Down Expand Up @@ -1882,10 +1890,12 @@ QWER
qzmp
RAII
RALT
rasterbar
rasterfont
rasterization
rawinput
RAWPATH
raytracers
razzlerc
rbar
rbegin
Expand Down Expand Up @@ -1956,9 +1966,11 @@ RESETCONTENT
resheader
resizable
resmimetype
reso
restrictedcapabilities
resw
resx
RETROII
retval
rfa
rfc
Expand Down Expand Up @@ -2029,11 +2041,14 @@ SBCSDBCS
sbi
sbiex
sbold
sbri
scanbri
scancode
scanline
schemename
SCL
scm
scol
scprintf
SCRBUF
SCRBUFSIZE
Expand Down Expand Up @@ -2167,6 +2182,9 @@ SOURCESDIRECTORY
SPACEBAR
spammy
spand
spe
sph
spherefunctions
splashscreen
sprintf
sqlproj
Expand Down Expand Up @@ -2296,6 +2314,7 @@ tellp
telnet
telnetd
templated
teraflop
terminalcore
TERMINALSCROLLING
terminfo
Expand Down Expand Up @@ -2544,6 +2563,7 @@ vga
vgaoem
viewkind
viewports
Viginetting
Virt
VIRTTERM
Virtualizing
Expand Down Expand Up @@ -2760,6 +2780,7 @@ wwaproj
WWith
wx
wxh
wz
xa
xact
xamarin
Expand Down Expand Up @@ -2809,7 +2830,10 @@ xutr
xvalue
XVIRTUALSCREEN
XWalk
XWV
xy
xyw
Xzn
yact
YAML
YCast
Expand All @@ -2825,6 +2849,7 @@ YVIRTUALSCREEN
Yw
YWalk
yx
yzx
Zc
ZCmd
ZCtrl
Expand Down
6 changes: 5 additions & 1 deletion doc/cascadia/profiles.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"toggleFocusMode",
"toggleFullscreen",
"togglePaneZoom",
"toggleRetroEffect",
"toggleShaderEffects",
"wt",
"unbound"
],
Expand Down Expand Up @@ -936,6 +936,10 @@
"description": "When set to true, enable retro terminal effects. This is an experimental feature, and its continued existence is not guaranteed.",
"type": "boolean"
},
"experimental.pixelShaderPath": {
"description": "Use to set a path to a pixel shader to use with the Terminal. Overrides `experimental.retroTerminalEffect`. This is an experimental feature, and its continued existence is not guaranteed.",
"type": "string"
},
"fontFace": {
"default": "Cascadia Mono",
"description": "Name of the font face used in the profile.",
Expand Down
18 changes: 18 additions & 0 deletions samples/PixelShaders/Broken.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Broken, can be used for explorative testing of pixel shader error handling
Texture2D shaderTexture;
SamplerState samplerState;

cbuffer PixelShaderSettings {
float Time;
float Scale;
float2 Resolution;
float4 Background;
};

float4 main(float4 pos : SV_POSITION, float2 tex : TEXCOORD) : SV_TARGET
{
// OOPS; vec4 is not a hlsl but a glsl datatype!
vec4 color = shaderTexture.Sample(samplerState, tex);

return color;
}
18 changes: 18 additions & 0 deletions samples/PixelShaders/Error.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Shader used to indicate something went wrong during shader loading
Texture2D shaderTexture;
SamplerState samplerState;

cbuffer PixelShaderSettings {
float Time;
float Scale;
float2 Resolution;
float4 Background;
};

float4 main(float4 pos : SV_POSITION, float2 tex : TEXCOORD) : SV_TARGET
{
float4 color = shaderTexture.Sample(samplerState, tex);
float bars = 0.5+0.5*sin(tex.y*100);
color.x += pow(bars, 20.0);
return color;
}
32 changes: 32 additions & 0 deletions samples/PixelShaders/Grayscale.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// A minimal pixel shader that inverts the colors

// The terminal graphics as a texture
Texture2D shaderTexture;
SamplerState samplerState;

// Terminal settings such as the resolution of the texture
cbuffer PixelShaderSettings {
// Time since pixel shader was enabled
float Time;
// UI Scale
float Scale;
// Resolution of the shaderTexture
float2 Resolution;
// Background color as rgba
float4 Background;
};

// A pixel shader is a program that given a texture coordinate (tex) produces a color
// Just ignore the pos parameter
float4 main(float4 pos : SV_POSITION, float2 tex : TEXCOORD) : SV_TARGET
{
// Read the color value at the current texture coordinate (tex)
// float4 is tuple of 4 floats, rgba
float4 color = shaderTexture.Sample(samplerState, tex);
float avg = (color.x + color.y + color.z) / 3.0;
// Inverts the rgb values (xyz) but don't touch the alpha (w)
color.xyz = avg;

// Return the final color
return color;
}
32 changes: 32 additions & 0 deletions samples/PixelShaders/Invert.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// A minimal pixel shader that inverts the colors

// The terminal graphics as a texture
Texture2D shaderTexture;
SamplerState samplerState;

// Terminal settings such as the resolution of the texture
cbuffer PixelShaderSettings {
// Time since pixel shader was enabled
float Time;
// UI Scale
float Scale;
// Resolution of the shaderTexture
float2 Resolution;
// Background color as rgba
float4 Background;
};

// A pixel shader is a program that given a texture coordinate (tex) produces a color
// Just ignore the pos parameter
float4 main(float4 pos : SV_POSITION, float2 tex : TEXCOORD) : SV_TARGET
{
// Read the color value at the current texture coordinate (tex)
// float4 is tuple of 4 floats, rgba
float4 color = shaderTexture.Sample(samplerState, tex);

// Inverts the rgb values (xyz) but don't touch the alpha (w)
color.xyz = 1.0 - color.xyz;

// Return the final color
return color;
}
Loading

0 comments on commit b140299

Please sign in to comment.