Skip to content

Commit

Permalink
Use unsigned int for bit flags
Browse files Browse the repository at this point in the history
  • Loading branch information
grondag committed Nov 21, 2020
1 parent b3e3739 commit 1c21d62
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/main/java/grondag/canvas/shader/GlMaterialShader.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;

// PERF: emit switch statments on non-Mac
public class GlMaterialShader extends GlShader {
GlMaterialShader(Identifier shaderSource, int shaderType, ProgramType programType) {
super(shaderSource, shaderType, programType);
Expand Down
222 changes: 222 additions & 0 deletions src/main/java/grondag/canvas/shader/GlProgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@

import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.EXTGPUShader4;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL21;
import org.lwjgl.opengl.GL30;
import org.lwjgl.system.MemoryUtil;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.util.math.Matrix3f;
import net.minecraft.util.math.Matrix4f;
Expand All @@ -39,14 +42,19 @@
import grondag.frex.api.material.Uniform;
import grondag.frex.api.material.Uniform.Uniform1f;
import grondag.frex.api.material.Uniform.Uniform1i;
import grondag.frex.api.material.Uniform.Uniform1ui;
import grondag.frex.api.material.Uniform.Uniform2f;
import grondag.frex.api.material.Uniform.Uniform2i;
import grondag.frex.api.material.Uniform.Uniform2ui;
import grondag.frex.api.material.Uniform.Uniform3f;
import grondag.frex.api.material.Uniform.Uniform3i;
import grondag.frex.api.material.Uniform.Uniform3ui;
import grondag.frex.api.material.Uniform.Uniform4f;
import grondag.frex.api.material.Uniform.Uniform4i;
import grondag.frex.api.material.Uniform.Uniform4ui;
import grondag.frex.api.material.Uniform.UniformArrayf;
import grondag.frex.api.material.Uniform.UniformArrayi;
import grondag.frex.api.material.Uniform.UniformArrayui;
import grondag.frex.api.material.Uniform.UniformMatrix3f;
import grondag.frex.api.material.Uniform.UniformMatrix4f;
import grondag.frex.api.material.UniformRefreshFrequency;
Expand Down Expand Up @@ -139,6 +147,26 @@ public UniformArrayi uniformArrayi(String name, UniformRefreshFrequency frequenc
return new UniformArrayiImpl(name, initializer, frequency, size);
}

public Uniform1ui uniform1ui(String name, UniformRefreshFrequency frequency, Consumer<Uniform1ui> initializer) {
return new Uniform1uiImpl(name, initializer, frequency);
}

public Uniform2ui uniform2ui(String name, UniformRefreshFrequency frequency, Consumer<Uniform2ui> initializer) {
return new Uniform2uiImpl(name, initializer, frequency);
}

public Uniform3ui uniform3ui(String name, UniformRefreshFrequency frequency, Consumer<Uniform3ui> initializer) {
return new Uniform3uiImpl(name, initializer, frequency);
}

public Uniform4ui uniform4ui(String name, UniformRefreshFrequency frequency, Consumer<Uniform4ui> initializer) {
return new Uniform4uiImpl(name, initializer, frequency);
}

public UniformArrayui uniformArrayui(String name, UniformRefreshFrequency frequency, Consumer<UniformArrayui> initializer, int size) {
return new UniformArrayuiImpl(name, initializer, frequency, size);
}

public final void activate() {
if (needsLoad) {
load();
Expand Down Expand Up @@ -743,6 +771,200 @@ public String searchString() {
}
}

public class Uniform1uiImpl extends UniformInt<Uniform1ui> implements Uniform1ui {
protected Uniform1uiImpl(String name, Consumer<Uniform1ui> initializer, UniformRefreshFrequency frequency) {
super(name, initializer, frequency, 1);
}

@Override
public final void set(int value) {
if (unifID == -1) {
return;
}

if (uniformIntBuffer.get(0) != value) {
uniformIntBuffer.put(0, value);
setDirty();
}
}

@Override
protected void uploadInner() {
if (MinecraftClient.IS_SYSTEM_MAC) {
EXTGPUShader4.glUniform1uivEXT(unifID, uniformIntBuffer);
} else {
GL30.glUniform1uiv(unifID, uniformIntBuffer);
}
}

@Override
public String searchString() {
return "uint";
}
}

public class Uniform2uiImpl extends UniformInt<Uniform2ui> implements Uniform2ui {
protected Uniform2uiImpl(String name, Consumer<Uniform2ui> initializer, UniformRefreshFrequency frequency) {
super(name, initializer, frequency, 2);
}

@Override
public final void set(int v0, int v1) {
if (unifID == -1) {
return;
}

if (uniformIntBuffer.get(0) != v0) {
uniformIntBuffer.put(0, v0);
setDirty();
}

if (uniformIntBuffer.get(1) != v1) {
uniformIntBuffer.put(1, v1);
setDirty();
}
}

@Override
protected void uploadInner() {
if (MinecraftClient.IS_SYSTEM_MAC) {
EXTGPUShader4.glUniform2uivEXT(unifID, uniformIntBuffer);
} else {
GL30.glUniform2uiv(unifID, uniformIntBuffer);
}
}

@Override
public String searchString() {
return "uvec2";
}
}

public class Uniform3uiImpl extends UniformInt<Uniform3ui> implements Uniform3ui {
protected Uniform3uiImpl(String name, Consumer<Uniform3ui> initializer, UniformRefreshFrequency frequency) {
super(name, initializer, frequency, 3);
}

@Override
public final void set(int v0, int v1, int v2) {
if (unifID == -1) {
return;
}

if (uniformIntBuffer.get(0) != v0) {
uniformIntBuffer.put(0, v0);
setDirty();
}

if (uniformIntBuffer.get(1) != v1) {
uniformIntBuffer.put(1, v1);
setDirty();
}

if (uniformIntBuffer.get(2) != v2) {
uniformIntBuffer.put(2, v2);
setDirty();
}
}

@Override
protected void uploadInner() {
if (MinecraftClient.IS_SYSTEM_MAC) {
EXTGPUShader4.glUniform3uivEXT(unifID, uniformIntBuffer);
} else {
GL30.glUniform3uiv(unifID, uniformIntBuffer);
}
}

@Override
public String searchString() {
return "uvec3";
}
}

public class Uniform4uiImpl extends UniformInt<Uniform4ui> implements Uniform4ui {
protected Uniform4uiImpl(String name, Consumer<Uniform4ui> initializer, UniformRefreshFrequency frequency) {
super(name, initializer, frequency, 4);
}

@Override
public final void set(int v0, int v1, int v2, int v3) {
if (unifID == -1) {
return;
}

if (uniformIntBuffer.get(0) != v0) {
uniformIntBuffer.put(0, v0);
setDirty();
}

if (uniformIntBuffer.get(1) != v1) {
uniformIntBuffer.put(1, v1);
setDirty();
}

if (uniformIntBuffer.get(2) != v2) {
uniformIntBuffer.put(2, v2);
setDirty();
}

if (uniformIntBuffer.get(3) != v3) {
uniformIntBuffer.put(3, v3);
setDirty();
}
}

@Override
protected void uploadInner() {
if (MinecraftClient.IS_SYSTEM_MAC) {
EXTGPUShader4.glUniform4uivEXT(unifID, uniformIntBuffer);
} else {
GL30.glUniform4uiv(unifID, uniformIntBuffer);
}
}

@Override
public String searchString() {
return "uvec4";
}
}

public class UniformArrayuiImpl extends UniformInt<UniformArrayui> implements UniformArrayui {
protected UniformArrayuiImpl(String name, Consumer<UniformArrayui> initializer, UniformRefreshFrequency frequency, int size) {
super(name, initializer, frequency, size);
}

@Override
public final void set(int[] data) {
if (unifID == -1) {
return;
}

final int limit = data.length;

for (int i = 0; i < limit; i++) {
if (uniformIntBuffer.get(i) != data[i]) {
uniformIntBuffer.put(i, data[i]);
setDirty();
}
}
}

@Override
protected void uploadInner() {
if (MinecraftClient.IS_SYSTEM_MAC) {
EXTGPUShader4.glUniform1uivEXT(unifID, uniformIntBuffer);
} else {
GL30.glUniform1uiv(unifID, uniformIntBuffer);
}
}

@Override
public String searchString() {
return "uint\\s*\\[\\s*[0-9]+\\s*]";
}
}

public class UniformMatrix4fImpl extends UniformImpl<UniformMatrix4f> implements UniformMatrix4f {
protected final FloatBuffer uniformFloatBuffer;
protected final long bufferAddress;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/grondag/canvas/shader/GlShader.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ private String getSource() {

if (!MinecraftClient.IS_SYSTEM_MAC) {
result = StringUtils.replace(result, "#version 120", "#version 130");
result = StringUtils.replace(result, "#extension GL_EXT_gpu_shader4 : enable", "//#extension GL_EXT_gpu_shader4 : enable");
result = StringUtils.replace(result, "#extension GL_EXT_gpu_shader4 : require", "//#extension GL_EXT_gpu_shader4 : require");
}

source = result;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/grondag/canvas/shader/ShaderData.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class ShaderData {
public static final Consumer<GlProgram> STANDARD_UNIFORM_SETUP = program -> {
program.uniformArrayf("_cvu_world", UniformRefreshFrequency.PER_TICK, u -> u.set(WorldDataManager.data()), WorldDataManager.LENGTH);

program.uniform1ui("_cvu_world_flags", UniformRefreshFrequency.PER_TICK, u -> u.set(WorldDataManager.flags()));

program.uniformSampler2d("frxs_spriteAltas", UniformRefreshFrequency.ON_LOAD, u -> u.set(TextureData.MC_SPRITE_ATLAS - GL21.GL_TEXTURE0));

program.uniformSampler2d("frxs_overlay", UniformRefreshFrequency.ON_LOAD, u -> u.set(TextureData.MC_OVELAY - GL21.GL_TEXTURE0));
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/grondag/canvas/varia/WorldDataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ public class WorldDataManager {
private static final int MOON_SIZE = 3;
private static final int WORLD_TIME = 4;
private static final int WORLD_DAYS = 5;
private static final int FLAGS_0 = 6;
@SuppressWarnings("unused") // was previously used for flags
private static final int RESERVED_A = 6;
@SuppressWarnings("unused") // was previously used for fog
private static final int RESERVED = 7;
private static final int RESERVED_B = 7;
private static final int EMISSIVE_COLOR_RED = 8;
private static final int EMISSIVE_COLOR_GREEN = 9;
private static final int EMISSIVE_COLOR_BLUE = 10;
Expand All @@ -64,6 +65,7 @@ public class WorldDataManager {

private static final float[] DATA = new float[LENGTH];
private static final long baseRenderTime = System.currentTimeMillis();
private static int flags;

static {
if (Configurator.enableLifeCycleDebug) {
Expand Down Expand Up @@ -128,7 +130,7 @@ public static void update(Camera camera) {
flags |= FLAG0_IS_THUNDERING;
}

DATA[FLAGS_0] = flags;
WorldDataManager.flags = flags;

DATA[RAIN_STRENGTH] = world.getRainGradient(tickDelta);

Expand Down Expand Up @@ -183,4 +185,8 @@ private static void putViewVector(int index, float yaw, float pitch) {
DATA[index + 1] = -MathHelper.sin(p);
DATA[index + 2] = MathHelper.cos(y) * MathHelper.cos(p);
}

public static int flags() {
return flags;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,3 @@
#define TARGET_EMISSIVE -1

#define HANDHELD_LIGHT_RADIUS 0

#define VERTEX_SHADER
5 changes: 2 additions & 3 deletions src/main/resources/assets/canvas/shaders/internal/flags.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@

#define _CV_TRANSLUCENT_CUTOUT_THRESHOLD 0.003921569

// prevents problems on some NVidia cards/drives
flat varying float _cvv_flags;
flat varying uint _cvv_flags;

float _cv_getFlag(int flagId) {
return frx_bitValue(int(_cvv_flags), flagId);
return frx_bitValue(_cvv_flags, flagId);
}
15 changes: 14 additions & 1 deletion src/main/resources/assets/canvas/shaders/internal/header.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
// updated to 130 when not running on a mac
#version 120
// removed when not running on a mac
#extension GL_EXT_gpu_shader4 : enable
#extension GL_EXT_gpu_shader4 : require

#define VERTEX_SHADER

// mac doesn't understand uint syntax
#if __VERSION__ < 130
#define uint unsigned int
#else
#ifdef VERTEX_SHADER
#define varying out
#else
#define varying in
#endif
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void main() {

// Adding +0.5 prevents striping or other strangeness in flag-dependent rendering
// due to FP error on some cards/drivers. Also made varying attribute invariant (rolls eyes at OpenGL)
_cvv_flags = in_normal_flags.w + 0.5;
_cvv_flags = uint(in_normal_flags.w + 0.5);

_cv_setupProgram();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ int _cv_fragmentProgramId() {
#define PROGRAM_FLAG_GUI 0

float _cv_isGui() {
return frx_bitValue(int(_cvu_program.z), PROGRAM_FLAG_GUI);
return frx_bitValue(uint(_cvu_program.z), PROGRAM_FLAG_GUI);
}

#ifdef VERTEX_SHADER
Expand Down
Loading

0 comments on commit 1c21d62

Please sign in to comment.