d7samurai

minimal d3d 11.0 framework

Jun 13th, 2016
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.66 KB | None | 0 0
  1. // minimal d3d 11.0 framework
  2. // for software rendered framebuffer
  3. // d7samurai 2016
  4.  
  5. #pragma comment(lib, "dxgi")
  6. #pragma comment(lib, "d3d11")
  7.  
  8. #include <windows.h>
  9. #include <d3d11.h>
  10.  
  11. #define                                             FRAMEBUFFER_WIDTH 32
  12. #define                                             FRAMEBUFFER_HEIGHT 8
  13.  
  14. HWND                                                Window;
  15.  
  16. IDXGISwapChain*                                     SwapChain;
  17.  
  18. ID3D11Device*                                       Device;
  19. ID3D11DeviceContext*                                DeviceContext;
  20. ID3D11RenderTargetView*                             RenderTargetView;
  21. ID3D11VertexShader*                                 VertexShader;
  22. ID3D11PixelShader*                                  PixelShader;
  23. ID3D11RasterizerState*                              RasterizerState;
  24. ID3D11SamplerState*                                 SamplerState;
  25. ID3D11Texture2D*                                    FrameBuffer;
  26. ID3D11ShaderResourceView*                           FrameBufferView;
  27.  
  28. unsigned char VertexShader_data[] =
  29. {
  30.     0x44, 0x58, 0x42, 0x43, 0xe9, 0xbb, 0x34, 0xe5, 0xd7, 0x73, 0x53, 0xa4,
  31.     0x62, 0x83, 0xa0, 0xd3, 0xcf, 0xef, 0x25, 0x60, 0x01, 0x00, 0x00, 0x00,
  32.     0x88, 0x03, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00,
  33.     0x8c, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x18, 0x01, 0x00, 0x00,
  34.     0x0c, 0x03, 0x00, 0x00, 0x52, 0x44, 0x45, 0x46, 0x50, 0x00, 0x00, 0x00,
  35.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  36.     0x1c, 0x00, 0x00, 0x00, 0x00, 0x04, 0xfe, 0xff, 0x00, 0x01, 0x00, 0x00,
  37.     0x1c, 0x00, 0x00, 0x00, 0x4d, 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66,
  38.     0x74, 0x20, 0x28, 0x52, 0x29, 0x20, 0x48, 0x4c, 0x53, 0x4c, 0x20, 0x53,
  39.     0x68, 0x61, 0x64, 0x65, 0x72, 0x20, 0x43, 0x6f, 0x6d, 0x70, 0x69, 0x6c,
  40.     0x65, 0x72, 0x20, 0x36, 0x2e, 0x33, 0x2e, 0x39, 0x36, 0x30, 0x30, 0x2e,
  41.     0x31, 0x36, 0x33, 0x38, 0x34, 0x00, 0xab, 0xab, 0x49, 0x53, 0x47, 0x4e,
  42.     0x2c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  43.     0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
  44.     0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00,
  45.     0x53, 0x56, 0x5f, 0x56, 0x45, 0x52, 0x54, 0x45, 0x58, 0x49, 0x44, 0x00,
  46.     0x4f, 0x53, 0x47, 0x4e, 0x50, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  47.     0x08, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48.     0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49.     0x0f, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  50.     0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  51.     0x03, 0x0c, 0x00, 0x00, 0x53, 0x56, 0x5f, 0x50, 0x4f, 0x53, 0x49, 0x54,
  52.     0x49, 0x4f, 0x4e, 0x00, 0x54, 0x45, 0x58, 0x43, 0x4f, 0x4f, 0x52, 0x44,
  53.     0x00, 0xab, 0xab, 0xab, 0x53, 0x48, 0x44, 0x52, 0xec, 0x01, 0x00, 0x00,
  54.     0x40, 0x00, 0x01, 0x00, 0x7b, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x04,
  55.     0x12, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
  56.     0x67, 0x00, 0x00, 0x04, 0xf2, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
  57.     0x01, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x03, 0x32, 0x20, 0x10, 0x00,
  58.     0x01, 0x00, 0x00, 0x00, 0x68, 0x00, 0x00, 0x02, 0x01, 0x00, 0x00, 0x00,
  59.     0x69, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
  60.     0x04, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
  61.     0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x09,
  62.     0x32, 0x30, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  63.     0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x80, 0xbf, 0x00, 0x00, 0x80, 0xbf,
  64.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x09,
  65.     0x32, 0x30, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  66.     0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x80, 0xbf, 0x00, 0x00, 0x80, 0x3f,
  67.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x09,
  68.     0x32, 0x30, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  69.     0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x80, 0xbf,
  70.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x09,
  71.     0x32, 0x30, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  72.     0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x80, 0x3f,
  73.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x09,
  74.     0x32, 0x30, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  75.     0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f,
  76.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x09,
  77.     0x32, 0x30, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  78.     0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  79.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x09,
  80.     0x32, 0x30, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  81.     0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x80, 0x3f,
  82.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x09,
  83.     0x32, 0x30, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  84.     0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x00, 0x00, 0x00, 0x00,
  85.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x05,
  86.     0x12, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x10, 0x10, 0x00,
  87.     0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x07, 0x32, 0x20, 0x10, 0x00,
  88.     0x00, 0x00, 0x00, 0x00, 0x46, 0x30, 0x20, 0x04, 0x00, 0x00, 0x00, 0x00,
  89.     0x0a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x07,
  90.     0x32, 0x20, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x46, 0x30, 0x20, 0x04,
  91.     0x01, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
  92.     0x36, 0x00, 0x00, 0x08, 0xc2, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
  93.     0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  94.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x3f, 0x3e, 0x00, 0x00, 0x01,
  95.     0x53, 0x54, 0x41, 0x54, 0x74, 0x00, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x00,
  96.     0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  97.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  98.     0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  99.     0x08, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  100.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  101.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  102.     0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  103.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  104.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  105.     0x00, 0x00, 0x00, 0x00
  106. };
  107.  
  108. unsigned char PixelShader_data[] =
  109. {
  110.     0x44, 0x58, 0x42, 0x43, 0x2c, 0xa2, 0xbe, 0xee, 0x11, 0x9c, 0xfd, 0x0e,
  111.     0x0e, 0x37, 0x37, 0xd2, 0x91, 0x80, 0xc6, 0x41, 0x01, 0x00, 0x00, 0x00,
  112.     0x50, 0x02, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00,
  113.     0xdc, 0x00, 0x00, 0x00, 0x34, 0x01, 0x00, 0x00, 0x68, 0x01, 0x00, 0x00,
  114.     0xd4, 0x01, 0x00, 0x00, 0x52, 0x44, 0x45, 0x46, 0xa0, 0x00, 0x00, 0x00,
  115.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  116.     0x1c, 0x00, 0x00, 0x00, 0x00, 0x04, 0xff, 0xff, 0x00, 0x01, 0x00, 0x00,
  117.     0x6c, 0x00, 0x00, 0x00, 0x5c, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  118.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  119.     0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  120.     0x62, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
  121.     0x04, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
  122.     0x01, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x73, 0x74, 0x61, 0x74,
  123.     0x65, 0x00, 0x74, 0x65, 0x78, 0x74, 0x75, 0x72, 0x65, 0x32, 0x64, 0x00,
  124.     0x4d, 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66, 0x74, 0x20, 0x28, 0x52,
  125.     0x29, 0x20, 0x48, 0x4c, 0x53, 0x4c, 0x20, 0x53, 0x68, 0x61, 0x64, 0x65,
  126.     0x72, 0x20, 0x43, 0x6f, 0x6d, 0x70, 0x69, 0x6c, 0x65, 0x72, 0x20, 0x36,
  127.     0x2e, 0x33, 0x2e, 0x39, 0x36, 0x30, 0x30, 0x2e, 0x31, 0x36, 0x33, 0x38,
  128.     0x34, 0x00, 0xab, 0xab, 0x49, 0x53, 0x47, 0x4e, 0x50, 0x00, 0x00, 0x00,
  129.     0x02, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00,
  130.     0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  131.     0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00,
  132.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
  133.     0x01, 0x00, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00, 0x53, 0x56, 0x5f, 0x50,
  134.     0x4f, 0x53, 0x49, 0x54, 0x49, 0x4f, 0x4e, 0x00, 0x54, 0x45, 0x58, 0x43,
  135.     0x4f, 0x4f, 0x52, 0x44, 0x00, 0xab, 0xab, 0xab, 0x4f, 0x53, 0x47, 0x4e,
  136.     0x2c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  137.     0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  138.     0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00,
  139.     0x53, 0x56, 0x5f, 0x54, 0x41, 0x52, 0x47, 0x45, 0x54, 0x00, 0xab, 0xab,
  140.     0x53, 0x48, 0x44, 0x52, 0x64, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  141.     0x19, 0x00, 0x00, 0x00, 0x5a, 0x00, 0x00, 0x03, 0x00, 0x60, 0x10, 0x00,
  142.     0x00, 0x00, 0x00, 0x00, 0x58, 0x18, 0x00, 0x04, 0x00, 0x70, 0x10, 0x00,
  143.     0x00, 0x00, 0x00, 0x00, 0x55, 0x55, 0x00, 0x00, 0x62, 0x10, 0x00, 0x03,
  144.     0x32, 0x10, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x65, 0x00, 0x00, 0x03,
  145.     0xf2, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0x00, 0x00, 0x09,
  146.     0xf2, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46, 0x10, 0x10, 0x00,
  147.     0x01, 0x00, 0x00, 0x00, 0x46, 0x7e, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
  148.     0x00, 0x60, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x01,
  149.     0x53, 0x54, 0x41, 0x54, 0x74, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  150.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  151.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  152.     0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  153.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  154.     0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  155.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  156.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  157.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  158.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  159.     0x00, 0x00, 0x00, 0x00
  160. };
  161.  
  162. unsigned int FrameBuffer_data[FRAMEBUFFER_WIDTH * FRAMEBUFFER_HEIGHT];
  163.  
  164. void InitializeD3D()
  165. {
  166.     D3D_FEATURE_LEVEL featureLevels[] =
  167.     {
  168.         D3D_FEATURE_LEVEL_11_0,
  169.         D3D_FEATURE_LEVEL_10_1,
  170.         D3D_FEATURE_LEVEL_10_0, };
  171.  
  172.     D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, featureLevels, ARRAYSIZE(featureLevels), D3D11_SDK_VERSION, &Device, nullptr, &DeviceContext);
  173.  
  174.     IDXGIFactory* dxgiFactory;
  175.     CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&dxgiFactory);
  176.  
  177.     DXGI_SWAP_CHAIN_DESC swpDesc;
  178.     swpDesc.BufferDesc.Width =                      0;
  179.     swpDesc.BufferDesc.Height =                     0;
  180.     swpDesc.BufferDesc.RefreshRate.Numerator =      0;
  181.     swpDesc.BufferDesc.RefreshRate.Denominator =    0;
  182.     swpDesc.BufferDesc.Format =                     DXGI_FORMAT_B8G8R8A8_UNORM;
  183.     swpDesc.BufferDesc.ScanlineOrdering =           DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  184.     swpDesc.BufferDesc.Scaling =                    DXGI_MODE_SCALING_UNSPECIFIED;
  185.     swpDesc.SampleDesc.Count =                      1;
  186.     swpDesc.SampleDesc.Quality =                    0;
  187.     swpDesc.BufferUsage =                           DXGI_USAGE_RENDER_TARGET_OUTPUT;
  188.     swpDesc.BufferCount =                           2;
  189.     swpDesc.OutputWindow =                          Window;
  190.     swpDesc.Windowed =                              TRUE;
  191.     swpDesc.SwapEffect =                            DXGI_SWAP_EFFECT_DISCARD;
  192.     swpDesc.Flags =                                 0;
  193.  
  194.     dxgiFactory->CreateSwapChain(Device, &swpDesc, &SwapChain);
  195.  
  196.     ID3D11Texture2D* backBuffer;
  197.     SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&backBuffer);
  198.  
  199.     Device->CreateRenderTargetView(backBuffer, nullptr, &RenderTargetView);
  200.  
  201.     Device->CreateVertexShader(VertexShader_data, sizeof(VertexShader_data), nullptr, &VertexShader);
  202.     Device->CreatePixelShader(PixelShader_data, sizeof(PixelShader_data), nullptr, &PixelShader);
  203.  
  204.     D3D11_RASTERIZER_DESC rstDesc;
  205.     rstDesc.FillMode =                              D3D11_FILL_SOLID;
  206.     rstDesc.CullMode =                              D3D11_CULL_NONE;
  207.     rstDesc.FrontCounterClockwise =                 FALSE;
  208.     rstDesc.DepthBias =                             0;
  209.     rstDesc.DepthBiasClamp =                        0.0f;
  210.     rstDesc.SlopeScaledDepthBias =                  0.0f;
  211.     rstDesc.DepthClipEnable =                       FALSE;
  212.     rstDesc.ScissorEnable =                         FALSE;
  213.     rstDesc.MultisampleEnable =                     FALSE;
  214.     rstDesc.AntialiasedLineEnable =                 FALSE;
  215.  
  216.     Device->CreateRasterizerState(&rstDesc, &RasterizerState);
  217.  
  218.     D3D11_SAMPLER_DESC smpDesc;
  219.     smpDesc.Filter =                                D3D11_FILTER_MIN_MAG_MIP_POINT;
  220.     smpDesc.AddressU =                              D3D11_TEXTURE_ADDRESS_WRAP;
  221.     smpDesc.AddressV =                              D3D11_TEXTURE_ADDRESS_WRAP;
  222.     smpDesc.AddressW =                              D3D11_TEXTURE_ADDRESS_WRAP;
  223.     smpDesc.MipLODBias =                            0.0f;
  224.     smpDesc.MaxAnisotropy =                         0;
  225.     smpDesc.ComparisonFunc =                        D3D11_COMPARISON_ALWAYS;
  226.     smpDesc.BorderColor[0] =                        0.0f;
  227.     smpDesc.BorderColor[1] =                        0.0f;
  228.     smpDesc.BorderColor[2] =                        0.0f;
  229.     smpDesc.BorderColor[3] =                        0.0f;
  230.     smpDesc.MinLOD =                                0.0f;
  231.     smpDesc.MaxLOD =                                D3D11_FLOAT32_MAX;
  232.  
  233.     Device->CreateSamplerState(&smpDesc, &SamplerState);
  234.  
  235.     D3D11_TEXTURE2D_DESC texDesc;
  236.     texDesc.Width =                                 FRAMEBUFFER_WIDTH;
  237.     texDesc.Height =                                FRAMEBUFFER_HEIGHT;
  238.     texDesc.MipLevels =                             1;
  239.     texDesc.ArraySize =                             1;
  240.     texDesc.Format =                                DXGI_FORMAT_B8G8R8A8_UNORM;
  241.     texDesc.SampleDesc.Count =                      1;
  242.     texDesc.SampleDesc.Quality =                    0;
  243.     texDesc.Usage =                                 D3D11_USAGE_DYNAMIC;
  244.     texDesc.BindFlags =                             D3D11_BIND_SHADER_RESOURCE;
  245.     texDesc.CPUAccessFlags =                        D3D11_CPU_ACCESS_WRITE;
  246.     texDesc.MiscFlags =                             0;
  247.  
  248.     Device->CreateTexture2D(&texDesc, nullptr, &FrameBuffer);
  249.     Device->CreateShaderResourceView(FrameBuffer, nullptr, &FrameBufferView);
  250.  
  251.     dxgiFactory->Release();
  252.     backBuffer->Release();
  253. }
  254.  
  255. void Render()
  256. {
  257.     D3D11_MAPPED_SUBRESOURCE frameBuffer;
  258.     DeviceContext->Map(FrameBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &frameBuffer);
  259.     {
  260.         unsigned char* src = reinterpret_cast<unsigned char*>(FrameBuffer_data);
  261.         unsigned char* dst = reinterpret_cast<unsigned char*>(frameBuffer.pData);
  262.  
  263.         for (int y = 0; y < FRAMEBUFFER_HEIGHT; ++y)
  264.         {
  265.             memcpy(dst, src, FRAMEBUFFER_WIDTH * 4);
  266.  
  267.             src += FRAMEBUFFER_WIDTH * 4;
  268.             dst += frameBuffer.RowPitch;
  269.         }
  270.     }
  271.     DeviceContext->Unmap(FrameBuffer, 0);
  272.  
  273.     FLOAT backgroundColor[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  274.     DeviceContext->ClearRenderTargetView(RenderTargetView, backgroundColor);
  275.  
  276.     DXGI_SWAP_CHAIN_DESC swpDesc;
  277.     SwapChain->GetDesc(&swpDesc);
  278.  
  279.     D3D11_VIEWPORT viewport;
  280.     viewport.TopLeftX =                             0.0f;
  281.     viewport.TopLeftY =                             0.0f;
  282.     viewport.Width =                                (float)swpDesc.BufferDesc.Width;
  283.     viewport.Height =                               (float)swpDesc.BufferDesc.Height;
  284.     viewport.MinDepth =                             0.0f;
  285.     viewport.MaxDepth =                             1.0f;
  286.  
  287.     DeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
  288.     DeviceContext->VSSetShader(VertexShader, nullptr, 0);
  289.     DeviceContext->RSSetViewports(1, &viewport);
  290.     DeviceContext->RSSetState(RasterizerState);
  291.     DeviceContext->PSSetShader(PixelShader, nullptr, 0);
  292.     DeviceContext->PSSetShaderResources(0, 1, &FrameBufferView);
  293.     DeviceContext->PSSetSamplers(0, 1, &SamplerState);
  294.     DeviceContext->OMSetRenderTargets(1, &RenderTargetView, nullptr);
  295.  
  296.     DeviceContext->Draw(4, 0);
  297.  
  298.     HRESULT hr = SwapChain->Present(1, 0);
  299.  
  300.     if (hr != S_OK && hr != DXGI_STATUS_OCCLUDED)
  301.     {
  302.         SwapChain->Release();
  303.  
  304.         Device->Release();
  305.         DeviceContext->Release();
  306.         RenderTargetView->Release();
  307.         VertexShader->Release();
  308.         PixelShader->Release();
  309.         RasterizerState->Release();
  310.         SamplerState->Release();
  311.         FrameBuffer->Release();
  312.         FrameBufferView->Release();
  313.  
  314.         InitializeD3D();
  315.     }
  316. }
  317.  
  318. void Update() // example code that draws a 'd7' logo to the framebuffer
  319. {
  320.     static int index = 0;
  321.     static int d7[] = { 0x0000037e, 0x03063f0c, 0x63186330, 0x3f600000 };
  322.  
  323.     for (int data = 0; data < 4; ++data)
  324.     {
  325.         for (int bit = 31; bit >= 0; --bit)
  326.         {
  327.             int line = index + (data << 1) + ((31 - bit) >> 4);
  328.  
  329.             for (int pixel = 0; pixel < 2; ++pixel)
  330.             {
  331.                 FrameBuffer_data[(data << 6) + ((31 - bit) << 1) + pixel] = (d7[data] >> bit) & 1 ? 0xffffffff : 0xff000000 + (0x00100000 * (line & 0xf));
  332.             }
  333.         }
  334.     }
  335.  
  336.     ++index;
  337. }
  338.  
  339. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  340. {
  341.     switch (message)
  342.     {
  343.     case WM_SIZE:
  344.     {
  345.         UINT width = (UINT)(lParam & 0xffff);
  346.         UINT height = (UINT)(lParam >> 16);
  347.  
  348.         RenderTargetView->Release();
  349.  
  350.         SwapChain->ResizeBuffers(0, width, height, DXGI_FORMAT_UNKNOWN, 0);
  351.  
  352.         ID3D11Texture2D* backBuffer;
  353.         SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&backBuffer);
  354.  
  355.         Device->CreateRenderTargetView(backBuffer, nullptr, &RenderTargetView);
  356.  
  357.         backBuffer->Release();
  358.     }
  359.     return 0;
  360.  
  361.     case WM_DESTROY:
  362.     {
  363.         PostQuitMessage(0);
  364.     }
  365.     return 0;
  366.     }
  367.  
  368.     return DefWindowProc(hWnd, message, wParam, lParam);
  369. }
  370.  
  371. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
  372. {
  373.     char* title = "Minimal D3D11 by d7samurai";
  374.  
  375.     WNDCLASSEX wndClassEx = { sizeof(wndClassEx) };
  376.     wndClassEx.lpfnWndProc = WndProc;
  377.     wndClassEx.hCursor = LoadCursor(nullptr, IDC_ARROW);
  378.     wndClassEx.lpszClassName = title;
  379.  
  380.     RegisterClassEx(&wndClassEx);
  381.  
  382.     Window = CreateWindowEx(0, title, title, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, hInstance, nullptr);
  383.  
  384.     InitializeD3D();
  385.  
  386.     ShowWindow(Window, SW_SHOWNORMAL);
  387.  
  388.     bool run = true;
  389.  
  390.     while (run)
  391.     {
  392.         MSG msg;
  393.  
  394.         while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE) > 0)
  395.         {
  396.             if (msg.message != WM_QUIT)
  397.             {
  398.                 TranslateMessage(&msg);
  399.                 DispatchMessage(&msg);
  400.             }
  401.             else
  402.             {
  403.                 run = false;
  404.             }
  405.         }
  406.  
  407.         Update();
  408.         Render();
  409.     }
  410.  
  411.     return 0;
  412. }
Add Comment
Please, Sign In to add comment