Pbr shading

From Nebulavenus/forge-gpu · View interactive version

Pbr shading is an AI agent skill from the GitHub repository Nebulavenus/forge-gpu. Add Cook-Torrance PBR shading with GGX, Schlick-GGX, and Schlick Fresnel to an SDL GPU project alongside forge_scene.h

How to use this skill

Skill file

Add physically-based rendering to an SDL GPU project. Implements a

Cook-Torrance microfacet BRDF fragment shader that uses the same texture

bindings and uniform layout as forge_scene.h's built-in model pipeline,

enabling side-by-side comparison with Blinn-Phong.

When to use

Key API calls

Correct order

  1. Load model via forge_scene_load_model()
  2. Create PBR fragment shader from compiled bytecode
  3. Create vertex shader from scene_model_vert_spirv/dxil/msl (available

via FORGE_SCENE_IMPLEMENTATION)

  1. Create pipeline with model vertex layout using sizeof(ForgeSceneModelVertex)

for pitch and offsetof() for attribute offsets

  1. Release shaders after pipeline creation
  2. Per frame: bind custom pipeline, iterate model submeshes, push uniforms

with overridden PBR parameters, bind textures, draw

Cook-Torrance BRDF components

/* GGX Normal Distribution */
float D = alpha2 / (PI * pow(NdotH * NdotH * (alpha2 - 1.0) + 1.0, 2.0));

/* Schlick-GGX Geometry (Smith method) */
float k = (roughness + 1.0) * (roughness + 1.0) / 8.0;
float G = (NdotV / (NdotV * (1-k) + k)) * (NdotL / (NdotL * (1-k) + k));

/* Schlick Fresnel */
float3 F = F0 + (1.0 - F0) * pow(1.0 - VdotH, 5.0);

/* Full specular: D * G * F / (4 * NdotV * NdotL) */
/* Energy-conserving diffuse: (1 - F) * (1 - metallic) * albedo / PI */

Common mistakes

not a fixed 0.04 for all materials

IBL: r^2/2

Cook-Torrance denominator

energy than it receives

Reference

See Lesson 51 — PBR Shading Model

for the full implementation.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.