Zh/Shader Authoring: Difference between revisions

From Valve Developer Community
< Zh
Jump to navigation Jump to search
(Created page with "{{subst:#if: Translation of 'Shader Authoring' to '中文' via Template:LanguageBar buttons * * * * * * * * * * * * * * * * * * * * * * * * * * * *...")
 
No edit summary
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{subst:#if:|||{{LAuto/t}}
{{lang|Shader Authoring|title = 着色器开发入门}}
{{LanguageBar}}
{{stub}}


--- DON'T JUST BLINDLY DELETE THIS PART. DO REPLACE THE LINKS AND CATEGORIES. THE PICTURE SHOWS HOW TO USE IT ! ---
这篇文档会带你入门{{src|4}}的着色器开发,更多有关着色器的信息以及它们有什么用、应该怎么用,可参考 [[Shader]]。


SEARCH FOR:
==开始吧==
\[\[(?!#|File(?:[ _]talk)?:|Image(?:[ _]talk)?:|Media:|Template(?:[ _]talk)?:|MediaWiki(?:[ _]talk)?:|Talk:|Category[ _]talk:|Project[ _]talk:|Valve[ _]Developer[ _]Community[ _]talk:|Help[ _]talk:|User(?:[ _]talk)?:|c:|commons:|Dictionary:|Google:|GoogleGroups:|IMDB:|M:|Meta:|Metawikipedia:|MW:|SdkBug:|SourceForge:|Steampowered:|W:|Wiki:|WikiBooks:|Wikipedia:|Wikiquote:|Wiktionary:|WP:)(:?(?:Category|Category|Help|Project|Valve[ _]Developer[ _]Community|Special|)(?:[^\|\]]+))(\|?.*?)\]\]


REPLACE WITH:
每个{{src|4}}的材质都有它们特定的着色器,着色器用于渲染材质的视觉效果。一般来说,着色器会定义材质表面的基本光传输逻辑,以便最终呈现高质量的渲染效果。
{{subst:LAuto|$1$2}}


}}{{wip}}{{translating}}
{{src|4}} SDK 拥有完整的 DirectX 着色语言 [[HLSL]] (High-Level Shading Language)以及着色器汇编的支持。我们建议一开始就用 HLSL 写着色器,最后涉及到精细的系统性能优化再使用汇编。
{{LanguageBar}}{{shadertut}}


This document describes how to author shaders in the Source SDK, for further information on what a shader is and how they can be used, please see [[Shader]].
我们需要一些额外的工具来为{{src|4}} SDK 编译着色器,接下来的部分会详细介绍这些工具,并提供它们的链接和简要的安装说明。


==Getting Started==
用到的工具有:Perl,DirectX SDK 和 make。一般而言以下过程只需进行一次,因为只是环境配置。


Every material that is used in Source specifies which shader it uses to render itself. The shader contains the logic and equations to take the source artwork and lighting, and produce the final rendered color for every pixel that the shader is used on.
==创建着色器的选择==
 
{{important| 更准确地说,在{{src13mp|2}}更新了{{tf2branch}}的代码后,默认情况下包含了 SCell555 着色器编译器的修改版本。[https://github.com/misyltoad/ShaderCompile2 this] version.}}
The Source SDK fully supports Microsoft DirectX [[HLSL]] (High-Level Shading Language) and shader assembly languages for writing shaders. It is recommended to use HLSL whenever possible and to only write shader assembly as a last resort.
===选择一:Perl + DirectX SDK===
 
There are a number of external tools required to compile shaders for use in the Source SDK, the following sections detail the tools and provide links for download along with brief instructions on installation and usage.
 
The tools used are Perl, the DirectX SDK and make, the following processes, in general, only have to be performed once before the process of compiling shaders can begin.
 
==Options for Building Shaders==
{{important| After the 2013MP SDK got updated with TF2's code, a modified version of SCell555's shader compiler is included by default, more precisely [https://github.com/misyltoad/ShaderCompile2 this] version.}}
===Option 1 - Perl & DirectX SDK===


====Perl====
====Perl====
To install Perl, go to the [https://www.perl.com/download.csp#win32 download page] on https://www.perl.com/, download, and install Perl - a recommended version of Perl for Windows is [https://strawberryperl.com/ Strawberry Perl].
Perl 可在 [https://www.perl.com/download.csp#win32 download page] 安装,推荐安装的一个版本是 [https://strawberryperl.com/ Strawberry Perl]


The Orange Box SDK also relies on the non-standard Perl package, String::CRC32. If you are using Strawberry Perl, you can install it through the CPAN Client which can be launched from <code><StrawberryPerlInstallDir>\perl\bin\cpan.bat</code>
{{orangebox|4|nt=0}} SDK 也可依赖于非标准的 Perl, String::CRC32。如果你用的是 Strawberry Perl, 你可以通过 CPAN Client 安装它并通过 <code><StrawberryPerlInstallDir>\perl\bin\cpan.bat</code> 启动。


Enter the following command to install the package: <code>install String::CRC32</code>
输入以下命令来安装包文件: <code>install String::CRC32</code>


{{note|If you have problems with cpan.bat, try to open PowerShell in <code><StrawberryPerlInstallDir>\perl\bin\</code> and run command: <code>cpan String::CRC32</code>}}
{{note|如果你的 cpan.bat 有问题, 试试在 PowerShell 里输入 <code><StrawberryPerlInstallDir>\perl\bin\</code> 然后再运行 <code>cpan String::CRC32</code>}}




====DirectX SDK====
====DirectX SDK====
To install the November 2008 DirectX SDK, go to [https://archive.org/details/dxsdk_nov08 https://archive.org/details/dxsdk_nov08], download, and install the DirectX SDK (The old Microsoft link is dead, so we need to use the Wayback Machine).
为安装 November 2008 DirectX SDK, 请访问 [https://archive.org/details/dxsdk_nov08 https://archive.org/details/dxsdk_nov08]。(因为微软早就不给这么老的 API 提供支持了,所以只能去 Internet Archive 下载)
 


{{note|只是安装了 DirectX 9 的运行时 '''并不''' 满足开发需求, 我们必须安装开发套件 <code>DirectX SDK</code>!这就好像只安装 VC 运行库并不满足 C++ 开发一样。}}
{{note|'''因为一些需要的二进制(psa.exe, vsa.exe)已从更新的 DirectX SDK 中移除,所以建议安装 November 2008 SDK。'''}}


{{note|Just having DirectX 9.x installed is '''not''' sufficient, the <code>DirectX SDK</code> must be installed!}}
{{note|'''It is recommended that you use the November 2008 SDK as needed binaries (psa.exe, vsa.exe) have been removed from newer releases.'''}}


====设置环境变量====
当 Perl 和 DirectX SDK 安装好之后, 确保你的 "Path" 环境变量已被正确配置。在{{win|4|nt=1}}里, 你可以通过'''设置 -> 系统 -> 系统信息 -> 高级系统设置 '''来找到环境变量的配置窗口,如下图:
[[File:系统设置位置.png|thumb|系统设置]]


====Setting The Path====
假设 Perl 安装在 <code>C:\Perl </code>,DirectX 安装在 <code>C:\DXSDK </code>,则 "Path "变量应包含 <code>C:\Perl\bin</code> <code>C:\DXSDK\Utilities\bin\x86</code>
With Perl and the DirectX SDK installed, make sure your "Path" environmental variable has been updated appropriately.  In Windows, you can check this by right clicking on My Computer -> properties, going to the Advanced tab, and clicking on "Environment Variables".  Assuming that Perl is installed into <code>C:\Perl</code>, and DirectX into <code>C:\DXSDK</code>, your "Path" variable should contain both <code>C:\Perl\bin</code> and <code>C:\DXSDK\Utilities\bin\x86</code> in a semicolon delimited list. 


By setting up the Environmental Variable in this way, you can update the Perl and DirectX SDK as you please without having to copy files.  If for some reason you do not wish to do this, you can simply copy the required files into a different directory and add that to your path instead.
通过这种方式设置环境变量,你可以随心所欲地更新 Perl DirectX SDK 而无需再手动复制文件。


{{note|You may need to restart your computer for these changes to the environment to take full effect.}}
{{note|可能需要重新启动计算机以使这些环境变化完全生效。}}




===Option 2 - SCell555's Shader Compiler===
===选择二:SCell555 的着色器编译工具===
To install SCell555's Shader Compile tools, you must download the zip file which they provide with every [https://github.com/SCell555/ShaderCompile/releases/latest release].
为安装 SCell555 的着色器编译工具, 访问 [https://github.com/SCell555/ShaderCompile/releases/latest release] 来下载。


Once you have downloaded the zip (or .7z) file, open it up and you should see the following folders:
当你下载好压缩文件后,打开压缩包应该会发现以下三个文件夹:
*bin
*bin
*headers
*headers
Line 63: Line 57:




Place the <code>bin</code> folder into the <code>devtools</code> folder.
<code>bin</code> 文件夹放入 <code>devtools</code> 文件夹。


Place the <code>stdshaders</code> folder into the <code>materialsytem</code> folder.  If it asks you to replace the files, say yes.
<code>stdshaders</code> 文件夹放入 <code>materialsytem</code> 文件夹。如果系统询问是否替换文件,选择是。


Navigate into the <code>headers</code> folder in the zip.  You should see a <code>cshader.h</code> file and a <code>VS2013</code> folder (which also contains another <code>cshader.h</code> file).  If you are using Visual Studio 2013 or above, place the <code>cshader.h</code> in the <code>VS2013</code> folder into <code>public\shaderlib</code> and replace it.  If you are using a Visual Studio version before 2013, use the {{path|cshader|h}} file that isn't in the <code>VS2013</code> folder instead.
导航至压缩包中的 <code>headers</code> 文件夹,你应该会看到一个 <code>cshader.h</code> 文件和 <code>VS2013</code> 文件夹(其中还包含另一个 <code>cshader.h</code> 文件)。如果用的是 Visual Studio 2013 或更高版本,请将 <code>VS2013</code> 文件夹中的 <code>cshader.h</code> 文件放到 <code>public\shaderlib</code> 中并替换它。如果用的是 Visual Studio 2013 之前的版本,请使用 <code>VS2013</code> 文件夹之外的 {{path|cshader|h}} 文件。


==Completing the Setup==
==完成配置==


====Modify stdshader_dx9_20b.txt====
====修改 stdshader_dx9_20b.txt====
Open up {{path|materialsystem/stdshaders/stdshader_dx9_20b|txt}} and comment out all of the lines which compile shaders like so:
打开 {{{path|materialsystem/stdshaders/stdshader_dx9_20b|txt}}} ,并像这样注释掉所有编译着色器的行:
<code>
<code>
  //
  //
Line 89: Line 83:
</code>
</code>


====Modify buildepisodicshaders.bat====
====修改 buildepisodicshaders.bat====
Open {{path|src\materialsystem\stdshaders\buildepisodicshaders|bat}} and set the GAMEDIR AND SDKBINDIR as instructed.
打开 {{path|src\materialsystem\stdshaders\buildepisodicshaders|bat}} 并按照提示设置 GAMEDIR 和 SDKBINDIR。


Inside {{path|buildsdkshaders|bat}}, you might also need to modify the call to vsvars32.bat on line 7 - If you are using Visual Studio 2013 it should point to %VS120COMNTOOLS% instead.
{{path|buildsdkshaders|bat}} 中,可能还需要修改第 7 行对 vsvars32.bat 的调用,如果使用的是 Visual Studio 2013,则应指向 %VS120COMNTOOLS%


Note: GAMEDIR AND SDKBINDIR must be kept to the 8.3 directory name standard.
注意:GAMEDIR 和 SDKBINDIR 必须符合 8.3 目录名称标准。
<code>
<code>
  rem == Set the absolute path to your mod's game directory here ==
  rem == Set the absolute path to your mod's game directory here ==
Line 109: Line 103:
</code>
</code>


====Shader Compile====
====着色器编译====
[[File:compileshadersmd.png|thumb|Example of a successful compile.]]
[[File:compileshadersmd.png|thumb|成功编译示例]]
Navigate into the {{path|materialsystem\stdshaders}} folder. Then type in the name of the bat file for compiling your shaders; it will probably be {{path|buildepisodicshaders|bat}}. If you did everything correctly, it should compile with no issues.
进入 {{path|materialsystem\stdshaders}} 文件夹,然后输入用于编译着色器的 bat 文件名。可能是 {{path|buildepisodicshaders|bat}}。如果你的操作正确,编译应该就没有问题了。
{{warning|You will encounter errors about <code>D3DVERTEXTEXTURESAMPLER0</code> being undefined when using SCell555's compiler.  The fix is to simply remove any references to this definition from <code>register</code> functions in the Vertex Shader files. {{todo|List which files that have <code>register( D3DVERTEXTEXTURESAMPLER0, s0 )</code> in them to make finding the references easier.}} }}
{{warning|使用 SCell555 的编译器时,您会遇到 <code>D3DVERTEXTEXTURESAMPLER0</code> 未定义的错误。解决方法是删除顶点着色器文件中 <code>register</code> 函数对该定义的任何引用。 {{todo|列出其中包含 <code>register( D3DVERTEXTEXTURESAMPLER0, s0 )</code> 的文件,以便于查找引用。}} }}
{{warning|SCell's shader compiler doesn't produce any Pixel Shader files below 2.0b, and no Vertex Shader files below 2.0.  The stock SDK shaders rely on fallbacks to lower versions of these files and, thus will need to be modified to account for this limitation.}}
{{warning|SCell 着色器编译器不会生成任何低于 Shader Model 2.0b 的像素着色器文件,也不会生成低于 2.0 的顶点着色器文件,但是 SDK 里的着色器依赖于低版本文件的 fallback,因此需要修改以适应这一限制。}}


== See also ==
== 另见 ==
* You can get more information about HLSL and shader assembly programming in [https://msdn2.microsoft.com/en-us/library/bb509561.aspx the online MSDN docs].
* 有关 HLSL 和着色器汇编的更多信息,请参阅 [https://msdn2.microsoft.com/en-us/library/bb509561.aspx the online MSDN docs].
* [[:Category:Material System]] -- for more general information on the usage of shaders in materials.
* [[:Category:Material System]] -- 获取更多关于在材质中使用着色器的一般信息。


[[Category:Programming]]
[[Category:Programming]]
[[Category:Shaders]]
[[Category:Shaders]]

Latest revision as of 00:48, 23 August 2025

English (en)Русский (ru)中文 (zh)Translate (Translate)
English (en)Русский (ru)中文 (zh)Translate (Translate)

小作品

This article or section is a stub. You can help by expanding it.

这篇文档会带你入门起源 起源的着色器开发,更多有关着色器的信息以及它们有什么用、应该怎么用,可参考 Shader

开始吧

每个起源 起源的材质都有它们特定的着色器,着色器用于渲染材质的视觉效果。一般来说,着色器会定义材质表面的基本光传输逻辑,以便最终呈现高质量的渲染效果。

起源 起源 SDK 拥有完整的 DirectX 着色语言 HLSL (High-Level Shading Language)以及着色器汇编的支持。我们建议一开始就用 HLSL 写着色器,最后涉及到精细的系统性能优化再使用汇编。

我们需要一些额外的工具来为起源 起源 SDK 编译着色器,接下来的部分会详细介绍这些工具,并提供它们的链接和简要的安装说明。

用到的工具有:Perl,DirectX SDK 和 make。一般而言以下过程只需进行一次,因为只是环境配置。

创建着色器的选择

Icon-Important.png重要: 更准确地说,在起源2013 多人分支 起源2013 多人分支更新了军团要塞2分支的代码后,默认情况下包含了 SCell555 着色器编译器的修改版本。this version.

选择一:Perl + DirectX SDK

Perl

Perl 可在 download page 安装,推荐安装的一个版本是 Strawberry Perl

橙盒 分支 橙盒 引擎分支 SDK 也可依赖于非标准的 Perl, String::CRC32。如果你用的是 Strawberry Perl, 你可以通过 CPAN Client 安装它并通过 <StrawberryPerlInstallDir>\perl\bin\cpan.bat 启动。

输入以下命令来安装包文件: install String::CRC32

Note.png注意:如果你的 cpan.bat 有问题, 试试在 PowerShell 里输入 <StrawberryPerlInstallDir>\perl\bin\ 然后再运行 cpan String::CRC32


DirectX SDK

为安装 November 2008 DirectX SDK, 请访问 https://archive.org/details/dxsdk_nov08。(因为微软早就不给这么老的 API 提供支持了,所以只能去 Internet Archive 下载)


Note.png注意:只是安装了 DirectX 9 的运行时 并不 满足开发需求, 我们必须安装开发套件 DirectX SDK!这就好像只安装 VC 运行库并不满足 C++ 开发一样。
Note.png注意:因为一些需要的二进制(psa.exe, vsa.exe)已从更新的 DirectX SDK 中移除,所以建议安装 November 2008 SDK。


设置环境变量

当 Perl 和 DirectX SDK 安装好之后, 确保你的 "Path" 环境变量已被正确配置。在Windows Microsoft Windows里, 你可以通过设置 -> 系统 -> 系统信息 -> 高级系统设置 来找到环境变量的配置窗口,如下图:

系统设置

假设 Perl 安装在 C:\Perl 中,DirectX 安装在 C:\DXSDK 中,则 "Path "变量应包含 C:\Perl\binC:\DXSDK\Utilities\bin\x86

通过这种方式设置环境变量,你可以随心所欲地更新 Perl 和 DirectX SDK 而无需再手动复制文件。

Note.png注意:可能需要重新启动计算机以使这些环境变化完全生效。


选择二:SCell555 的着色器编译工具

为安装 SCell555 的着色器编译工具, 访问 release 来下载。

当你下载好压缩文件后,打开压缩包应该会发现以下三个文件夹:

  • bin
  • headers
  • stdshaders


bin 文件夹放入 devtools 文件夹。

stdshaders 文件夹放入 materialsytem 文件夹。如果系统询问是否替换文件,选择是。

导航至压缩包中的 headers 文件夹,你应该会看到一个 cshader.h 文件和 VS2013 文件夹(其中还包含另一个 cshader.h 文件)。如果用的是 Visual Studio 2013 或更高版本,请将 VS2013 文件夹中的 cshader.h 文件放到 public\shaderlib 中并替换它。如果用的是 Visual Studio 2013 之前的版本,请使用 VS2013 文件夹之外的 🖿cshader.h 文件。

完成配置

修改 stdshader_dx9_20b.txt

打开 materialsystem/stdshaders/stdshader_dx9_20b ,并像这样注释掉所有编译着色器的行:

//
// Standard shaders collection
//
//  These shaders are compiled as the following shader models:
//        _ps20.vcs
//        _ps20b.vcs
//        _vs20.vcs
//
//example_model_ps20b.fxc
//example_model_vs20.fxc
//SDK_Bloom_ps2x.fxc
//SDK_screenspaceeffect_vs20.fxc
//SDK_bloomadd_ps2x.fxc

修改 buildepisodicshaders.bat

打开 🖿src\materialsystem\stdshaders\buildepisodicshaders.bat 并按照提示设置 GAMEDIR 和 SDKBINDIR。

🖿buildsdkshaders.bat 中,可能还需要修改第 7 行对 vsvars32.bat 的调用,如果使用的是 Visual Studio 2013,则应指向 %VS120COMNTOOLS%。

注意:GAMEDIR 和 SDKBINDIR 必须符合 8.3 目录名称标准。

rem == Set the absolute path to your mod's game directory here ==
rem == Note that this path needs does not support long file/directory names ==
rem == So instead of a path such as "C:\Program Files\Steam\steamapps\mymod" ==
rem == you need to find the 8.3 abbreviation for the directory name using 'dir /x' ==
rem == and set the directory to something like C:\PROGRA~2\Steam\steamapps\sourcemods\mymod ==
set GAMEDIR= YOUR PATH HERE
rem == Set the relative path to SourceSDK\bin\orangebox\bin ==
rem == As above, this path does not support long directory names or spaces ==
rem == e.g. ..\..\..\..\..\PROGRA~2\Steam\steamapps\<USER NAME>\sourcesdk\bin\orangebox\bin ==
set SDKBINDIR= YOUR PATH HERE

着色器编译

成功编译示例

进入 🖿materialsystem\stdshaders 文件夹,然后输入用于编译着色器的 bat 文件名。可能是 🖿buildepisodicshaders.bat。如果你的操作正确,编译应该就没有问题了。

Warning.png警告:使用 SCell555 的编译器时,您会遇到 D3DVERTEXTEXTURESAMPLER0 未定义的错误。解决方法是删除顶点着色器文件中 register 函数对该定义的任何引用。
待完善: 列出其中包含 register( D3DVERTEXTEXTURESAMPLER0, s0 ) 的文件,以便于查找引用。
Warning.png警告:SCell 着色器编译器不会生成任何低于 Shader Model 2.0b 的像素着色器文件,也不会生成低于 2.0 的顶点着色器文件,但是 SDK 里的着色器依赖于低版本文件的 fallback,因此需要修改以适应这一限制。

另见