[skslc] Support HLSL output

- skslc can now generate HLSL using spirv-cross if invokved with an output
file with the ".hlsl" extension
- The SkSL::SPIRVtoHLSL function used to be conditionally compiled based
on the SK_USE_DIRECT3D define, which gates D3D support for the entire
gpu backend. This function is now conditionally compiled using a new,
more narrowly scoped define, which is always enabled when building
skslc.

Bug: skia:12691
Test: Run:
Change-Id: I6967c26ab28954ec09e1c62513187c8986135290
      1. ./skslc ../../resources/sksl/spirv/Ossfuzz35916.sksl TEST.hlsl
      2. ./dxc -T ps_6_7 TEST.hlsl
      Ensure that 1 runs without any errors and 2 does not detect any
      issues in the hlsl.
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/482257
Auto-Submit: Arman Uguray <armansito@google.com>
Reviewed-by: John Stiles <johnstiles@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
This commit is contained in:
Arman Uguray 2021-12-09 12:24:51 -08:00 committed by SkCQ
parent 57043197c1
commit b58bab1fed
5 changed files with 32 additions and 5 deletions

View File

@ -627,6 +627,7 @@ if (skia_compile_sksl_tests) {
defines = [
"SKSL_STANDALONE",
"SK_DISABLE_TRACING",
"SK_ENABLE_SPIRV_CROSS",
"SK_ENABLE_SPIRV_VALIDATION",
]
sources = [
@ -668,6 +669,7 @@ if (skia_compile_sksl_tests) {
":run_sksllex",
"//third_party/externals/spirv-tools:spvtools",
"//third_party/externals/spirv-tools:spvtools_val",
"//third_party/spirv-cross:spirv_cross",
]
}
@ -951,7 +953,10 @@ optional("gpu") {
}
if (skia_use_direct3d) {
public_defines += [ "SK_DIRECT3D" ]
public_defines += [
"SK_DIRECT3D",
"SK_ENABLE_SPIRV_CROSS",
]
deps += [
"//third_party/d3d12allocator",
"//third_party/spirv-cross:spirv_cross",

View File

@ -675,13 +675,28 @@ bool Compiler::toGLSL(Program& program, String* out) {
return result;
}
bool Compiler::toHLSL(Program& program, OutputStream& out) {
TRACE_EVENT0("skia.shaders", "SkSL::Compiler::toHLSL");
String hlsl;
if (!this->toHLSL(program, &hlsl)) {
return false;
}
out.writeString(hlsl);
return true;
}
bool Compiler::toHLSL(Program& program, String* out) {
String spirv;
if (!this->toSPIRV(program, &spirv)) {
return false;
}
return SPIRVtoHLSL(spirv, out);
if (!SPIRVtoHLSL(spirv, out)) {
fErrorText += "HLSL cross-compilation not enabled";
return false;
}
return true;
}
bool Compiler::toMetal(Program& program, OutputStream& out) {

View File

@ -152,6 +152,8 @@ public:
bool toGLSL(Program& program, String* out);
bool toHLSL(Program& program, OutputStream& out);
bool toHLSL(Program& program, String* out);
bool toMetal(Program& program, OutputStream& out);

View File

@ -394,6 +394,11 @@ ResultCode processCommand(std::vector<SkSL::String>& args) {
[](SkSL::Compiler& compiler, SkSL::Program& program, SkSL::OutputStream& out) {
return compiler.toMetal(program, out);
});
} else if (outputPath.ends_with(".hlsl")) {
return compileProgram(
[](SkSL::Compiler& compiler, SkSL::Program& program, SkSL::OutputStream& out) {
return compiler.toHLSL(program, out);
});
} else if (outputPath.ends_with(".skvm")) {
return compileProgramForSkVM(
[&](SkSL::Compiler&, SkSL::Program& program, SkSL::OutputStream& out) {
@ -505,8 +510,8 @@ ResultCode processCommand(std::vector<SkSL::String>& args) {
return ResultCode::kOutputError;
}
} else {
printf("expected output path to end with one of: .glsl, .metal, .spirv, .asm.frag, .skvm, "
".stage, .asm.vert (got '%s')\n", outputPath.c_str());
printf("expected output path to end with one of: .glsl, .metal, .hlsl, .spirv, .asm.frag, "
".skvm, .stage, .asm.vert, .dehydrated.sksl (got '%s')\n", outputPath.c_str());
return ResultCode::kConfigurationError;
}
return ResultCode::kSuccess;

View File

@ -7,7 +7,7 @@
#include "src/sksl/codegen/SkSLSPIRVtoHLSL.h"
#if defined(SK_DIRECT3D)
#if defined(SK_ENABLE_SPIRV_CROSS)
#include "third_party/externals/spirv-cross/spirv_hlsl.hpp"