#version 450 const int MAX_LIGHTS = 4; struct Light { vec4 direction; vec4 color; }; layout(location = 0) in vec3 inPosition; layout(location = 1) in vec3 inNormal; layout(location = 2) in vec2 inUV; layout(location = 0) out vec2 fragUV; layout(location = 1) out vec3 fragNormal; layout(location = 2) out vec3 fragWorldPos; layout(location = 3) out vec3 fragLightSpacePos; // GPUDriven:模型矩阵从实例缓冲按实例索引取。 // 同一 mesh 的 N 个实体 = 1 次 drawIndexed(instanceCount=N), // draw 侧用 firstInstance 指向该 batch 在实例数组中的起始下标。 layout(std430, set = 0, binding = 1) readonly buffer ObjectBuffer { mat4 models[]; // 连续 mat4 数组(16B 对齐,std430/std140 布局一致) } objects; layout(set = 0, binding = 0) uniform UniformBufferObject { mat4 view; mat4 proj; mat4 lightSpace; vec4 ambientColor; Light lights[MAX_LIGHTS]; } ubo; void main() { mat4 model = objects.models[gl_InstanceIndex]; vec4 worldPos = model * vec4(inPosition, 1.0); gl_Position = ubo.proj * ubo.view * worldPos; fragWorldPos = worldPos.xyz; // 光空间坐标(含 Vulkan [0,1] 深度重映射,与 shadow pass 写入端共用同一矩阵) fragLightSpacePos = (ubo.lightSpace * worldPos).xyz; mat3 normalMatrix = mat3(transpose(inverse(model))); fragNormal = normalize(normalMatrix * inNormal); fragUV = inUV; }