Advertisement
xiahanlu

t1t1

Sep 13th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.58 KB | None | 0 0
  1. /*------------------------------------------------------------------------
  2. HELLOWIN.C -- Displays "Hello, Windows 98!" in client area
  3. (c) Charles Petzold, 1998
  4. -----------------------------------------------------------------------*/
  5.  
  6. # include <windows.h>
  7. # include <d3d9.h>
  8. # include <d3dx9.h>
  9. # include <assert.h>
  10. # include <stdio.h>
  11. # include <stdint.h>
  12.  
  13. static IDirect3D9 *sm_Direct3D9_root=0;
  14. static IDirect3DDevice9 *sm_Direct3D9_device = NULL; /* device object */
  15. static IDirect3DSurface9 *sm_Direct3D9_surface = NULL; /* vram object */
  16. static IDirect3DTexture9 *sm_Direct3D9_texture = NULL;
  17. static int img_h = 0;
  18. static int img_w = 0;
  19.  
  20. extern "C"
  21. void __cdecl bilinear_x (DWORD *dstPtr, intptr_t dstPitch,
  22. DWORD *srcPtr, intptr_t srcPitch,
  23. intptr_t dstW, intptr_t dstH,
  24. intptr_t srcW, intptr_t srcH) ;
  25.  
  26.  
  27.  
  28. # define FILENAME "C:\\Users\\nozaki\\Desktop\\main.bmp"
  29. # define MOD_SHIFT_ 11
  30. # define MOD_MAPPER_ ( (1 << MOD_SHIFT_))/* MOD_MAPPER is 2^n*/
  31. # define MOD_SHIFT_MUL_ (MOD_MAPPER_*MOD_MAPPER_)
  32.  
  33. DWORD AlphaRGB32_S (DWORD RGB32, DWORD max, DWORD cur) {
  34. DWORD D0 = RGB32 & 0xFF;
  35. DWORD D1 = RGB32 & 0xFF00;
  36. DWORD D2 = RGB32 & 0xFF0000;
  37. D0 >>= 0;
  38. D1 >>= 8;
  39. D2 >>= 16;
  40. D0 *= cur;
  41. D1 *= cur;
  42. D2 *= cur;
  43. D0 /= max;
  44. D1 /= max;
  45. D2 /= max;
  46. D0 &= 0xFF;
  47. D1 &= 0xFF;
  48. D2 &= 0xFF;
  49. D0 <<= 0;
  50. D1 <<= 8;
  51. D2 <<= 16;
  52. return (D0 | D1 | D2);
  53. }
  54.  
  55. DWORD SuratADD (DWORD U, DWORD V) {
  56. DWORD U0 = U & 0xFF;
  57. DWORD U1 = U & 0xFF00;
  58. DWORD U2 = U & 0xFF0000;
  59. DWORD V0 = V & 0xFF;
  60. DWORD V1 = V & 0xFF00;
  61. DWORD V2 = V & 0xFF0000;
  62. U0 += V0;
  63. U1 += V1;
  64. U2 += V2;
  65. if (U0 > 0xFF) U0 = 0xFF;
  66. if (U1 > 0xFF00) U1 = 0xFF00;
  67. if (U2 > 0xFF0000) U2 = 0xFF0000;
  68. return (U0 | U1 | U2);
  69. }
  70.  
  71. # if 1
  72. void Bilinear (DWORD *pDstVPTR, DWORD dwDstPitch,
  73. DWORD *pSrcVPTR, DWORD dwSrcPitch,
  74. DWORD dwDstWidth, DWORD dwDstHeight,
  75. DWORD dwSrcWidth, DWORD dwSrcHeight)
  76. {
  77. DWORD dwMapX = (DWORD) ( ((float)MOD_MAPPER_) * (( (float) dwSrcWidth) / ( (float) dwDstWidth) )); // 2->3
  78. DWORD dwMapY = (DWORD) ( ((float)MOD_MAPPER_) * (( (float) dwSrcHeight) / ( (float) dwDstHeight))); // 2->3
  79. DWORD dwPosInSrcY = 0;
  80. DWORD dwPosInSrcX = 0;
  81. DWORD dwRTInSrcY;
  82. DWORD dwRTInSrcX;
  83. DWORD dwsPit = dwSrcPitch >> 2;
  84. DWORD dwdPit = dwDstPitch >> 2;
  85. DWORD dwMapTotal_X = 0;
  86. DWORD dwMapTotal_Y = 0;
  87. DWORD dwRVADst = dwDstPitch - dwDstWidth *4;
  88. DWORD dwCahceScanLine_Y = 0;
  89. DWORD dwCahceScanLine_Y2 = 0;
  90. for (DWORD dwScanY = 0; dwScanY != dwDstHeight; dwScanY++) {
  91. dwPosInSrcY = dwMapTotal_Y >> MOD_SHIFT_;
  92. dwRTInSrcY = dwMapTotal_Y & (MOD_MAPPER_ - 1);
  93. dwMapTotal_X = 0;
  94. dwCahceScanLine_Y = dwsPit*dwPosInSrcY;
  95. dwCahceScanLine_Y2 = dwdPit*dwScanY;
  96. for (DWORD dwScanX = 0; dwScanX != dwDstWidth; dwScanX++) {
  97. // fetch top-left, top-right, botton-left, bottom-right
  98. // source sample .
  99. dwPosInSrcX = dwMapTotal_X >> MOD_SHIFT_;
  100. dwRTInSrcX = dwMapTotal_X & (MOD_MAPPER_ - 1);
  101.  
  102. const DWORD temp = dwCahceScanLine_Y+dwPosInSrcX;
  103.  
  104. DWORD cpTopLeft = pSrcVPTR[temp];
  105. DWORD cpTopRight = pSrcVPTR[temp+1];
  106. DWORD cpBottomLeft = pSrcVPTR[temp+dwsPit];
  107. DWORD cpBottomRight = pSrcVPTR[temp+dwsPit+1];
  108.  
  109. // Get X, Y ratio .
  110. DWORD apLeft = dwRTInSrcX;
  111. DWORD apRight = MOD_MAPPER_ - apLeft;
  112. DWORD apTop = dwRTInSrcY;
  113. DWORD apBottom = MOD_MAPPER_ - apTop;
  114. // Get Weighted ratio
  115. DWORD wtTopLeft = apRight * apBottom;
  116. DWORD wtTopRight = apLeft * apBottom;
  117. DWORD wtBottomLeft = apRight * apTop;
  118. DWORD wtBottomRight = apLeft * apTop;
  119. // Get RGB percent. Write Into Buffer.
  120. DWORD dbTopLeft = AlphaRGB32_S (cpTopLeft, MOD_MAPPER_, apRight);
  121. DWORD dbTopRight = AlphaRGB32_S (cpTopRight, MOD_MAPPER_, dwRTInSrcX);
  122. DWORD dbBottomLeft = AlphaRGB32_S (cpBottomLeft, MOD_SHIFT_MUL_, wtBottomLeft);
  123. DWORD dbBottomRight = AlphaRGB32_S (cpBottomRight, MOD_SHIFT_MUL_, wtBottomRight);
  124. DWORD dbTopMix = SuratADD ( dbTopLeft, dbTopRight);
  125. DWORD dbBottomMix = SuratADD ( dbBottomLeft, dbBottomRight);
  126. DWORD dbOutputMix = SuratADD ( dbTopMix, dbBottomMix);
  127.  
  128. pDstVPTR[dwCahceScanLine_Y2+dwScanX] = dbTopMix;
  129. dwMapTotal_X += dwMapX;
  130. }
  131. dwMapTotal_Y += dwMapY;
  132. }
  133.  
  134. }
  135. # else
  136.  
  137.  
  138. void zoom_sc (
  139. /* des pointer */ DWORD *d,
  140. /* src pointer */ DWORD *s,
  141. /* des pitch */ DWORD dp,
  142. /* src pitch */ DWORD sp,
  143. /* width */ DWORD w,
  144. /* height */ DWORD h,
  145. /* zoom count */ float cx, float cy)
  146. {
  147. #define GET_B(x)((float)(((x) & 0x000000FF) >> 0))
  148. #define GET_G(x)((float)(((x) & 0x0000FF00) >> 8))
  149. #define GET_R(x)((float)(((x) & 0x00FF0000) >> 16))
  150.  
  151. DWORD X;
  152. DWORD Y;
  153. DWORD NewWidth = (DWORD)(cx * (float)w);
  154. DWORD NewHeight = (DWORD)(cy * (float)h);
  155. DWORD DesPitch;
  156. DWORD SrcPitch;
  157. DWORD BPosTX;
  158. DWORD BPosTY;
  159. DWORD TLeft;
  160. DWORD TRight;
  161. DWORD BLeft;
  162. DWORD BRight;
  163. DWORD R;
  164. DWORD G;
  165. DWORD B;
  166. float RevseRatioX;
  167. float RevseRatioY;
  168. float XOffsetRatio;
  169. float YOffsetRatio;
  170. float r0;
  171. float r1;
  172. float r2;
  173. float r3;
  174. if (!NewWidth || !NewHeight)
  175. return;
  176.  
  177. RevseRatioX = 1.0/cx;
  178. RevseRatioY = 1.0/cy;
  179.  
  180. SrcPitch = sp >> 2;
  181. DesPitch = dp >> 2;
  182.  
  183. for (Y = 0; Y != NewHeight; Y ++) {
  184. YOffsetRatio = (RevseRatioY * (float)Y);
  185. BPosTY = (DWORD)YOffsetRatio;
  186. YOffsetRatio-=(float)BPosTY;
  187. for (X = 0; X != NewWidth; X ++) {
  188. XOffsetRatio = RevseRatioX * (float)X;
  189. BPosTX = (DWORD)XOffsetRatio;
  190. XOffsetRatio-=(float)BPosTX;
  191.  
  192. TLeft = s[BPosTX + BPosTY * SrcPitch + 0];
  193. TRight= s[BPosTX + BPosTY * SrcPitch + 1];
  194. BLeft = s[BPosTX + BPosTY * SrcPitch + SrcPitch + 0];
  195. BRight= s[BPosTX + BPosTY * SrcPitch + SrcPitch + 1];
  196.  
  197. r3 = XOffsetRatio * YOffsetRatio;
  198. r2 = (1.0 - XOffsetRatio) * YOffsetRatio;
  199. r1 = (1.0 - YOffsetRatio) * XOffsetRatio;
  200. r0 = (1.0 - XOffsetRatio) * (1.0 - YOffsetRatio);
  201.  
  202. B = (DWORD)(((GET_B(TLeft) * r0)) + ((GET_B(TRight) * r1)) + ((GET_B(BLeft) * r2)) + ((GET_B(BRight) * r3))) & 255;
  203. G = (DWORD)(((GET_G(TLeft) * r0)) + ((GET_G(TRight) * r1)) + ((GET_G(BLeft) * r2)) + ((GET_G(BRight) * r3))) & 255;
  204. R = (DWORD)(((GET_R(TLeft) * r0)) + ((GET_R(TRight) * r1)) + ((GET_R(BLeft) * r2)) + ((GET_R(BRight) * r3))) & 255;
  205.  
  206. d[X + Y * DesPitch] = ((R << 16) | (G << 8) | B);
  207. }
  208.  
  209. }
  210. }
  211.  
  212. void Bilinear (DWORD *pDstVPTR, DWORD dwDstPitch,
  213. DWORD *pSrcVPTR, DWORD dwSrcPitch,
  214. DWORD dwDstWidth, DWORD dwDstHeight,
  215. DWORD dwSrcWidth, DWORD dwSrcHeight)
  216. {
  217. zoom_sc (
  218. /* des pointer */ pDstVPTR,
  219. /* src pointer */ pSrcVPTR,
  220. /* des pitch */ dwDstPitch,
  221. /* src pitch */ dwSrcPitch,
  222. /* width */ dwSrcWidth,
  223. /* height */ dwSrcHeight,
  224. /* zoom count */ (float) dwDstWidth / (float) dwSrcWidth,
  225. (float) dwDstHeight / (float) dwSrcHeight
  226.  
  227.  
  228.  
  229. );
  230.  
  231.  
  232.  
  233. }
  234.  
  235.  
  236. # endif
  237.  
  238. void LoadImage (void) {
  239. D3DXIMAGE_INFO t;
  240. D3DXCreateTextureFromFileExA (sm_Direct3D9_device, FILENAME,
  241. D3DX_DEFAULT_NONPOW2,
  242. D3DX_DEFAULT_NONPOW2,
  243. D3DX_FROM_FILE,
  244. 0,
  245. D3DFMT_FROM_FILE,
  246. D3DPOOL_MANAGED,
  247. D3DX_DEFAULT, // D3DX_DEFAULT
  248. D3DX_DEFAULT, // D3DX_DEFAULT
  249. 0, //D3DCOLOR_XRGB (0, 0, 0),
  250. & t, NULL, & sm_Direct3D9_texture);
  251. img_w = t.Width;
  252. img_h = t.Height;
  253. }
  254.  
  255. void ResetD3dpp (D3DPRESENT_PARAMETERS &d3dpp, HWND window, int width, int height) {
  256.  
  257. ZeroMemory (& d3dpp, sizeof (d3dpp));
  258.  
  259. // d3dpp.BackBufferFormat = D3DFMT_X1R5G5B5;
  260. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  261. d3dpp.Flags = 0;
  262. d3dpp.hDeviceWindow = window;
  263. d3dpp.Windowed = TRUE;
  264. d3dpp.BackBufferWidth = GetSystemMetrics (SM_CXSCREEN);
  265. d3dpp.BackBufferHeight = GetSystemMetrics (SM_CYSCREEN);
  266. d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; /* Disable VSync */
  267. }
  268.  
  269. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  270.  
  271. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  272. PSTR szCmdLine, int iCmdShow)
  273. {
  274. static TCHAR szAppName[] = TEXT ("HelloWin") ;
  275. HWND hwnd ;
  276. MSG msg ;
  277. WNDCLASS wndclass ;
  278.  
  279. wndclass.style = CS_HREDRAW | CS_VREDRAW ;
  280. wndclass.lpfnWndProc = WndProc ;
  281. wndclass.cbClsExtra = 0 ;
  282. wndclass.cbWndExtra = 0 ;
  283. wndclass.hInstance = hInstance ;
  284. wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
  285. wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
  286. wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  287. wndclass.lpszMenuName = NULL ;
  288. wndclass.lpszClassName = szAppName ;
  289.  
  290. if (!RegisterClass (&wndclass)) {
  291. MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
  292. szAppName, MB_ICONERROR) ;
  293. return 0 ;
  294. }
  295.  
  296. hwnd = CreateWindow( szAppName, // window class name
  297. TEXT ("The Hello Program"), // window caption
  298. WS_OVERLAPPEDWINDOW, // window style
  299. CW_USEDEFAULT, // initial x position
  300. CW_USEDEFAULT, // initial y position
  301. CW_USEDEFAULT, // initial x size
  302. CW_USEDEFAULT, // initial y size
  303. NULL, // parent window handle
  304. NULL, // window menu handle
  305. hInstance, // program instance handle
  306. NULL) ; // creation parameters
  307.  
  308. // create direct3d 9
  309. sm_Direct3D9_root = Direct3DCreate9 (DIRECT3D_VERSION);
  310. assert (sm_Direct3D9_root != NULL);
  311.  
  312. // create device
  313. D3DPRESENT_PARAMETERS d3dpp;
  314. ResetD3dpp (d3dpp, hwnd, 0, 0);
  315.  
  316. HRESULT si = sm_Direct3D9_root->CreateDevice (0,
  317. D3DDEVTYPE_HAL,
  318. hwnd,
  319. D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, & sm_Direct3D9_device);
  320. assert (SUCCEEDED (si));
  321.  
  322. sm_Direct3D9_device->CreateOffscreenPlainSurface (GetSystemMetrics (SM_CXSCREEN),
  323. GetSystemMetrics (SM_CYSCREEN), D3DFMT_X8R8G8B8, D3DPOOL_SYSTEMMEM,
  324. & sm_Direct3D9_surface, NULL);
  325.  
  326. LoadImage ();
  327.  
  328. ShowWindow (hwnd, iCmdShow) ;
  329. UpdateWindow (hwnd) ;
  330.  
  331. D3DLOCKED_RECT lock_rect2;
  332. sm_Direct3D9_texture->LockRect (0, & lock_rect2, NULL, 0);
  333. while (TRUE)
  334. {
  335. static DWORD t = 0;
  336. static DWORD ticks = 0;
  337.  
  338. if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
  339. {
  340. if (msg.message == WM_QUIT)
  341. break ;
  342. TranslateMessage (&msg) ;
  343. DispatchMessage (&msg) ;
  344. }
  345. else
  346. {
  347. DWORD n = timeGetTime ();
  348. RECT rcClient;
  349. D3DLOCKED_RECT lock_rect;
  350.  
  351. IDirect3DSurface9 *surface_temp;
  352. GetClientRect (hwnd, & rcClient);
  353. int w = abs (rcClient.left - rcClient.right);
  354. int h = abs (rcClient.bottom - rcClient.top);
  355. if (w <= 0 || h <= 0)
  356. return 0;
  357.  
  358. si = sm_Direct3D9_device->GetBackBuffer (0, 0, D3DBACKBUFFER_TYPE_MONO, & surface_temp);
  359. assert (SUCCEEDED (si));
  360. si = sm_Direct3D9_surface->LockRect (& lock_rect, & rcClient, D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE);
  361. assert (SUCCEEDED (si));
  362.  
  363. // bilinear entryu .
  364. bilinear_x ((DWORD *)lock_rect.pBits, lock_rect.Pitch,
  365. (DWORD *)lock_rect2.pBits, lock_rect2.Pitch,
  366. rcClient.right - rcClient.left,
  367. rcClient.bottom - rcClient.top, img_w, img_h);
  368.  
  369. sm_Direct3D9_surface->UnlockRect ();
  370. sm_Direct3D9_device->UpdateSurface (sm_Direct3D9_surface, & rcClient, surface_temp, NULL);
  371. sm_Direct3D9_device->Present (& rcClient, NULL, NULL, NULL);
  372. printf ("Present::frame:%d!!!!\n", timeGetTime () - n);
  373. }
  374. }
  375. sm_Direct3D9_texture->UnlockRect (0);
  376. sm_Direct3D9_device->Release ();
  377. sm_Direct3D9_root->Release ();
  378. return msg.wParam ;
  379. }
  380.  
  381. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  382. {
  383. HDC hdc ;
  384. PAINTSTRUCT ps ;
  385.  
  386. switch (message)
  387. {
  388. case WM_PAINT:
  389. hdc = BeginPaint (hwnd, &ps) ;
  390. EndPaint (hwnd, &ps) ;
  391. return 0 ;
  392.  
  393. case WM_DESTROY:
  394. PostQuitMessage (0) ;
  395. return 0 ;
  396. }
  397. return DefWindowProc (hwnd, message, wParam, lParam) ;
  398. }
  399. #include <smmintrin.h>
  400. int main (void) {
  401.  
  402. _CRT_ALIGN(16) BYTE s[128];
  403.  
  404. //__asm pshufw mm2, [s+1], 3
  405. WinMain (GetModuleHandle (NULL), NULL, NULL, SW_SHOWNORMAL);
  406. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement