• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

3D [GMS 1.4] Get surface depth buffer

Hello guys. I am trying to implement some shadow mapping in my game using GMS 1.4.
To optimize things i need to disable fragment shader and sample depth buffer only (as a texture).
So,
1. Can i get surface's depth buffer somehow?
2. Can i disable fragment shader (but it is not so important, because i can just create a dummy fragment shader)

Thanks.
 

rytan451

Member
Here's a quick shader I whipped up to write a depth buffer.

C:
// Vertex shader
attribute vec3 in_Position;
void main() {
	gl_Position = gm_Matrices[MATRIX_WORLD_VIEW_PROJECTION] * vec4(in_Position, 1.);
}

// Fragment shader
void main() {
	gl_FragColor = fract(vec4(gl_FragCoord.z) * vec4(1., 256., 65536., 16777216.));
}

// Compare operator (returns true if lval > rval)
bool zbuffGreaterThan(vec4 lval, vec4 rval) {
	return (lval.x > rval.x) || (lval.x == rval.x && lval.y > rval.y) || (lval.xy == rval.xy && lval.z > rval.z) || (lval.xyz == rval.xyz && lval.w > rval.w);
}
 

sp202

Member
@rytan451 That's not necessary, surfaces do have working depth buffers, they just can't be read from, hence depth has to be encoded using colour instead.
 

Binsk

Member
Thats what i need i guess. But how??)
In GM it is a bit more complicated. You need to do either an extra pass to render the depth to a surface as your buffer (via custom fragment shader) or use MRTs to render your depth alongside your regular render.

GameMaker only has RGBA8 surfaces, however, so for precision you have to split your 32bit or 24bit depth data into separate 8 bit chanels when writing and re-combine when reading. All this happens in your fragment shader.
 
Top