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