- Notifications
You must be signed in to change notification settings - Fork91
Translates native shader languages into CrossGL universal shader language and vice versa.
License
CrossGL/crosstl
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The CrossTL is a core component of our platform, enabling the conversion of CrossGL shader code directly into various graphics APIs, such as DirectX, Metal, Vulkan, and OpenGL and vice-versa. This translator simplifies shader development by allowing a single, unified shader language to be used across multiple platforms.
In the ever-evolving world of graphics programming,CrossGL emerges as a solution to bridge the gap between diverse graphics APIs.
Imagine writing a shaderonce and deploying it across:
- 🍎Metal
- 🎮DirectX
- 🖥️OpenGL
- 🖥️Vulkan
- ⚙️Slang
- 🔥Mojo
...all without changing a single line of code!
- Metal
- DirectX
- OpenGL
- Slang
- ⏱️ Time-Saving: Slash development time by eliminating the need for multiple shader versions.
- 🛠️ Consistency: Ensure uniform behavior across all platforms.
- 🧠 Simplified Learning Curve: Master one language instead of many.
- 🔍 Enhanced Debugging: Develop universal tools for shader analysis.
- 🔮 Future-Proof: Easily adapt to new graphics APIs as they emerge.
The translator takes CrossGL shader code and processes it through several stages:
- Parsing: The code is parsed into an abstract syntax tree (AST).
- Intermediate Representation: The AST is converted into an intermediate representation (IR) for optimization.
- Code Generation: The IR is translated into the target backend code.
- Optimization: Various optimization passes are applied to ensure maximum performance.
- Source Output: The final output is produced and ready for use.
CrossGL doesn't just translate from a universal language to platform-specific shaders - it also works in reverse! This powerful feature allows developers to convert existing shaders from various platforms into CrossGL.
shader main { vertex { input vec3 position; output vec2 vUV;voidmain() { vUV = position.xy *10.0; gl_Position =vec4(position,1.0); } }floatperlinNoise(vec2 p) {returnfract(sin(dot(p,vec2(12.9898,78.233))) *43758.5453); } fragment { input vec2 vUV; output vec4 fragColor;voidmain() {float noise =perlinNoise(vUV);float height = noise *10.0; vec3 color =vec3(height /10.0,1.0 - height /10.0,0.0); fragColor =vec4(color,1.0); } }}
First, install CrossGL's translation library using pip:
pip install crosstl
- Create a CrossGL shader file (e.g.,
shader.cgl
):
shader main { vertex { inputvec3 position; outputvec2 vUV;void main() { vUV= position.xy*10.0;gl_Position=vec4(position,1.0); } } fragment { inputvec2 vUV; outputvec4 fragColor;void main() { fragColor=vec4(vUV,0.0,1.0); } }}
- Translate to your desired backend:
importcrosstl# Translate to Metalmetal_code=crosstl.translate('shader.cgl',backend='metal',save_shader='shader.metal')# Translate to DirectX (HLSL)hlsl_code=crosstl.translate('shader.cgl',backend='directx',save_shader='shader.hlsl')# Translate to OpenGLopengl_code=crosstl.translate('shader.cgl',backend='opengl',save_shader='shader.glsl')
- write your HLSL shader (e.g.,
shader.hlsl
):
struct VS_INPUT {float3 position :POSITION;};struct PS_INPUT {float4 position :SV_POSITION;float2 uv :TEXCOORD0;};PS_INPUTVSMain(VS_INPUT input) { PS_INPUT output; output.position =float4(input.position,1.0); output.uv = input.position.xy *10.0;return output;}float4PSMain(PS_INPUT input) :SV_TARGET {returnfloat4(input.uv,0.0,1.0);}
- Convert to CrossGL:
importcrosstlcrossgl_code=crosstl.translate('shader.hlsl',backend='cgl',save_shader='shader.cgl')print(crossgl_code)
- write your Metal shader (e.g.,
shader.metal
):
#include<metal_stdlib>usingnamespacemetal;structVertexInput { float3 position [[attribute(0)]];};structVertexOutput { float4 position [[position]]; float2 uv;};vertex VertexOutputvertexShader(VertexInput in [[stage_in]]) { VertexOutput out; out.position =float4(in.position,1.0); out.uv = in.position.xy *10.0;return out;}fragment float4fragmentShader(VertexOutput in [[stage_in]]) {returnfloat4(in.uv,0.0,1.0);}
- Convert to CrossGL:
importcrosstlcrossgl_code=crosstl.translate('shader.metal',backend='cgl')print(crossgl_code)
With these examples, you can easily get started with CrossGL, translating between different shader languages, and integrating existing shaders into your CrossGL workflow. Happy shader coding! 🚀✨
For more deep dive into crosstl , check out ourGetting Started Notebook.
We believe that everyone can contribute and make a difference. Whetherit's writing code, fixing bugs, or simply sharing feedback,your contributions are definitely welcome and appreciated 🙌
find out more info in ourContributing guide
Stay connected and follow our latest updates and announcements
See you there!
The CrossGL Translator is open-source and licensed under theLicense.
Thank you for using the CrossGL Translator!
The CrossGL Team
About
Translates native shader languages into CrossGL universal shader language and vice versa.