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