#include #include #include #include "cal.h" #include "calcl.h" std::string ILmin= "il_ps_2_0\n" "ret_dyn\n" "end\n"; std::string ILcheck= "il_ps_2_0\n" "dcl_input_generic vObjIndex0.x \n" "dcl_cb cb0[1] \n" "dcl_input_position_interp(linear_noperspective) vWinCoord0.xy__\n" "itof r0,vObjIndex0.xxxx\n" "mov r0.xy__,vWinCoord0.xyxx\n" "ftoi r1.xy__,vWinCoord0.xyxx\n" "imad r2.x,r1.y,cb0[0].x,r1.x\n" "mov g[r2.x],r0 \n" "ret_dyn\n" "end\n"; using namespace std; int main(int argc, char** argv) { int n=256; int n4=n/4; int viewstartx=0; int viewstarty=0; int viewwidth= n; int viewheight=n; if(n%4!=0) { cout << "Sorry, only matrices with size a multiple \ of 4 are supported." << endl; return 1; }; if(n4<64){ cout << "Sorry, according to release notes, for a global buffer\ to work properly it has to be at least of size 64." << endl; return 1; }; std::string kernel0 = ILcheck; calInit(); CALuint numDevices = 0; calDeviceGetCount(&numDevices); cout << "Num devices =" << numDevices << endl; CALdevice device = 0; calDeviceOpen(&device, 0); CALdeviceinfo info; calDeviceGetInfo(&info, 0); CALcontext ctx = 0; calCtxCreate(&ctx, device); CALobject obj0 = NULL; CALimage image0 = NULL; CALlanguage lang0 = CAL_LANGUAGE_IL; if (calclCompile(&obj0, lang0, kernel0.c_str(), info.target) != CAL_RESULT_OK) { cout << "Kernel0 compilation failed. Exiting." << endl; return 1; } else { cout << "Kernel0 compiled fine." << endl; }; if (calclLink(&image0, &obj0, 1) != CAL_RESULT_OK) { cout << "Kernel0 linking failed. Exiting." << endl; return 1; } else { cout << "Kernel0 linked fine." << endl; }; CALresource part0=0; if(calResAllocLocal2D(&part0, device,n4,n, CAL_FORMAT_FLOAT_4, CAL_RESALLOC_GLOBAL_BUFFER) !=CAL_RESULT_OK) { cout << "part0 resource allocation failed." << endl; } else { cout << "part0 fine." << endl; }; float* part0ptr=NULL; CALuint part0pitch=0; calResMap((CALvoid**)&part0ptr, &part0pitch, part0, 0); cout << "Part0 pitch =" << part0pitch << "." << endl; for (int i = 0; i < n4; ++i) { float* tmp = &part0ptr[i * part0pitch*4]; for(int j = 0; j < n; ++j) { tmp[j] =0.f; } } calResUnmap(part0); CALresource const0=0; if(calResAllocLocal1D(&const0,device,1,CAL_FORMAT_INT_4,0) !=CAL_RESULT_OK) { cout << "const0 resource allocation failed." << endl; } else { cout << "const0 fine." << endl; }; int* const0ptr=NULL; CALuint const0pitch=0; calResMap((CALvoid**)&const0ptr, &const0pitch, const0, 0); cout << "Const0 pitch =" << const0pitch << "." << endl; const0ptr[0]=part0pitch; const0ptr[1]=0; const0ptr[2]=0; const0ptr[3]=0; calResUnmap(const0); CALmem part0mem=0; calCtxGetMem(&part0mem, ctx, part0); CALmem const0mem=0; calCtxGetMem(&const0mem, ctx, const0); CALmodule module0 = 0; calModuleLoad(&module0, ctx, image0); CALfunc func0 = 0; CALname globname0=0; CALname constname0=0; calModuleGetEntry(&func0, ctx, module0, "main"); calModuleGetName(&constname0, ctx, module0, "cb0"); calModuleGetName(&globname0, ctx, module0, "g[]"); calCtxSetMem(ctx, globname0, part0mem); calCtxSetMem(ctx, constname0, const0mem); CALevent e = 0; // Change domain here. // Try from e.g. ... // CALdomain domain0 = {0,0, n4, n}; CALdomain domain0 = {1,2, 4, 10}; //CALdomain domain0 = {1,1, 10,10}; //CALdomain domain0 = {2,2, 2, 2}; //CALdomain domain0 = {2,2, 4, 4}; //CALdomain domain0 = {1,1, 4, 4}; // Now we seem to have a top-left,bottom-right meaning! //Also note the way vObjIndex0 "wraps around" 8x8 blocks of the domain, //based on 2x2 quads (try odd values for x and y...), //presumably related to the rasterization pattern. calCtxRunProgram(&e, ctx, func0, &domain0); while (calCtxIsEventDone(ctx, e) == CAL_RESULT_PENDING);e=0; cout << calGetErrorString()<