Implement GLSL writer backend.

This is a modified version of the HLSL writer.
Basic types, arrays, entry points, reserved keywords, uniforms,
builtin uniforms, structs, some builtin functions, zero initialization
are implemented. Textures, SSBOs and storage textures in particular are
unimplemented. All the unit tests "pass", but the output is not correct
in many cases.

triangle.wgsl outputs correct vertex and fragment shaders that pass
GLSL validation via glslang. compute_boids.wgsl outputs a valid but not
correct compute shader.

Change-Id: I96c7aaf60cf2d4237e45d732e5f51b345aea0552
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/57780
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Stephen White <senorblanco@chromium.org>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
Stephen White
2021-10-06 18:55:10 +00:00
committed by Tint LUCI CQ
parent 08146e2300
commit a9f8c7db81
46 changed files with 10570 additions and 0 deletions

View File

@@ -54,6 +54,7 @@ enum class Format {
kWgsl,
kMsl,
kHlsl,
kGlsl,
};
struct Options {
@@ -142,6 +143,11 @@ Format parse_format(const std::string& fmt) {
return Format::kHlsl;
#endif // TINT_BUILD_HLSL_WRITER
#if TINT_BUILD_GLSL_WRITER
if (fmt == "glsl")
return Format::kGlsl;
#endif // TINT_BUILD_GLSL_WRITER
return Format::kNone;
}
@@ -844,6 +850,33 @@ bool GenerateHlsl(const tint::Program* program, const Options& options) {
#endif // TINT_BUILD_HLSL_WRITER
}
/// Generate GLSL code for a program.
/// @param program the program to generate
/// @param options the options that Tint was invoked with
/// @returns true on success
bool GenerateGlsl(const tint::Program* program, const Options& options) {
#if TINT_BUILD_GLSL_WRITER
tint::writer::glsl::Options gen_options;
auto result = tint::writer::glsl::Generate(program, gen_options);
if (!result.success) {
PrintWGSL(std::cerr, *program);
std::cerr << "Failed to generate: " << result.error << std::endl;
return false;
}
if (!WriteFile(options.output_file, "w", result.glsl)) {
return false;
}
// TODO(senorblanco): implement GLSL validation
return true;
#else
std::cerr << "GLSL writer not enabled in tint build" << std::endl;
return false;
#endif // TINT_BUILD_GLSL_WRITER
}
} // namespace
int main(int argc, const char** argv) {
@@ -996,6 +1029,14 @@ int main(int argc, const char** argv) {
#endif // TINT_BUILD_MSL_WRITER
break;
}
#if TINT_BUILD_GLSL_WRITER
case Format::kGlsl: {
transform_inputs.Add<tint::transform::Renamer::Config>(
tint::transform::Renamer::Target::kGlslKeywords);
transform_manager.Add<tint::transform::Renamer>();
break;
}
#endif // TINT_BUILD_GLSL_WRITER
case Format::kHlsl: {
#if TINT_BUILD_HLSL_WRITER
transform_inputs.Add<tint::transform::Renamer::Config>(
@@ -1066,6 +1107,9 @@ int main(int argc, const char** argv) {
case Format::kHlsl:
success = GenerateHlsl(program.get(), options);
break;
case Format::kGlsl:
success = GenerateGlsl(program.get(), options);
break;
default:
std::cerr << "Unknown output format specified" << std::endl;
return 1;