Menu

tyo Engine Revolution 开发日志3

从深圳出差回来了,最近都在折腾公司引擎的编辑器,所以没有多少的时间可以投入到自己的引擎中。经过反复的思考和选择,我最终还是决定了直接实现一套3D的渲染器,不再使用Direct2D来单独实现一套2D渲染。

引擎的渲染模块是一个比较头疼的问题,整体的可视效果应该都取决于这个模块了。最近买了一个黑寡妇键盘,感觉还不错,就是左边多了一排键,盲打的时候总是习惯性的按错。

今天实现了DX11的初始化操作,不过对于这个初始化,估计最后对于RenderTargetView和BackBuffer的地方估计还要修改。因为,最终的引擎,估计会用到2个Buffer来进行缓冲。

对于初始化接口的实现如下:


_tyoER_void _tyoER_call Render::_tyoER_RenderDX11Implement::Initialize( HWND _hWnd , _tyoER_int _iScreenWidth , _tyoER_int _iScreenHeight )
{
  HWnd = _hWnd;
  ScreenWidth = _iScreenWidth;
  ScreenHeight = _iScreenHeight;

  if(HWnd)
  {
    HRESULT _hr;

    D3D_DRIVER_TYPE _driverTypes[] =
    {
      D3D_DRIVER_TYPE_HARDWARE,
      D3D_DRIVER_TYPE_WARP,
      D3D_DRIVER_TYPE_REFERENCE,
    };

    _tyoER_uint _numDriverTypes = ARRAYSIZE( _driverTypes );

    D3D_FEATURE_LEVEL _featureLevels[] =
    {
      D3D_FEATURE_LEVEL_11_0,//Targets features supported by Direct3D 11.0 including shader shader model 5.
      D3D_FEATURE_LEVEL_10_1,//Targets features supported by Direct3D 10.1 including shader shader model 4.
      D3D_FEATURE_LEVEL_10_0,
    };

    _tyoER_uint _numFeatureLevels = ARRAYSIZE( _featureLevels );

    // create a struct to hold information about the swap chain
    DXGI_SWAP_CHAIN_DESC _scd;

    // clear out the struct for use
    ZeroMemory(&_scd, sizeof(DXGI_SWAP_CHAIN_DESC));

    // fill the swap chain description struct
    _scd.BufferCount = 1;                                   // one back buffer
    _scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;    // use 32-bit color
    _scd.BufferDesc.Width = _iScreenWidth;                   // set the back buffer width
    _scd.BufferDesc.Height = _iScreenHeight;                 // set the back buffer height
    _scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT | DXGI_USAGE_SHADER_INPUT;     // how swap chain is to be used
    _scd.OutputWindow = HWnd;                             // the window to be used
    _scd.SampleDesc.Count = 1;                              // how many multisamples
    _scd.Windowed = TRUE;                                   // windowed/full-screen mode
    _scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;    // allow full-screen switching

    // create a device, device context and swap chain using the information in the scd struct
    
    _tyoER_uint _createDeviceFlags = 0;

#ifdef _DEBUG
    _createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

    for( _tyoER_uint _driverTypeIndex = 0; _driverTypeIndex < _numDriverTypes; _driverTypeIndex++ )
    {
      DriverType = _driverTypes[_driverTypeIndex];

      _hr = D3D11CreateDeviceAndSwapChain(_tyoER_null, DriverType, _tyoER_null , _createDeviceFlags,
        _featureLevels, _numFeatureLevels, D3D11_SDK_VERSION,
        &_scd,
        &DXGISwapChain, //a swap chain interface
        &D3D11Device, //a device interface
        &FeatureLevel,//a pointer to the feature level
        &D3D11ImmediateContext);// an immediate context interface [8/30/2012 Garfield Chen]

      if( SUCCEEDED( _hr ) )
      {
        break;
      }
    }

    if(SUCCEEDED(_hr))
    {
      ID3D11Texture2D* _pBackBuffer = _tyoER_null;
      
      _hr = DXGISwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&_pBackBuffer ); //get the back buffer object

      if(SUCCEEDED(_hr))
      {
        _hr = D3D11Device->CreateRenderTargetView( _pBackBuffer, _tyoER_null, &RenderTargetView );//create a default render target view

        _pBackBuffer->Release();

        if(SUCCEEDED(_hr))
        {
          //bind the render target view to the pipeline

          D3D11ImmediateContext->OMSetRenderTargets( 1, &RenderTargetView, _tyoER_null );

          // Setup the viewport

          D3D11_VIEWPORT _viewPort;
          _viewPort.Width = (_tyoER_float)ScreenWidth;
          _viewPort.Height = (_tyoER_float)ScreenHeight;
          _viewPort.MinDepth = 0.0f;
          _viewPort.MaxDepth = 1.0f;
          _viewPort.TopLeftX = 0;
          _viewPort.TopLeftY = 0;

          D3D11ImmediateContext->RSSetViewports( 1, &_viewPort );
        }
      }
    }
  }
}

最后,简单的Clear了一下RenderTargetView,发现已经挂起了,可用了。

截图:

最近有时间都会搞引擎,也下载了Win8和vs2012,还没有安装- –

我也想优先体验一下,不过,不知道Mac装上后,驱动是否能够继续正常的工作- –

Categories:   Garfield's Diary

Comments