comparison src/render/direct3d/SDL_d3drender.c @ 5176:ebfedf3787b1

Standardized on using the managed texture pool. Also experimented with dynamic textures, but didn't get any speed increase with them. More research and testing is needed.
author Sam Lantinga <slouken@libsdl.org>
date Fri, 04 Feb 2011 12:25:26 -0800
parents 4d39eeaad00b
children d976b67150c5
comparison
equal deleted inserted replaced
5175:51b4cfdf7ebb 5176:ebfedf3787b1
85 85
86 #endif /* ASSEMBLE_SHADER */ 86 #endif /* ASSEMBLE_SHADER */
87 87
88 88
89 /* Direct3D renderer implementation */ 89 /* Direct3D renderer implementation */
90
91 #if 1
92 /* This takes more memory but you won't lose your texture data */
93 #define D3DPOOL_SDL D3DPOOL_MANAGED
94 #define SDL_MEMORY_POOL_MANAGED
95 #else
96 #define D3DPOOL_SDL D3DPOOL_DEFAULT
97 #define SDL_MEMORY_POOL_DEFAULT
98 #endif
99 90
100 static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags); 91 static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
101 static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture); 92 static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
102 static int D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture, 93 static int D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
103 const SDL_Rect * rect, const void *pixels, 94 const SDL_Rect * rect, const void *pixels,
142 SDL_bool beginScene; 133 SDL_bool beginScene;
143 } D3D_RenderData; 134 } D3D_RenderData;
144 135
145 typedef struct 136 typedef struct
146 { 137 {
147 Uint32 format;
148 IDirect3DTexture9 *texture; 138 IDirect3DTexture9 *texture;
149 } D3D_TextureData; 139 } D3D_TextureData;
150 140
151 typedef struct 141 typedef struct
152 { 142 {
491 { 481 {
492 D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata; 482 D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
493 SDL_Window *window = renderer->window; 483 SDL_Window *window = renderer->window;
494 D3DFORMAT display_format = renderdata->pparams.BackBufferFormat; 484 D3DFORMAT display_format = renderdata->pparams.BackBufferFormat;
495 D3D_TextureData *data; 485 D3D_TextureData *data;
486 D3DPOOL pool;
487 DWORD usage;
496 HRESULT result; 488 HRESULT result;
497 489
498 data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data)); 490 data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
499 if (!data) { 491 if (!data) {
500 SDL_OutOfMemory(); 492 SDL_OutOfMemory();
501 return -1; 493 return -1;
502 } 494 }
503 495
504 texture->driverdata = data; 496 texture->driverdata = data;
505 497
506 data->format = texture->format; 498 #ifdef USE_DYNAMIC_TEXTURE
499 if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
500 pool = D3DPOOL_DEFAULT;
501 usage = D3DUSAGE_DYNAMIC;
502 } else
503 #endif
504 {
505 pool = D3DPOOL_MANAGED;
506 usage = 0;
507 }
507 508
508 result = 509 result =
509 IDirect3DDevice9_CreateTexture(renderdata->device, texture->w, 510 IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
510 texture->h, 1, 0, 511 texture->h, 1, usage,
511 PixelFormatToD3DFMT(data->format), 512 PixelFormatToD3DFMT(texture->format),
512 D3DPOOL_SDL, &data->texture, NULL); 513 pool, &data->texture, NULL);
513 if (FAILED(result)) { 514 if (FAILED(result)) {
514 D3D_SetError("CreateTexture()", result); 515 D3D_SetError("CreateTexture()", result);
515 return -1; 516 return -1;
516 } 517 }
517 518
522 D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture, 523 D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
523 const SDL_Rect * rect, const void *pixels, int pitch) 524 const SDL_Rect * rect, const void *pixels, int pitch)
524 { 525 {
525 D3D_TextureData *data = (D3D_TextureData *) texture->driverdata; 526 D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
526 D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata; 527 D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
527
528 #ifdef SDL_MEMORY_POOL_DEFAULT
529 IDirect3DTexture9 *temp;
530 RECT d3drect; 528 RECT d3drect;
531 D3DLOCKED_RECT locked; 529 D3DLOCKED_RECT locked;
532 const Uint8 *src; 530 const Uint8 *src;
533 Uint8 *dst; 531 Uint8 *dst;
534 int row, length; 532 int row, length;
535 HRESULT result; 533 HRESULT result;
536 534
537 result = 535 #ifdef USE_DYNAMIC_TEXTURE
538 IDirect3DDevice9_CreateTexture(renderdata->device, texture->w, 536 if (texture->access == SDL_TEXTUREACCESS_STREAMING &&
539 texture->h, 1, 0, 537 rect->x == 0 && rect->y == 0 &&
540 PixelFormatToD3DFMT(texture-> format), 538 rect->w == texture->w && rect->h == texture->h) {
541 D3DPOOL_SYSTEMMEM, &temp, NULL); 539 result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, NULL, D3DLOCK_DISCARD);
542 if (FAILED(result)) { 540 } else
543 D3D_SetError("CreateTexture()", result); 541 #endif
544 return -1; 542 {
545 } 543 d3drect.left = rect->x;
546 544 d3drect.right = rect->x + rect->w;
547 d3drect.left = rect->x; 545 d3drect.top = rect->y;
548 d3drect.right = rect->x + rect->w; 546 d3drect.bottom = rect->y + rect->h;
549 d3drect.top = rect->y; 547 result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
550 d3drect.bottom = rect->y + rect->h; 548 }
551 549
552 result = IDirect3DTexture9_LockRect(temp, 0, &locked, &d3drect, 0); 550 if (FAILED(result)) {
553 if (FAILED(result)) {
554 IDirect3DTexture9_Release(temp);
555 D3D_SetError("LockRect()", result); 551 D3D_SetError("LockRect()", result);
556 return -1; 552 return -1;
557 } 553 }
558 554
559 src = pixels; 555 src = pixels;
560 dst = locked.pBits; 556 dst = locked.pBits;
561 length = rect->w * SDL_BYTESPERPIXEL(texture->format); 557 length = rect->w * SDL_BYTESPERPIXEL(texture->format);
562 for (row = 0; row < rect->h; ++row) { 558 if (length == pitch && length == locked.Pitch) {
563 SDL_memcpy(dst, src, length); 559 SDL_memcpy(dst, src, length*rect->h);
564 src += pitch; 560 } else {
565 dst += locked.Pitch; 561 for (row = 0; row < rect->h; ++row) {
566 } 562 SDL_memcpy(dst, src, length);
567 IDirect3DTexture9_UnlockRect(temp, 0); 563 src += pitch;
568 564 dst += locked.Pitch;
569 result = 565 }
570 IDirect3DDevice9_UpdateTexture(renderdata->device,
571 (IDirect3DBaseTexture9 *) temp,
572 (IDirect3DBaseTexture9 *)
573 data->texture);
574 IDirect3DTexture9_Release(temp);
575 if (FAILED(result)) {
576 D3D_SetError("UpdateTexture()", result);
577 return -1;
578 }
579 #else
580 RECT d3drect;
581 D3DLOCKED_RECT locked;
582 const Uint8 *src;
583 Uint8 *dst;
584 int row, length;
585 HRESULT result;
586
587 d3drect.left = rect->x;
588 d3drect.right = rect->x + rect->w;
589 d3drect.top = rect->y;
590 d3drect.bottom = rect->y + rect->h;
591
592 result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
593 if (FAILED(result)) {
594 D3D_SetError("LockRect()", result);
595 return -1;
596 }
597
598 src = pixels;
599 dst = locked.pBits;
600 length = rect->w * SDL_BYTESPERPIXEL(texture->format);
601 for (row = 0; row < rect->h; ++row) {
602 SDL_memcpy(dst, src, length);
603 src += pitch;
604 dst += locked.Pitch;
605 } 566 }
606 IDirect3DTexture9_UnlockRect(data->texture, 0); 567 IDirect3DTexture9_UnlockRect(data->texture, 0);
607 #endif // SDL_MEMORY_POOL_DEFAULT
608 568
609 return 0; 569 return 0;
610 } 570 }
611 571
612 static int 572 static int