📄 voutfloatn.br
字号:
#include <stdio.h>#include <stdlib.h>unsigned int debug_vout = 0;#define sentinelStream brook::sentinelStream#ifdef _WIN32// do not let it warn us that we use more than 64K lines of code#pragma warning(disable:4049)#endif#define debugStreamPrint(stream,title) if (debug_vout) { printf ("%s\n",title); streamPrint(stream,1); printf ("\n\n"); }kernel void kernelReadItem (float items[][], float2 index, out float item<>) { item = items[index];}void cpuGetIndexAt(float2 inputindex, float shiftRight, float2 maxvalue, float2 *index) { index->x=inputindex.x+shiftRight; index->y=inputindex.y+floor(index->x/maxvalue.x); index->x=fmod(index->x,maxvalue.x); if (index->x<0) index->x+=maxvalue.x;//only necessary if shiftRight<0}kernel void getIndexAt(float4 inputindex, float shiftRight, float2 maxvalue, out float2 outputindex<>) { float2 index; index.x=inputindex.x+shiftRight; index.y=inputindex.y+floor((.5+index.x)/maxvalue.x); index.x=round(fmod(round(index.x),maxvalue.x)); if (index.x<=-.50) index.x+=maxvalue.x;//only necessary if shiftRight<0 if (index.x+.25 >= maxvalue.x) index.x = 0;//if fmod fails us outputindex=index; // printf(maxvalue.x,maxvalue.y,outputindex.x,outputindex.y);}const unsigned int MAX_VOUT_STREAMS=31;//if you change this, change one 5 lineskernel void calculateDividedIndex(float4 index, float modulus, float length, out float2 newindex<>){ float epsilon=1.0f/32.0f;//this is needed because the division may result in // loss of accuracy. We know that for a 2048 texture the mantissa holds // 1/32 precision newindex=float2(index.x,index.y); newindex/=modulus; newindex.x=floor(fmod(newindex.x+frac(newindex.y)*length+epsilon,length)); newindex.y=floor(newindex.y+epsilon);}kernel void calculateIndexModulus (float4 index, float modulus, float offset, float lengthmodmodulus, out float which <>) { which= floor(fmod(index.y*lengthmodmodulus + fmod(index.x,modulus), modulus)-offset);}#define finite_float2(output) finite_float(output.x)#define finite_float3(output) finite_float(output.x)#define finite_float4(output) finite_float(output.x)#define VECTOR_TEMPLATIZED_FUNCTIONSreduce void valueProducedfloat (float input <>, reduce float output<>) { output=isinf(input.x)?output:input;}kernel void isFiniteKernelfloat(float inp<>, out float outp<>) { outp=!isinf(inp.x);}int finiteValueProducedfloat (float input<>) { float output<1,1>; float finiteout<1,1>; float rettype; float ret; debugStreamPrint (input,"Finite Values in..."); valueProducedfloat(input,output); streamWrite(output,&rettype); isFiniteKernelfloat(output,finiteout); streamWrite(finiteout,&ret); return (int)ret;}kernel void valueAtfloat (float value[][], float2 index, out float output<>, float2 maxvalue, float nothing) { if (index.y>=maxvalue.y||index.y<-.1) output = nothing; else output = value[index];}kernel void NanToBoolRightfloat (float value[][], out float output<>, float sign, float2 maxvalue) { float2 nextPlaceToLook; float neighbor; getIndexAt(indexof(output),sign,maxvalue,nextPlaceToLook); valueAtfloat(value,nextPlaceToLook,neighbor,maxvalue,0); output = (isinf(value[indexof(output)].x)?1:0) + (isinf(neighbor.x)?1:0);}kernel void NanToRightfloat (float value [][], out float output<>, float twotoi, float2 maxvalue) { float2 nextPlaceToLook; float neighbor; getIndexAt(indexof(output),twotoi,maxvalue,nextPlaceToLook); valueAtfloat(value,nextPlaceToLook,neighbor,maxvalue,0); output = round(value[indexof(output)]+neighbor);}kernel void CountToRightfloat (float value [][], out float output<>, float twotoi, float2 maxvalue) { float2 nextPlaceToLook; float neighbor; getIndexAt(indexof(output),twotoi,maxvalue,nextPlaceToLook); valueAtfloat(value,nextPlaceToLook,neighbor,maxvalue,0); output = value[indexof(output)]+neighbor;}kernel void GatherGuessfloat(float scatterindex[][], out float output<>, float value[][], float twotologkminusi, float2 maxvalue, float halfk, float sign) { float neighbor; float2 nextPlaceToLook; getIndexAt(indexof(output),-sign*halfk,maxvalue,nextPlaceToLook); valueAtfloat(scatterindex,nextPlaceToLook,neighbor,maxvalue,0); if (neighbor>halfk) { output=halfk+twotologkminusi; }else { float actualValue; valueAtfloat(value,nextPlaceToLook,actualValue,maxvalue,0); if (neighbor==halfk&&!isinf(actualValue.x)) { output=halfk; }else { output = halfk-twotologkminusi; } }}kernel void EstablishGuessfloat(float scatterindex[][], out float output<>, float value[][], float twotologkminusi, float2 maxvalue, float halfk, float sign) { if (scatterindex[indexof(output)]==0) { output=0; } else { GatherGuessfloat(scatterindex, output, value, twotologkminusi, maxvalue, halfk, sign); }}kernel void UpdateGuessfloat(float scatterindex[][], out float output<>, float value[][], float twotologkminusi, float2 maxvalue, float lastguess<>, float sign) { GatherGuessfloat(scatterindex, output, value, twotologkminusi, maxvalue, lastguess, sign);}kernel void RelativeGatherfloat(out float output<>, float gatherindex[][], float value[][], float inf<>, float sign, float2 maxvalue, float maxshift) { float2 nextPlaceToLook; float2 isoffedge; getIndexAt(indexof(output), -sign.x*gatherindex[indexof(output)], maxvalue, nextPlaceToLook); getIndexAt(indexof(output),-sign.x*maxshift,maxvalue,isoffedge); isoffedge-=maxvalue; if (isoffedge.y>=-.0625 || (isoffedge.y>=-1.0625&&isoffedge.x>=-.0625)) { output=inf; }else { output=value[nextPlaceToLook]; }}float shiftValuesfloat(float list_stream <>, float (*output_stream)<>, int WIDTH, int LENGTH, int sign) { float tmp_stream<WIDTH,LENGTH>; float ret_stream<WIDTH,LENGTH>; float guess_stream<WIDTH,LENGTH>; unsigned int i; float2 maxvalue; unsigned int logN; unsigned int LogNMinusK; float maxshift; maxvalue.x=(float)LENGTH;maxvalue.y=(float)WIDTH; logN=(unsigned int)ceil(log((float)LENGTH*WIDTH)/log(2.0f)); debugStreamPrint(list_stream,"Combined..."); NanToBoolRightfloat (list_stream,ret_stream,(float)sign,maxvalue); for (i=1;i<logN;++i) { streamSwap(ret_stream,tmp_stream); NanToRightfloat(tmp_stream,ret_stream,(float)sign*(1<<i),maxvalue); } debugStreamPrint(ret_stream,"scattering..."); { float item<1>; float2 index; if (sign==-1) { index.y = (float)(WIDTH-1); index.x = (float)(LENGTH-1); }else { index.y=index.x=0; } kernelReadItem(ret_stream,index,item); streamWrite(item,&maxshift); } LogNMinusK=logN-2; i= logN-1;//could make this k! rather than N // where k = num elements pushed (N-logN%2?ret_stream,tmp_stream EstablishGuessfloat(ret_stream, guess_stream, list_stream, (float)(1 << LogNMinusK), maxvalue, (float)(1<<i), (float)sign); for (i=1;i<logN;++i) { LogNMinusK=logN-1-i; streamSwap(tmp_stream,guess_stream); UpdateGuessfloat (ret_stream,//scatter values guess_stream,//new guess list_stream,//actual values (float)(1<<LogNMinusK), maxvalue, tmp_stream,//old guess (float)sign); } debugStreamPrint(guess_stream,"Gather Value"); if (1) { unsigned int size,width; size = (unsigned int)LENGTH*(unsigned int)WIDTH - (unsigned int) maxshift; width = size/LENGTH+((size%LENGTH)?1:0); if (1) { float proper_output_stream<width,LENGTH>; if (width) { RelativeGatherfloat(proper_output_stream, guess_stream, list_stream, *sentinelStream(2), (float)sign, maxvalue, maxshift); } streamSwap(*output_stream,proper_output_stream); } debugStreamPrint(*output_stream, "Final Value"); } return maxshift;}kernel void floatstreamCombine1(float input0[][], float modulus, float offset, float length, float lengthmodmodulus, float oldoutput<>, out float output <>) { float2 newindex; float whichmod; calculateDividedIndex(indexof(output), modulus, length, newindex); calculateIndexModulus(indexof(output), modulus, offset, lengthmodmodulus, whichmod); if (whichmod==0||whichmod==modulus) { output=input0[newindex]; }else { output=oldoutput; }}kernel void floatstreamCombine2f(float input0[][], float input1[][], float modulus, float length, float lengthmodmodulus, out float output <>) { float2 newindex; float whichmod; calculateDividedIndex(indexof(output), modulus, length, newindex); calculateIndexModulus(indexof(output), modulus, -0.5f, lengthmodmodulus, whichmod); if (whichmod==0||whichmod==modulus) { output=input0[newindex]; }else { output=input1[newindex]; }}kernel void floatstreamCombine2(float input0[][], float input1[][], float modulus, float offset, float length, float lengthmodmodulus, float oldoutput<>, out float output <>) { float2 newindex; float whichmod; calculateDividedIndex(indexof(output), modulus, length, newindex); calculateIndexModulus(indexof(output), modulus, offset, lengthmodmodulus, whichmod); if (whichmod==0||whichmod==modulus) { output=input0[newindex]; }else if (whichmod==1) { output=input1[newindex]; }else { output=oldoutput; }}kernel void floatstreamCombine3f(float input0[][], float input1[][], float input2[][], float modulus, float length, float lengthmodmodulus, out float output <>) { float2 newindex; float whichmod;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -