More steps towards actually using the new loader

This commit is contained in:
Jozufozu 2021-08-05 12:16:17 -07:00
parent 9e699b7715
commit a35c7150c2
8 changed files with 139 additions and 25 deletions

View file

@ -3,15 +3,17 @@ package com.jozufozu.flywheel.backend.gl.shader;
import org.lwjgl.opengl.GL20;
public enum ShaderType {
VERTEX("vertex", GL20.GL_VERTEX_SHADER),
FRAGMENT("fragment", GL20.GL_FRAGMENT_SHADER),
VERTEX("vertex", "VERTEX_SHADER", GL20.GL_VERTEX_SHADER),
FRAGMENT("fragment", "FRAGMENT_SHADER", GL20.GL_FRAGMENT_SHADER),
;
public final String name;
public final String define;
public final int glEnum;
ShaderType(String name, int glEnum) {
ShaderType(String name, String define, int glEnum) {
this.name = name;
this.define = define;
this.glEnum = glEnum;
}
}

View file

@ -0,0 +1,18 @@
package com.jozufozu.flywheel.backend.pipeline;
public enum GLSLVersion {
V110(110),
V120(120),
;
public final int version;
GLSLVersion(int version) {
this.version = version;
}
@Override
public String toString() {
return Integer.toString(version);
}
}

View file

@ -0,0 +1,39 @@
package com.jozufozu.flywheel.backend.pipeline;
import org.lwjgl.opengl.GL20;
import com.jozufozu.flywheel.backend.Backend;
import com.jozufozu.flywheel.backend.gl.GlObject;
import com.jozufozu.flywheel.backend.gl.shader.ShaderType;
import com.jozufozu.flywheel.backend.gl.versioned.GlCompat;
public class GlShader extends GlObject {
public GlShader(ShaderType type, CharSequence source) {
int handle = GL20.glCreateShader(type.glEnum);
GlCompat.safeShaderSource(handle, source);
GL20.glCompileShader(handle);
String log = GL20.glGetShaderInfoLog(handle);
if (!log.isEmpty()) {
Backend.log.error("Shader compilation log for " + "DUBUG"+ ": " + log);
Backend.log.error(source);
}
//Backend.log.debug(shader.printSource());
if (GL20.glGetShaderi(handle, GL20.GL_COMPILE_STATUS) != GL20.GL_TRUE) {
throw new RuntimeException("Could not compile " + "DEBUG" + ". See log for details.");
}
setHandle(handle);
}
@Override
protected void deleteInternal(int handle) {
}
}

View file

@ -0,0 +1,31 @@
package com.jozufozu.flywheel.backend.pipeline;
import com.jozufozu.flywheel.backend.gl.shader.ShaderType;
public class Shader {
private final GLSLVersion version;
private final CharSequence source;
public Shader(GLSLVersion version, CharSequence source) {
this.version = version;
this.source = source;
}
public GlShader create(ShaderType type) {
StringBuilder source = new StringBuilder();
source.append("#version ")
.append(version.version)
.append('\n');
source.append("#define ")
.append(type.define)
.append('\n');
source.append(this.source);
return new GlShader(type, source);
}
}

View file

@ -37,6 +37,7 @@ public class SourceFile {
private final ShaderSources parent;
private final String source;
private final CharSequence elided;
private final ImmutableList<String> lines;
private final IntList lineStarts;
@ -62,12 +63,18 @@ public class SourceFile {
this.includes = parseIncludes();
this.functions = parseFunctions();
this.structs = parseStructs();
this.elided = createElidedSource();
}
public String getSource() {
return source;
}
public CharSequence getElidedSource() {
return elided;
}
public ShaderSources getParent() {
return parent;
}
@ -119,26 +126,20 @@ public class SourceFile {
return builder.toString();
}
private CharSequence elided = null;
private CharSequence createElidedSource() {
StringBuilder out = new StringBuilder();
public CharSequence getElidedSource() {
if (elided == null) {
StringBuilder out = new StringBuilder();
int lastEnd = 0;
int lastEnd = 0;
for (Span elision : elisions) {
out.append(source, lastEnd, elision.getStartPos());
for (Span elision : elisions) {
out.append(source, lastEnd, elision.getStartPos());
lastEnd = elision.getEndPos();
}
out.append(source, lastEnd, source.length());
elided = out.toString();
lastEnd = elision.getEndPos();
}
return elided;
out.append(source, lastEnd, source.length());
return out;
}
/**

View file

@ -5,6 +5,8 @@ import java.util.List;
import javax.annotation.Nullable;
import com.jozufozu.flywheel.backend.ShaderSources;
import com.jozufozu.flywheel.backend.gl.shader.GlProgram;
import com.jozufozu.flywheel.backend.loading.Program;
import com.jozufozu.flywheel.core.shader.IMultiProgram;
import com.jozufozu.flywheel.core.shader.WorldProgram;
import com.jozufozu.flywheel.core.shader.spec.ProgramSpec;
@ -34,7 +36,9 @@ public class WorldShaderPipeline<P extends WorldProgram> {
builder.include(file);
builder.build();
CharSequence output = builder.build();
//Program program = new Program()
return null;
}

View file

@ -1,6 +1,7 @@
package com.jozufozu.flywheel.core.shader;
import static org.lwjgl.opengl.GL20.glUniform1f;
import static org.lwjgl.opengl.GL20.glUniform2f;
import static org.lwjgl.opengl.GL20.glUniform3f;
import java.util.List;
@ -9,21 +10,21 @@ import com.jozufozu.flywheel.backend.loading.Program;
import com.jozufozu.flywheel.core.shader.extension.IProgramExtension;
import com.jozufozu.flywheel.util.AnimationTickHolder;
import net.minecraft.client.MainWindow;
import net.minecraft.client.Minecraft;
import net.minecraft.util.math.vector.Matrix4f;
public class WorldProgram extends ExtensibleGlProgram {
protected final int uTime;
protected final int uViewProjection;
protected final int uCameraPos;
protected final int uTime = getUniformLocation("uTime");
protected final int uViewProjection = getUniformLocation("uViewProjection");
protected final int uCameraPos = getUniformLocation("uCameraPos");
protected final int uWindowSize = getUniformLocation("uWindowSize");
protected int uBlockAtlas;
protected int uLightMap;
public WorldProgram(Program program, List<IProgramExtension> extensions) {
super(program, extensions);
uTime = getUniformLocation("uTime");
uViewProjection = getUniformLocation("uViewProjection");
uCameraPos = getUniformLocation("uCameraPos");
super.bind();
registerSamplers();
@ -36,14 +37,30 @@ public class WorldProgram extends ExtensibleGlProgram {
}
public void uploadViewProjection(Matrix4f viewProjection) {
if (uViewProjection < 0) return;
uploadMatrixUniform(uViewProjection, viewProjection);
}
public void uploadWindowSize() {
if (uWindowSize < 0) return;
MainWindow window = Minecraft.getInstance().getWindow();
int height = window.getScreenHeight();
int width = window.getScreenWidth();
glUniform2f(uWindowSize, width, height);
}
public void uploadCameraPos(double camX, double camY, double camZ) {
if (uCameraPos < 0) return;
glUniform3f(uCameraPos, (float) camX, (float) camY, (float) camZ);
}
public void uploadTime(float renderTime) {
if (uTime < 0) return;
glUniform1f(uTime, renderTime);
}
@ -51,6 +68,7 @@ public class WorldProgram extends ExtensibleGlProgram {
public void bind() {
super.bind();
uploadWindowSize();
uploadTime(AnimationTickHolder.getRenderTime());
}
}

View file

@ -3,6 +3,7 @@
uniform sampler2D uBlockAtlas;
uniform sampler2D uLightMap;
uniform vec2 uWindowSize;
vec4 FLWBlockTexture(vec2 texCoords) {
return texture2D(uBlockAtlas, texCoords);