Skip to content

Commit

Permalink
Save some rendering time on old planets by integrating depth textures
Browse files Browse the repository at this point in the history
  • Loading branch information
GlennFolker committed Dec 13, 2024
1 parent cf13ed8 commit d90d9a1
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 148 deletions.
17 changes: 13 additions & 4 deletions assets/shaders/confictura/depth-atmosphere.frag
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ in vec3 v_position;

out vec4 fragColor;

uniform mat4 u_invProj;
uniform vec3 u_camPos;
uniform vec3 u_relCamPos;
uniform vec2 u_camRange;
uniform vec2 u_depthRange;
uniform vec3 u_center;
uniform vec3 u_light;
uniform vec3 u_color;
Expand Down Expand Up @@ -98,8 +99,16 @@ vec3 inScatter(vec3 eye, vec3 ray, vec2 bound, vec3 light){
return sum * (peak * u_color * rayleighPhase(cc) + flare * miePhase(gm, c, cc)) * intensity;
}

float unpack(vec4 pack){
return dot(pack, 1.0 / vec4(1.0, 255.0, 65025.0, 16581375.0)) * u_camRange.y + u_camRange.x;
float depth(vec2 uv){
float depth = texture(u_topology, uv).r;

float x_ndc = uv.x * 2.0 - 1.0;
float y_ndc = uv.y * 2.0 - 1.0;
float z_ndc = depth * 2.0 - 1.0;
vec4 clip = vec4(x_ndc, y_ndc, z_ndc, 1.0);

vec4 view = u_invProj * clip;
return length(view.xyz / view.w);
}

void main(){
Expand All @@ -108,7 +117,7 @@ void main(){
vec3 normal = normalize(v_position - u_center);

vec2 bound = intersect(eye, ray, u_outerRadius);
bound.y = min(bound.y, unpack(texture(u_topology, gl_FragCoord.xy / u_viewport)));
bound.y = min(bound.y, depth(gl_FragCoord.xy / u_viewport));

fragColor = vec4(inScatter(eye, ray, bound, u_light), 1.0);
}
4 changes: 2 additions & 2 deletions assets/shaders/confictura/depth-atmosphere.vert
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ in vec3 a_position;

out vec3 v_position;

uniform mat4 u_proj;
uniform mat4 u_projView;
uniform mat4 u_trans;
uniform float u_outerRadius;

void main(){
vec4 pos = u_trans * vec4(a_position * u_outerRadius, 1.0);

v_position = pos.xyz;
gl_Position = u_proj * pos;
gl_Position = u_projView * pos;
}
20 changes: 0 additions & 20 deletions assets/shaders/confictura/depth.frag

This file was deleted.

16 changes: 0 additions & 16 deletions assets/shaders/confictura/depth.vert

This file was deleted.

21 changes: 15 additions & 6 deletions assets/shaders/confictura/portal-forcefield.frag
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ in vec4 v_color;

out vec4 fragColor;

uniform mat4 u_proj;
uniform mat4 u_projView;
uniform mat4 u_invProj;
uniform float u_radius;

uniform vec3 u_camPos;
uniform vec3 u_relCamPos;
uniform vec2 u_camRange;
uniform vec2 u_depthRange;
uniform vec3 u_center;
uniform vec3 u_light;

Expand Down Expand Up @@ -183,8 +184,16 @@ vec2 intersect(vec3 ray_origin, vec3 ray_dir, float radius){
return vec2(near, far);
}

float unpack(vec4 pack){
return dot(pack, 1.0 / vec4(1.0, 255.0, 65025.0, 16581375.0)) * u_camRange.y + u_camRange.x;
float depth(vec2 uv){
float depth = texture(u_topology, uv).r;

float x_ndc = uv.x * 2.0 - 1.0;
float y_ndc = uv.y * 2.0 - 1.0;
float z_ndc = depth * 2.0 - 1.0;
vec4 clip = vec4(x_ndc, y_ndc, z_ndc, 1.0);

vec4 view = u_invProj * clip;
return length(view.xyz / view.w);
}

void main(){
Expand All @@ -193,7 +202,7 @@ void main(){
vec3 normal = normalize(v_position - u_center);

vec2 intersect = intersect(eye, ray, u_radius - 0.01);
float topo = unpack(texture(u_topology, gl_FragCoord.xy / u_viewport));
float topo = depth(gl_FragCoord.xy / u_viewport);

float dst = (intersect.y - intersect.x) / ((u_radius - 0.01) * 2.0);
float noise = octNoise(vec3(eye + ray * intersect.x), 4, 1.8, 1.8, 0.67);
Expand Down Expand Up @@ -228,6 +237,6 @@ void main(){
fragColor = vec4(baseColor + outlineColor, 1.0);

float far = gl_DepthRange.far, near = gl_DepthRange.near;
vec4 clip = u_proj * vec4(u_camPos + ray * intersect.x, 1.0);
vec4 clip = u_projView * vec4(u_camPos + ray * intersect.x, 1.0);
gl_FragDepth = (((far - near) * (clip.z / clip.w)) + near + far) / 2.0;
}
4 changes: 2 additions & 2 deletions assets/shaders/confictura/portal-forcefield.vert
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ out vec3 v_position;
out float v_progress;
out vec4 v_color;

uniform mat4 u_proj;
uniform mat4 u_projView;
uniform mat4 u_trans;
uniform float u_radius;

Expand All @@ -19,5 +19,5 @@ void main(){
v_progress = a_normal.x;
v_color = a_color;

gl_Position = u_proj * pos;
gl_Position = u_projView * pos;
}
2 changes: 0 additions & 2 deletions src/confictura/graphics/CShaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
public final class CShaders{
public static DepthScreenspaceShader depthScreenspace;
public static DepthShader depth;
public static DepthAtmosphereShader depthAtmosphere;
public static PortalForcefieldShader portalForcefield;
public static EmissiveBatchShader emissiveBatch;
Expand All @@ -32,7 +31,6 @@ public static void load(){
Shader.prependVertexCode = Shader.prependFragmentCode = "";

depthScreenspace = new DepthScreenspaceShader();
depth = new DepthShader();
depthAtmosphere = new DepthAtmosphereShader();
portalForcefield = new PortalForcefieldShader();
emissiveBatch = new EmissiveBatchShader();
Expand Down
7 changes: 4 additions & 3 deletions src/confictura/graphics/shaders/DepthAtmosphereShader.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,21 @@ public DepthAtmosphereShader(){

@Override
public void apply(){
setUniformMatrix4("u_proj", camera.combined.val);
setUniformMatrix4("u_projView", camera.combined.val);
setUniformMatrix4("u_invProj", mat.set(camera.projection).inv().val);
setUniformMatrix4("u_trans", planet.getTransform(mat).val);

setUniformf("u_camPos", camera.position);
setUniformf("u_relCamPos", Tmp.v31.set(camera.position).sub(planet.position));
setUniformf("u_camRange", camera.near, camera.far - camera.near);
setUniformf("u_depthRange", camera.near, camera.far);
setUniformf("u_center", planet.position);
setUniformf("u_light", planet.getLightNormal());
setUniformf("u_color", planet.atmosphereColor.r, planet.atmosphereColor.g, planet.atmosphereColor.b);

setUniformf("u_innerRadius", planet.radius + planet.atmosphereRadIn);
setUniformf("u_outerRadius", planet.radius + planet.atmosphereRadOut);

planet.depthBuffer.getTexture().bind(0);
planet.buffer.getDepthTexture().bind(0);
setUniformi("u_topology", 0);
setUniformf("u_viewport", graphics.getWidth(), graphics.getHeight());
}
Expand Down
7 changes: 4 additions & 3 deletions src/confictura/graphics/shaders/PortalForcefieldShader.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@ public PortalForcefieldShader(){

@Override
public void apply(){
setUniformMatrix4("u_proj", camera.combined.val);
setUniformMatrix4("u_projView", camera.combined.val);
setUniformMatrix4("u_invProj", mat.set(camera.projection).inv().val);
setUniformMatrix4("u_trans", planet.getTransform(mat).rotate(axis, Vec3.Y.angle(axis)).val);
setUniformf("u_radius", planet.forcefieldRadius);

setUniformf("u_camPos", camera.position);
setUniformf("u_relCamPos", Tmp.v31.set(camera.position).sub(planet.position));
setUniformf("u_camRange", camera.near, camera.far - camera.near);
setUniformf("u_depthRange", camera.near, camera.far);
setUniformf("u_center", planet.position);
setUniformf("u_light", Tmp.v31.set(planet.position).sub(planet.solarSystem.position).nor());

setUniformf("u_time", Time.globalTime / 60f);
setUniformf("u_baseColor", planet.atmosphereColor);

planet.depthBuffer.getTexture().bind(0);
planet.buffer.getDepthTexture().bind(0);
setUniformi("u_topology", 0);
setUniformf("u_viewport", graphics.getWidth(), graphics.getHeight());
}
Expand Down
39 changes: 3 additions & 36 deletions src/confictura/world/celestial/AtmospherePlanet.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
* @author GlFolker
*/
public class AtmospherePlanet extends Planet{
public @Nullable FrameBuffer depthBuffer;
public @Nullable CFrameBuffer buffer;

public AtmospherePlanet(String name, Planet parent, float radius){
Expand All @@ -35,12 +34,7 @@ public AtmospherePlanet(String name, Planet parent, float radius, int sectorSize
@Override
public void load(){
super.load();
if(depthBuffer == null){
depthBuffer = new FrameBuffer(graphics.getWidth(), graphics.getHeight(), true);
depthBuffer.getTexture().setFilter(TextureFilter.nearest);
}

if(buffer == null){
if(!headless && buffer == null){
buffer = new CFrameBuffer(2, 2, true);
buffer.getTexture().setFilter(TextureFilter.nearest);
}
Expand Down Expand Up @@ -75,7 +69,7 @@ public AtmosphereHexMesh(int divisions){

@Override
public void render(PlanetParams params, Mat3D projection, Mat3D transform){
/*buffer.resize(graphics.getWidth(), graphics.getHeight());
buffer.resize(graphics.getWidth(), graphics.getHeight());
buffer.begin(Color.clear);

var shader = Shaders.planet;
Expand All @@ -92,34 +86,7 @@ public void render(PlanetParams params, Mat3D projection, Mat3D transform){

var blit = CShaders.depthScreenspace;
blit.buffer = buffer;
Draw.blit(blit);*/

if(params.alwaysDrawAtmosphere || settings.getBool("atmosphere")){
var depth = CShaders.depth;
depthBuffer.resize(graphics.getWidth(), graphics.getHeight());
depthBuffer.begin(Tmp.c1.set(0xffffff00));
Blending.disabled.apply();

depth.camera = renderer.planets.cam;
depth.bind();
depth.setUniformMatrix4("u_proj", renderer.planets.cam.combined.val);
depth.setUniformMatrix4("u_trans", transform.val);
depth.apply();
mesh.render(depth, Gl.triangles);

Blending.normal.apply();
depthBuffer.end();
}

var shader = Shaders.planet;
shader.planet = AtmospherePlanet.this;
shader.lightDir.set(solarSystem.position).sub(position).rotate(Vec3.Y, getRotation()).nor();
shader.ambientColor.set(solarSystem.lightColor);
shader.bind();
shader.setUniformMatrix4("u_proj", renderer.planets.cam.combined.val);
shader.setUniformMatrix4("u_trans", transform.val);
shader.apply();
mesh.render(shader, Gl.triangles);
Draw.blit(blit);
}
}
}
45 changes: 23 additions & 22 deletions src/confictura/world/celestial/BlackHole.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,32 @@ public BlackHole(String name, float radius){
@Override
public void load(){
super.load();
if(mesh == null) mesh = MeshBuilder.buildIcosphere(3, radius);

if(skybox == null){
var base = "skyboxes/confictura/megalith/";
skybox = new Cubemap(
tree.get(base + "right.png"),
tree.get(base + "left.png"),
tree.get(base + "top.png"),
tree.get(base + "bottom.png"),
tree.get(base + "front.png"),
tree.get(base + "back.png")
);
}
if(!headless){
if(mesh == null) mesh = MeshBuilder.buildIcosphere(3, radius);
if(skybox == null){
var base = "skyboxes/confictura/megalith/";
skybox = new Cubemap(
tree.get(base + "right.png"),
tree.get(base + "left.png"),
tree.get(base + "top.png"),
tree.get(base + "bottom.png"),
tree.get(base + "front.png"),
tree.get(base + "back.png")
);
}

if(pov == null){
pov = new CFrameBufferCubemap(2, 2, true);
pov.getTexture().setFilter(TextureFilter.nearest);
}
if(pov == null){
pov = new CFrameBufferCubemap(2, 2, true);
pov.getTexture().setFilter(TextureFilter.nearest);
}

if(orbit == null){
orbit = new CFrameBuffer(2, 2, true);
orbit.getTexture().setFilter(TextureFilter.nearest);
}
if(orbit == null){
orbit = new CFrameBuffer(2, 2, true);
orbit.getTexture().setFilter(TextureFilter.nearest);
}

if(orbitRef == null) orbitRef = new CFrameBuffer(2, 2, true);
if(orbitRef == null) orbitRef = new CFrameBuffer(2, 2, true);
}
}

@Override
Expand Down
Loading

0 comments on commit d90d9a1

Please sign in to comment.