
文档来自 https://github.com/sp614x/optifine/blob/master/OptiFineDoc/doc/shaders.txt
由chatgpt翻译,人工校验
建议使用obsidian等可以打开.md文件的阅读

==========optifine_shader_开发指南.md==============
## 概述
Shaders Mod(着色器模组)使用了延迟渲染管线。
在渲染管线中,首先执行的是 gbuffer(几何缓冲区)着色器。它们将数据渲染到纹理上,这些纹理将被发送到合成着色器(composite shaders)。
在阴影贴图(shadowcomp)之后、地形渲染(prepare)之前和水渲染(deferred)之前,可以添加可选的合成着色器。
合成着色器会将数据渲染到纹理中,这些纹理随后会被发送到最终着色器。
最终着色器直接渲染到屏幕上。
## 着色器文件
所有着色器文件都放置在着色器包的“shaders”文件夹中。
着色器源文件的名称使用它们要使用的程序名称,并根据其类型使用相应的扩展名。
| 扩展名 | 类型 |
|---------|-----------------------|
| .csh | 计算着色器 |
| .vsh | 顶点着色器 |
| .gsh | 几何着色器 |
| .fsh | 片段着色器 |
几何着色器需要 OpenGL 3.2 版本支持布局限定符,或者使用扩展 `GL_ARB_geometry_shader4`(`GL_EXT_geometry_shader4`),并配置“maxVerticesOut”。
## 颜色附件
数据通过颜色附件在着色器之间传递。
所有机器至少支持 4 个颜色附件。对于支持的机器,最多可以有 16 个颜色附件。
即使使用现代 GPU,MacOS 也只能支持 8 个颜色附件。
在延迟、合成和最终着色器中,这些附件通过 `gcolor`、`gdepth`、`gnormal`、`composite`、`gaux1`、`gaux2`、`gaux3` 和 `gaux4` 这些 uniform 进行引用。(`colortex0` 到 `colortex15` 也可以用来代替 `gcolor`、`gdepth` 等。)
尽管名称不同,所有这些颜色附件都是相同的,可以用于任何目的,除了前两个。
第一个 `gcolor` 的颜色在渲染前会被清除为当前的雾颜色。
第二个 `gdepth` 的颜色在渲染前会被清除为纯白色,并使用高精度存储缓冲区来存储深度值。
其余的颜色会被清除为黑色,alpha 为 0。
每个颜色附件使用 2 个缓冲区(A 和 B),逻辑名称为 "main" 和 "alt",可以用作乒乓缓冲区。
当缓冲区翻转时,main/alt 和 A/B 之间的映射会反转。
Gbuffer 程序总是从 "main" 读取(仅限 `gaux1-4`)并写入到 "main" 缓冲区(它们不应该在同一个缓冲区中同时进行读写)。
延迟/合成程序总是从 "main" 读取并写入到 "alt" 缓冲区。
在延迟/合成程序渲染后,它写入的缓冲区将被翻转,因此下一个程序可以将当前输出作为输入。
属性 `flip.<program>.<buffer>=<true|false>` 可用于独立于缓冲区写入来启用或禁用翻转。
虚拟程序 `deferred_pre` 和 `composite_pre` 可用于在延迟/合成通道之前进行缓冲区翻转。
输出颜色附件通过在片段着色器中放置的 `/* DRAWBUFFERS:XYZ */` 或 `/* RENDERTARGETS: X,Y,Z */` 注释进行配置。
Gbuffer、延迟和合成程序可以写入任何颜色附件,但同时写入的附件数量不能超过 8 个。
如果没有配置输出颜色附件,则程序将写入前 8 个颜色附件。
在使用现代语法(130及以上版本)的着色器中,片段着色器的输出应使用 `outColor<n>` 名称。例如:
```glsl
/* DRAWBUFFERS:3,4,7 */
out vec4 outColor0; // 写入到缓冲区 3
out vec4 outColor1; // 写入到缓冲区 4
out vec4 outColor2; // 写入到缓冲区 7
```
### 写入颜色附件
在合成着色器中写入颜色附件时,混合功能是禁用的。
如果合成着色器同时读取和写入颜色附件,将会产生伪影(除非你只是复制原始内容)。
### 着色器配置解析
着色器配置解析受到预处理器条件编译指令的影响。
当前识别的预处理器指令如下:
- `#define <macro>`
- `#undef <macro>`
- `#ifdef <macro>`
- `#ifndef <macro>`
- `#if <int>`
- `#if defined <macro>`
- `#if !defined <macro>`
- `#elif <int>`
- `#elif defined <macro>`
- `#elif !defined <macro>`
- `#else`
- `#endif`
当前的着色器包可以通过按 "F3+R" 或使用命令 `/reloadShaders` 来重新加载。
### 计算着色器
每个程序(除了 gbuffer 程序)都可以附加一列计算着色器。
它们的命名方式与程序相同,并可以有可选的后缀,例如 "composite.csh"、"composite_a.csh" …… "composite_z.csh"。
计算着色器在程序之前运行,并可以通过纹理采样器读取所有缓冲区。
它们可以使用别名 `colorimg0-5` 和 `shadowcolorimg0-1` 作为图像来读取和写入 `colortex0-5` 和 `shadowcolor0-1` 缓冲区,例如:
```glsl
layout (rgba8) uniform image2D colorimg0;
```
计算着色器需要至少 "#version 430" 和局部大小定义,例如:
```glsl
layout (local_size_x = 16, local_size_y = 16) in;
```
工作组可以通过以下方式定义:
- 固定的:
```glsl
const ivec3 workGroups = ivec3(50, 30, 1);
```
- 相对于渲染大小:
```glsl
const vec2 workGroupsRender = vec2(0.5f, 0.5f);
```
默认配置为:
```glsl
const vec2 workGroupsRender = vec2(1.0f, 1.0f);
```
这表示每个像素执行一次计算着色器。
### 图像访问
所有程序可以使用 `imageLoad()` 和 `imageStore()` 来读取和写入 `colorimg0-5` 和 `shadowcolorimg0-1`。
### 着色器程序
| 名称 | 渲染 | 未定义时使用 |
|--------------------------------|-------------------------------------|----------------------------------------|
| `<none>` | gui、菜单 | `<none>` |
| --- 阴影图 --- |
| shadow | 阴影通道中的一切 | `<none>` |
| shadow_solid | `<未使用>` | shadow |
| shadow_cutout | `<未使用>` | shadow |
| --- 阴影合成 --- |
| shadowcomp | `<shadowcomp>` | `<none>` |
| shadowcomp1 | `<shadowcomp>` | `<none>` |
| ... | `<shadowcomp>` | `<none>` |
| shadowcomp99 | `<shadowcomp>` | `<none>` |
| --- 准备 --- |
| prepare | `<prepare>` | `<none>` |
| prepare1 | `<prepare>` | `<none>` |
| ... | `<prepare>` | `<none>` |
| prepare99 | `<prepare>` | `<none>` |
| --- GBuffer --- |
| gbuffers_basic | 牵引、方块选择框 | `<none>` |
| gbuffers_line | 方块轮廓、钓鱼线 | gbuffers_basic |
| gbuffers_textured | 粒子 | gbuffers_basic |
| gbuffers_textured_lit | 发光粒子、世界边界 | gbuffers_textured |
| gbuffers_skybasic | 天空、地平线、星星、虚空 | gbuffers_basic |
| gbuffers_skytextured | 太阳、月亮 | gbuffers_textured |
| gbuffers_clouds | 云 | gbuffers_textured |
| gbuffers_terrain | 实心、切割、切割_多级纹理 | gbuffers_textured_lit |
| gbuffers_terrain_solid | `<未使用>` | gbuffers_terrain |
| gbuffers_terrain_cutout_mip | `<未使用>` | gbuffers_terrain |
| gbuffers_terrain_cutout | `<未使用>` | gbuffers_terrain |
| gbuffers_damagedblock | 受损方块 | gbuffers_terrain |
| gbuffers_block | 方块实体 | gbuffers_terrain |
| gbuffers_beaconbeam | 信标光束 | gbuffers_textured |
| gbuffers_item | `<未使用>` | gbuffers_textured_lit |
| gbuffers_entities | 实体 | gbuffers_textured_lit |
| gbuffers_entities_glowing | 发光实体、光谱效果 | gbuffers_entities |
| gbuffers_armor_glint | 盔甲和手持物品上的闪光 | gbuffers_textured |
| gbuffers_spidereyes | 蜘蛛、末影人和龙的眼睛 | gbuffers_textured |
| gbuffers_hand | 手和不透明的手持物品 | gbuffers_textured_lit |
| gbuffers_weather | 雨、雪 | gbuffers_textured_lit |
| --- 延迟 --- |
| deferred_pre | `<虚拟>` 翻转 ping-pong 缓冲区 | `<none>` |
| deferred | `<deferred>` | `<none>` |
| deferred1 | `<deferred>` | `<none>` |
| ... | `<deferred>` | `<none>` |
| deferred99 | `<deferred>` | `<none>` |
| --- GBuffer 半透明 --- |
| gbuffers_water | 半透明 | gbuffers_terrain |
| gbuffers_hand_water | 半透明手持物品 | gbuffers_hand |
| --- 合成 --- |
| composite_pre | `<虚拟>` 翻转 ping-pong 缓冲区 | `<none>` |
| composite | `<composite>` | `<none>` |
| composite1 | `<composite>` | `<none>` |
| ... | `<composite>` | `<none>` |
| composite99 | `<composite>` | `<none>` |
| --- 最终 --- |
| final | `<final>` | `<none>` |
备注:
- `shadow_solid`、`shadow_cutout`、`gbuffers_terrain_solid`、`gbuffers_terrain_cutout` 和 `gbuffers_terrain_cutout_mip` 目前没有使用。
待办事项:
- 为世界边界、实体(按 ID、按类型)、披风、elytra、狼项圈等添加单独的程序。
### 属性
| 源 | 值 | 注释 |
|-----------------------------|---------------------------------------------|---------------------------------------------------------------|
| `in vec3 vaPosition;` | 位置 (x, y, z) | 1.17+ 版本中,地形相对于区块原点,参见 "chunkOffset" |
| `in vec4 vaColor;` | 颜色 (r, g, b, a) | 1.17+ 版本 |
| `in vec2 vaUV0;` | 纹理 (u, v) | 1.17+ 版本 |
| `in ivec2 vaUV1;` | 覆盖层 (u, v) | 1.17+ 版本 |
| `in ivec2 vaUV2;` | 光照图 (u, v) | 1.17+ 版本 |
| `in vec3 vaNormal;` | 法线 (x, y, z) | 1.17+ 版本 |
| `in vec3 mc_Entity;` | xy = blockId, 渲染类型 | "blockId" 仅用于 "block.properties" 中指定的方块 |
| `in vec2 mc_midTexCoord;` | st = midTexU, midTexV | 精灵中间的 UV 坐标 |
| `in vec4 at_tangent;` | xyz = 切线向量,w = 手性 | |
| `in vec3 at_velocity;` | 顶点到前一帧的偏移量 | 在视图空间,仅用于实体和方块实体 |
| `in vec3 at_midBlock;` | 相对于方块中心的偏移量(单位:1/64m) | 仅用于方块 |
### Uniforms(统一变量)
| 源 | 值 |
|-----------------------------------------|---------------------------------------------------------|
| `uniform int heldItemId;` | 手持物品 ID(主手),仅用于 "item.properties" 中定义的物品 |
| `uniform int heldBlockLightValue;` | 手持物品光照值(主手) |
| `uniform int heldItemId2;` | 手持物品 ID(副手),仅用于 "item.properties" 中定义的物品 |
| `uniform int heldBlockLightValue2;` | 手持物品光照值(副手) |
| `uniform int fogMode;` | GL_LINEAR、GL_EXP 或 GL_EXP2 |
| `uniform float fogStart;` | 雾开始距离(米) |
| `uniform float fogEnd;` | 雾结束距离(米) |
| `uniform int fogShape;` | 0 = 球形,1 = 圆柱形 |
| `uniform float fogDensity;` | 0.0-1.0 |
| `uniform vec3 fogColor;` | 雾的颜色 (r, g, b) |
| `uniform vec3 skyColor;` | 天空的颜色 (r, g, b) |
| `uniform int worldTime;` | 世界时间(<ticks> = worldTicks % 24000) |
| `uniform int worldDay;` | 世界天数(<days> = worldTicks / 24000) |
| `uniform int moonPhase;` | 月相 0-7 |
| `uniform int frameCounter;` | 帧索引(0 到 720719,然后重置为 0) |
| `uniform float frameTime;` | 上一帧时间(秒) |
| `uniform float frameTimeCounter;` | 运行时间(秒)(3600秒后重置为 0) |
| `uniform float sunAngle;` | 太阳角度 0.0-1.0 |
| `uniform float shadowAngle;` | 阴影角度 0.0-1.0 |
| `uniform float rainStrength;` | 雨强度 0.0-1.0 |
| `uniform float aspectRatio;` | 视图宽度 / 视图高度 |
| `uniform float viewWidth;` | 视图宽度 |
| `uniform float viewHeight;` | 视图高度 |
| `uniform float near;` | 近裁剪面距离 |
| `uniform float far;` | 远裁剪面距离 |
| `uniform vec3 sunPosition;` | 太阳位置(眼睛空间) |
| `uniform vec3 moonPosition;` | 月亮位置(眼睛空间) |
| `uniform vec3 shadowLightPosition;` | 阴影光源(太阳或月亮)位置(眼睛空间) |
| `uniform vec3 upPosition;` | 向上方向 |
| `uniform vec3 cameraPosition;` | 相机位置(世界空间) |
| `uniform vec3 previousCameraPosition;` | 上一帧相机位置 |
| `uniform mat4 gbufferModelView;` | 设置相机变换后的模型视图矩阵 |
| `uniform mat4 gbufferModelViewInverse;` | gbufferModelView 的逆矩阵 |
| `uniform mat4 gbufferPreviousModelView;`| 上一帧的 gbufferModelView |
| `uniform mat4 gbufferProjection;` | 生成 gbuffers 时的投影矩阵 |
| `uniform mat4 gbufferProjectionInverse;`| gbufferProjection 的逆矩阵 |
| `uniform mat4 gbufferPreviousProjection;`| 上一帧的 gbufferProjection |
| `uniform mat4 shadowProjection;` | 生成阴影图时的投影矩阵 |
| `uniform mat4 shadowProjectionInverse;` | shadowProjection 的逆矩阵 |
| `uniform mat4 shadowModelView;` | 生成阴影图时的模型视图矩阵 |
| `uniform mat4 shadowModelViewInverse;` | shadowModelView 的逆矩阵 |
| `uniform float wetness;` | 雨水强度,经过 wetnessHalfLife 或 drynessHalfLife 平滑 |
| `uniform float eyeAltitude;` | 视点实体 Y 位置 |
| `uniform ivec2 eyeBrightness;` | x = 方块亮度, y = 天空亮度,光照 0-15 = 亮度 0-240 |
| `uniform ivec2 eyeBrightnessSmooth;` | eyeBrightness 经过 eyeBrightnessHalflife 平滑 |
| `uniform ivec2 terrainTextureSize;` | 未使用 |
| `uniform int terrainIconSize;` | 未使用 |
| `uniform int isEyeInWater;` | 1 = 相机在水中,2 = 相机在岩浆中,3 = 相机在粉雪中 |
| `uniform float nightVision;` | 夜视(0.0-1.0) |
| `uniform float blindness;` | 盲目(0.0-1.0) |
| `uniform float screenBrightness;` | 屏幕亮度(0.0-1.0) |
| `uniform int hideGUI;` | GUI 是否隐藏 |
| `uniform float centerDepthSmooth;` | centerDepth 经过 centerDepthSmoothHalflife 平滑 |
| `uniform ivec2 atlasSize;` | 纹理图集大小(仅在纹理图集被绑定时设置) |
| `uniform vec4 spriteBounds;` | 精灵在纹理图集中的边界 (u0, v0, u1, v1),当 MC_ANISOTROPIC_FILTERING 启用时设置 |
| `uniform vec4 entityColor;` | 实体颜色乘法因子(实体受伤,爆炸中的克里普) |
| `uniform int entityId;` | 实体 ID |
| `uniform int blockEntityId;` | 方块实体 ID(方块 ID,用于 tile entity,仅用于 "block.properties" 中指定的方块)|
| `uniform ivec4 blendFunc;` | 混合函数 (srcRGB, dstRGB, srcAlpha, dstAlpha) |
| `uniform int instanceId;` | 实例 ID,当启用实例化时 (countInstances > 1),0 = 原始,1-N = 副本 |
| `uniform float playerMood;` | 玩家情绪(0.0-1.0),玩家在地下待的时间越长情绪越高 |
| `uniform int renderStage;` | 渲染阶段,参见 "Standard Macros", "J. Render stages"|
| `uniform int bossBattle;` | 1 = 自定义,2 = 末影龙,3 = 凋零,4 = 突袭 |
| `uniform mat4 modelViewMatrix;` | 模型视图矩阵 |
| `uniform mat4 modelViewMatrixInverse;
|// 1.19+ |
| `uniform float darknessFactor;` | 黑暗效果的强度 (0.0-1.0)|
| `uniform float darknessLightFactor;` | 黑暗效果导致的光照图变化 (0.0-1.0)|
### 常量
```cpp
// 光照图纹理矩阵,1.17+ 版本
const mat4 TEXTURE_MATRIX_2 = mat4(
vec4(0.00390625, 0.0, 0.0, 0.0),
vec4(0.0, 0.00390625, 0.0, 0.0),
vec4(0.0, 0.0, 0.00390625, 0.0),
vec4(0.03125, 0.03125, 0.03125, 1.0)
);
```
### GBuffers Uniforms
程序:basic, textured, textured_lit, skybasic, skytextured, clouds, terrain, terrain_solid, terrain_cutout_mip, terrain_cutout, damagedblock, water, block, beaconbeam, item, entities, armor_glint, spidereyes, hand, hand_water, weather
| 来源 | 值 |
|---------------------------------|----------------------------------------------------------|
| `uniform sampler2D gtexture;` | 0 |
| `uniform sampler2D lightmap;` | 1 |
| `uniform sampler2D normals;` | 2 |
| `uniform sampler2D specular;` | 3 |
| `uniform sampler2D shadow;` | `waterShadowEnabled ? 5 : 4` |
| `uniform sampler2D watershadow;` | 4 |
| `uniform sampler2D shadowtex0;` | 4 |
| `uniform sampler2D shadowtex1;` | 5 |
| `uniform sampler2D depthtex0;` | 6 |
| `uniform sampler2D gaux1;` | 7 <自定义纹理或延迟渲染程序的输出> |
| `uniform sampler2D gaux2;` | 8 <自定义纹理或延迟渲染程序的输出> |
| `uniform sampler2D gaux3;` | 9 <自定义纹理或延迟渲染程序的输出> |
| `uniform sampler2D gaux4;` | 10 <自定义纹理或延迟渲染程序的输出> |
| `uniform sampler2D colortex4;` | 7 <自定义纹理或延迟渲染程序的输出> |
| `uniform sampler2D colortex5;` | 8 <自定义纹理或延迟渲染程序的输出> |
| `uniform sampler2D colortex6;` | 9 <自定义纹理或延迟渲染程序的输出> |
| `uniform sampler2D colortex7;` | 10 <自定义纹理或延迟渲染程序的输出> |
| `uniform sampler2D colortex8;` | 16 <自定义纹理或延迟渲染程序的输出> |
| `uniform sampler2D colortex9;` | 17 <自定义纹理或延迟渲染程序的输出> |
| `uniform sampler2D colortex10;` | 18 <自定义纹理或延迟渲染程序的输出> |
| `uniform sampler2D colortex11;` | 19 <自定义纹理或延迟渲染程序的输出> |
| `uniform sampler2D colortex12;` | 20 <自定义纹理或延迟渲染程序的输出> |
| `uniform sampler2D colortex13;` | 21 <自定义纹理或延迟渲染程序的输出> |
| `uniform sampler2D colortex14;` | 22 <自定义纹理或延迟渲染程序的输出> |
| `uniform sampler2D colortex15;` | 23 <自定义纹理或延迟渲染程序的输出> |
| `uniform sampler2D depthtex1;` | 11 |
| `uniform sampler2D shadowcolor;` | 13 |
| `uniform sampler2D shadowcolor0;`| 13 |
| `uniform sampler2D shadowcolor1;`| 14 |
| `uniform sampler2D noisetex;` | 15 |
### 阴影 Uniforms
程序:shadow, shadow_solid, shadow_cutout
| 来源 | 值 |
|---------------------------------|----------------------------------------------------------|
| `uniform sampler2D tex;` | 0 |
| `uniform sampler2D gtexture;` | 0 |
| `uniform sampler2D lightmap;` | 1 |
| `uniform sampler2D normals;` | 2 |
| `uniform sampler2D specular;` | 3 |
| `uniform sampler2D shadow;` | `waterShadowEnabled ? 5 : 4` |
| `uniform sampler2D watershadow;` | 4 |
| `uniform sampler2D shadowtex0;` | 4 |
| `uniform sampler2D shadowtex1;` | 5 |
| `uniform sampler2D gaux1;` | 7 <自定义纹理> |
| `uniform sampler2D gaux2;` | 8 <自定义纹理> |
| `uniform sampler2D gaux3;` | 9 <自定义纹理> |
| `uniform sampler2D gaux4;` | 10 <自定义纹理> |
| `uniform sampler2D colortex4;` | 7 <自定义纹理> |
| `uniform sampler2D colortex5;` | 8 <自定义纹理> |
| `uniform sampler2D colortex6;` | 9 <自定义纹理> |
| `uniform sampler2D colortex7;` | 10 <自定义纹理> |
| `uniform sampler2D colortex8;` | 16 <自定义纹理> |
| `uniform sampler2D colortex9;` | 17 <自定义纹理> |
| `uniform sampler2D colortex10;` | 18 <自定义纹理> |
| `uniform sampler2D colortex11;` | 19 <自定义纹理> |
| `uniform sampler2D colortex12;` | 20 <自定义纹理> |
| `uniform sampler2D colortex13;` | 21 <自定义纹理> |
| `uniform sampler2D colortex14;` | 22 <自定义纹理> |
| `uniform sampler2D colortex15;` | 23 <自定义纹理> |
| `uniform sampler2D shadowcolor;` | 13 |
| `uniform sampler2D shadowcolor0;`| 13 |
| `uniform sampler2D shadowcolor1;`| 14 |
| `uniform sampler2D noisetex;` | 15 |
### 合成和延迟 Uniforms
程序:composite, composite1, composite2, composite3, composite4, composite5, composite6, composite7, final, deferred, deferred1, deferred2, deferred3, deferred4,
| 来源 | 值 |
|---------------------------------|----------------------------------------------------------|
| `uniform sampler2D gcolor;` | 0 |
| `uniform sampler2D gdepth;` | 1 |
| `uniform sampler2D gnormal;` | 2 |
| `uniform sampler2D composite;` | 3 |
| `uniform sampler2D gaux1;` | 7 |
| `uniform sampler2D gaux2;` | 8 |
| `uniform sampler2D gaux3;` | 9 |
| `uniform sampler2D gaux4;` | 10 |
| `uniform sampler2D colortex0;` | 0 |
| `uniform sampler2D colortex1;` | 1 |
| `uniform sampler2D colortex2;` | 2 |
| `uniform sampler2D colortex3;` | 3 |
| `uniform sampler2D colortex4;` | 7 |
| `uniform sampler2D colortex5;` | 8 |
| `uniform sampler2D colortex6;` | 9 |
| `uniform sampler2D colortex7;` | 10 |
| `uniform sampler2D colortex8;` | 16 |
| `uniform sampler2D colortex9;` | 17 |
| `uniform sampler2D colortex10;` | 18 |
| `uniform sampler2D colortex11;` | 19 |
| `uniform sampler2D colortex12;` | 20 |
| `uniform sampler2D colortex13;` | 21 |
| `uniform sampler2D colortex14;` | 22 |
| `uniform sampler2D colortex15;` | 23 |
| `uniform sampler2D shadow;` | `waterShadowEnabled ? 5 : 4` |
| `uniform sampler2D watershadow;`| 4 |
| `uniform sampler2D shadowtex0;` | 4 |
| `uniform sampler2D shadowtex1;` | 5 |
| `uniform sampler2D gdepthtex;` | 6 |
| `uniform sampler2D depthtex0;` | 6 |
| `uniform sampler2D depthtex1;` | 11 |
| `uniform sampler2D depthtex2;` | 12 |
| `uniform sampler2D shadowcolor;`| 13 |
| `uniform sampler2D shadowcolor0;`| 13 |
| `uniform sampler2D shadowcolor1;`| 14 |
| `uniform sampler2D noisetex;` | 15 |
### GBuffers 纹理
| Id | 名称 | 旧名称 |
|-----|----------------|--------------------------------------------------|
| 0 | `gtexture` | `texture` |
| 1 | `lightmap` | - |
| 2 | `normals` | - |
| 3 | `specular` | - |
| 4 | `shadowtex0` | `shadow`, `watershadow` |
| 5 | `shadowtex1` | `shadow` (当使用 `watershadow` 时) |
| 6 | `depthtex0` | - |
| 7 | `gaux1` | `colortex4` <自定义纹理或延迟程序的输出> |
| 8 | `gaux2` | `colortex5` <自定义纹理或延迟程序的输出> |
| 9 | `gaux3` | `colortex6` <自定义纹理或延迟程序的输出> |
| 10 | `gaux4` | `colortex7` <自定义纹理或延迟程序的输出> |
| 12 | `depthtex1` | - |
| 13 | `shadowcolor0` | `shadowcolor` |
| 14 | `shadowcolor1` | - |
| 15 | `noisetex` | - |
| 16 | `colortex8` | <自定义纹理或延迟程序的输出> |
| 17 | `colortex9` | <自定义纹理或延迟程序的输出> |
| 18 | `colortex10` | <自定义纹理或延迟程序的输出> |
| 19 | `colortex11` | <自定义纹理或延迟程序的输出> |
| 20 | `colortex12` | <自定义纹理或延迟程序的输出> |
| 21 | `colortex13` | <自定义纹理或延迟程序的输出> |
| 22 | `colortex14` | <自定义纹理或延迟程序的输出> |
| 23 | `colortex15` | <自定义纹理或延迟程序的输出> |
### 阴影纹理
| Id | 名称 | 旧名称 |
|-----|----------------|--------------------------------------------------|
| 0 | `gtexture` | `tex`, `texture` |
| 1 | `lightmap` | - |
| 2 | `normals` | - |
| 3 | `specular` | - |
| 4 | `shadowtex0` | `shadow`, `watershadow` |
| 5 | `shadowtex1` | `shadow` (当使用 `watershadow` 时) |
| 7 | `gaux1` | `colortex4` <自定义纹理> |
| 8 | `gaux2` | `colortex5` <自定义纹理> |
| 9 | `gaux3` | `colortex6` <自定义纹理> |
| 10 | `gaux4` | `colortex7` <自定义纹理> |
| 13 | `shadowcolor0` | `shadowcolor` |
| 14 | `shadowcolor1` | - |
| 15 | `noisetex` | - |
| 16 | `colortex8` | <自定义纹理> |
| 17 | `colortex9` | <自定义纹理> |
| 18 | `colortex10` | <自定义纹理> |
| 19 | `colortex11` | <自定义纹理> |
| 20 | `colortex12` | <自定义纹理> |
| 21 | `colortex13` | <自定义纹理> |
| 22 | `colortex14` | <自定义纹理> |
| 23 | `colortex15` | <自定义纹理> |
### 合成和延迟纹理
| Id | 名称 | 旧名称 |
|-----|----------------|--------------------------|
| 0 | `colortex0` | `gcolor` |
| 1 | `colortex1` | `gdepth` |
| 2 | `colortex2` | `gnormal` |
| 3 | `colortex3` | `composite` |
| 4 | `shadowtex0` | `shadow`, `watershadow` |
| 5 | `shadowtex1` | `shadow` (当使用 `watershadow` 时) |
| 6 | `depthtex0` | `gdepthtex` |
| 7 | `colortex4` | `gaux1` |
| 8 | `colortex5` | `gaux2` |
| 9 | `colortex6` | `gaux3` |
| 10 | `colortex7` | `gaux4` |
| 11 | `depthtex1` | - |
| 12 | `depthtex2` | - |
| 13 | `shadowcolor0` | `shadowcolor` |
| 14 | `shadowcolor1` | - |
| 15 | `noisetex` | - |
| 16 | `colortex8` | - |
| 17 | `colortex9` | - |
| 18 | `colortex10` | - |
| 19 | `colortex11` | - |
| 20 | `colortex12` | - |
| 21 | `colortex13` | - |
| 22 | `colortex14` | - |
| 23 | `colortex15` | - |
### 深度缓冲区的使用
| 名称 | 使用情况 |
|-------------|---------------------------------------------------------------|
| `depthtex0` | 用于所有情况 |
| `depthtex1` | 不用于透明物体(如水、镶嵌玻璃) |
| `depthtex2` | 不用于透明物体(如水、镶嵌玻璃),也不用于手持物体 |
### 阴影缓冲区的使用
| 名称 | 使用情况 |
|-------------|---------------------------------------------------------------|
| `shadowtex0` | 用于所有情况 |
| `shadowtex1` | 不用于透明物体(如水、镶嵌玻璃) |
### 顶点着色器配置
| 来源 | 效果 | 备注 |
|-----------------------------------------------|---------------------------------------------------------|--------------------------------------------|
| `in vec3 mc_Entity;` | `useEntityAttrib = true` | - |
| `in vec2 mc_midTexCoord;` | `useMidTexCoordAttrib = true` | - |
| `in vec4 at_tangent;` | `useTangentAttrib = true` | - |
| `const int countInstances = 1;` | 当 `countInstances > 1` 时,几何体会被渲染多次,见 `uniform "instanceId"` | - |
### 几何着色器配置
| 来源 | 效果 | 备注 |
|-----------------------------------------------|---------------------------------------------------------|--------------------------------------------|
| `#extension GL_ARB_geometry_shader4 : enable` | 启用 `GL_ARB_geometry_shader4` | - |
| `const int maxVerticesOut = 3;` | 设置 `GEOMETRY_VERTICES_OUT_ARB` 以支持 `GL_ARB_geometry_shader4` | - |
### 片段着色器配置
| 来源 | 效果 | 备注 |
|-----------------------------------------------|-------------------------------------------------------------|-----------------------------------------------------|
| `uniform <type> shadow;` | `shadowDepthBuffers = 1` | - |
| `uniform <type> watershadow;` | `shadowDepthBuffers = 2` | - |
| `uniform <type> shadowtex0;` | `shadowDepthBuffers = 1` | - |
| `uniform <type> shadowtex1;` | `shadowDepthBuffers = 2` | - |
| `uniform <type> shadowcolor;` | `shadowColorBuffers = 1` | - |
| `uniform <type> shadowcolor0;` | `shadowColorBuffers = 1` | - |
| `uniform <type> shadowcolor1;` | `shadowColorBuffers = 2` | - |
| `uniform <type> depthtex0;` | `depthBuffers = 1` | - |
| `uniform <type> depthtex1;` | `depthBuffers = 2` | - |
| `uniform <type> depthtex2;` | `depthBuffers = 3` | - |
| `uniform <type> gdepth;` | 如果 `bufferFormat[1] == RGBA` 则 `bufferFormat[1] = RGBA32F` | - |
| `uniform <type> gaux1;` | `colorBuffers = 5` | - |
| `uniform <type> gaux2;` | `colorBuffers = 6` | - |
| `uniform <type> gaux3;` | `colorBuffers = 7` | - |
| `uniform <type> gaux4;` | `colorBuffers = 8` | - |
| `uniform <type> colortex4;` | `colorBuffers = 5` | - |
| `uniform <type> colortex5;` | `colorBuffers = 6` | - |
| `uniform <type> colortex6;` | `colorBuffers = 7` | - |
| `uniform <type> colortex7;` | `colorBuffers = 8` | - |
| `uniform <type> centerDepthSmooth;` | `centerDepthSmooth = true` | - |
| `/* SHADOWRES:1024 */` | `shadowMapWidth = shadowMapHeight = 1024` | - |
| `const int shadowMapResolution = 1024;` | `shadowMapWidth = shadowMapHeight = 1024` | - |
| `/* SHADOWFOV:90.0 */` | `shadowMapFov = 90` | - |
| `const float shadowMapFov = 90.0;` | `shadowMapFov = 90` | - |
| `/* SHADOWHPL:160.0 */` | `shadowMapDistance = 160.0` | - |
| `const float shadowDistance = 160.0f;` | `shadowMapDistance = 160.0` | - |
| `const float shadowDistanceRenderMul = -1f;` | `shadowDistanceRenderMul = -1` | 当大于 0 时启用阴影优化 (`shadowRenderDistance = shadowDistance * shadowDistanceRenderMul`) |
| `const float shadowIntervalSize = 2.0f;` | `shadowIntervalSize = 2.0` | - |
| `const bool generateShadowMipmap = true;` | `shadowMipmap = true` | - |
| `const bool generateShadowColorMipmap = true;` | `shadowColorMipmap = true` | - |
| `const bool shadowHardwareFiltering = true;` | `shadowHardwareFiltering = true` | - |
| `const bool shadowHardwareFiltering0 = true;` | `shadowHardwareFiltering[0] = true` | - |
| `const bool shadowHardwareFiltering1 = true;` | `shadowHardwareFiltering[1] = true` | - |
| `const bool shadowtexMipmap = true;` | `shadowMipmap[0] = true` | - |
| `const bool shadowtex0Mipmap = true;` | `shadowMipmap[0] = true` | - |
| `const bool shadowtex1Mipmap = true;` | `shadowMipmap[1] = true` | - |
| `const bool shadowcolor0Mipmap = true;` | `shadowColorMipmap[0] = true` | - |
| `const bool shadowColor0Mipmap = true;` | `shadowColorMipmap[0] = true` | - |
| `const bool shadowcolor1Mipmap = true;` | `shadowColorMipmap[1] = true` | - |
| `const bool shadowColor1Mipmap = true;` | `shadowColorMipmap[1] = true` | - |
| `const bool shadowtexNearest = true;` | `shadowFilterNearest[0] = true` | - |
| `const bool shadowtex0Nearest = true;` | `shadowFilterNearest[0] = true` | - |
| `const bool shadow0MinMagNearest = true;` | `shadowFilterNearest[0] = true` | - |
| `const bool shadowtex1Nearest = true;` | `shadowFilterNearest[1] = true` | - |
| `const bool shadow1MinMagNearest = true;` | `shadowFilterNearest[1] = true` | - |
| `const bool shadowcolor0Nearest = true;` | `shadowColorFilterNearest[0] = true` | - |
| `const bool shadowColor0Nearest = true;` | `shadowColorFilterNearest[0] = true` | - |
| `const bool shadowColor0MinMagNearest = true;` | `shadowColorFilterNearest[0] = true` | - |
| `const bool shadowcolor1Nearest = true;` | `shadowColorFilterNearest[1] = true` | - |
| `const bool shadowColor1Nearest = true;` | `shadowColorFilterNearest[1] = true` | - |
| `const bool shadowColor1MinMagNearest = true;` | `shadowColorFilterNearest[1] = true` | - |
| `/* WETNESSHL:600.0 */` | `wetnessHalfLife = 600 (ticks)` | - |
| `const float wetnessHalflife = 600.0f;` | `wetnessHalfLife = 600 (ticks)` | - |
| `/* DRYNESSHL:200.0 */` | `drynessHalfLife = 200 (ticks)` | - |
| `const float drynessHalflife = 200.0f;` | `drynessHalfLife = 200 (ticks)` | - |
| `const float eyeBrightnessHalflife = 10.0f;` | `eyeBrightnessHalflife = 10 (ticks)` | - |
| `const float centerDepthHalflife = 1.0f;` | `centerDepthSmoothHalflife = 1 (ticks)` | - |
| `const float sunPathRotation = 0f;` | `sunPathRotation = 0f` | - |
| `const float ambientOcclusionLevel = 1.0f;` | `ambientOcclusionLevel = 1.0f` | 0.0f = 禁用 AO,1.0f = 默认 AO |
| `const int superSamplingLevel = 1;` | `superSamplingLevel = 1` | - |
| `const int noiseTextureResolution = 256;` | `noiseTextureResolution = 256` | - |
| `/* GAUX4FORMAT:RGBA32F */` | `buffersFormat[7] = GL_RGBA32F` | - |
| `/* GAUX4FORMAT:RGB32F */` | `buffersFormat[7] = GL_RGB32F` | - |
| `/* GAUX4FORMAT:RGB16 */` | `buffersFormat[7] = GL_RGB16` | - |
| `const int <bufferIndex>Format = <format>;` | `bufferFormats[index] = <format>` | 参见 "Draw Buffer Index" 和 "Texture Formats" |
| `const bool <bufferIndex>Clear = false;` | `gbuffersClear[index] = false` | 跳过 `glClear()` 对于指定的缓冲区,仅适用于 "composite" 和 "deferred" 程序 |
| `const vec4 <bufferIndex>ClearColor = vec4();` | `gbuffersClearColor[index
### Draw Buffer Index(绘图缓冲区索引)
|前缀 | 索引|
|--------|----------|
|colortex<0-15> | 0-15|
|gcolor | 0 |
|gdepth | 1 |
|gnormal | 2 |
|composite | 3 |
|gaux1 | 4 |
|gaux2 | 5 |
|gaux3 | 6 |
|gaux4 | 7 |
### Shadow Buffer Index(阴影缓冲区索引)
|前缀 | 索引|
|--------|----------|
|shadowcolor | 0 |
|shadowcolor<0-1> | 0-1 |
### Texture Formats(纹理格式)
1. **8-bit(8位)**
| 标准化 | 签名标准化 | 整数 | 无符号整数 |
|--------------|----------------|--------------|---------------|
| R8 | R8_SNORM | R8I | R8UI |
| RG8 | RG8_SNORM | RG8I | RG8UI |
| RGB8 | RGB8_SNORM | RGB8I | RGB8UI |
| RGBA8 | RGBA8_SNORM | RGBA8I | RGBA8UI |
2. **16-bit(16位)**
| 标准化 | 签名标准化 | 浮点 | 整数 | 无符号整数 |
|--------------|----------------|--------------|--------------|--------------|
| R16 | R16_SNORM | R16F | R16I | R16UI |
| RG16 | RG16_SNORM | RG16F | RG16I | RG16UI |
| RGB16 | RGB16_SNORM | RGB16F | RGB16I | RGB16UI |
| RGBA16 | RGBA16_SNORM | RGBA16F | RGBA16I | RGBA16UI |
3. **32-bit(32位)**
| 浮点 | 整数 | 无符号整数 |
|--------------|--------------|--------------|
| R32F | R32I | R32UI |
| RG32F | RG32I | RG32UI |
| RGB32F | RGB32I | RGB32UI |
| RGBA32F | RGBA32I | RGBA32UI |
4. **Mixed(混合)**
| 类型 |
|-----------------|
| R3_G3_B2 |
| RGB5_A1 |
| RGB10_A2 |
| R11F_G11F_B10F |
| RGB9_E5 |
### Pixel Formats(像素格式)
1. 标准化
- RED
- RG
- RGB
- BGR
- RGBA
- BGRA
2. 整数
- RED_INTEGER
- RG_INTEGER
- RGB_INTEGER
- BGR_INTEGER
- RGBA_INTEGER
- BGRA_INTEGER
### Pixel Types(像素类型)
- BYTE
- SHORT
- INT
- HALF_FLOAT
- FLOAT
- UNSIGNED_BYTE
- UNSIGNED_BYTE_3_3_2
- UNSIGNED_BYTE_2_3_3_REV
- UNSIGNED_SHORT
- UNSIGNED_SHORT_5_6_5
- UNSIGNED_SHORT_5_6_5_REV
- UNSIGNED_SHORT_4_4_4_4
- UNSIGNED_SHORT_4_4_4_4_REV
- UNSIGNED_SHORT_5_5_5_1
- UNSIGNED_SHORT_1_5_5_5_REV
- UNSIGNED_INT
- UNSIGNED_INT_8_8_8_8
- UNSIGNED_INT_8_8_8_8_REV
- UNSIGNED_INT_10_10_10_2
- UNSIGNED_INT_2_10_10_10_REV
### Block ID Mapping(方块 ID 映射)
方块 ID 映射定义在着色器包中的 `shaders/block.properties` 文件中。
Forge 模组可以在模组 JAR 文件中的 `assets/<modid>/shaders/block.properties` 添加自定义方块映射。
`block.properties` 文件可以使用条件预处理指令(如 `#ifdef`、`#if` 等)。
有关更多详细信息,请参见“标准宏”部分 A 至 I。还可以使用选项宏。
格式为 `block.<id>=<block1> <block2> ...`
键是替代的方块 ID,值是要替换的方块。
每个方块 ID 只能有一行。
有关方块匹配规则,请参见 `properties_files.txt`。
##### 简短格式
block.31=red_flower yellow_flower reeds
##### 长格式
block.32=minecraft:red_flower ic2:nether_flower botania:reeds
##### 属性
block.33=minecraft:red_flower:type=white_tulip minecraft:red_flower:type=pink_tulip botania:reeds:type=green
有关详细信息,请参见 `properties.files`。
### Block Render Layers(方块渲染层)
自定义方块渲染层定义在着色器包中的 `shaders/block.properties` 文件中。
```glsl
layer.solid=<blocks> // 不透明,无混合(不透明纹理)
layer.cutout=<blocks> // 有 alpha,无混合(剪切纹理)
layer.cutout_mipped=<blocks> // 有 alpha,无混合,使用 mipmaps(带 mipmaps 的剪切纹理)
layer.translucent=<blocks> // 有 alpha,有混合,使用 mipmaps(水、染色玻璃)
```
##### 层
- solid:无 alpha,无混合(不透明纹理)
- cutout:有 alpha,无混合(剪切纹理)
- cutout_mipped:有 alpha,无混合,使用 mipmaps(带 mipmaps 的剪切纹理)
- translucent:有 alpha,有混合,使用 mipmaps(水、染色玻璃)
固体不透明立方体(如石头、泥土、矿石等)不能在自定义层上渲染,因为这会影响面剔除、环境光遮蔽、光照传播等。
例如:
layer.translucent=glass_pane fence wooden_door
### Item ID Mapping(物品 ID 映射)
物品 ID 映射定义在着色器包中的 `shaders/item.properties` 文件中。
Forge 模组可以在模组 JAR 文件中的 `assets/<modid>/shaders/item.properties` 添加自定义物品映射。
`item.properties` 文件可以使用条件预处理指令(如 `#ifdef`、`#if` 等)。
有关更多详细信息,请参见“标准宏”部分 A 至 I。还可以使用选项宏。
格式为 `item.<id>=<item1> <item2> ...`
键是替代的物品 ID,值是要替换的物品。
每个物品 ID 只能有一行。
##### 简短格式
item.5000=diamond_sword dirt
##### 长格式
item.5001=minecraft:diamond_sword botania:reeds
### Entity ID Mapping(实体 ID 映射)
实体 ID 映射定义在着色器包中的 `shaders/entity.properties` 文件中。
Forge 模组可以在模组 JAR 文件中的 `assets/<modid>/shaders/entity.properties` 添加自定义实体映射。
`entity.properties` 文件可以使用条件预处理指令(如 `#ifdef`、`#if` 等)。
有关更多详细信息,请参见“标准宏”部分 A 至 I。还可以使用选项宏。
格式为 `entity.<id>=<entity1> <entity2> ...`
键是替代的实体 ID,值是要替换的实体。
每个实体 ID 只能有一行。
##### 简短格式
entity.2000=sheep cow
##### 长格式
entity.2001=minecraft:pig botania:pixie
### 标准宏
标准宏在每个着色器文件的 `#version` 声明之后会自动包含。
A. Minecraft 版本
```cpp
#define MC_VERSION <value>
```
值的格式为 122(主版本 1,次版本 2,修订 2)
例如:1.9.4 -> 10904,1.11.2 -> 11102 等。
B. 最大支持的 GL 版本
```cpp
#define MC_GL_VERSION <value>
```
值为整数,例如:210、320、450
C. 最大支持的 GLSL 版本
```cpp
#define MC_GLSL_VERSION <value>
```
值为整数,例如:120、150、450
D. 操作系统
以下之一:
```cpp
#define MC_OS_WINDOWS
#define MC_OS_MAC
#define MC_OS_LINUX
#define MC_OS_OTHER
```
E. 驱动程序
以下之一:
```cpp
#define MC_GL_VENDOR_AMD
#define MC_GL_VENDOR_ATI
#define MC_GL_VENDOR_INTEL
#define MC_GL_VENDOR_MESA
#define MC_GL_VENDOR_NVIDIA
#define MC_GL_VENDOR_XORG
#define MC_GL_VENDOR_OTHER
```
F. GPU
以下之一:
```cpp
#define MC_GL_RENDERER_RADEON
#define MC_GL_RENDERER_GEFORCE
#define MC_GL_RENDERER_QUADRO
#define MC_GL_RENDERER_INTEL
#define MC_GL_RENDERER_GALLIUM
#define MC_GL_RENDERER_MESA
#define MC_GL_RENDERER_OTHER
```
G. OpenGL 扩展
支持的 OpenGL 扩展的宏命名与相应的扩展名称相同,并带有前缀 "MC_"。
例如,当支持扩展 "GL_ARB_shader_texture_lod" 时,定义宏 "MC_GL_ARB_shader_texture_lod"。
仅添加被引用和支持的宏到着色器文件中。
H. 选项
```cpp
#define MC_FXAA_LEVEL <value> // 当 FXAA 启用时,值:2、4
#define MC_NORMAL_MAP // 当法线贴图启用时
#define MC_SPECULAR_MAP // 当高光贴图启用时
#define MC_RENDER_QUALITY <value> // 值:0.5、0.70710677、1.0、1.4142135、2.0
#define MC_SHADOW_QUALITY <value> // 值:0.5、0.70710677、1.0、1.4142135、2.0
#define MC_HAND_DEPTH <value> // 值:0.0625、0.125、0.25
#define MC_OLD_HAND_LIGHT // 当旧手部光照启用时
#define MC_OLD_LIGHTING // 当旧光照启用时
#define MC_ANISOTROPIC_FILTERING <value> // 当各向异性过滤启用时
```
I. 纹理
```cpp
#define MC_TEXTURE_FORMAT_LAB_PBR // 纹理格式 LabPBR (https://github.com/rre36/lab-pbr/wiki)
#define MC_TEXTURE_FORMAT_LAB_PBR_1_3 // 版本 1.3
```
(见 "texture.properties")
J. 渲染阶段
// 常量用于 uniform "renderStage"
// 常量按阶段执行的顺序给出
```cpp
#define MC_RENDER_STAGE_NONE <const> // 未定义
#define MC_RENDER_STAGE_SKY <const> // 天空
#define MC_RENDER_STAGE_SUNSET <const> // 日落和日出覆盖
#define MC_RENDER_STAGE_CUSTOM_SKY <const> // 自定义天空
#define MC_RENDER_STAGE_SUN <const> // 太阳
#define MC_RENDER_STAGE_MOON <const> // 月亮
#define MC_RENDER_STAGE_STARS <const> // 星星
#define MC_RENDER_STAGE_VOID <const> // 虚空
#define MC_RENDER_STAGE_TERRAIN_SOLID <const> // 实体地形
#define MC_RENDER_STAGE_TERRAIN_CUTOUT_MIPPED <const> // 切割地形(带有 mipmaps)
#define MC_RENDER_STAGE_TERRAIN_CUTOUT <const> // 切割地形
#define MC_RENDER_STAGE_ENTITIES <const> // 实体
#define MC_RENDER_STAGE_BLOCK_ENTITIES <const> // 方块实体
#define MC_RENDER_STAGE_DESTROY <const> // 毁坏覆盖
#define MC_RENDER_STAGE_OUTLINE <const> // 选择轮廓
#define MC_RENDER_STAGE_DEBUG <const> // 调试渲染器
#define MC_RENDER_STAGE_HAND_SOLID <const> // 实心手持物体
#define MC_RENDER_STAGE_TERRAIN_TRANSLUCENT <const> // 半透明地形
#define MC_RENDER_STAGE_TRIPWIRE <const> // 滑轮绳
#define MC_RENDER_STAGE_PARTICLES <const> // 粒子
#define MC_RENDER_STAGE_CLOUDS <const> // 云
#define MC_RENDER_STAGE_RAIN_SNOW <const> // 雨和雪
#define MC_RENDER_STAGE_WORLD_BORDER <const> // 世界边界
#define MC_RENDER_STAGE_HAND_TRANSLUCENT <const> // 半透明手持物体
```
### 参考资料
- [Daxnitro Wiki: Editing Shaders](http://daxnitro.wikia.com/wiki/Editing_Shaders_%28Shaders2%29)
- [Minecraft Forum: Shaders Mod Updated by Karyonix](http://www.minecraftforum.net/forums/mapping-and-modding/minecraft-mods/1286604-shaders-mod-updated-by-karyonix)
- [Minecraft Forum: Search by Karyonix](http://www.minecraftforum.net/forums/search?by-author=karyonix&display-type=posts)
- [FBO Feedback](http://www.seas.upenn.edu/~cis565/fbo.htm#feedback)