Why the depth texture is broken and sheared into rows like this? This cube mesh is in the middle of the screen but half the rows are shifted left, half to the right.
I used this guide (using C# instead of gdscript):
https://1hue.github.io/depth-texture-scene-data-in-compute-shader
Minimal code below.
#[compute]
#version 450
// Input depth texture
layout(set = 0, binding = 0) uniform sampler2D depth_texture;
// Output storage image
layout(set = 0, binding = 1, r32f) uniform writeonly image2D output_image;
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
void main() {
ivec2 pos = ivec2(gl_GlobalInvocationID.xy);
ivec2 size = imageSize(output_image);
// Bounds check
if (pos.x >= size.x || pos.y >= size.y) {
return;
}
// Calculate normalized UV coordinates
vec2 uv = (vec2(pos) + vec2(0.5)) / vec2(size);
// Sample depth texture
float depth = texture(depth_texture, uv).r;
// Write to output image
imageStore(output_image, pos, vec4(depth, 0.0, 0.0, 0.0));
}
Godot is a fantastic tool. It is also a gateway to Alice's Adventures in Shaderland. Let's explore compute shaders and depth textures.