// File: /workspace/github_c_makefile_star2_O0/archer1357[P]Offline-Raytracer/main.c
// Function: create_gbuffer
bool create_gbuffer(int canvasWidth, int canvasHeight,
                    struct Scene *scene,float aspectRatio,float yFov,
                    struct Ray **rayStacks,
                    struct KdTraverseNode **traverseStacks) {
  int canvasSize=canvasWidth*canvasHeight;

  float viewRot[9]={1.0f,0.0f,0.0f,
                    0.0f,1.0f,0.0f,
                    0.0f,0.0f, 1.0f};

  float viewPos[3]={0.0f,0.0f, 50.0f};
  // float viewPos[3]={0.0f,0.0f, 0.0f};


  sobol_state_t qrnd;
  sobol_init(&qrnd,2);

  //
  struct GBufferPixel *canvas;
  canvas=(struct GBufferPixel*)malloc(sizeof(struct GBufferPixel)*canvasSize);


  //
  createDir("output");
  int fileNum=lastFileNum("output")+1;
  printf("fn %i\n",fileNum);

  //
  char allFn[512],colFn[512],norFn[512],depthFn[512];
  snprintf(allFn,sizeof(allFn),"output/output%04i_all.jpg",fileNum);

  snprintf(colFn,sizeof(colFn),"output/output%04i_col.jpg",fileNum);
  snprintf(norFn,sizeof(norFn),"output/output%04i_nor.jpg",fileNum);
  snprintf(depthFn,sizeof(depthFn),"output/output%04i_depth.jpg",fileNum);

  // snprintf(colFn,sizeof(colFn),"deferred/out_col.jpg");
  // snprintf(norFn,sizeof(norFn),"deferred/out_nor.jpg");
  // snprintf(depthFn,sizeof(depthFn),"deferred/out_depth.jpg");

  //
  if(access(allFn,F_OK )!=-1) {
    fprintf(stderr,"'%s' already exists.\n",allFn);
    return false;
  }

  // if(access(colFn,F_OK )!=-1) {
  //   fprintf(stderr,"'%s' already exists.\n",colFn);
  //   return false;
  // }

  // if(access(norFn,F_OK )!=-1) {
  //   fprintf(stderr,"'%s' already exists.\n",norFn);
  //   return false;
  // }

  // if(access(depthFn,F_OK )!=-1) {
  //   fprintf(stderr,"'%s' already exists.\n",depthFn);
  //   return false;
  // }

  //
  renderSceneGBuffer(0,canvasSize,
                     canvasWidth,canvasHeight,
                     aspectRatio,yFov,
                     scene->minBound,scene->maxBound,
                     viewPos,
                     viewRot,
                     rayStacks[0],
                     traverseStacks[0],
                     scene->rootNode,
                     scene->triangles,scene->trianglesNum,
                     scene->spheres,scene->spheresNum,
                     &qrnd,
                     canvas);


  //

  unsigned char *allBitmap,*colBitmap,*norBitmap,*depthBitmap;
  allBitmap=(unsigned char*)malloc(canvasSize*4*3);
  colBitmap=allBitmap;
  norBitmap=&colBitmap[canvasSize*4];
  depthBitmap=&norBitmap[canvasSize*4];

  //
  int i;

  for(i=0;i<canvasSize;i++) {



    colBitmap[i*4+0]=floatToByte(canvas[i].col[0]);
    colBitmap[i*4+1]=floatToByte(canvas[i].col[1]);
    colBitmap[i*4+2]=floatToByte(canvas[i].col[2]);
    colBitmap[i*4+3]=floatToByte(1.0f);


    float sphereNor2[2];
    encodeNorSphereMap(canvas[i].nor,sphereNor2);

    norBitmap[i*4+0]=floatToByte(sphereNor2[0]);
    norBitmap[i*4+1]=floatToByte(sphereNor2[1]);
    norBitmap[i*4+2]=floatToByte(0.0f);
    norBitmap[i*4+3]=floatToByte(1.0f);

    // float sphereNor4[4];
    // EncodeFloatRG(sphereNor2[0],sphereNor4);
    // EncodeFloatRG(sphereNor2[1],&sphereNor4[2]);

    // norBitmap[i*4+0]=floatToByte(sphereNor4[0]);
    // norBitmap[i*4+1]=floatToByte(sphereNor4[1]);
    // norBitmap[i*4+2]=floatToByte(sphereNor4[2]);
    // norBitmap[i*4+3]=floatToByte(sphereNor4[3]);

    // // norBitmap[i*4+0]=floatToByte(canvas[i].nor[0]*0.5f+0.5f);
    // // norBitmap[i*4+1]=floatToByte(canvas[i].nor[1]*0.5f+0.5f);
    // // norBitmap[i*4+2]=floatToByte(canvas[i].nor[2]*0.5f+0.5f);
    // // norBitmap[i*4+3]=floatToByte(1.0f);

    float depth4[4];
    EncodeFloatRGBA(canvas[i].depth,depth4);

    depthBitmap[i*4+0]=floatToByte(depth4[0]);
    depthBitmap[i*4+1]=floatToByte(depth4[1]);
    depthBitmap[i*4+2]=floatToByte(depth4[2]);
    depthBitmap[i*4+3]=floatToByte(depth4[3]);

    // float dd= DecodeFloatRGBA(depth4);


    // dd=(2.0f*1.0f)/(100.0f+1.0f-canvas[i].depth*(100.0f-1.0f));
    // depthBitmap[i*4+0]=floatToByte(dd);
    // depthBitmap[i*4+1]=floatToByte(dd);
    // depthBitmap[i*4+2]=floatToByte(dd);
    // depthBitmap[i*4+3]=255;

    // depthBitmap[i*4+0]=floatToByte(canvas[i].depth);
    // depthBitmap[i*4+1]=floatToByte(canvas[i].depth);
    // depthBitmap[i*4+2]=floatToByte(canvas[i].depth);
    // depthBitmap[i*4+3]=255;
  }

  //

  jo_write_jpg(allFn,allBitmap,canvasWidth,canvasHeight*3,4,100);
  printf("Wrote '%s'.\n",colFn);

  // bitmapFlipVertical(colBitmap,4,canvasWidth,canvasHeight);
  jo_write_jpg(colFn,colBitmap,canvasWidth,canvasHeight,4,100);
  printf("Wrote '%s'.\n",colFn);

  // bitmapFlipVertical(norBitmap,4,canvasWidth,canvasHeight);
  jo_write_jpg(norFn,norBitmap,canvasWidth,canvasHeight,4,100);
  printf("Wrote '%s'.\n",norFn);

  // bitmapFlipVertical(depthBitmap,4,canvasWidth,canvasHeight);
  jo_write_jpg(depthFn,depthBitmap,canvasWidth,canvasHeight,4,100);
  printf("Wrote '%s'.\n",depthFn);

  //
  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c98, %rsp           # imm = 0x1C98
movl %edi, -0x30(%rbp)
movl %esi, -0x34(%rbp)
movq %rdx, -0x40(%rbp)
movss %xmm0, -0x44(%rbp)
movss %xmm1, -0x48(%rbp)
movq %rcx, -0x50(%rbp)
movq %r8, -0x58(%rbp)
movl -0x30(%rbp), %eax
imull -0x34(%rbp), %eax
movl %eax, -0x5c(%rbp)
leaq -0x80(%rbp), %rdi
xorl %esi, %esi
movl $0x24, %edx
callq 0x4010e0
movss 0xd79c2(%rip), %xmm0    # 0x4e3484
movss %xmm0, -0x80(%rbp)
movss 0xd79b5(%rip), %xmm0    # 0x4e3484
movss %xmm0, -0x70(%rbp)
movss 0xd79a8(%rip), %xmm0    # 0x4e3484
movss %xmm0, -0x60(%rbp)
movq 0xd90e0(%rip), %rax     # 0x4e4bc8
movq %rax, -0x8c(%rbp)
movl 0xd90db(%rip), %eax     # 0x4e4bd0
movl %eax, -0x84(%rbp)
leaq -0x1400(%rbp), %rdi
movl $0x2, %esi
callq 0x410b70
movslq -0x5c(%rbp), %rax
imulq $0x1c, %rax, %rdi
callq 0x452d20
movq %rax, -0x1408(%rbp)
leaq 0xd8e49(%rip), %rdi     # 0x4e4970
callq 0x4245c0
leaq 0xd8e3d(%rip), %rdi     # 0x4e4970
callq 0x4245e0
addl $0x1, %eax
movl %eax, -0x140c(%rbp)
movl -0x140c(%rbp), %esi
leaq 0xd8eb7(%rip), %rdi     # 0x4e4a05
movb $0x0, %al
callq 0x435930
leaq -0x1610(%rbp), %rdi
movl -0x140c(%rbp), %ecx
movl $0x200, %esi            # imm = 0x200
leaq 0xd8eb4(%rip), %rdx     # 0x4e4a22
movb $0x0, %al
callq 0x435a00
leaq -0x1810(%rbp), %rdi
movl -0x140c(%rbp), %ecx
movl $0x200, %esi            # imm = 0x200
leaq 0xd8eae(%rip), %rdx     # 0x4e4a3c
movb $0x0, %al
callq 0x435a00
leaq -0x1a10(%rbp), %rdi
movl -0x140c(%rbp), %ecx
movl $0x200, %esi            # imm = 0x200
leaq 0xd8ea8(%rip), %rdx     # 0x4e4a56
movb $0x0, %al
callq 0x435a00
leaq -0x1c10(%rbp), %rdi
movl -0x140c(%rbp), %ecx
movl $0x200, %esi            # imm = 0x200
leaq 0xd8ea2(%rip), %rdx     # 0x4e4a70
movb $0x0, %al
callq 0x435a00
leaq -0x1610(%rbp), %rdi
xorl %esi, %esi
callq 0x48fbd0
cmpl $-0x1, %eax
je 0x40bc10
movq $0x526c68, %rax         # imm = 0x526C68
movq (%rax), %rdi
leaq -0x1610(%rbp), %rdx
leaq 0xd8d98(%rip), %rsi     # 0x4e4998
movb $0x0, %al
callq 0x435870
movb $0x0, -0x29(%rbp)
jmp 0x40c0b7
movl -0x5c(%rbp), %esi
movl -0x30(%rbp), %edx
movl -0x34(%rbp), %ecx
movss -0x44(%rbp), %xmm0
movss -0x48(%rbp), %xmm1
movq -0x40(%rbp), %r8
addq $0x34, %r8
movq -0x40(%rbp), %r9
addq $0x40, %r9
leaq -0x8c(%rbp), %rax
leaq -0x80(%rbp), %rdi
movq %rdi, -0x1c68(%rbp)
movq -0x50(%rbp), %rdi
movq (%rdi), %rdi
movq %rdi, -0x1c60(%rbp)
movq -0x58(%rbp), %rdi
movq (%rdi), %r13
movq -0x40(%rbp), %rdi
movq 0x28(%rdi), %r12
movq -0x40(%rbp), %rdi
movq (%rdi), %r15
movq -0x40(%rbp), %rdi
movl 0x18(%rdi), %r14d
movq -0x40(%rbp), %rdi
movq 0x8(%rdi), %rbx
movq -0x40(%rbp), %rdi
movl 0x1c(%rdi), %r11d
movq -0x1408(%rbp), %rdi
movq %rdi, -0x1c58(%rbp)
xorl %edi, %edi
leaq -0x1400(%rbp), %r10
movq %rax, (%rsp)
movq -0x1c68(%rbp), %rax
movq %rax, 0x8(%rsp)
movq -0x1c60(%rbp), %rax
movq %rax, 0x10(%rsp)
movq -0x1c58(%rbp), %rax
movq %r13, 0x18(%rsp)
movq %r12, 0x20(%rsp)
movq %r15, 0x28(%rsp)
movl %r14d, 0x30(%rsp)
movq %rbx, 0x38(%rsp)
movl %r11d, 0x40(%rsp)
movq %r10, 0x48(%rsp)
movq %rax, 0x50(%rsp)
callq 0x423d30
movl -0x5c(%rbp), %eax
shll $0x2, %eax
imull $0x3, %eax, %eax
movslq %eax, %rdi
callq 0x452d20
movq %rax, -0x1c18(%rbp)
movq -0x1c18(%rbp), %rax
movq %rax, -0x1c20(%rbp)
movq -0x1c20(%rbp), %rax
movl -0x5c(%rbp), %ecx
shll $0x2, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x1c28(%rbp)
movq -0x1c28(%rbp), %rax
movl -0x5c(%rbp), %ecx
shll $0x2, %ecx
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0x1c30(%rbp)
movl $0x0, -0x1c34(%rbp)
movl -0x1c34(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jge 0x40bfca
movq -0x1408(%rbp), %rax
movslq -0x1c34(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss (%rax), %xmm0
callq 0x424770
movb %al, %dl
movq -0x1c20(%rbp), %rax
movl -0x1c34(%rbp), %ecx
shll $0x2, %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x1408(%rbp), %rax
movslq -0x1c34(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0x4(%rax), %xmm0
callq 0x424770
movb %al, %dl
movq -0x1c20(%rbp), %rax
movl -0x1c34(%rbp), %ecx
shll $0x2, %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x1408(%rbp), %rax
movslq -0x1c34(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0x8(%rax), %xmm0
callq 0x424770
movb %al, %dl
movq -0x1c20(%rbp), %rax
movl -0x1c34(%rbp), %ecx
shll $0x2, %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movss 0xd7674(%rip), %xmm0    # 0x4e3484
callq 0x424770
movb %al, %dl
movq -0x1c20(%rbp), %rax
movl -0x1c34(%rbp), %ecx
shll $0x2, %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x1408(%rbp), %rdi
movslq -0x1c34(%rbp), %rax
imulq $0x1c, %rax, %rax
addq %rax, %rdi
addq $0xc, %rdi
leaq -0x1c3c(%rbp), %rsi
callq 0x424ab0
movss -0x1c3c(%rbp), %xmm0
callq 0x424770
movb %al, %dl
movq -0x1c28(%rbp), %rax
movl -0x1c34(%rbp), %ecx
shll $0x2, %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movss -0x1c38(%rbp), %xmm0
callq 0x424770
movb %al, %dl
movq -0x1c28(%rbp), %rax
movl -0x1c34(%rbp), %ecx
shll $0x2, %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
xorps %xmm0, %xmm0
callq 0x424770
movb %al, %dl
movq -0x1c28(%rbp), %rax
movl -0x1c34(%rbp), %ecx
shll $0x2, %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movss 0xd75b4(%rip), %xmm0    # 0x4e3484
callq 0x424770
movb %al, %dl
movq -0x1c28(%rbp), %rax
movl -0x1c34(%rbp), %ecx
shll $0x2, %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movq -0x1408(%rbp), %rax
movslq -0x1c34(%rbp), %rcx
imulq $0x1c, %rcx, %rcx
addq %rcx, %rax
movss 0x18(%rax), %xmm0
leaq -0x1c50(%rbp), %rdi
callq 0x424830
movss -0x1c50(%rbp), %xmm0
callq 0x424770
movb %al, %dl
movq -0x1c30(%rbp), %rax
movl -0x1c34(%rbp), %ecx
shll $0x2, %ecx
addl $0x0, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movss -0x1c4c(%rbp), %xmm0
callq 0x424770
movb %al, %dl
movq -0x1c30(%rbp), %rax
movl -0x1c34(%rbp), %ecx
shll $0x2, %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movss -0x1c48(%rbp), %xmm0
callq 0x424770
movb %al, %dl
movq -0x1c30(%rbp), %rax
movl -0x1c34(%rbp), %ecx
shll $0x2, %ecx
addl $0x2, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movss -0x1c44(%rbp), %xmm0
callq 0x424770
movb %al, %dl
movq -0x1c30(%rbp), %rax
movl -0x1c34(%rbp), %ecx
shll $0x2, %ecx
addl $0x3, %ecx
movslq %ecx, %rcx
movb %dl, (%rax,%rcx)
movl -0x1c34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c34(%rbp)
jmp 0x40bd4c
leaq -0x1610(%rbp), %rdi
movq -0x1c18(%rbp), %rsi
movl -0x30(%rbp), %edx
imull $0x3, -0x34(%rbp), %ecx
movl $0x4, %r8d
movl $0x64, %r9d
callq 0x4085a0
leaq -0x1810(%rbp), %rsi
leaq 0xd89fa(%rip), %rdi     # 0x4e49f8
movb $0x0, %al
callq 0x435930
leaq -0x1810(%rbp), %rdi
movq -0x1c20(%rbp), %rsi
movl -0x30(%rbp), %edx
movl -0x34(%rbp), %ecx
movl $0x4, %r8d
movl $0x64, %r9d
callq 0x4085a0
leaq -0x1810(%rbp), %rsi
leaq 0xd89c0(%rip), %rdi     # 0x4e49f8
movb $0x0, %al
callq 0x435930
leaq -0x1a10(%rbp), %rdi
movq -0x1c28(%rbp), %rsi
movl -0x30(%rbp), %edx
movl -0x34(%rbp), %ecx
movl $0x4, %r8d
movl $0x64, %r9d
callq 0x4085a0
leaq -0x1a10(%rbp), %rsi
leaq 0xd8986(%rip), %rdi     # 0x4e49f8
movb $0x0, %al
callq 0x435930
leaq -0x1c10(%rbp), %rdi
movq -0x1c30(%rbp), %rsi
movl -0x30(%rbp), %edx
movl -0x34(%rbp), %ecx
movl $0x4, %r8d
movl $0x64, %r9d
callq 0x4085a0
leaq -0x1c10(%rbp), %rsi
leaq 0xd894c(%rip), %rdi     # 0x4e49f8
movb $0x0, %al
callq 0x435930
movb $0x1, -0x29(%rbp)
movb -0x29(%rbp), %al
andb $0x1, %al
addq $0x1c98, %rsp           # imm = 0x1C98
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop