// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1bf8, %rsp           # imm = 0x1BF8
movq %r8, (%rsp)
movq %rcx, 0x8(%rsp)
movss %xmm1, 0x1c(%rsp)
movss %xmm0, 0x18(%rsp)
movq %rdx, %r14
movl %edi, %ebp
movq %rsi, 0x20(%rsp)
movl %esi, %ebx
imull %edi, %ebx
xorps %xmm0, %xmm0
movaps %xmm0, 0x50(%rsp)
movaps %xmm0, 0x60(%rsp)
movl $0x3f800000, %eax       # imm = 0x3F800000
movl %eax, 0x50(%rsp)
movl %eax, 0x60(%rsp)
movl %eax, 0x70(%rsp)
movl $0x42480000, 0x48(%rsp) # imm = 0x42480000
movq $0x0, 0x40(%rsp)
leaq 0x888(%rsp), %rdi
movl $0x2, %esi
callq 0x409f0c
movslq %ebx, %rax
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rdi
addq %rax, %rdi
callq 0x4449d0
movq %rax, %r15
leaq 0xcfc88(%rip), %r12     # 0x4d69d0
movq %r12, %rdi
callq 0x4165da
movq %r12, %rdi
callq 0x4165e4
leal 0x1(%rax), %r13d
leaq 0xcfcfe(%rip), %rdi     # 0x4d6a61
movl %r13d, %esi
xorl %eax, %eax
callq 0x427440
leaq 0xcfd0a(%rip), %rdx     # 0x4d6a7e
leaq 0x80(%rsp), %r12
movl $0x200, %esi            # imm = 0x200
movq %r12, %rdi
movl %r13d, %ecx
xorl %eax, %eax
callq 0x427510
leaq 0xcfd03(%rip), %rdx     # 0x4d6a98
leaq 0x680(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
movl %r13d, %ecx
xorl %eax, %eax
callq 0x427510
leaq 0xcfcff(%rip), %rdx     # 0x4d6ab2
leaq 0x480(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
movl %r13d, %ecx
xorl %eax, %eax
callq 0x427510
leaq 0xcfcfb(%rip), %rdx     # 0x4d6acc
leaq 0x280(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
movl %r13d, %ecx
xorl %eax, %eax
callq 0x427510
movq %r12, %rdi
xorl %esi, %esi
callq 0x481890
movl %eax, %r12d
cmpl $-0x1, %eax
jne 0x40709e
movl %r12d, 0x10(%rsp)
leaq 0x34(%r14), %r8
leaq 0x40(%r14), %r9
movl 0x18(%r14), %r10d
movl 0x1c(%r14), %r11d
subq $0x8, %rsp
leaq 0x58(%rsp), %r12
leaq 0x48(%rsp), %rax
xorl %edi, %edi
movl %ebx, %esi
movl %ebp, 0x1c(%rsp)
movl %ebp, %edx
movq 0x28(%rsp), %rcx
movss 0x20(%rsp), %xmm0
movss 0x24(%rsp), %xmm1
pushq %r15
leaq 0x898(%rsp), %r13
pushq %r13
pushq %r11
pushq 0x8(%r14)
pushq %r10
pushq (%r14)
pushq 0x28(%r14)
movq 0x40(%rsp), %r10
pushq (%r10)
movq 0x50(%rsp), %r10
pushq (%r10)
pushq %r12
pushq %rax
callq 0x41618b
addq $0x60, %rsp
leal (,%rbx,4), %r12d
leal (%r12,%r12,2), %eax
movslq %eax, %rdi
callq 0x4449d0
movq %rax, %r14
movslq %r12d, %rax
leaq (%r14,%rax), %rcx
movq %rcx, (%rsp)
addq %rax, %rcx
movq %rcx, 0x8(%rsp)
testl %ebx, %ebx
jle 0x406fab
movl %ebx, %r12d
addq $0xc, %r15
leaq (%r14,%rax,2), %rbp
addq $0x3, %rbp
leaq (%rax,%r14), %rbx
addq $0x3, %rbx
xorl %r13d, %r13d
movss -0xc(%r15), %xmm0
callq 0x4166f7
movb %al, (%r14,%r13,4)
movss -0x8(%r15), %xmm0
callq 0x4166f7
movb %al, 0x1(%r14,%r13,4)
movss -0x4(%r15), %xmm0
callq 0x4166f7
movb %al, 0x2(%r14,%r13,4)
movss 0xce59a(%rip), %xmm0    # 0x4d5490
callq 0x4166f7
movb %al, 0x3(%r14,%r13,4)
movq %r15, %rdi
leaq 0x28(%rsp), %rsi
callq 0x4168a5
movss 0x28(%rsp), %xmm0
callq 0x4166f7
movb %al, -0x3(%rbx,%r13,4)
movss 0x2c(%rsp), %xmm0
callq 0x4166f7
movb %al, -0x2(%rbx,%r13,4)
xorps %xmm0, %xmm0
callq 0x4166f7
movb %al, -0x1(%rbx,%r13,4)
movss 0xce54e(%rip), %xmm0    # 0x4d5490
callq 0x4166f7
movb %al, (%rbx,%r13,4)
movss 0xc(%r15), %xmm0
leaq 0x30(%rsp), %rdi
callq 0x41674a
movss 0x30(%rsp), %xmm0
callq 0x4166f7
movb %al, -0x3(%rbp,%r13,4)
movss 0x34(%rsp), %xmm0
callq 0x4166f7
movb %al, -0x2(%rbp,%r13,4)
movss 0x38(%rsp), %xmm0
callq 0x4166f7
movb %al, -0x1(%rbp,%r13,4)
movss 0x3c(%rsp), %xmm0
callq 0x4166f7
movb %al, (%rbp,%r13,4)
incq %r13
addq $0x1c, %r15
cmpq %r13, %r12
jne 0x406ebf
movq 0x20(%rsp), %r12
leal (%r12,%r12,2), %ecx
leaq 0x80(%rsp), %rdi
movq %r14, %rsi
movl 0x14(%rsp), %ebp
movl %ebp, %edx
movl $0x4, %r8d
movl $0x64, %r9d
callq 0x404b44
leaq 0xcfa77(%rip), %rbx     # 0x4d6a54
leaq 0x680(%rsp), %r15
movq %rbx, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x427440
movq %r15, %rdi
movq %r14, %rsi
movl %ebp, %edx
movl %r12d, %ecx
movl $0x4, %r8d
movl $0x64, %r9d
callq 0x404b44
movq %rbx, %rdi
movq %r15, %rsi
xorl %eax, %eax
callq 0x427440
leaq 0x480(%rsp), %r14
movq %r14, %rdi
movq (%rsp), %rsi
movl %ebp, %edx
movl %r12d, %ecx
movl $0x4, %r8d
movl $0x64, %r9d
callq 0x404b44
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x427440
leaq 0x280(%rsp), %r14
movq %r14, %rdi
movq 0x8(%rsp), %rsi
movl %ebp, %edx
movl %r12d, %ecx
movl $0x4, %r8d
movl $0x64, %r9d
callq 0x404b44
movq %rbx, %rdi
movq %r14, %rsi
xorl %eax, %eax
callq 0x427440
movl 0x10(%rsp), %r12d
cmpl $-0x1, %r12d
sete %al
addq $0x1bf8, %rsp           # imm = 0x1BF8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x518868, %rax         # imm = 0x518868
movq (%rax), %rdi
leaq 0xcf949(%rip), %rsi     # 0x4d69f8
leaq 0x80(%rsp), %rdx
xorl %eax, %eax
callq 0x427380
jmp 0x407085