// File: /workspace/github_c_makefile_star2_O2/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, %r12
movl %edi, %r13d
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
andq $0x0, 0x40(%rsp)
leaq 0x888(%rsp), %rdi
pushq $0x2
popq %rsi
callq 0x409184
movslq %ebx, %rax
imulq $0x1c, %rax, %rdi
callq 0x442160
movq %rax, %r15
leaq 0xcd268(%rip), %r14     # 0x4d39c0
movq %r14, %rdi
callq 0x413e50
movq %r14, %rdi
callq 0x413e5a
leal 0x1(%rax), %r14d
leaq 0xcd2de(%rip), %rdi     # 0x4d3a51
xorl %ebp, %ebp
movl %r14d, %esi
xorl %eax, %eax
callq 0x424bd0
leaq 0xcd2e8(%rip), %rdx     # 0x4d3a6e
leaq 0x80(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
movl %r14d, %ecx
xorl %eax, %eax
callq 0x424ca0
leaq 0xcd2e4(%rip), %rdx     # 0x4d3a88
leaq 0x680(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
movl %r14d, %ecx
xorl %eax, %eax
callq 0x424ca0
leaq 0xcd2e0(%rip), %rdx     # 0x4d3aa2
leaq 0x480(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
movl %r14d, %ecx
xorl %eax, %eax
callq 0x424ca0
leaq 0xcd2dc(%rip), %rdx     # 0x4d3abc
leaq 0x280(%rsp), %rdi
movl $0x200, %esi            # imm = 0x200
movl %r14d, %ecx
xorl %eax, %eax
callq 0x424ca0
leaq 0x80(%rsp), %rdi
xorl %esi, %esi
callq 0x47f010
movl %eax, %r14d
cmpl $-0x1, %eax
jne 0x406abb
movl %r14d, 0x10(%rsp)
leaq 0x34(%r12), %r8
leaq 0x40(%r12), %r9
movl 0x18(%r12), %r10d
movl 0x1c(%r12), %r11d
subq $0x8, %rsp
leaq 0x58(%rsp), %r14
leaq 0x48(%rsp), %rax
xorl %edi, %edi
movl %ebx, %esi
movl %r13d, 0x1c(%rsp)
movl %r13d, %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(%r12)
pushq %r10
pushq (%r12)
pushq 0x28(%r12)
movq 0x40(%rsp), %r10
pushq (%r10)
movq 0x50(%rsp), %r10
pushq (%r10)
pushq %r14
pushq %rax
callq 0x413a63
addq $0x60, %rsp
leal (,%rbx,4), %r14d
imull $0xc, %ebx, %eax
movslq %eax, %rdi
callq 0x442160
movq %rax, %r13
movslq %r14d, %r12
addq %r12, %rax
testl %ebx, %ebx
cmovgl %ebx, %ebp
movq %rax, (%rsp)
addq %r12, %rax
movq %rax, 0x8(%rsp)
leaq 0x3(,%r12,2), %rbx
orq $0x3, %r12
addq $0xc, %r15
pushq $0x3
popq %r14
subq $0x1, %rbp
jb 0x4069ce
movss -0xc(%r15), %xmm0
callq 0x413f51
movb %al, -0x3(%r13,%r14)
movss -0x8(%r15), %xmm0
callq 0x413f51
movb %al, -0x2(%r13,%r14)
movss -0x4(%r15), %xmm0
callq 0x413f51
movb %al, -0x1(%r13,%r14)
movss 0xcbb4d(%rip), %xmm0    # 0x4d2460
callq 0x413f51
movb %al, (%r13,%r14)
movq %r15, %rdi
leaq 0x28(%rsp), %rsi
callq 0x4140d4
movss 0x28(%rsp), %xmm0
callq 0x413f51
movb %al, -0x3(%r13,%r12)
movss 0x2c(%rsp), %xmm0
callq 0x413f51
movb %al, -0x2(%r13,%r12)
xorps %xmm0, %xmm0
callq 0x413f51
movb %al, -0x1(%r13,%r12)
movss 0xcbb01(%rip), %xmm0    # 0x4d2460
callq 0x413f51
movb %al, (%r13,%r12)
movss 0xc(%r15), %xmm0
leaq 0x30(%rsp), %rdi
callq 0x413f9a
movss 0x30(%rsp), %xmm0
callq 0x413f51
movb %al, -0x3(%r13,%rbx)
movss 0x34(%rsp), %xmm0
callq 0x413f51
movb %al, -0x2(%r13,%rbx)
movss 0x38(%rsp), %xmm0
callq 0x413f51
movb %al, -0x1(%r13,%rbx)
movss 0x3c(%rsp), %xmm0
callq 0x413f51
movb %al, (%r13,%rbx)
addq $0x4, %rbx
addq $0x4, %r12
addq $0x4, %r14
addq $0x1c, %r15
jmp 0x4068d1
movq 0x20(%rsp), %rbx
leal (%rbx,%rbx,2), %ecx
leaq 0x80(%rsp), %rdi
pushq $0x4
popq %r15
pushq $0x64
popq %r9
movq %r13, %rsi
movl 0x14(%rsp), %r14d
movl %r14d, %edx
movl %r15d, %r8d
callq 0x4047f4
leaq 0xcd044(%rip), %r12     # 0x4d3a44
leaq 0x680(%rsp), %rbp
movq %r12, %rdi
movq %rbp, %rsi
xorl %eax, %eax
callq 0x424bd0
movq %rbp, %rdi
movq %r13, %rsi
movl %r14d, %edx
movl %ebx, %ecx
movl %r15d, %r8d
pushq $0x64
popq %r15
movl %r15d, %r9d
callq 0x4047f4
movq %r12, %rdi
movq %rbp, %rsi
xorl %eax, %eax
callq 0x424bd0
leaq 0x480(%rsp), %r13
movq %r13, %rdi
movq (%rsp), %rsi
movl %r14d, %edx
movl %ebx, %ecx
pushq $0x4
popq %r8
movl %r8d, %ebp
movl %r15d, %r9d
callq 0x4047f4
movq %r12, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0x424bd0
leaq 0x280(%rsp), %r12
movq %r12, %rdi
movq 0x8(%rsp), %rsi
movl %r14d, %edx
movl %ebx, %ecx
movl %ebp, %r8d
movl %r15d, %r9d
callq 0x4047f4
leaq 0xccfb1(%rip), %rdi     # 0x4d3a44
movq %r12, %rsi
xorl %eax, %eax
callq 0x424bd0
movl 0x10(%rsp), %r14d
cmpl $-0x1, %r14d
sete %al
addq $0x1bf8, %rsp           # imm = 0x1BF8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq $0x516868, %rax         # imm = 0x516868
movq (%rax), %rdi
leaq 0xccf1c(%rip), %rsi     # 0x4d39e8
leaq 0x80(%rsp), %rdx
xorl %eax, %eax
callq 0x424b10
jmp 0x406aa2