Archived

This topic is now archived and is closed to further replies.

gimp

Understanding cg

Recommended Posts

Hello, I''m an old opengl coder who never really got time to look in to all that assembler shader stuff. Since cg has come out I''ve been ignoring that too, however the old GF2 needs replacement and I was looking at the GF-FX(in a few months). 1) From a programming perspective what can all this stuff really be used for? I''ve looked over a few shaders but I can''t seem to gauge the extents of this technology. Will it even replace the need for most textures perhaps by proceedurally creating them? 2) Is cg\shaders like a congruent method for performing special effects on a card? 3) Will this replace most of the requirements for all those Gl extentions? 4) If I add a cg block of code on a a surface in my game am I likely to run it on say most of the HW out there, if not, then what would happen on those machines? 5) How does this all fit in with OpenGL? Thanks for any answers. I have actually read some nVidia bumpf but all the ''industry leading'' marketing crap is obsuring the details. Feel free to boot me to some newbie link if these are really basic questions... Chris Brodie http:\\fourth.flipcode.com

Share this post


Link to post
Share on other sites
I heard a LOT of CG lately, I even went to NVidia''s site, and they said how nice their CG compiler is, etc. but there was no information whatsoever about WTF CG is!
Now, I might seem ignorant, but can ANYONE explain me what they are, in the first place? I mean, I got so far that it is some sort of programming language, but... nothing more.

Height Map Editor | Eternal lands

Share this post


Link to post
Share on other sites
Cg is a high level shader language. Can be used to create vertex and shader programs that run on the video card such as the ones demonstrated at cgshaders.org. And while it is made by nVidia, Cg can reportedly be compiled for other cards, such as ATI and 3DLabs (though I haven''t tested either).

--------------------------

Those who dance are considered insane by those who cannot hear the music.

Focus On: 3-D Models

Share this post


Link to post
Share on other sites
CG stands for C for graphics. Instead of programming in assembler we now program in C/C++

So you can program your GPU in CG instead of assembler code. On the NVidia website you will find all the things you need for CG including tutorials..

Share this post


Link to post
Share on other sites
quote:
Original post by gimp
Oh, and I''ve noticed that a nVidia specific compiler exists, does that mean that I need to get the ati compiler to also have my code run on ATI cards? Why is a compiler card specific?

Chris Brodie
http:\\fourth.flipcode.com


Nope.
You have to specify a target profile for compilation.

Available profiles include DX8 and DX9 vertex and pixel shaders.
Of course such profiles makes you able to run your programs on every vard supporting DX8/9 shaders.

On OGL side, supported profiles include arb vertex program and arb fragment program.
ATI cards should support them like latest nvidia drivers release.

As far as I know today Cg is the only High Level Shading Language available that can run on different cards and different systems (of course DX HLSL is bounded to win32 applications).



Share this post


Link to post
Share on other sites
Uhm just to make some concepts clearer...

quote:
Original post by gimp
1) From a programming perspective what can all this stuff really be used for? I''ve looked over a few shaders but I can''t seem to gauge the extents of this technology. Will it even replace the need for most textures perhaps by proceedurally creating them?


I don''t think shaders will ever replace textures.
Actually a shader can be used to lower CPU usage (i.e. you can implement a skinning shader, a particle system manager, etc.) or to create astonishing effects.

In order to draw polygons fast it is suggested to have vertex buffers stored in video card memory.
To modify every frame a vertex buffer can slow down your application. By using shaders you can keep your data throughput as high as possible, while the GPU makes all the work.

quote:

2) Is cg\shaders like a congruent method for performing special effects on a card?


Yes.

quote:

3) Will this replace most of the requirements for all those Gl extentions?


Yes and no. Yes because extensions like arb_vertex_program, arb_fragment_program, nv_vertex_program, etc. are directly accessed by Cg libraries. No because the bigger part of OGL extensions are not about vertex/pixel shaders. Vertex and pixel shaders come into play after you send a primitive. Everything you do before it is a your own problem..

quote:

4) If I add a cg block of code on a a surface in my game am I likely to run it on say most of the HW out there, if not, then what would happen on those machines?


You should take care of it and it depends upon the type of program you''re writing. If I have a cloth simulation on a vertex shader and I''m running my app on non-vs hw probably there''s no problem. The cloth is non-interactive and drawn always like original mesh. On the other hand, if I have a skinning shader, my charachters would never animate.

quote:

5) How does this all fit in with OpenGL?


It doesn''t fit into OpenGL. You make some calls to Cg and CgGL libs, and they directly interface with OpenGL.

Share this post


Link to post
Share on other sites
Thanks for the effort in that response. I think I have a handle on it now.

Does anybody know what say the oldest nVidia card would be that would actually do something if cg threw some programs at it?

I''m hoping that you don''t need a 9700 or FX to run these things...

Share this post


Link to post
Share on other sites
if you want to do nice stuff, you should get a radeon9700 or something equal from power (means something wich is named dx9 card on it)..

opengl2.0 will have glslang as shader language, and ati is workign on getting as much as possible from glslang working on the radeon.

cg is dead, never was alive

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
are the books like the redbook for shader programming?

i just learned opengland now iam wondering

can i draw all the surfaces and so on with this shader languages as well or do i draw it like i did til now and use the shaders to calculate for example the shadows

and where can i find a detailed description on what i could do with this shader things
everyone talks about vertex and pixel programs but nobody explains what those programs are ....


i am really confused

i personally prefer a well written book

Share this post


Link to post
Share on other sites
Shaders are little programs you write that the graphics card chip then executes. You have vertex and its elements like position, color, texture coordinate, etc. that can be modified by the vertex program you write. The pixel shader interpolates vertex color previously computed in vertex shader that it can now modify. I think output of pixel shader is pixel''s color and fog color. In new shaders ver.3, both the vertex and pixel shaders can load and sample or lookup textures and their encoded color or math function values. In older shaders only the pixel shader can sample texture. So texture is not simply something that is put onto polygons or is blended with other textures for final effect, it can contain math function that modify the vertex passed into vertex shader from your geometry. You can for example compute attenuated distance for your pixel lights by subtracting vertex''s world position from light''s world position then figure out if the geometry is in light''s radius or not. You can do this on cpu also but you can offload many ''cpu'' operations onto the gfx card''s shading hw. People are only now realizing the freedom that new shaders allow. The high level shading language (hlsl) simplifies writing vertex/pixel shaders because until now you had to write them in assembly like way with register twiddling and all that, ie. c vs. assembly. You don''t have to use shaders and instead can rely on fixed function vertex/texture blending pipeline. Shaders are more flexible though and I don''t think fixed function pipeline is going to be the ihv focus anymore. There will still be need for xforms to do on cpu like for tools, editors, etc. where shaders might not be appropriate. Though only time will tell where shaders really stand.

Share this post


Link to post
Share on other sites
hm would it be possible to use shadow coordinates instead of lightmaps?

lets say you have a static geometry and you have around 25 points per polygon and define whether or not these points are covered with shadow if so you modify the shader so it draws shadows ....you know what i mean

but how would i get the shadow coordinates to the shader?
that would be interesting
can i use vertex shaders on GF2 ultra cards? cause i am planing to update my system next summer and i can t wait to test this

Share this post


Link to post
Share on other sites
You want to use stencil shadow volumes or shadow buffers. Lightmaps are going away because of their static nature. Instead of geometry being split up to accept max lightmap size for shadows or lights you instead let stencil buffer mask off areas where light can be drawn using per-pixel lights so no need to split your polys.

You can create degenerate vertices for your geometry and extrude them inside the vertex shader creating a shadow volume poly for stencil shadow volumes. I haven''t done this but there is an article on this site that talks about it. Ati has some docs on this as well, in theri sushi engine docs I think.

Vertices go first thru tesselator then if tesselator creates additional vertices these are then passed to your vertex shader as far as I understand it. You can''t create new vertices in vertex shader I don''t think. I haven''t thought about it fully so I might be wrong, but nvidia shader docs said you couldn''t. Maybe in later version shaders you will be able to, I think once we get the primitive engine into gpu. It won''t be in gffx 1.

You can use vertex shaders that are reading from system memory vertex buffers and which are done with simd/3dnow asm cpu instructions. Pixel shaders to my knowledge can only be done in hardware except d3d''s reference rasterizer which is just software emulated pipeline and too slow for shipping apps. Opengl arb_fragment_program extension might also emulate pixel shaders in software though I''m not sure.

Today''s gfx cards support various shader versions. Gf3 supports ps1.0-1.3, radeon 8500/9000/9100 have ps1.4 and emulate ps1.0-1.3, gffx can do vs/ps 2 extended while r9700 can do vs/ps 2 only. New hw shader engines can emulate older shaders from what I understand.

Gf2 can only use vertex shaders 1.1,2.0,3.0 emulated in software. No pixel shaders. You can still use the texture blending stages for per-pixel lights and stencil support for shadows, I''ve done this but it can be slow with many dynamic lights casting shadows all over the place. The gf2 hw can also do dot3 for bump mapping and other stuff but it''s definitely not as flexible or feature rich as pixel shaders.

Share this post


Link to post
Share on other sites
well casting dynamic shadows on a brush base map ala halflife would be very difficult i stick with lightmaps for static geometry and use stencil shadows for the playermodels and entities

are there book about vertex programming?

Share this post


Link to post
Share on other sites