⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subdiv.br

📁 用于GPU通用计算的编程语言BrookGPU 0.4
💻 BR
📖 第 1 页 / 共 3 页
字号:
  float generous=0;  if(i.x>=0&&i.x<65536/*&&(tri.A.w!=0||tri.B.w!=0||tri.C.w!=0)*/) {    if ((generous||oldneighbors.AB.w==0)&&tri.A.w==0) {      //that triangle is rastered as is, we better push out a repair triangle      cracksA=float3(i.x,i.y,0);      push(cracksA);      if ((generous||oldneighbors.BC.w==0)&&tri.B.w==0) {        //that triangle is rastered as is, we better push out a repair triangle        cracksB=float3(i.x,i.y,1);        push(cracksB);      }      if ((generous||oldneighbors.AC.w==0)&&tri.C.w==0) {        //that triangle is rastered as is, we better push out a repair triangle        cracksB=float3(i.x,i.y,2);        push(cracksB);      }    }else    if ((generous||oldneighbors.BC.w==0)&&tri.B.w==0) {     //that triangle is rastered as is, we better push out a repair triangle      cracksA=float3(i.x,i.y,1);      push(cracksA);      if ((generous||oldneighbors.AC.w==0)&&tri.C.w==0) {        //that triangle is rastered as is, we better push out a repair triangle        cracksB=float3(i.x,i.y,2);        push(cracksB);      }    }else    if ((generous||oldneighbors.AC.w==0)&&tri.C.w==0) {      //that triangle is rastered as is, we better push out a repair triangle      cracksA=float3(i.x,i.y,2);      push(cracksA);    }  }}kernel SplitTri splitTriangles (STri tri<>, Neighbor oldneighbors<>, float noAdaptive<>) {  SplitTri splittri;  float eAAB=isNeighbor(oldneighbors.AAB);  float eAAC=isNeighbor(oldneighbors.AAC);  float eABB=isNeighbor(oldneighbors.ABB);  float eBBC=isNeighbor(oldneighbors.BBC);  float eBCC=isNeighbor(oldneighbors.BCC);  float eACC=isNeighbor(oldneighbors.ACC);  float3 fiveandhalf=5.5;  float3 fourandhalf=4.5;  float3 weights={.625,.579533905371,0.515625};  float3 ABCneighbors = {4.0f+eAAC+eAAB,4.0f+eBBC+eABB,4.0f+eBCC+eACC};  float3 abcn =ABCneighbors>fiveandhalf?weights.xxx:(ABCneighbors>fourandhalf?weights.yyy:weights.zzz);  //float an = .375+(.375+sqr(.25*cos(2*3.1415926536/Aneighbors)));  //float bn = //.375+(.375+sqr(.25*cos(2*3.1415926536/Bneighbors)));  //float cn = //.375+(.375+sqr(.25*cos(2*3.1415926536/Cneighbors)));  float an = abcn.x;  float bn = abcn.y;  float cn = abcn.z;  float3 abcnon = (1-abcn)/ABCneighbors;  float anon = abcnon.x;  float bnon = abcnon.y;  float cnon = abcnon.z;  splittri.AC = avg4 (tri.A,tri.C);  if(1)if(noAdaptive||tri.C.w)    splittri.AC = addN(lerp(splittri.AC.xyz,                            avg (tri.B.xyz,oldneighbors.AC.xyz),                            .25),                       1);  splittri.AB = avg4 (tri.A,tri.B);  if(1)if(noAdaptive||tri.A.w)    splittri.AB = addN(lerp(splittri.AB.xyz,                                avg(tri.C.xyz,oldneighbors.AB.xyz),                                .25),                           1);  splittri.BC = avg4 (tri.B,tri.C);  if(1)if(noAdaptive||tri.B.w)    splittri.BC = addN(lerp(splittri.BC.xyz,                                avg(tri.A.xyz,oldneighbors.BC.xyz),                                .25),                           1);  splittri.A = tri.A;  if(1)if(noAdaptive||(tri.A.w&&                       tri.C.w&&                       (oldneighbors.AC.w==4||oldneighbors.AC.w==8)&&                       (oldneighbors.AB.w==2||oldneighbors.AB.w==8)&&                       oldneighbors.AAB.w&&                       oldneighbors.AAC.w))            splittri.A.xyz = tri.A.xyz*an        +anon*add6(oldneighbors.AB.xyz,                   tri.B.xyz,                   tri.C.xyz,                   oldneighbors.AC.xyz,                   oldneighbors.AAC.xyz,                   oldneighbors.AAB.xyz);  splittri.B = tri.B;  if(1)if(noAdaptive||(tri.A.w&&                       tri.B.w&&                       (oldneighbors.BC.w==2||oldneighbors.BC.w==8)&&                       (oldneighbors.AB.w==4||oldneighbors.AB.w==8)&&                       oldneighbors.ABB.w&&                       oldneighbors.BBC.w))            splittri.B.xyz= tri.B.xyz*bn       +bnon*add6(oldneighbors.ABB.xyz,                  oldneighbors.BBC.xyz,                  oldneighbors.BC.xyz,                  tri.C.xyz,                  tri.A.xyz,                  oldneighbors.AB.xyz);  splittri.C = tri.C;  if(1)if(noAdaptive||(tri.C.w&&                       tri.B.w&&                       (oldneighbors.BC.w==4||oldneighbors.BC.w==8)&&                       (oldneighbors.AC.w==2||oldneighbors.AC.w==8)&&                       oldneighbors.ACC.w&&                       oldneighbors.BCC.w))     splittri.C.xyz = tri.C.xyz*cn        +cnon*add6(tri.B.xyz,                   oldneighbors.BC.xyz,                   oldneighbors.BCC.xyz,                   oldneighbors.ACC.xyz,                   oldneighbors.AC.xyz,                   tri.A.xyz);  return splittri;}//this function takes as input an index that tells which triangle must be split//the output is 6 new vertices that make up the 4 new triangles and 12 neighbors // (stored in 9 float4s)kernel void splitTrianglesAdaptive (STri triangles[][],                                     Neighbor neighbors[][],                                    float2 i<>,                                    out SplitTri splittri<>) {  float2 zero2=0;  float2 index = (i>=0&&i<65536)?i:zero2;  STri tri=triangles[index];  Neighbor oldneighbors=neighbors[index];  splittri=splitTriangles(tri,oldneighbors,0);}kernel void splitTrianglesNoAdaptive (STri triangles<>,                                       Neighbor neighbors<>,                                      out SplitTri splittri<>) {  splittri=splitTriangles(triangles,neighbors,1);}/*void __printf_cpu_inner(float f) {  printf("%.2f ",f);  }*/void __printnew_cpu_inner() {  printf ("\n");}kernel float whichMod4 (float2 index, float stretchX) {  float num;  num= (stretchX*index.x+((float)(stretchX!=1))*index.y);  num = fmod(num,4.0f);  return num>3.5?0:num;}//moves the 6 split values into 4 individual triangles (with a result of 12 vertices)//moves the 12 split neighbors into 4 individual neighbor lists//uses fmod with x or y to determine which direction should matter wrt fmod.kernel void linearReorgSplitTriangles (SplitTri splittri<>,                                       Neighbor newnei<>,                                       out STri tri<>,                                       out Neighbor neighbor<>,                                       float stretchX) {  //  float sindex = stretchX?(indexof tri).x:(indexof tri).y;  //  float unclampedwhich = round(fmod(sindex,4));  //  float which = whichMod4(unclampedwhich>3.5?0:unclampedwhich;  float which = whichMod4((indexof tri).xy,stretchX);  float4 ac = splittri.AC;  float4 ab = splittri.AB;  float4 bc = splittri.BC;  float4 a = splittri.A;  float4 b = splittri.B;  float4 c = splittri.C;  float4 BC_C= {newnei.BC.w,                newnei.ACC.w,                newnei.BCC.w,1};  float4 AC_A = {newnei.AC.w,                 newnei.AAB.w,                 newnei.AAC.w,1};  float4 AB_B = {newnei.AB.w,                 newnei.BBC.w,                 newnei.ABB.w,1};    if (which<.5) {    tri.A=identity(a,1);    tri.B=identity(ab,1);    tri.C=identity(ac,1);    neighbor.BCC = c;    neighbor.BC = bc;    neighbor.BBC= b;    neighbor.ABB= AB_B;    neighbor.AB = newnei.AB;    neighbor.AAB =  newnei.AAB;    neighbor.AAC =  newnei.AAC;    neighbor.AC = AC_A;    neighbor.ACC = newnei.AC;  }else if (which <1.5) {    tri.A= identity(ab,1);    tri.B=identity(b,1);    tri.C=identity(bc,1);    neighbor.BCC = BC_C;    neighbor.BC = newnei.BC;//b0rken    neighbor.BBC=newnei.BBC;    neighbor.ABB=newnei.ABB;    neighbor.AB = AB_B;    neighbor.AAB = newnei.AB;    neighbor.AAC = a;    neighbor.AC = ac;    neighbor.ACC = c;  }else if (which<2.5) {        tri.A=identity(ac,1);    tri.B=identity(bc,1);    tri.C=identity(c,1);    neighbor.BCC=newnei.BCC;    neighbor.BC=BC_C;    neighbor.BBC = newnei.BC;//b0rken!?    neighbor.ABB = b;    neighbor.AB = ab;    neighbor.AAB = a;    neighbor.AAC = AC_A;    neighbor.AC = newnei.AC;    neighbor.ACC = newnei.ACC;  }else {    tri.A=identity(bc,1);    tri.B=identity(ac,1);    tri.C=identity(ab,1);    neighbor.BCC = newnei.AB;    neighbor.BC = a;    neighbor.BBC= AC_A;    neighbor.ABB= newnei.AC;    neighbor.AB = c;    neighbor.AAB = BC_C;    neighbor.AAC = newnei.BC;//b0rken    neighbor.AC = b;    neighbor.ACC = AB_B;  }}kernel void adaptiveReorgSplitTriangles (SplitTri splittri<>,                                       Neighbor newnei<>,                                       out STri tri<>,                                       out Neighbor neighbor<>,                                       float epsilon,                                       float stretchX) {  linearReorgSplitTriangles(splittri,newnei,tri,neighbor,stretchX);  smallEnough(tri,neighbor,tri,neighbor,epsilon);  }struct VertexArray4{  float4 * v;  unsigned int size;  unsigned int alloc;};void initVertexArray4(struct VertexArray4 *v) {  v->v=0;v->size=0;v->alloc=0;}void destroyVertexArray4(struct VertexArray4 *v) {  if (v->v) free(v->v);}void expandVertexArray4(struct VertexArray4 * v,                       unsigned int more) {  v->size+=more;  while(v->size>v->alloc) {    if (v->alloc==0)      v->alloc=16;    v->alloc*=2;    v->v = (float4*)realloc(v->v,v->alloc*sizeof(float4));  }}                   void pushBackVertexArray4(struct VertexArray4 * v,                         float4 f) {  if (++v->size>v->alloc) {    if (v->alloc==0)      v->alloc=16;    v->alloc*=2;    v->v = (float4*)realloc(v->v,v->alloc*sizeof(float4));  }  v->v[v->size-1]=f;}                   //simple growable array, similar to std::vector (I hate C parsers)struct VertexArray{  float3 * v;  unsigned int size;  unsigned int alloc;};void initVertexArray(struct VertexArray *v) {  v->v=0;v->size=0;v->alloc=0;}void destroyVertexArray(struct VertexArray *v) {  if (v->v) free(v->v);}void expandVertexArray(struct VertexArray * v,                       unsigned int more) {  v->size+=more;  while(v->size>v->alloc) {    if (v->alloc==0)      v->alloc=16;    v->alloc*=2;    v->v = (float3*)realloc(v->v,v->alloc*sizeof(float3));  }}                   void pushBackVertexArray(struct VertexArray * v,                         float3 f) {  if (++v->size>v->alloc) {    if (v->alloc==0)      v->alloc=16;    v->alloc*=2;    v->v = (float3*)realloc(v->v,v->alloc*sizeof(float3));  }  v->v[v->size-1]=f;}                   static int toi(float f) {return (int)f;}void subdivide (Neighbor (*neighbors)<>,                STri (*triangles)<>,                struct VertexArray *output,                struct VertexArray4 *output4,                int counter,                char out4) {  unsigned int streamY=toi(streamSize(*triangles).y);  unsigned int streamX=toi(streamSize(*triangles).x);  float stretchX = streamY*4>maxTexDim?(streamY*2>maxTexDim?1.0f:2.0f):0.0f;        int xfactor=stretchX==2.0f?2:(stretchX==1.0f?4:1);  int yfactor=stretchX==2.0f?2:(stretchX==1.0f?1:4);  subdivisiondepth++;  if (counter>=counterMax) {    if (out4) {      int sizey = output4->size;      expandVertexArray4(output4,                         3*toi(streamSize(*triangles).y)*toi(streamSize(*triangles).x));      streamWrite(*triangles,output4->v+sizey);    }else {      int sizey = output->size;      float3 outputTri<toi(streamSize(*triangles).y),        toi(streamSize(*triangles).x*3)>;      copyFinalTriangles(*triangles,outputTri);      TallyKernel("copyFinalTriangles",outputTri);      expandVertexArray(output,                      3*toi(streamSize(*triangles).y)*toi(streamSize(*triangles).x));      streamWrite(outputTri,output->v+sizey);    }  }else {    //allocate shared neighbor structs that hold the extra generated floats    Neighbor sharedNeighbors<streamY,streamX>;    SplitTri sharedTriangles <streamY,streamX>;    //these new neighbor structs are 4x as big as the old ones    //input is thus stretched through them based on the fmod    Neighbor newNeighbors<(streamY*yfactor),      (streamX*xfactor)>;    STri newTriangles<(streamY*yfactor),      (streamX*xfactor)>;    fprintf (stderr,"stretching %f to %d %d\n",stretchX,streamX*xfactor,streamY*yfactor);    //computes the 12 shared neighbor values    computeNeighborsNoAdaptive(*triangles,*neighbors,sharedNeighbors);    TallyKernel("computeNeighborsNoAdaptive",sharedNeighbors);    //computes the 6 new triangle vertex values    splitTrianglesNoAdaptive(*triangles,*neighbors,sharedTriangles);    TallyKernel("splitTrianglesNoAdaptive",sharedTriangles);    // combines the 12 neighbors and 6 vertices to produce 4 individual     // sets of triangles    // with the new triangles getting stretched in the x or y direction depending    // on stretchX    linearReorgSplitTriangles(sharedTriangles,                              sharedNeighbors,                              newTriangles,                              newNeighbors,                              stretchX);    TallyKernel("linearReorgSplitTriangles",sharedTriangles);    streamSwap(*triangles,newTriangles);    streamSwap(*neighbors,newNeighbors);    if (debugLoop) {      STri * tri;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -