mainProcess ghmd hmd' = do
  !glhdl <- initGL
  let hmd = fromJust hmd'
  traceIO $ "create hmd OK : " ++ show hmd
  !msg <- ovrHmd_GetLastError hmd
  traceIO $ "GetLastError = " ++ msg ++ " Msg End"
  traceIO " == Print HmdDesc =="
  hmdDesc <- castToOvrHmdDesc hmd
  printHmdDesc hmdDesc
  traceIO " ==================="
  !r <- ovrHmd_ConfigureTracking hmd
               ( ovrTrackingCap_Orientation  
               .|. ovrTrackingCap_MagYawCorrection
               .|. ovrTrackingCap_Position)
               ovrTrackingCap_None
  traceIO $ "ConfigureTracking : " ++ show r
  -- 
#if defined(mingw32_HOST_OS)
  !hwnd <- getWindowHandle "oculus test" 
  traceIO $ "windowHandle : " ++ show hwnd
  -- !hdc <- getWinDC hwnd
  !ba <- ovrHmd_AttachToWindow hmd hwnd
                               Nothing Nothing 
  let nativeWindow = Just hwnd
  traceIO $ "AttachToWindow : " ++ show (ba,hwnd)
#else
  let nativeWindow = Nothing
#endif
  
  recommenedTex0Size <- ovrHmd_GetDefaultFovTextureSize
                          hmd ovrEye_Left 1.0
  recommenedTex1Size <- ovrHmd_GetDefaultFovTextureSize
                          hmd ovrEye_Right 1.0
  traceIO $ "recommentedTexSize L : "
    ++ show recommenedTex0Size
    ++ " R : "
    ++ show recommenedTex1Size
  let !renderTargetSizeW = (si_w recommenedTex0Size)
                        + (si_w recommenedTex1Size)
      !renderTargetSizeH = max (si_h recommenedTex0Size)
                              (si_h recommenedTex1Size)
      twidth = fromIntegral renderTargetSizeW
      theight = fromIntegral renderTargetSizeH
  !tex <- genColorTexture 0 twidth theight
  !fbo <- genColorFrameBuffer tex twidth theight 
  --
  let !eyeTexture = genEyeTextureData tex renderTargetSizeW
                                          renderTargetSizeH
      !hd = OvrRenderAPIConfigHeader
                   ovrRenderAPI_OpenGL
                   (resolution hmdDesc) 
                   0 --  1
      !apiconf = OvrRenderAPIConfig hd nativeWindow Nothing --  (Just hdc)
      !caps =
                 ovrDistortionCap_Vignette
         --    .|. ovrDistortionCap_SRGB
             .|. ovrDistortionCap_Overdrive 
             .|. ovrDistortionCap_TimeWarp
         --    .|. ovrDistortionCap_ProfileNoSpinWaits 
             .|. ovrDistortionCap_HqDistortion
         --    .|. ovrDistortionCap_ComputeShader
         --
         --    .|. ovrDistortionCap_NoRestore
         --    .|. ovrDistortionCap_FlipInput 
  traceIO $ "OvrEyeTexture : " ++ show eyeTexture
  traceIO $ "OvrRenderAPIConfigHeader : " ++ show hd
  traceIO $ "render caps : " ++ show caps
  !lfv <- ovrHmd_GetDefaultFov hmd ovrEye_Left
  !rfv <- ovrHmd_GetDefaultFov hmd ovrEye_Right
  !(bret, eyeRD) <- ovrHmd_ConfigureRendering hmd
                    (Just apiconf) caps [lfv,rfv]
  traceIO $ "ConfigureRendering : " ++ show (bret,eyeRD)
  --
  ovrHmd_SetEnabledCaps hmd ( 
   --     ovrHmdCap_Present          
   -- .|. ovrHmdCap_Available
   -- .|. ovrHmdCap_Captured
   --  ovrHmdCap_ExtendDesktop 
   -- .|. ovrHmdCap_DisplayOff
        ovrHmdCap_LowPersistence
    .|. ovrHmdCap_DynamicPrediction
   -- .|. ovrHmdCap_NoMirrorToWindow
   -- .|. ovrHmdCap_NoVSync          
    )
  -- !tis <- ovr_GetTimeInSeconds
  -- traceIO $ "GetTimeInSeconds : " ++ (show tis)

  msg2 <- ovrHmd_GetLastError hmd
  traceIO $ "GetLastError 2 = " ++ msg2 ++ " Msg End"
  printError 
  ovrHmd_RecenterPose hmd
  tex <- loadTextureObj "test/sample_tex.png"
  mainLoop hmd ghmd glhdl (eyeTexture,tex,fbo) eyeRD tex 0
  --
  ovrHmd_ConfigureRendering hmd Nothing caps [lfv,rfv]
  return ()
  where
mainProcess ghmd hmd' = do
  !glhdl <- initGL
  let hmd = fromJust hmd'
  traceIO $ "create hmd OK : " ++ show hmd
  !msg <- ovrHmd_GetLastError hmd
  traceIO $ "GetLastError = " ++ msg ++ " Msg End"
  traceIO " == Print HmdDesc =="
  hmdDesc <- castToOvrHmdDesc hmd
  printHmdDesc hmdDesc
  traceIO " ==================="
  !r <- ovrHmd_ConfigureTracking hmd
               ( ovrTrackingCap_Orientation  
               .|. ovrTrackingCap_MagYawCorrection
               .|. ovrTrackingCap_Position)
               ovrTrackingCap_None
  traceIO $ "ConfigureTracking : " ++ show r
  -- 
#if defined(mingw32_HOST_OS)
  !hwnd <- getWindowHandle "oculus test" 
  traceIO $ "windowHandle : " ++ show hwnd
  -- !hdc <- getWinDC hwnd
  !ba <- ovrHmd_AttachToWindow hmd hwnd
                               Nothing Nothing 
  let nativeWindow = Just hwnd
  traceIO $ "AttachToWindow : " ++ show (ba,hwnd)
#else
  let nativeWindow = Nothing
#endif
  
  recommenedTex0Size <- ovrHmd_GetDefaultFovTextureSize
                          hmd ovrEye_Left 1.0
  recommenedTex1Size <- ovrHmd_GetDefaultFovTextureSize
                          hmd ovrEye_Right 1.0
  traceIO $ "recommentedTexSize L : "
    ++ show recommenedTex0Size
    ++ " R : "
    ++ show recommenedTex1Size
  let !renderTargetSizeW = (si_w recommenedTex0Size)
                        + (si_w recommenedTex1Size)
      !renderTargetSizeH = max (si_h recommenedTex0Size)
                              (si_h recommenedTex1Size)
      twidth = fromIntegral renderTargetSizeW
      theight = fromIntegral renderTargetSizeH
  !tex <- genColorTexture 0 twidth theight
  !fbo <- genColorFrameBuffer tex twidth theight 
  --
  let !eyeTexture = genEyeTextureData tex renderTargetSizeW
                                          renderTargetSizeH
      !hd = OvrRenderAPIConfigHeader
                   ovrRenderAPI_OpenGL
                   (resolution hmdDesc) 
                   0 --  1
      !apiconf = OvrRenderAPIConfig hd nativeWindow Nothing --  (Just hdc)
      !caps =
                 ovrDistortionCap_Vignette
         --    .|. ovrDistortionCap_SRGB
             .|. ovrDistortionCap_Overdrive 
             .|. ovrDistortionCap_TimeWarp
         --    .|. ovrDistortionCap_ProfileNoSpinWaits 
             .|. ovrDistortionCap_HqDistortion
         --    .|. ovrDistortionCap_ComputeShader
         --
         --    .|. ovrDistortionCap_NoRestore
         --    .|. ovrDistortionCap_FlipInput 
  traceIO $ "OvrEyeTexture : " ++ show eyeTexture
  traceIO $ "OvrRenderAPIConfigHeader : " ++ show hd
  traceIO $ "render caps : " ++ show caps
  !lfv <- ovrHmd_GetDefaultFov hmd ovrEye_Left
  !rfv <- ovrHmd_GetDefaultFov hmd ovrEye_Right
  !(bret, eyeRD) <- ovrHmd_ConfigureRendering hmd
                    (Just apiconf) caps [lfv,rfv]
  traceIO $ "ConfigureRendering : " ++ show (bret,eyeRD)
  --
  ovrHmd_SetEnabledCaps hmd ( 
   --     ovrHmdCap_Present          
   -- .|. ovrHmdCap_Available
   -- .|. ovrHmdCap_Captured
   --  ovrHmdCap_ExtendDesktop 
   -- .|. ovrHmdCap_DisplayOff
        ovrHmdCap_LowPersistence
    .|. ovrHmdCap_DynamicPrediction
   -- .|. ovrHmdCap_NoMirrorToWindow
   -- .|. ovrHmdCap_NoVSync          
    )
  -- !tis <- ovr_GetTimeInSeconds
  -- traceIO $ "GetTimeInSeconds : " ++ (show tis)

  msg2 <- ovrHmd_GetLastError hmd
  traceIO $ "GetLastError 2 = " ++ msg2 ++ " Msg End"
  printError 
  ovrHmd_RecenterPose hmd
  tex <- loadTextureObj "test/sample_tex.png"
  mainLoop hmd ghmd glhdl (eyeTexture,tex,fbo) eyeRD tex 0
  --
  ovrHmd_ConfigureRendering hmd Nothing caps [lfv,rfv]
  return ()
  where
mainProcess ghmd hmd' = do
  !glhdl <- initGL
  let hmd = fromJust hmd'
  traceIO $ "create hmd OK : " ++ show hmd
  !msg <- ovrHmd_GetLastError hmd
  traceIO $ "GetLastError = " ++ msg ++ " Msg End"
  traceIO " == Print HmdDesc =="
  hmdDesc <- castToOvrHmdDesc hmd
  printHmdDesc hmdDesc
  traceIO " ==================="
  !r <- ovrHmd_ConfigureTracking hmd
               ( ovrTrackingCap_Orientation  
               .|. ovrTrackingCap_MagYawCorrection
               .|. ovrTrackingCap_Position)
               ovrTrackingCap_None
  traceIO $ "ConfigureTracking : " ++ show r
  -- 
#if defined(mingw32_HOST_OS)
  !hwnd <- getWindowHandle "oculus test" 
  traceIO $ "windowHandle : " ++ show hwnd
  -- !hdc <- getWinDC hwnd
  !ba <- ovrHmd_AttachToWindow hmd hwnd
                               Nothing Nothing 
  let nativeWindow = Just hwnd
  traceIO $ "AttachToWindow : " ++ show (ba,hwnd)
#else
  let nativeWindow = Nothing
#endif
  
  recommenedTex0Size <- ovrHmd_GetDefaultFovTextureSize
                          hmd ovrEye_Left 1.0
  recommenedTex1Size <- ovrHmd_GetDefaultFovTextureSize
                          hmd ovrEye_Right 1.0
  traceIO $ "recommentedTexSize L : "
    ++ show recommenedTex0Size
    ++ " R : "
    ++ show recommenedTex1Size
  let !renderTargetSizeW = (si_w recommenedTex0Size)
                        + (si_w recommenedTex1Size)
      !renderTargetSizeH = max (si_h recommenedTex0Size)
                              (si_h recommenedTex1Size)
      twidth = fromIntegral renderTargetSizeW
      theight = fromIntegral renderTargetSizeH
  !tex <- genColorTexture 0 twidth theight
  !fbo <- genColorFrameBuffer tex twidth theight 
  --
  let !eyeTexture = genEyeTextureData tex renderTargetSizeW
                                          renderTargetSizeH
      !hd = OvrRenderAPIConfigHeader
                   ovrRenderAPI_OpenGL
                   (resolution hmdDesc) 
                   0 --  1
      !apiconf = OvrRenderAPIConfig hd nativeWindow Nothing --  (Just hdc)
      !caps =
                 ovrDistortionCap_Vignette
         --    .|. ovrDistortionCap_SRGB
             .|. ovrDistortionCap_Overdrive 
             .|. ovrDistortionCap_TimeWarp
         --    .|. ovrDistortionCap_ProfileNoSpinWaits 
             .|. ovrDistortionCap_HqDistortion
         --    .|. ovrDistortionCap_ComputeShader
         --
         --    .|. ovrDistortionCap_NoRestore
         --    .|. ovrDistortionCap_FlipInput 
  traceIO $ "OvrEyeTexture : " ++ show eyeTexture
  traceIO $ "OvrRenderAPIConfigHeader : " ++ show hd
  traceIO $ "render caps : " ++ show caps
  !lfv <- ovrHmd_GetDefaultFov hmd ovrEye_Left
  !rfv <- ovrHmd_GetDefaultFov hmd ovrEye_Right
  !(bret, eyeRD) <- ovrHmd_ConfigureRendering hmd
                    (Just apiconf) caps [lfv,rfv]
  traceIO $ "ConfigureRendering : " ++ show (bret,eyeRD)
  --
  ovrHmd_SetEnabledCaps hmd ( 
   --     ovrHmdCap_Present          
   -- .|. ovrHmdCap_Available
   -- .|. ovrHmdCap_Captured
   --  ovrHmdCap_ExtendDesktop 
   -- .|. ovrHmdCap_DisplayOff
        ovrHmdCap_LowPersistence
    .|. ovrHmdCap_DynamicPrediction
   -- .|. ovrHmdCap_NoMirrorToWindow
   -- .|. ovrHmdCap_NoVSync          
    )
  -- !tis <- ovr_GetTimeInSeconds
  -- traceIO $ "GetTimeInSeconds : " ++ (show tis)

  msg2 <- ovrHmd_GetLastError hmd
  traceIO $ "GetLastError 2 = " ++ msg2 ++ " Msg End"
  printError 
  ovrHmd_RecenterPose hmd
  tex <- loadTextureObj "test/sample_tex.png"
  mainLoop hmd ghmd glhdl (eyeTexture,tex,fbo) eyeRD tex 0
  --
  ovrHmd_ConfigureRendering hmd Nothing caps [lfv,rfv]
  return ()
  where