Shader学习笔记-HLSL

前言 上一篇介绍了ShaderLab的结构,这一篇继续介绍Shader的主要逻辑部分。 Unity的旧版本用的是CG语言,现在逐步淘汰了,主要用微软的HLSL,并加了一些Unity自己的语法。 预处理 include include and include_with_pragmas directives in HLSL - Unity 手册 在 HLSL 中,#include 指令是一种预处理器指令。它们指示编译器将一个 HLSL 文件的内容包含在另一个 HLSL 文件中。它们包含的文件称为包含文件。 #include_with_pragmas 指令的工作方式与常规 #include 指令相同,但它也允许您在包含文件中使用 #pragma 指令。这意味着 #include_with_pragmas 指令允许您在多个文件之间共享 #pragma 指令。 在 HLSL 中向着色器编译器提供信息 Provide information to the shader compiler in HLSL - Unity 手册 #pragma target 3.0 #pragma exclude_renderers vulkan #pragma vertex vert #pragma fragment frag // The rest of your HLSL code goes here 指定着色器阶段 语句 功能 #pragma vertex <name> Compile the function with the given name as the vertex shader. Replace with the function name. This directive is required in regular graphics shaders.将具有给定名称的函数编译为顶点着色器。 替换为函数名称。此指令在常规图形着色器中是必需的。 #pragma fragment <name> Compile the function with the given name as the fragment shader. Replace with the function name. This directive is required in regular graphics shaders.将具有给定名称的函数编译为片段着色器。 替换为函数名称。此指令在常规图形着色器中是必需的。 #pragma geometry <name> Compile the function with the given name as the geometry shader. Replace with the function name. This option automatically turns on #pragma require geometry; for more information, see Targeting shader models and GPU features in HLSL.将具有给定名称的函数编译为几何着色器。 替换为函数名称。此选项会自动打开 #pragma 需要几何图形;有关详细信息,请参阅在 HLSL 中面向着色器模型和 GPU 功能。Note: Metal does not support geometry shaders.注意:Metal 不支持几何体着色器。 #pragma hull <name> Compile the function with the given name as the DirectX 11 hull shader. Replace with the function name. This automatically adds #pragma require tessellation; for more information, see Targeting shader models and GPU features in HLSL.将具有给定名称的函数编译为 DirectX 11 hull 着色器。 替换为函数名称。这会自动添加 #pragma 需要曲面细分;有关详细信息,请参阅在 HLSL 中面向着色器模型和 GPU 功能。 #pragma domain <name> Compile the function with the given name as the DirectX 11 domain shader. Replace with the function name. This option automatically turns on #pragma require tessellation; for more information, see Targeting shader models and GPU features in HLSL.将具有给定名称的函数编译为 DirectX 11 域着色器。 替换为函数名称。此选项会自动打开 #pragma 需要镶嵌;有关详细信息,请参阅在 HLSL 中面向着色器模型和 GPU 功能。 着色器变体和关键字 ...

2024-9-1 · Dand

Shader学习笔记-ShaderLab结构

前言 之前的业务一直对图形学涉及不多,现在打算系统学习一下。 Shader代码见得多,但也仅限于对着代码猜其功能,具体的结构和写法不求甚解,现在总结下。 属性 定义可变属性 Color Vector4 Texture2D 等等 Properties { // 在此处定义您的属性,例如颜色、纹理等 _Color ("Color", Color) = (1, 1, 1, 1) _MainTex ("Texture", 2D) = "white" {} } SubShader ShaderLab:定义 Shader 对象 - Unity 手册 ShaderLab:定义子着色器 - Unity 手册 子Shader,渲染时会逐个使用,找到第一个生效的,如果都不生效,执行FallBack的Shader。 Tags(子着色器) ShaderLab:向子着色器分配标签 - Unity 手册 子着色器标签定义了子着色器块或一个通道在何时以及在何种条件下被执行。 RenderPipeline RenderPipeline 标签向 Unity 告知子着色器是否与通用渲染管线 (URP) 或高清渲染管线 (HDRP) 兼容。 Queue Queue 标签向 Unity 告知要用于它渲染的几何体的渲染队列。 “Queue” = “[queue name] + [offset]” [queue name] Background 指定背景渲染队列。 Geometry 指定几何体渲染队列。 AlphaTest 指定 AlphaTest 渲染队列。 Transparent 指定透明渲染队列。 Overlay 指定覆盖渲染队列。 [offset] 整数 指定 Unity 渲染未命名队列处的索引(相对于命名队列)。 RenderType 在运行时替换着色器 - Unity 手册 ...

2024-8-7 · Dand