Skip to content
This repository has been archived by the owner before Nov 9, 2022. It is now read-only.

8252385: Lanai: Implement LinearGradient paint in shader #85

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -324,6 +324,28 @@ void setPaint(Graphics2D g2d, int id) {
}
}

static class LinGrad3OvalRotParticleRenderer extends FlatOvalRotParticleRenderer {


LinGrad3OvalRotParticleRenderer(int n, float r) {
super(n, r);
}

@Override
void setPaint(Graphics2D g2d, int id) {
Point2D start = new Point2D.Double(- r, - 0.5*r);
Point2D end = new Point2D.Double( 2 * r, r);
float[] dist = {0.0f, 0.5f, 1.0f};
Color[] cls = {
colors[id %colors.length],
colors[(colors.length - id) %colors.length],
colors[(id*5) %colors.length]};
LinearGradientPaint p =
new LinearGradientPaint(start, end, dist, cls);
g2d.setPaint(p);
}
}

static class FlatBoxParticleRenderer extends FlatParticleRenderer {


@@ -616,6 +638,7 @@ private boolean isAlmostEqual(Color c1, Color c2) {
private static final ParticleRenderer clipFlatBoxParticleRenderer = new ClipFlatBoxParticleRenderer(N, R);
private static final ParticleRenderer flatBoxRotRenderer = new FlatBoxRotParticleRenderer(N, R);
private static final ParticleRenderer linGradOvalRotRenderer = new LinGradOvalRotParticleRenderer(N, R);
private static final ParticleRenderer linGrad3OvalRotRenderer = new LinGrad3OvalRotParticleRenderer(N, R);
private static final ParticleRenderer wiredRenderer = new WiredParticleRenderer(N, R);
private static final ParticleRenderer wiredBoxRenderer = new WiredBoxParticleRenderer(N, R);
private static final ParticleRenderer segRenderer = new SegParticleRenderer(N, R);
@@ -694,6 +717,14 @@ public void testFlatOvalRotBubblesAA() throws Exception {
(new PerfMeter("RotatedOvalAA")).exec(createPR(flatOvalRotRenderer).configure(AA)).report();
}

public void testLinGrad3OvalRotBubbles() throws Exception {
(new PerfMeter("LinGrad3RotatedOval")).exec(createPR(linGrad3OvalRotRenderer)).report();
}

public void testLinGrad3OvalRotBubblesAA() throws Exception {
(new PerfMeter("LinGrad3RotatedOvalAA")).exec(createPR(linGrad3OvalRotRenderer).configure(AA)).report();
}

public void testLinGradOvalRotBubbles() throws Exception {
(new PerfMeter("LinGradRotatedOval")).exec(createPR(linGradOvalRotRenderer)).report();
}
@@ -48,6 +48,7 @@
import java.security.AccessController;
import java.security.PrivilegedAction;

import static sun.java2d.metal.MTLContext.MTLContextCaps.CAPS_EXT_GRAD_SHADER;
import static sun.java2d.opengl.OGLSurfaceData.TEXTURE;
import static sun.java2d.pipe.hw.AccelSurface.RT_TEXTURE;
import static sun.java2d.pipe.hw.ContextCapabilities.*;
@@ -159,7 +160,8 @@ public static MTLGraphicsConfig getConfig(CGraphicsDevice device,
ContextCapabilities caps = new MTLContext.MTLContextCaps(
CAPS_PS30 | CAPS_PS20 | CAPS_RT_PLAIN_ALPHA |
CAPS_RT_TEXTURE_ALPHA | CAPS_RT_TEXTURE_OPAQUE |
CAPS_MULTITEXTURE | CAPS_TEXNONPOW2 | CAPS_TEXNONSQUARE | CAPS_EXT_BIOP_SHADER,
CAPS_MULTITEXTURE | CAPS_TEXNONPOW2 | CAPS_TEXNONSQUARE |
CAPS_EXT_BIOP_SHADER | CAPS_EXT_GRAD_SHADER,
ids[0]);
return new MTLGraphicsConfig(device, pixfmt, cfginfo, textureSize, caps);
}
@@ -193,5 +193,9 @@ boolean isPaintValid(SunGraphics2D sg2d) {

private static class RadialGradient extends MultiGradient {
private RadialGradient() {}
@Override
boolean isPaintValid(SunGraphics2D sg2d) {
return false;
}
}
}
@@ -30,7 +30,13 @@

#define PGRAM_VERTEX_COUNT 6
#define QUAD_VERTEX_COUNT 4
#define GRAD_MAX_FRACTIONS 12

enum GradCycleMethod {
GradNoCycle = 0,
GradReflect = 1,
GradRepeat = 2
};
enum VertexAttributes {
VertexAttributePosition = 0,
VertexAttributeTexPos = 1
@@ -57,6 +63,14 @@ struct GradFrameUniforms {
int isCyclic;
};

struct LinGradFrameUniforms {
vector_float3 params;
float fract[GRAD_MAX_FRACTIONS];
vector_float4 color[GRAD_MAX_FRACTIONS];
int numFracts;
int cycleMethod;
};

struct Vertex {
float position[2];
};
@@ -221,6 +221,42 @@ fragment half4 frag_txt_grad(GradShaderInOut in [[stage_in]],
renderColor.a);
}

fragment half4 frag_txt_lin_grad(GradShaderInOut in [[stage_in]],
constant LinGradFrameUniforms& uniforms [[buffer(0)]],
texture2d<float, access::sample> renderTexture [[texture(0)]])
{
constexpr sampler textureSampler (address::repeat, mag_filter::nearest,
min_filter::nearest);

float4 renderColor = renderTexture.sample(textureSampler, in.texCoords);

float3 v = float3(in.position.x, in.position.y, 1);
float a = dot(v,uniforms.params);
float lf = 1.0/(uniforms.numFracts - 1);

if (uniforms.cycleMethod > GradNoCycle) {
int fa = floor(a);
a = a - fa;
if (uniforms.cycleMethod == GradReflect && fa%2) {
a = 1.0 - a;
}
}

int n = floor(a/lf);
if (uniforms.cycleMethod > GradNoCycle) {
n = ((n % uniforms.numFracts) + uniforms.numFracts) % uniforms.numFracts;
} else {
if (n < 0) n = 0;
if (n > uniforms.numFracts - 2) n = uniforms.numFracts - 2;
}
a = (a - n*lf)/lf;
float4 c = mix(uniforms.color[n], uniforms.color[n + 1], a);
return half4(c.r*renderColor.a,
c.g*renderColor.a,
c.b*renderColor.a,
renderColor.a);
}

fragment half4 aa_frag_txt(
TxtShaderInOut vert [[stage_in]],
texture2d<float, access::sample> renderTexture [[texture(0)]],
@@ -372,6 +408,32 @@ fragment half4 frag_grad(GradShaderInOut in [[stage_in]],
return half4(c);
}

// LinGradFrameUniforms
fragment half4 frag_lin_grad(GradShaderInOut in [[stage_in]],
constant LinGradFrameUniforms& uniforms [[buffer(0)]]) {
float3 v = float3(in.position.x, in.position.y, 1);
float a = dot(v,uniforms.params);
float lf = 1.0/(uniforms.numFracts - 1);

if (uniforms.cycleMethod > GradNoCycle) {
int fa = floor(a);
a = a - fa;
if (uniforms.cycleMethod == GradReflect && fa%2) {
a = 1.0 - a;
}
}

int n = floor(a/lf);
if (uniforms.cycleMethod > GradNoCycle) {
n = ((n % uniforms.numFracts) + uniforms.numFracts) % uniforms.numFracts;
} else {
if (n < 0) n = 0;
if (n > uniforms.numFracts - 2) n = uniforms.numFracts - 2;
}
a = (a - n*lf)/lf;
float4 c = mix(uniforms.color[n], uniforms.color[n + 1], a);
return half4(c);
}

vertex TxtShaderInOut vert_tp(VertexInput in [[stage_in]],
constant AnchorData& anchorData [[buffer(FrameUniformBuffer)]],
@@ -217,13 +217,13 @@
pixel2:(jint) pixel2;
- (void)setLinearGradientPaint:(jboolean)useMask
linear:(jboolean)linear
cycleMethod:(jboolean)cycleMethod
cycleMethod:(jint)cycleMethod
numStops:(jint)numStops
p0:(jfloat)p0
p1:(jfloat)p1
p3:(jfloat)p3
fractions:(void *)fractions
pixels:(void *)pixels;
fractions:(jfloat *)fractions
pixels:(jint *)pixels;
- (void)setRadialGradientPaint:(jboolean)useMask
linear:(jboolean)linear
cycleMethod:(jboolean)cycleMethod
@@ -336,13 +336,17 @@ - (void)setGradientPaintUseMask:(jboolean)useMask

- (void)setLinearGradientPaint:(jboolean)useMask
linear:(jboolean)linear
cycleMethod:(jboolean)cycleMethod
cycleMethod:(jint)cycleMethod
// 0 - NO_CYCLE
// 1 - REFLECT
// 2 - REPEAT

numStops:(jint)numStops
p0:(jfloat)p0
p1:(jfloat)p1
p3:(jfloat)p3
fractions:(void *)fractions
pixels:(void *)pixels
fractions:(jfloat*)fractions
pixels:(jint*)pixels
{
J2dTraceLn(J2D_TRACE_INFO, "MTLContext.setLinearGradientPaint");
[_paint setLinearGradient:useMask
@@ -64,8 +64,8 @@
p0:(jfloat)p0
p1:(jfloat)p1
p3:(jfloat)p3
fractions:(void *)fractions
pixels:(void *)pixels;
fractions:(jfloat *)fractions
pixels:(jint *)pixels;

- (void)setRadialGradient:(jboolean)useMask
linear:(jboolean)linear