comparison src/video/win32/SDL_d3drender.c @ 1913:83420da906a5

Implemented Windows OpenGL support Fixed slowdown enumerating display modes, which was hosing OpenGL as well... Removed SDL_ from the render driver prefixes
author Sam Lantinga <slouken@libsdl.org>
date Mon, 17 Jul 2006 06:47:33 +0000
parents 06c27a737b7a
children c773b0c0ac89
comparison
equal deleted inserted replaced
1912:8d384b647307 1913:83420da906a5
25 25
26 #include "SDL_win32video.h" 26 #include "SDL_win32video.h"
27 27
28 /* Direct3D renderer implementation */ 28 /* Direct3D renderer implementation */
29 29
30 static SDL_Renderer *SDL_D3D_CreateRenderer(SDL_Window * window, 30 static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
31 Uint32 flags); 31 static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
32 static int SDL_D3D_CreateTexture(SDL_Renderer * renderer, 32 static int D3D_SetTexturePalette(SDL_Renderer * renderer,
33 SDL_Texture * texture); 33 SDL_Texture * texture,
34 static int SDL_D3D_SetTexturePalette(SDL_Renderer * renderer, 34 const SDL_Color * colors, int firstcolor,
35 SDL_Texture * texture, 35 int ncolors);
36 const SDL_Color * colors, int firstcolor, 36 static int D3D_GetTexturePalette(SDL_Renderer * renderer,
37 int ncolors); 37 SDL_Texture * texture, SDL_Color * colors,
38 static int SDL_D3D_GetTexturePalette(SDL_Renderer * renderer, 38 int firstcolor, int ncolors);
39 SDL_Texture * texture, 39 static int D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
40 SDL_Color * colors, int firstcolor, 40 const SDL_Rect * rect, const void *pixels,
41 int ncolors); 41 int pitch);
42 static int SDL_D3D_UpdateTexture(SDL_Renderer * renderer, 42 static int D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
43 SDL_Texture * texture, const SDL_Rect * rect, 43 const SDL_Rect * rect, int markDirty,
44 const void *pixels, int pitch); 44 void **pixels, int *pitch);
45 static int SDL_D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture, 45 static void D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
46 const SDL_Rect * rect, int markDirty, 46 static void D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
47 void **pixels, int *pitch); 47 int numrects, const SDL_Rect * rects);
48 static void SDL_D3D_UnlockTexture(SDL_Renderer * renderer, 48 static int D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
49 SDL_Texture * texture); 49 Uint32 color);
50 static void SDL_D3D_DirtyTexture(SDL_Renderer * renderer, 50 static int D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
51 SDL_Texture * texture, int numrects, 51 const SDL_Rect * srcrect, const SDL_Rect * dstrect,
52 const SDL_Rect * rects); 52 int blendMode, int scaleMode);
53 static int SDL_D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, 53 static void D3D_RenderPresent(SDL_Renderer * renderer);
54 Uint32 color); 54 static void D3D_DestroyTexture(SDL_Renderer * renderer,
55 static int SDL_D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, 55 SDL_Texture * texture);
56 const SDL_Rect * srcrect, 56 static void D3D_DestroyRenderer(SDL_Renderer * renderer);
57 const SDL_Rect * dstrect, int blendMode, 57
58 int scaleMode); 58
59 static void SDL_D3D_RenderPresent(SDL_Renderer * renderer); 59 SDL_RenderDriver D3D_RenderDriver = {
60 static void SDL_D3D_DestroyTexture(SDL_Renderer * renderer, 60 D3D_CreateRenderer,
61 SDL_Texture * texture);
62 static void SDL_D3D_DestroyRenderer(SDL_Renderer * renderer);
63
64
65 SDL_RenderDriver SDL_D3D_RenderDriver = {
66 SDL_D3D_CreateRenderer,
67 { 61 {
68 "d3d", 62 "d3d",
69 (SDL_Renderer_SingleBuffer | SDL_Renderer_PresentCopy | 63 (SDL_Renderer_SingleBuffer | SDL_Renderer_PresentCopy |
70 SDL_Renderer_PresentFlip2 | SDL_Renderer_PresentFlip3 | 64 SDL_Renderer_PresentFlip2 | SDL_Renderer_PresentFlip3 |
71 SDL_Renderer_PresentDiscard | SDL_Renderer_PresentVSync), 65 SDL_Renderer_PresentDiscard | SDL_Renderer_PresentVSync),
91 85
92 typedef struct 86 typedef struct
93 { 87 {
94 IDirect3DDevice9 *device; 88 IDirect3DDevice9 *device;
95 SDL_bool beginScene; 89 SDL_bool beginScene;
96 } SDL_D3D_RenderData; 90 } D3D_RenderData;
97 91
98 typedef struct 92 typedef struct
99 { 93 {
100 IDirect3DTexture9 *texture; 94 IDirect3DTexture9 *texture;
101 } SDL_D3D_TextureData; 95 } D3D_TextureData;
102 96
103 typedef struct 97 typedef struct
104 { 98 {
105 float x, y, z; 99 float x, y, z;
106 float rhw; 100 float rhw;
223 D3D_AddRenderDriver(_THIS) 217 D3D_AddRenderDriver(_THIS)
224 { 218 {
225 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; 219 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
226 220
227 if (data->d3d) { 221 if (data->d3d) {
228 SDL_AddRenderDriver(0, &SDL_D3D_RenderDriver); 222 SDL_AddRenderDriver(0, &D3D_RenderDriver);
229 } 223 }
230 } 224 }
231 225
232 SDL_Renderer * 226 SDL_Renderer *
233 SDL_D3D_CreateRenderer(SDL_Window * window, Uint32 flags) 227 D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
234 { 228 {
235 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window); 229 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
236 SDL_VideoData *videodata = (SDL_VideoData *) display->device->driverdata; 230 SDL_VideoData *videodata = (SDL_VideoData *) display->device->driverdata;
237 SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata; 231 SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
238 SDL_Renderer *renderer; 232 SDL_Renderer *renderer;
239 SDL_D3D_RenderData *data; 233 D3D_RenderData *data;
240 HRESULT result; 234 HRESULT result;
241 D3DPRESENT_PARAMETERS pparams; 235 D3DPRESENT_PARAMETERS pparams;
242 IDirect3DSwapChain9 *chain; 236 IDirect3DSwapChain9 *chain;
243 237
244 renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer)); 238 renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer));
246 SDL_OutOfMemory(); 240 SDL_OutOfMemory();
247 return NULL; 241 return NULL;
248 } 242 }
249 SDL_zerop(renderer); 243 SDL_zerop(renderer);
250 244
251 data = (SDL_D3D_RenderData *) SDL_malloc(sizeof(*data)); 245 data = (D3D_RenderData *) SDL_malloc(sizeof(*data));
252 if (!data) { 246 if (!data) {
253 SDL_D3D_DestroyRenderer(renderer); 247 D3D_DestroyRenderer(renderer);
254 SDL_OutOfMemory(); 248 SDL_OutOfMemory();
255 return NULL; 249 return NULL;
256 } 250 }
257 SDL_zerop(data); 251 SDL_zerop(data);
258 252
259 renderer->CreateTexture = SDL_D3D_CreateTexture; 253 renderer->CreateTexture = D3D_CreateTexture;
260 renderer->SetTexturePalette = SDL_D3D_SetTexturePalette; 254 renderer->SetTexturePalette = D3D_SetTexturePalette;
261 renderer->GetTexturePalette = SDL_D3D_GetTexturePalette; 255 renderer->GetTexturePalette = D3D_GetTexturePalette;
262 renderer->UpdateTexture = SDL_D3D_UpdateTexture; 256 renderer->UpdateTexture = D3D_UpdateTexture;
263 renderer->LockTexture = SDL_D3D_LockTexture; 257 renderer->LockTexture = D3D_LockTexture;
264 renderer->UnlockTexture = SDL_D3D_UnlockTexture; 258 renderer->UnlockTexture = D3D_UnlockTexture;
265 renderer->DirtyTexture = SDL_D3D_DirtyTexture; 259 renderer->DirtyTexture = D3D_DirtyTexture;
266 renderer->RenderFill = SDL_D3D_RenderFill; 260 renderer->RenderFill = D3D_RenderFill;
267 renderer->RenderCopy = SDL_D3D_RenderCopy; 261 renderer->RenderCopy = D3D_RenderCopy;
268 renderer->RenderPresent = SDL_D3D_RenderPresent; 262 renderer->RenderPresent = D3D_RenderPresent;
269 renderer->DestroyTexture = SDL_D3D_DestroyTexture; 263 renderer->DestroyTexture = D3D_DestroyTexture;
270 renderer->DestroyRenderer = SDL_D3D_DestroyRenderer; 264 renderer->DestroyRenderer = D3D_DestroyRenderer;
271 renderer->info = SDL_D3D_RenderDriver.info; 265 renderer->info = D3D_RenderDriver.info;
272 renderer->window = window->id; 266 renderer->window = window->id;
273 renderer->driverdata = data; 267 renderer->driverdata = data;
274 268
275 renderer->info.flags = SDL_Renderer_Accelerated; 269 renderer->info.flags = SDL_Renderer_Accelerated;
276 270
314 D3DDEVTYPE_HAL, 308 D3DDEVTYPE_HAL,
315 windowdata->hwnd, 309 windowdata->hwnd,
316 D3DCREATE_SOFTWARE_VERTEXPROCESSING, 310 D3DCREATE_SOFTWARE_VERTEXPROCESSING,
317 &pparams, &data->device); 311 &pparams, &data->device);
318 if (FAILED(result)) { 312 if (FAILED(result)) {
319 SDL_D3D_DestroyRenderer(renderer); 313 D3D_DestroyRenderer(renderer);
320 D3D_SetError("CreateDevice()", result); 314 D3D_SetError("CreateDevice()", result);
321 return NULL; 315 return NULL;
322 } 316 }
323 data->beginScene = SDL_TRUE; 317 data->beginScene = SDL_TRUE;
324 318
325 /* Get presentation parameters to fill info */ 319 /* Get presentation parameters to fill info */
326 result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain); 320 result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
327 if (FAILED(result)) { 321 if (FAILED(result)) {
328 SDL_D3D_DestroyRenderer(renderer); 322 D3D_DestroyRenderer(renderer);
329 D3D_SetError("GetSwapChain()", result); 323 D3D_SetError("GetSwapChain()", result);
330 return NULL; 324 return NULL;
331 } 325 }
332 result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams); 326 result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
333 if (FAILED(result)) { 327 if (FAILED(result)) {
334 IDirect3DSwapChain9_Release(chain); 328 IDirect3DSwapChain9_Release(chain);
335 SDL_D3D_DestroyRenderer(renderer); 329 D3D_DestroyRenderer(renderer);
336 D3D_SetError("GetPresentParameters()", result); 330 D3D_SetError("GetPresentParameters()", result);
337 return NULL; 331 return NULL;
338 } 332 }
339 IDirect3DSwapChain9_Release(chain); 333 IDirect3DSwapChain9_Release(chain);
340 switch (pparams.SwapEffect) { 334 switch (pparams.SwapEffect) {
374 368
375 return renderer; 369 return renderer;
376 } 370 }
377 371
378 static int 372 static int
379 SDL_D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture) 373 D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
380 { 374 {
381 SDL_D3D_RenderData *renderdata = 375 D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
382 (SDL_D3D_RenderData *) renderer->driverdata;
383 SDL_Window *window = SDL_GetWindowFromID(renderer->window); 376 SDL_Window *window = SDL_GetWindowFromID(renderer->window);
384 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window); 377 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
385 SDL_D3D_TextureData *data; 378 D3D_TextureData *data;
386 D3DPOOL pool; 379 D3DPOOL pool;
387 HRESULT result; 380 HRESULT result;
388 381
389 data = (SDL_D3D_TextureData *) SDL_malloc(sizeof(*data)); 382 data = (D3D_TextureData *) SDL_malloc(sizeof(*data));
390 if (!data) { 383 if (!data) {
391 SDL_OutOfMemory(); 384 SDL_OutOfMemory();
392 return -1; 385 return -1;
393 } 386 }
394 SDL_zerop(data); 387 SDL_zerop(data);
413 406
414 return 0; 407 return 0;
415 } 408 }
416 409
417 static int 410 static int
418 SDL_D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture, 411 D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
419 const SDL_Color * colors, int firstcolor, 412 const SDL_Color * colors, int firstcolor, int ncolors)
420 int ncolors) 413 {
421 { 414 D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
422 SDL_D3D_RenderData *renderdata = 415 D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
423 (SDL_D3D_RenderData *) renderer->driverdata;
424 SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
425 416
426 return 0; 417 return 0;
427 } 418 }
428 419
429 static int 420 static int
430 SDL_D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture, 421 D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
431 SDL_Color * colors, int firstcolor, int ncolors) 422 SDL_Color * colors, int firstcolor, int ncolors)
432 { 423 {
433 SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata; 424 D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
434 425
435 return 0; 426 return 0;
436 } 427 }
437 428
438 static int 429 static int
439 SDL_D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture, 430 D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
440 const SDL_Rect * rect, const void *pixels, int pitch) 431 const SDL_Rect * rect, const void *pixels, int pitch)
441 { 432 {
442 SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata; 433 D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
443 SDL_D3D_RenderData *renderdata = 434 D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
444 (SDL_D3D_RenderData *) renderer->driverdata;
445 IDirect3DTexture9 *temp; 435 IDirect3DTexture9 *temp;
446 RECT d3drect; 436 RECT d3drect;
447 D3DLOCKED_RECT locked; 437 D3DLOCKED_RECT locked;
448 const Uint8 *src; 438 const Uint8 *src;
449 Uint8 *dst; 439 Uint8 *dst;
494 } 484 }
495 return 0; 485 return 0;
496 } 486 }
497 487
498 static int 488 static int
499 SDL_D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture, 489 D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
500 const SDL_Rect * rect, int markDirty, void **pixels, 490 const SDL_Rect * rect, int markDirty, void **pixels,
501 int *pitch) 491 int *pitch)
502 { 492 {
503 SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata; 493 D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
504 RECT d3drect; 494 RECT d3drect;
505 D3DLOCKED_RECT locked; 495 D3DLOCKED_RECT locked;
506 HRESULT result; 496 HRESULT result;
507 497
508 if (texture->access != SDL_TextureAccess_Local) { 498 if (texture->access != SDL_TextureAccess_Local) {
526 *pitch = locked.Pitch; 516 *pitch = locked.Pitch;
527 return 0; 517 return 0;
528 } 518 }
529 519
530 static void 520 static void
531 SDL_D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture) 521 D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
532 { 522 {
533 SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata; 523 D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
534 524
535 IDirect3DTexture9_UnlockRect(data->texture, 0); 525 IDirect3DTexture9_UnlockRect(data->texture, 0);
536 } 526 }
537 527
538 static void 528 static void
539 SDL_D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, 529 D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
540 int numrects, const SDL_Rect * rects) 530 const SDL_Rect * rects)
541 { 531 {
542 SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata; 532 D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
543 RECT d3drect; 533 RECT d3drect;
544 int i; 534 int i;
545 535
546 for (i = 0; i < numrects; ++i) { 536 for (i = 0; i < numrects; ++i) {
547 const SDL_Rect *rect = &rects[i]; 537 const SDL_Rect *rect = &rects[i];
554 IDirect3DTexture9_AddDirtyRect(data->texture, &d3drect); 544 IDirect3DTexture9_AddDirtyRect(data->texture, &d3drect);
555 } 545 }
556 } 546 }
557 547
558 static int 548 static int
559 SDL_D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, 549 D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 color)
560 Uint32 color) 550 {
561 { 551 D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
562 SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
563 D3DRECT d3drect; 552 D3DRECT d3drect;
564 HRESULT result; 553 HRESULT result;
565 554
566 if (data->beginScene) { 555 if (data->beginScene) {
567 IDirect3DDevice9_BeginScene(data->device); 556 IDirect3DDevice9_BeginScene(data->device);
582 } 571 }
583 return 0; 572 return 0;
584 } 573 }
585 574
586 static int 575 static int
587 SDL_D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, 576 D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
588 const SDL_Rect * srcrect, const SDL_Rect * dstrect, 577 const SDL_Rect * srcrect, const SDL_Rect * dstrect,
589 int blendMode, int scaleMode) 578 int blendMode, int scaleMode)
590 { 579 {
591 SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata; 580 D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
592 SDL_D3D_TextureData *texturedata = 581 D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
593 (SDL_D3D_TextureData *) texture->driverdata;
594 float minx, miny, maxx, maxy; 582 float minx, miny, maxx, maxy;
595 float minu, maxu, minv, maxv; 583 float minu, maxu, minv, maxv;
596 Vertex vertices[4]; 584 Vertex vertices[4];
597 HRESULT result; 585 HRESULT result;
598 586
656 } 644 }
657 return 0; 645 return 0;
658 } 646 }
659 647
660 static void 648 static void
661 SDL_D3D_RenderPresent(SDL_Renderer * renderer) 649 D3D_RenderPresent(SDL_Renderer * renderer)
662 { 650 {
663 SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata; 651 D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
664 HRESULT result; 652 HRESULT result;
665 653
666 if (!data->beginScene) { 654 if (!data->beginScene) {
667 IDirect3DDevice9_EndScene(data->device); 655 IDirect3DDevice9_EndScene(data->device);
668 data->beginScene = SDL_TRUE; 656 data->beginScene = SDL_TRUE;
673 D3D_SetError("Present()", result); 661 D3D_SetError("Present()", result);
674 } 662 }
675 } 663 }
676 664
677 static void 665 static void
678 SDL_D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture) 666 D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
679 { 667 {
680 SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata; 668 D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
681 669
682 if (!data) { 670 if (!data) {
683 return; 671 return;
684 } 672 }
685 if (data->texture) { 673 if (data->texture) {
688 SDL_free(data); 676 SDL_free(data);
689 texture->driverdata = NULL; 677 texture->driverdata = NULL;
690 } 678 }
691 679
692 void 680 void
693 SDL_D3D_DestroyRenderer(SDL_Renderer * renderer) 681 D3D_DestroyRenderer(SDL_Renderer * renderer)
694 { 682 {
695 SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata; 683 D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
696 684
697 if (data) { 685 if (data) {
698 if (data->device) { 686 if (data->device) {
699 IDirect3DDevice9_Release(data->device); 687 IDirect3DDevice9_Release(data->device);
700 } 688 }