Shader examples

Started by spasi, March 30, 2004, 23:51:59

Previous topic - Next topic

spasi

Just added 4 shader examples in cvs. They can be found in org.lwjgl.test.opengl.shaders package. It's actually one program, the ShadersTest class contains the main(), you pass arguments to get different shaders. I kept the shaders simple, just showing how to use them through lwjgl.

There's only one argument, accepting the following values:

"none" - Doesn't use any shaders, a plain lit sphere is rendered.
"vp" - Uses ARB_vertex_program to do a per-vertex effect.
"fp" - Uses ARB_vertex_program + ARB_fragment_program to do a per-pixel effect.
"vsh" - Uses ARB_vertex_shader to do the same effect as vp.
"fsh" - Uses ARB_vertex_shader + ARB_fragment_shader to do the same effect as fp.

Please use the newest drivers you can get.

"vp" should run on most hardware out there, although in software mode in older cards.
"fp" on GeForce FX+, Radeon 9500+, even on plain GeForces in software mode (NVEmulate)

I don't expect "vsh"+"fsh" to run correctly, or even run at all, given really-really buggy drivers. ATI is the only one with official GLSL support, but AFAIK they still have a lot of bugs. Haven't tested on their hardware though. As for NVIDIA... you'll need the newest drivers and a registry hack (win32 only) to make it work. Add the following:

in "HKEY_LOCAL_MACHINE\SOFTWARE\NVIDIA Corporation\Global\OpenGL\Debug" create:
REG_WORD "ShaderObjects" (value=1)
REG_WORD "WriteProgramObjectAssembly" (value=1), if you want to get a low-level output of your shader programs. Some .txt files will be written in your app directory, containing the sources. Very useful!

"vsh" should be supported on GeForce 3+4 and Radeon 8500 hardware and up. Or even lower in software mode. Although the NV implementation is messed up (for now). They compile to VP2.0 profile, even though nothing is used from it, which of course is not supported on pre-FX hardware. Too bad.
As for "fsh", same as "fp".

Have fun!

Matzon

Man, and I just uninstalled my GLSL drivers :/
Good work though!

I am getting:
Quote
Info Log of Shader Object: shaderFSH.vsh
--------------------------
ERROR: 0:9: 'dot' : no matching overloaded function found
ERROR: 0:10: 'dot' : no matching overloaded function found
ERROR: 2 compilation errors.  No code generated.


The ShaderTest program was terminated because an error occured.

Reason: A compilation error occured in a vertex shader.

and

Quote
Info Log of Shader Object: shaderVSH.vsh
--------------------------
ERROR: 0:8: 'dot' : no matching overloaded function found
ERROR: 0:9: 'dot' : no matching overloaded function found
ERROR: 0:19: '+' :  wrong operand types  no operation '+' exists that takes a left-hand operand of type '3-component vector of float' and a right operand of type 'uniform 4-component vector of float' (or there is no acceptable conversion)
ERROR: 0:20: '+' :  wrong operand types  no operation '+' exists that takes a left-hand operand of type '4-component vector of float' and a right operand of type '3-component vector of float' (or there is no acceptable conversion)
ERROR: 0:20: 'assign' :  cannot convert from '4-component vector of float' to '3-component vector of float'
ERROR: 5 compilation errors.  No code generated.


The ShaderTest program was terminated because an error occured.

Reason: A compilation error occured in a vertex shader.

The none, vp and fp run fine though - so it is probably because of my buggy glsl drivers in 4.3

spasi

Hmm, I think that MY drivers are buggy! I'm not sure, but I believe that it works here because NV actually goes against the spec. Now that I think about it, there's no auto conversion between types in GLSL. The problem is that I do operations between vec3 and vec4 (although I actually use only the 3 first components), which is not allowed.

Try putting a vec3(...) around vec4s I use, I'll have to return home to fix it in cvs.

For example:

dot(normal, vec3(gl_LightSource[0].position))
color3D = diffuseDot * color3D + vec3(gl_LightModel.ambient)

spasi

I forgot to mention that with -/+ you control the sphere specularity. Just a little example of how to pass parameters to your shaders.

spasi

It should work OK now. I also fixed a stupid nv driver bug. Tell me how it works.

By the way, our glu sphere has a hole on it. Someone should fix this.

Matzon

QuoteInfo Log of Shader Object: shaderFSH.fsh
--------------------------
ERROR: 0:11: 'assign' :  cannot convert from '3-component vector of float' to '2
-component vector of float'
ERROR: 1 compilation errors.  No code generated.

vsh works fine now

elias

That's a very nice demo collection, even though my linux nvidia drivers does not support GLSL yet. One thing though, the chooseMode calls is not really appropriate for linux, since all the modes returned have 24 as color depth, not 32 or 16. I fixed it here by just selecting 24 instead of 32 at the first call, but it should be fixed a better way. Why not sort all display modes after how close they are to 640,480,16,60 and choose the first one?

- elias

spasi

OK, fixed the bugs.

elias, I heard the v60 forceware (probably available at NV40 launch) will get linux GLSL support. I don't know how's ATI doing though...

I personally can't wait for decent drivers to appear. Currently Marathon has 19 (!) different ways (shadow mapping on/off, bump mapping on/off, parallax mapping on/off, register combiners, ati fragment shaders, fp, nvfp, now glsl) to render the terrain and it's getting bigger and bigger. GLSL will solve most of our problems.