Considerations about the shader cross-platform solution

Source: Internet
Author: User
After Apple launched metal, in addition to the new metal framewrok, it also had a new Shader Language. Recently, some metal porting tests were conducted, it is mainly about how the existing engine can quickly support metal solutions. I want to write my own experiences on edge Writing. The syntax of metal shader is closer to the HLSL of sm5. Therefore, the HLSL of sm4 or sm5 is easier to convert to metal shader, and its performance is similar to that of gles3.1. It provides vextrex shader, fragment shader and computer shader. With the support of new features of the mobile graphic API, the next generation of game engines for mobile terminal porting has become possible. For large engines such as ue4 and SSPS, the shader code itself is very large. It must support multiple platforms. If multiple shader sets are written, both development and testing will multiply the workload, so we chose to convert shader to cross-platform. Currently, the common shader conversion is either the conversion of HLSL bytecode compiled by fxc, or the conversion between source code directly. For HLSL-> glsl, it is easier to convert byte code. However, metal shader does not provide the bytecode format, so the bytecode conversion method may not work. During the two years of development, ue4 also searched for cross-platform compilers that could solve the HLSL conversion of sm5. The results were only extended based on the features of mesa3d and hlslcc was developed. Currently, there are several HLSL-> glsl solutions:
Hlsl2glslfork, developed by Ati's hlsl2glsl, is also based on some monoshader code (ue3 was the first monoshader to be used). The problem is that the shader source code translation only supports the DX9 style HLSL, some syntaxes of dx10 and dx11 are not supported, nor are there geometry shader, tesseliaton shadr and compute shader. Currently, unity3d still uses this syntax. The hlsl2glsl of klayge is converted from the HLSL bytecode to glsl. The hlslcc of ue4 is also influenced by the design of glsl-optimizer by their PPT at gdc2014. Both of them refer to mesa3d, however, mesa3d only analyzes glsl and converts it to Mesa IR. hlslcc is changed to HLSL Analysis of sm5, And the converter of IR-> glsl is added. The metal shader support is also added after ue4.3. Next, I would like to share my opinion with the hlslcc PPT. The conversion process of hlslcc is HLSL-> Ast-> ir-> glsl/metal + parametermap, some glsl-optimizer functions are used for the output, but there are still many problems when running ue4 hlslcc. 1. shader Marco, like ue. USF, ce. fxc uses macros for some shader Conditional compilation and Static branch processing, which is similar to Uber shader's concept. However, there are basically no conversion tools to handle such complicated work, in addition, there are also frequent vs, PS of multiple entry functions (main) written into a USF file, directly the whole. it is unrealistic for USF to throw the conversion tool. The UE and Ce solutions should be managed internally to separate the parts of each entry function. For example, ue4 defines the golbalshader subclass, set parameters and entry function names by yourself. Then the independent shader entry function is converted. While # include, # define, # If macro and branch processing must pass before conversion, leaving only the required part of runtime, it is converted into shader of different platforms and compiled into cache for storage. 2. OGL transmits parameters from CPU to GPU through uniform from each frame. In order to reduce the number of API calls, dx11 provides const buffer, and es3.0 and metal also provide the concept of uniform buffer, for large games, the number of Apis called by uniform update is also a large part of consumption. Therefore, you need to use const buffer to organize Parameters Based on the update frequency and sequence, in DX9, which does not support this concept, uses a simulated const buffer to cache and reassemble the parameters before updating. Although ue4 directly supports dx11, however, es2.0 devices on the mobile platform do not have the uniform buffer feature. They use the simulated uniform buffer function (shadow buffer in PPT) and package it into the uniform Array Based on the update frequency. Therefore, hlslcc not only outputs glsl, but also generates parameter map.

3. for shader version, if it is the conversion of dx11 HLSL, you can modify and customize hlslcc. hlslcc itself also supports output of GL and metal of different versions. If it is a DX9 HLSL style, some preprocessing work is required. The main changes of DX9 and dx11 are in the use of semantic, texture, and sample. For example, the input and dx11 values of pixel shader are changed to sv_position. in output, color is replaced by sv_target.
DX9:
struct vs2ps{     float4 Pos : POSITION;     float4 TexCd : TEXCOORD0;};

Dx11:

struct vs2ps{    float4 Pos: SV_POSITION;    float4 TexCd: TEXCOORD0;};

DX9:

float4 PS(vs2ps In): COLOR

Dx11:

float4 PS(vs2ps In): SV_Target 

 

Texture sample definition:

DX9:
texture Tex <string uiname="Texture";>;sampler Samp = sampler_state //sampler for doing the texture-lookup{    Texture = (Tex); //apply a texture to the sampler    MipFilter = LINEAR; //sampler states    MinFilter = LINEAR;    MagFilter = LINEAR;};

Dx11:

Texture2D texture;SamplerState g_samLinear{    Filter = MIN_MAG_MIP_LINEAR;    AddressU = Wrap;    AddressV = Wrap;};

 

If you want sample texture:

DX9:
float4 col = tex2D(Samp, In.TexCd.xy);

Dx11:

float4 col = texture2d.Sample( g_samLinear, In.TexCd.xy);

DX9:

float4 col = tex2Dlod(Samp, float4(In.TexCd.xy,0,level));

Dx11:

float4 col = texture2d.SampleLevel( g_samLinear, In.TexCd.xy,level);

DX9:

float4 col  = tex2Dproj(Samp,screenProj.xyzw);

Dx11:

float4 col = Samp.Sample(g_samLinear, .creenProj.xy / screenProj.w );

 

4 gles3.1 and metal both provide the compute shader function, which is called the kernel function in metal shader. Unfortunately, this part of ue4 is not supported yet, therefore, the compute shader conversion has not yet been implemented in hlslcc, but I believe it will be supported soon. Wait and see.

In summary, my work in this area is just the beginning, so the depth of description is limited. With the in-depth work, we should further analyze how hlslcc and various APIs are optimized.

Reference link: bringing Unreal Engine 4 to OpenGL

Considerations about the shader cross-platform solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.