From 2521a52ba61ec33302e537db7b2915fb8e78e0bc Mon Sep 17 00:00:00 2001 From: DaniTheSkunk <> Date: Mon, 12 Dec 2022 07:53:08 +0000 Subject: [PATCH] finished'ish refactor of pipelines --- com/danitheskunk/skunkworks/BaseGame.java | 14 +++- com/danitheskunk/skunkworks/IWindow.java | 22 +++--- .../skunkworks/backends/gl/BasePipeline.java | 9 ++- .../skunkworks/backends/gl/Pipeline2D.java | 44 ++++++++++- .../skunkworks/backends/gl/Pipeline3D.java | 43 +++++++++- .../backends/gl/RenderContext3D.java | 3 + .../skunkworks/backends/gl/Window.java | 79 ++++++------------- .../skunkworks/gfx/IPipeline.java | 7 ++ 8 files changed, 147 insertions(+), 74 deletions(-) create mode 100644 com/danitheskunk/skunkworks/gfx/IPipeline.java diff --git a/com/danitheskunk/skunkworks/BaseGame.java b/com/danitheskunk/skunkworks/BaseGame.java index 044920a..1965158 100644 --- a/com/danitheskunk/skunkworks/BaseGame.java +++ b/com/danitheskunk/skunkworks/BaseGame.java @@ -161,6 +161,7 @@ public abstract class BaseGame { if(currentFrameTime >= 1.0 / 60.0) { rootNode.tick(); update(1.0 / 60.0); + /* var rc3d = window.renderStart3D(); render3D(rc3d); window.renderFinish3D(rc3d); @@ -169,8 +170,19 @@ public abstract class BaseGame { rootNode.render(rc); render(rc); window.renderFinish(rc); - window.runScaler(); + */ + var p3d = window.getPipeline3D(); + var p2d = window.getPipeline2D(); + window.startFrame(); + p3d.startFrame(); + render3D(p3d.getRenderContext()); + p3d.finishFrame(); + p2d.startFrame(); + render(p2d.getRenderContext()); + rootNode.render(p2d.getRenderContext()); + p2d.finishFrame(); + window.finishFrame(); currentFrameTime -= 1.0 / 60.0; } else { try { diff --git a/com/danitheskunk/skunkworks/IWindow.java b/com/danitheskunk/skunkworks/IWindow.java index 2568c7f..142193b 100644 --- a/com/danitheskunk/skunkworks/IWindow.java +++ b/com/danitheskunk/skunkworks/IWindow.java @@ -1,22 +1,24 @@ package com.danitheskunk.skunkworks; -import com.danitheskunk.skunkworks.backends.gl.Gamepad; -import com.danitheskunk.skunkworks.gfx.IRenderContext; -import com.danitheskunk.skunkworks.gfx.ITexture; -import com.danitheskunk.skunkworks.gfx.Image; -import com.danitheskunk.skunkworks.gfx.NineSlice; +import com.danitheskunk.skunkworks.gfx.*; import com.danitheskunk.skunkworks.gfx.font.IFont; import com.danitheskunk.skunkworks.gfx.threedee.IRenderContext3D; import java.util.List; public interface IWindow { + void finishFrame(); + Engine getEngine(); BaseGamepad getGamepad(int id); Vec2i getMousePos(); + IPipeline getPipeline2D(); + + IPipeline getPipeline3D(); + boolean isMouseClicked(int button); boolean isMouseDown(int button); @@ -37,14 +39,6 @@ public interface IWindow { List loadTextureArray(String path, Vec2i tileSize); - void renderFinish(IRenderContext context); - - void renderFinish3D(IRenderContext3D context); - - IRenderContext renderStart(); - - IRenderContext3D renderStart3D(); - //needs to be run after rendering void runScaler(); @@ -54,5 +48,7 @@ public interface IWindow { boolean shouldClose(); + void startFrame(); + void tick(); } diff --git a/com/danitheskunk/skunkworks/backends/gl/BasePipeline.java b/com/danitheskunk/skunkworks/backends/gl/BasePipeline.java index c6e7bc4..87121d3 100644 --- a/com/danitheskunk/skunkworks/backends/gl/BasePipeline.java +++ b/com/danitheskunk/skunkworks/backends/gl/BasePipeline.java @@ -1,6 +1,6 @@ package com.danitheskunk.skunkworks.backends.gl; -public abstract class BasePipeline { +public abstract class BasePipeline { protected Program program; protected TRenderContext renderContext; @@ -8,8 +8,8 @@ public abstract class BasePipeline { this.program = program; } - public TRenderContext getRenderContext() { - return renderContext; + public TIRenderContext getRenderContext() { + return (TIRenderContext) renderContext; } public void setRenderContext(TRenderContext renderContext) { @@ -19,4 +19,7 @@ public abstract class BasePipeline { public void use() { program.use(); } + + public abstract void startFrame(); + public abstract void finishFrame(); } diff --git a/com/danitheskunk/skunkworks/backends/gl/Pipeline2D.java b/com/danitheskunk/skunkworks/backends/gl/Pipeline2D.java index 8bfe0e5..ba4c3f4 100644 --- a/com/danitheskunk/skunkworks/backends/gl/Pipeline2D.java +++ b/com/danitheskunk/skunkworks/backends/gl/Pipeline2D.java @@ -1,8 +1,13 @@ package com.danitheskunk.skunkworks.backends.gl; +import com.danitheskunk.skunkworks.Recti; import com.danitheskunk.skunkworks.Vec2i; +import com.danitheskunk.skunkworks.gfx.IPipeline; +import com.danitheskunk.skunkworks.gfx.IRenderContext; -public class Pipeline2D extends BasePipeline { +import static org.lwjgl.opengl.GL11.*; + +public class Pipeline2D extends BasePipeline implements IPipeline { private static final String fragmentSource = """ #version 450 layout(location = 1) in vec2 texCoord; @@ -32,9 +37,44 @@ public class Pipeline2D extends BasePipeline { out_texCoord = texCoord; } """; + private final TextureAtlas atlas; + private boolean debug; + private Vec2i size; + private final Framebuffer framebuffer; - public Pipeline2D(Vec2i size) { + public Pipeline2D(Vec2i size, Framebuffer framebuffer, TextureAtlas atlas) { super(new Program(vertexSource, fragmentSource)); + this.size = size; renderContext = new RenderContext(program, size); + this.atlas = atlas; + this.framebuffer = framebuffer; + debug = false; + } + + @Override + public void finishFrame() { + if(debug) { + renderContext.drawTextureRectangle(new Recti(Vec2i.ZERO, size), + atlas.getAtlasTexture(), + false + ); + } + //Framebuffer.unbind(); + } + + public void setDebug(boolean debug) { + this.debug = debug; + } + + @Override + public void startFrame() { + use(); + atlas.bind(); + framebuffer.bind(); + glViewport(0, 0, size.getX(), size.getY()); + atlas.update(); + glClear(GL_DEPTH_BUFFER_BIT); + glDepthFunc(GL_ALWAYS); + renderContext.preFrame(); } } diff --git a/com/danitheskunk/skunkworks/backends/gl/Pipeline3D.java b/com/danitheskunk/skunkworks/backends/gl/Pipeline3D.java index a403711..e30e898 100644 --- a/com/danitheskunk/skunkworks/backends/gl/Pipeline3D.java +++ b/com/danitheskunk/skunkworks/backends/gl/Pipeline3D.java @@ -1,6 +1,14 @@ package com.danitheskunk.skunkworks.backends.gl; -public class Pipeline3D extends BasePipeline { +import com.danitheskunk.skunkworks.Mat4f; +import com.danitheskunk.skunkworks.Vec2i; +import com.danitheskunk.skunkworks.gfx.IPipeline; +import com.danitheskunk.skunkworks.gfx.threedee.IRenderContext3D; + +import static org.lwjgl.opengl.GL11.*; +import static org.lwjgl.opengl.GL20.glUniformMatrix4fv; + +public class Pipeline3D extends BasePipeline implements IPipeline { private static final String fragmentSource = """ #version 450 layout(location = 1) in vec2 texCoord; @@ -26,9 +34,40 @@ public class Pipeline3D extends BasePipeline { } """; - public Pipeline3D(TextureAtlas atlas) { + private Vec2i size; + private final Framebuffer framebuffer; + private final TextureAtlas atlas; + + public Pipeline3D(Vec2i size, Framebuffer framebuffer, TextureAtlas atlas) { super(new Program(vertexSource, fragmentSource)); renderContext = new RenderContext3D(program); renderContext.setTextureAtlas(atlas); + this.size = size; + this.framebuffer = framebuffer; + this.atlas = atlas; + } + + @Override + public void finishFrame() { + //Framebuffer.unbind(); + } + + @Override + public void startFrame() { + use(); + atlas.bind(); + framebuffer.bind(); + glViewport(0, 0, size.getX(), size.getY()); + atlas.update(); + glClear(GL_DEPTH_BUFFER_BIT); + glDepthFunc(GL_LESS); + + //todo: stuff? + var projection = Mat4f.perspective(Math.PI / 2, 16.0 / 9.0, 0.1, 100.0); + glUniformMatrix4fv(program.getUniformLocation("projection"), + true, + projection.asFloatArray() + ); + renderContext.preFrame(); } } diff --git a/com/danitheskunk/skunkworks/backends/gl/RenderContext3D.java b/com/danitheskunk/skunkworks/backends/gl/RenderContext3D.java index 0df95e3..0c94a66 100644 --- a/com/danitheskunk/skunkworks/backends/gl/RenderContext3D.java +++ b/com/danitheskunk/skunkworks/backends/gl/RenderContext3D.java @@ -4,6 +4,7 @@ import com.danitheskunk.skunkworks.Vec2f; import com.danitheskunk.skunkworks.Vec3f; import com.danitheskunk.skunkworks.gfx.Color; import com.danitheskunk.skunkworks.gfx.threedee.BaseRenderContext3D; +import com.danitheskunk.skunkworks.gfx.threedee.IRenderContext3D; import com.danitheskunk.skunkworks.gfx.threedee.Mesh; import com.danitheskunk.skunkworks.gfx.threedee.Model; @@ -14,6 +15,8 @@ public class RenderContext3D extends BaseRenderContext3D { private final int tintIndex; private TextureAtlas textureAtlas; + public static final Class INTERFACE = IRenderContext3D.class; + public RenderContext3D(Program program) { texCoordIndex = program.getAttribLocation("texCoord"); tintIndex = program.getUniformLocation("tint"); diff --git a/com/danitheskunk/skunkworks/backends/gl/Window.java b/com/danitheskunk/skunkworks/backends/gl/Window.java index 7a90f8f..25d4a92 100644 --- a/com/danitheskunk/skunkworks/backends/gl/Window.java +++ b/com/danitheskunk/skunkworks/backends/gl/Window.java @@ -1,6 +1,7 @@ package com.danitheskunk.skunkworks.backends.gl; import com.danitheskunk.skunkworks.*; +import com.danitheskunk.skunkworks.gfx.IPipeline; import com.danitheskunk.skunkworks.gfx.IRenderContext; import com.danitheskunk.skunkworks.gfx.ITexture; import com.danitheskunk.skunkworks.gfx.Image; @@ -47,7 +48,6 @@ public class Window extends BaseWindow { """; private final Framebuffer framebuffer; private final Program programScaler; - private final Mat4f projection; private final Vec2i size; private final boolean[] stateMouseClicked; private final boolean[] stateMouseDown; @@ -97,10 +97,8 @@ public class Window extends BaseWindow { textureAtlas = new TextureAtlas(); programScaler = new Program(vertexSourceScaler, fragmentSourceScaler); - projection = Mat4f.perspective(Math.PI / 2, 16.0 / 9.0, 0.1, 100.0); - - pipeline2D = new Pipeline2D(size); - pipeline3D = new Pipeline3D(textureAtlas); + pipeline2D = new Pipeline2D(size, framebuffer, textureAtlas); + pipeline3D = new Pipeline3D(size, framebuffer, textureAtlas); glProgramUniform2f(programScaler.getProgram(), @@ -116,6 +114,12 @@ public class Window extends BaseWindow { System.out.println(GL11.glGetInteger(GL_MAX_TEXTURE_SIZE)); } + @Override + public void finishFrame() { + runScaler(); + glfwSwapBuffers(window); + } + @Override public BaseGamepad getGamepad(int id) { return new Gamepad(id); @@ -159,6 +163,16 @@ public class Window extends BaseWindow { ); } + @Override + public IPipeline getPipeline2D() { + return pipeline2D; + } + + @Override + public IPipeline getPipeline3D() { + return pipeline3D; + } + @Override public boolean isMouseClicked(int button) { return stateMouseClicked[button]; @@ -218,52 +232,6 @@ public class Window extends BaseWindow { } } - @Override - public void renderFinish(IRenderContext context) { - if(debug) { - context.drawTextureRectangle(new Recti(Vec2i.ZERO, size), - textureAtlas.getAtlasTexture(), - false - ); - } - } - - public void renderFinish3D(IRenderContext3D context) { - - } - - @Override - public IRenderContext renderStart() { - pipeline2D.use(); - textureAtlas.bind(); - framebuffer.bind(); - glViewport(0, 0, size.getX(), size.getY()); - textureAtlas.update(); - glClearColor(0.f, 0.f, 0.f, 0.0f); - glClear(GL_DEPTH_BUFFER_BIT); - glDepthFunc(GL_ALWAYS); - //glBegin(GL_TRIANGLES); - return pipeline2D.getRenderContext(); - } - - @Override - public IRenderContext3D renderStart3D() { - pipeline3D.use(); - textureAtlas.bind(); - framebuffer.bind(); - glViewport(0, 0, size.getX(), size.getY()); - textureAtlas.update(); - glClearColor(0.f, 0.f, 0.f, 0.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glDepthFunc(GL_LESS); - //glBegin(GL_TRIANGLES); - glUniformMatrix4fv(pipeline3D.program.getUniformLocation("projection"), - true, - projection.asFloatArray() - ); - - return pipeline3D.getRenderContext(); - } @Override public void runScaler() { @@ -345,8 +313,6 @@ public class Window extends BaseWindow { glVertex2i(tlx2, tly2); glEnd(); - - glfwSwapBuffers(window); } @Override @@ -359,6 +325,13 @@ public class Window extends BaseWindow { return shouldClose; } + @Override + public void startFrame() { + framebuffer.bind(); + glClearColor(0.f, 0.f, 0.f, 0.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + } + @Override public void tick() { //one tick per update call? glfwMakeContextCurrent(window); diff --git a/com/danitheskunk/skunkworks/gfx/IPipeline.java b/com/danitheskunk/skunkworks/gfx/IPipeline.java new file mode 100644 index 0000000..c3ada2b --- /dev/null +++ b/com/danitheskunk/skunkworks/gfx/IPipeline.java @@ -0,0 +1,7 @@ +package com.danitheskunk.skunkworks.gfx; + +public interface IPipeline { + void startFrame(); + void finishFrame(); + TIRenderContext getRenderContext(); +}