Sample Code

From ShWiki

(Difference between revisions)
Revision as of 04:04, 7 January 2006
RocWood (Talk | contribs)
Simple stream program
← Previous diff
Revision as of 16:45, 9 January 2006
Francois (Talk | contribs)
Having to initialize glut and set the backend is probably a bug...
Next diff →
Line 43: Line 43:
glutCreateWindow("Sh Sample Code"); glutCreateWindow("Sh Sample Code");
SH::shSetBackend("glsl");</cpp> SH::shSetBackend("glsl");</cpp>
 +
 +: 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 <tt>setBackend("glsl")</tt> 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 [http://lists.libsh.org/cgi-bin/mailman/listinfo/libsh-users mailing list] if you have anything else to add. --[[User_Francois|Francois]] 11:45, 9 January 2006 (EST)

Revision as of 16:45, 9 January 2006

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

Simple stream program

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

  1. #include "sh.hpp"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5. using namespace SH;
  6.  
  7. int main()
  8. {
  9. ShProgram prg = SH_BEGIN_PROGRAM("gpu:stream") {
  10. ShInputAttrib3f a;
  11. ShOutputAttrib3f b;
  12. b = a + ShAttrib3f(42.0, 42.0, 42.0);
  13. } SH_END;
  14.  
  15. float data[] = { 1.0, 0.5, -0.5 };
  16. ShHostMemoryPtr mem_in = new ShHostMemory(sizeof(float) * 3, data, SH_FLOAT);
  17. ShChannel<ShAttrib3f> in(mem_in, 1);
  18.  
  19. float outdata[3];
  20. ShHostMemoryPtr mem_out = new ShHostMemory(sizeof(float) * 3, outdata, SH_FLOAT);
  21. ShChannel<ShAttrib3f> out(mem_out, 1);
  22.  
  23. out = prg << in;
  24. float* results = static_cast<float*>(mem_out->hostStorage()->data());
  25. cout << "out = (" << results[0] << ", " << results[1] << ", "
  26. << results[2] << ")" << endl;
  27. }

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.

  1. glutInitDisplayMode(GLUT_RGBA);
  2. glutInitWindowSize(512, 512);
  3. glutCreateWindow("Sh Sample Code");
  4. 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)