comparison src/video/win32/SDL_d3drender.c @ 1900:5c6bdbf3aadf

First stab at a D3D renderer, only 30FPS so far... ?
author Sam Lantinga <slouken@libsdl.org>
date Wed, 12 Jul 2006 09:25:17 +0000
parents f89e49e51e89
children f1828a500391
comparison
equal deleted inserted replaced
1899:6a11e61bf805 1900:5c6bdbf3aadf
76 76
77 SDL_RenderDriver SDL_D3D_RenderDriver = { 77 SDL_RenderDriver SDL_D3D_RenderDriver = {
78 SDL_D3D_CreateRenderer, 78 SDL_D3D_CreateRenderer,
79 { 79 {
80 "d3d", 80 "d3d",
81 (SDL_Renderer_Minimal | 81 ( //SDL_Renderer_Minimal |
82 SDL_Renderer_SingleBuffer | SDL_Renderer_PresentCopy | 82 SDL_Renderer_SingleBuffer | SDL_Renderer_PresentCopy |
83 SDL_Renderer_PresentFlip2 | SDL_Renderer_PresentFlip3 | 83 SDL_Renderer_PresentFlip2 | SDL_Renderer_PresentFlip3 |
84 SDL_Renderer_PresentDiscard | SDL_Renderer_RenderTarget), 84 SDL_Renderer_PresentDiscard | SDL_Renderer_RenderTarget),
85 (SDL_TextureBlendMode_None | 85 (SDL_TextureBlendMode_None |
86 SDL_TextureBlendMode_Mask | SDL_TextureBlendMode_Blend), 86 SDL_TextureBlendMode_Mask | SDL_TextureBlendMode_Blend),
87 (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast), 87 (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast),
88 11, 88 11,
89 { 89 {
103 }; 103 };
104 104
105 typedef struct 105 typedef struct
106 { 106 {
107 IDirect3DDevice9 *device; 107 IDirect3DDevice9 *device;
108 SDL_bool beginScene;
108 } SDL_D3D_RenderData; 109 } SDL_D3D_RenderData;
109 110
110 typedef struct 111 typedef struct
111 { 112 {
112 SDL_SW_YUVTexture *yuv; 113 SDL_SW_YUVTexture *yuv;
113 } SDL_D3D_TextureData; 114 } SDL_D3D_TextureData;
115
116 static void
117 D3D_SetError(const char *prefix, HRESULT result)
118 {
119 const char *error;
120
121 switch (result) {
122 case D3DERR_WRONGTEXTUREFORMAT:
123 error = "WRONGTEXTUREFORMAT";
124 break;
125 case D3DERR_UNSUPPORTEDCOLOROPERATION:
126 error = "UNSUPPORTEDCOLOROPERATION";
127 break;
128 case D3DERR_UNSUPPORTEDCOLORARG:
129 error = "UNSUPPORTEDCOLORARG";
130 break;
131 case D3DERR_UNSUPPORTEDALPHAOPERATION:
132 error = "UNSUPPORTEDALPHAOPERATION";
133 break;
134 case D3DERR_UNSUPPORTEDALPHAARG:
135 error = "UNSUPPORTEDALPHAARG";
136 break;
137 case D3DERR_TOOMANYOPERATIONS:
138 error = "TOOMANYOPERATIONS";
139 break;
140 case D3DERR_CONFLICTINGTEXTUREFILTER:
141 error = "CONFLICTINGTEXTUREFILTER";
142 break;
143 case D3DERR_UNSUPPORTEDFACTORVALUE:
144 error = "UNSUPPORTEDFACTORVALUE";
145 break;
146 case D3DERR_CONFLICTINGRENDERSTATE:
147 error = "CONFLICTINGRENDERSTATE";
148 break;
149 case D3DERR_UNSUPPORTEDTEXTUREFILTER:
150 error = "UNSUPPORTEDTEXTUREFILTER";
151 break;
152 case D3DERR_CONFLICTINGTEXTUREPALETTE:
153 error = "CONFLICTINGTEXTUREPALETTE";
154 break;
155 case D3DERR_DRIVERINTERNALERROR:
156 error = "DRIVERINTERNALERROR";
157 break;
158 case D3DERR_NOTFOUND:
159 error = "NOTFOUND";
160 break;
161 case D3DERR_MOREDATA:
162 error = "MOREDATA";
163 break;
164 case D3DERR_DEVICELOST:
165 error = "DEVICELOST";
166 break;
167 case D3DERR_DEVICENOTRESET:
168 error = "DEVICENOTRESET";
169 break;
170 case D3DERR_NOTAVAILABLE:
171 error = "NOTAVAILABLE";
172 break;
173 case D3DERR_OUTOFVIDEOMEMORY:
174 error = "OUTOFVIDEOMEMORY";
175 break;
176 case D3DERR_INVALIDDEVICE:
177 error = "INVALIDDEVICE";
178 break;
179 case D3DERR_INVALIDCALL:
180 error = "INVALIDCALL";
181 break;
182 case D3DERR_DRIVERINVALIDCALL:
183 error = "DRIVERINVALIDCALL";
184 break;
185 case D3DERR_WASSTILLDRAWING:
186 error = "WASSTILLDRAWING";
187 break;
188 default:
189 error = "UNKNOWN";
190 break;
191 }
192 SDL_SetError("%s: %s", prefix, error);
193 }
114 194
115 static void 195 static void
116 UpdateYUVTextureData(SDL_Texture * texture) 196 UpdateYUVTextureData(SDL_Texture * texture)
117 { 197 {
118 SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata; 198 SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
142 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window); 222 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
143 SDL_VideoData *videodata = (SDL_VideoData *) display->device->driverdata; 223 SDL_VideoData *videodata = (SDL_VideoData *) display->device->driverdata;
144 SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata; 224 SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
145 SDL_Renderer *renderer; 225 SDL_Renderer *renderer;
146 SDL_D3D_RenderData *data; 226 SDL_D3D_RenderData *data;
227 HRESULT result;
228 D3DPRESENT_PARAMETERS pparams;
147 229
148 renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer)); 230 renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer));
149 if (!renderer) { 231 if (!renderer) {
150 SDL_OutOfMemory(); 232 SDL_OutOfMemory();
151 return NULL; 233 return NULL;
157 SDL_D3D_DestroyRenderer(renderer); 239 SDL_D3D_DestroyRenderer(renderer);
158 SDL_OutOfMemory(); 240 SDL_OutOfMemory();
159 return NULL; 241 return NULL;
160 } 242 }
161 SDL_zerop(data); 243 SDL_zerop(data);
162
163 //data->device = IDirect3D9_CreateDevice(videodata->d3d,
164 244
165 renderer->CreateTexture = SDL_D3D_CreateTexture; 245 renderer->CreateTexture = SDL_D3D_CreateTexture;
166 renderer->QueryTexturePixels = SDL_D3D_QueryTexturePixels; 246 renderer->QueryTexturePixels = SDL_D3D_QueryTexturePixels;
167 renderer->SetTexturePalette = SDL_D3D_SetTexturePalette; 247 renderer->SetTexturePalette = SDL_D3D_SetTexturePalette;
168 renderer->GetTexturePalette = SDL_D3D_GetTexturePalette; 248 renderer->GetTexturePalette = SDL_D3D_GetTexturePalette;
182 renderer->window = window->id; 262 renderer->window = window->id;
183 renderer->driverdata = data; 263 renderer->driverdata = data;
184 264
185 renderer->info.flags = SDL_Renderer_RenderTarget; 265 renderer->info.flags = SDL_Renderer_RenderTarget;
186 266
267 SDL_zero(pparams);
268 pparams.BackBufferWidth = window->w;
269 pparams.BackBufferHeight = window->h;
270 pparams.BackBufferFormat = D3DFMT_UNKNOWN; /* FIXME */
271 if (flags & SDL_Renderer_PresentFlip2) {
272 pparams.BackBufferCount = 2;
273 pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
274 } else if (flags & SDL_Renderer_PresentFlip3) {
275 pparams.BackBufferCount = 3;
276 pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
277 } else if (flags & SDL_Renderer_PresentCopy) {
278 pparams.BackBufferCount = 1;
279 pparams.SwapEffect = D3DSWAPEFFECT_COPY;
280 } else {
281 pparams.BackBufferCount = 1;
282 pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
283 }
284 if (window->flags & SDL_WINDOW_FULLSCREEN) {
285 pparams.Windowed = FALSE;
286 } else {
287 pparams.Windowed = TRUE;
288 }
289 pparams.FullScreen_RefreshRateInHz = 0; /* FIXME */
290
291 result = IDirect3D9_CreateDevice(videodata->d3d, D3DADAPTER_DEFAULT, /* FIXME */
292 D3DDEVTYPE_HAL,
293 windowdata->hwnd,
294 D3DCREATE_SOFTWARE_VERTEXPROCESSING,
295 &pparams, &data->device);
296 if (FAILED(result)) {
297 SDL_D3D_DestroyRenderer(renderer);
298 D3D_SetError("CreateDevice()", result);
299 return NULL;
300 }
301 data->beginScene = SDL_TRUE;
302
187 return renderer; 303 return renderer;
188 } 304 }
189 305
190 static int 306 static int
191 SDL_D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture) 307 SDL_D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
281 397
282 if (data->yuv) { 398 if (data->yuv) {
283 return SDL_SW_LockYUVTexture(data->yuv, rect, markDirty, pixels, 399 return SDL_SW_LockYUVTexture(data->yuv, rect, markDirty, pixels,
284 pitch); 400 pitch);
285 } else { 401 } else {
286 return 0; 402 return -1;
287 } 403 }
288 } 404 }
289 405
290 static void 406 static void
291 SDL_D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture) 407 SDL_D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
313 static int 429 static int
314 SDL_D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, 430 SDL_D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
315 Uint32 color) 431 Uint32 color)
316 { 432 {
317 SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata; 433 SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
318 Uint8 r, g, b; 434 HRESULT result;
319 435
320 r = (Uint8) ((color >> 16) & 0xFF); 436 if (data->beginScene) {
321 g = (Uint8) ((color >> 8) & 0xFF); 437 IDirect3DDevice9_BeginScene(data->device);
322 b = (Uint8) (color & 0xFF); 438 data->beginScene = SDL_FALSE;
323 439 }
440
441 result =
442 IDirect3DDevice9_Clear(data->device, 0, NULL, D3DCLEAR_TARGET,
443 (D3DCOLOR) color, 1.0f, 0);
444 if (FAILED(result)) {
445 D3D_SetError("Clear()", result);
446 return -1;
447 }
324 return 0; 448 return 0;
325 } 449 }
326 450
327 static int 451 static int
328 SDL_D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, 452 SDL_D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
331 { 455 {
332 SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata; 456 SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
333 SDL_D3D_TextureData *texturedata = 457 SDL_D3D_TextureData *texturedata =
334 (SDL_D3D_TextureData *) texture->driverdata; 458 (SDL_D3D_TextureData *) texture->driverdata;
335 459
460 if (data->beginScene) {
461 IDirect3DDevice9_BeginScene(data->device);
462 data->beginScene = SDL_FALSE;
463 }
336 return 0; 464 return 0;
337 } 465 }
338 466
339 static int 467 static int
340 SDL_D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect, 468 SDL_D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
355 } 483 }
356 484
357 static void 485 static void
358 SDL_D3D_RenderPresent(SDL_Renderer * renderer) 486 SDL_D3D_RenderPresent(SDL_Renderer * renderer)
359 { 487 {
488 SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
489 HRESULT result;
490
491 if (!data->beginScene) {
492 IDirect3DDevice9_EndScene(data->device);
493 data->beginScene = SDL_TRUE;
494 }
495
496 result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
497 if (FAILED(result)) {
498 D3D_SetError("Present()", result);
499 }
360 } 500 }
361 501
362 static void 502 static void
363 SDL_D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture) 503 SDL_D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
364 { 504 {
375 SDL_D3D_DestroyRenderer(SDL_Renderer * renderer) 515 SDL_D3D_DestroyRenderer(SDL_Renderer * renderer)
376 { 516 {
377 SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata; 517 SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
378 518
379 if (data) { 519 if (data) {
520 if (data->device) {
521 IDirect3DDevice9_Release(data->device);
522 }
380 SDL_free(data); 523 SDL_free(data);
381 } 524 }
382 SDL_free(renderer); 525 SDL_free(renderer);
383 } 526 }
384 527