finished'ish refactor of pipelines

This commit is contained in:
DaniTheSkunk 2022-12-12 07:53:08 +00:00
parent baddb2cca8
commit 2521a52ba6
8 changed files with 147 additions and 74 deletions

View File

@ -161,6 +161,7 @@ public abstract class BaseGame {
if(currentFrameTime >= 1.0 / 60.0) { if(currentFrameTime >= 1.0 / 60.0) {
rootNode.tick(); rootNode.tick();
update(1.0 / 60.0); update(1.0 / 60.0);
/*
var rc3d = window.renderStart3D(); var rc3d = window.renderStart3D();
render3D(rc3d); render3D(rc3d);
window.renderFinish3D(rc3d); window.renderFinish3D(rc3d);
@ -169,8 +170,19 @@ public abstract class BaseGame {
rootNode.render(rc); rootNode.render(rc);
render(rc); render(rc);
window.renderFinish(rc); window.renderFinish(rc);
window.runScaler(); 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; currentFrameTime -= 1.0 / 60.0;
} else { } else {
try { try {

View File

@ -1,22 +1,24 @@
package com.danitheskunk.skunkworks; package com.danitheskunk.skunkworks;
import com.danitheskunk.skunkworks.backends.gl.Gamepad; import com.danitheskunk.skunkworks.gfx.*;
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.font.IFont; import com.danitheskunk.skunkworks.gfx.font.IFont;
import com.danitheskunk.skunkworks.gfx.threedee.IRenderContext3D; import com.danitheskunk.skunkworks.gfx.threedee.IRenderContext3D;
import java.util.List; import java.util.List;
public interface IWindow { public interface IWindow {
void finishFrame();
Engine getEngine(); Engine getEngine();
BaseGamepad getGamepad(int id); BaseGamepad getGamepad(int id);
Vec2i getMousePos(); Vec2i getMousePos();
IPipeline<IRenderContext> getPipeline2D();
IPipeline<IRenderContext3D> getPipeline3D();
boolean isMouseClicked(int button); boolean isMouseClicked(int button);
boolean isMouseDown(int button); boolean isMouseDown(int button);
@ -37,14 +39,6 @@ public interface IWindow {
List<ITexture> loadTextureArray(String path, Vec2i tileSize); List<ITexture> loadTextureArray(String path, Vec2i tileSize);
void renderFinish(IRenderContext context);
void renderFinish3D(IRenderContext3D context);
IRenderContext renderStart();
IRenderContext3D renderStart3D();
//needs to be run after rendering //needs to be run after rendering
void runScaler(); void runScaler();
@ -54,5 +48,7 @@ public interface IWindow {
boolean shouldClose(); boolean shouldClose();
void startFrame();
void tick(); void tick();
} }

View File

@ -1,6 +1,6 @@
package com.danitheskunk.skunkworks.backends.gl; package com.danitheskunk.skunkworks.backends.gl;
public abstract class BasePipeline<TRenderContext> { public abstract class BasePipeline<TRenderContext, TIRenderContext> {
protected Program program; protected Program program;
protected TRenderContext renderContext; protected TRenderContext renderContext;
@ -8,8 +8,8 @@ public abstract class BasePipeline<TRenderContext> {
this.program = program; this.program = program;
} }
public TRenderContext getRenderContext() { public TIRenderContext getRenderContext() {
return renderContext; return (TIRenderContext) renderContext;
} }
public void setRenderContext(TRenderContext renderContext) { public void setRenderContext(TRenderContext renderContext) {
@ -19,4 +19,7 @@ public abstract class BasePipeline<TRenderContext> {
public void use() { public void use() {
program.use(); program.use();
} }
public abstract void startFrame();
public abstract void finishFrame();
} }

View File

@ -1,8 +1,13 @@
package com.danitheskunk.skunkworks.backends.gl; package com.danitheskunk.skunkworks.backends.gl;
import com.danitheskunk.skunkworks.Recti;
import com.danitheskunk.skunkworks.Vec2i; import com.danitheskunk.skunkworks.Vec2i;
import com.danitheskunk.skunkworks.gfx.IPipeline;
import com.danitheskunk.skunkworks.gfx.IRenderContext;
public class Pipeline2D extends BasePipeline<RenderContext> { import static org.lwjgl.opengl.GL11.*;
public class Pipeline2D extends BasePipeline<RenderContext, IRenderContext> implements IPipeline<IRenderContext> {
private static final String fragmentSource = """ private static final String fragmentSource = """
#version 450 #version 450
layout(location = 1) in vec2 texCoord; layout(location = 1) in vec2 texCoord;
@ -32,9 +37,44 @@ public class Pipeline2D extends BasePipeline<RenderContext> {
out_texCoord = texCoord; 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)); super(new Program(vertexSource, fragmentSource));
this.size = size;
renderContext = new RenderContext(program, 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();
} }
} }

View File

@ -1,6 +1,14 @@
package com.danitheskunk.skunkworks.backends.gl; package com.danitheskunk.skunkworks.backends.gl;
public class Pipeline3D extends BasePipeline<RenderContext3D> { 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<RenderContext3D, IRenderContext3D> implements IPipeline<IRenderContext3D> {
private static final String fragmentSource = """ private static final String fragmentSource = """
#version 450 #version 450
layout(location = 1) in vec2 texCoord; layout(location = 1) in vec2 texCoord;
@ -26,9 +34,40 @@ public class Pipeline3D extends BasePipeline<RenderContext3D> {
} }
"""; """;
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)); super(new Program(vertexSource, fragmentSource));
renderContext = new RenderContext3D(program); renderContext = new RenderContext3D(program);
renderContext.setTextureAtlas(atlas); 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();
} }
} }

View File

@ -4,6 +4,7 @@ import com.danitheskunk.skunkworks.Vec2f;
import com.danitheskunk.skunkworks.Vec3f; import com.danitheskunk.skunkworks.Vec3f;
import com.danitheskunk.skunkworks.gfx.Color; import com.danitheskunk.skunkworks.gfx.Color;
import com.danitheskunk.skunkworks.gfx.threedee.BaseRenderContext3D; 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.Mesh;
import com.danitheskunk.skunkworks.gfx.threedee.Model; import com.danitheskunk.skunkworks.gfx.threedee.Model;
@ -14,6 +15,8 @@ public class RenderContext3D extends BaseRenderContext3D {
private final int tintIndex; private final int tintIndex;
private TextureAtlas textureAtlas; private TextureAtlas textureAtlas;
public static final Class INTERFACE = IRenderContext3D.class;
public RenderContext3D(Program program) { public RenderContext3D(Program program) {
texCoordIndex = program.getAttribLocation("texCoord"); texCoordIndex = program.getAttribLocation("texCoord");
tintIndex = program.getUniformLocation("tint"); tintIndex = program.getUniformLocation("tint");

View File

@ -1,6 +1,7 @@
package com.danitheskunk.skunkworks.backends.gl; package com.danitheskunk.skunkworks.backends.gl;
import com.danitheskunk.skunkworks.*; import com.danitheskunk.skunkworks.*;
import com.danitheskunk.skunkworks.gfx.IPipeline;
import com.danitheskunk.skunkworks.gfx.IRenderContext; import com.danitheskunk.skunkworks.gfx.IRenderContext;
import com.danitheskunk.skunkworks.gfx.ITexture; import com.danitheskunk.skunkworks.gfx.ITexture;
import com.danitheskunk.skunkworks.gfx.Image; import com.danitheskunk.skunkworks.gfx.Image;
@ -47,7 +48,6 @@ public class Window extends BaseWindow {
"""; """;
private final Framebuffer framebuffer; private final Framebuffer framebuffer;
private final Program programScaler; private final Program programScaler;
private final Mat4f projection;
private final Vec2i size; private final Vec2i size;
private final boolean[] stateMouseClicked; private final boolean[] stateMouseClicked;
private final boolean[] stateMouseDown; private final boolean[] stateMouseDown;
@ -97,10 +97,8 @@ public class Window extends BaseWindow {
textureAtlas = new TextureAtlas(); textureAtlas = new TextureAtlas();
programScaler = new Program(vertexSourceScaler, fragmentSourceScaler); programScaler = new Program(vertexSourceScaler, fragmentSourceScaler);
projection = Mat4f.perspective(Math.PI / 2, 16.0 / 9.0, 0.1, 100.0); pipeline2D = new Pipeline2D(size, framebuffer, textureAtlas);
pipeline3D = new Pipeline3D(size, framebuffer, textureAtlas);
pipeline2D = new Pipeline2D(size);
pipeline3D = new Pipeline3D(textureAtlas);
glProgramUniform2f(programScaler.getProgram(), glProgramUniform2f(programScaler.getProgram(),
@ -116,6 +114,12 @@ public class Window extends BaseWindow {
System.out.println(GL11.glGetInteger(GL_MAX_TEXTURE_SIZE)); System.out.println(GL11.glGetInteger(GL_MAX_TEXTURE_SIZE));
} }
@Override
public void finishFrame() {
runScaler();
glfwSwapBuffers(window);
}
@Override @Override
public BaseGamepad getGamepad(int id) { public BaseGamepad getGamepad(int id) {
return new Gamepad(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 @Override
public boolean isMouseClicked(int button) { public boolean isMouseClicked(int button) {
return stateMouseClicked[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 @Override
public void runScaler() { public void runScaler() {
@ -345,8 +313,6 @@ public class Window extends BaseWindow {
glVertex2i(tlx2, tly2); glVertex2i(tlx2, tly2);
glEnd(); glEnd();
glfwSwapBuffers(window);
} }
@Override @Override
@ -359,6 +325,13 @@ public class Window extends BaseWindow {
return shouldClose; 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 @Override
public void tick() { //one tick per update call? public void tick() { //one tick per update call?
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);

View File

@ -0,0 +1,7 @@
package com.danitheskunk.skunkworks.gfx;
public interface IPipeline<TIRenderContext> {
void startFrame();
void finishFrame();
TIRenderContext getRenderContext();
}