《Unity着色器圣经》2.0.2 | 编程语言介绍

目录索引

译文

在开始编写着色器(shader)之前,我们需要先了解一下 Unity 中的三种与 shader 相关的编程语言,它们分别是 HLSL(高级着色器语言 – Microsoft)、Cg(用于图形的C – NVIDIA)和 ShaderLab(声明式语言 – Unity)。由 Cg 编写的着色器仍可被编译,但在最新版本的Unity中已不再使用。

我们的学习之旅将从在内置渲染管线(Built-in RP)中使用的 Cg 和 ShaderLab 语言开始,过渡到在通用渲染管线(URP)中使用的 HLSL。

Cg 是一种被设计用于在大多数 GPU 上编译的高级编程语言,由 NVIDIA 与微软合作开发,语法与 HLSL 非常相似。之所以可以使用 Cg 语言编写着色器,是因为它可以被编译成 HLSL 或 GLSL(OpenGL 着色器语言),这样可以加快并优化为游戏创建材质的过程。

当我们创建着色器时,我们的代码编写在一个名为 CGPROGRAM 的字段中。Unity 目前正在努力提升 Cg 和 HLSL 的兼容性,因为 HLSL 是2019版后的 Unity 版本所使用的官方着色器编程语言, CGPROGRAM 和 ENDCG 很可能很快就会被 HLSLPROGRAM 和 ENDHLSL 所取代。

Unity 中除 shader graph 和计算着色器外的所有着色器都是用一种名为 ShaderLab 的声明式语言编写的。这种语言的语法允许我们在 Unity 检查器中查看着色器内的属性。这非常有趣,因为我们可以实时修改这些属性的值,调整着色器以获得理想的效果。

在 ShaderLab 中,我们可以人为定义多个属性和命令,其中包括回退(Fallback)代码块。

回退是一种基本的代码块,它允许我们的着色器在编译产生错误时返回一个不同的着色器,这样图形硬件就能继续工作。

子着色器(SubShader)是 ShaderLab 中的另一种代码块,它允许我们声明命令并生成pass。使用 Cg/HLSL 编写的着色器可以包含多个 SubShader 或 pass ;但在可编程渲染管线(SRP)的环境下,每个着色器只能包含一个 pass 。


原文对照

Before starting in the definition of code, we must take into consideration that in Unity there are three programming languages associated with shader development, these are HLSL (High-Level Shader Language – Microsoft), Cg (C for Graphics – NVIDIA) which still compiles into the shader but is no longer used in current versions of the software, and ShaderLab (declarative language – Unity).

We will start our adventure working with the Cg and ShaderLab languages in Built-in RP and later give way to the introduction of HLSL in Universal RP.

Cg is a high-level programming language designed to compile on most GPU. It has been developed by NVIDIA in collaboration with Microsoft and uses a syntax very similar to HLSL. The reason why shaders work with the Cg language is that they can compile both HLSL and GLSL (OpenGL Shading Language), accelerating and optimizing the process of creating materials for video games.

When we create a shader, our code compiles in a field called CGPROGRAM. Unity is currently working on providing further support and compatibility between Cg and HLSL, therefore it is very likely that shortly these blocks will be replaced by HLSLPROGRAM and ENDHLSL since HLSL is the official shader programming language in current versions of Unity (version 2019 onwards).

All shaders in Unity (except Shader Graph and Compute) are written within a declarative language called ShaderLab. The syntax of this language allows you to display the properties of a shader in the Unity inspector. This is very interesting since we can manipulate the values of variables and vectors in real-time, adjusting our shader to get the desired result.

In ShaderLab we can manually define several properties and commands, among which is the Fallback block, which is compatible with the different types of rendering pipelines that exist.

Fallback is a fundamental code block in multiplatform games. It allows us to compile a different shader to one that has generated an error. If the shader breaks in its compilation process, Fallback returns a different shader, and so the graphics hardware can continue its work.

SubShader is another block in ShaderLab which allows us to declare commands and generate passes. When written in Cg/HLSL a shader can contain more than one SubShader or pass, however, in the case of Scriptable RP, a shader can only contain one pass per SubShader.

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容