Skip to content

Commit

Permalink
Merge branch 'dev' into combine-draw-commands
Browse files Browse the repository at this point in the history
# Conflicts:
#	common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/compile/tasks/ChunkBuilderMeshingTask.java
#	common/src/main/java/net/caffeinemc/mods/sodium/client/render/chunk/region/RenderRegionManager.java
  • Loading branch information
douira committed Nov 25, 2024
2 parents e962b37 + 87c1bd5 commit 0de9587
Show file tree
Hide file tree
Showing 106 changed files with 1,808 additions and 1,375 deletions.
10 changes: 5 additions & 5 deletions buildSrc/src/main/kotlin/BuildConfig.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import org.gradle.api.Project

object BuildConfig {
val MINECRAFT_VERSION: String = "1.21.1"
val NEOFORGE_VERSION: String = "21.1.46"
val FABRIC_LOADER_VERSION: String = "0.16.4"
val FABRIC_API_VERSION: String = "0.103.0+1.21.1"
val MINECRAFT_VERSION: String = "1.21.3"
val NEOFORGE_VERSION: String = "21.3.40-beta"
val FABRIC_LOADER_VERSION: String = "0.16.9"
val FABRIC_API_VERSION: String = "0.109.0+1.21.3"

// This value can be set to null to disable Parchment.
// TODO: Re-add Parchment
val PARCHMENT_VERSION: String? = null

// https://semver.org/
var MOD_VERSION: String = "0.6.0-beta.2"
var MOD_VERSION: String = "0.6.0"

fun createVersionString(project: Project): String {
val builder = StringBuilder()
Expand Down
3 changes: 1 addition & 2 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ dependencies {

// We need to be careful during pre-launch that we don't touch any Minecraft classes, since other mods
// will not yet have an opportunity to apply transformations.
configurationPreLaunch("org.apache.commons:commons-lang3:3.14.0")
configurationPreLaunch("commons-io:commons-io:2.15.1")
configurationPreLaunch("org.lwjgl:lwjgl:3.3.3")
configurationPreLaunch("org.lwjgl:lwjgl-opengl:3.3.3")
configurationPreLaunch("net.java.dev.jna:jna:5.14.0")
configurationPreLaunch("net.java.dev.jna:jna-platform:5.14.0")
configurationPreLaunch("org.slf4j:slf4j-api:2.0.9")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.caffeinemc.mods.sodium.api.util;

import java.nio.ByteOrder;

/**
* Provides some utilities for packing and unpacking color components from packed integer colors in ABGR format, which
* is used by OpenGL for color vectors.
Expand All @@ -9,11 +11,16 @@
* | Alpha | Blue | Green | Red |
*/
public class ColorABGR implements ColorU8 {
private static final int RED_COMPONENT_OFFSET = 0;
private static final int RED_COMPONENT_OFFSET = 0;
private static final int GREEN_COMPONENT_OFFSET = 8;
private static final int BLUE_COMPONENT_OFFSET = 16;
private static final int BLUE_COMPONENT_OFFSET = 16;
private static final int ALPHA_COMPONENT_OFFSET = 24;

private static final int RED_COMPONENT_MASK = COMPONENT_MASK << RED_COMPONENT_OFFSET;
private static final int GREEN_COMPONENT_MASK = COMPONENT_MASK << GREEN_COMPONENT_OFFSET;
private static final int BLUE_COMPONENT_MASK = COMPONENT_MASK << BLUE_COMPONENT_OFFSET;
private static final int ALPHA_COMPONENT_MASK = COMPONENT_MASK << ALPHA_COMPONENT_OFFSET;

/**
* Packs the specified color components into ABGR format. The alpha component is fully opaque.
* @param r The red component of the color
Expand Down Expand Up @@ -98,4 +105,51 @@ public static int unpackBlue(int color) {
public static int unpackAlpha(int color) {
return (color >> ALPHA_COMPONENT_OFFSET) & COMPONENT_MASK;
}

/**
* Multiplies the RGB components of the color with the provided factor. The alpha component is not modified.
*
* @param color The packed 32-bit ABGR color to be multiplied
* @param factor The darkening factor (in the range of 0..255) to multiply with
*/
public static int mulRGB(int color, int factor) {
return (ColorMixer.mul(color, factor) & ~ALPHA_COMPONENT_MASK) | (color & ALPHA_COMPONENT_MASK);
}

/**
* See {@link #mulRGB(int, int)}. This function is identical, but it accepts a float in [0.0, 1.0] instead, which
* is then mapped to [0, 255].
*
* @param color The packed 32-bit ABGR color to be multiplied
* @param factor The darkening factor (in the range of 0.0..1.0) to multiply with
*/
public static int mulRGB(int color, float factor) {
return mulRGB(color, ColorU8.normalizedFloatToByte(factor));
}

private static final boolean BIG_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;

/**
* Shuffles the ordering of the ABGR color so that it can then be written out to memory in the platform's native
* byte ordering. This should be used when writing the packed color to memory (in native-order) as a 32-bit word.
*/
public static int fromNativeByteOrder(int color) {
if (BIG_ENDIAN) {
return Integer.reverseBytes(color);
} else {
return color;
}
}

/**
* Shuffles the ordering of the ABGR color from the platform's native byte ordering. This should be used when reading
* the packed color from memory (in native-order) as a 32-bit word.
*/
public static int toNativeByteOrder(int color) {
if (BIG_ENDIAN) {
return Integer.reverseBytes(color);
} else {
return color;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public class ColorARGB implements ColorU8 {
private static final int GREEN_COMPONENT_OFFSET = 8;
private static final int BLUE_COMPONENT_OFFSET = 0;

private static final int RED_COMPONENT_MASK = COMPONENT_MASK << RED_COMPONENT_OFFSET;
private static final int GREEN_COMPONENT_MASK = COMPONENT_MASK << GREEN_COMPONENT_OFFSET;
private static final int BLUE_COMPONENT_MASK = COMPONENT_MASK << BLUE_COMPONENT_OFFSET;
private static final int ALPHA_COMPONENT_MASK = COMPONENT_MASK << ALPHA_COMPONENT_OFFSET;

/**
* Packs the specified color components into big-endian format for consumption by OpenGL.
* @param r The red component of the color
Expand Down Expand Up @@ -73,23 +78,40 @@ public static int unpackBlue(int color) {
}

/**
* Re-packs the ARGB color into an ABGR color with the specified alpha component.
* Swizzles from ARGB format into ABGR format, replacing the alpha component with {@param alpha}.
*/
public static int toABGR(int color, float alpha) {
return Integer.reverseBytes(color << 8 | ColorU8.normalizedFloatToByte(alpha));
public static int toABGR(int color, int alpha) {
// shl(ARGB, 8) -> RGB0
// or(RGB0, 000A) -> RGBA
return Integer.reverseBytes(color << 8 | alpha);
}

/**
* Re-packs the ARGB color into a aBGR color with the specified alpha component.
* Swizzles from ARGB format into ABGR format, replacing the alpha component with {@param alpha}. The alpha
* component is mapped from [0.0, 1.0] to [0, 255].
*/
public static int toABGR(int color, int alpha) {
return Integer.reverseBytes(color << 8 | alpha);
public static int toABGR(int color, float alpha) {
return toABGR(color, ColorU8.normalizedFloatToByte(alpha));
}

/**
* Swizzles from ARGB format into ABGR format.
*/
public static int toABGR(int color) {
// rotateLeft(ARGB, 8) -> RGBA
// reverseBytes(RGBA) -> ABGR
return Integer.reverseBytes(Integer.rotateLeft(color, 8));
}

/**
* Swizzles from ABGR format into ARGB format.
*/
public static int fromABGR(int color) {
// reverseBytes(ABGR) -> RGBA
// rotateRight(RGBA, 8) -> ARGB
return Integer.rotateRight(Integer.reverseBytes(color), 8);
}

/**
* Packs the specified color components into ARGB format.
* @param rgb The red/green/blue component of the color
Expand All @@ -98,4 +120,25 @@ public static int toABGR(int color) {
public static int withAlpha(int rgb, int alpha) {
return (alpha << ALPHA_COMPONENT_OFFSET) | (rgb & ~(COMPONENT_MASK << ALPHA_COMPONENT_OFFSET));
}

/**
* Multiplies the RGB components of the color with the provided factor. The alpha component is not modified.
*
* @param color The packed 32-bit ABGR color to be multiplied
* @param factor The darkening factor (in the range of 0..255) to multiply with
*/
public static int mulRGB(int color, int factor) {
return (ColorMixer.mul(color, factor) & ~ALPHA_COMPONENT_MASK) | (color & ALPHA_COMPONENT_MASK);
}

/**
* See {@link #mulRGB(int, int)}. This function is identical, but it accepts a float in [0.0, 1.0] instead, which
* is then mapped to [0, 255].
*
* @param color The packed 32-bit ABGR color to be multiplied
* @param factor The darkening factor (in the range of 0.0..1.0) to multiply with
*/
public static int mulRGB(int color, float factor) {
return mulRGB(color, ColorU8.normalizedFloatToByte(factor));
}
}
Original file line number Diff line number Diff line change
@@ -1,77 +1,122 @@
package net.caffeinemc.mods.sodium.api.util;

import org.jetbrains.annotations.ApiStatus;

/**
* A collection of optimized color mixing functions which directly operate on packed color values. These functions are
* agnostic to the ordering of color channels, and the output value will always use the same channel ordering as
* the input values.
*/
public class ColorMixer {
private static final int CHANNEL_MASK = 0x00FF00FF;
/**
* <p>Linearly interpolate between the {@param start} and {@param end} points, represented as packed unsigned 8-bit
* values within a 32-bit integer. The result is computed as <pre>(start * weight) + (end * (255 - weight))</pre>.</p>
*
* <p>The results are undefined if {@param weight} is not within the interval [0, 255].</p>
* @param start The start of the range to interpolate
* @param end The end of the range to interpolate
* @param weight The weight value used to interpolate between color values (in 0..255 range)
* @return The color that was interpolated between the start and end points
*/
public static int mix(int start, int end, int weight) {
// Overflow is not possible, so adding the values is fine.
return mul(start, weight) + mul(end, ColorU8.COMPONENT_MASK - weight);
}

/**
* <p>This function is identical to {@link ColorMixer#mix(int, int, int)}, but {@param weight} is a normalized
* floating-point value within the interval of [0.0, 1.0].</p>
*
* <p>The results are undefined if {@param weight} is not within the interval [0.0, 1.0].</p>
*
* @param start The start of the range to interpolate
* @param end The end of the range to interpolate
* @param weight The weight value used to interpolate between color values (in 0.0..1.0 range)
* @return The color that was interpolated between the start and end points
*/
public static int mix(int start, int end, float weight) {
return mix(start, end, ColorU8.normalizedFloatToByte(weight));
}

/**
* Mixes a 32-bit color (with packed 8-bit components) into another using the given ratio. This is equivalent to
* (color1 * ratio) + (color2 * (1.0 - ratio)) but uses bitwise trickery for maximum performance.
* <p>
* The order of the channels within the packed color does not matter, and the output color will always
* have the same ordering as the input colors.
* <p>Performs bi-linear interpolation on a 2x2 matrix of color values to derive the point (x, y). This is more
* efficient than chaining {@link #mul(int, float)} calls.</p>
*
* <p>The results are undefined if {@param x} and {@param y} are not within the interval [0.0, 1.0].</p>
*
* @param aColor The color to mix towards
* @param bColor The color to mix away from
* @param ratio The percentage (in 0.0..1.0 range) to mix the first color into the second color
* @return The mixed color in packed 32-bit format
* @param m00 The packed color value for (0, 0)
* @param m01 The packed color value for (0, 1)
* @param m10 The packed color value for (1, 0)
* @param m11 The packed color value for (1, 1)
* @param x The amount to interpolate between x=0 and x=1
* @param y The amount to interpolate between y=0 and y=1
* @return The interpolated color value
*/
public static int mix(int aColor, int bColor, float ratio) {
int aRatio = (int) (256 * ratio); // int(ratio)
int bRatio = 256 - aRatio; // int(1.0 - ratio)
@ApiStatus.Experimental
public static int mix2d(int m00, int m01, int m10, int m11, float x, float y) {
// The weights for each row and column in the matrix
int x1 = ColorU8.normalizedFloatToByte(x), x0 = 255 - x1;
int y1 = ColorU8.normalizedFloatToByte(y), y0 = 255 - y1;

// Mask off and shift two components from each color into a packed vector of 16-bit components, where the
// high 8 bits are all zeroes.
int a1 = (aColor >> 0) & CHANNEL_MASK;
int b1 = (bColor >> 0) & CHANNEL_MASK;
int a2 = (aColor >> 8) & CHANNEL_MASK;
int b2 = (bColor >> 8) & CHANNEL_MASK;
// Blend across the X-axis
// (M00 * X0) + (M10 * X1)
long row0a = ((((m00 & 0x00FF00FFL) * x0) + (((m10 & 0x00FF00FFL) * x1)) + 0x00FF00FFL) >>> 8) & 0x00FF00FFL;
long row0b = ((((m00 & 0xFF00FF00L) * x0) + (((m10 & 0xFF00FF00L) * x1)) + 0xFF00FF00L) >>> 8) & 0xFF00FF00L;

// Multiply the packed 16-bit components against each mix factor, and add the components of each color
// to produce the mixed result. This will never overflow since both 16-bit integers are in 0..255 range.
// (M10 * X0) + (M11 * X1)
long row1a = ((((m01 & 0x00FF00FFL) * x0) + (((m11 & 0x00FF00FFL) * x1)) + 0x00FF00FFL) >>> 8) & 0x00FF00FFL;
long row1b = ((((m01 & 0xFF00FF00L) * x0) + (((m11 & 0xFF00FF00L) * x1)) + 0xFF00FF00L) >>> 8) & 0xFF00FF00L;

// Then, shift the high 8 bits of each packed 16-bit component into the low 8 bits, and mask off the high bits of
// each 16-bit component to produce a vector of packed 8-bit components, where every other component is empty.
int c1 = (((a1 * aRatio) + (b1 * bRatio)) >> 8) & CHANNEL_MASK;
int c2 = (((a2 * aRatio) + (b2 * bRatio)) >> 8) & CHANNEL_MASK;
// Blend across the Y-axis
// (ROW0 * Y0) + (ROW1 * Y1)
long result = ((((row0a * y0) + ((row1a * y1)) + 0x00FF00FFL) >>> 8) & 0x00FF00FFL) |
((((row0b * y0) + ((row1b * y1)) + 0xFF00FF00L) >>> 8) & 0xFF00FF00L);

// Join the color components into the original order
return ((c1 << 0) | (c2 << 8));
return (int) result;
}

/**
* Multiplies the 32-bit colors (with packed 8-bit components) together.
* <p>
* The order of the channels within the packed color does not matter, and the output color will always
* have the same ordering as the input colors.
* <p>Multiplies the packed 8-bit values component-wise to produce 16-bit intermediaries, and then round to the
* nearest 8-bit representation (similar to floating-point.)</p>
*
* @param a The first color to multiply
* @param b The second color to multiply
* @return The multiplied color in packed 32-bit format
* @param color0 The first color to multiply
* @param color1 The second color to multiply
* @return The product of the two colors
*/
public static int mul(int a, int b) {
// Take each 8-bit component pair, multiply them together to create intermediate 16-bit integers,
// and then shift the high half of each 16-bit integer into 8-bit integers.
int c0 = (((a >> 0) & 0xFF) * ((b >> 0) & 0xFF)) >> 8;
int c1 = (((a >> 8) & 0xFF) * ((b >> 8) & 0xFF)) >> 8;
int c2 = (((a >> 16) & 0xFF) * ((b >> 16) & 0xFF)) >> 8;
int c3 = (((a >> 24) & 0xFF) * ((b >> 24) & 0xFF)) >> 8;
public static int mulComponentWise(int color0, int color1) {
int comp0 = ((((color0 >>> 0) & 0xFF) * ((color1 >>> 0) & 0xFF)) + 0xFF) >>> 8;
int comp1 = ((((color0 >>> 8) & 0xFF) * ((color1 >>> 8) & 0xFF)) + 0xFF) >>> 8;
int comp2 = ((((color0 >>> 16) & 0xFF) * ((color1 >>> 16) & 0xFF)) + 0xFF) >>> 8;
int comp3 = ((((color0 >>> 24) & 0xFF) * ((color1 >>> 24) & 0xFF)) + 0xFF) >>> 8;

// Pack the components
return (c0 << 0) | (c1 << 8) | (c2 << 16) | (c3 << 24);
return (comp0 << 0) | (comp1 << 8) | (comp2 << 16) | (comp3 << 24);
}

/**
* Multiplies the 32-bit colors with one component
* <p>Multiplies each 8-bit component against the factor to produce 16-bit intermediaries, and then round to the
* nearest 8-bit representation (similar to floating-point.)</p>
*
* <p>The results are undefined if {@param factor} is not within the interval [0, 255].</p>
*
* @param color The packed color values
* @param factor The multiplication factor (in 0..255 range)
* @return The result of the multiplication
*/
public static int mulSingle(int a, int b) {
// Take each 8-bit component pair, multiply them together to create intermediate 16-bit integers,
// and then shift the high half of each 16-bit integer into 8-bit integers.
int c0 = (((a) & 0xFF) * b) >> 8;
int c1 = (((a >> 8) & 0xFF) * b) >> 8;
int c2 = (((a >> 16) & 0xFF) * b) >> 8;
int c3 = (((a >> 24) & 0xFF) * b) >> 8;
public static int mul(int color, int factor) {
final long result = (((((color & 0x00FF00FFL) * factor) + 0x00FF00FFL) >>> 8) & 0x00FF00FFL) |
(((((color & 0xFF00FF00L) * factor) + 0xFF00FF00L) >>> 8) & 0xFF00FF00L);

// Pack the components
return (c0 << 0) | (c1 << 8) | (c2 << 16) | (c3 << 24);
return (int) result;
}

/**
* See {@link #mul(int, int)}, which this function is identical to, except that it takes a floating point value in
* the interval of [0.0, 1.0] and maps it to [0, 255].
*
* <p>The results are undefined if {@param factor} is not within the interval [0.0, 1.0].</p>
*/
public static int mul(int color, float factor) {
return mul(color, ColorU8.normalizedFloatToByte(factor));
}
}

This file was deleted.

Loading

0 comments on commit 0de9587

Please sign in to comment.