-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvideoc-d3d9.cpp
374 lines (308 loc) · 11 KB
/
videoc-d3d9.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "io.h"
#ifdef VIDEO_D3D9
#define video cvideo
#undef bind
#define interface struct
#include <D3D9.h>
#undef interface
#define bind bind_func
//force some year-old C code to compile properly as C++ - I decided to switch long ago but still haven't finished.
#define this This
#define D3DSWAPEFFECT_FLIPEX ((D3DSWAPEFFECT)5)//lazy compiler. and it's an enum so I can't #ifdef it
//(if this one exists, it's safe to ignore; 5 is still 5,
// and they can't break ABI so it must remain 5.)
#ifndef D3DPRESENT_FORCEIMMEDIATE
#define D3DPRESENT_FORCEIMMEDIATE 0x00000100L
#endif
#ifndef D3DPRESENT_DONOTWAIT
#define D3DPRESENT_DONOTWAIT 0x00000001L
#endif
static HMODULE hD3D9=NULL;
static HRESULT (WINAPI * lpDirect3DCreate9Ex)(UINT SDKVersion, IDirect3D9Ex* * ppD3D);
static IDirect3D9* (WINAPI * lpDirect3DCreate9)(UINT SDKVersion);
static bool libLoad();
static void libRelease();
struct video_d3d9 {
struct video i;
HWND hwnd;
bool ex;
IDirect3D9Ex* d3d;
IDirect3DDevice9Ex* device;
IDirect3DVertexBuffer9* vertexbuf;
unsigned int screenwidth;
unsigned int screenheight;
D3DFORMAT texformat;
UINT texwidth;
UINT texheight;
UINT bytes_per_row;
IDirect3DTexture9* texture;
float texoffsetx;
float texoffsety;
DWORD syncflags;
};
static void clear(struct video_d3d9 * this)
{
if (this->vertexbuf) this->vertexbuf->Release();
this->vertexbuf=NULL;
if (this->texture) this->texture->Release();
this->texture=NULL;
this->texwidth=0;
this->texheight=0;
if (this->device) this->device->Release();
this->device=NULL;
}
static bool recreate(struct video_d3d9 * this, unsigned int screenwidth, unsigned int screenheight, videoformat depth)
{
clear(this);
//depth=0 is allowed too, it means keep current value
if (depth==fmt_xrgb1555) this->texformat=D3DFMT_A1R5G5B5;//X1R5G5B5 fails for no obvious reason
if (depth==fmt_rgb565) this->texformat=D3DFMT_R5G6B5;
if (depth==fmt_xrgb8888) this->texformat=D3DFMT_X8R8G8B8;
D3DPRESENT_PARAMETERS parameters;
memset(¶meters, 0, sizeof(parameters));
parameters.BackBufferWidth=screenwidth;
parameters.BackBufferHeight=screenheight;
//parameters.BackBufferFormat=this->texformat;
parameters.BackBufferFormat=D3DFMT_UNKNOWN;
parameters.BackBufferCount=2;//this value is confirmed by experiments; it is the lowest value that doesn't force vsync on.
parameters.MultiSampleType=D3DMULTISAMPLE_NONE;
parameters.MultiSampleQuality=0;
if (this->ex) parameters.SwapEffect=D3DSWAPEFFECT_FLIPEX;
else parameters.SwapEffect=D3DSWAPEFFECT_DISCARD;
parameters.hDeviceWindow=this->hwnd;
parameters.Windowed=TRUE;
parameters.EnableAutoDepthStencil=FALSE;//TODO: is this useful?
//parameters.AutoDepthStencilFormat;//ignored
parameters.Flags=0;
parameters.FullScreen_RefreshRateInHz=0;
parameters.PresentationInterval=D3DPRESENT_INTERVAL_DEFAULT;//TODO: try _ONE
#ifndef NO_D3D9_EX
if (this->ex)
{
if (FAILED(this->d3d->CreateDeviceEx(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, this->hwnd,
D3DCREATE_MIXED_VERTEXPROCESSING/* | D3DCREATE_PUREDEVICE*/,
//PUREDEVICE doesn't work for everyone, and is of questionable use anyways
¶meters, NULL, &this->device)) &&
FAILED(this->d3d->CreateDeviceEx(D3DADAPTER_DEFAULT, D3DDEVTYPE_SW, this->hwnd,
D3DCREATE_MIXED_VERTEXPROCESSING/* | D3DCREATE_PUREDEVICE*/,
¶meters, NULL, &this->device)))
{
return false;
}
}
#endif
if (!this->ex)
{
parameters.PresentationInterval=(this->syncflags?D3DPRESENT_INTERVAL_DEFAULT:D3DPRESENT_INTERVAL_IMMEDIATE);
if (FAILED(this->d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, this->hwnd,
D3DCREATE_MIXED_VERTEXPROCESSING/* | D3DCREATE_PUREDEVICE*/,
¶meters, (IDirect3DDevice9**)&this->device)) &&
FAILED(this->d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_SW, this->hwnd,
D3DCREATE_MIXED_VERTEXPROCESSING/* | D3DCREATE_PUREDEVICE*/,
¶meters, (IDirect3DDevice9**)&this->device)))
{
return false;
}
}
this->device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
this->device->SetRenderState(D3DRS_LIGHTING, FALSE);
if (FAILED(this->device->CreateVertexBuffer(sizeof(float)*5*4, 0, D3DFVF_XYZ|D3DFVF_TEX1, D3DPOOL_DEFAULT, &this->vertexbuf, NULL)))
{
return false;
}
if (screenwidth)
{
this->screenwidth=screenwidth;
this->screenheight=screenheight;
}
this->texoffsetx=0.5/this->screenwidth;
this->texoffsety=0.5/this->screenheight;
return true;
}
static void reinit(struct video * this_, unsigned int screen_width, unsigned int screen_height, videoformat depth, double fps)
{
struct video_d3d9 * this=(struct video_d3d9*)this_;
recreate(this, screen_width, screen_height, depth);
}
static void draw(struct video * this_, unsigned int width, unsigned int height, const void * data, unsigned int pitch)
{
struct video_d3d9 * this=(struct video_d3d9*)this_;
HRESULT status=this->device->TestCooperativeLevel();
if (status==D3DERR_DEVICELOST) return;
if (status==D3DERR_DEVICENOTRESET)
{
recreate(this, 0,0, fmt_none);
status=this->device->TestCooperativeLevel();
if (status!=D3D_OK) return;
}
//this->device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);
if (this->texwidth!=width || this->texheight!=height || !this->texture)
{
this->texoffsetx=0.5/this->screenwidth;
this->texoffsety=0.5/this->screenheight;
this->texwidth=width;
this->texheight=height;
if (this->texture) this->texture->Release();
this->texture=NULL;
HRESULT hr=this->device->CreateTexture(width, height, 1, D3DUSAGE_DYNAMIC, this->texformat, D3DPOOL_DEFAULT, &this->texture, NULL);
if (FAILED(hr))
{
this->texture=NULL;
}
if (!this->texture)
{
this->device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 0, 0), 1.0f, 0);
goto present;
}
if (this->texformat==D3DFMT_X8R8G8B8) this->bytes_per_row=width*4;
else this->bytes_per_row=width*2;
float* vertices;
if (FAILED(this->vertexbuf->Lock(0, 0, (void**)&vertices, 0)))
{
this->texture->Release();
this->texture=NULL;
return;
}
float srcvertices[]={
-1,-1,0, 0+this->texoffsetx,1+this->texoffsety,
-1, 1,0, 0+this->texoffsetx,0+this->texoffsety,
1,-1,0, 1+this->texoffsetx,1+this->texoffsety,
1, 1,0, 1+this->texoffsetx,0+this->texoffsety,
};
memcpy(vertices, srcvertices, sizeof(float)*5*4);
this->vertexbuf->Unlock();
}
if (data)
{
D3DLOCKED_RECT locked;
if (FAILED(this->texture->LockRect(0, &locked, NULL, D3DLOCK_DISCARD|D3DLOCK_NOOVERWRITE)))
{
this->device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 255, 0), 1.0f, 0);
if (this->ex) this->device->PresentEx(NULL, NULL, NULL, NULL, 0);
else this->device->Present(NULL, NULL, NULL, NULL);
return;
}
if ((unsigned int)locked.Pitch==pitch) memcpy(locked.pBits, data, pitch*(height-1)+this->bytes_per_row);
else
{
for (unsigned int i=0;i<height;i++)
{
memcpy((uint8_t*)locked.pBits + i*locked.Pitch, (uint8_t*)data + i*pitch, this->bytes_per_row);
}
}
this->texture->UnlockRect(0);
// [in] UINT Level,
// [out] D3DLOCKED_RECT *pLockedRect,
// [in] const RECT *pRect,
// [in] DWORD Flags
//);
}
this->device->BeginScene();
this->device->SetTexture(0, (struct IDirect3DBaseTexture9*)this->texture);//apparently this one is subclassed
this->device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
this->device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
this->device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
this->device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
this->device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
this->device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
//this->device->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT4 | D3DTTFF_PROJECTED );
//this->device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACEPOSITION );
this->device->SetStreamSource(0, this->vertexbuf, 0, sizeof(float)*5);
this->device->SetFVF(D3DFVF_XYZ|D3DFVF_TEX1);
this->device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
this->device->EndScene();
present:
if (this->ex) this->device->PresentEx(NULL, NULL, NULL, NULL, this->syncflags);
else this->device->Present(NULL, NULL, NULL, NULL);
}
static bool set_sync(struct video * this_, bool sync)
{
struct video_d3d9 * this=(struct video_d3d9*)this_;
bool ret=this->syncflags;
if (this->ex)
{
this->syncflags=(sync ? 0 : D3DPRESENT_FORCEIMMEDIATE|D3DPRESENT_DONOTWAIT);
}
else
{
if (this->syncflags != (DWORD)sync)
{
this->syncflags=(sync);
recreate(this, 0,0, fmt_none);
}
}
return ret;
}
static bool has_sync(struct video * this_)
{
//struct video_d3d9 * this=(struct video_d3d9*)this_;
//return (this->ex);
return true;
}
static void free_(struct video * this_)
{
struct video_d3d9 * this=(struct video_d3d9*)this_;
clear(this);
if (this->d3d) this->d3d->Release();
libRelease();
free(this);
}
static bool libLoad()
{
hD3D9=LoadLibrary("d3d9.dll");
if (!hD3D9) return false;
//lpDirect3DCreate9=Direct3DCreate9;//these are for type checking; they're not needed anymore
//lpDirect3DCreate9Ex=Direct3DCreate9Ex;
lpDirect3DCreate9=(IDirect3D9* (WINAPI*)(UINT))GetProcAddress(hD3D9, "Direct3DCreate9");
if (!lpDirect3DCreate9) { FreeLibrary(hD3D9); return false; }
lpDirect3DCreate9Ex=(HRESULT (WINAPI*)(UINT,IDirect3D9Ex**))GetProcAddress(hD3D9, "Direct3DCreate9Ex");
//if (!lpDirect3DCreate9Ex) return false;
return true;
}
static void libRelease()
{
FreeLibrary(hD3D9);
}
static struct video * cvideo_create_d3d9(uintptr_t windowhandle, unsigned int screen_width, unsigned int screen_height,
videoformat depth, double fps)
{
if (!libLoad()) return NULL;
struct video_d3d9 * this=malloc(sizeof(struct video_d3d9));
this->i.reinit=reinit;
this->i.draw=draw;
this->i.set_sync=set_sync;
this->i.has_sync=has_sync;
this->i.free=free_;
this->hwnd=(HWND)windowhandle;
this->ex=false;
this->d3d=NULL;
this->device=NULL;
this->texture=NULL;
this->vertexbuf=NULL;
this->syncflags=0;
#ifndef NO_D3D9_EX
if (lpDirect3DCreate9Ex && !FAILED(lpDirect3DCreate9Ex(D3D_SDK_VERSION, &this->d3d)))
{
this->ex=true;
}
#endif
//try creating old d3d9 if ex is not available
if(!this->ex)
{
this->d3d=(IDirect3D9Ex*)lpDirect3DCreate9(D3D_SDK_VERSION);
if (!this->d3d) goto cancel;
this->syncflags=true;
}
if (!recreate(this, screen_width, screen_height, depth)) goto cancel;
return (struct video*)this;
cancel:
free_((struct video*)this);
return NULL;
}
#undef video
static video* video_create_d3d9(uintptr_t windowhandle)
{
return video_create_compat(cvideo_create_d3d9(windowhandle, 32, 32, fmt_xrgb1555, 60));
}
const video::driver video::driver_d3d9 = {"Direct3D", video_create_d3d9, NULL, video::f_vsync};
#endif