// File: /workspace/github_c_makefile_star2_O2/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 $0x148, %rsp            # imm = 0x148
movq %r9, 0x78(%rsp)
movq %r8, 0x70(%rsp)
movss %xmm0, 0x50(%rsp)
movl %ecx, %r12d
movl %edx, %ebx
movl %esi, 0x38(%rsp)
movl %edi, %r13d
movq 0x188(%rsp), %rax
movq 0x180(%rsp), %r14
movss (%r14), %xmm0
movaps 0xc01a4(%rip), %xmm2    # 0x4d3c50
xorps %xmm2, %xmm0
leaq 0x100(%rsp), %rcx
movss %xmm0, 0xc(%rcx)
movss 0x4(%r14), %xmm0
xorps %xmm2, %xmm0
movss %xmm0, 0x1c(%rcx)
movss 0x20(%rax), %xmm0
movss %xmm0, 0x28(%rcx)
movss 0x8(%r14), %xmm3
xorps %xmm2, %xmm3
movss %xmm3, 0x2c(%rcx)
movaps 0xc1837(%rip), %xmm2    # 0x4d5320
movaps %xmm2, 0x30(%rcx)
movups (%rax), %xmm3
movups 0x10(%rax), %xmm2
movss %xmm3, (%rcx)
movaps %xmm3, %xmm4
shufps $0xff, %xmm3, %xmm4     # xmm4 = xmm4[3,3],xmm3[3,3]
movss %xmm4, 0x4(%rcx)
movaps %xmm2, %xmm4
unpckhpd %xmm2, %xmm4            # xmm4 = xmm4[1],xmm2[1]
movss %xmm4, 0x8(%rcx)
movaps %xmm3, %xmm4
shufps $0x55, %xmm3, %xmm4     # xmm4 = xmm4[1,1],xmm3[1,1]
movss %xmm4, 0x10(%rcx)
movss %xmm2, 0x14(%rcx)
movaps %xmm2, %xmm4
shufps $0xff, %xmm2, %xmm4     # xmm4 = xmm4[3,3],xmm2[3,3]
movss %xmm4, 0x18(%rcx)
movaps %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
movss %xmm4, 0x20(%rcx)
movaps %xmm2, %xmm4
shufps $0x55, %xmm2, %xmm4     # xmm4 = xmm4[1,1],xmm2[1,1]
movss %xmm4, 0x24(%rcx)
movaps %xmm2, %xmm4
shufps $0x12, %xmm3, %xmm4     # xmm4 = xmm4[2,0],xmm3[1,0]
movaps %xmm3, %xmm5
shufps $0x8c, %xmm4, %xmm5     # xmm5 = xmm5[0,3],xmm4[0,2]
shufps $0x12, %xmm2, %xmm3     # xmm3 = xmm3[2,0],xmm2[1,0]
shufps $0x8c, %xmm3, %xmm2     # xmm2 = xmm2[0,3],xmm3[0,2]
leaq 0xd0(%rsp), %rax
movaps %xmm2, 0x10(%rax)
movaps %xmm5, (%rax)
movss %xmm0, 0x20(%rax)
mulss 0xbe905(%rip), %xmm1    # 0x4d247c
movq 0x190(%rsp), %rbp
movq 0x1d0(%rsp), %r15
movaps %xmm1, %xmm0
callq 0x4158e0
movl %ebx, 0x4c(%rsp)
cvtsi2ss %ebx, %xmm1
movss %xmm1, 0x48(%rsp)
cvtsi2ss %r12d, %xmm1
movss %xmm1, 0x44(%rsp)
movss 0xc0090(%rip), %xmm1    # 0x4d3c40
divss %xmm0, %xmm1
movss %xmm1, 0x40(%rsp)
leaq 0xc(%rbp), %r12
movslq %r13d, %r13
movslq 0x38(%rsp), %rax
movq %rax, 0x38(%rsp)
imulq $0x1c, %r13, %rax
addq %rax, %r15
addq $0xc, %r15
movss 0xbe882(%rip), %xmm2    # 0x4d2460
leaq 0x94(%rsp), %rbx
cmpq 0x38(%rsp), %r13
jge 0x413e3e
andl $0x0, 0x18(%rbp)
movss (%r14), %xmm0
movss %xmm0, (%rbp)
movss 0x4(%r14), %xmm0
movss %xmm0, 0x4(%rbp)
movss 0x8(%r14), %xmm0
movss %xmm0, 0x8(%rbp)
movl $0x3f800000, 0x1c(%rbp) # imm = 0x3F800000
movl %r13d, %eax
cltd
idivl 0x4c(%rsp)
cvtsi2ss %edx, %xmm0
cvtsi2ss %eax, %xmm1
divss 0x48(%rsp), %xmm0
divss 0x44(%rsp), %xmm1
subss %xmm1, %xmm2
addss %xmm0, %xmm0
movss 0xbfff8(%rip), %xmm1    # 0x4d3c40
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 %r12, %rdi
movq 0x188(%rsp), %rsi
leaq 0x54(%rsp), %rdx
callq 0x4079d0
movq %r12, %rdi
movq %r12, %rsi
callq 0x406f42
movups (%rbp), %xmm0
movups 0x10(%rbp), %xmm1
movaps %xmm1, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
leaq 0x80(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %rbx, 0x20(%rsp)
leaq 0x88(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x1b8(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x1b0(%rsp), %eax
movl %eax, (%rsp)
leaq 0xb0(%rsp), %rdi
movq 0x198(%rsp), %rsi
movq 0x70(%rsp), %rdx
movq 0x78(%rsp), %rcx
movq 0x1a0(%rsp), %r8
movq 0x1a8(%rsp), %r9
callq 0x412f6b
testb %al, %al
je 0x413e08
movq 0x80(%rsp), %rax
movss (%rax), %xmm0
movss %xmm0, -0xc(%r15)
movss 0x4(%rax), %xmm0
movss %xmm0, -0x8(%r15)
movss 0x8(%rax), %xmm0
movss %xmm0, -0x4(%r15)
movq %r15, %rdi
leaq 0xd0(%rsp), %rsi
movq %rbx, %rdx
callq 0x4079d0
movsd 0x88(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movss 0x90(%rsp), %xmm0
movss %xmm0, 0x68(%rsp)
movl $0x3f800000, 0x6c(%rsp) # imm = 0x3F800000
leaq 0xa0(%rsp), %rdi
leaq 0x100(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x407107
movss 0xa8(%rsp), %xmm0
movaps %xmm0, %xmm1
mulss 0xc155a(%rip), %xmm1    # 0x4d5300
addss 0xc1556(%rip), %xmm1    # 0x4d5304
divss 0xbfb6e(%rip), %xmm1    # 0x4d3924
divss %xmm0, %xmm1
xorps %xmm2, %xmm2
addss %xmm2, %xmm1
movss 0xbe697(%rip), %xmm3    # 0x4d2460
addss %xmm3, %xmm1
mulss 0xbe6a7(%rip), %xmm1    # 0x4d247c
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(%r15)
movaps %xmm3, %xmm2
jmp 0x413e32
movaps 0xc1521(%rip), %xmm0    # 0x4d5330
movups %xmm0, -0xc(%r15)
movsd 0xc1524(%rip), %xmm0    # 0x4d5340
movsd %xmm0, 0x4(%r15)
movl $0x3f800000, 0xc(%r15)  # imm = 0x3F800000
movss 0xbe62e(%rip), %xmm2    # 0x4d2460
incq %r13
addq $0x1c, %r15
jmp 0x413bde
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq