| 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_D3D11 |
| 21 |
#include "renderDX11.h" |
| 22 |
#include <SDL_syswm.h> |
| 23 |
#include "backends/imgui_impl_dx11.h" |
| 24 |
#include "../../ta-log.h" |
| 25 |
#include "../../utfutils.h" |
| 26 |
|
| 27 |
typedef HRESULT (__stdcall *D3DCompile_t)(LPCVOID,SIZE_T,LPCSTR,D3D_SHADER_MACRO*,ID3DInclude*,LPCSTR,LPCSTR,UINT,UINT,ID3DBlob**,ID3DBlob*); |
| 28 |
|
| 29 |
const char* shD3D11_wipe_srcV= |
| 30 |
"cbuffer WipeUniform: register(b0) {\n" |
| 31 |
" float alpha;\n" |
| 32 |
" float padding1;\n" |
| 33 |
" float padding2;\n" |
| 34 |
" float padding3;\n" |
| 35 |
" float4 padding4;\n" |
| 36 |
"};\n" |
| 37 |
"\n" |
| 38 |
"struct vsInput {\n" |
| 39 |
" float4 pos: POSITION;\n" |
| 40 |
"};\n" |
| 41 |
"\n" |
| 42 |
"struct fsInput {\n" |
| 43 |
" float4 pos: SV_POSITION;\n" |
| 44 |
" float4 color: COLOR0;\n" |
| 45 |
"};\n" |
| 46 |
"\n" |
| 47 |
"fsInput main(vsInput input) {\n" |
| 48 |
" fsInput output;\n" |
| 49 |
" output.pos=input.pos;\n" |
| 50 |
" output.color=float4(0.0f,0.0f,0.0f,alpha);\n" |
| 51 |
" return output;\n" |
| 52 |
"}"; |
| 53 |
|
| 54 |
const char* shD3D11_wipe_srcF= |
| 55 |
"struct fsInput {\n" |
| 56 |
" float4 pos: SV_POSITION;\n" |
| 57 |
" float4 color: COLOR0;\n" |
| 58 |
"};\n" |
| 59 |
"\n" |
| 60 |
"float4 main(fsInput input): SV_Target {\n" |
| 61 |
" return input.color;\n" |
| 62 |
"}"; |
| 63 |
|
| 64 |
const D3D11_INPUT_ELEMENT_DESC shD3D11_wipe_inputLayout={ |
| 65 |
"POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 |
| 66 |
}; |
| 67 |
|
| 68 |
const D3D_FEATURE_LEVEL possibleFeatureLevels[2]={ |
| 69 |
D3D_FEATURE_LEVEL_11_0, |
| 70 |
D3D_FEATURE_LEVEL_10_0 |
| 71 |
}; |
| 72 |
|
| 73 |
class FurnaceDXTexture: public FurnaceGUITexture { |
| 74 |
public: |
| 75 |
ID3D11Texture2D* tex; |
| 76 |
ID3D11ShaderResourceView* view; |
| 77 |
int width, height; |
| 78 |
FurnaceGUITextureFormat format; |
| 79 |
unsigned char* lockedData; |
| 80 |
bool dynamic; |
| 81 |
FurnaceDXTexture(): |
| 82 |
tex(NULL), |
| 83 |
view(NULL), |
| 84 |
width(0), |
| 85 |
height(0), |
| 86 |
format(GUI_TEXFORMAT_UNKNOWN), |
| 87 |
lockedData(NULL), |
| 88 |
dynamic(false) {} |
| 89 |
}; |
| 90 |
|
| 91 |
bool FurnaceGUIRenderDX11::destroyRenderTarget() { |
| 92 |
if (renderTarget!=NULL) { |
| 93 |
renderTarget->Release(); |
| 94 |
renderTarget=NULL; |
| 95 |
return true; |
| 96 |
} |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
bool FurnaceGUIRenderDX11::createRenderTarget() { |
| 101 |
ID3D11Texture2D* screen=NULL; |
| 102 |
HRESULT result; |
| 103 |
|
| 104 |
destroyRenderTarget(); |
| 105 |
|
| 106 |
if (swapchain==NULL || device==NULL) { |
| 107 |
logW("createRenderTarget: swapchain or device are NULL!"); |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
DXGI_SWAP_CHAIN_DESC chainDesc; |
| 112 |
memset(&chainDesc,0,sizeof(chainDesc)); |
| 113 |
if (swapchain->GetDesc(&chainDesc)!=S_OK) { |
| 114 |
logW("createRenderTarget: could not get swapchain desc!"); |
| 115 |
} else { |
| 116 |
outW=chainDesc.BufferDesc.Width; |
| 117 |
outH=chainDesc.BufferDesc.Height; |
| 118 |
logI("DX11: buffer desc sizes: %d, %d",chainDesc.BufferDesc.Width,chainDesc.BufferDesc.Height); |
| 119 |
} |
| 120 |
|
| 121 |
result=swapchain->GetBuffer(0,IID_PPV_ARGS(&screen)); |
| 122 |
if (result!=S_OK) { |
| 123 |
logW("createRenderTarget: could not get buffer! %.8x",result); |
| 124 |
return false; |
| 125 |
} |
| 126 |
if (screen==NULL) { |
| 127 |
logW("createRenderTarget: screen is null!"); |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
result=device->CreateRenderTargetView(screen,NULL,&renderTarget); |
| 132 |
if (result!=S_OK) { |
| 133 |
logW("createRenderTarget: could not create render target view! %.8x",result); |
| 134 |
screen->Release(); |
| 135 |
return false; |
| 136 |
} |
| 137 |
if (renderTarget==NULL) { |
| 138 |
logW("createRenderTarget: what the hell the render target is null?"); |
| 139 |
screen->Release(); |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
screen->Release(); |
| 144 |
return true; |
| 145 |
} |
| 146 |
|
| 147 |
ImTextureID FurnaceGUIRenderDX11::getTextureID(FurnaceGUITexture* which) { |
| 148 |
FurnaceDXTexture* t=(FurnaceDXTexture*)which; |
| 149 |
return (ImTextureID)t->view; |
| 150 |
} |
| 151 |
|
| 152 |
FurnaceGUITextureFormat FurnaceGUIRenderDX11::getTextureFormat(FurnaceGUITexture* which) { |
| 153 |
FurnaceDXTexture* t=(FurnaceDXTexture*)which; |
| 154 |
return t->format; |
| 155 |
} |
| 156 |
|
| 157 |
bool FurnaceGUIRenderDX11::lockTexture(FurnaceGUITexture* which, void** data, int* pitch) { |
| 158 |
FurnaceDXTexture* t=(FurnaceDXTexture*)which; |
| 159 |
if (t->lockedData!=NULL) return false; |
| 160 |
|
| 161 |
D3D11_MAPPED_SUBRESOURCE mappedRes; |
| 162 |
memset(&mappedRes,0,sizeof(mappedRes)); |
| 163 |
|
| 164 |
HRESULT result=context->Map(t->tex,D3D11CalcSubresource(0,0,1),D3D11_MAP_WRITE_DISCARD,0,&mappedRes); |
| 165 |
if (result!=S_OK) { |
| 166 |
logW("could not map texture! %.8x",result); |
| 167 |
return false; |
| 168 |
} |
| 169 |
t->lockedData=(unsigned char*)mappedRes.pData; |
| 170 |
*data=mappedRes.pData; |
| 171 |
*pitch=mappedRes.RowPitch; |
| 172 |
|
| 173 |
logV("texture locked... pitch: %d",mappedRes.RowPitch); |
| 174 |
return true; |
| 175 |
} |
| 176 |
|
| 177 |
bool FurnaceGUIRenderDX11::unlockTexture(FurnaceGUITexture* which) { |
| 178 |
FurnaceDXTexture* t=(FurnaceDXTexture*)which; |
| 179 |
if (t->lockedData==NULL) return false; |
| 180 |
context->Unmap(t->tex,D3D11CalcSubresource(0,0,1)); |
| 181 |
t->lockedData=NULL; |
| 182 |
return true; |
| 183 |
} |
| 184 |
|
| 185 |
bool FurnaceGUIRenderDX11::updateTexture(FurnaceGUITexture* which, void* data, int pitch) { |
| 186 |
FurnaceDXTexture* t=(FurnaceDXTexture*)which; |
| 187 |
if (t->dynamic) { |
| 188 |
unsigned char* d=NULL; |
| 189 |
int p=0; |
| 190 |
if (!lockTexture(t,(void**)&d,&p)) return false; |
| 191 |
if (p==pitch) { |
| 192 |
memcpy(d,data,p*t->height); |
| 193 |
} else { |
| 194 |
unsigned char* ucData=(unsigned char*)data; |
| 195 |
int srcPos=0; |
| 196 |
int destPos=0; |
| 197 |
for (int i=0; i<t->height; i++) { |
| 198 |
memcpy(&d[destPos],&ucData[srcPos],pitch); |
| 199 |
srcPos+=pitch; |
| 200 |
destPos+=p; |
| 201 |
} |
| 202 |
} |
| 203 |
unlockTexture(t); |
| 204 |
} else { |
| 205 |
context->UpdateSubresource(t->tex,D3D11CalcSubresource(0,0,1),NULL,data,pitch,pitch*t->height); |
| 206 |
} |
| 207 |
return true; |
| 208 |
} |
| 209 |
|
| 210 |
FurnaceGUITexture* FurnaceGUIRenderDX11::createTexture(bool dynamic, int width, int height, bool interpolate, FurnaceGUITextureFormat format) { |
| 211 |
D3D11_TEXTURE2D_DESC texDesc; |
| 212 |
D3D11_SHADER_RESOURCE_VIEW_DESC viewDesc; |
| 213 |
ID3D11Texture2D* tex=NULL; |
| 214 |
ID3D11ShaderResourceView* view=NULL; |
| 215 |
HRESULT result; |
| 216 |
|
| 217 |
memset(&texDesc,0,sizeof(texDesc)); |
| 218 |
memset(&viewDesc,0,sizeof(viewDesc)); |
| 219 |
|
| 220 |
texDesc.Width=width; |
| 221 |
texDesc.Height=height; |
| 222 |
texDesc.MipLevels=1; |
| 223 |
texDesc.ArraySize=1; |
| 224 |
switch (format) { |
| 225 |
case GUI_TEXFORMAT_ABGR32: |
| 226 |
texDesc.Format=DXGI_FORMAT_R8G8B8A8_UNORM; |
| 227 |
break; |
| 228 |
case GUI_TEXFORMAT_ARGB32: |
| 229 |
texDesc.Format=DXGI_FORMAT_B8G8R8A8_UNORM; |
| 230 |
break; |
| 231 |
default: |
| 232 |
logE("unsupported texture format!"); |
| 233 |
return NULL; |
| 234 |
} |
| 235 |
texDesc.SampleDesc.Count=1; |
| 236 |
texDesc.SampleDesc.Quality=0; |
| 237 |
texDesc.Usage=dynamic?D3D11_USAGE_DYNAMIC:D3D11_USAGE_DEFAULT; |
| 238 |
texDesc.BindFlags=D3D11_BIND_SHADER_RESOURCE; |
| 239 |
texDesc.CPUAccessFlags=dynamic?D3D11_CPU_ACCESS_WRITE:0; |
| 240 |
texDesc.MiscFlags=0; |
| 241 |
|
| 242 |
result=device->CreateTexture2D(&texDesc,NULL,&tex); |
| 243 |
if (result!=S_OK) { |
| 244 |
logW("could not create texture! %.8x",result); |
| 245 |
return NULL; |
| 246 |
} |
| 247 |
|
| 248 |
viewDesc.Format=texDesc.Format=texDesc.Format; |
| 249 |
viewDesc.ViewDimension=D3D11_SRV_DIMENSION_TEXTURE2D; |
| 250 |
viewDesc.Texture2D.MostDetailedMip=0; |
| 251 |
viewDesc.Texture2D.MipLevels=texDesc.MipLevels; |
| 252 |
|
| 253 |
result=device->CreateShaderResourceView(tex,&viewDesc,&view); |
| 254 |
if (result!=S_OK) { |
| 255 |
logW("could not create texture view! %.8x",result); |
| 256 |
tex->Release(); |
| 257 |
return NULL; |
| 258 |
} |
| 259 |
|
| 260 |
FurnaceDXTexture* ret=new FurnaceDXTexture; |
| 261 |
ret->width=width; |
| 262 |
ret->height=height; |
| 263 |
ret->tex=tex; |
| 264 |
ret->view=view; |
| 265 |
ret->dynamic=dynamic; |
| 266 |
ret->format=format; |
| 267 |
return ret; |
| 268 |
} |
| 269 |
|
| 270 |
bool FurnaceGUIRenderDX11::destroyTexture(FurnaceGUITexture* which) { |
| 271 |
FurnaceDXTexture* t=(FurnaceDXTexture*)which; |
| 272 |
t->view->Release(); |
| 273 |
t->tex->Release(); |
| 274 |
delete t; |
| 275 |
return true; |
| 276 |
} |
| 277 |
|
| 278 |
void FurnaceGUIRenderDX11::setTextureBlendMode(FurnaceGUITexture* which, FurnaceGUIBlendMode mode) { |
| 279 |
} |
| 280 |
|
| 281 |
void FurnaceGUIRenderDX11::setBlendMode(FurnaceGUIBlendMode mode) { |
| 282 |
} |
| 283 |
|
| 284 |
void FurnaceGUIRenderDX11::resized(const SDL_Event& ev) { |
| 285 |
destroyRenderTarget(); |
| 286 |
logI("DX11: resizing buffers"); |
| 287 |
HRESULT result=swapchain->ResizeBuffers(0,(unsigned int)ev.window.data1,(unsigned int)ev.window.data2,DXGI_FORMAT_UNKNOWN,DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH); |
| 288 |
if (result!=S_OK) { |
| 289 |
if (result==DXGI_ERROR_DEVICE_REMOVED || result==DXGI_ERROR_DEVICE_RESET) { |
| 290 |
dead=true; |
| 291 |
} |
| 292 |
logW("error while resizing swapchain buffers! %.8x",result); |
| 293 |
} |
| 294 |
if (!dead) { |
| 295 |
createRenderTarget(); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
void FurnaceGUIRenderDX11::clear(ImVec4 color) { |
| 300 |
float floatColor[4]={ |
| 301 |
color.x*color.w, |
| 302 |
color.y*color.w, |
| 303 |
color.z*color.w, |
| 304 |
color.w, |
| 305 |
}; |
| 306 |
|
| 307 |
context->OMSetRenderTargets(1,&renderTarget,NULL); |
| 308 |
context->ClearRenderTargetView(renderTarget,floatColor); |
| 309 |
} |
| 310 |
|
| 311 |
void FurnaceGUIRenderDX11::newFrame() { |
| 312 |
ImGui_ImplDX11_NewFrame(); |
| 313 |
} |
| 314 |
|
| 315 |
bool FurnaceGUIRenderDX11::canVSync() { |
| 316 |
// TODO: find out how to retrieve VSync status |
| 317 |
return true; |
| 318 |
} |
| 319 |
|
| 320 |
void FurnaceGUIRenderDX11::renderGUI() { |
| 321 |
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData()); |
| 322 |
} |
| 323 |
|
| 324 |
const float blendFactor[4]={ |
| 325 |
1.0f, 1.0f, 1.0f, 1.0f |
| 326 |
}; |
| 327 |
|
| 328 |
void FurnaceGUIRenderDX11::wipe(float alpha) { |
| 329 |
D3D11_VIEWPORT viewPort; |
| 330 |
unsigned int strides=4*sizeof(float); |
| 331 |
unsigned int offsets=0; |
| 332 |
|
| 333 |
memset(&viewPort,0,sizeof(viewPort)); |
| 334 |
viewPort.TopLeftX=0.0f; |
| 335 |
viewPort.TopLeftY=0.0f; |
| 336 |
viewPort.Width=outW; |
| 337 |
viewPort.Height=outH; |
| 338 |
viewPort.MinDepth=0.0f; |
| 339 |
viewPort.MaxDepth=1.0f; |
| 340 |
|
| 341 |
D3D11_MAPPED_SUBRESOURCE mappedUniform; |
| 342 |
if (context->Map(sh_wipe_uniform,0,D3D11_MAP_WRITE_DISCARD,0,&mappedUniform)!=S_OK) { |
| 343 |
logW("could not map constant"); |
| 344 |
} |
| 345 |
WipeUniform* sh_wipe_uniformState=(WipeUniform*)mappedUniform.pData; |
| 346 |
sh_wipe_uniformState->alpha=alpha; |
| 347 |
context->Unmap(sh_wipe_uniform,0); |
| 348 |
|
| 349 |
context->RSSetViewports(1,&viewPort); |
| 350 |
context->RSSetState(rsState); |
| 351 |
|
| 352 |
context->OMSetBlendState(omBlendState,blendFactor,0xffffffff); |
| 353 |
context->IASetInputLayout(sh_wipe_inputLayout); |
| 354 |
context->IASetVertexBuffers(0,1,&quadVertex,&strides,&offsets); |
| 355 |
context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); |
| 356 |
context->VSSetShader(sh_wipe_vertex,NULL,0); |
| 357 |
context->VSSetConstantBuffers(0,1,&sh_wipe_uniform); |
| 358 |
context->PSSetShader(sh_wipe_fragment,NULL,0); |
| 359 |
|
| 360 |
context->Draw(4,0); |
| 361 |
} |
| 362 |
|
| 363 |
void FurnaceGUIRenderDX11::present() { |
| 364 |
HRESULT result=swapchain->Present(swapInterval,0); |
| 365 |
if (result==DXGI_ERROR_DEVICE_REMOVED || result==DXGI_ERROR_DEVICE_RESET) { |
| 366 |
dead=true; |
| 367 |
} else if (result!=S_OK && result!=DXGI_STATUS_OCCLUDED) { |
| 368 |
logE("DX11: present failed! %.8x",result); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
bool FurnaceGUIRenderDX11::getOutputSize(int& w, int& h) { |
| 373 |
w=outW; |
| 374 |
h=outH; |
| 375 |
return true; |
| 376 |
} |
| 377 |
|
| 378 |
int FurnaceGUIRenderDX11::getWindowFlags() { |
| 379 |
return 0; |
| 380 |
} |
| 381 |
|
| 382 |
int FurnaceGUIRenderDX11::getMaxTextureWidth() { |
| 383 |
return maxWidth; |
| 384 |
} |
| 385 |
|
| 386 |
int FurnaceGUIRenderDX11::getMaxTextureHeight() { |
| 387 |
return maxHeight; |
| 388 |
} |
| 389 |
|
| 390 |
unsigned int FurnaceGUIRenderDX11::getTextureFormats() { |
| 391 |
return GUI_TEXFORMAT_ABGR32|GUI_TEXFORMAT_ARGB32; |
| 392 |
} |
| 393 |
|
| 394 |
const char* FurnaceGUIRenderDX11::getBackendName() { |
| 395 |
return "DirectX 11"; |
| 396 |
} |
| 397 |
|
| 398 |
const char* FurnaceGUIRenderDX11::getVendorName() { |
| 399 |
return vendorName.c_str(); |
| 400 |
} |
| 401 |
|
| 402 |
const char* FurnaceGUIRenderDX11::getDeviceName() { |
| 403 |
return deviceName.c_str(); |
| 404 |
} |
| 405 |
|
| 406 |
const char* FurnaceGUIRenderDX11::getAPIVersion() { |
| 407 |
return apiVersion.c_str(); |
| 408 |
} |
| 409 |
|
| 410 |
void FurnaceGUIRenderDX11::setSwapInterval(int swapInt) { |
| 411 |
swapInterval=swapInt; |
| 412 |
} |
| 413 |
|
| 414 |
void FurnaceGUIRenderDX11::preInit(const DivConfig& conf) { |
| 415 |
} |
| 416 |
|
| 417 |
const float wipeVertices[4][4]={ |
| 418 |
{-1.0, -1.0, 0.0, 1.0}, |
| 419 |
{ 1.0, -1.0, 0.0, 1.0}, |
| 420 |
{-1.0, 1.0, 0.0, 1.0}, |
| 421 |
{ 1.0, 1.0, 0.0, 1.0} |
| 422 |
}; |
| 423 |
|
| 424 |
bool FurnaceGUIRenderDX11::init(SDL_Window* win, int swapInt) { |
| 425 |
SDL_SysWMinfo sysWindow; |
| 426 |
D3D_FEATURE_LEVEL featureLevel; |
| 427 |
|
| 428 |
SDL_VERSION(&sysWindow.version); |
| 429 |
if (SDL_GetWindowWMInfo(win,&sysWindow)==SDL_FALSE) { |
| 430 |
logE("could not get window WM info! %s",SDL_GetError()); |
| 431 |
return false; |
| 432 |
} |
| 433 |
HWND window=(HWND)sysWindow.info.win.window; |
| 434 |
|
| 435 |
// prepare swapchain |
| 436 |
swapInterval=swapInt; |
| 437 |
|
| 438 |
DXGI_SWAP_CHAIN_DESC chainDesc; |
| 439 |
memset(&chainDesc,0,sizeof(chainDesc)); |
| 440 |
chainDesc.BufferDesc.Width=0; |
| 441 |
chainDesc.BufferDesc.Height=0; |
| 442 |
chainDesc.BufferDesc.Format=DXGI_FORMAT_R8G8B8A8_UNORM; |
| 443 |
chainDesc.BufferDesc.RefreshRate.Numerator=60; |
| 444 |
chainDesc.BufferDesc.RefreshRate.Denominator=1; |
| 445 |
chainDesc.SampleDesc.Count=1; |
| 446 |
chainDesc.SampleDesc.Quality=0; |
| 447 |
chainDesc.BufferUsage=DXGI_USAGE_RENDER_TARGET_OUTPUT; |
| 448 |
chainDesc.BufferCount=2; |
| 449 |
chainDesc.OutputWindow=window; |
| 450 |
chainDesc.Windowed=TRUE; // TODO: what if we're in full screen mode? |
| 451 |
chainDesc.SwapEffect=DXGI_SWAP_EFFECT_DISCARD; |
| 452 |
chainDesc.Flags=DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; |
| 453 |
|
| 454 |
// initialize |
| 455 |
HRESULT result=D3D11CreateDeviceAndSwapChain(NULL,D3D_DRIVER_TYPE_HARDWARE,NULL,0,possibleFeatureLevels,2,D3D11_SDK_VERSION,&chainDesc,&swapchain,&device,&featureLevel,&context); |
| 456 |
if (result!=S_OK) { |
| 457 |
logE("could not create device and/or swap chain! %.8x",result); |
| 458 |
return false; |
| 459 |
} |
| 460 |
|
| 461 |
IDXGIDevice* giDevice=NULL; |
| 462 |
IDXGIAdapter* adapter=NULL; |
| 463 |
|
| 464 |
result=device->QueryInterface(__uuidof(IDXGIDevice),(void**)&giDevice); |
| 465 |
if (result==S_OK) { |
| 466 |
result=giDevice->GetAdapter(&adapter); |
| 467 |
if (result==S_OK) { |
| 468 |
DXGI_ADAPTER_DESC adapterDesc; |
| 469 |
|
| 470 |
result=adapter->GetDesc(&adapterDesc); |
| 471 |
if (result!=S_OK) { |
| 472 |
logE("could not get adapter info! %.8x",result); |
| 473 |
} else { |
| 474 |
deviceName=utf16To8(adapterDesc.Description); |
| 475 |
vendorName=fmt::sprintf("%.4x:%.4x",adapterDesc.VendorId,adapterDesc.DeviceId); |
| 476 |
logV("device: %s",deviceName); |
| 477 |
} |
| 478 |
} else { |
| 479 |
logE("could not get adapter! %.8x",result); |
| 480 |
logE("won't be able to get adapter info..."); |
| 481 |
} |
| 482 |
} else { |
| 483 |
logE("could not query interface! %.8x",result); |
| 484 |
logE("won't be able to get adapter info..."); |
| 485 |
} |
| 486 |
|
| 487 |
if (featureLevel>=0xb000) { |
| 488 |
maxWidth=16384; |
| 489 |
maxHeight=16384; |
| 490 |
} else if (featureLevel>=0xa000) { |
| 491 |
maxWidth=8192; |
| 492 |
maxHeight=8192; |
| 493 |
} else { |
| 494 |
maxWidth=4096; |
| 495 |
maxHeight=4096; |
| 496 |
} |
| 497 |
apiVersion=fmt::sprintf("%d.%d",((int)featureLevel)>>12,((int)featureLevel)>>8); |
| 498 |
|
| 499 |
// https://github.com/ocornut/imgui/pull/638 |
| 500 |
D3DCompile_t D3DCompile=NULL; |
| 501 |
char dllBuffer[20]; |
| 502 |
for (int i=47; (i>30 && !D3DCompile); i--) { |
| 503 |
snprintf(dllBuffer,20,"d3dcompiler_%d.dll",i); |
| 504 |
HMODULE hDll=LoadLibraryA(dllBuffer); |
| 505 |
if (hDll) { |
| 506 |
D3DCompile=(D3DCompile_t)GetProcAddress(hDll,"D3DCompile"); |
| 507 |
} |
| 508 |
} |
| 509 |
if (!D3DCompile) { |
| 510 |
logE("could not find D3DCompile!"); |
| 511 |
return false; |
| 512 |
} |
| 513 |
|
| 514 |
// create wipe shader |
| 515 |
ID3DBlob* wipeBlobV=NULL; |
| 516 |
ID3DBlob* wipeBlobF=NULL; |
| 517 |
D3D11_BUFFER_DESC wipeConstantDesc; |
| 518 |
|
| 519 |
result=D3DCompile(shD3D11_wipe_srcV,strlen(shD3D11_wipe_srcV),NULL,NULL,NULL,"main","vs_4_0",0,0,&wipeBlobV,NULL); |
| 520 |
if (result!=S_OK) { |
| 521 |
logE("could not compile vertex shader! %.8x",result); |
| 522 |
return false; |
| 523 |
} |
| 524 |
result=D3DCompile(shD3D11_wipe_srcF,strlen(shD3D11_wipe_srcF),NULL,NULL,NULL,"main","ps_4_0",0,0,&wipeBlobF,NULL); |
| 525 |
if (result!=S_OK) { |
| 526 |
logE("could not compile pixel shader! %.8x",result); |
| 527 |
return false; |
| 528 |
} |
| 529 |
|
| 530 |
result=device->CreateVertexShader(wipeBlobV->GetBufferPointer(),wipeBlobV->GetBufferSize(),NULL,&sh_wipe_vertex); |
| 531 |
if (result!=S_OK) { |
| 532 |
logE("could not create vertex shader! %.8x",result); |
| 533 |
return false; |
| 534 |
} |
| 535 |
result=device->CreatePixelShader(wipeBlobF->GetBufferPointer(),wipeBlobF->GetBufferSize(),NULL,&sh_wipe_fragment); |
| 536 |
if (result!=S_OK) { |
| 537 |
logE("could not create pixel shader! %.8x",result); |
| 538 |
return false; |
| 539 |
} |
| 540 |
|
| 541 |
result=device->CreateInputLayout(&shD3D11_wipe_inputLayout,1,wipeBlobV->GetBufferPointer(),wipeBlobV->GetBufferSize(),&sh_wipe_inputLayout); |
| 542 |
if (result!=S_OK) { |
| 543 |
logE("could not create input layout! %.8x",result); |
| 544 |
return false; |
| 545 |
} |
| 546 |
|
| 547 |
memset(&wipeConstantDesc,0,sizeof(wipeConstantDesc)); |
| 548 |
wipeConstantDesc.ByteWidth=sizeof(WipeUniform); |
| 549 |
wipeConstantDesc.Usage=D3D11_USAGE_DYNAMIC; |
| 550 |
wipeConstantDesc.BindFlags=D3D11_BIND_CONSTANT_BUFFER; |
| 551 |
wipeConstantDesc.CPUAccessFlags=D3D11_CPU_ACCESS_WRITE; |
| 552 |
wipeConstantDesc.MiscFlags=0; |
| 553 |
wipeConstantDesc.StructureByteStride=0; |
| 554 |
|
| 555 |
result=device->CreateBuffer(&wipeConstantDesc,NULL,&sh_wipe_uniform); |
| 556 |
if (result!=S_OK) { |
| 557 |
logE("could not create constant buffer! %.8x",result); |
| 558 |
return false; |
| 559 |
} |
| 560 |
|
| 561 |
// create wipe vertices |
| 562 |
D3D11_BUFFER_DESC vertexDesc; |
| 563 |
D3D11_SUBRESOURCE_DATA vertexRes; |
| 564 |
|
| 565 |
memset(&vertexDesc,0,sizeof(vertexDesc)); |
| 566 |
memset(&vertexRes,0,sizeof(vertexRes)); |
| 567 |
|
| 568 |
vertexDesc.ByteWidth=4*4*sizeof(float); |
| 569 |
vertexDesc.Usage=D3D11_USAGE_DEFAULT; |
| 570 |
vertexDesc.BindFlags=D3D11_BIND_VERTEX_BUFFER; |
| 571 |
vertexDesc.CPUAccessFlags=0; |
| 572 |
vertexDesc.MiscFlags=0; |
| 573 |
vertexDesc.StructureByteStride=0; |
| 574 |
|
| 575 |
vertexRes.pSysMem=wipeVertices; |
| 576 |
vertexRes.SysMemPitch=0; |
| 577 |
vertexRes.SysMemSlicePitch=0; |
| 578 |
|
| 579 |
result=device->CreateBuffer(&vertexDesc,&vertexRes,&quadVertex); |
| 580 |
if (result!=S_OK) { |
| 581 |
logE("could not create vertex buffer! %.8x",result); |
| 582 |
return false; |
| 583 |
} |
| 584 |
|
| 585 |
// initialize the rest |
| 586 |
D3D11_RASTERIZER_DESC rasterDesc; |
| 587 |
D3D11_BLEND_DESC blendDesc; |
| 588 |
|
| 589 |
memset(&rasterDesc,0,sizeof(rasterDesc)); |
| 590 |
memset(&blendDesc,0,sizeof(blendDesc)); |
| 591 |
|
| 592 |
rasterDesc.FillMode=D3D11_FILL_SOLID; |
| 593 |
rasterDesc.CullMode=D3D11_CULL_NONE; |
| 594 |
rasterDesc.FrontCounterClockwise=false; |
| 595 |
rasterDesc.DepthBias=0; |
| 596 |
rasterDesc.DepthBiasClamp=0.0f; |
| 597 |
rasterDesc.SlopeScaledDepthBias=0.0f; |
| 598 |
rasterDesc.DepthClipEnable=false; |
| 599 |
rasterDesc.ScissorEnable=false; |
| 600 |
rasterDesc.MultisampleEnable=false; |
| 601 |
rasterDesc.AntialiasedLineEnable=false; |
| 602 |
result=device->CreateRasterizerState(&rasterDesc,&rsState); |
| 603 |
if (result!=S_OK) { |
| 604 |
logE("could not create rasterizer state! %.8x",result); |
| 605 |
return false; |
| 606 |
} |
| 607 |
|
| 608 |
blendDesc.AlphaToCoverageEnable=false; |
| 609 |
blendDesc.IndependentBlendEnable=false; |
| 610 |
blendDesc.RenderTarget[0].BlendEnable=true; |
| 611 |
blendDesc.RenderTarget[0].SrcBlend=D3D11_BLEND_SRC_ALPHA; |
| 612 |
blendDesc.RenderTarget[0].DestBlend=D3D11_BLEND_INV_SRC_ALPHA; |
| 613 |
blendDesc.RenderTarget[0].BlendOp=D3D11_BLEND_OP_ADD; |
| 614 |
blendDesc.RenderTarget[0].SrcBlendAlpha=D3D11_BLEND_ONE; |
| 615 |
blendDesc.RenderTarget[0].DestBlendAlpha=D3D11_BLEND_INV_SRC_ALPHA; |
| 616 |
blendDesc.RenderTarget[0].BlendOpAlpha=D3D11_BLEND_OP_ADD; |
| 617 |
blendDesc.RenderTarget[0].RenderTargetWriteMask=D3D11_COLOR_WRITE_ENABLE_ALL; |
| 618 |
result=device->CreateBlendState(&blendDesc,&omBlendState); |
| 619 |
if (result!=S_OK) { |
| 620 |
logE("could not create blend state! %.8x",result); |
| 621 |
return false; |
| 622 |
} |
| 623 |
|
| 624 |
createRenderTarget(); |
| 625 |
return true; |
| 626 |
} |
| 627 |
|
| 628 |
void FurnaceGUIRenderDX11::initGUI(SDL_Window* win) { |
| 629 |
ImGui_ImplSDL2_InitForD3D(win); |
| 630 |
ImGui_ImplDX11_Init(device,context); |
| 631 |
} |
| 632 |
|
| 633 |
bool FurnaceGUIRenderDX11::quit() { |
| 634 |
destroyRenderTarget(); |
| 635 |
|
| 636 |
if (swapchain!=NULL) { |
| 637 |
swapchain->Release(); |
| 638 |
swapchain=NULL; |
| 639 |
} |
| 640 |
if (context!=NULL) { |
| 641 |
context->Release(); |
| 642 |
context=NULL; |
| 643 |
} |
| 644 |
if (device!=NULL) { |
| 645 |
device->Release(); |
| 646 |
device=NULL; |
| 647 |
} |
| 648 |
|
| 649 |
dead=false; |
| 650 |
return true; |
| 651 |
} |
| 652 |
|
| 653 |
void FurnaceGUIRenderDX11::quitGUI() { |
| 654 |
ImGui_ImplDX11_Shutdown(); |
| 655 |
} |
| 656 |
|
| 657 |
bool FurnaceGUIRenderDX11::isDead() { |
| 658 |
return dead; |
| 659 |
} |