WinRT: made the Direct3D 11.1 renderer directly initialize more of itself, rather than deferring to code in the WinRT video driver
authorDavid Ludwig <dludwig@pobox.com>
Sat, 09 Feb 2013 15:22:49 -0500
changeset 84134ae81c5dc55e
parent 8412 59f1191a5cca
child 8414 25a7605097a2
WinRT: made the Direct3D 11.1 renderer directly initialize more of itself, rather than deferring to code in the WinRT video driver
src/render/direct3d11/SDL_render_d3d11.cpp
src/video/windowsrt/SDL_winrtrenderer.cpp
src/video/windowsrt/SDL_winrtrenderer.h
src/video/windowsrt/SDL_winrtvideo.cpp
     1.1 --- a/src/render/direct3d11/SDL_render_d3d11.cpp	Sat Feb 09 14:56:32 2013 -0500
     1.2 +++ b/src/render/direct3d11/SDL_render_d3d11.cpp	Sat Feb 09 15:22:49 2013 -0500
     1.3 @@ -79,11 +79,11 @@
     1.4  static void D3D11_RenderPresent(SDL_Renderer * renderer);
     1.5  //static void D3D11_DestroyTexture(SDL_Renderer * renderer,
     1.6  //                               SDL_Texture * texture);
     1.7 -//static void D3D11_DestroyRenderer(SDL_Renderer * renderer);
     1.8 +static void D3D11_DestroyRenderer(SDL_Renderer * renderer);
     1.9  
    1.10  /* Direct3D 11.1 Internal Functions */
    1.11  HRESULT D3D11_CreateDeviceResources(SDL_Renderer * renderer);
    1.12 -
    1.13 +HRESULT D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer);
    1.14  
    1.15  extern "C" {
    1.16      SDL_RenderDriver D3D11_RenderDriver = {
    1.17 @@ -110,17 +110,6 @@
    1.18  {
    1.19      SDL_Renderer *renderer;
    1.20      D3D11_RenderData *data;
    1.21 -//    SDL_SysWMinfo windowinfo;
    1.22 - //   HRESULT result;
    1.23 - //   D3DPRESENT_PARAMETERS pparams;
    1.24 - //   IDirect3DSwapChain9 *chain;
    1.25 - //   D3DCAPS9 caps;
    1.26 - //   Uint32 window_flags;
    1.27 - //   int w, h;
    1.28 - //   SDL_DisplayMode fullscreen_mode;
    1.29 - //   D3DMATRIX matrix;
    1.30 - //   int d3dxVersion;
    1.31 -	//char d3dxDLLFile[50];
    1.32  
    1.33      renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
    1.34      if (!renderer) {
    1.35 @@ -158,149 +147,38 @@
    1.36      //renderer->RenderReadPixels = D3D11_RenderReadPixels;
    1.37      renderer->RenderPresent = D3D11_RenderPresent;
    1.38      //renderer->DestroyTexture = D3D11_DestroyTexture;
    1.39 -    //renderer->DestroyRenderer = D3D11_DestroyRenderer;
    1.40 +    renderer->DestroyRenderer = D3D11_DestroyRenderer;
    1.41      renderer->info = D3D11_RenderDriver.info;
    1.42 +    renderer->info.flags = SDL_RENDERER_ACCELERATED;
    1.43      renderer->driverdata = data;
    1.44  
    1.45 -    renderer->info.flags = SDL_RENDERER_ACCELERATED;
    1.46 +    // HACK: make sure the SDL_Renderer references the SDL_Window data now, in
    1.47 +    // order to give init functions access to the underlying window handle:
    1.48 +    renderer->window = window;
    1.49  
    1.50 -    //SDL_VERSION(&windowinfo.version);
    1.51 -    //SDL_GetWindowWMInfo(window, &windowinfo);
    1.52 -
    1.53 -    //window_flags = SDL_GetWindowFlags(window);
    1.54 -    //SDL_GetWindowSize(window, &w, &h);
    1.55 -    //SDL_GetWindowDisplayMode(window, &fullscreen_mode);
    1.56 -
    1.57 -    //SDL_zero(pparams);
    1.58 -    //pparams.hDeviceWindow = windowinfo.info.win.window;
    1.59 -    //pparams.BackBufferWidth = w;
    1.60 -    //pparams.BackBufferHeight = h;
    1.61 -    //if (window_flags & SDL_WINDOW_FULLSCREEN) {
    1.62 -    //    pparams.BackBufferFormat =
    1.63 -    //        PixelFormatToD3DFMT(fullscreen_mode.format);
    1.64 -    //} else {
    1.65 -    //    pparams.BackBufferFormat = D3DFMT_UNKNOWN;
    1.66 -    //}
    1.67 -    //pparams.BackBufferCount = 1;
    1.68 -    //pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
    1.69 -
    1.70 -    //if (window_flags & SDL_WINDOW_FULLSCREEN) {
    1.71 -    //    pparams.Windowed = FALSE;
    1.72 -    //    pparams.FullScreen_RefreshRateInHz =
    1.73 -    //        fullscreen_mode.refresh_rate;
    1.74 -    //} else {
    1.75 -    //    pparams.Windowed = TRUE;
    1.76 -    //    pparams.FullScreen_RefreshRateInHz = 0;
    1.77 -    //}
    1.78 -    //if (flags & SDL_RENDERER_PRESENTVSYNC) {
    1.79 -    //    pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
    1.80 -    //} else {
    1.81 -    //    pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
    1.82 -    //}
    1.83 -
    1.84 -    ///* FIXME: Which adapter? */
    1.85 -    //data->adapter = D3DADAPTER_DEFAULT;
    1.86 -    //IDirect3D9_GetDeviceCaps(data->d3d, data->adapter, D3DDEVTYPE_HAL, &caps);
    1.87 -
    1.88 -    //result = IDirect3D9_CreateDevice(data->d3d, data->adapter,
    1.89 -    //                                 D3DDEVTYPE_HAL,
    1.90 -    //                                 pparams.hDeviceWindow,
    1.91 -    //                                 D3DCREATE_FPU_PRESERVE | ((caps.
    1.92 -    //                                  DevCaps &
    1.93 -    //                                  D3DDEVCAPS_HWTRANSFORMANDLIGHT) ?
    1.94 -    //                                 D3DCREATE_HARDWARE_VERTEXPROCESSING :
    1.95 -    //                                 D3DCREATE_SOFTWARE_VERTEXPROCESSING),
    1.96 -    //                                 &pparams, &data->device);
    1.97 -    //if (FAILED(result)) {
    1.98 -    //    D3D11_DestroyRenderer(renderer);
    1.99 -    //    D3D11_SetError("CreateDevice()", result);
   1.100 -    //    return NULL;
   1.101 -    //}
   1.102 -    //data->beginScene = SDL_TRUE;
   1.103 -    //data->scaleMode = D3DTEXF_FORCE_DWORD;
   1.104 -
   1.105 -    ///* Get presentation parameters to fill info */
   1.106 -    //result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
   1.107 -    //if (FAILED(result)) {
   1.108 -    //    D3D11_DestroyRenderer(renderer);
   1.109 -    //    D3D11_SetError("GetSwapChain()", result);
   1.110 -    //    return NULL;
   1.111 -    //}
   1.112 -    //result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
   1.113 -    //if (FAILED(result)) {
   1.114 -    //    IDirect3DSwapChain9_Release(chain);
   1.115 -    //    D3D11_DestroyRenderer(renderer);
   1.116 -    //    D3D11_SetError("GetPresentParameters()", result);
   1.117 -    //    return NULL;
   1.118 -    //}
   1.119 -    //IDirect3DSwapChain9_Release(chain);
   1.120 -    //if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
   1.121 -    //    renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
   1.122 -    //}
   1.123 -    //data->pparams = pparams;
   1.124 -
   1.125 -    //IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
   1.126 -    //renderer->info.max_texture_width = caps.MaxTextureWidth;
   1.127 -    //renderer->info.max_texture_height = caps.MaxTextureHeight;
   1.128 -    //if (caps.NumSimultaneousRTs >= 2) {
   1.129 -    //    renderer->info.flags |= SDL_RENDERER_TARGETTEXTURE;
   1.130 -    //}
   1.131 -
   1.132 -    ///* Set up parameters for rendering */
   1.133 -    //IDirect3DDevice9_SetVertexShader(data->device, NULL);
   1.134 -    //IDirect3DDevice9_SetFVF(data->device,
   1.135 -    //                        D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1);
   1.136 -    //IDirect3DDevice9_SetRenderState(data->device, D3DRS_ZENABLE, D3DZB_FALSE);
   1.137 -    //IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
   1.138 -    //                                D3DCULL_NONE);
   1.139 -    //IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
   1.140 -    ///* Enable color modulation by diffuse color */
   1.141 -    //IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLOROP,
   1.142 -    //                                      D3DTOP_MODULATE);
   1.143 -    //IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG1,
   1.144 -    //                                      D3DTA_TEXTURE);
   1.145 -    //IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG2,
   1.146 -    //                                      D3DTA_DIFFUSE);
   1.147 -    ///* Enable alpha modulation by diffuse alpha */
   1.148 -    //IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAOP,
   1.149 -    //                                      D3DTOP_MODULATE);
   1.150 -    //IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG1,
   1.151 -    //                                      D3DTA_TEXTURE);
   1.152 -    //IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG2,
   1.153 -    //                                      D3DTA_DIFFUSE);
   1.154 -    ///* Disable second texture stage, since we're done */
   1.155 -    //IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_COLOROP,
   1.156 -    //                                      D3DTOP_DISABLE);
   1.157 -    //IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_ALPHAOP,
   1.158 -    //                                      D3DTOP_DISABLE);
   1.159 -
   1.160 -    ///* Store the default render target */
   1.161 -    //IDirect3DDevice9_GetRenderTarget(data->device, 0, &data->defaultRenderTarget );
   1.162 -    //data->currentRenderTarget = NULL;
   1.163 -
   1.164 -    ///* Set an identity world and view matrix */
   1.165 -    //matrix.m[0][0] = 1.0f;
   1.166 -    //matrix.m[0][1] = 0.0f;
   1.167 -    //matrix.m[0][2] = 0.0f;
   1.168 -    //matrix.m[0][3] = 0.0f;
   1.169 -    //matrix.m[1][0] = 0.0f;
   1.170 -    //matrix.m[1][1] = 1.0f;
   1.171 -    //matrix.m[1][2] = 0.0f;
   1.172 -    //matrix.m[1][3] = 0.0f;
   1.173 -    //matrix.m[2][0] = 0.0f;
   1.174 -    //matrix.m[2][1] = 0.0f;
   1.175 -    //matrix.m[2][2] = 1.0f;
   1.176 -    //matrix.m[2][3] = 0.0f;
   1.177 -    //matrix.m[3][0] = 0.0f;
   1.178 -    //matrix.m[3][1] = 0.0f;
   1.179 -    //matrix.m[3][2] = 0.0f;
   1.180 -    //matrix.m[3][3] = 1.0f;
   1.181 -    //IDirect3DDevice9_SetTransform(data->device, D3DTS_WORLD, &matrix);
   1.182 -    //IDirect3DDevice9_SetTransform(data->device, D3DTS_VIEW, &matrix);
   1.183 +    /* Initialize Direct3D resources */
   1.184 +    if (FAILED(D3D11_CreateDeviceResources(renderer))) {
   1.185 +        D3D11_DestroyRenderer(renderer);
   1.186 +        return NULL;
   1.187 +    }
   1.188 +    if (FAILED(D3D11_CreateWindowSizeDependentResources(renderer))) {
   1.189 +        D3D11_DestroyRenderer(renderer);
   1.190 +        return NULL;
   1.191 +    }
   1.192  
   1.193      return renderer;
   1.194  }
   1.195  
   1.196 +static void
   1.197 +D3D11_DestroyRenderer(SDL_Renderer * renderer)
   1.198 +{
   1.199 +    D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
   1.200 +    if (data) {
   1.201 +        delete data;
   1.202 +        data = NULL;
   1.203 +    }
   1.204 +}
   1.205 +
   1.206  static bool
   1.207  D3D11_ReadFileContents(const wstring & fileName, vector<char> & out)
   1.208  {
     2.1 --- a/src/video/windowsrt/SDL_winrtrenderer.cpp	Sat Feb 09 14:56:32 2013 -0500
     2.2 +++ b/src/video/windowsrt/SDL_winrtrenderer.cpp	Sat Feb 09 15:22:49 2013 -0500
     2.3 @@ -36,13 +36,6 @@
     2.4      }
     2.5  }
     2.6  
     2.7 -// Initialize the Direct3D resources required to run.
     2.8 -void SDL_winrtrenderer::Initialize()
     2.9 -{ 
    2.10 -    CreateDeviceResources();
    2.11 -    CreateWindowSizeDependentResources();
    2.12 -}
    2.13 -
    2.14  // Recreate all device resources and set them back to the current state.
    2.15  void SDL_winrtrenderer::HandleDeviceLost()
    2.16  {
     3.1 --- a/src/video/windowsrt/SDL_winrtrenderer.h	Sat Feb 09 14:56:32 2013 -0500
     3.2 +++ b/src/video/windowsrt/SDL_winrtrenderer.h	Sat Feb 09 15:22:49 2013 -0500
     3.3 @@ -16,7 +16,6 @@
     3.4  
     3.5  public:
     3.6      virtual ~SDL_winrtrenderer();
     3.7 -    virtual void Initialize();
     3.8      virtual void HandleDeviceLost();
     3.9      virtual void CreateDeviceResources();
    3.10      virtual void CreateWindowSizeDependentResources();
     4.1 --- a/src/video/windowsrt/SDL_winrtvideo.cpp	Sat Feb 09 14:56:32 2013 -0500
     4.2 +++ b/src/video/windowsrt/SDL_winrtvideo.cpp	Sat Feb 09 15:22:49 2013 -0500
     4.3 @@ -232,7 +232,7 @@
     4.4      SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE);
     4.5      SDL_WinRTGlobalApp->m_renderer->m_sdlRenderer = renderer;
     4.6      SDL_WinRTGlobalApp->m_renderer->m_sdlRendererData = (D3D11_RenderData *) renderer->driverdata;
     4.7 -    SDL_WinRTGlobalApp->m_renderer->Initialize();
     4.8 +    //SDL_WinRTGlobalApp->m_renderer->Initialize();
     4.9  
    4.10      /* All done! */
    4.11      return 0;