Sample Code
From ShWiki
(Difference between revisions)
Revision as of 22:00, 13 October 2005
[edit]
Simple stream program
Here is a minimal stream program showing how to create storage for program inputs and outputs.
#include "sh.hpp"
#include <iostream>
using namespace std;
using namespace SH;
int main()
{
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;
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)
