Sample Code

From ShWiki

Revision as of 17:44, 30 March 2006; view current revision
←Older revision | Newer revision→

Here are some examples of programs which use Sh. Feel free to suggest additions to this page if you want.

Contents

Simple stream program

Here is a minimal stream program showing how to create storage for program inputs and outputs.

#include <sh/sh.hpp>
#include <iostream>

using namespace std;
using namespace SH;

int main()
{
  shInit();

  ShProgram prg = SH_BEGIN_PROGRAM("gpu:stream") {
    ShInputAttrib3f a;
    ShOutputAttrib3f b;
    b = a + ShAttrib3f(42.0, 42.0, 42.0);
  } SH_END;

  float data[] = { 1.0, 0.5, -0.5 };
  ShHostMemoryPtr mem_in = new ShHostMemory(sizeof(float) * 3, data, SH_FLOAT);
  ShChannel<ShAttrib3f> in(mem_in, 1);

  float outdata[3];
  ShHostMemoryPtr mem_out = new ShHostMemory(sizeof(float) * 3, outdata, SH_FLOAT);
  ShChannel<ShAttrib3f> out(mem_out, 1);

  out = prg << in;
  
  mem_out->hostStorage()->sync();
  float* results = static_cast<float*>(mem_out->hostStorage()->data());
  cout << "out = (" << results[0] << ", " << results[1] << ", "
       << results[2] << ")" << endl;
}

It should simply output the following:

out = (43, 42.5, 41.5)

FIX: Try to insert these lines right after main if you encountered some GL runtime error(test on Windows/Visual C++ 7.1). It seems that it went down without initializing OpenGL...hmm, I have no idea, but if anyone could tell me, thanks in advance.

  glutInitDisplayMode(GLUT_RGBA);
  glutInitWindowSize(512, 512);
  glutCreateWindow("Sh Sample Code");
  SH::shSetBackend("glsl");
Hmmm, that's strange, it shouldn't be necessary to initialize any of this, it should be taken care of by Sh. Which version of Sh are you using? 0.8.0rc0 ? Another thing to try is to only include the setBackend("glsl") call in case the thing that's broken on your machine is the arb backend (the default backend if you don't set any backend). Feel free to discuss this bug further on the mailing list if you have anything else to add. --Francois 11:45, 9 January 2006 (EST)


Simply setting the backend tends to work right now, but you should really call "shInit" before using Sh to initialize the internal context (Note: it *may* be necessary to create a window and initialize a GL context before calling shInit... I don't remember). It's probably also a good idea to subsequently call "shUseBackend(...)" for each backend that you want available. --atlaurit 16:16, 30 January 2006 (EST)



Could someone provide the gcc - command (link to which libraries etc.) for total beginners linke me who just want to verify the correct installation of Sh?


Sure can ... assuming you have both glut (http://freeglut.sourceforge.net/) and sh libraries installed you should be able to run

g++ source.cpp -o simpleStream -lsh -lglut

The code -does- compile without error if you do not include the glut library ... though it fails at runtime with something similar to

 symbol lookup error: /usr/local/lib/sh/libshgl.so.0: undefined symbol: glXGetCurrentDisplay

--Rob 12:04, 23 January 2006 (EST)

Using the connect operator

The connect operator allows one to "chain" shaders together. This is described in the book on page 190. Here's an example extending the stream program described above.

Here's the new shader which does the actual doubling:

 ShProgram double_prg = SH_BEGIN_PROGRAM("gpu:stream") {
   ShInputAttrib3f a;
   ShOutputAttrib3f b;
   b = 2 * a;
 } SH_END;

and here's how it can be chained to the previous one:

 out = double_prg << prg << in;

Making this change to the sample stream program will now make it return the following:

 out = (86, 85, 83)

Note that the doubling happens after the original shader but that it is positioned to the left of prg.

Discussing the examples in the libsh-(version)/examples directory

particle

This is an example on how to use streams with Sh.

When using libsh-0.8.0rc0, I noticed severe performance differences when changing between the 2 settings cpu:stream and gpu:stream in the main.cpp. The strange part was that the CPU was much faster than the GPU setting (well, it should be the other way round, otherwise, what would be the point? ;)

Is this simply a result of the unmature state of the stream-computing part or is it just the fault of my graphics card? Could anyone with a better card (nvidia 6800/7800) verify this? --Hmueller 03:46, 27 January 2006 (EST)

Talking to sdt on the IRC channel verified that it was due to inefficiencies in the current code --Hmueller 12:38, 27 January 2006 (EST)