-
Notifications
You must be signed in to change notification settings - Fork 1
/
Compile-Shader.ps1
190 lines (184 loc) · 6.11 KB
/
Compile-Shader.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<#
.SYNOPSIS
Compiles a single .hlsl file into its composite .cso compiled shader files.
.DESCRIPTION
Compile a single .hlsl file into its composite .cso compiled shader files. Certain pairs of
switches are required when any of them are used. At a minimum, a shader must
use -vs and -ps to be a valid renderable shader. To correctly use the
Tessellation stage both -hs and -ds must be used together. The geometry stage
is optional in all use cases and can be used by itself. The compute shader
stage is also optional and can be used by itself.
.PARAMETER Path
The path to the .hlsl script to compile.
.PARAMETER vs
Tells the script to compile as a Vertex Shader.
The entry point of the shader must be VertexFunction.
If used, -ps must also be supplied.
.PARAMETER ps
Tells the script to compile as a Pixel Shader.
The entry point of the shader must be PixelFunction.
If used, -vs must also be supplied.
.PARAMETER hs
Tells the script to compile as a Hull Shader.
The entry point of the shader must be HullFunction.
If used, -ds must also be supplied.
.PARAMETER ds
Tells the script to compile as a Domain Shader.
The entry point of the shader must be DomainFunction.
If used, -hs must also be supplied.
.PARAMETER gs
Tells the script to compile as a Geometry Shader.
The entry point of the shader must be GeometryFunction.
This switch is optional.
.PARAMETER cs
Tells the script to compile as a Compute Shader.
The entry point of the shader must be ComputeFunction.
This switch it optional and can appear by itself.
.PARAMETER d
Tells the script to compile in debug mode.
This enables validation and disables optimizations.
.INPUTS
None
.OUTPUTS
None
.EXAMPLE
PS> Compile-Shader.ps1 -Path "renderableshader.hlsl" -vs -ps
.EXAMPLE
PS> Compile-Shader.ps1 -Path "tesselatedshader.hlsl" -hs -ds
.EXAMPLE
PS> Compile-Shader.ps1 -Path "computeshader.hlsl" -cs
.EXAMPLE
PS> Compile-Shader.ps1 -Path "geometryshader.hlsl" -gs
.EXAMPLE
PS> Compile-Shader.ps1 -Path "renderabletesselatedshader.hlsl" -vs -ps -hs -ds
.EXAMPLE
PS> Compile-Shader.ps1 -Path "renderablegeometryshader.hlsl" -vs -ps -gs
.EXAMPLE
PS> Compile-Shader.ps1 -Path "renderablecomputeshader.hlsl" -vs -ps -cs
.EXAMPLE
PS> Compile-Shader.ps1 -Path "complexshader.hlsl" -vs -ps -hs -ds -gs -cs
#>
[CmdletBinding()]
param(
[Parameter( Mandatory )]
[ValidateNotNullOrEmpty()]
[System.IO.FileInfo]$Path,
[Parameter( Mandatory , ParameterSetName = 'VSPS')]
[Switch]$vs,
[Parameter( Mandatory , ParameterSetName = 'VSPS' )]
[Switch]$ps,
[Parameter( Mandatory , ParameterSetName = 'HSDS' )]
[Parameter( ParameterSetName = 'VSPS')]
[Switch]$hs,
[Parameter( Mandatory , ParameterSetName = 'HSDS' )]
[Parameter( ParameterSetName = 'VSPS')]
[Switch]$ds,
[Parameter( Mandatory , ParameterSetName = 'GS' )]
[Parameter( ParameterSetName = 'VSPS')]
[Parameter( ParameterSetName = 'HSDS')]
[Switch]$gs,
[Parameter( Mandatory , ParameterSetName = 'CS' )]
[Parameter( ParameterSetName = 'VSPS')]
[Parameter( ParameterSetName = 'HSDS')]
[Switch]$cs,
[Switch]$d
)
function Test-Args {
[CmdletBinding()]
[OutputType([bool])]
param(
[Parameter(Mandatory=$false)]
[bool]$vs,
[Parameter(Mandatory=$false)]
[bool]$hs,
[Parameter(Mandatory=$false)]
[bool]$ds,
[Parameter(Mandatory=$false)]
[bool]$gs,
[Parameter(Mandatory=$false)]
[bool]$ps,
[Parameter(Mandatory=$false)]
[bool]$cs
)
$nostages = $false -eq ($vs -or $hs -or $ds -or $gs -or $ps -or $cs)
if(!$nostages) {
$vsps = !(!$vs -or !$ps) -and !($vs -xor $ps)
$hsds = !(!$hs -or !$ds) -and !($hs -xor $ds)
$invalid = -not ($vsps -or $hsds -or $gs -or $cs)
if($invalid) {
Write-Output "Missing required arguments. VS and PS, HS and DS, or GS, or CS"
Return $false
}
Return $true
} else {
Write-Output "No shader stages specified."
Return $false
}
}
function Get-EntryPointForStage {
[CmdletBinding()]
[OutputType([String])]
param(
[Parameter(Mandatory)]
[string]$stageId
)
if($stageId -eq "vs") { Return "VertexFunction" }
if($stageId -eq "ps") { Return "PixelFunction" }
if($stageId -eq "hs") { Return "HullFunction" }
if($stageId -eq "ds") { Return "DomainFunction" }
if($stageId -eq "gs") { Return "GeometryFunction" }
if($stageId -eq "cs") { Return "ComputeFunction" }
Return $null
}
function Get-StagesFromArgs() {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[bool]$vs,
[Parameter(Mandatory)]
[bool]$hs,
[Parameter(Mandatory)]
[bool]$ds,
[Parameter(Mandatory)]
[bool]$gs,
[Parameter(Mandatory)]
[bool]$ps,
[Parameter(Mandatory)]
[bool]$cs
)
$array = @()
if($vs) { $array += "vs" }
if($hs) { $array += "hs" }
if($ds) { $array += "ds" }
if($gs) { $array += "gs" }
if($ps) { $array += "ps" }
if($cs) { $array += "cs" }
return $array
}
$validPath = Test-Path -Path $Path
if(!$validPath) {
Write-Output "Script: $Path does not exist."
Return;
}
$validArgs = Test-Args -vs $vs -hs $hs -ds $ds -gs $gs -ps $ps -cs $cs
if($validArgs) {
$file = (Get-Item $Path).BaseName #$Path.Substring(0, $Path.LastIndexOf('.'))
$ShaderArgs = Get-StagesFromArgs -vs $vs -hs $hs -ds $ds -gs $gs -ps $ps -cs $cs
if($d) {
Write-Output "Debug mode."
} else {
Write-Output "Release mode."
}
foreach($stage in $ShaderArgs) {
$CsoPath = $file + "_" + $stage.ToUpper() + ".cso"
$EntryPoint = Get-EntryPointForStage $stage
$target = $stage + "_5_0"
if($d) {
fxc.exe /T $target /Fo $CsoPath /E $EntryPoint /Zi /Od /WX /nologo /Zpc $Path
} else {
fxc.exe /T $target /Fo $CsoPath /E $EntryPoint /O3 /WX /Vd /nologo /Zpc $Path
}
}
} else {
Write-Output "Script terminated due to invalid arguments."
}