Debian Codegraph @

src/gui/render/renderDX9.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define INCLUDE_D3D9
21 #include "renderDX9.h"
22 #include <SDL_syswm.h>
23 #include "backends/imgui_impl_dx9.h"
24 #include "../../ta-log.h"
25 #include "../../utfutils.h"
26 #include "../engine/bsr.h"
27
28 class FurnaceDX9Texture: public FurnaceGUITexture {
29 public:
30 IDirect3DTexture9* tex;
31 IDirect3DTexture9* texPre;
32 int width, height, widthReal, heightReal;
33 FurnaceGUITextureFormat format;
34 unsigned char* lockedData;
35 bool dynamic;
36 FurnaceDX9Texture():
37 tex(NULL),
38 texPre(NULL),
39 width(0),
40 height(0),
41 widthReal(0),
42 heightReal(0),
43 format(GUI_TEXFORMAT_UNKNOWN),
44 lockedData(NULL),
45 dynamic(false) {}
46 };
47
48 struct FurnaceGUIRenderDX9Private {
49 D3DPRESENT_PARAMETERS present;
50 std::vector<FurnaceDX9Texture*> texPool;
51 };
52
53 struct WipeVertex {
54 float x, y, z;
55 unsigned int color;
56
57 WipeVertex(float _x, float _y, float _z, unsigned int c):
58 x(_x),
59 y(_y),
60 z(_z),
61 color(c) {}
62 WipeVertex():
63 x(0),
64 y(0),
65 z(0),
66 color(0) {}
67 };
68
69
70 ImTextureID FurnaceGUIRenderDX9::getTextureID(FurnaceGUITexture* which) {
71 FurnaceDX9Texture* t=(FurnaceDX9Texture*)which;
72 return (ImTextureID)t->tex;
73 }
74
75 float FurnaceGUIRenderDX9::getTextureU(FurnaceGUITexture* which) {
76 FurnaceDX9Texture* t=(FurnaceDX9Texture*)which;
77 if (which==NULL) return 0.0;
78 if (t->widthReal<1) return 0.0f;
79 return (float)t->width/(float)t->widthReal;
80 }
81
82 float FurnaceGUIRenderDX9::getTextureV(FurnaceGUITexture* which) {
83 FurnaceDX9Texture* t=(FurnaceDX9Texture*)which;
84 if (which==NULL) return 0.0;
85 if (t->heightReal<1) return 0.0f;
86 return (float)t->height/(float)t->heightReal;
87 }
88
89 FurnaceGUITextureFormat FurnaceGUIRenderDX9::getTextureFormat(FurnaceGUITexture* which) {
90 FurnaceDX9Texture* t=(FurnaceDX9Texture*)which;
91 return t->format;
92 }
93
94 bool FurnaceGUIRenderDX9::isTextureValid(FurnaceGUITexture* which) {
95 if (which==NULL) return false;
96 FurnaceDX9Texture* t=(FurnaceDX9Texture*)which;
97 return (t->tex!=NULL);
98 }
99
100 bool FurnaceGUIRenderDX9::lockTexture(FurnaceGUITexture* which, void** data, int* pitch) {
101 FurnaceDX9Texture* t=(FurnaceDX9Texture*)which;
102 D3DLOCKED_RECT lockedRect;
103
104 HRESULT result=t->tex->LockRect(0,&lockedRect,NULL,D3DLOCK_DISCARD);
105
106 if (result!=D3D_OK) {
107 logW("could not lock texture!");
108 return false;
109 }
110
111 *data=lockedRect.pBits;
112 *pitch=lockedRect.Pitch;
113 return true;
114 }
115
116 bool FurnaceGUIRenderDX9::unlockTexture(FurnaceGUITexture* which) {
117 FurnaceDX9Texture* t=(FurnaceDX9Texture*)which;
118 HRESULT result=t->tex->UnlockRect(0);
119
120 if (result!=D3D_OK) {
121 logW("could not unlock texture!");
122 return false;
123 }
124
125 return true;
126 }
127
128 bool FurnaceGUIRenderDX9::updateTexture(FurnaceGUITexture* which, void* data, int pitch) {
129 FurnaceDX9Texture* t=(FurnaceDX9Texture*)which;
130 IDirect3DTexture9* crap=NULL;
131
132 if (t->texPre==NULL) {
133 // update by locking
134 if (!t->dynamic) {
135 logW("updating static texture but texPre does not exist!");
136 return false;
137 }
138 crap=t->tex;
139 } else {
140 // update by calling UpdateTexture
141 crap=t->texPre;
142 }
143
144 D3DLOCKED_RECT lockedRect;
145 HRESULT result=crap->LockRect(0,&lockedRect,NULL,D3DLOCK_DISCARD);
146 if (result!=D3D_OK) {
147 logW("could not update texture (lock)! %.8x",result);
148 return false;
149 }
150
151 if (lockedRect.Pitch==pitch) {
152 memcpy(lockedRect.pBits,data,pitch*t->height);
153 } else {
154 unsigned char* ucData=(unsigned char*)data;
155 unsigned char* d=(unsigned char*)lockedRect.pBits;
156 int srcPos=0;
157 int destPos=0;
158 for (int i=0; i<t->height; i++) {
159 memcpy(&d[destPos],&ucData[srcPos],pitch);
160 srcPos+=pitch;
161 destPos+=lockedRect.Pitch;
162 }
163 }
164
165 crap->UnlockRect(0);
166
167 if (t->texPre!=NULL) {
168 result=t->tex->AddDirtyRect(NULL);
169 if (result!=D3D_OK) {
170 logW("could not taint texture! %.8x",result);
171 }
172 result=device->UpdateTexture(t->texPre,t->tex);
173 if (result!=D3D_OK) {
174 logW("could not update texture! %.8x",result);
175 return false;
176 }
177 }
178
179 return true;
180 }
181
182 FurnaceGUITexture* FurnaceGUIRenderDX9::createTexture(bool dynamic, int width, int height, bool interpolate, FurnaceGUITextureFormat format) {
183 IDirect3DTexture9* tex=NULL;
184 IDirect3DTexture9* texPre=NULL;
185 int widthReal=width;
186 int heightReal=height;
187
188 if (format!=GUI_TEXFORMAT_ARGB32) {
189 logE("unsupported texture format!");
190 return NULL;
191 }
192
193 if ((widthReal&(widthReal-1))!=0) {
194 widthReal=1<<bsr(width);
195 }
196 if ((heightReal&(heightReal-1))!=0) {
197 heightReal=1<<bsr(height);
198 }
199 if (squareTex) {
200 if (widthReal>heightReal) {
201 heightReal=widthReal;
202 } else {
203 widthReal=heightReal;
204 }
205 }
206 logV("width: %d (requested)... %d (actual)",width,widthReal);
207 logV("height: %d (requested)... %d (actual)",height,heightReal);
208
209 if (!supportsDynamicTex) dynamic=false;
210
211 HRESULT result=device->CreateTexture(widthReal,heightReal,1,dynamic?D3DUSAGE_DYNAMIC:0,D3DFMT_A8R8G8B8,D3DPOOL_DEFAULT,&tex,NULL);
212
213 if (result!=D3D_OK) {
214 logW("could not create texture! %.8x",result);
215 return NULL;
216 }
217
218 if (!dynamic) {
219 HRESULT result=device->CreateTexture(widthReal,heightReal,1,0,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&texPre,NULL);
220
221 if (result!=D3D_OK) {
222 logW("could not create pre-texture! %.8x",result);
223 tex->Release();
224 return NULL;
225 }
226 }
227
228 FurnaceDX9Texture* ret=new FurnaceDX9Texture;
229 ret->width=width;
230 ret->height=height;
231 ret->widthReal=widthReal;
232 ret->heightReal=heightReal;
233 ret->tex=tex;
234 ret->texPre=texPre;
235 ret->dynamic=dynamic;
236 ret->format=format;
237
238 priv->texPool.push_back(ret);
239 return ret;
240 }
241
242 bool FurnaceGUIRenderDX9::destroyTexture(FurnaceGUITexture* which) {
243 FurnaceDX9Texture* t=(FurnaceDX9Texture*)which;
244 if (t->texPre!=NULL) t->texPre->Release();
245 if (t->tex!=NULL) t->tex->Release();
246 delete t;
247
248 for (size_t i=0; i<priv->texPool.size(); i++) {
249 if (priv->texPool[i]==t) {
250 priv->texPool.erase(priv->texPool.begin()+i);
251 break;
252 }
253 }
254 return true;
255 }
256
257 void FurnaceGUIRenderDX9::setTextureBlendMode(FurnaceGUITexture* which, FurnaceGUIBlendMode mode) {
258 }
259
260 void FurnaceGUIRenderDX9::setBlendMode(FurnaceGUIBlendMode mode) {
261 }
262
263 void FurnaceGUIRenderDX9::resized(const SDL_Event& ev) {
264 mustResize=true;
265 outW=ev.window.data1;
266 outH=ev.window.data2;
267 }
268
269 void FurnaceGUIRenderDX9::clear(ImVec4 color) {
270 device->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,ImGui::ColorConvertFloat4ToU32(color),0,0);
271 }
272
273 void FurnaceGUIRenderDX9::present() {
274 if (inScene) {
275 device->EndScene();
276 inScene=false;
277 }
278
279 HRESULT result=device->Present(NULL,NULL,NULL,NULL);
280
281 if (result==D3DERR_DEVICEREMOVED) {
282 logI("device is gone");
283 dead=true;
284 return;
285 } else if (result==D3DERR_DEVICEHUNG) {
286 logI("device has been executed");
287 dead=true;
288 return;
289 } else if (result==D3DERR_DEVICELOST) {
290 logI("device lost");
291 dead=true;
292 return;
293 } else if (result==D3DERR_DEVICENOTRESET) {
294 logI("device not reset");
295 dead=true;
296 return;
297 } else if (result!=D3D_OK) {
298 logE("DX9: Present ERROR! %x",result);
299 }
300
301 if (mustResize) {
302 logI("DX9: resizing buffers");
303 ImGui_ImplDX9_InvalidateDeviceObjects();
304
305 if (wipeBuf) {
306 wipeBuf->Release();
307 wipeBuf=NULL;
308 }
309
310 for (FurnaceDX9Texture* i: priv->texPool) {
311 if (i->tex) {
312 i->tex->Release();
313 i->tex=NULL;
314 }
315 if (i->texPre) {
316 i->texPre->Release();
317 i->texPre=NULL;
318 }
319 }
320
321 priv->present.BackBufferWidth=outW;
322 priv->present.BackBufferHeight=outH;
323 priv->present.BackBufferCount=1;
324 HRESULT result=device->Reset(&priv->present);
325 priv->present.BackBufferWidth=outW;
326 priv->present.BackBufferHeight=outH;
327 priv->present.BackBufferCount=1;
328 if (result==D3DERR_INVALIDCALL) {
329 logE("OH NO");
330 dead=true;
331 mustResize=false;
332 return;
333 }
334
335 ImGui_ImplDX9_CreateDeviceObjects();
336
337 result=device->CreateVertexBuffer(sizeof(WipeVertex)*4,0,D3DFVF_XYZ|D3DFVF_DIFFUSE,D3DPOOL_DEFAULT,&wipeBuf,NULL);
338
339 if (result!=D3D_OK) {
340 logE("could not create wipe buffer! %.8x",result);
341 }
342
343 mustResize=false;
344 }
345 }
346
347 void FurnaceGUIRenderDX9::newFrame() {
348 ImGui_ImplDX9_NewFrame();
349 }
350
351 bool FurnaceGUIRenderDX9::canVSync() {
352 return supportsVSync;
353 }
354
355 void FurnaceGUIRenderDX9::renderGUI() {
356 if (!inScene) {
357 HRESULT result=device->BeginScene();
358 if (result!=D3D_OK) {
359 logW("couldn't render GUI! %.8x",result);
360 return;
361 }
362 inScene=true;
363 }
364
365 ImGui_ImplDX9_RenderDrawData(ImGui::GetDrawData());
366 }
367
368 void FurnaceGUIRenderDX9::wipe(float alpha) {
369 if (wipeBuf==NULL) return;
370
371 logV("WIPE...");
372
373 if (!inScene) {
374 HRESULT result=device->BeginScene();
375 if (result!=D3D_OK) {
376 logW("couldn't render GUI! %.8x",result);
377 return;
378 }
379 inScene=true;
380 }
381
382 D3DVIEWPORT9 view;
383 view.X=0;
384 view.Y=0;
385 view.Width=outW;
386 view.Height=outH;
387 view.MinZ=0.0f;
388 view.MaxZ=1.0f;
389 HRESULT result=device->SetViewport(&view);
390 if (result!=D3D_OK) {
391 logW("could not set viewport! %.8x",result);
392 }
393
394 unsigned char alphaU=alpha*255.0f;
395 unsigned int color=alphaU<<24;
396
397 void* lockedData;
398 WipeVertex vertex[4];
399 vertex[0]=WipeVertex(0,0,0,color);
400 vertex[1]=WipeVertex(outW,0,0,color);
401 vertex[2]=WipeVertex(outW,outH,0,color);
402 vertex[3]=WipeVertex(0,outH,0,color);
403
404 result=wipeBuf->Lock(0,0,&lockedData,D3DLOCK_DISCARD);
405 if (result==D3D_OK) {
406 memcpy(lockedData,vertex,sizeof(WipeVertex)*4);
407 wipeBuf->Unlock();
408
409 result=device->SetRenderState(D3DRS_SCISSORTESTENABLE,FALSE);
410 if (result!=D3D_OK) {
411 logE("SHIT! scissor, %.8x",result);
412 }
413 result=device->SetTexture(0,NULL);
414 if (result!=D3D_OK) {
415 logE("SHIT! set texture, %.8x",result);
416 }
417 result=device->SetStreamSource(0,wipeBuf,0,sizeof(WipeVertex));
418 if (result!=D3D_OK) {
419 logE("SHIT! set stream source, %.8x",result);
420 }
421 result=device->SetTexture(0,NULL);
422 if (result!=D3D_OK) {
423 logE("SHIT! set texture, %.8x",result);
424 }
425 result=device->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE);
426 if (result!=D3D_OK) {
427 logE("SHIT! set FVF, %.8x",result);
428 }
429 result=device->DrawPrimitive(D3DPT_TRIANGLEFAN,0,2);
430 if (result!=D3D_OK) {
431 logE("SHIT! draw primitive, %.8x",result);
432 }
433 } else {
434 logE("SHIT! lock, %.8x",result);
435 }
436 }
437
438 bool FurnaceGUIRenderDX9::getOutputSize(int& w, int& h) {
439 w=outW;
440 h=outH;
441 return true;
442 }
443
444 int FurnaceGUIRenderDX9::getWindowFlags() {
445 return 0;
446 }
447
448 int FurnaceGUIRenderDX9::getMaxTextureWidth() {
449 return maxWidth;
450 }
451
452 int FurnaceGUIRenderDX9::getMaxTextureHeight() {
453 return maxHeight;
454 }
455
456 unsigned int FurnaceGUIRenderDX9::getTextureFormats() {
457 return GUI_TEXFORMAT_ARGB32;
458 }
459
460 const char* FurnaceGUIRenderDX9::getBackendName() {
461 return "DirectX 9";
462 }
463
464 const char* FurnaceGUIRenderDX9::getVendorName() {
465 return vendorName.c_str();
466 }
467
468 const char* FurnaceGUIRenderDX9::getDeviceName() {
469 return deviceName.c_str();
470 }
471
472 const char* FurnaceGUIRenderDX9::getAPIVersion() {
473 return apiVersion.c_str();
474 }
475
476 void FurnaceGUIRenderDX9::setSwapInterval(int swapInt) {
477 swapInterval=swapInt;
478 }
479
480 // I would use caps but...
481 bool FurnaceGUIRenderDX9::areTexturesSquare() {
482 return true;
483 }
484
485 void FurnaceGUIRenderDX9::preInit(const DivConfig& conf) {
486 }
487
488 bool FurnaceGUIRenderDX9::init(SDL_Window* win, int swapInt) {
489 D3DADAPTER_IDENTIFIER9 adapterInfo;
490 SDL_SysWMinfo sysWindow;
491
492 SDL_VERSION(&sysWindow.version);
493 if (SDL_GetWindowWMInfo(win,&sysWindow)==SDL_FALSE) {
494 logE("could not get window WM info! %s",SDL_GetError());
495 return false;
496 }
497 HWND window=(HWND)sysWindow.info.win.window;
498
499 iface=Direct3DCreate9(D3D_SDK_VERSION);
500 if (iface==NULL) {
501 logE("could not create Direct3D 9!");
502 return false;
503 }
504
505 priv=new FurnaceGUIRenderDX9Private;
506
507 SDL_GetWindowSize(win,&outW,&outH);
508
509 memset(&priv->present,0,sizeof(D3DPRESENT_PARAMETERS));
510 priv->present.Windowed=TRUE;
511 priv->present.SwapEffect=D3DSWAPEFFECT_DISCARD;
512 priv->present.BackBufferWidth=outW;
513 priv->present.BackBufferHeight=outH;
514 priv->present.BackBufferCount=1;
515 priv->present.BackBufferFormat=D3DFMT_UNKNOWN;
516 //priv->present.EnableAutoDepthStencil=TRUE;
517 //priv->present.AutoDepthStencilFormat=D3DFMT_D16;
518 if (swapInt>0) {
519 priv->present.PresentationInterval=D3DPRESENT_INTERVAL_ONE;
520 } else {
521 priv->present.PresentationInterval=D3DPRESENT_INTERVAL_IMMEDIATE;
522 }
523 priv->present.hDeviceWindow=window;
524
525 HRESULT result=iface->GetAdapterIdentifier(D3DADAPTER_DEFAULT,0,&adapterInfo);
526
527 if (result==D3D_OK) {
528 vendorName=fmt::sprintf("0x%.4X",adapterInfo.VendorId);
529 deviceName=fmt::sprintf("%s (%s)",adapterInfo.Description,adapterInfo.DeviceName);
530 apiVersion=fmt::sprintf("%.8X %.8X %s",adapterInfo.DriverVersion.HighPart,adapterInfo.DriverVersion.LowPart,adapterInfo.Driver);
531 } else {
532 logW("could not get adapter info! %.8x",result);
533 }
534
535 result=iface->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,window,D3DCREATE_HARDWARE_VERTEXPROCESSING,&priv->present,&device);
536
537 if (result!=D3D_OK) {
538 logW("no hardware vertex processing!");
539 result=iface->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,window,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&priv->present,&device);
540 if (result!=D3D_OK) {
541 logE("could not create device! %.8x",result);
542 iface->Release();
543 iface=NULL;
544 return false;
545 }
546 }
547
548 D3DCAPS9 caps;
549
550 result=device->GetDeviceCaps(&caps);
551
552 if (result==D3D_OK) {
553 supportsDynamicTex=(caps.Caps2&D3DCAPS2_DYNAMICTEXTURES);
554 squareTex=(caps.TextureCaps&D3DPTEXTURECAPS_SQUAREONLY);
555 supportsVSync=(caps.PresentationIntervals&D3DPRESENT_INTERVAL_ONE);
556 maxWidth=caps.MaxTextureWidth;
557 maxHeight=caps.MaxTextureHeight;
558
559 if (!supportsDynamicTex) {
560 logI("no support for dynamic textures");
561 }
562 if (squareTex) {
563 logI("square textures only");
564 }
565 }
566
567 result=device->CreateVertexBuffer(sizeof(WipeVertex)*4,0,D3DFVF_XYZ|D3DFVF_DIFFUSE,D3DPOOL_DEFAULT,&wipeBuf,NULL);
568
569 if (result!=D3D_OK) {
570 logE("could not create wipe buffer! %.8x",result);
571 }
572
573 return true;
574 }
575
576 void FurnaceGUIRenderDX9::initGUI(SDL_Window* win) {
577 ImGui_ImplSDL2_InitForD3D(win);
578 ImGui_ImplDX9_Init(device);
579 }
580
581 bool FurnaceGUIRenderDX9::quit() {
582 if (wipeBuf) {
583 wipeBuf->Release();
584 wipeBuf=NULL;
585 }
586 if (device) {
587 device->Release();
588 device=NULL;
589 }
590 if (iface) {
591 iface->Release();
592 iface=NULL;
593 }
594 priv->texPool.clear();
595 dead=false;
596 return true;
597 }
598
599 void FurnaceGUIRenderDX9::quitGUI() {
600 ImGui_ImplDX9_Shutdown();
601 }
602
603 bool FurnaceGUIRenderDX9::isDead() {
604 return dead;
605 }