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