src/video/win32/SDL_d3drender.c
changeset 1903 f132024010be
parent 1901 f1828a500391
child 1904 1a713f9d1f71
equal deleted inserted replaced
1902:eb2891493b66 1903:f132024010be
    22 #include "SDL_config.h"
    22 #include "SDL_config.h"
    23 
    23 
    24 #if SDL_VIDEO_RENDER_D3D
    24 #if SDL_VIDEO_RENDER_D3D
    25 
    25 
    26 #include "SDL_win32video.h"
    26 #include "SDL_win32video.h"
    27 #include "../SDL_yuv_sw_c.h"
       
    28 
    27 
    29 /* Direct3D renderer implementation */
    28 /* Direct3D renderer implementation */
    30 
    29 
    31 static SDL_Renderer *SDL_D3D_CreateRenderer(SDL_Window * window,
    30 static SDL_Renderer *SDL_D3D_CreateRenderer(SDL_Window * window,
    32                                             Uint32 flags);
    31                                             Uint32 flags);
    33 static int SDL_D3D_CreateTexture(SDL_Renderer * renderer,
    32 static int SDL_D3D_CreateTexture(SDL_Renderer * renderer,
    34                                  SDL_Texture * texture);
    33                                  SDL_Texture * texture);
    35 static int SDL_D3D_QueryTexturePixels(SDL_Renderer * renderer,
       
    36                                       SDL_Texture * texture, void **pixels,
       
    37                                       int *pitch);
       
    38 static int SDL_D3D_SetTexturePalette(SDL_Renderer * renderer,
    34 static int SDL_D3D_SetTexturePalette(SDL_Renderer * renderer,
    39                                      SDL_Texture * texture,
    35                                      SDL_Texture * texture,
    40                                      const SDL_Color * colors, int firstcolor,
    36                                      const SDL_Color * colors, int firstcolor,
    41                                      int ncolors);
    37                                      int ncolors);
    42 static int SDL_D3D_GetTexturePalette(SDL_Renderer * renderer,
    38 static int SDL_D3D_GetTexturePalette(SDL_Renderer * renderer,
    83          SDL_Renderer_PresentFlip2 | SDL_Renderer_PresentFlip3 |
    79          SDL_Renderer_PresentFlip2 | SDL_Renderer_PresentFlip3 |
    84          SDL_Renderer_PresentDiscard | SDL_Renderer_RenderTarget),
    80          SDL_Renderer_PresentDiscard | SDL_Renderer_RenderTarget),
    85      (SDL_TextureBlendMode_None |
    81      (SDL_TextureBlendMode_None |
    86       SDL_TextureBlendMode_Mask | SDL_TextureBlendMode_Blend),
    82       SDL_TextureBlendMode_Mask | SDL_TextureBlendMode_Blend),
    87      (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast),
    83      (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast),
    88      11,
    84      12,
    89      {
    85      {
    90       SDL_PixelFormat_Index8,
    86       SDL_PixelFormat_Index8,
       
    87       SDL_PixelFormat_RGB332,
       
    88       SDL_PixelFormat_RGB444,
    91       SDL_PixelFormat_RGB555,
    89       SDL_PixelFormat_RGB555,
       
    90       SDL_PixelFormat_ARGB4444,
       
    91       SDL_PixelFormat_ARGB1555,
    92       SDL_PixelFormat_RGB565,
    92       SDL_PixelFormat_RGB565,
    93       SDL_PixelFormat_RGB888,
    93       SDL_PixelFormat_RGB888,
    94       SDL_PixelFormat_BGR888,
       
    95       SDL_PixelFormat_ARGB8888,
    94       SDL_PixelFormat_ARGB8888,
    96       SDL_PixelFormat_RGBA8888,
    95       SDL_PixelFormat_ARGB2101010,
    97       SDL_PixelFormat_ABGR8888,
    96       SDL_PixelFormat_UYVY,
    98       SDL_PixelFormat_BGRA8888,
    97       SDL_PixelFormat_YUY2},
    99       SDL_PixelFormat_YUY2,
       
   100       SDL_PixelFormat_UYVY},
       
   101      0,
    98      0,
   102      0}
    99      0}
   103 };
   100 };
   104 
   101 
   105 typedef struct
   102 typedef struct
   108     SDL_bool beginScene;
   105     SDL_bool beginScene;
   109 } SDL_D3D_RenderData;
   106 } SDL_D3D_RenderData;
   110 
   107 
   111 typedef struct
   108 typedef struct
   112 {
   109 {
   113     SDL_SW_YUVTexture *yuv;
   110     IDirect3DTexture9 *texture;
   114 } SDL_D3D_TextureData;
   111 } SDL_D3D_TextureData;
       
   112 
       
   113 typedef struct
       
   114 {
       
   115     float x, y, z;
       
   116     float tu, tv;
       
   117 } Vertex;
   115 
   118 
   116 static void
   119 static void
   117 D3D_SetError(const char *prefix, HRESULT result)
   120 D3D_SetError(const char *prefix, HRESULT result)
   118 {
   121 {
   119     const char *error;
   122     const char *error;
   190         break;
   193         break;
   191     }
   194     }
   192     SDL_SetError("%s: %s", prefix, error);
   195     SDL_SetError("%s: %s", prefix, error);
   193 }
   196 }
   194 
   197 
   195 static void
   198 static D3DFORMAT
   196 UpdateYUVTextureData(SDL_Texture * texture)
   199 PixelFormatToD3DFMT(Uint32 format)
   197 {
   200 {
   198     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   201     switch (format) {
   199     SDL_Rect rect;
   202     case SDL_PixelFormat_Index8:
   200 
   203         return D3DFMT_P8;
   201     rect.x = 0;
   204     case SDL_PixelFormat_RGB332:
   202     rect.y = 0;
   205         return D3DFMT_R3G3B2;
   203     rect.w = texture->w;
   206     case SDL_PixelFormat_RGB444:
   204     rect.h = texture->h;
   207         return D3DFMT_X4R4G4B4;
   205     //SDL_SW_CopyYUVToRGB(data->yuv, &rect, data->format, texture->w,
   208     case SDL_PixelFormat_RGB555:
   206     //                    texture->h, data->pixels, data->pitch);
   209         return D3DFMT_X1R5G5B5;
       
   210     case SDL_PixelFormat_ARGB4444:
       
   211         return D3DFMT_A4R4G4B4;
       
   212     case SDL_PixelFormat_ARGB1555:
       
   213         return D3DFMT_A1R5G5B5;
       
   214     case SDL_PixelFormat_RGB565:
       
   215         return D3DFMT_R5G6B5;
       
   216     case SDL_PixelFormat_RGB888:
       
   217         return D3DFMT_X8R8G8B8;
       
   218     case SDL_PixelFormat_ARGB8888:
       
   219         return D3DFMT_A8R8G8B8;
       
   220     case SDL_PixelFormat_ARGB2101010:
       
   221         return D3DFMT_A2R10G10B10;
       
   222     case SDL_PixelFormat_UYVY:
       
   223         return D3DFMT_UYVY;
       
   224     case SDL_PixelFormat_YUY2:
       
   225         return D3DFMT_YUY2;
       
   226     default:
       
   227         return D3DFMT_UNKNOWN;
       
   228     }
   207 }
   229 }
   208 
   230 
   209 void
   231 void
   210 D3D_AddRenderDriver(_THIS)
   232 D3D_AddRenderDriver(_THIS)
   211 {
   233 {
   241         return NULL;
   263         return NULL;
   242     }
   264     }
   243     SDL_zerop(data);
   265     SDL_zerop(data);
   244 
   266 
   245     renderer->CreateTexture = SDL_D3D_CreateTexture;
   267     renderer->CreateTexture = SDL_D3D_CreateTexture;
   246     renderer->QueryTexturePixels = SDL_D3D_QueryTexturePixels;
       
   247     renderer->SetTexturePalette = SDL_D3D_SetTexturePalette;
   268     renderer->SetTexturePalette = SDL_D3D_SetTexturePalette;
   248     renderer->GetTexturePalette = SDL_D3D_GetTexturePalette;
   269     renderer->GetTexturePalette = SDL_D3D_GetTexturePalette;
   249     renderer->UpdateTexture = SDL_D3D_UpdateTexture;
   270     renderer->UpdateTexture = SDL_D3D_UpdateTexture;
   250     renderer->LockTexture = SDL_D3D_LockTexture;
   271     renderer->LockTexture = SDL_D3D_LockTexture;
   251     renderer->UnlockTexture = SDL_D3D_UnlockTexture;
   272     renderer->UnlockTexture = SDL_D3D_UnlockTexture;
   265     renderer->info.flags = SDL_Renderer_RenderTarget;
   286     renderer->info.flags = SDL_Renderer_RenderTarget;
   266 
   287 
   267     SDL_zero(pparams);
   288     SDL_zero(pparams);
   268     pparams.BackBufferWidth = window->w;
   289     pparams.BackBufferWidth = window->w;
   269     pparams.BackBufferHeight = window->h;
   290     pparams.BackBufferHeight = window->h;
   270     pparams.BackBufferFormat = D3DFMT_UNKNOWN;  /* FIXME */
   291     if (window->flags & SDL_WINDOW_FULLSCREEN) {
       
   292         pparams.BackBufferFormat =
       
   293             PixelFormatToD3DFMT(display->fullscreen_mode->format);
       
   294     } else {
       
   295         pparams.BackBufferFormat = D3DFMT_UNKNOWN;
       
   296     }
   271     if (flags & SDL_Renderer_PresentFlip2) {
   297     if (flags & SDL_Renderer_PresentFlip2) {
   272         pparams.BackBufferCount = 2;
   298         pparams.BackBufferCount = 2;
   273         pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
   299         pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
   274     } else if (flags & SDL_Renderer_PresentFlip3) {
   300     } else if (flags & SDL_Renderer_PresentFlip3) {
   275         pparams.BackBufferCount = 3;
   301         pparams.BackBufferCount = 3;
   281         pparams.BackBufferCount = 1;
   307         pparams.BackBufferCount = 1;
   282         pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
   308         pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
   283     }
   309     }
   284     if (window->flags & SDL_WINDOW_FULLSCREEN) {
   310     if (window->flags & SDL_WINDOW_FULLSCREEN) {
   285         pparams.Windowed = FALSE;
   311         pparams.Windowed = FALSE;
       
   312         pparams.FullScreen_RefreshRateInHz =
       
   313             display->fullscreen_mode->refresh_rate;
   286     } else {
   314     } else {
   287         pparams.Windowed = TRUE;
   315         pparams.Windowed = TRUE;
   288     }
   316         pparams.FullScreen_RefreshRateInHz = 0;
   289     pparams.FullScreen_RefreshRateInHz = 0;     /* FIXME */
   317     }
   290     pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
   318     pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
   291 
   319 
   292     result = IDirect3D9_CreateDevice(videodata->d3d, D3DADAPTER_DEFAULT,        /* FIXME */
   320     result = IDirect3D9_CreateDevice(videodata->d3d, D3DADAPTER_DEFAULT,        /* FIXME */
   293                                      D3DDEVTYPE_HAL,
   321                                      D3DDEVTYPE_HAL,
   294                                      windowdata->hwnd,
   322                                      windowdata->hwnd,
   299         D3D_SetError("CreateDevice()", result);
   327         D3D_SetError("CreateDevice()", result);
   300         return NULL;
   328         return NULL;
   301     }
   329     }
   302     data->beginScene = SDL_TRUE;
   330     data->beginScene = SDL_TRUE;
   303 
   331 
       
   332     /* Set up parameters for rendering */
       
   333     IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
       
   334                                     D3DCULL_NONE);
       
   335     IDirect3DDevice9_SetFVF(data->device, D3DFVF_XYZ | D3DFVF_TEX1);
       
   336 
   304     return renderer;
   337     return renderer;
   305 }
   338 }
   306 
   339 
   307 static int
   340 static int
   308 SDL_D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   341 SDL_D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   310     SDL_D3D_RenderData *renderdata =
   343     SDL_D3D_RenderData *renderdata =
   311         (SDL_D3D_RenderData *) renderer->driverdata;
   344         (SDL_D3D_RenderData *) renderer->driverdata;
   312     SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   345     SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   313     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   346     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   314     SDL_D3D_TextureData *data;
   347     SDL_D3D_TextureData *data;
       
   348     D3DPOOL pool;
       
   349     HRESULT result;
   315 
   350 
   316     data = (SDL_D3D_TextureData *) SDL_malloc(sizeof(*data));
   351     data = (SDL_D3D_TextureData *) SDL_malloc(sizeof(*data));
   317     if (!data) {
   352     if (!data) {
   318         SDL_OutOfMemory();
   353         SDL_OutOfMemory();
   319         return -1;
   354         return -1;
   320     }
   355     }
   321     SDL_zerop(data);
   356     SDL_zerop(data);
   322 
   357 
   323     texture->driverdata = data;
   358     texture->driverdata = data;
   324 
   359 
   325     return 0;
   360     if (texture->access == SDL_TextureAccess_Local) {
   326 }
   361         pool = D3DPOOL_MANAGED;
   327 
       
   328 static int
       
   329 SDL_D3D_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
       
   330                            void **pixels, int *pitch)
       
   331 {
       
   332     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
       
   333 
       
   334     if (data->yuv) {
       
   335         return SDL_SW_QueryYUVTexturePixels(data->yuv, pixels, pitch);
       
   336     } else {
   362     } else {
   337         return 0;
   363         pool = D3DPOOL_DEFAULT;
   338     }
   364     }
       
   365     result =
       
   366         IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
       
   367                                        texture->h, 1, 0,
       
   368                                        PixelFormatToD3DFMT(texture->format),
       
   369                                        pool, &data->texture, NULL);
       
   370     if (FAILED(result)) {
       
   371         SDL_free(data);
       
   372         D3D_SetError("CreateTexture()", result);
       
   373         return -1;
       
   374     }
       
   375 
       
   376     return 0;
   339 }
   377 }
   340 
   378 
   341 static int
   379 static int
   342 SDL_D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   380 SDL_D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   343                           const SDL_Color * colors, int firstcolor,
   381                           const SDL_Color * colors, int firstcolor,
   345 {
   383 {
   346     SDL_D3D_RenderData *renderdata =
   384     SDL_D3D_RenderData *renderdata =
   347         (SDL_D3D_RenderData *) renderer->driverdata;
   385         (SDL_D3D_RenderData *) renderer->driverdata;
   348     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   386     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   349 
   387 
   350     if (data->yuv) {
   388     return 0;
   351         SDL_SetError("YUV textures don't have a palette");
       
   352         return -1;
       
   353     } else {
       
   354         return 0;
       
   355     }
       
   356 }
   389 }
   357 
   390 
   358 static int
   391 static int
   359 SDL_D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   392 SDL_D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   360                           SDL_Color * colors, int firstcolor, int ncolors)
   393                           SDL_Color * colors, int firstcolor, int ncolors)
   361 {
   394 {
   362     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   395     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   363 
   396 
   364     if (data->yuv) {
   397     return 0;
   365         SDL_SetError("YUV textures don't have a palette");
       
   366         return -1;
       
   367     } else {
       
   368         return 0;
       
   369     }
       
   370 }
   398 }
   371 
   399 
   372 static int
   400 static int
   373 SDL_D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   401 SDL_D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   374                       const SDL_Rect * rect, const void *pixels, int pitch)
   402                       const SDL_Rect * rect, const void *pixels, int pitch)
   375 {
   403 {
   376     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   404     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   377 
   405     SDL_D3D_RenderData *renderdata =
   378     if (data->yuv) {
   406         (SDL_D3D_RenderData *) renderer->driverdata;
   379         if (SDL_SW_UpdateYUVTexture(data->yuv, rect, pixels, pitch) < 0) {
   407     IDirect3DTexture9 *temp;
   380             return -1;
   408     RECT d3drect;
   381         }
   409     D3DLOCKED_RECT locked;
   382         UpdateYUVTextureData(texture);
   410     const Uint8 *src;
   383         return 0;
   411     Uint8 *dst;
   384     } else {
   412     int row, length;
   385         SDL_D3D_RenderData *renderdata =
   413     HRESULT result;
   386             (SDL_D3D_RenderData *) renderer->driverdata;
   414 
   387 
   415     result =
   388         return 0;
   416         IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
   389     }
   417                                        texture->h, 1, 0,
       
   418                                        PixelFormatToD3DFMT(texture->format),
       
   419                                        D3DPOOL_SYSTEMMEM, &temp, NULL);
       
   420     if (FAILED(result)) {
       
   421         D3D_SetError("CreateTexture()", result);
       
   422         return -1;
       
   423     }
       
   424 
       
   425     d3drect.left = rect->x;
       
   426     d3drect.right = rect->x + rect->w;
       
   427     d3drect.top = rect->y;
       
   428     d3drect.bottom = rect->y + rect->h;
       
   429 
       
   430     result = IDirect3DTexture9_LockRect(temp, 0, &locked, &d3drect, 0);
       
   431     if (FAILED(result)) {
       
   432         IDirect3DTexture9_Release(temp);
       
   433         D3D_SetError("LockRect()", result);
       
   434         return -1;
       
   435     }
       
   436 
       
   437     src = pixels;
       
   438     dst = locked.pBits;
       
   439     length = rect->w * SDL_BYTESPERPIXEL(texture->format);
       
   440     for (row = 0; row < rect->h; ++row) {
       
   441         SDL_memcpy(dst, src, length);
       
   442         src += pitch;
       
   443         dst += locked.Pitch;
       
   444     }
       
   445     IDirect3DTexture9_UnlockRect(temp, 0);
       
   446 
       
   447     result =
       
   448         IDirect3DDevice9_UpdateTexture(renderdata->device,
       
   449                                        (IDirect3DBaseTexture9 *) temp,
       
   450                                        (IDirect3DBaseTexture9 *) data->
       
   451                                        texture);
       
   452     IDirect3DTexture9_Release(temp);
       
   453     if (FAILED(result)) {
       
   454         D3D_SetError("UpdateTexture()", result);
       
   455         return -1;
       
   456     }
       
   457     return 0;
   390 }
   458 }
   391 
   459 
   392 static int
   460 static int
   393 SDL_D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   461 SDL_D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   394                     const SDL_Rect * rect, int markDirty, void **pixels,
   462                     const SDL_Rect * rect, int markDirty, void **pixels,
   395                     int *pitch)
   463                     int *pitch)
   396 {
   464 {
   397     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   465     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   398 
   466     RECT d3drect;
   399     if (data->yuv) {
   467     D3DLOCKED_RECT locked;
   400         return SDL_SW_LockYUVTexture(data->yuv, rect, markDirty, pixels,
   468     HRESULT result;
   401                                      pitch);
   469 
   402     } else {
   470     if (texture->access != SDL_TextureAccess_Local) {
   403         return -1;
   471         SDL_SetError("Can't lock remote video memory");
   404     }
   472         return -1;
       
   473     }
       
   474 
       
   475     d3drect.left = rect->x;
       
   476     d3drect.right = rect->x + rect->w;
       
   477     d3drect.top = rect->y;
       
   478     d3drect.bottom = rect->y + rect->h;
       
   479 
       
   480     result =
       
   481         IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect,
       
   482                                    markDirty ? 0 : D3DLOCK_NO_DIRTY_UPDATE);
       
   483     if (FAILED(result)) {
       
   484         D3D_SetError("LockRect()", result);
       
   485         return -1;
       
   486     }
       
   487     *pixels = locked.pBits;
       
   488     *pitch = locked.Pitch;
       
   489     return 0;
   405 }
   490 }
   406 
   491 
   407 static void
   492 static void
   408 SDL_D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   493 SDL_D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   409 {
   494 {
   410     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   495     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   411 
   496 
   412     if (data->yuv) {
   497     IDirect3DTexture9_UnlockRect(data->texture, 0);
   413         SDL_SW_UnlockYUVTexture(data->yuv);
       
   414         UpdateYUVTextureData(texture);
       
   415     }
       
   416 }
   498 }
   417 
   499 
   418 static void
   500 static void
   419 SDL_D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   501 SDL_D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   420                      int numrects, const SDL_Rect * rects)
   502                      int numrects, const SDL_Rect * rects)
   421 {
   503 {
       
   504     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
       
   505     RECT d3drect;
       
   506     int i;
       
   507 
       
   508     for (i = 0; i < numrects; ++i) {
       
   509         const SDL_Rect *rect = &rects[i];
       
   510 
       
   511         d3drect.left = rect->x;
       
   512         d3drect.right = rect->x + rect->w;
       
   513         d3drect.top = rect->y;
       
   514         d3drect.bottom = rect->y + rect->h;
       
   515 
       
   516         IDirect3DTexture9_AddDirtyRect(data->texture, &d3drect);
       
   517     }
   422 }
   518 }
   423 
   519 
   424 static void
   520 static void
   425 SDL_D3D_SelectRenderTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   521 SDL_D3D_SelectRenderTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   426 {
   522 {
   439         IDirect3DDevice9_BeginScene(data->device);
   535         IDirect3DDevice9_BeginScene(data->device);
   440         data->beginScene = SDL_FALSE;
   536         data->beginScene = SDL_FALSE;
   441     }
   537     }
   442 
   538 
   443     d3drect.x1 = rect->x;
   539     d3drect.x1 = rect->x;
   444     d3drect.x2 = rect->x+rect->w;
   540     d3drect.x2 = rect->x + rect->w;
   445     d3drect.y1 = rect->y;
   541     d3drect.y1 = rect->y;
   446     d3drect.y2 = rect->y+rect->h;
   542     d3drect.y2 = rect->y + rect->h;
   447 
   543 
   448     result = IDirect3DDevice9_Clear(data->device, 1, &d3drect, D3DCLEAR_TARGET, (D3DCOLOR) color, 1.0f, 0);
   544     result =
       
   545         IDirect3DDevice9_Clear(data->device, 1, &d3drect, D3DCLEAR_TARGET,
       
   546                                (D3DCOLOR) color, 1.0f, 0);
   449     if (FAILED(result)) {
   547     if (FAILED(result)) {
   450         D3D_SetError("Clear()", result);
   548         D3D_SetError("Clear()", result);
   451         return -1;
   549         return -1;
   452     }
   550     }
   453     return 0;
   551     return 0;
   459                    int blendMode, int scaleMode)
   557                    int blendMode, int scaleMode)
   460 {
   558 {
   461     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   559     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   462     SDL_D3D_TextureData *texturedata =
   560     SDL_D3D_TextureData *texturedata =
   463         (SDL_D3D_TextureData *) texture->driverdata;
   561         (SDL_D3D_TextureData *) texture->driverdata;
       
   562     float minx, miny, maxx, maxy;
       
   563     float mintu, maxtu, mintv, maxtv;
       
   564     Vertex vertices[4];
       
   565     HRESULT result;
   464 
   566 
   465     if (data->beginScene) {
   567     if (data->beginScene) {
   466         IDirect3DDevice9_BeginScene(data->device);
   568         IDirect3DDevice9_BeginScene(data->device);
   467         data->beginScene = SDL_FALSE;
   569         data->beginScene = SDL_FALSE;
   468     }
   570     }
       
   571 
       
   572     minx = (float) dstrect->x;
       
   573     miny = (float) dstrect->y;
       
   574     maxx = (float) dstrect->x + dstrect->w;
       
   575     maxy = (float) dstrect->y + dstrect->h;
       
   576 
       
   577     mintu = (float) srcrect->x / texture->w;
       
   578     maxtu = (float) (srcrect->x + srcrect->w) / texture->w;
       
   579     mintv = (float) srcrect->y / texture->h;
       
   580     maxtv = (float) (srcrect->y + srcrect->h) / texture->h;
       
   581 
       
   582     vertices[0].x = minx;
       
   583     vertices[0].y = miny;
       
   584     vertices[0].z = 0.0f;
       
   585     vertices[0].tu = mintu;
       
   586     vertices[0].tv = mintv;
       
   587     vertices[1].x = maxx;
       
   588     vertices[1].y = miny;
       
   589     vertices[1].z = 0.0f;
       
   590     vertices[1].tu = maxtu;
       
   591     vertices[1].tv = mintv;
       
   592     vertices[2].x = maxx;
       
   593     vertices[2].y = maxy;
       
   594     vertices[2].z = 0.0f;
       
   595     vertices[2].tu = maxtu;
       
   596     vertices[2].tv = maxtv;
       
   597     vertices[3].x = minx;
       
   598     vertices[3].y = maxy;
       
   599     vertices[3].z = 0.0f;
       
   600     vertices[3].tu = mintu;
       
   601     vertices[3].tv = maxtv;
       
   602 
       
   603     result =
       
   604         IDirect3DDevice9_SetTexture(data->device, 0,
       
   605                                     (IDirect3DBaseTexture9 *) texturedata->
       
   606                                     texture);
       
   607     if (FAILED(result)) {
       
   608         D3D_SetError("SetTexture()", result);
       
   609         return -1;
       
   610     }
       
   611     result =
       
   612         IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
       
   613                                          vertices, sizeof(*vertices));
       
   614     if (FAILED(result)) {
       
   615         D3D_SetError("DrawPrimitiveUP()", result);
       
   616         return -1;
       
   617     }
   469     return 0;
   618     return 0;
   470 }
   619 }
   471 
   620 
   472 static int
   621 static int
   473 SDL_D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   622 SDL_D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   509 {
   658 {
   510     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   659     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   511 
   660 
   512     if (!data) {
   661     if (!data) {
   513         return;
   662         return;
       
   663     }
       
   664     if (data->texture) {
       
   665         IDirect3DTexture9_Release(data->texture);
   514     }
   666     }
   515     SDL_free(data);
   667     SDL_free(data);
   516     texture->driverdata = NULL;
   668     texture->driverdata = NULL;
   517 }
   669 }
   518 
   670