2508
|
1 #include <d3dcompiler.h>
|
2496
|
2 #include <comdef.h> // _com_error
|
|
3
|
2541
|
4 #include "Engine/Engine.h"
|
2496
|
5 #include "RenderD3D11.h"
|
2518
|
6 #include "Texture.h"
|
2524
|
7 #include "Sprites.h"
|
2496
|
8
|
|
9 #define ErrorD3D(x)\
|
|
10 {\
|
|
11 HRESULT hr = x;\
|
|
12 if (FAILED(hr))\
|
|
13 {\
|
|
14 _com_error com_error(hr);\
|
|
15 Error("HRESULT = %08X\n%S", hr, com_error.ErrorMessage());\
|
|
16 }\
|
|
17 }
|
|
18
|
|
19
|
2526
|
20 RenderD3D11::RenderD3D11(): IRender() {}
|
2496
|
21 RenderD3D11::~RenderD3D11() {}
|
|
22
|
|
23
|
2518
|
24 bool CompileShader(ID3D11Device *d3dd, const wchar_t *pShaderSourceFile, D3D11_INPUT_ELEMENT_DESC *input_desc, int input_desc_size,
|
|
25 ID3D11VertexShader **vertex_out, ID3D11PixelShader **pixel_out, ID3D11InputLayout **layout_out);
|
|
26
|
|
27
|
2496
|
28 void RenderD3D11::ClearBlack() {__debugbreak();}
|
|
29 void RenderD3D11::SaveWinnersCertificate(const char *a1) {__debugbreak();}
|
|
30 void RenderD3D11::_49FD3A_fullscreen() {__debugbreak();}
|
|
31 bool RenderD3D11::InitializeFullscreen() {__debugbreak(); return 0;}
|
|
32 void RenderD3D11::CreateZBuffer() {__debugbreak();}
|
|
33 void RenderD3D11::Release() {__debugbreak();}
|
|
34 void RenderD3D11::RasterLine2D(signed int uX, signed int uY, signed int uZ, signed int uW, unsigned __int16 uColor) {__debugbreak();}
|
|
35 void RenderD3D11::SetRasterClipRect(unsigned int uX, unsigned int uY, unsigned int uZ, unsigned int uW) {__debugbreak();}
|
|
36 bool RenderD3D11::LockSurface_DDraw4(IDirectDrawSurface4 *pSurface, DDSURFACEDESC2 *pDesc, unsigned int uLockFlags) {__debugbreak(); return 0;}
|
|
37 void RenderD3D11::GetTargetPixelFormat(DDPIXELFORMAT *pOut) {__debugbreak();}
|
|
38 void RenderD3D11::LockRenderSurface(void **pOutSurfacePtr, unsigned int *pOutPixelsPerRow) {__debugbreak();}
|
|
39 void RenderD3D11::UnlockBackBuffer() {__debugbreak();}
|
|
40 void RenderD3D11::LockFrontBuffer(void **pOutSurface, unsigned int *pOutPixelsPerRow) {__debugbreak();}
|
|
41 void RenderD3D11::UnlockFrontBuffer() {__debugbreak();}
|
2518
|
42 void RenderD3D11::RestoreFrontBuffer() {}
|
|
43 void RenderD3D11::RestoreBackBuffer() {}
|
2496
|
44 void RenderD3D11::BltToFront(RECT *pDstRect, IDirectDrawSurface *pSrcSurface, RECT *pSrcRect, unsigned int uBltFlags) {__debugbreak();}
|
|
45 void RenderD3D11::BltBackToFontFast(int a2, int a3, RECT *a4) {__debugbreak();}
|
2526
|
46 void RenderD3D11::BeginSceneD3D() {}
|
2496
|
47 unsigned int RenderD3D11::GetActorTintColor(float a2, int tint, int a4, int a5, RenderBillboard *a6) {__debugbreak(); return 0;}
|
|
48 void RenderD3D11::DrawPolygon(unsigned int uNumVertices, struct Polygon *a3, ODMFace *a4, IDirect3DTexture2 *pTexture) {__debugbreak();}
|
|
49 void RenderD3D11::DrawTerrainPolygon(unsigned int uNumVertices, struct Polygon *a4, IDirect3DTexture2 *a5, bool transparent, bool clampAtTextureBorders) {__debugbreak();}
|
|
50 void RenderD3D11::DrawIndoorPolygon(unsigned int uNumVertices, struct BLVFace *a3, IDirect3DTexture2 *pHwTex, struct Texture *pTex, int uPackedID, unsigned int uColor, int a8) {__debugbreak();}
|
|
51 void RenderD3D11::MakeParticleBillboardAndPush_BLV(RenderBillboardTransform_local0 *a2, IDirect3DTexture2 *a3, unsigned int uDiffuse, int angle) {__debugbreak();}
|
|
52 void RenderD3D11::MakeParticleBillboardAndPush_ODM(RenderBillboardTransform_local0 *a2, IDirect3DTexture2 *a3, unsigned int uDiffuse, int angle) {__debugbreak();}
|
|
53 void RenderD3D11::DrawBillboards_And_MaybeRenderSpecialEffects_And_EndScene() {__debugbreak();}
|
|
54 void RenderD3D11::DrawBillboard_Indoor(RenderBillboardTransform_local0 *pSoftBillboard, Sprite *pSprite, int dimming_level) {__debugbreak();}
|
|
55 void RenderD3D11::_4A4CC9_AddSomeBillboard(struct stru6_stru1_indoor_sw_billboard *a1, int diffuse) {__debugbreak();}
|
|
56 void RenderD3D11::TransformBillboardsAndSetPalettesODM() {__debugbreak();}
|
|
57 void RenderD3D11::DrawBillboardList_BLV() {__debugbreak();}
|
|
58 void RenderD3D11::DrawProjectile(float srcX, float srcY, float a3, float a4, float dstX, float dstY, float a7, float a8, IDirect3DTexture2 *a9) {__debugbreak();}
|
|
59 void RenderD3D11::ScreenFade(unsigned int color, float t) {__debugbreak();}
|
|
60 void RenderD3D11::CreditsTextureScroll(unsigned int pX, unsigned int pY, int move_X, int move_Y, RGBTexture *pTexture) {__debugbreak();}
|
|
61 void RenderD3D11::ZBuffer_Fill_2(signed int a2, signed int a3, struct Texture *pTexture, int a5) {__debugbreak();}
|
|
62 void RenderD3D11::DrawMaskToZBuffer(signed int uOutX, unsigned int uOutY, struct Texture *pTexture, int zVal) {__debugbreak();}
|
|
63 void RenderD3D11::DrawAura(unsigned int a2, unsigned int a3, struct Texture *a4, struct Texture *a5, int a6, int a7, int a8) {__debugbreak();}
|
|
64 void RenderD3D11::_4A65CC(unsigned int x, unsigned int y, struct Texture *a4, struct Texture *a5, int a6, int a7, int a8) {__debugbreak();}
|
|
65 void RenderD3D11::DrawTransparentRedShade(unsigned int a2, unsigned int a3, struct Texture *a4) {__debugbreak();}
|
|
66 void RenderD3D11::DrawTransparentGreenShade(signed int a2, signed int a3, struct Texture *pTexture) {__debugbreak();}
|
|
67 void RenderD3D11::DrawFansTransparent(const RenderVertexD3D3 *vertices, unsigned int num_vertices) {__debugbreak();}
|
|
68 void RenderD3D11::DrawMasked(signed int a2, signed int a3, struct Texture *pTexture, unsigned __int16 mask) {__debugbreak();}
|
|
69 void RenderD3D11::GetLeather(unsigned int a2, unsigned int a3, struct Texture *a4, __int16 height) {__debugbreak();}
|
|
70 void RenderD3D11::FillRectFast(unsigned int uX, unsigned int uY, unsigned int uWidth, unsigned int uHeight, unsigned int uColor16) {__debugbreak();}
|
|
71 void RenderD3D11::_4A6DF5(unsigned __int16 *pBitmap, unsigned int uBitmapPitch, struct Vec2_int_ *pBitmapXY, void *pTarget, unsigned int uTargetPitch, Vec4_int_ *a7) {__debugbreak();}
|
|
72 void RenderD3D11::DrawTranslucent(unsigned int a2, unsigned int a3, struct Texture *a4) {__debugbreak();}
|
|
73 void RenderD3D11::DrawBuildingsD3D() {__debugbreak();}
|
|
74 void RenderD3D11::DrawIndoorSky(unsigned int uNumVertices, unsigned int uFaceID) {__debugbreak();}
|
|
75 void RenderD3D11::DrawOutdoorSkyD3D() {__debugbreak();}
|
|
76 void RenderD3D11::DrawOutdoorSkyPolygon(unsigned int uNumVertices, struct Polygon *pSkyPolygon, IDirect3DTexture2 *pTexture) {__debugbreak();}
|
|
77 void RenderD3D11::DrawIndoorSkyPolygon(signed int uNumVertices, struct Polygon *pSkyPolygon, IDirect3DTexture2 *pTexture) {__debugbreak();}
|
|
78 void RenderD3D11::PrepareDecorationsRenderList_ODM() {__debugbreak();}
|
|
79 void RenderD3D11::DrawSpriteObjects_ODM() {__debugbreak();}
|
|
80 void RenderD3D11::RenderTerrainD3D() {__debugbreak();}
|
2524
|
81 bool RenderD3D11::AreRenderSurfacesOk() {return true;}
|
2496
|
82 void RenderD3D11::SaveScreenshot(const char *pFilename, unsigned int width, unsigned int height) {__debugbreak();}
|
|
83 void RenderD3D11::SavePCXScreenshot() {__debugbreak();}
|
|
84 int RenderD3D11::_46À6ÀÑ_GetActorsInViewport(int pDepth) {__debugbreak(); return 0;}
|
|
85 void RenderD3D11::BeginLightmaps() {__debugbreak();}
|
|
86 void RenderD3D11::EndLightmaps() {__debugbreak();}
|
|
87 void RenderD3D11::BeginLightmaps2() {__debugbreak();}
|
|
88 void RenderD3D11::EndLightmaps2() {__debugbreak();}
|
|
89 bool RenderD3D11::DrawLightmap(struct Lightmap *pLightmap, struct Vec3_float_ *pColorMult, float z_bias) {__debugbreak(); return 0;}
|
|
90 void RenderD3D11::BeginDecals() {__debugbreak();}
|
|
91 void RenderD3D11::EndDecals() {__debugbreak();}
|
|
92 void RenderD3D11::DrawDecal(struct Decal *pDecal, float z_bias) {__debugbreak();}
|
|
93 void RenderD3D11::do_draw_debug_line_d3d(const RenderVertexD3D3 *pLineBegin, signed int sDiffuseBegin, const RenderVertexD3D3 *pLineEnd, signed int sDiffuseEnd, float z_stuff) {__debugbreak();}
|
|
94 void RenderD3D11::DrawLines(const RenderVertexD3D3 *vertices, unsigned int num_vertices) {__debugbreak();}
|
|
95 void RenderD3D11::DrawSpecialEffectsQuad(const RenderVertexD3D3 *vertices, IDirect3DTexture2 *texture) {__debugbreak();}
|
|
96 void RenderD3D11::am_Blt_Copy(RECT *pSrcRect, POINT *pTargetXY, int a3) {__debugbreak();}
|
|
97 void RenderD3D11::am_Blt_Chroma(RECT *pSrcRect, POINT *pTargetPoint, int a3, int blend_mode) {__debugbreak();}
|
|
98
|
|
99 void RenderD3D11::Sub01() {__debugbreak();}
|
|
100
|
|
101
|
|
102
|
|
103
|
2525
|
104 void RenderD3D11::PackScreenshot(unsigned int width, unsigned int height, void *out_data, unsigned int data_size, unsigned int *screenshot_size) {}
|
2496
|
105
|
2524
|
106 void RenderD3D11::SetUIClipRect(unsigned int uX, unsigned int uY, unsigned int uZ, unsigned int uW)
|
|
107 {
|
|
108 ui_clip_rect.left = uX;
|
|
109 ui_clip_rect.top = uY;
|
|
110 ui_clip_rect.right = uZ;
|
|
111 ui_clip_rect.bottom = uW;
|
|
112 }
|
|
113
|
|
114 void RenderD3D11::ResetUIClipRect()
|
|
115 {
|
|
116 ui_clip_rect.left = 0;
|
|
117 ui_clip_rect.top = 0;
|
|
118 ui_clip_rect.right = window->GetWidth();
|
|
119 ui_clip_rect.bottom = window->GetHeight();
|
|
120 }
|
|
121
|
2496
|
122 void RenderD3D11::PresentBlackScreen()
|
|
123 {
|
|
124 ClearTarget(0xFF000000);
|
|
125 ErrorD3D(pSwapChain->Present(0, 0));
|
|
126 }
|
|
127
|
|
128 void RenderD3D11::BeginScene() {}
|
|
129 void RenderD3D11::EndScene() {}
|
|
130
|
|
131 void RenderD3D11::ClearTarget(unsigned int uColor)
|
|
132 {
|
|
133 float clear_color[] =
|
|
134 {
|
|
135 ((uColor & 0x00FF0000) >> 16) / 255.0f,
|
|
136 ((uColor & 0x0000FF00) >> 8) / 255.0f,
|
|
137 ((uColor & 0x000000FF) >> 0) / 255.0f,
|
|
138 ((uColor & 0xFF000000) >> 24) / 255.0f
|
|
139 };
|
|
140 d3dc->ClearRenderTargetView(primary_srv, clear_color);
|
|
141 }
|
|
142
|
2525
|
143 void RenderD3D11::ClearZBuffer(int, int)
|
|
144 {
|
|
145 d3dc->ClearDepthStencilView(default_depth_srv, D3D11_CLEAR_DEPTH, 1.0f, 0);
|
|
146 }
|
2496
|
147
|
2524
|
148
|
2536
|
149 void RenderD3D11::DrawTextureIndexed(signed int x, signed int y, struct Texture *tex)
|
2524
|
150 {
|
2536
|
151 PrepareTextureIndexed(tex);
|
|
152 DrawTexture((float)x / window->GetWidth(), (float)y / window->GetHeight(), tex->uTextureWidth, tex->uTextureHeight, tex->d3d11_srv, ui_blend_solid);
|
2524
|
153 }
|
|
154
|
|
155 void RenderD3D11::DrawTextureIndexedAlpha(unsigned int uX, unsigned int uY, struct Texture *a4)
|
|
156 {
|
|
157 PrepareTextureIndexed(a4);
|
|
158 DrawTexture((float)uX / window->GetWidth(), (float)uY / window->GetHeight(), a4->uTextureWidth, a4->uTextureHeight, a4->d3d11_srv, ui_blend_alpha);
|
|
159 }
|
|
160
|
2525
|
161 void RenderD3D11::DrawTextureRGB(unsigned int uX, unsigned int uY, RGBTexture *a4)
|
2496
|
162 {
|
2525
|
163 PrepareTexture(a4);
|
|
164 DrawTexture((float)uX / window->GetWidth(), (float)uY / window->GetHeight(), a4->uWidth, a4->uHeight, a4->d3d11_srv, ui_blend_solid);
|
2496
|
165 }
|
|
166
|
2518
|
167 void RenderD3D11::DrawTextureNew(float u, float v, Texture *tex)
|
|
168 {
|
2521
|
169 __debugbreak();
|
2518
|
170 }
|
|
171
|
|
172 void RenderD3D11::DrawTextureNew(float u, float v, RGBTexture *tex)
|
|
173 {
|
2524
|
174 PrepareTexture(tex);
|
|
175 DrawTexture(u, v, tex->uWidth, tex->uHeight, tex->d3d11_srv, ui_blend_solid);
|
|
176 }
|
2518
|
177
|
2524
|
178 void RenderD3D11::DrawText(signed int uX, signed int uY, unsigned __int8 *pFontPixels, unsigned int uCharWidth, unsigned int uCharHeight, unsigned __int16 *pFontPalette, unsigned __int16 uFaceColor, unsigned __int16 uShadowColor)
|
|
179 {
|
|
180 auto srv = PrepareFontTexture(pFontPixels, uCharWidth, uCharHeight, pFontPalette, uFaceColor, uShadowColor);
|
|
181 {
|
|
182 DrawTexture((float)uX / window->GetWidth(), (float)uY / window->GetHeight(), uCharWidth, uCharHeight, srv, ui_blend_alpha);
|
|
183 }
|
|
184 srv->Release();
|
|
185 }
|
2518
|
186
|
2525
|
187 void RenderD3D11::DrawTextAlpha(int x, int y, unsigned char* font_pixels, int a5, unsigned int uFontHeight, unsigned __int16 *pPalette, bool present_time_transparency)
|
|
188 {
|
|
189 auto srv = PrepareFontTexture(font_pixels, a5, uFontHeight, pPalette);
|
|
190 {
|
|
191 DrawTexture((float)x / window->GetWidth(), (float)y / window->GetHeight(), a5, uFontHeight, srv, ui_blend_alpha);
|
|
192 }
|
|
193 srv->Release();
|
|
194 }
|
2518
|
195
|
2524
|
196 void RenderD3D11::DrawTexture(float u, float v, int texture_width, int texture_height, ID3D11ShaderResourceView *srv, ID3D11BlendState *blend)
|
|
197 {
|
|
198 bool clipping = false;
|
|
199 if (ui_clip_rect.left != 0 || ui_clip_rect.top != 0
|
|
200 || ui_clip_rect.right != window->GetWidth() || ui_clip_rect.bottom != window->GetHeight())
|
|
201 clipping = true;
|
2521
|
202
|
|
203 float blendFactor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
|
2525
|
204 d3dc->OMSetRenderTargets(1, &primary_srv, default_depth_srv);
|
2524
|
205 d3dc->OMSetBlendState(blend, blendFactor, 0xFFFFFFFF);
|
2521
|
206 d3dc->OMSetDepthStencilState(ui_depthstencil, 1);
|
|
207
|
2523
|
208 {
|
|
209 struct cb_fast
|
|
210 {
|
|
211 float pos_x;
|
|
212 float pos_y;
|
|
213 float size_x;
|
|
214 float size_y;
|
|
215 };
|
|
216
|
|
217 D3D11_MAPPED_SUBRESOURCE map;
|
|
218 d3dc->Map(ui_cb_fast, 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
|
|
219 {
|
|
220 cb_fast data;
|
|
221 {
|
|
222 data.pos_x = u;
|
|
223 data.pos_y = v;
|
2524
|
224 data.size_x = (float)texture_width / window->GetWidth();
|
|
225 data.size_y = (float)texture_height / window->GetHeight();
|
2523
|
226 }
|
|
227 memcpy(map.pData, &data, sizeof(data));
|
|
228 }
|
|
229 d3dc->Unmap(ui_cb_fast, 0);
|
|
230 }
|
|
231
|
2521
|
232 d3dc->VSSetShader(ui_vs, nullptr, 0);
|
2523
|
233 d3dc->VSSetConstantBuffers(0, 1, &ui_cb_fast);
|
|
234
|
2521
|
235 d3dc->PSSetShader(ui_ps, nullptr, 0);
|
2523
|
236 d3dc->PSSetConstantBuffers(0, 1, &ui_cb_fast);
|
2524
|
237 d3dc->PSSetShaderResources(0, 1, &srv);
|
|
238
|
|
239 if (clipping)
|
|
240 {
|
|
241 d3dc->RSSetState(ui_rasterizer);
|
|
242 d3dc->RSSetScissorRects(1, &ui_clip_rect);
|
|
243 }
|
|
244 //d3dc->RSSetViewports(1, &ui_viewport);
|
2521
|
245
|
|
246 uint uOffset = 0;
|
|
247 uint uStride = 4 * sizeof(float);
|
|
248 d3dc->IASetVertexBuffers(0, 1, &ui_vb, &uStride, &uOffset);
|
|
249 d3dc->IASetInputLayout(ui_layout);
|
|
250 d3dc->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
|
251
|
|
252 d3dc->Draw(6, 0);
|
|
253
|
2524
|
254 if (clipping)
|
|
255 {
|
|
256 d3dc->RSSetState(default_rasterizer);
|
|
257 //d3dc->RSSetScissorRects(0, nullptr);
|
|
258 }
|
|
259 }
|
|
260
|
|
261
|
|
262
|
|
263 void RenderD3D11::Present()
|
|
264 {
|
2521
|
265 pSwapChain->Present(0, 0);
|
2518
|
266 }
|
|
267
|
2496
|
268 bool RenderD3D11::IsGammaSupported()
|
|
269 {
|
|
270 return false;
|
|
271 }
|
|
272
|
|
273 struct
|
|
274 {
|
|
275 unsigned char restore_resolution;
|
|
276 unsigned char _saved_screen_bpp;
|
|
277 unsigned short _saved_screen_width;
|
|
278 unsigned short _saved_screen_height;
|
|
279 } on_exit;
|
|
280
|
|
281 void ChangeResolution(int new_width, int new_height, int new_bpp)
|
|
282 {
|
|
283 if (!on_exit.restore_resolution)
|
|
284 {
|
|
285 on_exit.restore_resolution = true;
|
|
286
|
|
287 auto hdc = GetDC(nullptr);
|
|
288 {
|
|
289 on_exit._saved_screen_width = GetDeviceCaps(hdc, HORZRES);
|
|
290 on_exit._saved_screen_height = GetDeviceCaps(hdc, VERTRES);
|
|
291 on_exit._saved_screen_bpp = GetDeviceCaps(hdc, BITSPIXEL);
|
|
292 }
|
|
293 ReleaseDC(nullptr, hdc);
|
|
294 }
|
|
295
|
|
296 DEVMODEA dm;
|
|
297 dm.dmSize = sizeof(dm);
|
|
298 dm.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
|
|
299 dm.dmBitsPerPel = new_bpp;
|
|
300 dm.dmPelsWidth = new_width;
|
|
301 dm.dmPelsHeight = new_height;
|
|
302
|
|
303 if (ChangeDisplaySettingsA(&dm, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
|
|
304 Error("ChangeDisplaySettingsA");
|
|
305 }
|
|
306
|
|
307 __declspec(noreturn) void ExitApp()
|
|
308 {
|
|
309 if (on_exit.restore_resolution)
|
|
310 ChangeResolution(on_exit._saved_screen_width, on_exit._saved_screen_height, on_exit._saved_screen_bpp);
|
|
311
|
|
312 ExitProcess(0);
|
|
313 }
|
|
314
|
|
315
|
|
316
|
|
317 bool RenderD3D11::SwitchToWindow()
|
|
318 {
|
|
319 if (on_exit.restore_resolution)
|
|
320 {
|
|
321 on_exit.restore_resolution = false;
|
|
322 ChangeResolution(on_exit._saved_screen_width, on_exit._saved_screen_height, on_exit._saved_screen_bpp);
|
|
323 }
|
|
324 return true;
|
|
325 }
|
|
326
|
|
327
|
|
328
|
|
329
|
|
330 bool RenderD3D11::Initialize(OSWindow *window)
|
|
331 {
|
|
332 this->window = window;
|
|
333
|
|
334 auto d3d_lib = LoadLibraryW(L"d3d11.dll");
|
|
335 if (!d3d_lib)
|
2524
|
336 {
|
|
337 Error("d3d11.dll is missing");
|
|
338 return false;
|
|
339 }
|
2496
|
340
|
|
341
|
|
342 DXGI_SWAP_CHAIN_DESC swapChainDesc;
|
|
343 memset(&swapChainDesc, 0, sizeof(swapChainDesc));
|
|
344 swapChainDesc.BufferDesc.Width = window->GetWidth();
|
|
345 swapChainDesc.BufferDesc.Height = window->GetHeight();
|
|
346 //swapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
|
|
347 //swapChainDesc.BufferDesc.RefreshRate.Denominator = 0;
|
|
348 swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
349 //swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
|
|
350 //swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
|
|
351 swapChainDesc.SampleDesc.Count = 1;
|
|
352 //swapChainDesc.SampleDesc.Quality = 0;
|
|
353 swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
|
|
354 swapChainDesc.BufferCount = 2;
|
|
355 swapChainDesc.OutputWindow = window->GetApiHandle();
|
|
356 swapChainDesc.Windowed = true;
|
|
357 //swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
|
|
358 //swapChainDesc.Flags = 0;
|
|
359
|
|
360
|
|
361 D3D_FEATURE_LEVEL requested_feature_level = D3D_FEATURE_LEVEL_11_0,
|
|
362 received_feature_level;
|
|
363
|
|
364 unsigned int device_flags = 0;//D3D11_CREATE_DEVICE_DISABLE_GPU_TIMEOUT;
|
|
365 #ifndef NODEBUG
|
2518
|
366 device_flags |= D3D11_CREATE_DEVICE_DEBUG;
|
|
367 #endif
|
2496
|
368
|
|
369 HRESULT (__stdcall *dll_D3D11CreateDeviceAndSwapChain)(IDXGIAdapter* pAdapter, D3D_DRIVER_TYPE DriverType, HMODULE Software, UINT Flags, CONST D3D_FEATURE_LEVEL* pFeatureLevels, UINT FeatureLevels, UINT SDKVersion, CONST DXGI_SWAP_CHAIN_DESC* pSwapChainDesc, IDXGISwapChain** ppSwapChain, ID3D11Device** ppDevice, D3D_FEATURE_LEVEL* pFeatureLevel, ID3D11DeviceContext** ppImmediateContext);
|
|
370 dll_D3D11CreateDeviceAndSwapChain = (decltype(dll_D3D11CreateDeviceAndSwapChain))GetProcAddress(d3d_lib, "D3D11CreateDeviceAndSwapChain");
|
|
371 ErrorD3D(dll_D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, device_flags, nullptr, 0, D3D11_SDK_VERSION, &swapChainDesc, &pSwapChain, &d3dd, &received_feature_level, &d3dc));
|
|
372
|
|
373 if (received_feature_level < D3D_FEATURE_LEVEL_10_0)
|
|
374 {
|
|
375 MessageBoxA(nullptr, "Received Direct3D 9 or lower", "", 0);
|
|
376 __debugbreak();
|
2508
|
377 }
|
2496
|
378 /*if (fullscreen)
|
|
379 {
|
|
380 extern void ChangeResolution(int new_dith, int new_height, int new_bpp);
|
|
381 ChangeResolution(target_window->GetWidth(), target_window->GetHeight(), 32);
|
|
382
|
|
383 target_window->SetPosition(0, 0);
|
|
384 target_window->SetTopmost(true);
|
|
385 }
|
|
386 ErrorD3D(pSwapChain->SetFullscreenState(fullscreen, nullptr));*/
|
|
387
|
2508
|
388
|
2496
|
389 ID3D11Texture2D *pSwapChainSurface;
|
|
390 {
|
|
391 ErrorD3D(pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void **)&pSwapChainSurface));
|
|
392 ErrorD3D(d3dd->CreateRenderTargetView(pSwapChainSurface, nullptr, &primary_srv));
|
|
393 }
|
|
394 pSwapChainSurface->Release();
|
|
395
|
|
396
|
|
397 D3D11_TEXTURE2D_DESC z_desc;
|
|
398 memset(&z_desc, 0, sizeof(z_desc));
|
|
399 z_desc.Width = window->GetWidth();
|
|
400 z_desc.Height = window->GetHeight();
|
|
401 z_desc.MipLevels = 1;
|
|
402 z_desc.ArraySize = 1;
|
|
403 z_desc.Format = DXGI_FORMAT_D32_FLOAT;
|
|
404 z_desc.SampleDesc.Count = 1;
|
|
405 //z_desc.SampleDesc.Quality = 0;
|
|
406 //z_desc.Usage = D3D11_USAGE_DEFAULT;
|
|
407 z_desc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
|
|
408 //z_desc.CPUAccessFlags = 0;
|
|
409 //z_desc.MiscFlags = 0;
|
|
410
|
|
411 ID3D11Texture2D *depth_surface;
|
|
412 ErrorD3D(d3dd->CreateTexture2D(&z_desc, nullptr, &depth_surface));
|
|
413
|
|
414 D3D11_DEPTH_STENCIL_VIEW_DESC depth_srv_desc;
|
|
415 memset(&depth_srv_desc, 0, sizeof(depth_srv_desc));
|
|
416 depth_srv_desc.Format = DXGI_FORMAT_D32_FLOAT;
|
|
417 depth_srv_desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
|
|
418 //depth_srv_desc.Texture2D.MipSlice = 0;
|
2525
|
419 ErrorD3D(d3dd->CreateDepthStencilView(depth_surface, &depth_srv_desc, &default_depth_srv));
|
2496
|
420
|
2525
|
421 d3dc->OMSetRenderTargets(1, &primary_srv, default_depth_srv);
|
|
422 d3dc->ClearDepthStencilView(default_depth_srv, D3D11_CLEAR_DEPTH, 1.0f, 0);
|
2496
|
423
|
|
424
|
|
425
|
2521
|
426 D3D11_RASTERIZER_DESC default_rasterizer_desc;
|
|
427 memset(&default_rasterizer_desc, 0, sizeof(default_rasterizer_desc));
|
|
428 default_rasterizer_desc.FillMode = D3D11_FILL_SOLID;
|
|
429 default_rasterizer_desc.CullMode = D3D11_CULL_NONE;
|
|
430 //default_rasterizer_desc.FrontCounterClockwise = false;
|
|
431 //default_rasterizer_desc.DepthBias = 0;
|
|
432 //default_rasterizer_desc.DepthBiasClamp = 0.0f;
|
|
433 //default_rasterizer_desc.SlopeScaledDepthBias = 0.0f;
|
|
434 //default_rasterizer_desc.DepthClipEnable = true;
|
|
435 //default_rasterizer_desc.ScissorEnable = false;
|
|
436 //default_rasterizer_desc.MultisampleEnable = false;
|
|
437 //default_rasterizer_desc.AntialiasedLineEnable = false;
|
2496
|
438
|
2521
|
439 ErrorD3D(d3dd->CreateRasterizerState(&default_rasterizer_desc, &default_rasterizer));
|
|
440 d3dc->RSSetState(default_rasterizer);
|
|
441
|
2524
|
442
|
|
443
|
|
444 D3D11_RASTERIZER_DESC ui_rasterizer_desc;
|
|
445 memset(&ui_rasterizer_desc, 0, sizeof(ui_rasterizer_desc));
|
|
446 ui_rasterizer_desc.FillMode = D3D11_FILL_SOLID;
|
|
447 ui_rasterizer_desc.CullMode = D3D11_CULL_NONE;
|
|
448 //ui_rasterizer_desc.FrontCounterClockwise = false;
|
|
449 //ui_rasterizer_desc.DepthBias = 0;
|
|
450 //ui_rasterizer_desc.DepthBiasClamp = 0.0f;
|
|
451 //ui_rasterizer_desc.SlopeScaledDepthBias = 0.0f;
|
|
452 //ui_rasterizer_desc.DepthClipEnable = true;
|
|
453 ui_rasterizer_desc.ScissorEnable = true;
|
|
454 //ui_rasterizer_desc.MultisampleEnable = false;
|
|
455 //ui_rasterizer_desc.AntialiasedLineEnable = false;
|
|
456
|
|
457 ErrorD3D(d3dd->CreateRasterizerState(&ui_rasterizer_desc, &ui_rasterizer));
|
|
458
|
|
459
|
2521
|
460 default_depthstencil = nullptr;
|
|
461 default_blend = nullptr;
|
2496
|
462
|
|
463
|
|
464
|
|
465 D3D11_VIEWPORT viewport;
|
|
466 memset(&viewport, 0, sizeof(viewport));
|
2524
|
467 viewport.TopLeftX = 0;
|
|
468 viewport.TopLeftY = 0;
|
2523
|
469 viewport.Width = window->GetWidth();
|
|
470 viewport.Height = window->GetHeight();
|
2521
|
471 viewport.MinDepth = 0;
|
2496
|
472 viewport.MaxDepth = 1;
|
|
473 d3dc->RSSetViewports(1, &viewport);
|
|
474
|
2524
|
475 ResetUIClipRect();
|
|
476
|
2518
|
477
|
|
478 D3D11_INPUT_ELEMENT_DESC layout_desc[] =
|
|
479 {
|
2521
|
480 { "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
|
|
481 //{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 16, D3D11_INPUT_PER_VERTEX_DATA, 0 },
|
2518
|
482 };
|
2524
|
483 CompileShader(d3dd, L"data/shaders/UI.hlsl", layout_desc, 1/*2*/, &ui_vs, &ui_ps, &ui_layout);
|
2518
|
484
|
|
485 {
|
|
486 uint uVertexSize = 4 * sizeof(float);
|
|
487
|
2523
|
488 float ui_mesh[] =
|
2518
|
489 {
|
2523
|
490 1, 1, 0, 0, // pos
|
|
491 1, 1, 1, 0, // pos + size.x
|
|
492 1, 1, 0, 1, // pos + size.y
|
|
493
|
|
494 1, 1, 0, 1, // pos + size.y
|
|
495 1, 1, 1, 0, // pos + size.x
|
|
496 1, 1, 1, 1 // pos + size.xy
|
|
497 /*-1, 1, 0.1, 1,
|
2521
|
498 1, 1, 0.1, 1,
|
|
499 -1, -1, 0.1, 1,
|
2518
|
500
|
2521
|
501 -1, -1, 0.1, 1,
|
|
502 1, 1, 0.1, 1,
|
2523
|
503 1, -1, 0.1, 1*/
|
2518
|
504 };
|
|
505
|
|
506 D3D11_BUFFER_DESC vbdesc;
|
|
507 vbdesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
|
508 vbdesc.ByteWidth = 6 * uVertexSize;
|
|
509 vbdesc.CPUAccessFlags = 0;
|
|
510 vbdesc.MiscFlags = 0;
|
|
511 vbdesc.StructureByteStride = 0;
|
|
512 vbdesc.Usage = D3D11_USAGE_DEFAULT;
|
|
513
|
|
514 D3D11_SUBRESOURCE_DATA vbdata;
|
2523
|
515 vbdata.pSysMem = ui_mesh;
|
2518
|
516 vbdata.SysMemPitch = 0;
|
|
517 vbdata.SysMemSlicePitch = 0;
|
|
518
|
2521
|
519 d3dd->CreateBuffer(&vbdesc, &vbdata, &ui_vb);
|
2518
|
520 }
|
|
521
|
2523
|
522 D3D11_BUFFER_DESC ui_cb_fast_desc;
|
|
523 ZeroMemory(&ui_cb_fast_desc, sizeof(ui_cb_fast_desc));
|
|
524 // TODO: Fix Bug Prone size
|
|
525 ui_cb_fast_desc.ByteWidth = 1 * 4 * sizeof(float);
|
|
526 ui_cb_fast_desc.Usage = D3D11_USAGE_DYNAMIC;
|
|
527 ui_cb_fast_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
|
528 ui_cb_fast_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
|
529 ui_cb_fast_desc.MiscFlags = 0;
|
|
530 ui_cb_fast_desc.StructureByteStride = 0;
|
|
531 d3dd->CreateBuffer(&ui_cb_fast_desc, nullptr, &ui_cb_fast);
|
2521
|
532
|
|
533 D3D11_DEPTH_STENCIL_DESC ui_depthstencil_desc;
|
|
534 ui_depthstencil_desc.DepthEnable = true;
|
|
535 ui_depthstencil_desc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
|
|
536 ui_depthstencil_desc.DepthFunc = D3D11_COMPARISON_ALWAYS;
|
|
537 ui_depthstencil_desc.StencilEnable = false;
|
|
538 ui_depthstencil_desc.StencilReadMask = 0xFF;
|
|
539 ui_depthstencil_desc.StencilWriteMask = 0xFF;
|
|
540 // Stencil operations if pixel is front-facing
|
|
541 ui_depthstencil_desc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
|
|
542 ui_depthstencil_desc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
|
|
543 ui_depthstencil_desc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
|
|
544 ui_depthstencil_desc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
|
|
545 // Stencil operations if pixel is back-facing
|
|
546 ui_depthstencil_desc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
|
|
547 ui_depthstencil_desc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
|
|
548 ui_depthstencil_desc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
|
|
549 ui_depthstencil_desc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
|
|
550
|
|
551 ErrorD3D(d3dd->CreateDepthStencilState(&ui_depthstencil_desc, &ui_depthstencil));
|
|
552
|
|
553
|
2524
|
554 D3D11_BLEND_DESC ui_blend_solid_desc;
|
|
555 memset(&ui_blend_solid_desc, 0, sizeof(ui_blend_solid_desc));
|
|
556 ui_blend_solid_desc.RenderTarget[0].BlendEnable = false;
|
|
557 ui_blend_solid_desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
|
|
558 ErrorD3D(d3dd->CreateBlendState(&ui_blend_solid_desc, &ui_blend_solid));
|
2521
|
559
|
2524
|
560 D3D11_BLEND_DESC ui_blend_alpha_desc;
|
|
561 memset(&ui_blend_alpha_desc, 0, sizeof(ui_blend_alpha_desc));
|
|
562 ui_blend_alpha_desc.RenderTarget[0].BlendEnable = true;
|
|
563 ui_blend_alpha_desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
|
|
564 ui_blend_alpha_desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
|
|
565 ui_blend_alpha_desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
|
|
566 ui_blend_alpha_desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
|
|
567 ui_blend_alpha_desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
|
|
568 ui_blend_alpha_desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
|
|
569 ui_blend_alpha_desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE;
|
|
570 ErrorD3D(d3dd->CreateBlendState(&ui_blend_alpha_desc, &ui_blend_alpha));
|
|
571
|
|
572
|
|
573 return pD3DBitmaps.Load(L"data\\d3dbitmap.hwl") && pD3DSprites.Load(L"data\\d3dsprite.hwl");
|
2518
|
574 }
|
|
575
|
|
576
|
|
577
|
|
578
|
2524
|
579 bool RenderD3D11::MoveSpriteToDevice(Sprite *pSprite)
|
|
580 {
|
|
581 HWLTexture *sprite_texture; // eax@1
|
|
582 unsigned __int16 *v9; // edx@5
|
|
583 LPVOID v10; // eax@5
|
|
584 DDSURFACEDESC2 Dst; // [sp+Ch] [bp-7Ch]@4
|
|
585
|
|
586 sprite_texture = pD3DSprites.LoadTexture(pSprite->pName, pSprite->uPaletteID);
|
|
587 if ( sprite_texture )
|
|
588 {
|
|
589 pSprite->uAreaX = sprite_texture->uAreaX;
|
|
590 pSprite->uAreaY = sprite_texture->uAreaY;
|
|
591 pSprite->uBufferWidth = sprite_texture->uBufferWidth;
|
|
592 pSprite->uBufferHeight = sprite_texture->uBufferHeight;
|
|
593 pSprite->uAreaWidth = sprite_texture->uAreaWidth;
|
|
594 pSprite->uAreaHeight = sprite_texture->uAreaHeigth;
|
|
595
|
|
596 {
|
|
597 D3D11_TEXTURE2D_DESC desc;
|
|
598 desc.Width = sprite_texture->uWidth;
|
|
599 desc.Height = sprite_texture->uHeight;
|
|
600 desc.ArraySize = 1;
|
|
601 desc.MipLevels = 1;
|
|
602 desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
603 desc.SampleDesc.Count = 1;
|
|
604 desc.SampleDesc.Quality = 0;
|
|
605 desc.Usage = D3D11_USAGE_DEFAULT;
|
|
606 desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
|
607 desc.CPUAccessFlags = 0;
|
|
608 desc.MiscFlags = 0;
|
|
609
|
|
610 ID3D11Texture2D *vram_texture = nullptr;
|
|
611 ErrorD3D(d3dd->CreateTexture2D(&desc, nullptr, &vram_texture));
|
|
612
|
|
613 D3D11_TEXTURE2D_DESC ram_desc;
|
|
614 memcpy(&ram_desc, &desc, sizeof(ram_desc));
|
|
615 ram_desc.Usage = D3D11_USAGE_STAGING;
|
|
616 ram_desc.BindFlags = 0;
|
|
617 ram_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
|
618
|
|
619 ID3D11Texture2D *ram_texture = nullptr;
|
|
620 ErrorD3D(d3dd->CreateTexture2D(&ram_desc, nullptr, &ram_texture));
|
|
621
|
|
622 D3D11_MAPPED_SUBRESOURCE map;
|
|
623 ErrorD3D(d3dc->Map(ram_texture, 0, D3D11_MAP_WRITE, 0, &map));
|
|
624 for (unsigned int y = 0; y < desc.Height; ++y)
|
|
625 for (unsigned int x = 0; x < desc.Width; ++x)
|
|
626 {
|
|
627 auto src = sprite_texture->pPixels + y * desc.Width + x;
|
|
628 auto dst = (unsigned int *)((char *)map.pData + y * map.RowPitch) + x;
|
|
629
|
|
630 extern unsigned __int32 Color32_SwapRedBlue(unsigned __int16 color16);
|
|
631 *dst = 0xFF000000 | Color32_SwapRedBlue(*src);
|
|
632 }
|
|
633
|
|
634
|
|
635 d3dc->Unmap(ram_texture, 0);
|
|
636 d3dc->CopyResource(vram_texture, ram_texture);
|
|
637 ram_texture->Release();
|
|
638
|
|
639 ID3D11ShaderResourceView *srv = nullptr;
|
|
640 ErrorD3D(d3dd->CreateShaderResourceView(vram_texture, nullptr, &srv));
|
|
641 vram_texture->Release();
|
|
642
|
|
643 pSprite->d3d11_srv = srv;
|
|
644 }
|
|
645 /*if (!pRenderD3D->CreateTexture(sprite_texture->uWidth, sprite_texture->uHeight, &pSprite->pTextureSurface, &pSprite->pTexture, 1u, 0, uMinDeviceTextureDim))
|
|
646 Error("HiScreen16::LoadTexture - D3Drend->CreateTexture() failed: %x", 0);
|
|
647 memset(&Dst, 0, sizeof(DDSURFACEDESC2));
|
|
648 Dst.dwSize = 124;
|
|
649 if ( LockSurface_DDraw4((IDirectDrawSurface4 *)pSprite->pTextureSurface, &Dst, DDLOCK_WAIT | DDLOCK_WRITEONLY) )
|
|
650 {
|
|
651 v9 = sprite_texture->pPixels;
|
|
652 v10 = Dst.lpSurface;
|
|
653 for (uint i=0; i<sprite_texture->uHeight; ++i)
|
|
654 {
|
|
655 for (uint j=0; j<sprite_texture->uWidth/2; ++j)
|
|
656 {
|
|
657 *(int *)v10 = *(int *)v9;
|
|
658 v9 += 2;
|
|
659 v10 = (char *)v10 + 4;
|
|
660 }
|
|
661 v10 = (char *)v10 + Dst.lPitch-sprite_texture->uWidth*2;
|
|
662 }
|
|
663 ErrD3D(pSprite->pTextureSurface->Unlock(NULL));
|
|
664 }*/
|
|
665 delete [] sprite_texture->pPixels;
|
|
666 delete sprite_texture;
|
|
667 return true;
|
|
668 }
|
|
669 return false;
|
|
670 }
|
2518
|
671
|
|
672
|
|
673 ID3DBlob *DoD3DCompiling(const wchar_t *shader_name, const char *pShaderSource, uint uShaderSourceLen, const char *pEntry, const char *pVersionString, uint uCompileOptions);
|
|
674 void DoCompile(const wchar_t *pFilename, const char *pShaderSource, unsigned int uShaderSourceLen,
|
|
675 unsigned char **vs, unsigned int *vs_size, unsigned char **ps, unsigned int *ps_size);
|
|
676 bool CompileShader(ID3D11Device *d3dd, const wchar_t *pShaderSourceFile, D3D11_INPUT_ELEMENT_DESC *input_desc, int input_desc_size,
|
|
677 ID3D11VertexShader **vertex_out, ID3D11PixelShader **pixel_out, ID3D11InputLayout **layout_out)
|
|
678 {
|
|
679 unsigned char *vs, *ps;
|
|
680 unsigned int vs_size, ps_size;
|
|
681
|
|
682 HANDLE hSourceFile = CreateFileW(pShaderSourceFile, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr);
|
|
683 if (hSourceFile == INVALID_HANDLE_VALUE)
|
|
684 Error("%S:\n\nCannot read shader file", pShaderSourceFile);
|
|
685
|
|
686 char *pShaderSource = nullptr;
|
|
687 int uShaderSourceLen = 0;
|
|
688 uint uFileSize = GetFileSize(hSourceFile, nullptr);
|
|
689 {
|
|
690 DWORD w;
|
|
691 char *p = new char[uFileSize + 1];
|
|
692 ReadFile(hSourceFile, p, uFileSize, &w, nullptr);
|
|
693 CloseHandle(hSourceFile);
|
|
694 p[uFileSize] = 0;
|
|
695
|
|
696 pShaderSource = p;
|
|
697 uShaderSourceLen = uFileSize;
|
|
698 }
|
|
699
|
|
700 DoCompile(pShaderSourceFile, pShaderSource, uShaderSourceLen, &vs, &vs_size, &ps, &ps_size);
|
|
701
|
2524
|
702 ErrorD3D(d3dd->CreateVertexShader(vs, vs_size, nullptr, vertex_out));
|
|
703 ErrorD3D(d3dd->CreatePixelShader(ps, ps_size, nullptr, pixel_out));
|
|
704 ErrorD3D(d3dd->CreateInputLayout(input_desc, input_desc_size, vs, vs_size, layout_out));
|
2518
|
705
|
|
706 delete [] vs;
|
|
707 delete [] ps;
|
|
708
|
|
709 return true;
|
|
710 }
|
|
711
|
|
712
|
|
713
|
|
714 static HMODULE compiler_dll = nullptr;
|
|
715 void DoCompile(const wchar_t *pFilename, const char *pSahderSource, unsigned int uShaderSourceLen,
|
|
716 unsigned char **vs, unsigned int *vs_size, unsigned char **ps, unsigned int *ps_size)
|
|
717 {
|
|
718 const char *pVSEntry = "vs",
|
|
719 *pVSVersionString = "vs_4_0";
|
|
720 const char *pPSEntry = "main",
|
|
721 *pPSVersionString = "ps_4_0";
|
|
722
|
|
723
|
|
724 uint uVSCompileOptions =
|
|
725 #ifndef NDEBUG
|
|
726 D3D10_SHADER_SKIP_VALIDATION |
|
|
727 D3D10_SHADER_SKIP_OPTIMIZATION |
|
|
728 D3D10_SHADER_PREFER_FLOW_CONTROL;
|
|
729 #else
|
|
730 D3D10_SHADER_SKIP_VALIDATION |
|
|
731 D3D10_SHADER_OPTIMIZATION_LEVEL3 |
|
|
732 D3D10_SHADER_WARNINGS_ARE_ERRORS |
|
|
733 D3D10_SHADER_AVOID_FLOW_CONTROL |
|
|
734 D3D10_SHADER_ENABLE_STRICTNESS |
|
|
735 D3D10_SHADER_IEEE_STRICTNESS;
|
|
736 #endif
|
|
737
|
|
738 *vs_size = 0;
|
|
739 *vs = nullptr;
|
|
740 {
|
|
741 auto pVSBlob = DoD3DCompiling(pFilename, pSahderSource, uShaderSourceLen, pVSEntry, pVSVersionString, uVSCompileOptions);
|
|
742 if (pVSBlob)
|
|
743 {
|
|
744 *vs = new unsigned char[*vs_size = pVSBlob->GetBufferSize()];
|
|
745 memcpy(*vs, pVSBlob->GetBufferPointer(), *vs_size);
|
|
746
|
|
747 pVSBlob->Release();
|
|
748 }
|
|
749 }
|
|
750
|
|
751
|
|
752 uint uPSCompileOptions =
|
|
753 #ifndef NDEBUG
|
|
754 D3D10_SHADER_SKIP_VALIDATION |
|
|
755 D3D10_SHADER_SKIP_OPTIMIZATION |
|
|
756 D3D10_SHADER_PREFER_FLOW_CONTROL;
|
|
757 #else
|
|
758 D3D10_SHADER_SKIP_VALIDATION |
|
|
759 D3D10_SHADER_OPTIMIZATION_LEVEL3 |
|
|
760 D3D10_SHADER_WARNINGS_ARE_ERRORS |
|
|
761 D3D10_SHADER_AVOID_FLOW_CONTROL |
|
|
762 D3D10_SHADER_ENABLE_STRICTNESS |
|
|
763 D3D10_SHADER_IEEE_STRICTNESS;
|
|
764 #endif
|
|
765
|
|
766 *ps_size = 0;
|
|
767 *ps = nullptr;
|
|
768 {
|
|
769 auto pPSBlob = DoD3DCompiling(pFilename, pSahderSource, uShaderSourceLen, pPSEntry, pPSVersionString, uPSCompileOptions);
|
|
770 if (pPSBlob)
|
|
771 {
|
|
772 *ps = new unsigned char[*ps_size = pPSBlob->GetBufferSize()];
|
|
773 memcpy(*ps, pPSBlob->GetBufferPointer(), *ps_size);
|
|
774
|
|
775 pPSBlob->Release();
|
|
776 }
|
|
777 }
|
|
778 }
|
|
779
|
|
780 ID3DBlob *DoD3DCompiling(const wchar_t *shader_name, const char *pShaderSource, uint uShaderSourceLen, const char *pEntry, const char *pVersionString, uint uCompileOptions)
|
|
781 {
|
|
782 ID3DBlob *pShader,
|
|
783 *pErrors;
|
|
784
|
|
785 if (!compiler_dll)
|
|
786 for (int i = 45; i >= 38; --i)
|
|
787 {
|
|
788 wchar_t dll_name[64];
|
|
789 swprintf(dll_name, L"d3dcompiler_%u.dll", i);
|
|
790
|
|
791 compiler_dll = LoadLibraryW(dll_name);
|
|
792 if (compiler_dll)
|
|
793 break;
|
|
794 }
|
|
795
|
|
796 if (!compiler_dll)
|
|
797 Error("Cannot find any suitable d3dcompiler.dll");
|
|
798
|
|
799 typedef HRESULT (__stdcall *fnD3DCompile)(const void * pSrcData, SIZE_T SrcDataSize, LPCSTR pSourceName, CONST D3D_SHADER_MACRO* pDefines, ID3DInclude* pInclude, LPCSTR pEntrypoint, LPCSTR pTarget, UINT Flags1, UINT Flags2, ID3DBlob** ppCode, ID3DBlob** ppErrorMsgs);
|
|
800 auto pD3DCompile = (fnD3DCompile)GetProcAddress(compiler_dll, "D3DCompile");
|
|
801
|
|
802 HRESULT hr = pD3DCompile(pShaderSource, uShaderSourceLen, nullptr, nullptr, nullptr, pEntry, pVersionString, uCompileOptions, 0, &pShader, &pErrors);
|
|
803
|
|
804 if (FAILED(hr))
|
|
805 {
|
|
806 if (pErrors)
|
|
807 Error("%S (%s) build failed:\n\n%s", shader_name, pVersionString, pErrors->GetBufferPointer());
|
|
808 else
|
|
809 Error("%S (%s) build failed", shader_name, pVersionString);
|
|
810 }
|
|
811 else if (pErrors)
|
|
812 {
|
|
813 Log::Warning(L"%s (%S) build warnings:\n\n%S", shader_name, pVersionString, pErrors->GetBufferPointer());
|
|
814 pErrors->Release();
|
|
815 }
|
|
816
|
|
817 return pShader;
|
2524
|
818 }
|
|
819
|
|
820
|
|
821 void RenderD3D11::PrepareTexture(RGBTexture *p)
|
|
822 {
|
|
823 if (!p->d3d11_srv)
|
|
824 {
|
|
825 auto desc = p->d3d11_desc = new D3D11_TEXTURE2D_DESC;
|
|
826 desc->Width = p->uWidth;
|
|
827 desc->Height = p->uHeight;
|
|
828 desc->ArraySize = 1;
|
|
829 desc->MipLevels = 1;
|
|
830 desc->Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
831 desc->SampleDesc.Count = 1;
|
|
832 desc->SampleDesc.Quality = 0;
|
|
833 desc->Usage = D3D11_USAGE_DEFAULT;
|
|
834 desc->BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
|
835 desc->CPUAccessFlags = 0;
|
|
836 desc->MiscFlags = 0;
|
|
837
|
|
838 ID3D11Texture2D *vram_texture = nullptr;
|
|
839 ErrorD3D(d3dd->CreateTexture2D(desc, nullptr, &vram_texture));
|
|
840
|
|
841 D3D11_TEXTURE2D_DESC ram_desc;
|
|
842 memcpy(&ram_desc, desc, sizeof(ram_desc));
|
|
843 ram_desc.Usage = D3D11_USAGE_STAGING;
|
|
844 ram_desc.BindFlags = 0;
|
|
845 ram_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
|
846
|
|
847 ID3D11Texture2D *ram_texture = nullptr;
|
|
848 ErrorD3D(d3dd->CreateTexture2D(&ram_desc, nullptr, &ram_texture));
|
|
849
|
|
850 D3D11_MAPPED_SUBRESOURCE map;
|
|
851 ErrorD3D(d3dc->Map(ram_texture, 0, D3D11_MAP_WRITE, 0, &map));
|
|
852 for (unsigned int y = 0; y < desc->Height; ++y)
|
|
853 for (unsigned int x = 0; x < desc->Width; ++x)
|
|
854 {
|
|
855 auto src = p->pPixels + y * p->uWidth + x;
|
|
856 auto dst = (unsigned int *)((char *)map.pData + y * map.RowPitch) + x;
|
|
857
|
|
858 extern unsigned __int32 Color32_SwapRedBlue(unsigned __int16 color16);
|
|
859 *dst = 0xFF000000 | Color32_SwapRedBlue(*src);
|
|
860 }
|
|
861
|
|
862 d3dc->Unmap(ram_texture, 0);
|
|
863 d3dc->CopyResource(vram_texture, ram_texture);
|
|
864 ram_texture->Release();
|
|
865
|
|
866 ErrorD3D(d3dd->CreateShaderResourceView(vram_texture, nullptr, (ID3D11ShaderResourceView **)&p->d3d11_srv));
|
|
867 vram_texture->Release();
|
|
868 }
|
|
869 }
|
|
870
|
|
871
|
|
872
|
|
873 void RenderD3D11::PrepareTextureIndexed(Texture *p)
|
|
874 {
|
|
875 if (!p->d3d11_srv)
|
|
876 {
|
|
877 auto desc = p->d3d11_desc = new D3D11_TEXTURE2D_DESC;
|
|
878 desc->Width = p->uTextureWidth;
|
|
879 desc->Height = p->uTextureHeight;
|
|
880 desc->ArraySize = 1;
|
|
881 desc->MipLevels = 1;
|
|
882 desc->Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
883 desc->SampleDesc.Count = 1;
|
|
884 desc->SampleDesc.Quality = 0;
|
|
885 desc->Usage = D3D11_USAGE_DEFAULT;
|
|
886 desc->BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
|
887 desc->CPUAccessFlags = 0;
|
|
888 desc->MiscFlags = 0;
|
|
889
|
|
890 ID3D11Texture2D *vram_texture = nullptr;
|
|
891 ErrorD3D(d3dd->CreateTexture2D(desc, nullptr, &vram_texture));
|
|
892
|
|
893 D3D11_TEXTURE2D_DESC ram_desc;
|
|
894 memcpy(&ram_desc, desc, sizeof(ram_desc));
|
|
895 ram_desc.Usage = D3D11_USAGE_STAGING;
|
|
896 ram_desc.BindFlags = 0;
|
|
897 ram_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
|
898
|
|
899 ID3D11Texture2D *ram_texture = nullptr;
|
|
900 ErrorD3D(d3dd->CreateTexture2D(&ram_desc, nullptr, &ram_texture));
|
|
901
|
|
902 D3D11_MAPPED_SUBRESOURCE map;
|
|
903 ErrorD3D(d3dc->Map(ram_texture, 0, D3D11_MAP_WRITE, 0, &map));
|
|
904 for (unsigned int y = 0; y < desc->Height; ++y)
|
|
905 for (unsigned int x = 0; x < desc->Width; ++x)
|
|
906 {
|
|
907 auto index = p->pLevelOfDetail0_prolly_alpha_mask[y * p->uTextureWidth + x];
|
|
908 auto src = p->pPalette16[index];
|
|
909 auto dst = (unsigned int *)((char *)map.pData + y * map.RowPitch) + x;
|
|
910
|
|
911 if (index)
|
|
912 {
|
|
913 extern unsigned __int32 Color32_SwapRedBlue(unsigned __int16 color16);
|
|
914 *dst = 0xFF000000 | Color32_SwapRedBlue(src);
|
|
915 }
|
|
916 else
|
|
917 *dst = 0x00000000;
|
|
918 }
|
|
919
|
|
920 d3dc->Unmap(ram_texture, 0);
|
|
921 d3dc->CopyResource(vram_texture, ram_texture);
|
|
922 ram_texture->Release();
|
|
923
|
|
924 ErrorD3D(d3dd->CreateShaderResourceView(vram_texture, nullptr, (ID3D11ShaderResourceView **)&p->d3d11_srv));
|
|
925 vram_texture->Release();
|
|
926 }
|
|
927 }
|
|
928
|
|
929
|
|
930 ID3D11ShaderResourceView *RenderD3D11::PrepareFontTexture(unsigned char *pFontPixels, unsigned int uCharWidth, unsigned int uCharHeight, unsigned short *pFontPalette, unsigned short uFaceColor, unsigned short uShadowColor)
|
|
931 {
|
|
932 D3D11_TEXTURE2D_DESC desc;
|
|
933 desc.Width = uCharWidth;
|
|
934 desc.Height = uCharHeight;
|
|
935 desc.ArraySize = 1;
|
|
936 desc.MipLevels = 1;
|
|
937 desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
938 desc.SampleDesc.Count = 1;
|
|
939 desc.SampleDesc.Quality = 0;
|
|
940 desc.Usage = D3D11_USAGE_DEFAULT;
|
|
941 desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
|
942 desc.CPUAccessFlags = 0;
|
|
943 desc.MiscFlags = 0;
|
|
944
|
|
945 ID3D11Texture2D *vram_texture = nullptr;
|
|
946 ErrorD3D(d3dd->CreateTexture2D(&desc, nullptr, &vram_texture));
|
|
947
|
|
948 D3D11_TEXTURE2D_DESC ram_desc;
|
|
949 memcpy(&ram_desc, &desc, sizeof(ram_desc));
|
|
950 ram_desc.Usage = D3D11_USAGE_STAGING;
|
|
951 ram_desc.BindFlags = 0;
|
|
952 ram_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
|
953
|
|
954 ID3D11Texture2D *ram_texture = nullptr;
|
|
955 ErrorD3D(d3dd->CreateTexture2D(&ram_desc, nullptr, &ram_texture));
|
|
956
|
|
957 D3D11_MAPPED_SUBRESOURCE map;
|
|
958 ErrorD3D(d3dc->Map(ram_texture, 0, D3D11_MAP_WRITE, 0, &map));
|
|
959 for (unsigned int y = 0; y < desc.Height; ++y)
|
|
960 for (unsigned int x = 0; x < desc.Width; ++x)
|
|
961 {
|
|
962 auto index = pFontPixels[y * desc.Width + x];
|
|
963 auto src = pFontPalette[index];
|
|
964 auto dst = (unsigned int *)((char *)map.pData + y * map.RowPitch) + x;
|
|
965
|
|
966 if (index)
|
|
967 {
|
|
968 extern unsigned __int32 Color32_SwapRedBlue(unsigned __int16 color16);
|
|
969 *dst = 0xFF000000 | Color32_SwapRedBlue(index == 1 ? uShadowColor : uFaceColor);
|
|
970 }
|
|
971 else
|
|
972 *dst = 0x00000000;
|
|
973 }
|
|
974
|
|
975 d3dc->Unmap(ram_texture, 0);
|
|
976 d3dc->CopyResource(vram_texture, ram_texture);
|
|
977 ram_texture->Release();
|
|
978
|
|
979 ID3D11ShaderResourceView *srv = nullptr;
|
|
980 ErrorD3D(d3dd->CreateShaderResourceView(vram_texture, nullptr, &srv));
|
|
981 vram_texture->Release();
|
|
982
|
|
983 return srv;
|
|
984 }
|
|
985
|
2525
|
986
|
|
987 ID3D11ShaderResourceView *RenderD3D11::PrepareFontTexture(unsigned char *pFontPixels, unsigned int uCharWidth, unsigned int uCharHeight, unsigned short *pFontPalette)
|
|
988 {
|
|
989 D3D11_TEXTURE2D_DESC desc;
|
|
990 desc.Width = uCharWidth;
|
|
991 desc.Height = uCharHeight;
|
|
992 desc.ArraySize = 1;
|
|
993 desc.MipLevels = 1;
|
|
994 desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
995 desc.SampleDesc.Count = 1;
|
|
996 desc.SampleDesc.Quality = 0;
|
|
997 desc.Usage = D3D11_USAGE_DEFAULT;
|
|
998 desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
|
|
999 desc.CPUAccessFlags = 0;
|
|
1000 desc.MiscFlags = 0;
|
|
1001
|
|
1002 ID3D11Texture2D *vram_texture = nullptr;
|
|
1003 ErrorD3D(d3dd->CreateTexture2D(&desc, nullptr, &vram_texture));
|
|
1004
|
|
1005 D3D11_TEXTURE2D_DESC ram_desc;
|
|
1006 memcpy(&ram_desc, &desc, sizeof(ram_desc));
|
|
1007 ram_desc.Usage = D3D11_USAGE_STAGING;
|
|
1008 ram_desc.BindFlags = 0;
|
|
1009 ram_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
|
1010
|
|
1011 ID3D11Texture2D *ram_texture = nullptr;
|
|
1012 ErrorD3D(d3dd->CreateTexture2D(&ram_desc, nullptr, &ram_texture));
|
|
1013
|
|
1014 D3D11_MAPPED_SUBRESOURCE map;
|
|
1015 ErrorD3D(d3dc->Map(ram_texture, 0, D3D11_MAP_WRITE, 0, &map));
|
|
1016 for (unsigned int y = 0; y < desc.Height; ++y)
|
|
1017 for (unsigned int x = 0; x < desc.Width; ++x)
|
|
1018 {
|
|
1019 auto index = pFontPixels[y * desc.Width + x];
|
|
1020 auto src = pFontPalette[index];
|
|
1021 auto dst = (unsigned int *)((char *)map.pData + y * map.RowPitch) + x;
|
|
1022
|
|
1023 if (index)
|
|
1024 {
|
|
1025 extern unsigned __int32 Color32_SwapRedBlue(unsigned __int16 color16);
|
|
1026 *dst = 0xFF000000 | Color32_SwapRedBlue(src);
|
|
1027 }
|
|
1028 else
|
|
1029 *dst = 0x00000000;
|
|
1030 }
|
|
1031
|
|
1032 d3dc->Unmap(ram_texture, 0);
|
|
1033 d3dc->CopyResource(vram_texture, ram_texture);
|
|
1034 ram_texture->Release();
|
|
1035
|
|
1036 ID3D11ShaderResourceView *srv = nullptr;
|
|
1037 ErrorD3D(d3dd->CreateShaderResourceView(vram_texture, nullptr, &srv));
|
|
1038 vram_texture->Release();
|
|
1039
|
|
1040 return srv;
|
|
1041 }
|
|
1042
|
|
1043
|
2524
|
1044 void d3d11_release(ID3D11ShaderResourceView *srv)
|
|
1045 {
|
|
1046 srv->Release();
|
|
1047 }
|
|
1048
|
|
1049
|
|
1050 //----- (004A4DE1) --------------------------------------------------------
|
|
1051 bool RenderD3D11::LoadTexture(const char *pName, unsigned int bMipMaps, IDirectDrawSurface4 **pOutSurface, IDirect3DTexture2 **pOutTexture)
|
|
1052 {
|
|
1053 unsigned __int16 *v13; // ecx@19
|
|
1054 unsigned __int16 *v14; // eax@19
|
|
1055 DWORD v15; // edx@20
|
|
1056
|
|
1057 HWLTexture* pHWLTexture = pD3DBitmaps.LoadTexture(pName, bMipMaps);
|
|
1058 if (!pHWLTexture)
|
|
1059 return false;
|
|
1060
|
|
1061 int num_min_levels = 1;
|
|
1062 {
|
|
1063 int dim = min(pHWLTexture->uWidth, pHWLTexture->uHeight);
|
|
1064 while (dim > 1)
|
|
1065 {
|
|
1066 dim /= 2;
|
|
1067 num_min_levels++;
|
|
1068 }
|
|
1069 }
|
|
1070
|
|
1071
|
|
1072 {
|
|
1073 D3D11_TEXTURE2D_DESC desc;
|
|
1074 desc.Width = pHWLTexture->uWidth;
|
|
1075 desc.Height = pHWLTexture->uHeight;
|
|
1076 desc.ArraySize = 1;
|
|
1077 desc.MipLevels = num_min_levels;
|
|
1078 desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
1079 desc.SampleDesc.Count = 1;
|
|
1080 desc.SampleDesc.Quality = 0;
|
|
1081 desc.Usage = D3D11_USAGE_DEFAULT;
|
|
1082 desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET/* for mipmap generation */;
|
|
1083 desc.CPUAccessFlags = 0;
|
|
1084 desc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS;
|
|
1085
|
|
1086 ID3D11Texture2D *vram_texture = nullptr;
|
|
1087 ErrorD3D(d3dd->CreateTexture2D(&desc, nullptr, &vram_texture));
|
|
1088
|
|
1089 D3D11_TEXTURE2D_DESC ram_desc;
|
|
1090 memcpy(&ram_desc, &desc, sizeof(ram_desc));
|
|
1091 ram_desc.Usage = D3D11_USAGE_STAGING;
|
|
1092 ram_desc.BindFlags = 0;
|
|
1093 ram_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
|
|
1094 ram_desc.MiscFlags = 0;
|
|
1095
|
|
1096 ID3D11Texture2D *ram_texture = nullptr;
|
|
1097 ErrorD3D(d3dd->CreateTexture2D(&ram_desc, nullptr, &ram_texture));
|
|
1098
|
|
1099 D3D11_MAPPED_SUBRESOURCE map;
|
|
1100 ErrorD3D(d3dc->Map(ram_texture, 0, D3D11_MAP_WRITE, 0, &map));
|
|
1101 for (unsigned int y = 0; y < desc.Height; ++y)
|
|
1102 for (unsigned int x = 0; x < desc.Width; ++x)
|
|
1103 {
|
|
1104 auto src = pHWLTexture->pPixels + y * desc.Width + x;
|
|
1105 auto dst = (unsigned int *)((char *)map.pData + y * map.RowPitch) + x;
|
|
1106
|
|
1107 extern unsigned __int32 Color32_SwapRedBlue(unsigned __int16 color16);
|
|
1108 *dst = 0xFF000000 | Color32_SwapRedBlue(*src);
|
|
1109 }
|
|
1110
|
|
1111 d3dc->Unmap(ram_texture, 0);
|
|
1112 d3dc->CopyResource(vram_texture, ram_texture);
|
|
1113 ram_texture->Release();
|
|
1114
|
|
1115 ID3D11ShaderResourceView *srv = nullptr;
|
|
1116 ErrorD3D(d3dd->CreateShaderResourceView(vram_texture, nullptr, &srv));
|
|
1117 vram_texture->Release();
|
|
1118
|
|
1119 d3dc->GenerateMips(srv);
|
|
1120 *pOutTexture = (IDirect3DTexture2 *)srv;
|
|
1121 *pOutSurface = nullptr;
|
|
1122 }
|
|
1123
|
|
1124 delete [] pHWLTexture->pPixels;
|
|
1125 delete pHWLTexture;
|
|
1126 return true;
|
2525
|
1127 }
|
|
1128
|