// File: /workspace/github_c_makefile_star2_O1/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, 0xb0(%rsp)
movq %r8, 0xa8(%rsp)
movss %xmm0, 0x50(%rsp)
movl %ecx, %ebx
movl %edx, 0x3c(%rsp)
movl %esi, %r14d
movl %edi, %r12d
movq 0x198(%rsp), %rax
movq 0x190(%rsp), %rbp
movss (%rax), %xmm0
movss %xmm0, 0xd0(%rsp)
movss 0xc(%rax), %xmm2
movss %xmm2, 0xd4(%rsp)
movss 0x18(%rax), %xmm3
movss %xmm3, 0xd8(%rsp)
movss (%rbp), %xmm4
movaps 0xc0a4d(%rip), %xmm5    # 0x4d6c50
xorps %xmm5, %xmm4
movss %xmm4, 0xdc(%rsp)
movss 0x4(%rax), %xmm4
movss %xmm4, 0xe0(%rsp)
movss 0x10(%rax), %xmm6
movss %xmm6, 0xe4(%rsp)
movss 0x1c(%rax), %xmm7
movss %xmm7, 0xe8(%rsp)
movss 0x4(%rbp), %xmm8
xorps %xmm5, %xmm8
movss %xmm8, 0xec(%rsp)
movss 0x8(%rax), %xmm8
movss %xmm8, 0xf0(%rsp)
movss 0x14(%rax), %xmm9
movss %xmm9, 0xf4(%rsp)
movss 0x20(%rax), %xmm10
movss %xmm10, 0xf8(%rsp)
movss 0x8(%rbp), %xmm11
xorps %xmm5, %xmm11
movss %xmm11, 0xfc(%rsp)
movaps 0xc20a8(%rip), %xmm5    # 0x4d8340
movaps %xmm5, 0x100(%rsp)
movss %xmm0, 0x60(%rsp)
movss %xmm2, 0x64(%rsp)
movss %xmm3, 0x68(%rsp)
movss %xmm4, 0x6c(%rsp)
movss %xmm6, 0x70(%rsp)
movss %xmm7, 0x74(%rsp)
movss %xmm8, 0x78(%rsp)
movss %xmm9, 0x7c(%rsp)
movss %xmm10, 0x80(%rsp)
mulss 0xbf1c8(%rip), %xmm1    # 0x4d54ac
movaps %xmm1, %xmm0
callq 0x418150
movl %r14d, 0x4c(%rsp)
cmpl %r14d, %r12d
jge 0x4165c8
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), %r14
movss 0xc1fd6(%rip), %xmm1    # 0x4d8304
divss %xmm0, %xmm1
movss %xmm1, 0x40(%rsp)
leaq 0xc(%r14), %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 0xbf136(%rip), %xmm2    # 0x4d5490
movl $0x0, 0x18(%r14)
movss (%rbp), %xmm0
movss %xmm0, (%r14)
movss 0x4(%rbp), %xmm0
movss %xmm0, 0x4(%r14)
movss 0x8(%rbp), %xmm0
movss %xmm0, 0x8(%r14)
movl $0x3f800000, 0x1c(%r14) # 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 0xc1f48(%rip), %xmm1    # 0x4d8304
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 0x408297
movq %r13, %rdi
movq %r13, %rsi
callq 0x40759c
movups (%r14), %xmm0
movups 0x10(%r14), %xmm1
movaps %xmm1, 0x130(%rsp)
movaps %xmm0, 0x120(%rsp)
leaq 0xb8(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x14c(%rsp), %r15
movq %r15, 0x20(%rsp)
leaq 0xc4(%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 0xa8(%rsp), %rdx
movq 0xb0(%rsp), %rcx
movq 0x1b0(%rsp), %r8
movq 0x1b8(%rsp), %r9
callq 0x41557c
testb %al, %al
je 0x41658e
movq 0xb8(%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 0x60(%rsp), %rsi
movq %r15, %rdx
callq 0x408297
movss 0xc4(%rsp), %xmm0
movss %xmm0, 0x90(%rsp)
movq 0xc8(%rsp), %rax
movq %rax, 0x94(%rsp)
movl $0x3f800000, 0x9c(%rsp) # imm = 0x3F800000
leaq 0x110(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq 0x4077bb
movss 0x118(%rsp), %xmm0
movaps %xmm0, %xmm1
mulss 0xc1de7(%rip), %xmm1    # 0x4d8314
addss 0xc1de3(%rip), %xmm1    # 0x4d8318
divss 0xc0417(%rip), %xmm1    # 0x4d6954
divss %xmm0, %xmm1
xorps %xmm2, %xmm2
addss %xmm2, %xmm1
movss 0xbef40(%rip), %xmm3    # 0x4d5490
addss %xmm3, %xmm1
mulss 0xbef50(%rip), %xmm1    # 0x4d54ac
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 0x4165b6
movaps 0xc1dbb(%rip), %xmm0    # 0x4d8350
movups %xmm0, -0xc(%rbx)
movabsq $0x3f0000003f000000, %rax # imm = 0x3F0000003F000000
movq %rax, 0x4(%rbx)
movl $0x3f800000, 0xc(%rbx)  # imm = 0x3F800000
movss 0xbeeda(%rip), %xmm2    # 0x4d5490
addq $0x1c, %rbx
incl %r12d
cmpl %r12d, 0x4c(%rsp)
jne 0x41635a
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq