Skip to content

04 · Write and inspect MRT data ​

Write color, normal and teaching material data in one opaque draw, then display a normal debug view. Prerequisites: 03, attachment locations, uint and bit operations.

Changes and complete snapshot ​

Add RGBA8 normal/material textures. Named Terrain target topology contains RT0/RT1/RT2 and the shared depth. Opaque explicitly uses drawBuffers=[0,1,2], its fragment shader writes three outputs, and grade reads the auxiliary textures. Keep frameTarget at RT0 + depth for ordinary world drawing. Totals: 5 textures and 6 commands.

Download and inspect all files

Resource flow ​

opaque → RT0 color / RT1 normal / RT2 data → grade debug view → post/color → present

Target topology names attached images; drawBuffers selects active outputs for this draw. The three BuiltIn fallbacks retain the named three-color topology, while their resolved recipe enables RT0 only. Authoring drawBuffers directly on a fallback is rejected with DLMAN0001. Opaque clears auxiliary targets, loads existing color and clears depth under this ABI. Host normal data contains oct-encoded RG bytes and a sign byte, stored unchanged as RGB divided by 255; A=1 marks a course opaque write. This chapter displays the encoding directly. RGB is not XYZ and must not be decoded with rgb*2-1 followed by normalization; direction decoding is deferred. Material R/G hold low/high bytes of the vertex identifier’s lower 16 bits, B stores demonstration luminance, and A marks validity. This is a course convention, not a host G-buffer or stable content/material-ID ABI. Read with texelFetch and integer coordinates to avoid filtering identifier bytes.

Complete shaders ​

glsl
#version 320 es
precision highp float;
out vec2 v_uv;
void main() {
    const vec2 corners[4] = vec2[4](vec2(-1.0, -1.0), vec2(1.0, -1.0), vec2(-1.0, 1.0), vec2(1.0, 1.0));
    vec2 position = corners[gl_VertexID & 3];
    v_uv = position * 0.5 + 0.5;
    gl_Position = vec4(position, 0.0, 1.0);
}
glsl
#version 320 es
// <Semantic Name='TEXCOORD' Attribute='a_texcoord' />
// <Semantic Name='POSITION' Attribute='a_position' />
// <Semantic Name='COLOR' Attribute='a_color' />
// <Semantic Name='IDENTIFIER' Attribute='a_identifier' />
// <Semantic Name='NORMAL' Attribute='a_normal' />
// <Semantic Name='LIGHTLEVEL' Attribute='a_lightLevel' />
// <Semantic Name='PURELIGHT' Attribute='a_pureLight' />
// <Semantic Name='HEIGHTDATA' Attribute='a_heightData' />
uniform mat4 u_viewProjectionMatrix;
uniform vec2 u_origin;
uniform float u_lightTable[96];
layout(location = 0) in vec2 a_texcoord;
layout(location = 1) in vec3 a_position;
layout(location = 2) in vec4 a_color;
layout(location = 3) in uint a_identifier;
layout(location = 4) in vec3 a_normal;
layout(location = 5) in float a_lightLevel;
layout(location = 6) in float a_pureLight;
layout(location = 7) in float a_heightData;
out vec2 v_texcoord;
out vec4 v_color;
out vec3 v_normalEncoded;
flat out uint v_identifier;
void main() {
    int lightIndex = clamp(int(a_lightLevel) - 1, 0, 95);
    float light = clamp(u_lightTable[lightIndex], 0.0, 1.0);
    v_texcoord = a_texcoord;
    v_color = vec4(a_color.rgb * light, a_color.a);
    v_normalEncoded = clamp(a_normal / 255.0, 0.0, 1.0);
    v_identifier = a_identifier;
    vec3 relativePosition = vec3(a_position.x - u_origin.x, a_position.y, a_position.z - u_origin.y);
    gl_Position = u_viewProjectionMatrix * vec4(relativePosition, 1.0);
    OPENGL_POSITION_FIX;
}
glsl
#version 320 es
// <Sampler Name='u_samplerState' Texture='u_texture' />
precision highp float;
precision highp int;
uniform sampler2D u_texture;
in vec2 v_texcoord;
in vec4 v_color;
in vec3 v_normalEncoded;
flat in uint v_identifier;
layout(location = 0) out vec4 o_sceneColor;
layout(location = 1) out vec4 o_sceneNormal;
layout(location = 2) out vec4 o_sceneMaterial;
void main() {
    vec4 albedo = texture(u_texture, v_texcoord) * v_color;
    o_sceneColor = vec4(albedo.rgb * vec3(0.92, 1.0, 0.92), albedo.a);
    o_sceneNormal = vec4(v_normalEncoded, 1.0);
    // Pack convention: two identifier bytes, demonstration luminance, validity.
    float lowByte = float(v_identifier & 255u) / 255.0;
    float highByte = float((v_identifier >> 8u) & 255u) / 255.0;
    float luminance = dot(albedo.rgb, vec3(0.2126, 0.7152, 0.0722));
    o_sceneMaterial = vec4(lowByte, highByte, clamp(luminance, 0.0, 1.0), 1.0);
}
glsl
#version 320 es
precision highp float;
precision highp int;
uniform sampler2D u_sceneColor;
uniform sampler2D u_sceneNormal;
uniform sampler2D u_sceneMaterial;
in vec2 v_uv;
layout(location = 0) out vec4 o_color;
void main() {
    const int DEBUG_VIEW = 1; // 0: color, 1: encoded normal, 2: identifier bytes
    vec3 color = texture(u_sceneColor, v_uv).rgb;
    ivec2 size = textureSize(u_sceneNormal, 0);
    ivec2 pixel = clamp(ivec2(v_uv * vec2(size)), ivec2(0), size - 1);
    vec4 normalData = texelFetch(u_sceneNormal, pixel, 0);
    vec4 materialData = texelFetch(u_sceneMaterial, pixel, 0);
    if (DEBUG_VIEW == 1) {
        // Host encoded-normal bytes (oct RG plus a sign byte), not XYZ.
        // Display the encoding directly; decoding is outside this chapter.
        color = normalData.a > 0.5 ? normalData.rgb : vec3(0.12);
    } else if (DEBUG_VIEW == 2) {
        uint identifier = uint(round(materialData.r * 255.0)) | (uint(round(materialData.g * 255.0)) << 8u);
        color = materialData.a > 0.5 ? vec3(float(identifier & 15u) / 15.0, materialData.b, float((identifier >> 4u) & 15u) / 15.0) : vec3(0.12);
    }
    o_color = vec4(color, 1.0);
}
glsl
#version 320 es
precision highp float;
uniform sampler2D u_postColor;
in vec2 v_uv;
layout(location = 0) out vec4 o_color;
void main() {
    o_color = texture(u_postColor, v_uv);
}

Complete manifest ​

shaderpack.json
json
{
  "manifestVersion": 5,
  "sceneAbiVersion": 2,
  "expectedCatalogSha256": "4bc28c17beffea2311a422fff9f633d2e4f03784460d0254adda8af2e766d84c",
  "id": "course:04-mrt",
  "name": "DawnlightCore Course 04-mrt",
  "version": "0.1.0",
  "author": "EltanceX",
  "description": "Write MRT data; independent v5 course snapshot, runtime not yet verified.",
  "shaderRoot": "shaders",
  "options": [],
  "resources": [
    {
      "id": "course:scene/color",
      "kind": "texture2D",
      "lifetime": "persistent",
      "size": {
        "mode": "viewport"
      },
      "format": "rgba8"
    },
    {
      "id": "course:scene/depth",
      "kind": "texture2D",
      "lifetime": "persistent",
      "size": {
        "mode": "viewport"
      },
      "format": "depth24"
    },
    {
      "id": "course:post/color",
      "kind": "texture2D",
      "lifetime": "persistent",
      "size": {
        "mode": "viewport"
      },
      "format": "rgba8"
    },
    {
      "id": "course:scene/normal",
      "kind": "texture2D",
      "lifetime": "persistent",
      "size": {
        "mode": "viewport"
      },
      "format": "rgba8"
    },
    {
      "id": "course:scene/material",
      "kind": "texture2D",
      "lifetime": "persistent",
      "size": {
        "mode": "viewport"
      },
      "format": "rgba8"
    }
  ],
  "programs": [
    {
      "id": "course:scene/terrain",
      "kind": "graphics",
      "vertex": "terrain.vsh",
      "fragment": "terrain.psh",
      "compileMode": "officialCompatible"
    },
    {
      "id": "course:post/grade",
      "kind": "graphics",
      "vertex": "fullscreen.vsh",
      "fragment": "grade.psh",
      "compileMode": "manifestDriven"
    },
    {
      "id": "course:post/present",
      "kind": "graphics",
      "vertex": "fullscreen.vsh",
      "fragment": "present.psh",
      "compileMode": "manifestDriven"
    }
  ],
  "frameTarget": {
    "targets": {
      "colors": [
        {
          "location": 0,
          "resource": "course:scene/color",
          "loadOp": "clear",
          "storeOp": "store",
          "clearValue": [
            0,
            0,
            0,
            1
          ]
        }
      ],
      "depth": {
        "resource": "course:scene/depth",
        "loadOp": "clear",
        "storeOp": "store",
        "clearValue": 1
      }
    }
  },
  "passes": [
    {
      "id": "course:terrain_opaque",
      "enabled": true,
      "stage": {
        "template": "dawnlight:command_list",
        "version": 1,
        "target": "world",
        "phase": "opaque"
      },
      "services": [],
      "programs": [
        "course:scene/terrain"
      ],
      "commands": [
        {
          "type": "engineDraw",
          "sceneProvider": {
            "id": "dawnlight:scene_draw/terrain_opaque",
            "version": 2
          },
          "targets": {
            "colors": [
              {
                "location": 0,
                "resource": "course:scene/color",
                "loadOp": "load",
                "storeOp": "store"
              },
              {
                "location": 1,
                "resource": "course:scene/normal",
                "loadOp": "clear",
                "storeOp": "store",
                "clearValue": [
                  0,
                  0,
                  0,
                  0
                ]
              },
              {
                "location": 2,
                "resource": "course:scene/material",
                "loadOp": "clear",
                "storeOp": "store",
                "clearValue": [
                  0,
                  0,
                  0,
                  0
                ]
              }
            ],
            "depth": {
              "resource": "course:scene/depth",
              "loadOp": "clear",
              "storeOp": "store",
              "clearValue": 1
            }
          },
          "state": {
            "blend": "opaque",
            "depth": "default",
            "rasterizer": "cullCounterClockwiseScissor"
          },
          "viewport": {
            "source": "target",
            "restore": "frame",
            "synchronizeScissor": true
          },
          "program": "course:scene/terrain",
          "sceneBindings": [
            {
              "symbol": "u_viewProjectionMatrix",
              "contract": "dawnlight:scene_binding/view_projection",
              "version": 1,
              "frequency": "view"
            },
            {
              "symbol": "u_origin",
              "contract": "dawnlight:scene_binding/terrain_origin",
              "version": 1,
              "frequency": "view"
            },
            {
              "symbol": "u_lightTable",
              "contract": "dawnlight:scene_binding/light_table",
              "version": 1,
              "frequency": "batch"
            },
            {
              "symbol": "u_texture",
              "contract": "dawnlight:scene_binding/block_texture",
              "version": 1,
              "frequency": "draw",
              "binding": 0
            }
          ],
          "drawBuffers": [
            0,
            1,
            2
          ]
        }
      ],
      "inputs": [
        "course:scene/color",
        "course:scene/depth"
      ],
      "outputs": [
        "course:scene/color",
        "course:scene/depth",
        "course:scene/normal",
        "course:scene/material"
      ]
    },
    {
      "id": "course:terrain_alpha_tested",
      "enabled": true,
      "stage": {
        "template": "dawnlight:command_list",
        "version": 1,
        "target": "world",
        "phase": "opaque",
        "ordering": {
          "after": [
            "course:terrain_opaque"
          ]
        }
      },
      "services": [],
      "programs": [],
      "commands": [
        {
          "type": "engineDraw",
          "sceneProvider": {
            "id": "dawnlight:scene_draw/terrain_alpha_tested",
            "version": 2
          },
          "targets": {
            "colors": [
              {
                "location": 0,
                "resource": "course:scene/color",
                "loadOp": "load",
                "storeOp": "store"
              },
              {
                "location": 1,
                "resource": "course:scene/normal",
                "loadOp": "load",
                "storeOp": "store"
              },
              {
                "location": 2,
                "resource": "course:scene/material",
                "loadOp": "load",
                "storeOp": "store"
              }
            ],
            "depth": {
              "resource": "course:scene/depth",
              "loadOp": "load",
              "storeOp": "store"
            }
          },
          "state": {
            "blend": "opaque",
            "depth": "default",
            "rasterizer": "cullCounterClockwiseScissor"
          },
          "viewport": {
            "source": "target",
            "restore": "frame",
            "synchronizeScissor": true
          }
        }
      ],
      "inputs": [
        "course:scene/color",
        "course:scene/depth",
        "course:scene/normal",
        "course:scene/material"
      ],
      "outputs": [
        "course:scene/color",
        "course:scene/depth",
        "course:scene/normal",
        "course:scene/material"
      ]
    },
    {
      "id": "course:terrain_transparent",
      "enabled": true,
      "stage": {
        "template": "dawnlight:command_list",
        "version": 1,
        "target": "world",
        "phase": "transparent"
      },
      "services": [],
      "programs": [],
      "commands": [
        {
          "type": "engineDraw",
          "sceneProvider": {
            "id": "dawnlight:scene_draw/terrain_transparent",
            "version": 2
          },
          "targets": {
            "colors": [
              {
                "location": 0,
                "resource": "course:scene/color",
                "loadOp": "load",
                "storeOp": "store"
              },
              {
                "location": 1,
                "resource": "course:scene/normal",
                "loadOp": "load",
                "storeOp": "store"
              },
              {
                "location": 2,
                "resource": "course:scene/material",
                "loadOp": "load",
                "storeOp": "store"
              }
            ],
            "depth": {
              "resource": "course:scene/depth",
              "loadOp": "load",
              "storeOp": "store"
            }
          },
          "state": {
            "blend": "alphaBlend",
            "depth": "default",
            "rasterizer": "cullCounterClockwiseScissor"
          },
          "viewport": {
            "source": "target",
            "restore": "frame",
            "synchronizeScissor": true
          }
        }
      ],
      "inputs": [
        "course:scene/color",
        "course:scene/depth",
        "course:scene/normal",
        "course:scene/material"
      ],
      "outputs": [
        "course:scene/color",
        "course:scene/depth",
        "course:scene/normal",
        "course:scene/material"
      ]
    },
    {
      "id": "course:terrain_sorted_translucent",
      "enabled": true,
      "stage": {
        "template": "dawnlight:command_list",
        "version": 1,
        "target": "world",
        "phase": "transparent",
        "ordering": {
          "after": [
            "course:terrain_transparent"
          ]
        }
      },
      "services": [],
      "programs": [],
      "commands": [
        {
          "type": "engineDraw",
          "sceneProvider": {
            "id": "dawnlight:scene_draw/terrain_sorted_translucent",
            "version": 2
          },
          "targets": {
            "colors": [
              {
                "location": 0,
                "resource": "course:scene/color",
                "loadOp": "load",
                "storeOp": "store"
              },
              {
                "location": 1,
                "resource": "course:scene/normal",
                "loadOp": "load",
                "storeOp": "store"
              },
              {
                "location": 2,
                "resource": "course:scene/material",
                "loadOp": "load",
                "storeOp": "store"
              }
            ],
            "depth": {
              "resource": "course:scene/depth",
              "loadOp": "load",
              "storeOp": "store"
            }
          },
          "state": {
            "blend": "alphaBlend",
            "depth": "default",
            "rasterizer": "cullCounterClockwiseScissor"
          },
          "viewport": {
            "source": "target",
            "restore": "frame",
            "synchronizeScissor": true
          }
        }
      ],
      "inputs": [
        "course:scene/color",
        "course:scene/depth",
        "course:scene/normal",
        "course:scene/material"
      ],
      "outputs": [
        "course:scene/color",
        "course:scene/depth",
        "course:scene/normal",
        "course:scene/material"
      ]
    },
    {
      "id": "course:grade",
      "enabled": true,
      "stage": {
        "template": "dawnlight:command_list",
        "version": 1,
        "target": "afterDraw",
        "phase": "afterDraw"
      },
      "services": [],
      "programs": [
        "course:post/grade"
      ],
      "commands": [
        {
          "type": "fullscreen",
          "program": "course:post/grade",
          "bindings": [
            {
              "kind": "sampler2D",
              "symbol": "u_sceneColor",
              "binding": 0,
              "resource": "course:scene/color"
            },
            {
              "kind": "sampler2D",
              "symbol": "u_sceneNormal",
              "binding": 1,
              "resource": "course:scene/normal"
            },
            {
              "kind": "sampler2D",
              "symbol": "u_sceneMaterial",
              "binding": 2,
              "resource": "course:scene/material"
            }
          ],
          "state": {
            "blend": "opaque",
            "depth": "disabled",
            "rasterizer": "cullNoneScissor"
          },
          "viewport": {
            "source": "target",
            "restore": "frame",
            "synchronizeScissor": true
          },
          "targets": {
            "colors": [
              {
                "location": 0,
                "resource": "course:post/color",
                "loadOp": "dontCare",
                "storeOp": "store"
              }
            ]
          }
        }
      ],
      "inputs": [
        "course:scene/color",
        "course:scene/normal",
        "course:scene/material"
      ],
      "outputs": [
        "course:post/color"
      ]
    },
    {
      "id": "course:present",
      "enabled": true,
      "stage": {
        "template": "dawnlight:command_list",
        "version": 1,
        "target": "afterDraw",
        "phase": "screenOutput",
        "ordering": {
          "after": [
            "course:grade"
          ]
        }
      },
      "services": [],
      "programs": [
        "course:post/present"
      ],
      "commands": [
        {
          "type": "present",
          "program": "course:post/present",
          "bindings": [
            {
              "kind": "sampler2D",
              "symbol": "u_postColor",
              "binding": 0,
              "resource": "course:post/color"
            }
          ],
          "state": {
            "blend": "opaque",
            "depth": "disabled",
            "rasterizer": "cullNoneScissor"
          },
          "viewport": {
            "source": "frame",
            "restore": "none",
            "synchronizeScissor": true
          }
        }
      ],
      "inputs": [
        "course:post/color"
      ],
      "outputs": []
    }
  ],
  "sceneCategories": [
    {
      "id": "dawnlight:scene/terrain_opaque",
      "version": 1,
      "mode": "override",
      "stage": "course:terrain_opaque"
    },
    {
      "id": "dawnlight:scene/terrain_alpha_tested",
      "version": 1,
      "mode": "builtin"
    },
    {
      "id": "dawnlight:scene/terrain_transparent",
      "version": 1,
      "mode": "builtin"
    },
    {
      "id": "dawnlight:scene/terrain_sorted_translucent",
      "version": 1,
      "mode": "builtin"
    }
  ]
}

Checks and images ​

Extract to a fresh directory, open PowerShell in that pack root and use the pinned Analyzer installed in environment setup. Expected: exit code 0 with no diagnostics; scene shows four active Terrain routes. Static checks do not compile against a real driver.

powershell
$analyzer = "C:\Tools\Dawnlight\shaderpack-analyzer.exe"
& $analyzer pack validate . --format json
& $analyzer scene explain . --include-inactive --format json
& $analyzer graph dump . --contract 2 --include-inactive
& $analyzer program interface . --program course:post/present --contract 2
& $analyzer program interface . --program course:post/grade --contract 2
& $analyzer program interface . --program course:scene/terrain --contract 2

Actual game observations

Loaded, reloaded, resized between two window sizes and returned to BuiltIn on the recorded Windows host and NVIDIA driver. All reported Programs linked; Analyzer matched the driver report and every pack-owned Program interface. The image is a single-scene observation of this source snapshot.

2625bd72b6789d7ad170c827c588944190645fdd33cbaa4d66f19ddcca6a6600

Matched driver report · Lifecycle observations

Host oct-normal bytes displayed directly; gray areas lack course opaque data.

Test host, method and limits →

Troubleshooting ​

Gray output: verify opaque writes A=1, then inspect grade bindings. Color without RT1/2 data: check output locations and drawBuffers. Missing sky/water normals reflect the coverage boundary. Auxiliary images describe opaque Terrain only and may retain surfaces obscured by later transparent drawing; they are insufficient for complete visible-surface deferred rendering. Two added RGBA8 images have a nominal 8×W×H-byte capacity, excluding driver allocation overhead.

Exercise ​

In a copy change DEBUG_VIEW from 1 to 2 for identifier visualization, then to 0 for color. Recheck interfaces and driver compilation for changed source. Explain why three named attachments do not mean every scene category writes all three.

Runtime method and evidence · Review the resource model

Next: options and settings

Author documentation · Manifest v5 / Scene ABI v2