Giter Club home page Giter Club logo

xshadercompiler's Introduction

XShaderCompiler ("Cross Shader Compiler")

Features

  • Cross (or trans-) 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
  • Meaningful report output
  • Commentary preserving
  • Written in C++11

License

3-Clause BSD License

Documentation

Status

Version: 0.02 Alpha (Do not use in production code!)

TODO List:
  • Common HLSL IO semantics to GLSL transformation.
  • Geometry and Tessellation semantics.
  • 'interface' and 'class' declarations.

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

#include <Xsc/Xsc.h>
#include <fstream>

/* ... */

auto inputStream = std::make_shared<std::ifstream>("Example.hlsl");
std::ofstream outputStream("Example.vertex.glsl");

Xsc::ShaderInput inputDesc;
inputDesc.sourceCode     = inputStream;
inputDesc.shaderVersion  = Xsc::InputShaderVersion::HLSL5;
inputDesc.entryPoint     = "VS";
inputDesc.shaderTarget   = Xsc::ShaderTarget::VertexShader;

Xsc::ShaderOutput outputDesc;
outputDesc.sourceCode    = &outputStream;
outputDesc.shaderVersion = Xsc::OutputShaderVersion::GLSL330;

// Translate HLSL code into GLSL
Xsc::StdLog log;
bool result = Xsc::CompileShader(inputDesc, outputDesc, &log);

Output Example

Meaningful output messages with line marker:

docu/screenshot_03.png

A few thoughts on translating HLSL

Although HLSL lacks lots of features commonly seen in general purpose programming languages like C++ and Java, HLSL is a very complex language, in both syntax and context! The XShaderCompiler has to be prepared for a lot of weird corner cases, especially syntactically. Take a look at the following example of an unnecessarily complex expression:

float f = ((vector<float, (1+4)/5+3>)1).w;

The XShaderCompiler is able to translate this to the follwing GLSL code:

float f = (vec4(1)).w;

Many other features like structure inheritance (which does not seem to be documented in the HLSL manual pages) must be translated to other constructs in GLSL, because GLSL is a more simpler language -- which pleases the compiler builder ;-).

Besides parsing a complex syntax, the XShaderCompiler tries to produce pretty output code which you'll love to maintain, in contrast to most auto-generated code. Consider the following simple HLSL vertex shader:

struct VertexIn
{
    float4 position : POSITION;
    float3 normal : NORMAL;
};

struct VertexOut
{
    float4 position : SV_Position;
    float3 normal : NORMAL;
};

VertexOut VertexMain(VertexIn inp)
{
    VertexOut outp;
    outp.position = inp.position;
    outp.normal = inp.normal;
    return outp;
}

Many shader cross compilers wrap the HLSL entry point into the GLSL 'main' function like this:

#version 330

in vec4 position;
in vec3 normal;

out vec3 outp_normal;

struct VertexIn
{
    vec4 position;
    vec3 normal;
};

struct VertexOut
{
    vec4 position;
    vec3 normal;
};

VertexOut VertexMain(VertexIn inp)
{
    VertexOut outp;
    outp.position = inp.position;
    outp.normal = inp.normal;
    return outp;
}

void main()
{
    VertexIn inp;
    inp.position = position;
    inp.normal = normal;
    VertexOut outp = VertexMain(inp);
    gl_Position = outp.position;
    outp_normal = outp.normal;
}

The XShaderCompiler will automatically solve overlapping names and structures that are used as shader input or output, so that unnecessary function wrappers are not required. This is what XShaderCompiler makes out of this:

#version 330

in vec3 position;
in vec3 normal;

out VertexOut
{
    vec3 normal;
}
outp;

void main()
{
    gl_Position = position;
    outp.normal = normal;
}

xshadercompiler's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.