转载请注明出处为KlayGE游戏引擎,本文的永久链接为http://www.klayge.org/?p=2826

Three years ago, I’ve planned a long-term R&D sub-project of KlayGE, D3D11 HLSL bytecode to GLSL complier. Two years ago, derived from d3d1x for linux, a simple bytecode parser and a disasm tool have been implemented. Today, we are glad to announce that this sub-project is named DXBC2GLSL. It provides conversion from HLSL bytecode (DXBC) to GLSL, at both library and tool level. At the end of 2013, an initial version of DXBC2GLSL, with VS and PS support, are finished and submitted to the development version, by our new team member Shenghua Lin. GS support are also added last month. Currently all shaders inside KlayGE have been tested.

The Inputs

Similar projects, such as hlsl2glsl fork and mojoshader, only support SM3. For SM4+ shader, we have to build a compiler our own. The inputs of DXBC2GLSL is SM5 bytecodes (also compatible to SM4). After parsing, input variables, output variables, texture declaration, shader instructions, etc, can be retrieved.

Because we are using d3dcompiler.dll, shader conversion become platform dependent. If you need to do it on Linux, you may need Wine to call d3dcompiler.

The Outputs

DXBC2GLSL contains a library and two tools. Users call the library to do the conversion in their programs. Or run the tool to convert in offline. The output GLSL can be VS/PS/GS in OpenGL 2.0-4.4. HS/DS/CS support, and OpenGL ES version is under development.

Compare with Cg

The original pipeline in KlayGE is a 3-pass conversion. Firstly, a HLSL with #ifdef are converted in token-level, generating Cg code. Then Cg compiler is called to convert it to GLSL. Finally, another token-level conversion is involved to generate “modern” GLSL. With those conversions, GLSL generated by Cg is no longer dependent to vendor hardware. This process is quite complex. Although the stability is ensured, it’s quite slow. Also, because of the differences between Cg and HLSL, to keep the compatibility, some advanced HLSL features are disabled.

After using DXBC2GLSL, HLSL is fully compatible. Only one conversion can get the final GLSL.

Here is a comparison between DXBC2GLSL pipeline and Cg pipeline in KlayGE. Compiling speed and shader performance rows come from DeferredRendering sample.

DXBC2GLSL pipeline Cg pipeline
Shader Model SM5 SM3 and some SM4
Conversion 1-pass 3-pass
Compiling speed 5s 9s
Shader performance 95FPS 80FPS
Vertex Shader Fully supported No gl_VertexID、gl_InstanceID…
Pixel Shader Fully supported No gl_PrimitiveID、gl_SampleID…
Geometry Shader Fully supported No gl_PrimitiveIDIn、gl_Layer…
Hull Shader Work in progress No
Domain Shader Work in progress No
Compute Shader Work in progress No
Uint instructions Yes No
Atomic instructions Yes No
Texture array Yes (but not enabled in KlayGE) No

Future

Currently DXBC2GLSL is only the very first version. We need more tests, refactors and optimizations in the future. Besides, in theory, this framework can even convert compute shader to OpenCL.