https://github.com/LukasBanana/XShaderCompiler
Shader cross compiler to translate HLSL (Shader Model 4 and 5) to GLSL
https://github.com/LukasBanana/XShaderCompiler
c cpp cpp11 csharp glsl hlsl shader-cross-compiler
Last synced: 6 months ago
JSON representation
Shader cross compiler to translate HLSL (Shader Model 4 and 5) to GLSL
- Host: GitHub
- URL: https://github.com/LukasBanana/XShaderCompiler
- Owner: LukasBanana
- License: bsd-3-clause
- Created: 2014-10-09T18:56:52.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2019-09-07T03:42:42.000Z (about 6 years ago)
- Last Synced: 2025-04-06T19:11:34.481Z (7 months ago)
- Topics: c, cpp, cpp11, csharp, glsl, hlsl, shader-cross-compiler
- Language: C++
- Homepage:
- Size: 7.39 MB
- Stars: 358
- Watchers: 21
- Forks: 48
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- AwesomeCppGameDev - XShaderCompiler
README
[](https://ci.appveyor.com/project/LukasBanana/xshadercompiler)
# XShaderCompiler ("Cross Shader Compiler") #

Features
--------
* Cross compiles HLSL shader code (Shader Model 4 and 5) into GLSL
* Simple to integrate into other projects
* Low overhead translation (i.e. avoidance of unnecessary wrapper functions)
* Dead code removal
* Code reflection
* Meaningful report output
* Commentary preserving
* Written in C++11
Language Bindings
-----------------
* C Wrapper (ISO C99)
* C# Wrapper
License
-------
3-Clause BSD License
Documentation
-------------
* [Getting Started with XShaderCompiler](https://github.com/LukasBanana/XShaderCompiler/blob/master/docu/GettingStarted/Getting%20Started%20with%20XShaderCompiler.pdf) (PDF)
* [XShaderCompiler Reference Manual](https://github.com/LukasBanana/XShaderCompiler/blob/master/docu/refman.pdf) (PDF)
Progress
--------
**Version: 0.11 Alpha** (*Do not use in production code!*)
See the [TODO.md](https://github.com/LukasBanana/XShaderCompiler/blob/master/TODO.md) file for more information.
| Feature | Progress | Remarks |
|---------|:--------:|---------|
| Vertex Shader | ~80% | Few language features are still left |
| Tessellation Control Shader | ~20% | InputPatch and patch-constant-function translation in progress |
| Tessellation Evaluation Shader | ~20% | OutputPatch translation in progress |
| Geometry Shader | ~60% | Code generation is incomplete |
| Fragment Shader | ~80% | Few language features are still left |
| Compute Shader | ~80% | Few language features are still left |
Offline Compiler
----------------
The following command line translates the "Example.hlsl" file with the vertex shader entry point "VS", and the fragment shader entry point "PS":
```
xsc -E VS -T vert Example.hlsl -E PS -T frag Example.hlsl
```
The result are two GLSL shader files: "Example.VS.vert" and "Example.PS.frag".
Library Usage (C++)
-------------------
```cpp
#include
#include
#include
int main()
{
// Input file stream (use std::stringstream for in-code shaders).
auto inputStream = std::make_shared("Example.hlsl");
// Output file stream (GLSL vertex shader)
std::ofstream outputStream("Example.VS.vert");
// Fill the shader input descriptor structure
Xsc::ShaderInput inputDesc;
inputDesc.sourceCode = inputStream;
inputDesc.shaderVersion = Xsc::InputShaderVersion::HLSL5;
inputDesc.entryPoint = "VS";
inputDesc.shaderTarget = Xsc::ShaderTarget::VertexShader;
// Fill the shader output descriptor structure
// Use outputDesc.options, outputDesc.formatting, and outputDesc.nameMangling for more settings
Xsc::ShaderOutput outputDesc;
outputDesc.sourceCode = &outputStream;
// Optional output log (can also be a custom class)
Xsc::StdLog log;
// Optional shader reflection data (for shader code feedback)
Xsc::Reflection::ReflectionData reflectData;
// Translate HLSL code into GLSL
try
{
bool result = Xsc::CompileShader(inputDesc, outputDesc, &log, &reflectData);
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
```
Library Usage (C#)
------------------
```cs
using System;
using System.IO;
namespace Example
{
class Program
{
static void Main()
{
// Create instance of the XShaderCompiler
var compiler = new XscCompiler();
// Fill the shader input descriptor structure
var inputDesc = new XscCompiler.ShaderInput();
inputDesc.SourceCode = File.ReadAllText("Example.hlsl");
inputDesc.ShaderVersion = XscCompiler.InputShaderVersion.HLSL5;
inputDesc.EntryPoint = "VS";
inputDesc.Target = XscCompiler.ShaderTarget.VertexShader;
// Fill the shader output descriptor structure
// Use outputDesc.Options, outputDesc.Formatting, and outputDesc.MameMangling for more settings
var outputDesc = new XscCompiler.ShaderOutput();
// Optional output log (can also be a custom class)
var log = compiler.StandardLog;
// Optional shader reflection data (for shader code feedback)
var reflectData = new XscCompiler.ReflectionData();
// Translate HLSL code into GLSL
try
{
bool result = compiler.CompileShader(inputDesc, outputDesc, log, reflectData);
if (result)
{
// Write output shader code
File.WriteAllText("Example.VS.vert", outputDesc.SourceCode);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}
```
Real-time Debugger
------------------
A real-time debugger with UI is provided. Although this debugger is mainly used for developement of the compiler itself,
it can also be used for a quick translation overview, to see how language constructs are being cross compiled.
Example of the real-time debugger (requires wxWidgets 3.1.0 or later):

Language Differences
--------------------
Here is a brief outline of High-Level differences between HLSL and GLSL, which XShaderCompiler is able to translate properly:
| Feature | HLSL | GLSL |
|---------|:----:|:----:|
| Separation of Textures and Samplers | Yes | Only for Vulkan |
| Structure Inheritance | Yes | No |
| Nested Structures | Yes | No |
| Anonymous Structures | Yes | No |
| Structure Member Functions | Yes | No |
| Default Parameters | Yes | No |
| Object-Oriented Intrinsics | Yes | No |
| Multiple Entry Points | Yes | No |
| Type Aliasing | Yes | No |
| L-Values of Input Semantics | Yes | No |
| Implicit Type Conversion | Extensive | Restricted |