First stab at a D3D renderer, only 30FPS so far... ?
authorSam Lantinga <slouken@libsdl.org>
Wed, 12 Jul 2006 09:25:17 +0000
changeset 19005c6bdbf3aadf
parent 1899 6a11e61bf805
child 1901 f1828a500391
First stab at a D3D renderer, only 30FPS so far... ?
src/video/win32/SDL_d3drender.c
src/video/win32/SDL_gdirender.c
     1.1 --- a/src/video/win32/SDL_d3drender.c	Wed Jul 12 08:09:57 2006 +0000
     1.2 +++ b/src/video/win32/SDL_d3drender.c	Wed Jul 12 09:25:17 2006 +0000
     1.3 @@ -78,10 +78,10 @@
     1.4      SDL_D3D_CreateRenderer,
     1.5      {
     1.6       "d3d",
     1.7 -     (SDL_Renderer_Minimal |
     1.8 -      SDL_Renderer_SingleBuffer | SDL_Renderer_PresentCopy |
     1.9 -      SDL_Renderer_PresentFlip2 | SDL_Renderer_PresentFlip3 |
    1.10 -      SDL_Renderer_PresentDiscard | SDL_Renderer_RenderTarget),
    1.11 +     (                          //SDL_Renderer_Minimal |
    1.12 +         SDL_Renderer_SingleBuffer | SDL_Renderer_PresentCopy |
    1.13 +         SDL_Renderer_PresentFlip2 | SDL_Renderer_PresentFlip3 |
    1.14 +         SDL_Renderer_PresentDiscard | SDL_Renderer_RenderTarget),
    1.15       (SDL_TextureBlendMode_None |
    1.16        SDL_TextureBlendMode_Mask | SDL_TextureBlendMode_Blend),
    1.17       (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast),
    1.18 @@ -105,6 +105,7 @@
    1.19  typedef struct
    1.20  {
    1.21      IDirect3DDevice9 *device;
    1.22 +    SDL_bool beginScene;
    1.23  } SDL_D3D_RenderData;
    1.24  
    1.25  typedef struct
    1.26 @@ -113,6 +114,85 @@
    1.27  } SDL_D3D_TextureData;
    1.28  
    1.29  static void
    1.30 +D3D_SetError(const char *prefix, HRESULT result)
    1.31 +{
    1.32 +    const char *error;
    1.33 +
    1.34 +    switch (result) {
    1.35 +    case D3DERR_WRONGTEXTUREFORMAT:
    1.36 +        error = "WRONGTEXTUREFORMAT";
    1.37 +        break;
    1.38 +    case D3DERR_UNSUPPORTEDCOLOROPERATION:
    1.39 +        error = "UNSUPPORTEDCOLOROPERATION";
    1.40 +        break;
    1.41 +    case D3DERR_UNSUPPORTEDCOLORARG:
    1.42 +        error = "UNSUPPORTEDCOLORARG";
    1.43 +        break;
    1.44 +    case D3DERR_UNSUPPORTEDALPHAOPERATION:
    1.45 +        error = "UNSUPPORTEDALPHAOPERATION";
    1.46 +        break;
    1.47 +    case D3DERR_UNSUPPORTEDALPHAARG:
    1.48 +        error = "UNSUPPORTEDALPHAARG";
    1.49 +        break;
    1.50 +    case D3DERR_TOOMANYOPERATIONS:
    1.51 +        error = "TOOMANYOPERATIONS";
    1.52 +        break;
    1.53 +    case D3DERR_CONFLICTINGTEXTUREFILTER:
    1.54 +        error = "CONFLICTINGTEXTUREFILTER";
    1.55 +        break;
    1.56 +    case D3DERR_UNSUPPORTEDFACTORVALUE:
    1.57 +        error = "UNSUPPORTEDFACTORVALUE";
    1.58 +        break;
    1.59 +    case D3DERR_CONFLICTINGRENDERSTATE:
    1.60 +        error = "CONFLICTINGRENDERSTATE";
    1.61 +        break;
    1.62 +    case D3DERR_UNSUPPORTEDTEXTUREFILTER:
    1.63 +        error = "UNSUPPORTEDTEXTUREFILTER";
    1.64 +        break;
    1.65 +    case D3DERR_CONFLICTINGTEXTUREPALETTE:
    1.66 +        error = "CONFLICTINGTEXTUREPALETTE";
    1.67 +        break;
    1.68 +    case D3DERR_DRIVERINTERNALERROR:
    1.69 +        error = "DRIVERINTERNALERROR";
    1.70 +        break;
    1.71 +    case D3DERR_NOTFOUND:
    1.72 +        error = "NOTFOUND";
    1.73 +        break;
    1.74 +    case D3DERR_MOREDATA:
    1.75 +        error = "MOREDATA";
    1.76 +        break;
    1.77 +    case D3DERR_DEVICELOST:
    1.78 +        error = "DEVICELOST";
    1.79 +        break;
    1.80 +    case D3DERR_DEVICENOTRESET:
    1.81 +        error = "DEVICENOTRESET";
    1.82 +        break;
    1.83 +    case D3DERR_NOTAVAILABLE:
    1.84 +        error = "NOTAVAILABLE";
    1.85 +        break;
    1.86 +    case D3DERR_OUTOFVIDEOMEMORY:
    1.87 +        error = "OUTOFVIDEOMEMORY";
    1.88 +        break;
    1.89 +    case D3DERR_INVALIDDEVICE:
    1.90 +        error = "INVALIDDEVICE";
    1.91 +        break;
    1.92 +    case D3DERR_INVALIDCALL:
    1.93 +        error = "INVALIDCALL";
    1.94 +        break;
    1.95 +    case D3DERR_DRIVERINVALIDCALL:
    1.96 +        error = "DRIVERINVALIDCALL";
    1.97 +        break;
    1.98 +    case D3DERR_WASSTILLDRAWING:
    1.99 +        error = "WASSTILLDRAWING";
   1.100 +        break;
   1.101 +    default:
   1.102 +        error = "UNKNOWN";
   1.103 +        break;
   1.104 +    }
   1.105 +    SDL_SetError("%s: %s", prefix, error);
   1.106 +}
   1.107 +
   1.108 +static void
   1.109  UpdateYUVTextureData(SDL_Texture * texture)
   1.110  {
   1.111      SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   1.112 @@ -144,6 +224,8 @@
   1.113      SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
   1.114      SDL_Renderer *renderer;
   1.115      SDL_D3D_RenderData *data;
   1.116 +    HRESULT result;
   1.117 +    D3DPRESENT_PARAMETERS pparams;
   1.118  
   1.119      renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer));
   1.120      if (!renderer) {
   1.121 @@ -160,8 +242,6 @@
   1.122      }
   1.123      SDL_zerop(data);
   1.124  
   1.125 -    //data->device = IDirect3D9_CreateDevice(videodata->d3d,
   1.126 -
   1.127      renderer->CreateTexture = SDL_D3D_CreateTexture;
   1.128      renderer->QueryTexturePixels = SDL_D3D_QueryTexturePixels;
   1.129      renderer->SetTexturePalette = SDL_D3D_SetTexturePalette;
   1.130 @@ -184,6 +264,42 @@
   1.131  
   1.132      renderer->info.flags = SDL_Renderer_RenderTarget;
   1.133  
   1.134 +    SDL_zero(pparams);
   1.135 +    pparams.BackBufferWidth = window->w;
   1.136 +    pparams.BackBufferHeight = window->h;
   1.137 +    pparams.BackBufferFormat = D3DFMT_UNKNOWN;  /* FIXME */
   1.138 +    if (flags & SDL_Renderer_PresentFlip2) {
   1.139 +        pparams.BackBufferCount = 2;
   1.140 +        pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
   1.141 +    } else if (flags & SDL_Renderer_PresentFlip3) {
   1.142 +        pparams.BackBufferCount = 3;
   1.143 +        pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
   1.144 +    } else if (flags & SDL_Renderer_PresentCopy) {
   1.145 +        pparams.BackBufferCount = 1;
   1.146 +        pparams.SwapEffect = D3DSWAPEFFECT_COPY;
   1.147 +    } else {
   1.148 +        pparams.BackBufferCount = 1;
   1.149 +        pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
   1.150 +    }
   1.151 +    if (window->flags & SDL_WINDOW_FULLSCREEN) {
   1.152 +        pparams.Windowed = FALSE;
   1.153 +    } else {
   1.154 +        pparams.Windowed = TRUE;
   1.155 +    }
   1.156 +    pparams.FullScreen_RefreshRateInHz = 0;     /* FIXME */
   1.157 +
   1.158 +    result = IDirect3D9_CreateDevice(videodata->d3d, D3DADAPTER_DEFAULT,        /* FIXME */
   1.159 +                                     D3DDEVTYPE_HAL,
   1.160 +                                     windowdata->hwnd,
   1.161 +                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING,
   1.162 +                                     &pparams, &data->device);
   1.163 +    if (FAILED(result)) {
   1.164 +        SDL_D3D_DestroyRenderer(renderer);
   1.165 +        D3D_SetError("CreateDevice()", result);
   1.166 +        return NULL;
   1.167 +    }
   1.168 +    data->beginScene = SDL_TRUE;
   1.169 +
   1.170      return renderer;
   1.171  }
   1.172  
   1.173 @@ -283,7 +399,7 @@
   1.174          return SDL_SW_LockYUVTexture(data->yuv, rect, markDirty, pixels,
   1.175                                       pitch);
   1.176      } else {
   1.177 -        return 0;
   1.178 +        return -1;
   1.179      }
   1.180  }
   1.181  
   1.182 @@ -315,12 +431,20 @@
   1.183                     Uint32 color)
   1.184  {
   1.185      SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   1.186 -    Uint8 r, g, b;
   1.187 +    HRESULT result;
   1.188  
   1.189 -    r = (Uint8) ((color >> 16) & 0xFF);
   1.190 -    g = (Uint8) ((color >> 8) & 0xFF);
   1.191 -    b = (Uint8) (color & 0xFF);
   1.192 +    if (data->beginScene) {
   1.193 +        IDirect3DDevice9_BeginScene(data->device);
   1.194 +        data->beginScene = SDL_FALSE;
   1.195 +    }
   1.196  
   1.197 +    result =
   1.198 +        IDirect3DDevice9_Clear(data->device, 0, NULL, D3DCLEAR_TARGET,
   1.199 +                               (D3DCOLOR) color, 1.0f, 0);
   1.200 +    if (FAILED(result)) {
   1.201 +        D3D_SetError("Clear()", result);
   1.202 +        return -1;
   1.203 +    }
   1.204      return 0;
   1.205  }
   1.206  
   1.207 @@ -333,6 +457,10 @@
   1.208      SDL_D3D_TextureData *texturedata =
   1.209          (SDL_D3D_TextureData *) texture->driverdata;
   1.210  
   1.211 +    if (data->beginScene) {
   1.212 +        IDirect3DDevice9_BeginScene(data->device);
   1.213 +        data->beginScene = SDL_FALSE;
   1.214 +    }
   1.215      return 0;
   1.216  }
   1.217  
   1.218 @@ -357,6 +485,18 @@
   1.219  static void
   1.220  SDL_D3D_RenderPresent(SDL_Renderer * renderer)
   1.221  {
   1.222 +    SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   1.223 +    HRESULT result;
   1.224 +
   1.225 +    if (!data->beginScene) {
   1.226 +        IDirect3DDevice9_EndScene(data->device);
   1.227 +        data->beginScene = SDL_TRUE;
   1.228 +    }
   1.229 +
   1.230 +    result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
   1.231 +    if (FAILED(result)) {
   1.232 +        D3D_SetError("Present()", result);
   1.233 +    }
   1.234  }
   1.235  
   1.236  static void
   1.237 @@ -377,6 +517,9 @@
   1.238      SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   1.239  
   1.240      if (data) {
   1.241 +        if (data->device) {
   1.242 +            IDirect3DDevice9_Release(data->device);
   1.243 +        }
   1.244          SDL_free(data);
   1.245      }
   1.246      SDL_free(renderer);
     2.1 --- a/src/video/win32/SDL_gdirender.c	Wed Jul 12 08:09:57 2006 +0000
     2.2 +++ b/src/video/win32/SDL_gdirender.c	Wed Jul 12 09:25:17 2006 +0000
     2.3 @@ -79,10 +79,10 @@
     2.4      SDL_GDI_CreateRenderer,
     2.5      {
     2.6       "gdi",
     2.7 -     (//SDL_Renderer_Minimal |
     2.8 -      SDL_Renderer_SingleBuffer | SDL_Renderer_PresentCopy |
     2.9 -      SDL_Renderer_PresentFlip2 | SDL_Renderer_PresentFlip3 |
    2.10 -      SDL_Renderer_PresentDiscard | SDL_Renderer_RenderTarget),
    2.11 +     (                          //SDL_Renderer_Minimal |
    2.12 +         SDL_Renderer_SingleBuffer | SDL_Renderer_PresentCopy |
    2.13 +         SDL_Renderer_PresentFlip2 | SDL_Renderer_PresentFlip3 |
    2.14 +         SDL_Renderer_PresentDiscard | SDL_Renderer_RenderTarget),
    2.15       (SDL_TextureBlendMode_None | SDL_TextureBlendMode_Mask |
    2.16        SDL_TextureBlendMode_Blend),
    2.17       (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast),