Giter VIP home page Giter VIP logo

hlslparser's Introduction

HLSLParser

This is a fork of Unknownworld's hlslparser adapted to our needs in The Witness. We currently use it to translate pseudo-HLSL shaders (using the legacy D3D9 syntax) to HLSL10 and Metal Shading Language (MSL). There's also a GLSL translator available that we do not use yet, but that is being maintained by community contributions.

The HLSL parser has been extended with many HLSL10 features, but retaining the original HLSL C-based syntax.

For example, the following functions in our HLSL dialect:

float tex2Dcmp(sampler2DShadow s, float3 texcoord_comparevalue);
float4 tex2DMSfetch(sampler2DMS s, int2 texcoord, int sample);
int2 tex2Dsize(sampler2D s);

Are equivalent to these methods in HLSL10:

float Texture2D::SampleCmp(SamplerComparisonState s, float2 texcoord, float comparevalue);
float4 Texture2DMS<float4>::Load(int2 texcoord, int sample);
void Texture2D<float4>::GetDimensions(out uint w, out uint h);

Here are the original release notes:

HLSL Parser and GLSL code generator

This is the code we used in Natural Selection 2 to convert HLSL shader code to GLSL for use with OpenGL. The code is pulled from a larger codebase and has some dependencies which have been replaced with stubs. These dependencies are all very basic (array classes, memory allocators, etc.) so replacing them with our own equivalent should be simple if you want to use this code.

The parser is designed to work with HLSL code written in the legacy Direct3D 9 style (e.g. D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY should be used with D3D11). The parser works with cbuffers for uniforms, so in addition to generating GLSL, there is a class provided for generating D3D9-compatible HLSL which doesn't support cbuffers. The GLSL code requires version 3.1 for support of uniform blocks. The parser is designed to catch all errors and generate "clean" GLSL which can then be compiled without any errors.

The HLSL parsing is done though a basic recursive descent parser coded by hand rather than using a parser generator. We believe makes the code easier to understand and work with.

To get consistent results from Direct3D and OpenGL, our engine renders in OpenGL "upside down". This is automatically added into the generated GLSL vertex shaders.

Although this code was written specifically for our use, we hope that it may be useful as an educational tool or a base for someone who wants to do something similar.

hlslparser's People

Contributors

zeux avatar castano avatar ruslankutdusov avatar maxmcguire avatar bonizz avatar dmartinek avatar magcius avatar pufonbarn avatar

Watchers

simongao 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.