The Source for Java Technology Collaboration

Home » java.net Forums » Java Desktop Technologies » Java 2D

Thread: java2d Compositing -> OpenGL fragment shaders

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 5 - Last Post: Feb 22, 2007 10:37 AM by: campbell
Michele Puccini
java2d Compositing -> OpenGL fragment shaders
Posted: Feb 22, 2007 2:20 AM
  Click to reply to this thread Reply

Hello all at java2d,
Hello Chris,

after bouncing my head for some time over OpenGL, alpha compositing,
accumulated and premultiplied alpha I decided (curiosity) to take a look at
the j2se6 source code just to see what happens behind the scenes.

All I discovered (surprise ?) is that the OpenGL codepath of java2d has to
deal with premultiplied alpha pixels. That's probably because the Porter and
Duff compositing rules "work better" with premultiplied data ;)

But premultiplying and unpremultiplying is done in software loops, so this
generates some overhead, expecially when using java2d for creating offscreen
graphics with unpremultiplied images (me).

So I decided to implement my very first OpenGL fragment shader that does a
SRC_OVER composite on unpremultiplied data. I'm a true beginer, so please
forgive me.

Here's the code

-----

uniform sampler2D texture;

void main()
{
vec4 s = texture2D(texture,gl_TexCoord[0].st);
vec4 d = gl_FragColor;

float extra_alpha = 0.1;

s.a = s.a * extra_alpha;

if (s.a == 0.0) gl_FragColor = d;
else if (s.a == 1.0) gl_FragColor = s;
else if (d.a == 0.0) {gl_FragColor = s;}
else
{
float a = s.a + d.a - s.a * d.a;
gl_FragColor.a = a;
gl_FragColor.r = (s.a*s.r+d.a*d.r-s.a*d.a*d.r)/a;
gl_FragColor.g = (s.a*s.g+d.a*d.g-s.a*d.a*d.g)/a;
gl_FragColor.b = (s.a*s.b+d.a*d.b-s.a*d.a*d.b)/a;
}
}

-----

I works very well and, apart that compiler warning (!), the results are 1:1
the SRC_OVER I get from the Java2D software loops.

So the question: why don't implement the entire OpenGL java2d Composite with
fragment shaders and avoid premultiplied data ?

Cheers!

Mik
============================================================================
> ClassX Development Italy Via Francesca, 368/I I-56030 S.M. a Monte (PI) Tel.(+39)-0587-705153 Fax.(+39)-0587-705153 WEB: http://www.classx.it

Michael Toula
Re: [JAVA2D] java2d Compositing -> OpenGL fragment shaders
Posted: Feb 22, 2007 3:27 AM   in response to: Michele Puccini
  Click to reply to this thread Reply

Michele,

I guess the reason why you implement it using a fragment shader is to see
a speed increase.
What speed increase can we expect using your technique (What CPU/Graphic
card are you using) ?
I was thinking of implementing something similar for BufferedImageOp (
speed up ConvolveOp for exemple ), so I'm interested in your results.
On a side note, not everybody has a "fragment shader" capable graphic
card/OpenGL driver.

Best,
Mike
******************************************
Michael TOULA
Software Engineer

Dalim Software GmbH
Strassburger Str. 6
D-77694
Kehl am Rhein
GERMANY

tel: +49 7851 919 612
fax: +49 7851 735 76
web: www.dalim.com
******************************************



Michele Puccini <mik@CLASSX.IT>
Sent by: Discussion list for Java 2D API <JAVA2D-INTEREST@JAVA.SUN.COM>
22/02/07 11:20
Please respond to
Michele Puccini <mik@CLASSX.IT>


To
JAVA2D-INTEREST@JAVA.SUN.COM
cc

Subject
[JAVA2D] java2d Compositing -> OpenGL fragment shaders






Hello all at java2d,
Hello Chris,

after bouncing my head for some time over OpenGL, alpha compositing,
accumulated and premultiplied alpha I decided (curiosity) to take a look
at
the j2se6 source code just to see what happens behind the scenes.

All I discovered (surprise ?) is that the OpenGL codepath of java2d has to
deal with premultiplied alpha pixels. That's probably because the Porter
and
Duff compositing rules "work better" with premultiplied data ;)

But premultiplying and unpremultiplying is done in software loops, so this
generates some overhead, expecially when using java2d for creating
offscreen
graphics with unpremultiplied images (me).

So I decided to implement my very first OpenGL fragment shader that does a
SRC_OVER composite on unpremultiplied data. I'm a true beginer, so please
forgive me.

Here's the code

-----

uniform sampler2D texture;

void main()
{
vec4 s = texture2D(texture,gl_TexCoord[0].st);
vec4 d = gl_FragColor;

float extra_alpha = 0.1;

s.a = s.a * extra_alpha;

if (s.a == 0.0) gl_FragColor = d;
else if (s.a == 1.0) gl_FragColor = s;
else if (d.a == 0.0) {gl_FragColor = s;}
else
{
float a = s.a + d.a - s.a * d.a;
gl_FragColor.a = a;
gl_FragColor.r = (s.a*s.r+d.a*d.r-s.a*d.a*d.r)/a;
gl_FragColor.g = (s.a*s.g+d.a*d.g-s.a*d.a*d.g)/a;
gl_FragColor.b = (s.a*s.b+d.a*d.b-s.a*d.a*d.b)/a;
}
}

-----

I works very well and, apart that compiler warning (!), the results are
1:1
the SRC_OVER I get from the Java2D software loops.

So the question: why don't implement the entire OpenGL java2d Composite
with
fragment shaders and avoid premultiplied data ?

Cheers!

Mik
============================================================================
> ClassX Development Italy Via Francesca, 368/I I-56030 S.M. a Monte (PI)
Tel.(+39)-0587-705153 Fax.(+39)-0587-705153 WEB: http://www.classx.it
]

Michele Puccini
Re: [JAVA2D] java2d Compositing -> OpenGL fragment shaders
Posted: Feb 22, 2007 4:00 AM   in response to: Michael Toula
  Click to reply to this thread Reply

Hi Mike,

of course the main aim is always speed, speed, speed.
Your question about speed gains is quite crucial and the answer may be of course very complicated to elaborate. Just mix up some aspects:

implementation
- how can shaders be implemented in java2d-ogl ?
- what can be affected from the implemention ?
- which gfx ops can benefit from shaders implementation ?
- can we really avoid software loops in any case ?
- <add here>..

application
- what kind of application am I developing
- how can I get the most out of the new hava2d implementation
- is it true that a faster graphics operation may speedup the whole application ?
- <add here>..

hardware implementation
- are shaders implemented in my hardware ?
- which is their speed (n. of onboard shaders, pipeline implementation, gpu speed, mem speed,..) ?
- are there any compatibility issues from gpu to gpu / driver to driver ?
- <add here>..

Well, given the current and future graphics hardware implementation, the use of shaders will definitely boost java2d speeds in many ways, from advanced compositing to bufferedimage ops, to convolves.

Cheers,

Mik
============================================================================
> ClassX Development Italy Via Francesca, 368/I I-56030 S.M. a Monte (PI) Tel.(+39)-0587-705153 Fax.(+39)-0587-705153 WEB: http://www.classx.it ] OpenGL fragment shaders



Michele,

I guess the reason why you implement it using a fragment shader is to see a speed increase.
What speed increase can we expect using your technique (What CPU/Graphic card are you using) ?
I was thinking of implementing something similar for BufferedImageOp ( speed up ConvolveOp for exemple ), so I'm interested in your results.
On a side note, not everybody has a "fragment shader" capable graphic card/OpenGL driver.

Best,
Mike
******************************************
Michael TOULA
Software Engineer

Dalim Software GmbH
Strassburger Str. 6
D-77694
Kehl am Rhein
GERMANY

tel: +49 7851 919 612
fax: +49 7851 735 76
web: www.dalim.com
******************************************


Michele Puccini <mik@CLASSX.IT>
Sent by: Discussion list for Java 2D API <JAVA2D-INTEREST@JAVA.SUN.COM>
22/02/07 11:20 Please respond to
Michele Puccini <mik@CLASSX.IT>


To JAVA2D-INTEREST@JAVA.SUN.COM
cc
Subject [JAVA2D] java2d Compositing -> OpenGL fragment shaders







Hello all at java2d,
Hello Chris,

after bouncing my head for some time over OpenGL, alpha compositing,
accumulated and premultiplied alpha I decided (curiosity) to take a look at
the j2se6 source code just to see what happens behind the scenes.

All I discovered (surprise ?) is that the OpenGL codepath of java2d has to
deal with premultiplied alpha pixels. That's probably because the Porter and
Duff compositing rules "work better" with premultiplied data ;)

But premultiplying and unpremultiplying is done in software loops, so this
generates some overhead, expecially when using java2d for creating offscreen
graphics with unpremultiplied images (me).

So I decided to implement my very first OpenGL fragment shader that does a
SRC_OVER composite on unpremultiplied data. I'm a true beginer, so please
forgive me.

Here's the code

-----

uniform sampler2D texture;

void main()
{
vec4 s = texture2D(texture,gl_TexCoord[0].st);
vec4 d = gl_FragColor;

float extra_alpha = 0.1;

s.a = s.a * extra_alpha;

if (s.a == 0.0) gl_FragColor = d;
else if (s.a == 1.0) gl_FragColor = s;
else if (d.a == 0.0) {gl_FragColor = s;}
else
{
float a = s.a + d.a - s.a * d.a;
gl_FragColor.a = a;
gl_FragColor.r = (s.a*s.r+d.a*d.r-s.a*d.a*d.r)/a;
gl_FragColor.g = (s.a*s.g+d.a*d.g-s.a*d.a*d.g)/a;
gl_FragColor.b = (s.a*s.b+d.a*d.b-s.a*d.a*d.b)/a;
}
}

-----

I works very well and, apart that compiler warning (!), the results are 1:1
the SRC_OVER I get from the Java2D software loops.

So the question: why don't implement the entire OpenGL java2d Composite with
fragment shaders and avoid premultiplied data ?

Cheers!

Mik
============================================================================
> ClassX Development Italy Via Francesca, 368/I I-56030 S.M. a Monte (PI) Tel.(+39)-0587-705153 Fax.(+39)-0587-705153 WEB: http://www.classx.it ]

Michael Toula
Re: [JAVA2D] java2d Compositing -> OpenGL fragment shaders
Posted: Feb 22, 2007 5:15 AM   in response to: Michele Puccini
  Click to reply to this thread Reply

Michele,

My question was more:
What speed improvement did you personally measure using the shader
implementation vs the plain java one (x2 faster ?, x10 ? , ect...)
I'm also interested in the hardware config (CPU/GPU) you've used to take
those measures, because of course, the result will be dependent on the
hardware.
It's really just to give me an idea of what to expect :o)

Thanks,
Mike
******************************************
Michael TOULA
Software Engineer

Dalim Software GmbH
Strassburger Str. 6
D-77694
Kehl am Rhein
GERMANY

tel: +49 7851 919 612
fax: +49 7851 735 76
web: www.dalim.com
******************************************



Michele Puccini <mik@CLASSX.IT>
Sent by: Discussion list for Java 2D API <JAVA2D-INTEREST@JAVA.SUN.COM>
22/02/07 13:00
Please respond to
Michele Puccini <mik@CLASSX.IT>


To
JAVA2D-INTEREST@JAVA.SUN.COM
cc

Subject
Re: [JAVA2D] java2d Compositing -> OpenGL fragment shaders






Hi Mike,

of course the main aim is always speed, speed, speed.
Your question about speed gains is quite crucial and the answer may be of
course very complicated to elaborate. Just mix up some aspects:

implementation
- how can shaders be implemented in java2d-ogl ?
- what can be affected from the implemention ?
- which gfx ops can benefit from shaders implementation ?
- can we really avoid software loops in any case ?
- <add here>..

application
- what kind of application am I developing
- how can I get the most out of the new hava2d implementation
- is it true that a faster graphics operation may speedup the whole
application ?
- <add here>..

hardware implementation
- are shaders implemented in my hardware ?
- which is their speed (n. of onboard shaders, pipeline
implementation, gpu speed, mem speed,..) ?
- are there any compatibility issues from gpu to gpu / driver to
driver ?
- <add here>..

Well, given the current and future graphics hardware implementation, the
use of shaders will definitely boost java2d speeds in many ways, from
advanced compositing to bufferedimage ops, to convolves.

Cheers,

Mik
============================================================================
> ClassX Development Italy Via Francesca, 368/I I-56030 S.M. a Monte (PI)
Tel.(+39)-0587-705153 Fax.(+39)-0587-705153 WEB: http://www.classx.it
] OpenGL fragment shaders


Michele,

I guess the reason why you implement it using a fragment shader is to see
a speed increase.
What speed increase can we expect using your technique (What CPU/Graphic
card are you using) ?
I was thinking of implementing something similar for BufferedImageOp (
speed up ConvolveOp for exemple ), so I'm interested in your results.
On a side note, not everybody has a "fragment shader" capable graphic
card/OpenGL driver.

Best,
Mike
******************************************
Michael TOULA
Software Engineer

Dalim Software GmbH
Strassburger Str. 6
D-77694
Kehl am Rhein
GERMANY

tel: +49 7851 919 612
fax: +49 7851 735 76
web: www.dalim.com
******************************************


Michele Puccini <mik@CLASSX.IT>
Sent by: Discussion list for Java 2D API <JAVA2D-INTEREST@JAVA.SUN.COM>
22/02/07 11:20

Please respond to
Michele Puccini <mik@CLASSX.IT>



To
JAVA2D-INTEREST@JAVA.SUN.COM
cc

Subject
[JAVA2D] java2d Compositing -> OpenGL fragment shaders








Hello all at java2d,
Hello Chris,

after bouncing my head for some time over OpenGL, alpha compositing,
accumulated and premultiplied alpha I decided (curiosity) to take a look
at
the j2se6 source code just to see what happens behind the scenes.

All I discovered (surprise ?) is that the OpenGL codepath of java2d has to
deal with premultiplied alpha pixels. That's probably because the Porter
and
Duff compositing rules "work better" with premultiplied data ;)

But premultiplying and unpremultiplying is done in software loops, so this
generates some overhead, expecially when using java2d for creating
offscreen
graphics with unpremultiplied images (me).

So I decided to implement my very first OpenGL fragment shader that does a
SRC_OVER composite on unpremultiplied data. I'm a true beginer, so please
forgive me.

Here's the code

-----

uniform sampler2D texture;

void main()
{
vec4 s = texture2D(texture,gl_TexCoord[0].st);
vec4 d = gl_FragColor;

float extra_alpha = 0.1;

s.a = s.a * extra_alpha;

if (s.a == 0.0) gl_FragColor = d;
else if (s.a == 1.0) gl_FragColor = s;
else if (d.a == 0.0) {gl_FragColor = s;}
else
{
float a = s.a + d.a - s.a * d.a;
gl_FragColor.a = a;
gl_FragColor.r = (s.a*s.r+d.a*d.r-s.a*d.a*d.r)/a;
gl_FragColor.g = (s.a*s.g+d.a*d.g-s.a*d.a*d.g)/a;
gl_FragColor.b = (s.a*s.b+d.a*d.b-s.a*d.a*d.b)/a;
}
}

-----

I works very well and, apart that compiler warning (!), the results are
1:1
the SRC_OVER I get from the Java2D software loops.

So the question: why don't implement the entire OpenGL java2d Composite
with
fragment shaders and avoid premultiplied data ?

Cheers!

Mik
============================================================================
> ClassX Development Italy Via Francesca, 368/I I-56030 S.M. a Monte (PI)
Tel.(+39)-0587-705153 Fax.(+39)-0587-705153 WEB: http://www.classx.it
]

Michele Puccini
Re: [JAVA2D] java2d Compositing -> OpenGL fragment shaders
Posted: Feb 22, 2007 6:20 AM   in response to: Michael Toula
  Click to reply to this thread Reply

Since I did not implement shaders into my java code yet (only did some simple test in separate test snippets) and I'm not a ogl/shader expert, I was focused to implementation aspects. Anyway I'm on a NV7600GT 256MB DDRIII and the shaders here are generally faster than the sw implementation. Can't say a number, sorry.

Cheers,

Mik
--
----- Original Message -----
From: Michael Toula
To: JAVA2D-INTEREST@JAVA.SUN.COM
Sent: Thursday, February 22, 2007 2:15 PM
Subject: Re: [JAVA2D] java2d Compositing -> OpenGL fragment shaders



Michele,

My question was more:
What speed improvement did you personally measure using the shader implementation vs the plain java one (x2 faster ?, x10 ? , ect...)
I'm also interested in the hardware config (CPU/GPU) you've used to take those measures, because of course, the result will be dependent on the hardware.
It's really just to give me an idea of what to expect :o)

Thanks,
Mike
******************************************
Michael TOULA
Software Engineer

Dalim Software GmbH
Strassburger Str. 6
D-77694
Kehl am Rhein
GERMANY

tel: +49 7851 919 612
fax: +49 7851 735 76
web: www.dalim.com
******************************************


Michele Puccini <mik@CLASSX.IT>
Sent by: Discussion list for Java 2D API <JAVA2D-INTEREST@JAVA.SUN.COM>
22/02/07 13:00 Please respond to
Michele Puccini <mik@CLASSX.IT>


To JAVA2D-INTEREST@JAVA.SUN.COM
cc
Subject Re: [JAVA2D] java2d Compositing -> OpenGL fragment shaders







Hi Mike,

of course the main aim is always speed, speed, speed.
Your question about speed gains is quite crucial and the answer may be of course very complicated to elaborate. Just mix up some aspects:

implementation
- how can shaders be implemented in java2d-ogl ?
- what can be affected from the implemention ?
- which gfx ops can benefit from shaders implementation ?
- can we really avoid software loops in any case ?
- <add here>..

application
- what kind of application am I developing
- how can I get the most out of the new hava2d implementation
- is it true that a faster graphics operation may speedup the whole application ?
- <add here>..

hardware implementation
- are shaders implemented in my hardware ?
- which is their speed (n. of onboard shaders, pipeline implementation, gpu speed, mem speed,..) ?
- are there any compatibility issues from gpu to gpu / driver to driver ?
- <add here>..

Well, given the current and future graphics hardware implementation, the use of shaders will definitely boost java2d speeds in many ways, from advanced compositing to bufferedimage ops, to convolves.

Cheers,

Mik
============================================================================
> ClassX Development Italy Via Francesca, 368/I I-56030 S.M. a Monte (PI) Tel.(+39)-0587-705153 Fax.(+39)-0587-705153 WEB: http://www.classx.it ] OpenGL fragment shaders


Michele,

I guess the reason why you implement it using a fragment shader is to see a speed increase.
What speed increase can we expect using your technique (What CPU/Graphic card are you using) ?
I was thinking of implementing something similar for BufferedImageOp ( speed up ConvolveOp for exemple ), so I'm interested in your results.
On a side note, not everybody has a "fragment shader" capable graphic card/OpenGL driver.

Best,
Mike
******************************************
Michael TOULA
Software Engineer

Dalim Software GmbH
Strassburger Str. 6
D-77694
Kehl am Rhein
GERMANY

tel: +49 7851 919 612
fax: +49 7851 735 76
web: www.dalim.com
******************************************

Michele Puccini <mik@CLASSX.IT>
Sent by: Discussion list for Java 2D API <JAVA2D-INTEREST@JAVA.SUN.COM>
22/02/07 11:20
Please respond to
Michele Puccini <mik@CLASSX.IT>



To JAVA2D-INTEREST@JAVA.SUN.COM
cc
Subject [JAVA2D] java2d Compositing -> OpenGL fragment shaders









Hello all at java2d,
Hello Chris,

after bouncing my head for some time over OpenGL, alpha compositing,
accumulated and premultiplied alpha I decided (curiosity) to take a look at
the j2se6 source code just to see what happens behind the scenes.

All I discovered (surprise ?) is that the OpenGL codepath of java2d has to
deal with premultiplied alpha pixels. That's probably because the Porter and
Duff compositing rules "work better" with premultiplied data ;)

But premultiplying and unpremultiplying is done in software loops, so this
generates some overhead, expecially when using java2d for creating offscreen
graphics with unpremultiplied images (me).

So I decided to implement my very first OpenGL fragment shader that does a
SRC_OVER composite on unpremultiplied data. I'm a true beginer, so please
forgive me.

Here's the code

-----

uniform sampler2D texture;

void main()
{
vec4 s = texture2D(texture,gl_TexCoord[0].st);
vec4 d = gl_FragColor;

float extra_alpha = 0.1;

s.a = s.a * extra_alpha;

if (s.a == 0.0) gl_FragColor = d;
else if (s.a == 1.0) gl_FragColor = s;
else if (d.a == 0.0) {gl_FragColor = s;}
else
{
float a = s.a + d.a - s.a * d.a;
gl_FragColor.a = a;
gl_FragColor.r = (s.a*s.r+d.a*d.r-s.a*d.a*d.r)/a;
gl_FragColor.g = (s.a*s.g+d.a*d.g-s.a*d.a*d.g)/a;
gl_FragColor.b = (s.a*s.b+d.a*d.b-s.a*d.a*d.b)/a;
}
}

-----

I works very well and, apart that compiler warning (!), the results are 1:1
the SRC_OVER I get from the Java2D software loops.

So the question: why don't implement the entire OpenGL java2d Composite with
fragment shaders and avoid premultiplied data ?

Cheers!

Mik
============================================================================
> ClassX Development Italy Via Francesca, 368/I I-56030 S.M. a Monte (PI) Tel.(+39)-0587-705153 Fax.(+39)-0587-705153 WEB: http://www.classx.it ]

campbell

Posts: 63
Re: java2d Compositing -> OpenGL fragment shaders
Posted: Feb 22, 2007 10:37 AM   in response to: Michele Puccini
  Click to reply to this thread Reply

BTW, if you're following this thread on the forum (and not the mailing list), I posted a long reply on a separate thread:
http://forums.java.net/jive/thread.jspa?threadID=23376&tstart=0

Thanks,
Chris




 XML java.net RSS