Skip to content

Commit 5bb06d3

Browse files
committed
Allow compiling HLSL SM 6.0 to DXBC via SPIRV roundtrip
1 parent b5fa7eb commit 5bb06d3

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/SDL_gpu_shadercross.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,46 @@ void *SDL_ShaderCross_CompileDXBCFromHLSL(
591591
const char *shaderProfile,
592592
size_t *size) // filled in with number of bytes of returned buffer
593593
{
594+
const char *originalHlslSource = hlslSource;
595+
596+
if (SDL_strstr(shaderProfile, "6_0")) {
597+
// Need to roundtrip to SM 5.0
598+
size_t spirv_size;
599+
void *spirv = SDL_ShaderCross_CompileSPIRVFromHLSL(
600+
hlslSource,
601+
entrypoint,
602+
shaderProfile,
603+
&spirv_size);
604+
605+
if (spirv == NULL) {
606+
return NULL;
607+
}
608+
609+
if (SDL_strstr(shaderProfile, "vs")) {
610+
shaderProfile = "vs_5_0";
611+
} else if (SDL_strstr(shaderProfile, "ps")) {
612+
shaderProfile = "ps_5_0";
613+
} else if (SDL_strstr(shaderProfile, "cs")) {
614+
shaderProfile = "cs_5_0";
615+
} else {
616+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s", "Invalid shader profile!");
617+
SDL_free(spirv);
618+
return NULL;
619+
}
620+
621+
hlslSource = SDL_ShaderCross_TranspileHLSLFromSPIRV(
622+
spirv,
623+
spirv_size,
624+
entrypoint,
625+
shaderProfile
626+
);
627+
SDL_free(spirv);
628+
629+
if (hlslSource == NULL) {
630+
return NULL;
631+
}
632+
}
633+
594634
ID3DBlob *blob = SDL_ShaderCross_INTERNAL_CompileDXBC(
595635
hlslSource,
596636
entrypoint,
@@ -606,6 +646,10 @@ void *SDL_ShaderCross_CompileDXBCFromHLSL(
606646
SDL_memcpy(buffer, blob->lpVtbl->GetBufferPointer(blob), *size);
607647
blob->lpVtbl->Release(blob);
608648

649+
if (originalHlslSource != hlslSource) {
650+
SDL_free((void *)hlslSource);
651+
}
652+
609653
return buffer;
610654
}
611655

0 commit comments

Comments
 (0)