// File: /workspace/github_c_makefile_star2_O3/archer1357[P]Offline-Raytracer/utils.c
// Function: renderSceneGBuffer
void renderSceneGBuffer(int updateFrom,int updateTo,
                        int width, int height,
                        float aspect,float yFov,
                        const float *minBound,const float *maxBound,
                        const float *origin,
                        const float *rotMat,
                        struct Ray *rayStack,
                        struct KdTraverseNode *traverseStack,
                        struct KdNode *rootNode,
                        struct Triangle *triangles,int trianglesNum,
                        struct Sphere *spheres,int spheresNum,
                        sobol_state_t *qrnd,
                        struct GBufferPixel *canvas) {

  // float mat[16]={viewRot[0],viewRot[1],viewRot[2],viewPos[0],
  //                    viewRot[3],viewRot[4],viewRot[5],viewPos[1],
  //                    viewRot[6],viewRot[7],viewRot[8],viewPos[2],
  //                    0.0f,      0.0f,      0.0f,      1.0f};

  float viewMat[16]={rotMat[0],rotMat[3],rotMat[6],-origin[0],
                     rotMat[1],rotMat[4],rotMat[7],-origin[1],
                     rotMat[2],rotMat[5],rotMat[8],-origin[2],
                     0.0f,      0.0f,      0.0f,       1.0f};

  float viewRotMat[9]={rotMat[0],rotMat[3],rotMat[6],
                       rotMat[1],rotMat[4],rotMat[7],
                       rotMat[2],rotMat[5],rotMat[8]};
  float znear=20.0f;
  float zfar=100.0f;

  float d;
  int i;

  //
  d=1.0f/tanf(yFov/2.0f);

  //
  int raycastsNum;


  for(i=updateFrom;i<updateTo;i++) {

    int x,y;
    float u,v,px,py;


    raycastsNum=1;

    rayStack[raycastsNum-1].bounces=0;
    rayStack[raycastsNum-1].origin[0]=origin[0];
    rayStack[raycastsNum-1].origin[1]=origin[1];
    rayStack[raycastsNum-1].origin[2]=origin[2];
    rayStack[raycastsNum-1].ratio=1.0f;

    x=i%width;
    y=i/width;
    u=x/(float)width;
    v=1.0f-y/(float)height; //flip
    px=2.0f*u-1.0f;
    py=2.0f*v-1.0f;

    float bla[3]={px*aspect,py,-d};//rhs


    mat3_mulVector(rayStack[raycastsNum-1].vec,rotMat,bla);
    vec3_normal(rayStack[raycastsNum-1].vec,rayStack[raycastsNum-1].vec);

    //



    //
    while(raycastsNum > 0) {
    float nor[3],colPt[3];


    struct Material *material;

    //
    raycastsNum--;
    struct Ray ray=rayStack[raycastsNum];

    if(!intersectSceneObject(&ray,traverseStack,minBound,maxBound,rootNode,
                             triangles,trianglesNum,spheres,spheresNum,
                             colPt,nor,&material)) {
      canvas[i].col[0]=0.0f;
      canvas[i].col[1]=0.0f;
      canvas[i].col[2]=0.0f;
      canvas[i].nor[0]=0.5f;
      canvas[i].nor[1]=0.5f;
      canvas[i].nor[2]=0.5f;
      canvas[i].depth=1.0f;
      break;
    }



    //
     canvas[i].col[0]=material->col[0];
     canvas[i].col[1]=material->col[1];
     canvas[i].col[2]=material->col[2];


     mat3_mulVector(canvas[i].nor,viewRotMat,nor);


     float viewPos[4];
     float pos[4]={colPt[0],colPt[1],colPt[2],1.0f};
     mat4_mulVector(viewPos,viewMat,pos);



     float p10=(zfar+znear)/(znear-zfar);
     float p11=2.0f*zfar*znear/(znear-zfar);

     // float clipZ=p10*viewPos[2]+p11;

     float mvZ=viewPos[2];
     float clipZ=(mvZ*(zfar+znear)+(2.0f*zfar*znear))/(znear-zfar);
     float clipW=-mvZ;
     float ndcZ = clipZ/clipW;

     // if(ndcZ > 1.0f || ndcZ < -1.0f) {
     //   printf("depth %g\n",ndcZ);
     // }

     // float depth=ndcZ*0.5f+0.5f;
     float nearRange=0.0f;
     float farRange=1.0f;
     float depth = (((farRange-nearRange) * ndcZ) + nearRange + farRange) / 2.0f;
     if(depth>1.0f || depth<0.0f) {
       depth=1.0f;

     }
     // if(depth > 1.0f || depth < 0.0f) {
     //   printf("depth %g\n",depth);
     // }
     canvas[i].depth=depth;

     // float linearDepth=(2.0f*znear)/(zfar+znear-depth*(zfar-znear));
     // canvas[i].depth=linearDepth;
     // if(depth<0.98f) {
     //   printf("less\n");
     // }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x158, %rsp            # imm = 0x158
movq %r9, 0x80(%rsp)
movq %r8, 0x78(%rsp)
movss %xmm0, 0x50(%rsp)
movl %ecx, %ebx
movl %edx, 0x3c(%rsp)
movl %esi, %r15d
movl %edi, %r12d
movq 0x198(%rsp), %rax
movq 0x190(%rsp), %rbp
movss (%rbp), %xmm0
movaps 0xc0750(%rip), %xmm2    # 0x4d7950
xorps %xmm2, %xmm0
movss %xmm0, 0xac(%rsp)
movss 0x4(%rbp), %xmm0
xorps %xmm2, %xmm0
movss %xmm0, 0xbc(%rsp)
movss 0x20(%rax), %xmm0
movss %xmm0, 0xc8(%rsp)
movss 0x8(%rbp), %xmm3
xorps %xmm2, %xmm3
movss %xmm3, 0xcc(%rsp)
movaps 0xc226d(%rip), %xmm2    # 0x4d94b0
movaps %xmm2, 0xd0(%rsp)
movups (%rax), %xmm2
movups 0x10(%rax), %xmm3
movss %xmm2, 0xa0(%rsp)
movaps %xmm2, %xmm4
shufps $0xff, %xmm2, %xmm4     # xmm4 = xmm4[3,3],xmm2[3,3]
movss %xmm4, 0xa4(%rsp)
movaps %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
movss %xmm4, 0xa8(%rsp)
movaps %xmm2, %xmm4
shufps $0x55, %xmm2, %xmm4     # xmm4 = xmm4[1,1],xmm2[1,1]
movss %xmm4, 0xb0(%rsp)
movss %xmm3, 0xb4(%rsp)
movaps %xmm3, %xmm4
shufps $0xff, %xmm3, %xmm4     # xmm4 = xmm4[3,3],xmm3[3,3]
movss %xmm4, 0xb8(%rsp)
movaps %xmm2, %xmm4
unpckhpd %xmm2, %xmm4            # xmm4 = xmm4[1],xmm2[1]
movss %xmm4, 0xc0(%rsp)
movaps %xmm3, %xmm4
shufps $0x55, %xmm3, %xmm4     # xmm4 = xmm4[1,1],xmm3[1,1]
movss %xmm4, 0xc4(%rsp)
movaps %xmm3, %xmm4
shufps $0x12, %xmm2, %xmm4     # xmm4 = xmm4[2,0],xmm2[1,0]
movaps %xmm2, %xmm5
shufps $0x8c, %xmm4, %xmm5     # xmm5 = xmm5[0,3],xmm4[0,2]
shufps $0x12, %xmm3, %xmm2     # xmm2 = xmm2[2,0],xmm3[1,0]
shufps $0x8c, %xmm2, %xmm3     # xmm3 = xmm3[0,3],xmm2[0,2]
movaps %xmm3, 0x100(%rsp)
movaps %xmm5, 0xf0(%rsp)
movss %xmm0, 0x110(%rsp)
mulss 0xbf18d(%rip), %xmm1    # 0x4d6488
movaps %xmm1, %xmm0
callq 0x419040
movl %r15d, 0x4c(%rsp)
cmpl %r15d, %r12d
jge 0x4175d4
movq 0x1e0(%rsp), %rax
xorps %xmm1, %xmm1
cvtsi2ssl 0x3c(%rsp), %xmm1
movss %xmm1, 0x48(%rsp)
xorps %xmm1, %xmm1
cvtsi2ss %ebx, %xmm1
movss %xmm1, 0x44(%rsp)
movq 0x1a0(%rsp), %r15
movss 0xc213f(%rip), %xmm1    # 0x4d9484
divss %xmm0, %xmm1
movss %xmm1, 0x40(%rsp)
leaq 0xc(%r15), %r13
movslq %r12d, %rcx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
leaq (%rax,%rdx), %rbx
addq $0xc, %rbx
movss 0xc05af(%rip), %xmm2    # 0x4d7920
movl $0x0, 0x18(%r15)
movss (%rbp), %xmm0
movss %xmm0, (%r15)
movss 0x4(%rbp), %xmm0
movss %xmm0, 0x4(%r15)
movss 0x8(%rbp), %xmm0
movss %xmm0, 0x8(%r15)
movl $0x3f800000, 0x1c(%r15) # imm = 0x3F800000
movl %r12d, %eax
cltd
idivl 0x3c(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %edx, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
divss 0x48(%rsp), %xmm0
divss 0x44(%rsp), %xmm1
subss %xmm1, %xmm2
addss %xmm0, %xmm0
movss 0xc20b1(%rip), %xmm1    # 0x4d9484
addss %xmm1, %xmm0
addss %xmm2, %xmm2
addss %xmm1, %xmm2
mulss 0x50(%rsp), %xmm0
movss %xmm0, 0x54(%rsp)
movss %xmm2, 0x58(%rsp)
movss 0x40(%rsp), %xmm0
movss %xmm0, 0x5c(%rsp)
movq %r13, %rdi
movq 0x198(%rsp), %rsi
leaq 0x54(%rsp), %rdx
callq 0x40865a
movq %r13, %rdi
movq %r13, %rsi
callq 0x40761b
movups (%r15), %xmm0
movups 0x10(%r15), %xmm1
movaps %xmm1, 0x130(%rsp)
movaps %xmm0, 0x120(%rsp)
leaq 0x88(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x14c(%rsp), %r14
movq %r14, 0x20(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x1c8(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x1c0(%rsp), %eax
movl %eax, (%rsp)
leaq 0x120(%rsp), %rdi
movq 0x1a8(%rsp), %rsi
movq 0x78(%rsp), %rdx
movq 0x80(%rsp), %rcx
movq 0x1b0(%rsp), %r8
movq 0x1b8(%rsp), %r9
callq 0x416687
testb %al, %al
je 0x41759b
movq 0x88(%rsp), %rax
movss (%rax), %xmm0
movss %xmm0, -0xc(%rbx)
movss 0x4(%rax), %xmm0
movss %xmm0, -0x8(%rbx)
movss 0x8(%rax), %xmm0
movss %xmm0, -0x4(%rbx)
movq %rbx, %rdi
leaq 0xf0(%rsp), %rsi
movq %r14, %rdx
callq 0x40865a
movsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movss 0x98(%rsp), %xmm0
movss %xmm0, 0x68(%rsp)
movl $0x3f800000, 0x6c(%rsp) # imm = 0x3F800000
leaq 0xe0(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x40784f
movss 0xe8(%rsp), %xmm0
movaps %xmm0, %xmm1
mulss 0xc1f5a(%rip), %xmm1    # 0x4d9494
addss 0xc1f56(%rip), %xmm1    # 0x4d9498
divss 0xc03ee(%rip), %xmm1    # 0x4d7938
divss %xmm0, %xmm1
xorps %xmm2, %xmm2
addss %xmm2, %xmm1
movss 0xc03c3(%rip), %xmm3    # 0x4d7920
addss %xmm3, %xmm1
mulss 0xbef1f(%rip), %xmm1    # 0x4d6488
movaps %xmm1, %xmm0
cmpltss %xmm2, %xmm0
movaps %xmm0, %xmm2
andnps %xmm1, %xmm2
andps %xmm3, %xmm0
orps %xmm2, %xmm0
movaps %xmm3, %xmm2
cmpltss %xmm1, %xmm2
movaps %xmm2, %xmm1
andnps %xmm0, %xmm1
andps %xmm3, %xmm2
orps %xmm1, %xmm2
movss %xmm2, 0xc(%rbx)
movaps %xmm3, %xmm2
jmp 0x4175c2
movaps 0xc1f1e(%rip), %xmm0    # 0x4d94c0
movups %xmm0, -0xc(%rbx)
movsd 0xc1f22(%rip), %xmm0    # 0x4d94d0
movsd %xmm0, 0x4(%rbx)
movl $0x3f800000, 0xc(%rbx)  # imm = 0x3F800000
movss 0xc035e(%rip), %xmm2    # 0x4d7920
addq $0x1c, %rbx
incl %r12d
cmpl %r12d, 0x4c(%rsp)
jne 0x417371
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq