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