Forward Shading Pipeline前向渲染管线
Pipeline Overview管线概览
NeoX's primary rendering path is forward shading — every visible pixel evaluates its material and all affecting lights in a single pass. This is configured by forward_shading.render (the pipeline declaration XML).
NeoX 的主要渲染路径是前向着色——每个可见像素在单次 Pass 中计算其材质和所有影响光源。这由 forward_shading.render(管线声明 XML)配置。
Vertex Shader Stage顶点着色器阶段
The vertex shader performs geometry transformation with support for GPU skinning (up to 90 bones), vertex animation, and special projection modes:
顶点着色器执行几何变换,支持 GPU 蒙皮(最多 90 根骨骼)、顶点动画和特殊投影模式:
01Transform Chain变换链
Fragment vs_main(Vertex input) { CommonVertex vert = TransformCommonVertex(input, vert_inter); ApplyVertexOffsetLocal(vert, vert.position); // material-driven offset #if GPU_SKIN_ENABLE DoSkin(vert.blendWeights, vert.blendIndices, vert.position, vert.normal, vert.tangent); #endif float4 world_position = Transform(vert.world_mat, vert.position); PresetupFragment(...); ApplyVertexOffsetWorld(...); // world-space WPO SetupFragment(...); // pack all interpolants }
02Special Projection Modes特殊投影模式
| Mode模式 | Macro宏 | Use Case使用场景 |
|---|---|---|
| IS_PARABOLOID | #if IS_PARABOLOID | Dual-paraboloid shadow maps for omni lights全向光源双抛物面阴影贴图 |
| IS_SKYBOX | #if IS_SKYBOX | Force depth to far plane (z = w*0.9999)强制深度到远平面 |
| TEXTURE_BAKE | #if TEXTURE_BAKE | UV-space rendering for lightmap bakingUV 空间渲染用于光照贴图烘焙 |
| DEPTH_BIAS | #if DEPTH_BIAS | Manual depth offset (shadow bias)手动深度偏移(阴影偏差) |
Pixel Shader Stage像素着色器阶段
The pixel shader is where all material evaluation and lighting happens. The flow follows a strict order:
像素着色器是所有材质计算和光照发生的地方。流程遵循严格顺序:
float4 ps_main(Fragment input, bool is_front_face : SV_IsFrontFace) : SV_Target0 { // 1. Setup Material mtl = (Material)0; View v; v.position = CameraPosition; v.direction = normalize(input.position_world - CameraPosition); v.distance = length(input.position_world - CameraPosition); // 2. Material setup (calls into material_*.hlsl) RawData raw_data = GetRawData(input); // texture sampling SetupMaterial(input, raw_data, mtl); // decode --> Material struct // 3. Lighting (calls into lighting.hlsl --> shading model) LightingResult result = Lighting(input, mtl, v); // 4. Compose final color float3 color = result.direct_diff + result.direct_spec + result.env_diff + result.env_spec + mtl.emissive; // 5. Tone mapping color = get_ACES_tone_mapping(color); return float4(color, opacity); }
Lighting Loop — How Lights Are Evaluated光照循环 — 光源如何计算
Inside lighting.hlsl (1982 lines), the main lighting function iterates over all active lights. Each light goes through shadow evaluation, then calls the active shading model's DirectLighting():
在 lighting.hlsl(1982 行)中,主光照函数遍历所有活动光源。每个光源经过阴影计算,然后调用当前着色模型的 DirectLighting():
Special Modes特殊模式
| Mode模式 | Macro宏 | What it does功能 |
|---|---|---|
| SKIN4S_ENABLE | SKIN4S_SPECULARPASS | 4-pass subsurface scattering (split diffuse/specular)4-Pass 次表面散射(分离漫反射/高光) |
| TOON_EFFECT | #if TOON_EFFECT | Toon shading via Lerp3 color ramp通过 Lerp3 色阶实现卡通着色 |
| IMAGE_DECAL_ENABLE | #if IMAGE_DECAL_ENABLE | Apply projected image decals to GBuffer应用投影图片贴花到 GBuffer |
| DEBUG_DATA | #if DEBUG_DATA | Output material properties as color for debugging输出材质属性为颜色用于调试 |
| RECORD_MODE | #if RECORD_MODE | Record rendering data for replay记录渲染数据用于回放 |
Source Files源码文件
- built_in/forward_shading.hlslMain forward shader (285 lines) — vs_main + ps_main主前向着色器(285 行)— vs_main + ps_main
- built_in/forward_shading.renderPipeline declaration XML — all compositor definitions管线声明 XML — 所有合成器定义
- built_in/common/lighting.hlslLighting core (1982 lines) — light loop + shading dispatch光照核心(1982 行)— 光源循环 + 着色分发
- built_in/shadow/shadow.hlslShadow map sampling (CSM + paraboloid)阴影贴图采样(CSM + 抛物面)
- built_in/common/decal.hlslDeferred decal application延迟贴花应用