Skip to content

Commit f0e5365

Browse files
committed
Refactor the shader compiler to use the new unsafe helpers
1 parent 2afaaf5 commit f0e5365

1 file changed

Lines changed: 49 additions & 23 deletions

File tree

sources/engine/Stride.Shaders.Compiler/Direct3D/ShaderCompiler.cs

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,30 +56,16 @@ public ShaderBytecodeResult Compile(string shaderSource, string entryPoint, Shad
5656
case 3: shaderFlags |= D3DCOMPILE_OPTIMIZATION_LEVEL3; break;
5757
}
5858

59-
var shaderBytes = Encoding.ASCII.GetBytes(shaderSource);
60-
var shaderModelBytes = Encoding.ASCII.GetBytes(shaderModel);
59+
var shaderBytes = shaderSource.GetAsciiSpan();
6160

6261
ID3D10Blob* byteCode = null;
6362
ID3D10Blob* compileErrors = null;
64-
65-
// Compile using D3DCompiler
66-
var d3dCompiler = D3DCompiler.GetApi();
67-
68-
HResult result = d3dCompiler.Compile(in shaderBytes[0], (nuint) shaderBytes.Length, pSourceName: nameof(shaderSource),
69-
pDefines: null, pInclude: null, entryPoint, shaderModel, shaderFlags, effectFlags,
70-
ref byteCode, ref compileErrors);
71-
7263
var byteCodeResult = new ShaderBytecodeResult();
7364

74-
if (result.IsFailure || byteCode == null)
75-
{
76-
// Log compilation errors
77-
if (compileErrors is not null)
78-
{
79-
byteCodeResult.Error(GetTextFromBlob(compileErrors));
80-
}
81-
}
82-
else
65+
HResult result = Compile(shaderSource, entryPoint, shaderModel, shaderFlags, effectFlags,
66+
&byteCode, &compileErrors, byteCodeResult);
67+
68+
if (result.IsSuccess && byteCode != null)
8369
{
8470
// TODO: Make this optional
8571
byteCodeResult.DisassembleText = Disassemble(byteCode);
@@ -106,15 +92,53 @@ public ShaderBytecodeResult Compile(string shaderSource, string entryPoint, Shad
10692

10793
return byteCodeResult;
10894

95+
/// <summary>
96+
/// Compiles shader source code to bytecode for the specified shader model.
97+
/// </summary>
98+
static HResult Compile(string shaderSource, string entryPoint, string shaderModel,
99+
uint shaderFlags, uint effectFlags,
100+
ID3D10Blob** bytecode, ID3D10Blob** errorMessages,
101+
ShaderBytecodeResult bytecodeResult)
102+
{
103+
HResult result;
104+
105+
var d3dCompiler = D3DCompiler.GetApi();
106+
107+
var shaderSourceSpan = shaderSource.GetUtf8Span();
108+
109+
fixed (sbyte* pShaderSource = shaderSourceSpan)
110+
fixed (sbyte* pEntryPoint = entryPoint.GetUtf8Span())
111+
fixed (sbyte* pShaderModel = shaderModel.GetUtf8Span())
112+
fixed (sbyte* pShaderSourceName = nameof(shaderSource).GetUtf8Span())
113+
{
114+
result = d3dCompiler.Compile(pShaderSource, (nuint) shaderSourceSpan.Length, (byte*) pShaderSourceName,
115+
pDefines: null, pInclude: null, (byte*) pEntryPoint, (byte*) pShaderModel, shaderFlags, effectFlags,
116+
bytecode, errorMessages);
117+
}
118+
119+
if (result.IsFailure || bytecode == null)
120+
{
121+
// Log compilation errors
122+
if (errorMessages is not null)
123+
{
124+
bytecodeResult.Error(GetTextFromBlob(*errorMessages));
125+
}
126+
}
127+
128+
return result;
129+
}
130+
109131
/// <summary>
110132
/// Disassembles a blob of shader bytecode to its textual equivalent in HLSL code.
111133
/// </summary>
112134
string Disassemble(ID3D10Blob* byteCode)
113135
{
114136
ID3D10Blob* disassembly = null;
115137

138+
var d3dCompiler = D3DCompiler.GetApi();
139+
116140
d3dCompiler.Disassemble(byteCode->GetBufferPointer(), byteCode->GetBufferSize(), Flags: 0,
117-
szComments: in Unsafe.NullRef<byte>(), ref disassembly);
141+
szComments: in NullRef<byte>(), ref disassembly);
118142

119143
string shaderDisassembly = GetTextFromBlob(disassembly);
120144
Free(disassembly);
@@ -130,8 +154,10 @@ string Disassemble(ID3D10Blob* byteCode)
130154

131155
const uint StripDebugAndReflection = (uint) (CompilerStripFlags.ReflectionData | CompilerStripFlags.DebugInfo);
132156

133-
HResult result = d3dCompiler.StripShader(byteCode->GetBufferPointer(), byteCode->GetBufferSize(), StripDebugAndReflection, ref strippedByteCode);
157+
var d3dCompiler = D3DCompiler.GetApi();
134158

159+
HResult result = d3dCompiler.StripShader(byteCode->GetBufferPointer(), byteCode->GetBufferSize(),
160+
StripDebugAndReflection, ref strippedByteCode);
135161
if (result.IsFailure)
136162
return null;
137163

@@ -144,8 +170,8 @@ string Disassemble(ID3D10Blob* byteCode)
144170
static string GetTextFromBlob(ID3D10Blob* blob)
145171
{
146172
return blob != null
147-
? SilkMarshal.PtrToString((nint) blob->GetBufferPointer())
148-
: string.Empty;
173+
? blob->Buffer.As<byte, sbyte>().GetString()
174+
: null;
149175
}
150176

151177
/// <summary>

0 commit comments

Comments
 (0)