forked from douira/sodium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into combine-draw-commands
# 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
Showing
106 changed files
with
1,808 additions
and
1,375 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 98 additions & 53 deletions
151
common/src/api/java/net/caffeinemc/mods/sodium/api/util/ColorMixer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
19 changes: 0 additions & 19 deletions
19
common/src/main/java/net/caffeinemc/mods/sodium/client/gl/GlContextInfo.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.