Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ dependencies {

api("com.github.GTNewHorizons:AE2FluidCraft-Rework:1.5.95-gtnh:dev")
api("com.github.GTNewHorizons:Applied-Energistics-2-Unofficial:rv3-beta-999-GTNH:dev")
implementation("com.github.GTNewHorizons:GTNHLib:0.11.43:dev") {transitive = false}
implementation("com.github.GTNewHorizons:GTNHLib:0.11.54:dev") {transitive = false}

compileOnly("com.github.GTNewHorizons:Angelica:2.1.52:api") {transitive = false}
compileOnly("com.github.GTNewHorizons:Angelica:2.2.36:api") {transitive = false}
compileOnly("com.github.GTNewHorizons:Avaritiaddons:1.9.4-GTNH:dev") {transitive = false}
compileOnly("com.github.GTNewHorizons:BloodMagic:1.9.4:dev") {transitive = false}
compileOnly("com.github.GTNewHorizons:BuildCraft:7.1.61:dev") {transitive = false}
Expand Down Expand Up @@ -55,4 +55,5 @@ dependencies {
testImplementation("org.mockito:mockito-all:1.10.19")
testImplementation("org.scalatest:scalatest_2.11:2.2.6")
testImplementation("junit:junit:4.13.2")
testRuntimeOnly("it.unimi.dsi:fastutil:8.5.18")
}
3 changes: 2 additions & 1 deletion src/main/java/li/cil/oc/api/driver/item/UpgradeRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public interface UpgradeRenderer {
*
* @param stack the item stack of the upgrade to render.
* @param robot the robot the upgrade is rendered on.
* @param availableMountPoints the mount points available for rendering in.
* @param availableMountPoints the mount points available for rendering in;
* reused between calls, do not keep a reference.
* @return the mount point to reserve for the upgrade.
*/
String computePreferredMountPoint(ItemStack stack, Robot robot, Set<String> availableMountPoints);
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/li/cil/oc/client/renderer/glsl/DrawUniforms.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package li.cil.oc.client.renderer.glsl;

import java.nio.FloatBuffer;

import org.joml.Matrix3f;
import org.joml.Matrix4f;
import org.joml.Matrix4fc;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;

final class DrawUniforms {

private static final int LIGHTS = 2;

private static final Matrix4f projection = new Matrix4f();
private static final Matrix4f modelView = new Matrix4f();
private static final Matrix4f mvp = new Matrix4f();
private static final Matrix3f normal = new Matrix3f();

private static final FloatBuffer scratch = BufferUtils.createFloatBuffer(16);
private static final FloatBuffer lights = BufferUtils.createFloatBuffer(LIGHTS * 3);

private DrawUniforms() {}

static void uploadMVP(int location, Matrix4fc model) {
scratch.clear();
GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, scratch);
projection.set(scratch);
scratch.clear();
GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, scratch);
modelView.set(scratch)
.mul(model);
upload(location, projection.mul(modelView, mvp));
}

static void uploadLit(int mvpLocation, int normalLocation, int lightDirLocation, int lightDiffuseLocation,
Matrix4fc model) {
uploadMVP(mvpLocation, model);

scratch.clear();
modelView.normal(normal)
.get(scratch);
scratch.limit(9);
GL20.glUniformMatrix3(normalLocation, false, scratch);

uploadLight(lightDirLocation, GL11.GL_POSITION);
uploadLight(lightDiffuseLocation, GL11.GL_DIFFUSE);
}

private static void uploadLight(int location, int parameter) {
lights.clear();
for (int i = 0; i < LIGHTS; i++) {
scratch.clear();
GL11.glGetLight(GL11.GL_LIGHT0 + i, parameter, scratch);
lights.put(scratch.get(0))
.put(scratch.get(1))
.put(scratch.get(2));
}
lights.flip();
GL20.glUniform3(location, lights);
}

static void upload(int location, Matrix4fc matrix) {
scratch.clear();
matrix.get(scratch);
GL20.glUniformMatrix4(location, false, scratch);
}
}
43 changes: 43 additions & 0 deletions src/main/java/li/cil/oc/client/renderer/glsl/GlslDialect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package li.cil.oc.client.renderer.glsl;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.lwjgl.opengl.ContextCapabilities;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL32;
import org.lwjgl.opengl.GLContext;

// GLSL330 sources must stay free of compat builtins and of angelica_*/iris_* uniform names
public enum GlslDialect {

GLSL120("#version 120\n"),
GLSL330("#version 330 core\n" + "#define OC_CORE\n");

static final Logger LOG = LogManager.getLogger("OpenComputers");

private static GlslDialect preferred;

private final String preamble;

GlslDialect(String preamble) {
this.preamble = preamble;
}

public String source(String defines, String prelude, String body) {
return preamble + defines + prelude + body;
}

public static void init() {
if (preferred != null) return;
final ContextCapabilities capabilities = GLContext.getCapabilities();
final boolean coreContext = capabilities.OpenGL32
&& (GL11.glGetInteger(GL32.GL_CONTEXT_PROFILE_MASK) & GL32.GL_CONTEXT_CORE_PROFILE_BIT) != 0;
preferred = coreContext ? GLSL330 : GLSL120;
LOG.info("Preferred GLSL dialect: {}", preferred);
}

public static GlslDialect preferred() {
if (preferred == null) throw new IllegalStateException("GlslDialect.init() has not run");
return preferred;
}
}
73 changes: 73 additions & 0 deletions src/main/java/li/cil/oc/client/renderer/glsl/Program.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package li.cil.oc.client.renderer.glsl;

import java.util.Arrays;

import org.joml.Matrix4fc;
import org.lwjgl.opengl.GL20;

public final class Program {

private final ProgramRecipe recipe;
private final GlslDialect dialect;

private final int[] slots;

private final int transformLocation;
private final int normalLocation;
private final int lightDirLocation;
private final int lightDiffuseLocation;

private int program;

Program(ProgramRecipe recipe, GlslDialect dialect, int program, int[] slots, int transformLocation,
int normalLocation, int lightDirLocation, int lightDiffuseLocation) {
this.recipe = recipe;
this.dialect = dialect;
this.program = program;
this.slots = slots;
this.transformLocation = transformLocation;
this.normalLocation = normalLocation;
this.lightDirLocation = lightDirLocation;
this.lightDiffuseLocation = lightDiffuseLocation;
}

static Program invalid(ProgramRecipe recipe) {
final int[] slots = new int[recipe.uniformCount()];
Arrays.fill(slots, -1);
return new Program(recipe, GlslDialect.preferred(), 0, slots, -1, -1, -1, -1);
}

public boolean isValid() {
return program != 0;
}

public void use() {
if (program != 0) GL20.glUseProgram(program);
}

public int loc(Uniform uniform) {
if (uniform.owner != recipe) {
throw new IllegalArgumentException(recipe.id() + ": " + uniform + " belongs to another shader");
}
return slots[uniform.slot];
}

public void uploadModel(Matrix4fc model) {
if (program == 0) return;

if (dialect != GlslDialect.GLSL330) {
DrawUniforms.upload(transformLocation, model);
} else if (normalLocation < 0) {
DrawUniforms.uploadMVP(transformLocation, model);
} else {
DrawUniforms.uploadLit(transformLocation, normalLocation, lightDirLocation, lightDiffuseLocation, model);
}
}

public void release() {
if (program != 0) {
GL20.glDeleteProgram(program);
program = 0;
}
}
}
Loading
Loading