bool RenderDevice::SetParams(const RendererParams& newParams)
{
    String oldMonitor = Params.MonitorName;

    Params = newParams;
    if (newParams.MonitorName != oldMonitor)
    {
        UpdateMonitorOutputs();
    }

    return RecreateSwapChain();
}
bool RenderDevice::SetParams(const RendererParams& newParams)
{
    String oldMonitor = Params.MonitorName;

    Params = newParams;
    if (newParams.MonitorName != oldMonitor)
    {
        UpdateMonitorOutputs();
    }

    // Cause this to be recreated with the new multisample mode.
    pSceneColorTex = NULL;
    return RecreateSwapChain();
}
RenderDevice::RenderDevice(const RendererParams& p, HWND window)
{
    RECT rc;
    if (p.Resolution == Sizei(0))
    {
        GetClientRect(window, &rc);        
        WindowWidth = rc.right - rc.left;
        WindowHeight= rc.bottom - rc.top;
    }
    else
    {
        // TBD: Rename from WindowHeight or use Resolution from params for surface
        WindowWidth = p.Resolution.w;
        WindowHeight= p.Resolution.h;
    }

    Window       = window;
    Params       = p;

    DXGIFactory = NULL;
    HRESULT hr = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)(&DXGIFactory.GetRawRef()));
    if (FAILED(hr))    
        return;

    // Find the adapter & output (monitor) to use for fullscreen, based on the reported name of the HMD's monitor.
    if (Params.MonitorName.GetLength() > 0)
    {
        for(UINT AdapterIndex = 0; ; AdapterIndex++)
        {
            Adapter = NULL;
            HRESULT hr = DXGIFactory->EnumAdapters(AdapterIndex, &Adapter.GetRawRef());
            if (hr == DXGI_ERROR_NOT_FOUND)
                break;

            DXGI_ADAPTER_DESC Desc;
            Adapter->GetDesc(&Desc);

            UpdateMonitorOutputs();

            if (FullscreenOutput)
                break;
        }

        if (!FullscreenOutput)
            Adapter = NULL;
    }

    if (!Adapter)
    {
        DXGIFactory->EnumAdapters(0, &Adapter.GetRawRef());
    }

    int flags = 0;
    //int flags =  D3D11_CREATE_DEVICE_DEBUG;    

    Device = NULL;
    Context = NULL;
    hr = D3D11CreateDevice(Adapter, Adapter ? D3D_DRIVER_TYPE_UNKNOWN : D3D_DRIVER_TYPE_HARDWARE,
                            NULL, flags, NULL, 0, D3D11_SDK_VERSION,
                            &Device.GetRawRef(), NULL, &Context.GetRawRef());

    if (FAILED(hr))
        return;

    if (!RecreateSwapChain())
        return;

    if (Params.Fullscreen)
        SwapChain->SetFullscreenState(1, FullscreenOutput);

    initShadersAndStates();
}
RenderDevice::RenderDevice(const RendererParams& p, HWND window)
{
    RECT rc;
    GetClientRect(window, &rc);
    UINT width   = rc.right - rc.left;
    UINT height  = rc.bottom - rc.top;
    WindowWidth  = width;
    WindowHeight = height;
    Window       = window;
    Params       = p;

	memset(UniformBuffers, 0, sizeof(UniformBuffers));
	memset(CommonUniforms, 0, sizeof(CommonUniforms));
	QuadVertexBuffer = NULL;

    HRESULT hr = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)(&DXGIFactory.GetRawRef()));
    if (FAILED(hr))    
        return;

    // Find the adapter & output (monitor) to use for fullscreen, based on the reported name of the HMD's monitor.
    if (Params.MonitorName.GetLength() > 0)
    {
        for(UINT AdapterIndex = 0; ; AdapterIndex++)
        {
            HRESULT hr = DXGIFactory->EnumAdapters(AdapterIndex, &Adapter.GetRawRef());
            if (hr == DXGI_ERROR_NOT_FOUND)
                break;

            DXGI_ADAPTER_DESC Desc;
            Adapter->GetDesc(&Desc);

            UpdateMonitorOutputs();

            if (FullscreenOutput)
                break;
        }

        if (!FullscreenOutput)
            Adapter = NULL;
    }

    if (!Adapter)
    {
        DXGIFactory->EnumAdapters(0, &Adapter.GetRawRef());
    }

    int flags = 0;

    hr = D3D10CreateDevice(Adapter, D3D10_DRIVER_TYPE_HARDWARE, NULL, flags, D3D1x_(SDK_VERSION),
                           &Device.GetRawRef());
    Context = Device;
    Context->AddRef();

    if (FAILED(hr))
        return;

    if (!RecreateSwapChain())
        return;

    if (Params.Fullscreen)
        SwapChain->SetFullscreenState(1, FullscreenOutput);

    CurRenderTarget = NULL;
    for(int i = 0; i < Shader_Count; i++)
    {
        UniformBuffers[i] = CreateBuffer();
        MaxTextureSet[i] = 0;
    }

    ID3D10Blob* vsData = CompileShader("vs_4_0", DirectVertexShaderSrc);
    VertexShaders[VShader_MV] = *new VertexShader(this, vsData);
    for(int i = 1; i < VShader_Count; i++)
    {
        VertexShaders[i] = *new VertexShader(this, CompileShader("vs_4_0", VShaderSrcs[i]));
    }

    for(int i = 0; i < FShader_Count; i++)
    {
        PixelShaders[i] = *new PixelShader(this, CompileShader("ps_4_0", FShaderSrcs[i]));
    }

    SPInt               bufferSize = vsData->GetBufferSize();
    const void*         buffer     = vsData->GetBufferPointer();
    ID3D1xInputLayout** objRef     = &ModelVertexIL.GetRawRef();

    HRESULT validate = Device->CreateInputLayout(ModelVertexDesc, sizeof(ModelVertexDesc)/sizeof(D3D1x_(INPUT_ELEMENT_DESC)),
                                                 buffer, bufferSize, objRef);
    OVR_UNUSED(validate);

    Ptr<ShaderSet> gouraudShaders = *new ShaderSet();
    gouraudShaders->SetShader(VertexShaders[VShader_MVP]);
    gouraudShaders->SetShader(PixelShaders[FShader_Gouraud]);
    DefaultFill = *new ShaderFill(gouraudShaders);

    D3D1x_(BLEND_DESC) bm;
    memset(&bm, 0, sizeof(bm));
    bm.BlendEnable[0] = true;
    bm.BlendOp      = bm.BlendOpAlpha   = D3D1x_(BLEND_OP_ADD);
    bm.SrcBlend     = bm.SrcBlendAlpha  = D3D1x_(BLEND_SRC_ALPHA);
    bm.DestBlend    = bm.DestBlendAlpha = D3D1x_(BLEND_INV_SRC_ALPHA);
    bm.RenderTargetWriteMask[0]         = D3D1x_(COLOR_WRITE_ENABLE_ALL);
    Device->CreateBlendState(&bm, &BlendState.GetRawRef());

    D3D1x_(RASTERIZER_DESC) rs;
    memset(&rs, 0, sizeof(rs));
    rs.AntialiasedLineEnable = true;
    rs.CullMode              = D3D1x_(CULL_BACK);    
    rs.DepthClipEnable       = true;
    rs.FillMode              = D3D1x_(FILL_SOLID);
    Device->CreateRasterizerState(&rs, &Rasterizer.GetRawRef());

    QuadVertexBuffer = CreateBuffer();
    const RenderTiny::Vertex QuadVertices[] =
    { Vertex(Vector3f(0, 1, 0)), Vertex(Vector3f(1, 1, 0)),
      Vertex(Vector3f(0, 0, 0)), Vertex(Vector3f(1, 0, 0)) };
    QuadVertexBuffer->Data(Buffer_Vertex, QuadVertices, sizeof(QuadVertices));

    SetDepthMode(0, 0);
}
RenderDevice::RenderDevice(const RendererParams& p, HWND window) :
    WindowWidth(0),
    WindowHeight(0),
    Params(),
    Proj(),
    pTextVertexBuffer(),
    LightingBuffer(),
    DXGIFactory(),
    Window(window),
    Device(),
    Context(),
    SwapChain(),
    Adapter(),
    FullscreenOutput(),
    FSDesktopX(-1),
    FSDesktopY(-1),
    BackBuffer(),
    BackBufferRT(),
    CurRenderTarget(),
    CurDepthBuffer(),
    Rasterizer(),
    BlendState(),
  //D3DViewport()
  //DepthStates();
    CurDepthState(),
    ModelVertexIL(),
  //SamplerStates(),
    StdUniforms(),
  //UniformBuffers(),
  //MaxTextureSet(),
  //VertexShaders(),
  //PixelShaders(),
  //CommonUniforms(),
    DefaultFill(),
    QuadVertexBuffer(),
    DepthBuffers()
{
    memset(&D3DViewport, 0, sizeof(D3DViewport));
    memset(MaxTextureSet, 0, sizeof(MaxTextureSet));

    RECT rc;
    if (p.Resolution == Sizei(0))
    {
        GetClientRect(window, &rc);        
        WindowWidth  = rc.right - rc.left;
        WindowHeight = rc.bottom - rc.top;
    }
    else
    {
        // TBD: Rename from WindowHeight or use Resolution from params for surface
        WindowWidth  = p.Resolution.w;
        WindowHeight = p.Resolution.h;
    }

    Window       = window;
    Params       = p;

    DXGIFactory = NULL;
    HRESULT hr = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)(&DXGIFactory.GetRawRef()));
    if (FAILED(hr))
        return;

    // Find the adapter & output (monitor) to use for fullscreen, based on the reported name of the HMD's monitor.
    if (Params.MonitorName.GetLength() > 0)
    {
        for (UINT AdapterIndex = 0; ; AdapterIndex++)
        {
            Adapter = NULL;
            HRESULT hr = DXGIFactory->EnumAdapters(AdapterIndex, &Adapter.GetRawRef());
            if (hr == DXGI_ERROR_NOT_FOUND)
                break;

            DXGI_ADAPTER_DESC Desc;
            Adapter->GetDesc(&Desc);

            UpdateMonitorOutputs();

            if (FullscreenOutput)
                break;
        }

        if (!FullscreenOutput)
            Adapter = NULL;
    }

    if (!Adapter)
    {
        hr = DXGIFactory->EnumAdapters(0, &Adapter.GetRawRef());
        if (FAILED(hr) || !Adapter)
        {
            OVR_DEBUG_LOG(("WARNING: No graphics adapter."));
            OVR_ASSERT(false);
        }
    }

    int flags = 0;
    //int flags =  D3D11_CREATE_DEVICE_DEBUG;

    Device = NULL;
    Context = NULL;
    hr = D3D11CreateDevice(Adapter, Adapter ? D3D_DRIVER_TYPE_UNKNOWN : D3D_DRIVER_TYPE_HARDWARE,
                            NULL, flags, NULL, 0, D3D11_SDK_VERSION,
                            &Device.GetRawRef(), NULL, &Context.GetRawRef());

    if (FAILED(hr))
        return;

    if (!RecreateSwapChain())
        return;

    if (Params.Fullscreen)
        SwapChain->SetFullscreenState(1, FullscreenOutput);

    initShadersAndStates();
}